Commit Graph
373 Commits
Author SHA1 Message Date
obgm b8c66fb401 Merge pull request #537 from mrdeep1/deprecated_warnings
Deprecated: Replace deprecated functions in library
2020-10-20 14:46:04 +02:00
Jon Shallow 1a349d5cb4 coap_cache: Add in Cache Key and Cache Entry support
This updates the initial work done for adding in cache support

include/coap2/coap_cache.h: (new)
include/coap2/coap_cache_internal.h: (new)
src/coap_cache.c: (new)

New files to support cache support.

CMakeLists.txt:
Makefile.amMakefile.am:
Makefile.libcoap:
configure.ac:
examples/lwip/Makefile:
include/coap2/coap.h.in:
include/coap2/coap.h.windows:
win32/libcoap.vcxproj:
win32/libcoap.vcxproj.filters:

Add in the new files.

examples/client.c:

Fix valgrind reported memory leak.

examples/coap-server.c:

Add in new example_data resource that uses the new caching logic.
Update hnd_put() to use the new cache logic for BLOCK1 PUTs.

include/coap2/coap_forward_decls.h:
include/coap2/coap_internal.h:
include/coap2/coap_session.h:
include/coap2/net.h:

Add in new cache support.

libcoap-2.map:
libcoap-2.sym:

Include the new functions.

man/Makefile.am:
man/coap.txt.in:
man/coap_cache.txt.in: (new)

Update documentation.

src/coap_io.c:

Idle timeout cache entries.

src/coap_session.c:

Delete cache entry when session is deleted.

src/net.c:

Delete cache entries when context is freed off.

src/coap_gnutls.c:
src/coap_mbedtls.c:
src/coap_notls.c:
src/coap_openssl.c:
src/coap_tinydtls.c:

Add in SHA256 digest support.
2020-10-14 10:04:12 -04:00
obgm 9ee4895797 Merge pull request #540 from mrdeep1/fetch_observe
Observe: Support FETCH
2020-09-10 18:08:46 +02:00
Jon Shallow 26cd2446a8 Deprecated: Replace deprecated functions in library
examples/contiki/coap-observer.c:
examples/etsi_iot_01.c:
include/coap2/coap_prng.h:
include/coap2/net.h:
src/coap_debug.c:
src/coap_openssl.c:
src/coap_session.c:
src/net.c:
src/option.c:
tests/test_error_response.c:
tests/test_options.c:

prng -> coap_prng
prng_init -> coap_prng_init
coap_option_setb -> coap_option_filter_set
coap_option_clrb -> coap_option_filter_unset
coap_option_getb -> coap_option_filter_get

include/coap2/coap.h.windows:
win32/libcoap.vcxproj:
win32/libcoap.vcxproj.filters:

prng.h -> coap_prng.h

include/coap2/option.h:

Add in COAP_DEPRECATED in deprecated functions.
2020-09-08 17:21:34 +01:00
Jon Shallow 7033555d29 RFC8768: Add in support for CoAP Option Hop Limit loop detection
All this is handled by libcoap with coap-client having the ability to define
an initial Hop Count value for testing. No other application support is
required.

Incoming requests are checked for the Hop Limit option - if found, the value
is decremented in the PDU before passing it up to the application. If it
decrements to 0, a 5.08 response is generated.

coap_add_option() checks if this is a Proxy option for a request. If so, and
Hop Limit option does not exist, then the Hop Limit option is inserted into
the PDU with the default value (16).

coap_send() checks if it is a 5.08 response and if so, prepends the diagnostic
data with the IP address of the transmitting interface. If the IP address
already exists, then the PDU is dropped as the return path for the 5.08 response
is looping.  An alternative method for return path looping check is also
used by initially setting, then testing and then decrementing the Hop Limit
value as response packets pass back through the proxies.  If a proxy does not
copy the Hop Limit options when returning the 5.08 response, then the IP address
duplication is used.

Makefile.am:

Hide the coap_insert_option(), coap_update_option and coap_pdu_check_resize()
function from applications.

README.md:

Add in RFC8768 is supported by libcoap.

examples/client.c:

Add in the -H hoplimit option.

include/coap2/pdu.h:

