libcoap  4.3.0
net.c File Reference
#include "coap3/coap_internal.h"
#include <ctype.h>
#include <stdio.h>
#include <errno.h>
+ Include dependency graph for net.c:

Go to the source code of this file.

Macros

#define INET6_ADDRSTRLEN   40
 
#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)))
 

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_tcoap_malloc_node (void)
 
COAP_STATIC_INLINE void coap_free_node (coap_queue_t *node)
 
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_tcoap_new_node (void)
 Creates a new node suitable for adding to the CoAP sendqueue. More...
 
coap_queue_tcoap_peek_next (coap_context_t *context)
 Returns the next pdu to send without removing from sendqeue. More...
 
coap_queue_tcoap_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_psk2 (coap_context_t *ctx, coap_dtls_spsk_t *setup_data)
 Set the context's default PSK hint and/or key for a server. More...
 
int coap_context_set_pki (coap_context_t *ctx, const 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...
 
void coap_context_set_max_idle_sessions (coap_context_t *context, unsigned int max_idle_sessions)
 Set the maximum idle sessions count. More...
 
unsigned int coap_context_get_max_idle_sessions (const coap_context_t *context)
 Get the maximum idle sessions count. More...
 
void coap_context_set_max_handshake_sessions (coap_context_t *context, unsigned int max_handshake_sessions)
 Set the maximum number of sessions in (D)TLS handshake value. More...
 
unsigned int coap_context_get_max_handshake_sessions (const coap_context_t *context)
 Get the session timeout value. More...
 
void coap_context_set_csm_timeout (coap_context_t *context, unsigned int csm_timeout)
 Set the CSM timeout value. More...
 
unsigned int coap_context_get_csm_timeout (const coap_context_t *context)
 Get the CSM timeout value. More...
 
void coap_context_set_session_timeout (coap_context_t *context, unsigned int session_timeout)
 Set the session timeout value. More...
 
unsigned int coap_context_get_session_timeout (const coap_context_t *context)
 Get the session timeout value. More...
 
int coap_context_get_coap_fd (const coap_context_t *context)
 Get the libcoap internal file descriptor for using in an application's select() or returned as an event in an application's epoll_wait() call. More...
 
coap_context_tcoap_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_mid_t coap_send_ack (coap_session_t *session, const 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_mid_t coap_send_error (coap_session_t *session, const coap_pdu_t *request, coap_pdu_code_t code, coap_opt_filter_t *opts)
 Sends an error response with code code for request request to dst. More...
 
coap_mid_t coap_send_message_type (coap_session_t *session, const coap_pdu_t *request, coap_pdu_type_t type)
 Helper function 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_mid_t coap_wait_ack (coap_context_t *context, coap_session_t *session, coap_queue_t *node)
 
COAP_STATIC_INLINE int token_match (const uint8_t *a, size_t alen, const uint8_t *b, size_t blen)
 
coap_mid_t coap_send (coap_session_t *session, coap_pdu_t *pdu)
 Sends a CoAP message to given peer. More...
 
coap_mid_t coap_send_internal (coap_session_t *session, coap_pdu_t *pdu)
 Sends a CoAP message to given peer. More...
 
coap_mid_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_io_do_io (coap_context_t *ctx, coap_tick_t now)
 Processes any outstanding read, write, accept or connect I/O as indicated in the coap_socket_t structures (COAP_SOCKET_CAN_xxx set) embedded in endpoints or sessions associated with ctx. More...
 
void coap_io_do_epoll (coap_context_t *ctx, struct epoll_event *events, size_t nevents)
 Process all the epoll events. 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_mid_t id, coap_queue_t **node)
 This function removes the element with given id from the list given list. More...
 
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_pdu_tcoap_new_error_response (const coap_pdu_t *request, coap_pdu_code_t 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_tcoap_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, coap_session_t *session)
 
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...
 
coap_tick_t coap_check_async (coap_context_t *context, coap_tick_t now)
 Checks if there are any pending Async requests - if so, send them off. More...
 
void coap_startup (void)
 
void coap_cleanup (void)
 
void coap_register_response_handler (coap_context_t *context, coap_response_handler_t handler)
 Registers a new message handler that is called whenever a response is received. More...
 
void coap_register_nack_handler (coap_context_t *context, coap_nack_handler_t handler)
 Registers a new message handler that is called whenever a confirmable message (request or response) is dropped after all retries have been exhausted, or a rst message was received, or a network or TLS level event was received that indicates delivering the message is not possible. More...
 
void coap_register_ping_handler (coap_context_t *context, coap_ping_handler_t handler)
 Registers a new message handler that is called whenever a CoAP Ping message is received. More...
 
void coap_register_pong_handler (coap_context_t *context, coap_pong_handler_t handler)
 Registers a new message handler that is called whenever a CoAP Pong message is received. More...
 
void coap_register_option (coap_context_t *ctx, uint16_t type)
 Registers the option type type with the given context object ctx. More...
 
int coap_join_mcast_group_intf (coap_context_t *ctx, const char *group_name, const char *ifname)
 Function interface for joining a multicast group for listening for the currently defined endpoints that are UDP. More...
 
int coap_mcast_set_hops (coap_session_t *session, size_t hops)
 Function interface for defining the hop count (ttl) for sending multicast traffic. More...
 

Variables

static coap_str_const_t coap_default_uri_wellknown
 
static int coap_started = 0
 

Macro Definition Documentation

◆ ACK_RANDOM_FACTOR

#define ACK_RANDOM_FACTOR    Q(FRAC_BITS, session->ack_random_factor)

creates a Qx.FRAC_BITS from session's 'ack_random_factor'

Definition at line 91 of file net.c.

◆ ACK_TIMEOUT

#define ACK_TIMEOUT   Q(FRAC_BITS, session->ack_timeout)

creates a Qx.FRAC_BITS from session's 'ack_timeout'

Definition at line 95 of file net.c.

◆ FP1

#define FP1   Q(FRAC_BITS, ((coap_fixed_point_t){1,0}))

◆ FRAC_BITS

#define FRAC_BITS   6

The number of bits for the fractional part of ACK_TIMEOUT and ACK_RANDOM_FACTOR.

Must be less or equal 8.

Definition at line 74 of file net.c.

◆ INET6_ADDRSTRLEN

#define INET6_ADDRSTRLEN   40

Definition at line 63 of file net.c.

◆ MAX_BITS

#define MAX_BITS   8

The maximum number of bits for fixed point integers that are used for retransmission time calculation.

Currently this must be 8.

Definition at line 80 of file net.c.

◆ min

#define min (   a,
 
)    ((a) < (b) ? (a) : (b))

