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>
49 #if defined(WITH_POSIX) || defined(WITH_ARDUINO)
53 #if defined(WITH_DTLS)
55 #endif /* WITH_DTLS */
57 #define MOD_NAME ("net.c")
61 static inline coap_queue_t *
63 return (coap_queue_t *)coap_malloc(sizeof(coap_queue_t));
67 coap_free_node(coap_queue_t *node) {
70 #endif /* WITH_POSIX || WITH_ARDUINO */
73 #include <lwip/memp.h>
75 static void coap_retransmittimer_execute(void *arg);
76 static void coap_retransmittimer_restart(coap_context_t *ctx);
78 static inline coap_queue_t *
80 return (coap_queue_t *)memp_malloc(MEMP_COAP_NODE);
84 coap_free_node(coap_queue_t *node) {
85 memp_free(MEMP_COAP_NODE, node);
88 #endif /* WITH_LWIP */
91 # define DEBUG DEBUG_PRINT
95 #include "net/uip-debug.h"
97 clock_time_t clock_offset;
99 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
100 #define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[UIP_LLIPH_LEN])
102 void coap_resources_init();
103 void coap_pdu_resources_init();
105 unsigned char initialized = 0;
106 coap_context_t the_coap_context;
108 MEMB(node_storage, coap_queue_t, COAP_PDU_MAXCNT);
110 PROCESS(coap_retransmit_process, "message retransmit process");
112 static inline coap_queue_t *
114 return (coap_queue_t *)memb_alloc(&node_storage);
118 coap_free_node(coap_queue_t *node) {
119 memb_free(&node_storage, node);
121 #endif /* WITH_CONTIKI */
124 /** Callback to udp_recv when using lwIP. Gets called by lwIP on arriving
125 * packages, places a reference in context->pending_package, and calls
126 * coap_read to process the package. Thus, coap_read needs not be called in
127 * lwIP main loops. (When modifying this for thread-like operation, ie. if you
128 * remove the coap_read call from this, make sure that coap_read gets a chance
129 * to run before this callback is entered the next time.)
131 static void received_package(void *arg, struct udp_pcb *upcb, struct pbuf *p, ip_addr_t *addr, u16_t port)
133 struct coap_context_t *context = (coap_context_t *)arg;
135 LWIP_ASSERT("pending_package was not cleared.", context->pending_package == NULL);
137 context->pending_package = p; /* we don't free it, coap_read has to do that */
138 context->pending_address.addr = addr->addr; /* FIXME: this has to become address-type independent, probably there'll be an lwip function for that */
139 context->pending_port = port;
141 coap_read(context, -1); /* we want to read from unicast socket */
144 #endif /* WITH_LWIP */
146 unsigned int coap_adjust_basetime(coap_context_t *ctx, coap_tick_t now) {
147 unsigned int result = 0;
148 coap_tick_diff_t delta = now - ctx->sendqueue_basetime;
150 if (ctx->sendqueue) {
151 /* delta < 0 means that the new time stamp is before the old. */
153 ctx->sendqueue->t -= delta;
155 /* This case is more complex: The time must be advanced forward,
156 * thus possibly leading to timed out elements at the queue's
157 * start. For every element that has timed out, its relative
158 * time is set to zero and the result counter is increased. */
160 coap_queue_t *q = ctx->sendqueue;
162 while (q && (t + q->t < (coap_tick_t) delta)) {
169 /* finally adjust the first element that has not expired */
171 q->t = (coap_tick_t) delta - t;
176 /* adjust basetime */
177 ctx->sendqueue_basetime += delta;
182 int coap_insert_node(coap_queue_t **queue, coap_queue_t *node) {
187 /* set queue head if empty */
193 /* replace queue head if PDU's time is less than head's time */
195 if (node->t < q->t) {
198 q->t -= node->t; /* make q->t relative to node->t */
202 /* search for right place to insert */
204 node->t -= q->t; /* make node-> relative to q->t */
207 } while (q && q->t <= node->t);
209 /* insert new item */
211 q->t -= node->t; /* make q->t relative to node->t */
218 int coap_delete_node(coap_queue_t *node) {
222 coap_delete_pdu(node->pdu);
223 coap_free_node(node);
228 void coap_delete_all(coap_queue_t *queue) {
232 coap_delete_all(queue->next);
233 coap_delete_node(queue);
239 node = coap_malloc_node();
243 coap_log(LOG_WARNING, "coap_new_node: malloc\n");
248 memset(node, 0, sizeof *node);
253 coap_peek_next(coap_context_t *context) {
254 if (!context || !context->sendqueue)
257 return context->sendqueue;
261 coap_pop_next(coap_context_t *context) {
264 if (!context || !context->sendqueue)
267 next = context->sendqueue;
268 context->sendqueue = context->sendqueue->next;
269 if (context->sendqueue) {
270 context->sendqueue->t += next->t;
276 #ifdef COAP_DEFAULT_WKC_HASHKEY
277 /** Checks if @p Key is equal to the pre-defined hash key for.well-known/core. */
278 #define is_wkc(Key) \
279 (memcmp((Key), COAP_DEFAULT_WKC_HASHKEY, sizeof(coap_key_t)) == 0)
281 /* Implements a singleton to store a hash key for the .wellknown/core
284 is_wkc(coap_key_t k) {
285 static coap_key_t wkc;
286 static unsigned char _initialized = 0;
288 _initialized = coap_hash_path((unsigned char *)COAP_DEFAULT_URI_WELLKNOWN,
289 sizeof(COAP_DEFAULT_URI_WELLKNOWN) - 1, wkc);
291 return memcmp(k, wkc, sizeof(coap_key_t)) == 0;
296 coap_new_context(const coap_address_t *listen_addr) {
297 #if defined(WITH_POSIX) || defined(WITH_ARDUINO)
298 coap_context_t *c = (coap_context_t*)coap_malloc( sizeof( coap_context_t ) );
300 #endif /* WITH_POSIX || WITH_ARDUINO */
302 coap_context_t *c = memp_malloc(MEMP_COAP_CONTEXT);
303 #endif /* WITH_LWIP */
309 #endif /* WITH_CONTIKI */
313 coap_log(LOG_EMERG, "no listen address specified\n");
319 prng_init(LWIP_RAND());
320 #else /* WITH_LWIP */
321 prng_init((unsigned long)listen_addr ^ clock_offset);
322 #endif /* WITH_LWIP */
327 coap_log(LOG_EMERG, "coap_init: malloc:\n");
331 #endif /* not WITH_CONTIKI */
333 coap_resources_init();
334 coap_pdu_resources_init();
336 c = &the_coap_context;
338 #endif /* WITH_CONTIKI */
340 memset(c, 0, sizeof(coap_context_t));
342 /* set well-known sockfd to uninitialize value */
343 c->sockfd_wellknown = -1;
345 /* initialize message id */
346 prng((unsigned char * )&c->message_id, sizeof(unsigned short));
348 /* register the critical options that we know */
349 coap_register_option(c, COAP_OPTION_IF_MATCH);
350 coap_register_option(c, COAP_OPTION_URI_HOST);
351 coap_register_option(c, COAP_OPTION_IF_NONE_MATCH);
352 coap_register_option(c, COAP_OPTION_URI_PORT);
353 coap_register_option(c, COAP_OPTION_URI_PATH);
354 coap_register_option(c, COAP_OPTION_URI_QUERY);
355 coap_register_option(c, COAP_OPTION_ACCEPT);
356 coap_register_option(c, COAP_OPTION_PROXY_URI);
357 coap_register_option(c, COAP_OPTION_PROXY_SCHEME);
358 coap_register_option(c, COAP_OPTION_BLOCK2);
359 coap_register_option(c, COAP_OPTION_BLOCK1);
361 #if defined(WITH_POSIX) || defined(WITH_ARDUINO)
362 if (OCInitUDP((OCDevAddr *)listen_addr, (int32_t *)&(c->sockfd)) != ERR_SUCCESS) {
367 #if defined(WITH_DTLS)
368 if (coap_dtls_init(c) != 0) {
373 /* set dtls socket file descriptor to uninitialize value */
375 #endif /* WITH_DTLS */
378 #endif /* WITH_POSIX || WITH_ARDUINO */
380 c->conn = udp_new(NULL, 0, NULL);
381 udp_bind(c->conn, listen_addr->port);
383 process_start(&coap_retransmit_process, (char *)c);
385 PROCESS_CONTEXT_BEGIN(&coap_retransmit_process);
386 #ifndef WITHOUT_OBSERVE
387 etimer_set(&c->notify_timer, COAP_RESOURCE_CHECK_TIME * COAP_TICKS_PER_SECOND);
388 #endif /* WITHOUT_OBSERVE */
389 /* the retransmit timer must be initialized to some large value */
390 etimer_set(&the_coap_context.retransmit_timer, 0xFFFF);
391 PROCESS_CONTEXT_END(&coap_retransmit_process);
393 #endif /* WITH_CONTIKI */
396 /* hard assert: this is not expected to fail dynamically */
397 LWIP_ASSERT("Failed to allocate PCB for CoAP", c->pcb != NULL);
399 udp_recv(c->pcb, received_package, (void*)c);
400 udp_bind(c->pcb, &listen_addr->addr, listen_addr->port);
402 c->timer_configured = 0;
409 void coap_free_context(coap_context_t *context) {
413 coap_delete_all(context->recvqueue);
414 coap_delete_all(context->sendqueue);
417 context->sendqueue = NULL;
418 coap_retransmittimer_restart(context);
421 #if defined(WITH_POSIX) || defined(WITH_ARDUINO)
422 /* coap_delete_list(context->subscriptions); */
423 OCClose( context->sockfd );
424 if (context->sockfd_wellknown != -1) {
425 OCClose( context->sockfd_wellknown );
427 #if defined(WITH_DTLS)
428 coap_dtls_deinit( context );
429 #endif /* WITH_DTLS */
430 coap_free( context );
433 udp_remove(context->pcb);
434 memp_free(MEMP_COAP_CONTEXT, context);
437 memset(&the_coap_context, 0, sizeof(coap_context_t));
439 #endif /* WITH_CONTIKI */
442 int coap_join_wellknown_group(coap_context_t *ctx,
443 const coap_address_t *multicast_addr) {
444 #if defined(WITH_POSIX) || defined(WITH_ARDUINO)
445 if (OCInitUDPMulticast((OCDevAddr *)multicast_addr,
446 (int32_t *)&(ctx->sockfd_wellknown)) != ERR_SUCCESS) {
454 int coap_option_check_critical(coap_context_t *ctx, coap_pdu_t *pdu,
455 coap_opt_filter_t unknown) {
457 coap_opt_iterator_t opt_iter;
460 coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL);
462 while (coap_option_next(&opt_iter)) {
464 /* The following condition makes use of the fact that
465 * coap_option_getb() returns -1 if type exceeds the bit-vector
466 * filter. As the vector is supposed to be large enough to hold
467 * the largest known option, we know that everything beyond is
470 if ((opt_iter.type & 0x01)
471 && coap_option_getb(ctx->known_options, opt_iter.type) < 1) {
472 debug("unknown critical option %d\n", opt_iter.type);
476 /* When opt_iter.type is beyond our known option range,
477 * coap_option_setb() will return -1 and we are safe to leave
479 if (coap_option_setb(unknown, opt_iter.type) == -1)
487 void coap_transaction_id(const coap_address_t *peer, const coap_pdu_t *pdu,
491 memset(h, 0, sizeof(coap_key_t));
493 /* Compare the complete address structure in case of IPv4. For IPv6,
494 * we need to look at the transport address only. */
497 switch (peer->addr.sa.sa_family) {
499 coap_hash((const unsigned char *)&peer->addr.sa, peer->size, h);
502 coap_hash((const unsigned char *)&peer->addr.sin6.sin6_port,
503 sizeof(peer->addr.sin6.sin6_port), h);
504 coap_hash((const unsigned char *)&peer->addr.sin6.sin6_addr,
505 sizeof(peer->addr.sin6.sin6_addr), h);
513 coap_hash((const unsigned char *)peer->addr, peer->size, h);
514 #endif /* WITH_ARDUINO */
516 #if defined(WITH_LWIP) || defined(WITH_CONTIKI)
517 /* FIXME: with lwip, we can do better */
518 coap_hash((const unsigned char *)&peer->port, sizeof(peer->port), h);
519 coap_hash((const unsigned char *)&peer->addr, sizeof(peer->addr), h);
520 #endif /* WITH_LWIP || WITH_CONTIKI */
522 coap_hash((const unsigned char * )&pdu->hdr->id, sizeof(unsigned short), h);
524 *id = ((h[0] << 8) | h[1]) ^ ((h[2] << 8) | h[3]);
527 coap_tid_t coap_send_ack(coap_context_t *context, const coap_address_t *dst,
528 coap_pdu_t *request, coap_send_flags_t flag) {
529 coap_pdu_t *response;
530 coap_tid_t result = COAP_INVALID_TID;
532 if (request && request->hdr->type == COAP_MESSAGE_CON) {
533 response = coap_pdu_init(COAP_MESSAGE_ACK, 0, request->hdr->id,
536 result = coap_send(context, dst, response, flag, NULL);
537 coap_delete_pdu(response);
543 #if defined(WITH_POSIX) || defined(WITH_ARDUINO)
544 /* releases space allocated by PDU if free_pdu is set */
546 coap_send_impl(coap_context_t *context,
547 const coap_address_t *dst,
550 int bytes_written = -1;
552 if ( !context || !dst || !pdu )
553 return bytes_written;
555 bytes_written = OCSendTo( context->sockfd, (uint8_t*)(pdu->hdr), pdu->length, 0,
557 debug("bytes_written %d\n", (int)bytes_written);
559 return bytes_written;
561 #endif /* WITH_POSIX || WITH_ARDUINO */
563 /* releases space allocated by PDU if free_pdu is set */
565 coap_send_impl(coap_context_t *context,
566 const coap_address_t *dst,
568 coap_tid_t id = COAP_INVALID_TID;
570 if ( !context || !dst || !pdu )
573 /* FIXME: is there a way to check if send was successful? */
574 uip_udp_packet_sendto(context->conn, pdu->hdr, pdu->length,
575 &dst->addr, dst->port);
577 coap_transaction_id(dst, pdu, &id);
581 #endif /* WITH_CONTIKI */
584 coap_send_impl(coap_context_t *context,
585 const coap_address_t *dst,
587 coap_tid_t id = COAP_INVALID_TID;
592 if ( !context || !dst || !pdu )
597 data_backup = pdu->data;
599 /* FIXME: we can't check this here with the existing infrastructure, but we
600 * should actually check that the pdu is not held by anyone but us. the
601 * respective pbuf is already exclusively owned by the pdu. */
604 LWIP_ASSERT("The PDU header is not where it is expected", pdu->hdr == p->payload + sizeof(coap_pdu_t));
606 err = pbuf_header(p, -sizeof(coap_pdu_t));
609 debug("coap_send_impl: pbuf_header failed\n");
614 coap_transaction_id(dst, pdu, &id);
616 pbuf_realloc(p, pdu->length);
618 udp_sendto(context->pcb, p,
619 &dst->addr, dst->port);
621 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 */
623 err = pbuf_header(p, sizeof(coap_pdu_t));
624 LWIP_ASSERT("Cannot undo pbuf_header", err == 0);
626 /* restore destroyed pdu data */
627 LWIP_ASSERT("PDU not restored", p->payload == pdu);
628 pdu->max_size = p->tot_len - sizeof(coap_pdu_t); /* reduced after pbuf_realloc */
629 pdu->hdr = p->payload + sizeof(coap_pdu_t);
630 pdu->max_delta = 0; /* won't be used any more */
631 pdu->length = pdu->max_size;
632 pdu->data = data_backup;
637 #endif /* WITH_LWIP */
639 coap_tid_t coap_send_error(coap_context_t *context, coap_pdu_t *request,
640 const coap_address_t *dst, unsigned char code, coap_opt_filter_t opts,
641 coap_send_flags_t flag) {
642 coap_pdu_t *response;
643 coap_tid_t result = COAP_INVALID_TID;
648 response = coap_new_error_response(request, code, opts);
650 result = coap_send(context, dst, response, flag, NULL);
651 coap_delete_pdu(response);
657 coap_tid_t coap_send_message_type(coap_context_t *context,
658 const coap_address_t *dst, coap_pdu_t *request,
659 coap_send_flags_t flag, unsigned char type) {
660 coap_pdu_t *response;
661 coap_tid_t result = COAP_INVALID_TID;
664 response = coap_pdu_init(type, 0, request->hdr->id, sizeof(coap_pdu_t));
666 result = coap_send(context, dst, response, flag, NULL);
667 coap_delete_pdu(response);
673 coap_tid_t coap_send(coap_context_t *context,
674 const coap_address_t *dst, coap_pdu_t *pdu, coap_send_flags_t flag,
677 coap_queue_t *node = NULL;
684 return COAP_INVALID_TID;
685 if(!(flag & SEND_RETX)){
686 coap_transaction_id(dst, pdu, &tid);
688 if((flag & SEND_NOW) || (flag & SEND_RETX))
693 node = coap_new_node();
695 debug("coap_send: insufficient memory\n");
696 return COAP_INVALID_TID;
699 prng((unsigned char * )&r, sizeof(r));
700 /* add randomized RESPONSE_TIMEOUT to determine retransmission timeout */
701 if(flag & SEND_NOW_CON) {
702 node->timeout = COAP_DEFAULT_RESPONSE_TIMEOUT * COAP_TICKS_PER_SECOND
703 + (COAP_DEFAULT_RESPONSE_TIMEOUT >> 1)
704 * ((COAP_TICKS_PER_SECOND * (r & 0xFF)) >> 8);
708 node->timeout = MAX_MULTICAST_DELAY_SEC * ((COAP_TICKS_PER_SECOND * (r & 0xFF)) >> 8);
709 node->delayedResponse = 1;
712 if (flag & SEND_SECURE_PORT) {
716 memcpy(&node->remote, dst, sizeof(coap_address_t));
720 /* Set timer for pdu retransmission. If this is the first element in
721 * the retransmission queue, the base time is set to the current
722 * time and the retransmission time is node->timeout. If there is
723 * already an entry in the sendqueue, we must check if this node is
724 * to be retransmitted earlier. Therefore, node->timeout is first
725 * normalized to the base time and then inserted into the queue with
726 * an adjusted relative time.
730 if (context->sendqueue == NULL)
732 node->t = node->timeout;
733 context->sendqueue_basetime = now;
737 /* make node->t relative to context->sendqueue_basetime */
738 node->t = (now - context->sendqueue_basetime) + node->timeout;
740 coap_insert_node(&context->sendqueue, node);
743 if (node == context->sendqueue)
744 /* don't bother with timer stuff if there are earlier retransmits */
745 coap_retransmittimer_restart(context);
749 { /* (re-)initialize retransmission timer */
750 coap_queue_t *nextpdu;
752 nextpdu = coap_peek_next(context);
753 assert(nextpdu); /* we have just inserted a node */
755 /* must set timer within the context of the retransmit process */
756 PROCESS_CONTEXT_BEGIN(&coap_retransmit_process);
757 etimer_set(&context->retransmit_timer, nextpdu->t);
758 PROCESS_CONTEXT_END(&coap_retransmit_process);
760 #endif /* WITH_CONTIKI */
762 if(flag & SEND_NOW_CON)
769 OC_LOG_V(DEBUG, MOD_NAME, PCF("sending 0x%x"), flag);
770 #if defined(WITH_DTLS)
771 // A secure packet is first encrypted by DTLS library and then send
773 if (flag & SEND_SECURE_PORT) {
774 bytesWritten = coap_dtls_encrypt(context, (OCDevAddr*)dst,
775 pdu, &node, tid, cache_flag);
777 bytesWritten = coap_send_impl(context, dst, pdu);
780 bytesWritten = coap_send_impl(context, dst, pdu);
781 #endif /* WITH_DTLS */
782 if(bytesWritten > 0) {
785 debug("coap_send_impl: error sending pdu\n");
786 coap_free_node(node);
787 return COAP_INVALID_TID;
790 coap_tid_t coap_retransmit(coap_context_t *context, coap_queue_t *node) {
791 coap_tid_t tid = COAP_INVALID_TID;
792 coap_send_flags_t flag;
794 if (!context || !node)
795 return COAP_INVALID_TID;
797 /* re-initialize timeout when maximum number of retransmissions are not reached yet */
798 if (node->retransmit_cnt < COAP_DEFAULT_MAX_RETRANSMIT) {
799 node->retransmit_cnt++;
800 node->t = node->timeout << node->retransmit_cnt;
801 coap_insert_node(&context->sendqueue, node);
803 if (node == context->sendqueue) /* don't bother with timer stuff if there are earlier retransmits */
804 coap_retransmittimer_restart(context);
807 debug("** retransmission #%d of transaction %d\n", node->retransmit_cnt,
808 ntohs(node->pdu->hdr->id));
809 flag = (coap_send_flags_t)(SEND_RETX | (node->secure ? SEND_SECURE_PORT : 0));
810 tid = coap_send(context, (coap_address_t *)&(node->remote),node->pdu, flag, NULL);
811 return (tid == COAP_INVALID_TID)? COAP_INVALID_TID : node->id;
814 /* no more retransmissions, remove node from system */
817 debug("** removed transaction %d\n", ntohs(node->id));
820 // deletion of node will happen in ocoap since we still need the info node has
821 return COAP_INVALID_TID;
825 * Checks if @p opt fits into the message that ends with @p maxpos.
826 * This function returns @c 1 on success, or @c 0 if the option @p opt
827 * would exceed @p maxpos.
829 static inline int check_opt_size(coap_opt_t *opt, unsigned char *maxpos) {
830 if (opt && opt < maxpos) {
831 if (((*opt & 0x0f) < 0x0f) || (opt + 1 < maxpos))
832 return opt + COAP_OPT_SIZE(opt) < maxpos;
837 int coap_read(coap_context_t *ctx, int sockfd) {
838 #if defined(WITH_POSIX) || defined(WITH_ARDUINO)
839 static char buf[COAP_MAX_PDU_SIZE];
841 #if defined(WITH_LWIP) || defined(WITH_CONTIKI)
848 coap_address_t src, dst;
850 unsigned char delayRes = 0;
854 #endif /* WITH_CONTIKI */
856 LWIP_ASSERT("No package pending", ctx->pending_package != NULL);
857 LWIP_ASSERT("Can only deal with contiguous PBUFs to read the initial details", ctx->pending_package->tot_len == ctx->pending_package->len);
858 pbuf = ctx->pending_package->payload;
859 #endif /* WITH_LWIP */
861 coap_address_init(&src);
863 #if defined(WITH_POSIX) || defined(WITH_ARDUINO)
864 bytes_read = OCRecvFrom( sockfd, (uint8_t*)pbuf, sizeof(buf), 0,
867 // Set the delayed response flag for responding to multicast requests
868 if (sockfd == ctx->sockfd_wellknown && bytes_read > 0) {
871 #if defined(WITH_DTLS)
872 // Perform the DTLS decryption if packet is coming on secure port
873 if (sockfd == ctx->sockfd_dtls && bytes_read > 0) {
874 if (coap_dtls_decrypt(ctx, (OCDevAddr*)&src, (uint8_t*)pbuf, bytes_read,
875 (uint8_t**)&pbuf, &bytes_read) < 0) {
879 #endif /* WITH_DTLS */
881 pdu = (coap_hdr_t *) pbuf;
882 #endif /* WITH_POSIX || WITH_ARDUINO */
885 uip_ipaddr_copy(&src.addr, &UIP_IP_BUF->srcipaddr);
886 src.port = UIP_UDP_BUF->srcport;
887 uip_ipaddr_copy(&dst.addr, &UIP_IP_BUF->destipaddr);
888 dst.port = UIP_UDP_BUF->destport;
890 bytes_read = uip_datalen();
891 ((char *)uip_appdata)[bytes_read] = 0;
892 PRINTF("Server received %d bytes from [", (int)bytes_read);
893 PRINT6ADDR(&src.addr);
894 PRINTF("]:%d\n", uip_ntohs(src.port));
896 #endif /* WITH_CONTIKI */
898 /* FIXME: use lwip address operation functions */
899 src.addr.addr = ctx->pending_address.addr;
900 src.port = ctx->pending_port;
901 bytes_read = ctx->pending_package->tot_len;
902 #endif /* WITH_LWIP */
904 if (bytes_read < 0) {
905 warn("coap_read: recvfrom\n");
909 if ((size_t) bytes_read < sizeof(coap_hdr_t)) {
910 debug("coap_read: discarded invalid frame\n");
914 if (pdu->version != COAP_DEFAULT_VERSION) {
915 debug("coap_read: unknown protocol version\n");
919 node = coap_new_node();
924 node->pdu = coap_pdu_from_pbuf(ctx->pending_package);
925 ctx->pending_package = NULL;
927 node->pdu = coap_pdu_init(0, 0, 0, bytes_read);
932 coap_ticks(&node->t);
933 memcpy(&node->local, &dst, sizeof(coap_address_t));
934 memcpy(&node->remote, &src, sizeof(coap_address_t));
936 if (!coap_pdu_parse((unsigned char *) pbuf, bytes_read, node->pdu)) {
937 warn("discard malformed PDU");
941 //set the delayed response flag
942 node->delayedResponse = delayRes;
944 //set the secure flag on the received packet
945 #if defined(WITH_DTLS)
946 node->secure = (sockfd == ctx->sockfd_dtls) ? 1 : 0;
949 #endif /* WITH_DTLS */
951 /* and add new node to receive queue */
952 coap_transaction_id(&node->remote, node->pdu, &node->id);
953 coap_insert_node(&ctx->recvqueue, node);
956 if (LOG_DEBUG <= coap_get_log_level()) {
957 #ifndef INET6_ADDRSTRLEN
958 #define INET6_ADDRSTRLEN 40
960 unsigned char addr[INET6_ADDRSTRLEN + 8];
962 if (coap_print_addr(&src, addr, INET6_ADDRSTRLEN + 8))
963 debug("** received %d bytes from %s:\n", (int )bytes_read, addr);
965 coap_show_pdu(node->pdu);
972 /* FIXME: send back RST? */
973 coap_delete_node(node);
977 /* even if there was an error, clean up */
978 pbuf_free(ctx->pending_package);
979 ctx->pending_package = NULL;
984 int coap_remove_from_queue(coap_queue_t **queue, coap_tid_t id,
985 coap_queue_t **node) {
988 if (!queue || !*queue)
991 /* replace queue head if PDU's time is less than head's time */
993 if (id == (*queue)->id) { /* found transaction */
995 *queue = (*queue)->next;
996 if (*queue) { /* adjust relative time of new queue head */
997 (*queue)->t += (*node)->t;
999 (*node)->next = NULL;
1000 /* coap_delete_node( q ); */
1001 debug("*** removed transaction %u\n", id);
1005 /* search transaction to remove (only first occurence will be removed) */
1010 } while (q && id != q->id);
1012 if (q) { /* found transaction */
1014 if (p->next) { /* must update relative time of p->next */
1019 /* coap_delete_node( q ); */
1020 debug("*** removed transaction %u\n", id);
1028 static inline int token_match(const unsigned char *a, size_t alen,
1029 const unsigned char *b, size_t blen) {
1030 return alen == blen && (alen == 0 || memcmp(a, b, alen) == 0);
1033 void coap_cancel_all_messages(coap_context_t *context,
1034 const coap_address_t *dst, const unsigned char *token,
1035 size_t token_length) {
1036 /* cancel all messages in sendqueue that are for dst
1037 * and use the specified token */
1038 coap_queue_t *p, *q;
1040 debug("cancel_all_messages\n");
1041 while (context->sendqueue
1042 && coap_address_equals(dst, &context->sendqueue->remote)
1043 && token_match(token, token_length,
1044 context->sendqueue->pdu->hdr->token,
1045 context->sendqueue->pdu->hdr->token_length)) {
1046 q = context->sendqueue;
1047 context->sendqueue = q->next;
1048 debug("**** removed transaction %d\n", ntohs(q->pdu->hdr->id));
1049 coap_delete_node(q);
1052 if (!context->sendqueue)
1055 p = context->sendqueue;
1058 /* when q is not NULL, it does not match (dst, token), so we can skip it */
1060 if (coap_address_equals(dst, &q->remote)
1061 && token_match(token, token_length, q->pdu->hdr->token,
1062 q->pdu->hdr->token_length)) {
1064 debug("**** removed transaction %d\n", ntohs(q->pdu->hdr->id));
1065 coap_delete_node(q);
1075 coap_find_transaction(coap_queue_t *queue, coap_tid_t id) {
1076 while (queue && queue->id != id)
1077 queue = queue->next;
1083 coap_new_error_response(coap_pdu_t *request, unsigned char code,
1084 coap_opt_filter_t opts) {
1085 coap_opt_iterator_t opt_iter;
1086 coap_pdu_t *response;
1087 size_t size = sizeof(coap_hdr_t) + request->hdr->token_length;
1090 unsigned short opt_type = 0; /* used for calculating delta-storage */
1092 #if COAP_ERROR_PHRASE_LENGTH > 0
1093 const char *phrase = coap_response_phrase(code);
1095 /* Need some more space for the error phrase and payload start marker */
1097 size += strlen(phrase) + 1;
1102 /* cannot send ACK if original request was not confirmable */
1103 type = request->hdr->type == COAP_MESSAGE_CON ?
1104 COAP_MESSAGE_ACK : COAP_MESSAGE_NON;
1106 /* Estimate how much space we need for options to copy from
1107 * request. We always need the Token, for 4.02 the unknown critical
1108 * options must be included as well. */
1109 coap_option_clrb(opts, COAP_OPTION_CONTENT_TYPE); /* we do not want this */
1111 coap_option_iterator_init(request, &opt_iter, opts);
1113 /* Add size of each unknown critical option. As known critical
1114 options as well as elective options are not copied, the delta
1117 while ((option = coap_option_next(&opt_iter))) {
1118 unsigned short delta = opt_iter.type - opt_type;
1119 /* calculate space required to encode (opt_iter.type - opt_type) */
1122 } else if (delta < 269) {
1128 /* add coap_opt_length(option) and the number of additional bytes
1129 * required to encode the option length */
1131 size += coap_opt_length(option);
1132 switch (*option & 0x0f) {
1144 opt_type = opt_iter.type;
1147 /* Now create the response and fill with options and payload data. */
1148 response = coap_pdu_init(type, code, request->hdr->id, size);
1151 if (!coap_add_token(response, request->hdr->token_length,
1152 request->hdr->token)) {
1153 debug("cannot add token to error response\n");
1154 coap_delete_pdu(response);
1158 /* copy all options */
1159 coap_option_iterator_init(request, &opt_iter, opts);
1160 while ((option = coap_option_next(&opt_iter)))
1161 coap_add_option(response, opt_iter.type, COAP_OPT_LENGTH(option),
1162 COAP_OPT_VALUE(option));
1164 #if COAP_ERROR_PHRASE_LENGTH > 0
1165 /* note that diagnostic messages do not need a Content-Format option. */
1167 coap_add_data(response, strlen(phrase), (unsigned char *) phrase);
1175 #define SZX_TO_BYTES(SZX) ((size_t)(1 << ((SZX) + 4)))
1177 #define WANT_WKC(Pdu,Key) \
1178 (((Pdu)->hdr->code == COAP_REQUEST_GET) && is_wkc(Key))
1180 /************************************************************************************************
1181 * Following code will be moved to newer handle_request in the future and kept for reference
1182 ************************************************************************************************/
1185 handle_request(coap_context_t *context, coap_queue_t *node) {
1186 coap_method_handler_t h = NULL;
1187 coap_pdu_t *response = NULL;
1188 coap_opt_filter_t opt_filter;
1189 coap_resource_t *resource;
1192 coap_option_filter_clear(opt_filter);
1194 /* try to find the resource from the request URI */
1195 coap_hash_request_uri(node->pdu, key);
1196 resource = coap_get_resource_from_key(context, key);
1199 /* The resource was not found. Check if the request URI happens to
1200 * be the well-known URI. In that case, we generate a default
1201 * response, otherwise, we return 4.04 */
1203 switch(node->pdu->hdr->code) {
1205 case COAP_REQUEST_GET:
1206 if (is_wkc(key)) { /* GET request for .well-known/core */
1207 info("create default response for %s\n", COAP_DEFAULT_URI_WELLKNOWN);
1208 response = wellknown_response(context, node->pdu);
1210 } else { /* GET request for any another resource, return 4.04 */
1212 debug("GET for unknown resource 0x%02x%02x%02x%02x, return 4.04\n",
1213 key[0], key[1], key[2], key[3]);
1215 coap_new_error_response(node->pdu, COAP_RESPONSE_CODE(404),
1220 default: /* any other request type */
1222 debug("unhandled request for unknown resource 0x%02x%02x%02x%02x\r\n",
1223 key[0], key[1], key[2], key[3]);
1224 if (!coap_is_mcast(&node->local))
1225 response = coap_new_error_response(node->pdu, COAP_RESPONSE_CODE(405),
1229 if (response && coap_send(context, &node->remote, response) == COAP_INVALID_TID) {
1230 warn("cannot send response for transaction %u\n", node->id);
1232 coap_delete_pdu(response);
1237 /* the resource was found, check if there is a registered handler */
1238 if ((size_t)node->pdu->hdr->code - 1 <
1239 sizeof(resource->handler)/sizeof(coap_method_handler_t))
1240 h = resource->handler[node->pdu->hdr->code - 1];
1243 debug("call custom handler for resource 0x%02x%02x%02x%02x\n",
1244 key[0], key[1], key[2], key[3]);
1245 response = coap_pdu_init(node->pdu->hdr->type == COAP_MESSAGE_CON
1248 0, node->pdu->hdr->id, COAP_MAX_PDU_SIZE);
1250 /* Implementation detail: coap_add_token() immediately returns 0
1251 if response == NULL */
1252 if (coap_add_token(response, node->pdu->hdr->token_length,
1253 node->pdu->hdr->token)) {
1254 str token = {node->pdu->hdr->token_length, node->pdu->hdr->token};
1256 h(context, resource, &node->remote,
1257 node->pdu, &token, response);
1258 if (response->hdr->type != COAP_MESSAGE_NON ||
1259 (response->hdr->code >= 64
1260 && !coap_is_mcast(&node->local))) {
1261 if (coap_send(context, &node->remote, response) == COAP_INVALID_TID) {
1262 debug("cannot send response for message %d\n", node->pdu->hdr->id);
1266 coap_delete_pdu(response);
1268 warn("cannot generate response\r\n");
1271 if (WANT_WKC(node->pdu, key)) {
1272 debug("create default response for %s\n", COAP_DEFAULT_URI_WELLKNOWN);
1273 response = wellknown_response(context, node->pdu);
1275 response = coap_new_error_response(node->pdu, COAP_RESPONSE_CODE(405),
1278 if (!response || (coap_send(context, &node->remote, response)
1279 == COAP_INVALID_TID)) {
1280 debug("cannot send response for transaction %u\n", node->id);
1282 coap_delete_pdu(response);
1286 static void handle_request(coap_context_t *context, coap_queue_t *rcvd) {
1287 /* Call application-specific reponse handler when available. If
1288 * not, we must acknowledge confirmable messages. */
1289 if (context->request_handler) {
1290 context->request_handler(context, rcvd);
1292 coap_send_flags_t flag = SEND_NOW;
1293 flag = (coap_send_flags_t)(flag | (rcvd->secure ? SEND_SECURE_PORT : 0));
1294 /* send ACK if rcvd is confirmable (i.e. a separate response) */
1295 coap_send_ack(context, &rcvd->remote, rcvd->pdu, flag);
1299 static void handle_response(coap_context_t *context, coap_queue_t *rcvd) {
1300 /* Call application-specific reponse handler when available. If
1301 * not, we must acknowledge confirmable messages. */
1302 if (context->response_handler) {
1303 context->response_handler(context, rcvd);
1305 coap_send_flags_t flag = SEND_NOW;
1306 flag = (coap_send_flags_t)(flag | (rcvd->secure ? SEND_SECURE_PORT : 0));
1307 /* send ACK if rcvd is confirmable (i.e. a separate response) */
1308 coap_send_ack(context, &rcvd->remote, rcvd->pdu, flag);
1312 static void handle_ack_rst(coap_context_t *context, uint8_t msgType, coap_queue_t *sent) {
1313 /* Call application-specific reponse handler when available. If
1314 * not, we must acknowledge confirmable messages. */
1315 if (context->ack_rst_handler) {
1316 context->ack_rst_handler(context, msgType, sent);
1322 handle_locally(coap_context_t *context __attribute__ ((unused)),
1323 coap_queue_t *node __attribute__ ((unused))) {
1324 #else /* not a GCC */
1325 handle_locally(coap_context_t *context, coap_queue_t *node) {
1327 /* this function can be used to check if node->pdu is really for us */
1331 void coap_dispatch(coap_context_t *context) {
1332 coap_queue_t *rcvd = NULL, *sent = NULL;
1333 coap_pdu_t *response;
1334 coap_opt_filter_t opt_filter;
1339 memset(opt_filter, 0, sizeof(coap_opt_filter_t));
1341 while (context->recvqueue) {
1342 rcvd = context->recvqueue;
1344 /* remove node from recvqueue */
1345 context->recvqueue = context->recvqueue->next;
1348 if (rcvd->pdu->hdr->version != COAP_DEFAULT_VERSION) {
1349 debug("dropped packet with unknown version %u\n",
1350 rcvd->pdu->hdr->version);
1354 switch (rcvd->pdu->hdr->type) {
1355 case COAP_MESSAGE_ACK:
1356 /* find transaction in sendqueue to stop retransmission */
1357 if(coap_remove_from_queue(&context->sendqueue, rcvd->id, &sent)){
1358 handle_ack_rst(context, COAP_MESSAGE_ACK, sent);
1361 //delete empty messages, this is ACK only message no piggybacked response
1362 if (rcvd->pdu->hdr->code == 0)
1366 case COAP_MESSAGE_NON: /* check for unknown critical options */
1367 if (coap_option_check_critical(context, rcvd->pdu, opt_filter)
1372 case COAP_MESSAGE_CON: /* check for unknown critical options */
1373 if (coap_option_check_critical(context, rcvd->pdu, opt_filter)
1375 /* FIXME: send response only if we have received a request. Otherwise,
1377 response = coap_new_error_response(rcvd->pdu,
1378 COAP_RESPONSE_CODE(402), opt_filter);
1380 warn("coap_dispatch: cannot create error reponse\n");
1382 coap_send_flags_t flag = SEND_NOW;
1383 flag = (coap_send_flags_t)(flag | rcvd->secure ? SEND_SECURE_PORT : 0);
1384 if (coap_send(context, &rcvd->remote, response, flag, NULL)
1385 == COAP_INVALID_TID) {
1386 warn("coap_dispatch: error sending reponse\n");
1388 coap_delete_pdu(response);
1394 case COAP_MESSAGE_RST:
1395 /* find transaction in sendqueue to stop retransmission */
1396 if(coap_remove_from_queue(&context->sendqueue, rcvd->id, &sent)){
1397 handle_ack_rst(context, COAP_MESSAGE_RST, sent);
1404 "TODO: Need to handle other message types in coap_dispatch");
1407 /************************************************************************************************
1408 * Following code will be replaced at different parts of the stack
1409 ************************************************************************************************/
1411 switch (rcvd->pdu->hdr->type) {
1412 case COAP_MESSAGE_ACK:
1413 /* find transaction in sendqueue to stop retransmission */
1414 coap_remove_from_queue(&context->sendqueue, rcvd->id, &sent);
1416 if (rcvd->pdu->hdr->code == 0)
1419 /* FIXME: if sent code was >= 64 the message might have been a
1420 * notification. Then, we must flag the observer to be alive
1421 * by setting obs->fail_cnt = 0. */
1422 if (sent && COAP_RESPONSE_CLASS(sent->pdu->hdr->code) == 2) {
1423 const str token = {sent->pdu->hdr->token_length,
1424 sent->pdu->hdr->token};
1425 coap_touch_observer(context, &sent->remote, &token);
1429 case COAP_MESSAGE_RST:
1430 /* We have sent something the receiver disliked, so we remove
1431 * not only the transaction but also the subscriptions we might
1434 coap_log(LOG_ALERT, "got RST for message %u\n",
1435 ntohs(rcvd->pdu->hdr->id));
1437 /* find transaction in sendqueue to stop retransmission */
1438 coap_remove_from_queue(&context->sendqueue, rcvd->id, &sent);
1441 coap_handle_rst(context, sent);
1444 case COAP_MESSAGE_NON: /* check for unknown critical options */
1445 if (coap_option_check_critical(context, rcvd->pdu, opt_filter)
1450 case COAP_MESSAGE_CON: /* check for unknown critical options */
1451 if (coap_option_check_critical(context, rcvd->pdu, opt_filter)
1454 /* FIXME: send response only if we have received a request. Otherwise,
1456 response = coap_new_error_response(rcvd->pdu,
1457 COAP_RESPONSE_CODE(402), opt_filter);
1460 warn("coap_dispatch: cannot create error reponse\n");
1462 if (coap_send(context, &rcvd->remote,
1463 response) == COAP_INVALID_TID) {
1464 warn("coap_dispatch: error sending reponse\n");
1466 coap_delete_pdu(response);
1475 /* Pass message to upper layer if a specific handler was
1476 * registered for a request that should be handled locally. */
1477 if (handle_locally(context, rcvd)) {
1478 if (COAP_MESSAGE_IS_REQUEST(rcvd->pdu->hdr)){
1479 handle_request(context, rcvd);
1481 else if (COAP_MESSAGE_IS_RESPONSE(rcvd->pdu->hdr)){
1482 handle_response(context, rcvd);
1485 coap_send_flags_t flag;
1486 flag = (coap_send_flags_t)(SEND_NOW |
1487 (rcvd->secure ? SEND_SECURE_PORT : 0));
1488 debug("dropped message with invalid code\n");
1489 coap_send_message_type(context, &rcvd->remote, rcvd->pdu,
1490 flag, COAP_MESSAGE_RST);
1494 // we should not retry responses.....
1496 coap_delete_node(sent);
1498 coap_delete_node(rcvd);
1503 int coap_can_exit(coap_context_t *context) {
1505 || (context->recvqueue == NULL && context->sendqueue == NULL);
1510 /*---------------------------------------------------------------------------*/
1511 /* CoAP message retransmission */
1512 /*---------------------------------------------------------------------------*/
1513 PROCESS_THREAD(coap_retransmit_process, ev, data)
1516 coap_queue_t *nextpdu;
1520 debug("Started retransmit process\r\n");
1524 if (ev == PROCESS_EVENT_TIMER) {
1525 if (etimer_expired(&the_coap_context.retransmit_timer)) {
1527 nextpdu = coap_peek_next(&the_coap_context);
1530 while (nextpdu && nextpdu->t <= now) {
1531 coap_retransmit(&the_coap_context, coap_pop_next(&the_coap_context));
1532 nextpdu = coap_peek_next(&the_coap_context);
1535 /* need to set timer to some value even if no nextpdu is available */
1536 etimer_set(&the_coap_context.retransmit_timer,
1537 nextpdu ? nextpdu->t - now : 0xFFFF);
1539 #ifndef WITHOUT_OBSERVE
1540 if (etimer_expired(&the_coap_context.notify_timer)) {
1541 coap_check_notify(&the_coap_context);
1542 etimer_reset(&the_coap_context.notify_timer);
1544 #endif /* WITHOUT_OBSERVE */
1550 /*---------------------------------------------------------------------------*/
1552 #endif /* WITH_CONTIKI */
1555 /* FIXME: retransmits that are not required any more due to incoming packages
1556 * do *not* get cleared at the moment, the wakeup when the transmission is due
1557 * is silently accepted. this is mainly due to the fact that the required
1558 * checks are similar in two places in the code (when receiving ACK and RST)
1559 * and that they cause more than one patch chunk, as it must be first checked
1560 * whether the sendqueue item to be dropped is the next one pending, and later
1561 * the restart function has to be called. nothing insurmountable, but it can
1562 * also be implemented when things have stabilized, and the performance
1563 * penality is minimal
1565 * also, this completely ignores COAP_RESOURCE_CHECK_TIME.
1568 static void coap_retransmittimer_execute(void *arg)
1570 coap_context_t *ctx = (coap_context_t*)arg;
1572 coap_tick_t elapsed;
1573 coap_queue_t *nextinqueue;
1575 ctx->timer_configured = 0;
1579 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 */
1581 nextinqueue = coap_peek_next(ctx);
1582 while (nextinqueue != NULL)
1584 if (nextinqueue->t > elapsed) {
1585 nextinqueue->t -= elapsed;
1588 elapsed -= nextinqueue->t;
1589 coap_retransmit(ctx, coap_pop_next(ctx));
1590 nextinqueue = coap_peek_next(ctx);
1594 ctx->sendqueue_basetime = now;
1596 coap_retransmittimer_restart(ctx);
1599 static void coap_retransmittimer_restart(coap_context_t *ctx)
1601 coap_tick_t now, elapsed, delay;
1603 if (ctx->timer_configured)
1605 printf("clearing\n");
1606 sys_untimeout(coap_retransmittimer_execute, (void*)ctx);
1607 ctx->timer_configured = 0;
1609 if (ctx->sendqueue != NULL)
1612 elapsed = now - ctx->sendqueue_basetime;
1613 if (ctx->sendqueue->t >= elapsed) {
1614 delay = ctx->sendqueue->t - elapsed;
1616 /* a strange situation, but not completely impossible.
1618 * this happens, for example, right after
1619 * coap_retransmittimer_execute, when a retransmission
1620 * was *just not yet* due, and the clock ticked before
1621 * our coap_ticks was called.
1623 * not trying to retransmit anything now, as it might
1624 * cause uncontrollable recursion; let's just try again
1625 * with the next main loop run.
1630 printf("scheduling for %d ticks\n", delay);
1631 sys_timeout(delay, coap_retransmittimer_execute, (void*)ctx);
1632 ctx->timer_configured = 1;