libcoap  4.1.1
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
Macros | Functions
net.c File Reference
#include "config.h"
#include <ctype.h>
#include <stdio.h>
#include <limits.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "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 is_wkc(Key)   (memcmp((Key), COAP_DEFAULT_WKC_HASHKEY, sizeof(coap_key_t)) == 0)
 Checks if Key is equal to the pre-defined hash key for.well-known/core. More...
 
#define INET6_ADDRSTRLEN   40
 
#define SZX_TO_BYTES(SZX)   ((size_t)(1 << ((SZX) + 4)))
 
#define WANT_WKC(Pdu, Key)   (((Pdu)->hdr->code == COAP_REQUEST_GET) && is_wkc(Key))
 

Functions

int print_wellknown (coap_context_t *, unsigned char *, size_t *, size_t, coap_opt_t *)
 Prints the names of all known resources to buf. More...
 
void coap_handle_failed_notify (coap_context_t *, const coap_address_t *, const str *)
 
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 node->t. 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 ()
 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...
 
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_free_context (coap_context_t *context)
 
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...
 
void coap_transaction_id (const coap_address_t *peer, const coap_pdu_t *pdu, coap_tid_t *id)
 Calculates a unique transaction id from given arguments peer and pdu. More...
 
coap_tid_t coap_send_ack (coap_context_t *context, const coap_address_t *dst, coap_pdu_t *request)
 Sends an ACK message with code 0 for the specified request to dst. More...
 
coap_tid_t coap_send (coap_context_t *context, const coap_address_t *dst, coap_pdu_t *pdu)
 Sends a non-confirmed CoAP message to given destination. More...
 
coap_tid_t coap_send_error (coap_context_t *context, coap_pdu_t *request, const coap_address_t *dst, 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_context_t *context, const coap_address_t *dst, coap_pdu_t *request, unsigned char type)
 Helper funktion to create and send a message with type (usually ACK or RST). More...
 
coap_tid_t coap_send_confirmed (coap_context_t *context, const coap_address_t *dst, coap_pdu_t *pdu)
 Sends a confirmed CoAP message to given destination. More...
 
coap_tid_t coap_retransmit (coap_context_t *context, coap_queue_t *node)
 Handles retransmissions of confirmable messages. More...
 
static int check_opt_size (coap_opt_t *opt, unsigned char *maxpos)
 Checks if opt fits into the message that ends with maxpos. More...
 
int coap_read (coap_context_t *ctx)
 Reads data from the network and tries to parse as CoAP PDU. More...
 
int coap_remove_from_queue (coap_queue_t **queue, coap_tid_t id, coap_queue_t **node)
 This function removes the element with given id from the list given list. More...
 
static int token_match (const unsigned char *a, size_t alen, const unsigned char *b, size_t blen)
 
void coap_cancel_all_messages (coap_context_t *context, const coap_address_t *dst, const unsigned char *token, size_t token_length)
 Cancels all outstanding messages for peer dst that have the specified token. More...
 
coap_queue_tcoap_find_transaction (coap_queue_t *queue, coap_tid_t id)
 Retrieves transaction from queue. More...
 
coap_pdu_tcoap_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...
 
static 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_twellknown_response (coap_context_t *context, coap_pdu_t *request)
 
void handle_request (coap_context_t *context, coap_queue_t *node)
 
static void handle_response (coap_context_t *context, coap_queue_t *sent, coap_queue_t *rcvd)
 
static int handle_locally (coap_context_t *context, coap_queue_t *node)
 
static void coap_handle_rst (coap_context_t *context, const coap_queue_t *sent)
 This function handles RST messages received for the message passed in sent. More...
 
void coap_dispatch (coap_context_t *context)
 Dispatches the PDUs from the receive queue in given context. 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...
 

Macro Definition Documentation

#define INET6_ADDRSTRLEN   40
#define is_wkc (   Key)    (memcmp((Key), COAP_DEFAULT_WKC_HASHKEY, sizeof(coap_key_t)) == 0)

Checks if Key is equal to the pre-defined hash key for.well-known/core.

Definition at line 278 of file net.c.

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

Definition at line 1163 of file net.c.

#define WANT_WKC (   Pdu,
  Key 
)    (((Pdu)->hdr->code == COAP_REQUEST_GET) && is_wkc(Key))