Add in the default Hop-Limit: option COAP_OPTION_HOP_LIMIT.
Re-organize the COAP_OPTION_ descriptions to include
the C, U, N, R, E, I and U flags.
Expose the internal coap_pdu_check_resize() function so src/net.c can use it.
Define the two new internal functions coap_insert_option() and
coap_update_option() and expose them so that the test suites can use them.

man/coap-client.txt.in:

Document the new -H hoplimit option.

man/coap.txt.in:

Include the new RFC8768.

man/coap_pdu_setup.txt.in:

Update documentation for the CoAP options.

src/coap_debug.c:

Add in support for printing out COAP_OPTION_HOP_LIMIT.

src/net.c:

Make sure that COAP_OPTION_HOP_LIMIT is not returned in an error response.
Decrement received Hop-limit in handle_request() and generate 5.08 response
if appropriate.
Update coap_send() to prepend IP in 5.08 response.
Support COAP_OPTION_HOP_LIMIT being used in the 5.08 response message.

src/pdu.c:

Report on CoAP options that are repeated, but not defined as repeatable.
Define new (5.08) Hop Limit Reached error response.

Add in new internal coap_insert_option() function. This now means that
callers of coap_add_option() no longer have to have the options in the
correct order.

Add in new internal coap_update_option() function that updates the value of
an option.

Make coap_pdu_check_resize() non-static.

tests/test_pdu.c:

Check that options are getting inserted in the correct order when using
coap_insert_option(), even though they are provided in a random order.

Test the coap_update_option() function.
2020-09-07 12:34:04 +01:00
Jon Shallow e85e3abb82 Observe: Support FETCH
Support observe for both GET and FETCH.

Correct coap-client programatic addition of observe establish option which
"happened" to work previously because COAP_OBSERVE_ESTABLISH was 0.
2020-08-19 11:24:08 +01:00
Jon Shallow 9443d9034a PKCS11: Add in PKCS11 support for PKI
Add in a new PKI_KEY type for PKCS11 URI to get the PKI information from a
smart card or HSM device.

Currently only OpenSSL and GnuTLS are supported.

Makefile.am:

Do not expose coap_dtls_shutdown() function.

examples/client.c:
examples/coap-server.c:

Add in support for using pkcs11: URIs for defining the Certificates and
Private Keys.

include/coap2/coap_dtls.h:

Add in COAP_PKI_KEY_PKCS11 Key type.

man/coap-client.txt.in:
man/coap-server.txt.in:

Update documentation for supporting PKCS11.

man/coap_encryption.txt.in:

Add in documentation for COAP_PKI_KEY_PKCS11.
Correct some typos.

HOWTO.dual.gnutls (new):
HOWTO.dual.openssl (new):
HOWTO.dual.softhsm2 (new):
HOWTO.pkcs11 (new):

HowTo information.

src/coap_gnutls.c:
src/coap_openssl.c:

Add in PKCS11 support.

src/coap_mbedtls.c:

PKCS11 not yet supported.
Add in no-op coap_dtls_shutdown() function.

src/coap_notls.c:
src/coap_tinydtls.c:

Add in no-op coap_dtls_shutdown() function.

src/net.c:

Invoke coap_dtls_shutdown() function in coap_cleanup().

NOTE: There are memory leaks after cleanly closing down.  These are not within
libcoap as far as can be determined.  These leaks are a work in progress.
2020-07-07 09:15:58 +01:00
Olaf Bergmann f0e8ebe0d0 coap-server.c: free allocated uri memory in hnd_delete
This fixes CID 1483548.
2020-06-19 16:51:55 +02:00
Olaf Bergmann 784c2ddc5f coap-server.c: fix memory leak in hnd_unkown_put()
Reverse the sequence of sanity checks to avoid allocating a new object
when not necessary.

This fixes CID 1483549.
2020-06-19 16:49:13 +02:00
Olaf Bergmann ec3dfda228 coap-server.c: Fix memory leak in hnd_put()
Added missing coap_delete_string() for uri_path in case of an error
return.

