libcoap  4.3.0beta
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 "coap_prng.h"
31 #include "coap_session.h"
36 typedef struct coap_queue_t {
37  struct coap_queue_t *next;
39  unsigned char retransmit_cnt;
41  unsigned int timeout;
55 int coap_insert_node(coap_queue_t **queue, coap_queue_t *node);
73 void coap_delete_all(coap_queue_t *queue);
91 typedef void (*coap_response_handler_t)(struct coap_context_t *context,
92  coap_session_t *session,
93  coap_pdu_t *sent,
94  coap_pdu_t *received,
95  const coap_tid_t id);
106 typedef void (*coap_nack_handler_t)(struct coap_context_t *context,
107  coap_session_t *session,
108  coap_pdu_t *sent,
109  coap_nack_reason_t reason,
110  const coap_tid_t id);
120 typedef void (*coap_ping_handler_t)(struct coap_context_t *context,
121  coap_session_t *session,
122  coap_pdu_t *received,
123  const coap_tid_t id);
133 typedef void (*coap_pong_handler_t)(struct coap_context_t *context,
134  coap_session_t *session,
135  coap_pdu_t *received,
136  const coap_tid_t id);
141 typedef struct coap_context_t {
150 #ifndef WITHOUT_ASYNC
154 #endif /* WITHOUT_ASYNC */
164 #ifdef WITH_CONTIKI
165  struct uip_udp_conn *conn;
166  struct etimer retransmit_timer;
167  struct etimer notify_timer;
168 #endif /* WITH_CONTIKI */
170 #ifdef WITH_LWIP
171  uint8_t timer_configured;
174 #endif /* WITH_LWIP */
187  ssize_t (*network_send)(coap_socket_t *sock, const coap_session_t *session, const uint8_t *data, size_t datalen);
189  ssize_t (*network_read)(coap_socket_t *sock, struct coap_packet_t *packet);
191  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);
192  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);
193  size_t(*get_server_hint)(const coap_session_t *session, uint8_t *hint, size_t max_hint_len);
199  unsigned int session_timeout;
200  unsigned int max_idle_sessions;
201  unsigned int max_handshake_sessions;
202  unsigned int ping_timeout;
203  unsigned int csm_timeout;
209  void *app;
211  int epfd;
212  int eptimerfd;
213  coap_tick_t next_timeout;
214 #endif /* COAP_EPOLL_SUPPORT */
226  coap_response_handler_t handler) {
227  context->response_handler = handler;
228 }
241  coap_nack_handler_t handler) {
242  context->nack_handler = handler;
243 }
254  coap_ping_handler_t handler) {
255  context->ping_handler = handler;
256 }
267  coap_pong_handler_t handler) {
268  context->pong_handler = handler;
269 }
278 coap_register_option(coap_context_t *ctx, uint16_t type) {
280 }
287 unsigned int coap_adjust_basetime(coap_context_t *ctx, coap_tick_t now);
302 coap_context_t *coap_new_context(const coap_address_t *listen_addr);
316 int coap_context_set_psk( coap_context_t *context, const char *hint,
317  const uint8_t *key, size_t key_len );
329  coap_dtls_spsk_t *setup_data);
340 int
342  const coap_dtls_pki_t *setup_data);
355 int
357  const char *ca_file,
358  const char *ca_dir);
378 void coap_context_set_keepalive(coap_context_t *context, unsigned int seconds);
401  return ++session->tx_mid;
402 }
412 void coap_free_context(coap_context_t *context);
423 void coap_set_app_data(coap_context_t *context, void *data);
434 void *coap_get_app_data(const coap_context_t *context);
454  unsigned char code,
455  coap_opt_filter_t opts);
473  coap_pdu_t *request,
474  unsigned char code,
475  coap_opt_filter_t opts);
489 coap_send_message_type(coap_session_t *session, coap_pdu_t *request, unsigned char type);
517  return coap_send_message_type(session, request, COAP_MESSAGE_RST);
518 }
557 int coap_handle_dgram(coap_context_t *ctx, coap_session_t *session, uint8_t *data, size_t data_len);
569 int coap_handle_event(coap_context_t *context,
570  coap_event_t event,
571  coap_session_t *session);
589  coap_session_t *session,
590  coap_tid_t id,
591  coap_queue_t **node);
594 coap_wait_ack( coap_context_t *context, coap_session_t *session,
595  coap_queue_t *node);
618  coap_session_t *session,
619  const uint8_t *token,
620  size_t token_length);
629 void
631  coap_session_t *session,
632  coap_nack_reason_t reason);
637 void coap_dispatch(coap_context_t *context, coap_session_t *session,
638  coap_pdu_t *pdu);
643 int coap_can_exit(coap_context_t *context);
683  coap_pdu_t *pdu,
684  coap_opt_filter_t unknown);
698  coap_session_t *session,
699  coap_pdu_t *request);
714 unsigned int coap_calc_timeout(coap_session_t *session, unsigned char r);
724 int
725 coap_join_mcast_group(coap_context_t *ctx, const char *groupname);
733 #define COAP_IO_WAIT 0
734 #define COAP_IO_NO_WAIT ((uint32_t)-1)
764 int coap_io_process(coap_context_t *ctx, uint32_t timeout_ms);
766 #ifndef RIOT_VERSION
795  int nfds, fd_set *readfds, fd_set *writefds,
796  fd_set *exceptfds);
797 #endif /* !RIOT_VERSION */
840 unsigned int
842  coap_socket_t *sockets[],
843  unsigned int max_sockets,
844  unsigned int *num_sockets,
845  coap_tick_t now
846 );
882 unsigned int
885 struct epoll_event;
900 void coap_io_do_epoll(coap_context_t *ctx, struct epoll_event* events,
901  size_t nevents);
926 {
927  return coap_io_process(ctx, timeout_ms);
928 }
950  coap_socket_t *sockets[],
951  unsigned int max_sockets,
952  unsigned int *num_sockets,
953  coap_tick_t now
954 ) {
955  return coap_io_prepare_io(ctx, sockets, max_sockets, num_sockets, now);
956 }
970 ) {
971  coap_io_do_io(ctx, now);
972 }
974 /* Old definitions which may be hanging around in old code - be helpful! */
976 #define COAP_RUN_BLOCK COAP_RUN_BLOCK_deprecated_use_COAP_IO_WAIT
978 #endif /* COAP_NET_H_ */
Definition: coap_io.h:214
Pseudo Random Numbers.
Defines the application visible session information.
Clock Handling.
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 struct...
Definition: net.c:1589
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:991
void coap_io_do_epoll(coap_context_t *ctx, struct epoll_event *events, size_t nevents)
Process all the epoll events.
Definition: net.c:1650
unsigned int coap_io_prepare_io(coap_context_t *ctx, coap_socket_t *sockets[], unsigned int max_sockets, unsigned int *num_sockets, coap_tick_t now)
Iterates through all the coap_socket_t structures embedded in endpoints or sessions associated with t...
Definition: coap_io.c:1039
int coap_io_process(coap_context_t *ctx, uint32_t timeout_ms)
The main I/O processing function.
Definition: coap_io.c:1224
int coap_io_process_with_fds(coap_context_t *ctx, uint32_t timeout_ms, int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds)
The main message processing loop with additional fds for internal select.
Definition: coap_io.c:1229
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.h:120
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:57
unsigned int coap_event_t
Scalar type to represent different events, e.g.
Definition: coap_event.h:25
uint16_t coap_opt_filter_t[COAP_OPT_FILTER_SIZE]
Fixed-size vector we use for option filtering.
Definition: option.h:119
int coap_option_filter_set(coap_opt_filter_t filter, uint16_t type)
Sets the corresponding entry for type in filter.
Definition: option.c:494
Definition: libcoap.h:46
Definition: libcoap.h:38
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:253
coap_queue_t * coap_peek_next(coap_context_t *context)
Returns the next pdu to send without removing from sendqeue.
Definition: net.c:266
int coap_join_mcast_group(coap_context_t *ctx, const char *groupname)
Function interface for joining a multicast group for listening.
Definition: net.c:2946
int coap_context_set_psk2(coap_context_t *context, coap_dtls_spsk_t *setup_data)
Set the context's default PSK hint and/or key for a server.
Definition: net.c:400
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:149
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: net.c:2895
void coap_delete_all(coap_queue_t *queue)
Removes all items from given queue and frees the allocated storage.
Definition: net.c:243
int coap_context_get_coap_fd(coap_context_t *context)
Get the libcoap internal file descriptor for using in an application's select() or returned as an eve...
Definition: net.c:443
void(* coap_ping_handler_t)(struct coap_context_t *context, coap_session_t *session, coap_pdu_t *received, const coap_tid_t id)
Received Ping handler that is used as callback in coap_context_t.
Definition: net.h:120
int coap_delete_node(coap_queue_t *node)
Destroys specified node.
Definition: net.c:223
coap_queue_t * coap_new_node(void)
Creates a new node suitable for adding to the CoAP sendqueue.
Definition: net.c:252
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:1785
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:274
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:575
void coap_free_context(coap_context_t *context)
CoAP stack context must be released with coap_free_context().
Definition: net.c:581
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:266
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 callback in coap_context_t.
Definition: net.h:106
struct coap_queue_t coap_queue_t
Queue entry.
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:516
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:2884
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.
Definition: net.c:380
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:278
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:2051
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:876
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
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.
Definition: net.c:429
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:2713
coap_tid_t coap_wait_ack(coap_context_t *context, coap_session_t *session, coap_queue_t *node)
Definition: net.c:928
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:225
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:186
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',...
Definition: net.c:902
void(* coap_pong_handler_t)(struct coap_context_t *context, coap_session_t *session, coap_pdu_t *received, const coap_tid_t id)
Received Pong handler that is used as callback in coap_context_t.
Definition: net.h:133
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:453
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:1913
coap_tid_t coap_retransmit(coap_context_t *context, coap_queue_t *node)
Handles retransmissions of confirmable messages.
Definition: net.c:1191
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:649
int coap_context_set_pki(coap_context_t *context, const coap_dtls_pki_t *setup_data)
Set the context's default PKI information for a server.
Definition: net.c:414
coap_tid_t coap_send(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
Definition: net.c:1006
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:1872
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:858
COAP_STATIC_INLINE COAP_DEPRECATED unsigned int coap_write(coap_context_t *ctx, coap_socket_t *sockets[], unsigned int max_sockets, unsigned int *num_sockets, coap_tick_t now)
Definition: net.h:949
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:1921
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:701
COAP_STATIC_INLINE COAP_DEPRECATED int coap_run_once(coap_context_t *ctx, uint32_t timeout_ms)
Definition: net.h:925
struct coap_context_t coap_context_t
The CoAP stack's global state is stored in a coap_context_t object.
void coap_context_set_keepalive(coap_context_t *context, unsigned int seconds)
Set the context keepalive timer for sessions.
Definition: net.c:439
void coap_set_app_data(coap_context_t *context, void *data)
Stores data with the given CoAP context.
Definition: net.c:569
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:1835
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:400
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:240
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:1749
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 callback in coap_context_t.
Definition: net.h:91
COAP_STATIC_INLINE COAP_DEPRECATED void coap_read(coap_context_t *ctx, coap_tick_t now)
Definition: net.h:969
Helpers for handling options in CoAP PDUs.
Pre-defined constants that reflect defaults for CoAP.
Definition: pdu.h:77
int coap_tid_t
coap_tid_t is used to store CoAP transaction id, i.e.
Definition: pdu.h:244
multi-purpose address abstraction
Definition: address.h:94
The CoAP stack's global state is stored in a coap_context_t object.
Definition: net.h:141
coap_tick_t sendqueue_basetime
The time stamp in the first element of the sendqeue is relative to sendqueue_basetime.
Definition: net.h:159
coap_pong_handler_t pong_handler
Definition: net.h:179
ssize_t(* network_read)(coap_socket_t *sock, struct coap_packet_t *packet)
Definition: net.h:189
unsigned int csm_timeout
Timeout for waiting for a CSM from the remote side.
Definition: net.h:203
void * app
application-specific data
Definition: net.h:209
struct coap_resource_t * unknown_resource
can be used for handling unknown resources
Definition: net.h:145
int observe_pending
Observe response pending.
Definition: net.h:204
struct coap_async_state_t * async_state
list of asynchronous transactions
Definition: net.h:153
coap_session_t * sessions
client sessions
Definition: net.h:162
coap_nack_handler_t nack_handler
Definition: net.h:177
unsigned int ping_timeout
Minimum inactivity time before sending a ping message.
Definition: net.h:202
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)
Definition: net.h:192
ssize_t(* network_send)(coap_socket_t *sock, const coap_session_t *session, const uint8_t *data, size_t datalen)
Definition: net.h:187
uint16_t * cache_ignore_options
CoAP options to ignore when creating a cache-key.
Definition: net.h:207
void * dtls_context
Definition: net.h:195
coap_opt_filter_t known_options
Definition: net.h:142
coap_ping_handler_t ping_handler
Definition: net.h:178
size_t cache_ignore_count
The number of CoAP options to ignore when creating a cache-key.
Definition: net.h:208
unsigned int max_handshake_sessions
Maximum number of simultaneous negotating sessions per endpoint.
Definition: net.h:201
coap_queue_t * sendqueue
Definition: net.h:160
coap_response_handler_t response_handler
Definition: net.h:176
struct coap_resource_t * resources
hash table or list of known resources
Definition: net.h:143
coap_cache_entry_t * cache
CoAP cache-entry cache.
Definition: net.h:206
coap_endpoint_t * endpoint
the endpoints used for listening
Definition: net.h:161
size_t(* get_server_hint)(const coap_session_t *session, uint8_t *hint, size_t max_hint_len)
Definition: net.h:193
coap_event_handler_t handle_event
Callback function that is used to signal events to the application.
Definition: net.h:185
unsigned int session_timeout
Number of seconds of inactivity after which an unused session will be closed.
Definition: net.h:199
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)
Definition: net.h:191
coap_dtls_spsk_t spsk_setup_data
Contains the initial PSK server setup data.
Definition: net.h:197
struct coap_resource_t * proxy_uri_resource
can be used for handling proxy URI resources
Definition: net.h:147
unsigned int max_idle_sessions
Maximum number of simultaneous unused sessions per endpoint.
Definition: net.h:200
The structure used for defining the PKI setup data to be used.
Definition: coap_dtls.h:245
The structure used for defining the Server PSK setup data to be used.
Definition: coap_dtls.h:426
Abstraction of virtual endpoint that can be attached to coap_context_t.
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
Definition: pdu.h:287
Queue entry.
Definition: net.h:36
coap_session_t * session
the CoAP session
Definition: net.h:42
coap_pdu_t * pdu
the CoAP PDU to send
Definition: net.h:44
unsigned int timeout
the randomized timeout value
Definition: net.h:41
struct coap_queue_t * next
Definition: net.h:37
coap_tick_t t
when to send PDU for the next time
Definition: net.h:38
coap_tid_t id
CoAP transaction id.
Definition: net.h:43
unsigned char retransmit_cnt
retransmission counter, will be removed when zero
Definition: net.h:39
uint16_t tx_mid
the last message id that was used in this session
Definition: coap_session.h:75
unsigned int uint32_t
Definition: uthash.h:78
unsigned char uint8_t
Definition: uthash.h:79