Commit Graph
435 Commits
Author SHA1 Message Date
Jon Shallow b694c4f5e5 coap-rd: Fix coap_str_const_t deallocate errors
Fix calls to coap_resource_init() and coap_add_attr() where proper
coap_str_const_t * were not being passed in that were safe to free off.
2021-05-20 13:28:44 +01:00
Jon Shallow fb40a231a4 coap-rd: Remove requirement for uthash.h
Add rd_t information as user data to the resource (which has hashed lookup)
rather than maintaining a separate hash within coap-rd.
2021-05-20 13:24:49 +01:00
obgm 46c4315e17 Merge pull request #678 from mrdeep1/pkcs11_logging
TLS Support: Move common TLS support descriptions into libcoap
2021-05-20 13:50:26 +02:00
Jon Shallow c987bee475 RPK: Support verification of peer certificate
Implement CN and SNI call-back checks for GnuTLS.  The validate_cn_call_back
function provides "RPK" for the common name and the ASN1 of the peers
certificate.

Update coap-server and coap-client to enable validate_cn_call_back and
validate_sni_call_back for RPK.
2021-05-19 13:27:09 +01:00
Jon Shallow aaf4d874d4 TLS Support: Move common TLS support descriptions into libcoap
Added new function coap_string_tls_support() for outputting what key types the
TLS library supports to remove duplicated code out of the examples.
2021-05-12 18:02:37 +01:00
Jon Shallow feb8c15b45 examples: Provide standalone source and simple Makefile for examples
Install in PREFIX/share/libcoap/examples the source for coap-client.c and
coap-server.c with a standalone Makefile for ease of standalone code
development.

Add in configure option --enable-examples-source.

Included is a README, LICENSE and COPYING.
2021-05-11 14:35:23 +01:00
obgm 832477845f Merge pull request #668 from mrdeep1/truncation
examples: Fix truncated usage() output
2021-04-29 15:10:55 +02:00
Olaf Bergmann f6eede8077 examples/Makefile.am: fix include path
Example applications are built against the libcoap public API
and therefore prefix libcoap includes with "coap3)/".

The only exception is coap-rd which uses uthash.h from the internal
API. Therefore, "-Iinclude/coap3" is additionally passed as CPPFLAG
for coap-rd.
2021-04-29 13:16:10 +02:00
Olaf Bergmann aa09d1fcfe coap-rd.c: Update file name in doxygen comment 2021-04-29 12:41:33 +02:00
Olaf Bergmann e31de3b13a coap-rd.c: include uthash.h
The coap-rd example uses uthash.h from the libcoap core. As uthash.h
is not part of the public libcoap API, it must be included explicitly.
2021-04-29 12:41:33 +02:00
Jon Shallow dbaf580e75 examples: Fix truncated usage() output
Increase scratch buffer size (needed for Mbed TLS)
2021-04-29 11:39:02 +01:00
Jon Shallow 5bcd89da01 handlers: Make parameters const where appropriate
Also remove the unneeded parameters which can easily be derived - e.g context
and token.
2021-04-22 07:18:47 +01:00
Olaf Bergmann 3f7ba0afc8 Bump API version to 3, release candidate 2 2021-04-21 14:38:54 +02:00
Olaf Bergmann 63f1369269 tiny.c: Update request method to COAP_REQUEST_CODE_POST 2021-04-21 13:45:56 +02:00
obgm 04c641f7f0 Merge pull request #639 from mrdeep1/obf_pdu_t
coap_pdu_t: Obfuscate coap_pdu_t for applications
2021-04-20 15:22:08 +02:00
Jon Shallow e4e067e5fb coap_pdu_t: Obfuscate coap_pdu_t for applications
Move coap_pdu_t to coap_pdu_internal.h.

Move internal PDU based function definitions to coap_pdu_internal.h.

Replace most references to "struct coap_pdu_t" with "coap_pdu_t".

Create new enum coap_pdu_type_t, enum coap_pdu_code_t and coap_option_num_t,
using them where appropriate for clarity of documentation.

Provide new functions to allow applications to access/update parts of
coap_pdu_t as appropriate and document as appropriate.

Update examples to use these new access functions where appropriate.
2021-04-20 10:31:14 +01:00
Olaf Bergmann fd5ecf5697 Add in configuration option for async (separate responses)
The new configuration option --disable-async now allows to switch
of support for asynchronous operations.