This fixes CID 1483553.
2020-06-19 16:13:50 +02:00
Olaf Bergmann ba45a57efc coap-server.c: add missing fclose in read_file_mem
This fixes CID 1495722.
2020-06-19 16:01:29 +02:00
Olaf Bergmann e413fd545b coap-server.c: avoid fopen on NULL pointer in read_file_mem
As the behavior for calling fopen() on a NULL pointer is undefined in
ISO C99, a check is added before passing the file name to fopen().

This fixes CID 1495723.
2020-06-19 16:01:29 +02:00
Olaf Bergmann 1fded5dace coap-server.c: close file after reading SNI file
Add missing fclose() to cmdline_read_pki_sni_check() and
cmdline_read_psk_sni_check().

This fixes CIDs 1495719 and 1495724.
2020-06-19 16:01:09 +02:00
Jon Shallow 8559e40cce async: Fix session->ref assertion failure and timeout values
Calling coap_free_context() does not free off any async entries, and so
raises assertion errors when coming to freeing off sessions if any async
entries exist.

async_data_t is a union, but the size of val and ptr are not the same - so
usage can have uninitialized data - hence bad timeout values.

Makefile.am:

Do not expose coap_delete_all*() functions.

examples/coap-server.c:

Fix async_data_t issue.

examples/etsi_iot_01.c:

Fix 32 bit compiler warnings using same logic as coap-server for holding the
async timeout value.

examples/lwip/Makefile:

Include async.c for the LwIP build.

include/coap2/async.h

Define coap_delete_all_async() function.

include/coap2/net.h
include/coap2/resource.h

Document coap_delete_all*() functions as internal.

libcoap-2.map
libcoap-2.sym

Remove non-exposed coap_delete_all*() functions.

src/async.c

Provide coap_delete_all_async() function.

src/net.c

Call coap_delete_all_async() from within coap_free_context().
2020-05-07 09:44:41 +01:00
Jon Shallow 7fff53227a Windows: Fix compiler warnings
Fix size_t to int warnings by explicit casting.
Fix usage of fileno.
2020-05-06 12:38:49 +01:00
obgm 4c299ddc95 Merge pull request #444 from mrdeep1/sign-conversion
Windows C4267 errors: Fix size_t conversion warnings in windows builds
2020-05-04 17:49:45 +02:00
Jon Shallow 21e045bb8f coap-server: Make sure observe updates are sent off asap
examples/coap-server.c:

Track time spent in the epoll branch for the select() and coap_io_process()
calls to correctly adjust wait_ms.

Adjust wait_ms to be the remaining amount in the current second, so that
any 'time' observers get the updated time just after the new second starts.

include/coap2/net.h:

Add in support to track that an observe is pending transmission - primarily
a saving of needing to look up if a observe needs to be sent.

include/coap2/resource.h:

Add in support to track the context that 'owns' a resource.

src/resource.c:

Track the context that 'owns' a resource in coap_add_resource().

Track the fact that when we are making a resource partially dirty by recording
that an observe is pending in coap_notify_observers().

In the epoll case, make sure that timerfd triggers immediately when a pending
observe is set so that the application will return immediately from any select()
etc. call to process the libcoap outstanding activity (in particular pending
observes) in coap_resource_notify_observers().

Only call coap_notify_observers() in coap_check_notify if there are any
observes pending.

man/coap_observe.txt.in:

Minor clarifications in the examples.
2020-05-04 11:04:23 +01:00
Jon Shallow afefd95ceb coap_io_process: Fix 1ms busy loop and indefinite sleep
Replace COAP_RUN_BLOCK with COAP_IO_WAIT and COAP_RUN_NOBLOCK with
COAP_IO_NO_WAIT for naming clarity.

Instead of COAP_IO_NO_WAIT being 1, make it -1 to fix 1ms busy loop.
Handle the case when coap_io_prepare_io returns 0 to prevent indefinite
select() wait.

include/coap2/net.h:

Make the timeout_ms parameter uint32_t in coap_io_process*() functions to
make sure special case of COAP_IO_NO_BLOCK is correctly handled on different
architectures.
Update COAP_IO_NO_BLOCK to -1.

src/coap_io.c:

Fix the 1ms busy loop and potential infinite wait in the non epoll case.

man/coap_io.txt.in:

Update documentation to reflect the changes.

examples/coap-server.c:
examples/contiki/server.c:

