1 /* net.c -- CoAP network interface
3 * Copyright (C) 2010--2014 Olaf Bergmann <bergmann@tzi.org>
5 * This file is part of the CoAP library libcoap. Please see
6 * README for terms of use.
18 #elif HAVE_SYS_UNISTD_H
19 #include <sys/unistd.h>
21 #ifdef HAVE_SYS_TYPES_H
22 #include <sys/types.h>
24 #ifdef HAVE_SYS_SOCKET_H
25 #include <sys/socket.h>
27 #ifdef HAVE_NETINET_IN_H
28 #include <netinet/in.h>
30 #ifdef HAVE_ARPA_INET_H
31 #include <arpa/inet.h>
35 #include <lwip/pbuf.h>
37 #include <lwip/timers.h>
50 #if defined(WITH_POSIX) || defined(WITH_ARDUINO)
52 time_t clock_offset=0;
54 static inline coap_queue_t *
57 return (coap_queue_t *)coap_malloc(sizeof(coap_queue_t));
61 coap_free_node(coap_queue_t *node)
65 #endif /* WITH_POSIX || WITH_ARDUINO */
68 #include <lwip/memp.h>
70 static void coap_retransmittimer_execute(void *arg);
71 static void coap_retransmittimer_restart(coap_context_t *ctx);
73 static inline coap_queue_t *
76 return (coap_queue_t *)memp_malloc(MEMP_COAP_NODE);
80 coap_free_node(coap_queue_t *node)
82 memp_free(MEMP_COAP_NODE, node);
85 #endif /* WITH_LWIP */
88 # define DEBUG DEBUG_PRINT
92 #include "net/uip-debug.h"
94 clock_time_t clock_offset;
96 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
97 #define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[UIP_LLIPH_LEN])
99 void coap_resources_init();
100 void coap_pdu_resources_init();
102 unsigned char initialized = 0;
103 coap_context_t the_coap_context;
105 MEMB(node_storage, coap_queue_t, COAP_PDU_MAXCNT);
107 PROCESS(coap_retransmit_process, "message retransmit process");
109 static inline coap_queue_t *
112 return (coap_queue_t *)memb_alloc(&node_storage);
116 coap_free_node(coap_queue_t *node)
118 memb_free(&node_storage, node);
120 #endif /* WITH_CONTIKI */
123 /** Callback to udp_recv when using lwIP. Gets called by lwIP on arriving
124 * packages, places a reference in context->pending_package, and calls
125 * coap_read to process the package. Thus, coap_read needs not be called in
126 * lwIP main loops. (When modifying this for thread-like operation, ie. if you
127 * remove the coap_read call from this, make sure that coap_read gets a chance
128 * to run before this callback is entered the next time.)
130 static void received_package(void *arg, struct udp_pcb *upcb, struct pbuf *p, ip_addr_t *addr, u16_t port)
132 struct coap_context_t *context = (coap_context_t *)arg;
134 LWIP_ASSERT("pending_package was not cleared.", context->pending_package == NULL);
136 context->pending_package = p; /* we don't free it, coap_read has to do that */
137 context->pending_address.addr = addr->addr; /* FIXME: this has to become address-type independent, probably there'll be an lwip function for that */
138 context->pending_port = port;
143 #endif /* WITH_LWIP */
145 int print_wellknown(coap_context_t *, unsigned char *, size_t *, size_t, coap_opt_t *);
147 void coap_handle_failed_notify(coap_context_t *, const coap_address_t *, const str *);
149 unsigned int coap_adjust_basetime(coap_context_t *ctx, coap_tick_t now)
151 unsigned int result = 0;
152 coap_tick_diff_t delta = now - ctx->sendqueue_basetime;
156 /* delta < 0 means that the new time stamp is before the old. */
159 ctx->sendqueue->t -= delta;
163 /* This case is more complex: The time must be advanced forward,
164 * thus possibly leading to timed out elements at the queue's
165 * start. For every element that has timed out, its relative
166 * time is set to zero and the result counter is increased. */
168 coap_queue_t *q = ctx->sendqueue;
170 while (q && (t + q->t < (coap_tick_t) delta))
178 /* finally adjust the first element that has not expired */
181 q->t = (coap_tick_t) delta - t;
186 /* adjust basetime */
187 ctx->sendqueue_basetime += delta;
192 int coap_insert_node(coap_queue_t **queue, coap_queue_t *node)
198 /* set queue head if empty */
205 /* replace queue head if PDU's time is less than head's time */
211 q->t -= node->t; /* make q->t relative to node->t */
215 /* search for right place to insert */
218 node->t -= q->t; /* make node-> relative to q->t */
221 } while (q && q->t <= node->t);
223 /* insert new item */
226 q->t -= node->t; /* make q->t relative to node->t */
233 int coap_delete_node(coap_queue_t *node)
238 coap_delete_pdu(node->pdu);
239 coap_free_node(node);
244 void coap_delete_all(coap_queue_t *queue)
249 coap_delete_all(queue->next);
250 coap_delete_node(queue);
257 node = coap_malloc_node();
262 coap_log(LOG_WARNING, "coap_new_node: malloc\n");
267 memset(node, 0, sizeof *node);
272 coap_peek_next(coap_context_t *context)
274 if (!context || !context->sendqueue)
277 return context->sendqueue;
281 coap_pop_next(coap_context_t *context)
285 if (!context || !context->sendqueue)
288 next = context->sendqueue;
289 context->sendqueue = context->sendqueue->next;
290 if (context->sendqueue)
292 context->sendqueue->t += next->t;
298 #ifdef COAP_DEFAULT_WKC_HASHKEY
299 /** Checks if @p Key is equal to the pre-defined hash key for.well-known/core. */
300 #define is_wkc(Key) \
301 (memcmp((Key), COAP_DEFAULT_WKC_HASHKEY, sizeof(coap_key_t)) == 0)
303 /* Implements a singleton to store a hash key for the .wellknown/core
308 static coap_key_t wkc;
309 static unsigned char _initialized = 0;
312 _initialized = coap_hash_path((unsigned char *)COAP_DEFAULT_URI_WELLKNOWN,
313 sizeof(COAP_DEFAULT_URI_WELLKNOWN) - 1, wkc);
315 return memcmp(k, wkc, sizeof(coap_key_t)) == 0;
322 coap_new_context(const coap_address_t *listen_addr)
324 #if defined(WITH_POSIX)
325 coap_context_t *c = coap_malloc( sizeof( coap_context_t ) );
328 coap_context_t *c =NULL;
334 coap_context_t *c = memp_malloc(MEMP_COAP_CONTEXT);
335 #endif /* WITH_POSIX */
338 coap_log(LOG_EMERG, "no listen address specified\n");
339 #if defined(WITH_POSIX)
340 coap_free_context(c);
349 prng_init(LWIP_RAND());
350 #else /* WITH_LWIP */
351 prng_init((unsigned long)listen_addr ^ clock_offset);
352 #endif /* WITH_LWIP */
358 coap_log(LOG_EMERG, "coap_init: malloc:\n");
362 #endif /* not WITH_CONTIKI */
364 coap_resources_init();
365 coap_pdu_resources_init();
367 c = &the_coap_context;
369 #endif /* WITH_CONTIKI */
371 memset(c, 0, sizeof(coap_context_t));
373 /* initialize message id */
374 prng((unsigned char *)&c->message_id, sizeof(unsigned short));
376 /* register the critical options that we know */
377 coap_register_option(c, COAP_OPTION_IF_MATCH);
378 coap_register_option(c, COAP_OPTION_URI_HOST);
379 coap_register_option(c, COAP_OPTION_IF_NONE_MATCH);
380 coap_register_option(c, COAP_OPTION_URI_PORT);
381 coap_register_option(c, COAP_OPTION_URI_PATH);
382 coap_register_option(c, COAP_OPTION_URI_QUERY);
383 coap_register_option(c, COAP_OPTION_ACCEPT);
384 coap_register_option(c, COAP_OPTION_PROXY_URI);
385 coap_register_option(c, COAP_OPTION_PROXY_SCHEME);
386 coap_register_option(c, COAP_OPTION_BLOCK2);
387 coap_register_option(c, COAP_OPTION_BLOCK1);
389 #if defined(WITH_POSIX) || defined(WITH_ARDUINO)
390 c->sockfd = socket(listen_addr->addr.sa.sa_family, SOCK_DGRAM, 0);
394 coap_log(LOG_EMERG, "coap_new_context: socket\n");
395 #endif /* WITH_POSIX */
399 if ( setsockopt( c->sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse) ) < 0 )
402 coap_log(LOG_WARNING, "setsockopt SO_REUSEADDR\n");
406 if (bind(c->sockfd, &listen_addr->addr.sa, listen_addr->size) < 0)
409 coap_log(LOG_EMERG, "coap_new_context: bind\n");
417 if ( c->sockfd >= 0 )
422 #endif /* WITH_POSIX || WITH_ARDUINO */
424 c->conn = udp_new(NULL, 0, NULL);
425 udp_bind(c->conn, listen_addr->port);
427 process_start(&coap_retransmit_process, (char *)c);
429 PROCESS_CONTEXT_BEGIN(&coap_retransmit_process);
430 #ifndef WITHOUT_OBSERVE
431 etimer_set(&c->notify_timer, COAP_RESOURCE_CHECK_TIME * COAP_TICKS_PER_SECOND);
432 #endif /* WITHOUT_OBSERVE */
433 /* the retransmit timer must be initialized to some large value */
434 etimer_set(&the_coap_context.retransmit_timer, 0xFFFF);
435 PROCESS_CONTEXT_END(&coap_retransmit_process);
437 #endif /* WITH_CONTIKI */
440 /* hard assert: this is not expected to fail dynamically */
441 LWIP_ASSERT("Failed to allocate PCB for CoAP", c->pcb != NULL);
443 udp_recv(c->pcb, received_package, (void*)c);
444 udp_bind(c->pcb, &listen_addr->addr, listen_addr->port);
446 c->timer_configured = 0;
452 void coap_free_context(coap_context_t *context)
454 #if defined(WITH_POSIX) || defined(WITH_LWIP)
455 coap_resource_t *res;
456 #ifndef COAP_RESOURCES_NOHASH
457 coap_resource_t *rtmp;
459 #endif /* WITH_POSIX || WITH_LWIP */
463 coap_delete_all(context->recvqueue);
464 coap_delete_all(context->sendqueue);
467 context->sendqueue = NULL;
468 coap_retransmittimer_restart(context);
471 #if defined(WITH_POSIX) || defined(WITH_LWIP) || defined(WITH_ARDUINO)
472 #ifdef COAP_RESOURCES_NOHASH
473 LL_FOREACH(context->resources, res)
476 HASH_ITER(hh, context->resources, res, rtmp)
479 coap_delete_resource(context, res->key);
481 #endif /* WITH_POSIX || WITH_LWIP */
484 /* coap_delete_list(context->subscriptions); */
485 close( context->sockfd );
486 coap_free( context );
489 udp_remove(context->pcb);
490 memp_free(MEMP_COAP_CONTEXT, context);
493 memset(&the_coap_context, 0, sizeof(coap_context_t));
495 #endif /* WITH_CONTIKI */
497 #endif //ifndef WITH_ARDUINO
498 int coap_option_check_critical(coap_context_t *ctx, coap_pdu_t *pdu, coap_opt_filter_t unknown)
501 coap_opt_iterator_t opt_iter;
504 coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL, coap_udp);
506 while (coap_option_next(&opt_iter))
509 /* The following condition makes use of the fact that
510 * coap_option_getb() returns -1 if type exceeds the bit-vector
511 * filter. As the vector is supposed to be large enough to hold
512 * the largest known option, we know that everything beyond is
515 if (opt_iter.type & 0x01 && coap_option_getb(ctx->known_options, opt_iter.type) < 1)
517 debug("unknown critical option %d\n", opt_iter.type);
521 /* When opt_iter.type is beyond our known option range,
522 * coap_option_setb() will return -1 and we are safe to leave
524 if (coap_option_setb(unknown, opt_iter.type) == -1)
532 void coap_transaction_id(const coap_address_t *peer, const coap_pdu_t *pdu, coap_tid_t *id)
536 memset(h, 0, sizeof(coap_key_t));
538 /* Compare the complete address structure in case of IPv4. For IPv6,
539 * we need to look at the transport address only. */
542 switch (peer->addr.sa.sa_family)
545 coap_hash((const unsigned char *)&peer->addr.sa, peer->size, h);
548 coap_hash((const unsigned char *)&peer->addr.sin6.sin6_port,
549 sizeof(peer->addr.sin6.sin6_port), h);
550 coap_hash((const unsigned char *)&peer->addr.sin6.sin6_addr,
551 sizeof(peer->addr.sin6.sin6_addr), h);
559 coap_hash((const unsigned char *)peer->addr, peer->size, h);
560 #endif /* WITH_ARDUINO */
562 #if defined(WITH_LWIP) || defined(WITH_CONTIKI)
563 /* FIXME: with lwip, we can do better */
564 coap_hash((const unsigned char *)&peer->port, sizeof(peer->port), h);
565 coap_hash((const unsigned char *)&peer->addr, sizeof(peer->addr), h);
566 #endif /* WITH_LWIP || WITH_CONTIKI */
568 coap_hash((const unsigned char *)&pdu->hdr->coap_hdr_udp_t.id, sizeof(unsigned short), h);
570 *id = ((h[0] << 8) | h[1]) ^ ((h[2] << 8) | h[3]);
573 coap_tid_t coap_send_ack(coap_context_t *context, const coap_address_t *dst, coap_pdu_t *request)
575 coap_pdu_t *response;
576 coap_tid_t result = COAP_INVALID_TID;
578 if (request && request->hdr->coap_hdr_udp_t.type == COAP_MESSAGE_CON)
580 response = coap_pdu_init(COAP_MESSAGE_ACK, 0, request->hdr->coap_hdr_udp_t.id,
581 sizeof(coap_pdu_t), coap_udp);
584 result = coap_send(context, dst, response);
585 coap_delete_pdu(response);
591 #if defined(WITH_ARDUINO)
593 coap_send_impl(coap_context_t *context,
594 const coap_address_t *dst,
601 #if defined(WITH_POSIX)
602 /* releases space allocated by PDU if free_pdu is set */
604 coap_send_impl(coap_context_t *context,
605 const coap_address_t *dst,
608 ssize_t bytes_written;
609 coap_tid_t id = COAP_INVALID_TID;
611 if ( !context || !dst || !pdu )
614 bytes_written = sendto( context->sockfd, pdu->hdr, pdu->length, 0,
615 &dst->addr.sa, dst->size);
617 if (bytes_written >= 0)
619 coap_transaction_id(dst, pdu, &id);
623 coap_log(LOG_CRIT, "coap_send: sendto\n");
628 #endif /* WITH_POSIX || WITH_ARDUINO */
630 /* releases space allocated by PDU if free_pdu is set */
632 coap_send_impl(coap_context_t *context,
633 const coap_address_t *dst,
636 coap_tid_t id = COAP_INVALID_TID;
638 if ( !context || !dst || !pdu )
641 /* FIXME: is there a way to check if send was successful? */
642 uip_udp_packet_sendto(context->conn, pdu->hdr, pdu->length,
643 &dst->addr, dst->port);
645 coap_transaction_id(dst, pdu, &id);
649 #endif /* WITH_CONTIKI */
652 coap_send_impl(coap_context_t *context,
653 const coap_address_t *dst,
656 coap_tid_t id = COAP_INVALID_TID;
661 if ( !context || !dst || !pdu )
666 data_backup = pdu->data;
668 /* FIXME: we can't check this here with the existing infrastructure, but we
669 * should actually check that the pdu is not held by anyone but us. the
670 * respective pbuf is already exclusively owned by the pdu. */
673 LWIP_ASSERT("The PDU header is not where it is expected", pdu->hdr == p->payload + sizeof(coap_pdu_t));
675 err = pbuf_header(p, -sizeof(coap_pdu_t));
678 debug("coap_send_impl: pbuf_header failed\n");
683 coap_transaction_id(dst, pdu, &id);
685 pbuf_realloc(p, pdu->length);
687 udp_sendto(context->pcb, p,
688 &dst->addr, dst->port);
690 pbuf_header(p, -(ptrdiff_t)((uint8_t*)pdu - (uint8_t*)p->payload) - sizeof(coap_pdu_t)); /* FIXME hack around udp_sendto not restoring; see http://lists.gnu.org/archive/html/lwip-users/2013-06/msg00008.html. for udp over ip over ethernet, this was -42; as we're doing ppp too, this has to be calculated generically */
692 err = pbuf_header(p, sizeof(coap_pdu_t));
693 LWIP_ASSERT("Cannot undo pbuf_header", err == 0);
695 /* restore destroyed pdu data */
696 LWIP_ASSERT("PDU not restored", p->payload == pdu);
697 pdu->max_size = p->tot_len - sizeof(coap_pdu_t); /* reduced after pbuf_realloc */
698 pdu->hdr = p->payload + sizeof(coap_pdu_t);
699 pdu->max_delta = 0; /* won't be used any more */
700 pdu->length = pdu->max_size;
701 pdu->data = data_backup;
706 #endif /* WITH_LWIP */
708 coap_tid_t coap_send(coap_context_t *context, const coap_address_t *dst, coap_pdu_t *pdu)
711 return coap_send_impl(context, dst, pdu);
715 coap_tid_t coap_send_error(coap_context_t *context, coap_pdu_t *request, const coap_address_t *dst,
716 unsigned char code, coap_opt_filter_t opts)
718 coap_pdu_t *response;
719 coap_tid_t result = COAP_INVALID_TID;
724 response = coap_new_error_response(request, code, opts);
727 result = coap_send(context, dst, response);
728 coap_delete_pdu(response);
734 coap_tid_t coap_send_message_type(coap_context_t *context, const coap_address_t *dst,
735 coap_pdu_t *request, unsigned char type)
737 coap_pdu_t *response;
738 coap_tid_t result = COAP_INVALID_TID;
742 response = coap_pdu_init(type, 0, request->hdr->coap_hdr_udp_t.id,
743 sizeof(coap_pdu_t), coap_udp);
746 result = coap_send(context, dst, response);
747 coap_delete_pdu(response);
753 coap_tid_t coap_send_confirmed(coap_context_t *context, const coap_address_t *dst, coap_pdu_t *pdu)
759 node = coap_new_node();
762 debug("coap_send_confirmed: insufficient memory\n");
763 return COAP_INVALID_TID;
766 node->id = coap_send_impl(context, dst, pdu);
767 if (COAP_INVALID_TID == node->id)
769 debug("coap_send_confirmed: error sending pdu\n");
770 coap_free_node(node);
771 return COAP_INVALID_TID;
774 prng((unsigned char *)&r, sizeof(r));
776 /* add randomized RESPONSE_TIMEOUT to determine retransmission timeout */
777 node->timeout = COAP_DEFAULT_RESPONSE_TIMEOUT * COAP_TICKS_PER_SECOND
778 + (COAP_DEFAULT_RESPONSE_TIMEOUT >> 1) * ((COAP_TICKS_PER_SECOND * (r & 0xFF)) >> 8);
780 memcpy(&node->remote, dst, sizeof(coap_address_t));
783 /* Set timer for pdu retransmission. If this is the first element in
784 * the retransmission queue, the base time is set to the current
785 * time and the retransmission time is node->timeout. If there is
786 * already an entry in the sendqueue, we must check if this node is
787 * to be retransmitted earlier. Therefore, node->timeout is first
788 * normalized to the base time and then inserted into the queue with
789 * an adjusted relative time.
791 if (context->sendqueue == NULL)
793 node->t = node->timeout;
794 context->sendqueue_basetime = now;
798 /* make node->t relative to context->sendqueue_basetime */
799 node->t = (now - context->sendqueue_basetime) + node->timeout;
802 coap_insert_node(&context->sendqueue, node);
805 if (node == context->sendqueue) /* don't bother with timer stuff if there are earlier retransmits */
806 coap_retransmittimer_restart(context);
810 { /* (re-)initialize retransmission timer */
811 coap_queue_t *nextpdu;
813 nextpdu = coap_peek_next(context);
814 assert(nextpdu); /* we have just inserted a node */
816 /* must set timer within the context of the retransmit process */
817 PROCESS_CONTEXT_BEGIN(&coap_retransmit_process);
818 etimer_set(&context->retransmit_timer, nextpdu->t);
819 PROCESS_CONTEXT_END(&coap_retransmit_process);
821 #endif /* WITH_CONTIKI */
826 coap_tid_t coap_retransmit(coap_context_t *context, coap_queue_t *node)
828 if (!context || !node)
829 return COAP_INVALID_TID;
831 /* re-initialize timeout when maximum number of retransmissions are not reached yet */
832 if (node->retransmit_cnt < COAP_DEFAULT_MAX_RETRANSMIT)
834 node->retransmit_cnt++;
835 node->t = node->timeout << node->retransmit_cnt;
836 coap_insert_node(&context->sendqueue, node);
838 /* don't bother with timer stuff if there are earlier retransmits */
839 if (node == context->sendqueue)
840 coap_retransmittimer_restart(context);
844 "** retransmission #%d of transaction %d\n", node->retransmit_cnt,
845 ntohs(node->pdu->hdr->coap_hdr_udp_t.id));
847 node->id = coap_send_impl(context, &node->remote, node->pdu);
851 /* no more retransmissions, remove node from system */
853 #if !defined(WITH_CONTIKI) && !defined(WITH_ARDUINO)
854 debug("** removed transaction %d\n", ntohs(node->id));
857 #ifndef WITHOUT_OBSERVE
858 /* Check if subscriptions exist that should be canceled after
859 COAP_MAX_NOTIFY_FAILURES */
860 if (node->pdu->hdr->coap_hdr_udp_t.code >= 64)
865 token.length = node->pdu->hdr->coap_hdr_udp_t.token_length;
866 token.s = node->pdu->hdr->coap_hdr_udp_t.token;
868 coap_handle_failed_notify(context, &node->remote, &token);
870 #endif /* WITHOUT_OBSERVE */
872 /* And finally delete the node */
873 coap_delete_node(node);
874 return COAP_INVALID_TID;
878 * Checks if @p opt fits into the message that ends with @p maxpos.
879 * This function returns @c 1 on success, or @c 0 if the option @p opt
880 * would exceed @p maxpos.
882 static inline int check_opt_size(coap_opt_t *opt, unsigned char *maxpos)
884 if (opt && opt < maxpos)
886 if (((*opt & 0x0f) < 0x0f) || (opt + 1 < maxpos))
887 return opt + COAP_OPT_SIZE(opt) < maxpos;
893 int coap_read(coap_context_t *ctx)
895 #if defined(WITH_POSIX)
896 static char buf[COAP_MAX_PDU_SIZE];
898 #if defined(WITH_LWIP) || defined(WITH_CONTIKI)
902 ssize_t bytes_read = -1;
903 coap_address_t src, dst;
908 #endif /* WITH_CONTIKI */
910 LWIP_ASSERT("No package pending", ctx->pending_package != NULL);
911 LWIP_ASSERT("Can only deal with contiguous PBUFs to read the initial details", ctx->pending_package->tot_len == ctx->pending_package->len);
912 buf = ctx->pending_package->payload;
913 #endif /* WITH_LWIP */
915 pdu = (coap_hdr_t *) buf;
917 coap_address_init(&src);
919 #if defined(WITH_POSIX)
920 bytes_read = recvfrom(ctx->sockfd, buf, sizeof(buf), 0, &src.addr.sa, &src.size);
922 #endif /* WITH_POSIX || WITH_ARDUINO */
926 uip_ipaddr_copy(&src.addr, &UIP_IP_BUF->srcipaddr);
927 src.port = UIP_UDP_BUF->srcport;
928 uip_ipaddr_copy(&dst.addr, &UIP_IP_BUF->destipaddr);
929 dst.port = UIP_UDP_BUF->destport;
931 bytes_read = uip_datalen();
932 ((char *)uip_appdata)[bytes_read] = 0;
933 PRINTF("Server received %d bytes from [", (int)bytes_read);
934 PRINT6ADDR(&src.addr);
935 PRINTF("]:%d\n", uip_ntohs(src.port));
937 #endif /* WITH_CONTIKI */
939 /* FIXME: use lwip address operation functions */
940 src.addr.addr = ctx->pending_address.addr;
941 src.port = ctx->pending_port;
942 bytes_read = ctx->pending_package->tot_len;
943 #endif /* WITH_LWIP */
947 warn("coap_read: recvfrom");
951 if ((size_t) bytes_read < sizeof(coap_hdr_t))
953 debug("coap_read: discarded invalid frame\n");
957 if (pdu->coap_hdr_udp_t.version != COAP_DEFAULT_VERSION)
959 debug("coap_read: unknown protocol version\n");
963 node = coap_new_node();
968 node->pdu = coap_pdu_from_pbuf(ctx->pending_package);
969 ctx->pending_package = NULL;
971 node->pdu = coap_pdu_init(0, 0, 0, bytes_read, coap_udp);
976 coap_ticks(&node->t);
977 memcpy(&node->local, &dst, sizeof(coap_address_t));
978 memcpy(&node->remote, &src, sizeof(coap_address_t));
980 if (!coap_pdu_parse((unsigned char *) buf, bytes_read, node->pdu, coap_udp))
982 warn("discard malformed PDU");
986 /* and add new node to receive queue */
987 coap_transaction_id(&node->remote, node->pdu, &node->id);
988 coap_insert_node(&ctx->recvqueue, node);
991 if (LOG_DEBUG <= coap_get_log_level())
993 #ifndef INET6_ADDRSTRLEN
994 #define INET6_ADDRSTRLEN 40
996 unsigned char addr[INET6_ADDRSTRLEN + 8];
998 if (coap_print_addr(&src, addr, INET6_ADDRSTRLEN + 8))
999 debug("** received %d bytes from %s:\n", (int)bytes_read, addr);
1001 coap_show_pdu(node->pdu);
1008 /* FIXME: send back RST? */
1009 coap_delete_node(node);
1013 /* even if there was an error, clean up */
1014 pbuf_free(ctx->pending_package);
1015 ctx->pending_package = NULL;
1019 #endif //#ifndef WITH_ARDUINO
1021 int coap_remove_from_queue(coap_queue_t **queue, coap_tid_t id, coap_queue_t **node)
1023 coap_queue_t *p, *q;
1025 if (!queue || !*queue)
1028 /* replace queue head if PDU's time is less than head's time */
1030 if (id == (*queue)->id)
1031 { /* found transaction */
1033 *queue = (*queue)->next;
1035 { /* adjust relative time of new queue head */
1036 (*queue)->t += (*node)->t;
1038 (*node)->next = NULL;
1039 /* coap_delete_node( q ); */
1040 debug("*** removed transaction %u\n", id);
1044 /* search transaction to remove (only first occurence will be removed) */
1050 } while (q && id != q->id);
1053 { /* found transaction */
1056 { /* must update relative time of p->next */
1061 /* coap_delete_node( q ); */
1062 debug("*** removed transaction %u\n", id);
1070 static inline int token_match(const unsigned char *a, size_t alen, const unsigned char *b,
1073 return alen == blen && (alen == 0 || memcmp(a, b, alen) == 0);
1076 void coap_cancel_all_messages(coap_context_t *context, const coap_address_t *dst,
1077 const unsigned char *token, size_t token_length)
1079 /* cancel all messages in sendqueue that are for dst
1080 * and use the specified token */
1081 coap_queue_t *p, *q;
1083 debug("cancel_all_messages\n");
1084 while (context->sendqueue && coap_address_equals(dst, &context->sendqueue->remote)
1085 && token_match(token, token_length, context->sendqueue->pdu->hdr->coap_hdr_udp_t.token,
1086 context->sendqueue->pdu->hdr->coap_hdr_udp_t.token_length))
1088 q = context->sendqueue;
1089 context->sendqueue = q->next;
1090 debug("**** removed transaction %d\n", ntohs(q->pdu->hdr->coap_hdr_udp_t.id));
1091 coap_delete_node(q);
1094 if (!context->sendqueue)
1097 p = context->sendqueue;
1100 /* when q is not NULL, it does not match (dst, token), so we can skip it */
1103 if (coap_address_equals(dst, &q->remote)
1104 && token_match(token, token_length, q->pdu->hdr->coap_hdr_udp_t.token,
1105 q->pdu->hdr->coap_hdr_udp_t.token_length))
1108 debug("**** removed transaction %d\n", ntohs(q->pdu->hdr->coap_hdr_udp_t.id));
1109 coap_delete_node(q);
1121 coap_find_transaction(coap_queue_t *queue, coap_tid_t id)
1123 while (queue && queue->id != id)
1124 queue = queue->next;
1130 coap_new_error_response(coap_pdu_t *request, unsigned char code, coap_opt_filter_t opts)
1132 coap_opt_iterator_t opt_iter;
1133 coap_pdu_t *response;
1134 size_t size = sizeof(coap_hdr_t) + request->hdr->coap_hdr_udp_t.token_length;
1137 unsigned short opt_type = 0; /* used for calculating delta-storage */
1139 #if COAP_ERROR_PHRASE_LENGTH > 0
1140 char *phrase = coap_response_phrase(code);
1142 /* Need some more space for the error phrase and payload start marker */
1144 size += strlen(phrase) + 1;
1149 /* cannot send ACK if original request was not confirmable */
1150 type = request->hdr->coap_hdr_udp_t.type == COAP_MESSAGE_CON ? COAP_MESSAGE_ACK : COAP_MESSAGE_NON;
1152 /* Estimate how much space we need for options to copy from
1153 * request. We always need the Token, for 4.02 the unknown critical
1154 * options must be included as well. */
1155 coap_option_clrb(opts, COAP_OPTION_CONTENT_TYPE); /* we do not want this */
1157 coap_option_iterator_init(request, &opt_iter, opts, coap_udp);
1159 /* Add size of each unknown critical option. As known critical
1160 options as well as elective options are not copied, the delta
1163 while ((option = coap_option_next(&opt_iter)))
1165 unsigned short delta = opt_iter.type - opt_type;
1166 /* calculate space required to encode (opt_iter.type - opt_type) */
1171 else if (delta < 269)
1180 /* add coap_opt_length(option) and the number of additional bytes
1181 * required to encode the option length */
1183 size += coap_opt_length(option);
1184 switch (*option & 0x0f)
1196 opt_type = opt_iter.type;
1199 /* Now create the response and fill with options and payload data. */
1200 response = coap_pdu_init(type, code, request->hdr->coap_hdr_udp_t.id, size, coap_udp);
1204 if (!coap_add_token(response, request->hdr->coap_hdr_udp_t.token_length,
1205 request->hdr->coap_hdr_udp_t.token, coap_udp))
1207 debug("cannot add token to error response\n");
1208 coap_delete_pdu(response);
1212 /* copy all options */
1213 coap_option_iterator_init(request, &opt_iter, opts, coap_udp);
1214 while ((option = coap_option_next(&opt_iter)))
1215 coap_add_option(response, opt_iter.type, COAP_OPT_LENGTH(option),
1216 COAP_OPT_VALUE(option), coap_udp);
1218 #if COAP_ERROR_PHRASE_LENGTH > 0
1219 /* note that diagnostic messages do not need a Content-Format option. */
1221 coap_add_data(response, strlen(phrase), (unsigned char *) phrase);
1229 * Quick hack to determine the size of the resource description for
1232 static inline size_t get_wkc_len(coap_context_t *context, coap_opt_t *query_filter)
1234 unsigned char buf[1];
1237 if (print_wellknown(context, buf, &len, UINT_MAX, query_filter) & COAP_PRINT_STATUS_ERROR)
1239 warn("cannot determine length of /.well-known/core\n");
1243 debug("get_wkc_len: print_wellknown() returned %zu\n", len);
1248 #define SZX_TO_BYTES(SZX) ((size_t)(1 << ((SZX) + 4)))
1251 wellknown_response(coap_context_t *context, coap_pdu_t *request)
1254 coap_opt_iterator_t opt_iter;
1255 size_t len, wkc_len;
1256 unsigned char buf[2];
1258 int need_block2 = 0; /* set to 1 if Block2 option is required */
1260 coap_opt_t *query_filter;
1263 resp = coap_pdu_init(
1264 request->hdr->coap_hdr_udp_t.type == COAP_MESSAGE_CON ? COAP_MESSAGE_ACK : COAP_MESSAGE_NON,
1265 COAP_RESPONSE_CODE(205),
1266 request->hdr->coap_hdr_udp_t.id, COAP_MAX_PDU_SIZE, coap_udp);
1269 debug("wellknown_response: cannot create PDU\n");
1273 if (!coap_add_token(resp, request->hdr->coap_hdr_udp_t.token_length,
1274 request->hdr->coap_hdr_udp_t.token, coap_udp))
1276 debug("wellknown_response: cannot add token\n");
1280 query_filter = coap_check_option(request, COAP_OPTION_URI_QUERY, &opt_iter);
1281 wkc_len = get_wkc_len(context, query_filter);
1283 /* check whether the request contains the Block2 option */
1284 if (coap_get_block(request, COAP_OPTION_BLOCK2, &block))
1286 offset = block.num << (block.szx + 4);
1288 { /* invalid, MUST lead to 4.00 Bad Request */
1289 resp->hdr->coap_hdr_udp_t.code = COAP_RESPONSE_CODE(400);
1292 else if (block.szx > COAP_MAX_BLOCK_SZX)
1294 block.szx = COAP_MAX_BLOCK_SZX;
1295 block.num = offset >> (block.szx + 4);
1301 /* Check if there is sufficient space to add Content-Format option
1302 * and data. We do this before adding the Content-Format option to
1303 * avoid sending error responses with that option but no actual
1305 if (resp->max_size <= (size_t) resp->length + 3)
1307 debug("wellknown_response: insufficient storage space\n");
1311 /* Add Content-Format. As we have checked for available storage,
1312 * nothing should go wrong here. */
1313 assert(coap_encode_var_bytes(buf, COAP_MEDIATYPE_APPLICATION_LINK_FORMAT) == 1);
1314 coap_add_option(resp, COAP_OPTION_CONTENT_FORMAT,
1315 coap_encode_var_bytes(buf, COAP_MEDIATYPE_APPLICATION_LINK_FORMAT), buf, coap_udp);
1317 /* check if Block2 option is required even if not requested */
1318 if (!need_block2 && (resp->max_size - (size_t) resp->length < wkc_len))
1320 assert(resp->length <= resp->max_size);
1321 const size_t payloadlen = resp->max_size - resp->length;
1322 /* yes, need block-wise transfer */
1324 block.m = 0; /* the M bit is set by coap_write_block_opt() */
1325 block.szx = COAP_MAX_BLOCK_SZX;
1326 while (payloadlen < SZX_TO_BYTES(block.szx))
1330 debug("wellknown_response: message to small even for szx == 0\n");
1342 /* write Block2 option if necessary */
1345 if (coap_write_block_opt(&block, COAP_OPTION_BLOCK2, resp, wkc_len) < 0)
1347 debug("wellknown_response: cannot add Block2 option\n");
1352 /* Manually set payload of response to let print_wellknown() write,
1353 * into our buffer without copying data. */
1355 resp->data = (unsigned char *) resp->hdr + resp->length;
1356 *resp->data = COAP_PAYLOAD_START;
1359 len = need_block2 ? SZX_TO_BYTES(block.szx) : resp->max_size - resp->length;
1361 result = print_wellknown(context, resp->data, &len, offset, query_filter);
1362 if ((result & COAP_PRINT_STATUS_ERROR) != 0)
1364 debug("print_wellknown failed\n");
1368 resp->length += COAP_PRINT_OUTPUT_LENGTH(result);
1372 /* set error code 5.03 and remove all options and data from response */
1373 resp->hdr->coap_hdr_udp_t.code = COAP_RESPONSE_CODE(503);
1374 resp->length = sizeof(coap_hdr_t) + resp->hdr->coap_hdr_udp_t.token_length;
1378 #define WANT_WKC(Pdu,Key) \
1379 (((Pdu)->hdr->coap_hdr_udp_t.code == COAP_REQUEST_GET) && is_wkc(Key))
1381 void handle_request(coap_context_t *context, coap_queue_t *node, const char* responseData)
1384 coap_method_handler_t h = NULL;
1385 coap_pdu_t *response = NULL;
1386 coap_opt_filter_t opt_filter;
1387 coap_resource_t *resource;
1390 coap_option_filter_clear(opt_filter);
1392 /* try to find the resource from the request URI */
1393 coap_hash_request_uri(node->pdu, key);
1394 resource = coap_get_resource_from_key(context, key);
1398 /* The resource was not found. Check if the request URI happens to
1399 * be the well-known URI. In that case, we generate a default
1400 * response, otherwise, we return 4.04 */
1402 switch (node->pdu->hdr->coap_hdr_udp_t.code)
1405 case COAP_REQUEST_GET:
1407 { /* GET request for .well-known/core */
1408 info("create default response for %s\n", COAP_DEFAULT_URI_WELLKNOWN);
1410 response = wellknown_response(context, node->pdu);
1414 { /* GET request for any another resource, return 4.04 */
1417 "GET for unknown resource 0x%02x%02x%02x%02x, return 4.04\n", key[0], key[1], key[2], key[3]);
1418 response = coap_new_error_response(node->pdu, COAP_RESPONSE_CODE(404),
1423 default: /* any other request type */
1426 "unhandled request for unknown resource 0x%02x%02x%02x%02x\r\n", key[0], key[1], key[2], key[3]);
1427 if (!coap_is_mcast(&node->local))
1428 response = coap_new_error_response(node->pdu, COAP_RESPONSE_CODE(405),
1432 if (response && coap_send(context, &node->remote, response) == COAP_INVALID_TID)
1434 warn("cannot send response for transaction %u\n", node->id);
1436 coap_delete_pdu(response);
1441 /* the resource was found, check if there is a registered handler */
1442 if ((size_t) node->pdu->hdr->coap_hdr_udp_t.code - 1
1443 < sizeof(resource->handler) / sizeof(coap_method_handler_t))
1444 h = resource->handler[node->pdu->hdr->coap_hdr_udp_t.code - 1];
1449 "call custom handler for resource 0x%02x%02x%02x%02x\n", key[0], key[1], key[2], key[3]);
1450 response = coap_pdu_init(
1451 node->pdu->hdr->coap_hdr_udp_t.type ==
1452 COAP_MESSAGE_CON ? COAP_MESSAGE_ACK : COAP_MESSAGE_NON,
1453 0, node->pdu->hdr->coap_hdr_udp_t.id, COAP_MAX_PDU_SIZE, coap_udp);
1455 /* Implementation detail: coap_add_token() immediately returns 0
1456 if response == NULL */
1457 if (coap_add_token(response, node->pdu->hdr->coap_hdr_udp_t.token_length,
1458 node->pdu->hdr->coap_hdr_udp_t.token, coap_udp))
1461 { node->pdu->hdr->coap_hdr_udp_t.token_length, node->pdu->hdr->coap_hdr_udp_t.token };
1463 h(context, resource, &node->remote, node->pdu, &token, response);
1465 unsigned char buf[3];
1466 response->hdr->coap_hdr_udp_t.code = COAP_RESPONSE_CODE(205);
1467 coap_add_option(response, COAP_OPTION_CONTENT_TYPE,
1468 coap_encode_var_bytes(buf, COAP_MEDIATYPE_TEXT_PLAIN), buf, coap_udp);
1469 coap_add_option(response, COAP_OPTION_MAXAGE, coap_encode_var_bytes(buf, 0x2ffff), buf, coap_udp);
1470 coap_add_data(response, strlen(responseData), (unsigned char *) responseData);
1472 if (response->hdr->coap_hdr_udp_t.type != COAP_MESSAGE_NON
1473 || (response->hdr->coap_hdr_udp_t.code >= 64 && !coap_is_mcast(&node->local)))
1476 if (coap_send(context, &node->remote, response) == COAP_INVALID_TID)
1478 debug("cannot send response for message %d\n", node->pdu->hdr->coap_hdr_udp_t.id);
1482 coap_delete_pdu(response);
1486 warn("cannot generate response\r\n");
1491 if (WANT_WKC(node->pdu, key))
1493 debug("create default response for %s\n", COAP_DEFAULT_URI_WELLKNOWN);
1494 response = wellknown_response(context, node->pdu);
1497 response = coap_new_error_response(node->pdu, COAP_RESPONSE_CODE(405), opt_filter);
1499 if (!response || (coap_send(context, &node->remote, response) == COAP_INVALID_TID))
1501 debug("cannot send response for transaction %u\n", node->id);
1503 coap_delete_pdu(response);
1507 static inline void handle_response(coap_context_t *context, coap_queue_t *sent, coap_queue_t *rcvd)
1510 /* Call application-specific reponse handler when available. If
1511 * not, we must acknowledge confirmable messages. */
1512 if (context->response_handler)
1515 context->response_handler(context, &rcvd->remote, sent ? sent->pdu : NULL, rcvd->pdu,
1520 /* send ACK if rcvd is confirmable (i.e. a separate response) */
1521 coap_send_ack(context, &rcvd->remote, rcvd->pdu);
1527 handle_locally(coap_context_t *context __attribute__ ((unused)),
1528 coap_queue_t *node __attribute__ ((unused)))
1530 #else /* not a GCC */
1531 handle_locally(coap_context_t *context, coap_queue_t *node)
1534 /* this function can be used to check if node->pdu is really for us */
1539 * This function handles RST messages received for the message passed
1542 static void coap_handle_rst(coap_context_t *context, const coap_queue_t *sent)
1544 #ifndef WITHOUT_OBSERVE
1546 #ifndef COAP_RESOURCES_NOHASH
1547 coap_resource_t *tmp;
1552 /* remove observer for this resource, if any
1553 * get token from sent and try to find a matching resource. Uh!
1556 COAP_SET_STR(&token, sent->pdu->hdr->coap_hdr_udp_t.token_length,
1557 sent->pdu->hdr->coap_hdr_udp_t.token);
1559 #ifndef WITH_CONTIKI
1560 #ifdef COAP_RESOURCES_NOHASH
1561 LL_FOREACH(context->resources, r)
1564 HASH_ITER(hh, context->resources, r, tmp)
1567 coap_delete_observer(r, &sent->remote, &token);
1568 coap_cancel_all_messages(context, &sent->remote, token.s, token.length);
1570 #else /* WITH_CONTIKI */
1571 r = (coap_resource_t *)resource_storage.mem;
1572 for (i = 0; i < resource_storage.num; ++i, ++r)
1574 if (resource_storage.count[i])
1576 coap_delete_observer(r, &sent->remote, &token);
1577 coap_cancel_all_messages(context, &sent->remote, token.s, token.length);
1580 #endif /* WITH_CONTIKI */
1581 #endif /* WITOUT_OBSERVE */
1584 void coap_dispatch(coap_context_t *context, const char* responseData)
1586 coap_queue_t *rcvd = NULL, *sent = NULL;
1587 coap_pdu_t *response;
1588 coap_opt_filter_t opt_filter;
1593 memset(opt_filter, 0, sizeof(coap_opt_filter_t));
1595 while (context->recvqueue)
1597 rcvd = context->recvqueue;
1599 /* remove node from recvqueue */
1600 context->recvqueue = context->recvqueue->next;
1603 if (rcvd->pdu->hdr->coap_hdr_udp_t.version != COAP_DEFAULT_VERSION)
1605 debug("dropped packet with unknown version %u\n", rcvd->pdu->hdr->coap_hdr_udp_t.version);
1609 switch (rcvd->pdu->hdr->coap_hdr_udp_t.type)
1611 case COAP_MESSAGE_ACK:
1612 /* find transaction in sendqueue to stop retransmission */
1613 coap_remove_from_queue(&context->sendqueue, rcvd->id, &sent);
1615 if (rcvd->pdu->hdr->coap_hdr_udp_t.code == 0)
1618 /* FIXME: if sent code was >= 64 the message might have been a
1619 * notification. Then, we must flag the observer to be alive
1620 * by setting obs->fail_cnt = 0. */
1621 if (sent && COAP_RESPONSE_CLASS(sent->pdu->hdr->coap_hdr_udp_t.code) == 2)
1624 { sent->pdu->hdr->coap_hdr_udp_t.token_length,
1625 sent->pdu->hdr->coap_hdr_udp_t.token };
1627 coap_touch_observer(context, &sent->remote, &token);
1631 case COAP_MESSAGE_RST:
1632 /* We have sent something the receiver disliked, so we remove
1633 * not only the transaction but also the subscriptions we might
1636 coap_log(LOG_ALERT, "got RST for message %u\n",
1637 ntohs(rcvd->pdu->hdr->coap_hdr_udp_t.id));
1639 /* find transaction in sendqueue to stop retransmission */
1640 coap_remove_from_queue(&context->sendqueue, rcvd->id, &sent);
1643 coap_handle_rst(context, sent);
1646 case COAP_MESSAGE_NON: /* check for unknown critical options */
1647 if (coap_option_check_critical(context, rcvd->pdu, opt_filter) == 0)
1651 case COAP_MESSAGE_CON: /* check for unknown critical options */
1652 if (coap_option_check_critical(context, rcvd->pdu, opt_filter) == 0)
1655 /* FIXME: send response only if we have received a request. Otherwise,
1657 response = coap_new_error_response(rcvd->pdu, COAP_RESPONSE_CODE(402),
1661 warn("coap_dispatch: cannot create error reponse\n");
1664 if (coap_send(context, &rcvd->remote, response) == COAP_INVALID_TID)
1666 warn("coap_dispatch: error sending reponse\n");
1668 coap_delete_pdu(response);
1676 /* Pass message to upper layer if a specific handler was
1677 * registered for a request that should be handled locally. */
1678 if (handle_locally(context, rcvd))
1680 if (COAP_MESSAGE_IS_REQUEST(rcvd->pdu->hdr->coap_hdr_udp_t))
1681 handle_request(context, rcvd, responseData);
1682 else if (COAP_MESSAGE_IS_RESPONSE(rcvd->pdu->hdr->coap_hdr_udp_t))
1683 handle_response(context, sent, rcvd);
1686 debug("dropped message with invalid code\n");
1687 coap_send_message_type(context, &rcvd->remote, rcvd->pdu, COAP_MESSAGE_RST);
1691 cleanup: coap_delete_node(sent);
1692 coap_delete_node(rcvd);
1696 int coap_can_exit(coap_context_t *context)
1698 return !context || (context->recvqueue == NULL && context->sendqueue == NULL);
1703 /*---------------------------------------------------------------------------*/
1704 /* CoAP message retransmission */
1705 /*---------------------------------------------------------------------------*/
1706 PROCESS_THREAD(coap_retransmit_process, ev, data)
1709 coap_queue_t *nextpdu;
1713 debug("Started retransmit process\r\n");
1718 if (ev == PROCESS_EVENT_TIMER)
1720 if (etimer_expired(&the_coap_context.retransmit_timer))
1723 nextpdu = coap_peek_next(&the_coap_context);
1726 while (nextpdu && nextpdu->t <= now)
1728 coap_retransmit(&the_coap_context, coap_pop_next(&the_coap_context));
1729 nextpdu = coap_peek_next(&the_coap_context);
1732 /* need to set timer to some value even if no nextpdu is available */
1733 etimer_set(&the_coap_context.retransmit_timer,
1734 nextpdu ? nextpdu->t - now : 0xFFFF);
1736 #ifndef WITHOUT_OBSERVE
1737 if (etimer_expired(&the_coap_context.notify_timer))
1739 coap_check_notify(&the_coap_context);
1740 etimer_reset(&the_coap_context.notify_timer);
1742 #endif /* WITHOUT_OBSERVE */
1748 /*---------------------------------------------------------------------------*/
1750 #endif /* WITH_CONTIKI */
1753 /* FIXME: retransmits that are not required any more due to incoming packages
1754 * do *not* get cleared at the moment, the wakeup when the transmission is due
1755 * is silently accepted. this is mainly due to the fact that the required
1756 * checks are similar in two places in the code (when receiving ACK and RST)
1757 * and that they cause more than one patch chunk, as it must be first checked
1758 * whether the sendqueue item to be dropped is the next one pending, and later
1759 * the restart function has to be called. nothing insurmountable, but it can
1760 * also be implemented when things have stabilized, and the performance
1761 * penality is minimal
1763 * also, this completely ignores COAP_RESOURCE_CHECK_TIME.
1766 static void coap_retransmittimer_execute(void *arg)
1768 coap_context_t *ctx = (coap_context_t*)arg;
1770 coap_tick_t elapsed;
1771 coap_queue_t *nextinqueue;
1773 ctx->timer_configured = 0;
1777 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 */
1779 nextinqueue = coap_peek_next(ctx);
1780 while (nextinqueue != NULL)
1782 if (nextinqueue->t > elapsed)
1784 nextinqueue->t -= elapsed;
1789 elapsed -= nextinqueue->t;
1790 coap_retransmit(ctx, coap_pop_next(ctx));
1791 nextinqueue = coap_peek_next(ctx);
1795 ctx->sendqueue_basetime = now;
1797 coap_retransmittimer_restart(ctx);
1800 static void coap_retransmittimer_restart(coap_context_t *ctx)
1802 coap_tick_t now, elapsed, delay;
1804 if (ctx->timer_configured)
1806 printf("clearing\n");
1807 sys_untimeout(coap_retransmittimer_execute, (void*)ctx);
1808 ctx->timer_configured = 0;
1810 if (ctx->sendqueue != NULL)
1813 elapsed = now - ctx->sendqueue_basetime;
1814 if (ctx->sendqueue->t >= elapsed)
1816 delay = ctx->sendqueue->t - elapsed;
1820 /* a strange situation, but not completely impossible.
1822 * this happens, for example, right after
1823 * coap_retransmittimer_execute, when a retransmission
1824 * was *just not yet* due, and the clock ticked before
1825 * our coap_ticks was called.
1827 * not trying to retransmit anything now, as it might
1828 * cause uncontrollable recursion; let's just try again
1829 * with the next main loop run.
1834 printf("scheduling for %d ticks\n", delay);
1835 sys_timeout(delay, coap_retransmittimer_execute, (void*)ctx);
1836 ctx->timer_configured = 1;