Commit Graph
281 Commits
Author SHA1 Message Date
Olaf Bergmann 842c2467c0 address.[hc]: Add convenience functions to get/set port
The layout of struct coap_address_t is platform-dependent. This
change introduces getter/setter functions to retrieve and modify
the port component:

   uint16_t coap_address_get_port(const coap_address_t *addr)
   void coap_address_set_port(coap_address_t *addr, uint16_t port)

The respective port is given in host byte order and will be
converted to/from network byte order automatically.
2020-04-24 20:16:10 +01:00
Olaf Bergmann 5d930d8683 Makefile.am: Added new file coap_riot.h
The new header file is required by RIOT applications to execute
thread-specific startup code.
2020-04-24 20:16:10 +01:00
Olaf Bergmann 7949440ee0 [RIOT] coap_io.c: replace coap_network_read()
A RIOT-specific version of the function coap_network_read() is
now included in the new file coap_io_riot.c. This function requires
the received gnrc_pktsnip_t structure to be passed with the
coap_socket_t structure.
2020-04-24 20:16:10 +01:00
Olaf Bergmann f510b86573 coap_time.h: RIOT support based on xtimer 2020-04-24 20:16:10 +01:00
obgm 17957e1e68 Merge pull request #430 from gocarlos/develop
add cmake support to libcoap
2020-04-24 20:42:04 +02:00
Carlos Gomes Martinho e191e2d726 build: add cmake support 2020-04-22 17:07:24 +02: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 c3be323749 pdu.h: Remove unneeded backward compatible #defines from 2011
The # COAP_RESPONSE_nnn entries were introduced to handle a response change
introduced by draft-ietf-core-coap-04 in 2011.  These are no longer used
and can cause confusion - e.g.
 define COAP_RESPONSE_304      COAP_RESPONSE_CODE(203)  /* 2.03 Valid */
as raised in #456.  Entries now removed.
2020-02-21 17:27:48 +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
Jon Shallow 15f9cf568a coap_openssl.c: OpenSSL < 1.1.1 fails to build with --disable-tcp
Missing COAP_DISABLE_TCP wrapper in OpenSSL 1.1.0 - 1.1.1 specific code

doc/main.md:

Update Copyright year.

include/coap2/coap_tcp_internal.h:

Update documentation for the internal TCP functions

src/coap_session.c
src/coap_tinydtls.c

Delete trailing space
2020-02-04 10:30:16 +00:00
Jon Shallow e9cffe9b91 Make COAP_REQUEST_* enum for clarity
Make the COAP_REQUEST_* #defines enum coap_request_t for clarity.

This should help prevent mistakes such as in #448.
2020-02-03 09:33:15 +00:00
Jon Shallow c237b15b16 Correct Disable TCP for TLS and Windows
Makefile.am:

include coap_tcp_internal.h in EXTRA_DIST

libcoap-2.map
libcoap-2.sym:

Don't expose previously static coap_socket_format_errno().

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

Correct ENABLE_TCP (never defined) to !COAP_DISABLE_TCP.
Conditionally compile in TLS code as appropriate.

src/coap_io.c:

Correct #endif description.

src/coap_session.c:

Conditionally compile in TLS code as appropriate.

src/coap_tcp.c

Merge in appropriate parts of https://github.com/obgm/libcoap/pull/396 and
https://github.com/obgm/libcoap/pull/345 fixes.

include/coap2/coap_tcp.h:
include/coap2/coap_tcp_internal.h:

Rename include/coap2/coap_tcp.h to include/coap2/coap_tcp_internal.h and
remove reference to coap_tcp_is_supported().

include/coap2/coap.h.in:

Remove reference to coap_tcp.h.

coap_config.h.windows:

Include #define COAP_DISABLE_TCP 0.

include/coap2/coap.h.windows:

Reorder #includes

include/coap2/coap_internal.h:

Add #include coap_tcp_internal.h

include/coap2/coap_io.h:

Add in definition of coap_tcp_is_supported().

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

Include src/coap_tcp.c and include/coap2/coap_tcp_internal.h.
2020-01-16 15:17:10 +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 f601ddbf78 Add in MbedTLS support
Add in MbedTLS library support as another alternative TLS library

Makefile.am:

Include new src/coap_mbedtls.c file.

configure.ac:

Add in --with-mbedtls support which generates HAVE_MBEDTLS in coap_config.h.

include/coap2/coap_dtls.h:

Add in TLS Library type COAP_TLS_LIBRARY_MBEDTLS and make COAP_TLS_LIBRARY_*
defines an enum.
Add in PKI Key type COAP_PKI_KEY_PEM_BUF for passing in PEM information
that is held in memory instead of in a file.
Add in #define COAP_DTLS_RETRANSMIT_MS, COAP_DTLS_RETRANSMIT_TOTAL_MS
and COAP_DTLS_RETRANSMIT_COAP_TICKS

src/coap_debug.c:

Output MbedTLS information if appropriate.

src/coap_mbedtls.c: (New)

Contains the MbedTLS interface code.

src/coap_notls.c:

Do not use code if MbedTLS is being built.

tests/test_tls.c:

Include MbedTLS library checks.

README.md:

Include MbedTLS as a valid (D)TLS library.
2020-01-16 09:38:43 +00:00
Jon Shallow d2f7b4f23a observe: Add in support for all observe responses to be non-confirmable
https://tools.ietf.org/html/rfc7641#section-4.5 requires that non-confirmable
observe responses should be sent as confirmable at least once every 24 hours.

However there is a requirement to override this periodical confirmable sending
behavior in lossy under DDoS attack type environments.

https://tools.ietf.org/html/draft-ietf-dots-signal-channel-41#section-4.4.2.1

Add in support for, and document, a new coap_resource_init() flag
COAP_RESOURCE_FLAGS_NOTIFY_NON_ALWAYS to handle this non RFC7641 requirement.
2020-01-13 13:14:25 +00:00
obgm 03cc5791c5 Merge pull request #417 from mrdeep1/coap_run_once_fds
coap_io.c: Add in additional FD checking support for coap_run_once()
2019-12-20 17:47:35 +01:00
Jon Shallow 48cc799d42 Update DTLS interface modules to use the parameters in _psk2() functions
include/coap2/coap_dtls.h:
include/coap2/coap_session.h:

Update coap_dtls_cpsk_key_t and coap_dtls_spsk_key_t to use coap_bin_const_t.
Update coap_dtls_id_callback_t to return coap_bin_const_t*.
Update coap_session_t to use coap_bin_const_t for psk_identity, psk_hint
and psk_key.
Update coap_session_refresh_psk_key() to have a coap_bin_const_t parameter.

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

Add in new callback functions to handle SNI, Identity and Hint request
information following the principles used for the PKI logic.

Install the appropriate callback functions by setting them up by using the
appropriate TLS library functions.

src/coap_session.c:
src/net.c:

Use the new versions of coap_dtls_cpsk_key_t and coap_dtls_spsk_key_t.

src/coap_session.c:

Add in new coap_session_refresh_psk_hint() function.
Update coap_session_refresh_psk_key() function.
2019-12-05 21:48:44 +00:00
Jon Shallow 23b3e6e17e Add in coap_new_client_session_psk2 and coap_context_set_psk2 functions
This fix is in preparation for the full coap_new_client_session_psk2()
and coap_context_set_psk2() functions.  Structures are updated with the
necessary information, as well as provide the high level support.