Update to use COAP_IO WAIT and COAP_IO_NO_WAIT as appropriate.
2020-02-25 22:03:46 +00:00
Jon Shallow 4b072cd706 DISABLE_TCP: Make TCP enabled code checking more rigorous
In the same way that coap_split_uri() allows coaps:// scheme even if DTLS is
not available, it should allow coap+tcp:// and coaps+tcp:// scheme and let
other functions check whether TCP/TLS is available or not.

examples/client.c:

Verify if TCP is supported for scheme coap+tcp://.

src/coap_session.c:

Verify that the underlying CoAP transport protocol is supported in
coap_session_create_client().
Add verify for TCP protocol in coap_new_endpoint(), as well as always check
if TLS is supported.

src/uri.c:

Always allow coap+tcp:// and coaps+tcp:// in coap_split_uri() as a parsed
scheme in same way that coaps:// is always allowed.

Makefile.libcoap:

Include coap_tcp.c to include coap_tcp_is_supported() function.

examples/lwip/Makefile:

Include coap_tcp.o to include coap_tcp_is_supported() function.
2020-02-24 09:58:14 +00:00
Jon Shallow bb38e995bc Windows C4267 errors: Fix size_t conversion warnings in windows builds
examples/client.c:
examples/coap-rd.c:
examples/coap-server.c:
include/coap2/block.h:
include/coap2/coap_hashkey.h:
src/block.c:
src/coap_debug.c:
src/coap_hashkey.c:
src/coap_io.c:
src/coap_openssl.c:
src/net.c:

Fix C4267 windows compilation warnings when using Visual Studio where
size_t variable parameters are being converted  in both size and sign.

There still are some C4116 warnings to fix.
2020-02-21 17:28:11 +00:00
Jon Shallow 9cf4f10b35 Rename coap_run_once() to coap_io_process()
coap_read(), coap_write() and coap_run_once() are confusing in their names
and do not really describe what they do.

For example, coap_read() does both reading and writing, coap_write() may
do some writing, but primarily is used to determine what should be monitored
and for how long to wait in the following select() or epoll_wait() calls.

coap_run_once() implies that the code is only to be run once - again confusing.

By renaming coap_read() to coap_io_do_io(), this better describes what this
function does.

By renaming coap_write() to coap_io_prepare_io() this better describes what this
function does.

By renaming coap_run_once() to coap_io_process(), this better describes what
this function does.

By renaming coap_io_do_events() to coap_io_do_epoll(), this better describes what
this function does and is associated with.

A wrapper function coap_io_prepare_epoll() has been created, using
coap_io_prepare_io() with some preset values to simplify function usage.

include/coap2/net.h:

Update with the new function names, and deprecate the old functions.  Also
document the fact that coap_io_prepare_io(), coap_io_do_io(),
coap_io_prepare_epoll(), and coap_io_do_epoll() are really internal functions.

Create app_io module for Doxygen.

libcoap-2.map:
libcoap-2.sym:

Expose the new functions to applications.

examples/client.c:
examples/coap-rd.c:
examples/coap-server.c:
examples/contiki/coap-observer.c:
examples/contiki/server.c:
examples/etsi_iot_01.c:
man/coap_io.txt.in:
man/coap_observe.txt.in:
src/coap_io.c:
src/coap_io_lwip.c:
src/net.c:
src/pdu.c:

Update using the new function names.

Setting the etimer has been moved out of coap_io_process() into
coap_io_do_epoll(), making the logic easier for applications to use
coap_io_do_epoll() should they need to.  This also gets rid of a
potential timing window where a pending etimer event gets missed.

coap_io_do_epoll() now calls coap_io_prepare_epoll() if the etimer has
triggered removing the need for applications using coap_io_do_epoll() to
set up their own loops if etimer is triggered.
2020-02-13 19:54:48 +00:00
Olaf Bergmann fca966385e coap-server.c: Immediate exit when no endpoints defined
get_context() creates a new context object and adds coap_endpoint_t
objects for all listen addresses. The function now clears the
context object and returns NULL when no endpoint was created,
causing the server to exit immediately.
2020-02-12 13:28:39 +01:00
obgm b136504d22 Merge pull request #446 from mrdeep1/coap-rd-windows
coap-rd: Fix windows build
2020-01-17 10:58:25 +01:00
Jon Shallow 2a48f44fe4 coap-rd: Fix windows build
Define S_ISDIR if needed for Windows.
2020-01-16 15:36:28 +00:00
Olaf Bergmann b43907ef2b Conditionally disable TCP
The pre-processor flag COAP_DISABLE_TCP controls if TCP support is
compiled into the library. If set to 1, some bytes can be saved
by omitting TCP-specific code. By default, this flag is set to 0.

