Commit Graph
648 Commits
Author SHA1 Message Date
Jon Shallow 0de22aae0b Trust Certs: Load in the PKI trust store for peer cert checking
Load in the PKI trust store with a new function
  coap_context_load_pki_trust_store()

By default, make the client and server applications do this unless
explicitely disabled.

This enables clients doing (D)TLS to not need to be provided with specific
certificates but can validate trust in the server provided certificates.

Note: A default trust store is not supported across all platform types or
some TLS libraries (version dependent).
2025-03-21 09:18:46 +00:00
Jon Shallow cce7b65398 Threads: Support multiple threads doing coap_io_process()
Set up a coap_io_process() loop spread across multiple threads, each thread
repeatably calling coap_io_process() and so able to handle any input packets.

Note that the application handlers need to be thread-safe for application
level activity.

Add 2 new main functions
  coap_io_process_loop()
  coap_io_process_terminate_loop()

Add in 2 new sub functions
  coap_io_process_configure_threads()
  coap_io_process_remove_threads()
to set up the parallel threads.

Update example servers to include using coap_io_process_loop().

Support optional thread number logging by configuration to maintain
backwards logging format output if required.
2025-03-19 09:18:35 +00:00
Jon Shallow 0ba446d788 App Data: Provide new set of storing app data in various structures
New functions
 coap_async_set_app_data2()
 coap_cache_set_app_data2()
 coap_context_set_app_data2()
 coap_session_set_app_data2()
that, multi-thread safe, store an application data pointer and return the
previous data pointer (if any). There is an optional callback that will
be called when the respective storage object is released that can free
off as appropriate the data that was stored.

These replace the existing coap_*_set_app_data() functions and
coap_*_get_app_data() can be used to get the stored data pointer.
2025-03-18 16:57:52 +00:00
Peter Harper b9526c7ae0 coap_threadsafe_internal.h: Fix coap_lock_invert.
This code fails to compile if COAP_THREAD_SAFE is false as the macro
parameter is "alt_lock" not "func".
2025-02-21 08:50:42 +00:00
Jon Shallow c4d933a969 Proxy: Update documentation, streamline the code
Tidy up naming to reduce confusion between supported proxy types.

Provides simpler proxy examples.

Fixes issue with COAP_PROXY_REVERSE_STRIP.

Support upstream server using AF_UNIX.
2025-02-20 10:12:05 +00:00
Jon Shallow 4289cb072d Copyright: Update to 2025 where appropriate 2025-02-17 09:37:55 +00:00
Jon Shallow 18274073da Proxy Support: Update documentation 2025-02-13 19:36:53 +00:00
Jon Shallow 60fbbd3e3b KeepAlive: Better session cleanup after KeepAlive failure 2025-02-13 12:53:32 +00:00
kkrentz cea3dd449b Contiki-NG: Replace u_char with uint8_t to fix compilation errors 2025-02-13 10:23:42 +00:00
Jon Shallow 0b41ee0511 cache_response: Cache the latest response to a CON request
If the ACK (not empty) response gets lost, the repeated CON request will just
get the cached response without invoking further analysis.

This stops things like OSCORE reporting retry issues.

This functionality needs to be enabled by setting the COAP_BLOCK_CACHE_RESPONSE
bit when calling coap_context_set_block_mode().  If not enabled, then, for OSCORE
the response is sent as a separate response (empty ACK followed by CON response),
which is how code previous to this PR works.
2025-02-03 08:51:31 +00:00
Jon Shallow 38c8ba5790 coap_send_internal: Update parameters
Preparatory work for supporting caching responses by updating
coap_send_internal() parameters.
2025-01-29 09:50:12 +00:00
Jon Shallow ba09d0f643 coap_delete_pdu: Add in multi-threaded lock protection
The reference counter needs to be protected.
2025-01-28 15:23:19 +00:00
Jon Shallow 109842b833 Proxy: Fix memory leaks in proxy logic 2025-01-14 15:58:15 +00:00
Jon Shallow 4d84859620 keepalive: Add in server keepalive support
If coap_context_set_keepalive() is set in the server, CoAP keepalive packets
will get sent for any CoAP session that is curently observing a resource.