The new function coap_async_is_supported() can be used to check
if the library was built with async support.
2021-04-20 10:36:48 +02:00
Jon Shallow e008d905fd Multicast: Add in response delay
See https://tools.ietf.org/html/rfc7252#section-8.2

Also updated coap_find_async() to search on token instead of mid as
mid is not used in reliable transports.
2021-04-19 11:09:19 +01:00
Jon Shallow dc1ca3c001 coap_async_state_t: Obfuscate coap_async_state_t for applications
Move coap_async_state_t to coap_async_internal.h and rename to coap_async_t
as it no longer has any state.

Refactor the async logic to simplify usage and provide libcoap based delay
functionality support for request/response traffic such as multicast.  Let
libcoap decide when the delayed response is to be sent.

Move internal async based function definitions to coap_async_internal.h.

Replace most references to "struct coap_async_state_t" with
"coap_async_t".

Provide new functions to allow applications to access parts of
coap_async_t as appropriate.

Update examples to use these new access functions where appropriate.

Add in new man page.
2021-04-16 14:29:52 +01:00
Jon Shallow f8d9af4b93 coap_context_t: Obfuscate coap_context_t for applications
Move coap_context_t and coap_queue_t to coap_net_internal.h.

Move internal context and queue based function definitions to
coap_net_internal.h.

Replace most references to "struct coap_context_t" with "coap_context_t".

Provide new functions to allow applications to access parts of coap_context_t
as appropriate and document these functions.

Update examples to use these new access functions where appropriate.

For the man documentation, move the server session setup functions to (new)
coap_endpoint_server(3) and update coap_context(3) with all the context access
functions as these are common to both the client and server.
2021-04-15 17:27:02 +01:00
Jon Shallow 0c3928f536 coap_session_t: Obfuscate coap_session_t for applications
Move coap_session_t and coap_addr_hash_t to coap_session_internal.h.

Move internal session based function definitions to coap_session_internal.h.

Replace most references to "struct coap_session_t" with "coap_session_t".

Provide new functions to allow applications to access parts of coap_session_t
as appropriate and document these functions.

Update examples to use these new access functions where appropriate.

For the man documentation, move the client session setup functions to (new)
coap_endpoint_client(3) and update coap_session(3) with all the session access
functions as these are common to both the client and server.
[coap_endpoint_server(3) will be created when coap_context_t is obfuscated.]
2021-04-09 13:13:46 +01:00
Olaf Bergmann 60dd7aa32a [LWIP] Makefile: Fix include path for lwippools.h
Create path for pre-processor includes from LIBCOAP_API_VERSION in
configure.ac. This avoids breaking the example after API version
change.
2021-04-08 14:32:20 +02:00
Jon Shallow db075ce659 Examples: Output runtime libcoap version in usage()
Helps to diagnose issues.
2021-04-06 14:07:09 +01:00
obgm 7906bcf2a3 Merge pull request #613 from mrdeep1/message_id
message id: clean up documentation usage of transaction which is message id
2021-03-18 13:52:10 +01:00
Jon Shallow a34228c80f multicast: Add in IPv4 and update IPv6 multicast support
include/coap2/net.h:
src/net.c:

Update code using #587 as a base to fix potential issues.

Fixes CID 1502429

CMakeLists.txt:
cmake_coap_config.h.in:

Test for <net/if.h>

examples/coap-rd.c:
examples/coap-server.c:
man/coap-rd.txt.in:
man/coap-server.txt.in:

Add in -G option to support specifying interface to use for multicasting.
Otherwise, the interface allocated by the O/S is used depending on the
address (if any) bound to.

man/Makefile.am:
man/coap_context.txt.in:

Document the coap_join_mcast_group_intf() function.

src/coap_debug.c:

Make sure that coap_print_addr() returns a NULL terminated buffer.
2021-03-17 17:43:14 +00:00
Szabolcs Szekelyi 42b2f03470 Allow network interface name to be specified when joining multicast group
This change renames `coap_join_mcast_group` to `coap_join_mcast_group_intf`
and adds an extra `const char *ifname` parameter to specify the interface
name.

A `coap_join_mcast_group` macro is defined to maintain backwards API
compatibility.

`coap_is_mcast` has been modified to recognize IPv6-mapped IPv4 multicast
addresses. Without this `coap_network_send` treats these addresses as
unicast which makes `semdmsg` fail with EINVAL.