The new configure option '--disable-tcp' will set this flag to 1.
2020-01-16 13:37:24 +00:00
Jon Shallow 2d6c50e041 coap-server: Add in support for using/testing COAP_PKI_KEY_PEM_BUF
Add in -m option to enable using COAP_PKI_KEY_PEM_BUF instead of
COAP_PKI_KEY_PEM when defining certificates.  This is done by reading in
the contents of the file into a temporary buffer and the buffer is
passed in instead of the filename to the PKI logic.
2020-01-16 09:40:44 +00:00
Jon Shallow da41a91a11 coap-rd: Add in (D)TLS support
Add in basic (D)TLS support using code and options from coap-server.

Closes #435
2020-01-15 17:29:10 +00:00
obgm 0a5bf521b4 Merge pull request #440 from mrdeep1/coap-server-doc
coap-server: Correct man and help documentation
2020-01-15 17:52:35 +01:00
Jon Shallow ba7089994d coap-server: Correct man and help documentation
Found when updating the coap-rd documentation.
2019-12-28 16:58:51 +00:00
Jon Shallow 82a68f4457 Windows: Update code to build on Windows
Add in specific _WIN32 definitions for the missing functionality from
recent PR updates.

R_OK (used by access())
strndup()
strcasecmp()
2019-12-26 20:55:53 +00:00
Jon Shallow 0be55e96ed client.c: Fix ordering of encryption selection to fix PSK
PR #338 broke the PKI/PSK selection order for the encryption type
to use when trying to include the SNI even if PSK or PKI had not
been configured - even though coaps or coaps+tcp was in use. If
PSK had been defined, then it failed.

This fix puts the creation of the correct type of session into a separate function
which is then called from 2 places.
2019-12-18 17:06:41 +00:00
Jon Shallow 543581d1ae coap-server: Add in support for SNI to change PKI certificates
examples/client.c:

Update to send SNI if coaps, but certs/PSK not defined.

examples/coap-server.c:

Add in -S option to support changing certificate/ca if there is as SNI match.

man/coap-server.txt.in:

Update man page with new -S option.
2019-12-16 15:16:45 +00:00
Jon Shallow 1c8dce06b9 Update coap-client and coap-server to use new _psk2 functions
examples/client.c:

Update code to use the new coap_new_client_session_psk2() function, adding
the new Identity Hint callback.

Add in new option -h to define a file containing new PSK information if
Identity Hint is matched.

examples/coap-server.c:

Update code to use the new coap_context_set_psk2() function, adding
the new Identity and SNI callbacks.

Add in new option -i to define a file containing new Pre-Shared Key
if Hint and Identity is matched.
Add in new option -s to define a file containing new PSK information if
SNI is matched.
2019-12-09 09:35:30 +00:00
Olaf Bergmann d6c25a9a07 coap-server.c: Fix warnings on 32-bit architectures
Casting an unsigned long to void * caused a compiler warning
on 32-bit architectures. This change uses unsigned int as
this has a sufficient range for the respective calculation
and introduces a union to avoid the explicit type cast.
2019-11-08 11:17:54 +01:00
Jon Shallow 8faa8c28ff Add --disable-assert support for the library
Let configure enable (the default) or disable assert() code.

configure.ac:

Add in --enable-assert (the default) / --disable-assert support in libcoap.
Previously this was always enabled unless -DNDEBUG was passed in as a compiler
option.  Now NDEBUG is set or unset as appropriate in coap_config.h
This does not affect how assert()s work within the examples etc., only in the
library.

TODO:

HAVE_ASSERT_H is now cleaned up, so remove entry.

include/coap2/coap_internal.h:

Include assert.h for the library.

