libcoap
4.2.0
|
#include "coap_config.h"
#include <ctype.h>
#include <stdio.h>
#include <errno.h>
#include "libcoap.h"
#include "utlist.h"
#include "coap_debug.h"
#include "mem.h"
#include "str.h"
#include "async.h"
#include "resource.h"
#include "option.h"
#include "encode.h"
#include "block.h"
#include "net.h"
Go to the source code of this file.
Macros | |
#define | min(a, b) ((a) < (b) ? (a) : (b)) |
#define | FRAC_BITS 6 |
The number of bits for the fractional part of ACK_TIMEOUT and ACK_RANDOM_FACTOR. More... | |
#define | MAX_BITS 8 |
The maximum number of bits for fixed point integers that are used for retransmission time calculation. More... | |
#define | Q(frac, fval) |
creates a Qx.frac from fval in coap_fixed_point_t More... | |
#define | ACK_RANDOM_FACTOR Q(FRAC_BITS, session->ack_random_factor) |
creates a Qx.FRAC_BITS from session's 'ack_random_factor' More... | |
#define | ACK_TIMEOUT Q(FRAC_BITS, session->ack_timeout) |
creates a Qx.FRAC_BITS from session's 'ack_timeout' More... | |
#define | FP1 Q(FRAC_BITS, ((coap_fixed_point_t){1,0})) |
#define | SHR_FP(val, frac) (((val) + (1 << ((frac) - 1))) >> (frac)) |
#define | SZX_TO_BYTES(SZX) ((size_t)(1 << ((SZX) + 4))) |
#define | INET6_ADDRSTRLEN 40 |
Enumerations | |
enum | respond_t { RESPONSE_DEFAULT, RESPONSE_DROP, RESPONSE_SEND } |
Internal flags to control the treatment of responses (specifically in presence of the No-Response option). More... | |
Functions | |
COAP_STATIC_INLINE coap_queue_t * | coap_malloc_node (void) |
COAP_STATIC_INLINE void | coap_free_node (coap_queue_t *node) |
void | coap_free_endpoint (coap_endpoint_t *ep) |
unsigned int | coap_adjust_basetime (coap_context_t *ctx, coap_tick_t now) |
Set sendqueue_basetime in the given context object ctx to now . More... | |
int | coap_insert_node (coap_queue_t **queue, coap_queue_t *node) |
Adds node to given queue , ordered by variable t in node . More... | |
int | coap_delete_node (coap_queue_t *node) |
Destroys specified node . More... | |
void | coap_delete_all (coap_queue_t *queue) |
Removes all items from given queue and frees the allocated storage. More... | |
coap_queue_t * | coap_new_node (void) |
Creates a new node suitable for adding to the CoAP sendqueue. More... | |
coap_queue_t * | coap_peek_next (coap_context_t *context) |
Returns the next pdu to send without removing from sendqeue. More... | |
coap_queue_t * | coap_pop_next (coap_context_t *context) |
Returns the next pdu to send and removes it from the sendqeue. More... | |
static size_t | coap_get_session_client_psk (const coap_session_t *session, const uint8_t *hint, size_t hint_len, uint8_t *identity, size_t *identity_len, size_t max_identity_len, uint8_t *psk, size_t max_psk_len) |
static size_t | coap_get_context_server_psk (const coap_session_t *session, const uint8_t *identity, size_t identity_len, uint8_t *psk, size_t max_psk_len) |
static size_t | coap_get_context_server_hint (const coap_session_t *session, uint8_t *hint, size_t max_hint_len) |
int | coap_context_set_psk (coap_context_t *ctx, const char *hint, const uint8_t *key, size_t key_len) |
Set the context's default PSK hint and/or key for a server. More... | |
int | coap_context_set_pki (coap_context_t *ctx, coap_dtls_pki_t *setup_data) |
Set the context's default PKI information for a server. More... | |
int | coap_context_set_pki_root_cas (coap_context_t *ctx, const char *ca_file, const char *ca_dir) |
Set the context's default Root CA information for a client or server. More... | |
void | coap_context_set_keepalive (coap_context_t *context, unsigned int seconds) |
Set the context keepalive timer for sessions. More... | |
coap_context_t * | coap_new_context (const coap_address_t *listen_addr) |
Creates a new coap_context_t object that will hold the CoAP stack status. More... | |
void | coap_set_app_data (coap_context_t *ctx, void *app_data) |
Stores data with the given CoAP context. More... | |
void * | coap_get_app_data (const coap_context_t *ctx) |
Returns any application-specific data that has been stored with context using the function coap_set_app_data(). More... | |
void | coap_free_context (coap_context_t *context) |
CoAP stack context must be released with coap_free_context(). More... | |
int | coap_option_check_critical (coap_context_t *ctx, coap_pdu_t *pdu, coap_opt_filter_t unknown) |
Verifies that pdu contains no unknown critical options. More... | |
coap_tid_t | coap_send_ack (coap_session_t *session, coap_pdu_t *request) |
Sends an ACK message with code 0 for the specified request to dst . More... | |
ssize_t | coap_session_send_pdu (coap_session_t *session, coap_pdu_t *pdu) |
Send a pdu according to the session's protocol. More... | |
static ssize_t | coap_send_pdu (coap_session_t *session, coap_pdu_t *pdu, coap_queue_t *node) |
coap_tid_t | coap_send_error (coap_session_t *session, coap_pdu_t *request, unsigned char code, coap_opt_filter_t opts) |
Sends an error response with code code for request request to dst . More... | |
coap_tid_t | coap_send_message_type (coap_session_t *session, coap_pdu_t *request, unsigned char type) |
Helper funktion to create and send a message with type (usually ACK or RST). More... | |
unsigned int | coap_calc_timeout (coap_session_t *session, unsigned char r) |
Calculates the initial timeout based on the session CoAP transmission parameters 'ack_timeout', 'ack_random_factor', and COAP_TICKS_PER_SECOND. More... | |
coap_tid_t | coap_wait_ack (coap_context_t *context, coap_session_t *session, coap_queue_t *node) |
coap_tid_t | coap_send (coap_session_t *session, coap_pdu_t *pdu) |
Sends a CoAP message to given peer. More... | |
coap_tid_t | coap_retransmit (coap_context_t *context, coap_queue_t *node) |
Handles retransmissions of confirmable messages. More... | |
static int | coap_handle_dgram_for_proto (coap_context_t *ctx, coap_session_t *session, coap_packet_t *packet) |
static void | coap_connect_session (coap_context_t *ctx, coap_session_t *session, coap_tick_t now) |
static void | coap_write_session (coap_context_t *ctx, coap_session_t *session, coap_tick_t now) |
static void | coap_read_session (coap_context_t *ctx, coap_session_t *session, coap_tick_t now) |
static int | coap_read_endpoint (coap_context_t *ctx, coap_endpoint_t *endpoint, coap_tick_t now) |
static int | coap_write_endpoint (coap_context_t *ctx, coap_endpoint_t *endpoint, coap_tick_t now) |
static int | coap_accept_endpoint (coap_context_t *ctx, coap_endpoint_t *endpoint, coap_tick_t now) |
void | coap_read (coap_context_t *ctx, coap_tick_t now) |
For applications with their own message loop, reads all data from the network. More... | |
int | coap_handle_dgram (coap_context_t *ctx, coap_session_t *session, uint8_t *msg, size_t msg_len) |
Parses and interprets a CoAP datagram with context ctx . More... | |
int | coap_remove_from_queue (coap_queue_t **queue, coap_session_t *session, coap_tid_t id, coap_queue_t **node) |
This function removes the element with given id from the list given list. More... | |
COAP_STATIC_INLINE int | token_match (const uint8_t *a, size_t alen, const uint8_t *b, size_t blen) |
void | coap_cancel_session_messages (coap_context_t *context, coap_session_t *session, coap_nack_reason_t reason) |
Cancels all outstanding messages for session session . More... | |
void | coap_cancel_all_messages (coap_context_t *context, coap_session_t *session, const uint8_t *token, size_t token_length) |
Cancels all outstanding messages for session session that have the specified token. More... | |
coap_queue_t * | coap_find_transaction (coap_queue_t *queue, coap_session_t *session, coap_tid_t id) |
Retrieves transaction from the queue. More... | |
coap_pdu_t * | coap_new_error_response (coap_pdu_t *request, unsigned char code, coap_opt_filter_t opts) |
Creates a new ACK PDU with specified error code . More... | |
COAP_STATIC_INLINE size_t | get_wkc_len (coap_context_t *context, coap_opt_t *query_filter) |
Quick hack to determine the size of the resource description for .well-known/core. More... | |
coap_pdu_t * | coap_wellknown_response (coap_context_t *context, coap_session_t *session, coap_pdu_t *request) |
Creates a new response for given request with the contents of .well-known/core. More... | |
static int | coap_cancel (coap_context_t *context, const coap_queue_t *sent) |
This function cancels outstanding messages for the session and token specified in sent . More... | |
static enum respond_t | no_response (coap_pdu_t *request, coap_pdu_t *response) |
Checks for No-Response option in given request and returns 1 if response should be suppressed according to RFC 7967. More... | |
static void | handle_request (coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu) |
static void | handle_response (coap_context_t *context, coap_session_t *session, coap_pdu_t *sent, coap_pdu_t *rcvd) |
static void | handle_signaling (coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu) |
void | coap_dispatch (coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu) |
Dispatches the PDUs from the receive queue in given context. More... | |
int | coap_handle_event (coap_context_t *context, coap_event_t event, coap_session_t *session) |
Invokes the event handler of context for the given event and data . More... | |
int | coap_can_exit (coap_context_t *context) |
Returns 1 if there are no messages to send or to dispatch in the context's queues. More... | |
void | coap_startup (void) |
void | coap_cleanup (void) |
Variables | |
static coap_str_const_t | coap_default_uri_wellknown |
static int | coap_started = 0 |
#define FP1 Q(FRAC_BITS, ((coap_fixed_point_t){1,0})) |
#define FRAC_BITS 6 |
#define INET6_ADDRSTRLEN 40 |
#define MAX_BITS 8 |
#define Q | ( | frac, | |
fval | |||
) |
creates a Qx.frac from fval in coap_fixed_point_t
#define SHR_FP | ( | val, | |
frac | |||
) | (((val) + (1 << ((frac) - 1))) >> (frac)) |
enum respond_t |
|
static |
unsigned int coap_adjust_basetime | ( | coap_context_t * | ctx, |
coap_tick_t | now | ||
) |
unsigned int coap_calc_timeout | ( | coap_session_t * | session, |
unsigned char | r | ||
) |
Calculates the initial timeout based on the session CoAP transmission parameters 'ack_timeout', 'ack_random_factor', and COAP_TICKS_PER_SECOND.
The calculation requires 'ack_timeout' and 'ack_random_factor' to be in Qx.FRAC_BITS fixed point notation, whereas the passed parameter r
is interpreted as the fractional part of a Q0.MAX_BITS random value.
session | session timeout is associated with |
r | random value as fractional part of a Q0.MAX_BITS fixed point value |
Definition at line 798 of file net.c.
int coap_can_exit | ( | coap_context_t * | context | ) |
|
static |
This function cancels outstanding messages for the session and token specified in sent
.
Any observation relationship for sent->session and the token are removed. Calling this function is required when receiving an RST message (usually in response to a notification) or a GET request with the Observe option set to 1.
This function returns 0
when the token is unknown with this peer, or a value greater than zero otherwise.
Definition at line 1786 of file net.c.
void coap_cancel_all_messages | ( | coap_context_t * | context, |
coap_session_t * | session, | ||
const uint8_t * | token, | ||
size_t | token_length | ||
) |
Cancels all outstanding messages for session session
that have the specified token.
context | The context in use. |
session | Session of the messages to remove. |
token | Message token. |
token_length | Actual length of token . |
Definition at line 1484 of file net.c.
void coap_cancel_session_messages | ( | coap_context_t * | context, |
coap_session_t * | session, | ||
coap_nack_reason_t | reason | ||
) |
Cancels all outstanding messages for session session
.
context | The context in use. |
session | Session of the messages to remove. |
reason | The reasion for the session cancellation |
Definition at line 1447 of file net.c.
void coap_cleanup | ( | void | ) |
|
static |
void coap_context_set_keepalive | ( | coap_context_t * | context, |
unsigned int | seconds | ||
) |
Set the context keepalive timer for sessions.
A keepalive message will be sent after if a session has been inactive, i.e. no packet sent or received, for the given number of seconds. For reliable protocols, a PING message will be sent. If a PONG has not been received before the next PING is due to be sent, the session will considered as disconnected.
context | The coap_context_t object. |
seconds | Number of seconds for the inactivity timer, or zero to disable CoAP-level keepalive messages. |
Definition at line 417 of file net.c.
int coap_context_set_pki | ( | coap_context_t * | context, |
coap_dtls_pki_t * | setup_data | ||
) |
Set the context's default PKI information for a server.
context | The current coap_context_t object. |
setup_data | If NULL , PKI authentication will fail. Certificate information required. |
1
if successful, else 0
. Definition at line 392 of file net.c.
int coap_context_set_pki_root_cas | ( | coap_context_t * | context, |
const char * | ca_file, | ||
const char * | ca_dir | ||
) |
Set the context's default Root CA information for a client or server.
context | The current coap_context_t object. |
ca_file | If not NULL , is the full path name of a PEM encoded file containing all the Root CAs to be used. |
ca_dir | If not NULL , points to a directory containing PEM encoded files containing all the Root CAs to be used. |
1
if successful, else 0
. Definition at line 407 of file net.c.
int coap_context_set_psk | ( | coap_context_t * | context, |
const char * | hint, | ||
const uint8_t * | key, | ||
size_t | key_len | ||
) |
Set the context's default PSK hint and/or key for a server.
context | The current coap_context_t object. |
hint | The default PSK server hint sent to a client. If NULL , PSK authentication is disabled. Empty string is a valid hint. |
key | The default PSK key. If NULL , PSK authentication will fail. |
key_len | The default PSK key's length. If 0 , PSK authentication will fail. |
1
if successful, else 0
. Definition at line 349 of file net.c.
void coap_delete_all | ( | coap_queue_t * | queue | ) |
int coap_delete_node | ( | coap_queue_t * | node | ) |
void coap_dispatch | ( | coap_context_t * | context, |
coap_session_t * | session, | ||
coap_pdu_t * | pdu | ||
) |
coap_queue_t* coap_find_transaction | ( | coap_queue_t * | queue, |
coap_session_t * | session, | ||
coap_tid_t | id | ||
) |
void coap_free_context | ( | coap_context_t * | context | ) |
CoAP stack context must be released with coap_free_context().
This function clears all entries from the receive queue and send queue and deletes the resources that have been registered with context
, and frees the attached endpoints.
context | The current coap_context_t object to free off. |
Definition at line 518 of file net.c.
void coap_free_endpoint | ( | coap_endpoint_t * | ep | ) |
COAP_STATIC_INLINE void coap_free_node | ( | coap_queue_t * | node | ) |
void* coap_get_app_data | ( | const coap_context_t * | context | ) |
Returns any application-specific data that has been stored with context
using the function coap_set_app_data().
This function will return NULL
if no data has been stored.
context | The CoAP context. |
NULL
if not data stored. Definition at line 512 of file net.c.
|
static |
|
static |
|
static |
int coap_handle_dgram | ( | coap_context_t * | ctx, |
coap_session_t * | session, | ||
uint8_t * | data, | ||
size_t | data_len | ||
) |
Parses and interprets a CoAP datagram with context ctx
.
This function returns 0
if the datagram was handled, or a value less than zero on error.
ctx | The current CoAP context. |
session | The current CoAP session. |
data | The received packet'd data. |
data_len | The received packet'd data length. |
0
if message was handled successfully, or less than zero on error. Definition at line 1369 of file net.c.
|
static |
int coap_handle_event | ( | coap_context_t * | context, |
coap_event_t | event, | ||
coap_session_t * | session | ||
) |
Invokes the event handler of context
for the given event
and data
.
context | The CoAP context whose event handler is to be called. |
event | The event to deliver. |
session | The session related to event . |
Definition at line 2310 of file net.c.
int coap_insert_node | ( | coap_queue_t ** | queue, |
coap_queue_t * | node | ||
) |
COAP_STATIC_INLINE coap_queue_t* coap_malloc_node | ( | void | ) |
coap_context_t* coap_new_context | ( | const coap_address_t * | listen_addr | ) |
Creates a new coap_context_t object that will hold the CoAP stack status.
Definition at line 422 of file net.c.
coap_pdu_t* coap_new_error_response | ( | coap_pdu_t * | request, |
unsigned char | code, | ||
coap_opt_filter_t | opts | ||
) |
Creates a new ACK PDU with specified error code
.
The options specified by the filter expression opts
will be copied from the original request contained in request
. Unless SHORT_ERROR_RESPONSE
was defined at build time, the textual reason phrase for code
will be added as payload, with Content-Type 0
. This function returns a pointer to the new response message, or NULL
on error. The storage allocated for the new message must be relased with coap_free().
request | Specification of the received (confirmable) request. |
code | The error code to set. |
opts | An option filter that specifies which options to copy from the original request in node . |
NULL
on error. Definition at line 1533 of file net.c.
coap_queue_t* coap_new_node | ( | void | ) |
int coap_option_check_critical | ( | coap_context_t * | ctx, |
coap_pdu_t * | pdu, | ||
coap_opt_filter_t | unknown | ||
) |
Verifies that pdu
contains no unknown critical options.
Options must be registered at ctx
, using the function coap_register_option(). A basic set of options is registered automatically by coap_new_context(). This function returns 1
if pdu
is ok, 0
otherwise. The given filter object unknown
will be updated with the unknown options. As only COAP_MAX_OPT
options can be signalled this way, remaining options must be examined manually.
ctx | The context where all known options are registered. |
pdu | The PDU to check. |
unknown | The output filter that will be updated to indicate the unknown critical options found in pdu . |
1
if everything was ok, 0
otherwise. Definition at line 561 of file net.c.
coap_queue_t* coap_peek_next | ( | coap_context_t * | context | ) |
coap_queue_t* coap_pop_next | ( | coap_context_t * | context | ) |
void coap_read | ( | coap_context_t * | ctx, |
coap_tick_t | now | ||
) |
|
static |
|
static |
int coap_remove_from_queue | ( | coap_queue_t ** | queue, |
coap_session_t * | session, | ||
coap_tid_t | id, | ||
coap_queue_t ** | node | ||
) |
This function removes the element with given id
from the list given list.
If id
was found, node
is updated to point to the removed element. Note that the storage allocated by node
is not released. The caller must do this manually using coap_delete_node(). This function returns 1
if the element with id id
was found, 0
otherwise. For a return value of 0
, the contents of node
is undefined.
queue | The queue to search for id . |
session | The session to look for. |
id | The transaction id to look for. |
node | If found, node is updated to point to the removed node. You must release the storage pointed to by node manually. |
1
if id
was found, 0
otherwise. Definition at line 1397 of file net.c.
coap_tid_t coap_retransmit | ( | coap_context_t * | context, |
coap_queue_t * | node | ||
) |
Handles retransmissions of confirmable messages.
context | The CoAP context. |
node | The node to retransmit. |
COAP_INVALID_TID
on error. Definition at line 931 of file net.c.
coap_tid_t coap_send | ( | coap_session_t * | session, |
coap_pdu_t * | pdu | ||
) |
Sends a CoAP message to given peer.
The memory that is allocated by pdu will be released by coap_send(). The caller must not use the pdu after calling coap_send().
session | The CoAP session. |
pdu | The CoAP PDU to send. |
COAP_INVALID_TID
on error. Definition at line 876 of file net.c.
coap_tid_t coap_send_ack | ( | coap_session_t * | session, |
coap_pdu_t * | request | ||
) |
Sends an ACK message with code 0
for the specified request
to dst
.
This function returns the corresponding transaction id if the message was sent or COAP_INVALID_TID
on error.
session | The CoAP session. |
request | The request to be acknowledged. |
COAP_INVALID_TID
on error. Definition at line 613 of file net.c.
coap_tid_t coap_send_error | ( | coap_session_t * | session, |
coap_pdu_t * | request, | ||
unsigned char | code, | ||
coap_opt_filter_t | opts | ||
) |
Sends an error response with code code
for request request
to dst
.
opts
will be passed to coap_new_error_response() to copy marked options from the request. This function returns the transaction id if the message was sent, or COAP_INVALID_TID
otherwise.
session | The CoAP session. |
request | The original request to respond to. |
code | The response code. |
opts | A filter that specifies the options to copy from the request . |
COAP_INVALID_TID
otherwise. Definition at line 754 of file net.c.
coap_tid_t coap_send_message_type | ( | coap_session_t * | session, |
coap_pdu_t * | request, | ||
unsigned char | type | ||
) |
Helper funktion to create and send a message with type
(usually ACK or RST).
This function returns COAP_INVALID_TID
when the message was not sent, a valid transaction id otherwise.
session | The CoAP session. |
request | The request that should be responded to. |
type | Which type to set. |
COAP_INVALID_TID
otherwise. Definition at line 772 of file net.c.
|
static |
ssize_t coap_session_send_pdu | ( | coap_session_t * | session, |
coap_pdu_t * | pdu | ||
) |
Send a pdu according to the session's protocol.
This function returns the number of bytes that have been transmitted, or a value less than zero on error.
session | Session to send pdu on. |
pdu | The pdu to send. |
Definition at line 627 of file net.c.
void coap_set_app_data | ( | coap_context_t * | context, |
void * | data | ||
) |
Stores data
with the given CoAP context.
This function overwrites any value that has previously been stored with context
.
context | The CoAP context. |
data | The data to store with wih the context. Note that this data must be valid during the lifetime of context . |
Definition at line 506 of file net.c.
void coap_startup | ( | void | ) |
coap_tid_t coap_wait_ack | ( | coap_context_t * | context, |
coap_session_t * | session, | ||
coap_queue_t * | node | ||
) |
coap_pdu_t* coap_wellknown_response | ( | coap_context_t * | context, |
coap_session_t * | session, | ||
coap_pdu_t * | request | ||
) |
Creates a new response for given request
with the contents of .well-known/core.
The result is NULL on error or a newly allocated PDU that must be either sent with coap_sent() or released by coap_delete_pdu().
context | The current coap context to use. |
session | The CoAP session. |
request | The request for .well-known/core . |
.well-known/core or NULL on error. Definition at line 1649 of file net.c.
|
static |
|
static |
COAP_STATIC_INLINE size_t get_wkc_len | ( | coap_context_t * | context, |
coap_opt_t * | query_filter | ||
) |
|
static |
|
static |
|
static |
|
static |
Checks for No-Response option in given request
and returns 1
if response
should be suppressed according to RFC 7967.
The value of the No-Response option is encoded as follows:
* +-------+-----------------------+-----------------------------------+ * | Value | Binary Representation | Description | * +-------+-----------------------+-----------------------------------+ * | 0 | <empty> | Interested in all responses. | * +-------+-----------------------+-----------------------------------+ * | 2 | 00000010 | Not interested in 2.xx responses. | * +-------+-----------------------+-----------------------------------+ * | 8 | 00001000 | Not interested in 4.xx responses. | * +-------+-----------------------+-----------------------------------+ * | 16 | 00010000 | Not interested in 5.xx responses. | * +-------+-----------------------+-----------------------------------+ *
request | The CoAP request to check for the No-Response option. This parameter must not be NULL. |
response | The response that is potentially suppressed. This parameter must not be NULL. |
Definition at line 1845 of file net.c.
COAP_STATIC_INLINE int token_match | ( | const uint8_t * | a, |
size_t | alen, | ||
const uint8_t * | b, | ||
size_t | blen | ||
) |
|
static |