Commit Graph
1690 Commits
Author SHA1 Message Date
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
obgm 66100dba2f Merge pull request #393 from mrdeep1/sigpipe
Fix SIGPIPE causing unexpected exit of example applications
2019-08-29 17:40:38 +02:00
obgm b2c6040d03 Merge pull request #392 from mrdeep1/handler-doc
coap_handler.txt.in: Add in clarification notes for handler parameters
2019-08-29 17:36:17 +02:00
obgm 34e9c910d1 Merge pull request #391 from mrdeep1/multicase-enforce
net.c: Enforce non-confirmable for multicast request messages
2019-08-29 17:35:31 +02:00
obgm c73214f882 Merge pull request #390 from mrdeep1/multicast-logging
net.c: Log correct address in coap_read_session()
2019-08-29 17:33:55 +02: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
obgm 16b77c3e6c Merge pull request #383 from kb2ma/develop
Client example show payload on block1 completion
2019-08-29 12:14:23 +02:00
Ken Bannister f76e5d9062 Client example show payload on block1 completion 2019-08-25 08:29:24 -04:00
Jon Shallow 82b04b8df8 net.c: Enforce non-confirmable for multicast request messages
Enforce RFC7252 8.1 "Such multicast requests MUST be Non-confirmable"
by logging the PDU error and dropping the PDU tranamission.
2019-08-22 14:19:04 +01:00
Jon Shallow 6e3e65f128 coap_handler.txt.in: Add in clarification notes for handler parameters
Document why certain parameters may be NULL.
2019-08-22 14:15:29 +01:00
Jon Shallow ddf8691b8d net.c: Log correct address in coap_read_session()
Move the coap_log() entry to after updating the session information.

Seen when coap-client is doing multicast request sends
2019-08-22 13:07:24 +01:00
obgm bee3bb2862 Merge pull request #387 from mrdeep1/doc_ordering
Order the doxygen Modules and Manual Pages listings alphabetically
2019-08-14 21:29:28 +02:00
obgm 4a6d885889 Merge pull request #386 from mrdeep1/define_enum
Convert #define to enum where possible
2019-08-14 12:53:26 +02:00
Jon Shallow 3574d0c8b0 Order the doxygen Modules and Manual Pages listings alphabetically
doc/Doxyfile.in:

SORT_GOUPNAME = YES to sort Modules

doc/Makefile.am:

Sort the MAN_FILES list.  coap(7) first and examples last.
2019-08-09 17:40:28 +02: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
Olaf Bergmann 3c9125736e uri.c: Fix integer overflow in URI parser
In coap_split_uri(), the manual conversion of a URI port may run into
an integer overflow for values greater than UINT16_MAX (i.e., 65535).

Although the subsequent typecast into a uint16_t limits possible
damage, a wrong port may be used when exceeding the positive integer
range. This fix adds an additional check to trigger error -4 before
this happens.

Fixes OSS-Fuzz 16115. [1]

[1] https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=16115
2019-07-31 12:22:53 +02:00
obgm 3a25eb6bb7 Merge pull request #218 from bshastry/oss-fuzz
Add coap_show_pdu() and coap_split_uri() to existing/new oss fuzz test harness
2019-07-30 13:42:27 +02: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
Olaf Bergmann b79a43de30 net.c: Fix use-after-free in coap_cancel()
When a subscription is canceled, coap_cancel() iterates through the
resources and deletes the observer. In addition, all outstanding
messages for this observer are removed from the send queue.

With commit 5b1a8f67, the session object associated with the deleted
observer may be automatically released by coap_delete_observer().
Therefore, coap_cancel_all_messages() must be invoked on the session
before deletion.

Fixes CID 1484386 found by Coverity scan.
2019-07-26 10:36:32 +02:00
obgm f2ce60e185 Merge pull request #355 from mrdeep1/fix_common_name
Rename get_common_name_from_cert() to get_san_or_cn_from_cert()
2019-07-25 13:45:43 +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
obgm fe4ee0b527 Merge pull request #379 from mrdeep1/dtls_close_event
Report event COAP_EVENT_DTLS_CLOSED when idle (D)TLS session times out
2019-07-25 11:07:11 +02:00
Jon Shallow a27ec8b3d1 Report event COAP_EVENT_DTLS_CLOSED when idle (D)TLS session times out
When an idle server (D)TLS session times out, no event is reported to a
defined event handler.  COAP_EVENT_DTLS_CLOSED is reported whenever there is
a read / write issue, and this fix also reports the event COAP_EVENT_DTLS_CLOSED
when the (D)TLS session is closed.

Some extra code had to be added in to prevent event COAP_EVENT_DTLS_CLOSED
being reported twice.

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

Report event COAP_EVENT_DTLS_CLOSED when coap_dtls_free_session() and
coap_tls_free_session() are called.

src/coap_session.c:

Do not log spurious (D)TLS Alert packets received after session is closed.
2019-07-24 14:36:20 +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
obgm f1a5435193 Merge pull request #376 from mrdeep1/ping_pong
Update Keepalive support to include unreliable sessions
2019-07-11 15:51:07 +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
obgm cfe83f19dc Merge pull request #374 from mrdeep1/fix_prng_init
net.c: Start new coap_context with a random message identifier
2019-07-09 10:38:49 +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 98954eb30a Merge pull request #375 from mrdeep1/fix_coap_ticks
coap_time.c: Make coap_ticks() more accurate than to the nearest second
2019-07-05 10:12:24 +02:00
Jon Shallow 0aa8cde1ad coap_time.c: Make coap_ticks() more accurate than to the nearest second
With
define Q(frac,fval) ((coap_tick_t)(((1 << (frac)) * (fval))))
and
tmp = SHR_FP(tv.tv_nsec * Q(FRAC, (COAP_TICKS_PER_SECOND/1000000000.0)), FRAC);