include/coap2/coap_io.h:
include/coap2/net.h:
include/coap2/resource.h:
src/address.c:
src/block.c:
src/coap_debug.c:
src/encode.c:
src/mem.c:
src/option.c:
src/pdu.c:
src/subscribe.c:
src/uri.c:

Remove un-needed '#include <assert.h>'
Note: address.h and utlist.h still include assert.h as there are assert()
within the header files.

examples/client.c:
src/coap_io.c:
src/coap_session.c:
src/net.c:
src/pdu.c:
src/resource.c:
src/str.c:

Remove any '#ifndef NDEBUG' wrappers for coap_log() output as they were true
before, but may not be now.
2019-09-20 13:09:56 +01:00
Jon Shallow 9f9a197e95 Fix -Winline compiler warnings about inlining failed
This is compiler version dependent when doing static inline expansions.

examples/client.c:

Make hex2char() just static.

examples/etsi_iot_01.c:

Make coap_add_payload() just static.

include/coap2/address.h:
src/address.c:

Make coap_address_init() an actual function.

libcoap-2.map:
libcoap-2.sym:

Expose coap_address_init() as a function.

Makefile.libcoap:

Include address.c as one of the libcoap files for Contiki.

examples/lwip/Makefile:

Include address.o as one of the libcoap files for LwIP.
2019-09-05 13:54:05 +01:00
Jon Shallow 2900f72a64 Make coap_subscription_t and coap_endpoint_t opaque to applications
include/coap2/coap_subscribe_internal.h and
include/coap2/coap_session_internal.h contain the information not exposed
to applications.

This internal information was moved out of the respective
include/coap2/subscribe.h and include/coap2/coap_session.h files comprising of
the coap_subscription_t and coap_endpoint_t definitions.

To ease inter .h file dependencies, a new file
include/coap2/coap_forward_decls.h is added to include forward declarations
for both libcoap and applications.

Makefile.am:

Include include/coap2/coap_*internal.h as files for distribution, but not
installed for applications to use.
Include include/coap2/coap_forward_decls.h, so it is installed for
applications.

include/coap2/coap.h.in:

Include "coap_forward_decls.h"

include/coap2/coap_forward_decls.h: (New)

Forward declare structures and typedef the structures defined in
coap_internal.h.

include/coap2/coap_internal.h: (New)

Includes all the required #include files that libcoap internally needs.

include/coap2/coap_session_internal.h: (New)

Include the coap_endpoint_t information taken from
include/coap2/coap_session.h.

include/coap2/coap_session.h:

Remove coap_endpoint_t definition.

include/coap2/lwippools.h:

Add in coap_forward_decls.h and coap_internal.h to handle the LwIP memory
allocations.

include/coap2/resource.h:

Add in "block.h" to allow things to build.

include/coap2/coap_subscribe_internal.h: (New)

Include the coap_subscription_t information taken from
include/coap2/subscribe.h.

include/coap2/subscribe.h:

Remove coap_subscription_t definition.

libcoap-2.map:
libcoap-2.sym:

Updated, removing coap_subscription_init().

src/address.c:
src/asyn.c:
src/block.c:
src/coap_debug.c:
src/coap_event.c:
src/coap_gnutls.c:
src/coap_hashkey.c:
src/coap_io.c:
src/coap_io_lwip.c:
src/coap_notls.c:
src/coap_openssl.c:
src/coap_session.c:
src/coap_time.c:
src/coap_tinydtls.c:
src/encode.c:
src/mem.c:
src/net.c:
src/option.c:
src/pdu.c:
src/resource.c:
src/str.c:
src/subscribe.c:
src/uri.c:

Add in '#include "coap_internal.h"' to the source files.
Remove other libcoap #include files.

win32/libcoap.vcxproj:
win32/libcoap.vcxproj.filters:

Update for Windows build (not tested).

examples/contiki/server.c:
src/coap_io.c:

Remove knowledge of endpoint from the server and update Contiki's
version of coap_run_once() to do the work instead.

