Commit Graph
483 Commits
Author SHA1 Message Date
Jon Shallow de369eb7c7 coap-client.c: Cleanup following observe cancellation
Delay exit briefly checking if there are any response packets following
observe cancellation and process as appropriate.

If multiple observes are outstanding, cancelling them using CON will
cause issues as NSTART limit is hit.  The server then does not know
about all of the cancellations.

Also, this can allow the server to properly shutdown observe
activity when blocks are used.
2022-10-14 08:44:21 +02:00
Jon Shallow c5c4f9bf1d uri.c: Add coap_uri_into_options() function
Simplify code for applications by providing coap_uri_into_options()
which then adds the (optional) port and splits out the path / query
as CoAP options, adding them to the optlist_chain for subsequent use.

Added a new coap_delete_uri() for completeness.

Update documentation to include the uri based functions.

Update examples to use the new coap_uri_into_options() function.
2022-10-12 15:24:02 +01:00
Jon Shallow 3659e3c0cb LwIP: Support receiving ethernet MTU sized packets
Define PBUF_POOL_BUFSIZE appropriately in lwipopts.h
2022-10-12 14:37:20 +01:00
Jon Shallow 6a633e3d49 CMake: Force all Warnings to Errors when doing github actions
Add new option WARNING_TO_ERROR for CMakeLists.txt

Includes cmake builds for Windows and macOS
[Fix coap-client S_ISDIR() duplication bug]
2022-09-30 17:25:00 +02:00
Jon Shallow 692c06d95f build: Fix client and server only builds
Also correct LwIP build directory.
2022-09-27 10:37:38 +01:00
Jon Shallow de1bc9f3cb LwIP: Add in support for client
New demo client app.

Update some of the memory sizes.
New coap_lwip_dump_memory_pools() to dump LwIP memory pool usage.

Add in logic to handle new client sessions.

New coap_io_process() to handle LwIP which can invoke a callback handler
to wait / timeout for the next input handler.
New coap_lwip_set_input_wait_handler() to define this handler.

Update coap_io_prepare_io() to handle LwIP.

Support re-use of pbuf for PDU re-transmissions.

Make sure that NSTART is correctly obeyed.

Build the client with client only libcoap code.

Build the server with server only libcoap code.

Update LwIP source to latest stable release.

Reserve memory for a response PDU.

New coap_lwip(3) man page.

Update directory structures for config files.

Update files contained in "make dist" and check lwip rebuilds
in the github workflows.
2022-09-22 10:14:48 +01:00
Jon Shallow 2f25c950af net.c: Call nack handler for badly formatted response packet
If a response packet is received that is badly formatted, invoke the
nack handler (if defined) with COAP_NACK_BAD_RESPONSE so that the
requestor knows there is an issue, rather than timing out.

However, if the response packet is so badly formatted, or it is not
possible to find the requesting packet, then this nack event will not
trigger, but an event is triggered with COAP_EVENT_BAD_PACKET.
2022-09-20 15:12:29 +02:00
Jon Shallow 43bfbea924 Copyright: Update dates to 2022 where appropriate 2022-08-18 13:23:36 +01:00
Jon Shallow f4507e6e9a Block: Report event on large xmit failures
Add in new event COAP_EVENT_XMIT_BLOCK_FAIL which gets triggered on
detecting transmission failure of large body using BlockX.

Track the last time a lg_xmit was sent, so it can be timed out on failure.

Correctly track the next lg_xmit timeout time (and lg_crcv, lg_srcv) so that
the lg_xmit can be expired.
2022-08-13 14:28:38 +01:00
Jon Shallow d2306569d1 proxy: Make proxy requests separate responses
As an incoming proxy request may delay its response (e.g. initiate an
ongoing connection), send back empty ACK immediately so that the
client does not timeout and re-transmit the request.

Do not process duplicated (i.e. re-transmitted) proxy requests.