As new sessions are now created for every genuine incoming session (#333),
coap_context_t will only hold the initial information provided by
coap_context_set_psk2() (server mode), but if there is any updated
(triggered by SNI or Indentity), this will be kept in coap_session_t.

Likewise, the client initial information will be kept in the
coap_session_t, and if modified (triggered by Hint) will be kept separately
in coap_session_t.

This change does not include the support required in the DTLS interface modules
other than changes required to make the code compilable and still run the
existing examples - so it is a standalone change.

Makefile.am:

Make coap_dtls_context_set_cpsk, coap_dtls_context_set_spsk,
coap_session_refresh_psk_key and coap_session_refresh_psk_hint hidden
Remove coap_dtls_context_set_psk

include/coap2/*.h:

Replace call-back with callback for consistency

include/coap2/coap_dtls.h:

Include coap_dtls_cpsk_key_t and coap_dtls_spsk_key_t definitions
Define coap_dtls_spsk_key_t for the new coap_*_psk2 functions.
Include all the new call back function definitions

include/coap2/coap_session.h:

Update coap_session_t with the new PSK information
Define coap_new_client_session_psk2(), coap_session_refresh_psk_key() and
coap_session_refresh_psk_hint() functions

include/coap2/net.h:

Update coap_context_t with the new PSK information
Define the coap_context_set_psk2() function

include/coap2/str.h:

Add in a new struct coap_bin_const_t
Define new functions coap_new_bin_const(), coap_delete_bin_const()
and coap_binary_equal().

libcoap-2.{map|sym}

Expose the new coap_context_set_psk2, coap_new_client_session_psk2,
coap_new_bin_const and coap_delete_bin_const functions

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

Replace coap_dtls_context_set_psk() with coap_dtls_context_set_spsk()
and coap_dtls_context_set_cpsk()

src/coap_openssl.c:

Replace coap_dtls_context_set_psk() with coap_dtls_context_set_spsk()
and coap_dtls_context_set_cpsk()
Update the SSL_CTX_use* and SSL_set_* functions with the new variable
locations

src/coap_session.c:

Free off new information on coap_session_t in coap_session_mfree()

Build parameters for coap_new_client_session_psk2() in
coap_new_client_session_psk() and call coap_new_client_session_psk2() instead
of coap_session_create_client().
Create new coap_new_client_session_psk2().
Create new coap_session_refresh_psk_key() (will be used when DTLS interface
code is updated

src/net.c:

Update coap_get_session_client_psk() to use the new PSK locations.
Update coap_get_session_server_psk() to use the new PSK locations.
Update coap_get_session_server_hint() to use the new PSK locations.
Build parameters for coap_context_set_psk2() in
coap_context_set_psk() and call coap_context_set_psk2().
Create new coap_context_set_psk2().
Cleanup usage of old PSK information in coap_free_context()

src/str.c:

Code for coap_new_bin_const() and coap_delete_bin_const() functions
2019-12-03 14:46:26 +00:00
Jon Shallow 02923e7ffb Update resource documentation
man/coap_resource.txt.in:

Add in coap_resource_set_userdata() and coap_resource_get_userdata()
documentation

include/coap2/resource.h:

Re-organize order and put into appropriate doxygen groups

include/coap2/subscribe.h:

Fix Doxygen title case
2019-11-28 16:19:18 +00:00
Jon Shallow 5d01cf3139 OpenSSL: Update ciphers
src/coap_openssl.c:

Remove NULL as a valid cipher from the cipher list.

Add in TLSv1.3 ciphers if OpenSSL 1.1.1 (they are already included in
TLSv1.2 definition, but this is to document this is happening).

Remove the TLSv1.0 deprecated ciphers.

Cipher strings can now be overridden during compilation by
using -DCOAP_OPENSSL_CIPHERS=xxx compiler option where xxx is the
cipher string as defined in
https://www.openssl.org/docs/man1.1.0/man1/ciphers.html

Explicit PSK cipher strings can now be overridden during compilation
by using -DCOAP_OPENSSL_PSK_CIPHERS=xxx compiler option where xxx is the
cipher string.

Log the client ciphers presented, and the cipher chosen.

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

Add in COAP_LOG_CIPHERS as a debug level (value 9) which matches getting the
cipher information reported in GnuTLS, and is now used for OpenSSL.

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

Include OpenSSL for verbosity > 7.
2019-11-05 10:19:45 +00:00
Jon Shallow 039826b83e coap_io.c: Add in additional FD checking support for coap_run_once()
coap_write() and coap_read() are really internal functions that are called
from within coap_run_once() separated by a select() to wait for i/o and
ideally should not be used in applications.

However, currently, coap_write() and coap_read() are needed instead of
coap_run_once() if there is no epoll support in libcoap so that if additional
non libcoap FDs need to be set up and monitored by the select() call they
then can be processed accordingly if the non libcoap FDs are set in the
select() response.

coap_io_process_with_fds() is a new function extending coap_run_once() that
allows non libcoap FDs to be monitored in the coap_run_once() internal
select() call and can then be tested by the caller of
coap_io_process_with_fds().

coap_run_once() now calls coap_io_process_with_fds() with readfds, writefds,
exceptfds set to NULL and nfds set to 0.

Note: nfds, readfds, writefds and exceptfds are ignored if there is underlying
epoll support in libcoap as epoll functions are used, not select().  Use
coap_context_get_coap_fd() to get a single FD that covers all the libcoap
FDs that can be used in the application along with other non-libcoap FDs
when there is epoll support.  See coap_io(3) for further information.

include/coap2/net.h:
libcoap-2.map:
libcoap-2.sym:
man/coap_io.txt.in:
src/coap_io.c:

Include and document the new coap_io_process_with_fds() function.
2019-11-04 17:16:16 +00:00
obgm 0389a6c0ae Merge pull request #398 from mrdeep1/epoll_timing
Fix timing window in epoll logic
2019-11-04 17:00:51 +01:00
obgm 9b09c7dd2f Merge pull request #408 from mrdeep1/encode8
encode.c: Support encoding/decoding 8 byte options
2019-10-02 16:57:30 +02: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 851cb374e8 encode.c: Support encoding/decoding 8 byte options
Options such as COAP_OPTION_ETAG are up to 8 bytes in size.  While
being opaque, they can internally be represented as an 8 byte number.

include/coap2/encode.h:
libcoap-2.map:
libcoap-2.sym:
src/encode.c:

Create 8 byte versions coap_decode_var_bytes8() and coap_encode_var_safe8().
2019-09-18 11:55:33 +01:00
Jon Shallow 23a6daf1e5 Fix timing window in epoll logic
It is possible to miss a packet retransmission if it is just about to happen
and the coap_run_once() logic takes the time past the packet retransmission.
The retransmission will take place when coap_run_once() is next run, but that
could be significantly later.

1) Create a new coap_io_prepare_epoll() function that calls coap_write(), but
sets the etimer within this function instead of at the end of coap_run_once().

2) Handle the etimer trigger within coap_io_do_events() rather than loop
checking coap_io_do_events() within coap_run_once().

include/coap2/net.h:

Define coap_io_prepare_epoll().
Update coap_io_do_events() to be type void.

src/coap_io.c:

Add in coap_io_prepare_epoll().
Update coap_run_once() to use new/updated functions.

src/net.c:

Update coap_io_do_events().
Make coap_new_context() always fail if there is an epoll logic setup failure
libcoap compiled for epoll will not continue to run.
Give a runtime warning if coap_read() or coap_io_do_events() are called when
libcoap is compiled or not compiled respectively for using epoll.
[Both coap_read() and coap_io_do_events() need to be in libcoap-2.{map|sym} to
support when compiling with or without epoll support]

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

Make coap_io_prepare_epoll() application accessible.
2019-09-13 21:08:04 +01:00
Jon Shallow c5ab99369f Fix Windows builds
coap_config.h.windows:

Windows has struct cmsghdr so use it.
Update libcoap versions.

include/coap2/coap.h.windows:

Update libcoap versions.
Bring in line with build include/coap2/coap.h.

include/coap2/coap_session.h:

Include coap_forward_decls.h so things build correctly.

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

Add in missing include files.
2019-09-12 11:49:26 +01:00
Olaf Bergmann abec5b8228 coap_mutex.h: Use pthread mutexes if detected by configure
On systems that support pthread mutexes, stack usage can be minimized
with the use of static variables. Concurrent access is protected
on these systems with mutexes. Platforms that do not have pthread
support must map the coap_mutex_* macros accordingly.
2019-09-10 13:28:44 +02:00
Olaf Bergmann 30583fbed9 coap_session.h: Adjust whitespace 2019-09-09 17:17:49 +02:00
Jon Shallow 209c8a9267 Revert out changes to make 1 byte enums
include/coap2/coap_dtls.h
include/coap2/coap_session.h
include/coap2/pdu.h
man/coap_encryption.txt.in
src/net.c

Revert out all the #define -> enum in #386 that require the enum to be
one byte long.
2019-09-09 09:59:44 +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 49c6b57cba Convert #define to enum where possible
As suggested in #292, several lists of #define could easily be defined
as enum instead - which allows for extra compiler checking.

The challenge was for where the (#define/enum) parameter defined in a
structure was one byte in size.  Doxygen was picky and got confused, not
maintaining the fact that it was an enum if macros were used to differentiate
between using Microsoft or GCC when changing the enum size default from that
of an int.

include/coap2/coap_dtls.h:
include/coap2/coap_session.h:
include/coap2/pdu.h:

Use enums instead of #define where practical.

src/net.c:

Handle all the enum cases.

man/coap_encryption.txt.in:
man/coap_tls_library.txt.in:

Update man documentation.
2019-08-09 17:30:26 +02:00
Olaf Bergmann 3531087bb3 Revert "str.h: Fix coap_make_str_const() for C++"
This reverts commit 2309691770.

As the temporary object is cleaned up, the contents pointed to
by the coap_str_const_t object are destroyed.
2019-08-02 15:47:56 +02:00
Olaf Bergmann 2309691770 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
a template function coap_make_str_const() that creates and initializes
an automatic variable that is moved to the caller on exit.

Note that at least -std=c++14 is required.

Fixes #368.

[1] https://github.com/obgm/libcoap/issues/368
2019-08-02 13:29:42 +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
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
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
Jon Shallow 55aeaa6cc1 Update Keepalive support to include unreliable sessions
Add in support for both reliable and unreliable sessions when calling
coap_context_set_keepalive().

include/coap2/coap_session.h:

Track the last ping mid in coap_session_t.

src/coap_io.c:
src/coap_session.c:

Support both reliable and unreliable sending of pings in coap_write().

src/net.c:

Determine whether RST packet response is associated with a CoAP Empty Ping
that was sent and handle accordingly.

configure.ac:
examples/client.c:
include/coap2/net.h:
man/Makefile.am:
man/coap-client.txt.in:
man/coap.txt.in:
man/coap_handler.txt.in:
man/coap_keepalive.txt.in: (New)
man/coap_observe.txt.in:

Add in a new man page (coap_keepalive).
Update documentation to remove references to TCP and reliable only.
2019-07-11 11:14:15 +02:00
Jon Shallow 3a6f9e8499 net.c: Start new coap_context with a random message identifier
This is done by calling prng_init() with a non-zero value in coap_startup().

Change 7fb4ace2b7 modified the parameter to
prng_init() from a computed value to 0 when the code was re-organized.

This change uses the current time in microseconds as the seed to prng_init().

include/coap2/coap_time.h:

Add in coap_ticks_to_rt_us() support for lwIP and Contiki

src/net.c:

Update call to prng_init() in coap_startup() to use time in microsecs as the
seed parameter

See #373 where the issue was raised.
2019-07-05 15:32:06 +02:00
obgm 27eec0c1c5 Merge pull request #350 from lewixliu/develop
Fixed two minor bugs in endpoint free and session address update
2019-06-28 17:11:41 +02:00
Olaf Bergmann fa00362b72 coap.h.windows: Include libcoap.h as extern C
Fixes #369.
2019-06-28 16:58:46 +02:00
obgm 5b1d753da0 Merge pull request #365 from mrdeep1/dtls_retry
Fix GnuTLS DTLS retries when Client Hello is not responded to
2019-06-28 12:19:45 +02:00
obgm 5f5ab48e34 Merge pull request #340 from mrdeep1/cancel_rst_observe
Fix RST deleting observer for non-confirmable observe
2019-06-26 14:39:25 +02:00
Jon Shallow 3a50a29ced Fix GnuTLS DTLS retries when Client Hello is not responded to
GnuTLS currently does not retry to send the Client Hello packets if there was
no (Server Hello) response.  This was never correctly implemented.

src/coap_gnutls.c:

Fix receive_timout() so that it differentiates between DTLS and TLS.
If DTLS, if data has already been read in, indicate this.
if this is a coap_dtls_handle_timeout() call, only check readfds
(writefds will always be true, causing confusion).

Set DTLS timeouts where appropriate.

Update coap_dtls_*_timeout() functions to return the correct values.

include/coap2/coap_dtls.h:
src/coap_gnutls.c:
src/coap_io.c:
src/coap_notls.c:
src/coap_openssl.c:
src/coap_tinydtls.c:

Add in coap_tick_t now parameter to coap_dtls_get_timeout() for better
accuracy.
2019-06-21 16:51:01 +01:00
Jon Shallow da33226631 Fix COAP_DEPRECATED for Windows C++ builds
include/coap2/libcoap.h:

Simplify COAP_DEPRECATED definition to generically handle Windows
or other builds when setting the define for COAP_DEPRECATED.

Fixes #363
2019-06-21 09:47:24 +01:00
LewixLiu 80b2bef1b1 Correct the copy address direction 2019-06-04 23:14:29 +08:00