Commit Graph
674 Commits
Author SHA1 Message Date
Jon Shallow f62c836ece coap_resource.c: Make dynamic resource handling thread safe
Let libcoap do the necessary multi-thread locking during resource
generation, lookup and deletion.

Supported using a new handler set up by coap_register_dynamic_resource_handler().
2025-07-08 13:37:57 +01:00
Jon Shallow d33b7fa571 coap_session.c: Support converting a Client session to a Server session
Server to Client is already supported, this provides the reverse with
coap_session_set_type_server().

Add in call-home support to coap-client and coap-server for testing.

Addition of a new event COAP_EVENT_SERVER_SESSION_CONNECTED.
2025-07-02 17:41:07 +01:00
fj-blanco 4fa3dfa0c7 Zephyr: add POSIX API support
Changes:
- Remove duplicate OBSERVE_PERSIST configuration block

Improve example build:

- Make virtual environment activation optional in Makefile
- Remove --fresh flag unsupported in newer west versions
- Copy mbedTLS config header to workspace include directory
- Add TARGET check for mbedTLS before configuring include directories
- Add mbedTLS linking to coap-3 target

Add POSIX API support:

- Enable CONFIG_POSIX_API=y in examples/zephyr
- Replace direct Linux/Windows system calls with Zephyr POSIX API
equivalents
- Add Zephyr's sys_rand_get() to coap_prng_default()
- Define missing network macros (IN_MULTICAST, IN6_IS_ADDR_MULTICAST,
etc.)
- Add FIONBIO and OPTVAL_T/OPTVAL_GT macro definitions for Zephyr
- Add native Zephyr timing functions to replace missing mbedTLS timing
support
- Fix fd_set mapping using zephyr/net/socket_select.h
- Exclude strnlen redefinition conflicts for Zephyr
- Add proper IPv6 packet info and address mapped definitions

(Patch) Add DNS resolution and net setup to example client:
- Enable CONFIG_DNS_RESOLVER for hostname resolution
- Add Linux networking setup instructions for native_sim board
- Configure static IP gateway and DNS server (8.8.8.8)
- Add DHCP vs static IP configuration documentation
Co-authored-by: mrdeep1
2025-06-26 15:23:23 +01:00
Jon Shallow f28f4109ec coap_check_async: Fix async callback timing window
Multiple threads can call coap_check_async() when one of them does a callback
into application space. Only allow one thread to be scanning async_state.
2025-06-20 19:23:02 +01:00
Jon Shallow 06ff7e092b Proxy: Remove proxy subscribe on unsolicited response timeout
Also handle large upstream stream packets being converted to downstream
datagram packets.
2025-06-09 10:25:34 +01:00
Jon Shallow d328fc5330 OSCORE: Handle extended tokens when Echo option is returned for B.1.2 2025-06-03 21:35:56 +01:00
Jon Shallow 358da10706 Proxy: Correctly handle OBSERVE cancellations 2025-05-26 17:04:04 +01:00
Mario Kicherer 0ec5f799f2 add coap_session_get_endpoint, coap_endpoint_get_app_data and coap_endpoint_set_app_data 2025-05-22 08:20:45 +01:00
Jon Shallow ce2057b7a9 Locking: Remove context from a subset of the lock function macros (3 of 3)
Makes reading the code simpler.

Context is no longer used as global_lock is used in the coap_lock_lock() and
coap_lock_unlock() functions.

Part 3 of 3.

Update coap_lock_lock() and coap_lock_unlock() macros and all users.
2025-05-21 16:50:00 +01:00
Jon Shallow 882cd6b735 Locking: Remove context from a subset of the lock function macros (2 of 3)
Makes reading the code simpler.

Context is no longer used as global_lock is used in the coap_lock_lock() and
coap_lock_unlock() functions.

Part 2 of 3.

Update coap_lock_check_locked() and all users.
2025-05-21 15:49:40 +01:00
Jon Shallow ccf53a41af Locking: Remove context from a subset of the lock function macros (1 of 3)
Makes reading the code simpler.

Context is no longer used as global_lock is used in the coap_lock_lock() and
coap_lock_unlock() functions.

Part 1 of 3.

Update coap_lock_callback(), coap_lock_callback_ret(),
coap_lock_callback_release(), coap_lock_callback_ret_release() and
coap_lock_invert() macros and all users.
2025-05-21 14:54:54 +01:00
Jon Shallow 8975c15cd8 coap_resource.c: Fix observe callback timing window
If multiple clients subscribe to the same resource, one of the clients
'goes away', the server will eventually close down the session for that client
which will then remove any subscriptions, iterating through the different
resources.

If this happens at the same time that coap_resource_notify_observers() is called
in another thread, and the GET/FETCH handler is called for a common resource
and in particular for the session that is now going away, on return from the
callback the subscription may no longer exist.  This PR fixes this window.

In addition, if the resource subscriber list is being traversed, then a another
call to coap_notify_observers() for the same resource will not generate a new set
unsolicited observe responses for multi-thread protection.

