libcoap  4.1.2
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 #include <sys/time.h>
17 #include <time.h>
18 
19 #ifdef WITH_LWIP
20 #include <lwip/ip_addr.h>
21 #endif
22 
23 #include "coap_io.h"
24 #include "coap_time.h"
25 #include "option.h"
26 #include "pdu.h"
27 #include "prng.h"
28 
29 struct coap_queue_t;
30 
31 typedef struct coap_queue_t {
32  struct coap_queue_t *next;
34  unsigned char retransmit_cnt;
36  unsigned int timeout;
41 } coap_queue_t;
42 
44 int coap_insert_node(coap_queue_t **queue, coap_queue_t *node);
45 
48 
50 void coap_delete_all(coap_queue_t *queue);
51 
54 
55 struct coap_resource_t;
56 struct coap_context_t;
57 #ifndef WITHOUT_ASYNC
58 struct coap_async_state_t;
59 #endif
60 
62 typedef void (*coap_response_handler_t)(struct coap_context_t *,
63  const coap_endpoint_t *local_interface,
64  const coap_address_t *remote,
65  coap_pdu_t *sent,
66  coap_pdu_t *received,
67  const coap_tid_t id);
68 
69 #define COAP_MID_CACHE_SIZE 3
70 typedef struct {
71  unsigned char flags[COAP_MID_CACHE_SIZE];
74 
76 typedef struct coap_context_t {
80 #ifndef WITHOUT_ASYNC
81 
84 #endif /* WITHOUT_ASYNC */
85 
93 #ifdef WITH_POSIX
94  int sockfd;
95 #endif /* WITH_POSIX */
96 
97 #ifdef WITH_CONTIKI
98  struct uip_udp_conn *conn;
99  struct etimer retransmit_timer;
100  struct etimer notify_timer;
101 #endif /* WITH_CONTIKI */
102 
103 #ifdef WITH_LWIP
104  uint8_t timer_configured;
107 #endif /* WITH_LWIP */
108 
114  unsigned short message_id;
115 
120  unsigned int observe;
121 
123 
124  ssize_t (*network_send)(struct coap_context_t *context,
125  const coap_endpoint_t *local_interface,
126  const coap_address_t *dst,
127  unsigned char *data, size_t datalen);
128 
129  ssize_t (*network_read)(coap_endpoint_t *ep, coap_packet_t **packet);
130 
132 
140 static inline void
142  coap_response_handler_t handler) {
143  context->response_handler = handler;
144 }
145 
152 inline static void
153 coap_register_option(coap_context_t *ctx, unsigned char type) {
154  coap_option_setb(ctx->known_options, type);
155 }
156 
162 unsigned int coap_adjust_basetime(coap_context_t *ctx, coap_tick_t now);
163 
168 
173 
177 coap_context_t *coap_new_context(const coap_address_t *listen_addr);
178 
188 static inline unsigned short
190  context->message_id++;
191 #ifndef WITH_CONTIKI
192  return htons(context->message_id);
193 #else /* WITH_CONTIKI */
194  return uip_htons(context->message_id);
195 #endif
196 }
197 
204 void coap_free_context(coap_context_t *context);
205 
206 
222  const coap_endpoint_t *local_interface,
223  const coap_address_t *dst,
224  coap_pdu_t *pdu);
225 
244  unsigned char code,
245  coap_opt_filter_t opts);
246 
262  const coap_endpoint_t *local_interface,
263  const coap_address_t *dst,
264  coap_pdu_t *pdu);
265 
285  coap_pdu_t *request,
286  const coap_endpoint_t *local_interface,
287  const coap_address_t *dst,
288  unsigned char code,
289  coap_opt_filter_t opts);
290 
307  const coap_endpoint_t *local_interface,
308  const coap_address_t *dst,
309  coap_pdu_t *request,
310  unsigned char type);
311 
327  const coap_endpoint_t *local_interface,
328  const coap_address_t *dst,
329  coap_pdu_t *request);
330 
345 static inline coap_tid_t
347  const coap_endpoint_t *local_interface,
348  const coap_address_t *dst,
349  coap_pdu_t *request) {
350  return coap_send_message_type(context,
351  local_interface,
352  dst, request,
354 }
355 
360 
366 int coap_read(coap_context_t *context);
367 
380  coap_packet_t *packet);
381 
390 void coap_transaction_id(const coap_address_t *peer,
391  const coap_pdu_t *pdu,
392  coap_tid_t *id);
393 
410  coap_tid_t id,
411  coap_queue_t **node);
412 
423 inline static int
425  coap_queue_t *node;
426  if (!coap_remove_from_queue(queue, id, &node))
427  return 0;
428 
429  coap_delete_node(node);
430  return 1;
431 }
432 
442 
453  const coap_address_t *dst,
454  const unsigned char *token,
455  size_t token_length);
456 
460 void coap_dispatch(coap_context_t *context, coap_queue_t *rcvd);
461 
465 int coap_can_exit(coap_context_t *context);
466 
471 void coap_ticks(coap_tick_t *);
472 
505  coap_pdu_t *pdu,
506  coap_opt_filter_t unknown);
507 
519  coap_pdu_t *request);
520 
521 #endif /* _COAP_NET_H_ */
coap_queue_t * sendqueue
Definition: net.h:90
unsigned int observe
The next value to be used for Observe.
Definition: net.h:120
unsigned char coap_key_t[4]
Definition: hashkey.h:20
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
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
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
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
unsigned char retransmit_cnt
retransmission counter, will be removed when zero
Definition: net.h:34
ssize_t(* network_read)(coap_endpoint_t *ep, coap_packet_t **packet)
Definition: net.h:129
#define COAP_MESSAGE_RST
Definition: pdu.h:46
coap_endpoint_t * endpoint
the endpoint used for listening
Definition: net.h:91
multi-purpose address abstraction
Definition: address.h:59
int coap_tid_t
coap_tid_t is used to store CoAP transaction id, i.e.
Definition: pdu.h:163
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
Clock Handling.
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_endpoint_t local_if
the local address interface
Definition: net.h:37
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
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
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_address_t remote
remote address
Definition: net.h:38
struct coap_context_t coap_context_t
The CoAP stack&#39;s global state is stored in a coap_context_t object.
coap_queue_t * coap_find_transaction(coap_queue_t *queue, coap_tid_t id)
Retrieves transaction from the queue.
Definition: net.c:1020
struct coap_async_state_t * async_state
list of asynchronous transactions
Definition: net.h:83
Helpers for handling options in CoAP PDUs.
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_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
unsigned short message_id
The last message id that was used is stored in this field.
Definition: net.h:114
unsigned long coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.h:84
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
Abstraction of virtual endpoint that can be attached to coap_context_t.
Definition: coap_io.h:35
coap_tid_t coap_retransmit(coap_context_t *context, coap_queue_t *node)
Handles retransmissions of confirmable messages.
Definition: net.c:771
coap_tid_t id
unique transaction id
Definition: net.h:39
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_tick_t sendqueue_basetime
The time stamp in the first element of the sendqeue is relative to sendqueue_basetime.
Definition: net.h:89
coap_pdu_t * pdu
the CoAP PDU to send
Definition: net.h:40
coap_tick_t t
when to send PDU for the next time
Definition: net.h:33
#define COAP_MID_CACHE_SIZE
Definition: net.h:69
Header structure for CoAP PDUs.
Definition: pdu.h:227
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_response_handler_t response_handler
Definition: net.h:122
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
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
struct coap_queue_t * next
Definition: net.h:32
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
Definition: coap_time.c:49
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
unsigned int timeout
the randomized timeout value
Definition: net.h:36
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
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
int coap_delete_node(coap_queue_t *node)
Destroys specified node.
Definition: net.c:254
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
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:135
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
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_queue_t * coap_peek_next(coap_context_t *context)
Returns the next pdu to send without removing from sendqeue.
Definition: net.c:290
int sockfd
send/receive socket
Definition: net.h:94
int coap_read(coap_context_t *context)
Reads data from the network and tries to parse as CoAP PDU.
Definition: net.c:822
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
void coap_delete_all(coap_queue_t *queue)
Removes all items from given queue and frees the allocated storage.
Definition: net.c:265
Pseudo Random Numbers.
coap_queue_t * coap_new_node(void)
Creates a new node suitable for adding to the CoAP sendqueue.
Definition: net.c:274
struct coap_queue_t coap_queue_t
void coap_free_context(coap_context_t *context)
CoAP stack context must be released with coap_free_context().
Definition: net.c:418
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
The CoAP stack&#39;s global state is stored in a coap_context_t object.
Definition: net.h:76
struct coap_resource_t * resources
hash table or list of known resources
Definition: net.h:78
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_opt_filter_t known_options
Definition: net.h:77