Examples have been updated to use the new call.
2021-03-17 17:25:31 +00:00
Jon Shallow 74342abe4d Coverity: Fix scan issues
Fixes

CID 1502435:
CID 1502432:
CID 1502423:

False Positives

CID 1502434:  Cant see why code is not reached
CID 1502433:  Could make blk_size uint8_t:3 I guess.
CID 1502430:  Dont know why it is complaining
CID 1502427:
CID 1502426:  Code looks fine to me - context->session could be set.
CID 1502424:

Fixed by other PRs

CID 1502422: #610
CID 1502431: #610
CID 1502428: #610
CID 1502429: #587 with my additional fixes
2021-03-17 11:17:18 +00:00
Jon Shallow 48ae6b0fdc message id: clean up documentation usage of transaction which is message id
CoAP transaction id was formerly defined as a hash of the remote address and
message id of the CoAP PDU.  It now only refers to, and is used as, the
message id.

Replace occurences of 'transaction id' with 'message id' to reflect naming in
RFC7252.

Correct the definition of coap_tid_t to what it actually represents.

Replace occurrences of coap_tid_t with coap_mid_t.

Replace uses of tid with mid

Remove unused function coap_find_transaction().
2021-03-08 18:49:30 +00:00
obgm ae752e9e7a Merge pull request #612 from mrdeep1/coap_unused
COAP_UNUSED: Remove UNUSED and UNUSED_PARAM and use COAP_UNUSED instead
2021-03-08 18:43:45 +01:00
obgm 301f6e10c0 Merge pull request #606 from mrdeep1/refactor_headers
include: Move header files into libcoap namespace
2021-03-08 18:39:01 +01:00
Jon Shallow 0c031d7904 include: Move header files into libcoap namespace
I.E. '#include "mem.h"' becomes '#include "coap2/mem.h"'

This prevents header name clashes.

Replacement for #487
2021-03-08 16:45:02 +00:00
Jon Shallow 4a264b2b4c COAP_UNUSED: Remove UNUSED and UNUSED_PARAM and use COAP_UNUSED instead
Remove the UNUSED and UNUSED_PARAM definitions and use a single definition
of COAP_UNUSED as defined in libcoap.h

Replace all UNUSED / UNUSED_PARAM usages with COAP_UNUSED.
2021-03-05 11:49:34 +00:00
Jon Shallow e2c8df147b RFC7959: Handle both Block1 and Block2 for same request/response
Correctly track the tokens so that the token that application sees in the
response is the same token as used in the initial request.

Update coap-server so that response data can be sent back to echo the data
that was in the request.

Fixes
CID 1502431
CID 1502428
CID 1502422
2021-03-04 15:31:20 +00:00
Jon Shallow 640bb4e667 variables: Remove size changing warnings when using Visual Studio
Change some of the types of variables, and cast change sizes where known to
be safe.

There still are some C4116 warning and a couple of C4090 warning which are OK,
so disabled by a #pragma.
2021-03-03 12:23:25 +00:00
Jon Shallow c8ab318e48 Ubuntu 20.4: Fix builds as Ubuntu 20.4 will be used by github workflows
.github/workflows/main.yml:

Build all the cmake TLS variants.

CMakeLists.txt:

Add in missing file.

examples/tiny.c:
include/coap2/encode.h:

Move COAP_PSEUDOFP_ENCODE_8_4 functions from encode.h to tiny.c.
Fix COAP_PSEUDOFP_ENCODE_8_4_DOWN 'ls' ordering complaint.

man/examples-code-check.c:

Fix compile warning, allow 'Unused but set variables' in examples and set
-Werror when compling the examples.

examples/lwip/Makefile:

Remove  compilation option -fpack-struct
2021-03-01 15:47:55 +00:00
Jon Shallow 7c7b15f6f0 net.c: Fix side effect from merging 2 PRs
Merging #552 and #554 into develop caused Observe CON 2.05 initial block
response not to get an ACK from the client and so the Observe response kept
getting re-transmitted and caused other CON traffic to get queued up.

Troubleshooting highlighted an issue when a PUT/POST was updating the contents
of a resource and an Observe was active sending many payloads in a body.

src/net.c:

Make sure that an ACK is sent (if appropriate) when working with a block based
response.

examples/coap-server.c:

Give dynamic data associated with a resource a reference count, and only free
off the data when the reference count has dropped to 0.
2021-03-01 13:17:21 +00:00
Jon Shallow a31dcba587 coap-client: Rename client.c to coap-client.c
Keep the examples source names and executables in the same form
for consistency.
2021-02-26 14:21:12 +00:00
obgm 8770152571 Merge pull request #552 from mrdeep1/ack_response
net.c: Delay sending ACK until after response_handler() is called
2021-02-26 15:15:20 +01:00
Jon Shallow e2e783033c coap_opt_filter_t: Make it a proper structure
Update coap_opt_filter_t to be a structure rather than a 16bit array that is
aliased with the real structure format.

Update code to use pointers where appropriate.
2021-02-26 09:24:34 +00:00
obgm 9f1d96a829 Merge pull request #600 from mrdeep1/github_workflows
workflow: Add in distribution and CMake checks
2021-02-26 08:46:53 +01:00
Jon Shallow dc6c5e6b00 workflow: Add in distribution and CMake checks
.github/workflows/main.yml:

Do a 'make check' for matrix builds.
Move documentation into separate job to reduce matrix build times
Add in CMake job.
Add in distribution job.
Add in tinydtls job.
Add in contiki/lwip job.

scripts/github_dist.sh:

Do the distribution checks using a script.

examples/etsi_iot_01.c:
examples/tiny.c:

Fix include coap.h directory in old examples.

README.md:

Update status badges to get only the branch actions and remove CMake
status badge.
2021-02-24 18:15:25 +00:00
Jon Shallow da18643702 coap_resource_t: Obfuscate coap_resource_t from applications.
Create a new include/coap2/coap_resource_internal.h to hold all the information
not available to applications.

Makefile.am:

Include coap_resource_internal.h in EXTRA_DIST.
Remove functions no longer visible from CTAGS_IGNORE.

examples/coap-rd.c:
examples/contiki/server.c:
examples/etsi_iot_01.c:
tests/test_wellknown.c

Replace direct access to coap_resource_t variables with functions as
appropriate.

include/coap2/resource.h:
include/coap2/subscribe.h:
include/coap2/coap_resource_internal.h: (New)
include/coap2/coap_subscribe_internal.h:

Move coap_resource_t, coap_addr_t, non API #defines and non API functions from
resource.h into coap_resource_internal.h, coap_subscribe_internal.h and
subscribe.h.

Replace inline functions accessing coap_resource_t with actual function
definitions.

Add new functions coap_resource_release_userdata_handler() and
coap_attr_get_value().

include/coap2/block.h:

Use coap_resource_t typedef.

include/coap2/coap_forward_decls.h:

Include coap_resource_t and coap_addr_t typedefs.  Also typedef coap_context_t
to get rid of #include circular requirements.

include/coap2/coap_internal.h:
win32/libcoap.vcxproj:
win32/libcoap.vcxproj.filters:

Include coap_resource_internal.h.

include/coap2/net.h:

Remove "typedef struct coap_context_t" and do this in coap_forward_decls.h.

src/resource.c:

Update with the replaced inline functions. Add in the new functions
coap_resource_release_userdata_handler() and coap_attr_get_value().

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

Update with new function visibility.

man/Makefile.am:
man/coap_attribute.txt.in:
man/coap_observe.txt.in:
man/coap_resource.txt.in:

Update with the new coap_resource_t and coap_attr_t usage.
2021-02-24 16:34:27 +00:00
Jon Shallow 14e75a54b8 net.c: Delay sending ACK until after response_handler() is called
In handle_response() the ACK is sent before calling ctx->response_handler() so
if the response_handler() wants to send RST the server ignores it as the ACK
has already been received.

By updating coap_response_handler_t to return a value, this can be used to
send the ACK (return of COAP_RESPONSE_OK) or a RST (return of COAP_RESPONSE_FAIL)
after ctx->response_handler() is called.

include/coap2/net.h:
man/coap_handler.txt.in:

Create a new enum coap_response_t for use as the return value for
coap_response_handler_t.
Update coap_response_handler_t and associated documentation.

src/net.c:

Send CoAP ACK or RST depending on return value of ctx->response_handler().

examples/client.c:

Update message_handler() to use the new coap_response_handler_t return values
(coap_response_t).
2021-02-24 09:37:32 +00:00
Jon Shallow 4cd633e763 RFC7641: Add in example support using updated libcoap handling of blocks
Remove block handling logic from coap-client and coap-server and make use of the
new libcoap functionality.

