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 #include <sys/types.h>
22 #ifdef HAVE_SYS_SOCKET_H
23 #include <sys/socket.h>
25 #ifdef HAVE_NETINET_IN_H
26 #include <netinet/in.h>
28 #ifdef HAVE_ARPA_INET_H
29 #include <arpa/inet.h>
33 #include <lwip/pbuf.h>
35 #include <lwip/timers.h>
48 #if defined(WITH_POSIX)
52 static inline coap_queue_t *
55 return (coap_queue_t *)coap_malloc(sizeof(coap_queue_t));
59 coap_free_node(coap_queue_t *node)
63 #endif /* WITH_POSIX */
66 #include <lwip/memp.h>
68 static void coap_retransmittimer_execute(void *arg);
69 static void coap_retransmittimer_restart(coap_context_t *ctx);
71 static inline coap_queue_t *
74 return (coap_queue_t *)memp_malloc(MEMP_COAP_NODE);
78 coap_free_node(coap_queue_t *node)
80 memp_free(MEMP_COAP_NODE, node);
83 #endif /* WITH_LWIP */
86 # define DEBUG DEBUG_PRINT
90 #include "net/uip-debug.h"
92 clock_time_t clock_offset;
94 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
95 #define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[UIP_LLIPH_LEN])
97 void coap_resources_init();
98 void coap_pdu_resources_init();
100 unsigned char initialized = 0;
101 coap_context_t the_coap_context;
103 MEMB(node_storage, coap_queue_t, COAP_PDU_MAXCNT);
105 PROCESS(coap_retransmit_process, "message retransmit process");
107 static inline coap_queue_t *
110 return (coap_queue_t *)memb_alloc(&node_storage);
114 coap_free_node(coap_queue_t *node)
116 memb_free(&node_storage, node);
118 #endif /* WITH_CONTIKI */
121 /** Callback to udp_recv when using lwIP. Gets called by lwIP on arriving
122 * packages, places a reference in context->pending_package, and calls
123 * coap_read to process the package. Thus, coap_read needs not be called in
124 * lwIP main loops. (When modifying this for thread-like operation, ie. if you
125 * remove the coap_read call from this, make sure that coap_read gets a chance
126 * to run before this callback is entered the next time.)
128 static void received_package(void *arg, struct udp_pcb *upcb, struct pbuf *p, ip_addr_t *addr, u16_t port)
130 struct coap_context_t *context = (coap_context_t *)arg;
132 LWIP_ASSERT("pending_package was not cleared.", context->pending_package == NULL);
134 context->pending_package = p; /* we don't free it, coap_read has to do that */
135 context->pending_address.addr = addr->addr; /* FIXME: this has to become address-type independent, probably there'll be an lwip function for that */
136 context->pending_port = port;
139 coap_read(context, data);
142 #endif /* WITH_LWIP */
144 int print_wellknown(coap_context_t *, unsigned char *, size_t *, size_t, coap_opt_t *);
146 void coap_handle_failed_notify(coap_context_t *, const coap_address_t *, const str *);
148 unsigned int coap_adjust_basetime(coap_context_t *ctx, coap_tick_t now)
150 unsigned int result = 0;
151 coap_tick_diff_t delta = now - ctx->sendqueue_basetime;
155 /* delta < 0 means that the new time stamp is before the old. */
158 ctx->sendqueue->t -= delta;
162 /* This case is more complex: The time must be advanced forward,
163 * thus possibly leading to timed out elements at the queue's
164 * start. For every element that has timed out, its relative
165 * time is set to zero and the result counter is increased. */
167 coap_queue_t *q = ctx->sendqueue;
169 while (q && (t + q->t < (coap_tick_t) delta))
177 /* finally adjust the first element that has not expired */
180 q->t = (coap_tick_t) delta - t;
185 /* adjust basetime */
186 ctx->sendqueue_basetime += delta;
191 int coap_insert_node(coap_queue_t **queue, coap_queue_t *node)
197 /* set queue head if empty */
204 /* replace queue head if PDU's time is less than head's time */
210 q->t -= node->t; /* make q->t relative to node->t */
214 /* search for right place to insert */
217 node->t -= q->t; /* make node-> relative to q->t */
220 } while (q && q->t <= node->t);
222 /* insert new item */
225 q->t -= node->t; /* make q->t relative to node->t */
232 int coap_delete_node(coap_queue_t *node)
237 coap_delete_pdu(node->pdu);
238 coap_free_node(node);
243 void coap_delete_all(coap_queue_t *queue)
248 coap_delete_all(queue->next);
249 coap_delete_node(queue);
256 node = coap_malloc_node();
261 coap_log(LOG_WARNING, "coap_new_node: malloc\n");
266 memset(node, 0, sizeof *node);
271 coap_peek_next(coap_context_t *context)
273 if (!context || !context->sendqueue)
276 return context->sendqueue;
280 coap_pop_next(coap_context_t *context)
284 if (!context || !context->sendqueue)
287 next = context->sendqueue;
288 context->sendqueue = context->sendqueue->next;
289 if (context->sendqueue)
291 context->sendqueue->t += next->t;
297 #ifdef COAP_DEFAULT_WKC_HASHKEY
298 /** Checks if @p Key is equal to the pre-defined hash key for.well-known/core. */
299 #define is_wkc(Key) \
300 (memcmp((Key), COAP_DEFAULT_WKC_HASHKEY, sizeof(coap_key_t)) == 0)
302 /* Implements a singleton to store a hash key for the .wellknown/core
307 static coap_key_t wkc;
308 static unsigned char _initialized = 0;
311 _initialized = coap_hash_path((unsigned char *)COAP_DEFAULT_URI_WELLKNOWN,
312 sizeof(COAP_DEFAULT_URI_WELLKNOWN) - 1, wkc);
314 return memcmp(k, wkc, sizeof(coap_key_t)) == 0;
319 coap_new_context(const coap_address_t *listen_addr)
322 coap_context_t *c = coap_malloc( sizeof( coap_context_t ) );
324 #endif /* WITH_POSIX */
326 coap_context_t *c = memp_malloc(MEMP_COAP_CONTEXT);
327 #endif /* WITH_LWIP */
333 #endif /* WITH_CONTIKI */
337 coap_log(LOG_EMERG, "no listen address specified\n");
343 prng_init(LWIP_RAND());
344 #else /* WITH_LWIP */
345 prng_init((unsigned long)listen_addr ^ clock_offset);
346 #endif /* WITH_LWIP */
352 coap_log(LOG_EMERG, "coap_init: malloc:\n");
356 #endif /* not WITH_CONTIKI */
358 coap_resources_init();
359 coap_pdu_resources_init();
361 c = &the_coap_context;
363 #endif /* WITH_CONTIKI */
365 memset(c, 0, sizeof(coap_context_t));
367 /* initialize message id */
368 prng((unsigned char *)&c->message_id, sizeof(unsigned short));
370 /* register the critical options that we know */
371 coap_register_option(c, COAP_OPTION_IF_MATCH);
372 coap_register_option(c, COAP_OPTION_URI_HOST);
373 coap_register_option(c, COAP_OPTION_IF_NONE_MATCH);
374 coap_register_option(c, COAP_OPTION_URI_PORT);
375 coap_register_option(c, COAP_OPTION_URI_PATH);
376 coap_register_option(c, COAP_OPTION_URI_QUERY);
377 coap_register_option(c, COAP_OPTION_ACCEPT);
378 coap_register_option(c, COAP_OPTION_PROXY_URI);
379 coap_register_option(c, COAP_OPTION_PROXY_SCHEME);
380 coap_register_option(c, COAP_OPTION_BLOCK2);
381 coap_register_option(c, COAP_OPTION_BLOCK1);
384 c->sockfd = socket(listen_addr->addr.sa.sa_family, SOCK_DGRAM, 0);
388 coap_log(LOG_EMERG, "coap_new_context: socket\n");
389 #endif /* WITH_POSIX */
393 if ( setsockopt( c->sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse) ) < 0 )
396 coap_log(LOG_WARNING, "setsockopt SO_REUSEADDR\n");
400 if (bind(c->sockfd, &listen_addr->addr.sa, listen_addr->size) < 0)
403 coap_log(LOG_EMERG, "coap_new_context: bind\n");
411 if ( c->sockfd >= 0 )
416 #endif /* WITH_POSIX */
418 c->conn = udp_new(NULL, 0, NULL);
419 udp_bind(c->conn, listen_addr->port);
421 process_start(&coap_retransmit_process, (char *)c);
423 PROCESS_CONTEXT_BEGIN(&coap_retransmit_process);
424 #ifndef WITHOUT_OBSERVE
425 etimer_set(&c->notify_timer, COAP_RESOURCE_CHECK_TIME * COAP_TICKS_PER_SECOND);
426 #endif /* WITHOUT_OBSERVE */
427 /* the retransmit timer must be initialized to some large value */
428 etimer_set(&the_coap_context.retransmit_timer, 0xFFFF);
429 PROCESS_CONTEXT_END(&coap_retransmit_process);
431 #endif /* WITH_CONTIKI */
434 /* hard assert: this is not expected to fail dynamically */
435 LWIP_ASSERT("Failed to allocate PCB for CoAP", c->pcb != NULL);
437 udp_recv(c->pcb, received_package, (void*)c);
438 udp_bind(c->pcb, &listen_addr->addr, listen_addr->port);
440 c->timer_configured = 0;
446 void coap_free_context(coap_context_t *context)
448 #if defined(WITH_POSIX) || defined(WITH_LWIP)
449 coap_resource_t *res;
450 #ifndef COAP_RESOURCES_NOHASH
451 coap_resource_t *rtmp;
453 #endif /* WITH_POSIX || WITH_LWIP */
457 coap_delete_all(context->recvqueue);
458 coap_delete_all(context->sendqueue);
461 context->sendqueue = NULL;
462 coap_retransmittimer_restart(context);
465 #if defined(WITH_POSIX) || defined(WITH_LWIP)
466 #ifdef COAP_RESOURCES_NOHASH
467 LL_FOREACH(context->resources, res)
470 HASH_ITER(hh, context->resources, res, rtmp)
473 coap_delete_resource(context, res->key);
475 #endif /* WITH_POSIX || WITH_LWIP */
478 /* coap_delete_list(context->subscriptions); */
479 close( context->sockfd );
480 coap_free( context );
483 udp_remove(context->pcb);
484 memp_free(MEMP_COAP_CONTEXT, context);
487 memset(&the_coap_context, 0, sizeof(coap_context_t));
489 #endif /* WITH_CONTIKI */
492 int coap_option_check_critical(coap_context_t *ctx, coap_pdu_t *pdu, coap_opt_filter_t unknown)
495 coap_opt_iterator_t opt_iter;
498 coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL);
500 while (coap_option_next(&opt_iter))
503 /* The following condition makes use of the fact that
504 * coap_option_getb() returns -1 if type exceeds the bit-vector
505 * filter. As the vector is supposed to be large enough to hold
506 * the largest known option, we know that everything beyond is
509 if (opt_iter.type & 0x01 && coap_option_getb(ctx->known_options, opt_iter.type) < 1)
511 debug("unknown critical option %d\n", opt_iter.type);
515 /* When opt_iter.type is beyond our known option range,
516 * coap_option_setb() will return -1 and we are safe to leave
518 if (coap_option_setb(unknown, opt_iter.type) == -1)
526 void coap_transaction_id(const coap_address_t *peer, const coap_pdu_t *pdu, coap_tid_t *id)
530 memset(h, 0, sizeof(coap_key_t));
532 /* Compare the complete address structure in case of IPv4. For IPv6,
533 * we need to look at the transport address only. */
536 switch (peer->addr.sa.sa_family)
539 coap_hash((const unsigned char *)&peer->addr.sa, peer->size, h);
542 coap_hash((const unsigned char *)&peer->addr.sin6.sin6_port,
543 sizeof(peer->addr.sin6.sin6_port), h);
544 coap_hash((const unsigned char *)&peer->addr.sin6.sin6_addr,
545 sizeof(peer->addr.sin6.sin6_addr), h);
551 #if defined(WITH_LWIP) || defined(WITH_CONTIKI)
552 /* FIXME: with lwip, we can do better */
553 coap_hash((const unsigned char *)&peer->port, sizeof(peer->port), h);
554 coap_hash((const unsigned char *)&peer->addr, sizeof(peer->addr), h);
555 #endif /* WITH_LWIP || WITH_CONTIKI */
557 coap_hash((const unsigned char *)&pdu->hdr->id, sizeof(unsigned short), h);
559 *id = ((h[0] << 8) | h[1]) ^ ((h[2] << 8) | h[3]);
562 coap_tid_t coap_send_ack(coap_context_t *context, const coap_address_t *dst, coap_pdu_t *request)
564 coap_pdu_t *response;
565 coap_tid_t result = COAP_INVALID_TID;
567 if (request && request->hdr->type == COAP_MESSAGE_CON)
569 response = coap_pdu_init(COAP_MESSAGE_ACK, 0, request->hdr->id, sizeof(coap_pdu_t));
572 result = coap_send(context, dst, response);
573 coap_delete_pdu(response);
580 /* releases space allocated by PDU if free_pdu is set */
582 coap_send_impl(coap_context_t *context,
583 const coap_address_t *dst,
587 char* z = inet_ntoa(*(struct in_addr *)&(dst->addr));
589 ssize_t bytes_written;
590 coap_tid_t id = COAP_INVALID_TID;
592 if ( !context || !dst || !pdu )
595 bytes_written = sendto( context->sockfd, pdu->hdr, pdu->length, 0,
596 &dst->addr.sa, dst->size);
598 if (bytes_written >= 0)
600 coap_transaction_id(dst, pdu, &id);
604 coap_log(LOG_CRIT, "coap_send: sendto\n");
609 #endif /* WITH_POSIX */
611 /* releases space allocated by PDU if free_pdu is set */
613 coap_send_impl(coap_context_t *context,
614 const coap_address_t *dst,
617 coap_tid_t id = COAP_INVALID_TID;
619 if ( !context || !dst || !pdu )
622 /* FIXME: is there a way to check if send was successful? */
623 uip_udp_packet_sendto(context->conn, pdu->hdr, pdu->length,
624 &dst->addr, dst->port);
626 coap_transaction_id(dst, pdu, &id);
630 #endif /* WITH_CONTIKI */
633 coap_send_impl(coap_context_t *context,
634 const coap_address_t *dst,
637 coap_tid_t id = COAP_INVALID_TID;
642 if ( !context || !dst || !pdu )
647 data_backup = pdu->data;
649 /* FIXME: we can't check this here with the existing infrastructure, but we
650 * should actually check that the pdu is not held by anyone but us. the
651 * respective pbuf is already exclusively owned by the pdu. */
654 LWIP_ASSERT("The PDU header is not where it is expected", pdu->hdr == p->payload + sizeof(coap_pdu_t));
656 err = pbuf_header(p, -sizeof(coap_pdu_t));
659 debug("coap_send_impl: pbuf_header failed\n");
664 coap_transaction_id(dst, pdu, &id);
666 pbuf_realloc(p, pdu->length);
668 udp_sendto(context->pcb, p,
669 &dst->addr, dst->port);
671 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 */
673 err = pbuf_header(p, sizeof(coap_pdu_t));
674 LWIP_ASSERT("Cannot undo pbuf_header", err == 0);
676 /* restore destroyed pdu data */
677 LWIP_ASSERT("PDU not restored", p->payload == pdu);
678 pdu->max_size = p->tot_len - sizeof(coap_pdu_t); /* reduced after pbuf_realloc */
679 pdu->hdr = p->payload + sizeof(coap_pdu_t);
680 pdu->max_delta = 0; /* won't be used any more */
681 pdu->length = pdu->max_size;
682 pdu->data = data_backup;
687 #endif /* WITH_LWIP */
689 coap_tid_t coap_send(coap_context_t *context, const coap_address_t *dst, coap_pdu_t *pdu)
691 return coap_send_impl(context, dst, pdu);
694 coap_tid_t coap_send_error(coap_context_t *context, coap_pdu_t *request, const coap_address_t *dst,
695 unsigned char code, coap_opt_filter_t opts)
697 coap_pdu_t *response;
698 coap_tid_t result = COAP_INVALID_TID;
703 response = coap_new_error_response(request, code, opts);
706 result = coap_send(context, dst, response);
707 coap_delete_pdu(response);
713 coap_tid_t coap_send_message_type(coap_context_t *context, const coap_address_t *dst,
714 coap_pdu_t *request, unsigned char type)
716 coap_pdu_t *response;
717 coap_tid_t result = COAP_INVALID_TID;
721 response = coap_pdu_init(type, 0, request->hdr->id, sizeof(coap_pdu_t));
724 result = coap_send(context, dst, response);
725 coap_delete_pdu(response);
731 coap_tid_t coap_send_confirmed(coap_context_t *context, const coap_address_t *dst, coap_pdu_t *pdu)
737 node = coap_new_node();
740 debug("coap_send_confirmed: insufficient memory\n");
741 return COAP_INVALID_TID;
744 node->id = coap_send_impl(context, dst, pdu);
745 if (COAP_INVALID_TID == node->id)
747 debug("coap_send_confirmed: error sending pdu\n");
748 coap_free_node(node);
749 return COAP_INVALID_TID;
752 prng((unsigned char *)&r, sizeof(r));
754 /* add randomized RESPONSE_TIMEOUT to determine retransmission timeout */
755 node->timeout = COAP_DEFAULT_RESPONSE_TIMEOUT * COAP_TICKS_PER_SECOND
756 + (COAP_DEFAULT_RESPONSE_TIMEOUT >> 1) * ((COAP_TICKS_PER_SECOND * (r & 0xFF)) >> 8);
758 memcpy(&node->remote, dst, sizeof(coap_address_t));
761 /* Set timer for pdu retransmission. If this is the first element in
762 * the retransmission queue, the base time is set to the current
763 * time and the retransmission time is node->timeout. If there is
764 * already an entry in the sendqueue, we must check if this node is
765 * to be retransmitted earlier. Therefore, node->timeout is first
766 * normalized to the base time and then inserted into the queue with
767 * an adjusted relative time.
769 if (context->sendqueue == NULL)
771 node->t = node->timeout;
772 context->sendqueue_basetime = now;
776 /* make node->t relative to context->sendqueue_basetime */
777 node->t = (now - context->sendqueue_basetime) + node->timeout;
780 coap_insert_node(&context->sendqueue, node);
783 if (node == context->sendqueue) /* don't bother with timer stuff if there are earlier retransmits */
784 coap_retransmittimer_restart(context);
788 { /* (re-)initialize retransmission timer */
789 coap_queue_t *nextpdu;
791 nextpdu = coap_peek_next(context);
792 assert(nextpdu); /* we have just inserted a node */
794 /* must set timer within the context of the retransmit process */
795 PROCESS_CONTEXT_BEGIN(&coap_retransmit_process);
796 etimer_set(&context->retransmit_timer, nextpdu->t);
797 PROCESS_CONTEXT_END(&coap_retransmit_process);
799 #endif /* WITH_CONTIKI */
804 coap_tid_t coap_retransmit(coap_context_t *context, coap_queue_t *node)
806 if (!context || !node)
807 return COAP_INVALID_TID;
809 /* re-initialize timeout when maximum number of retransmissions are not reached yet */
810 if (node->retransmit_cnt < COAP_DEFAULT_MAX_RETRANSMIT)
812 node->retransmit_cnt++;
813 node->t = node->timeout << node->retransmit_cnt;
814 coap_insert_node(&context->sendqueue, node);
816 if (node == context->sendqueue) /* don't bother with timer stuff if there are earlier retransmits */
817 coap_retransmittimer_restart(context);
821 "** retransmission #%d of transaction %d\n", node->retransmit_cnt, ntohs(node->pdu->hdr->id));
823 node->id = coap_send_impl(context, &node->remote, node->pdu);
827 /* no more retransmissions, remove node from system */
830 debug("** removed transaction %d\n", ntohs(node->id));
833 #ifndef WITHOUT_OBSERVE
834 /* Check if subscriptions exist that should be canceled after
835 COAP_MAX_NOTIFY_FAILURES */
836 if (node->pdu->hdr->code >= 64)
841 token.length = node->pdu->hdr->token_length;
842 token.s = node->pdu->hdr->token;
844 coap_handle_failed_notify(context, &node->remote, &token);
846 #endif /* WITHOUT_OBSERVE */
848 /* And finally delete the node */
849 coap_delete_node(node);
850 return COAP_INVALID_TID;
854 * Checks if @p opt fits into the message that ends with @p maxpos.
855 * This function returns @c 1 on success, or @c 0 if the option @p opt
856 * would exceed @p maxpos.
858 static inline int check_opt_size(coap_opt_t *opt, unsigned char *maxpos)
860 if (opt && opt < maxpos)
862 if (((*opt & 0x0f) < 0x0f) || (opt + 1 < maxpos))
863 return opt + COAP_OPT_SIZE(opt) < maxpos;
868 int coap_read(coap_context_t *ctx, char* data)
871 static char buf[COAP_MAX_PDU_SIZE];
873 #if defined(WITH_LWIP) || defined(WITH_CONTIKI)
877 ssize_t bytes_read = -1;
878 coap_address_t src, dst;
883 #endif /* WITH_CONTIKI */
885 LWIP_ASSERT("No package pending", ctx->pending_package != NULL);
886 LWIP_ASSERT("Can only deal with contiguous PBUFs to read the initial details", ctx->pending_package->tot_len == ctx->pending_package->len);
887 buf = ctx->pending_package->payload;
888 #endif /* WITH_LWIP */
890 pdu = (coap_hdr_t *) buf;
892 coap_address_init(&src);
895 bytes_read = recvfrom(ctx->sockfd, buf, sizeof(buf), 0, &src.addr.sa, &src.size);
897 #endif /* WITH_POSIX */
901 uip_ipaddr_copy(&src.addr, &UIP_IP_BUF->srcipaddr);
902 src.port = UIP_UDP_BUF->srcport;
903 uip_ipaddr_copy(&dst.addr, &UIP_IP_BUF->destipaddr);
904 dst.port = UIP_UDP_BUF->destport;
906 bytes_read = uip_datalen();
907 ((char *)uip_appdata)[bytes_read] = 0;
908 PRINTF("Server received %d bytes from [", (int)bytes_read);
909 PRINT6ADDR(&src.addr);
910 PRINTF("]:%d\n", uip_ntohs(src.port));
912 #endif /* WITH_CONTIKI */
914 /* FIXME: use lwip address operation functions */
915 src.addr.addr = ctx->pending_address.addr;
916 src.port = ctx->pending_port;
917 bytes_read = ctx->pending_package->tot_len;
918 #endif /* WITH_LWIP */
922 warn("coap_read: recvfrom");
926 if ((size_t) bytes_read < sizeof(coap_hdr_t))
928 debug("coap_read: discarded invalid frame\n");
932 if (pdu->version != COAP_DEFAULT_VERSION)
934 debug("coap_read: unknown protocol version\n");
938 node = coap_new_node();
943 node->pdu = coap_pdu_from_pbuf(ctx->pending_package);
944 ctx->pending_package = NULL;
946 node->pdu = coap_pdu_init(0, 0, 0, bytes_read);
951 coap_ticks(&node->t);
952 memcpy(&node->local, &dst, sizeof(coap_address_t));
953 memcpy(&node->remote, &src, sizeof(coap_address_t));
955 if (!coap_pdu_parse((unsigned char *) buf, bytes_read, node->pdu))
957 warn("discard malformed PDU");
961 /* and add new node to receive queue */
962 coap_transaction_id(&node->remote, node->pdu, &node->id);
963 coap_insert_node(&ctx->recvqueue, node);
966 if (LOG_DEBUG <= coap_get_log_level())
968 #ifndef INET6_ADDRSTRLEN
969 #define INET6_ADDRSTRLEN 40
971 unsigned char addr[INET6_ADDRSTRLEN + 8];
973 if (coap_print_addr(&src, addr, INET6_ADDRSTRLEN + 8))
974 debug("** received %d bytes from %s:\n", (int)bytes_read, addr);
976 coap_show_pdu(node->pdu);
983 /* FIXME: send back RST? */
984 coap_delete_node(node);
988 /* even if there was an error, clean up */
989 pbuf_free(ctx->pending_package);
990 ctx->pending_package = NULL;
995 int coap_remove_from_queue(coap_queue_t **queue, coap_tid_t id, coap_queue_t **node)
999 if (!queue || !*queue)
1002 /* replace queue head if PDU's time is less than head's time */
1004 if (id == (*queue)->id)
1005 { /* found transaction */
1007 *queue = (*queue)->next;
1009 { /* adjust relative time of new queue head */
1010 (*queue)->t += (*node)->t;
1012 (*node)->next = NULL;
1013 /* coap_delete_node( q ); */
1014 debug("*** removed transaction %u\n", id);
1018 /* search transaction to remove (only first occurence will be removed) */
1024 } while (q && id != q->id);
1027 { /* found transaction */
1030 { /* must update relative time of p->next */
1035 /* coap_delete_node( q ); */
1036 debug("*** removed transaction %u\n", id);
1044 static inline int token_match(const unsigned char *a, size_t alen, const unsigned char *b,
1047 return alen == blen && (alen == 0 || memcmp(a, b, alen) == 0);
1050 void coap_cancel_all_messages(coap_context_t *context, const coap_address_t *dst,
1051 const unsigned char *token, size_t token_length)
1053 /* cancel all messages in sendqueue that are for dst
1054 * and use the specified token */
1055 coap_queue_t *p, *q;
1057 debug("cancel_all_messages\n");
1058 while (context->sendqueue && coap_address_equals(dst, &context->sendqueue->remote)
1059 && token_match(token, token_length, context->sendqueue->pdu->hdr->token,
1060 context->sendqueue->pdu->hdr->token_length))
1062 q = context->sendqueue;
1063 context->sendqueue = q->next;
1064 debug("**** removed transaction %d\n", ntohs(q->pdu->hdr->id));
1065 coap_delete_node(q);
1068 if (!context->sendqueue)
1071 p = context->sendqueue;
1074 /* when q is not NULL, it does not match (dst, token), so we can skip it */
1077 if (coap_address_equals(dst, &q->remote)
1078 && token_match(token, token_length, q->pdu->hdr->token, q->pdu->hdr->token_length))
1081 debug("**** removed transaction %d\n", ntohs(q->pdu->hdr->id));
1082 coap_delete_node(q);
1094 coap_find_transaction(coap_queue_t *queue, coap_tid_t id)
1096 while (queue && queue->id != id)
1097 queue = queue->next;
1103 coap_new_error_response(coap_pdu_t *request, unsigned char code, coap_opt_filter_t opts)
1105 coap_opt_iterator_t opt_iter;
1106 coap_pdu_t *response;
1107 size_t size = sizeof(coap_hdr_t) + request->hdr->token_length;
1110 unsigned short opt_type = 0; /* used for calculating delta-storage */
1112 #if COAP_ERROR_PHRASE_LENGTH > 0
1113 char *phrase = coap_response_phrase(code);
1115 /* Need some more space for the error phrase and payload start marker */
1117 size += strlen(phrase) + 1;
1122 /* cannot send ACK if original request was not confirmable */
1123 type = request->hdr->type == COAP_MESSAGE_CON ? COAP_MESSAGE_ACK : COAP_MESSAGE_NON;
1125 /* Estimate how much space we need for options to copy from
1126 * request. We always need the Token, for 4.02 the unknown critical
1127 * options must be included as well. */
1128 coap_option_clrb(opts, COAP_OPTION_CONTENT_TYPE); /* we do not want this */
1130 coap_option_iterator_init(request, &opt_iter, opts);
1132 /* Add size of each unknown critical option. As known critical
1133 options as well as elective options are not copied, the delta
1136 while ((option = coap_option_next(&opt_iter)))
1138 unsigned short delta = opt_iter.type - opt_type;
1139 /* calculate space required to encode (opt_iter.type - opt_type) */
1144 else if (delta < 269)
1153 /* add coap_opt_length(option) and the number of additional bytes
1154 * required to encode the option length */
1156 size += coap_opt_length(option);
1157 switch (*option & 0x0f)
1169 opt_type = opt_iter.type;
1172 /* Now create the response and fill with options and payload data. */
1173 response = coap_pdu_init(type, code, request->hdr->id, size);
1177 if (!coap_add_token(response, request->hdr->token_length, request->hdr->token))
1179 debug("cannot add token to error response\n");
1180 coap_delete_pdu(response);
1184 /* copy all options */
1185 coap_option_iterator_init(request, &opt_iter, opts);
1186 while ((option = coap_option_next(&opt_iter)))
1187 coap_add_option(response, opt_iter.type, COAP_OPT_LENGTH(option),
1188 COAP_OPT_VALUE(option));
1190 #if COAP_ERROR_PHRASE_LENGTH > 0
1191 /* note that diagnostic messages do not need a Content-Format option. */
1193 coap_add_data(response, strlen(phrase), (unsigned char *) phrase);
1201 * Quick hack to determine the size of the resource description for
1204 static inline size_t get_wkc_len(coap_context_t *context, coap_opt_t *query_filter)
1206 unsigned char buf[1];
1209 if (print_wellknown(context, buf, &len, UINT_MAX, query_filter) & COAP_PRINT_STATUS_ERROR)
1211 warn("cannot determine length of /.well-known/core\n");
1215 debug("get_wkc_len: print_wellknown() returned %zu\n", len);
1220 #define SZX_TO_BYTES(SZX) ((size_t)(1 << ((SZX) + 4)))
1223 wellknown_response(coap_context_t *context, coap_pdu_t *request)
1226 coap_opt_iterator_t opt_iter;
1227 size_t len, wkc_len;
1228 unsigned char buf[2];
1230 int need_block2 = 0; /* set to 1 if Block2 option is required */
1232 coap_opt_t *query_filter;
1235 resp = coap_pdu_init(
1236 request->hdr->type == COAP_MESSAGE_CON ? COAP_MESSAGE_ACK : COAP_MESSAGE_NON,
1237 COAP_RESPONSE_CODE(205),
1238 request->hdr->id, COAP_MAX_PDU_SIZE);
1241 debug("wellknown_response: cannot create PDU\n");
1245 if (!coap_add_token(resp, request->hdr->token_length, request->hdr->token))
1247 debug("wellknown_response: cannot add token\n");
1251 query_filter = coap_check_option(request, COAP_OPTION_URI_QUERY, &opt_iter);
1252 wkc_len = get_wkc_len(context, query_filter);
1254 /* check whether the request contains the Block2 option */
1255 if (coap_get_block(request, COAP_OPTION_BLOCK2, &block))
1257 offset = block.num << (block.szx + 4);
1259 { /* invalid, MUST lead to 4.00 Bad Request */
1260 resp->hdr->code = COAP_RESPONSE_CODE(400);
1263 else if (block.szx > COAP_MAX_BLOCK_SZX)
1265 block.szx = COAP_MAX_BLOCK_SZX;
1266 block.num = offset >> (block.szx + 4);
1272 /* Check if there is sufficient space to add Content-Format option
1273 * and data. We do this before adding the Content-Format option to
1274 * avoid sending error responses with that option but no actual
1276 if (resp->max_size <= (size_t) resp->length + 3)
1278 debug("wellknown_response: insufficient storage space\n");
1282 /* Add Content-Format. As we have checked for available storage,
1283 * nothing should go wrong here. */
1284 assert(coap_encode_var_bytes(buf, COAP_MEDIATYPE_APPLICATION_LINK_FORMAT) == 1);
1285 coap_add_option(resp, COAP_OPTION_CONTENT_FORMAT,
1286 coap_encode_var_bytes(buf, COAP_MEDIATYPE_APPLICATION_LINK_FORMAT), buf);
1288 /* check if Block2 option is required even if not requested */
1289 if (!need_block2 && (resp->max_size - (size_t) resp->length < wkc_len))
1291 assert(resp->length <= resp->max_size);
1292 const size_t payloadlen = resp->max_size - resp->length;
1293 /* yes, need block-wise transfer */
1295 block.m = 0; /* the M bit is set by coap_write_block_opt() */
1296 block.szx = COAP_MAX_BLOCK_SZX;
1297 while (payloadlen < SZX_TO_BYTES(block.szx))
1301 debug("wellknown_response: message to small even for szx == 0\n");
1313 /* write Block2 option if necessary */
1316 if (coap_write_block_opt(&block, COAP_OPTION_BLOCK2, resp, wkc_len) < 0)
1318 debug("wellknown_response: cannot add Block2 option\n");
1323 /* Manually set payload of response to let print_wellknown() write,
1324 * into our buffer without copying data. */
1326 resp->data = (unsigned char *) resp->hdr + resp->length;
1327 *resp->data = COAP_PAYLOAD_START;
1330 len = need_block2 ? SZX_TO_BYTES(block.szx) : resp->max_size - resp->length;
1332 result = print_wellknown(context, resp->data, &len, offset, query_filter);
1333 if ((result & COAP_PRINT_STATUS_ERROR) != 0)
1335 debug("print_wellknown failed\n");
1339 resp->length += COAP_PRINT_OUTPUT_LENGTH(result);
1343 /* set error code 5.03 and remove all options and data from response */
1344 resp->hdr->code = COAP_RESPONSE_CODE(503);
1345 resp->length = sizeof(coap_hdr_t) + resp->hdr->token_length;
1349 #define WANT_WKC(Pdu,Key) \
1350 (((Pdu)->hdr->code == COAP_REQUEST_GET) && is_wkc(Key))
1352 void handle_request(coap_context_t *context, coap_queue_t *node, const char* responseData)
1355 coap_method_handler_t h = NULL;
1356 coap_pdu_t *response = NULL;
1357 coap_opt_filter_t opt_filter;
1358 coap_resource_t *resource;
1361 coap_option_filter_clear(opt_filter);
1363 /* try to find the resource from the request URI */
1364 coap_hash_request_uri(node->pdu, key);
1365 resource = coap_get_resource_from_key(context, key);
1369 /* The resource was not found. Check if the request URI happens to
1370 * be the well-known URI. In that case, we generate a default
1371 * response, otherwise, we return 4.04 */
1373 switch (node->pdu->hdr->code)
1376 case COAP_REQUEST_GET:
1378 { /* GET request for .well-known/core */
1379 info("create default response for %s\n", COAP_DEFAULT_URI_WELLKNOWN);
1381 response = wellknown_response(context, node->pdu);
1385 { /* GET request for any another resource, return 4.04 */
1388 "GET for unknown resource 0x%02x%02x%02x%02x, return 4.04\n", key[0], key[1], key[2], key[3]);
1389 response = coap_new_error_response(node->pdu, COAP_RESPONSE_CODE(404),
1394 default: /* any other request type */
1397 "unhandled request for unknown resource 0x%02x%02x%02x%02x\r\n", key[0], key[1], key[2], key[3]);
1398 if (!coap_is_mcast(&node->local))
1399 response = coap_new_error_response(node->pdu, COAP_RESPONSE_CODE(405),
1403 if (response && coap_send(context, &node->remote, response) == COAP_INVALID_TID)
1405 warn("cannot send response for transaction %u\n", node->id);
1407 coap_delete_pdu(response);
1412 /* the resource was found, check if there is a registered handler */
1413 if ((size_t) node->pdu->hdr->code - 1
1414 < sizeof(resource->handler) / sizeof(coap_method_handler_t))
1415 h = resource->handler[node->pdu->hdr->code - 1];
1420 "call custom handler for resource 0x%02x%02x%02x%02x\n", key[0], key[1], key[2], key[3]);
1421 response = coap_pdu_init(
1422 node->pdu->hdr->type == COAP_MESSAGE_CON ? COAP_MESSAGE_ACK : COAP_MESSAGE_NON,
1423 0, node->pdu->hdr->id, COAP_MAX_PDU_SIZE);
1425 /* Implementation detail: coap_add_token() immediately returns 0
1426 if response == NULL */
1427 if (coap_add_token(response, node->pdu->hdr->token_length, node->pdu->hdr->token))
1430 { node->pdu->hdr->token_length, node->pdu->hdr->token };
1432 //h(context, resource, &node->remote,
1433 //node->pdu, &token, response);
1435 unsigned char buf[3];
1436 response->hdr->code = COAP_RESPONSE_CODE(205);
1437 coap_add_option(response, COAP_OPTION_CONTENT_TYPE,
1438 coap_encode_var_bytes(buf, COAP_MEDIATYPE_TEXT_PLAIN), buf);
1439 coap_add_option(response, COAP_OPTION_MAXAGE, coap_encode_var_bytes(buf, 0x2ffff), buf);
1440 coap_add_data(response, strlen(responseData), (unsigned char *) responseData);
1442 if (response->hdr->type != COAP_MESSAGE_NON
1443 || (response->hdr->code >= 64 && !coap_is_mcast(&node->local)))
1446 if (coap_send(context, &node->remote, response) == COAP_INVALID_TID)
1448 debug("cannot send response for message %d\n", node->pdu->hdr->id);
1452 coap_delete_pdu(response);
1456 warn("cannot generate response\r\n");
1461 if (WANT_WKC(node->pdu, key))
1463 debug("create default response for %s\n", COAP_DEFAULT_URI_WELLKNOWN);
1464 response = wellknown_response(context, node->pdu);
1467 response = coap_new_error_response(node->pdu, COAP_RESPONSE_CODE(405), opt_filter);
1469 if (!response || (coap_send(context, &node->remote, response) == COAP_INVALID_TID))
1471 debug("cannot send response for transaction %u\n", node->id);
1473 coap_delete_pdu(response);
1477 static inline void handle_response(coap_context_t *context, coap_queue_t *sent, coap_queue_t *rcvd)
1480 /* Call application-specific reponse handler when available. If
1481 * not, we must acknowledge confirmable messages. */
1482 if (context->response_handler)
1485 context->response_handler(context, &rcvd->remote, sent ? sent->pdu : NULL, rcvd->pdu,
1490 /* send ACK if rcvd is confirmable (i.e. a separate response) */
1491 coap_send_ack(context, &rcvd->remote, rcvd->pdu);
1497 handle_locally(coap_context_t *context __attribute__ ((unused)),
1498 coap_queue_t *node __attribute__ ((unused)))
1500 #else /* not a GCC */
1501 handle_locally(coap_context_t *context, coap_queue_t *node)
1504 /* this function can be used to check if node->pdu is really for us */
1509 * This function handles RST messages received for the message passed
1512 static void coap_handle_rst(coap_context_t *context, const coap_queue_t *sent)
1514 #ifndef WITHOUT_OBSERVE
1516 #ifndef COAP_RESOURCES_NOHASH
1517 coap_resource_t *tmp;
1522 /* remove observer for this resource, if any
1523 * get token from sent and try to find a matching resource. Uh!
1526 COAP_SET_STR(&token, sent->pdu->hdr->token_length, sent->pdu->hdr->token);
1528 #ifndef WITH_CONTIKI
1529 #ifdef COAP_RESOURCES_NOHASH
1530 LL_FOREACH(context->resources, r)
1533 HASH_ITER(hh, context->resources, r, tmp)
1536 coap_delete_observer(r, &sent->remote, &token);
1537 coap_cancel_all_messages(context, &sent->remote, token.s, token.length);
1539 #else /* WITH_CONTIKI */
1540 r = (coap_resource_t *)resource_storage.mem;
1541 for (i = 0; i < resource_storage.num; ++i, ++r)
1543 if (resource_storage.count[i])
1545 coap_delete_observer(r, &sent->remote, &token);
1546 coap_cancel_all_messages(context, &sent->remote, token.s, token.length);
1549 #endif /* WITH_CONTIKI */
1550 #endif /* WITOUT_OBSERVE */
1553 void coap_dispatch(coap_context_t *context, const char* responseData)
1555 coap_queue_t *rcvd = NULL, *sent = NULL;
1556 coap_pdu_t *response;
1557 coap_opt_filter_t opt_filter;
1562 memset(opt_filter, 0, sizeof(coap_opt_filter_t));
1564 while (context->recvqueue)
1566 rcvd = context->recvqueue;
1568 /* remove node from recvqueue */
1569 context->recvqueue = context->recvqueue->next;
1572 if (rcvd->pdu->hdr->version != COAP_DEFAULT_VERSION)
1574 debug("dropped packet with unknown version %u\n", rcvd->pdu->hdr->version);
1578 switch (rcvd->pdu->hdr->type)
1580 case COAP_MESSAGE_ACK:
1581 /* find transaction in sendqueue to stop retransmission */
1582 coap_remove_from_queue(&context->sendqueue, rcvd->id, &sent);
1584 if (rcvd->pdu->hdr->code == 0)
1587 /* FIXME: if sent code was >= 64 the message might have been a
1588 * notification. Then, we must flag the observer to be alive
1589 * by setting obs->fail_cnt = 0. */
1590 if (sent && COAP_RESPONSE_CLASS(sent->pdu->hdr->code) == 2)
1593 { sent->pdu->hdr->token_length, sent->pdu->hdr->token };
1594 coap_touch_observer(context, &sent->remote, &token);
1598 case COAP_MESSAGE_RST:
1599 /* We have sent something the receiver disliked, so we remove
1600 * not only the transaction but also the subscriptions we might
1603 coap_log(LOG_ALERT, "got RST for message %u\n", ntohs(rcvd->pdu->hdr->id));
1605 /* find transaction in sendqueue to stop retransmission */
1606 coap_remove_from_queue(&context->sendqueue, rcvd->id, &sent);
1609 coap_handle_rst(context, sent);
1612 case COAP_MESSAGE_NON: /* check for unknown critical options */
1613 if (coap_option_check_critical(context, rcvd->pdu, opt_filter) == 0)
1617 case COAP_MESSAGE_CON: /* check for unknown critical options */
1618 if (coap_option_check_critical(context, rcvd->pdu, opt_filter) == 0)
1621 /* FIXME: send response only if we have received a request. Otherwise,
1623 response = coap_new_error_response(rcvd->pdu, COAP_RESPONSE_CODE(402),
1627 warn("coap_dispatch: cannot create error reponse\n");
1630 if (coap_send(context, &rcvd->remote, response) == COAP_INVALID_TID)
1632 warn("coap_dispatch: error sending reponse\n");
1634 coap_delete_pdu(response);
1642 /* Pass message to upper layer if a specific handler was
1643 * registered for a request that should be handled locally. */
1644 if (handle_locally(context, rcvd))
1646 if (COAP_MESSAGE_IS_REQUEST(rcvd->pdu->hdr))
1647 handle_request(context, rcvd, responseData);
1648 else if (COAP_MESSAGE_IS_RESPONSE(rcvd->pdu->hdr))
1649 handle_response(context, sent, rcvd);
1652 debug("dropped message with invalid code\n");
1653 coap_send_message_type(context, &rcvd->remote, rcvd->pdu, COAP_MESSAGE_RST);
1657 cleanup: coap_delete_node(sent);
1658 coap_delete_node(rcvd);
1662 int coap_can_exit(coap_context_t *context)
1664 return !context || (context->recvqueue == NULL && context->sendqueue == NULL);
1669 /*---------------------------------------------------------------------------*/
1670 /* CoAP message retransmission */
1671 /*---------------------------------------------------------------------------*/
1672 PROCESS_THREAD(coap_retransmit_process, ev, data)
1675 coap_queue_t *nextpdu;
1679 debug("Started retransmit process\r\n");
1684 if (ev == PROCESS_EVENT_TIMER)
1686 if (etimer_expired(&the_coap_context.retransmit_timer))
1689 nextpdu = coap_peek_next(&the_coap_context);
1692 while (nextpdu && nextpdu->t <= now)
1694 coap_retransmit(&the_coap_context, coap_pop_next(&the_coap_context));
1695 nextpdu = coap_peek_next(&the_coap_context);
1698 /* need to set timer to some value even if no nextpdu is available */
1699 etimer_set(&the_coap_context.retransmit_timer,
1700 nextpdu ? nextpdu->t - now : 0xFFFF);
1702 #ifndef WITHOUT_OBSERVE
1703 if (etimer_expired(&the_coap_context.notify_timer))
1705 coap_check_notify(&the_coap_context);
1706 etimer_reset(&the_coap_context.notify_timer);
1708 #endif /* WITHOUT_OBSERVE */
1714 /*---------------------------------------------------------------------------*/
1716 #endif /* WITH_CONTIKI */
1719 /* FIXME: retransmits that are not required any more due to incoming packages
1720 * do *not* get cleared at the moment, the wakeup when the transmission is due
1721 * is silently accepted. this is mainly due to the fact that the required
1722 * checks are similar in two places in the code (when receiving ACK and RST)
1723 * and that they cause more than one patch chunk, as it must be first checked
1724 * whether the sendqueue item to be dropped is the next one pending, and later
1725 * the restart function has to be called. nothing insurmountable, but it can
1726 * also be implemented when things have stabilized, and the performance
1727 * penality is minimal
1729 * also, this completely ignores COAP_RESOURCE_CHECK_TIME.
1732 static void coap_retransmittimer_execute(void *arg)
1734 coap_context_t *ctx = (coap_context_t*)arg;
1736 coap_tick_t elapsed;
1737 coap_queue_t *nextinqueue;
1739 ctx->timer_configured = 0;
1743 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 */
1745 nextinqueue = coap_peek_next(ctx);
1746 while (nextinqueue != NULL)
1748 if (nextinqueue->t > elapsed)
1750 nextinqueue->t -= elapsed;
1755 elapsed -= nextinqueue->t;
1756 coap_retransmit(ctx, coap_pop_next(ctx));
1757 nextinqueue = coap_peek_next(ctx);
1761 ctx->sendqueue_basetime = now;
1763 coap_retransmittimer_restart(ctx);
1766 static void coap_retransmittimer_restart(coap_context_t *ctx)
1768 coap_tick_t now, elapsed, delay;
1770 if (ctx->timer_configured)
1772 printf("clearing\n");
1773 sys_untimeout(coap_retransmittimer_execute, (void*)ctx);
1774 ctx->timer_configured = 0;
1776 if (ctx->sendqueue != NULL)
1779 elapsed = now - ctx->sendqueue_basetime;
1780 if (ctx->sendqueue->t >= elapsed)
1782 delay = ctx->sendqueue->t - elapsed;
1786 /* a strange situation, but not completely impossible.
1788 * this happens, for example, right after
1789 * coap_retransmittimer_execute, when a retransmission
1790 * was *just not yet* due, and the clock ticked before
1791 * our coap_ticks was called.
1793 * not trying to retransmit anything now, as it might
1794 * cause uncontrollable recursion; let's just try again
1795 * with the next main loop run.
1800 printf("scheduling for %d ticks\n", delay);
1801 sys_timeout(delay, coap_retransmittimer_execute, (void*)ctx);
1802 ctx->timer_configured = 1;