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  coap_context_t *c;
336 
337  if (!listen_addr) {
338  coap_log(LOG_EMERG, "no listen address specified\n");
339  return NULL;
340  }
341 #ifdef WITH_CONTIKI
342  if (initialized)
343  return NULL;
344 #endif /* WITH_CONTIKI */
345 #ifndef WITH_CONTIKI
347 #endif /* not WITH_CONTIKI */
348 
349  coap_clock_init();
350 #ifdef WITH_LWIP
351  prng_init(LWIP_RAND());
352 #endif /* WITH_LWIP */
353 #ifdef WITH_CONTIKI
354  prng_init((ptrdiff_t)listen_addr ^ clock_offset);
355 #endif /* WITH_LWIP */
356 #ifdef WITH_POSIX
357  prng_init((unsigned long)listen_addr ^ clock_offset);
358 #endif /* WITH_POSIX */
359 
360 #ifndef WITH_CONTIKI
361  if (!c) {
362 #ifndef NDEBUG
363  coap_log(LOG_EMERG, "coap_init: malloc:\n");
364 #endif
365  return NULL;
366  }
367 #endif /* not WITH_CONTIKI */
368 #ifdef WITH_CONTIKI
369  coap_resources_init();
371 
372  c = &the_coap_context;
373  initialized = 1;
374 #endif /* WITH_CONTIKI */
375 
376  memset(c, 0, sizeof( coap_context_t ) );
377 
378  /* initialize message id */
379  prng((unsigned char *)&c->message_id, sizeof(unsigned short));
380 
382  if (c->endpoint == NULL) {
383  goto onerror;
384  }
385 #ifdef WITH_LWIP
386  c->endpoint->context = c;
387 #endif
388 
389 #ifdef WITH_POSIX
390  c->sockfd = c->endpoint->handle.fd;
391 #endif /* WITH_POSIX */
392 
393 #if defined(WITH_POSIX) || defined(WITH_CONTIKI)
396 #endif /* WITH_POSIX or WITH_CONTIKI */
397 
398 #ifdef WITH_CONTIKI
399  process_start(&coap_retransmit_process, (char *)c);
400 
401  PROCESS_CONTEXT_BEGIN(&coap_retransmit_process);
402 #ifndef WITHOUT_OBSERVE
403  etimer_set(&c->notify_timer, COAP_RESOURCE_CHECK_TIME * COAP_TICKS_PER_SECOND);
404 #endif /* WITHOUT_OBSERVE */
405  /* the retransmit timer must be initialized to some large value */
406  etimer_set(&the_coap_context.retransmit_timer, 0xFFFF);
407  PROCESS_CONTEXT_END(&coap_retransmit_process);
408 #endif /* WITH_CONTIKI */
409 
410  return c;
411 
412  onerror:
414  return NULL;
415 }
416 
417 void
419 
420  if (!context)
421  return;
422 
423  coap_delete_all(context->sendqueue);
424 
425 #ifdef WITH_LWIP
426  context->sendqueue = NULL;
427  coap_retransmittimer_restart(context);
428 #endif
429 
430  coap_delete_all_resources(context);
431 
432  coap_free_endpoint(context->endpoint);
433 #ifndef WITH_CONTIKI
434  coap_free_type(COAP_CONTEXT, context);
435 #endif/* not WITH_CONTIKI */
436 #ifdef WITH_CONTIKI
437  memset(&the_coap_context, 0, sizeof(coap_context_t));
438  initialized = 0;
439 #endif /* WITH_CONTIKI */
440 }
441 
442 int
444  coap_pdu_t *pdu,
445  coap_opt_filter_t unknown) {
446 
447  coap_opt_iterator_t opt_iter;
448  int ok = 1;
449 
450  coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL);
451 
452  while (coap_option_next(&opt_iter)) {
453 
454  /* The following condition makes use of the fact that
455  * coap_option_getb() returns -1 if type exceeds the bit-vector
456  * filter. As the vector is supposed to be large enough to hold
457  * the largest known option, we know that everything beyond is
458  * bad.
459  */
460  if (opt_iter.type & 0x01) {
461  /* first check the built-in critical options */
462  switch (opt_iter.type) {
469  case COAP_OPTION_ACCEPT:
472  case COAP_OPTION_BLOCK2:
473  case COAP_OPTION_BLOCK1:
474  break;
475  default:
476  if (coap_option_filter_get(ctx->known_options, opt_iter.type) <= 0) {
477  debug("unknown critical option %d\n", opt_iter.type);
478  ok = 0;
479 
480  /* When opt_iter.type is beyond our known option range,
481  * coap_option_filter_set() will return -1 and we are safe to leave
482  * this loop. */
483  if (coap_option_filter_set(unknown, opt_iter.type) == -1) {
484  break;
485  }
486  }
487  }
488  }
489  }
490 
491  return ok;
492 }
493 
494 void
496  coap_tid_t *id) {
497  coap_key_t h;
498 
499  memset(h, 0, sizeof(coap_key_t));
500 
501  /* Compare the transport address. */
502 
503 #ifdef WITH_POSIX
504  switch (peer->addr.sa.sa_family) {
505  case AF_INET:
506  coap_hash((const unsigned char *)&peer->addr.sin.sin_port,
507  sizeof(peer->addr.sin.sin_port), h);
508  coap_hash((const unsigned char *)&peer->addr.sin.sin_addr,
509  sizeof(peer->addr.sin.sin_addr), h);
510  break;
511  case AF_INET6:
512  coap_hash((const unsigned char *)&peer->addr.sin6.sin6_port,
513  sizeof(peer->addr.sin6.sin6_port), h);
514  coap_hash((const unsigned char *)&peer->addr.sin6.sin6_addr,
515  sizeof(peer->addr.sin6.sin6_addr), h);
516  break;
517  default:
518  return;
519  }
520 #endif
521 #if defined(WITH_LWIP) || defined(WITH_CONTIKI)
522  /* FIXME: with lwip, we can do better */
523  coap_hash((const unsigned char *)&peer->port, sizeof(peer->port), h);
524  coap_hash((const unsigned char *)&peer->addr, sizeof(peer->addr), h);
525 #endif /* WITH_LWIP || WITH_CONTIKI */
526 
527  coap_hash((const unsigned char *)&pdu->hdr->id, sizeof(unsigned short), h);
528 
529  *id = (((h[0] << 8) | h[1]) ^ ((h[2] << 8) | h[3])) & INT_MAX;
530 }
531 
534  const coap_endpoint_t *local_interface,
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, local_interface, dst, response);
545  coap_delete_pdu(response);
546  }
547  }
548  return result;
549 }
550 
551 #if defined(WITH_POSIX) || defined(WITH_CONTIKI)
552 static coap_tid_t
554  const coap_endpoint_t *local_interface,
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  /* Do not send error responses for requests that were received via
564  * IP multicast.
565  * FIXME: If No-Response option indicates interest, these responses
566  * must not be dropped. */
567  if (coap_is_mcast(&local_interface->addr) &&
568  COAP_RESPONSE_CLASS(pdu->hdr->code) > 2) {
569  return COAP_DROPPED_RESPONSE;
570  }
571 
572  bytes_written = context->network_send(context, local_interface, dst,
573  (unsigned char *)pdu->hdr, pdu->length);
574 
575  if (bytes_written >= 0) {
576  coap_transaction_id(dst, pdu, &id);
577  } else {
578  coap_log(LOG_CRIT, "coap_send_impl: %s\n", strerror(errno));
579  }
580 
581  return id;
582 }
583 #endif /* WITH_POSIX */
584 #ifdef WITH_LWIP
587  const coap_endpoint_t *local_interface,
588  const coap_address_t *dst,
589  coap_pdu_t *pdu) {
591 
592  if ( !context || !dst || !pdu )
593  {
594  return id;
595  }
596 
597  /* FIXME: we can't check this here with the existing infrastructure, but we
598  * should actually check that the pdu is not held by anyone but us. the
599  * respective pbuf is already exclusively owned by the pdu. */
600 
601  pbuf_realloc(pdu->pbuf, pdu->length);
602 
603  coap_transaction_id(dst, pdu, &id);
604 
605  udp_sendto(context->endpoint->pcb, pdu->pbuf,
606  &dst->addr, dst->port);
607 
608  return id;
609 }
610 #endif /* WITH_LWIP */
611 
612 coap_tid_t
614  const coap_endpoint_t *local_interface,
615  const coap_address_t *dst,
616  coap_pdu_t *pdu) {
617  return coap_send_impl(context, local_interface, dst, pdu);
618 }
619 
622  coap_pdu_t *request,
623  const coap_endpoint_t *local_interface,
624  const coap_address_t *dst,
625  unsigned char code,
626  coap_opt_filter_t opts) {
627  coap_pdu_t *response;
628  coap_tid_t result = COAP_INVALID_TID;
629 
630  assert(request);
631  assert(dst);
632 
633  response = coap_new_error_response(request, code, opts);
634  if (response) {
635  result = coap_send(context, local_interface, dst, response);
636  coap_delete_pdu(response);
637  }
638 
639  return result;
640 }
641 
644  const coap_endpoint_t *local_interface,
645  const coap_address_t *dst,
646  coap_pdu_t *request,
647  unsigned char type) {
648  coap_pdu_t *response;
649  coap_tid_t result = COAP_INVALID_TID;
650 
651  if (request) {
652  response = coap_pdu_init(type, 0, request->hdr->id, sizeof(coap_pdu_t));
653  if (response) {
654  result = coap_send(context, local_interface, dst, response);
655  coap_delete_pdu(response);
656  }
657  }
658  return result;
659 }
660 
672 static inline unsigned int
673 calc_timeout(unsigned char r) {
674  unsigned int result;
675 
676  /* The integer 1.0 as a Qx.FRAC_BITS */
677 #define FP1 Q(FRAC_BITS, 1)
678 
679  /* rounds val up and right shifts by frac positions */
680 #define SHR_FP(val,frac) (((val) + (1 << ((frac) - 1))) >> (frac))
681 
682  /* Inner term: multiply ACK_RANDOM_FACTOR by Q0.MAX_BITS[r] and
683  * make the result a rounded Qx.FRAC_BITS */
684  result = SHR_FP((ACK_RANDOM_FACTOR - FP1) * r, MAX_BITS);
685 
686  /* Add 1 to the inner term and multiply with ACK_TIMEOUT, then
687  * make the result a rounded Qx.FRAC_BITS */
688  result = SHR_FP(((result + FP1) * ACK_TIMEOUT), FRAC_BITS);
689 
690  /* Multiply with COAP_TICKS_PER_SECOND to yield system ticks
691  * (yields a Qx.FRAC_BITS) and shift to get an integer */
692  return SHR_FP((COAP_TICKS_PER_SECOND * result), FRAC_BITS);
693 
694 #undef FP1
695 #undef SHR_FP
696 }
697 
700  const coap_endpoint_t *local_interface,
701  const coap_address_t *dst,
702  coap_pdu_t *pdu) {
703  coap_queue_t *node;
704  coap_tick_t now;
705  unsigned char r;
706 
707  node = coap_new_node();
708  if (!node) {
709  debug("coap_send_confirmed: insufficient memory\n");
710  return COAP_INVALID_TID;
711  }
712 
713  node->id = coap_send_impl(context, local_interface, dst, pdu);
714  if (COAP_INVALID_TID == node->id) {
715  debug("coap_send_confirmed: error sending pdu\n");
716  coap_free_node(node);
717  return COAP_INVALID_TID;
718  }
719 
720  prng((unsigned char *)&r,sizeof(r));
721 
722  /* add timeout in range [ACK_TIMEOUT...ACK_TIMEOUT * ACK_RANDOM_FACTOR] */
723  node->timeout = calc_timeout(r);
724 
725  node->local_if = *local_interface;
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->local_if,
789  &node->remote, node->pdu);
790  return node->id;
791  }
792 
793  /* no more retransmissions, remove node from system */
794 
795 #ifndef WITH_CONTIKI
796  debug("** removed transaction %d\n", ntohs(node->id));
797 #endif
798 
799 #ifndef WITHOUT_OBSERVE
800  /* Check if subscriptions exist that should be canceled after
801  COAP_MAX_NOTIFY_FAILURES */
802  if (node->pdu->hdr->code >= 64) {
803  str token = { 0, NULL };
804 
805  token.length = node->pdu->hdr->token_length;
806  token.s = node->pdu->hdr->token;
807 
808  coap_handle_failed_notify(context, &node->remote, &token);
809  }
810 #endif /* WITHOUT_OBSERVE */
811 
812  /* And finally delete the node */
813  coap_delete_node( node );
814  return COAP_INVALID_TID;
815 }
816 
817 void coap_dispatch(coap_context_t *context, coap_queue_t *rcvd);
818 
819 #ifndef WITH_LWIP
820 /* WITH_LWIP, this is handled by coap_recv in a different way */
821 int
823  ssize_t bytes_read = -1;
824  coap_packet_t *packet;
825  coap_address_t src;
826  int result = -1; /* the value to be returned */
827 
828  coap_address_init(&src);
829 
830  bytes_read = ctx->network_read(ctx->endpoint, &packet);
831 
832  if ( bytes_read < 0 ) {
833  warn("coap_read: recvfrom");
834  } else {
835  result = coap_handle_message(ctx, packet);
836  }
837 
838  coap_free_packet(packet);
839 
840  return result;
841 }
842 #endif /* not WITH_LWIP */
843 
844 int
846  coap_packet_t *packet) {
847  /* const coap_address_t *remote, */
848  /* unsigned char *msg, size_t msg_len) { */
849  unsigned char *msg;
850  size_t msg_len;
851  coap_queue_t *node;
852 
853  /* the negated result code */
854  enum result_t { RESULT_OK, RESULT_ERR_EARLY, RESULT_ERR };
855  int result = RESULT_ERR_EARLY;
856 
857  coap_packet_get_memmapped(packet, &msg, &msg_len);
858 
859  if (msg_len < sizeof(coap_hdr_t)) {
860  debug("coap_handle_message: discarded invalid frame\n" );
861  goto error_early;
862  }
863 
864  /* check version identifier */
865  if (((*msg >> 6) & 0x03) != COAP_DEFAULT_VERSION) {
866  debug("coap_handle_message: unknown protocol version %d\n", (*msg >> 6) & 0x03);
867  goto error_early;
868  }
869 
870  node = coap_new_node();
871  if (!node) {
872  goto error_early;
873  }
874 
875  /* from this point, the result code indicates that */
876  result = RESULT_ERR;
877 
878 #ifdef WITH_LWIP
879  node->pdu = coap_pdu_from_pbuf(coap_packet_extract_pbuf(packet));
880 #else
881  node->pdu = coap_pdu_init(0, 0, 0, msg_len);
882 #endif
883  if (!node->pdu) {
884  goto error;
885  }
886 
887  if (!coap_pdu_parse(msg, msg_len, node->pdu)) {
888  warn("discard malformed PDU\n");
889  goto error;
890  }
891 
892  coap_ticks(&node->t);
893 
894  coap_packet_populate_endpoint(packet, &node->local_if);
895  coap_packet_copy_source(packet, &node->remote);
896 
897  /* and add new node to receive queue */
898  coap_transaction_id(&node->remote, node->pdu, &node->id);
899 
900 #ifndef NDEBUG
901  if (LOG_DEBUG <= coap_get_log_level()) {
902 #ifndef INET6_ADDRSTRLEN
903 #define INET6_ADDRSTRLEN 40
904 #endif
905 
913  coap_show_pdu(node->pdu);
914  }
915 #endif
916 
917  coap_dispatch(ctx, node);
918  return -RESULT_OK;
919 
920  error:
921  /* FIXME: send back RST? */
922  coap_delete_node(node);
923  return -result;
924 
925  error_early:
926  return -result;
927 }
928 
929 int
931  coap_queue_t *p, *q;
932 
933  if ( !queue || !*queue)
934  return 0;
935 
936  /* replace queue head if PDU's time is less than head's time */
937 
938  if ( id == (*queue)->id ) { /* found transaction */
939  *node = *queue;
940  *queue = (*queue)->next;
941  if (*queue) { /* adjust relative time of new queue head */
942  (*queue)->t += (*node)->t;
943  }
944  (*node)->next = NULL;
945  /* coap_delete_node( q ); */
946  debug("*** removed transaction %u\n", id);
947  return 1;
948  }
949 
950  /* search transaction to remove (only first occurence will be removed) */
951  q = *queue;
952  do {
953  p = q;
954  q = q->next;
955  } while ( q && id != q->id );
956 
957  if ( q ) { /* found transaction */
958  p->next = q->next;
959  if (p->next) { /* must update relative time of p->next */
960  p->next->t += q->t;
961  }
962  q->next = NULL;
963  *node = q;
964  /* coap_delete_node( q ); */
965  debug("*** removed transaction %u\n", id);
966  return 1;
967  }
968 
969  return 0;
970 
971 }
972 
973 static inline int
974 token_match(const unsigned char *a, size_t alen,
975  const unsigned char *b, size_t blen) {
976  return alen == blen && (alen == 0 || memcmp(a, b, alen) == 0);
977 }
978 
979 void
981  const unsigned char *token, size_t token_length) {
982  /* cancel all messages in sendqueue that are for dst
983  * and use the specified token */
984  coap_queue_t *p, *q;
985 
986  while (context->sendqueue &&
987  coap_address_equals(dst, &context->sendqueue->remote) &&
988  token_match(token, token_length,
989  context->sendqueue->pdu->hdr->token,
990  context->sendqueue->pdu->hdr->token_length)) {
991  q = context->sendqueue;
992  context->sendqueue = q->next;
993  debug("**** removed transaction %d\n", ntohs(q->pdu->hdr->id));
994  coap_delete_node(q);
995  }
996 
997  if (!context->sendqueue)
998  return;
999 
1000  p = context->sendqueue;
1001  q = p->next;
1002 
1003  /* when q is not NULL, it does not match (dst, token), so we can skip it */
1004  while (q) {
1005  if (coap_address_equals(dst, &q->remote) &&
1006  token_match(token, token_length,
1007  q->pdu->hdr->token, q->pdu->hdr->token_length)) {
1008  p->next = q->next;
1009  debug("**** removed transaction %d\n", ntohs(q->pdu->hdr->id));
1010  coap_delete_node(q);
1011  q = p->next;
1012  } else {
1013  p = q;
1014  q = q->next;
1015  }
1016  }
1017 }
1018 
1019 coap_queue_t *
1021  while (queue && queue->id != id)
1022  queue = queue->next;
1023 
1024  return queue;
1025 }
1026 
1027 coap_pdu_t *
1028 coap_new_error_response(coap_pdu_t *request, unsigned char code,
1029  coap_opt_filter_t opts) {
1030  coap_opt_iterator_t opt_iter;
1031  coap_pdu_t *response;
1032  size_t size = sizeof(coap_hdr_t) + request->hdr->token_length;
1033  int type;
1034  coap_opt_t *option;
1035  unsigned short opt_type = 0; /* used for calculating delta-storage */
1036 
1038  char *phrase = coap_response_phrase(code);
1039 
1040  /* Need some more space for the error phrase and payload start marker */
1041  if (phrase)
1042  size += strlen(phrase) + 1;
1043 #endif
1044 
1045  assert(request);
1046 
1047  /* cannot send ACK if original request was not confirmable */
1048  type = request->hdr->type == COAP_MESSAGE_CON
1050  : COAP_MESSAGE_NON;
1051 
1052  /* Estimate how much space we need for options to copy from
1053  * request. We always need the Token, for 4.02 the unknown critical
1054  * options must be included as well. */
1055  coap_option_clrb(opts, COAP_OPTION_CONTENT_TYPE); /* we do not want this */
1056 
1057  coap_option_iterator_init(request, &opt_iter, opts);
1058 
1059  /* Add size of each unknown critical option. As known critical
1060  options as well as elective options are not copied, the delta
1061  value might grow.
1062  */
1063  while((option = coap_option_next(&opt_iter))) {
1064  unsigned short delta = opt_iter.type - opt_type;
1065  /* calculate space required to encode (opt_iter.type - opt_type) */
1066  if (delta < 13) {
1067  size++;
1068  } else if (delta < 269) {
1069  size += 2;
1070  } else {
1071  size += 3;
1072  }
1073 
1074  /* add coap_opt_length(option) and the number of additional bytes
1075  * required to encode the option length */
1076 
1077  size += coap_opt_length(option);
1078  switch (*option & 0x0f) {
1079  case 0x0e:
1080  size++;
1081  /* fall through */
1082  case 0x0d:
1083  size++;
1084  break;
1085  default:
1086  ;
1087  }
1088 
1089  opt_type = opt_iter.type;
1090  }
1091 
1092  /* Now create the response and fill with options and payload data. */
1093  response = coap_pdu_init(type, code, request->hdr->id, size);
1094  if (response) {
1095  /* copy token */
1096  if (!coap_add_token(response, request->hdr->token_length,
1097  request->hdr->token)) {
1098  debug("cannot add token to error response\n");
1099  coap_delete_pdu(response);
1100  return NULL;
1101  }
1102 
1103  /* copy all options */
1104  coap_option_iterator_init(request, &opt_iter, opts);
1105  while((option = coap_option_next(&opt_iter)))
1106  coap_add_option(response, opt_iter.type,
1107  COAP_OPT_LENGTH(option),
1108  COAP_OPT_VALUE(option));
1109 
1110 #if COAP_ERROR_PHRASE_LENGTH > 0
1111  /* note that diagnostic messages do not need a Content-Format option. */
1112  if (phrase)
1113  coap_add_data(response, strlen(phrase), (unsigned char *)phrase);
1114 #endif
1115  }
1116 
1117  return response;
1118 }
1119 
1124 static inline size_t
1125 get_wkc_len(coap_context_t *context, coap_opt_t *query_filter) {
1126  unsigned char buf[1];
1127  size_t len = 0;
1128 
1129  if (coap_print_wellknown(context, buf, &len, UINT_MAX, query_filter)
1131  warn("cannot determine length of /.well-known/core\n");
1132  return 0;
1133  }
1134 
1135  debug("get_wkc_len: coap_print_wellknown() returned %zu\n", len);
1136 
1137  return len;
1138 }
1139 
1140 #define SZX_TO_BYTES(SZX) ((size_t)(1 << ((SZX) + 4)))
1141 
1142 coap_pdu_t *
1144  coap_pdu_t *resp;
1145  coap_opt_iterator_t opt_iter;
1146  size_t len, wkc_len;
1147  unsigned char buf[2];
1148  int result = 0;
1149  int need_block2 = 0; /* set to 1 if Block2 option is required */
1150  coap_block_t block;
1151  coap_opt_t *query_filter;
1152  size_t offset = 0;
1153 
1154  resp = coap_pdu_init(request->hdr->type == COAP_MESSAGE_CON
1155  ? COAP_MESSAGE_ACK
1156  : COAP_MESSAGE_NON,
1157  COAP_RESPONSE_CODE(205),
1158  request->hdr->id, COAP_MAX_PDU_SIZE);
1159  if (!resp) {
1160  debug("coap_wellknown_response: cannot create PDU\n");
1161  return NULL;
1162  }
1163 
1164  if (!coap_add_token(resp, request->hdr->token_length, request->hdr->token)) {
1165  debug("coap_wellknown_response: cannot add token\n");
1166  goto error;
1167  }
1168 
1169  query_filter = coap_check_option(request, COAP_OPTION_URI_QUERY, &opt_iter);
1170  wkc_len = get_wkc_len(context, query_filter);
1171 
1172  /* The value of some resources is undefined and get_wkc_len will return 0.*/
1173  if (wkc_len == 0){
1174  debug("coap_wellknown_response: undefined resource\n");
1175  /* set error code 4.00 Bad Request*/
1176  resp->hdr->code = COAP_RESPONSE_CODE(400);
1177  resp->length = sizeof(coap_hdr_t) + resp->hdr->token_length;
1178  return resp;
1179  }
1180 
1181  /* check whether the request contains the Block2 option */
1182  if (coap_get_block(request, COAP_OPTION_BLOCK2, &block)) {
1183  debug("create block\n");
1184  offset = block.num << (block.szx + 4);
1185  if (block.szx > 6) { /* invalid, MUST lead to 4.00 Bad Request */
1186  resp->hdr->code = COAP_RESPONSE_CODE(400);
1187  return resp;
1188  } else if (block.szx > COAP_MAX_BLOCK_SZX) {
1189  block.szx = COAP_MAX_BLOCK_SZX;
1190  block.num = offset >> (block.szx + 4);
1191  }
1192 
1193  need_block2 = 1;
1194  }
1195 
1196  /* Check if there is sufficient space to add Content-Format option
1197  * and data. We do this before adding the Content-Format option to
1198  * avoid sending error responses with that option but no actual
1199  * content. */
1200  if (resp->max_size <= (size_t)resp->length + 3) {
1201  debug("coap_wellknown_response: insufficient storage space\n");
1202  goto error;
1203  }
1204 
1205  /* Add Content-Format. As we have checked for available storage,
1206  * nothing should go wrong here. */
1210  coap_encode_var_bytes(buf,
1212 
1213  /* check if Block2 option is required even if not requested */
1214  if (!need_block2 && (resp->max_size - (size_t)resp->length < wkc_len)) {
1215  assert(resp->length <= resp->max_size);
1216  const size_t payloadlen = resp->max_size - resp->length;
1217  /* yes, need block-wise transfer */
1218  block.num = 0;
1219  block.m = 0; /* the M bit is set by coap_write_block_opt() */
1220  block.szx = COAP_MAX_BLOCK_SZX;
1221  while (payloadlen < SZX_TO_BYTES(block.szx)) {
1222  if (block.szx == 0) {
1223  debug("coap_wellknown_response: message to small even for szx == 0\n");
1224  goto error;
1225  } else {
1226  block.szx--;
1227  }
1228  }
1229 
1230  need_block2 = 1;
1231  }
1232 
1233  /* write Block2 option if necessary */
1234  if (need_block2) {
1235  if (coap_write_block_opt(&block, COAP_OPTION_BLOCK2, resp, wkc_len) < 0) {
1236  debug("coap_wellknown_response: cannot add Block2 option\n");
1237  goto error;
1238  }
1239  }
1240 
1241  /* Manually set payload of response to let print_wellknown() write,
1242  * into our buffer without copying data. */
1243 
1244  resp->data = (unsigned char *)resp->hdr + resp->length;
1245  *resp->data = COAP_PAYLOAD_START;
1246  resp->data++;
1247  resp->length++;
1248  len = need_block2 ? SZX_TO_BYTES(block.szx) : resp->max_size - resp->length;
1249 
1250  result = coap_print_wellknown(context, resp->data, &len, offset, query_filter);
1251  if ((result & COAP_PRINT_STATUS_ERROR) != 0) {
1252  debug("coap_print_wellknown failed\n");
1253  goto error;
1254  }
1255 
1256  resp->length += COAP_PRINT_OUTPUT_LENGTH(result);
1257  return resp;
1258 
1259  error:
1260  /* set error code 5.03 and remove all options and data from response */
1261  resp->hdr->code = COAP_RESPONSE_CODE(503);
1262  resp->length = sizeof(coap_hdr_t) + resp->hdr->token_length;
1263  return resp;
1264 }
1265 
1276 static int
1277 coap_cancel(coap_context_t *context, const coap_queue_t *sent) {
1278 #ifndef WITHOUT_OBSERVE
1279  str token = { 0, NULL };
1280  int num_cancelled = 0; /* the number of observers cancelled */
1281 
1282  /* remove observer for this resource, if any
1283  * get token from sent and try to find a matching resource. Uh!
1284  */
1285 
1286  COAP_SET_STR(&token, sent->pdu->hdr->token_length, sent->pdu->hdr->token);
1287 
1288  RESOURCES_ITER(context->resources, r) {
1289  num_cancelled += coap_delete_observer(r, &sent->remote, &token);
1290  coap_cancel_all_messages(context, &sent->remote, token.s, token.length);
1291  }
1292 
1293  return num_cancelled;
1294 #else /* WITOUT_OBSERVE */
1295  return 0;
1296 #endif /* WITOUT_OBSERVE */
1297 }
1298 
1304 
1333 static enum respond_t
1334 no_response(coap_pdu_t *request, coap_pdu_t *response) {
1335  coap_opt_t *nores;
1336  coap_opt_iterator_t opt_iter;
1337  uint8_t val = 0;
1338 
1339  assert(request);
1340  assert(response);
1341 
1342  if (COAP_RESPONSE_CLASS(response->hdr->code) > 0) {
1343  nores = coap_check_option(request, COAP_OPTION_NORESPONSE, &opt_iter);
1344 
1345  if (nores) {
1347 
1348  /* The response should be dropped when the bit corresponding to
1349  * the response class is set (cf. table in funtion
1350  * documentation). When a No-Response option is present and the
1351  * bit is not set, the sender explicitly indicates interest in
1352  * this response. */
1353  if (((1 << (COAP_RESPONSE_CLASS(response->hdr->code) - 1)) & val) > 0) {
1354  return RESPONSE_DROP;
1355  } else {
1356  return RESPONSE_SEND;
1357  }
1358  }
1359  }
1360 
1361  /* Default behavior applies when we are not dealing with a response
1362  * (class == 0) or the request did not contain a No-Response option.
1363  */
1364  return RESPONSE_DEFAULT;
1365 }
1366 
1367 #define WANT_WKC(Pdu,Key) \
1368  (((Pdu)->hdr->code == COAP_REQUEST_GET) && is_wkc(Key))
1369 
1370 static void
1372  coap_method_handler_t h = NULL;
1373  coap_pdu_t *response = NULL;
1375  coap_resource_t *resource;
1376  coap_key_t key;
1377  /* The respond field indicates whether a response must be treated
1378  * specially due to a No-Response option that declares disinterest
1379  * or interest in a specific response class. DEFAULT indicates that
1380  * No-Response has not been specified. */
1381  enum respond_t respond = RESPONSE_DEFAULT;
1382 
1383  coap_option_filter_clear(opt_filter);
1384 
1385  /* try to find the resource from the request URI */
1386  coap_hash_request_uri(node->pdu, key);
1387  resource = coap_get_resource_from_key(context, key);
1388 
1389  if (!resource) {
1390  /* The resource was not found. Check if the request URI happens to
1391  * be the well-known URI. In that case, we generate a default
1392  * response, otherwise, we return 4.04 */
1393 
1394  if (is_wkc(key)) { /* request for .well-known/core */
1395  if (node->pdu->hdr->code == COAP_REQUEST_GET) { /* GET */
1396  info("create default response for %s\n", COAP_DEFAULT_URI_WELLKNOWN);
1397  response = coap_wellknown_response(context, node->pdu);
1398  } else {
1399  debug("method not allowed for .well-known/core\n");
1400  response = coap_new_error_response(node->pdu, COAP_RESPONSE_CODE(405),
1401  opt_filter);
1402  }
1403  } else { /* request for any another resource, return 4.04 */
1404 
1405  debug("request for unknown resource 0x%02x%02x%02x%02x, return 4.04\n",
1406  key[0], key[1], key[2], key[3]);
1407  response =
1409  opt_filter);
1410  }
1411 
1412  if (response
1413  && (no_response(node->pdu, response) != RESPONSE_DROP)
1414  && (coap_send(context, &node->local_if,
1415  &node->remote, response) == COAP_INVALID_TID)) {
1416  warn("cannot send response for transaction %u\n", node->id);
1417  }
1418  coap_delete_pdu(response);
1419  response = NULL;
1420 
1421  return;
1422  }
1423 
1424  /* the resource was found, check if there is a registered handler */
1425  if ((size_t)node->pdu->hdr->code - 1 <
1426  sizeof(resource->handler)/sizeof(coap_method_handler_t))
1427  h = resource->handler[node->pdu->hdr->code - 1];
1428 
1429  if (h) {
1430  debug("call custom handler for resource 0x%02x%02x%02x%02x\n",
1431  key[0], key[1], key[2], key[3]);
1432  response = coap_pdu_init(node->pdu->hdr->type == COAP_MESSAGE_CON
1434  : COAP_MESSAGE_NON,
1435  0, node->pdu->hdr->id, COAP_MAX_PDU_SIZE);
1436 
1437  /* Implementation detail: coap_add_token() immediately returns 0
1438  if response == NULL */
1439  if (coap_add_token(response, node->pdu->hdr->token_length,
1440  node->pdu->hdr->token)) {
1441  str token = { node->pdu->hdr->token_length, node->pdu->hdr->token };
1442  coap_opt_iterator_t opt_iter;
1443  coap_opt_t *observe = NULL;
1444  int observe_action = COAP_OBSERVE_CANCEL;
1445 
1446  /* check for Observe option */
1447  if (resource->observable) {
1448  observe = coap_check_option(node->pdu, COAP_OPTION_OBSERVE, &opt_iter);
1449  if (observe) {
1450  observe_action =
1452  coap_opt_length(observe));
1453 
1454  if ((observe_action & COAP_OBSERVE_CANCEL) == 0) {
1455  coap_subscription_t *subscription;
1456 
1457  coap_log(LOG_DEBUG, "create new subscription\n");
1458  subscription = coap_add_observer(resource, &node->local_if,
1459  &node->remote, &token);
1460  if (subscription) {
1461  coap_touch_observer(context, &node->remote, &token);
1462  }
1463  } else {
1464  coap_log(LOG_DEBUG, "removed observer\n");
1465  coap_delete_observer(resource, &node->remote, &token);
1466  }
1467  }
1468  }
1469 
1470  h(context, resource, &node->local_if, &node->remote,
1471  node->pdu, &token, response);
1472 
1473  respond = no_response(node->pdu, response);
1474  if (respond != RESPONSE_DROP) {
1475  if (observe && (COAP_RESPONSE_CLASS(response->hdr->code) > 2)) {
1476  coap_log(LOG_DEBUG, "removed observer\n");
1477  coap_delete_observer(resource, &node->remote, &token);
1478  }
1479 
1480  /* If original request contained a token, and the registered
1481  * application handler made no changes to the response, then
1482  * this is an empty ACK with a token, which is a malformed
1483  * PDU */
1484  if ((response->hdr->type == COAP_MESSAGE_ACK)
1485  && (response->hdr->code == 0)) {
1486  /* Remove token from otherwise-empty acknowledgment PDU */
1487  response->hdr->token_length = 0;
1488  response->length = sizeof(coap_hdr_t);
1489  }
1490 
1491  if ((respond == RESPONSE_SEND)
1492  || /* RESPOND_DEFAULT */
1493  (response->hdr->type != COAP_MESSAGE_NON ||
1494  (response->hdr->code >= 64
1495  && !coap_mcast_interface(&node->local_if)))) {
1496 
1497  if (coap_send(context, &node->local_if,
1498  &node->remote, response) == COAP_INVALID_TID) {
1499  debug("cannot send response for message %d\n", node->pdu->hdr->id);
1500  }
1501  }
1502  }
1503  coap_delete_pdu(response);
1504  response = NULL;
1505  } else {
1506  warn("cannot generate response\r\n");
1507  }
1508  } else {
1509  if (WANT_WKC(node->pdu, key)) {
1510  debug("create default response for %s\n", COAP_DEFAULT_URI_WELLKNOWN);
1511  response = coap_wellknown_response(context, node->pdu);
1512  debug("have wellknown response %p\n", (void *)response);
1513  } else
1514  response = coap_new_error_response(node->pdu, COAP_RESPONSE_CODE(405),
1515  opt_filter);
1516 
1517  if (response && (no_response(node->pdu, response) != RESPONSE_DROP)) {
1518  if (coap_send(context, &node->local_if, &node->remote,
1519  response) == COAP_INVALID_TID) {
1520  debug("cannot send response for transaction %u\n", node->id);
1521  }
1522  }
1523  coap_delete_pdu(response);
1524  response = NULL;
1525  }
1526 
1527  assert(response == NULL);
1528 }
1529 
1530 static inline void
1532  coap_queue_t *sent, coap_queue_t *rcvd) {
1533 
1534  coap_send_ack(context, &rcvd->local_if, &rcvd->remote, rcvd->pdu);
1535 
1536  /* In a lossy context, the ACK of a separate response may have
1537  * been lost, so we need to stop retransmitting requests with the
1538  * same token.
1539  */
1540  coap_cancel_all_messages(context, &rcvd->remote,
1541  rcvd->pdu->hdr->token,
1542  rcvd->pdu->hdr->token_length);
1543 
1544  /* Call application-specific response handler when available. */
1545  if (context->response_handler) {
1546  context->response_handler(context, &rcvd->local_if,
1547  &rcvd->remote, sent ? sent->pdu : NULL,
1548  rcvd->pdu, rcvd->id);
1549  }
1550 }
1551 
1552 static inline int
1553 #ifdef __GNUC__
1554 handle_locally(coap_context_t *context __attribute__ ((unused)),
1555  coap_queue_t *node __attribute__ ((unused))) {
1556 #else /* not a GCC */
1558 #endif /* GCC */
1559  /* this function can be used to check if node->pdu is really for us */
1560  return 1;
1561 }
1562 
1563 void
1565  coap_queue_t *sent = NULL;
1566  coap_pdu_t *response;
1568 
1569  if (!context)
1570  return;
1571 
1572  memset(opt_filter, 0, sizeof(coap_opt_filter_t));
1573 
1574  {
1575  /* version has been checked in coap_handle_message() */
1576  /* if ( rcvd->pdu->hdr->version != COAP_DEFAULT_VERSION ) { */
1577  /* debug("dropped packet with unknown version %u\n", rcvd->pdu->hdr->version); */
1578  /* goto cleanup; */
1579  /* } */
1580 
1581  switch (rcvd->pdu->hdr->type) {
1582  case COAP_MESSAGE_ACK:
1583  /* find transaction in sendqueue to stop retransmission */
1584  coap_remove_from_queue(&context->sendqueue, rcvd->id, &sent);
1585 
1586  if (rcvd->pdu->hdr->code == 0)
1587  goto cleanup;
1588 
1589  /* if sent code was >= 64 the message might have been a
1590  * notification. Then, we must flag the observer to be alive
1591  * by setting obs->fail_cnt = 0. */
1592  if (sent && COAP_RESPONSE_CLASS(sent->pdu->hdr->code) == 2) {
1593  const str token =
1594  { sent->pdu->hdr->token_length, sent->pdu->hdr->token };
1595  coap_touch_observer(context, &sent->remote, &token);
1596  }
1597  break;
1598 
1599  case COAP_MESSAGE_RST :
1600  /* We have sent something the receiver disliked, so we remove
1601  * not only the transaction but also the subscriptions we might
1602  * have. */
1603 
1604 #ifndef WITH_CONTIKI
1605  coap_log(LOG_ALERT, "got RST for message %u\n", ntohs(rcvd->pdu->hdr->id));
1606 #else /* WITH_CONTIKI */
1607  coap_log(LOG_ALERT, "got RST for message %u\n", uip_ntohs(rcvd->pdu->hdr->id));
1608 #endif /* WITH_CONTIKI */
1609 
1610  /* find transaction in sendqueue to stop retransmission */
1611  coap_remove_from_queue(&context->sendqueue, rcvd->id, &sent);
1612 
1613  if (sent)
1614  coap_cancel(context, sent);
1615  goto cleanup;
1616 
1617  case COAP_MESSAGE_NON : /* check for unknown critical options */
1618  if (coap_option_check_critical(context, rcvd->pdu, opt_filter) == 0)
1619  goto cleanup;
1620  break;
1621 
1622  case COAP_MESSAGE_CON : /* check for unknown critical options */
1623  if (coap_option_check_critical(context, rcvd->pdu, opt_filter) == 0) {
1624 
1625  /* FIXME: send response only if we have received a request. Otherwise,
1626  * send RST. */
1627  response =
1628  coap_new_error_response(rcvd->pdu, COAP_RESPONSE_CODE(402), opt_filter);
1629 
1630  if (!response)
1631  warn("coap_dispatch: cannot create error response\n");
1632  else {
1633  if (coap_send(context, &rcvd->local_if, &rcvd->remote, response)
1634  == COAP_INVALID_TID) {
1635  warn("coap_dispatch: error sending response\n");
1636  }
1637  coap_delete_pdu(response);
1638  }
1639 
1640  goto cleanup;
1641  }
1642  default: break;
1643  }
1644 
1645  /* Pass message to upper layer if a specific handler was
1646  * registered for a request that should be handled locally. */
1647  if (handle_locally(context, rcvd)) {
1648  if (COAP_MESSAGE_IS_REQUEST(rcvd->pdu->hdr))
1649  handle_request(context, rcvd);
1650  else if (COAP_MESSAGE_IS_RESPONSE(rcvd->pdu->hdr))
1651  handle_response(context, sent, rcvd);
1652  else {
1653  debug("dropped message with invalid code (%d.%02d)\n",
1654  COAP_RESPONSE_CLASS(rcvd->pdu->hdr->code),
1655  rcvd->pdu->hdr->code & 0x1f);
1656 
1657  if (!coap_is_mcast(&rcvd->local_if.addr)) {
1658  coap_send_message_type(context, &rcvd->local_if, &rcvd->remote,
1659  rcvd->pdu, COAP_MESSAGE_RST);
1660  }
1661  }
1662  }
1663 
1664  cleanup:
1665  coap_delete_node(sent);
1666  coap_delete_node(rcvd);
1667  }
1668 }
1669 
1670 int
1672  return !context || (context->sendqueue == NULL);
1673 }
1674 
1675 #ifdef WITH_CONTIKI
1676 
1677 /*---------------------------------------------------------------------------*/
1678 /* CoAP message retransmission */
1679 /*---------------------------------------------------------------------------*/
1680 PROCESS_THREAD(coap_retransmit_process, ev, data)
1681 {
1682  coap_tick_t now;
1683  coap_queue_t *nextpdu;
1684 
1685  PROCESS_BEGIN();
1686 
1687  debug("Started retransmit process\r\n");
1688 
1689  while(1) {
1690  PROCESS_YIELD();
1691  if (ev == PROCESS_EVENT_TIMER) {
1692  if (etimer_expired(&the_coap_context.retransmit_timer)) {
1693 
1694  nextpdu = coap_peek_next(&the_coap_context);
1695 
1696  coap_ticks(&now);
1697  while (nextpdu && nextpdu->t <= now) {
1698  coap_retransmit(&the_coap_context, coap_pop_next(&the_coap_context));
1699  nextpdu = coap_peek_next(&the_coap_context);
1700  }
1701 
1702  /* need to set timer to some value even if no nextpdu is available */
1703  etimer_set(&the_coap_context.retransmit_timer,
1704  nextpdu ? nextpdu->t - now : 0xFFFF);
1705  }
1706 #ifndef WITHOUT_OBSERVE
1707  if (etimer_expired(&the_coap_context.notify_timer)) {
1708  coap_check_notify(&the_coap_context);
1709  etimer_reset(&the_coap_context.notify_timer);
1710  }
1711 #endif /* WITHOUT_OBSERVE */
1712  }
1713  }
1714 
1715  PROCESS_END();
1716 }
1717 /*---------------------------------------------------------------------------*/
1718 
1719 #endif /* WITH_CONTIKI */
1720 
1721 #ifdef WITH_LWIP
1722 /* FIXME: retransmits that are not required any more due to incoming packages
1723  * do *not* get cleared at the moment, the wakeup when the transmission is due
1724  * is silently accepted. this is mainly due to the fact that the required
1725  * checks are similar in two places in the code (when receiving ACK and RST)
1726  * and that they cause more than one patch chunk, as it must be first checked
1727  * whether the sendqueue item to be dropped is the next one pending, and later
1728  * the restart function has to be called. nothing insurmountable, but it can
1729  * also be implemented when things have stabilized, and the performance
1730  * penality is minimal
1731  *
1732  * also, this completely ignores COAP_RESOURCE_CHECK_TIME.
1733  * */
1734 
1735 static void coap_retransmittimer_execute(void *arg)
1736 {
1737  coap_context_t *ctx = (coap_context_t*)arg;
1738  coap_tick_t now;
1739  coap_tick_t elapsed;
1740  coap_queue_t *nextinqueue;
1741 
1742  ctx->timer_configured = 0;
1743 
1744  coap_ticks(&now);
1745 
1746  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 */
1747 
1748  nextinqueue = coap_peek_next(ctx);
1749  while (nextinqueue != NULL)
1750  {
1751  if (nextinqueue->t > elapsed) {
1752  nextinqueue->t -= elapsed;
1753  break;
1754  } else {
1755  elapsed -= nextinqueue->t;
1756  coap_retransmit(ctx, coap_pop_next(ctx));
1757  nextinqueue = coap_peek_next(ctx);
1758  }
1759  }
1760 
1761  ctx->sendqueue_basetime = now;
1762 
1763  coap_retransmittimer_restart(ctx);
1764 }
1765 
1766 static void coap_retransmittimer_restart(coap_context_t *ctx)
1767 {
1768  coap_tick_t now, elapsed, delay;
1769 
1770  if (ctx->timer_configured)
1771  {
1772  printf("clearing\n");
1773  sys_untimeout(coap_retransmittimer_execute, (void*)ctx);
1774  ctx->timer_configured = 0;
1775  }
1776  if (ctx->sendqueue != NULL)
1777  {
1778  coap_ticks(&now);
1779  elapsed = now - ctx->sendqueue_basetime;
1780  if (ctx->sendqueue->t >= elapsed) {
1781  delay = ctx->sendqueue->t - elapsed;
1782  } else {
1783  /* a strange situation, but not completely impossible.
1784  *
1785  * this happens, for example, right after
1786  * coap_retransmittimer_execute, when a retransmission
1787  * was *just not yet* due, and the clock ticked before
1788  * our coap_ticks was called.
1789  *
1790  * not trying to retransmit anything now, as it might
1791  * cause uncontrollable recursion; let's just try again
1792  * with the next main loop run.
1793  * */
1794  delay = 0;
1795  }
1796 
1797  printf("scheduling for %d ticks\n", delay);
1798  sys_timeout(delay, coap_retransmittimer_execute, (void*)ctx);
1799  ctx->timer_configured = 1;
1800  }
1801 }
1802 #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:699
int coap_handle_message(coap_context_t *ctx, coap_packet_t *packet)
Parses and interprets a CoAP message with context ctx.
Definition: net.c:845
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:344
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:1143
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:1367
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:495
#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
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:1140
coap_queue_t * coap_find_transaction(coap_queue_t *queue, coap_tid_t id)
Retrieves transaction from the queue.
Definition: net.c:1020
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:1125
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:1028
#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:980
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:533
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:1671
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:613
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:673
#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:1371
#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:18
#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:102
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:643
Structure of Block options.
Definition: block.h:33
static int handle_locally(coap_context_t *context, coap_queue_t *node)
Definition: net.c:1557
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:418
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:1564
int coap_option_check_critical(coap_context_t *ctx, coap_pdu_t *pdu, coap_opt_filter_t unknown)
Verifies that pdu contains no unknown critical options.
Definition: net.c:443
#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:1334
#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:553
#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:974
#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:309
int coap_read(coap_context_t *ctx)
Reads data from the network and tries to parse as CoAP PDU.
Definition: net.c:822
int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
Definition: address.c:41
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:1277
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:621
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:930
coap_tid_t coap_retransmit(coap_context_t *context, coap_queue_t *node)
Handles retransmissions of confirmable messages.
Definition: net.c:771
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:1303
#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:1531
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