Correct a couple of typos in example proxy code.
2022-07-25 12:47:18 +02:00
Jon Shallow 347270b9ab file naming: Rename files to have coap_ prefix
Initial update - rename files that are not subject to any PRs.
2022-07-01 11:36:26 +01:00
Jon Shallow 88ae9563e6 mcast support: Support multicast granular to the resource level
Add in new function coap_mcast_per_resource() to enable granularity of
handling multicast requests differently at the resource level.

The granularity is handled by how the flags variable is set up in
coap_resource_init().  These additional flags are ignored if
function coap_mcast_per_resource() has not been invoked.

Added 2 functions coap_resource_unknown_init2() and
coap_resource_proxy_uri_init2() that support defining the mcast flags as
per coap_resource_init().

Responses are now delayed rather than the request being delayed before being
passed to the request handler.

Update coap-server with the -r option to enable multicast on specific
resources.

Do not reset wait_ms in coap-client for Block2 transfers.
2022-06-22 15:08:26 +02:00
Jon Shallow 5a10ce4890 Congestion Control: add in RFC7252 configuration flexibility
Create where appropriate default values for the Congestion control base
values and update the derived CC values accordingly.

Move derived value #define to coap_session_internal.h.

Update coap_session_t to hold the new CC values.

Update documentation for the new CC functions that update / get the
appropriate values.

NOTE: PROBING_WAIT is not currently checked in the code.
2022-06-17 12:15:19 +02:00
Jon Shallow 279755b1df coap_send: Make error checks for coap_send() more rigorous
Always delete PDUs on error before return from coap_send() (aligning with
previous documentation).

Check that the socket is still available before continuing with the PDU
transmission in coap_send().

Update documentation to be clearer on non-usage of PDU on return from
coap_send().

Trap coap_send() errors in coap-client.

Move event handlers setup code to earlier in coap-client to trap events
early on in the CoAP setup process.
2022-06-17 11:34:41 +02:00
Olaf Bergmann 925d39fd8c coap-server.c: Cleanup misplaced comment
Fix comment that has been left at the wrong place by a previous change.
2022-06-17 11:25:08 +02:00
Jon Shallow 256a758e02 TLS SIGPIPE: Stop programs exiting with code 141 (128 + 13:SIGPIPE)
The example coap-client application was setting the SIGPIPE signal to
SIG_IGN too late.

For applications that do not handle SIGPIPE, set MSG_NOSIGNAL in send() flags
to stop SIGPIPE being generated (but send() still returns EPIPE).

Drop the logging level when a client session cannot be extablished.

Can be reproduced when server requires certificates, but client does not
provide any certificate when using coaps+tcp:// and Mbed TLS.

Window 1
$ coap-server -c server.pem -C ca.pem
Window 2
$ coap-client coaps+tcp://127.0.0.1
$ echo $?
2022-06-09 09:21:14 +01:00
mrdeep1 c74b3efcb4 Merge pull request #744 from mrdeep1/cache_caution
Caching: Highlight requirements ignoring certain CoAP Options
2022-06-07 20:28:34 +01:00
Jon Shallow 069a0786ce CSM: Support different XMT and RCV Max-Message-Size
Track the RCV mtu size separately from the XMT mtu, so that sufficiently
large PDUs can be built for receiving incoming data.

Wait until reliable session is complete and the initial CSM have been exchanged
before building any new PDUs for transmission so that they can hold the
appropriate amount of Max-Message-Size data.

Add in support into coap-server and coap-client for defining the
Max-Message-Size to use by adding in 2 new functions
coap_context_set_csm_max_message_size() and
coap_context_get_csm_max_message_size().

Add in flexibility into coap-server to control the initial example_data size.

Requires #844 to prevent recursive mutex locking when initiating proxy ongoing
session to upstream server.
2022-05-04 10:01:37 +01:00
Hugo Hakim Damer d9c19c378f event.h: Add events for server session state management
This commit adds two new events (`COAP_EVENT_SERVER_SESSION_NEW`
and `COAP_EVENT_SERVER_SESSION_DEL`), which are triggered when the
IO loop creates or deletes server-side sessions.
2022-04-29 09:18:51 +02:00
Jon Shallow 65cba25cc7 coap-client.c: Delay sending each request using -G by 1 second
This then allows any (D)TLS to be set up between sending off the requests and
better demonstrates how this can be done in user code.