If the keepalive times out, then the observation(s) are canceled and the
server session is deleted.
2025-01-10 10:44:20 +00:00
Jon Shallow 080da9a843 coap_block.c: Handle missing Size1 option when using Block1 2025-01-09 15:42:09 +00:00
kkrentz 72190a8854 coap_subscribe_internal.h: Fix "unbalanced grouping commands" 2024-12-19 16:27:07 +00:00
Jon Shallow a87fb0c1cf coap_io.c: Add in support for getting state of internal file descriptors
Add in a new function coap_io_get_fds() to get the lists of (internal
to libcoap) file descriptors that are in a read or write pending state,
as well as the timeout time for the next internal to libcoap activity
(e.g. packet retransmission).
2024-12-18 14:09:48 +00:00
Mario Kicherer ebb8f07796 add helper functions to support external event loops, see #1554 2024-12-13 14:11:01 +00:00
Jon Shallow d82f857f67 LwIP: Support client sending multicast / broadcast (IPv4) packets 2024-12-05 13:36:01 +00:00
Jon Shallow 5f4c2ab96b coap_block.c: Fix returning 2.03 for ETag match
Tag server returned data with a digest computed ETag option value so
requests with an ETag option can return 2.03 if there is a match.

The size of bytes hash that is computed for the ETag can be overwritten by
localy defining COAP_ETAG_MAX_BYTES to be a value between 1 and 8 bytes
inclusive.  It is set to 4 by default.
2024-12-02 10:15:51 +00:00
Jon Shallow 0808b97009 coap_net.c: New coap_send_recv() function
Supports sending a PDU and waiting for the response PDU for subsequent analysis.

Handles multiple block transfers.
2024-11-01 15:27:22 +00:00
Jon Shallow 466f1e0b5c coap_oscore.c: Process queued OSCORE Observe responses after de-registration
If an Observe is de-registered, but an unsolicited Observe response is received
before the de-register response is received, then the unsolicited response
failed to get decrypted.
2024-10-28 14:33:34 +00:00
Jon Shallow 92b3db23e9 NACK handling: simplify libcoap code
Remove a lot of code duplication by providing new coap_handle_nack()
function.
2024-10-19 11:58:19 +01:00
Jon Shallow 79a6630478 blocks: Better reporting for large transfers
Update stateless token to track 20 bits - the largest no of blocks
as per RFC7959.

Make output of data detail configurable for coap_show_pdu().

Add in new function coap_enable_pdu_data_output() to do this.

Add -x option to coap-client and coap-server to enable this dropping
of data detail.
2024-09-26 10:50:31 +01:00
kkrentz ac83b32b33 Improve MTU handling
by:
- Correcting Contiki-NG's value of COAP_DEFAULT_MAX_PDU_RX_SIZE
- Denying to create PDUs longer than COAP_DEFAULT_MAX_PDU_RX_SIZE
- Using the MTU setter, thereby not overriding stack-specific limits
- Using existing constants for L4-specific header sizes
2024-09-17 13:28:44 +01:00
Jon Shallow 0d240530b4 Release 4.3.5: Release 4.3.5 2024-09-06 09:47:22 +01:00
Mario Kicherer 1c97907f2f coap_net.h: add ifdef WITH_LWIP around lwip functions 2024-08-27 11:59:23 +01:00
Jon Shallow e220c371be coap.h: Remove need for different coap.h files for different OS
./autogen.sh and ./configure no longer need to be run prior to running
make for Contiki, LwIP or RIOT from the appropriate examples sub-directory.

