Commit Graph
1675 Commits
Author SHA1 Message Date
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
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 ff8f8d2c6b Stop valgrind errors when using OpenSSL 1.1.1c
Issue introduced by OpenSSL 1.1.1c causes valgrind errors to be reported.  See
https://www.mail-archive.com/openssl-commits@openssl.org/msg21701.html and
https://github.com/openssl/openssl/pull/8606 which did not make 1.1.1c

scripts/build.sh:
tests/valgrind_suppression:

Temporarily suppress valgrind errors when SSL_CTS_new() is called when running
the tests.

Note: This suppression will also need to be applied when running valgrind
against other CoAP executables using OpenSSL 1.1.1c to prevent the valgrind
errors getting reported.

src/coap_openssl.c:

memset cookie_secret to 0 to stop valgrind warning.
2019-06-25 20:26:43 +01:00
obgm d7eab5f22d Merge pull request #364 from mrdeep1/coap_deprecated
Fix COAP_DEPRECATED for Windows C++ builds
2019-06-25 11:11:19 +02:00
Jon Shallow 21f16515b5 Add SIZE1 Option when sending BLOCK1 Option in packets
As per https://tools.ietf.org/html/rfc7959#section-4
...
Implementation Notes:

   o  As a quality of implementation consideration, block-wise transfers
      for which the total size considerably exceeds the size of one
      block are expected to include size indications, whenever those can
      be provided without undue effort (preferably with the first block
      exchanged)
...

examples/client.c:

Include SIZE1 Option when sending data using BLOCK1 Option.
2019-06-24 16:21:30 +01: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
Jon Shallow d84ac69b67 Fix final data size in last BLOCK2 response for .well-known/core
src/net.c:

Corect the length for the final block size.

Add in an ETAG and SIZE2 option into the response as suggested by RFC7959.

examples/client.c:

Warn if URI path is too long for internal buffer (found when testing that
a large WellKnown response triggered sending BLOCK2 correctly).

Increase internal buffersize to allow longer URIs.

To reproduce issue

coap-client -b 256 coap://127.0.0.1/.well-known/core

As reported by Zafi Ramarosandratana.  See #360 which this fixes.
2019-06-20 12:35:57 +01:00
Olaf Bergmann a5ffdf323d coap-server.c: Fix NULL pointer dereferencing in hnd_get()
As the field value in struct dynamic_resource_t may be NULL,
hnd_get() must check before accessing resource_entry->value->length
and resource_entry->value->s.

This bug was detected by Andy Ryan doing a GET after a PUT with
an empty payload to unknown_put/something.
2019-06-06 10:12:34 +02:00
LewixLiu b511257724 Remove endpoint from context list when free 2019-06-04 23:14:34 +08:00
LewixLiu 80b2bef1b1 Correct the copy address direction 2019-06-04 23:14:29 +08:00
obgm e7349da198 Merge pull request #348 from mrdeep1/message_id
Remove unused message_id from coap_context_t and coap_async_t
2019-06-04 14:34:48 +02:00
obgm b1cd797269 Merge pull request #339 from mrdeep1/client_tcp_b_issue
coap-client: Fix using -b NN with TCP connections
2019-06-04 14:33:01 +02:00
obgm 1a5f75e3c8 Merge pull request #356 from mrdeep1/no_listening_server
Fix handling ICMP Port Unreachable messages
2019-06-04 14:26:43 +02:00
Jon Shallow b1ebb503b4 Fix handling ICMP Port Unreachable messages
Once a ICMP Port Unreachable message is received, the session is
disconnected by a call to coap_session_disconnect().  However if the session
is of type CON,  the logic currently tries to retransmit the packet, but
nothing gets sent out.

src/coap_session.c:

Make sure that any entries on the context->sendqueue for the disconnected
session are removed.

examples/client.c:

Add in a Nack handler so that the client immediately exits on the session
disconnect.  Additionally this also tidies up the exit after transmission
retries.
2019-06-03 20:58:08 +01:00
Jon Shallow 388fe8ff31 Rename get_common_name_from_cert() to get_san_or_cn_from_cert()
src/coap_openssl.c:

Give the function that abstracts the Subject Alternative Name (SAN),
or the CN= if SAN is not available, a better descriptive name to prevent
possible confusion.
2019-06-03 16:07:39 +01:00