Add in new -L option to be able to define how libcoap is to work with block
handling.  Update documentation accordingly.
2021-02-23 18:01:14 +00:00
obgm b7fed2a706 Merge pull request #571 from Delsian/develop
refactor: replace magic numbers with defines
2021-02-23 17:50:06 +01:00
Eug Krashtan 6b58bfa96c examples: replace magic numbers with defines
CoAP response codes in examples represented by non-human-readable magic
numbers. It makes code harder to read, understand, and maintain.
Magic numbers there where replaced by human-readable enums.

Signed-off-by: Eug Krashtan <eug.krashtan@gmail.com>
2021-02-22 21:20:31 +02:00
Jon Shallow 05d30e5ef1 PKI: Make (D)TLS operation consistent across all TLS libraries
The use of the verify_peer_cert and require_peer_cert variables in the
coap_dtls_pki_t structure was giving inconsistent results across all the
TLS libraries.  This primarily was down to the large numbers of options
available to control the TLS handshakes in OpenSSL compared to the limited
control available to mbedTLS port which followed later. require_peer_cert is
not easy to control in mbedTLS as it is an implicit configuration based on how
other, not always related, items were configured.

require_peer_cert was used by the server to control whether the client could
use anonymous certificates or not.  This is now controlled by verify_peer_cert.

require_peer_cert variable has been replaced with check_common_ca, so that
the OpenSSL functionality can continue, but enable GnuTLS / mbedTLS to
produce the same results.  This allows peers to mutually authenticate (because
the peer certs are signed by the same common CA) or not which was in effect
controlled by verify_peer_cert previously.

If check_common_ca is set, then allow_self_signed is ignored.

If is_rpk_not_cert is set, then all certificate validation is ignored.

In the examples, use of the -R option unsets check_common_ca, so disables
mutual authentication support by having a common CA.  This was needed as
mbedTLS and GnuTLS only have a single trust store for CAs.

configure.ac:

Increase the number of mbed libraries to use when checking for mbedTLS.

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

Add in -n (unset verify_peer_cert) option. In the case of coap-server and
coap-rd, make -n refer to verify_peer_cert.
Add in TLS library capabilites in usage().

Update usage() documentation as appropriate, with some changes to fit
everything into a 80 column output.

include/coap2/coap_dtls.h:
include/coap2/net.h:

Update with variable changes, and make the coap_dtls_pki_t parameter const for
the *_context_set_pki() functions.

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

Update documentation to reflect the examples option usage.

man/coap_context.txt.in:
man/coap_encryption.txt.in:
man/coap_session.txt.in:

Update with the new variable name and document as appropriate.

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

Update to make variable usage consistent. Update logging from LOG_WARNING to
LOG_INFO where there is an override of a PKI check failure by one of the
coap_dtls_pki_t variables.

Timing window closed for TLS where the peer does not like a certificate, sends
fatal alert and closes connection.  local then fails on writing the next
handshake - but now also reads in alert and reports on it.

src/coap_io.c:

Update logging from LOG_WARNING to LOG_INFO for EPIPE or ECONNRESET errors in
coap_socket_write().

src/net.c:

Handle the const coap_dtls_pki_t parameter in coap_context_set_pki() function.
2021-02-18 16:51:32 +00:00
Jon Shallow 2e6cb55a50 message id: Report everywhere the hex value for consistency
coap_show_pdu() reports the Message ID (mid) as a hex value prefixes by i:.
Elsewhere it is reported as an integer or unsigned making those entries
difficult to match up with the reported PDU.

examples/coap-rd.c:
examples/etsi_iot_01.c:
src/async.c:
src/coap_session.c:
src/net.c:

Replace tid=%d, message %d and transaction %d with mid=0x%x for clarity.
2021-02-15 09:47:02 +00:00
obgm 85b45e0249 Merge pull request #590 from wenwu-glagle/pull_request
Fix some compile error in windows visual studio
2021-02-12 15:25:28 +01:00
Olaf Bergmann f681525272 coap-server.c: fix typecast warning for verify_cn_callback
This change fixes a warning output by clang for the cast from void* to
the potentially smaller enum coap_dtls_role_t (-Wvoid-pointer-to-enum-cast).
2021-01-13 15:56:28 +01:00