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 ) );
327 #endif /* WITH_POSIX */
329 coap_context_t *c = memp_malloc(MEMP_COAP_CONTEXT);
330 #endif /* WITH_LWIP */
336 #endif /* WITH_CONTIKI */
340 coap_log(LOG_EMERG, "no listen address specified\n");
346 prng_init(LWIP_RAND());
347 #else /* WITH_LWIP */
348 prng_init((unsigned long)listen_addr ^ clock_offset);
349 #endif /* WITH_LWIP */
355 coap_log(LOG_EMERG, "coap_init: malloc:\n");
359 #endif /* not WITH_CONTIKI */
361 coap_resources_init();
362 coap_pdu_resources_init();
364 c = &the_coap_context;
366 #endif /* WITH_CONTIKI */
368 memset(c, 0, sizeof(coap_context_t));
370 /* initialize message id */
371 prng((unsigned char *)&c->message_id, sizeof(unsigned short));
373 /* register the critical options that we know */
374 coap_register_option(c, COAP_OPTION_IF_MATCH);
375 coap_register_option(c, COAP_OPTION_URI_HOST);
376 coap_register_option(c, COAP_OPTION_IF_NONE_MATCH);
377 coap_register_option(c, COAP_OPTION_URI_PORT);
378 coap_register_option(c, COAP_OPTION_URI_PATH);
379 coap_register_option(c, COAP_OPTION_URI_QUERY);
380 coap_register_option(c, COAP_OPTION_ACCEPT);
381 coap_register_option(c, COAP_OPTION_PROXY_URI);
382 coap_register_option(c, COAP_OPTION_PROXY_SCHEME);
383 coap_register_option(c, COAP_OPTION_BLOCK2);
384 coap_register_option(c, COAP_OPTION_BLOCK1);
386 #if defined(WITH_POSIX) || defined(WITH_ARDUINO)
387 c->sockfd = socket(listen_addr->addr.sa.sa_family, SOCK_DGRAM, 0);
391 coap_log(LOG_EMERG, "coap_new_context: socket\n");
392 #endif /* WITH_POSIX */
396 if ( setsockopt( c->sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse) ) < 0 )
399 coap_log(LOG_WARNING, "setsockopt SO_REUSEADDR\n");
403 if (bind(c->sockfd, &listen_addr->addr.sa, listen_addr->size) < 0)
406 coap_log(LOG_EMERG, "coap_new_context: bind\n");
414 if ( c->sockfd >= 0 )
419 #endif /* WITH_POSIX || WITH_ARDUINO */
421 c->conn = udp_new(NULL, 0, NULL);
422 udp_bind(c->conn, listen_addr->port);
424 process_start(&coap_retransmit_process, (char *)c);
426 PROCESS_CONTEXT_BEGIN(&coap_retransmit_process);
427 #ifndef WITHOUT_OBSERVE
428 etimer_set(&c->notify_timer, COAP_RESOURCE_CHECK_TIME * COAP_TICKS_PER_SECOND);
429 #endif /* WITHOUT_OBSERVE */
430 /* the retransmit timer must be initialized to some large value */
431 etimer_set(&the_coap_context.retransmit_timer, 0xFFFF);
432 PROCESS_CONTEXT_END(&coap_retransmit_process);
434 #endif /* WITH_CONTIKI */
437 /* hard assert: this is not expected to fail dynamically */
438 LWIP_ASSERT("Failed to allocate PCB for CoAP", c->pcb != NULL);
440 udp_recv(c->pcb, received_package, (void*)c);
441 udp_bind(c->pcb, &listen_addr->addr, listen_addr->port);
443 c->timer_configured = 0;
449 void coap_free_context(coap_context_t *context)
451 #if defined(WITH_POSIX) || defined(WITH_LWIP)
452 coap_resource_t *res;
453 #ifndef COAP_RESOURCES_NOHASH
454 coap_resource_t *rtmp;
456 #endif /* WITH_POSIX || WITH_LWIP */
460 coap_delete_all(context->recvqueue);
461 coap_delete_all(context->sendqueue);
464 context->sendqueue = NULL;
465 coap_retransmittimer_restart(context);
468 #if defined(WITH_POSIX) || defined(WITH_LWIP) || defined(WITH_ARDUINO)
469 #ifdef COAP_RESOURCES_NOHASH
470 LL_FOREACH(context->resources, res)
473 HASH_ITER(hh, context->resources, res, rtmp)
476 coap_delete_resource(context, res->key);
478 #endif /* WITH_POSIX || WITH_LWIP */
481 /* coap_delete_list(context->subscriptions); */
482 close( context->sockfd );
483 coap_free( context );
486 udp_remove(context->pcb);
487 memp_free(MEMP_COAP_CONTEXT, context);
490 memset(&the_coap_context, 0, sizeof(coap_context_t));
492 #endif /* WITH_CONTIKI */
494 #endif //ifndef WITH_ARDUINO
495 int coap_option_check_critical(coap_context_t *ctx, coap_pdu_t *pdu, coap_opt_filter_t unknown)
498 coap_opt_iterator_t opt_iter;
501 coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL);
503 while (coap_option_next(&opt_iter))
506 /* The following condition makes use of the fact that
507 * coap_option_getb() returns -1 if type exceeds the bit-vector
508 * filter. As the vector is supposed to be large enough to hold
509 * the largest known option, we know that everything beyond is
512 if (opt_iter.type & 0x01 && coap_option_getb(ctx->known_options, opt_iter.type) < 1)
514 debug("unknown critical option %d\n", opt_iter.type);
518 /* When opt_iter.type is beyond our known option range,
519 * coap_option_setb() will return -1 and we are safe to leave
521 if (coap_option_setb(unknown, opt_iter.type) == -1)
529 void coap_transaction_id(const coap_address_t *peer, const coap_pdu_t *pdu, coap_tid_t *id)
533 memset(h, 0, sizeof(coap_key_t));
535 /* Compare the complete address structure in case of IPv4. For IPv6,
536 * we need to look at the transport address only. */
539 switch (peer->addr.sa.sa_family)
542 coap_hash((const unsigned char *)&peer->addr.sa, peer->size, h);
545 coap_hash((const unsigned char *)&peer->addr.sin6.sin6_port,
546 sizeof(peer->addr.sin6.sin6_port), h);
547 coap_hash((const unsigned char *)&peer->addr.sin6.sin6_addr,
548 sizeof(peer->addr.sin6.sin6_addr), h);
556 coap_hash((const unsigned char *)peer->addr, peer->size, h);
557 #endif /* WITH_ARDUINO */
559 #if defined(WITH_LWIP) || defined(WITH_CONTIKI)
560 /* FIXME: with lwip, we can do better */
561 coap_hash((const unsigned char *)&peer->port, sizeof(peer->port), h);
562 coap_hash((const unsigned char *)&peer->addr, sizeof(peer->addr), h);
563 #endif /* WITH_LWIP || WITH_CONTIKI */
565 coap_hash((const unsigned char *)&pdu->hdr->id, sizeof(unsigned short), h);
567 *id = ((h[0] << 8) | h[1]) ^ ((h[2] << 8) | h[3]);
570 coap_tid_t coap_send_ack(coap_context_t *context, const coap_address_t *dst, coap_pdu_t *request)
572 coap_pdu_t *response;
573 coap_tid_t result = COAP_INVALID_TID;
575 if (request && request->hdr->type == COAP_MESSAGE_CON)
577 response = coap_pdu_init(COAP_MESSAGE_ACK, 0, request->hdr->id, sizeof(coap_pdu_t));
580 result = coap_send(context, dst, response);
581 coap_delete_pdu(response);
587 #if defined(WITH_ARDUINO)
589 coap_send_impl(coap_context_t *context,
590 const coap_address_t *dst,
597 #if defined(WITH_POSIX)
598 /* releases space allocated by PDU if free_pdu is set */
600 coap_send_impl(coap_context_t *context,
601 const coap_address_t *dst,
604 ssize_t bytes_written;
605 coap_tid_t id = COAP_INVALID_TID;
607 if ( !context || !dst || !pdu )
610 bytes_written = sendto( context->sockfd, pdu->hdr, pdu->length, 0,
611 &dst->addr.sa, dst->size);
613 if (bytes_written >= 0)
615 coap_transaction_id(dst, pdu, &id);
619 coap_log(LOG_CRIT, "coap_send: sendto\n");
624 #endif /* WITH_POSIX || WITH_ARDUINO */
626 /* releases space allocated by PDU if free_pdu is set */
628 coap_send_impl(coap_context_t *context,
629 const coap_address_t *dst,
632 coap_tid_t id = COAP_INVALID_TID;
634 if ( !context || !dst || !pdu )
637 /* FIXME: is there a way to check if send was successful? */
638 uip_udp_packet_sendto(context->conn, pdu->hdr, pdu->length,
639 &dst->addr, dst->port);
641 coap_transaction_id(dst, pdu, &id);
645 #endif /* WITH_CONTIKI */
648 coap_send_impl(coap_context_t *context,
649 const coap_address_t *dst,
652 coap_tid_t id = COAP_INVALID_TID;
657 if ( !context || !dst || !pdu )
662 data_backup = pdu->data;
664 /* FIXME: we can't check this here with the existing infrastructure, but we
665 * should actually check that the pdu is not held by anyone but us. the
666 * respective pbuf is already exclusively owned by the pdu. */
669 LWIP_ASSERT("The PDU header is not where it is expected", pdu->hdr == p->payload + sizeof(coap_pdu_t));
671 err = pbuf_header(p, -sizeof(coap_pdu_t));
674 debug("coap_send_impl: pbuf_header failed\n");
679 coap_transaction_id(dst, pdu, &id);
681 pbuf_realloc(p, pdu->length);
683 udp_sendto(context->pcb, p,
684 &dst->addr, dst->port);
686 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 */
688 err = pbuf_header(p, sizeof(coap_pdu_t));
689 LWIP_ASSERT("Cannot undo pbuf_header", err == 0);
691 /* restore destroyed pdu data */
692 LWIP_ASSERT("PDU not restored", p->payload == pdu);
693 pdu->max_size = p->tot_len - sizeof(coap_pdu_t); /* reduced after pbuf_realloc */
694 pdu->hdr = p->payload + sizeof(coap_pdu_t);
695 pdu->max_delta = 0; /* won't be used any more */
696 pdu->length = pdu->max_size;
697 pdu->data = data_backup;
702 #endif /* WITH_LWIP */
704 coap_tid_t coap_send(coap_context_t *context, const coap_address_t *dst, coap_pdu_t *pdu)
707 return coap_send_impl(context, dst, pdu);
711 coap_tid_t coap_send_error(coap_context_t *context, coap_pdu_t *request, const coap_address_t *dst,
712 unsigned char code, coap_opt_filter_t opts)
714 coap_pdu_t *response;
715 coap_tid_t result = COAP_INVALID_TID;
720 response = coap_new_error_response(request, code, opts);
723 result = coap_send(context, dst, response);
724 coap_delete_pdu(response);
730 coap_tid_t coap_send_message_type(coap_context_t *context, const coap_address_t *dst,
731 coap_pdu_t *request, unsigned char type)
733 coap_pdu_t *response;
734 coap_tid_t result = COAP_INVALID_TID;
738 response = coap_pdu_init(type, 0, request->hdr->id, sizeof(coap_pdu_t));
741 result = coap_send(context, dst, response);
742 coap_delete_pdu(response);
748 coap_tid_t coap_send_confirmed(coap_context_t *context, const coap_address_t *dst, coap_pdu_t *pdu)
754 node = coap_new_node();
757 debug("coap_send_confirmed: insufficient memory\n");
758 return COAP_INVALID_TID;
761 node->id = coap_send_impl(context, dst, pdu);
762 if (COAP_INVALID_TID == node->id)
764 debug("coap_send_confirmed: error sending pdu\n");
765 coap_free_node(node);
766 return COAP_INVALID_TID;
769 prng((unsigned char *)&r, sizeof(r));
771 /* add randomized RESPONSE_TIMEOUT to determine retransmission timeout */
772 node->timeout = COAP_DEFAULT_RESPONSE_TIMEOUT * COAP_TICKS_PER_SECOND
773 + (COAP_DEFAULT_RESPONSE_TIMEOUT >> 1) * ((COAP_TICKS_PER_SECOND * (r & 0xFF)) >> 8);
775 memcpy(&node->remote, dst, sizeof(coap_address_t));
778 /* Set timer for pdu retransmission. If this is the first element in
779 * the retransmission queue, the base time is set to the current
780 * time and the retransmission time is node->timeout. If there is
781 * already an entry in the sendqueue, we must check if this node is
782 * to be retransmitted earlier. Therefore, node->timeout is first
783 * normalized to the base time and then inserted into the queue with
784 * an adjusted relative time.
786 if (context->sendqueue == NULL)
788 node->t = node->timeout;
789 context->sendqueue_basetime = now;
793 /* make node->t relative to context->sendqueue_basetime */
794 node->t = (now - context->sendqueue_basetime) + node->timeout;
797 coap_insert_node(&context->sendqueue, node);
800 if (node == context->sendqueue) /* don't bother with timer stuff if there are earlier retransmits */
801 coap_retransmittimer_restart(context);
805 { /* (re-)initialize retransmission timer */
806 coap_queue_t *nextpdu;
808 nextpdu = coap_peek_next(context);
809 assert(nextpdu); /* we have just inserted a node */
811 /* must set timer within the context of the retransmit process */
812 PROCESS_CONTEXT_BEGIN(&coap_retransmit_process);
813 etimer_set(&context->retransmit_timer, nextpdu->t);
814 PROCESS_CONTEXT_END(&coap_retransmit_process);
816 #endif /* WITH_CONTIKI */
821 coap_tid_t coap_retransmit(coap_context_t *context, coap_queue_t *node)
823 if (!context || !node)
824 return COAP_INVALID_TID;
826 /* re-initialize timeout when maximum number of retransmissions are not reached yet */
827 if (node->retransmit_cnt < COAP_DEFAULT_MAX_RETRANSMIT)
829 node->retransmit_cnt++;
830 node->t = node->timeout << node->retransmit_cnt;
831 coap_insert_node(&context->sendqueue, node);
833 if (node == context->sendqueue) /* don't bother with timer stuff if there are earlier retransmits */
834 coap_retransmittimer_restart(context);
838 "** retransmission #%d of transaction %d\n", node->retransmit_cnt, ntohs(node->pdu->hdr->id));
840 node->id = coap_send_impl(context, &node->remote, node->pdu);
844 /* no more retransmissions, remove node from system */
846 #if !defined(WITH_CONTIKI) && !defined(WITH_ARDUINO)
847 debug("** removed transaction %d\n", ntohs(node->id));
850 #ifndef WITHOUT_OBSERVE
851 /* Check if subscriptions exist that should be canceled after
852 COAP_MAX_NOTIFY_FAILURES */
853 if (node->pdu->hdr->code >= 64)
858 token.length = node->pdu->hdr->token_length;
859 token.s = node->pdu->hdr->token;
861 coap_handle_failed_notify(context, &node->remote, &token);
863 #endif /* WITHOUT_OBSERVE */
865 /* And finally delete the node */
866 coap_delete_node(node);
867 return COAP_INVALID_TID;
871 * Checks if @p opt fits into the message that ends with @p maxpos.
872 * This function returns @c 1 on success, or @c 0 if the option @p opt
873 * would exceed @p maxpos.
875 static inline int check_opt_size(coap_opt_t *opt, unsigned char *maxpos)
877 if (opt && opt < maxpos)
879 if (((*opt & 0x0f) < 0x0f) || (opt + 1 < maxpos))
880 return opt + COAP_OPT_SIZE(opt) < maxpos;
886 int coap_read(coap_context_t *ctx)
888 #if defined(WITH_POSIX)
889 static char buf[COAP_MAX_PDU_SIZE];
891 #if defined(WITH_LWIP) || defined(WITH_CONTIKI)
895 ssize_t bytes_read = -1;
896 coap_address_t src, dst;
901 #endif /* WITH_CONTIKI */
903 LWIP_ASSERT("No package pending", ctx->pending_package != NULL);
904 LWIP_ASSERT("Can only deal with contiguous PBUFs to read the initial details", ctx->pending_package->tot_len == ctx->pending_package->len);
905 buf = ctx->pending_package->payload;
906 #endif /* WITH_LWIP */
908 pdu = (coap_hdr_t *) buf;
910 coap_address_init(&src);
912 #if defined(WITH_POSIX)
913 bytes_read = recvfrom(ctx->sockfd, buf, sizeof(buf), 0, &src.addr.sa, &src.size);
915 #endif /* WITH_POSIX || WITH_ARDUINO */
919 uip_ipaddr_copy(&src.addr, &UIP_IP_BUF->srcipaddr);
920 src.port = UIP_UDP_BUF->srcport;
921 uip_ipaddr_copy(&dst.addr, &UIP_IP_BUF->destipaddr);
922 dst.port = UIP_UDP_BUF->destport;
924 bytes_read = uip_datalen();
925 ((char *)uip_appdata)[bytes_read] = 0;
926 PRINTF("Server received %d bytes from [", (int)bytes_read);
927 PRINT6ADDR(&src.addr);
928 PRINTF("]:%d\n", uip_ntohs(src.port));
930 #endif /* WITH_CONTIKI */
932 /* FIXME: use lwip address operation functions */
933 src.addr.addr = ctx->pending_address.addr;
934 src.port = ctx->pending_port;
935 bytes_read = ctx->pending_package->tot_len;
936 #endif /* WITH_LWIP */
940 warn("coap_read: recvfrom");
944 if ((size_t) bytes_read < sizeof(coap_hdr_t))
946 debug("coap_read: discarded invalid frame\n");
950 if (pdu->version != COAP_DEFAULT_VERSION)
952 debug("coap_read: unknown protocol version\n");
956 node = coap_new_node();
961 node->pdu = coap_pdu_from_pbuf(ctx->pending_package);
962 ctx->pending_package = NULL;
964 node->pdu = coap_pdu_init(0, 0, 0, bytes_read);
969 coap_ticks(&node->t);
970 memcpy(&node->local, &dst, sizeof(coap_address_t));
971 memcpy(&node->remote, &src, sizeof(coap_address_t));
973 if (!coap_pdu_parse((unsigned char *) buf, bytes_read, node->pdu))
975 warn("discard malformed PDU");
979 /* and add new node to receive queue */
980 coap_transaction_id(&node->remote, node->pdu, &node->id);
981 coap_insert_node(&ctx->recvqueue, node);
984 if (LOG_DEBUG <= coap_get_log_level())
986 #ifndef INET6_ADDRSTRLEN
987 #define INET6_ADDRSTRLEN 40
989 unsigned char addr[INET6_ADDRSTRLEN + 8];
991 if (coap_print_addr(&src, addr, INET6_ADDRSTRLEN + 8))
992 debug("** received %d bytes from %s:\n", (int)bytes_read, addr);
994 coap_show_pdu(node->pdu);
1001 /* FIXME: send back RST? */
1002 coap_delete_node(node);
1006 /* even if there was an error, clean up */
1007 pbuf_free(ctx->pending_package);
1008 ctx->pending_package = NULL;
1012 #endif //#ifndef WITH_ARDUINO
1014 int coap_remove_from_queue(coap_queue_t **queue, coap_tid_t id, coap_queue_t **node)
1016 coap_queue_t *p, *q;
1018 if (!queue || !*queue)
1021 /* replace queue head if PDU's time is less than head's time */
1023 if (id == (*queue)->id)
1024 { /* found transaction */
1026 *queue = (*queue)->next;
1028 { /* adjust relative time of new queue head */
1029 (*queue)->t += (*node)->t;
1031 (*node)->next = NULL;
1032 /* coap_delete_node( q ); */
1033 debug("*** removed transaction %u\n", id);
1037 /* search transaction to remove (only first occurence will be removed) */
1043 } while (q && id != q->id);
1046 { /* found transaction */
1049 { /* must update relative time of p->next */
1054 /* coap_delete_node( q ); */
1055 debug("*** removed transaction %u\n", id);
1063 static inline int token_match(const unsigned char *a, size_t alen, const unsigned char *b,
1066 return alen == blen && (alen == 0 || memcmp(a, b, alen) == 0);
1069 void coap_cancel_all_messages(coap_context_t *context, const coap_address_t *dst,
1070 const unsigned char *token, size_t token_length)
1072 /* cancel all messages in sendqueue that are for dst
1073 * and use the specified token */
1074 coap_queue_t *p, *q;
1076 debug("cancel_all_messages\n");
1077 while (context->sendqueue && coap_address_equals(dst, &context->sendqueue->remote)
1078 && token_match(token, token_length, context->sendqueue->pdu->hdr->token,
1079 context->sendqueue->pdu->hdr->token_length))
1081 q = context->sendqueue;
1082 context->sendqueue = q->next;
1083 debug("**** removed transaction %d\n", ntohs(q->pdu->hdr->id));
1084 coap_delete_node(q);
1087 if (!context->sendqueue)
1090 p = context->sendqueue;
1093 /* when q is not NULL, it does not match (dst, token), so we can skip it */
1096 if (coap_address_equals(dst, &q->remote)
1097 && token_match(token, token_length, q->pdu->hdr->token, q->pdu->hdr->token_length))
1100 debug("**** removed transaction %d\n", ntohs(q->pdu->hdr->id));
1101 coap_delete_node(q);
1113 coap_find_transaction(coap_queue_t *queue, coap_tid_t id)
1115 while (queue && queue->id != id)
1116 queue = queue->next;
1122 coap_new_error_response(coap_pdu_t *request, unsigned char code, coap_opt_filter_t opts)
1124 coap_opt_iterator_t opt_iter;
1125 coap_pdu_t *response;
1126 size_t size = sizeof(coap_hdr_t) + request->hdr->token_length;
1129 unsigned short opt_type = 0; /* used for calculating delta-storage */
1131 #if COAP_ERROR_PHRASE_LENGTH > 0
1132 char *phrase = coap_response_phrase(code);
1134 /* Need some more space for the error phrase and payload start marker */
1136 size += strlen(phrase) + 1;
1141 /* cannot send ACK if original request was not confirmable */
1142 type = request->hdr->type == COAP_MESSAGE_CON ? COAP_MESSAGE_ACK : COAP_MESSAGE_NON;
1144 /* Estimate how much space we need for options to copy from
1145 * request. We always need the Token, for 4.02 the unknown critical
1146 * options must be included as well. */
1147 coap_option_clrb(opts, COAP_OPTION_CONTENT_TYPE); /* we do not want this */
1149 coap_option_iterator_init(request, &opt_iter, opts);
1151 /* Add size of each unknown critical option. As known critical
1152 options as well as elective options are not copied, the delta
1155 while ((option = coap_option_next(&opt_iter)))
1157 unsigned short delta = opt_iter.type - opt_type;
1158 /* calculate space required to encode (opt_iter.type - opt_type) */
1163 else if (delta < 269)
1172 /* add coap_opt_length(option) and the number of additional bytes
1173 * required to encode the option length */
1175 size += coap_opt_length(option);
1176 switch (*option & 0x0f)
1188 opt_type = opt_iter.type;
1191 /* Now create the response and fill with options and payload data. */
1192 response = coap_pdu_init(type, code, request->hdr->id, size);
1196 if (!coap_add_token(response, request->hdr->token_length, request->hdr->token))
1198 debug("cannot add token to error response\n");
1199 coap_delete_pdu(response);
1203 /* copy all options */
1204 coap_option_iterator_init(request, &opt_iter, opts);
1205 while ((option = coap_option_next(&opt_iter)))
1206 coap_add_option(response, opt_iter.type, COAP_OPT_LENGTH(option),
1207 COAP_OPT_VALUE(option));
1209 #if COAP_ERROR_PHRASE_LENGTH > 0
1210 /* note that diagnostic messages do not need a Content-Format option. */
1212 coap_add_data(response, strlen(phrase), (unsigned char *) phrase);
1220 * Quick hack to determine the size of the resource description for
1223 static inline size_t get_wkc_len(coap_context_t *context, coap_opt_t *query_filter)
1225 unsigned char buf[1];
1228 if (print_wellknown(context, buf, &len, UINT_MAX, query_filter) & COAP_PRINT_STATUS_ERROR)
1230 warn("cannot determine length of /.well-known/core\n");
1234 debug("get_wkc_len: print_wellknown() returned %zu\n", len);
1239 #define SZX_TO_BYTES(SZX) ((size_t)(1 << ((SZX) + 4)))
1242 wellknown_response(coap_context_t *context, coap_pdu_t *request)
1245 coap_opt_iterator_t opt_iter;
1246 size_t len, wkc_len;
1247 unsigned char buf[2];
1249 int need_block2 = 0; /* set to 1 if Block2 option is required */
1251 coap_opt_t *query_filter;
1254 resp = coap_pdu_init(
1255 request->hdr->type == COAP_MESSAGE_CON ? COAP_MESSAGE_ACK : COAP_MESSAGE_NON,
1256 COAP_RESPONSE_CODE(205),
1257 request->hdr->id, COAP_MAX_PDU_SIZE);
1260 debug("wellknown_response: cannot create PDU\n");
1264 if (!coap_add_token(resp, request->hdr->token_length, request->hdr->token))
1266 debug("wellknown_response: cannot add token\n");
1270 query_filter = coap_check_option(request, COAP_OPTION_URI_QUERY, &opt_iter);
1271 wkc_len = get_wkc_len(context, query_filter);
1273 /* check whether the request contains the Block2 option */
1274 if (coap_get_block(request, COAP_OPTION_BLOCK2, &block))
1276 offset = block.num << (block.szx + 4);
1278 { /* invalid, MUST lead to 4.00 Bad Request */
1279 resp->hdr->code = COAP_RESPONSE_CODE(400);
1282 else if (block.szx > COAP_MAX_BLOCK_SZX)
1284 block.szx = COAP_MAX_BLOCK_SZX;
1285 block.num = offset >> (block.szx + 4);
1291 /* Check if there is sufficient space to add Content-Format option
1292 * and data. We do this before adding the Content-Format option to
1293 * avoid sending error responses with that option but no actual
1295 if (resp->max_size <= (size_t) resp->length + 3)
1297 debug("wellknown_response: insufficient storage space\n");
1301 /* Add Content-Format. As we have checked for available storage,
1302 * nothing should go wrong here. */
1303 assert(coap_encode_var_bytes(buf, COAP_MEDIATYPE_APPLICATION_LINK_FORMAT) == 1);
1304 coap_add_option(resp, COAP_OPTION_CONTENT_FORMAT,
1305 coap_encode_var_bytes(buf, COAP_MEDIATYPE_APPLICATION_LINK_FORMAT), buf);
1307 /* check if Block2 option is required even if not requested */
1308 if (!need_block2 && (resp->max_size - (size_t) resp->length < wkc_len))
1310 assert(resp->length <= resp->max_size);
1311 const size_t payloadlen = resp->max_size - resp->length;
1312 /* yes, need block-wise transfer */
1314 block.m = 0; /* the M bit is set by coap_write_block_opt() */
1315 block.szx = COAP_MAX_BLOCK_SZX;
1316 while (payloadlen < SZX_TO_BYTES(block.szx))
1320 debug("wellknown_response: message to small even for szx == 0\n");
1332 /* write Block2 option if necessary */
1335 if (coap_write_block_opt(&block, COAP_OPTION_BLOCK2, resp, wkc_len) < 0)
1337 debug("wellknown_response: cannot add Block2 option\n");
1342 /* Manually set payload of response to let print_wellknown() write,
1343 * into our buffer without copying data. */
1345 resp->data = (unsigned char *) resp->hdr + resp->length;
1346 *resp->data = COAP_PAYLOAD_START;
1349 len = need_block2 ? SZX_TO_BYTES(block.szx) : resp->max_size - resp->length;
1351 result = print_wellknown(context, resp->data, &len, offset, query_filter);
1352 if ((result & COAP_PRINT_STATUS_ERROR) != 0)
1354 debug("print_wellknown failed\n");
1358 resp->length += COAP_PRINT_OUTPUT_LENGTH(result);
1362 /* set error code 5.03 and remove all options and data from response */
1363 resp->hdr->code = COAP_RESPONSE_CODE(503);
1364 resp->length = sizeof(coap_hdr_t) + resp->hdr->token_length;
1368 #define WANT_WKC(Pdu,Key) \
1369 (((Pdu)->hdr->code == COAP_REQUEST_GET) && is_wkc(Key))
1371 void handle_request(coap_context_t *context, coap_queue_t *node, const char* responseData)
1374 coap_method_handler_t h = NULL;
1375 coap_pdu_t *response = NULL;
1376 coap_opt_filter_t opt_filter;
1377 coap_resource_t *resource;
1380 coap_option_filter_clear(opt_filter);
1382 /* try to find the resource from the request URI */
1383 coap_hash_request_uri(node->pdu, key);
1384 resource = coap_get_resource_from_key(context, key);
1388 /* The resource was not found. Check if the request URI happens to
1389 * be the well-known URI. In that case, we generate a default
1390 * response, otherwise, we return 4.04 */
1392 switch (node->pdu->hdr->code)
1395 case COAP_REQUEST_GET:
1397 { /* GET request for .well-known/core */
1398 info("create default response for %s\n", COAP_DEFAULT_URI_WELLKNOWN);
1400 response = wellknown_response(context, node->pdu);
1404 { /* GET request for any another resource, return 4.04 */
1407 "GET for unknown resource 0x%02x%02x%02x%02x, return 4.04\n", key[0], key[1], key[2], key[3]);
1408 response = coap_new_error_response(node->pdu, COAP_RESPONSE_CODE(404),
1413 default: /* any other request type */
1416 "unhandled request for unknown resource 0x%02x%02x%02x%02x\r\n", key[0], key[1], key[2], key[3]);
1417 if (!coap_is_mcast(&node->local))
1418 response = coap_new_error_response(node->pdu, COAP_RESPONSE_CODE(405),
1422 if (response && coap_send(context, &node->remote, response) == COAP_INVALID_TID)
1424 warn("cannot send response for transaction %u\n", node->id);
1426 coap_delete_pdu(response);
1431 /* the resource was found, check if there is a registered handler */
1432 if ((size_t) node->pdu->hdr->code - 1
1433 < sizeof(resource->handler) / sizeof(coap_method_handler_t))
1434 h = resource->handler[node->pdu->hdr->code - 1];
1439 "call custom handler for resource 0x%02x%02x%02x%02x\n", key[0], key[1], key[2], key[3]);
1440 response = coap_pdu_init(
1441 node->pdu->hdr->type == COAP_MESSAGE_CON ? COAP_MESSAGE_ACK : COAP_MESSAGE_NON,
1442 0, node->pdu->hdr->id, COAP_MAX_PDU_SIZE);
1444 /* Implementation detail: coap_add_token() immediately returns 0
1445 if response == NULL */
1446 if (coap_add_token(response, node->pdu->hdr->token_length, node->pdu->hdr->token))
1449 { node->pdu->hdr->token_length, node->pdu->hdr->token };
1451 h(context, resource, &node->remote, node->pdu, &token, response);
1453 unsigned char buf[3];
1454 response->hdr->code = COAP_RESPONSE_CODE(205);
1455 coap_add_option(response, COAP_OPTION_CONTENT_TYPE,
1456 coap_encode_var_bytes(buf, COAP_MEDIATYPE_TEXT_PLAIN), buf);
1457 coap_add_option(response, COAP_OPTION_MAXAGE, coap_encode_var_bytes(buf, 0x2ffff), buf);
1458 coap_add_data(response, strlen(responseData), (unsigned char *) responseData);
1460 if (response->hdr->type != COAP_MESSAGE_NON
1461 || (response->hdr->code >= 64 && !coap_is_mcast(&node->local)))
1464 if (coap_send(context, &node->remote, response) == COAP_INVALID_TID)
1466 debug("cannot send response for message %d\n", node->pdu->hdr->id);
1470 coap_delete_pdu(response);
1474 warn("cannot generate response\r\n");
1479 if (WANT_WKC(node->pdu, key))
1481 debug("create default response for %s\n", COAP_DEFAULT_URI_WELLKNOWN);
1482 response = wellknown_response(context, node->pdu);
1485 response = coap_new_error_response(node->pdu, COAP_RESPONSE_CODE(405), opt_filter);
1487 if (!response || (coap_send(context, &node->remote, response) == COAP_INVALID_TID))
1489 debug("cannot send response for transaction %u\n", node->id);
1491 coap_delete_pdu(response);
1495 static inline void handle_response(coap_context_t *context, coap_queue_t *sent, coap_queue_t *rcvd)
1498 /* Call application-specific reponse handler when available. If
1499 * not, we must acknowledge confirmable messages. */
1500 if (context->response_handler)
1503 context->response_handler(context, &rcvd->remote, sent ? sent->pdu : NULL, rcvd->pdu,
1508 /* send ACK if rcvd is confirmable (i.e. a separate response) */
1509 coap_send_ack(context, &rcvd->remote, rcvd->pdu);
1515 handle_locally(coap_context_t *context __attribute__ ((unused)),
1516 coap_queue_t *node __attribute__ ((unused)))
1518 #else /* not a GCC */
1519 handle_locally(coap_context_t *context, coap_queue_t *node)
1522 /* this function can be used to check if node->pdu is really for us */
1527 * This function handles RST messages received for the message passed
1530 static void coap_handle_rst(coap_context_t *context, const coap_queue_t *sent)
1532 #ifndef WITHOUT_OBSERVE
1534 #ifndef COAP_RESOURCES_NOHASH
1535 coap_resource_t *tmp;
1540 /* remove observer for this resource, if any
1541 * get token from sent and try to find a matching resource. Uh!
1544 COAP_SET_STR(&token, sent->pdu->hdr->token_length, sent->pdu->hdr->token);
1546 #ifndef WITH_CONTIKI
1547 #ifdef COAP_RESOURCES_NOHASH
1548 LL_FOREACH(context->resources, r)
1551 HASH_ITER(hh, context->resources, r, tmp)
1554 coap_delete_observer(r, &sent->remote, &token);
1555 coap_cancel_all_messages(context, &sent->remote, token.s, token.length);
1557 #else /* WITH_CONTIKI */
1558 r = (coap_resource_t *)resource_storage.mem;
1559 for (i = 0; i < resource_storage.num; ++i, ++r)
1561 if (resource_storage.count[i])
1563 coap_delete_observer(r, &sent->remote, &token);
1564 coap_cancel_all_messages(context, &sent->remote, token.s, token.length);
1567 #endif /* WITH_CONTIKI */
1568 #endif /* WITOUT_OBSERVE */
1571 void coap_dispatch(coap_context_t *context, const char* responseData)
1573 coap_queue_t *rcvd = NULL, *sent = NULL;
1574 coap_pdu_t *response;
1575 coap_opt_filter_t opt_filter;
1580 memset(opt_filter, 0, sizeof(coap_opt_filter_t));
1582 while (context->recvqueue)
1584 rcvd = context->recvqueue;
1586 /* remove node from recvqueue */
1587 context->recvqueue = context->recvqueue->next;
1590 if (rcvd->pdu->hdr->version != COAP_DEFAULT_VERSION)
1592 debug("dropped packet with unknown version %u\n", rcvd->pdu->hdr->version);
1596 switch (rcvd->pdu->hdr->type)
1598 case COAP_MESSAGE_ACK:
1599 /* find transaction in sendqueue to stop retransmission */
1600 coap_remove_from_queue(&context->sendqueue, rcvd->id, &sent);
1602 if (rcvd->pdu->hdr->code == 0)
1605 /* FIXME: if sent code was >= 64 the message might have been a
1606 * notification. Then, we must flag the observer to be alive
1607 * by setting obs->fail_cnt = 0. */
1608 if (sent && COAP_RESPONSE_CLASS(sent->pdu->hdr->code) == 2)
1611 { sent->pdu->hdr->token_length, sent->pdu->hdr->token };
1612 coap_touch_observer(context, &sent->remote, &token);
1616 case COAP_MESSAGE_RST:
1617 /* We have sent something the receiver disliked, so we remove
1618 * not only the transaction but also the subscriptions we might
1621 coap_log(LOG_ALERT, "got RST for message %u\n", ntohs(rcvd->pdu->hdr->id));
1623 /* find transaction in sendqueue to stop retransmission */
1624 coap_remove_from_queue(&context->sendqueue, rcvd->id, &sent);
1627 coap_handle_rst(context, sent);
1630 case COAP_MESSAGE_NON: /* check for unknown critical options */
1631 if (coap_option_check_critical(context, rcvd->pdu, opt_filter) == 0)
1635 case COAP_MESSAGE_CON: /* check for unknown critical options */
1636 if (coap_option_check_critical(context, rcvd->pdu, opt_filter) == 0)
1639 /* FIXME: send response only if we have received a request. Otherwise,
1641 response = coap_new_error_response(rcvd->pdu, COAP_RESPONSE_CODE(402),
1645 warn("coap_dispatch: cannot create error reponse\n");
1648 if (coap_send(context, &rcvd->remote, response) == COAP_INVALID_TID)
1650 warn("coap_dispatch: error sending reponse\n");
1652 coap_delete_pdu(response);
1660 /* Pass message to upper layer if a specific handler was
1661 * registered for a request that should be handled locally. */
1662 if (handle_locally(context, rcvd))
1664 if (COAP_MESSAGE_IS_REQUEST(rcvd->pdu->hdr))
1665 handle_request(context, rcvd, responseData);
1666 else if (COAP_MESSAGE_IS_RESPONSE(rcvd->pdu->hdr))
1667 handle_response(context, sent, rcvd);
1670 debug("dropped message with invalid code\n");
1671 coap_send_message_type(context, &rcvd->remote, rcvd->pdu, COAP_MESSAGE_RST);
1675 cleanup: coap_delete_node(sent);
1676 coap_delete_node(rcvd);
1680 int coap_can_exit(coap_context_t *context)
1682 return !context || (context->recvqueue == NULL && context->sendqueue == NULL);
1687 /*---------------------------------------------------------------------------*/
1688 /* CoAP message retransmission */
1689 /*---------------------------------------------------------------------------*/
1690 PROCESS_THREAD(coap_retransmit_process, ev, data)
1693 coap_queue_t *nextpdu;
1697 debug("Started retransmit process\r\n");
1702 if (ev == PROCESS_EVENT_TIMER)
1704 if (etimer_expired(&the_coap_context.retransmit_timer))
1707 nextpdu = coap_peek_next(&the_coap_context);
1710 while (nextpdu && nextpdu->t <= now)
1712 coap_retransmit(&the_coap_context, coap_pop_next(&the_coap_context));
1713 nextpdu = coap_peek_next(&the_coap_context);
1716 /* need to set timer to some value even if no nextpdu is available */
1717 etimer_set(&the_coap_context.retransmit_timer,
1718 nextpdu ? nextpdu->t - now : 0xFFFF);
1720 #ifndef WITHOUT_OBSERVE
1721 if (etimer_expired(&the_coap_context.notify_timer))
1723 coap_check_notify(&the_coap_context);
1724 etimer_reset(&the_coap_context.notify_timer);
1726 #endif /* WITHOUT_OBSERVE */
1732 /*---------------------------------------------------------------------------*/
1734 #endif /* WITH_CONTIKI */
1737 /* FIXME: retransmits that are not required any more due to incoming packages
1738 * do *not* get cleared at the moment, the wakeup when the transmission is due
1739 * is silently accepted. this is mainly due to the fact that the required
1740 * checks are similar in two places in the code (when receiving ACK and RST)
1741 * and that they cause more than one patch chunk, as it must be first checked
1742 * whether the sendqueue item to be dropped is the next one pending, and later
1743 * the restart function has to be called. nothing insurmountable, but it can
1744 * also be implemented when things have stabilized, and the performance
1745 * penality is minimal
1747 * also, this completely ignores COAP_RESOURCE_CHECK_TIME.
1750 static void coap_retransmittimer_execute(void *arg)
1752 coap_context_t *ctx = (coap_context_t*)arg;
1754 coap_tick_t elapsed;
1755 coap_queue_t *nextinqueue;
1757 ctx->timer_configured = 0;
1761 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 */
1763 nextinqueue = coap_peek_next(ctx);
1764 while (nextinqueue != NULL)
1766 if (nextinqueue->t > elapsed)
1768 nextinqueue->t -= elapsed;
1773 elapsed -= nextinqueue->t;
1774 coap_retransmit(ctx, coap_pop_next(ctx));
1775 nextinqueue = coap_peek_next(ctx);
1779 ctx->sendqueue_basetime = now;
1781 coap_retransmittimer_restart(ctx);
1784 static void coap_retransmittimer_restart(coap_context_t *ctx)
1786 coap_tick_t now, elapsed, delay;
1788 if (ctx->timer_configured)
1790 printf("clearing\n");
1791 sys_untimeout(coap_retransmittimer_execute, (void*)ctx);
1792 ctx->timer_configured = 0;
1794 if (ctx->sendqueue != NULL)
1797 elapsed = now - ctx->sendqueue_basetime;
1798 if (ctx->sendqueue->t >= elapsed)
1800 delay = ctx->sendqueue->t - elapsed;
1804 /* a strange situation, but not completely impossible.
1806 * this happens, for example, right after
1807 * coap_retransmittimer_execute, when a retransmission
1808 * was *just not yet* due, and the clock ticked before
1809 * our coap_ticks was called.
1811 * not trying to retransmit anything now, as it might
1812 * cause uncontrollable recursion; let's just try again
1813 * with the next main loop run.
1818 printf("scheduling for %d ticks\n", delay);
1819 sys_timeout(delay, coap_retransmittimer_execute, (void*)ctx);
1820 ctx->timer_configured = 1;