====
Other structures (plus some internal required #defines) will follow as
separate PRs.
2019-09-03 11:59:07 +01:00
obgm 34c5e52029 Merge pull request #347 from mrdeep1/epoll
Add in internal epoll support to expose a monitor-able file descriptor
2019-08-30 14:34:56 +02:00
Jon Shallow 0e02d751da Add in internal epoll support to expose a monitor-able file descriptor
If epoll is available, use it within libcoap to speed things up, as well as
present to applications a file descriptor that can be used in a select() or
returned as an event in a epoll_wait() call.

Whenever coap_new_context() is called. set up a new epoll infrastructure with
epoll_create() and destroy it with coap_free_context().

For each new endpoint or session, add the socket fd to epoll.  When the socket
is closed, remove the socket fd from epoll.

Whenever a socket wants a write, the appropriate epoll event gets EPOLLOUT
added and when the write happens reset the event back to just EPOLLIN.

As there may be a packet retry event sometime in the future, a timerfd is
created and added to the epoll structure (done in coap_new_context) which gets
freed off in coap_free_context. If coap_run_once() determines that there is
a future retry, timerfd is informed of when the event is to occur.  When
timerfd times out, the application file descriptor will indicate a read is
available.

A new function coap_context_get_coap_fd(ctx) returns the application file
descriptor.  The only other API change is that if wait_ms is set to 1 for
coap_run_once(ctx, wait_ms), there is no waiting for a new packet to arrive.

coap_run_once() is called, whether epoll support is available or not, to
process all the underlying required i/o.

If there is no epoll support, coap_run_once() continues to use the old
select() method, otherwise it will use epoll_wait(). If there is epoll
support, a modified version of coap_read() (coap_io_do_events()) is
used instead.

Makefile.am:

Do not expose coap_epoll_ctl_mod() and coap_io_do_events() to applications.

configure.ac:

Test for the presence of sys/epoll.h and sys/timerfd.h and if so setup
COAP_EPOLL_SUPPORT to be used for #ifdef testing.
Include new man page coap_io.txt.

examples/coap-server.c:

Include support for using this new exposed file descriptor.

include/coap2/coap_io.h:

Define COAP_MAX_EPOLL_EVENTS (to limit the number of events processed at once)
and include coap_endpoint_t and coap_session_t in coap_socket_t.
Include (internal) definition for coap_epoll_ctl_mod()

include/coap2/net.h:

Add epfd and eptimerfd to coap_context_t.
Define coap_context_get_coap_fd() for applications to use.
Include (internal) definition for coap_io_do_events().
Define COAP_RUN_BLOCK and COAP_RUN_NONBLOCK.

libcoap-2.map:
libcoap-2.sym:

Include coap_context_get_coap_fd.

configure.ac:
man/Makefile.am:
man/coap.txt.in:
man/coap_io.txt.in: (New)

Include the new coap_in(3) man page that documents coap_run_once() and
coap_context_get_coap_fd().

src/coap_io.c:
src/coap_openssl.c:
src/coap_session.c:
src/net.c:

Add in the epoll support.

src/net.c:

Add in coap_context_get_coap_fd() function.
2019-08-30 11:51:15 +01:00
Jon Shallow 5d32ffe920 str.h: Fix coap_make_str_const() for C++
As pointed out in issue #368 [1], C++ officially forbids aggregate
initialization for non-aggegrates. This change therefore introduces
an alternative version (which works for both C and C++) to provide
the required functionality.

str.c:
str.h:

Make coap_make_str_const() a function.

examples/coap-server.c:

Fix parameter type in coap_add_attr() call.

src/resource.c:

Static variables can no longer make use of coap_make_str_const().

libcoap-2.map:
libcoap-2.sym:

Make coap_make_str_const() appication visible.

Fixes #368.

[1] https://github.com/obgm/libcoap/issues/368

Update
2019-08-30 10:29:17 +01:00
Jon Shallow 003b64ab3f Fix SIGPIPE causing unexpected exit of example applications
examples/client.c:
examples/coap-rd.c:
examples/coap-server.c:
examples/etsi_iot_01.c:
examples/tiny.c:

Make the applications ignore SIGPIPE.

examples/coap-rd.c:

Support clean exit for SIGTERM / SIGQUIT for valgrind testing.
2019-08-29 12:58:15 +01:00
Ken Bannister f76e5d9062 Client example show payload on block1 completion 2019-08-25 08:29:24 -04:00
obgm 88e81f59c2 Merge pull request #380 from mrdeep1/0hex_doc
Update documentation for coap-client -O option
2019-07-26 15:25:25 +02:00
Olaf Bergmann 3ec6dffbf1 coap-server.c: Fix memory leak in hnd_put()
hnd_put() creates a new string object for the request's URI path.
When the permittwed number of dynamic resources was exceeded, this
object must be released manually.

This fixed CID 1483553 reported by Coverity scan.
2019-07-26 12:45:59 +02:00
Jon Shallow c46e6a6748 Update documentation for coap-client -O option
examples/client.c
man/coap-client.txt.in

Update information for the -O option to indicate that a leading 0x causes
the hex text to be included as binary data.
2019-07-26 12:17:53 +02:00
obgm 4f91bce186 Merge pull request #357 from mrdeep1/icmp_unreachable
Split COAP_NACK_RST into COAP_NACK_RST and COAP_NACK_ICMP_ISSUE
2019-07-25 13:41:49 +02:00
obgm b1266898c6 Merge pull request #378 from mrdeep1/session_lookup
Make sessions list hashed to make lookups faster
2019-07-25 11:14:53 +02:00
Jon Shallow f06172d71e Make sessions list hashed to make lookups faster
Make endpoint->sessions and context->sessions a hashed chain of sessions
based on a key comprising of local and remote addresses and ports instead of
a linked list that has to be linearly searched.

include/coap2/coap_io.h:
include/coap2/coap_session.h:

Create a new struct coap_addr_tuple_t for the local and remote addresses/ports
to be used as the hash key.  Update coap_packet_t and coap_session_t
appropriately.

Create SESSIONS_* macros to Add, Delete, Find and Iterate through the hashed
chain.

examples/coap-rd.c:
src/coap_io.c:
src/coap_io_lwip.c:
src/coap_openssl.c:
src/coap_session.c:
src/coap_tinydtls.c:
src/net.c:
src/resource.c:

Update with the new struct element names and replace the LL_* functions with
SESSIONS_* as appropriate.

Update coap_endpoint_get_session() to use SESSIONS_FIND() for fast lookup.

Future work is to review all the SESSIONS_ITER* and see if they can be sped up
as well.
2019-07-24 14:25:38 +02:00
Olaf Bergmann c8c46f1660 client.c: Allow hex-encoded generic option values
This change enables option values to be entered as byte sequences in
hexadecimal encoding. The value string is interpreted a hex sequence
when prefixed with "0x", e.g.:

    -O 23,0x0004

Will set the option Block1 (23) to { 0x00, 0x04 }.

Fixes #377. [1]

[1] https://github.com/obgm/libcoap/issues/377
2019-07-24 13:32:46 +02:00
Jon Shallow 54d52221ba Split COAP_NACK_RST into COAP_NACK_RST and COAP_NACK_ICMP_ISSUE
As COAP_NACK_RST is used for
1. ICMP Port Unreachable message is received
2. CoAP reply RST message.
there needs to be a way to differentiate between these conditions.

If there is detected to be an ICMP response issue to sending a UDP
packet, report this as COAP_NACK_ICMP_ISSUE instead of COAP_NACK_RST.

If the reported issue to coap_session_disconnected() is COAP_NACK_ICMP_ISSUE,
then make sure that the packet (plain or DTLS) gets re-transmitted.

As hinted in #131 discussion, retry of the packet should be attempted,
(as per https://tools.ietf.org/html/rfc7252#section-4.2 ICMP Unreachable)
this fix will cause the plain packet to get retransmitted up to the maximum
retry count if type CON.

Likewise, DTLS will get restarted up to the maximum retry count if type CON.

examples/client.c:

Include COAP_NACK_ICMP_ISSUE in the nack_handler.

include/coap2/coap_io.h:

Add in COAP_NACK_ICMP_ISSUE to coap_nack_reason_t.

src/coap_session.c:

Update coap_session_disconnected() so that plain/DTLS can restart.

src/net.c:

Fix coap_wait_ack() to exponentially back off the re-transmit times.

Update coap_read_session() to report COAP_NACK_ICMP_ISSUE instead of
COAP_NACK_RST.

man/coap_handler.txt.in:

Update documentation
2019-07-19 12:35:49 +02:00