The Q function always returns 0 in error - e.g.

(gdb) p ((coap_tick_t)(((1 << (FRAC))*((COAP_TICKS_PER_SECOND/1000000000.0)))))
$6 = 0
(gdb) p ((((1 << (FRAC))*((COAP_TICKS_PER_SECOND/1000000000.0)))))
$7 = 0.001024

whereas we need the value 0.001024 to multiple with tv_nsec.

This change fixes this by moving the coap_tick_t from Q to SHR_FP.

Found when debugging #374
2019-07-04 14:44:59 +01:00
obgm aff8681b9b Merge pull request #370 from mrdeep1/silent_compile
configure.ac: Enable silent compiles by default
2019-07-02 09:52:22 +02:00
Olaf Bergmann d873b1f752 scripts/build.sh: Disable silent rules by default
As discussed in PR #370 [1], silent rules are disabled for travis
builds.

[1] https://github.com/obgm/libcoap/pull/370
2019-07-02 09:33:38 +02:00
Jon Shallow 8e2acba4f2 configure.ac: Enable silent compiles by default
Add in option so default is to only output the compile line as in

  CC     src/libcoap_2-openssl_la-coap_event.lo

instead of

/bin/sh ./libtool  --tag=CC   --mode=compile gcc -DHAVE_CONFIG_H -I.    -fPIC
 -fPIE -I./include/coap2 -I./include/coap2 -pedantic -Wall -Wcast-qual -Wextra
 -Wformat-security -Winline -Wmissing-declarations -Wmissing-prototypes
 -Wnested-externs -Wpointer-arith -Wshadow -Wstrict-prototypes -Wswitch-default
 -Wswitch-enum -Wunused -Wwrite-strings  -Wlogical-op -I/opt/openssl/include
 -std=c99 -ggdb3 -DCOAP_CONSTRAINED_STACK -DDTLS_CONSTRAINED_STACK
 -D_GNU_SOURCE -MT src/libcoap_2-openssl_la-coap_event.lo -MD -MP
 -MF src/.deps/libcoap_2-openssl_la-coap_event.Tpo -c
 -o src/libcoap_2-openssl_la-coap_event.lo `test -f 'src/coap_event.c' ||
 echo './'`src/coap_event.c
libtool: compile:  gcc -DHAVE_CONFIG_H -I. -fPIC -I./include/coap2
 -I./include/coap2 -pedantic -Wall -Wcast-qual -Wextra -Wformat-security
 -Winline -Wmissing-declarations -Wmissing-prototypes -Wnested-externs
 -Wpointer-arith -Wshadow -Wstrict-prototypes -Wswitch-default -Wswitch-enum
 -Wunused -Wwrite-strings -Wlogical-op -I/opt/openssl/include -std=c99 -ggdb3
 -DCOAP_CONSTRAINED_STACK -DDTLS_CONSTRAINED_STACK -D_GNU_SOURCE
 -MT src/libcoap_2-openssl_la-coap_event.lo -MD -MP
 -MF src/.deps/libcoap_2-openssl_la-coap_event.Tpo -c src/coap_event.c -fPIE
 -o src/libcoap_2-openssl_la-coap_event.o
mv -f src/.deps/libcoap_2-openssl_la-coap_event.Tpo
 src/.deps/libcoap_2-openssl_la-coap_event.Plo

making it much easier to find compile time warnings.

Old version of output can still be obtained with

 make V=1

or

 ./configure --disable-silent-rules
2019-07-01 10:13:32 +01: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 465410cb93 Merge pull request #359 from mrdeep1/observe_trigger
Make coap-server only trigger any observers when dynamic resource is updated
2019-06-28 13:09:33 +02:00
Jon Shallow 453f46b543 coap-server.c: Trigger observers only when dynamic resource is updated
examples/coap-server.c:

Only call coap_resource_notify() whenever a PUT is done to a created dynamic
resource, instead of once a second.

Issue found by Andy Ryan.
2019-06-28 11:54:36 +01: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 0b75251acd Merge pull request #349 from mrdeep1/gnutls_mem_leek
Fix memory leak in GnuTLS client that does not have security defined
2019-06-27 18:19:39 +02:00
Jon Shallow cc63387dad Fix memory leak in GnuTLS client that does not have security defined
If a CoAP client using GnuTLS does not have PSK or PKI security defined
but communicatioes with coaps:// with a CoAP server that has PKI defined,
then there is a memory leak when the session is closed down.

src/coap_gnutls.c:

Free off PKI credentials if a client and PSK or PKI was not explicitly
defined in coap_dtls_free_gnutls_env().
2019-06-27 14:52:58 +01:00
obgm d1642c3e68 Merge pull request #361 from mrdeep1/block2_well-known_core
Fix final data size in last BLOCK2 response for .well-known/core
2019-06-27 15:32:36 +02:00
obgm 39b2505a2e Merge pull request #367 from mrdeep1/fix_openssl_1_1_1c
Stop valgrind errors when using OpenSSL 1.1.1c
2019-06-27 14:07:34 +02:00
obgm c4aa140127 Merge pull request #366 from mrdeep1/size1_addition
Add SIZE1 Option when sending BLOCK1 Option in packets in Client
2019-06-27 13:59:30 +02:00