Definition at line 67 of file net.c.

◆ Q

#define Q (   frac,
  fval 
)
Value:
((uint16_t)(((1 << (frac)) * fval.integer_part) + \
((1 << (frac)) * fval.fractional_part + 500)/1000))

creates a Qx.frac from fval in coap_fixed_point_t

Definition at line 87 of file net.c.

◆ SHR_FP

#define SHR_FP (   val,
  frac 
)    (((val) + (1 << ((frac) - 1))) >> (frac))

◆ SZX_TO_BYTES

#define SZX_TO_BYTES (   SZX)    ((size_t)(1 << ((SZX) + 4)))

Definition at line 2185 of file net.c.

Enumeration Type Documentation

◆ respond_t

enum respond_t

Internal flags to control the treatment of responses (specifically in presence of the No-Response option).

Enumerator
RESPONSE_DEFAULT 
RESPONSE_DROP 
RESPONSE_SEND 

Definition at line 2363 of file net.c.

Function Documentation

◆ coap_accept_endpoint()

static int coap_accept_endpoint ( coap_context_t ctx,
coap_endpoint_t endpoint,
coap_tick_t  now 
)
static

Definition at line 1729 of file net.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ coap_cancel()

static int coap_cancel ( coap_context_t context,
const coap_queue_t sent 
)
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 2341 of file net.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ coap_cleanup()