scripts/fix_version.sh now handles updating coap.h for the different
LIBCOAP_ and PACKAGE_ definitions.
2024-08-23 10:39:37 +01:00
Mario Kicherer 8899063a4a coap_ws: accept longer header lines and specific header sent by firefox browser 2024-08-23 10:39:17 +01:00
Jon Shallow 3d790a5964 reverse-proxy: Provide dedicated reverse-proxy handler setup function 2024-08-16 13:33:31 +01:00
Jon Shallow 626d4632d1 multicast: Document coap_mcast_set_hops() 2024-08-16 13:30:30 +01:00
Jon Shallow 540fede1b9 release 4.3.5rc3: Release candidate 3 for 4.3.5 2024-08-07 14:14:07 +01:00
Jon Shallow 92198b00b3 Supported: Break out run-time checks and libcoap build #defines
Group together and document the run-time checks available for checking
the libcoap built-in functionality based on the ./cconfigure and / or
CMakeLists.txt options.

Create include/coap3/coap_defines.h file that contains a list of
COAP_ #defines created from the options defined when running ./configure
or cmake.

Add additional coap_*_is_supported() functions.
2024-08-07 10:23:19 +01:00
Jon Shallow 5f3d7eb244 reverse-proxy: Add in reverse proxy support to coap-server
Tidy up the general proxy logic by moving common functionality
into libcoap.

New functions coap_proxy_forward_request(), coap_proxy_forward_response()
and coap_proxy_is_supported() added.

New --enable-proxy-code support added to configure.

New ENABLE_PROXY_CODE support added to CMakeLists.txt.

Support for common shared connection to next hop.

Support for round-robin balancing across multiple next hops.
2024-08-07 09:33:46 +01:00
Jon Shallow af024f9138 Connection-ID: Only change source port when CID has been negotiated
Only support changing the client's source port if the DTLS library
has support / configured for CID.

Used when debugging server handling source address changes and CID
is in use.

Add in support for TinyDTLS (Client only) if TinyDTLS is patched
appropriately (not currently available on TinyDTLS main branch).

Add in new function coap_dtls_cid_is_supported().
2024-08-07 09:13:52 +01:00
Jon Shallow 83718a6d2f Cleanup building header files for different OS
'make distclean' removes header files built from the .in files, which
means that they are not available for different OS builds.

The same is true for CMakeLists.txt.

Remove the need for the .in files and update for libcoap revisions
by using a new script scripts/fix_version.sh created from
scripts/fix_version.sh.in.

CI has been updated to check revisions have been updated in the
appropriate places.

Do not remove libcoap-3.sym and libcoap-3.map when doing 'make distclean'
as they are git tracked files.
2024-08-06 16:56:19 +01:00
Jon Shallow 8b2fb390b8 Connection-ID: Support changing client IP addresses
Support of RFC9146. Currently only Mbed TLS supported.

New function coap_context_set_cid_tuple_change() for a client to
force periodic source port changes to test out CID.

Configurable support for the client to use Connection-ID
assuming underlying DTLS library is configured for or supports it.
2024-07-22 08:56:17 +01:00
Jon Shallow b75c784a20 Constrained Stack: No need for individual locks
If a proxy reads in a packet via coap_read_endpoint, which then
invokes an ongoing proxy TCP connection, while waiting for the CSM
response of the new session, coap_read_endpoint would get invoked
again if there is any data to read on any of the listening endpoints..

With a Constrained Stack, the read in data is protected by mutex
m_read_enpoint - which is still locked when trying to wait for the
CSM.