Allocate separate data for each post/put PDU.

Hold off on starting timer for next transmission until after response has
been received from the server (handles relatively slow DTLS startup).
2022-04-07 09:45:28 +01:00
obgm ee171b2b8c Merge pull request #826 from mrdeep1/proxy_critical_options
Proxy: Support unknown Critical but Safe-To-Forward options
2022-02-21 17:40:03 +01:00
Jon Shallow 19928e81bd builds: Set CFLAGS += -Werror in all linux subdirectory compilations 2022-02-18 14:23:43 +00:00
Jon Shallow dfc678c33b Proxy: Support unknown Critical but Safe-To-Forward options
The CoAP Proxy may not know how to handle a Critical option, but if it
is safe to forward it should be able to handle this.

RFRC7252 5.4.1
   Critical/elective rules apply to non-proxying endpoints.  A proxy
   processes options based on Unsafe/Safe-to-Forward classes as defined
   in Section 5.7.

If this is an incoming proxy type request special case any
critical options that are safe to forward.

Track ongoing sessions from an enabled proxy server so unknown critical
response options can be passed back to the originating requestor.

Update coap-server to handle Proxy-Scheme for proxying.

Update tests to use critical option 25 instead of 9.
2022-02-18 12:51:35 +00:00
Jon Shallow b3d503cbff sessions: Prevent multiple client session confusion
Prevent multiple coap_new_client_session*() calls that are connecting to
the same server/port being bound to the same local ip/port.

Update logging for session creation / removal to easily differentiate between
sessions that have the same 4-tuple.

Document that coap_new_endpoint() is not required in client based code.

If coap-client '-p port' but not '-a addr' is provided, fix code so that
the port is bound to.

Only remove outstanding CON requests with token matches if there is an
ACK response.

Update the logging to differentiate between the 6 occurrences where the mid
is removed.
2022-02-08 16:52:15 +00:00
Jon Shallow aaf6115594 proxy_uri: Fix handling the resource for uri path in Proxy-Uri
If the server recieving a Proxy request determines that it is the final
endpoint of the request, the resource that the request was sent to
was wrong. The update to coap_get_uri_path() fixes this issue.

There is now also parameter support for the proxy definition for the
coap-server where any defined host is treated as being the final endpoint
is in:

  coap-server -P ,

This allows any actual server to be the final endpoint of any proxy request
without have to define all the names or IP addresses that the coap-server is
known by.  Note that if there is nothing following the , (comma), anything
before the , (comma) will never be used.

Documentation updated.
2022-02-07 14:46:46 +00:00
Jon Shallow 64e5641017 versioning: Make current git describe available
Add new function coap_package_build() which returns "git describe --tags"
if available at library build time, else standard package version.

The value of "git describe --tags" is created at run time and passed in as
a compiler -D definition.

Update copyright end dates for updated files.
2022-02-02 11:24:52 +00:00
obgm 2a329e1c76 Merge pull request #771 from mrdeep1/server_post
coap-server: Add in POST support for unknown request handler
2021-12-22 15:31:37 +01:00
Jon Shallow 17aaa81b5a Caching: Highlight requirements ignoring certain CoAP Options
When using the cache logic to build up the data of a body transferred over
several blocks, the BLOCK options must be excluded when deriving the cache key.

Update documentation reminding the user of the need to do this.
2021-12-22 13:38:04 +00:00
obgm 17f7fe8c52 Merge pull request #780 from mrdeep1/multicast_get
coap-client.c: Allow time for all server responses to mcast
2021-12-22 14:16:36 +01:00
Jon Shallow 74582eddde resource.c: Support deleting resources that have not yet been added
Support deleting a resource both before and after calling
coap_delete_resource().

Make the coap_delete_resource() usage in examples less complex.
2021-12-22 12:59:32 +00:00
Jon Shallow 694a205f28 coap-server.c: Fix proxy response type and code
Handle "separate" response types correctly.

