libcoap  4.3.0rc1
net.c
Go to the documentation of this file.
1 /* net.c -- CoAP network interface
2  *
3  * Copyright (C) 2010--2019 Olaf Bergmann <bergmann@tzi.org> and others
4  *
5  * This file is part of the CoAP library libcoap. Please see
6  * README for terms of use.
7  */
8 
9 #include "coap2/coap_internal.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 #else
20 #ifdef HAVE_SYS_UNISTD_H
21 #include <sys/unistd.h>
22 #endif
23 #endif
24 #ifdef HAVE_SYS_TYPES_H
25 #include <sys/types.h>
26 #endif
27 #ifdef HAVE_SYS_SOCKET_H
28 #include <sys/socket.h>
29 #endif
30 #ifdef HAVE_SYS_IOCTL_H
31 #include <sys/ioctl.h>
32 #endif
33 #ifdef HAVE_NETINET_IN_H
34 #include <netinet/in.h>
35 #endif
36 #ifdef HAVE_ARPA_INET_H
37 #include <arpa/inet.h>
38 #endif
39 #ifdef HAVE_NET_IF_H
40 #include <net/if.h>
41 #endif
42 #ifdef COAP_EPOLL_SUPPORT
43 #include <sys/epoll.h>
44 #include <sys/timerfd.h>
45 #endif /* COAP_EPOLL_SUPPORT */
46 #ifdef HAVE_WS2TCPIP_H
47 #include <ws2tcpip.h>
48 #endif
49 
50 #ifdef HAVE_NETDB_H
51 #include <netdb.h>
52 #endif
53 
54 #ifdef WITH_LWIP
55 #include <lwip/pbuf.h>
56 #include <lwip/udp.h>
57 #include <lwip/timeouts.h>
58 #endif
59 
60 #ifndef INET6_ADDRSTRLEN
61 #define INET6_ADDRSTRLEN 40
62 #endif
63 
64 #ifndef min
65 #define min(a,b) ((a) < (b) ? (a) : (b))
66 #endif
67 
72 #define FRAC_BITS 6
73 
78 #define MAX_BITS 8
79 
80 #if FRAC_BITS > 8
81 #error FRAC_BITS must be less or equal 8
82 #endif
83 
85 #define Q(frac,fval) ((uint16_t)(((1 << (frac)) * fval.integer_part) + \
86  ((1 << (frac)) * fval.fractional_part + 500)/1000))
87 
89 #define ACK_RANDOM_FACTOR \
90  Q(FRAC_BITS, session->ack_random_factor)
91 
93 #define ACK_TIMEOUT Q(FRAC_BITS, session->ack_timeout)
94 
95 #if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
96 
100 }
101 
104  coap_free_type(COAP_NODE, node);
105 }
106 #endif /* !defined(WITH_LWIP) && !defined(WITH_CONTIKI) */
107 
108 #ifdef WITH_LWIP
109 
110 #include <lwip/memp.h>
111 
112 static void coap_retransmittimer_execute(void *arg);
113 static void coap_retransmittimer_restart(coap_context_t *ctx);
114 
117  return (coap_queue_t *)memp_malloc(MEMP_COAP_NODE);
118 }
119 
122  memp_free(MEMP_COAP_NODE, node);
123 }
124 
125 #endif /* WITH_LWIP */
126 #ifdef WITH_CONTIKI
127 # ifndef DEBUG
128 # define DEBUG DEBUG_PRINT
129 # endif /* DEBUG */
130 
131 #include "net/ip/uip-debug.h"
132 
133 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
134 #define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[UIP_LLIPH_LEN])
135 
136 void coap_resources_init();
137 
138 unsigned char initialized = 0;
139 coap_context_t the_coap_context;
140 
141 PROCESS(coap_retransmit_process, "message retransmit process");
142 
146 }
147 
150  coap_free_type(COAP_NODE, node);
151 }
152 #endif /* WITH_CONTIKI */
153 
154 unsigned int
156  unsigned int result = 0;
157  coap_tick_diff_t delta = now - ctx->sendqueue_basetime;
158 
159  if (ctx->sendqueue) {
160  /* delta < 0 means that the new time stamp is before the old. */
161  if (delta <= 0) {
162  ctx->sendqueue->t -= delta;
163  } else {
164  /* This case is more complex: The time must be advanced forward,
165  * thus possibly leading to timed out elements at the queue's
166  * start. For every element that has timed out, its relative
167  * time is set to zero and the result counter is increased. */
168 
169  coap_queue_t *q = ctx->sendqueue;
170  coap_tick_t t = 0;
171  while (q && (t + q->t < (coap_tick_t)delta)) {
172  t += q->t;
173  q->t = 0;
174  result++;
175  q = q->next;
176  }
177 
178  /* finally adjust the first element that has not expired */
179  if (q) {
180  q->t = (coap_tick_t)delta - t;
181  }
182  }
183  }
184 
185  /* adjust basetime */
186  ctx->sendqueue_basetime += delta;
187 
188  return result;
189 }
190 
191 int
193  coap_queue_t *p, *q;
194  if (!queue || !node)
195  return 0;
196 
197  /* set queue head if empty */
198  if (!*queue) {
199  *queue = node;
200  return 1;
201  }
202 
203  /* replace queue head if PDU's time is less than head's time */
204  q = *queue;
205  if (node->t < q->t) {
206  node->next = q;
207  *queue = node;
208  q->t -= node->t; /* make q->t relative to node->t */
209  return 1;
210  }
211 
212  /* search for right place to insert */
213  do {
214  node->t -= q->t; /* make node-> relative to q->t */
215  p = q;
216  q = q->next;
217  } while (q && q->t <= node->t);
218 
219  /* insert new item */
220  if (q) {
221  q->t -= node->t; /* make q->t relative to node->t */
222  }
223  node->next = q;
224  p->next = node;
225  return 1;
226 }
227 
228 int
230  if (!node)
231  return 0;
232 
233  coap_delete_pdu(node->pdu);
234  if ( node->session ) {
235  /*
236  * Need to remove out of context->sendqueue as added in by coap_wait_ack()
237  */
238  if (node->session->context->sendqueue) {
239  LL_DELETE(node->session->context->sendqueue, node);
240  }
242  }
243  coap_free_node(node);
244 
245  return 1;
246 }
247 
248 void
250  if (!queue)
251  return;
252 
253  coap_delete_all(queue->next);
254  coap_delete_node(queue);
255 }
256 
257 coap_queue_t *
259  coap_queue_t *node;
260  node = coap_malloc_node();
261 
262  if (!node) {
263  coap_log(LOG_WARNING, "coap_new_node: malloc failed\n");
264  return NULL;
265  }
266 
267  memset(node, 0, sizeof(*node));
268  return node;
269 }
270 
271 coap_queue_t *
273  if (!context || !context->sendqueue)
274  return NULL;
275 
276  return context->sendqueue;
277 }
278 
279 coap_queue_t *
281  coap_queue_t *next;
282 
283  if (!context || !context->sendqueue)
284  return NULL;
285 
286  next = context->sendqueue;
287  context->sendqueue = context->sendqueue->next;
288  if (context->sendqueue) {
289  context->sendqueue->t += next->t;
290  }
291  next->next = NULL;
292  return next;
293 }
294 
295 static size_t
297  const coap_session_t *session,
298  const uint8_t *hint, size_t hint_len,
299  uint8_t *identity, size_t *identity_len, size_t max_identity_len,
300  uint8_t *psk, size_t max_psk_len
301 ) {
302  const coap_dtls_cpsk_info_t *psk_info;
303  (void)hint;
304  (void)hint_len;
305 
306  if (session->psk_identity && session->psk_key) {
307  if (session->psk_identity->length <= max_identity_len &&
308  session->psk_key->length <= max_psk_len) {
309  memcpy(identity, session->psk_identity->s, session->psk_identity->length);
310  memcpy(psk, session->psk_key->s, session->psk_key->length);
311  *identity_len = session->psk_identity->length;
312  return session->psk_key->length;
313  }
314  }
315  psk_info = &session->cpsk_setup_data.psk_info;
316  if (psk_info->identity.s && psk_info->identity.length > 0 &&
317  psk_info->key.s && psk_info->key.length > 0) {
318  if (psk_info->identity.length <= max_identity_len &&
319  psk_info->key.length <= max_psk_len) {
320  memcpy(identity, psk_info->identity.s, psk_info->identity.length);
321  memcpy(psk, psk_info->key.s, psk_info->key.length);
322  *identity_len = psk_info->identity.length;
323  return psk_info->key.length;
324  }
325  }
326  /* Not defined in coap_new_client_session_psk2() */
327  *identity_len = 0;
328  return 0;
329 }
330 
331 static size_t
333  const coap_session_t *session,
334  const uint8_t *identity, size_t identity_len,
335  uint8_t *psk, size_t max_psk_len
336 ) {
337  const coap_dtls_spsk_info_t *psk_info;
338  (void)identity;
339  (void)identity_len;
340 
341  if (!session)
342  return 0;
343 
344  if (session->psk_key &&
345  session->psk_key->length <= max_psk_len) {
346  memcpy(psk, session->psk_key->s, session->psk_key->length);
347  return session->psk_key->length;
348  }
349  psk_info = &session->context->spsk_setup_data.psk_info;
350  if (psk_info->key.s && psk_info->key.length > 0 &&
351  psk_info->key.length <= max_psk_len) {
352  memcpy(psk, psk_info->key.s, psk_info->key.length);
353  return psk_info->key.length;
354  }
355  /* Not defined in coap_context_set_psk2() */
356  return 0;
357 }
358 
359 static size_t
361  const coap_session_t *session,
362  uint8_t *hint, size_t max_hint_len
363 ) {
364  const coap_dtls_spsk_info_t *psk_info;
365 
366  if (!session)
367  return 0;
368 
369  if (session->psk_hint &&
370  session->psk_hint->s && session->psk_hint->length > 0 &&
371  session->psk_hint->length <= max_hint_len) {
372  memcpy(hint, session->psk_hint->s, session->psk_hint->length);
373  return session->psk_hint->length;
374  }
375  psk_info = &session->context->spsk_setup_data.psk_info;
376  if (psk_info->hint.s &&
377  psk_info->hint.length > 0 &&
378  psk_info->hint.length <= max_hint_len) {
379  memcpy(hint, psk_info->hint.s, psk_info->hint.length);
380  return psk_info->hint.length;
381  }
382  /* Not defined in coap_context_set_psk2() */
383  return 0;
384 }
385 
387  const char *hint,
388  const uint8_t *key, size_t key_len
389 ) {
390  coap_dtls_spsk_t setup_data;
391 
392  memset (&setup_data, 0, sizeof(setup_data));
393  if (hint) {
394  setup_data.psk_info.hint.s = (const uint8_t *)hint;
395  setup_data.psk_info.hint.length = strlen(hint);
396  }
397 
398  if (key && key_len > 0) {
399  setup_data.psk_info.key.s = key;
400  setup_data.psk_info.key.length = key_len;
401  }
402 
403  return coap_context_set_psk2(ctx, &setup_data);
404 }
405 
407  coap_dtls_spsk_t *setup_data
408 ) {
409  if (!setup_data)
410  return 0;
411 
412  ctx->spsk_setup_data = *setup_data;
413 
414  if (coap_dtls_is_supported()) {
415  return coap_dtls_context_set_spsk(ctx, setup_data);
416  }
417  return 0;
418 }
419 
421  const coap_dtls_pki_t* setup_data
422 ) {
423  if (!setup_data)
424  return 0;
425  if (setup_data->version != COAP_DTLS_PKI_SETUP_VERSION) {
426  coap_log(LOG_ERR, "coap_context_set_pki: Wrong version of setup_data\n");
427  return 0;
428  }
429  if (coap_dtls_is_supported()) {
430  return coap_dtls_context_set_pki(ctx, setup_data, COAP_DTLS_ROLE_SERVER);
431  }
432  return 0;
433 }
434 
436  const char *ca_file,
437  const char *ca_dir
438 ) {
439  if (coap_dtls_is_supported()) {
440  return coap_dtls_context_set_pki_root_cas(ctx, ca_file, ca_dir);
441  }
442  return 0;
443 }
444 
445 void coap_context_set_keepalive(coap_context_t *context, unsigned int seconds) {
446  context->ping_timeout = seconds;
447 }
448 
450 #ifdef COAP_EPOLL_SUPPORT
451  return context->epfd;
452 #else /* ! COAP_EPOLL_SUPPORT */
453  (void)context;
454  return -1;
455 #endif /* ! COAP_EPOLL_SUPPORT */
456 }
457 
460  const coap_address_t *listen_addr) {
461  coap_context_t *c;
462 
463 #ifdef WITH_CONTIKI
464  if (initialized)
465  return NULL;
466 #endif /* WITH_CONTIKI */
467 
468  coap_startup();
469 
470 #ifndef WITH_CONTIKI
472 #endif /* not WITH_CONTIKI */
473 
474 #ifndef WITH_CONTIKI
475  if (!c) {
476  coap_log(LOG_EMERG, "coap_init: malloc: failed\n");
477  return NULL;
478  }
479 #endif /* not WITH_CONTIKI */
480 #ifdef WITH_CONTIKI
481  coap_resources_init();
482 
483  c = &the_coap_context;
484  initialized = 1;
485 #endif /* WITH_CONTIKI */
486 
487  memset(c, 0, sizeof(coap_context_t));
488 
489 #ifdef COAP_EPOLL_SUPPORT
490  c->epfd = epoll_create1(0);
491  if (c->epfd == -1) {
492  coap_log(LOG_ERR, "coap_new_context: Unable to epoll_create: %s (%d)\n",
494  errno);
495  goto onerror;
496  }
497  if (c->epfd != -1) {
498  c->eptimerfd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK);
499  if (c->eptimerfd == -1) {
500  coap_log(LOG_ERR, "coap_new_context: Unable to timerfd_create: %s (%d)\n",
502  errno);
503  goto onerror;
504  }
505  else {
506  int ret;
507  struct epoll_event event;
508 
509  /* Needed if running 32bit as ptr is only 32bit */
510  memset(&event, 0, sizeof(event));
511  event.events = EPOLLIN;
512  /* We special case this event by setting to NULL */
513  event.data.ptr = NULL;
514 
515  ret = epoll_ctl(c->epfd, EPOLL_CTL_ADD, c->eptimerfd, &event);
516  if (ret == -1) {
518  "%s: epoll_ctl ADD failed: %s (%d)\n",
519  "coap_new_context",
520  coap_socket_strerror(), errno);
521  goto onerror;
522  }
523  }
524  }
525 #endif /* COAP_EPOLL_SUPPORT */
526 
527  if (coap_dtls_is_supported()) {
529  if (!c->dtls_context) {
530  coap_log(LOG_EMERG, "coap_init: no DTLS context available\n");
532  return NULL;
533  }
534  }
535 
536  /* set default CSM timeout */
537  c->csm_timeout = 30;
538 
539  if (listen_addr) {
540  coap_endpoint_t *endpoint = coap_new_endpoint(c, listen_addr, COAP_PROTO_UDP);
541  if (endpoint == NULL) {
542  goto onerror;
543  }
544  }
545 
546 #if !defined(WITH_LWIP)
549 #endif
550 
554 
555 #ifdef WITH_CONTIKI
556  process_start(&coap_retransmit_process, (char *)c);
557 
558  PROCESS_CONTEXT_BEGIN(&coap_retransmit_process);
559  etimer_set(&c->notify_timer, COAP_RESOURCE_CHECK_TIME * COAP_TICKS_PER_SECOND);
560  /* the retransmit timer must be initialized to some large value */
561  etimer_set(&the_coap_context.retransmit_timer, 0xFFFF);
562  PROCESS_CONTEXT_END(&coap_retransmit_process);
563 #endif /* WITH_CONTIKI */
564 
565  return c;
566 
567 onerror:
569  return NULL;
570 }
571 
572 void
573 coap_set_app_data(coap_context_t *ctx, void *app_data) {
574  assert(ctx);
575  ctx->app = app_data;
576 }
577 
578 void *
580  assert(ctx);
581  return ctx->app;
582 }
583 
584 void
586  coap_endpoint_t *ep, *tmp;
587  coap_session_t *sp, *rtmp;
588  coap_cache_entry_t *cp, *ctmp;
589 
590  if (!context)
591  return;
592 
593  /* Removing a resource may cause a CON observe to be sent */
594  coap_delete_all_resources(context);
595 
596  coap_delete_all(context->sendqueue);
597 
598 #ifdef WITH_LWIP
599  context->sendqueue = NULL;
600  coap_retransmittimer_restart(context);
601 #endif
602 
603 #ifndef WITHOUT_ASYNC
604  coap_delete_all_async(context);
605 #endif /* WITHOUT_ASYNC */
606  HASH_ITER(hh, context->cache, cp, ctmp) {
607  coap_delete_cache_entry(context, cp);
608  }
609  if (context->cache_ignore_count) {
611  }
612 
613  LL_FOREACH_SAFE(context->endpoint, ep, tmp) {
614  coap_free_endpoint(ep);
615  }
616 
617  SESSIONS_ITER_SAFE(context->sessions, sp, rtmp) {
619  }
620 
621  if (context->dtls_context)
623 #ifdef COAP_EPOLL_SUPPORT
624  if (context->eptimerfd != -1) {
625  int ret;
626  struct epoll_event event;
627 
628  /* Kernels prior to 2.6.9 expect non NULL event parameter */
629  ret = epoll_ctl(context->epfd, EPOLL_CTL_DEL, context->eptimerfd, &event);
630  if (ret == -1) {
632  "%s: epoll_ctl DEL failed: %s (%d)\n",
633  "coap_free_context",
634  coap_socket_strerror(), errno);
635  }
636  close(context->eptimerfd);
637  context->eptimerfd = -1;
638  }
639  if (context->epfd != -1) {
640  close(context->epfd);
641  context->epfd = -1;
642  }
643 #endif /* COAP_EPOLL_SUPPORT */
644 
645 #ifndef WITH_CONTIKI
646  coap_free_type(COAP_CONTEXT, context);
647 #else /* WITH_CONTIKI */
648  memset(&the_coap_context, 0, sizeof(coap_context_t));
649  initialized = 0;
650 #endif /* WITH_CONTIKI */
651 }
652 
653 int
655  coap_pdu_t *pdu,
656  coap_opt_filter_t unknown) {
657 
658  coap_opt_iterator_t opt_iter;
659  int ok = 1;
660 
661  coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL);
662 
663  while (coap_option_next(&opt_iter)) {
664 
665  /* The following condition makes use of the fact that
666  * coap_option_getb() returns -1 if type exceeds the bit-vector
667  * filter. As the vector is supposed to be large enough to hold
668  * the largest known option, we know that everything beyond is
669  * bad.
670  */
671  if (opt_iter.type & 0x01) {
672  /* first check the built-in critical options */
673  switch (opt_iter.type) {
680  case COAP_OPTION_ACCEPT:
683  case COAP_OPTION_BLOCK2:
684  case COAP_OPTION_BLOCK1:
685  break;
686  default:
687  if (coap_option_filter_get(&ctx->known_options, opt_iter.type) <= 0) {
688  coap_log(LOG_DEBUG, "unknown critical option %d\n", opt_iter.type);
689  ok = 0;
690 
691  /* When opt_iter.type is beyond our known option range,
692  * coap_option_filter_set() will return -1 and we are safe to leave
693  * this loop. */
694  if (coap_option_filter_set(&unknown, opt_iter.type) == -1) {
695  break;
696  }
697  }
698  }
699  }
700  }
701 
702  return ok;
703 }
704 
707  coap_pdu_t *response;
708  coap_mid_t result = COAP_INVALID_MID;
709 
710  if (request && request->type == COAP_MESSAGE_CON &&
711  COAP_PROTO_NOT_RELIABLE(session->proto)) {
712  response = coap_pdu_init(COAP_MESSAGE_ACK, 0, request->mid, 0);
713  if (response)
714  result = coap_send(session, response);
715  }
716  return result;
717 }
718 
719 ssize_t
721  ssize_t bytes_written = -1;
722  assert(pdu->hdr_size > 0);
723  switch(session->proto) {
724  case COAP_PROTO_UDP:
725  bytes_written = coap_session_send(session, pdu->token - pdu->hdr_size,
726  pdu->used_size + pdu->hdr_size);
727  break;
728  case COAP_PROTO_DTLS:
729  bytes_written = coap_dtls_send(session, pdu->token - pdu->hdr_size,
730  pdu->used_size + pdu->hdr_size);
731  break;
732  case COAP_PROTO_TCP:
733 #if !COAP_DISABLE_TCP
734  bytes_written = coap_session_write(session, pdu->token - pdu->hdr_size,
735  pdu->used_size + pdu->hdr_size);
736 #endif /* !COAP_DISABLE_TCP */
737  break;
738  case COAP_PROTO_TLS:
739 #if !COAP_DISABLE_TCP
740  bytes_written = coap_tls_write(session, pdu->token - pdu->hdr_size,
741  pdu->used_size + pdu->hdr_size);
742 #endif /* !COAP_DISABLE_TCP */
743  break;
744  default:
745  break;
746  }
747  coap_show_pdu(LOG_DEBUG, pdu);
748  return bytes_written;
749 }
750 
751 static ssize_t
753  ssize_t bytes_written;
754 
755 #ifdef WITH_LWIP
756 
757  coap_socket_t *sock = &session->sock;
758  if (sock->flags == COAP_SOCKET_EMPTY) {
759  assert(session->endpoint != NULL);
760  sock = &session->endpoint->sock;
761  }
762 
763  bytes_written = coap_socket_send_pdu(sock, session, pdu);
764  if (bytes_written >= 0 && pdu->type == COAP_MESSAGE_CON &&
765  COAP_PROTO_NOT_RELIABLE(session->proto))
766  session->con_active++;
767 
768  if (LOG_DEBUG <= coap_get_log_level()) {
769  coap_show_pdu(LOG_DEBUG, pdu);
770  }
771  coap_ticks(&session->last_rx_tx);
772 
773 #else
774 
775  if (session->state == COAP_SESSION_STATE_NONE) {
776  if (session->proto == COAP_PROTO_DTLS && !session->tls) {
777  session->tls = coap_dtls_new_client_session(session);
778  if (session->tls) {
780  return coap_session_delay_pdu(session, pdu, node);
781  }
782  coap_handle_event(session->context, COAP_EVENT_DTLS_ERROR, session);
783  return -1;
784 #if !COAP_DISABLE_TCP
785  } else if(COAP_PROTO_RELIABLE(session->proto)) {
787  &session->sock, &session->addr_info.local, &session->addr_info.remote,
788  session->proto == COAP_PROTO_TLS ? COAPS_DEFAULT_PORT :
790  &session->addr_info.local, &session->addr_info.remote
791  )) {
792  coap_handle_event(session->context, COAP_EVENT_TCP_FAILED, session);
793  return -1;
794  }
795  session->last_ping = 0;
796  session->last_pong = 0;
797  session->csm_tx = 0;
798  coap_ticks( &session->last_rx_tx );
799  if ((session->sock.flags & COAP_SOCKET_WANT_CONNECT) != 0) {
801  return coap_session_delay_pdu(session, pdu, node);
802  }
804  if (session->proto == COAP_PROTO_TLS) {
805  int connected = 0;
807  session->tls = coap_tls_new_client_session(session, &connected);
808  if (session->tls) {
809  if (connected) {
811  coap_session_send_csm(session);
812  }
813  return coap_session_delay_pdu(session, pdu, node);
814  }
815  coap_handle_event(session->context, COAP_EVENT_DTLS_ERROR, session);
817  return -1;
818  } else {
819  coap_session_send_csm(session);
820  }
821 #endif /* !COAP_DISABLE_TCP */
822  } else {
823  return -1;
824  }
825  }
826 
827  if (pdu->type == COAP_MESSAGE_CON &&
828  (session->sock.flags & COAP_SOCKET_NOT_EMPTY) &&
829  (session->sock.flags & COAP_SOCKET_MULTICAST)) {
830  /* Violates RFC72522 8.1 */
831  coap_log(LOG_ERR, "Multicast requests cannot be Confirmable (RFC7252 8.1)\n");
832  return -1;
833  }
834 
835  if (session->state != COAP_SESSION_STATE_ESTABLISHED ||
836  (pdu->type == COAP_MESSAGE_CON && session->con_active >= COAP_DEFAULT_NSTART)) {
837  return coap_session_delay_pdu(session, pdu, node);
838  }
839 
840  if ((session->sock.flags & COAP_SOCKET_NOT_EMPTY) &&
841  (session->sock.flags & COAP_SOCKET_WANT_WRITE))
842  return coap_session_delay_pdu(session, pdu, node);
843 
844  bytes_written = coap_session_send_pdu(session, pdu);
845  if (bytes_written >= 0 && pdu->type == COAP_MESSAGE_CON &&
846  COAP_PROTO_NOT_RELIABLE(session->proto))
847  session->con_active++;
848 
849 #endif /* WITH_LWIP */
850 
851  return bytes_written;
852 }
853 
856  coap_pdu_t *request,
857  unsigned char code,
858  coap_opt_filter_t *opts) {
859  coap_pdu_t *response;
860  coap_mid_t result = COAP_INVALID_MID;
861 
862  assert(request);
863  assert(session);
864 
865  response = coap_new_error_response(request, code, opts);
866  if (response)
867  result = coap_send(session, response);
868 
869  return result;
870 }
871 
873 coap_send_message_type(coap_session_t *session, coap_pdu_t *request, unsigned char type) {
874  coap_pdu_t *response;
875  coap_mid_t result = COAP_INVALID_MID;
876 
877  if (request) {
878  response = coap_pdu_init(type, 0, request->mid, 0);
879  if (response)
880  result = coap_send(session, response);
881  }
882  return result;
883 }
884 
898 unsigned int
899 coap_calc_timeout(coap_session_t *session, unsigned char r) {
900  unsigned int result;
901 
902  /* The integer 1.0 as a Qx.FRAC_BITS */
903 #define FP1 Q(FRAC_BITS, ((coap_fixed_point_t){1,0}))
904 
905  /* rounds val up and right shifts by frac positions */
906 #define SHR_FP(val,frac) (((val) + (1 << ((frac) - 1))) >> (frac))
907 
908  /* Inner term: multiply ACK_RANDOM_FACTOR by Q0.MAX_BITS[r] and
909  * make the result a rounded Qx.FRAC_BITS */
910  result = SHR_FP((ACK_RANDOM_FACTOR - FP1) * r, MAX_BITS);
911 
912  /* Add 1 to the inner term and multiply with ACK_TIMEOUT, then
913  * make the result a rounded Qx.FRAC_BITS */
914  result = SHR_FP(((result + FP1) * ACK_TIMEOUT), FRAC_BITS);
915 
916  /* Multiply with COAP_TICKS_PER_SECOND to yield system ticks
917  * (yields a Qx.FRAC_BITS) and shift to get an integer */
918  return SHR_FP((COAP_TICKS_PER_SECOND * result), FRAC_BITS);
919 
920 #undef FP1
921 #undef SHR_FP
922 }
923 
926  coap_queue_t *node) {
927  coap_tick_t now;
928 
929  node->session = coap_session_reference(session);
930 
931  /* Set timer for pdu retransmission. If this is the first element in
932  * the retransmission queue, the base time is set to the current
933  * time and the retransmission time is node->timeout. If there is
934  * already an entry in the sendqueue, we must check if this node is
935  * to be retransmitted earlier. Therefore, node->timeout is first
936  * normalized to the base time and then inserted into the queue with
937  * an adjusted relative time.
938  */
939  coap_ticks(&now);
940  if (context->sendqueue == NULL) {
941  node->t = node->timeout << node->retransmit_cnt;
942  context->sendqueue_basetime = now;
943  } else {
944  /* make node->t relative to context->sendqueue_basetime */
945  node->t = (now - context->sendqueue_basetime) +
946  (node->timeout << node->retransmit_cnt);
947  }
948 
949  coap_insert_node(&context->sendqueue, node);
950 
951 #ifdef WITH_LWIP
952  if (node == context->sendqueue) /* don't bother with timer stuff if there are earlier retransmits */
953  coap_retransmittimer_restart(context);
954 #endif
955 
956 #ifdef WITH_CONTIKI
957  { /* (re-)initialize retransmission timer */
958  coap_queue_t *nextpdu;
959 
960  nextpdu = coap_peek_next(context);
961  assert(nextpdu); /* we have just inserted a node */
962 
963  /* must set timer within the context of the retransmit process */
964  PROCESS_CONTEXT_BEGIN(&coap_retransmit_process);
965  etimer_set(&context->retransmit_timer, nextpdu->t);
966  PROCESS_CONTEXT_END(&coap_retransmit_process);
967  }
968 #endif /* WITH_CONTIKI */
969 
970  coap_log(LOG_DEBUG, "** %s: mid=0x%x: added to retransmit queue (%ums)\n",
971  coap_session_str(node->session), node->id,
972  (unsigned)(node->t * 1000 / COAP_TICKS_PER_SECOND));
973 
974 #ifdef COAP_EPOLL_SUPPORT
975  if (context->eptimerfd != -1) {
976  coap_ticks(&now);
977  if (context->next_timeout == 0 ||
978  context->next_timeout > now + (node->t * 1000 / COAP_TICKS_PER_SECOND)) {
979  struct itimerspec new_value;
980  int ret;
981 
982  context->next_timeout = now + (node->t * 1000 / COAP_TICKS_PER_SECOND);
983  memset(&new_value, 0, sizeof(new_value));
984  coap_tick_t rem_timeout = (node->t * 1000 / COAP_TICKS_PER_SECOND);
985  /* Need to trigger an event on context->epfd in the future */
986  new_value.it_value.tv_sec = rem_timeout / 1000;
987  new_value.it_value.tv_nsec = (rem_timeout % 1000) * 1000000;
988  ret = timerfd_settime(context->eptimerfd, 0, &new_value, NULL);
989  if (ret == -1) {
991  "%s: timerfd_settime failed: %s (%d)\n",
992  "coap_wait_ack",
993  coap_socket_strerror(), errno);
994  }
995  }
996  }
997 #endif /* COAP_EPOLL_SUPPORT */
998 
999  return node->id;
1000 }
1001 
1003 token_match(const uint8_t *a, size_t alen,
1004  const uint8_t *b, size_t blen) {
1005  return alen == blen && (alen == 0 || memcmp(a, b, alen) == 0);
1006 }
1007 
1008 coap_mid_t
1011  coap_lg_crcv_t *lg_crcv = NULL;
1012  coap_opt_iterator_t opt_iter;
1013  int observe_action = -1;
1014  int have_block1 = 0;
1015  coap_opt_t *opt;
1016 
1017  assert(pdu);
1018 
1019  if (!(session->block_mode & COAP_BLOCK_USE_LIBCOAP)) {
1021  "** %s: coap_send_large: COAP_BLOCK_USE_LIBCOAP not enabled\n",
1022  coap_session_str(session));
1023  return coap_send(session, pdu);
1024  }
1025 
1026  if (COAP_PDU_IS_REQUEST(pdu)) {
1027  coap_block_t block;
1028 
1029  opt = coap_check_option(pdu, COAP_OPTION_OBSERVE, &opt_iter);
1030 
1031  if (opt) {
1032  observe_action = coap_decode_var_bytes(coap_opt_value(opt),
1033  coap_opt_length(opt));
1034  }
1035 
1036  if (coap_get_block(pdu, COAP_OPTION_BLOCK1, &block) && block.m == 1)
1037  have_block1 = 1;
1038  }
1039 
1040  /*
1041  * If type is CON and protocol is not reliable, there is no need to set up
1042  * lg_crcv here as it can be built up based on sent PDU if there is a
1043  * Block2 in the response. However, still need it for observe and block1.
1044  */
1045  if (observe_action != -1 || have_block1 ||
1046  ((pdu->type == COAP_MESSAGE_NON || COAP_PROTO_RELIABLE(session->proto)) &&
1047  COAP_PDU_IS_REQUEST(pdu) && pdu->code != COAP_REQUEST_DELETE)) {
1048  /* See if this token is already in use for large body responses */
1049  LL_FOREACH(session->lg_crcv, lg_crcv) {
1050  if (token_match(pdu->token, pdu->token_length,
1051  lg_crcv->app_token->s, lg_crcv->app_token->length)) {
1052 
1053  if (observe_action == COAP_OBSERVE_CANCEL) {
1054  /* Need to update token to server's version */
1055  coap_update_token(pdu, lg_crcv->base_token_length,
1056  lg_crcv->base_token);
1057  memcpy(lg_crcv->token, lg_crcv->base_token,
1058  lg_crcv->base_token_length);
1059  lg_crcv->token_length = lg_crcv->base_token_length;
1060  lg_crcv->initial = 1;
1061  lg_crcv->observe_set = 0;
1062  /* de-reference lg_crcv as potentially linking in later */
1063  LL_DELETE(session->lg_crcv, lg_crcv);
1064  goto send_it;
1065  }
1066 
1067  /* Need to terminate and clean up previous response setup */
1068  LL_DELETE(session->lg_crcv, lg_crcv);
1069  coap_block_delete_lg_crcv(session, lg_crcv);
1070  break;
1071  }
1072  }
1073 
1074  lg_crcv = coap_block_new_lg_crcv(session, pdu);
1075  if (lg_crcv == NULL)
1076  return COAP_INVALID_MID;
1077  if (have_block1 && session->lg_xmit) {
1078  coap_lg_xmit_t *lg_xmit;
1079 
1080  LL_FOREACH(session->lg_xmit, lg_xmit) {
1081  if (COAP_PDU_IS_REQUEST(&lg_xmit->pdu) &&
1082  lg_xmit->b.b1.app_token &&
1083  token_match(pdu->token, pdu->token_length,
1084  lg_xmit->b.b1.app_token->s,
1085  lg_xmit->b.b1.app_token->length)) {
1086  /* Need to update the token as set up in the session->lg_xmit */
1087  coap_update_token(pdu, session->lg_xmit->b.b1.token_length,
1088  session->lg_xmit->b.b1.token);
1089  break;
1090  }
1091  }
1092  }
1093  }
1094 
1095 send_it:
1096  mid = coap_send(session, pdu);
1097  if (lg_crcv) {
1098  if (mid != COAP_INVALID_MID) {
1099  LL_PREPEND(session->lg_crcv, lg_crcv);
1100  }
1101  else {
1102  coap_block_delete_lg_crcv(session, lg_crcv);
1103  }
1104  }
1105  return mid;
1106 }
1107 
1108 coap_mid_t
1110  uint8_t r;
1111  ssize_t bytes_written;
1112  coap_opt_iterator_t opt_iter;
1113 
1114  if (pdu->code == COAP_RESPONSE_CODE(508)) {
1115  /*
1116  * Need to prepend our IP identifier to the data as per
1117  * https://www.rfc-editor.org/rfc/rfc8768.html#section-4
1118  */
1119  char addr_str[INET6_ADDRSTRLEN + 8 + 1];
1120  coap_opt_t *opt;
1121  size_t hop_limit;
1122 
1123  addr_str[sizeof(addr_str)-1] = '\000';
1124  if (coap_print_addr(&session->addr_info.local, (uint8_t*)addr_str,
1125  sizeof(addr_str) - 1)) {
1126  char *cp;
1127  size_t len;
1128 
1129  if (addr_str[0] == '[') {
1130  cp = strchr(addr_str, ']');
1131  if (cp) *cp = '\000';
1132  if (memcmp(&addr_str[1], "::ffff:", 7) == 0) {
1133  /* IPv4 embedded into IPv6 */
1134  cp = &addr_str[8];
1135  }
1136  else {
1137  cp = &addr_str[1];
1138  }
1139  }
1140  else {
1141  cp = strchr(addr_str, ':');
1142  if (cp) *cp = '\000';
1143  cp = addr_str;
1144  }
1145  len = strlen(cp);
1146 
1147  /* See if Hop Limit option is being used in return path */
1148  opt = coap_check_option(pdu, COAP_OPTION_HOP_LIMIT, &opt_iter);
1149  if (opt) {
1150  uint8_t buf[4];
1151 
1152  hop_limit =
1154  if (hop_limit == 1) {
1155  coap_log(LOG_WARNING, "Proxy loop detected '%s'\n",
1156  (char*)pdu->data);
1157  coap_delete_pdu(pdu);
1159  }
1160  else if (hop_limit < 1 || hop_limit > 255) {
1161  /* Something is bad - need to drop this pdu (TODO or delete option) */
1162  coap_log(LOG_WARNING, "Proxy return has bad hop limit count '%zu'\n",
1163  hop_limit);
1164  coap_delete_pdu(pdu);
1166  }
1167  hop_limit--;
1169  coap_encode_var_safe8(buf, sizeof(buf), hop_limit),
1170  buf);
1171  }
1172 
1173  /* Need to check that we are not seeing this proxy in the return loop */
1174  if (pdu->data && opt == NULL) {
1175  if (pdu->used_size + 1 <= pdu->max_size) {
1176  char *a_match;
1177  size_t data_len = pdu->used_size - (pdu->data - pdu->token);
1178  pdu->data[data_len] = '\000';
1179  a_match = strstr((char*)pdu->data, cp);
1180  if (a_match && (a_match == (char*)pdu->data || a_match[-1] == ' ') &&
1181  ((size_t)(a_match - (char*)pdu->data + len) == data_len ||
1182  a_match[len] == ' ')) {
1183  coap_log(LOG_WARNING, "Proxy loop detected '%s'\n",
1184  (char*)pdu->data);
1185  coap_delete_pdu(pdu);
1187  }
1188  }
1189  }
1190  if (pdu->used_size + len + 1 <= pdu->max_size) {
1191  size_t old_size = pdu->used_size;
1192  if (coap_pdu_resize(pdu, pdu->used_size + len + 1)) {
1193  if (pdu->data == NULL) {
1194  /*
1195  * Set Hop Limit to max for return path. If this libcoap is in
1196  * a proxy loop path, it will always decrement hop limit in code
1197  * above and hence timeout / drop the response as appropriate
1198  */
1199  hop_limit = 255;
1201  (uint8_t *)&hop_limit);
1202  coap_add_data(pdu, len, (uint8_t*)cp);
1203  }
1204  else {
1205  /* prepend with space separator, leaving hop limit "as is" */
1206  memmove(pdu->data + len + 1, pdu->data,
1207  old_size - (pdu->data - pdu->token));
1208  memcpy(pdu->data, cp, len);
1209  pdu->data[len] = ' ';
1210  pdu->used_size += len + 1;
1211  }
1212  }
1213  }
1214  }
1215  }
1216 
1217  if (!coap_pdu_encode_header(pdu, session->proto)) {
1218  goto error;
1219  }
1220 
1221 #if !COAP_DISABLE_TCP
1222  if (COAP_PROTO_RELIABLE(session->proto) &&
1223  session->state == COAP_SESSION_STATE_ESTABLISHED &&
1224  !session->csm_block_supported) {
1225  /*
1226  * Need to check that this instance is not sending any block options as the
1227  * remote end via CSM has not informed us that there is support
1228  * https://tools.ietf.org/html/rfc8323#section-5.3.2
1229  * Note that this also includes BERT which is application specific.
1230  */
1231  if (coap_check_option(pdu, COAP_OPTION_BLOCK1, &opt_iter) != NULL) {
1233  "Remote end did not indicate CSM support for BLOCK1 enabled\n");
1234  }
1235  if (coap_check_option(pdu, COAP_OPTION_BLOCK2, &opt_iter) != NULL) {
1237  "Remote end did not indicate CSM support for BLOCK2 enabled\n");
1238  }
1239  }
1240 #endif /* !COAP_DISABLE_TCP */
1241 
1242  bytes_written = coap_send_pdu( session, pdu, NULL );
1243 
1244  if (bytes_written == COAP_PDU_DELAYED) {
1245  /* do not free pdu as it is stored with session for later use */
1246  return pdu->mid;
1247  }
1248 
1249  if (bytes_written < 0) {
1250  coap_delete_pdu(pdu);
1251  return (coap_mid_t)bytes_written;
1252  }
1253 
1254 #if !COAP_DISABLE_TCP
1255  if (COAP_PROTO_RELIABLE(session->proto) &&
1256  (size_t)bytes_written < pdu->used_size + pdu->hdr_size) {
1257  if (coap_session_delay_pdu(session, pdu, NULL) == COAP_PDU_DELAYED) {
1258  session->partial_write = (size_t)bytes_written;
1259  /* do not free pdu as it is stored with session for later use */
1260  return pdu->mid;
1261  } else {
1262  goto error;
1263  }
1264  }
1265 #endif /* !COAP_DISABLE_TCP */
1266 
1267  if (pdu->type != COAP_MESSAGE_CON
1268  || COAP_PROTO_RELIABLE(session->proto)) {
1269  coap_mid_t id = pdu->mid;
1270  coap_delete_pdu(pdu);
1271  return id;
1272  }
1273 
1274  coap_queue_t *node = coap_new_node();
1275  if (!node) {
1276  coap_log(LOG_DEBUG, "coap_wait_ack: insufficient memory\n");
1277  goto error;
1278  }
1279 
1280  node->id = pdu->mid;
1281  node->pdu = pdu;
1282  coap_prng(&r, sizeof(r));
1283  /* add timeout in range [ACK_TIMEOUT...ACK_TIMEOUT * ACK_RANDOM_FACTOR] */
1284  node->timeout = coap_calc_timeout(session, r);
1285  return coap_wait_ack(session->context, session, node);
1286  error:
1287  coap_delete_pdu(pdu);
1288  return COAP_INVALID_MID;
1289 }
1290 
1291 coap_mid_t
1293  if (!context || !node)
1294  return COAP_INVALID_MID;
1295 
1296  /* re-initialize timeout when maximum number of retransmissions are not reached yet */
1297  if (node->retransmit_cnt < node->session->max_retransmit) {
1298  ssize_t bytes_written;
1299  coap_tick_t now;
1300 
1301  node->retransmit_cnt++;
1302  coap_ticks(&now);
1303  if (context->sendqueue == NULL) {
1304  node->t = node->timeout << node->retransmit_cnt;
1305  context->sendqueue_basetime = now;
1306  } else {
1307  /* make node->t relative to context->sendqueue_basetime */
1308  node->t = (now - context->sendqueue_basetime) + (node->timeout << node->retransmit_cnt);
1309  }
1310  coap_insert_node(&context->sendqueue, node);
1311 #ifdef WITH_LWIP
1312  if (node == context->sendqueue) /* don't bother with timer stuff if there are earlier retransmits */
1313  coap_retransmittimer_restart(context);
1314 #endif
1315 
1316  coap_log(LOG_DEBUG, "** %s: mid=0x%x: retransmission #%d\n",
1317  coap_session_str(node->session), node->id, node->retransmit_cnt);
1318 
1319  if (node->session->con_active)
1320  node->session->con_active--;
1321  bytes_written = coap_send_pdu(node->session, node->pdu, node);
1322 
1323  if (bytes_written == COAP_PDU_DELAYED) {
1324  /* PDU was not retransmitted immediately because a new handshake is
1325  in progress. node was moved to the send queue of the session. */
1326  return node->id;
1327  }
1328 
1329  if (bytes_written < 0)
1330  return (int)bytes_written;
1331 
1332  return node->id;
1333  }
1334 
1335  /* no more retransmissions, remove node from system */
1336 
1337 #ifndef WITH_CONTIKI
1338  coap_log(LOG_DEBUG, "** %s: mid=0x%x: give up after %d attempts\n",
1339  coap_session_str(node->session), node->id, node->retransmit_cnt);
1340 #endif
1341 
1342  /* Check if subscriptions exist that should be canceled after
1343  COAP_MAX_NOTIFY_FAILURES */
1344  if (node->pdu->code >= 64) {
1345  coap_binary_t token = { 0, NULL };
1346 
1347  token.length = node->pdu->token_length;
1348  token.s = node->pdu->token;
1349 
1350  coap_handle_failed_notify(context, node->session, &token);
1351  }
1352  if (node->session->con_active) {
1353  node->session->con_active--;
1355  /*
1356  * As there may be another CON in a different queue entry on the same
1357  * session that needs to be immediately released,
1358  * coap_session_connected() is called.
1359  * However, there is the possibility coap_wait_ack() may be called for
1360  * this node (queue) and re-added to context->sendqueue.
1361  * coap_delete_node(node) called shortly will handle this and remove it.
1362  */
1364  }
1365  }
1366 
1367  /* And finally delete the node */
1368  if (node->pdu->type == COAP_MESSAGE_CON && context->nack_handler)
1369  context->nack_handler(context, node->session, node->pdu, COAP_NACK_TOO_MANY_RETRIES, node->id);
1370  coap_delete_node(node);
1371  return COAP_INVALID_MID;
1372 }
1373 
1374 #ifdef WITH_LWIP
1375 /* WITH_LWIP, this is handled by coap_recv in a different way */
1376 void
1378  return;
1379 }
1380 #else /* WITH_LWIP */
1381 
1382 static int
1384  uint8_t *data;
1385  size_t data_len;
1386  int result = -1;
1387 
1388  coap_packet_get_memmapped(packet, &data, &data_len);
1389 
1390  if (session->proto == COAP_PROTO_DTLS) {
1391  if (session->type == COAP_SESSION_TYPE_HELLO)
1392  result = coap_dtls_hello(session, data, data_len);
1393  else if (session->tls)
1394  result = coap_dtls_receive(session, data, data_len);
1395  } else if (session->proto == COAP_PROTO_UDP) {
1396  result = coap_handle_dgram(ctx, session, data, data_len);
1397  }
1398  return result;
1399 }
1400 
1401 static void
1403  coap_session_t *session,
1404  coap_tick_t now) {
1405  (void)ctx;
1406 #if COAP_DISABLE_TCP
1407  (void)session;
1408  (void)now;
1409 #else /* !COAP_DISABLE_TCP */
1410  if (coap_socket_connect_tcp2(&session->sock, &session->addr_info.local,
1411  &session->addr_info.remote)) {
1412  session->last_rx_tx = now;
1414  if (session->proto == COAP_PROTO_TCP) {
1415  coap_session_send_csm(session);
1416  } else if (session->proto == COAP_PROTO_TLS) {
1417  int connected = 0;
1419  session->tls = coap_tls_new_client_session(session, &connected);
1420  if (session->tls) {
1421  if (connected) {
1423  session);
1424  coap_session_send_csm(session);
1425  }
1426  } else {
1427  coap_handle_event(session->context, COAP_EVENT_DTLS_ERROR, session);
1429  }
1430  }
1431  } else {
1432  coap_handle_event(session->context, COAP_EVENT_TCP_FAILED, session);
1434  }
1435 #endif /* !COAP_DISABLE_TCP */
1436 }
1437 
1438 static void
1440  (void)ctx;
1441  assert(session->sock.flags & COAP_SOCKET_CONNECTED);
1442 
1443  while (session->delayqueue) {
1444  ssize_t bytes_written;
1445  coap_queue_t *q = session->delayqueue;
1446  coap_log(LOG_DEBUG, "** %s: mid=0x%x: transmitted after delay\n",
1447  coap_session_str(session), (int)q->pdu->mid);
1448  assert(session->partial_write < q->pdu->used_size + q->pdu->hdr_size);
1449  switch (session->proto) {
1450  case COAP_PROTO_TCP:
1451 #if !COAP_DISABLE_TCP
1452  bytes_written = coap_session_write(
1453  session,
1454  q->pdu->token - q->pdu->hdr_size - session->partial_write,
1455  q->pdu->used_size + q->pdu->hdr_size - session->partial_write
1456  );
1457 #endif /* !COAP_DISABLE_TCP */
1458  break;
1459  case COAP_PROTO_TLS:
1460 #if !COAP_DISABLE_TCP
1461  bytes_written = coap_tls_write(
1462  session,
1463  q->pdu->token - q->pdu->hdr_size - session->partial_write,
1464  q->pdu->used_size + q->pdu->hdr_size - session->partial_write
1465  );
1466 #endif /* !COAP_DISABLE_TCP */
1467  break;
1468  default:
1469  bytes_written = -1;
1470  break;
1471  }
1472  if (bytes_written > 0)
1473  session->last_rx_tx = now;
1474  if (bytes_written <= 0 || (size_t)bytes_written < q->pdu->used_size + q->pdu->hdr_size - session->partial_write) {
1475  if (bytes_written > 0)
1476  session->partial_write += (size_t)bytes_written;
1477  break;
1478  }
1479  session->delayqueue = q->next;
1480  session->partial_write = 0;
1481  coap_delete_node(q);
1482  }
1483 }
1484 
1485 static void
1487 #if COAP_CONSTRAINED_STACK
1488  static coap_mutex_t s_static_mutex = COAP_MUTEX_INITIALIZER;
1489  static coap_packet_t s_packet;
1490 #else /* ! COAP_CONSTRAINED_STACK */
1491  coap_packet_t s_packet;
1492 #endif /* ! COAP_CONSTRAINED_STACK */
1493  coap_packet_t *packet = &s_packet;
1494 
1495 #if COAP_CONSTRAINED_STACK
1496  coap_mutex_lock(&s_static_mutex);
1497 #endif /* COAP_CONSTRAINED_STACK */
1498 
1499  assert(session->sock.flags & (COAP_SOCKET_CONNECTED | COAP_SOCKET_MULTICAST));
1500 
1501  if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
1502  ssize_t bytes_read;
1503  memcpy(&packet->addr_info, &session->addr_info, sizeof(packet->addr_info));
1504  bytes_read = ctx->network_read(&session->sock, packet);
1505 
1506  if (bytes_read < 0) {
1507  if (bytes_read == -2)
1508  /* Reset the session back to startup defaults */
1510  else
1511  coap_log(LOG_WARNING, "* %s: read error\n",
1512  coap_session_str(session));
1513  } else if (bytes_read > 0) {
1514  session->last_rx_tx = now;
1515  memcpy(&session->addr_info, &packet->addr_info,
1516  sizeof(session->addr_info));
1517  coap_log(LOG_DEBUG, "* %s: received %zd bytes\n",
1518  coap_session_str(session), bytes_read);
1519  coap_handle_dgram_for_proto(ctx, session, packet);
1520  }
1521 #if !COAP_DISABLE_TCP
1522  } else {
1523  ssize_t bytes_read = 0;
1524  const uint8_t *p;
1525  int retry;
1526  /* adjust for LWIP */
1527  uint8_t *buf = packet->payload;
1528  size_t buf_len = sizeof(packet->payload);
1529 
1530  do {
1531  if (session->proto == COAP_PROTO_TCP)
1532  bytes_read = coap_socket_read(&session->sock, buf, buf_len);
1533  else if (session->proto == COAP_PROTO_TLS)
1534  bytes_read = coap_tls_read(session, buf, buf_len);
1535  if (bytes_read > 0) {
1536  coap_log(LOG_DEBUG, "* %s: received %zd bytes\n",
1537  coap_session_str(session), bytes_read);
1538  session->last_rx_tx = now;
1539  }
1540  p = buf;
1541  retry = bytes_read == (ssize_t)buf_len;
1542  while (bytes_read > 0) {
1543  if (session->partial_pdu) {
1544  size_t len = session->partial_pdu->used_size
1545  + session->partial_pdu->hdr_size
1546  - session->partial_read;
1547  size_t n = min(len, (size_t)bytes_read);
1548  memcpy(session->partial_pdu->token - session->partial_pdu->hdr_size
1549  + session->partial_read, p, n);
1550  p += n;
1551  bytes_read -= n;
1552  if (n == len) {
1553  if (coap_pdu_parse_header(session->partial_pdu, session->proto)
1554  && coap_pdu_parse_opt(session->partial_pdu)) {
1555  coap_dispatch(ctx, session, session->partial_pdu);
1556  }
1557  coap_delete_pdu(session->partial_pdu);
1558  session->partial_pdu = NULL;
1559  session->partial_read = 0;
1560  } else {
1561  session->partial_read += n;
1562  }
1563  } else if (session->partial_read > 0) {
1564  size_t hdr_size = coap_pdu_parse_header_size(session->proto,
1565  session->read_header);
1566  size_t len = hdr_size - session->partial_read;
1567  size_t n = min(len, (size_t)bytes_read);
1568  memcpy(session->read_header + session->partial_read, p, n);
1569  p += n;
1570  bytes_read -= n;
1571  if (n == len) {
1572  size_t size = coap_pdu_parse_size(session->proto, session->read_header,
1573  hdr_size);
1574  if (size > COAP_DEFAULT_MAX_PDU_RX_SIZE) {
1576  "** %s: incoming PDU length too large (%zu > %lu)\n",
1577  coap_session_str(session),
1579  bytes_read = -1;
1580  break;
1581  }
1582  /* Need max space incase PDU is updated with updated token etc. */
1583  session->partial_pdu = coap_pdu_init(0, 0, 0,
1584  coap_session_max_pdu_size(session));
1585  if (session->partial_pdu == NULL) {
1586  bytes_read = -1;
1587  break;
1588  }
1589  if (session->partial_pdu->alloc_size < size && !coap_pdu_resize(session->partial_pdu, size)) {
1590  bytes_read = -1;
1591  break;
1592  }
1593  session->partial_pdu->hdr_size = (uint8_t)hdr_size;
1594  session->partial_pdu->used_size = size;
1595  memcpy(session->partial_pdu->token - hdr_size, session->read_header, hdr_size);
1596  session->partial_read = hdr_size;
1597  if (size == 0) {
1598  if (coap_pdu_parse_header(session->partial_pdu, session->proto)) {
1599  coap_dispatch(ctx, session, session->partial_pdu);
1600  }
1601  coap_delete_pdu(session->partial_pdu);
1602  session->partial_pdu = NULL;
1603  session->partial_read = 0;
1604  }
1605  } else {
1606  session->partial_read += bytes_read;
1607  }
1608  } else {
1609  session->read_header[0] = *p++;
1610  bytes_read -= 1;
1611  if (!coap_pdu_parse_header_size(session->proto,
1612  session->read_header)) {
1613  bytes_read = -1;
1614  break;
1615  }
1616  session->partial_read = 1;
1617  }
1618  }
1619  } while (bytes_read == 0 && retry);
1620  if (bytes_read < 0)
1622 #endif /* !COAP_DISABLE_TCP */
1623  }
1624 #if COAP_CONSTRAINED_STACK
1625  coap_mutex_unlock(&s_static_mutex);
1626 #endif /* COAP_CONSTRAINED_STACK */
1627 }
1628 
1629 static int
1631  ssize_t bytes_read = -1;
1632  int result = -1; /* the value to be returned */
1633 #if COAP_CONSTRAINED_STACK
1634  static coap_mutex_t e_static_mutex = COAP_MUTEX_INITIALIZER;
1635  static coap_packet_t e_packet;
1636 #else /* ! COAP_CONSTRAINED_STACK */
1637  coap_packet_t e_packet;
1638 #endif /* ! COAP_CONSTRAINED_STACK */
1639  coap_packet_t *packet = &e_packet;
1640 
1641  assert(COAP_PROTO_NOT_RELIABLE(endpoint->proto));
1642  assert(endpoint->sock.flags & COAP_SOCKET_BOUND);
1643 
1644 #if COAP_CONSTRAINED_STACK
1645  coap_mutex_lock(&e_static_mutex);
1646 #endif /* COAP_CONSTRAINED_STACK */
1647 
1648  /* Need to do this as there may be holes in addr_info */
1649  memset(&packet->addr_info, 0, sizeof(packet->addr_info));
1651  coap_address_copy(&packet->addr_info.local, &endpoint->bind_addr);
1652  bytes_read = ctx->network_read(&endpoint->sock, packet);
1653 
1654  if (bytes_read < 0) {
1655  coap_log(LOG_WARNING, "* %s: read failed\n", coap_endpoint_str(endpoint));
1656  } else if (bytes_read > 0) {
1657  coap_session_t *session = coap_endpoint_get_session(endpoint, packet, now);
1658  if (session) {
1659  coap_log(LOG_DEBUG, "* %s: received %zd bytes\n",
1660  coap_session_str(session), bytes_read);
1661  result = coap_handle_dgram_for_proto(ctx, session, packet);
1662  if (endpoint->proto == COAP_PROTO_DTLS && session->type == COAP_SESSION_TYPE_HELLO && result == 1)
1663  coap_session_new_dtls_session(session, now);
1664  }
1665  }
1666 #if COAP_CONSTRAINED_STACK
1667  coap_mutex_unlock(&e_static_mutex);
1668 #endif /* COAP_CONSTRAINED_STACK */
1669  return result;
1670 }
1671 
1672 static int
1674  (void)ctx;
1675  (void)endpoint;
1676  (void)now;
1677  return 0;
1678 }
1679 
1680 static int
1682  coap_tick_t now) {
1683  coap_session_t *session = coap_new_server_session(ctx, endpoint);
1684  if (session)
1685  session->last_rx_tx = now;
1686  return session != NULL;
1687 }
1688 
1689 void
1691 #ifdef COAP_EPOLL_SUPPORT
1692  (void)ctx;
1693  (void)now;
1695  "coap_io_do_io() requires libcoap not compiled for using epoll\n");
1696 #else /* ! COAP_EPOLL_SUPPORT */
1697  coap_endpoint_t *ep, *tmp;
1698  coap_session_t *s, *rtmp;
1699 
1700  LL_FOREACH_SAFE(ctx->endpoint, ep, tmp) {
1701  if ((ep->sock.flags & COAP_SOCKET_CAN_READ) != 0)
1702  coap_read_endpoint(ctx, ep, now);
1703  if ((ep->sock.flags & COAP_SOCKET_CAN_WRITE) != 0)
1704  coap_write_endpoint(ctx, ep, now);
1705  if ((ep->sock.flags & COAP_SOCKET_CAN_ACCEPT) != 0)
1706  coap_accept_endpoint(ctx, ep, now);
1707  SESSIONS_ITER_SAFE(ep->sessions, s, rtmp) {
1708  if ((s->sock.flags & COAP_SOCKET_CAN_READ) != 0) {
1709  /* Make sure the session object is not deleted in one of the callbacks */
1711  coap_read_session(ctx, s, now);
1713  }
1714  if ((s->sock.flags & COAP_SOCKET_CAN_WRITE) != 0) {
1715  /* Make sure the session object is not deleted in one of the callbacks */
1717  coap_write_session(ctx, s, now);
1719  }
1720  }
1721  }
1722 
1723  SESSIONS_ITER_SAFE(ctx->sessions, s, rtmp) {
1724  if ((s->sock.flags & COAP_SOCKET_CAN_CONNECT) != 0) {
1725  /* Make sure the session object is not deleted in one of the callbacks */
1727  coap_connect_session(ctx, s, now);
1728  coap_session_release( s );
1729  }
1730  if ((s->sock.flags & COAP_SOCKET_CAN_READ) != 0) {
1731  /* Make sure the session object is not deleted in one of the callbacks */
1733  coap_read_session(ctx, s, now);
1735  }
1736  if ((s->sock.flags & COAP_SOCKET_CAN_WRITE) != 0) {
1737  /* Make sure the session object is not deleted in one of the callbacks */
1739  coap_write_session(ctx, s, now);
1740  coap_session_release( s );
1741  }
1742  }
1743 #endif /* ! COAP_EPOLL_SUPPORT */
1744 }
1745 
1746 /*
1747  * While this code in part replicates coap_io_do_io(), doing the functions
1748  * directly saves having to iterate through the endpoints / sessions.
1749  */
1750 void
1751 coap_io_do_epoll(coap_context_t *ctx, struct epoll_event *events, size_t nevents) {
1752 #ifndef COAP_EPOLL_SUPPORT
1753  (void)ctx;
1754  (void)events;
1755  (void)nevents;
1757  "coap_io_do_epoll() requires libcoap compiled for using epoll\n");
1758 #else /* COAP_EPOLL_SUPPORT */
1759  coap_tick_t now;
1760  size_t j;
1761 
1762  coap_ticks(&now);
1763  for(j = 0; j < nevents; j++) {
1764  coap_socket_t *sock = (coap_socket_t*)events[j].data.ptr;
1765 
1766  /* Ignore 'timer trigger' ptr which is NULL */
1767  if (sock) {
1768  if (sock->endpoint) {
1769  coap_endpoint_t *endpoint = sock->endpoint;
1770  if ((sock->flags & COAP_SOCKET_WANT_READ) &&
1771  (events[j].events & EPOLLIN)) {
1772  sock->flags |= COAP_SOCKET_CAN_READ;
1773  coap_read_endpoint(endpoint->context, endpoint, now);
1774  }
1775 
1776  if ((sock->flags & COAP_SOCKET_WANT_WRITE) &&
1777  (events[j].events & EPOLLOUT)) {
1778  /*
1779  * Need to update this to EPOLLIN as EPOLLOUT will normally always
1780  * be true causing epoll_wait to return early
1781  */
1782  coap_epoll_ctl_mod(sock, EPOLLIN, __func__);
1783  sock->flags |= COAP_SOCKET_CAN_WRITE;
1784  coap_write_endpoint(endpoint->context, endpoint, now);
1785  }
1786 
1787  if ((sock->flags & COAP_SOCKET_WANT_ACCEPT) &&
1788  (events[j].events & EPOLLIN)) {
1789  sock->flags |= COAP_SOCKET_CAN_ACCEPT;
1790  coap_accept_endpoint(endpoint->context, endpoint, now);
1791  }
1792 
1793  }
1794  else if (sock->session) {
1795  coap_session_t *session = sock->session;
1796 
1797  /* Make sure the session object is not deleted
1798  in one of the callbacks */
1799  coap_session_reference(session);
1800  if ((sock->flags & COAP_SOCKET_WANT_CONNECT) &&
1801  (events[j].events & (EPOLLOUT|EPOLLERR|EPOLLHUP|EPOLLRDHUP))) {
1802  sock->flags |= COAP_SOCKET_CAN_CONNECT;
1803  coap_connect_session(session->context, session, now);
1804  if (!(sock->flags & COAP_SOCKET_WANT_WRITE)) {
1805  coap_epoll_ctl_mod(sock, EPOLLIN, __func__);
1806  }
1807  }
1808 
1809  if ((sock->flags & COAP_SOCKET_WANT_READ) &&
1810  (events[j].events & (EPOLLIN|EPOLLERR|EPOLLHUP|EPOLLRDHUP))) {
1811  sock->flags |= COAP_SOCKET_CAN_READ;
1812  coap_read_session(session->context, session, now);
1813  }
1814 
1815  if ((sock->flags & COAP_SOCKET_WANT_WRITE) &&
1816  (events[j].events & (EPOLLOUT|EPOLLERR|EPOLLHUP|EPOLLRDHUP))) {
1817  /*
1818  * Need to update this to EPOLLIN as EPOLLOUT will normally always
1819  * be true causing epoll_wait to return early
1820  */
1821  coap_epoll_ctl_mod(sock, EPOLLIN, __func__);
1822  sock->flags |= COAP_SOCKET_CAN_WRITE;
1823  coap_write_session(session->context, session, now);
1824  }
1825  /* Now dereference session so it can go away if needed */
1826  coap_session_release(session);
1827  }
1828  }
1829  else if (ctx->eptimerfd != -1) {
1830  /*
1831  * 'timer trigger' must have fired. eptimerfd needs to be read to clear
1832  * it so that it does not set EPOLLIN in the next epoll_wait().
1833  */
1834  uint64_t count;
1835 
1836  /* Check the result from read() to suppress the warning on
1837  * systems that declare read() with warn_unused_result. */
1838  if (read(ctx->eptimerfd, &count, sizeof(count)) == -1) {
1839  /* do nothing */;
1840  }
1841  }
1842  /* And update eptimerfd as to when to next trigger */
1843  coap_ticks(&now);
1844  coap_io_prepare_epoll(ctx, now);
1845  }
1846 #endif /* COAP_EPOLL_SUPPORT */
1847 }
1848 
1849 int
1851  uint8_t *msg, size_t msg_len) {
1852 
1853  coap_pdu_t *pdu = NULL;
1854 
1855  assert(COAP_PROTO_NOT_RELIABLE(session->proto));
1856  if (msg_len < 4) {
1857  /* Minimum size of CoAP header - ignore runt */
1858  return -1;
1859  }
1860 
1861  /* Need max space incase PDU is updated with updated token etc. */
1862  pdu = coap_pdu_init(0, 0, 0, coap_session_max_pdu_size(session));
1863  if (!pdu)
1864  goto error;
1865 
1866  if (!coap_pdu_parse(session->proto, msg, msg_len, pdu)) {
1867  coap_log(LOG_WARNING, "discard malformed PDU\n");
1868  goto error;
1869  }
1870 
1871  coap_dispatch(ctx, session, pdu);
1872  coap_delete_pdu(pdu);
1873  return 0;
1874 
1875 error:
1876  /*
1877  * https://tools.ietf.org/html/rfc7252#section-4.2 MUST send RST
1878  * https://tools.ietf.org/html/rfc7252#section-4.3 MAY send RST
1879  */
1880  coap_send_rst(session, pdu);
1881  coap_delete_pdu(pdu);
1882  return -1;
1883 }
1884 #endif /* not WITH_LWIP */
1885 
1886 int
1888  coap_queue_t *p, *q;
1889 
1890  if (!queue || !*queue)
1891  return 0;
1892 
1893  /* replace queue head if PDU's time is less than head's time */
1894 
1895  if (session == (*queue)->session && id == (*queue)->id) { /* found message id */
1896  *node = *queue;
1897  *queue = (*queue)->next;
1898  if (*queue) { /* adjust relative time of new queue head */
1899  (*queue)->t += (*node)->t;
1900  }
1901  (*node)->next = NULL;
1902  coap_log(LOG_DEBUG, "** %s: mid=0x%x: removed\n",
1903  coap_session_str(session), id);
1904  return 1;
1905  }
1906 
1907  /* search message id queue to remove (only first occurence will be removed) */
1908  q = *queue;
1909  do {
1910  p = q;
1911  q = q->next;
1912  } while (q && (session != q->session || id != q->id));
1913 
1914  if (q) { /* found message id */
1915  p->next = q->next;
1916  if (p->next) { /* must update relative time of p->next */
1917  p->next->t += q->t;
1918  }
1919  q->next = NULL;
1920  *node = q;
1921  coap_log(LOG_DEBUG, "** %s: mid=0x%x: removed\n",
1922  coap_session_str(session), id);
1923  return 1;
1924  }
1925 
1926  return 0;
1927 
1928 }
1929 
1930 void
1932  coap_nack_reason_t reason) {
1933  coap_queue_t *p, *q;
1934 
1935  while (context->sendqueue && context->sendqueue->session == session) {
1936  q = context->sendqueue;
1937  context->sendqueue = q->next;
1938  coap_log(LOG_DEBUG, "** %s: mid=0x%x: removed\n",
1939  coap_session_str(session), q->id);
1940  if (q->pdu->type == COAP_MESSAGE_CON && context->nack_handler)
1941  context->nack_handler(context, session, q->pdu, reason, q->id);
1942  coap_delete_node(q);
1943  }
1944 
1945  if (!context->sendqueue)
1946  return;
1947 
1948  p = context->sendqueue;
1949  q = p->next;
1950 
1951  while (q) {
1952  if (q->session == session) {
1953  p->next = q->next;
1954  coap_log(LOG_DEBUG, "** %s: mid=0x%x: removed\n",
1955  coap_session_str(session), q->id);
1956  if (q->pdu->type == COAP_MESSAGE_CON && context->nack_handler)
1957  context->nack_handler(context, session, q->pdu, reason, q->id);
1958  coap_delete_node(q);
1959  q = p->next;
1960  } else {
1961  p = q;
1962  q = q->next;
1963  }
1964  }
1965 }
1966 
1967 void
1969  const uint8_t *token, size_t token_length) {
1970  /* cancel all messages in sendqueue that belong to session
1971  * and use the specified token */
1972  coap_queue_t *p, *q;
1973 
1974  while (context->sendqueue && context->sendqueue->session == session &&
1975  token_match(token, token_length,
1976  context->sendqueue->pdu->token,
1977  context->sendqueue->pdu->token_length)) {
1978  q = context->sendqueue;
1979  context->sendqueue = q->next;
1980  coap_log(LOG_DEBUG, "** %s: mid=0x%x: removed\n",
1981  coap_session_str(session), q->id);
1982  coap_delete_node(q);
1983  }
1984 
1985  if (!context->sendqueue)
1986  return;
1987 
1988  p = context->sendqueue;
1989  q = p->next;
1990 
1991  /* when q is not NULL, it does not match (dst, token), so we can skip it */
1992  while (q) {
1993  if (q->session == session &&
1994  token_match(token, token_length,
1995  q->pdu->token, q->pdu->token_length)) {
1996  p->next = q->next;
1997  coap_log(LOG_DEBUG, "** %s: mid=0x%x: removed\n",
1998  coap_session_str(session), q->id);
1999  coap_delete_node(q);
2000  q = p->next;
2001  } else {
2002  p = q;
2003  q = q->next;
2004  }
2005  }
2006 }
2007 
2008 coap_pdu_t *
2009 coap_new_error_response(coap_pdu_t *request, unsigned char code,
2010  coap_opt_filter_t *opts) {
2011  coap_opt_iterator_t opt_iter;
2012  coap_pdu_t *response;
2013  size_t size = request->token_length;
2014  unsigned char type;
2015  coap_opt_t *option;
2016  uint16_t opt_type = 0; /* used for calculating delta-storage */
2017 
2018 #if COAP_ERROR_PHRASE_LENGTH > 0
2019  const char *phrase;
2020  if (code != COAP_RESPONSE_CODE(508)) {
2021  phrase = coap_response_phrase(code);
2022 
2023  /* Need some more space for the error phrase and payload start marker */
2024  if (phrase)
2025  size += strlen(phrase) + 1;
2026  }
2027  else {
2028  /*
2029  * Need space for IP for 5.08 response which is filled in in coap_send()
2030  * https://www.rfc-editor.org/rfc/rfc8768.html#section-4
2031  */
2032  phrase = NULL;
2033  size += INET6_ADDRSTRLEN;
2034  }
2035 #endif
2036 
2037  assert(request);
2038 
2039  /* cannot send ACK if original request was not confirmable */
2040  type = request->type == COAP_MESSAGE_CON
2042  : COAP_MESSAGE_NON;
2043 
2044  /* Estimate how much space we need for options to copy from
2045  * request. We always need the Token, for 4.02 the unknown critical
2046  * options must be included as well. */
2047 
2048  /* we do not want these */
2051 
2052  coap_option_iterator_init(request, &opt_iter, opts);
2053 
2054  /* Add size of each unknown critical option. As known critical
2055  options as well as elective options are not copied, the delta
2056  value might grow.
2057  */
2058  while ((option = coap_option_next(&opt_iter))) {
2059  uint16_t delta = opt_iter.type - opt_type;
2060  /* calculate space required to encode (opt_iter.type - opt_type) */
2061  if (delta < 13) {
2062  size++;
2063  } else if (delta < 269) {
2064  size += 2;
2065  } else {
2066  size += 3;
2067  }
2068 
2069  /* add coap_opt_length(option) and the number of additional bytes
2070  * required to encode the option length */
2071 
2072  size += coap_opt_length(option);
2073  switch (*option & 0x0f) {
2074  case 0x0e:
2075  size++;
2076  /* fall through */
2077  case 0x0d:
2078  size++;
2079  break;
2080  default:
2081  ;
2082  }
2083 
2084  opt_type = opt_iter.type;
2085  }
2086 
2087  /* Now create the response and fill with options and payload data. */
2088  response = coap_pdu_init(type, code, request->mid, size);
2089  if (response) {
2090  /* copy token */
2091  if (!coap_add_token(response, request->token_length,
2092  request->token)) {
2093  coap_log(LOG_DEBUG, "cannot add token to error response\n");
2094  coap_delete_pdu(response);
2095  return NULL;
2096  }
2097 
2098  /* copy all options */
2099  coap_option_iterator_init(request, &opt_iter, opts);
2100  while ((option = coap_option_next(&opt_iter))) {
2101  coap_add_option(response, opt_iter.type,
2102  coap_opt_length(option),
2103  coap_opt_value(option));
2104  }
2105 
2106 #if COAP_ERROR_PHRASE_LENGTH > 0
2107  /* note that diagnostic messages do not need a Content-Format option. */
2108  if (phrase)
2109  coap_add_data(response, (size_t)strlen(phrase), (const uint8_t *)phrase);
2110 #endif
2111  }
2112 
2113  return response;
2114 }
2115 
2120 COAP_STATIC_INLINE size_t
2121 get_wkc_len(coap_context_t *context, coap_opt_t *query_filter) {
2122  unsigned char buf[1];
2123  size_t len = 0;
2124 
2125  if (coap_print_wellknown(context, buf, &len, UINT_MAX, query_filter)
2127  coap_log(LOG_WARNING, "cannot determine length of /.well-known/core\n");
2128  return 0;
2129  }
2130 
2131  coap_log(LOG_DEBUG, "get_wkc_len: coap_print_wellknown() returned %zu\n", len);
2132 
2133  return len;
2134 }
2135 
2136 #define SZX_TO_BYTES(SZX) ((size_t)(1 << ((SZX) + 4)))
2137 
2138 coap_pdu_t *
2140  coap_pdu_t *request) {
2141  coap_pdu_t *resp;
2142  coap_opt_iterator_t opt_iter;
2143  size_t len, wkc_len;
2144  uint8_t buf[4];
2145  int result = 0;
2146  int need_block2 = 0; /* set to 1 if Block2 option is required */
2147  coap_block_t block;
2148  coap_opt_t *query_filter;
2149  size_t offset = 0;
2150  uint8_t *data;
2151 
2152  resp = coap_pdu_init(request->type == COAP_MESSAGE_CON
2154  : COAP_MESSAGE_NON,
2155  COAP_RESPONSE_CODE(205),
2156  request->mid, coap_session_max_pdu_size(session));
2157  if (!resp) {
2158  coap_log(LOG_DEBUG, "coap_wellknown_response: cannot create PDU\n");
2159  return NULL;
2160  }
2161 
2162  if (!coap_add_token(resp, request->token_length, request->token)) {
2163  coap_log(LOG_DEBUG, "coap_wellknown_response: cannot add token\n");
2164  goto error;
2165  }
2166 
2167  query_filter = coap_check_option(request, COAP_OPTION_URI_QUERY, &opt_iter);
2168  wkc_len = get_wkc_len(context, query_filter);
2169 
2170  /* The value of some resources is undefined and get_wkc_len will return 0.*/
2171  if (wkc_len == 0) {
2172  coap_log(LOG_DEBUG, "coap_wellknown_response: undefined resource\n");
2173  /* set error code 4.00 Bad Request*/
2174  resp->code = COAP_RESPONSE_CODE(400);
2175  resp->used_size = resp->token_length;
2176  return resp;
2177  }
2178 
2179  /* check whether the request contains the Block2 option */
2180  if (coap_get_block(request, COAP_OPTION_BLOCK2, &block)) {
2181  coap_log(LOG_DEBUG, "create block\n");
2182  offset = block.num << (block.szx + 4);
2183  if (block.szx > 6) { /* invalid, MUST lead to 4.00 Bad Request */
2184  resp->code = COAP_RESPONSE_CODE(400);
2185  return resp;
2186  } else if (block.szx > COAP_MAX_BLOCK_SZX) {
2187  block.szx = COAP_MAX_BLOCK_SZX;
2188  block.num = (unsigned int)(offset >> (block.szx + 4));
2189  }
2190 
2191  need_block2 = 1;
2192  }
2193 
2194  /* Check if there is sufficient space to add Content-Format option
2195  * and data. We do this before adding the Content-Format option to
2196  * avoid sending error responses with that option but no actual
2197  * content. */
2198  if (resp->max_size && resp->max_size <= resp->used_size + 8) {
2199  coap_log(LOG_DEBUG, "coap_wellknown_response: insufficient storage space\n");
2200  goto error;
2201  }
2202 
2203  /* check if Block2 option is required even if not requested */
2204  if (!need_block2 && resp->max_size && resp->max_size - resp->used_size < wkc_len + 1) {
2205  assert(resp->used_size <= resp->max_size);
2206  const size_t payloadlen = resp->max_size - resp->used_size;
2207  /* yes, need block-wise transfer */
2208  block.num = 0;
2209  block.m = 0; /* the M bit is set by coap_write_block_opt() */
2210  block.szx = COAP_MAX_BLOCK_SZX;
2211  while (payloadlen < SZX_TO_BYTES(block.szx) + 6) {
2212  if (block.szx == 0) {
2214  "coap_wellknown_response: message to small even for szx == 0\n");
2215  goto error;
2216  } else {
2217  block.szx--;
2218  }
2219  }
2220 
2221  need_block2 = 1;
2222  }
2223 
2224  if (need_block2) {
2225  /* Add in a pseudo etag (use wkc_len) in case .well-known/core
2226  changes over time */
2227  coap_add_option(resp,
2229  coap_encode_var_safe8(buf, sizeof(buf), wkc_len),
2230  buf);
2231  }
2232 
2233  /* Add Content-Format. As we have checked for available storage,
2234  * nothing should go wrong here. */
2235  assert(coap_encode_var_safe(buf, sizeof(buf),
2238  coap_encode_var_safe(buf, sizeof(buf),
2240 
2241 
2242  /* write Block2 option if necessary */
2243  if (need_block2) {
2244  if (coap_write_block_opt(&block, COAP_OPTION_BLOCK2, resp, wkc_len) < 0) {
2246  "coap_wellknown_response: cannot add Block2 option\n");
2247  goto error;
2248  }
2249  }
2250 
2251  coap_add_option(resp,
2253  coap_encode_var_safe8(buf, sizeof(buf), wkc_len),
2254  buf);
2255 
2256  len = need_block2 ?
2257  min(SZX_TO_BYTES(block.szx), wkc_len - (block.num << (block.szx + 4))) :
2258  resp->max_size && resp->used_size + wkc_len + 1 > resp->max_size ?
2259  resp->max_size - resp->used_size - 1 : wkc_len;
2260  data = coap_add_data_after(resp, len);
2261  if (!data) {
2262  coap_log(LOG_DEBUG, "coap_wellknown_response: coap_add_data failed\n" );
2263  goto error;
2264  }
2265 
2266  result = coap_print_wellknown(context, data, &len, offset, query_filter);
2267  if ((result & COAP_PRINT_STATUS_ERROR) != 0) {
2268  coap_log(LOG_DEBUG, "coap_print_wellknown failed\n");
2269  goto error;
2270  }
2271 
2272  return resp;
2273 
2274 error:
2275  /* set error code 5.03 and remove all options and data from response */
2276  resp->code = COAP_RESPONSE_CODE(503);
2277  resp->used_size = resp->token_length;
2278  return resp;
2279 }
2280 
2291 static int
2292 coap_cancel(coap_context_t *context, const coap_queue_t *sent) {
2293  coap_binary_t token = { 0, NULL };
2294  int num_cancelled = 0; /* the number of observers cancelled */
2295 
2296  /* remove observer for this resource, if any
2297  * get token from sent and try to find a matching resource. Uh!
2298  */
2299 
2300  COAP_SET_STR(&token, sent->pdu->token_length, sent->pdu->token);
2301 
2302  RESOURCES_ITER(context->resources, r) {
2303  coap_cancel_all_messages(context, sent->session, token.s, token.length);
2304  num_cancelled += coap_delete_observer(r, sent->session, &token);
2305  }
2306 
2307  return num_cancelled;
2308 }
2309 
2315 
2316 /*
2317  * Checks for No-Response option in given @p request and
2318  * returns @c RESPONSE_DROP if @p response should be suppressed
2319  * according to RFC 7967.
2320  *
2321  * If the response is a confirmable piggybacked response and RESPONSE_DROP,
2322  * change it to an empty ACK and @c RESPONSE_SEND so the client does not keep
2323  * on retrying.
2324  *
2325  * Checks if the response code is 0.00 and if either the session is reliable or
2326  * non-confirmable, @c RESPONSE_DROP is also returned.
2327  *
2328  * Multicast response checking is also carried out.
2329  *
2330  * NOTE: It is the responsibility of the application to determine whether
2331  * a delayed separate response should be sent as the original requesting packet
2332  * containing the No-Response option has long since gone.
2333  *
2334  * The value of the No-Response option is encoded as
2335  * follows:
2336  *
2337  * @verbatim
2338  * +-------+-----------------------+-----------------------------------+
2339  * | Value | Binary Representation | Description |
2340  * +-------+-----------------------+-----------------------------------+
2341  * | 0 | <empty> | Interested in all responses. |
2342  * +-------+-----------------------+-----------------------------------+
2343  * | 2 | 00000010 | Not interested in 2.xx responses. |
2344  * +-------+-----------------------+-----------------------------------+
2345  * | 8 | 00001000 | Not interested in 4.xx responses. |
2346  * +-------+-----------------------+-----------------------------------+
2347  * | 16 | 00010000 | Not interested in 5.xx responses. |
2348  * +-------+-----------------------+-----------------------------------+
2349  * @endverbatim
2350  *
2351  * @param request The CoAP request to check for the No-Response option.
2352  * This parameter must not be NULL.
2353  * @param response The response that is potentially suppressed.
2354  * This parameter must not be NULL.
2355  * @param session The session this request/response are associated with.
2356  * This parameter must not be NULL.
2357  * @return RESPONSE_DEFAULT when no special treatment is requested,
2358  * RESPONSE_DROP when the response must be discarded, or
2359  * RESPONSE_SEND when the response must be sent.
2360  */
2361 static enum respond_t
2362 no_response(coap_pdu_t *request, coap_pdu_t *response,
2363  coap_session_t *session) {
2364  coap_opt_t *nores;
2365  coap_opt_iterator_t opt_iter;
2366  unsigned int val = 0;
2367 
2368  assert(request);
2369  assert(response);
2370 
2371  if (COAP_RESPONSE_CLASS(response->code) > 0) {
2372  nores = coap_check_option(request, COAP_OPTION_NORESPONSE, &opt_iter);
2373 
2374  if (nores) {
2376 
2377  /* The response should be dropped when the bit corresponding to
2378  * the response class is set (cf. table in function
2379  * documentation). When a No-Response option is present and the
2380  * bit is not set, the sender explicitly indicates interest in
2381  * this response. */
2382  if (((1 << (COAP_RESPONSE_CLASS(response->code) - 1)) & val) > 0) {
2383  /* Should be dropping the response */
2384  if (response->type == COAP_MESSAGE_ACK &&
2385  COAP_PROTO_NOT_RELIABLE(session->proto)) {
2386  /* Still need to ACK the request */
2387  response->code = 0;
2388  /* Remove token/data from piggybacked acknowledgment PDU */
2389  response->token_length = 0;
2390  response->used_size = 0;
2391  return RESPONSE_SEND;
2392  }
2393  else {
2394  return RESPONSE_DROP;
2395  }
2396  } else {
2397  /* True for mcast as well RFC7967 2.1 */
2398  return RESPONSE_SEND;
2399  }
2400  }
2401  }
2402  else if (COAP_PDU_IS_EMPTY(response) &&
2403  (response->type == COAP_MESSAGE_NON ||
2404  COAP_PROTO_RELIABLE(session->proto))) {
2405  /* response is 0.00, and this is reliable or non-confirmable */
2406  return RESPONSE_DROP;
2407  }
2408 
2409  /*
2410  * Do not send error responses for requests that were received via
2411  * IP multicast. RFC7252 8.1
2412  */
2413 
2414  if (coap_is_mcast(&session->addr_info.local)) {
2415  if (request->type == COAP_MESSAGE_NON &&
2416  response->type == COAP_MESSAGE_RST)
2417  return RESPONSE_DROP;
2418 
2419  if (COAP_RESPONSE_CLASS(response->code) > 2)
2420  return RESPONSE_DROP;
2421  }
2422 
2423  /* Default behavior applies when we are not dealing with a response
2424  * (class == 0) or the request did not contain a No-Response option.
2425  */
2426  return RESPONSE_DEFAULT;
2427 }
2428 
2430  { sizeof(COAP_DEFAULT_URI_WELLKNOWN)-1,
2431  (const uint8_t *)COAP_DEFAULT_URI_WELLKNOWN };
2432 
2433 static void
2435  coap_method_handler_t h = NULL;
2436  coap_pdu_t *response = NULL;
2437  coap_opt_filter_t opt_filter;
2438  coap_resource_t *resource = NULL;
2439  /* The respond field indicates whether a response must be treated
2440  * specially due to a No-Response option that declares disinterest
2441  * or interest in a specific response class. DEFAULT indicates that
2442  * No-Response has not been specified. */
2443  enum respond_t respond = RESPONSE_DEFAULT;
2444  coap_opt_iterator_t opt_iter;
2445  coap_opt_t *opt;
2446  int is_proxy_uri = 0;
2447  int is_proxy_scheme = 0;
2448  int skip_hop_limit_check = 0;
2449  int resp;
2450 
2451  coap_option_filter_clear(&opt_filter);
2452  opt = coap_check_option(pdu, COAP_OPTION_PROXY_SCHEME, &opt_iter);
2453  if (opt)
2454  is_proxy_scheme = 1;
2455 
2456  opt = coap_check_option(pdu, COAP_OPTION_PROXY_URI, &opt_iter);
2457  if (opt)
2458  is_proxy_uri = 1;
2459 
2460  if (is_proxy_scheme || is_proxy_uri) {
2461  coap_uri_t uri;
2462 
2463  if (!context->proxy_uri_resource) {
2464  /* Need to return a 5.05 RFC7252 Section 5.7.2 */
2465  coap_log(LOG_DEBUG, "Proxy-%s support not configured\n",
2466  is_proxy_scheme ? "Scheme" : "Uri");
2467  resp = 505;
2468  goto fail_response;
2469  }
2470  if (((size_t)pdu->code - 1 <
2471  (sizeof(resource->handler) / sizeof(resource->handler[0]))) &&
2472  !(context->proxy_uri_resource->handler[pdu->code - 1])) {
2473  /* Need to return a 5.05 RFC7252 Section 5.7.2 */
2474  coap_log(LOG_DEBUG, "Proxy-%s code %d.%02d handler not supported\n",
2475  is_proxy_scheme ? "Scheme" : "Uri",
2476  pdu->code/100, pdu->code%100);
2477  resp = 505;
2478  goto fail_response;
2479  }
2480 
2481  /* Need to check if authority is the proxy endpoint RFC7252 Section 5.7.2 */
2482  if (is_proxy_uri) {
2484  coap_opt_length(opt), &uri) < 0) {
2485  /* Need to return a 5.05 RFC7252 Section 5.7.2 */
2486  coap_log(LOG_DEBUG, "Proxy-URI not decodable\n");
2487  resp = 505;
2488  goto fail_response;
2489  }
2490  }
2491  else {
2492  memset(&uri, 0, sizeof(uri));
2493  opt = coap_check_option(pdu, COAP_OPTION_URI_HOST, &opt_iter);
2494  if (opt) {
2495  uri.host.length = coap_opt_length(opt);
2496  uri.host.s = coap_opt_value(opt);
2497  }
2498  }
2499  resource = context->proxy_uri_resource;
2500  if (uri.host.length && resource->proxy_name_count && resource->proxy_name_list) {
2501  size_t i;
2502  for (i = 0; i < resource->proxy_name_count; i++) {
2503  if (coap_string_equal(&uri.host, resource->proxy_name_list[i])) {
2504  break;
2505  }
2506  }
2507  if (i != resource->proxy_name_count) {
2508  /* This server is hosting the proxy connection endpoint */
2509  is_proxy_uri = 0;
2510  is_proxy_scheme = 0;
2511  skip_hop_limit_check = 1;
2512  }
2513  }
2514  resource = NULL;
2515  }
2516 
2517  if (!skip_hop_limit_check) {
2518  opt = coap_check_option(pdu, COAP_OPTION_HOP_LIMIT, &opt_iter);
2519  if (opt) {
2520  size_t hop_limit;
2521  uint8_t buf[4];
2522 
2523  hop_limit =
2525  if (hop_limit == 1) {
2526  /* coap_send() will fill in the IP address for us */
2527  resp = 508;
2528  goto fail_response;
2529  }
2530  else if (hop_limit < 1 || hop_limit > 255) {
2531  /* Need to return a 4.00 RFC8768 Section 3 */
2532  coap_log(LOG_INFO, "Invalid Hop Limit\n");
2533  resp = 400;
2534  goto fail_response;
2535  }
2536  hop_limit--;
2538  coap_encode_var_safe8(buf, sizeof(buf), hop_limit),
2539  buf);
2540  }
2541  }
2542 
2543  coap_string_t *uri_path = coap_get_uri_path(pdu);
2544  if (!uri_path)
2545  return;
2546 
2547  if (!is_proxy_uri && !is_proxy_scheme) {
2548  /* try to find the resource from the request URI */
2549  coap_str_const_t uri_path_c = { uri_path->length, uri_path->s };
2550  resource = coap_get_resource_from_uri_path(context, &uri_path_c);
2551  }
2552 
2553  if ((resource == NULL) || (resource->is_unknown == 1) ||
2554  (resource->is_proxy_uri == 1)) {
2555  /* The resource was not found or there is an unexpected match against the
2556  * resource defined for handling unknown or proxy URIs.
2557  * Check if the request URI happens to be the well-known URI, or if the
2558  * unknown resource handler is defined, a PUT or optionally other methods,
2559  * if configured, for the unknown handler.
2560  *
2561  * if well-known URI generate a default response
2562  *
2563  * else if a PROXY URI/Scheme request and proxy URI handler defined, call the
2564  * proxy URI handler
2565  *
2566  * else if unknown URI handler defined, call the unknown
2567  * URI handler (to allow for potential generation of resource
2568  * [RFC7272 5.8.3]) if the appropriate method is defined.
2569  *
2570  * else if DELETE return 2.02 (RFC7252: 5.8.4. DELETE)
2571  *
2572  * else return 4.04 */
2573 
2575  /* request for .well-known/core */
2576  if (pdu->code == COAP_REQUEST_GET) { /* GET */
2577  coap_log(LOG_INFO, "create default response for %s\n",
2579  response = coap_wellknown_response(context, session, pdu);
2580  } else {
2581  coap_log(LOG_DEBUG, "method not allowed for .well-known/core\n");
2582  response = coap_new_error_response(pdu, COAP_RESPONSE_CODE(405),
2583  &opt_filter);
2584  }
2585  } else if (is_proxy_uri || is_proxy_scheme) {
2586  resource = context->proxy_uri_resource;
2587  } else if ((context->unknown_resource != NULL) &&
2588  ((size_t)pdu->code - 1 <
2589  (sizeof(resource->handler) / sizeof(coap_method_handler_t))) &&
2590  (context->unknown_resource->handler[pdu->code - 1])) {
2591  /*
2592  * The unknown_resource can be used to handle undefined resources
2593  * for a PUT request and can support any other registered handler
2594  * defined for it
2595  * Example set up code:-
2596  * r = coap_resource_unknown_init(hnd_put_unknown);
2597  * coap_register_handler(r, COAP_REQUEST_POST, hnd_post_unknown);
2598  * coap_register_handler(r, COAP_REQUEST_GET, hnd_get_unknown);
2599  * coap_register_handler(r, COAP_REQUEST_DELETE, hnd_delete_unknown);
2600  * coap_add_resource(ctx, r);
2601  *
2602  * Note: It is not possible to observe the unknown_resource, a separate
2603  * resource must be created (by PUT or POST) which has a GET
2604  * handler to be observed
2605  */
2606  resource = context->unknown_resource;
2607  } else if (pdu->code == COAP_REQUEST_DELETE) {
2608  /*
2609  * Request for DELETE on non-existant resource (RFC7252: 5.8.4. DELETE)
2610  */
2611  coap_log(LOG_DEBUG, "request for unknown resource '%*.*s',"
2612  " return 2.02\n",
2613  (int)uri_path->length,
2614  (int)uri_path->length,
2615  uri_path->s);
2616  response =
2618  &opt_filter);
2619  } else { /* request for any another resource, return 4.04 */
2620 
2621  coap_log(LOG_DEBUG, "request for unknown resource '%*.*s', return 4.04\n",
2622  (int)uri_path->length, (int)uri_path->length, uri_path->s);
2623  response =
2625  &opt_filter);
2626  }
2627 
2628  if (!resource) {
2629  if (response && (no_response(pdu, response, session) != RESPONSE_DROP)) {
2630  coap_mid_t mid = pdu->mid;
2631  if (coap_send(session, response) == COAP_INVALID_MID)
2632  coap_log(LOG_WARNING, "cannot send response for mid=0x%x\n", mid);
2633  } else {
2634  coap_delete_pdu(response);
2635  }
2636 
2637  response = NULL;
2638 
2639  coap_delete_string(uri_path);
2640  return;
2641  } else {
2642  if (response) {
2643  /* Need to delete unused response - it will get re-created further on */
2644  coap_delete_pdu(response);
2645  }
2646  }
2647  }
2648 
2649  /* the resource was found, check if there is a registered handler */
2650  if ((size_t)pdu->code - 1 <
2651  sizeof(resource->handler) / sizeof(coap_method_handler_t))
2652  h = resource->handler[pdu->code - 1];
2653 
2654  if (h) {
2655  coap_log(LOG_DEBUG, "call custom handler for resource '%*.*s'\n",
2656  (int)resource->uri_path->length, (int)resource->uri_path->length,
2657  resource->uri_path->s);
2658  response = coap_pdu_init(pdu->type == COAP_MESSAGE_CON
2660  : COAP_MESSAGE_NON,
2661  0, pdu->mid, coap_session_max_pdu_size(session));
2662 
2663  /* Implementation detail: coap_add_token() immediately returns 0
2664  if response == NULL */
2665  if (coap_add_token(response, pdu->token_length, pdu->token)) {
2666  coap_binary_t token = { pdu->token_length, pdu->token };
2667  coap_opt_t *observe = NULL;
2668  int observe_action = COAP_OBSERVE_CANCEL;
2669  coap_string_t *query = coap_get_query(pdu);
2670  int owns_query = 1;
2671  coap_block_t block;
2672  int added_block = 0;
2673 
2674  /* check for Observe option RFC7641 and RFC8132 */
2675  if (resource->observable &&
2676  (pdu->code == COAP_REQUEST_GET || pdu->code == COAP_REQUEST_FETCH)) {
2677  observe = coap_check_option(pdu, COAP_OPTION_OBSERVE, &opt_iter);
2678  if (observe) {
2679  observe_action =
2681  coap_opt_length(observe));
2682 
2683  if (observe_action == COAP_OBSERVE_ESTABLISH) {
2684  coap_subscription_t *subscription;
2685  int has_block2 = 0;
2686 
2687  if (coap_get_block(pdu, COAP_OPTION_BLOCK2, &block)) {
2688  has_block2 = 1;
2689  }
2690  subscription = coap_add_observer(resource, session, &token,
2691  query, has_block2,
2692  block, pdu->code);
2693  if (subscription) {
2694  /* Ownership of query is taken by subscription if not
2695  * NULL. In this case, we must not delete query here
2696  * hence owns_query is cleared. */
2697  owns_query = 0;
2698  coap_touch_observer(context, session, &token);
2699  }
2700  }
2701  else if (observe_action == COAP_OBSERVE_CANCEL) {
2702  coap_delete_observer(resource, session, &token);
2703  }
2704  else {
2705  coap_log(LOG_INFO, "observe: unexpected action %d\n", observe_action);
2706  }
2707  }
2708  }
2709 
2710  if (session->block_mode & COAP_BLOCK_USE_LIBCOAP) {
2711  if (coap_handle_request_put_block(context, session, pdu, response,
2712  resource, uri_path, observe, &token,
2713  query, h, &added_block)) {
2714  goto skip_handler;
2715  }
2716 
2717  if (coap_handle_request_send_block(session, pdu, response, resource,
2718  query)) {
2719  goto skip_handler;
2720  }
2721  }
2722 
2723  /*
2724  * Call the request handler with everything set up
2725  */
2726  h(context, resource, session, pdu, &token, query, response);
2727 
2728  /* Check if lg_xmit generated and update PDU code if so */
2729  coap_check_code_lg_xmit(session, response, resource, query);
2730 
2731 skip_handler:
2732  respond = no_response(pdu, response, session);
2733  if (respond != RESPONSE_DROP) {
2734  coap_mid_t mid = pdu->mid;
2735  if (COAP_RESPONSE_CLASS(response->code) > 2) {
2736  if (observe)
2737  coap_delete_observer(resource, session, &token);
2738  if (added_block)
2740  }
2741 
2742  /* If original request contained a token, and the registered
2743  * application handler made no changes to the response, then
2744  * this is an empty ACK with a token, which is a malformed
2745  * PDU */
2746  if ((response->type == COAP_MESSAGE_ACK)
2747  && (response->code == 0)) {
2748  /* Remove token from otherwise-empty acknowledgment PDU */
2749  response->token_length = 0;
2750  response->used_size = 0;
2751  }
2752 
2753  if (coap_send(session, response) == COAP_INVALID_MID) {
2754  coap_log(LOG_DEBUG, "cannot send response for mid=0x%x\n", mid);
2755  }
2756  } else {
2757  coap_delete_pdu(response);
2758  }
2759  if (query && owns_query)
2760  coap_delete_string(query);
2761  } else {
2762  coap_log(LOG_WARNING, "cannot generate response\r\n");
2763  coap_delete_pdu(response);
2764  }
2765  response = NULL;
2766  } else {
2768  /* request for .well-known/core */
2769  coap_log(LOG_DEBUG, "create default response for %s\n",
2771  response = coap_wellknown_response(context, session, pdu);
2772  coap_log(LOG_DEBUG, "have wellknown response %p\n", (void *)response);
2773  } else
2774  response = coap_new_error_response(pdu, COAP_RESPONSE_CODE(405),
2775  &opt_filter);
2776 
2777  if (response && (no_response(pdu, response, session) != RESPONSE_DROP)) {
2778  coap_mid_t mid = pdu->mid;
2779  if (coap_send(session, response) == COAP_INVALID_MID)
2780  coap_log(LOG_DEBUG, "cannot send response for mid=0x%x\n", mid);
2781  } else {
2782  coap_delete_pdu(response);
2783  }
2784  response = NULL;
2785  }
2786 
2787  assert(response == NULL);
2788  coap_delete_string(uri_path);
2789  return;
2790 
2791 fail_response:
2792  response =
2794  &opt_filter);
2795  if (response) {
2796  coap_mid_t mid = pdu->mid;
2797  if (coap_send(session, response) == COAP_INVALID_MID)
2798  coap_log(LOG_WARNING, "cannot send response for mid=0x%x\n", mid);
2799  }
2800 }
2801 
2802 static void
2804  coap_pdu_t *sent, coap_pdu_t *rcvd) {
2805 
2806  /* In a lossy context, the ACK of a separate response may have
2807  * been lost, so we need to stop retransmitting requests with the
2808  * same token.
2809  */
2810  coap_cancel_all_messages(context, session, rcvd->token, rcvd->token_length);
2811 
2812  if (session->block_mode & COAP_BLOCK_USE_LIBCOAP) {
2813  /* See if need to send next block to server */
2814  if (coap_handle_response_send_block(session, rcvd)) {
2815  /* Next block transmitted, no need to inform app */
2816  coap_send_ack(session, rcvd);
2817  return;
2818  }
2819 
2820  /* Need to see if needing to request next block */
2821  if (coap_handle_response_get_block(context, session, sent, rcvd,
2822  COAP_RECURSE_OK)) {
2823  /* Next block requested, no need to inform app */
2824  coap_send_ack(session, rcvd);
2825  return;
2826  }
2827  }
2828 
2829  /* Call application-specific response handler when available. */
2830  if (context->response_handler) {
2831  if (context->response_handler(context, session, sent, rcvd,
2832  rcvd->mid) == COAP_RESPONSE_FAIL)
2833  coap_send_rst(session, rcvd);
2834  else
2835  coap_send_ack(session, rcvd);
2836  }
2837  else {
2838  coap_send_ack(session, rcvd);
2839  }
2840 }
2841 
2842 #if !COAP_DISABLE_TCP
2843 static void
2845  coap_pdu_t *pdu) {
2846  coap_opt_iterator_t opt_iter;
2847  coap_opt_t *option;
2848  (void)context;
2849 
2850  coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL);
2851 
2852  if (pdu->code == COAP_SIGNALING_CSM) {
2853  while ((option = coap_option_next(&opt_iter))) {
2854  if (opt_iter.type == COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE) {
2856  coap_opt_length(option)));
2857  } else if (opt_iter.type == COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER) {
2858  session->csm_block_supported = 1;
2859  }
2860  }
2861  if (session->state == COAP_SESSION_STATE_CSM)
2862  coap_session_connected(session);
2863  } else if (pdu->code == COAP_SIGNALING_PING) {
2865  if (context->ping_handler) {
2866  context->ping_handler(context, session, pdu, pdu->mid);
2867  }
2868  if (pong) {
2870  coap_send(session, pong);
2871  }
2872  } else if (pdu->code == COAP_SIGNALING_PONG) {
2873  session->last_pong = session->last_rx_tx;
2874  if (context->pong_handler) {
2875  context->pong_handler(context, session, pdu, pdu->mid);
2876  }
2877  } else if (pdu->code == COAP_SIGNALING_RELEASE
2878  || pdu->code == COAP_SIGNALING_ABORT) {
2880  }
2881 }
2882 #endif /* !COAP_DISABLE_TCP */
2883 
2884 void
2886  coap_pdu_t *pdu) {
2887  coap_queue_t *sent = NULL;
2888  coap_pdu_t *response;
2889  coap_opt_filter_t opt_filter;
2890  int is_ping_rst;
2891 
2892  if (LOG_DEBUG <= coap_get_log_level()) {
2893  /* FIXME: get debug to work again **
2894  unsigned char addr[INET6_ADDRSTRLEN+8], localaddr[INET6_ADDRSTRLEN+8];
2895  if (coap_print_addr(remote, addr, INET6_ADDRSTRLEN+8) &&
2896  coap_print_addr(&packet->dst, localaddr, INET6_ADDRSTRLEN+8) )
2897  coap_log(LOG_DEBUG, "** received %d bytes from %s on interface %s:\n",
2898  (int)msg_len, addr, localaddr);
2899 
2900  */
2901  coap_show_pdu(LOG_DEBUG, pdu);
2902  }
2903 
2904  memset(&opt_filter, 0, sizeof(coap_opt_filter_t));
2905 
2906  switch (pdu->type) {
2907  case COAP_MESSAGE_ACK:
2908  /* find message id in sendqueue to stop retransmission */
2909  coap_remove_from_queue(&context->sendqueue, session, pdu->mid, &sent);
2910 
2911  if (sent && session->con_active) {
2912  session->con_active--;
2913  if (session->state == COAP_SESSION_STATE_ESTABLISHED)
2914  /* Flush out any entries on session->delayqueue */
2915  coap_session_connected(session);
2916  }
2917  if (coap_option_check_critical(context, pdu, opt_filter) == 0)
2918  goto cleanup;
2919 
2920  /* if sent code was >= 64 the message might have been a
2921  * notification. Then, we must flag the observer to be alive
2922  * by setting obs->fail_cnt = 0. */
2923  if (sent && COAP_RESPONSE_CLASS(sent->pdu->code) == 2) {
2924  const coap_binary_t token =
2925  { sent->pdu->token_length, sent->pdu->token };
2926  coap_touch_observer(context, sent->session, &token);
2927  }
2928 
2929  if (pdu->code == 0 && sent) {
2930  /* an empty ACK needs no further handling */
2931  goto cleanup;
2932  }
2933 
2934  break;
2935 
2936  case COAP_MESSAGE_RST:
2937  /* We have sent something the receiver disliked, so we remove
2938  * not only the message id but also the subscriptions we might
2939  * have. */
2940 
2941  is_ping_rst = 0;
2942  if (pdu->mid == session->last_ping_mid &&
2943  context->ping_timeout && session->last_ping > 0)
2944  is_ping_rst = 1;
2945 
2946  if (!is_ping_rst)
2947  coap_log(LOG_ALERT, "got RST for mid=0x%x\n", pdu->mid);
2948 
2949  if (session->con_active) {
2950  session->con_active--;
2951  if (session->state == COAP_SESSION_STATE_ESTABLISHED)
2952  /* Flush out any entries on session->delayqueue */
2953  coap_session_connected(session);
2954  }
2955 
2956  /* find message id in sendqueue to stop retransmission */
2957  coap_remove_from_queue(&context->sendqueue, session, pdu->mid, &sent);
2958 
2959  if (sent) {
2960  coap_cancel(context, sent);
2961 
2962  if (!is_ping_rst) {
2963  if(sent->pdu->type==COAP_MESSAGE_CON && context->nack_handler)
2964  context->nack_handler(context, sent->session, sent->pdu,
2965  COAP_NACK_RST, sent->id);
2966  }
2967  else {
2968  if (context->pong_handler) {
2969  context->pong_handler(context, session, pdu, pdu->mid);
2970  }
2971  session->last_pong = session->last_rx_tx;
2972  session->last_ping_mid = COAP_INVALID_MID;
2973  }
2974  }
2975  else {
2976  /* Need to check is there is a subscription active and delete it */
2977  RESOURCES_ITER(context->resources, r) {
2978  coap_subscription_t *obs, *tmp;
2979  LL_FOREACH_SAFE(r->subscribers, obs, tmp) {
2980  if (obs->mid == pdu->mid && obs->session == session) {
2981  coap_binary_t token = { 0, NULL };
2982  COAP_SET_STR(&token, obs->token_length, obs->token);
2983  coap_delete_observer(r, session, &token);
2984  goto cleanup;
2985  }
2986  }
2987  }
2988  }
2989  goto cleanup;
2990 
2991  case COAP_MESSAGE_NON:
2992  /* find transaction in sendqueue in case large response */
2993  coap_remove_from_queue(&context->sendqueue, session, pdu->mid, &sent);
2994  /* check for unknown critical options */
2995  if (coap_option_check_critical(context, pdu, opt_filter) == 0) {
2996  coap_send_rst(session, pdu);
2997  goto cleanup;
2998  }
2999  break;
3000 
3001  case COAP_MESSAGE_CON: /* check for unknown critical options */
3002  if (coap_option_check_critical(context, pdu, opt_filter) == 0) {
3003 
3004  if (COAP_PDU_IS_REQUEST(pdu)) {
3005  response =
3006  coap_new_error_response(pdu, COAP_RESPONSE_CODE(402), &opt_filter);
3007 
3008  if (!response) {
3010  "coap_dispatch: cannot create error response\n");
3011  } else {
3012  if (coap_send(session, response) == COAP_INVALID_MID)
3013  coap_log(LOG_WARNING, "coap_dispatch: error sending response\n");
3014  }
3015  }
3016  else {
3017  coap_send_rst(session, pdu);
3018  }
3019 
3020  goto cleanup;
3021  }
3022  default: break;
3023  }
3024 
3025  /* Pass message to upper layer if a specific handler was
3026  * registered for a request that should be handled locally. */
3027 #if !COAP_DISABLE_TCP
3028  if (COAP_PDU_IS_SIGNALING(pdu))
3029  handle_signaling(context, session, pdu);
3030  else
3031 #endif /* !COAP_DISABLE_TCP */
3032  if (COAP_PDU_IS_REQUEST(pdu))
3033  handle_request(context, session, pdu);
3034  else if (COAP_PDU_IS_RESPONSE(pdu))
3035  handle_response(context, session, sent ? sent->pdu : NULL, pdu);
3036  else {
3037  if (COAP_PDU_IS_EMPTY(pdu)) {
3038  if (context->ping_handler) {
3039  context->ping_handler(context, session,
3040  pdu, pdu->mid);
3041  }
3042  }
3043  coap_log(LOG_DEBUG, "dropped message with invalid code (%d.%02d)\n",
3044  COAP_RESPONSE_CLASS(pdu->code),
3045  pdu->code & 0x1f);
3046 
3047  if (!coap_is_mcast(&session->addr_info.local)) {
3048  if (COAP_PDU_IS_EMPTY(pdu)) {
3049  if (session->proto != COAP_PROTO_TCP && session->proto != COAP_PROTO_TLS) {
3050  coap_tick_t now;
3051  coap_ticks(&now);
3052  if (session->last_tx_rst + COAP_TICKS_PER_SECOND/4 < now) {
3054  session->last_tx_rst = now;
3055  }
3056  }
3057  }
3058  else {
3060  }
3061  }
3062  }
3063 
3064 cleanup:
3065  coap_delete_node(sent);
3066 }
3067 
3068 int
3070  coap_log(LOG_DEBUG, "***EVENT: 0x%04x\n", event);
3071 
3072  if (context->handle_event) {
3073  return context->handle_event(context, event, session);
3074  } else {
3075  return 0;
3076  }
3077 }
3078 
3079 int
3081  coap_endpoint_t *ep;
3082  coap_session_t *s, *rtmp;
3083  if (!context)
3084  return 1;
3085  if (context->sendqueue)
3086  return 0;
3087  LL_FOREACH(context->endpoint, ep) {
3088  SESSIONS_ITER(ep->sessions, s, rtmp) {
3089  if (s->delayqueue)
3090  return 0;
3091  if (s->lg_xmit)
3092  return 0;
3093  }
3094  }
3095  SESSIONS_ITER(context->sessions, s, rtmp) {
3096  if (s->delayqueue)
3097  return 0;
3098  if (s->lg_xmit)
3099  return 0;
3100  }
3101  return 1;
3102 }
3103 
3104 static int coap_started = 0;
3105 
3106 void coap_startup(void) {
3107  coap_tick_t now;
3108  uint64_t us;
3109  if (coap_started)
3110  return;
3111  coap_started = 1;
3112 #if defined(HAVE_WINSOCK2_H)
3113  WORD wVersionRequested = MAKEWORD(2, 2);
3114  WSADATA wsaData;
3115  WSAStartup(wVersionRequested, &wsaData);
3116 #endif
3117  coap_clock_init();
3118  coap_ticks(&now);
3119  us = coap_ticks_to_rt_us(now);
3120  /* Be accurate to the nearest (approx) us */
3121  coap_prng_init((unsigned int)us);
3122  coap_memory_init();
3124 }
3125 
3126 void coap_cleanup(void) {
3127 #if defined(HAVE_WINSOCK2_H)
3128  WSACleanup();
3129 #endif
3131 }
3132 
3133 #if ! defined WITH_CONTIKI && ! defined WITH_LWIP && ! defined RIOT_VERSION
3134 int
3135 coap_join_mcast_group_intf(coap_context_t *ctx, const char *group_name,
3136  const char *ifname) {
3137  struct ip_mreq mreq4;
3138  struct ipv6_mreq mreq6;
3139  struct addrinfo *resmulti = NULL, hints, *ainfo;
3140  int result = -1;
3141  coap_endpoint_t *endpoint;
3142  int mgroup_setup = 0;
3143 
3144  /* Need to have at least one endpoint! */
3145  assert(ctx->endpoint);
3146  if (!ctx->endpoint)
3147  return -1;
3148 
3149  /* Default is let the kernel choose */
3150  mreq6.ipv6mr_interface = 0;
3151  mreq4.imr_interface.s_addr = INADDR_ANY;
3152 
3153  memset(&hints, 0, sizeof(hints));
3154  hints.ai_socktype = SOCK_DGRAM;
3155 
3156  /* resolve the multicast group address */
3157  result = getaddrinfo(group_name, NULL, &hints, &resmulti);
3158 
3159  if (result != 0) {
3160  coap_log(LOG_ERR,
3161  "coap_join_mcast_group_intf: %s: "
3162  "Cannot resolve multicast address: %s\n",
3163  group_name, gai_strerror(result));
3164  goto finish;
3165  }
3166 
3167 /* Need to do a windows equivalent at some point */
3168 #ifndef _WIN32
3169  if (ifname) {
3170  /* interface specified - check if we have correct IPv4/IPv6 information */
3171  int done_ip4 = 0;
3172  int done_ip6 = 0;
3173  int ip4fd;
3174  struct ifreq ifr;
3175 
3176  /* See which mcast address family types are being asked for */
3177  for (ainfo = resmulti; ainfo != NULL && !(done_ip4 == 1 && done_ip6 == 1);
3178  ainfo = ainfo->ai_next) {
3179  switch (ainfo->ai_family) {
3180  case AF_INET6:
3181  if (done_ip6)
3182  break;
3183  done_ip6 = 1;
3184  memset (&ifr, 0, sizeof(ifr));
3185  strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
3186  ifr.ifr_name[IFNAMSIZ - 1] = '\000';
3187 
3188  result = ioctl(ctx->endpoint->sock.fd, SIOCGIFINDEX, &ifr);
3189  if (result != 0) {
3190  coap_log(LOG_WARNING, "coap_join_mcast_group_intf: "
3191  "cannot get interface index for '%s': %s\n",
3192  ifname, coap_socket_strerror());
3193  }
3194  else {
3195  /* Capture the IPv6 if_index for later */
3196  mreq6.ipv6mr_interface = ifr.ifr_ifindex;
3197  }
3198  break;
3199  case AF_INET:
3200  if (done_ip4)
3201  break;
3202  done_ip4 = 1;
3203  /*
3204  * Need an AF_INET socket to do this unfortunately to stop
3205  * "Invalid argument" error if AF_INET6 socket is used for SIOCGIFADDR
3206  */
3207  ip4fd = socket(AF_INET, SOCK_DGRAM, 0);
3208  if (ip4fd == -1) {
3209  coap_log(LOG_ERR,
3210  "coap_join_mcast_group_intf: %s: socket: %s\n",
3211  ifname, coap_socket_strerror());
3212  continue;
3213  }
3214  memset (&ifr, 0, sizeof(ifr));
3215  strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
3216  ifr.ifr_name[IFNAMSIZ - 1] = '\000';
3217  result = ioctl(ip4fd, SIOCGIFADDR, &ifr);
3218  if (result != 0) {
3219  coap_log(LOG_ERR,
3220  "coap_join_mcast_group_intf: %s: "
3221  "Cannot get IPv4 address: %s\n",
3222  ifname, coap_socket_strerror());
3223  }
3224  else {
3225  /* Capture the IPv4 address for later */
3226  mreq4.imr_interface = ((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr;
3227  }
3228  close(ip4fd);
3229  break;
3230  default:
3231  break;
3232  }
3233  }
3234  }
3235 #endif /* ! _WIN32 */
3236 
3237  /* Add in mcast address(es) to appropriate interface */
3238  for (ainfo = resmulti; ainfo != NULL; ainfo = ainfo->ai_next) {
3239  LL_FOREACH(ctx->endpoint, endpoint) {
3240  /* Only UDP currently supported */
3241  if (endpoint->proto == COAP_PROTO_UDP) {
3242  coap_address_t gaddr;
3243 
3244  coap_address_init(&gaddr);
3245  if (ainfo->ai_family == AF_INET6) {
3246  if (!ifname) {
3247  if(endpoint->bind_addr.addr.sa.sa_family == AF_INET6) {
3248  /*
3249  * Do it on the ifindex that the server is listening on
3250  * (sin6_scope_id could still be 0)
3251  */
3252  mreq6.ipv6mr_interface =
3253  endpoint->bind_addr.addr.sin6.sin6_scope_id;
3254  }
3255  else {
3256  mreq6.ipv6mr_interface = 0;
3257  }
3258  }
3259  gaddr.addr.sin6.sin6_family = AF_INET6;
3260  gaddr.addr.sin6.sin6_port = endpoint->bind_addr.addr.sin6.sin6_port;
3261  gaddr.addr.sin6.sin6_addr = mreq6.ipv6mr_multiaddr =
3262  ((struct sockaddr_in6 *)ainfo->ai_addr)->sin6_addr;
3263  result = setsockopt(endpoint->sock.fd, IPPROTO_IPV6, IPV6_JOIN_GROUP,
3264  (char *)&mreq6, sizeof(mreq6));
3265  }
3266  else if (ainfo->ai_family == AF_INET) {
3267  if (!ifname) {
3268  if(endpoint->bind_addr.addr.sa.sa_family == AF_INET) {
3269  /*
3270  * Do it on the interface that the server is listening on
3271  * (sin_addr could still be INADDR_ANY)
3272  */
3273  mreq4.imr_interface = endpoint->bind_addr.addr.sin.sin_addr;
3274  }
3275  else {
3276  mreq4.imr_interface.s_addr = INADDR_ANY;
3277  }
3278  }
3279  gaddr.addr.sin.sin_family = AF_INET;
3280  gaddr.addr.sin.sin_port = endpoint->bind_addr.addr.sin.sin_port;
3281  gaddr.addr.sin.sin_addr.s_addr = mreq4.imr_multiaddr.s_addr =
3282  ((struct sockaddr_in *)ainfo->ai_addr)->sin_addr.s_addr;
3283  result = setsockopt(endpoint->sock.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
3284  (char *)&mreq4, sizeof(mreq4));
3285  }
3286  else {
3287  continue;
3288  }
3289 
3290  if (result == COAP_SOCKET_ERROR) {
3291  coap_log(LOG_ERR,
3292  "coap_join_mcast_group_intf: %s: setsockopt: %s\n",
3293  group_name, coap_socket_strerror());
3294  }
3295  else {
3296  char addr_str[INET6_ADDRSTRLEN + 8 + 1];
3297 
3298  addr_str[sizeof(addr_str)-1] = '\000';
3299  if (coap_print_addr(&gaddr, (uint8_t*)addr_str,
3300  sizeof(addr_str) - 1)) {
3301  if (ifname)
3302  coap_log(LOG_DEBUG, "added mcast group %s i/f %s\n", addr_str,
3303  ifname);
3304  else
3305  coap_log(LOG_DEBUG, "added mcast group %s\n", addr_str);
3306  }
3307  mgroup_setup = 1;
3308  }
3309  }
3310  }
3311  }
3312  if (!mgroup_setup) {
3313  result = -1;
3314  }
3315 
3316  finish:
3317  freeaddrinfo(resmulti);
3318 
3319  return result;
3320 }
3321 #else /* defined WITH_CONTIKI || defined WITH_LWIP */
3322 int
3323 coap_join_mcast_group_intf(coap_context_t *ctx, const char *group_name,
3324  const char *ifname) {
3325  (void)ctx;
3326  (void)group_name;
3327  (void)ifname;
3328  return -1;
3329 }
3330 #endif /* defined WITH_CONTIKI || defined WITH_LWIP */
3331 
3332 #ifdef WITH_CONTIKI
3333 
3334 /*---------------------------------------------------------------------------*/
3335 /* CoAP message retransmission */
3336 /*---------------------------------------------------------------------------*/
3337 PROCESS_THREAD(coap_retransmit_process, ev, data) {
3338  coap_tick_t now;
3339  coap_queue_t *nextpdu;
3340 
3341  PROCESS_BEGIN();
3342 
3343  coap_log(LOG_DEBUG, "Started retransmit process\n");
3344 
3345  while (1) {
3346  PROCESS_YIELD();
3347  if (ev == PROCESS_EVENT_TIMER) {
3348  if (etimer_expired(&the_coap_context.retransmit_timer)) {
3349 
3350  nextpdu = coap_peek_next(&the_coap_context);
3351 
3352  coap_ticks(&now);
3353  while (nextpdu && nextpdu->t <= now) {
3354  coap_retransmit(&the_coap_context, coap_pop_next(&the_coap_context));
3355  nextpdu = coap_peek_next(&the_coap_context);
3356  }
3357 
3358  /* need to set timer to some value even if no nextpdu is available */
3359  etimer_set(&the_coap_context.retransmit_timer,
3360  nextpdu ? nextpdu->t - now : 0xFFFF);
3361  }
3362  if (etimer_expired(&the_coap_context.notify_timer)) {
3363  coap_check_notify(&the_coap_context);
3364  etimer_reset(&the_coap_context.notify_timer);
3365  }
3366  }
3367  }
3368 
3369  PROCESS_END();
3370 }
3371 /*---------------------------------------------------------------------------*/
3372 
3373 #endif /* WITH_CONTIKI */
3374 
3375 #ifdef WITH_LWIP
3376 /* FIXME: retransmits that are not required any more due to incoming packages
3377  * do *not* get cleared at the moment, the wakeup when the transmission is due
3378  * is silently accepted. this is mainly due to the fact that the required
3379  * checks are similar in two places in the code (when receiving ACK and RST)
3380  * and that they cause more than one patch chunk, as it must be first checked
3381  * whether the sendqueue item to be dropped is the next one pending, and later
3382  * the restart function has to be called. nothing insurmountable, but it can
3383  * also be implemented when things have stabilized, and the performance
3384  * penality is minimal
3385  *
3386  * also, this completely ignores COAP_RESOURCE_CHECK_TIME.
3387  * */
3388 
3389 static void coap_retransmittimer_execute(void *arg) {
3390  coap_context_t *ctx = (coap_context_t*)arg;
3391  coap_tick_t now;
3392  coap_tick_t elapsed;
3393  coap_queue_t *nextinqueue;
3394 
3395  ctx->timer_configured = 0;
3396 
3397  coap_ticks(&now);
3398 
3399  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 */
3400 
3401  nextinqueue = coap_peek_next(ctx);
3402  while (nextinqueue != NULL) {
3403  if (nextinqueue->t > elapsed) {
3404  nextinqueue->t -= elapsed;
3405  break;
3406  } else {
3407  elapsed -= nextinqueue->t;
3408  coap_retransmit(ctx, coap_pop_next(ctx));
3409  nextinqueue = coap_peek_next(ctx);
3410  }
3411  }
3412 
3413  ctx->sendqueue_basetime = now;
3414 
3415  coap_retransmittimer_restart(ctx);
3416 }
3417 
3418 static void coap_retransmittimer_restart(coap_context_t *ctx) {
3419  coap_tick_t now, elapsed, delay;
3420 
3421  if (ctx->timer_configured) {
3422  printf("clearing\n");
3423  sys_untimeout(coap_retransmittimer_execute, (void*)ctx);
3424  ctx->timer_configured = 0;
3425  }
3426  if (ctx->sendqueue != NULL) {
3427  coap_ticks(&now);
3428  elapsed = now - ctx->sendqueue_basetime;
3429  if (ctx->sendqueue->t >= elapsed) {
3430  delay = ctx->sendqueue->t - elapsed;
3431  } else {
3432  /* a strange situation, but not completely impossible.
3433  *
3434  * this happens, for example, right after
3435  * coap_retransmittimer_execute, when a retransmission
3436  * was *just not yet* due, and the clock ticked before
3437  * our coap_ticks was called.
3438  *
3439  * not trying to retransmit anything now, as it might
3440  * cause uncontrollable recursion; let's just try again
3441  * with the next main loop run.
3442  * */
3443  delay = 0;
3444  }
3445 
3446  printf("scheduling for %d ticks\n", delay);
3447  sys_timeout(delay, coap_retransmittimer_execute, (void*)ctx);
3448  ctx->timer_configured = 1;
3449  }
3450 }
3451 #endif
void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
Definition: address.c:100
int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
Definition: address.c:81
COAP_STATIC_INLINE void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
Definition: address.h:150
void coap_delete_all_async(coap_context_t *context)
Removes and frees off all of the async entries for the given context.
Definition: async.c:109
void coap_dtls_free_context(struct coap_dtls_context_t *dtls_context)
Releases the storage allocated for dtls_context.
int coap_dtls_send(struct coap_context_t *coap_context, struct coap_dtls_session_t *session, const coap_pdu_t *pdu)
Pulls together all the internal only header files.
ssize_t coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len)
Definition: coap_io.c:472
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's data in memory.
Definition: coap_io.c:739
ssize_t coap_network_read(coap_socket_t *sock, coap_packet_t *packet)
Function interface for reading data.
Definition: coap_io.c:746
ssize_t coap_network_send(coap_socket_t *sock, const coap_session_t *session, const uint8_t *data, size_t datalen)
Definition: coap_io.c:565
const char * coap_socket_strerror(void)
Definition: coap_io.c:1488
#define COAP_SOCKET_MULTICAST
socket is used for multicast communication
Definition: coap_io.h:100
#define COAP_SOCKET_WANT_ACCEPT
non blocking server socket is waiting for accept
Definition: coap_io.h:94
#define COAP_SOCKET_NOT_EMPTY
the socket is not empty
Definition: coap_io.h:89
#define COAP_SOCKET_CAN_WRITE
non blocking socket can now write without blocking
Definition: coap_io.h:97
#define COAP_SOCKET_BOUND
the socket is bound
Definition: coap_io.h:90
#define COAP_SOCKET_WANT_READ
non blocking socket is waiting for reading
Definition: coap_io.h:92
#define COAP_SOCKET_ERROR
Definition: coap_io.h:42
coap_nack_reason_t
Definition: coap_io.h:223
@ COAP_NACK_NOT_DELIVERABLE
Definition: coap_io.h:225
@ COAP_NACK_TOO_MANY_RETRIES
Definition: coap_io.h:224
@ COAP_NACK_TLS_FAILED
Definition: coap_io.h:227
@ COAP_NACK_ICMP_ISSUE
Definition: coap_io.h:228
@ COAP_NACK_RST
Definition: coap_io.h:226
#define COAP_SOCKET_CAN_ACCEPT
non blocking server socket can now accept without blocking
Definition: coap_io.h:98
#define COAP_SOCKET_WANT_WRITE
non blocking socket is waiting for writing
Definition: coap_io.h:93
#define COAP_SOCKET_CAN_CONNECT
non blocking client socket can now connect without blocking
Definition: coap_io.h:99
void coap_epoll_ctl_mod(coap_socket_t *sock, uint32_t events, const char *func)
#define COAP_SOCKET_WANT_CONNECT
non blocking client socket is waiting for connect
Definition: coap_io.h:95
#define COAP_SOCKET_CAN_READ
non blocking socket can now read without blocking
Definition: coap_io.h:96
#define COAP_SOCKET_CONNECTED
the socket is connected
Definition: coap_io.h:91
#define COAP_SOCKET_EMPTY
coap_socket_flags_t values
Definition: coap_io.h:88
void coap_free_endpoint(coap_endpoint_t *ep)
int coap_dtls_context_set_spsk(coap_context_t *ctx COAP_UNUSED, coap_dtls_spsk_t *setup_data COAP_UNUSED)
Definition: coap_notls.c:56
int coap_dtls_context_set_pki(coap_context_t *ctx COAP_UNUSED, const coap_dtls_pki_t *setup_data COAP_UNUSED, const coap_dtls_role_t role COAP_UNUSED)
Definition: coap_notls.c:33
void * coap_dtls_new_client_session(coap_session_t *session COAP_UNUSED)
Definition: coap_notls.c:99
ssize_t coap_tls_read(coap_session_t *session COAP_UNUSED, uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
Definition: coap_notls.c:171
int coap_dtls_receive(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
Definition: coap_notls.c:134
void * coap_tls_new_client_session(coap_session_t *session COAP_UNUSED, int *connected COAP_UNUSED)
Definition: coap_notls.c:153
int coap_dtls_hello(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
Definition: coap_notls.c:142
ssize_t coap_tls_write(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
Definition: coap_notls.c:164
int coap_dtls_context_set_pki_root_cas(struct coap_context_t *ctx COAP_UNUSED, const char *ca_file COAP_UNUSED, const char *ca_path COAP_UNUSED)
Definition: coap_notls.c:41
coap_session_t * coap_endpoint_get_session(coap_endpoint_t *endpoint, const coap_packet_t *packet, coap_tick_t now)
Definition: coap_session.c:555
ssize_t coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu, coap_queue_t *node)
Definition: coap_session.c:310
void coap_session_set_mtu(coap_session_t *session, unsigned mtu)
Set the session MTU.
Definition: coap_session.c:263
void coap_session_send_csm(coap_session_t *session)
Notify session transport has just connected and CSM exchange can now start.
Definition: coap_session.c:349
size_t coap_session_max_pdu_size(const coap_session_t *session)
Get maximum acceptable PDU size.
Definition: coap_session.c:242
ssize_t coap_session_send(coap_session_t *session, const uint8_t *data, size_t datalen)
Function interface for datagram data transmission.
Definition: coap_session.c:275
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
Definition: coap_session.c:68
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
Definition: coap_session.c:74
void coap_session_connected(coap_session_t *session)
Notify session that it has just connected or reconnected.
Definition: coap_session.c:397
coap_endpoint_t * coap_new_endpoint(coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto)
Create a new endpoint for communicating with peers.
coap_session_t * coap_new_server_session(struct coap_context_t *ctx, coap_endpoint_t *ep)
Creates a new server session for the specified endpoint.
void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
Definition: coap_session.c:454
ssize_t coap_session_write(coap_session_t *session, const uint8_t *data, size_t datalen)
Function interface for stream data transmission.
Definition: coap_session.c:296
#define SESSIONS_ITER_SAFE(e, el, rtmp)
Definition: coap_session.h:666
#define COAP_SESSION_STATE_HANDSHAKE
Definition: coap_session.h:56
#define COAP_SESSION_STATE_CSM
Definition: coap_session.h:57
#define COAP_SESSION_TYPE_HELLO
server-side ephemeral session for responding to a client hello
Definition: coap_session.h:48
#define COAP_PROTO_NOT_RELIABLE(p)
Definition: coap_session.h:39
#define COAP_SESSION_STATE_CONNECTING
Definition: coap_session.h:55
#define COAP_PROTO_RELIABLE(p)
Definition: coap_session.h:40
#define COAP_SESSION_STATE_NONE
coap_session_state_t values
Definition: coap_session.h:54
#define COAP_SESSION_STATE_ESTABLISHED
Definition: coap_session.h:58
#define SESSIONS_ITER(e, el, rtmp)
Definition: coap_session.h:663
void coap_io_do_io(coap_context_t *ctx, coap_tick_t now)
Processes any outstanding read, write, accept or connect I/O as indicated in the coap_socket_t struct...
Definition: net.c:1690
unsigned int coap_io_prepare_epoll(coap_context_t *ctx, coap_tick_t now)
Any now timed out delayed packet is transmitted, along with any packets associated with requested obs...
Definition: coap_io.c:1001
void coap_io_do_epoll(coap_context_t *ctx, struct epoll_event *events, size_t nevents)
Process all the epoll events.
Definition: net.c:1751
int coap_handle_request_put_block(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu, coap_pdu_t *response, coap_resource_t *resource, coap_string_t *uri_path, coap_opt_t *observe, coap_binary_t *token, coap_string_t *query, coap_method_handler_t h, int *added_block)
Definition: block.c:1240
void coap_block_delete_lg_crcv(coap_session_t *session, coap_lg_crcv_t *lg_crcv)
Definition: block.c:880
int coap_handle_response_get_block(coap_context_t *context, coap_session_t *session, coap_pdu_t *sent, coap_pdu_t *rcvd, coap_recurse_t recursive)
Definition: block.c:1630
void coap_check_code_lg_xmit(coap_session_t *session, coap_pdu_t *response, coap_resource_t *resource, coap_string_t *query)
The function checks that the code in a newly formed lg_xmit created by coap_add_data_large_response()...
Definition: block.c:1970
int coap_handle_request_send_block(coap_session_t *session, coap_pdu_t *pdu, coap_pdu_t *response, coap_resource_t *resource, coap_string_t *query)
Definition: block.c:969
coap_lg_crcv_t * coap_block_new_lg_crcv(coap_session_t *session, coap_pdu_t *pdu)
Definition: block.c:828
int coap_handle_response_send_block(coap_session_t *session, coap_pdu_t *rcvd)
Definition: block.c:1466
@ COAP_RECURSE_OK
int coap_write_block_opt(coap_block_t *block, uint16_t type, coap_pdu_t *pdu, size_t data_length)
Writes a block option of type type to message pdu.
Definition: block.c:63
#define COAP_MAX_BLOCK_SZX
The largest value for the SZX component in a Block option.
Definition: block.h:27
int coap_get_block(coap_pdu_t *pdu, uint16_t type, coap_block_t *block)
Initializes block from pdu.
Definition: block.c:35
#define COAP_BLOCK_USE_LIBCOAP
Definition: block.h:39
void coap_delete_cache_entry(coap_context_t *ctx, coap_cache_entry_t *cache_entry)
Remove a cache-entry from the hash list and free off all the appropriate contents apart from app_data...
Definition: coap_cache.c:205
#define COAP_DEFAULT_NSTART
The number of simultaneous outstanding interactions that a client maintains to a given server.
Definition: coap_session.h:503
int64_t coap_tick_diff_t
This data type is used to represent the difference between two clock_tick_t values.
Definition: coap_time.h:132
void coap_ticks(coap_tick_t *t)
Sets t to the internal time with COAP_TICKS_PER_SECOND resolution.
void coap_clock_init(void)
Initializes the internal clock.
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.h:120
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition: coap_time.h:135
uint64_t coap_ticks_to_rt_us(coap_tick_t t)
Helper function that converts coap ticks to POSIX wallclock time in us.
int coap_prng(void *buf, size_t len)
Fills buf with len random bytes using the default pseudo random number generator.
Definition: coap_prng.c:85
void coap_prng_init(unsigned int seed)
Seeds the default random number generation function with the given seed.
Definition: coap_prng.c:74
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:169
void coap_delete_all_resources(coap_context_t *context)
Deletes all resources from given context and frees their storage.
Definition: resource.c:588
#define RESOURCES_ITER(r, tmp)
coap_resource_t * coap_get_resource_from_uri_path(coap_context_t *context, coap_str_const_t *uri_path)
Returns the resource identified by the unique string uri_path.
Definition: resource.c:613
void(* coap_method_handler_t)(coap_context_t *, coap_resource_t *, coap_session_t *, coap_pdu_t *, coap_binary_t *, coap_string_t *, coap_pdu_t *)
Definition of message handler function.
Definition: resource.h:41
#define COAP_PRINT_STATUS_ERROR
Definition: resource.h:329
void coap_dtls_startup(void)
Initialize the underlying (D)TLS Library layer.
Definition: coap_notls.c:70
coap_session_t * coap_session_new_dtls_session(coap_session_t *session, coap_tick_t now)
Create a new DTLS session for the session.
Definition: coap_session.c:692
void coap_dtls_shutdown(void)
Close down the underlying (D)TLS Library layer.
Definition: coap_notls.c:73
struct coap_dtls_context_t * coap_dtls_new_context(struct coap_context_t *coap_context)
Creates a new DTLS context for the given coap_context.
@ COAP_DTLS_ROLE_SERVER
Internal function invoked for server.
Definition: coap_dtls.h:483
#define COAP_DTLS_PKI_SETUP_VERSION
Latest PKI setup version.
Definition: coap_dtls.h:240
int coap_dtls_is_supported(void)
Returns 1 if support for DTLS is enabled, or 0 otherwise.
unsigned int coap_encode_var_safe(uint8_t *buf, size_t length, unsigned int val)
Encodes multiple-length byte sequences.
Definition: encode.c:38
unsigned int coap_decode_var_bytes(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
Definition: encode.c:29
unsigned int coap_encode_var_safe8(uint8_t *buf, size_t length, uint64_t val)
Encodes multiple-length byte sequences.
Definition: encode.c:68
#define COAP_EVENT_DTLS_ERROR
Definition: coap_event.h:33
unsigned int coap_event_t
Scalar type to represent different events, e.g.
Definition: coap_event.h:25
#define COAP_EVENT_TCP_FAILED
Definition: coap_event.h:40
#define COAP_EVENT_DTLS_CONNECTED
Definition: coap_event.h:31
#define COAP_EVENT_TCP_CONNECTED
TCP events for COAP_PROTO_TCP and COAP_PROTO_TLS.
Definition: coap_event.h:38
coap_log_t coap_get_log_level(void)
Get the current logging level.
Definition: coap_debug.c:61
void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu)
Display the contents of the specified pdu.
Definition: coap_debug.c:505
size_t coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, size_t len)
Print the address into the defined buffer.
Definition: coap_debug.c:171
const char * coap_session_str(const coap_session_t *session)
Get session description.
const char * coap_endpoint_str(const coap_endpoint_t *endpoint)
Get endpoint description.
#define coap_log(level,...)
Logging function.
Definition: coap_debug.h:150
@ LOG_ERR
Error.
Definition: coap_debug.h:53
@ LOG_INFO
Information.
Definition: coap_debug.h:56
@ LOG_ALERT
Alert.
Definition: coap_debug.h:51
@ LOG_WARNING
Warning.
Definition: coap_debug.h:54
@ LOG_DEBUG
Debug.
Definition: coap_debug.h:57
@ LOG_EMERG
Emergency.
Definition: coap_debug.h:50
#define COAP_OBSERVE_CANCEL
The value COAP_OBSERVE_CANCEL in a GET/FETCH request option COAP_OPTION_OBSERVE indicates that the ob...
Definition: subscribe.h:37
#define COAP_OBSERVE_ESTABLISH
The value COAP_OBSERVE_ESTABLISH in a GET/FETCH request option COAP_OPTION_OBSERVE indicates a new ob...
Definition: subscribe.h:30
int coap_option_filter_set(coap_opt_filter_t *filter, uint16_t type)
Sets the corresponding entry for type in filter.
Definition: option.c:486
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
Definition: option.c:146
COAP_STATIC_INLINE void coap_option_filter_clear(coap_opt_filter_t *f)
Clears filter f.
Definition: option.h:113
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
Definition: option.c:209
int coap_option_filter_unset(coap_opt_filter_t *filter, uint16_t type)
Clears the corresponding entry for type in filter.
Definition: option.c:491
#define COAP_OPT_ALL
Pre-defined filter that includes all options.
Definition: option.h:105
int coap_option_filter_get(coap_opt_filter_t *filter, uint16_t type)
Checks if type is contained in filter.
Definition: option.c:496
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
Definition: option.c:246
coap_opt_t * coap_check_option(coap_pdu_t *pdu, uint16_t type, coap_opt_iterator_t *oi)
Retrieves the first option of type type from pdu.
Definition: option.c:196
coap_opt_iterator_t * coap_option_iterator_init(const coap_pdu_t *pdu, coap_opt_iterator_t *oi, const coap_opt_filter_t *filter)
Initializes the given option iterator oi to point to the beginning of the pdu's option list.
Definition: option.c:110
#define COAP_SET_STR(st, l, v)
Definition: str.h:43
#define coap_string_equal(string1, string2)
Compares the two strings for equality.
Definition: str.h:181
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
Definition: str.c:38
int coap_delete_observer(coap_resource_t *resource, coap_session_t *session, const coap_binary_t *token)
Removes any subscription for observer from resource and releases the allocated storage.
Definition: resource.c:804
coap_subscription_t * coap_add_observer(coap_resource_t *resource, coap_session_t *session, const coap_binary_t *token, coap_string_t *query, int has_block2, coap_block_t block, uint8_t code)
Adds the specified peer as observer for resource.
Definition: resource.c:720
void coap_check_notify(coap_context_t *context)
Checks all known resources to see if they are dirty and then notifies subscribed observers.
Definition: resource.c:1046
void coap_handle_failed_notify(coap_context_t *context, coap_session_t *session, const coap_binary_t *token)
Handles a failed observe notify.
Definition: resource.c:1109
void coap_touch_observer(coap_context_t *context, coap_session_t *session, const coap_binary_t *token)
Flags that data is ready to be sent to observers.
Definition: resource.c:791
int coap_socket_connect_tcp1(coap_socket_t *sock, const coap_address_t *local_if, const coap_address_t *server, int default_port, coap_address_t *local_addr, coap_address_t *remote_addr)
Create a new TCP socket and initiate the connection.
int coap_socket_connect_tcp2(coap_socket_t *sock, coap_address_t *local_addr, coap_address_t *remote_addr)
Complete the TCP Connection.
int coap_split_proxy_uri(const uint8_t *str_var, size_t len, coap_uri_t *uri)
Parses a given string into URI components.
Definition: uri.c:239
#define COAP_STATIC_INLINE
Definition: libcoap.h:38
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.
COAP_STATIC_INLINE void coap_free(void *object)
Wrapper function to coap_free_type() for backwards compatibility.
Definition: mem.h:103
void coap_memory_init(void)
Initializes libcoap's memory management.
@ COAP_NODE
Definition: mem.h:34
@ COAP_CONTEXT
Definition: mem.h:35
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
const uint32_t n
Definition: murmur3.c:56
coap_queue_t * coap_peek_next(coap_context_t *context)
Returns the next pdu to send without removing from sendqeue.
Definition: net.c:272
#define FRAC_BITS
The number of bits for the fractional part of ACK_TIMEOUT and ACK_RANDOM_FACTOR.
Definition: net.c:72
static ssize_t coap_send_pdu(coap_session_t *session, coap_pdu_t *pdu, coap_queue_t *node)
Definition: net.c:752
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:155
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:2009
COAP_STATIC_INLINE int token_match(const uint8_t *a, size_t alen, const uint8_t *b, size_t blen)
Definition: net.c:1003
respond_t
Internal flags to control the treatment of responses (specifically in presence of the No-Response opt...
Definition: net.c:2314
@ RESPONSE_DROP
Definition: net.c:2314
@ RESPONSE_DEFAULT
Definition: net.c:2314
@ RESPONSE_SEND
Definition: net.c:2314
static void handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu)
Definition: net.c:2434
int coap_can_exit(coap_context_t *context)
Returns 1 if there are no messages to send or to dispatch in the context's queues.
Definition: net.c:3080
void coap_delete_all(coap_queue_t *queue)
Removes all items from given queue and frees the allocated storage.
Definition: net.c:249
int coap_handle_dgram(coap_context_t *ctx, coap_session_t *session, uint8_t *msg, size_t msg_len)
Parses and interprets a CoAP datagram with context ctx.
Definition: net.c:1850
int coap_context_get_coap_fd(coap_context_t *context)
Get the libcoap internal file descriptor for using in an application's select() or returned as an eve...
Definition: net.c:449
static coap_str_const_t coap_default_uri_wellknown
Definition: net.c:2429
int coap_remove_from_queue(coap_queue_t **queue, coap_session_t *session, coap_mid_t id, coap_queue_t **node)
This function removes the element with given id from the list given list.
Definition: net.c:1887
int coap_delete_node(coap_queue_t *node)
Destroys specified node.
Definition: net.c:229
static int coap_read_endpoint(coap_context_t *ctx, coap_endpoint_t *endpoint, coap_tick_t now)
Definition: net.c:1630
coap_queue_t * coap_new_node(void)
Creates a new node suitable for adding to the CoAP sendqueue.
Definition: net.c:258
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:280
void coap_free_context(coap_context_t *context)
CoAP stack context must be released with coap_free_context().
Definition: net.c:585
COAP_STATIC_INLINE 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:2121
#define MAX_BITS
The maximum number of bits for fixed point integers that are used for retransmission time calculation...
Definition: net.c:78
static size_t coap_get_session_client_psk(const coap_session_t *session, const uint8_t *hint, size_t hint_len, uint8_t *identity, size_t *identity_len, size_t max_identity_len, uint8_t *psk, size_t max_psk_len)
Definition: net.c:296
void coap_cleanup(void)
Definition: net.c:3126
int coap_handle_event(coap_context_t *context, coap_event_t event, coap_session_t *session)
Invokes the event handler of context for the given event and data.
Definition: net.c:3069
#define ACK_TIMEOUT
creates a Qx.FRAC_BITS from session's 'ack_timeout'
Definition: net.c:93
int coap_context_set_pki(coap_context_t *ctx, const coap_dtls_pki_t *setup_data)
Set the context's default PKI information for a server.
Definition: net.c:420
#define SZX_TO_BYTES(SZX)
Definition: net.c:2136
static size_t coap_get_context_server_psk(const coap_session_t *session, const uint8_t *identity, size_t identity_len, uint8_t *psk, size_t max_psk_len)
Definition: net.c:332
coap_pdu_t * coap_wellknown_response(coap_context_t *context, coap_session_t *session, coap_pdu_t *request)
Creates a new response for given request with the contents of .well-known/core.
Definition: net.c:2139
static int coap_cancel(coap_context_t *context, const coap_queue_t *sent)
This function cancels outstanding messages for the session and token specified in sent.
Definition: net.c:2292
int coap_join_mcast_group_intf(coap_context_t *ctx, const char *group_name, const char *ifname)
Function interface for joining a multicast group for listening for the currently defined endpoints th...
Definition: net.c:3135
static int coap_started
Definition: net.c:3104
static int coap_accept_endpoint(coap_context_t *ctx, coap_endpoint_t *endpoint, coap_tick_t now)
Definition: net.c:1681
coap_mid_t coap_send_message_type(coap_session_t *session, coap_pdu_t *request, unsigned char type)
Helper function to create and send a message with type (usually ACK or RST).
Definition: net.c:873
void coap_dispatch(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu)
Dispatches the PDUs from the receive queue in given context.
Definition: net.c:2885
int coap_insert_node(coap_queue_t **queue, coap_queue_t *node)
Adds node to given queue, ordered by variable t in node.
Definition: net.c:192
static size_t coap_get_context_server_hint(const coap_session_t *session, uint8_t *hint, size_t max_hint_len)
Definition: net.c:360
unsigned int coap_calc_timeout(coap_session_t *session, unsigned char r)
Calculates the initial timeout based on the session CoAP transmission parameters 'ack_timeout',...
Definition: net.c:899
static int coap_write_endpoint(coap_context_t *ctx, coap_endpoint_t *endpoint, coap_tick_t now)
Definition: net.c:1673
coap_mid_t coap_send_ack(coap_session_t *session, coap_pdu_t *request)
Sends an ACK message with code 0 for the specified request to dst.
Definition: net.c:706
static int coap_handle_dgram_for_proto(coap_context_t *ctx, coap_session_t *session, coap_packet_t *packet)
Definition: net.c:1383
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:459
static void coap_write_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now)
Definition: net.c:1439
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:654
static void coap_connect_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now)
Definition: net.c:1402
COAP_STATIC_INLINE void coap_free_node(coap_queue_t *node)
Definition: net.c:103
int coap_context_set_psk(coap_context_t *ctx, const char *hint, const uint8_t *key, size_t key_len)
Set the context's default PSK hint and/or key for a server.
Definition: net.c:386
coap_mid_t coap_retransmit(coap_context_t *context, coap_queue_t *node)
Handles retransmissions of confirmable messages.
Definition: net.c:1292
void coap_set_app_data(coap_context_t *ctx, void *app_data)
Stores data with the given CoAP context.
Definition: net.c:573
ssize_t coap_session_send_pdu(coap_session_t *session, coap_pdu_t *pdu)
Send a pdu according to the session's protocol.
Definition: net.c:720
void coap_cancel_all_messages(coap_context_t *context, coap_session_t *session, const uint8_t *token, size_t token_length)
Cancels all outstanding messages for session session that have the specified token.
Definition: net.c:1968
#define SHR_FP(val, frac)
coap_mid_t coap_send_error(coap_session_t *session, coap_pdu_t *request, unsigned char code, coap_opt_filter_t *opts)
Sends an error response with code code for request request to dst.
Definition: net.c:855
int coap_context_set_psk2(coap_context_t *ctx, coap_dtls_spsk_t *setup_data)
Set the context's default PSK hint and/or key for a server.
Definition: net.c:406
void * coap_get_app_data(const coap_context_t *ctx)
Returns any application-specific data that has been stored with context using the function coap_set_a...
Definition: net.c:579
static enum respond_t no_response(coap_pdu_t *request, coap_pdu_t *response, coap_session_t *session)
Definition: net.c:2362
static void handle_signaling(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu)
Definition: net.c:2844
#define min(a, b)
Definition: net.c:65
coap_mid_t coap_send_large(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
Definition: net.c:1009
void coap_context_set_keepalive(coap_context_t *context, unsigned int seconds)
Set the context keepalive timer for sessions.
Definition: net.c:445
static void coap_read_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now)
Definition: net.c:1486
void coap_startup(void)
Definition: net.c:3106
coap_mid_t coap_wait_ack(coap_context_t *context, coap_session_t *session, coap_queue_t *node)
Definition: net.c:925
void coap_cancel_session_messages(coap_context_t *context, coap_session_t *session, coap_nack_reason_t reason)
Cancels all outstanding messages for session session.
Definition: net.c:1931
#define FP1
coap_mid_t coap_send(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
Definition: net.c:1109
int coap_context_set_pki_root_cas(coap_context_t *ctx, const char *ca_file, const char *ca_dir)
Set the context's default Root CA information for a client or server.
Definition: net.c:435
#define ACK_RANDOM_FACTOR
creates a Qx.FRAC_BITS from session's 'ack_random_factor'
Definition: net.c:89
COAP_STATIC_INLINE coap_queue_t * coap_malloc_node(void)
Definition: net.c:98
#define INET6_ADDRSTRLEN
Definition: net.c:61
static void handle_response(coap_context_t *context, coap_session_t *session, coap_pdu_t *sent, coap_pdu_t *rcvd)
Definition: net.c:2803
@ COAP_RESPONSE_FAIL
Response not liked - send CoAP RST packet.
Definition: net.h:84
COAP_STATIC_INLINE coap_mid_t coap_send_rst(coap_session_t *session, coap_pdu_t *request)
Sends an RST message with code 0 for the specified request to dst.
Definition: net.h:528
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
Definition: option.h:24
int coap_update_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Updates token in pdu with length len and data.
Definition: pdu.c:273
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
Definition: pdu.c:133
int coap_pdu_parse_header(coap_pdu_t *pdu, coap_proto_t proto)
Decode the protocol specific header for the specified PDU.
Definition: pdu.c:818
size_t coap_pdu_parse_header_size(coap_proto_t proto, const uint8_t *data)
Interprets data to determine the number of bytes in the header.
Definition: pdu.c:763
coap_pdu_t * coap_pdu_init(uint8_t type, uint8_t code, uint16_t mid, size_t size)
Creates a new CoAP PDU with at least enough storage space for the given size maximum message size.
Definition: pdu.c:85
int coap_add_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds token of length len to pdu.
Definition: pdu.c:249
size_t coap_add_option(coap_pdu_t *pdu, uint16_t type, size_t len, const uint8_t *data)
Adds option of given type to pdu that is passed as first parameter.
Definition: pdu.c:535
int coap_pdu_parse_opt(coap_pdu_t *pdu)
Verify consistency in the given CoAP PDU structure and locate the data.
Definition: pdu.c:970
uint8_t * coap_add_data_after(coap_pdu_t *pdu, size_t len)
Adds given data to the pdu that is passed as first parameter but does not copy it.
Definition: pdu.c:626
size_t coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto)
Compose the protocol specific header for the specified PDU.
Definition: pdu.c:1120
int coap_pdu_parse(coap_proto_t proto, const uint8_t *data, size_t length, coap_pdu_t *pdu)
Parses data into the CoAP PDU structure given in result.
Definition: pdu.c:1095
const char * coap_response_phrase(unsigned char code)
Returns a human-readable response phrase for the specified CoAP response code.
Definition: pdu.c:722
size_t coap_update_option(coap_pdu_t *pdu, uint16_t type, size_t len, const uint8_t *data)
Updates existing first option of given type in the pdu with the new data.
Definition: pdu.c:494
size_t coap_insert_option(coap_pdu_t *pdu, uint16_t type, size_t len, const uint8_t *data)
Inserts option of given type in the pdu with the appropriate data.
Definition: pdu.c:405
size_t coap_pdu_parse_size(coap_proto_t proto, const uint8_t *data, size_t length)
Parses data to extract the message size.
Definition: pdu.c:786
int coap_pdu_resize(coap_pdu_t *pdu, size_t new_size)
Dynamically grows the size of pdu to new_size.
Definition: pdu.c:198
int coap_remove_option(coap_pdu_t *pdu, uint16_t type)
Removes option of given type from the pdu.
Definition: pdu.c:305
int coap_add_data(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds given data to the pdu that is passed as first parameter.
Definition: pdu.c:614
#define COAP_PDU_IS_RESPONSE(pdu)
Definition: pdu.h:320
#define COAP_OPTION_HOP_LIMIT
Definition: pdu.h:137
#define COAP_OPTION_NORESPONSE
Definition: pdu.h:146
#define COAP_OPTION_URI_HOST
Definition: pdu.h:124
#define COAP_OPTION_IF_MATCH
Definition: pdu.h:123
#define COAP_OPTION_BLOCK2
Definition: pdu.h:140
#define COAP_OPTION_CONTENT_FORMAT
Definition: pdu.h:132
#define COAP_OPTION_SIZE2
Definition: pdu.h:142
#define COAP_PROTO_TCP
Definition: pdu.h:352
#define COAP_OPTION_BLOCK1
Definition: pdu.h:141
#define COAP_OPTION_PROXY_SCHEME
Definition: pdu.h:144
#define COAP_DEFAULT_PORT
Definition: pdu.h:27
#define COAP_SIGNALING_PONG
Definition: pdu.h:185
#define COAP_OPTION_URI_QUERY
Definition: pdu.h:136
#define COAP_DROPPED_RESPONSE
Indicates that a response is suppressed.
Definition: pdu.h:253
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition: pdu.h:244
#define COAP_OPTION_IF_NONE_MATCH
Definition: pdu.h:126
@ COAP_REQUEST_DELETE
Definition: pdu.h:86
@ COAP_REQUEST_GET
Definition: pdu.h:83
@ COAP_REQUEST_FETCH
Definition: pdu.h:87
#define COAP_PDU_DELAYED
Definition: pdu.h:255
#define COAP_OPTION_URI_PATH
Definition: pdu.h:131
#define COAP_SIGNALING_CSM
Definition: pdu.h:183
#define COAP_RESPONSE_CODE(N)
Definition: pdu.h:156
#define COAP_RESPONSE_CLASS(C)
Definition: pdu.h:159
#define COAP_PROTO_TLS
Definition: pdu.h:353
#define COAP_PDU_IS_EMPTY(pdu)
Definition: pdu.h:318
#define COAP_DEFAULT_MAX_PDU_RX_SIZE
Definition: pdu.h:54
#define COAP_PDU_IS_SIGNALING(pdu)
Definition: pdu.h:321
#define COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER
Definition: pdu.h:191
#define COAP_MESSAGE_NON
Definition: pdu.h:76
#define COAP_OPTION_CONTENT_TYPE
Definition: pdu.h:133
#define COAP_MESSAGE_RST
Definition: pdu.h:78
#define COAP_SIGNALING_OPTION_CUSTODY
Definition: pdu.h:193
#define COAP_MESSAGE_ACK
Definition: pdu.h:77
#define COAP_MESSAGE_CON
Definition: pdu.h:75
#define COAPS_DEFAULT_PORT
Definition: pdu.h:28
#define COAP_PROTO_DTLS
Definition: pdu.h:351
#define COAP_OPTION_URI_PORT
Definition: pdu.h:128
#define COAP_OPTION_ACCEPT
Definition: pdu.h:138
#define COAP_INVALID_MID
Indicates an invalid message id.
Definition: pdu.h:247
#define COAP_SIGNALING_RELEASE
Definition: pdu.h:186
#define COAP_OPTION_ETAG
Definition: pdu.h:125
#define COAP_OPTION_PROXY_URI
Definition: pdu.h:143
#define COAP_SIGNALING_PING
Definition: pdu.h:184
#define COAP_OPTION_OBSERVE
Definition: pdu.h:127
#define COAP_PDU_IS_REQUEST(pdu)
Definition: pdu.h:319
#define COAP_DEFAULT_URI_WELLKNOWN
well-known resources URI
Definition: pdu.h:71
#define COAP_PROTO_UDP
Definition: pdu.h:350
#define COAP_MEDIATYPE_APPLICATION_LINK_FORMAT
Definition: pdu.h:203
#define COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE
Definition: pdu.h:190
#define COAP_SIGNALING_ABORT
Definition: pdu.h:187
#define COAP_RESOURCE_CHECK_TIME
The interval in seconds to check if resources have changed.
Definition: resource.h:20
coap_address_t remote
remote address and port
Definition: coap_io.h:58
coap_address_t local
local address and port
Definition: coap_io.h:59
multi-purpose address abstraction
Definition: address.h:94
struct sockaddr_in sin
Definition: address.h:98
struct sockaddr_in6 sin6
Definition: address.h:99
struct sockaddr sa
Definition: address.h:97
union coap_address_t::@0 addr
size_t length
length of binary data
Definition: str.h:57
const uint8_t * s
read-only binary data
Definition: str.h:58
CoAP binary data definition.
Definition: str.h:48
size_t length
length of binary data
Definition: str.h:49
uint8_t * s
binary data
Definition: str.h:50
Structure of Block options.
Definition: block.h:33
unsigned int num
block number
Definition: block.h:34
unsigned int szx
block size
Definition: block.h:36
unsigned int m
1 if more blocks follow, 0 otherwise
Definition: block.h:35
The CoAP stack's global state is stored in a coap_context_t object.
Definition: net.h:150
coap_tick_t sendqueue_basetime
The time stamp in the first element of the sendqeue is relative to sendqueue_basetime.
Definition: net.h:171
coap_pong_handler_t pong_handler
Definition: net.h:191
ssize_t(* network_read)(coap_socket_t *sock, struct coap_packet_t *packet)
Definition: net.h:201
unsigned int csm_timeout
Timeout for waiting for a CSM from the remote side.
Definition: net.h:215
void * app
application-specific data
Definition: net.h:223
coap_session_t * sessions
client sessions
Definition: net.h:174
coap_nack_handler_t nack_handler
Definition: net.h:189
unsigned int ping_timeout
Minimum inactivity time before sending a ping message.
Definition: net.h:214
size_t(* get_server_psk)(const coap_session_t *session, const uint8_t *identity, size_t identity_len, uint8_t *psk, size_t max_psk_len)
Definition: net.h:204
coap_resource_t * resources
hash table or list of known resources
Definition: net.h:152
ssize_t(* network_send)(coap_socket_t *sock, const coap_session_t *session, const uint8_t *data, size_t datalen)
Definition: net.h:199
uint16_t * cache_ignore_options
CoAP options to ignore when creating a cache-key.
Definition: net.h:221
void * dtls_context
Definition: net.h:207
coap_opt_filter_t known_options
Definition: net.h:151
coap_ping_handler_t ping_handler
Definition: net.h:190
size_t cache_ignore_count
The number of CoAP options to ignore when creating a cache-key.
Definition: net.h:222
coap_queue_t * sendqueue
Definition: net.h:172
coap_response_handler_t response_handler
Definition: net.h:188
coap_cache_entry_t * cache
CoAP cache-entry cache.
Definition: net.h:220
coap_endpoint_t * endpoint
the endpoints used for listening
Definition: net.h:173
size_t(* get_server_hint)(const coap_session_t *session, uint8_t *hint, size_t max_hint_len)
Definition: net.h:205
coap_event_handler_t handle_event
Callback function that is used to signal events to the application.
Definition: net.h:197
size_t(* get_client_psk)(const coap_session_t *session, const uint8_t *hint, size_t hint_len, uint8_t *identity, size_t *identity_len, size_t max_identity_len, uint8_t *psk, size_t max_psk_len)
Definition: net.h:203
coap_resource_t * proxy_uri_resource
can be used for handling proxy URI resources
Definition: net.h:156
coap_dtls_spsk_t spsk_setup_data
Contains the initial PSK server setup data.
Definition: net.h:209
coap_resource_t * unknown_resource
can be used for handling unknown resources
Definition: net.h:154
The structure that holds the Client PSK information.
Definition: coap_dtls.h:308
coap_bin_const_t key
Definition: coap_dtls.h:310
coap_bin_const_t identity
Definition: coap_dtls.h:309
coap_dtls_cpsk_info_t psk_info
Client PSK definition.
Definition: coap_dtls.h:368
The structure used for defining the PKI setup data to be used.
Definition: coap_dtls.h:245
uint8_t version
Definition: coap_dtls.h:246
The structure that holds the Server Pre-Shared Key and Identity Hint information.
Definition: coap_dtls.h:375
coap_bin_const_t hint
Definition: coap_dtls.h:376
coap_bin_const_t key
Definition: coap_dtls.h:377
The structure used for defining the Server PSK setup data to be used.
Definition: coap_dtls.h:426
coap_dtls_spsk_info_t psk_info
Server PSK definition.
Definition: coap_dtls.h:456
Abstraction of virtual endpoint that can be attached to coap_context_t.
struct coap_context_t * context
endpoint's context
struct coap_session_t * sessions
hash table or list of active sessions
coap_address_t bind_addr
local interface address
coap_socket_t sock
socket object for the interface, if any
coap_proto_t proto
protocol used on this interface
size_t token_length
length of token
coap_binary_t * app_token
original PDU token
uint8_t token[8]
last used token
Structure to hold large body (many blocks) client receive information.
uint8_t initial
If set, has not been used yet.
coap_binary_t * app_token
app requesting PDU token
uint8_t token[8]
last used token
uint8_t base_token[8]
established base PDU token
size_t token_length
length of token
size_t base_token_length
length of token
uint8_t observe_set
Set if this is an observe receive PDU.
Structure to hold large body (many blocks) transmission information.
union coap_lg_xmit_t::@1 b
coap_pdu_t pdu
skeletal PDU
coap_l_block1_t b1
Iterator to run through PDU options.
Definition: option.h:169
uint16_t type
decoded option type
Definition: option.h:171
coap_addr_tuple_t addr_info
local and remote addresses
Definition: coap_io.h:215
unsigned char payload[COAP_RXBUFFER_SIZE]
payload
Definition: coap_io.h:218
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
Definition: pdu.h:287
uint8_t type
message type
Definition: pdu.h:288
uint8_t * token
first byte of token, if any, or options
Definition: pdu.h:298
size_t max_size
maximum size for token, options and payload, or zero for variable size pdu
Definition: pdu.h:297
uint8_t code
request method (value 1–31) or response code (value 64-255)
Definition: pdu.h:289
uint16_t mid
message id, if any, in regular host byte order
Definition: pdu.h:293
uint8_t token_length
length of Token
Definition: pdu.h:292
uint8_t hdr_size
actual size used for protocol-specific header
Definition: pdu.h:291
uint8_t * data
first byte of payload, if any
Definition: pdu.h:299
size_t used_size
used bytes of storage for token, options and payload
Definition: pdu.h:296
size_t alloc_size
allocated storage for token, options and payload
Definition: pdu.h:295
Queue entry.
Definition: net.h:37
coap_session_t * session
the CoAP session
Definition: net.h:43
coap_pdu_t * pdu
the CoAP PDU to send
Definition: net.h:45
unsigned int timeout
the randomized timeout value
Definition: net.h:42
struct coap_queue_t * next
Definition: net.h:38
coap_mid_t id
CoAP message id.
Definition: net.h:44
coap_tick_t t
when to send PDU for the next time
Definition: net.h:39
unsigned char retransmit_cnt
retransmission counter, will be removed when zero
Definition: net.h:40
Abstraction of resource that can be attached to coap_context_t.
coap_str_const_t ** proxy_name_list
Array valid names this host is known by (proxy support)
coap_str_const_t * uri_path
Request URI Path for this resource.
coap_method_handler_t handler[7]
Used to store handlers for the seven coap methods GET, POST, PUT, DELETE, FETCH, PATCH and IPATCH.
unsigned int is_proxy_uri
resource created for proxy URI handler
unsigned int is_unknown
resource created for unknown handler
unsigned int observable
can be observed
size_t proxy_name_count
Count of valid names this host is known by (proxy support)
coap_tick_t last_pong
Definition: coap_session.h:90
coap_lg_xmit_t * lg_xmit
list of large transmissions
Definition: coap_session.h:80
coap_bin_const_t * psk_key
If client, this field contains the current pre-shared key for server; When this field is NULL,...
Definition: coap_session.h:103
coap_endpoint_t * endpoint
session's endpoint
Definition: coap_session.h:72
coap_tick_t last_rx_tx
Definition: coap_session.h:87
coap_socket_t sock
socket object for the session, if any
Definition: coap_session.h:71
unsigned int max_retransmit
maximum re-transmit count (default 4)
Definition: coap_session.h:122
coap_pdu_t * partial_pdu
incomplete incoming pdu
Definition: coap_session.h:86
coap_bin_const_t * psk_identity
If client, this field contains the current identity for server; When this field is NULL,...
Definition: coap_session.h:94
coap_session_state_t state
current state of relationaship with peer
Definition: coap_session.h:63
uint8_t block_mode
Zero or more COAP_BLOCK_ or'd options.
Definition: coap_session.h:127
uint8_t read_header[8]
storage space for header of incoming message header
Definition: coap_session.h:84
coap_addr_tuple_t addr_info
key: remote/local address info
Definition: coap_session.h:69
coap_proto_t proto
protocol used
Definition: coap_session.h:61
coap_bin_const_t * psk_hint
If client, this field contains the server provided identity hint.
Definition: coap_session.h:112
coap_dtls_cpsk_t cpsk_setup_data
client provided PSK initial setup data
Definition: coap_session.h:92
struct coap_queue_t * delayqueue
list of delayed messages waiting to be sent
Definition: coap_session.h:79
struct coap_context_t * context
session's context
Definition: coap_session.h:73
size_t partial_read
if > 0 indicates number of bytes already read for an incoming message
Definition: coap_session.h:85
void * tls
security parameters
Definition: coap_session.h:74
uint8_t csm_block_supported
CSM TCP blocks supported.
Definition: coap_session.h:77
uint8_t con_active
Active CON request sent.
Definition: coap_session.h:76
coap_mid_t last_ping_mid
the last keepalive message id that was used in this session
Definition: coap_session.h:78
coap_tick_t last_ping
Definition: coap_session.h:89
coap_lg_crcv_t * lg_crcv
Client list of expected large receives.
Definition: coap_session.h:81
coap_session_type_t type
client or server side socket
Definition: coap_session.h:62
coap_tick_t last_tx_rst
Definition: coap_session.h:88
size_t partial_write
if > 0 indicates number of bytes already written from the pdu at the head of sendqueue
Definition: coap_session.h:83
coap_tick_t csm_tx
Definition: coap_session.h:91
coap_endpoint_t * endpoint
Definition: coap_io.h:78
coap_fd_t fd
Definition: coap_io.h:68
struct coap_session_t * session
Definition: coap_io.h:74
coap_socket_flags_t flags
Definition: coap_io.h:73
CoAP string data definition with const data.
Definition: str.h:38
const uint8_t * s
read-only string data
Definition: str.h:40
size_t length
length of string
Definition: str.h:39
CoAP string data definition.
Definition: str.h:30
uint8_t * s
string data
Definition: str.h:32
size_t length
length of string
Definition: str.h:31
Subscriber information.
uint16_t mid
request type code (GET/FETCH)
unsigned char token[8]
token used for subscription
size_t token_length
actual length of token
struct coap_session_t * session
subscriber session
Representation of parsed URI.
Definition: uri.h:38
coap_str_const_t host
host part of the URI
Definition: uri.h:39
coap_string_t * coap_get_uri_path(const coap_pdu_t *request)
Definition: uri.c:604
coap_string_t * coap_get_query(const coap_pdu_t *request)
Definition: uri.c:554
#define HASH_ITER(hh, head, el, tmp)
Definition: uthash.h:1059
#define LL_DELETE(head, del)
Definition: utlist.h:385
#define LL_FOREACH(head, el)
Definition: utlist.h:413
#define LL_PREPEND(head, add)
Definition: utlist.h:314
#define LL_FOREACH_SAFE(head, el, tmp)
Definition: utlist.h:419