Definition at line 1279 of file net.c.

Function Documentation

static int check_opt_size ( coap_opt_t opt,
unsigned char *  maxpos 
)
inlinestatic

Checks if opt fits into the message that ends with maxpos.

This function returns 1 on success, or 0 if the option opt would exceed maxpos.

Definition at line 822 of file net.c.

unsigned int coap_adjust_basetime ( coap_context_t ctx,
coap_tick_t  now 
)

Set sendqueue_basetime in the given context object ctx to now.

This function returns the number of elements in the queue head that have timed out.

Definition at line 143 of file net.c.

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.

Definition at line 1555 of file net.c.

void coap_cancel_all_messages ( coap_context_t context,
const coap_address_t *  dst,
const unsigned char *  token,
size_t  token_length 
)

Cancels all outstanding messages for peer dst that have the specified token.

Parameters
contextThe context in use
dstDestination address of the messages to remove.
tokenMessage token
token_lengthActual length of token

Definition at line 1002 of file net.c.

void coap_delete_all ( coap_queue_t queue)

Removes all items from given queue and frees the allocated storage.

Definition at line 228 of file net.c.

int coap_delete_node ( coap_queue_t node)

Destroys specified node.

Definition at line 217 of file net.c.

void coap_dispatch ( coap_context_t context)

Dispatches the PDUs from the receive queue in given context.

Definition at line 1452 of file net.c.

coap_queue_t* coap_find_transaction ( coap_queue_t queue,
coap_tid_t  id 
)

Retrieves transaction from queue.

The transaction queue to be searched Unique key of the transaction to find.

Returns
A pointer to the transaction object or NULL if not found

Definition at line 1043 of file net.c.

void coap_free_context ( coap_context_t context)

Definition at line 419 of file net.c.

void coap_handle_failed_notify ( coap_context_t ,
const coap_address_t *  ,
const str  
)

Definition at line 841 of file resource.c.

static void coap_handle_rst ( coap_context_t context,
const coap_queue_t sent 
)
static

This function handles RST messages received for the message passed in sent.

Definition at line 1416 of file net.c.

int coap_insert_node ( coap_queue_t **  queue,
coap_queue_t node 
)

Adds node to given queue, ordered by node->t.

Definition at line 180 of file net.c.

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 296 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().

Parameters
requestSpecification of the received (confirmable) request.
codeThe error code to set.
optsAn option filter that specifies which options to copy from the original request in node.
Returns
A pointer to the new message or NULL on error.

Definition at line 1051 of file net.c.

coap_queue_t* coap_new_node ( )

Creates a new node suitable for adding to the CoAP sendqueue.

Definition at line 237 of file net.c.

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.

coap_opt_filter_t f = COAP_OPT_NONE;
if (coap_option_check_critical(ctx, pdu, f) == 0) {
coap_option_iterator_init(pdu, &opt_iter, f);
while (coap_option_next(&opt_iter)) {
if (opt_iter.type & 0x01) {
... handle unknown critical option in opt_iter ...
}
}
}
Parameters
ctxThe context where all known options are registered.
pduThe PDU to check.
unknownThe output filter that will be updated to indicate the unknown critical options found in pdu.
Returns
1 if everything was ok, 0 otherwise.

Definition at line 463 of file net.c.

coap_queue_t* coap_peek_next ( coap_context_t context)

Returns the next pdu to send without removing from sendqeue.

Definition at line 253 of file net.c.

coap_queue_t* coap_pop_next ( coap_context_t context)

Returns the next pdu to send and removes it from the sendqeue.

Definition at line 261 of file net.c.

int coap_read ( coap_context_t context)

Reads data from the network and tries to parse as CoAP PDU.

On success, 0 is returned and a new node with the parsed PDU is added to the receive queue in the specified context object.

Definition at line 831 of file net.c.

