libcoap  4.2.1
Go to the documentation of this file.
1 /*
2  * net.h -- CoAP network interface
3  *
4  * Copyright (C) 2010-2015 Olaf Bergmann <>
5  *
6  * This file is part of the CoAP library libcoap. Please see README for terms
7  * of use.
8  */
10 #ifndef COAP_NET_H_
11 #define COAP_NET_H_
13 #include <stdlib.h>
14 #include <string.h>
15 #ifndef _WIN32
16 #include <sys/time.h>
17 #endif
18 #include <time.h>
20 #ifdef WITH_LWIP
21 #include <lwip/ip_addr.h>
22 #endif
24 #include "coap_io.h"
25 #include "coap_dtls.h"
26 #include "coap_event.h"
27 #include "coap_time.h"
28 #include "option.h"
29 #include "pdu.h"
30 #include "prng.h"
31 #include "coap_session.h"
33 struct coap_queue_t;
38 typedef struct coap_queue_t {
39  struct coap_queue_t *next;
41  unsigned char retransmit_cnt;
43  unsigned int timeout;
47 } coap_queue_t;
57 int coap_insert_node(coap_queue_t **queue, coap_queue_t *node);
73 void coap_delete_all(coap_queue_t *queue);
82 struct coap_resource_t;
83 struct coap_context_t;
84 #ifndef WITHOUT_ASYNC
85 struct coap_async_state_t;
86 #endif
97 typedef void (*coap_response_handler_t)(struct coap_context_t *context,
99  coap_pdu_t *sent,
100  coap_pdu_t *received,
101  const coap_tid_t id);
112 typedef void (*coap_nack_handler_t)(struct coap_context_t *context,
114  coap_pdu_t *sent,
115  coap_nack_reason_t reason,
116  const coap_tid_t id);
126 typedef void (*coap_ping_handler_t)(struct coap_context_t *context,
128  coap_pdu_t *received,
129  const coap_tid_t id);
139 typedef void (*coap_pong_handler_t)(struct coap_context_t *context,
141  coap_pdu_t *received,
142  const coap_tid_t id);
147 typedef struct coap_context_t {
154 #ifndef WITHOUT_ASYNC
158 #endif /* WITHOUT_ASYNC */
168 #ifdef WITH_CONTIKI
169  struct uip_udp_conn *conn;
170  struct etimer retransmit_timer;
171  struct etimer notify_timer;
172 #endif /* WITH_CONTIKI */
174 #ifdef WITH_LWIP
175  uint8_t timer_configured;
178 #endif /* WITH_LWIP */
191  ssize_t (*network_send)(coap_socket_t *sock, const coap_session_t *session, const uint8_t *data, size_t datalen);
193  ssize_t (*network_read)(coap_socket_t *sock, struct coap_packet_t *packet);
195  size_t(*get_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);
196  size_t(*get_server_psk)(const coap_session_t *session, const uint8_t *identity, size_t identity_len, uint8_t *psk, size_t max_psk_len);
197  size_t(*get_server_hint)(const coap_session_t *session, uint8_t *hint, size_t max_hint_len);
201  size_t psk_hint_len;
203  size_t psk_key_len;
205  unsigned int session_timeout;
206  unsigned int max_idle_sessions;
207  unsigned int max_handshake_sessions;
208  unsigned int ping_timeout;
209  unsigned int csm_timeout;
211  void *app;
213  int epfd;
214  int eptimerfd;
215  coap_tick_t next_timeout;
216 #endif /* COAP_EPOLL_SUPPORT */
228  coap_response_handler_t handler) {
229  context->response_handler = handler;
230 }
243  coap_nack_handler_t handler) {
244  context->nack_handler = handler;
245 }
256  coap_ping_handler_t handler) {
257  context->ping_handler = handler;
258 }
269  coap_pong_handler_t handler) {
270  context->pong_handler = handler;
271 }
280 coap_register_option(coap_context_t *ctx, uint16_t type) {
281  coap_option_setb(ctx->known_options, type);
282 }
289 unsigned int coap_adjust_basetime(coap_context_t *ctx, coap_tick_t now);
304 coap_context_t *coap_new_context(const coap_address_t *listen_addr);
318 int coap_context_set_psk( coap_context_t *context, const char *hint,
319  const uint8_t *key, size_t key_len );
330 int
332  coap_dtls_pki_t *setup_data);
345 int
347  const char *ca_file,
348  const char *ca_dir);
368 void coap_context_set_keepalive(coap_context_t *context, unsigned int seconds);
391  return ++session->tx_mid;
392 }
402 void coap_free_context(coap_context_t *context);
413 void coap_set_app_data(coap_context_t *context, void *data);
424 void *coap_get_app_data(const coap_context_t *context);
444  unsigned char code,
445  coap_opt_filter_t opts);
463  coap_pdu_t *request,
464  unsigned char code,
465  coap_opt_filter_t opts);
479 coap_send_message_type(coap_session_t *session, coap_pdu_t *request, unsigned char type);
507  return coap_send_message_type(session, request, COAP_MESSAGE_RST);
508 }
554 unsigned int
556  coap_socket_t *sockets[],
557  unsigned int max_sockets,
558  unsigned int *num_sockets,
559  coap_tick_t now
560 );
568 void coap_read(coap_context_t *ctx, coap_tick_t now);
570 #define COAP_RUN_BLOCK 0
571 #define COAP_RUN_NONBLOCK 1
588 int coap_run_once( coap_context_t *ctx, unsigned int timeout_ms );
609 unsigned int
612 struct epoll_event;
623 void coap_io_do_events(coap_context_t *ctx, struct epoll_event* events,
624  size_t nevents);
639 int coap_handle_dgram(coap_context_t *ctx, coap_session_t *session, uint8_t *data, size_t data_len);
651 int coap_handle_event(coap_context_t *context,
652  coap_event_t event,
653  coap_session_t *session);
671  coap_session_t *session,
672  coap_tid_t id,
673  coap_queue_t **node);
676 coap_wait_ack( coap_context_t *context, coap_session_t *session,
677  coap_queue_t *node);
700  coap_session_t *session,
701  const uint8_t *token,
702  size_t token_length);
711 void
713  coap_session_t *session,
714  coap_nack_reason_t reason);
719 void coap_dispatch(coap_context_t *context, coap_session_t *session,
720  coap_pdu_t *pdu);
725 int coap_can_exit(coap_context_t *context);
731 void coap_ticks(coap_tick_t *);
765  coap_pdu_t *pdu,
766  coap_opt_filter_t unknown);
780  coap_session_t *session,
781  coap_pdu_t *request);
796 unsigned int coap_calc_timeout(coap_session_t *session, unsigned char r);
806 int
807 coap_join_mcast_group(coap_context_t *ctx, const char *groupname);
809 #endif /* COAP_NET_H_ */
COAP_STATIC_INLINE 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) i...
Definition: net.h:242
unsigned int coap_write(coap_context_t *ctx, coap_socket_t *sockets[], unsigned int max_sockets, unsigned int *num_sockets, coap_tick_t now)
For applications with their own message loop, send all pending retransmits and return the list of soc...
Definition: coap_io.c:1294
coap_queue_t * sendqueue
Definition: net.h:164
void(* coap_pong_handler_t)(struct coap_context_t *context, coap_session_t *session, coap_pdu_t *received, const coap_tid_t id)
Recieved Pong handler that is used as call-back in coap_context_t.
Definition: net.h:139
int coap_context_set_pki_root_cas(coap_context_t *context, const char *ca_file, const char *ca_dir)
Set the context&#39;s default Root CA information for a client or server.
Definition: net.c:399
coap_queue_t * coap_find_transaction(coap_queue_t *queue, coap_session_t *session, coap_tid_t id)
Retrieves transaction from the queue.
Definition: net.c:1713
coap_ping_handler_t ping_handler
Definition: net.h:182
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_a...
Definition: net.c:546
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:270
coap_tid_t coap_send(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
Definition: net.c:966
unsigned char retransmit_cnt
retransmission counter, will be removed when zero
Definition: net.h:41
Definition: pdu.h:75
coap_endpoint_t * endpoint
the endpoints used for listening
Definition: net.h:165
COAP_STATIC_INLINE int coap_option_setb(coap_opt_filter_t filter, uint16_t type)
Sets the corresponding bit for type in filter.
Definition: option.h:183
multi-purpose address abstraction
Definition: address.h:62
uint16_t tx_mid
the last message id that was used in this session
Definition: coap_session.h:74
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.
Definition: net.c:818
int coap_tid_t
coap_tid_t is used to store CoAP transaction id, i.e.
Definition: pdu.h:238
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:617
size_t psk_key_len
Definition: net.h:203
uint8_t * psk_hint
Definition: net.h:200
Clock Handling.
int coap_context_set_pki(coap_context_t *context, coap_dtls_pki_t *setup_data)
Set the context&#39;s default PKI information for a server.
Definition: net.c:384
struct coap_context_t coap_context_t
The CoAP stack&#39;s global state is stored in a coap_context_t object.
coap_nack_handler_t nack_handler
Definition: net.h:181
struct coap_async_state_t * async_state
list of asynchronous transactions
Definition: net.h:157
Helpers for handling options in CoAP PDUs.
Definition: coap_io.h:226
void coap_io_do_events(coap_context_t *ctx, struct epoll_event *events, size_t nevents)
Process all the epoll events.
Definition: net.c:1458
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:423
int coap_context_get_coap_fd(coap_context_t *context)
Get the libcoap internal file descriptor for using in an application&#39;s select() or returned as an eve...
Definition: net.c:413
Abstraction of virtual endpoint that can be attached to coap_context_t.
COAP_STATIC_INLINE uint16_t coap_new_message_id(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
Definition: net.h:390
coap_tid_t coap_retransmit(coap_context_t *context, coap_queue_t *node)
Handles retransmissions of confirmable messages.
Definition: net.c:1021
unsigned int coap_io_prepare_epoll(coap_context_t *ctx, coap_tick_t now)
Any now timed out delayed packet is transmitted, along with any packets associated with requested obs...
Definition: coap_io.c:1248
coap_pong_handler_t pong_handler
Definition: net.h:183
coap_tid_t id
CoAP transaction id.
Definition: net.h:45
COAP_STATIC_INLINE 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:227
COAP_STATIC_INLINE 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.
Definition: net.h:255
coap_tick_t sendqueue_basetime
The time stamp in the first element of the sendqeue is relative to sendqueue_basetime.
Definition: net.h:163
COAP_STATIC_INLINE coap_tid_t coap_send_rst(coap_session_t *session, coap_pdu_t *request)
Sends an RST message with code 0 for the specified request to dst.
Definition: net.h:506
coap_pdu_t * pdu
the CoAP PDU to send
Definition: net.h:46
int coap_join_mcast_group(coap_context_t *ctx, const char *groupname)
Function interface for joining a multicast group for listening.
Definition: net.c:2602
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.
Definition: net.c:1672
coap_tick_t t
when to send PDU for the next time
Definition: net.h:40
unsigned int ping_timeout
Minimum inactivity time before sending a ping message.
Definition: net.h:208
size_t psk_hint_len
Definition: net.h:201
COAP_STATIC_INLINE 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.
Definition: net.h:268
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
Definition: pdu.h:287
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.
Definition: net.c:1557
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:145
coap_response_handler_t response_handler
Definition: net.h:180
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.h:93
int coap_run_once(coap_context_t *ctx, unsigned int timeout_ms)
The main message processing loop.
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:2553
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.
Definition: net.c:1585
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.
Definition: net.c:669
void(* coap_ping_handler_t)(struct coap_context_t *context, coap_session_t *session, coap_pdu_t *received, const coap_tid_t id)
Recieved Ping handler that is used as call-back in coap_context_t.
Definition: net.h:126
struct coap_queue_t * next
Definition: net.h:39
coap_event_handler_t handle_event
Callback function that is used to signal events to the application.
Definition: net.h:189
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:1721
unsigned int timeout
the randomized timeout value
Definition: net.h:43
void(* coap_nack_handler_t)(struct coap_context_t *context, coap_session_t *session, coap_pdu_t *sent, coap_nack_reason_t reason, const coap_tid_t id)
Negative Acknowedge handler that is used as call-back in coap_context_t.
Definition: net.h:112
void * dtls_context
Definition: net.h:199
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.
Definition: net.c:1837
int coap_delete_node(coap_queue_t *node)
Destroys specified node.
Definition: net.c:219
int coap_context_set_psk(coap_context_t *context, const char *hint, const uint8_t *key, size_t key_len)
Set the context&#39;s default PSK hint and/or key for a server.
Definition: net.c:341
Defines the application visible session information.
Definition: libcoap.h:38
Pre-defined constants that reflect defaults for CoAP.
uint16_t coap_opt_filter_t[COAP_OPT_FILTER_SIZE]
Fixed-size vector we use for option filtering.
Definition: option.h:119
unsigned int coap_event_t
Scalar type to represent different events, e.g.
Definition: coap_event.h:28
struct coap_resource_t * unknown_resource
can be used for handling unknown resources
Definition: net.h:151
The structure used for defining the PKI setup data to be used.
Definition: coap_dtls.h:193
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.
Definition: net.c:2542
coap_queue_t * coap_peek_next(coap_context_t *context)
Returns the next pdu to send without removing from sendqeue.
Definition: net.c:262
unsigned int csm_timeout
Timeout for waiting for a CSM from the remote side.
Definition: net.h:209
uint8_t * psk_key
Definition: net.h:202
void coap_context_set_keepalive(coap_context_t *context, unsigned int seconds)
Set the context keepalive timer for sessions.
Definition: net.c:409
unsigned int max_handshake_sessions
Maximum number of simultaneous negotating sessions per endpoint.
Definition: net.h:207
void(* coap_response_handler_t)(struct coap_context_t *context, coap_session_t *session, coap_pdu_t *sent, coap_pdu_t *received, const coap_tid_t id)
Response handler that is used as call-back in coap_context_t.
Definition: net.h:97
int(* coap_event_handler_t)(struct coap_context_t *, coap_event_t event, struct coap_session_t *session)
Type for event handler functions that can be registered with a CoAP context using the unction coap_se...
Definition: coap_event.h:60
void * app
application-specific data
Definition: net.h:211
void coap_delete_all(coap_queue_t *queue)
Removes all items from given queue and frees the allocated storage.
Definition: net.c:239
coap_session_t * sessions
client sessions
Definition: net.h:166
coap_session_t * session
the CoAP session
Definition: net.h:44
unsigned char uint8_t
Definition: uthash.h:79
Pseudo Random Numbers.
void coap_read(coap_context_t *ctx, coap_tick_t now)
For applications with their own message loop, reads all data from the network.
Definition: net.c:1397
coap_queue_t * coap_new_node(void)
Creates a new node suitable for adding to the CoAP sendqueue.
Definition: net.c:248
struct coap_queue_t coap_queue_t
Queue entry.
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.
Definition: net.c:1635
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.
Definition: net.c:2373
void coap_free_context(coap_context_t *context)
CoAP stack context must be released with coap_free_context().
Definition: net.c:552
void coap_set_app_data(coap_context_t *context, void *data)
Stores data with the given CoAP context.
Definition: net.c:540
COAP_STATIC_INLINE void coap_register_option(coap_context_t *ctx, uint16_t type)
Registers the option type type with the given context object ctx.
Definition: net.h:280
Queue entry.
Definition: net.h:38
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).
Definition: net.c:836
The CoAP stack&#39;s global state is stored in a coap_context_t object.
Definition: net.h:147
unsigned int max_idle_sessions
Maximum number of simultaneous unused sessions per endpoint.
Definition: net.h:206
struct coap_resource_t * resources
hash table or list of known resources
Definition: net.h:149
coap_tid_t coap_wait_ack(coap_context_t *context, coap_session_t *session, coap_queue_t *node)
Definition: net.c:888
unsigned int coap_calc_timeout(coap_session_t *session, unsigned char r)
Calculates the initial timeout based on the session CoAP transmission parameters &#39;ack_timeout&#39;, &#39;ack_random_factor&#39;, and COAP_TICKS_PER_SECOND.
Definition: net.c:862
unsigned int session_timeout
Number of seconds of inactivity after which an unused session will be closed.
Definition: net.h:205
int coap_insert_node(coap_queue_t **queue, coap_queue_t *node)
Adds node to given queue, ordered by variable t in node.
Definition: net.c:182
coap_opt_filter_t known_options
Definition: net.h:148