Make proxy based traffic handling mode COAP_BLOCK_SINGLE_BODY to remove
complexities.

Rename proxy_message_handler() to proxy_response_handler() as it is a
response handler.

Build a dummy 'received' PDU for use with coap_add_data_large_response() in
proxy_response_handler() to be consistant across all uses of
coap_add_data_large_response().

Add appropriate resource_flags (so that NON can be used in observe) for
resources "async" and "example_data".
2021-12-08 11:09:15 +00:00
Jon Shallow e8e33f0424 coap-server: Add in POST support for unknown request handler 2021-12-07 21:30:35 +00:00
Jon Shallow 76194be8cd coap-client.c: Allow time for all server responses to mcast
Instead of exiting after first response to a mcast request, wait for
DEFAULT_LEISURE for all potential server responses.
2021-11-29 11:59:27 +00:00
Jon Shallow 73389b8192 handlers: Clarify which handlers are client only, server only or both
Add in additional handler coap_register_request_handler() in addition to
coap_register_handler() but now only refer to and use
coap_register_request_handler() to aid readability.
2021-11-05 15:39:22 +00:00
Jon Shallow f026f5701e client+server: Reduce code size by building libcoap for client or server only
Add in new ./configure options --enable-client-mode or
--enable-server-mode which by default are enabled.

If --disable-client-mode is set, then disable all the client specific code.
[ COAP_CLIENT_SUPPORT is not defined ]

If --disable-server-mode is set, then disable all the server specific code.
[ COAP_SERVER_SUPPORT is not defined ]

Functions and variables are only #ifdef'd out in the include/coap3/*_internal.h
files.  They cannot be #ifdef'd out in the other include files as
COAP_CLIENT_SUPPORT and COAP_SERVER_SUPPORT are not available for application
compilations. The result of this is if, say, client only API functons are used
in a server application and COAP_CLIENT_SUPPORT is not defined, there will be
linking issues (missing functions).  Providing stub functions unecessarily
increases the code size.

For coap-server, if it is able to provide proxy support, requires both
COAP_SERVER_SUPPORT and COAP_CLIENT_SUPPORT to set up the ongoing sessions.
So, the build environment deliberatly sets SERVER_CAN_PROXY=0 if
COAP_CLIENT_SUPPORT is not available to allow linking.

Some of the tests have to be disabled if COAP_SERVER_SUPPORT or
COAP_CLIENT_SUPPORT is not set.

Code size change indications

   text    data     bss     sum   filename
 155912    1728   12080  169720   libcoap-3-notls.a
 138300    1728   12080  152108   libcoap-3-notls.a --disable-client-mode
 106734    1632   10304  118670   libcoap-3-notls.a --disable-server-mode

 180485    1733   12080  194298   libcoap-3-openssl.a
 160306    1733   12080  174119   libcoap-3-openssl.a --disable-client-mode
 125543    1637   10304  137484   libcoap-3-openssl.a --disable-server-mode

 181755    1728   12080  195563   libcoap-3-gnutls.a
 160972    1728   12080  174780   libcoap-3-gnutls.a --disable-client-mode
 127178    1632   10304  139114   libcoap-3-gnutls.a --disable-server-mode

 165879    1855   12128  179862   libcoap-3-tinydtls.a
 147022    1855   12128  161005   libcoap-3-tinydtls.a --disable-client-mode
 115618    1759   10352  127729   libcoap-3-tinydtls.a --disable-server-mode

 171690    1728   13744  187162   libcoap-3-mbedtls.a
 152099    1728   13712  167539   libcoap-3-mbedtls.a --disable-client-mode
 118786    1632   11984  132402   libcoap-3-mbedtls.a --disable-server-mode
2021-09-30 16:10:52 +01:00
obgm 38fa40857a Merge pull request #701 from mrdeep1/token_handling
coap-client: Add in support for generating multiple requests
2021-09-28 09:35:31 +02:00
obgm 65e380ab83 Merge pull request #733 from mrdeep1/event_enum
coap_event.h: Make coap_event_t an enum
2021-08-02 12:07:55 +02:00
Jon Shallow a1d78d505d tiny.c: Fix message id generation 2021-08-02 10:09:21 +01:00
Jon Shallow 8ce139d349 coap_event.h: Make coap_event_t an enum
examples/coap-client.c:
examples/coap-server.c:

