libcoap  4.2.0
net.h
Go to the documentation of this file.
1 /*
2  * net.h -- CoAP network interface
3  *
4  * Copyright (C) 2010-2015 Olaf Bergmann <bergmann@tzi.org>
5  *
6  * This file is part of the CoAP library libcoap. Please see README for terms
7  * of use.
8  */
9 
10 #ifndef COAP_NET_H_
11 #define COAP_NET_H_
12 
13 #include <assert.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #ifndef _WIN32
17 #include <sys/time.h>
18 #endif
19 #include <time.h>
20 
21 #ifdef WITH_LWIP
22 #include <lwip/ip_addr.h>
23 #endif
24 
25 #include "coap_io.h"
26 #include "coap_dtls.h"
27 #include "coap_event.h"
28 #include "coap_time.h"
29 #include "option.h"
30 #include "pdu.h"
31 #include "prng.h"
32 #include "coap_session.h"
33 
34 struct coap_queue_t;
35 
39 typedef struct coap_queue_t {
40  struct coap_queue_t *next;
42  unsigned char retransmit_cnt;
44  unsigned int timeout;
48 } coap_queue_t;
49 
58 int coap_insert_node(coap_queue_t **queue, coap_queue_t *node);
59 
68 
74 void coap_delete_all(coap_queue_t *queue);
75 
82 
83 struct coap_resource_t;
84 struct coap_context_t;
85 #ifndef WITHOUT_ASYNC
86 struct coap_async_state_t;
87 #endif
88 
98 typedef void (*coap_response_handler_t)(struct coap_context_t *context,
100  coap_pdu_t *sent,
101  coap_pdu_t *received,
102  const coap_tid_t id);
103 
113 typedef void (*coap_nack_handler_t)(struct coap_context_t *context,
115  coap_pdu_t *sent,
116  coap_nack_reason_t reason,
117  const coap_tid_t id);
118 
127 typedef void (*coap_ping_handler_t)(struct coap_context_t *context,
129  coap_pdu_t *received,
130  const coap_tid_t id);
131 
140 typedef void (*coap_pong_handler_t)(struct coap_context_t *context,
142  coap_pdu_t *received,
143  const coap_tid_t id);
144 
148 typedef struct coap_context_t {
155 #ifndef WITHOUT_ASYNC
156 
159 #endif /* WITHOUT_ASYNC */
160 
169 #ifdef WITH_CONTIKI
170  struct uip_udp_conn *conn;
171  struct etimer retransmit_timer;
172  struct etimer notify_timer;
173 #endif /* WITH_CONTIKI */
174 
175 #ifdef WITH_LWIP
176  uint8_t timer_configured;
179 #endif /* WITH_LWIP */
180 
186  uint16_t message_id;
187 
192 
198 
199  ssize_t (*network_send)(coap_socket_t *sock, const coap_session_t *session, const uint8_t *data, size_t datalen);
200 
201  ssize_t (*network_read)(coap_socket_t *sock, struct coap_packet_t *packet);
202 
203  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);
204  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);
205  size_t(*get_server_hint)(const coap_session_t *session, uint8_t *hint, size_t max_hint_len);
206 
209  size_t psk_hint_len;
211  size_t psk_key_len;
212 
213  unsigned int session_timeout;
214  unsigned int max_idle_sessions;
215  unsigned int max_handshake_sessions;
216  unsigned int ping_timeout;
217  unsigned int csm_timeout;
219  void *app;
221 
231  coap_response_handler_t handler) {
232  context->response_handler = handler;
233 }
234 
246  coap_nack_handler_t handler) {
247  context->nack_handler = handler;
248 }
249 
259  coap_ping_handler_t handler) {
260  context->ping_handler = handler;
261 }
262 
272  coap_pong_handler_t handler) {
273  context->pong_handler = handler;
274 }
275 
283 coap_register_option(coap_context_t *ctx, uint16_t type) {
284  coap_option_setb(ctx->known_options, type);
285 }
286 
292 unsigned int coap_adjust_basetime(coap_context_t *ctx, coap_tick_t now);
293 
298 
303 
307 coap_context_t *coap_new_context(const coap_address_t *listen_addr);
308 
321 int coap_context_set_psk( coap_context_t *context, const char *hint,
322  const uint8_t *key, size_t key_len );
323 
333 int
335  coap_dtls_pki_t *setup_data);
336 
348 int
350  const char *ca_file,
351  const char *ca_dir);
352 
367 void coap_context_set_keepalive(coap_context_t *context, unsigned int seconds);
368 
378 COAP_STATIC_INLINE uint16_t
380  return ++session->tx_mid;
381 }
382 
391 void coap_free_context(coap_context_t *context);
392 
402 void coap_set_app_data(coap_context_t *context, void *data);
403 
413 void *coap_get_app_data(const coap_context_t *context);
414 
433  unsigned char code,
434  coap_opt_filter_t opts);
435 
452  coap_pdu_t *request,
453  unsigned char code,
454  coap_opt_filter_t opts);
455 
468 coap_send_message_type(coap_session_t *session, coap_pdu_t *request, unsigned char type);
469 
482 
496  return coap_send_message_type(session, request, COAP_MESSAGE_RST);
497 }
498 
511 
522 
543 unsigned int
545  coap_socket_t *sockets[],
546  unsigned int max_sockets,
547  unsigned int *num_sockets,
548  coap_tick_t now
549 );
550 
557 void coap_read(coap_context_t *ctx, coap_tick_t now);
558 
568 int coap_run_once( coap_context_t *ctx, unsigned int timeout_ms );
569 
583 int coap_handle_dgram(coap_context_t *ctx, coap_session_t *session, uint8_t *data, size_t data_len);
584 
595 int coap_handle_event(coap_context_t *context,
596  coap_event_t event,
597  coap_session_t *session);
615  coap_session_t *session,
616  coap_tid_t id,
617  coap_queue_t **node);
618 
620 coap_wait_ack( coap_context_t *context, coap_session_t *session,
621  coap_queue_t *node);
622 
633 
644  coap_session_t *session,
645  const uint8_t *token,
646  size_t token_length);
647 
655 void
657  coap_session_t *session,
658  coap_nack_reason_t reason);
659 
663 void coap_dispatch(coap_context_t *context, coap_session_t *session,
664  coap_pdu_t *pdu);
665 
669 int coap_can_exit(coap_context_t *context);
670 
675 void coap_ticks(coap_tick_t *);
676 
709  coap_pdu_t *pdu,
710  coap_opt_filter_t unknown);
711 
724  coap_session_t *session,
725  coap_pdu_t *request);
726 
740 unsigned int coap_calc_timeout(coap_session_t *session, unsigned char r);
741 
742 #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:245
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:1117
coap_queue_t * sendqueue
Definition: net.h:165
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:140
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:407
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:1525
coap_ping_handler_t ping_handler
Definition: net.h:190
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:512
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:278
coap_tid_t coap_send(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
Definition: net.c:876
unsigned char retransmit_cnt
retransmission counter, will be removed when zero
Definition: net.h:42
#define COAP_MESSAGE_RST
Definition: pdu.h:75
coap_endpoint_t * endpoint
the endpoints used for listening
Definition: net.h:166
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:72
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:754
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:561
size_t psk_key_len
Definition: net.h:211
uint8_t * psk_hint
Definition: net.h:208
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:392
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:189
struct coap_async_state_t * async_state
list of asynchronous transactions
Definition: net.h:158
Helpers for handling options in CoAP PDUs.
coap_nack_reason_t
Definition: coap_io.h:206
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:422
Abstraction of virtual endpoint that can be attached to coap_context_t.
Definition: coap_session.h:301
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:379
coap_tid_t coap_retransmit(coap_context_t *context, coap_queue_t *node)
Handles retransmissions of confirmable messages.
Definition: net.c:931
coap_pong_handler_t pong_handler
Definition: net.h:191
coap_tid_t id
CoAP transaction id.
Definition: net.h:46
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:230
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:258
coap_tick_t sendqueue_basetime
The time stamp in the first element of the sendqeue is relative to sendqueue_basetime.
Definition: net.h:164
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:495
coap_pdu_t * pdu
the CoAP PDU to send
Definition: net.h:47
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:1484
coap_tick_t t
when to send PDU for the next time
Definition: net.h:41
unsigned int ping_timeout
Minimum inactivity time before sending a ping message.
Definition: net.h:216
size_t psk_hint_len
Definition: net.h:209
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:271
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:1369
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:151
coap_response_handler_t response_handler
Definition: net.h:188
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.h:85
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:2321
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:1397
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:613
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:127
struct coap_queue_t * next
Definition: net.h:40
coap_event_handler_t handle_event
Callback function that is used to signal events to the application.
Definition: net.h:197
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:1533
unsigned int timeout
the randomized timeout value
Definition: net.h:44
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:113
void * dtls_context
Definition: net.h:207
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:1649
int coap_delete_node(coap_queue_t *node)
Destroys specified node.
Definition: net.c:225
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:349
#define COAP_STATIC_INLINE
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:152
The structure used for defining the PKI setup data to be used.
Definition: coap_dtls.h:191
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:2310
coap_queue_t * coap_peek_next(coap_context_t *context)
Returns the next pdu to send without removing from sendqeue.
Definition: net.c:270
unsigned int csm_timeout
Timeout for waiting for a CSM from the remote side.
Definition: net.h:217
uint8_t * psk_key
Definition: net.h:210
void coap_context_set_keepalive(coap_context_t *context, unsigned int seconds)
Set the context keepalive timer for sessions.
Definition: net.c:417
unsigned int max_handshake_sessions
Maximum number of simultaneous negotating sessions per endpoint.
Definition: net.h:215
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:98
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:219
void coap_delete_all(coap_queue_t *queue)
Removes all items from given queue and frees the allocated storage.
Definition: net.c:245
coap_session_t * sessions
client sessions
Definition: net.h:167
coap_session_t * session
the CoAP session
Definition: net.h:45
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:1319
coap_queue_t * coap_new_node(void)
Creates a new node suitable for adding to the CoAP sendqueue.
Definition: net.c:254
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:1447
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:2169
void coap_free_context(coap_context_t *context)
CoAP stack context must be released with coap_free_context().
Definition: net.c:518
uint16_t message_id
The last message id that was used is stored in this field.
Definition: net.h:186
void coap_set_app_data(coap_context_t *context, void *data)
Stores data with the given CoAP context.
Definition: net.c:506
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:283
Queue entry.
Definition: net.h:39
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:772
The CoAP stack&#39;s global state is stored in a coap_context_t object.
Definition: net.h:148
unsigned int max_idle_sessions
Maximum number of simultaneous unused sessions per endpoint.
Definition: net.h:214
struct coap_resource_t * resources
hash table or list of known resources
Definition: net.h:150
coap_tid_t coap_wait_ack(coap_context_t *context, coap_session_t *session, coap_queue_t *node)
Definition: net.c:824
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:798
unsigned int session_timeout
Number of seconds of inactivity after which an unused session will be closed.
Definition: net.h:213
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:188
coap_opt_filter_t known_options
Definition: net.h:149