libcoap  4.1.1
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
net.c
Go to the documentation of this file.
1 /* net.c -- CoAP network interface
2  *
3  * Copyright (C) 2010--2014 Olaf Bergmann <bergmann@tzi.org>
4  *
5  * This file is part of the CoAP library libcoap. Please see
6  * README for terms of use.
7  */
8 
9 #include "config.h"
10 
11 #include <ctype.h>
12 #include <stdio.h>
13 #ifdef HAVE_LIMITS_H
14 #include <limits.h>
15 #endif
16 #ifdef HAVE_UNISTD_H
17 #include <unistd.h>
18 #elif HAVE_SYS_UNISTD_H
19 #include <sys/unistd.h>
20 #endif
21 #include <sys/types.h>
22 #ifdef HAVE_SYS_SOCKET_H
23 #include <sys/socket.h>
24 #endif
25 #ifdef HAVE_NETINET_IN_H
26 #include <netinet/in.h>
27 #endif
28 #ifdef HAVE_ARPA_INET_H
29 #include <arpa/inet.h>
30 #endif
31 
32 #ifdef WITH_LWIP
33 #include <lwip/pbuf.h>
34 #include <lwip/udp.h>
35 #include <lwip/timers.h>
36 #endif
37 
38 #include "debug.h"
39 #include "mem.h"
40 #include "str.h"
41 #include "async.h"
42 #include "resource.h"
43 #include "option.h"
44 #include "encode.h"
45 #include "block.h"
46 #include "net.h"
47 
48 #if defined(WITH_POSIX)
49 
50 time_t clock_offset;
51 
52 static inline coap_queue_t *
53 coap_malloc_node() {
54  return (coap_queue_t *)coap_malloc(sizeof(coap_queue_t));
55 }
56 
57 static inline void
58 coap_free_node(coap_queue_t *node) {
59  coap_free(node);
60 }
61 #endif /* WITH_POSIX */
62 #ifdef WITH_LWIP
63 
64 #include <lwip/memp.h>
65 
66 static void coap_retransmittimer_execute(void *arg);
67 static void coap_retransmittimer_restart(coap_context_t *ctx);
68 
69 static inline coap_queue_t *
70 coap_malloc_node() {
71  return (coap_queue_t *)memp_malloc(MEMP_COAP_NODE);
72 }
73 
74 static inline void
75 coap_free_node(coap_queue_t *node) {
76  memp_free(MEMP_COAP_NODE, node);
77 }
78 
79 #endif /* WITH_LWIP */
80 #ifdef WITH_CONTIKI
81 # ifndef DEBUG
82 # define DEBUG DEBUG_PRINT
83 # endif /* DEBUG */
84 
85 #include "memb.h"
86 #include "net/uip-debug.h"
87 
88 clock_time_t clock_offset;
89 
90 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
91 #define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[UIP_LLIPH_LEN])
92 
93 void coap_resources_init();
94 void coap_pdu_resources_init();
95 
96 unsigned char initialized = 0;
97 coap_context_t the_coap_context;
98 
99 MEMB(node_storage, coap_queue_t, COAP_PDU_MAXCNT);
100 
101 PROCESS(coap_retransmit_process, "message retransmit process");
102 
103 static inline coap_queue_t *
104 coap_malloc_node() {
105  return (coap_queue_t *)memb_alloc(&node_storage);
106 }
107 
108 static inline void
109 coap_free_node(coap_queue_t *node) {
110  memb_free(&node_storage, node);
111 }
112 #endif /* WITH_CONTIKI */
113 #ifdef WITH_LWIP
114 
122 static void received_package(void *arg, struct udp_pcb *upcb, struct pbuf *p, ip_addr_t *addr, u16_t port)
123 {
124  struct coap_context_t *context = (coap_context_t *)arg;
125 
126  LWIP_ASSERT("pending_package was not cleared.", context->pending_package == NULL);
127 
128  context->pending_package = p; /* we don't free it, coap_read has to do that */
129  context->pending_address.addr = addr->addr; /* FIXME: this has to become address-type independent, probably there'll be an lwip function for that */
130  context->pending_port = port;
131 
132  coap_read(context);
133 }
134 
135 #endif /* WITH_LWIP */
136 
137 int print_wellknown(coap_context_t *, unsigned char *, size_t *, size_t, coap_opt_t *);
138 
139 void coap_handle_failed_notify(coap_context_t *, const coap_address_t *,
140  const str *);
141 
142 unsigned int
143 coap_adjust_basetime(coap_context_t *ctx, coap_tick_t now) {
144  unsigned int result = 0;
145  coap_tick_diff_t delta = now - ctx->sendqueue_basetime;
146 
147  if (ctx->sendqueue) {
148  /* delta < 0 means that the new time stamp is before the old. */
149  if (delta <= 0) {
150  ctx->sendqueue->t -= delta;
151  } else {
152  /* This case is more complex: The time must be advanced forward,
153  * thus possibly leading to timed out elements at the queue's
154  * start. For every element that has timed out, its relative
155  * time is set to zero and the result counter is increased. */
156 
157  coap_queue_t *q = ctx->sendqueue;
158  coap_tick_t t = 0;
159  while (q && (t + q->t < (coap_tick_t)delta)) {
160  t += q->t;
161  q->t = 0;
162  result++;
163  q = q->next;
164  }
165 
166  /* finally adjust the first element that has not expired */
167  if (q) {
168  q->t = (coap_tick_t)delta - t;
169  }
170  }
171  }
172 
173  /* adjust basetime */
174  ctx->sendqueue_basetime += delta;
175 
176  return result;
177 }
178 
179 int
181  coap_queue_t *p, *q;
182  if ( !queue || !node )
183  return 0;
184 
185  /* set queue head if empty */
186  if ( !*queue ) {
187  *queue = node;
188  return 1;
189  }
190 
191  /* replace queue head if PDU's time is less than head's time */
192  q = *queue;
193  if (node->t < q->t) {
194  node->next = q;
195  *queue = node;
196  q->t -= node->t; /* make q->t relative to node->t */
197  return 1;
198  }
199 
200  /* search for right place to insert */
201  do {
202  node->t -= q->t; /* make node-> relative to q->t */
203  p = q;
204  q = q->next;
205  } while (q && q->t <= node->t);
206 
207  /* insert new item */
208  if (q) {
209  q->t -= node->t; /* make q->t relative to node->t */
210  }
211  node->next = q;
212  p->next = node;
213  return 1;
214 }
215 
216 int
218  if ( !node )
219  return 0;
220 
221  coap_delete_pdu(node->pdu);
222  coap_free_node(node);
223 
224  return 1;
225 }
226 
227 void
229  if ( !queue )
230  return;
231 
232  coap_delete_all( queue->next );
233  coap_delete_node( queue );
234 }
235 
236 coap_queue_t *
238  coap_queue_t *node;
239  node = coap_malloc_node();
240 
241  if ( ! node ) {
242 #ifndef NDEBUG
243  coap_log(LOG_WARNING, "coap_new_node: malloc\n");
244 #endif
245  return NULL;
246  }
247 
248  memset(node, 0, sizeof *node );
249  return node;
250 }
251 
252 coap_queue_t *
254  if ( !context || !context->sendqueue )
255  return NULL;
256 
257  return context->sendqueue;
258 }
259 
260 coap_queue_t *
262  coap_queue_t *next;
263 
264  if ( !context || !context->sendqueue )
265  return NULL;
266 
267  next = context->sendqueue;
268  context->sendqueue = context->sendqueue->next;
269  if (context->sendqueue) {
270  context->sendqueue->t += next->t;
271  }
272  next->next = NULL;
273  return next;
274 }
275 
276 #ifdef COAP_DEFAULT_WKC_HASHKEY
277 
278 #define is_wkc(Key) \
279  (memcmp((Key), COAP_DEFAULT_WKC_HASHKEY, sizeof(coap_key_t)) == 0)
280 #else
281 /* Implements a singleton to store a hash key for the .wellknown/core
282  * resources. */
283 int
284 is_wkc(coap_key_t k) {
285  static coap_key_t wkc;
286  static unsigned char _initialized = 0;
287  if (!_initialized) {
288  _initialized = coap_hash_path((unsigned char *)COAP_DEFAULT_URI_WELLKNOWN,
289  sizeof(COAP_DEFAULT_URI_WELLKNOWN) - 1, wkc);
290  }
291  return memcmp(k, wkc, sizeof(coap_key_t)) == 0;
292 }
293 #endif
294 
297  const coap_address_t *listen_addr) {
298 #ifdef WITH_POSIX
299  coap_context_t *c = coap_malloc( sizeof( coap_context_t ) );
300  int reuse = 1;
301 #endif /* WITH_POSIX */
302 #ifdef WITH_LWIP
303  coap_context_t *c = memp_malloc(MEMP_COAP_CONTEXT);
304 #endif /* WITH_LWIP */
305 #ifdef WITH_CONTIKI
306  coap_context_t *c;
307 
308  if (initialized)
309  return NULL;
310 #endif /* WITH_CONTIKI */
311 
312  if (!listen_addr) {
313  coap_log(LOG_EMERG, "no listen address specified\n");
314  return NULL;
315  }
316 
317  coap_clock_init();
318 #ifdef WITH_LWIP
319  prng_init(LWIP_RAND());
320 #else /* WITH_LWIP */
321  prng_init((unsigned long)listen_addr ^ clock_offset);
322 #endif /* WITH_LWIP */
323 
324 #ifndef WITH_CONTIKI
325  if ( !c ) {
326 #ifndef NDEBUG
327  coap_log(LOG_EMERG, "coap_init: malloc:\n");
328 #endif
329  return NULL;
330  }
331 #endif /* not WITH_CONTIKI */
332 #ifdef WITH_CONTIKI
333  coap_resources_init();
334  coap_pdu_resources_init();
335 
336  c = &the_coap_context;
337  initialized = 1;
338 #endif /* WITH_CONTIKI */
339 
340  memset(c, 0, sizeof( coap_context_t ) );
341 
342  /* initialize message id */
343  prng((unsigned char *)&c->message_id, sizeof(unsigned short));
344 
345  /* register the critical options that we know */
357 
358 #ifdef WITH_POSIX
359  c->sockfd = socket(listen_addr->addr.sa.sa_family, SOCK_DGRAM, 0);
360  if ( c->sockfd < 0 ) {
361 #ifndef NDEBUG
362  coap_log(LOG_EMERG, "coap_new_context: socket\n");
363 #endif /* WITH_POSIX */
364  goto onerror;
365  }
366 
367  if ( setsockopt( c->sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse) ) < 0 ) {
368 #ifndef NDEBUG
369  coap_log(LOG_WARNING, "setsockopt SO_REUSEADDR\n");
370 #endif
371  }
372 
373  if (bind(c->sockfd, &listen_addr->addr.sa, listen_addr->size) < 0) {
374 #ifndef NDEBUG
375  coap_log(LOG_EMERG, "coap_new_context: bind\n");
376 #endif
377  goto onerror;
378  }
379 
380  return c;
381 
382  onerror:
383  if ( c->sockfd >= 0 )
384  close ( c->sockfd );
385  coap_free( c );
386  return NULL;
387 
388 #endif /* WITH_POSIX */
389 #ifdef WITH_CONTIKI
390  c->conn = udp_new(NULL, 0, NULL);
391  udp_bind(c->conn, listen_addr->port);
392 
393  process_start(&coap_retransmit_process, (char *)c);
394 
395  PROCESS_CONTEXT_BEGIN(&coap_retransmit_process);
396 #ifndef WITHOUT_OBSERVE
397  etimer_set(&c->notify_timer, COAP_RESOURCE_CHECK_TIME * COAP_TICKS_PER_SECOND);
398 #endif /* WITHOUT_OBSERVE */
399  /* the retransmit timer must be initialized to some large value */
400  etimer_set(&the_coap_context.retransmit_timer, 0xFFFF);
401  PROCESS_CONTEXT_END(&coap_retransmit_process);
402  return c;
403 #endif /* WITH_CONTIKI */
404 #ifdef WITH_LWIP
405  c->pcb = udp_new();
406  /* hard assert: this is not expected to fail dynamically */
407  LWIP_ASSERT("Failed to allocate PCB for CoAP", c->pcb != NULL);
408 
409  udp_recv(c->pcb, received_package, (void*)c);
410  udp_bind(c->pcb, &listen_addr->addr, listen_addr->port);
411 
412  c->timer_configured = 0;
413 
414  return c;
415 #endif
416 }
417 
418 void
420 #if defined(WITH_POSIX) || defined(WITH_LWIP)
421  coap_resource_t *res;
422 #ifndef COAP_RESOURCES_NOHASH
423  coap_resource_t *rtmp;
424 #endif
425 #endif /* WITH_POSIX || WITH_LWIP */
426  if ( !context )
427  return;
428 
429  coap_delete_all(context->recvqueue);
430  coap_delete_all(context->sendqueue);
431 
432 #ifdef WITH_LWIP
433  context->sendqueue = NULL;
434  coap_retransmittimer_restart(context);
435 #endif
436 
437 #if defined(WITH_POSIX) || defined(WITH_LWIP)
438 #ifdef COAP_RESOURCES_NOHASH
439  LL_FOREACH(context->resources, res) {
440 #else
441  HASH_ITER(hh, context->resources, res, rtmp) {
442 #endif
443  coap_delete_resource(context, res->key);
444  }
445 #endif /* WITH_POSIX || WITH_LWIP */
446 
447 #ifdef WITH_POSIX
448  /* coap_delete_list(context->subscriptions); */
449  close( context->sockfd );
450  coap_free( context );
451 #endif
452 #ifdef WITH_LWIP
453  udp_remove(context->pcb);
454  memp_free(MEMP_COAP_CONTEXT, context);
455 #endif
456 #ifdef WITH_CONTIKI
457  memset(&the_coap_context, 0, sizeof(coap_context_t));
458  initialized = 0;
459 #endif /* WITH_CONTIKI */
460 }
461 
462 int
464  coap_pdu_t *pdu,
465  coap_opt_filter_t unknown) {
466 
467  coap_opt_iterator_t opt_iter;
468  int ok = 1;
469 
470  coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL);
471 
472  while (coap_option_next(&opt_iter)) {
473 
474  /* The following condition makes use of the fact that
475  * coap_option_getb() returns -1 if type exceeds the bit-vector
476  * filter. As the vector is supposed to be large enough to hold
477  * the largest known option, we know that everything beyond is
478  * bad.
479  */
480  if (opt_iter.type & 0x01 &&
481  coap_option_getb(ctx->known_options, opt_iter.type) < 1) {
482  debug("unknown critical option %d\n", opt_iter.type);
483 
484  ok = 0;
485 
486  /* When opt_iter.type is beyond our known option range,
487  * coap_option_setb() will return -1 and we are safe to leave
488  * this loop. */
489  if (coap_option_setb(unknown, opt_iter.type) == -1)
490  break;
491  }
492  }
493 
494  return ok;
495 }
496 
497 void
498 coap_transaction_id(const coap_address_t *peer, const coap_pdu_t *pdu,
499  coap_tid_t *id) {
500  coap_key_t h;
501 
502  memset(h, 0, sizeof(coap_key_t));
503 
504  /* Compare the complete address structure in case of IPv4. For IPv6,
505  * we need to look at the transport address only. */
506 
507 #ifdef WITH_POSIX
508  switch (peer->addr.sa.sa_family) {
509  case AF_INET:
510  coap_hash((const unsigned char *)&peer->addr.sa, peer->size, h);
511  break;
512  case AF_INET6:
513  coap_hash((const unsigned char *)&peer->addr.sin6.sin6_port,
514  sizeof(peer->addr.sin6.sin6_port), h);
515  coap_hash((const unsigned char *)&peer->addr.sin6.sin6_addr,
516  sizeof(peer->addr.sin6.sin6_addr), h);
517  break;
518  default:
519  return;
520  }
521 #endif
522 #if defined(WITH_LWIP) || defined(WITH_CONTIKI)
523  /* FIXME: with lwip, we can do better */
524  coap_hash((const unsigned char *)&peer->port, sizeof(peer->port), h);
525  coap_hash((const unsigned char *)&peer->addr, sizeof(peer->addr), h);
526 #endif /* WITH_LWIP || WITH_CONTIKI */
527 
528  coap_hash((const unsigned char *)&pdu->hdr->id, sizeof(unsigned short), h);
529 
530  *id = ((h[0] << 8) | h[1]) ^ ((h[2] << 8) | h[3]);
531 }
532 
535  const coap_address_t *dst,
536  coap_pdu_t *request) {
537  coap_pdu_t *response;
538  coap_tid_t result = COAP_INVALID_TID;
539 
540  if (request && request->hdr->type == COAP_MESSAGE_CON) {
541  response = coap_pdu_init(COAP_MESSAGE_ACK, 0, request->hdr->id,
542  sizeof(coap_pdu_t));
543  if (response) {
544  result = coap_send(context, dst, response);
545  coap_delete_pdu(response);
546  }
547  }
548  return result;
549 }
550 
551 #ifdef WITH_POSIX
552 /* releases space allocated by PDU if free_pdu is set */
554 coap_send_impl(coap_context_t *context,
555  const coap_address_t *dst,
556  coap_pdu_t *pdu) {
557  ssize_t bytes_written;
559 
560  if ( !context || !dst || !pdu )
561  return id;
562 
563  bytes_written = sendto( context->sockfd, pdu->hdr, pdu->length, 0,
564  &dst->addr.sa, dst->size);
565 
566  if (bytes_written >= 0) {
567  coap_transaction_id(dst, pdu, &id);
568  } else {
569  coap_log(LOG_CRIT, "coap_send: sendto\n");
570  }
571 
572  return id;
573 }
574 #endif /* WITH_POSIX */
575 #ifdef WITH_CONTIKI
576 /* releases space allocated by PDU if free_pdu is set */
578 coap_send_impl(coap_context_t *context,
579  const coap_address_t *dst,
580  coap_pdu_t *pdu) {
582 
583  if ( !context || !dst || !pdu )
584  return id;
585 
586  /* FIXME: is there a way to check if send was successful? */
587  uip_udp_packet_sendto(context->conn, pdu->hdr, pdu->length,
588  &dst->addr, dst->port);
589 
590  coap_transaction_id(dst, pdu, &id);
591 
592  return id;
593 }
594 #endif /* WITH_CONTIKI */
595 #ifdef WITH_LWIP
597 coap_send_impl(coap_context_t *context,
598  const coap_address_t *dst,
599  coap_pdu_t *pdu) {
601  struct pbuf *p;
602  uint8_t err;
603  char *data_backup;
604 
605  if ( !context || !dst || !pdu )
606  {
607  return id;
608  }
609 
610  data_backup = pdu->data;
611 
612  /* FIXME: we can't check this here with the existing infrastructure, but we
613  * should actually check that the pdu is not held by anyone but us. the
614  * respective pbuf is already exclusively owned by the pdu. */
615 
616  p = pdu->pbuf;
617  LWIP_ASSERT("The PDU header is not where it is expected", pdu->hdr == p->payload + sizeof(coap_pdu_t));
618 
619  err = pbuf_header(p, -sizeof(coap_pdu_t));
620  if (err)
621  {
622  debug("coap_send_impl: pbuf_header failed\n");
623  pbuf_free(p);
624  return id;
625  }
626 
627  coap_transaction_id(dst, pdu, &id);
628 
629  pbuf_realloc(p, pdu->length);
630 
631  udp_sendto(context->pcb, p,
632  &dst->addr, dst->port);
633 
634  pbuf_header(p, -(ptrdiff_t)((uint8_t*)pdu - (uint8_t*)p->payload) - sizeof(coap_pdu_t)); /* FIXME hack around udp_sendto not restoring; see http://lists.gnu.org/archive/html/lwip-users/2013-06/msg00008.html. for udp over ip over ethernet, this was -42; as we're doing ppp too, this has to be calculated generically */
635 
636  err = pbuf_header(p, sizeof(coap_pdu_t));
637  LWIP_ASSERT("Cannot undo pbuf_header", err == 0);
638 
639  /* restore destroyed pdu data */
640  LWIP_ASSERT("PDU not restored", p->payload == pdu);
641  pdu->max_size = p->tot_len - sizeof(coap_pdu_t); /* reduced after pbuf_realloc */
642  pdu->hdr = p->payload + sizeof(coap_pdu_t);
643  pdu->max_delta = 0; /* won't be used any more */
644  pdu->length = pdu->max_size;
645  pdu->data = data_backup;
646  pdu->pbuf = p;
647 
648  return id;
649 }
650 #endif /* WITH_LWIP */
651 
652 coap_tid_t
654  const coap_address_t *dst,
655  coap_pdu_t *pdu) {
656  return coap_send_impl(context, dst, pdu);
657 }
658 
661  coap_pdu_t *request,
662  const coap_address_t *dst,
663  unsigned char code,
664  coap_opt_filter_t opts) {
665  coap_pdu_t *response;
666  coap_tid_t result = COAP_INVALID_TID;
667 
668  assert(request);
669  assert(dst);
670 
671  response = coap_new_error_response(request, code, opts);
672  if (response) {
673  result = coap_send(context, dst, response);
674  coap_delete_pdu(response);
675  }
676 
677  return result;
678 }
679 
682  const coap_address_t *dst,
683  coap_pdu_t *request,
684  unsigned char type) {
685  coap_pdu_t *response;
686  coap_tid_t result = COAP_INVALID_TID;
687 
688  if (request) {
689  response = coap_pdu_init(type, 0, request->hdr->id, sizeof(coap_pdu_t));
690  if (response) {
691  result = coap_send(context, dst, response);
692  coap_delete_pdu(response);
693  }
694  }
695  return result;
696 }
697 
700  const coap_address_t *dst,
701  coap_pdu_t *pdu) {
702  coap_queue_t *node;
703  coap_tick_t now;
704  int r;
705 
706  node = coap_new_node();
707  if (!node) {
708  debug("coap_send_confirmed: insufficient memory\n");
709  return COAP_INVALID_TID;
710  }
711 
712  node->id = coap_send_impl(context, dst, pdu);
713  if (COAP_INVALID_TID == node->id) {
714  debug("coap_send_confirmed: error sending pdu\n");
715  coap_free_node(node);
716  return COAP_INVALID_TID;
717  }
718 
719  prng((unsigned char *)&r,sizeof(r));
720 
721  /* add randomized RESPONSE_TIMEOUT to determine retransmission timeout */
722  node->timeout = COAP_DEFAULT_RESPONSE_TIMEOUT * COAP_TICKS_PER_SECOND +
724  ((COAP_TICKS_PER_SECOND * (r & 0xFF)) >> 8);
725 
726  memcpy(&node->remote, dst, sizeof(coap_address_t));
727  node->pdu = pdu;
728 
729  /* Set timer for pdu retransmission. If this is the first element in
730  * the retransmission queue, the base time is set to the current
731  * time and the retransmission time is node->timeout. If there is
732  * already an entry in the sendqueue, we must check if this node is
733  * to be retransmitted earlier. Therefore, node->timeout is first
734  * normalized to the base time and then inserted into the queue with
735  * an adjusted relative time.
736  */
737  coap_ticks(&now);
738  if (context->sendqueue == NULL) {
739  node->t = node->timeout;
740  context->sendqueue_basetime = now;
741  } else {
742  /* make node->t relative to context->sendqueue_basetime */
743  node->t = (now - context->sendqueue_basetime) + node->timeout;
744  }
745 
746  coap_insert_node(&context->sendqueue, node);
747 
748 #ifdef WITH_LWIP
749  if (node == context->sendqueue) /* don't bother with timer stuff if there are earlier retransmits */
750  coap_retransmittimer_restart(context);
751 #endif
752 
753 #ifdef WITH_CONTIKI
754  { /* (re-)initialize retransmission timer */
755  coap_queue_t *nextpdu;
756 
757  nextpdu = coap_peek_next(context);
758  assert(nextpdu); /* we have just inserted a node */
759 
760  /* must set timer within the context of the retransmit process */
761  PROCESS_CONTEXT_BEGIN(&coap_retransmit_process);
762  etimer_set(&context->retransmit_timer, nextpdu->t);
763  PROCESS_CONTEXT_END(&coap_retransmit_process);
764  }
765 #endif /* WITH_CONTIKI */
766 
767  return node->id;
768 }
769 
772  if ( !context || !node )
773  return COAP_INVALID_TID;
774 
775  /* re-initialize timeout when maximum number of retransmissions are not reached yet */
777  node->retransmit_cnt++;
778  node->t = node->timeout << node->retransmit_cnt;
779  coap_insert_node(&context->sendqueue, node);
780 #ifdef WITH_LWIP
781  if (node == context->sendqueue) /* don't bother with timer stuff if there are earlier retransmits */
782  coap_retransmittimer_restart(context);
783 #endif
784 
785  debug("** retransmission #%d of transaction %d\n",
786  node->retransmit_cnt, ntohs(node->pdu->hdr->id));
787 
788  node->id = coap_send_impl(context, &node->remote, node->pdu);
789  return node->id;
790  }
791 
792  /* no more retransmissions, remove node from system */
793 
794 #ifndef WITH_CONTIKI
795  debug("** removed transaction %d\n", ntohs(node->id));
796 #endif
797 
798 #ifndef WITHOUT_OBSERVE
799  /* Check if subscriptions exist that should be canceled after
800  COAP_MAX_NOTIFY_FAILURES */
801  if (node->pdu->hdr->code >= 64) {
802  str token = { 0, NULL };
803 
804  token.length = node->pdu->hdr->token_length;
805  token.s = node->pdu->hdr->token;
806 
807  coap_handle_failed_notify(context, &node->remote, &token);
808  }
809 #endif /* WITHOUT_OBSERVE */
810 
811  /* And finally delete the node */
812  coap_delete_node( node );
813  return COAP_INVALID_TID;
814 }
815 
821 static inline int
822 check_opt_size(coap_opt_t *opt, unsigned char *maxpos) {
823  if (opt && opt < maxpos) {
824  if (((*opt & 0x0f) < 0x0f) || (opt + 1 < maxpos))
825  return opt + COAP_OPT_SIZE(opt) < maxpos;
826  }
827  return 0;
828 }
829 
830 int
832 #ifdef WITH_POSIX
833  static char buf[COAP_MAX_PDU_SIZE];
834 #endif
835 #if defined(WITH_LWIP) || defined(WITH_CONTIKI)
836  char *buf;
837 #endif
838  coap_hdr_t *pdu;
839  ssize_t bytes_read = -1;
840  coap_address_t src, dst;
841  coap_queue_t *node;
842 
843 #ifdef WITH_CONTIKI
844  buf = uip_appdata;
845 #endif /* WITH_CONTIKI */
846 #ifdef WITH_LWIP
847  LWIP_ASSERT("No package pending", ctx->pending_package != NULL);
848  LWIP_ASSERT("Can only deal with contiguous PBUFs to read the initial details", ctx->pending_package->tot_len == ctx->pending_package->len);
849  buf = ctx->pending_package->payload;
850 #endif /* WITH_LWIP */
851 
852  pdu = (coap_hdr_t *)buf;
853 
854  coap_address_init(&src);
855 
856 #ifdef WITH_POSIX
857  bytes_read = recvfrom(ctx->sockfd, buf, sizeof(buf), 0,
858  &src.addr.sa, &src.size);
859 #endif /* WITH_POSIX */
860 #ifdef WITH_CONTIKI
861  if(uip_newdata()) {
862  uip_ipaddr_copy(&src.addr, &UIP_IP_BUF->srcipaddr);
863  src.port = UIP_UDP_BUF->srcport;
864  uip_ipaddr_copy(&dst.addr, &UIP_IP_BUF->destipaddr);
865  dst.port = UIP_UDP_BUF->destport;
866 
867  bytes_read = uip_datalen();
868  ((char *)uip_appdata)[bytes_read] = 0;
869  PRINTF("Server received %d bytes from [", (int)bytes_read);
870  PRINT6ADDR(&src.addr);
871  PRINTF("]:%d\n", uip_ntohs(src.port));
872  }
873 #endif /* WITH_CONTIKI */
874 #ifdef WITH_LWIP
875  /* FIXME: use lwip address operation functions */
876  src.addr.addr = ctx->pending_address.addr;
877  src.port = ctx->pending_port;
878  bytes_read = ctx->pending_package->tot_len;
879 #endif /* WITH_LWIP */
880 
881  if ( bytes_read < 0 ) {
882  warn("coap_read: recvfrom");
883  goto error_early;
884  }
885 
886  if ( (size_t)bytes_read < sizeof(coap_hdr_t) ) {
887  debug("coap_read: discarded invalid frame\n" );
888  goto error_early;
889  }
890 
891  if ( pdu->version != COAP_DEFAULT_VERSION ) {
892  debug("coap_read: unknown protocol version\n" );
893  goto error_early;
894  }
895 
896  node = coap_new_node();
897  if ( !node )
898  goto error_early;
899 
900 #ifdef WITH_LWIP
901  node->pdu = coap_pdu_from_pbuf(ctx->pending_package);
902  ctx->pending_package = NULL;
903 #else
904  node->pdu = coap_pdu_init(0, 0, 0, bytes_read);
905 #endif
906  if (!node->pdu)
907  goto error;
908 
909  coap_ticks( &node->t );
910  memcpy(&node->local, &dst, sizeof(coap_address_t));
911  memcpy(&node->remote, &src, sizeof(coap_address_t));
912 
913 if (!coap_pdu_parse((unsigned char *)buf, bytes_read, node->pdu)) {
914  warn("discard malformed PDU");
915  goto error;
916  }
917 
918  /* and add new node to receive queue */
919  coap_transaction_id(&node->remote, node->pdu, &node->id);
920  coap_insert_node(&ctx->recvqueue, node);
921 
922 #ifndef NDEBUG
923  if (LOG_DEBUG <= coap_get_log_level()) {
924 #ifndef INET6_ADDRSTRLEN
925 #define INET6_ADDRSTRLEN 40
926 #endif
927  unsigned char addr[INET6_ADDRSTRLEN+8];
928 
929  if (coap_print_addr(&src, addr, INET6_ADDRSTRLEN+8))
930  debug("** received %d bytes from %s:\n", (int)bytes_read, addr);
931 
932  coap_show_pdu( node->pdu );
933  }
934 #endif
935 
936  return 0;
937 
938  error:
939  /* FIXME: send back RST? */
940  coap_delete_node(node);
941  return -1;
942  error_early:
943 #ifdef WITH_LWIP
944  /* even if there was an error, clean up */
945  pbuf_free(ctx->pending_package);
946  ctx->pending_package = NULL;
947 #endif
948  return -1;
949 }
950 
951 int
953  coap_queue_t *p, *q;
954 
955  if ( !queue || !*queue)
956  return 0;
957 
958  /* replace queue head if PDU's time is less than head's time */
959 
960  if ( id == (*queue)->id ) { /* found transaction */
961  *node = *queue;
962  *queue = (*queue)->next;
963  if (*queue) { /* adjust relative time of new queue head */
964  (*queue)->t += (*node)->t;
965  }
966  (*node)->next = NULL;
967  /* coap_delete_node( q ); */
968  debug("*** removed transaction %u\n", id);
969  return 1;
970  }
971 
972  /* search transaction to remove (only first occurence will be removed) */
973  q = *queue;
974  do {
975  p = q;
976  q = q->next;
977  } while ( q && id != q->id );
978 
979  if ( q ) { /* found transaction */
980  p->next = q->next;
981  if (p->next) { /* must update relative time of p->next */
982  p->next->t += q->t;
983  }
984  q->next = NULL;
985  *node = q;
986  /* coap_delete_node( q ); */
987  debug("*** removed transaction %u\n", id);
988  return 1;
989  }
990 
991  return 0;
992 
993 }
994 
995 static inline int
996 token_match(const unsigned char *a, size_t alen,
997  const unsigned char *b, size_t blen) {
998  return alen == blen && (alen == 0 || memcmp(a, b, alen) == 0);
999 }
1000 
1001 void
1002 coap_cancel_all_messages(coap_context_t *context, const coap_address_t *dst,
1003  const unsigned char *token, size_t token_length) {
1004  /* cancel all messages in sendqueue that are for dst
1005  * and use the specified token */
1006  coap_queue_t *p, *q;
1007 
1008  debug("cancel_all_messages\n");
1009  while (context->sendqueue &&
1010  coap_address_equals(dst, &context->sendqueue->remote) &&
1011  token_match(token, token_length,
1012  context->sendqueue->pdu->hdr->token,
1013  context->sendqueue->pdu->hdr->token_length)) {
1014  q = context->sendqueue;
1015  context->sendqueue = q->next;
1016  debug("**** removed transaction %d\n", ntohs(q->pdu->hdr->id));
1017  coap_delete_node(q);
1018  }
1019 
1020  if (!context->sendqueue)
1021  return;
1022 
1023  p = context->sendqueue;
1024  q = p->next;
1025 
1026  /* when q is not NULL, it does not match (dst, token), so we can skip it */
1027  while (q) {
1028  if (coap_address_equals(dst, &q->remote) &&
1029  token_match(token, token_length,
1030  q->pdu->hdr->token, q->pdu->hdr->token_length)) {
1031  p->next = q->next;
1032  debug("**** removed transaction %d\n", ntohs(q->pdu->hdr->id));
1033  coap_delete_node(q);
1034  q = p->next;
1035  } else {
1036  p = q;
1037  q = q->next;
1038  }
1039  }
1040 }
1041 
1042 coap_queue_t *
1044  while (queue && queue->id != id)
1045  queue = queue->next;
1046 
1047  return queue;
1048 }
1049 
1050 coap_pdu_t *
1051 coap_new_error_response(coap_pdu_t *request, unsigned char code,
1052  coap_opt_filter_t opts) {
1053  coap_opt_iterator_t opt_iter;
1054  coap_pdu_t *response;
1055  size_t size = sizeof(coap_hdr_t) + request->hdr->token_length;
1056  int type;
1057  coap_opt_t *option;
1058  unsigned short opt_type = 0; /* used for calculating delta-storage */
1059 
1061  char *phrase = coap_response_phrase(code);
1062 
1063  /* Need some more space for the error phrase and payload start marker */
1064  if (phrase)
1065  size += strlen(phrase) + 1;
1066 #endif
1067 
1068  assert(request);
1069 
1070  /* cannot send ACK if original request was not confirmable */
1071  type = request->hdr->type == COAP_MESSAGE_CON
1073  : COAP_MESSAGE_NON;
1074 
1075  /* Estimate how much space we need for options to copy from
1076  * request. We always need the Token, for 4.02 the unknown critical
1077  * options must be included as well. */
1078  coap_option_clrb(opts, COAP_OPTION_CONTENT_TYPE); /* we do not want this */
1079 
1080  coap_option_iterator_init(request, &opt_iter, opts);
1081 
1082  /* Add size of each unknown critical option. As known critical
1083  options as well as elective options are not copied, the delta
1084  value might grow.
1085  */
1086  while((option = coap_option_next(&opt_iter))) {
1087  unsigned short delta = opt_iter.type - opt_type;
1088  /* calculate space required to encode (opt_iter.type - opt_type) */
1089  if (delta < 13) {
1090  size++;
1091  } else if (delta < 269) {
1092  size += 2;
1093  } else {
1094  size += 3;
1095  }
1096 
1097  /* add coap_opt_length(option) and the number of additional bytes
1098  * required to encode the option length */
1099 
1100  size += coap_opt_length(option);
1101  switch (*option & 0x0f) {
1102  case 0x0e:
1103  size++;
1104  /* fall through */
1105  case 0x0d:
1106  size++;
1107  break;
1108  default:
1109  ;
1110  }
1111 
1112  opt_type = opt_iter.type;
1113  }
1114 
1115  /* Now create the response and fill with options and payload data. */
1116  response = coap_pdu_init(type, code, request->hdr->id, size);
1117  if (response) {
1118  /* copy token */
1119  if (!coap_add_token(response, request->hdr->token_length,
1120  request->hdr->token)) {
1121  debug("cannot add token to error response\n");
1122  coap_delete_pdu(response);
1123  return NULL;
1124  }
1125 
1126  /* copy all options */
1127  coap_option_iterator_init(request, &opt_iter, opts);
1128  while((option = coap_option_next(&opt_iter)))
1129  coap_add_option(response, opt_iter.type,
1130  COAP_OPT_LENGTH(option),
1131  COAP_OPT_VALUE(option));
1132 
1133 #if COAP_ERROR_PHRASE_LENGTH > 0
1134  /* note that diagnostic messages do not need a Content-Format option. */
1135  if (phrase)
1136  coap_add_data(response, strlen(phrase), (unsigned char *)phrase);
1137 #endif
1138  }
1139 
1140  return response;
1141 }
1142 
1147 static inline size_t
1148 get_wkc_len(coap_context_t *context, coap_opt_t *query_filter) {
1149  unsigned char buf[1];
1150  size_t len = 0;
1151 
1152  if (print_wellknown(context, buf, &len, UINT_MAX, query_filter)
1154  warn("cannot determine length of /.well-known/core\n");
1155  return 0;
1156  }
1157 
1158  debug("get_wkc_len: print_wellknown() returned %zu\n", len);
1159 
1160  return len;
1161 }
1162 
1163 #define SZX_TO_BYTES(SZX) ((size_t)(1 << ((SZX) + 4)))
1164 
1165 coap_pdu_t *
1167  coap_pdu_t *resp;
1168  coap_opt_iterator_t opt_iter;
1169  size_t len, wkc_len;
1170  unsigned char buf[2];
1171  int result = 0;
1172  int need_block2 = 0; /* set to 1 if Block2 option is required */
1174  coap_opt_t *query_filter;
1175  size_t offset = 0;
1176 
1177  resp = coap_pdu_init(request->hdr->type == COAP_MESSAGE_CON
1178  ? COAP_MESSAGE_ACK
1179  : COAP_MESSAGE_NON,
1180  COAP_RESPONSE_CODE(205),
1181  request->hdr->id, COAP_MAX_PDU_SIZE);
1182  if (!resp) {
1183  debug("wellknown_response: cannot create PDU\n");
1184  return NULL;
1185  }
1186 
1187  if (!coap_add_token(resp, request->hdr->token_length, request->hdr->token)) {
1188  debug("wellknown_response: cannot add token\n");
1189  goto error;
1190  }
1191 
1192  query_filter = coap_check_option(request, COAP_OPTION_URI_QUERY, &opt_iter);
1193  wkc_len = get_wkc_len(context, query_filter);
1194 
1195  /* check whether the request contains the Block2 option */
1196  if (coap_get_block(request, COAP_OPTION_BLOCK2, &block)) {
1197  offset = block.num << (block.szx + 4);
1198  if (block.szx > 6) { /* invalid, MUST lead to 4.00 Bad Request */
1199  resp->hdr->code = COAP_RESPONSE_CODE(400);
1200  return resp;
1201  } else if (block.szx > COAP_MAX_BLOCK_SZX) {
1202  block.szx = COAP_MAX_BLOCK_SZX;
1203  block.num = offset >> (block.szx + 4);
1204  }
1205 
1206  need_block2 = 1;
1207  }
1208 
1209  /* Check if there is sufficient space to add Content-Format option
1210  * and data. We do this before adding the Content-Format option to
1211  * avoid sending error responses with that option but no actual
1212  * content. */
1213  if (resp->max_size <= (size_t)resp->length + 3) {
1214  debug("wellknown_response: insufficient storage space\n");
1215  goto error;
1216  }
1217 
1218  /* Add Content-Format. As we have checked for available storage,
1219  * nothing should go wrong here. */
1220  assert(coap_encode_var_bytes(buf,
1223  coap_encode_var_bytes(buf,
1225 
1226  /* check if Block2 option is required even if not requested */
1227  if (!need_block2 && (resp->max_size - (size_t)resp->length < wkc_len)) {
1228  assert(resp->length <= resp->max_size);
1229  const size_t payloadlen = resp->max_size - resp->length;
1230  /* yes, need block-wise transfer */
1231  block.num = 0;
1232  block.m = 0; /* the M bit is set by coap_write_block_opt() */
1233  block.szx = COAP_MAX_BLOCK_SZX;
1234  while (payloadlen < SZX_TO_BYTES(block.szx)) {
1235  if (block.szx == 0) {
1236  debug("wellknown_response: message to small even for szx == 0\n");
1237  goto error;
1238  } else {
1239  block.szx--;
1240  }
1241  }
1242 
1243  need_block2 = 1;
1244  }
1245 
1246  /* write Block2 option if necessary */
1247  if (need_block2) {
1248  if (coap_write_block_opt(&block, COAP_OPTION_BLOCK2, resp, wkc_len) < 0) {
1249  debug("wellknown_response: cannot add Block2 option\n");
1250  goto error;
1251  }
1252  }
1253 
1254  /* Manually set payload of response to let print_wellknown() write,
1255  * into our buffer without copying data. */
1256 
1257  resp->data = (unsigned char *)resp->hdr + resp->length;
1258  *resp->data = COAP_PAYLOAD_START;
1259  resp->data++;
1260  resp->length++;
1261  len = need_block2 ? SZX_TO_BYTES(block.szx) : resp->max_size - resp->length;
1262 
1263  result = print_wellknown(context, resp->data, &len, offset, query_filter);
1264  if ((result & COAP_PRINT_STATUS_ERROR) != 0) {
1265  debug("print_wellknown failed\n");
1266  goto error;
1267  }
1268 
1269  resp->length += COAP_PRINT_OUTPUT_LENGTH(result);
1270  return resp;
1271 
1272  error:
1273  /* set error code 5.03 and remove all options and data from response */
1274  resp->hdr->code = COAP_RESPONSE_CODE(503);
1275  resp->length = sizeof(coap_hdr_t) + resp->hdr->token_length;
1276  return resp;
1277 }
1278 
1279 #define WANT_WKC(Pdu,Key) \
1280  (((Pdu)->hdr->code == COAP_REQUEST_GET) && is_wkc(Key))
1281 
1282 void
1284  coap_method_handler_t h = NULL;
1285  coap_pdu_t *response = NULL;
1286  coap_opt_filter_t opt_filter;
1287  coap_resource_t *resource;
1288  coap_key_t key;
1289 
1290  coap_option_filter_clear(opt_filter);
1291 
1292  /* try to find the resource from the request URI */
1293  coap_hash_request_uri(node->pdu, key);
1294  resource = coap_get_resource_from_key(context, key);
1295 
1296  if (!resource) {
1297  /* The resource was not found. Check if the request URI happens to
1298  * be the well-known URI. In that case, we generate a default
1299  * response, otherwise, we return 4.04 */
1300 
1301  switch(node->pdu->hdr->code) {
1302 
1303  case COAP_REQUEST_GET:
1304  if (is_wkc(key)) { /* GET request for .well-known/core */
1305  info("create default response for %s\n", COAP_DEFAULT_URI_WELLKNOWN);
1306  response = wellknown_response(context, node->pdu);
1307 
1308  } else { /* GET request for any another resource, return 4.04 */
1309 
1310  debug("GET for unknown resource 0x%02x%02x%02x%02x, return 4.04\n",
1311  key[0], key[1], key[2], key[3]);
1312  response =
1314  opt_filter);
1315  }
1316  break;
1317 
1318  default: /* any other request type */
1319 
1320  debug("unhandled request for unknown resource 0x%02x%02x%02x%02x\r\n",
1321  key[0], key[1], key[2], key[3]);
1322  if (!coap_is_mcast(&node->local))
1323  response = coap_new_error_response(node->pdu, COAP_RESPONSE_CODE(405),
1324  opt_filter);
1325  }
1326 
1327  if (response && coap_send(context, &node->remote, response) == COAP_INVALID_TID) {
1328  warn("cannot send response for transaction %u\n", node->id);
1329  }
1330  coap_delete_pdu(response);
1331 
1332  return;
1333  }
1334 
1335  /* the resource was found, check if there is a registered handler */
1336  if ((size_t)node->pdu->hdr->code - 1 <
1337  sizeof(resource->handler)/sizeof(coap_method_handler_t))
1338  h = resource->handler[node->pdu->hdr->code - 1];
1339 
1340  if (h) {
1341  debug("call custom handler for resource 0x%02x%02x%02x%02x\n",
1342  key[0], key[1], key[2], key[3]);
1343  response = coap_pdu_init(node->pdu->hdr->type == COAP_MESSAGE_CON
1345  : COAP_MESSAGE_NON,
1346  0, node->pdu->hdr->id, COAP_MAX_PDU_SIZE);
1347 
1348  /* Implementation detail: coap_add_token() immediately returns 0
1349  if response == NULL */
1350  if (coap_add_token(response, node->pdu->hdr->token_length,
1351  node->pdu->hdr->token)) {
1352  str token = { node->pdu->hdr->token_length, node->pdu->hdr->token };
1353 
1354  h(context, resource, &node->remote,
1355  node->pdu, &token, response);
1356  if (response->hdr->type != COAP_MESSAGE_NON ||
1357  (response->hdr->code >= 64
1358  && !coap_is_mcast(&node->local))) {
1359  if (coap_send(context, &node->remote, response) == COAP_INVALID_TID) {
1360  debug("cannot send response for message %d\n", node->pdu->hdr->id);
1361  }
1362  }
1363 
1364  coap_delete_pdu(response);
1365  } else {
1366  warn("cannot generate response\r\n");
1367  }
1368  } else {
1369  if (WANT_WKC(node->pdu, key)) {
1370  debug("create default response for %s\n", COAP_DEFAULT_URI_WELLKNOWN);
1371  response = wellknown_response(context, node->pdu);
1372  } else
1373  response = coap_new_error_response(node->pdu, COAP_RESPONSE_CODE(405),
1374  opt_filter);
1375 
1376  if (!response || (coap_send(context, &node->remote, response)
1377  == COAP_INVALID_TID)) {
1378  debug("cannot send response for transaction %u\n", node->id);
1379  }
1380  coap_delete_pdu(response);
1381  }
1382 }
1383 
1384 static inline void
1386  coap_queue_t *sent, coap_queue_t *rcvd) {
1387 
1388  /* Call application-specific reponse handler when available. If
1389  * not, we must acknowledge confirmable messages. */
1390  if (context->response_handler) {
1391  context->response_handler(context,
1392  &rcvd->remote, sent ? sent->pdu : NULL,
1393  rcvd->pdu, rcvd->id);
1394  } else {
1395  /* send ACK if rcvd is confirmable (i.e. a separate response) */
1396  coap_send_ack(context, &rcvd->remote, rcvd->pdu);
1397  }
1398 }
1399 
1400 static inline int
1401 #ifdef __GNUC__
1402 handle_locally(coap_context_t *context __attribute__ ((unused)),
1403  coap_queue_t *node __attribute__ ((unused))) {
1404 #else /* not a GCC */
1406 #endif /* GCC */
1407  /* this function can be used to check if node->pdu is really for us */
1408  return 1;
1409 }
1410 
1415 static void
1417 #ifndef WITHOUT_OBSERVE
1418  coap_resource_t *r;
1419 #ifndef COAP_RESOURCES_NOHASH
1420  coap_resource_t *tmp;
1421 #endif
1422  str token = { 0, NULL };
1423 
1424  /* remove observer for this resource, if any
1425  * get token from sent and try to find a matching resource. Uh!
1426  */
1427 
1428  COAP_SET_STR(&token, sent->pdu->hdr->token_length, sent->pdu->hdr->token);
1429 
1430 #ifndef WITH_CONTIKI
1431 #ifdef COAP_RESOURCES_NOHASH
1432  LL_FOREACH(context->resources, r) {
1433 #else
1434  HASH_ITER(hh, context->resources, r, tmp) {
1435 #endif
1436  coap_delete_observer(r, &sent->remote, &token);
1437  coap_cancel_all_messages(context, &sent->remote, token.s, token.length);
1438  }
1439 #else /* WITH_CONTIKI */
1440  r = (coap_resource_t *)resource_storage.mem;
1441  for (i = 0; i < resource_storage.num; ++i, ++r) {
1442  if (resource_storage.count[i]) {
1443  coap_delete_observer(r, &sent->remote, &token);
1444  coap_cancel_all_messages(context, &sent->remote, token.s, token.length);
1445  }
1446  }
1447 #endif /* WITH_CONTIKI */
1448 #endif /* WITOUT_OBSERVE */
1449 }
1450 
1451 void
1453  coap_queue_t *rcvd = NULL, *sent = NULL;
1454  coap_pdu_t *response;
1455  coap_opt_filter_t opt_filter;
1456 
1457  if (!context)
1458  return;
1459 
1460  memset(opt_filter, 0, sizeof(coap_opt_filter_t));
1461 
1462  while ( context->recvqueue ) {
1463  rcvd = context->recvqueue;
1464 
1465  /* remove node from recvqueue */
1466  context->recvqueue = context->recvqueue->next;
1467  rcvd->next = NULL;
1468 
1469  if ( rcvd->pdu->hdr->version != COAP_DEFAULT_VERSION ) {
1470  debug("dropped packet with unknown version %u\n", rcvd->pdu->hdr->version);
1471  goto cleanup;
1472  }
1473 
1474  switch ( rcvd->pdu->hdr->type ) {
1475  case COAP_MESSAGE_ACK:
1476  /* find transaction in sendqueue to stop retransmission */
1477  coap_remove_from_queue(&context->sendqueue, rcvd->id, &sent);
1478 
1479  if (rcvd->pdu->hdr->code == 0)
1480  goto cleanup;
1481 
1482  /* FIXME: if sent code was >= 64 the message might have been a
1483  * notification. Then, we must flag the observer to be alive
1484  * by setting obs->fail_cnt = 0. */
1485  if (sent && COAP_RESPONSE_CLASS(sent->pdu->hdr->code) == 2) {
1486  const str token =
1487  { sent->pdu->hdr->token_length, sent->pdu->hdr->token };
1488  coap_touch_observer(context, &sent->remote, &token);
1489  }
1490  break;
1491 
1492  case COAP_MESSAGE_RST :
1493  /* We have sent something the receiver disliked, so we remove
1494  * not only the transaction but also the subscriptions we might
1495  * have. */
1496 
1497  coap_log(LOG_ALERT, "got RST for message %u\n", ntohs(rcvd->pdu->hdr->id));
1498 
1499  /* find transaction in sendqueue to stop retransmission */
1500  coap_remove_from_queue(&context->sendqueue, rcvd->id, &sent);
1501 
1502  if (sent)
1503  coap_handle_rst(context, sent);
1504  goto cleanup;
1505 
1506  case COAP_MESSAGE_NON : /* check for unknown critical options */
1507  if (coap_option_check_critical(context, rcvd->pdu, opt_filter) == 0)
1508  goto cleanup;
1509  break;
1510 
1511  case COAP_MESSAGE_CON : /* check for unknown critical options */
1512  if (coap_option_check_critical(context, rcvd->pdu, opt_filter) == 0) {
1513 
1514  /* FIXME: send response only if we have received a request. Otherwise,
1515  * send RST. */
1516  response =
1517  coap_new_error_response(rcvd->pdu, COAP_RESPONSE_CODE(402), opt_filter);
1518 
1519  if (!response)
1520  warn("coap_dispatch: cannot create error reponse\n");
1521  else {
1522  if (coap_send(context, &rcvd->remote, response)
1523  == COAP_INVALID_TID) {
1524  warn("coap_dispatch: error sending reponse\n");
1525  }
1526  coap_delete_pdu(response);
1527  }
1528 
1529  goto cleanup;
1530  }
1531  break;
1532  }
1533 
1534  /* Pass message to upper layer if a specific handler was
1535  * registered for a request that should be handled locally. */
1536  if (handle_locally(context, rcvd)) {
1537  if (COAP_MESSAGE_IS_REQUEST(rcvd->pdu->hdr))
1538  handle_request(context, rcvd);
1539  else if (COAP_MESSAGE_IS_RESPONSE(rcvd->pdu->hdr))
1540  handle_response(context, sent, rcvd);
1541  else {
1542  debug("dropped message with invalid code\n");
1543  coap_send_message_type(context, &rcvd->remote, rcvd->pdu,
1545  }
1546  }
1547 
1548  cleanup:
1549  coap_delete_node(sent);
1550  coap_delete_node(rcvd);
1551  }
1552 }
1553 
1554 int
1556  return !context || (context->recvqueue == NULL && context->sendqueue == NULL);
1557 }
1558 
1559 #ifdef WITH_CONTIKI
1560 
1561 /*---------------------------------------------------------------------------*/
1562 /* CoAP message retransmission */
1563 /*---------------------------------------------------------------------------*/
1564 PROCESS_THREAD(coap_retransmit_process, ev, data)
1565 {
1566  coap_tick_t now;
1567  coap_queue_t *nextpdu;
1568 
1569  PROCESS_BEGIN();
1570 
1571  debug("Started retransmit process\r\n");
1572 
1573  while(1) {
1574  PROCESS_YIELD();
1575  if (ev == PROCESS_EVENT_TIMER) {
1576  if (etimer_expired(&the_coap_context.retransmit_timer)) {
1577 
1578  nextpdu = coap_peek_next(&the_coap_context);
1579 
1580  coap_ticks(&now);
1581  while (nextpdu && nextpdu->t <= now) {
1582  coap_retransmit(&the_coap_context, coap_pop_next(&the_coap_context));
1583  nextpdu = coap_peek_next(&the_coap_context);
1584  }
1585 
1586  /* need to set timer to some value even if no nextpdu is available */
1587  etimer_set(&the_coap_context.retransmit_timer,
1588  nextpdu ? nextpdu->t - now : 0xFFFF);
1589  }
1590 #ifndef WITHOUT_OBSERVE
1591  if (etimer_expired(&the_coap_context.notify_timer)) {
1592  coap_check_notify(&the_coap_context);
1593  etimer_reset(&the_coap_context.notify_timer);
1594  }
1595 #endif /* WITHOUT_OBSERVE */
1596  }
1597  }
1598 
1599  PROCESS_END();
1600 }
1601 /*---------------------------------------------------------------------------*/
1602 
1603 #endif /* WITH_CONTIKI */
1604 
1605 #ifdef WITH_LWIP
1606 /* FIXME: retransmits that are not required any more due to incoming packages
1607  * do *not* get cleared at the moment, the wakeup when the transmission is due
1608  * is silently accepted. this is mainly due to the fact that the required
1609  * checks are similar in two places in the code (when receiving ACK and RST)
1610  * and that they cause more than one patch chunk, as it must be first checked
1611  * whether the sendqueue item to be dropped is the next one pending, and later
1612  * the restart function has to be called. nothing insurmountable, but it can
1613  * also be implemented when things have stabilized, and the performance
1614  * penality is minimal
1615  *
1616  * also, this completely ignores COAP_RESOURCE_CHECK_TIME.
1617  * */
1618 
1619 static void coap_retransmittimer_execute(void *arg)
1620 {
1621  coap_context_t *ctx = (coap_context_t*)arg;
1622  coap_tick_t now;
1623  coap_tick_t elapsed;
1624  coap_queue_t *nextinqueue;
1625 
1626  ctx->timer_configured = 0;
1627 
1628  coap_ticks(&now);
1629 
1630  elapsed = now - ctx->sendqueue_basetime; /* that's positive for sure, and unless we haven't been called for a complete wrapping cycle, did not wrap */
1631 
1632  nextinqueue = coap_peek_next(ctx);
1633  while (nextinqueue != NULL)
1634  {
1635  if (nextinqueue->t > elapsed) {
1636  nextinqueue->t -= elapsed;
1637  break;
1638  } else {
1639  elapsed -= nextinqueue->t;
1640  coap_retransmit(ctx, coap_pop_next(ctx));
1641  nextinqueue = coap_peek_next(ctx);
1642  }
1643  }
1644 
1645  ctx->sendqueue_basetime = now;
1646 
1647  coap_retransmittimer_restart(ctx);
1648 }
1649 
1650 static void coap_retransmittimer_restart(coap_context_t *ctx)
1651 {
1652  coap_tick_t now, elapsed, delay;
1653 
1654  if (ctx->timer_configured)
1655  {
1656  printf("clearing\n");
1657  sys_untimeout(coap_retransmittimer_execute, (void*)ctx);
1658  ctx->timer_configured = 0;
1659  }
1660  if (ctx->sendqueue != NULL)
1661  {
1662  coap_ticks(&now);
1663  elapsed = now - ctx->sendqueue_basetime;
1664  if (ctx->sendqueue->t >= elapsed) {
1665  delay = ctx->sendqueue->t - elapsed;
1666  } else {
1667  /* a strange situation, but not completely impossible.
1668  *
1669  * this happens, for example, right after
1670  * coap_retransmittimer_execute, when a retransmission
1671  * was *just not yet* due, and the clock ticked before
1672  * our coap_ticks was called.
1673  *
1674  * not trying to retransmit anything now, as it might
1675  * cause uncontrollable recursion; let's just try again
1676  * with the next main loop run.
1677  * */
1678  delay = 0;
1679  }
1680 
1681  printf("scheduling for %d ticks\n", delay);
1682  sys_timeout(delay, coap_retransmittimer_execute, (void*)ctx);
1683  ctx->timer_configured = 1;
1684  }
1685 }
1686 #endif
int coap_get_block(coap_pdu_t *pdu, unsigned short type, coap_block_t *block)
Initializes block from pdu.
Definition: block.c:41
#define LL_FOREACH(head, el)
Definition: utlist.h:361
coap_queue_t * sendqueue
Definition: net.h:110
#define COAP_OPTION_IF_MATCH
Definition: pdu.h:57
#define COAP_DEFAULT_RESPONSE_TIMEOUT
Definition: pdu.h:22
void coap_check_notify(coap_context_t *context)
Checks for all known resources, if they are dirty and notifies subscribed observers.
Definition: resource.c:764
unsigned char token[]
Definition: pdu.h:174
int coap_write_block_opt(coap_block_t *block, unsigned short type, coap_pdu_t *pdu, size_t data_length)
Writes a block option of type type to message pdu.
Definition: block.c:60
unsigned char coap_key_t[4]
Definition: hashkey.h:19
#define warn(...)
Definition: debug.h:54
#define COAP_RESPONSE_CODE(N)
Definition: pdu.h:92
#define COAP_DEFAULT_URI_WELLKNOWN
well-known resources URI
Definition: pdu.h:34
void coap_dispatch(coap_context_t *context)
Dispatches the PDUs from the receive queue in given context.
Definition: net.c:1452
#define COAP_OPTION_PROXY_URI
Definition: pdu.h:70
#define COAP_OPTION_CONTENT_FORMAT
Definition: pdu.h:64
static void coap_handle_rst(coap_context_t *context, const coap_queue_t *sent)
This function handles RST messages received for the message passed in sent.
Definition: net.c:1416
unsigned char retransmit_cnt
retransmission counter, will be removed when zero
Definition: net.h:55
#define COAP_MESSAGE_RST
Definition: pdu.h:46
int coap_delete_resource(coap_context_t *context, coap_key_t key)
Deletes a resource identified by key.
Definition: resource.c:451
coap_address_t local
local address
Definition: net.h:58
void coap_show_pdu(const coap_pdu_t *pdu)
Definition: debug.c:231
#define COAP_PRINT_OUTPUT_LENGTH(v)
Definition: resource.h:189
state management for asynchronous messages
coap_tid_t coap_send_confirmed(coap_context_t *context, const coap_address_t *dst, coap_pdu_t *pdu)
Sends a confirmed CoAP message to given destination.
Definition: net.c:699
unsigned short id
Definition: pdu.h:173
int coap_tid_t
Definition: pdu.h:155
#define COAP_REQUEST_GET
Definition: pdu.h:50
#define HASH_ITER(hh, head, el, tmp)
Definition: uthash.h:897
unsigned short length
PDU length (including header, options, data)
Definition: pdu.h:211
#define COAP_OPT_ALL
Pre-defined filter that includes all options.
Definition: option.h:96
int coap_add_token(coap_pdu_t *pdu, size_t len, const unsigned char *data)
Adds token of length len to pdu.
Definition: pdu.c:157
coap_opt_t * coap_check_option(coap_pdu_t *pdu, unsigned char type, coap_opt_iterator_t *oi)
Retrieves the first option of type type from pdu.
Definition: option.c:207
void handle_request(coap_context_t *context, coap_queue_t *node)
Definition: net.c:1283
#define COAP_MESSAGE_NON
Definition: pdu.h:44
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:167
static int check_opt_size(coap_opt_t *opt, unsigned char *maxpos)
Checks if opt fits into the message that ends with maxpos.
Definition: net.c:822
#define COAP_OPTION_BLOCK1
Definition: pdu.h:82
int coap_pdu_parse(unsigned char *data, size_t length, coap_pdu_t *pdu)
Parses data into the CoAP PDU structure given in result.
Definition: pdu.c:345
coap_address_t remote
remote address
Definition: net.h:59
#define coap_malloc(size)
Definition: mem.h:15
void coap_handle_failed_notify(coap_context_t *, const coap_address_t *, const str *)
Definition: resource.c:841
size_t length
Definition: str.h:15
#define prng_init(Value)
Called to set the PRNG seed.
Definition: prng.h:80
static int coap_option_getb(const coap_opt_filter_t filter, unsigned short type)
Gets the corresponding bit for type in filter.
Definition: option.h:149
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
Definition: option.c:157
coap_log_t coap_get_log_level()
Returns the current log level.
Definition: debug.c:41
#define COAP_OPTION_CONTENT_TYPE
Definition: pdu.h:65
helpers for handling options in CoAP PDUs
coap_block_t block
Definition: client.c:53
static int coap_option_setb(coap_opt_filter_t filter, unsigned short type)
Sets the corresponding bit for type in filter.
Definition: option.h:119
coap_hdr_t * hdr
Definition: pdu.h:209
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:296
#define WANT_WKC(Pdu, Key)
Definition: net.c:1279
unsigned short message_id
The last message id that was used is stored in this field.
Definition: net.h:135
unsigned short max_delta
highest option number
Definition: pdu.h:210
coap_tid_t coap_send_error(coap_context_t *context, coap_pdu_t *request, 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:660
coap_queue_t * coap_peek_next(coap_context_t *context)
Returns the next pdu to send without removing from sendqeue.
Definition: net.c:253
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:498
#define COAP_OPTION_PROXY_SCHEME
Definition: pdu.h:71
#define COAP_RESOURCE_CHECK_TIME
The interval in seconds to check if resources have changed.
Definition: resource.h:26
#define COAP_SET_STR(st, l, v)
Definition: str.h:19
static int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
Definition: address.h:159
void coap_hash_request_uri(const coap_pdu_t *request, coap_key_t key)
Calculates the hash key for the resource requested by the Uri-Options of request. ...
Definition: resource.c:424
#define SZX_TO_BYTES(SZX)
Definition: net.c:1163
coap_queue_t * coap_find_transaction(coap_queue_t *queue, coap_tid_t id)
Retrieves transaction from queue.
Definition: net.c:1043
coap_tid_t id
unique transaction id
Definition: net.h:60
static size_t get_wkc_len(coap_context_t *context, coap_opt_t *query_filter)
Quick hack to determine the size of the resource description for .well-known/core.
Definition: net.c:1148
coap_tick_t sendqueue_basetime
The time stamp in the first element of the sendqeue is relative to sendqueue_basetime.
Definition: net.h:109
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:1051
size_t coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, size_t len)
Definition: debug.c:140
#define debug(...)
Definition: debug.h:55
coap_pdu_t * pdu
the CoAP PDU to send
Definition: net.h:62
coap_tick_t t
when to send PDU for the next time
Definition: net.h:54
#define COAP_DEFAULT_VERSION
Definition: pdu.h:30
#define COAP_INVALID_TID
Definition: pdu.h:156
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:1002
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
unsigned int code
Definition: pdu.h:172
#define coap_free(size)
Definition: mem.h:16
Header structure for CoAP PDUs.
Definition: pdu.h:206
int coap_insert_node(coap_queue_t **queue, coap_queue_t *node)
Adds node to given queue, ordered by node->t.
Definition: net.c:180
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:1555
coap_opt_iterator_t * coap_option_iterator_init(coap_pdu_t *pdu, coap_opt_iterator_t *oi, const coap_opt_filter_t filter)
Initializes the given option iterator oi to point to the beginning of the pdu's option list...
Definition: option.c:118
coap_key_t key
the actual key bytes for this resource
Definition: resource.h:75
#define COAP_MAX_BLOCK_SZX
The largest value for the SZX component in a Block option.
Definition: block.h:27
coap_response_handler_t response_handler
Definition: net.h:143
#define COAP_OPTION_URI_PORT
Definition: pdu.h:61
#define COAP_DEFAULT_MAX_RETRANSMIT
Definition: pdu.h:23
int print_wellknown(coap_context_t *, unsigned char *, size_t *, size_t, coap_opt_t *)
Prints the names of all known resources to buf.
Definition: resource.c:166
unsigned int coap_encode_var_bytes(unsigned char *buf, unsigned int val)
Encodes multiple-length byte sequences.
Definition: encode.c:34
static coap_tid_t id
Definition: tiny.c:23
size_t max_size
allocated storage for options and data
Definition: pdu.h:207
struct coap_queue_t * next
Definition: net.h:52
#define COAP_RESPONSE_CLASS(C)
Definition: pdu.h:95
#define COAP_OPTION_IF_NONE_MATCH
Definition: pdu.h:60
Iterator to run through PDU options.
Definition: option.h:169
#define COAP_MESSAGE_CON
Definition: pdu.h:43
#define COAP_MEDIATYPE_APPLICATION_LINK_FORMAT
Definition: pdu.h:142
size_t coap_add_option(coap_pdu_t *pdu, unsigned short type, unsigned int len, const unsigned char *data)
de-duplicate code with coap_add_option_later
Definition: pdu.c:175
unsigned int timeout
the randomized timeout value
Definition: net.h:56
generic resource handling
#define COAP_MAX_PDU_SIZE
Definition: pdu.h:27
#define info(...)
Definition: debug.h:53
#define COAP_MESSAGE_ACK
Definition: pdu.h:45
#define INET6_ADDRSTRLEN
#define COAP_OPT_SIZE(opt)
Definition: option.h:65
unsigned char coap_opt_filter_t[(COAP_MAX_OPT >> 3)+1]
Fixed-size bit-vector we use for option filtering.
Definition: option.h:93
#define coap_clock_init
Definition: coap_time.h:121
int coap_add_data(coap_pdu_t *pdu, unsigned int len, const unsigned char *data)
Adds given data to the pdu that is passed as first parameter.
Definition: pdu.c:238
#define coap_hash(String, Length, Result)
Definition: hashkey.h:34
coap_tid_t coap_send(coap_context_t *context, const coap_address_t *dst, coap_pdu_t *pdu)
Sends a non-confirmed CoAP message to given destination.
Definition: net.c:653
coap_pdu_t * wellknown_response(coap_context_t *context, coap_pdu_t *request)
Definition: net.c:1166
unsigned short type
decoded option type
Definition: option.h:171
coap_tid_t coap_send_ack(coap_context_t *context, 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:534
#define COAP_OPTION_BLOCK2
Definition: pdu.h:81
void coap_delete_pdu(coap_pdu_t *pdu)
Definition: pdu.c:143
static void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
Definition: address.h:134
void coap_delete_all(coap_queue_t *queue)
Removes all items from given queue and frees the allocated storage.
Definition: net.c:228
coap_pdu_t * coap_pdu_init(unsigned char type, unsigned char code, unsigned short id, size_t size)
Creates a new CoAP PDU of given size (must be large enough to hold the basic CoAP message header (coa...
Definition: pdu.c:82
Structure of Block options.
Definition: block.h:37
static int handle_locally(coap_context_t *context, coap_queue_t *node)
Definition: net.c:1405
static void coap_option_filter_clear(coap_opt_filter_t f)
Clears filter f.
Definition: option.h:104
int coap_hash_path(const unsigned char *path, size_t len, coap_key_t key)
Calculates a hash over the given path and stores the result in key.
Definition: uri.c:454
Definition: str.h:14
coap_queue_t * recvqueue
Definition: net.h:110
#define COAP_PAYLOAD_START
Definition: pdu.h:188
void coap_free_context(coap_context_t *context)
Definition: net.c:419
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:463
#define COAP_OPT_LENGTH(opt)
Definition: option.h:305
unsigned int token_length
Definition: pdu.h:169
#define COAP_OPTION_URI_PATH
Definition: pdu.h:63
void coap_touch_observer(coap_context_t *context, const coap_address_t *observer, const str *token)
Marks an observer as alive.
Definition: resource.c:643
#define COAP_ERROR_PHRASE_LENGTH
maximum length of error phrase
Definition: pdu.h:110
unsigned int version
Definition: pdu.h:171
#define COAP_PRINT_STATUS_ERROR
Definition: resource.h:190
#define COAP_OPTION_URI_QUERY
Definition: pdu.h:67
unsigned int type
Definition: pdu.h:170
unsigned char coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
Definition: option.h:26
#define COAP_MESSAGE_IS_REQUEST(MSG)
Definition: pdu.h:179
#define COAP_OPTION_ACCEPT
Definition: pdu.h:68
coap_queue_t * coap_new_node()
Creates a new node suitable for adding to the CoAP sendqueue.
Definition: net.c:237
static int token_match(const unsigned char *a, size_t alen, const unsigned char *b, size_t blen)
Definition: net.c:996
void coap_delete_observer(coap_resource_t *resource, const coap_address_t *observer, const str *token)
Removes any subscription for observer from resource and releases the allocated storage.
Definition: resource.c:674
#define coap_log(...)
Definition: debug.h:47
char * coap_response_phrase(unsigned char code)
Returns a human-readable response phrase for the specified CoAP response code.
Definition: pdu.c:310
int coap_read(coap_context_t *ctx)
Reads data from the network and tries to parse as CoAP PDU.
Definition: net.c:831
void(* coap_method_handler_t)(coap_context_t *, struct coap_resource_t *, coap_address_t *, coap_pdu_t *, str *, coap_pdu_t *)
Definition of message handler function (.
Definition: resource.h:46
unsigned char * s
Definition: str.h:16
#define prng(Buf, Length)
Fills Buf with Length bytes of random data.
Definition: prng.h:70
static int coap_option_clrb(coap_opt_filter_t filter, unsigned short type)
Clears the corresponding bit for type in filter.
Definition: option.h:134
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:952
coap_tid_t coap_send_message_type(coap_context_t *context, 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:681
coap_tid_t coap_retransmit(coap_context_t *context, coap_queue_t *node)
Handles retransmissions of confirmable messages.
Definition: net.c:771
#define COAP_OPTION_URI_HOST
Definition: pdu.h:58
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:261
unsigned short coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
Definition: option.c:249
unsigned char * data
payload
Definition: pdu.h:212
#define COAP_OPT_VALUE(opt)
Definition: option.h:318
The CoAP stack's global state is stored in a coap_context_t object.
Definition: net.h:97
static int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
Compares given address objects a and b.
Definition: address.h:149
#define COAP_MESSAGE_IS_RESPONSE(MSG)
Definition: pdu.h:181
int coap_delete_node(coap_queue_t *node)
Destroys specified node.
Definition: net.c:217
coap_resource_t * coap_get_resource_from_key(coap_context_t *context, coap_key_t key)
Returns the resource identified by the unique string key.
Definition: resource.c:503
struct coap_resource_t * resources
hash table or list of known resources
Definition: net.h:100
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:143
coap_method_handler_t handler[4]
Used to store handlers for the four coap methods GET, POST, PUT, and DELETE.
Definition: resource.h:73
static void handle_response(coap_context_t *context, coap_queue_t *sent, coap_queue_t *rcvd)
Definition: net.c:1385
unsigned int m
1 if more blocks follow, 0 otherwise
Definition: block.h:39
unsigned int szx
block size
Definition: block.h:40
#define is_wkc(Key)
Checks if Key is equal to the pre-defined hash key for.well-known/core.
Definition: net.c:278
unsigned int num
block number
Definition: block.h:38
coap_opt_filter_t known_options
Definition: net.h:98