As the code is either single threaded, or protected by global_lock
if multi-threaded, there is no need to maintain mutexes such as
m_read_endpoint.  This PR removes these not needed mutexes.
2024-07-22 08:47:05 +01:00
Jon Shallow 48b56aa830 coap_prng: Provide global_lock protection 2024-07-19 10:28:05 +01:00
Jon Shallow bd32823746 client-coap.c: Fix client spin loop for NO_SYS == 0
Do not call coap_free_context() from within sigint handler.
Always call coap_io_process().
Use sys_arch_sem_wait() for timeouts while waiting for input.
2024-07-19 10:05:30 +01:00
Jon Shallow 62686555b2 Locking: Unlock global_lock after coap_free_context() call 2024-07-17 14:15:10 +01:00
Jon Shallow f9c7223ecd Locking: Use single global lock instead of context lock
When context is de-allocated, other threads can still access the
context lock held within context. Indicating being freed when
coap_free_context() was called worked in most cases, but not all.
2024-07-17 08:52:50 +01:00
Jon Shallow af7da4596a TLS: EC-JPAKE support (RFC8236)
Currently only supported by Mbed TLS if the optional
MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED is defined for the Mbed TLS
library build.
2024-07-17 08:51:31 +01:00
Jon Shallow 7551d2e147 block handling: Update lg_crcv->last_used when sending Block1
This prevents premature de-allocation of the lg_crcv when large
files are getting uploaded.
2024-07-16 11:11:33 +01:00
Jon Shallow 3c720f232f release 4.3.5rc2: Release candidate 2 for 4.3.5 2024-06-25 09:31:27 +01:00
Jon Shallow eff1f38f9f path_query parsing: Remove need to define scratch buffer
Create new options coap_uri_into_optlist(), coap_path_into_optlist() and
coap_query_into_optlist() to be used instead of coap_uri_into_options(),
coap_split_path() and coap_split_query() functions so the requirement
for a scratch buffer is not needed.

Getting the minimum size for a scratch buffer is not straight forward and
potential for unexpected boundary condition errors to occurif too small.

Correct parsing and stripping out of "%2E%2E" ("..") and "%2E" (".")
path segments.  ".." now removes the previous segment (if it exists).

Update documentation and unit testcases.

Update examples to use new options.
2024-06-25 08:23:33 +01:00
Jon Shallow 0fbe59fc55 block: Handle separate responses that then are followed with Block2
Set up lg_crcv if separate ACK response received for a coap_send() CON
request to be able to handle Block2 responses.

Handle Block1 request getting back a Block2 request that has an initial
separate response once the Block1 has completed by adjusting the stateless
token counter.

Separate response is typical for proxy support (OSCORE previously handled)..
2024-06-14 11:43:18 +01:00
Jon Shallow f54686039b coap-client.c: Fix SNI generation
Move requirements for client to determine how to set up the SNI for
PSK/PKI encrypted communications down into libcoap. The client just
needs to indicate the host to connect to in the client_sni field of
coap_dtls_cpsk_t or coap_dtls_pki_t.

New internal function coap_sanitize_client_sni() makes sure that
the SNI sent follows RFC 6066.  As per
https://datatracker.ietf.org/doc/html/rfc6066#section-3

  Literal IPv4 and IPv6 addresses are not permitted in "HostName".

all IPv4 and IPv6 addresses are dropped for the SNI, and so no SNI is
sent.  DNS hostnames will get transmitted as the SNI.
2024-06-10 09:18:25 +01:00
Jon Shallow 0392d02217 release 4.3.5rc1: Release candidate 1 for 4.3.5
LIBCOAP_VERSION and LIBCOAP_PACKAGE_BASE are unchanged until
4.3.5 is released.
2024-06-06 10:07:50 +01:00
Jon Shallow 0223c1a3d4 Block1: Handle large FETCH with large blocked request and Observe
Handle Observe cancellation with large blocked FETCH that needs to use
diffferent tokens when there is packet loss by checking the cache-key
if the token is not found (Q-Block1).

Let FETCH with Q-Block1 use NON now that cache-key is getting checked
if token mismatch.

Handle getting an unsolicited Observe response in the middle of
sending the FETCH blocks that ultimately will be doing the Observe
cancellation following re-assembly. (The unsolicited Observe token will
be the same token that is used to respond to the observe cancellation).
2024-06-06 09:44:15 +01:00