libcoap  4.1.2
net.c
Go to the documentation of this file.
1 /* net.c -- CoAP network interface
2  *
3  * Copyright (C) 2010--2015 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 "coap_config.h"
10 
11 #include <ctype.h>
12 #include <stdio.h>
13 #include <errno.h>
14 #ifdef HAVE_LIMITS_H
15 #include <limits.h>
16 #endif
17 #ifdef HAVE_UNISTD_H
18 #include <unistd.h>
19 #elif HAVE_SYS_UNISTD_H
20 #include <sys/unistd.h>
21 #endif
22 #include <sys/types.h>
23 #ifdef HAVE_SYS_SOCKET_H
24 #include <sys/socket.h>
25 #endif
26 #ifdef HAVE_NETINET_IN_H
27 #include <netinet/in.h>
28 #endif
29 #ifdef HAVE_ARPA_INET_H
30 #include <arpa/inet.h>
31 #endif
32 
33 #ifdef WITH_LWIP
34 #include <lwip/pbuf.h>
35 #include <lwip/udp.h>
36 #include <lwip/timers.h>
37 #endif
38 
39 #include "debug.h"
40 #include "mem.h"
41 #include "str.h"
42 #include "async.h"
43 #include "resource.h"
44 #include "option.h"
45 #include "encode.h"
46 #include "block.h"
47 #include "net.h"
48 
57 #ifndef COAP_DEFAULT_ACK_TIMEOUT
58 
62 #define COAP_DEFAULT_ACK_TIMEOUT 2 /* see RFC 7252, Section 4.8 */
63 #endif
64 
65 #ifndef COAP_DEFAULT_ACK_RANDOM_FACTOR
66 
70 #define COAP_DEFAULT_ACK_RANDOM_FACTOR 1.5 /* see RFC 7252, Section 4.8 */
71 #endif
72 
73 #ifndef COAP_DEFAULT_MAX_RETRANSMIT
74 
77 #define COAP_DEFAULT_MAX_RETRANSMIT 4 /* see RFC 7252, Section 4.8 */
78 #endif
79 
80 #ifndef COAP_DEFAULT_NSTART
81 
85 #define COAP_DEFAULT_NSTART 1 /* see RFC 7252, Section 4.8 */
86 #endif
87 
94 #define FRAC_BITS 6
95 
100 #define MAX_BITS 8
101 
102 #if FRAC_BITS > 8
103 #error FRAC_BITS must be less or equal 8
104 #endif
105 
107 #define Q(frac,fval) ((unsigned short)(((1 << (frac)) * (fval))))
108 
110 #define ACK_RANDOM_FACTOR \
111  Q(FRAC_BITS, COAP_DEFAULT_ACK_RANDOM_FACTOR)
112 
114 #define ACK_TIMEOUT Q(FRAC_BITS, COAP_DEFAULT_ACK_TIMEOUT)
115 
116 #if defined(WITH_POSIX)
117 
119 
120 static inline coap_queue_t *
123 }
124 
125 static inline void
127  coap_free_type(COAP_NODE, node);
128 }
129 #endif /* WITH_POSIX */
130 #ifdef WITH_LWIP
131 
132 #include <lwip/memp.h>
133 
134 static void coap_retransmittimer_execute(void *arg);
135 static void coap_retransmittimer_restart(coap_context_t *ctx);
136 
137 static inline coap_queue_t *
139  return (coap_queue_t *)memp_malloc(MEMP_COAP_NODE);
140 }
141 
142 static inline void
144  memp_free(MEMP_COAP_NODE, node);
145 }
146 
147 #endif /* WITH_LWIP */
148 #ifdef WITH_CONTIKI
149 # ifndef DEBUG
150 # define DEBUG DEBUG_PRINT
151 # endif /* DEBUG */
152 
153 #include "mem.h"
154 #include "net/ip/uip-debug.h"
155 
156 clock_time_t clock_offset;
157 
158 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
159 #define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[UIP_LLIPH_LEN])
160 
161 void coap_resources_init();
162 
163 unsigned char initialized = 0;
164 coap_context_t the_coap_context;
165 
166 PROCESS(coap_retransmit_process, "message retransmit process");
167 
168 static inline coap_queue_t *
171 }
172 
173 static inline void
175  coap_free_type(COAP_NODE, node);
176 }
177 #endif /* WITH_CONTIKI */
178 
179 unsigned int
181  unsigned int result = 0;
182  coap_tick_diff_t delta = now - ctx->sendqueue_basetime;
183 
184  if (ctx->sendqueue) {
185  /* delta < 0 means that the new time stamp is before the old. */
186  if (delta <= 0) {
187  ctx->sendqueue->t -= delta;
188  } else {
189  /* This case is more complex: The time must be advanced forward,
190  * thus possibly leading to timed out elements at the queue's
191  * start. For every element that has timed out, its relative
192  * time is set to zero and the result counter is increased. */
193 
194  coap_queue_t *q = ctx->sendqueue;
195  coap_tick_t t = 0;
196  while (q && (t + q->t < (coap_tick_t)delta)) {
197  t += q->t;
198  q->t = 0;
199  result++;
200  q = q->next;
201  }
202 
203  /* finally adjust the first element that has not expired */
204  if (q) {
205  q->t = (coap_tick_t)delta - t;
206  }
207  }
208  }
209 
210  /* adjust basetime */
211  ctx->sendqueue_basetime += delta;
212 
213  return result;
214 }
215 
216 int
218  coap_queue_t *p, *q;
219  if ( !queue || !node )
220  return 0;
221 
222  /* set queue head if empty */
223  if ( !*queue ) {
224  *queue = node;
225  return 1;
226  }
227 
228  /* replace queue head if PDU's time is less than head's time */
229  q = *queue;
230  if (node->t < q->t) {
231  node->next = q;
232  *queue = node;
233  q->t -= node->t; /* make q->t relative to node->t */
234  return 1;
235  }
236 
237  /* search for right place to insert */
238  do {
239  node->t -= q->t; /* make node-> relative to q->t */
240  p = q;
241  q = q->next;
242  } while (q && q->t <= node->t);
243 
244  /* insert new item */
245  if (q) {
246  q->t -= node->t; /* make q->t relative to node->t */
247  }
248  node->next = q;
249  p->next = node;
250  return 1;
251 }
252 
253 int
255  if ( !node )
256  return 0;
257 
258  coap_delete_pdu(node->pdu);
259  coap_free_node(node);
260 
261  return 1;
262 }
263 
264 void
266  if ( !queue )
267  return;
268 
269  coap_delete_all( queue->next );
270  coap_delete_node( queue );
271 }
272 
273 coap_queue_t *
275  coap_queue_t *node;
276  node = coap_malloc_node();
277 
278  if ( ! node ) {
279 #ifndef NDEBUG
280  coap_log(LOG_WARNING, "coap_new_node: malloc\n");
281 #endif
282  return NULL;
283  }
284 
285  memset(node, 0, sizeof(*node));
286  return node;
287 }
288 
289 coap_queue_t *
291  if ( !context || !context->sendqueue )
292  return NULL;
293 
294  return context->sendqueue;
295 }
296 
297 coap_queue_t *
299  coap_queue_t *next;
300 
301  if ( !context || !context->sendqueue )
302  return NULL;
303 
304  next = context->sendqueue;
305  context->sendqueue = context->sendqueue->next;
306  if (context->sendqueue) {
307  context->sendqueue->t += next->t;
308  }
309  next->next = NULL;
310  return next;
311 }
312 
313 #ifdef COAP_DEFAULT_WKC_HASHKEY
314 
315 #define is_wkc(Key) \
316  (memcmp((Key), COAP_DEFAULT_WKC_HASHKEY, sizeof(coap_key_t)) == 0)
317 #else
318 /* Implements a singleton to store a hash key for the .wellknown/core
319  * resources. */
320 int
322  static coap_key_t wkc;
323  static unsigned char _initialized = 0;
324  if (!_initialized) {
325  _initialized = coap_hash_path((unsigned char *)COAP_DEFAULT_URI_WELLKNOWN,
326  sizeof(COAP_DEFAULT_URI_WELLKNOWN) - 1, wkc);
327  }
328  return memcmp(k, wkc, sizeof(coap_key_t)) == 0;
329 }
330 #endif
331 
334  const coap_address_t *listen_addr) {
335 #ifndef WITH_CONTIKI
337 #endif /* not WITH_CONTIKI */
338 #ifdef WITH_CONTIKI
339  coap_context_t *c;
340 
341  if (initialized)
342  return NULL;
343 #endif /* WITH_CONTIKI */
344 
345  if (!listen_addr) {
346  coap_log(LOG_EMERG, "no listen address specified\n");
347  return NULL;
348  }
349 
350  coap_clock_init();
351 #ifdef WITH_LWIP
352  prng_init(LWIP_RAND());
353 #endif /* WITH_LWIP */
354 #ifdef WITH_CONTIKI
355  prng_init((ptrdiff_t)listen_addr ^ clock_offset);
356 #endif /* WITH_LWIP */
357 #ifdef WITH_POSIX
358  prng_init((unsigned long)listen_addr ^ clock_offset);
359 #endif /* WITH_POSIX */
360 
361 #ifndef WITH_CONTIKI
362  if (!c) {
363 #ifndef NDEBUG
364  coap_log(LOG_EMERG, "coap_init: malloc:\n");
365 #endif
366  return NULL;
367  }
368 #endif /* not WITH_CONTIKI */
369 #ifdef WITH_CONTIKI
370  coap_resources_init();
372 
373  c = &the_coap_context;
374  initialized = 1;
375 #endif /* WITH_CONTIKI */
376 
377  memset(c, 0, sizeof( coap_context_t ) );
378 
379  /* initialize message id */
380  prng((unsigned char *)&c->message_id, sizeof(unsigned short));
381 
383  if (c->endpoint == NULL) {
384  goto onerror;
385  }
386 #ifdef WITH_LWIP
387  c->endpoint->context = c;
388 #endif
389 
390 #ifdef WITH_POSIX
391  c->sockfd = c->endpoint->handle.fd;
392 #endif /* WITH_POSIX */
393 
394 #if defined(WITH_POSIX) || defined(WITH_CONTIKI)
397 #endif /* WITH_POSIX or WITH_CONTIKI */
398 
399 #ifdef WITH_CONTIKI
400  process_start(&coap_retransmit_process, (char *)c);
401 
402  PROCESS_CONTEXT_BEGIN(&coap_retransmit_process);
403 #ifndef WITHOUT_OBSERVE
404  etimer_set(&c->notify_timer, COAP_RESOURCE_CHECK_TIME * COAP_TICKS_PER_SECOND);
405 #endif /* WITHOUT_OBSERVE */
406  /* the retransmit timer must be initialized to some large value */
407  etimer_set(&the_coap_context.retransmit_timer, 0xFFFF);
408  PROCESS_CONTEXT_END(&coap_retransmit_process);
409 #endif /* WITH_CONTIKI */
410 
411  return c;
412 
413  onerror:
415  return NULL;
416 }
417 
418 void
420 
421  if (!context)
422  return;
423 
424  coap_delete_all(context->sendqueue);
425 
426 #ifdef WITH_LWIP
427  context->sendqueue = NULL;
428  coap_retransmittimer_restart(context);
429 #endif
430 
431  coap_delete_all_resources(context);
432 
433  coap_free_endpoint(context->endpoint);
434 #ifndef WITH_CONTIKI
435  coap_free_type(COAP_CONTEXT, context);
436 #endif/* not WITH_CONTIKI */
437 #ifdef WITH_CONTIKI
438  memset(&the_coap_context, 0, sizeof(coap_context_t));
439  initialized = 0;
440 #endif /* WITH_CONTIKI */
441 }
442 
443 int
445  coap_pdu_t *pdu,
446  coap_opt_filter_t unknown) {
447 
448  coap_opt_iterator_t opt_iter;
449  int ok = 1;
450 
451  coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL);
452 
453  while (coap_option_next(&opt_iter)) {
454 
455  /* The following condition makes use of the fact that
456  * coap_option_getb() returns -1 if type exceeds the bit-vector
457  * filter. As the vector is supposed to be large enough to hold
458  * the largest known option, we know that everything beyond is
459  * bad.
460  */
461  if (opt_iter.type & 0x01) {
462  /* first check the built-in critical options */
463  switch (opt_iter.type) {
470  case COAP_OPTION_ACCEPT:
473  case COAP_OPTION_BLOCK2:
474  case COAP_OPTION_BLOCK1:
475  break;
476  default:
477  if (coap_option_filter_get(ctx->known_options, opt_iter.type) <= 0) {
478  debug("unknown critical option %d\n", opt_iter.type);
479  ok = 0;
480 
481  /* When opt_iter.type is beyond our known option range,
482  * coap_option_filter_set() will return -1 and we are safe to leave
483  * this loop. */
484  if (coap_option_filter_set(unknown, opt_iter.type) == -1) {
485  break;
486  }
487  }
488  }
489  }
490  }
491 
492  return ok;
493 }
494 
495 void
497  coap_tid_t *id) {
498  coap_key_t h;
499 
500  memset(h, 0, sizeof(coap_key_t));
501 
502  /* Compare the transport address. */
503 
504 #ifdef WITH_POSIX
505  switch (peer->addr.sa.sa_family) {
506  case AF_INET:
507  coap_hash((const unsigned char *)&peer->addr.sin.sin_port,
508  sizeof(peer->addr.sin.sin_port), h);
509  coap_hash((const unsigned char *)&peer->addr.sin.sin_addr,
510  sizeof(peer->addr.sin.sin_addr), 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])) & INT_MAX;
531 }
532 
535  const coap_endpoint_t *local_interface,
536  const coap_address_t *dst,
537  coap_pdu_t *request) {
538  coap_pdu_t *response;
539  coap_tid_t result = COAP_INVALID_TID;
540 
541  if (request && request->hdr->type == COAP_MESSAGE_CON) {
542  response = coap_pdu_init(COAP_MESSAGE_ACK, 0, request->hdr->id,
543  sizeof(coap_pdu_t));
544  if (response) {
545  result = coap_send(context, local_interface, dst, response);
546  coap_delete_pdu(response);
547  }
548  }
549  return result;
550 }
551 
552 #if defined(WITH_POSIX) || defined(WITH_CONTIKI)
553 static coap_tid_t
555  const coap_endpoint_t *local_interface,
556  const coap_address_t *dst,
557  coap_pdu_t *pdu) {
558  ssize_t bytes_written;
560 
561  if ( !context || !dst || !pdu )
562  return id;
563 
564  /* Do not send error responses for requests that were received via
565  * IP multicast.
566  * FIXME: If No-Response option indicates interest, these responses
567  * must not be dropped. */
568  if (coap_is_mcast(&local_interface->addr) &&
569  COAP_RESPONSE_CLASS(pdu->hdr->code) > 2) {
570  return COAP_DROPPED_RESPONSE;
571  }
572 
573  bytes_written = context->network_send(context, local_interface, dst,
574  (unsigned char *)pdu->hdr, pdu->length);
575 
576  if (bytes_written >= 0) {
577  coap_transaction_id(dst, pdu, &id);
578  } else {
579  coap_log(LOG_CRIT, "coap_send_impl: %s\n", strerror(errno));
580  }
581 
582  return id;
583 }
584 #endif /* WITH_POSIX */
585 #ifdef WITH_LWIP
588  const coap_endpoint_t *local_interface,
589  const coap_address_t *dst,
590  coap_pdu_t *pdu) {
592 
593  if ( !context || !dst || !pdu )
594  {
595  return id;
596  }
597 
598  /* FIXME: we can't check this here with the existing infrastructure, but we
599  * should actually check that the pdu is not held by anyone but us. the
600  * respective pbuf is already exclusively owned by the pdu. */
601 
602  pbuf_realloc(pdu->pbuf, pdu->length);
603 
604  coap_transaction_id(dst, pdu, &id);
605 
606  udp_sendto(context->endpoint->pcb, pdu->pbuf,
607  &dst->addr, dst->port);
608 
609  return id;
610 }
611 #endif /* WITH_LWIP */
612 
613 coap_tid_t
615  const coap_endpoint_t *local_interface,
616  const coap_address_t *dst,
617  coap_pdu_t *pdu) {
618  return coap_send_impl(context, local_interface, dst, pdu);
619 }
620 
623  coap_pdu_t *request,
624  const coap_endpoint_t *local_interface,
625  const coap_address_t *dst,
626  unsigned char code,
627  coap_opt_filter_t opts) {
628  coap_pdu_t *response;
629  coap_tid_t result = COAP_INVALID_TID;
630 
631  assert(request);
632  assert(dst);
633 
634  response = coap_new_error_response(request, code, opts);
635  if (response) {
636  result = coap_send(context, local_interface, dst, response);
637  coap_delete_pdu(response);
638  }
639 
640  return result;
641 }
642 
645  const coap_endpoint_t *local_interface,
646  const coap_address_t *dst,
647  coap_pdu_t *request,
648  unsigned char type) {
649  coap_pdu_t *response;
650  coap_tid_t result = COAP_INVALID_TID;
651 
652  if (request) {
653  response = coap_pdu_init(type, 0, request->hdr->id, sizeof(coap_pdu_t));
654  if (response) {
655  result = coap_send(context, local_interface, dst, response);
656  coap_delete_pdu(response);
657  }
658  }
659  return result;
660 }
661 
673 static inline unsigned int
674 calc_timeout(unsigned char r) {
675  unsigned int result;
676 
677  /* The integer 1.0 as a Qx.FRAC_BITS */
678 #define FP1 Q(FRAC_BITS, 1)
679 
680  /* rounds val up and right shifts by frac positions */
681 #define SHR_FP(val,frac) (((val) + (1 << ((frac) - 1))) >> (frac))
682 
683  /* Inner term: multiply ACK_RANDOM_FACTOR by Q0.MAX_BITS[r] and
684  * make the result a rounded Qx.FRAC_BITS */
685  result = SHR_FP((ACK_RANDOM_FACTOR - FP1) * r, MAX_BITS);
686 
687  /* Add 1 to the inner term and multiply with ACK_TIMEOUT, then
688  * make the result a rounded Qx.FRAC_BITS */
689  result = SHR_FP(((result + FP1) * ACK_TIMEOUT), FRAC_BITS);
690 
691  /* Multiply with COAP_TICKS_PER_SECOND to yield system ticks
692  * (yields a Qx.FRAC_BITS) and shift to get an integer */
693  return SHR_FP((COAP_TICKS_PER_SECOND * result), FRAC_BITS);
694 
695 #undef FP1
696 #undef SHR_FP
697 }
698 
701  const coap_endpoint_t *local_interface,
702  const coap_address_t *dst,
703  coap_pdu_t *pdu) {
704  coap_queue_t *node;
705  coap_tick_t now;
706  unsigned char r;
707 
708  node = coap_new_node();
709  if (!node) {
710  debug("coap_send_confirmed: insufficient memory\n");
711  return COAP_INVALID_TID;
712  }
713 
714  node->id = coap_send_impl(context, local_interface, dst, pdu);
715  if (COAP_INVALID_TID == node->id) {
716  debug("coap_send_confirmed: error sending pdu\n");
717  coap_free_node(node);
718  return COAP_INVALID_TID;
719  }
720 
721  prng((unsigned char *)&r,sizeof(r));
722 
723  /* add timeout in range [ACK_TIMEOUT...ACK_TIMEOUT * ACK_RANDOM_FACTOR] */
724  node->timeout = calc_timeout(r);
725 
726  node->local_if = *local_interface;
727  memcpy(&node->remote, dst, sizeof(coap_address_t));
728  node->pdu = pdu;
729 
730  /* Set timer for pdu retransmission. If this is the first element in
731  * the retransmission queue, the base time is set to the current
732  * time and the retransmission time is node->timeout. If there is
733  * already an entry in the sendqueue, we must check if this node is
734  * to be retransmitted earlier. Therefore, node->timeout is first
735  * normalized to the base time and then inserted into the queue with
736  * an adjusted relative time.
737  */
738  coap_ticks(&now);
739  if (context->sendqueue == NULL) {
740  node->t = node->timeout;
741  context->sendqueue_basetime = now;
742  } else {
743  /* make node->t relative to context->sendqueue_basetime */
744  node->t = (now - context->sendqueue_basetime) + node->timeout;
745  }
746 
747  coap_insert_node(&context->sendqueue, node);
748 
749 #ifdef WITH_LWIP
750  if (node == context->sendqueue) /* don't bother with timer stuff if there are earlier retransmits */
751  coap_retransmittimer_restart(context);
752 #endif
753 
754 #ifdef WITH_CONTIKI
755  { /* (re-)initialize retransmission timer */
756  coap_queue_t *nextpdu;
757 
758  nextpdu = coap_peek_next(context);
759  assert(nextpdu); /* we have just inserted a node */
760 
761  /* must set timer within the context of the retransmit process */
762  PROCESS_CONTEXT_BEGIN(&coap_retransmit_process);
763  etimer_set(&context->retransmit_timer, nextpdu->t);
764  PROCESS_CONTEXT_END(&coap_retransmit_process);
765  }
766 #endif /* WITH_CONTIKI */
767 
768  return node->id;
769 }
770 
773  if (!context || !node)
774  return COAP_INVALID_TID;
775 
776  /* re-initialize timeout when maximum number of retransmissions are not reached yet */
778  node->retransmit_cnt++;
779  node->t = node->timeout << node->retransmit_cnt;
780  coap_insert_node(&context->sendqueue, node);
781 #ifdef WITH_LWIP
782  if (node == context->sendqueue) /* don't bother with timer stuff if there are earlier retransmits */
783  coap_retransmittimer_restart(context);
784 #endif
785 
786  debug("** retransmission #%d of transaction %d\n",
787  node->retransmit_cnt, ntohs(node->pdu->hdr->id));
788 
789  node->id = coap_send_impl(context, &node->local_if,
790  &node->remote, node->pdu);
791  return node->id;
792  }
793 
794  /* no more retransmissions, remove node from system */
795 
796 #ifndef WITH_CONTIKI
797  debug("** removed transaction %d\n", ntohs(node->id));
798 #endif
799 
800 #ifndef WITHOUT_OBSERVE
801  /* Check if subscriptions exist that should be canceled after
802  COAP_MAX_NOTIFY_FAILURES */
803  if (node->pdu->hdr->code >= 64) {
804  str token = { 0, NULL };
805 
806  token.length = node->pdu->hdr->token_length;
807  token.s = node->pdu->hdr->token;
808 
809  coap_handle_failed_notify(context, &node->remote, &token);
810  }
811 #endif /* WITHOUT_OBSERVE */
812 
813  /* And finally delete the node */
814  coap_delete_node( node );
815  return COAP_INVALID_TID;
816 }
817 
818 void coap_dispatch(coap_context_t *context, coap_queue_t *rcvd);
819 
820 #ifndef WITH_LWIP
821 /* WITH_LWIP, this is handled by coap_recv in a different way */
822 int
824  ssize_t bytes_read = -1;
825  coap_packet_t *packet;
826  coap_address_t src;
827  int result = -1; /* the value to be returned */
828 
829  coap_address_init(&src);
830 
831  bytes_read = ctx->network_read(ctx->endpoint, &packet);
832 
833  if ( bytes_read < 0 ) {
834  warn("coap_read: recvfrom");
835  } else {
836  result = coap_handle_message(ctx, packet);
837  }
838 
839  coap_free_packet(packet);
840 
841  return result;
842 }
843 #endif /* not WITH_LWIP */
844 
845 int
847  coap_packet_t *packet) {
848  /* const coap_address_t *remote, */
849  /* unsigned char *msg, size_t msg_len) { */
850  unsigned char *msg;
851  size_t msg_len;
852  coap_queue_t *node;
853 
854  /* the negated result code */
855  enum result_t { RESULT_OK, RESULT_ERR_EARLY, RESULT_ERR };
856  int result = RESULT_ERR_EARLY;
857 
858  coap_packet_get_memmapped(packet, &msg, &msg_len);
859 
860  if (msg_len < sizeof(coap_hdr_t)) {
861  debug("coap_handle_message: discarded invalid frame\n" );
862  goto error_early;
863  }
864 
865  /* check version identifier */
866  if (((*msg >> 6) & 0x03) != COAP_DEFAULT_VERSION) {
867  debug("coap_handle_message: unknown protocol version %d\n", (*msg >> 6) & 0x03);
868  goto error_early;
869  }
870 
871  node = coap_new_node();
872  if (!node) {
873  goto error_early;
874  }
875 
876  /* from this point, the result code indicates that */
877  result = RESULT_ERR;
878 
879 #ifdef WITH_LWIP
880  node->pdu = coap_pdu_from_pbuf(coap_packet_extract_pbuf(packet));
881 #else
882  node->pdu = coap_pdu_init(0, 0, 0, msg_len);
883 #endif
884  if (!node->pdu) {
885  goto error;
886  }
887 
888  if (!coap_pdu_parse(msg, msg_len, node->pdu)) {
889  warn("discard malformed PDU\n");
890  goto error;
891  }
892 
893  coap_ticks(&node->t);
894 
895  coap_packet_populate_endpoint(packet, &node->local_if);
896  coap_packet_copy_source(packet, &node->remote);
897 
898  /* and add new node to receive queue */
899  coap_transaction_id(&node->remote, node->pdu, &node->id);
900 
901 #ifndef NDEBUG
902  if (LOG_DEBUG <= coap_get_log_level()) {
903 #ifndef INET6_ADDRSTRLEN
904 #define INET6_ADDRSTRLEN 40
905 #endif
906 
914  coap_show_pdu(node->pdu);
915  }
916 #endif
917 
918  coap_dispatch(ctx, node);
919  return -RESULT_OK;
920 
921  error:
922  /* FIXME: send back RST? */
923  coap_delete_node(node);
924  return -result;
925 
926  error_early:
927  return -result;
928 }
929 
930 int
932  coap_queue_t *p, *q;
933 
934  if ( !queue || !*queue)
935  return 0;
936 
937  /* replace queue head if PDU's time is less than head's time */
938 
939  if ( id == (*queue)->id ) { /* found transaction */
940  *node = *queue;
941  *queue = (*queue)->next;
942  if (*queue) { /* adjust relative time of new queue head */
943  (*queue)->t += (*node)->t;
944  }
945  (*node)->next = NULL;
946  /* coap_delete_node( q ); */
947  debug("*** removed transaction %u\n", id);
948  return 1;
949  }
950 
951  /* search transaction to remove (only first occurence will be removed) */
952  q = *queue;
953  do {
954  p = q;
955  q = q->next;
956  } while ( q && id != q->id );
957 
958  if ( q ) { /* found transaction */
959  p->next = q->next;
960  if (p->next) { /* must update relative time of p->next */
961  p->next->t += q->t;
962  }
963  q->next = NULL;
964  *node = q;
965  /* coap_delete_node( q ); */
966  debug("*** removed transaction %u\n", id);
967  return 1;
968  }
969 
970  return 0;
971 
972 }
973 
974 static inline int
975 token_match(const unsigned char *a, size_t alen,
976  const unsigned char *b, size_t blen) {
977  return alen == blen && (alen == 0 || memcmp(a, b, alen) == 0);
978 }
979 
980 void
982  const unsigned char *token, size_t token_length) {
983  /* cancel all messages in sendqueue that are for dst
984  * and use the specified token */
985  coap_queue_t *p, *q;
986 
987  while (context->sendqueue &&
988  coap_address_equals(dst, &context->sendqueue->remote) &&
989  token_match(token, token_length,
990  context->sendqueue->pdu->hdr->token,
991  context->sendqueue->pdu->hdr->token_length)) {
992  q = context->sendqueue;
993  context->sendqueue = q->next;
994  debug("**** removed transaction %d\n", ntohs(q->pdu->hdr->id));
995  coap_delete_node(q);
996  }
997 
998  if (!context->sendqueue)
999  return;
1000 
1001  p = context->sendqueue;
1002  q = p->next;
1003 
1004  /* when q is not NULL, it does not match (dst, token), so we can skip it */
1005  while (q) {
1006  if (coap_address_equals(dst, &q->remote) &&
1007  token_match(token, token_length,
1008  q->pdu->hdr->token, q->pdu->hdr->token_length)) {
1009  p->next = q->next;
1010  debug("**** removed transaction %d\n", ntohs(q->pdu->hdr->id));
1011  coap_delete_node(q);
1012  q = p->next;
1013  } else {
1014  p = q;
1015  q = q->next;
1016  }
1017  }
1018 }
1019 
1020 coap_queue_t *
1022  while (queue && queue->id != id)
1023  queue = queue->next;
1024 
1025  return queue;
1026 }
1027 
1028 coap_pdu_t *
1029 coap_new_error_response(coap_pdu_t *request, unsigned char code,
1030  coap_opt_filter_t opts) {
1031  coap_opt_iterator_t opt_iter;
1032  coap_pdu_t *response;
1033  size_t size = sizeof(coap_hdr_t) + request->hdr->token_length;
1034  int type;
1035  coap_opt_t *option;
1036  unsigned short opt_type = 0; /* used for calculating delta-storage */
1037 
1039  char *phrase = coap_response_phrase(code);
1040 
1041  /* Need some more space for the error phrase and payload start marker */
1042  if (phrase)
1043  size += strlen(phrase) + 1;
1044 #endif
1045 
1046  assert(request);
1047 
1048  /* cannot send ACK if original request was not confirmable */
1049  type = request->hdr->type == COAP_MESSAGE_CON
1051  : COAP_MESSAGE_NON;
1052 
1053  /* Estimate how much space we need for options to copy from
1054  * request. We always need the Token, for 4.02 the unknown critical
1055  * options must be included as well. */
1056  coap_option_clrb(opts, COAP_OPTION_CONTENT_TYPE); /* we do not want this */
1057 
1058  coap_option_iterator_init(request, &opt_iter, opts);
1059 
1060  /* Add size of each unknown critical option. As known critical
1061  options as well as elective options are not copied, the delta
1062  value might grow.
1063  */
1064  while((option = coap_option_next(&opt_iter))) {
1065  unsigned short delta = opt_iter.type - opt_type;
1066  /* calculate space required to encode (opt_iter.type - opt_type) */
1067  if (delta < 13) {
1068  size++;
1069  } else if (delta < 269) {
1070  size += 2;
1071  } else {
1072  size += 3;
1073  }
1074 
1075  /* add coap_opt_length(option) and the number of additional bytes
1076  * required to encode the option length */
1077 
1078  size += coap_opt_length(option);
1079  switch (*option & 0x0f) {
1080  case 0x0e:
1081  size++;
1082  /* fall through */
1083  case 0x0d:
1084  size++;
1085  break;
1086  default:
1087  ;
1088  }
1089 
1090  opt_type = opt_iter.type;
1091  }
1092 
1093  /* Now create the response and fill with options and payload data. */
1094  response = coap_pdu_init(type, code, request->hdr->id, size);
1095  if (response) {
1096  /* copy token */
1097  if (!coap_add_token(response, request->hdr->token_length,
1098  request->hdr->token)) {
1099  debug("cannot add token to error response\n");
1100  coap_delete_pdu(response);
1101  return NULL;
1102  }
1103 
1104  /* copy all options */
1105  coap_option_iterator_init(request, &opt_iter, opts);
1106  while((option = coap_option_next(&opt_iter)))
1107  coap_add_option(response, opt_iter.type,
1108  COAP_OPT_LENGTH(option),
1109  COAP_OPT_VALUE(option));
1110 
1111 #if COAP_ERROR_PHRASE_LENGTH > 0
1112  /* note that diagnostic messages do not need a Content-Format option. */
1113  if (phrase)
1114  coap_add_data(response, strlen(phrase), (unsigned char *)phrase);
1115 #endif
1116  }
1117 
1118  return response;
1119 }
1120 
1125 static inline size_t
1126 get_wkc_len(coap_context_t *context, coap_opt_t *query_filter) {
1127  unsigned char buf[1];
1128  size_t len = 0;
1129 
1130  if (coap_print_wellknown(context, buf, &len, UINT_MAX, query_filter)
1132  warn("cannot determine length of /.well-known/core\n");
1133  return 0;
1134  }
1135 
1136  debug("get_wkc_len: coap_print_wellknown() returned %zu\n", len);
1137 
1138  return len;
1139 }
1140 
1141 #define SZX_TO_BYTES(SZX) ((size_t)(1 << ((SZX) + 4)))
1142 
1143 coap_pdu_t *
1145  coap_pdu_t *resp;
1146  coap_opt_iterator_t opt_iter;
1147  size_t len, wkc_len;
1148  unsigned char buf[2];
1149  int result = 0;
1150  int need_block2 = 0; /* set to 1 if Block2 option is required */
1151  coap_block_t block;
1152  coap_opt_t *query_filter;
1153  size_t offset = 0;
1154 
1155  resp = coap_pdu_init(request->hdr->type == COAP_MESSAGE_CON
1156  ? COAP_MESSAGE_ACK
1157  : COAP_MESSAGE_NON,
1158  COAP_RESPONSE_CODE(205),
1159  request->hdr->id, COAP_MAX_PDU_SIZE);
1160  if (!resp) {
1161  debug("coap_wellknown_response: cannot create PDU\n");
1162  return NULL;
1163  }
1164 
1165  if (!coap_add_token(resp, request->hdr->token_length, request->hdr->token)) {
1166  debug("coap_wellknown_response: cannot add token\n");
1167  goto error;
1168  }
1169 
1170  query_filter = coap_check_option(request, COAP_OPTION_URI_QUERY, &opt_iter);
1171  wkc_len = get_wkc_len(context, query_filter);
1172 
1173  /* The value of some resources is undefined and get_wkc_len will return 0.*/
1174  if (wkc_len == 0){
1175  debug("coap_wellknown_response: undefined resource\n");
1176  /* set error code 4.00 Bad Request*/
1177  resp->hdr->code = COAP_RESPONSE_CODE(400);
1178  resp->length = sizeof(coap_hdr_t) + resp->hdr->token_length;
1179  return resp;
1180  }
1181 
1182  /* check whether the request contains the Block2 option */
1183  if (coap_get_block(request, COAP_OPTION_BLOCK2, &block)) {
1184  debug("create block\n");
1185  offset = block.num << (block.szx + 4);
1186  if (block.szx > 6) { /* invalid, MUST lead to 4.00 Bad Request */
1187  resp->hdr->code = COAP_RESPONSE_CODE(400);
1188  return resp;
1189  } else if (block.szx > COAP_MAX_BLOCK_SZX) {
1190  block.szx = COAP_MAX_BLOCK_SZX;
1191  block.num = offset >> (block.szx + 4);
1192  }
1193 
1194  need_block2 = 1;
1195  }
1196 
1197  /* Check if there is sufficient space to add Content-Format option
1198  * and data. We do this before adding the Content-Format option to
1199  * avoid sending error responses with that option but no actual
1200  * content. */
1201  if (resp->max_size <= (size_t)resp->length + 3) {
1202  debug("coap_wellknown_response: insufficient storage space\n");
1203  goto error;
1204  }
1205 
1206  /* Add Content-Format. As we have checked for available storage,
1207  * nothing should go wrong here. */
1211  coap_encode_var_bytes(buf,
1213 
1214  /* check if Block2 option is required even if not requested */
1215  if (!need_block2 && (resp->max_size - (size_t)resp->length < wkc_len)) {
1216  assert(resp->length <= resp->max_size);
1217  const size_t payloadlen = resp->max_size - resp->length;
1218  /* yes, need block-wise transfer */
1219  block.num = 0;
1220  block.m = 0; /* the M bit is set by coap_write_block_opt() */
1221  block.szx = COAP_MAX_BLOCK_SZX;
1222  while (payloadlen < SZX_TO_BYTES(block.szx)) {
1223  if (block.szx == 0) {
1224  debug("coap_wellknown_response: message to small even for szx == 0\n");
1225  goto error;
1226  } else {
1227  block.szx--;
1228  }
1229  }
1230 
1231  need_block2 = 1;
1232  }
1233 
1234  /* write Block2 option if necessary */
1235  if (need_block2) {
1236  if (coap_write_block_opt(&block, COAP_OPTION_BLOCK2, resp, wkc_len) < 0) {
1237  debug("coap_wellknown_response: cannot add Block2 option\n");
1238  goto error;
1239  }
1240  }
1241 
1242  /* Manually set payload of response to let print_wellknown() write,
1243  * into our buffer without copying data. */
1244 
1245  resp->data = (unsigned char *)resp->hdr + resp->length;
1246  *resp->data = COAP_PAYLOAD_START;
1247  resp->data++;
1248  resp->length++;
1249  len = need_block2 ? SZX_TO_BYTES(block.szx) : resp->max_size - resp->length;
1250 
1251  result = coap_print_wellknown(context, resp->data, &len, offset, query_filter);
1252  if ((result & COAP_PRINT_STATUS_ERROR) != 0) {
1253  debug("coap_print_wellknown failed\n");
1254  goto error;
1255  }
1256 
1257  resp->length += COAP_PRINT_OUTPUT_LENGTH(result);
1258  return resp;
1259 
1260  error:
1261  /* set error code 5.03 and remove all options and data from response */
1262  resp->hdr->code = COAP_RESPONSE_CODE(503);
1263  resp->length = sizeof(coap_hdr_t) + resp->hdr->token_length;
1264  return resp;
1265 }
1266 
1277 static int
1278 coap_cancel(coap_context_t *context, const coap_queue_t *sent) {
1279 #ifndef WITHOUT_OBSERVE
1280  str token = { 0, NULL };
1281  int num_cancelled = 0; /* the number of observers cancelled */
1282 
1283  /* remove observer for this resource, if any
1284  * get token from sent and try to find a matching resource. Uh!
1285  */
1286 
1287  COAP_SET_STR(&token, sent->pdu->hdr->token_length, sent->pdu->hdr->token);
1288 
1289  RESOURCES_ITER(context->resources, r) {
1290  num_cancelled += coap_delete_observer(r, &sent->remote, &token);
1291  coap_cancel_all_messages(context, &sent->remote, token.s, token.length);
1292  }
1293 
1294  return num_cancelled;
1295 #else /* WITOUT_OBSERVE */
1296  return 0;
1297 #endif /* WITOUT_OBSERVE */
1298 }
1299 
1305 
1334 static enum respond_t
1335 no_response(coap_pdu_t *request, coap_pdu_t *response) {
1336  coap_opt_t *nores;
1337  coap_opt_iterator_t opt_iter;
1338  uint8_t val = 0;
1339 
1340  assert(request);
1341  assert(response);
1342 
1343  if (COAP_RESPONSE_CLASS(response->hdr->code) > 0) {
1344  nores = coap_check_option(request, COAP_OPTION_NORESPONSE, &opt_iter);
1345 
1346  if (nores) {
1348 
1349  /* The response should be dropped when the bit corresponding to
1350  * the response class is set (cf. table in funtion
1351  * documentation). When a No-Response option is present and the
1352  * bit is not set, the sender explicitly indicates interest in
1353  * this response. */
1354  if (((1 << (COAP_RESPONSE_CLASS(response->hdr->code) - 1)) & val) > 0) {
1355  return RESPONSE_DROP;
1356  } else {
1357  return RESPONSE_SEND;
1358  }
1359  }
1360  }
1361 
1362  /* Default behavior applies when we are not dealing with a response
1363  * (class == 0) or the request did not contain a No-Response option.
1364  */
1365  return RESPONSE_DEFAULT;
1366 }
1367 
1368 #define WANT_WKC(Pdu,Key) \
1369  (((Pdu)->hdr->code == COAP_REQUEST_GET) && is_wkc(Key))
1370 
1371 static void
1373  coap_method_handler_t h = NULL;
1374  coap_pdu_t *response = NULL;
1376  coap_resource_t *resource;
1377  coap_key_t key;
1378  /* The respond field indicates whether a response must be treated
1379  * specially due to a No-Response option that declares disinterest
1380  * or interest in a specific response class. DEFAULT indicates that
1381  * No-Response has not been specified. */
1382  enum respond_t respond = RESPONSE_DEFAULT;
1383 
1384  coap_option_filter_clear(opt_filter);
1385 
1386  /* try to find the resource from the request URI */
1387  coap_hash_request_uri(node->pdu, key);
1388  resource = coap_get_resource_from_key(context, key);
1389 
1390  if (!resource) {
1391  /* The resource was not found. Check if the request URI happens to
1392  * be the well-known URI. In that case, we generate a default
1393  * response, otherwise, we return 4.04 */
1394 
1395  switch(node->pdu->hdr->code) {
1396 
1397  case COAP_REQUEST_GET:
1398  if (is_wkc(key)) { /* GET request for .well-known/core */
1399  info("create default response for %s\n", COAP_DEFAULT_URI_WELLKNOWN);
1400  response = coap_wellknown_response(context, node->pdu);
1401 
1402  } else { /* GET request for any another resource, return 4.04 */
1403 
1404  debug("GET for unknown resource 0x%02x%02x%02x%02x, return 4.04\n",
1405  key[0], key[1], key[2], key[3]);
1406  response =
1408  opt_filter);
1409  }
1410  break;
1411 
1412  default: /* any other request type */
1413 
1414  debug("unhandled request for unknown resource 0x%02x%02x%02x%02x\r\n",
1415  key[0], key[1], key[2], key[3]);
1416 
1417  response = coap_new_error_response(node->pdu, COAP_RESPONSE_CODE(405),
1418  opt_filter);
1419  }
1420 
1421  if (response
1422  && (no_response(node->pdu, response) != RESPONSE_DROP)
1423  && (coap_send(context, &node->local_if,
1424  &node->remote, response) == COAP_INVALID_TID)) {
1425  warn("cannot send response for transaction %u\n", node->id);
1426  }
1427  coap_delete_pdu(response);
1428 
1429  return;
1430  }
1431 
1432  /* the resource was found, check if there is a registered handler */
1433  if ((size_t)node->pdu->hdr->code - 1 <
1434  sizeof(resource->handler)/sizeof(coap_method_handler_t))
1435  h = resource->handler[node->pdu->hdr->code - 1];
1436 
1437  if (h) {
1438  debug("call custom handler for resource 0x%02x%02x%02x%02x\n",
1439  key[0], key[1], key[2], key[3]);
1440  response = coap_pdu_init(node->pdu->hdr->type == COAP_MESSAGE_CON
1442  : COAP_MESSAGE_NON,
1443  0, node->pdu->hdr->id, COAP_MAX_PDU_SIZE);
1444 
1445  /* Implementation detail: coap_add_token() immediately returns 0
1446  if response == NULL */
1447  if (coap_add_token(response, node->pdu->hdr->token_length,
1448  node->pdu->hdr->token)) {
1449  str token = { node->pdu->hdr->token_length, node->pdu->hdr->token };
1450  coap_opt_iterator_t opt_iter;
1451  coap_opt_t *observe = NULL;
1452  int observe_action = COAP_OBSERVE_CANCEL;
1453 
1454  /* check for Observe option */
1455  if (resource->observable) {
1456  observe = coap_check_option(node->pdu, COAP_OPTION_OBSERVE, &opt_iter);
1457  if (observe) {
1458  observe_action =
1460  coap_opt_length(observe));
1461 
1462  if ((observe_action & COAP_OBSERVE_CANCEL) == 0) {
1463  coap_subscription_t *subscription;
1464 
1465  coap_log(LOG_DEBUG, "create new subscription\n");
1466  subscription = coap_add_observer(resource, &node->local_if,
1467  &node->remote, &token);
1468  if (subscription) {
1469  coap_touch_observer(context, &node->remote, &token);
1470  }
1471  } else {
1472  coap_log(LOG_DEBUG, "removed observer\n");
1473  coap_delete_observer(resource, &node->remote, &token);
1474  }
1475  }
1476  }
1477 
1478  h(context, resource, &node->local_if, &node->remote,
1479  node->pdu, &token, response);
1480 
1481  respond = no_response(node->pdu, response);
1482  if (respond != RESPONSE_DROP) {
1483  if (observe && (COAP_RESPONSE_CLASS(response->hdr->code) > 2)) {
1484  coap_log(LOG_DEBUG, "removed observer\n");
1485  coap_delete_observer(resource, &node->remote, &token);
1486  }
1487 
1488  /* If original request contained a token, and the registered
1489  * application handler made no changes to the response, then
1490  * this is an empty ACK with a token, which is a malformed
1491  * PDU */
1492  if ((response->hdr->type == COAP_MESSAGE_ACK)
1493  && (response->hdr->code == 0)) {
1494  /* Remove token from otherwise-empty acknowledgment PDU */
1495  response->hdr->token_length = 0;
1496  response->length = sizeof(coap_hdr_t);
1497  }
1498 
1499  if ((respond == RESPONSE_SEND)
1500  || /* RESPOND_DEFAULT */
1501  (response->hdr->type != COAP_MESSAGE_NON ||
1502  (response->hdr->code >= 64
1503  && !coap_mcast_interface(&node->local_if)))) {
1504 
1505  if (coap_send(context, &node->local_if,
1506  &node->remote, response) == COAP_INVALID_TID) {
1507  debug("cannot send response for message %d\n", node->pdu->hdr->id);
1508  }
1509  }
1510  }
1511  coap_delete_pdu(response);
1512  } else {
1513  warn("cannot generate response\r\n");
1514  }
1515  } else {
1516  if (WANT_WKC(node->pdu, key)) {
1517  debug("create default response for %s\n", COAP_DEFAULT_URI_WELLKNOWN);
1518  response = coap_wellknown_response(context, node->pdu);
1519  debug("have wellknown response %p\n", (void *)response);
1520  } else
1521  response = coap_new_error_response(node->pdu, COAP_RESPONSE_CODE(405),
1522  opt_filter);
1523 
1524  if (response && (no_response(node->pdu, response) != RESPONSE_DROP)) {
1525  if (coap_send(context, &node->local_if, &node->remote,
1526  response) == COAP_INVALID_TID) {
1527  debug("cannot send response for transaction %u\n", node->id);
1528  }
1529  }
1530  coap_delete_pdu(response);
1531  }
1532 }
1533 
1534 static inline void
1536  coap_queue_t *sent, coap_queue_t *rcvd) {
1537 
1538  coap_send_ack(context, &rcvd->local_if, &rcvd->remote, rcvd->pdu);
1539 
1540  /* In a lossy context, the ACK of a separate response may have
1541  * been lost, so we need to stop retransmitting requests with the
1542  * same token.
1543  */
1544  coap_cancel_all_messages(context, &rcvd->remote,
1545  rcvd->pdu->hdr->token,
1546  rcvd->pdu->hdr->token_length);
1547 
1548  /* Call application-specific response handler when available. */
1549  if (context->response_handler) {
1550  context->response_handler(context, &rcvd->local_if,
1551  &rcvd->remote, sent ? sent->pdu : NULL,
1552  rcvd->pdu, rcvd->id);
1553  }
1554 }
1555 
1556 static inline int
1557 #ifdef __GNUC__
1558 handle_locally(coap_context_t *context __attribute__ ((unused)),
1559  coap_queue_t *node __attribute__ ((unused))) {
1560 #else /* not a GCC */
1562 #endif /* GCC */
1563  /* this function can be used to check if node->pdu is really for us */
1564  return 1;
1565 }
1566 
1567 void
1569  coap_queue_t *sent = NULL;
1570  coap_pdu_t *response;
1572 
1573  if (!context)
1574  return;
1575 
1576  memset(opt_filter, 0, sizeof(coap_opt_filter_t));
1577 
1578  {
1579  /* version has been checked in coap_handle_message() */
1580  /* if ( rcvd->pdu->hdr->version != COAP_DEFAULT_VERSION ) { */
1581  /* debug("dropped packet with unknown version %u\n", rcvd->pdu->hdr->version); */
1582  /* goto cleanup; */
1583  /* } */
1584 
1585  switch (rcvd->pdu->hdr->type) {
1586  case COAP_MESSAGE_ACK:
1587  /* find transaction in sendqueue to stop retransmission */
1588  coap_remove_from_queue(&context->sendqueue, rcvd->id, &sent);
1589 
1590  if (rcvd->pdu->hdr->code == 0)
1591  goto cleanup;
1592 
1593  /* if sent code was >= 64 the message might have been a
1594  * notification. Then, we must flag the observer to be alive
1595  * by setting obs->fail_cnt = 0. */
1596  if (sent && COAP_RESPONSE_CLASS(sent->pdu->hdr->code) == 2) {
1597  const str token =
1598  { sent->pdu->hdr->token_length, sent->pdu->hdr->token };
1599  coap_touch_observer(context, &sent->remote, &token);
1600  }
1601  break;
1602 
1603  case COAP_MESSAGE_RST :
1604  /* We have sent something the receiver disliked, so we remove
1605  * not only the transaction but also the subscriptions we might
1606  * have. */
1607 
1608 #ifndef WITH_CONTIKI
1609  coap_log(LOG_ALERT, "got RST for message %u\n", ntohs(rcvd->pdu->hdr->id));
1610 #else /* WITH_CONTIKI */
1611  coap_log(LOG_ALERT, "got RST for message %u\n", uip_ntohs(rcvd->pdu->hdr->id));
1612 #endif /* WITH_CONTIKI */
1613 
1614  /* find transaction in sendqueue to stop retransmission */
1615  coap_remove_from_queue(&context->sendqueue, rcvd->id, &sent);
1616 
1617  if (sent)
1618  coap_cancel(context, sent);
1619  goto cleanup;
1620 
1621  case COAP_MESSAGE_NON : /* check for unknown critical options */
1622  if (coap_option_check_critical(context, rcvd->pdu, opt_filter) == 0)
1623  goto cleanup;
1624  break;
1625 
1626  case COAP_MESSAGE_CON : /* check for unknown critical options */
1627  if (coap_option_check_critical(context, rcvd->pdu, opt_filter) == 0) {
1628 
1629  /* FIXME: send response only if we have received a request. Otherwise,
1630  * send RST. */
1631  response =
1632  coap_new_error_response(rcvd->pdu, COAP_RESPONSE_CODE(402), opt_filter);
1633 
1634  if (!response)
1635  warn("coap_dispatch: cannot create error response\n");
1636  else {
1637  if (coap_send(context, &rcvd->local_if, &rcvd->remote, response)
1638  == COAP_INVALID_TID) {
1639  warn("coap_dispatch: error sending response\n");
1640  }
1641  coap_delete_pdu(response);
1642  }
1643 
1644  goto cleanup;
1645  }
1646  default: break;
1647  }
1648 
1649  /* Pass message to upper layer if a specific handler was
1650  * registered for a request that should be handled locally. */
1651  if (handle_locally(context, rcvd)) {
1652  if (COAP_MESSAGE_IS_REQUEST(rcvd->pdu->hdr))
1653  handle_request(context, rcvd);
1654  else if (COAP_MESSAGE_IS_RESPONSE(rcvd->pdu->hdr))
1655  handle_response(context, sent, rcvd);
1656  else {
1657  debug("dropped message with invalid code (%d.%02d)\n",
1658  COAP_RESPONSE_CLASS(rcvd->pdu->hdr->code),
1659  rcvd->pdu->hdr->code & 0x1f);
1660 
1661  if (!coap_is_mcast(&rcvd->local_if.addr)) {
1662  coap_send_message_type(context, &rcvd->local_if, &rcvd->remote,
1663  rcvd->pdu, COAP_MESSAGE_RST);
1664  }
1665  }
1666  }
1667 
1668  cleanup:
1669  coap_delete_node(sent);
1670  coap_delete_node(rcvd);
1671  }
1672 }
1673 
1674 int
1676  return !context || (context->sendqueue == NULL);
1677 }
1678 
1679 #ifdef WITH_CONTIKI
1680 
1681 /*---------------------------------------------------------------------------*/
1682 /* CoAP message retransmission */
1683 /*---------------------------------------------------------------------------*/
1684 PROCESS_THREAD(coap_retransmit_process, ev, data)
1685 {
1686  coap_tick_t now;
1687  coap_queue_t *nextpdu;
1688 
1689  PROCESS_BEGIN();
1690 
1691  debug("Started retransmit process\r\n");
1692 
1693  while(1) {
1694  PROCESS_YIELD();
1695  if (ev == PROCESS_EVENT_TIMER) {
1696  if (etimer_expired(&the_coap_context.retransmit_timer)) {
1697 
1698  nextpdu = coap_peek_next(&the_coap_context);
1699 
1700  coap_ticks(&now);
1701  while (nextpdu && nextpdu->t <= now) {
1702  coap_retransmit(&the_coap_context, coap_pop_next(&the_coap_context));
1703  nextpdu = coap_peek_next(&the_coap_context);
1704  }
1705 
1706  /* need to set timer to some value even if no nextpdu is available */
1707  etimer_set(&the_coap_context.retransmit_timer,
1708  nextpdu ? nextpdu->t - now : 0xFFFF);
1709  }
1710 #ifndef WITHOUT_OBSERVE
1711  if (etimer_expired(&the_coap_context.notify_timer)) {
1712  coap_check_notify(&the_coap_context);
1713  etimer_reset(&the_coap_context.notify_timer);
1714  }
1715 #endif /* WITHOUT_OBSERVE */
1716  }
1717  }
1718 
1719  PROCESS_END();
1720 }
1721 /*---------------------------------------------------------------------------*/
1722 
1723 #endif /* WITH_CONTIKI */
1724 
1725 #ifdef WITH_LWIP
1726 /* FIXME: retransmits that are not required any more due to incoming packages
1727  * do *not* get cleared at the moment, the wakeup when the transmission is due
1728  * is silently accepted. this is mainly due to the fact that the required
1729  * checks are similar in two places in the code (when receiving ACK and RST)
1730  * and that they cause more than one patch chunk, as it must be first checked
1731  * whether the sendqueue item to be dropped is the next one pending, and later
1732  * the restart function has to be called. nothing insurmountable, but it can
1733  * also be implemented when things have stabilized, and the performance
1734  * penality is minimal
1735  *
1736  * also, this completely ignores COAP_RESOURCE_CHECK_TIME.
1737  * */
1738 
1739 static void coap_retransmittimer_execute(void *arg)
1740 {
1741  coap_context_t *ctx = (coap_context_t*)arg;
1742  coap_tick_t now;
1743  coap_tick_t elapsed;
1744  coap_queue_t *nextinqueue;
1745 
1746  ctx->timer_configured = 0;
1747 
1748  coap_ticks(&now);
1749 
1750  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 */
1751 
1752  nextinqueue = coap_peek_next(ctx);
1753  while (nextinqueue != NULL)
1754  {
1755  if (nextinqueue->t > elapsed) {
1756  nextinqueue->t -= elapsed;
1757  break;
1758  } else {
1759  elapsed -= nextinqueue->t;
1760  coap_retransmit(ctx, coap_pop_next(ctx));
1761  nextinqueue = coap_peek_next(ctx);
1762  }
1763  }
1764 
1765  ctx->sendqueue_basetime = now;
1766 
1767  coap_retransmittimer_restart(ctx);
1768 }
1769 
1770 static void coap_retransmittimer_restart(coap_context_t *ctx)
1771 {
1772  coap_tick_t now, elapsed, delay;
1773 
1774  if (ctx->timer_configured)
1775  {
1776  printf("clearing\n");
1777  sys_untimeout(coap_retransmittimer_execute, (void*)ctx);
1778  ctx->timer_configured = 0;
1779  }
1780  if (ctx->sendqueue != NULL)
1781  {
1782  coap_ticks(&now);
1783  elapsed = now - ctx->sendqueue_basetime;
1784  if (ctx->sendqueue->t >= elapsed) {
1785  delay = ctx->sendqueue->t - elapsed;
1786  } else {
1787  /* a strange situation, but not completely impossible.
1788  *
1789  * this happens, for example, right after
1790  * coap_retransmittimer_execute, when a retransmission
1791  * was *just not yet* due, and the clock ticked before
1792  * our coap_ticks was called.
1793  *
1794  * not trying to retransmit anything now, as it might
1795  * cause uncontrollable recursion; let's just try again
1796  * with the next main loop run.
1797  * */
1798  delay = 0;
1799  }
1800 
1801  printf("scheduling for %d ticks\n", delay);
1802  sys_timeout(delay, coap_retransmittimer_execute, (void*)ctx);
1803  ctx->timer_configured = 1;
1804  }
1805 }
1806 #endif
int coap_get_block(coap_pdu_t *pdu, unsigned short type, coap_block_t *block)
Initializes block from pdu.
Definition: block.c:45
coap_queue_t * sendqueue
Definition: net.h:90
#define COAP_OPTION_IF_MATCH
Definition: pdu.h:57
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:700
int coap_handle_message(coap_context_t *ctx, coap_packet_t *packet)
Parses and interprets a CoAP message with context ctx.
Definition: net.c:846
void coap_check_notify(coap_context_t *context)
Checks for all known resources, if they are dirty and notifies subscribed observers.
Definition: resource.c:689
unsigned char token[]
Definition: pdu.h:192
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:73
unsigned char coap_key_t[4]
Definition: hashkey.h:20
int coap_option_filter_set(coap_opt_filter_t filter, unsigned short type)
Sets the corresponding entry for type in filter.
Definition: option.c:509
ssize_t coap_network_send(struct coap_context_t *context UNUSED_PARAM, const coap_endpoint_t *local_interface, const coap_address_t *dst, unsigned char *data, size_t datalen)
Definition: coap_io.c:249
int 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:593
#define warn(...)
Definition: debug.h:65
#define COAP_RESPONSE_CODE(N)
Definition: pdu.h:96
#define COAP_DEFAULT_URI_WELLKNOWN
well-known resources URI
Definition: pdu.h:34
#define COAP_OPTION_PROXY_URI
Definition: pdu.h:70
#define COAP_OPTION_CONTENT_FORMAT
Definition: pdu.h:64
coap_address_t addr
local interface address
Definition: coap_io.h:50
struct sockaddr_in6 sin6
Definition: address.h:65
static coap_queue_t * coap_malloc_node(void)
Definition: net.c:121
struct sockaddr_in sin
Definition: address.h:64
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
void coap_packet_get_memmapped(coap_packet_t *packet, unsigned char **address, size_t *length)
Given a packet, set msg and msg_len to an address and length of the packet&#39;s data in memory...
Definition: coap_io.c:407
coap_endpoint_t * endpoint
the endpoint used for listening
Definition: net.h:91
void coap_show_pdu(const coap_pdu_t *pdu)
Definition: debug.c:375
#define COAP_OPTION_NORESPONSE
Definition: pdu.h:86
unsigned char * coap_opt_value(coap_opt_t *opt)
Returns a pointer to the value of the given option.
Definition: option.c:286
multi-purpose address abstraction
Definition: address.h:59
#define COAP_PRINT_OUTPUT_LENGTH(v)
Definition: resource.h:220
State management for asynchronous messages.
#define SHR_FP(val, frac)
unsigned short id
Definition: pdu.h:191
int coap_tid_t
coap_tid_t is used to store CoAP transaction id, i.e.
Definition: pdu.h:163
#define COAP_REQUEST_GET
Definition: pdu.h:50
void coap_free_endpoint(coap_endpoint_t *ep)
Definition: coap_io.c:208
unsigned short length
PDU length (including header, options, data)
Definition: pdu.h:234
#define COAP_OPT_ALL
Pre-defined filter that includes all options.
Definition: option.h:138
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:153
#define COAP_MESSAGE_NON
Definition: pdu.h:44
coap_endpoint_t local_if
the local address interface
Definition: net.h:37
#define COAP_OPTION_OBSERVE
Definition: pdu.h:76
void coap_clock_init(void)
Initializes the internal clock.
Definition: coap_time.c:27
coap_endpoint_t * coap_new_endpoint(const coap_address_t *addr, int flags)
Definition: coap_io.c:138
#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:341
coap_address_t remote
remote address
Definition: net.h:38
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:1144
size_t length
Definition: str.h:16
#define prng_init(Value)
Called to set the PRNG seed.
Definition: prng.h:101
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
Definition: option.c:159
#define COAP_OPTION_CONTENT_TYPE
Definition: pdu.h:65
Helpers for handling options in CoAP PDUs.
coap_hdr_t * hdr
Address of the first byte of the CoAP message.
Definition: pdu.h:229
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
#define WANT_WKC(Pdu, Key)
Definition: net.c:1368
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_queue_t * coap_peek_next(coap_context_t *context)
Returns the next pdu to send without removing from sendqeue.
Definition: net.c:290
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:496
#define COAP_OPTION_PROXY_SCHEME
Definition: pdu.h:71
Abstraction of virtual endpoint that can be attached to coap_context_t.
Definition: coap_io.h:35
#define COAP_RESOURCE_CHECK_TIME
The interval in seconds to check if resources have changed.
Definition: resource.h:22
#define COAP_SET_STR(st, l, v)
Definition: str.h:20
static int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
Definition: address.h:156
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:383
#define SZX_TO_BYTES(SZX)
Definition: net.c:1141
coap_queue_t * coap_find_transaction(coap_queue_t *queue, coap_tid_t id)
Retrieves transaction from the queue.
Definition: net.c:1021
coap_tid_t id
unique transaction id
Definition: net.h:39
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:1126
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 * 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:1029
#define debug(...)
Definition: debug.h:66
int fd
on POSIX systems
Definition: coap_io.h:38
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_DEFAULT_VERSION
Definition: pdu.h:30
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition: coap_time.h:99
#define FRAC_BITS
The number of bits for the fractional part of ACK_TIMEOUT and ACK_RANDOM_FACTOR.
Definition: net.c:94
#define ACK_TIMEOUT
creates a Qx.FRAC_BITS from COAP_DEFAULT_ACK_TIMEOUT
Definition: net.c:114
#define COAP_INVALID_TID
Indicates an invalid transaction id.
Definition: pdu.h:166
long coap_tick_diff_t
This data type is used to represent the difference between two clock_tick_t values.
Definition: coap_time.h:96
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:981
void coap_packet_populate_endpoint(coap_packet_t *packet, coap_endpoint_t *target)
Populate the coap_endpoint_t *target from the incoming packet&#39;s destination data. ...
Definition: coap_io.c:394
#define RESOURCES_ITER(r, tmp)
Definition: resource.h:387
unsigned int code
Definition: pdu.h:189
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:534
Header structure for CoAP PDUs.
Definition: pdu.h:227
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
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:1675
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&#39;s option list...
Definition: option.c:120
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:614
void coap_free_packet(coap_packet_t *packet)
Releases the storage allocated for packet.
Definition: coap_io.c:372
#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:122
#define COAP_OPTION_URI_PORT
Definition: pdu.h:61
unsigned int observable
can be observed
Definition: resource.h:79
ssize_t(* network_send)(struct coap_context_t *context, const coap_endpoint_t *local_interface, const coap_address_t *dst, unsigned char *data, size_t datalen)
Definition: net.h:124
static unsigned int calc_timeout(unsigned char r)
Calculates the initial timeout based on the global CoAP transmission parameters ACK_TIMEOUT, ACK_RANDOM_FACTOR, and COAP_TICKS_PER_SECOND.
Definition: net.c:674
#define assert(...)
Definition: mem.c:17
unsigned int coap_encode_var_bytes(unsigned char *buf, unsigned int val)
Encodes multiple-length byte sequences.
Definition: encode.c:34
size_t max_size
allocated storage for options and data
Definition: pdu.h:228
ssize_t coap_network_read(coap_endpoint_t *ep, coap_packet_t **packet)
Function interface for reading data.
Definition: coap_io.c:425
struct coap_queue_t * next
Definition: net.h:32
void coap_ticks(coap_tick_t *t)
Sets t to the internal time with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.c:49
#define COAP_RESPONSE_CLASS(C)
Definition: pdu.h:99
#define COAP_OPTION_IF_NONE_MATCH
Definition: pdu.h:60
#define COAP_OBSERVE_CANCEL
The value COAP_OBSERVE_CANCEL in a GET request indicates that the observe relationship for (sender ad...
Definition: subscribe.h:33
Iterator to run through PDU options.
Definition: option.h:253
#define COAP_MESSAGE_CON
Definition: pdu.h:43
#define COAP_MEDIATYPE_APPLICATION_LINK_FORMAT
Definition: pdu.h:146
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:171
unsigned int timeout
the randomized timeout value
Definition: net.h:36
static void handle_request(coap_context_t *context, coap_queue_t *node)
Definition: net.c:1372
#define coap_mcast_interface(Local)
Definition: coap_io.h:96
Generic resource handling.
#define COAP_MAX_PDU_SIZE
Definition: pdu.h:27
#define info(...)
Definition: debug.h:64
#define COAP_MESSAGE_ACK
Definition: pdu.h:45
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:234
coap_subscription_t * coap_add_observer(coap_resource_t *resource, const coap_endpoint_t *local_interface, const coap_address_t *observer, const str *token)
Adds the specified peer as observer for resource.
Definition: resource.c:542
#define coap_hash(String, Length, Result)
Definition: hashkey.h:34
Definition: debug.h:29
unsigned short type
decoded option type
Definition: option.h:255
uint16_t coap_opt_filter_t[COAP_OPT_FILTER_SIZE]
Fixed-size vector we use for option filtering.
Definition: option.h:135
int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
Compares given address objects a and b.
Definition: address.c:17
#define COAP_OPTION_BLOCK2
Definition: pdu.h:81
Subscriber information.
Definition: subscribe.h:54
void coap_delete_pdu(coap_pdu_t *pdu)
Definition: pdu.c:136
static void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
Definition: address.h:118
void coap_delete_all(coap_queue_t *queue)
Removes all items from given queue and frees the allocated storage.
Definition: net.c:265
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:75
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:644
Structure of Block options.
Definition: block.h:33
static int handle_locally(coap_context_t *context, coap_queue_t *node)
Definition: net.c:1561
static void coap_option_filter_clear(coap_opt_filter_t f)
Clears filter f.
Definition: option.h:146
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:483
Definition: str.h:15
#define COAP_PAYLOAD_START
Definition: pdu.h:207
void coap_free_context(coap_context_t *context)
CoAP stack context must be released with coap_free_context().
Definition: net.c:419
void coap_packet_copy_source(coap_packet_t *packet, coap_address_t *target)
Given an incoming packet, copy its source address into an address struct.
Definition: coap_io.c:402
void coap_dispatch(coap_context_t *context, coap_queue_t *rcvd)
Dispatches the PDUs from the receive queue in given context.
Definition: net.c:1568
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:444
#define COAP_OPT_LENGTH(opt)
Definition: option.h:392
unsigned int token_length
Definition: pdu.h:186
void coap_delete_all_resources(coap_context_t *context)
Deletes all resources from given context and frees their storage.
Definition: resource.c:449
#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:580
static enum respond_t no_response(coap_pdu_t *request, coap_pdu_t *response)
Checks for No-Response option in given request and returns 1 if response should be suppressed accordi...
Definition: net.c:1335
#define COAP_ERROR_PHRASE_LENGTH
maximum length of error phrase
Definition: pdu.h:114
int sockfd
send/receive socket
Definition: net.h:94
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
#define ACK_RANDOM_FACTOR
creates a Qx.FRAC_BITS from COAP_DEFAULT_ACK_RANDOM_FACTOR
Definition: net.c:110
static coap_tid_t coap_send_impl(coap_context_t *context, const coap_endpoint_t *local_interface, const coap_address_t *dst, coap_pdu_t *pdu)
Definition: net.c:554
#define COAP_PRINT_STATUS_ERROR
Definition: resource.h:221
time_t clock_offset
Definition: net.c:118
#define COAP_OPTION_URI_QUERY
Definition: pdu.h:67
int coap_option_filter_get(const coap_opt_filter_t filter, unsigned short type)
Checks if type is contained in filter.
Definition: option.c:519
unsigned int type
Definition: pdu.h:187
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:25
#define COAP_MESSAGE_IS_REQUEST(MSG)
Definition: pdu.h:197
union coap_address_t::@0 addr
#define COAP_OPTION_ACCEPT
Definition: pdu.h:68
coap_print_status_t coap_print_wellknown(coap_context_t *context, unsigned char *buf, size_t *buflen, size_t offset, coap_opt_t *query_filter)
Prints the names of all known resources to buf.
Definition: resource.c:167
coap_opt_t * coap_check_option(coap_pdu_t *pdu, unsigned short type, coap_opt_iterator_t *oi)
Retrieves the first option of type type from pdu.
Definition: option.c:209
void coap_memory_init(void)
Initializes libcoap&#39;s memory management.
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
Definition: mem.h:34
static int token_match(const unsigned char *a, size_t alen, const unsigned char *b, size_t blen)
Definition: net.c:975
#define coap_log(...)
Definition: debug.h:58
char * coap_response_phrase(unsigned char code)
Returns a human-readable response phrase for the specified CoAP response code.
Definition: pdu.c:306
int coap_read(coap_context_t *ctx)
Reads data from the network and tries to parse as CoAP PDU.
Definition: net.c:823
unsigned char * s
Definition: str.h:17
#define prng(Buf, Length)
Fills Buf with Length bytes of random data.
Definition: prng.h:91
static int coap_cancel(coap_context_t *context, const coap_queue_t *sent)
This function cancels outstanding messages for the remote peer and token specified in sent...
Definition: net.c:1278
union coap_endpoint_t::@1 handle
opaque handle to identify this endpoint
int is_wkc(coap_key_t k)
Definition: net.c:321
#define MAX_BITS
The maximum number of bits for fixed point integers that are used for retransmission time calculation...
Definition: net.c:100
static int coap_option_clrb(coap_opt_filter_t filter, unsigned short type)
Clears the corresponding bit for type in filter.
Definition: option.h:216
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:622
coap_log_t coap_get_log_level(void)
Returns the current log level.
Definition: debug.c:60
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:931
coap_tid_t coap_retransmit(coap_context_t *context, coap_queue_t *node)
Handles retransmissions of confirmable messages.
Definition: net.c:772
void(* coap_method_handler_t)(coap_context_t *, struct coap_resource_t *, const coap_endpoint_t *, coap_address_t *, coap_pdu_t *, str *, coap_pdu_t *)
Definition of message handler function (.
Definition: resource.h:42
void coap_handle_failed_notify(coap_context_t *context, const coap_address_t *peer, const str *token)
Definition: resource.c:748
unsigned int coap_decode_var_bytes(unsigned char *buf, unsigned int len)
Decodes multiple-length byte sequences.
Definition: encode.c:25
#define COAP_OPTION_URI_HOST
Definition: pdu.h:58
#define COAP_ENDPOINT_NOSEC
Definition: coap_io.h:55
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
unsigned short coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
Definition: option.c:251
unsigned char * data
payload
Definition: pdu.h:235
coap_queue_t * coap_new_node(void)
Creates a new node suitable for adding to the CoAP sendqueue.
Definition: net.c:274
respond_t
Internal flags to control the treatment of responses (specifically in presence of the No-Response opt...
Definition: net.c:1304
#define FP1
#define COAP_OPT_VALUE(opt)
Definition: option.h:406
The CoAP stack&#39;s global state is stored in a coap_context_t object.
Definition: net.h:76
struct sockaddr sa
Definition: address.h:62
#define COAP_DEFAULT_MAX_RETRANSMIT
Number of message retransmissions before message sending is stopped.
Definition: net.c:77
#define COAP_MESSAGE_IS_RESPONSE(MSG)
Definition: pdu.h:199
int coap_delete_node(coap_queue_t *node)
Destroys specified node.
Definition: net.c:254
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:469
struct coap_resource_t * resources
hash table or list of known resources
Definition: net.h:78
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_method_handler_t handler[4]
Used to store handlers for the four coap methods GET, POST, PUT, and DELETE.
Definition: resource.h:88
static void handle_response(coap_context_t *context, coap_queue_t *sent, coap_queue_t *rcvd)
Definition: net.c:1535
unsigned int m
1 if more blocks follow, 0 otherwise
Definition: block.h:35
unsigned int szx
block size
Definition: block.h:36
static void coap_free_node(coap_queue_t *node)
Definition: net.c:126
unsigned int num
block number
Definition: block.h:34
coap_opt_filter_t known_options
Definition: net.h:77
#define COAP_DROPPED_RESPONSE
Indicates that a response is suppressed.
Definition: pdu.h:172