Update
2025-05-20 15:25:31 +01:00
Jon Shallow 24ec7b19bf RIOT: Fix building RIOT with RIOT's utlist.h change 2025-05-20 11:46:26 +01:00
Jon Shallow 60e9f08e47 LwIP: Fix TCP support 2025-05-16 14:40:52 +01:00
Jon Shallow 07c880b56e coap_pdu.c: Report options with invalid length
Malformed PDUs were responsed to with a RST.  Now request PDUs with invalid
lengths are also reported with a 4.02 response.

See RFC7252 5.4.3 and 5.4.1.
2025-05-16 10:01:11 +01:00
Jon Shallow c816bf7fcf coap_net.c: Add Reserved options RFC7252 Table 7 to option check list
The checking can be overriden by using coap_register_option().

Updated the diagnostic payload for 4.02 responses to be inline with RFC 7252,
and not include the actual errant options in the response.
2025-05-15 09:51:56 +01:00
Jon Shallow daa4e05e18 Proxy: Add in support for multiplexing Observe requests
Add in limited caching for multiple clients requesting an Observe subscription
for the same upstream session resource so that only a single observation request
is sent upstream and any unsolicited response is sent back to all the
requesting clients.

This requires a change in the recently added handler registered to
coap_register_proxy_response_handler(), which no longer should no longer call
coap_proxy_forward_response(), but just return a PDU for forwarding on.
2025-05-05 09:46:53 +01:00
Jon Shallow 2e8decf756 Mcast: Support multiple large blocked body responses
When initating a multicast request, multiple unicast responses can occur.

If the unicast response subsequently generates further traffic (in other words
a BLOCK1 or BLOCK2 transfer) to complete the request/response sequence, each
unicast transfer can behave differently.

Set up different coap_lg_crcv_t for each unicast response for the session.

Set up different coap_lg_xmit_t for each unicast request if doing large transmits.

Reference initial transmitted request PDU rather than maintaining copies of the
PDU in coap_lg_crcv_t or coap_lg_xmit_t.
2025-05-05 09:12:30 +01:00
Jon Shallow 3afff2dba5 coap_io.c: Correct timeout values when entry has just timed out
If calculated timeout is 0 in coap_io_prepare_io_lkd(), then do not set overall
timeout to 0 so that when passed back to caller it does not get reset to the
caller's max defined timeout.

Updated some timeout check functions to update remaining timeout in a new
parameter and to return 1 if this parameter was updated, else 0. for simplicity.

It was also unsafe to set the initial max timeout value to -1 as coap_tick_t
can be int64_t.
2025-04-22 08:59:27 +01:00
Jon Shallow fada39dcba coap_cache.h: Fix documentation @return error 2025-04-14 17:15:56 +01:00
Jon Shallow 72691daec2 coap_pdu.c: Support incrementing reference count on const coap_pdu_t parameters
const coap_pdu_t * parameters are passed into application (call-back handler)
code.  If the application code then calls a libcoap API with a
const coap_pdu_t * parameter, it is not possible to increment the reference
counter so that multiple entities can reference the same PDU and an entity's
call to coap_delete_pdu() does not prematurely remove the PDU until all the
entities have finished.

This code allows the (const) PDU's reference counter to be incremented.
2025-04-11 16:26:44 +01:00
Jon Shallow 84778a7bb4 Proxy: Provide support for a proxy to create a client session
Add in functions
 coap_new_client_session_proxy()
 coap_register_proxy_response_handler()
2025-04-04 11:35:23 +01:00
Jon Shallow 499341c4f6 Proxy: Make upstream server handling more rigorous
Allow re-establishment with upstream server when server restarts.

When upstream server re-connects, re-establish any (D)TLS sessions
and active observe subscriptions.

Supported by a new function coap_context_set_session_reconnect_time().
2025-04-03 08:30:57 +01:00
Jon Shallow 1ba3158db8 Observe: Make sending observe failed messages optional
When a server cleanly closes down (calls coap_free_context()), when
a resource gets deleted, a 4.04 message is sent out (not when using
coap_persist(3)) for any active observe subscriptions. If a server knows it is
going to be restarted, then there is no need to send out a observe failed message.

When deleting a resource when normally running, it is correct to send out a 4.04
Not Found response as the resource has genuinely gone away.  However, when the
server is cleaning shutting down, it is better to use 5.03 Service Unavailable to
indicate the situation may be temporary.
2025-03-28 12:25:05 +00:00
Jon Shallow d38e177af1 Proxy: Remove unused proxy entries when they time out.
Add a common cleanup function to make sure all information is correctly
freed off.

Make sure that there is client session stickability to use the same proxy entry
when making new requests.
2025-03-27 08:57:37 +00:00
Jon Shallow df95d5bcc3 Proxy: Ignore idle timeout if observe subscription on session
Ignore the idle_timeout_secs parameter for a server if there is one
or more active observe subscriptions for the server session.
2025-03-24 16:17:14 +00:00
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