void coap_cleanup ( void  )

Definition at line 3242 of file net.c.

+ Here is the call graph for this function:

◆ coap_connect_session()

static void coap_connect_session ( coap_context_t ctx,
coap_session_t session,
coap_tick_t  now 
)
static

Definition at line 1447 of file net.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ coap_free_node()

COAP_STATIC_INLINE void coap_free_node ( coap_queue_t node)

Definition at line 105 of file net.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ coap_get_context_server_hint()

static size_t coap_get_context_server_hint ( const coap_session_t session,
uint8_t *  hint,
size_t  max_hint_len 
)
static

Definition at line 362 of file net.c.

+ Here is the caller graph for this function:

◆ coap_get_context_server_psk()

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

Definition at line 334 of file net.c.

+ Here is the caller graph for this function:

◆ coap_get_session_client_psk()

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

Definition at line 298 of file net.c.

+ Here is the caller graph for this function:

◆ coap_handle_dgram_for_proto()

static int coap_handle_dgram_for_proto ( coap_context_t ctx,
coap_session_t session,
coap_packet_t packet 
)
static

Definition at line 1428 of file net.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ coap_malloc_node()

COAP_STATIC_INLINE coap_queue_t* coap_malloc_node ( void  )

Definition at line 100 of file net.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ coap_read_endpoint()

static int coap_read_endpoint ( coap_context_t ctx,
coap_endpoint_t endpoint,
coap_tick_t  now 
)
static

Definition at line 1678 of file net.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ coap_read_session()

static void coap_read_session ( coap_context_t ctx,
coap_session_t session,
coap_tick_t  now 
)
static

Definition at line 1534 of file net.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ coap_send_pdu()

static ssize_t coap_send_pdu ( coap_session_t session,
coap_pdu_t pdu,
coap_queue_t node 
)
static

Definition at line 799 of file net.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ coap_startup()

void coap_startup ( void  )

Definition at line 3222 of file net.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ coap_write_endpoint()

static int coap_write_endpoint ( coap_context_t ctx,
coap_endpoint_t endpoint,
coap_tick_t  now 
)
static

Definition at line 1721 of file net.c.

+ Here is the caller graph for this function:

◆ coap_write_session()

static void coap_write_session ( coap_context_t ctx,
coap_session_t session,
coap_tick_t  now 
)
static

Definition at line 1484 of file net.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_wkc_len()

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.

Definition at line 2170 of file net.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ handle_request()

static void handle_request ( coap_context_t context,
coap_session_t session,
coap_pdu_t pdu 
)
static

Definition at line 2483 of file net.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ handle_response()

static void handle_response ( coap_context_t context,
coap_session_t session,
coap_pdu_t sent,
coap_pdu_t rcvd 
)
static

Definition at line 2898 of file net.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ handle_signaling()

static void handle_signaling ( coap_context_t context,
coap_session_t session,
coap_pdu_t pdu 
)
static

Definition at line 2939 of file net.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ no_response()

static enum respond_t no_response ( coap_pdu_t request,
coap_pdu_t response,
coap_session_t session 
)
static

Definition at line 2341 of file net.c.

+ Here is the caller graph for this function:

◆ token_match()

COAP_STATIC_INLINE int token_match ( const uint8_t *  a,
size_t  alen,
const uint8_t *  b,
size_t  blen 
)

Definition at line 1051 of file net.c.

+ Here is the caller graph for this function:

Variable Documentation

◆ coap_default_uri_wellknown

coap_str_const_t coap_default_uri_wellknown
static
Initial value:
=
(const uint8_t *)COAP_DEFAULT_URI_WELLKNOWN }
#define COAP_DEFAULT_URI_WELLKNOWN
well-known resources URI
Definition: pdu.h:49

Definition at line 2478 of file net.c.

◆ coap_started

int coap_started = 0
static

Definition at line 3220 of file net.c.