Explicitly define each enum case to remove complier warnings.
2021-07-31 17:08:10 +01:00
Jon Shallow 4bbf25ba33 coap-client: Add in support for generating multiple requests
Demonstrate Token usage for tracking the multiple requests so
that it is easy to see how to extend this for bespoke applications.
2021-06-10 10:35:13 +01:00
Olaf Bergmann 60421db0a5 Add SPDX-License-Identifier to source files
This change adds 'SPDX-License-Identifier: BSD-2-Clause' [1] to
the source code files that originate in the libcoap project.

Note that not all files are tagged because they are pulled in
from other sources and thus may fall under a different license.
More information about these is contained in the LICENSE file.

[1] https://spdx.org/licenses/BSD-2-Clause.html
2021-06-08 18:14:03 +02:00
Jon Shallow 7cff27fb7a examples: Fix RPM Package Manager complaining about duplicate objects
For example (when building OpenSSL variant), coap-client and
coap-client-openssl are identically built and debug information is being
abstracted, it complains that the two binaries are identical.

Make coap-server, coap-client and coap-rd non-installable, (they still need
to be built in the examples directory, so cannot use check_PROGRAMS) and then
soft-link them as appropriate in the target $(bindir).
2021-06-08 09:00:33 +01:00
Jon Shallow 8d73044579 coap_send(): Consolidate coap_send() and coap_send_large() for applications
As it is not that intuitive when to use coap_send() or coap_send_large()
in applications, rename libcoap usage of coap_send() to coap_send_internal()
and then all usages of coap_send_large() to coap_send() so that only
coap_send() is required in applications.

Documentation updated accordingly.
2021-05-29 14:08:00 +01:00
obgm 62b2be4da1 Merge pull request #667 from mrdeep1/dtls_default
configure.ac: Add in --enable-add-default-names option
2021-05-28 18:06:24 +02:00
Jon Shallow 587f7e574c coap-server: Add in FETCH example 2021-05-21 13:52:45 +01:00
Jon Shallow f726d0460a Observe: Support FETCH with data for triggered responses
The original requesting PDU is now kept with the Observe request along with
its data and used whenever an additional response needs to be generated.

Abstracted information from the requesting PDU is no longer kept in
coap_subscription_t as it can be obtained from the kept PDU.

No longer any need to check for request PDU in request handlers as it is now
always provided.

coap_resource_notify_observers() now ignores the query parameter, as a
cache_key is now used for request matching.
2021-05-21 13:08:11 +01:00
Jon Shallow 7cd5bfa638 configure.ac: Add in --enable-add-default-names option
If --enable-add-default-names is set (the default), then the installed default
libraries and installed default example executables will point to the
appropriate object that has the (D)TLS library type in the name.  E.g.

libcoap-3.so -> libcoap-3-openssl.so
coap-client -> coap-client-openssl

If --enable-add-default-names is not set, then this will not happen.

As well as the libraries, the example binaries are now created with the (D)TLS
library extension in all cases.
2021-05-20 16:33:28 +01:00
Jon Shallow 939f7d4380 examples/Makefile.am: Revert commit f6eede8077
It is no longer needed now that uthash.h is no longer required by coap_rd.
2021-05-20 13:28:44 +01:00
Jon Shallow b694c4f5e5 coap-rd: Fix coap_str_const_t deallocate errors
Fix calls to coap_resource_init() and coap_add_attr() where proper
coap_str_const_t * were not being passed in that were safe to free off.
2021-05-20 13:28:44 +01:00
Jon Shallow fb40a231a4 coap-rd: Remove requirement for uthash.h
Add rd_t information as user data to the resource (which has hashed lookup)
rather than maintaining a separate hash within coap-rd.
2021-05-20 13:24:49 +01:00