int coap_remove_from_queue ( coap_queue_t **  queue,
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.

Parameters
queueThe queue to search for id.
idThe node id to look for.
nodeIf found, node is updated to point to the removed node. You must release the storage pointed to by node manually.
Returns
1 if id was found, 0 otherwise.

Definition at line 952 of file net.c.

coap_tid_t coap_retransmit ( coap_context_t context,
coap_queue_t node 
)

Handles retransmissions of confirmable messages.

Definition at line 771 of file net.c.

coap_tid_t coap_send ( coap_context_t context,
const coap_address_t *  dst,
coap_pdu_t pdu 
)

Sends a non-confirmed CoAP message to given destination.

The memory that is allocated by pdu will not be released by coap_send(). The caller must release the memory.

Parameters
contextThe CoAP context to use.
dstThe address to send to.
pduThe CoAP PDU to send.
Returns
The message id of the sent message or COAP_INVALID_TID on error.

Definition at line 653 of file net.c.

coap_tid_t coap_send_ack ( coap_context_t context,
const coap_address_t *  dst,
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.

Parameters
contextThe context to use.
dstThe destination address.
requestThe request to be acknowledged.
Returns
The transaction id if ACK was sent or COAP_INVALID_TID on error.

Definition at line 534 of file net.c.

coap_tid_t coap_send_confirmed ( coap_context_t context,
const coap_address_t *  dst,
coap_pdu_t pdu 
)

Sends a confirmed CoAP message to given destination.

The memory that is allocated by pdu will not be released by coap_send_confirmed(). The caller must release the memory.

Parameters
contextThe CoAP context to use.
dstThe address to send to.
pduThe CoAP PDU to send.
Returns
The message id of the sent message or COAP_INVALID_TID on error.

Definition at line 699 of file net.c.

coap_tid_t coap_send_error ( coap_context_t context,
coap_pdu_t request,
const coap_address_t *  dst,
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.

Parameters
contextThe context to use.
requestThe original request to respond to.
dstThe remote peer that sent the request.
codeThe reponse code.
optsA filter that specifies the options to copy from the request.
Returns
The transaction id if the message was sent, or COAP_INVALID_TID otherwise.

Definition at line 660 of file net.c.

coap_tid_t coap_send_message_type ( coap_context_t context,
const coap_address_t *  dst,
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.

Parameters
contextThe CoAP context.
dstWhere to send the context.
requestThe request that should be responded to.
typeWhich type to set
Returns
transaction id on success or COAP_INVALID_TID otherwise.

Definition at line 681 of file net.c.

void coap_transaction_id ( const coap_address_t *  peer,
const coap_pdu_t pdu,
coap_tid_t id 
)

Calculates a unique transaction id from given arguments peer and pdu.

The id is returned in id.

Parameters
peerThe remote party who sent pdu.
pduThe message that initiated the transaction.
idSet to the new id.

Definition at line 498 of file net.c.

static size_t get_wkc_len ( coap_context_t context,
coap_opt_t query_filter 
)
inlinestatic

Quick hack to determine the size of the resource description for .well-known/core.

Definition at line 1148 of file net.c.

static int handle_locally ( coap_context_t context,
coap_queue_t node 
)
inlinestatic

Definition at line 1405 of file net.c.

void handle_request ( coap_context_t context,
coap_queue_t node 
)

Definition at line 1283 of file net.c.

static void handle_response ( coap_context_t context,
coap_queue_t sent,
coap_queue_t rcvd 
)
inlinestatic

Definition at line 1385 of file net.c.

int print_wellknown ( coap_context_t context,
unsigned char *  buf,
size_t *  buflen,
size_t  offset,
coap_opt_t query_filter 
)

Prints the names of all known resources to buf.

This function sets buflen to the number of bytes actually written and returns 1 on succes. On error, the value in buflen is undefined and the return value will be 0.

Parameters
contextThe context with the resource map.
bufThe buffer to write the result.
buflenMust be initialized to the maximum length of buf and will be set to the length of the well-known response on return.
offsetThe offset in bytes where the output shall start and is shifted accordingly with the characters that have been processed. This parameter is used to support the block option.
query_filterA filter query according to Link Format
Returns
COAP_PRINT_STATUS_ERROR on error. Otherwise, the lower 28 bits are set to the number of bytes that have actually been written to buf. COAP_PRINT_STATUS_TRUNC is set when the output has been truncated.

Definition at line 166 of file resource.c.

static int token_match ( const unsigned char *  a,
size_t  alen,
const unsigned char *  b,
size_t  blen 
)
inlinestatic

Definition at line 996 of file net.c.

coap_pdu_t* wellknown_response ( coap_context_t context,
coap_pdu_t request 
)

Definition at line 1166 of file net.c.