net.h
Go to the documentation of this file.
/*
* net.h -- CoAP network interface
*
* Copyright (C) 2010-2015 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
#ifndef _COAP_NET_H_
#define _COAP_NET_H_
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#ifdef WITH_LWIP
#include <lwip/ip_addr.h>
#endif
#include "coap_io.h"
#include "coap_time.h"
#include "option.h"
#include "pdu.h"
#include "prng.h"
struct coap_queue_t;
typedef struct coap_queue_t {
struct coap_queue_t *next;
coap_tick_t t;
unsigned char retransmit_cnt;
unsigned int timeout;
coap_endpoint_t local_if;
coap_address_t remote;
coap_tid_t id;
coap_pdu_t *pdu;
} coap_queue_t;
int coap_insert_node(coap_queue_t **queue, coap_queue_t *node);
int coap_delete_node(coap_queue_t *node);
void coap_delete_all(coap_queue_t *queue);
coap_queue_t *coap_new_node(void);
struct coap_resource_t;
struct coap_context_t;
#ifndef WITHOUT_ASYNC
struct coap_async_state_t;
#endif
typedef void (*coap_response_handler_t)(struct coap_context_t *,
const coap_endpoint_t *local_interface,
const coap_address_t *remote,
coap_pdu_t *sent,
coap_pdu_t *received,
const coap_tid_t id);
#define COAP_MID_CACHE_SIZE 3
typedef struct {
unsigned char flags[COAP_MID_CACHE_SIZE];
coap_key_t item[COAP_MID_CACHE_SIZE];
} coap_mid_cache_t;
typedef struct coap_context_t {
coap_opt_filter_t known_options;
struct coap_resource_t *resources;
#ifndef WITHOUT_ASYNC
struct coap_async_state_t *async_state;
#endif /* WITHOUT_ASYNC */
coap_tick_t sendqueue_basetime;
coap_queue_t *sendqueue;
coap_endpoint_t *endpoint;
#ifdef WITH_POSIX
int sockfd;
#endif /* WITH_POSIX */
#ifdef WITH_CONTIKI
struct uip_udp_conn *conn;
struct etimer retransmit_timer;
struct etimer notify_timer;
#endif /* WITH_CONTIKI */
#ifdef WITH_LWIP
uint8_t timer_configured;
#endif /* WITH_LWIP */
unsigned short message_id;
unsigned int observe;
coap_response_handler_t response_handler;
ssize_t (*network_send)(struct coap_context_t *context,
const coap_endpoint_t *local_interface,
const coap_address_t *dst,
unsigned char *data, size_t datalen);
ssize_t (*network_read)(coap_endpoint_t *ep, coap_packet_t **packet);
} coap_context_t;
static inline void
coap_register_response_handler(coap_context_t *context,
coap_response_handler_t handler) {
context->response_handler = handler;
}
inline static void
coap_register_option(coap_context_t *ctx, unsigned char type) {
coap_option_setb(ctx->known_options, type);
}
unsigned int coap_adjust_basetime(coap_context_t *ctx, coap_tick_t now);
coap_queue_t *coap_peek_next( coap_context_t *context );
coap_queue_t *coap_pop_next( coap_context_t *context );
coap_context_t *coap_new_context(const coap_address_t *listen_addr);
static inline unsigned short
coap_new_message_id(coap_context_t *context) {
context->message_id++;
#ifndef WITH_CONTIKI
return htons(context->message_id);
#else /* WITH_CONTIKI */
return uip_htons(context->message_id);
#endif
}
void coap_free_context(coap_context_t *context);
coap_tid_t coap_send_confirmed(coap_context_t *context,
const coap_endpoint_t *local_interface,
const coap_address_t *dst,
coap_pdu_t *pdu);
coap_pdu_t *coap_new_error_response(coap_pdu_t *request,
unsigned char code,
coap_opt_filter_t opts);
coap_tid_t coap_send(coap_context_t *context,
const coap_endpoint_t *local_interface,
const coap_address_t *dst,
coap_pdu_t *pdu);
coap_tid_t coap_send_error(coap_context_t *context,
coap_pdu_t *request,
const coap_endpoint_t *local_interface,
const coap_address_t *dst,
unsigned char code,
coap_opt_filter_t opts);
coap_tid_t
coap_send_message_type(coap_context_t *context,
const coap_endpoint_t *local_interface,
const coap_address_t *dst,
coap_pdu_t *request,
unsigned char type);
coap_tid_t coap_send_ack(coap_context_t *context,
const coap_endpoint_t *local_interface,
const coap_address_t *dst,
coap_pdu_t *request);
static inline coap_tid_t
coap_send_rst(coap_context_t *context,
const coap_endpoint_t *local_interface,
const coap_address_t *dst,
coap_pdu_t *request) {
return coap_send_message_type(context,
local_interface,
dst, request,
COAP_MESSAGE_RST);
}
coap_tid_t coap_retransmit(coap_context_t *context, coap_queue_t *node);
int coap_read(coap_context_t *context);
int coap_handle_message(coap_context_t *ctx,
coap_packet_t *packet);
void coap_transaction_id(const coap_address_t *peer,
const coap_pdu_t *pdu,
coap_tid_t *id);
int coap_remove_from_queue(coap_queue_t **queue,
coap_tid_t id,
coap_queue_t **node);
inline static int
coap_remove_transaction(coap_queue_t **queue, coap_tid_t id) {
coap_queue_t *node;
if (!coap_remove_from_queue(queue, id, &node))
return 0;
coap_delete_node(node);
return 1;
}
coap_queue_t *coap_find_transaction(coap_queue_t *queue, coap_tid_t id);
void coap_cancel_all_messages(coap_context_t *context,
const coap_address_t *dst,
const unsigned char *token,
size_t token_length);
void coap_dispatch(coap_context_t *context, coap_queue_t *rcvd);
int coap_can_exit(coap_context_t *context);
void coap_ticks(coap_tick_t *);
int coap_option_check_critical(coap_context_t *ctx,
coap_pdu_t *pdu,
coap_opt_filter_t unknown);
coap_pdu_t *coap_wellknown_response(coap_context_t *context,
coap_pdu_t *request);
#endif /* _COAP_NET_H_ */
coap_context_t::sendqueue
coap_queue_t * sendqueue
Definition: net.h:90
coap_context_t::observe
unsigned int observe
The next value to be used for Observe.
Definition: net.h:120
coap_key_t
unsigned char coap_key_t[4]
Definition: hashkey.h:20
coap_pop_next
coap_queue_t * coap_pop_next(coap_context_t *context)
Returns the next pdu to send and removes it from the sendqeue.
Definition: net.c:298
coap_dispatch
void coap_dispatch(coap_context_t *context, coap_queue_t *rcvd)
Dispatches the PDUs from the receive queue in given context.
Definition: net.c:1564
coap_transaction_id
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.
Definition: net.c:495
coap_response_handler_t
void(* coap_response_handler_t)(struct coap_context_t *, const coap_endpoint_t *local_interface, const coap_address_t *remote, coap_pdu_t *sent, coap_pdu_t *received, const coap_tid_t id)
Message handler that is used as call-back in coap_context_t.
Definition: net.h:62
coap_queue_t::retransmit_cnt
unsigned char retransmit_cnt
retransmission counter, will be removed when zero
Definition: net.h:34
coap_context_t::network_read
ssize_t(* network_read)(coap_endpoint_t *ep, coap_packet_t **packet)
Definition: net.h:129
COAP_MESSAGE_RST
#define COAP_MESSAGE_RST
Definition: pdu.h:46
coap_context_t::endpoint
coap_endpoint_t * endpoint
the endpoint used for listening
Definition: net.h:91
coap_address_t
multi-purpose address abstraction
Definition: address.h:59
coap_tid_t
int coap_tid_t
coap_tid_t is used to store CoAP transaction id, i.e.
Definition: pdu.h:163
coap_option_check_critical
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.
Definition: net.c:443
coap_time.h
Clock Handling.
coap_register_option
static void coap_register_option(coap_context_t *ctx, unsigned char type)
Registers the option type type with the given context object ctx.
Definition: net.h:153
coap_queue_t::local_if
coap_endpoint_t local_if
the local address interface
Definition: net.h:37
coap_send_confirmed
coap_tid_t coap_send_confirmed(coap_context_t *context, const coap_endpoint_t *local_interface, const coap_address_t *dst, coap_pdu_t *pdu)
Sends a confirmed CoAP message to given destination.
Definition: net.c:699
coap_remove_transaction
static int coap_remove_transaction(coap_queue_t **queue, coap_tid_t id)
Removes the transaction identified by id from given queue.
Definition: net.h:424
coap_packet_t
Definition: coap_io.c:59
coap_send_rst
static coap_tid_t coap_send_rst(coap_context_t *context, const coap_endpoint_t *local_interface, const coap_address_t *dst, coap_pdu_t *request)
Sends an RST message with code 0 for the specified request to dst.
Definition: net.h:346
coap_queue_t::remote
coap_address_t remote
remote address
Definition: net.h:38
coap_context_t
struct coap_context_t coap_context_t
The CoAP stack&#39;s global state is stored in a coap_context_t object.
coap_find_transaction
coap_queue_t * coap_find_transaction(coap_queue_t *queue, coap_tid_t id)
Retrieves transaction from the queue.
Definition: net.c:1020
coap_context_t::async_state
struct coap_async_state_t * async_state
list of asynchronous transactions
Definition: net.h:83
option.h
Helpers for handling options in CoAP PDUs.
coap_option_setb
static int coap_option_setb(coap_opt_filter_t filter, unsigned short type)
Sets the corresponding bit for type in filter.
Definition: option.h:199
coap_new_context
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: net.c:333
coap_context_t::message_id
unsigned short message_id
The last message id that was used is stored in this field.
Definition: net.h:114
coap_async_state_t
Definition: async.h:31
coap_tick_t
unsigned long coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.h:84
coap_send_error
coap_tid_t coap_send_error(coap_context_t *context, coap_pdu_t *request, const coap_endpoint_t *local_interface, 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.
Definition: net.c:621
coap_endpoint_t
Abstraction of virtual endpoint that can be attached to coap_context_t.
Definition: coap_io.h:35
coap_retransmit
coap_tid_t coap_retransmit(coap_context_t *context, coap_queue_t *node)
Handles retransmissions of confirmable messages.
Definition: net.c:771
coap_queue_t::id
coap_tid_t id
unique transaction id
Definition: net.h:39
coap_register_response_handler
static 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 was received that matches an ongoi...
Definition: net.h:141
coap_context_t::sendqueue_basetime
coap_tick_t sendqueue_basetime
The time stamp in the first element of the sendqeue is relative to sendqueue_basetime.
Definition: net.h:89
coap_resource_t
Definition: resource.h:75
coap_queue_t::pdu
coap_pdu_t * pdu
the CoAP PDU to send
Definition: net.h:40
coap_queue_t::t
coap_tick_t t
when to send PDU for the next time
Definition: net.h:33
COAP_MID_CACHE_SIZE
#define COAP_MID_CACHE_SIZE
Definition: net.h:69
coap_pdu_t
Header structure for CoAP PDUs.
Definition: pdu.h:227
coap_adjust_basetime
unsigned int coap_adjust_basetime(coap_context_t *ctx, coap_tick_t now)
Set sendqueue_basetime in the given context object ctx to now.
Definition: net.c:180
coap_context_t::response_handler
coap_response_handler_t response_handler
Definition: net.h:122
coap_wellknown_response
coap_pdu_t * coap_wellknown_response(coap_context_t *context, coap_pdu_t *request)
Creates a new response for given request with the contents of .well-known/core.
Definition: net.c:1143
coap_can_exit
int coap_can_exit(coap_context_t *context)
Returns 1 if there are no messages to send or to dispatch in the context&#39;s queues.
Definition: net.c:1671
coap_queue_t::next
struct coap_queue_t * next
Definition: net.h:32
coap_ticks
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
Definition: coap_time.c:49
coap_new_error_response
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.
Definition: net.c:1028
coap_queue_t::timeout
unsigned int timeout
the randomized timeout value
Definition: net.h:36
coap_cancel_all_messages
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.
Definition: net.c:980
coap_remove_from_queue
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.
Definition: net.c:930
coap_delete_node
int coap_delete_node(coap_queue_t *node)
Destroys specified node.
Definition: net.c:254
coap_handle_message
int coap_handle_message(coap_context_t *ctx, coap_packet_t *packet)
Parses and interprets a CoAP message with context ctx.
Definition: net.c:845
pdu.h
Pre-defined constants that reflect defaults for CoAP.
coap_opt_filter_t
uint16_t coap_opt_filter_t[COAP_OPT_FILTER_SIZE]
Fixed-size vector we use for option filtering.
Definition: option.h:135
coap_io.h
coap_send_message_type
coap_tid_t coap_send_message_type(coap_context_t *context, const coap_endpoint_t *local_interface, 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).
Definition: net.c:643
coap_new_message_id
static unsigned short coap_new_message_id(coap_context_t *context)
Returns a new message id and updates context->message_id accordingly.
Definition: net.h:189
coap_peek_next
coap_queue_t * coap_peek_next(coap_context_t *context)
Returns the next pdu to send without removing from sendqeue.
Definition: net.c:290
coap_context_t::sockfd
int sockfd
send/receive socket
Definition: net.h:94
coap_read
int coap_read(coap_context_t *context)
Reads data from the network and tries to parse as CoAP PDU.
Definition: net.c:822
coap_send
coap_tid_t coap_send(coap_context_t *context, const coap_endpoint_t *local_interface, const coap_address_t *dst, coap_pdu_t *pdu)
Sends a non-confirmed CoAP message to given destination.
Definition: net.c:613
coap_delete_all
void coap_delete_all(coap_queue_t *queue)
Removes all items from given queue and frees the allocated storage.
Definition: net.c:265
prng.h
Pseudo Random Numbers.
coap_new_node
coap_queue_t * coap_new_node(void)
Creates a new node suitable for adding to the CoAP sendqueue.
Definition: net.c:274
coap_queue_t
struct coap_queue_t coap_queue_t
coap_free_context
void coap_free_context(coap_context_t *context)
CoAP stack context must be released with coap_free_context().
Definition: net.c:418
coap_mid_cache_t
Definition: net.h:70
coap_send_ack
coap_tid_t coap_send_ack(coap_context_t *context, const coap_endpoint_t *local_interface, const coap_address_t *dst, coap_pdu_t *request)
Sends an ACK message with code 0 for the specified request to dst.
Definition: net.c:533
coap_queue_t
Definition: net.h:31
coap_context_t
The CoAP stack&#39;s global state is stored in a coap_context_t object.
Definition: net.h:76
coap_context_t::resources
struct coap_resource_t * resources
hash table or list of known resources
Definition: net.h:78
coap_insert_node
int coap_insert_node(coap_queue_t **queue, coap_queue_t *node)
Adds node to given queue, ordered by node->t.
Definition: net.c:217
coap_context_t::known_options
coap_opt_filter_t known_options
Definition: net.h:77