Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / resource / csdk / connectivity / lib / libcoap-4.1.1 / net.c
1 /* net.c -- CoAP network interface
2  *
3  * Copyright (C) 2010--2014 Olaf Bergmann <bergmann@tzi.org>
4  *
5  * This file is part of the CoAP library libcoap. Please see
6  * README for terms of use.
7  */
8
9 #include "config.h"
10
11 #include <ctype.h>
12 #include <stdio.h>
13 #ifdef HAVE_LIMITS_H
14 #include <limits.h>
15 #endif
16 #ifdef HAVE_UNISTD_H
17 #include <unistd.h>
18 #elif HAVE_SYS_UNISTD_H
19 #include <sys/unistd.h>
20 #endif
21 #ifdef HAVE_SYS_TYPES_H
22 #include <sys/types.h>
23 #endif
24 #ifdef HAVE_SYS_SOCKET_H
25 #include <sys/socket.h>
26 #endif
27 #ifdef HAVE_NETINET_IN_H
28 #include <netinet/in.h>
29 #endif
30 #ifdef HAVE_ARPA_INET_H
31 #include <arpa/inet.h>
32 #endif
33
34 #ifdef WITH_LWIP
35 #include <lwip/pbuf.h>
36 #include <lwip/udp.h>
37 #include <lwip/timers.h>
38 #endif
39
40 #include "debug.h"
41 #include "mem.h"
42 #include "str.h"
43 #include "async.h"
44 #include "resource.h"
45 #include "option.h"
46 #include "encode.h"
47 #include "block.h"
48 #include "net.h"
49
50 #if defined(WITH_POSIX) || defined(WITH_ARDUINO)
51
52 time_t clock_offset=0;
53
54 static inline coap_queue_t *
55 coap_malloc_node()
56 {
57     return (coap_queue_t *)coap_malloc(sizeof(coap_queue_t));
58 }
59
60 static inline void
61 coap_free_node(coap_queue_t *node)
62 {
63     coap_free(node);
64 }
65 #endif /* WITH_POSIX || WITH_ARDUINO */
66 #ifdef WITH_LWIP
67
68 #include <lwip/memp.h>
69
70 static void coap_retransmittimer_execute(void *arg);
71 static void coap_retransmittimer_restart(coap_context_t *ctx);
72
73 static inline coap_queue_t *
74 coap_malloc_node()
75 {
76     return (coap_queue_t *)memp_malloc(MEMP_COAP_NODE);
77 }
78
79 static inline void
80 coap_free_node(coap_queue_t *node)
81 {
82     memp_free(MEMP_COAP_NODE, node);
83 }
84
85 #endif /* WITH_LWIP */
86 #ifdef WITH_CONTIKI
87 # ifndef DEBUG
88 #  define DEBUG DEBUG_PRINT
89 # endif /* DEBUG */
90
91 #include "memb.h"
92 #include "net/uip-debug.h"
93
94 clock_time_t clock_offset;
95
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])
98
99 void coap_resources_init();
100 void coap_pdu_resources_init();
101
102 unsigned char initialized = 0;
103 coap_context_t the_coap_context;
104
105 MEMB(node_storage, coap_queue_t, COAP_PDU_MAXCNT);
106
107 PROCESS(coap_retransmit_process, "message retransmit process");
108
109 static inline coap_queue_t *
110 coap_malloc_node()
111 {
112     return (coap_queue_t *)memb_alloc(&node_storage);
113 }
114
115 static inline void
116 coap_free_node(coap_queue_t *node)
117 {
118     memb_free(&node_storage, node);
119 }
120 #endif /* WITH_CONTIKI */
121 #ifdef WITH_LWIP
122
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.)
129  */
130 static void received_package(void *arg, struct udp_pcb *upcb, struct pbuf *p, ip_addr_t *addr, u16_t port)
131 {
132     struct coap_context_t *context = (coap_context_t *)arg;
133
134     LWIP_ASSERT("pending_package was not cleared.", context->pending_package == NULL);
135
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;
139
140     coap_read(context);
141 }
142
143 #endif /* WITH_LWIP */
144
145 int print_wellknown(coap_context_t *, unsigned char *, size_t *, size_t, coap_opt_t *);
146
147 void coap_handle_failed_notify(coap_context_t *, const coap_address_t *, const str *);
148
149 unsigned int coap_adjust_basetime(coap_context_t *ctx, coap_tick_t now)
150 {
151     unsigned int result = 0;
152     coap_tick_diff_t delta = now - ctx->sendqueue_basetime;
153
154     if (ctx->sendqueue)
155     {
156         /* delta < 0 means that the new time stamp is before the old. */
157         if (delta <= 0)
158         {
159             ctx->sendqueue->t -= delta;
160         }
161         else
162         {
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. */
167
168             coap_queue_t *q = ctx->sendqueue;
169             coap_tick_t t = 0;
170             while (q && (t + q->t < (coap_tick_t) delta))
171             {
172                 t += q->t;
173                 q->t = 0;
174                 result++;
175                 q = q->next;
176             }
177
178             /* finally adjust the first element that has not expired */
179             if (q)
180             {
181                 q->t = (coap_tick_t) delta - t;
182             }
183         }
184     }
185
186     /* adjust basetime */
187     ctx->sendqueue_basetime += delta;
188
189     return result;
190 }
191
192 int coap_insert_node(coap_queue_t **queue, coap_queue_t *node)
193 {
194     coap_queue_t *p, *q;
195     if (!queue || !node)
196         return 0;
197
198     /* set queue head if empty */
199     if (!*queue)
200     {
201         *queue = node;
202         return 1;
203     }
204
205     /* replace queue head if PDU's time is less than head's time */
206     q = *queue;
207     if (node->t < q->t)
208     {
209         node->next = q;
210         *queue = node;
211         q->t -= node->t; /* make q->t relative to node->t */
212         return 1;
213     }
214
215     /* search for right place to insert */
216     do
217     {
218         node->t -= q->t; /* make node-> relative to q->t */
219         p = q;
220         q = q->next;
221     } while (q && q->t <= node->t);
222
223     /* insert new item */
224     if (q)
225     {
226         q->t -= node->t; /* make q->t relative to node->t */
227     }
228     node->next = q;
229     p->next = node;
230     return 1;
231 }
232
233 int coap_delete_node(coap_queue_t *node)
234 {
235     if (!node)
236         return 0;
237
238     coap_delete_pdu(node->pdu);
239     coap_free_node(node);
240
241     return 1;
242 }
243
244 void coap_delete_all(coap_queue_t *queue)
245 {
246     if (!queue)
247         return;
248
249     coap_delete_all(queue->next);
250     coap_delete_node(queue);
251 }
252
253 coap_queue_t *
254 coap_new_node()
255 {
256     coap_queue_t *node;
257     node = coap_malloc_node();
258
259     if (!node)
260     {
261 #ifndef NDEBUG
262         coap_log(LOG_WARNING, "coap_new_node: malloc\n");
263 #endif
264         return NULL;
265     }
266
267     memset(node, 0, sizeof *node);
268     return node;
269 }
270
271 coap_queue_t *
272 coap_peek_next(coap_context_t *context)
273 {
274     if (!context || !context->sendqueue)
275         return NULL;
276
277     return context->sendqueue;
278 }
279
280 coap_queue_t *
281 coap_pop_next(coap_context_t *context)
282 {
283     coap_queue_t *next;
284
285     if (!context || !context->sendqueue)
286         return NULL;
287
288     next = context->sendqueue;
289     context->sendqueue = context->sendqueue->next;
290     if (context->sendqueue)
291     {
292         context->sendqueue->t += next->t;
293     }
294     next->next = NULL;
295     return next;
296 }
297
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)
302 #else
303 /* Implements a singleton to store a hash key for the .wellknown/core
304  * resources. */
305 int
306 is_wkc(coap_key_t k)
307 {
308     static coap_key_t wkc;
309     static unsigned char _initialized = 0;
310     if (!_initialized)
311     {
312         _initialized = coap_hash_path((unsigned char *)COAP_DEFAULT_URI_WELLKNOWN,
313                 sizeof(COAP_DEFAULT_URI_WELLKNOWN) - 1, wkc);
314     }
315     return memcmp(k, wkc, sizeof(coap_key_t)) == 0;
316 }
317 #endif
318
319
320 #ifndef WITH_ARDUINO
321 coap_context_t *
322 coap_new_context(const coap_address_t *listen_addr)
323 {
324 #if defined(WITH_POSIX)
325     coap_context_t *c = coap_malloc( sizeof( coap_context_t ) );
326     int reuse = 1;
327 #endif /* WITH_POSIX */
328 #ifdef WITH_LWIP
329     coap_context_t *c = memp_malloc(MEMP_COAP_CONTEXT);
330 #endif /* WITH_LWIP */
331 #ifdef WITH_CONTIKI
332     coap_context_t *c;
333
334     if (initialized)
335     return NULL;
336 #endif /* WITH_CONTIKI */
337
338     if (!listen_addr)
339     {
340         coap_log(LOG_EMERG, "no listen address specified\n");
341         return NULL;
342     }
343
344     coap_clock_init();
345 #ifdef WITH_LWIP
346     prng_init(LWIP_RAND());
347 #else /* WITH_LWIP */
348     prng_init((unsigned long)listen_addr ^ clock_offset);
349 #endif /* WITH_LWIP */
350
351 #ifndef WITH_CONTIKI
352     if (!c)
353     {
354 #ifndef NDEBUG
355         coap_log(LOG_EMERG, "coap_init: malloc:\n");
356 #endif
357         return NULL;
358     }
359 #endif /* not WITH_CONTIKI */
360 #ifdef WITH_CONTIKI
361     coap_resources_init();
362     coap_pdu_resources_init();
363
364     c = &the_coap_context;
365     initialized = 1;
366 #endif /* WITH_CONTIKI */
367
368     memset(c, 0, sizeof(coap_context_t));
369
370     /* initialize message id */
371     prng((unsigned char *)&c->message_id, sizeof(unsigned short));
372
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);
385
386 #if defined(WITH_POSIX) || defined(WITH_ARDUINO)
387     c->sockfd = socket(listen_addr->addr.sa.sa_family, SOCK_DGRAM, 0);
388     if ( c->sockfd < 0 )
389     {
390 #ifndef NDEBUG
391         coap_log(LOG_EMERG, "coap_new_context: socket\n");
392 #endif /* WITH_POSIX */
393         goto onerror;
394     }
395
396     if ( setsockopt( c->sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse) ) < 0 )
397     {
398 #ifndef NDEBUG
399         coap_log(LOG_WARNING, "setsockopt SO_REUSEADDR\n");
400 #endif
401     }
402
403     if (bind(c->sockfd, &listen_addr->addr.sa, listen_addr->size) < 0)
404     {
405 #ifndef NDEBUG
406         coap_log(LOG_EMERG, "coap_new_context: bind\n");
407 #endif
408         goto onerror;
409     }
410
411     return c;
412
413     onerror:
414     if ( c->sockfd >= 0 )
415     close ( c->sockfd );
416     coap_free( c );
417     return NULL;
418
419 #endif /* WITH_POSIX || WITH_ARDUINO */
420 #ifdef WITH_CONTIKI
421     c->conn = udp_new(NULL, 0, NULL);
422     udp_bind(c->conn, listen_addr->port);
423
424     process_start(&coap_retransmit_process, (char *)c);
425
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);
433     return c;
434 #endif /* WITH_CONTIKI */
435 #ifdef WITH_LWIP
436     c->pcb = udp_new();
437     /* hard assert: this is not expected to fail dynamically */
438     LWIP_ASSERT("Failed to allocate PCB for CoAP", c->pcb != NULL);
439
440     udp_recv(c->pcb, received_package, (void*)c);
441     udp_bind(c->pcb, &listen_addr->addr, listen_addr->port);
442
443     c->timer_configured = 0;
444
445     return c;
446 #endif
447 }
448
449 void coap_free_context(coap_context_t *context)
450 {
451 #if defined(WITH_POSIX) || defined(WITH_LWIP)
452     coap_resource_t *res;
453 #ifndef COAP_RESOURCES_NOHASH
454     coap_resource_t *rtmp;
455 #endif
456 #endif /* WITH_POSIX || WITH_LWIP */
457     if (!context)
458         return;
459
460     coap_delete_all(context->recvqueue);
461     coap_delete_all(context->sendqueue);
462
463 #ifdef WITH_LWIP
464     context->sendqueue = NULL;
465     coap_retransmittimer_restart(context);
466 #endif
467
468 #if defined(WITH_POSIX) || defined(WITH_LWIP) || defined(WITH_ARDUINO)
469 #ifdef COAP_RESOURCES_NOHASH
470     LL_FOREACH(context->resources, res)
471     {
472 #else
473         HASH_ITER(hh, context->resources, res, rtmp)
474         {
475 #endif
476             coap_delete_resource(context, res->key);
477         }
478 #endif /* WITH_POSIX || WITH_LWIP */
479
480 #ifdef WITH_POSIX
481     /* coap_delete_list(context->subscriptions); */
482     close( context->sockfd );
483     coap_free( context );
484 #endif
485 #ifdef WITH_LWIP
486     udp_remove(context->pcb);
487     memp_free(MEMP_COAP_CONTEXT, context);
488 #endif
489 #ifdef WITH_CONTIKI
490     memset(&the_coap_context, 0, sizeof(coap_context_t));
491     initialized = 0;
492 #endif /* WITH_CONTIKI */
493 }
494 #endif //ifndef WITH_ARDUINO
495 int coap_option_check_critical(coap_context_t *ctx, coap_pdu_t *pdu, coap_opt_filter_t unknown)
496 {
497
498     coap_opt_iterator_t opt_iter;
499     int ok = 1;
500
501     coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL);
502
503     while (coap_option_next(&opt_iter))
504     {
505
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
510          * bad.
511          */
512         if (opt_iter.type & 0x01 && coap_option_getb(ctx->known_options, opt_iter.type) < 1)
513         {
514             debug("unknown critical option %d\n", opt_iter.type);
515
516             ok = 0;
517
518             /* When opt_iter.type is beyond our known option range,
519              * coap_option_setb() will return -1 and we are safe to leave
520              * this loop. */
521             if (coap_option_setb(unknown, opt_iter.type) == -1)
522                 break;
523         }
524     }
525
526     return ok;
527 }
528
529 void coap_transaction_id(const coap_address_t *peer, const coap_pdu_t *pdu, coap_tid_t *id)
530 {
531     coap_key_t h;
532
533     memset(h, 0, sizeof(coap_key_t));
534
535     /* Compare the complete address structure in case of IPv4. For IPv6,
536      * we need to look at the transport address only. */
537
538 #ifdef WITH_POSIX
539     switch (peer->addr.sa.sa_family)
540     {
541         case AF_INET:
542         coap_hash((const unsigned char *)&peer->addr.sa, peer->size, h);
543         break;
544         case AF_INET6:
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);
549         break;
550         default:
551         return;
552     }
553 #endif
554
555 #ifdef WITH_ARDUINO
556     coap_hash((const unsigned char *)peer->addr, peer->size, h);
557 #endif /* WITH_ARDUINO */
558
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 */
564
565     coap_hash((const unsigned char *)&pdu->hdr->id, sizeof(unsigned short), h);
566
567     *id = ((h[0] << 8) | h[1]) ^ ((h[2] << 8) | h[3]);
568 }
569
570 coap_tid_t coap_send_ack(coap_context_t *context, const coap_address_t *dst, coap_pdu_t *request)
571 {
572     coap_pdu_t *response;
573     coap_tid_t result = COAP_INVALID_TID;
574
575     if (request && request->hdr->type == COAP_MESSAGE_CON)
576     {
577         response = coap_pdu_init(COAP_MESSAGE_ACK, 0, request->hdr->id, sizeof(coap_pdu_t));
578         if (response)
579         {
580             result = coap_send(context, dst, response);
581             coap_delete_pdu(response);
582         }
583     }
584     return result;
585 }
586
587 #if defined(WITH_ARDUINO)
588 coap_tid_t
589 coap_send_impl(coap_context_t *context,
590                const coap_address_t *dst,
591                coap_pdu_t *pdu)
592 {
593     return 0;
594 }
595 #endif
596
597 #if defined(WITH_POSIX)
598 /* releases space allocated by PDU if free_pdu is set */
599 coap_tid_t
600 coap_send_impl(coap_context_t *context,
601         const coap_address_t *dst,
602         coap_pdu_t *pdu)
603 {
604     ssize_t bytes_written;
605     coap_tid_t id = COAP_INVALID_TID;
606
607     if ( !context || !dst || !pdu )
608     return id;
609
610     bytes_written = sendto( context->sockfd, pdu->hdr, pdu->length, 0,
611             &dst->addr.sa, dst->size);
612
613     if (bytes_written >= 0)
614     {
615         coap_transaction_id(dst, pdu, &id);
616     }
617     else
618     {
619         coap_log(LOG_CRIT, "coap_send: sendto\n");
620     }
621
622     return id;
623 }
624 #endif /* WITH_POSIX || WITH_ARDUINO */
625 #ifdef WITH_CONTIKI
626 /* releases space allocated by PDU if free_pdu is set */
627 coap_tid_t
628 coap_send_impl(coap_context_t *context,
629         const coap_address_t *dst,
630         coap_pdu_t *pdu)
631 {
632     coap_tid_t id = COAP_INVALID_TID;
633
634     if ( !context || !dst || !pdu )
635     return id;
636
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);
640
641     coap_transaction_id(dst, pdu, &id);
642
643     return id;
644 }
645 #endif /* WITH_CONTIKI */
646 #ifdef WITH_LWIP
647 coap_tid_t
648 coap_send_impl(coap_context_t *context,
649         const coap_address_t *dst,
650         coap_pdu_t *pdu)
651 {
652     coap_tid_t id = COAP_INVALID_TID;
653     struct pbuf *p;
654     uint8_t err;
655     char *data_backup;
656
657     if ( !context || !dst || !pdu )
658     {
659         return id;
660     }
661
662     data_backup = pdu->data;
663
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. */
667
668     p = pdu->pbuf;
669     LWIP_ASSERT("The PDU header is not where it is expected", pdu->hdr == p->payload + sizeof(coap_pdu_t));
670
671     err = pbuf_header(p, -sizeof(coap_pdu_t));
672     if (err)
673     {
674         debug("coap_send_impl: pbuf_header failed\n");
675         pbuf_free(p);
676         return id;
677     }
678
679     coap_transaction_id(dst, pdu, &id);
680
681     pbuf_realloc(p, pdu->length);
682
683     udp_sendto(context->pcb, p,
684             &dst->addr, dst->port);
685
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 */
687
688     err = pbuf_header(p, sizeof(coap_pdu_t));
689     LWIP_ASSERT("Cannot undo pbuf_header", err == 0);
690
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;
698     pdu->pbuf = p;
699
700     return id;
701 }
702 #endif /* WITH_LWIP */
703
704 coap_tid_t coap_send(coap_context_t *context, const coap_address_t *dst, coap_pdu_t *pdu)
705 {
706 #ifndef WITH_ARDUINO
707     return coap_send_impl(context, dst, pdu);
708 #endif
709 }
710
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)
713 {
714     coap_pdu_t *response;
715     coap_tid_t result = COAP_INVALID_TID;
716
717     assert(request);
718     assert(dst);
719
720     response = coap_new_error_response(request, code, opts);
721     if (response)
722     {
723         result = coap_send(context, dst, response);
724         coap_delete_pdu(response);
725     }
726
727     return result;
728 }
729
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)
732 {
733     coap_pdu_t *response;
734     coap_tid_t result = COAP_INVALID_TID;
735
736     if (request)
737     {
738         response = coap_pdu_init(type, 0, request->hdr->id, sizeof(coap_pdu_t));
739         if (response)
740         {
741             result = coap_send(context, dst, response);
742             coap_delete_pdu(response);
743         }
744     }
745     return result;
746 }
747
748 coap_tid_t coap_send_confirmed(coap_context_t *context, const coap_address_t *dst, coap_pdu_t *pdu)
749 {
750     coap_queue_t *node;
751     coap_tick_t now;
752     int r;
753
754     node = coap_new_node();
755     if (!node)
756     {
757         debug("coap_send_confirmed: insufficient memory\n");
758         return COAP_INVALID_TID;
759     }
760
761     node->id = coap_send_impl(context, dst, pdu);
762     if (COAP_INVALID_TID == node->id)
763     {
764         debug("coap_send_confirmed: error sending pdu\n");
765         coap_free_node(node);
766         return COAP_INVALID_TID;
767     }
768
769     prng((unsigned char *)&r, sizeof(r));
770
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);
774
775     memcpy(&node->remote, dst, sizeof(coap_address_t));
776     node->pdu = pdu;
777
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.
785      */coap_ticks(&now);
786     if (context->sendqueue == NULL)
787     {
788         node->t = node->timeout;
789         context->sendqueue_basetime = now;
790     }
791     else
792     {
793         /* make node->t relative to context->sendqueue_basetime */
794         node->t = (now - context->sendqueue_basetime) + node->timeout;
795     }
796
797     coap_insert_node(&context->sendqueue, node);
798
799 #ifdef WITH_LWIP
800     if (node == context->sendqueue) /* don't bother with timer stuff if there are earlier retransmits */
801     coap_retransmittimer_restart(context);
802 #endif
803
804 #ifdef WITH_CONTIKI
805     { /* (re-)initialize retransmission timer */
806         coap_queue_t *nextpdu;
807
808         nextpdu = coap_peek_next(context);
809         assert(nextpdu); /* we have just inserted a node */
810
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);
815     }
816 #endif /* WITH_CONTIKI */
817
818     return node->id;
819 }
820
821 coap_tid_t coap_retransmit(coap_context_t *context, coap_queue_t *node)
822 {
823     if (!context || !node)
824         return COAP_INVALID_TID;
825
826     /* re-initialize timeout when maximum number of retransmissions are not reached yet */
827     if (node->retransmit_cnt < COAP_DEFAULT_MAX_RETRANSMIT)
828     {
829         node->retransmit_cnt++;
830         node->t = node->timeout << node->retransmit_cnt;
831         coap_insert_node(&context->sendqueue, node);
832 #ifdef WITH_LWIP
833         if (node == context->sendqueue) /* don't bother with timer stuff if there are earlier retransmits */
834         coap_retransmittimer_restart(context);
835 #endif
836
837         debug(
838                 "** retransmission #%d of transaction %d\n", node->retransmit_cnt, ntohs(node->pdu->hdr->id));
839
840         node->id = coap_send_impl(context, &node->remote, node->pdu);
841         return node->id;
842     }
843
844     /* no more retransmissions, remove node from system */
845
846 #if !defined(WITH_CONTIKI) && !defined(WITH_ARDUINO)
847     debug("** removed transaction %d\n", ntohs(node->id));
848 #endif
849
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)
854     {
855         str token =
856         { 0, NULL };
857
858         token.length = node->pdu->hdr->token_length;
859         token.s = node->pdu->hdr->token;
860
861         coap_handle_failed_notify(context, &node->remote, &token);
862     }
863 #endif /* WITHOUT_OBSERVE */
864
865     /* And finally delete the node */
866     coap_delete_node(node);
867     return COAP_INVALID_TID;
868 }
869
870 /** 
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.
874  */
875 static inline int check_opt_size(coap_opt_t *opt, unsigned char *maxpos)
876 {
877     if (opt && opt < maxpos)
878     {
879         if (((*opt & 0x0f) < 0x0f) || (opt + 1 < maxpos))
880             return opt + COAP_OPT_SIZE(opt) < maxpos;
881     }
882     return 0;
883 }
884
885 #ifndef WITH_ARDUINO
886 int coap_read(coap_context_t *ctx)
887 {
888 #if defined(WITH_POSIX)
889     static char buf[COAP_MAX_PDU_SIZE];
890 #endif
891 #if defined(WITH_LWIP) || defined(WITH_CONTIKI)
892     char *buf;
893 #endif
894     coap_hdr_t *pdu;
895     ssize_t bytes_read = -1;
896     coap_address_t src, dst;
897     coap_queue_t *node;
898
899 #ifdef WITH_CONTIKI
900     buf = uip_appdata;
901 #endif /* WITH_CONTIKI */
902 #ifdef WITH_LWIP
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 */
907
908     pdu = (coap_hdr_t *) buf;
909
910     coap_address_init(&src);
911
912 #if defined(WITH_POSIX)
913     bytes_read = recvfrom(ctx->sockfd, buf, sizeof(buf), 0, &src.addr.sa, &src.size);
914
915 #endif /* WITH_POSIX || WITH_ARDUINO */
916 #ifdef WITH_CONTIKI
917     if(uip_newdata())
918     {
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;
923
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));
929     }
930 #endif /* WITH_CONTIKI */
931 #ifdef WITH_LWIP
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 */
937
938     if (bytes_read < 0)
939     {
940         warn("coap_read: recvfrom");
941         goto error_early;
942     }
943
944     if ((size_t) bytes_read < sizeof(coap_hdr_t))
945     {
946         debug("coap_read: discarded invalid frame\n");
947         goto error_early;
948     }
949
950     if (pdu->version != COAP_DEFAULT_VERSION)
951     {
952         debug("coap_read: unknown protocol version\n");
953         goto error_early;
954     }
955
956     node = coap_new_node();
957     if (!node)
958         goto error_early;
959
960 #ifdef WITH_LWIP
961     node->pdu = coap_pdu_from_pbuf(ctx->pending_package);
962     ctx->pending_package = NULL;
963 #else
964     node->pdu = coap_pdu_init(0, 0, 0, bytes_read);
965 #endif
966     if (!node->pdu)
967         goto error;
968
969     coap_ticks(&node->t);
970     memcpy(&node->local, &dst, sizeof(coap_address_t));
971     memcpy(&node->remote, &src, sizeof(coap_address_t));
972
973     if (!coap_pdu_parse((unsigned char *) buf, bytes_read, node->pdu))
974     {
975         warn("discard malformed PDU");
976         goto error;
977     }
978
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);
982
983 #ifndef NDEBUG
984     if (LOG_DEBUG <= coap_get_log_level())
985     {
986 #ifndef INET6_ADDRSTRLEN
987 #define INET6_ADDRSTRLEN 40
988 #endif
989         unsigned char addr[INET6_ADDRSTRLEN + 8];
990
991         if (coap_print_addr(&src, addr, INET6_ADDRSTRLEN + 8))
992             debug("** received %d bytes from %s:\n", (int)bytes_read, addr);
993
994         coap_show_pdu(node->pdu);
995     }
996 #endif
997
998     return 0;
999
1000     error:
1001     /* FIXME: send back RST? */
1002     coap_delete_node(node);
1003     return -1;
1004     error_early:
1005 #ifdef WITH_LWIP
1006     /* even if there was an error, clean up */
1007     pbuf_free(ctx->pending_package);
1008     ctx->pending_package = NULL;
1009 #endif
1010     return -1;
1011 }
1012 #endif //#ifndef WITH_ARDUINO
1013
1014 int coap_remove_from_queue(coap_queue_t **queue, coap_tid_t id, coap_queue_t **node)
1015 {
1016     coap_queue_t *p, *q;
1017
1018     if (!queue || !*queue)
1019         return 0;
1020
1021     /* replace queue head if PDU's time is less than head's time */
1022
1023     if (id == (*queue)->id)
1024     { /* found transaction */
1025         *node = *queue;
1026         *queue = (*queue)->next;
1027         if (*queue)
1028         { /* adjust relative time of new queue head */
1029             (*queue)->t += (*node)->t;
1030         }
1031         (*node)->next = NULL;
1032         /* coap_delete_node( q ); */
1033         debug("*** removed transaction %u\n", id);
1034         return 1;
1035     }
1036
1037     /* search transaction to remove (only first occurence will be removed) */
1038     q = *queue;
1039     do
1040     {
1041         p = q;
1042         q = q->next;
1043     } while (q && id != q->id);
1044
1045     if (q)
1046     { /* found transaction */
1047         p->next = q->next;
1048         if (p->next)
1049         { /* must update relative time of p->next */
1050             p->next->t += q->t;
1051         }
1052         q->next = NULL;
1053         *node = q;
1054         /* coap_delete_node( q ); */
1055         debug("*** removed transaction %u\n", id);
1056         return 1;
1057     }
1058
1059     return 0;
1060
1061 }
1062
1063 static inline int token_match(const unsigned char *a, size_t alen, const unsigned char *b,
1064         size_t blen)
1065 {
1066     return alen == blen && (alen == 0 || memcmp(a, b, alen) == 0);
1067 }
1068
1069 void coap_cancel_all_messages(coap_context_t *context, const coap_address_t *dst,
1070         const unsigned char *token, size_t token_length)
1071 {
1072     /* cancel all messages in sendqueue that are for dst
1073      * and use the specified token */
1074     coap_queue_t *p, *q;
1075
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))
1080     {
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);
1085     }
1086
1087     if (!context->sendqueue)
1088         return;
1089
1090     p = context->sendqueue;
1091     q = p->next;
1092
1093     /* when q is not NULL, it does not match (dst, token), so we can skip it */
1094     while (q)
1095     {
1096         if (coap_address_equals(dst, &q->remote)
1097                 && token_match(token, token_length, q->pdu->hdr->token, q->pdu->hdr->token_length))
1098         {
1099             p->next = q->next;
1100             debug("**** removed transaction %d\n", ntohs(q->pdu->hdr->id));
1101             coap_delete_node(q);
1102             q = p->next;
1103         }
1104         else
1105         {
1106             p = q;
1107             q = q->next;
1108         }
1109     }
1110 }
1111
1112 coap_queue_t *
1113 coap_find_transaction(coap_queue_t *queue, coap_tid_t id)
1114 {
1115     while (queue && queue->id != id)
1116         queue = queue->next;
1117
1118     return queue;
1119 }
1120
1121 coap_pdu_t *
1122 coap_new_error_response(coap_pdu_t *request, unsigned char code, coap_opt_filter_t opts)
1123 {
1124     coap_opt_iterator_t opt_iter;
1125     coap_pdu_t *response;
1126     size_t size = sizeof(coap_hdr_t) + request->hdr->token_length;
1127     int type;
1128     coap_opt_t *option;
1129     unsigned short opt_type = 0; /* used for calculating delta-storage */
1130
1131 #if COAP_ERROR_PHRASE_LENGTH > 0
1132     char *phrase = coap_response_phrase(code);
1133
1134     /* Need some more space for the error phrase and payload start marker */
1135     if (phrase)
1136         size += strlen(phrase) + 1;
1137 #endif
1138
1139     assert(request);
1140
1141     /* cannot send ACK if original request was not confirmable */
1142     type = request->hdr->type == COAP_MESSAGE_CON ? COAP_MESSAGE_ACK : COAP_MESSAGE_NON;
1143
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 */
1148
1149     coap_option_iterator_init(request, &opt_iter, opts);
1150
1151     /* Add size of each unknown critical option. As known critical
1152      options as well as elective options are not copied, the delta
1153      value might grow.
1154      */
1155     while ((option = coap_option_next(&opt_iter)))
1156     {
1157         unsigned short delta = opt_iter.type - opt_type;
1158         /* calculate space required to encode (opt_iter.type - opt_type) */
1159         if (delta < 13)
1160         {
1161             size++;
1162         }
1163         else if (delta < 269)
1164         {
1165             size += 2;
1166         }
1167         else
1168         {
1169             size += 3;
1170         }
1171
1172         /* add coap_opt_length(option) and the number of additional bytes
1173          * required to encode the option length */
1174
1175         size += coap_opt_length(option);
1176         switch (*option & 0x0f)
1177         {
1178             case 0x0e:
1179                 size++;
1180                 /* fall through */
1181             case 0x0d:
1182                 size++;
1183                 break;
1184             default:
1185                 ;
1186         }
1187
1188         opt_type = opt_iter.type;
1189     }
1190
1191     /* Now create the response and fill with options and payload data. */
1192     response = coap_pdu_init(type, code, request->hdr->id, size);
1193     if (response)
1194     {
1195         /* copy token */
1196         if (!coap_add_token(response, request->hdr->token_length, request->hdr->token))
1197         {
1198             debug("cannot add token to error response\n");
1199             coap_delete_pdu(response);
1200             return NULL;
1201         }
1202
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));
1208
1209 #if COAP_ERROR_PHRASE_LENGTH > 0
1210         /* note that diagnostic messages do not need a Content-Format option. */
1211         if (phrase)
1212             coap_add_data(response, strlen(phrase), (unsigned char *) phrase);
1213 #endif
1214     }
1215
1216     return response;
1217 }
1218
1219 /**
1220  * Quick hack to determine the size of the resource description for
1221  * .well-known/core.
1222  */
1223 static inline size_t get_wkc_len(coap_context_t *context, coap_opt_t *query_filter)
1224 {
1225     unsigned char buf[1];
1226     size_t len = 0;
1227
1228     if (print_wellknown(context, buf, &len, UINT_MAX, query_filter) & COAP_PRINT_STATUS_ERROR)
1229     {
1230         warn("cannot determine length of /.well-known/core\n");
1231         return 0;
1232     }
1233
1234     debug("get_wkc_len: print_wellknown() returned %zu\n", len);
1235
1236     return len;
1237 }
1238
1239 #define SZX_TO_BYTES(SZX) ((size_t)(1 << ((SZX) + 4)))
1240
1241 coap_pdu_t *
1242 wellknown_response(coap_context_t *context, coap_pdu_t *request)
1243 {
1244     coap_pdu_t *resp;
1245     coap_opt_iterator_t opt_iter;
1246     size_t len, wkc_len;
1247     unsigned char buf[2];
1248     int result = 0;
1249     int need_block2 = 0; /* set to 1 if Block2 option is required */
1250     coap_block_t block;
1251     coap_opt_t *query_filter;
1252     size_t offset = 0;
1253
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);
1258     if (!resp)
1259     {
1260         debug("wellknown_response: cannot create PDU\n");
1261         return NULL;
1262     }
1263
1264     if (!coap_add_token(resp, request->hdr->token_length, request->hdr->token))
1265     {
1266         debug("wellknown_response: cannot add token\n");
1267         goto error;
1268     }
1269
1270     query_filter = coap_check_option(request, COAP_OPTION_URI_QUERY, &opt_iter);
1271     wkc_len = get_wkc_len(context, query_filter);
1272
1273     /* check whether the request contains the Block2 option */
1274     if (coap_get_block(request, COAP_OPTION_BLOCK2, &block))
1275     {
1276         offset = block.num << (block.szx + 4);
1277         if (block.szx > 6)
1278         { /* invalid, MUST lead to 4.00 Bad Request */
1279             resp->hdr->code = COAP_RESPONSE_CODE(400);
1280             return resp;
1281         }
1282         else if (block.szx > COAP_MAX_BLOCK_SZX)
1283         {
1284             block.szx = COAP_MAX_BLOCK_SZX;
1285             block.num = offset >> (block.szx + 4);
1286         }
1287
1288         need_block2 = 1;
1289     }
1290
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
1294      * content. */
1295     if (resp->max_size <= (size_t) resp->length + 3)
1296     {
1297         debug("wellknown_response: insufficient storage space\n");
1298         goto error;
1299     }
1300
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);
1306
1307     /* check if Block2 option is required even if not requested */
1308     if (!need_block2 && (resp->max_size - (size_t) resp->length < wkc_len))
1309     {
1310         assert(resp->length <= resp->max_size);
1311         const size_t payloadlen = resp->max_size - resp->length;
1312         /* yes, need block-wise transfer */
1313         block.num = 0;
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))
1317         {
1318             if (block.szx == 0)
1319             {
1320                 debug("wellknown_response: message to small even for szx == 0\n");
1321                 goto error;
1322             }
1323             else
1324             {
1325                 block.szx--;
1326             }
1327         }
1328
1329         need_block2 = 1;
1330     }
1331
1332     /* write Block2 option if necessary */
1333     if (need_block2)
1334     {
1335         if (coap_write_block_opt(&block, COAP_OPTION_BLOCK2, resp, wkc_len) < 0)
1336         {
1337             debug("wellknown_response: cannot add Block2 option\n");
1338             goto error;
1339         }
1340     }
1341
1342     /* Manually set payload of response to let print_wellknown() write,
1343      * into our buffer without copying data. */
1344
1345     resp->data = (unsigned char *) resp->hdr + resp->length;
1346     *resp->data = COAP_PAYLOAD_START;
1347     resp->data++;
1348     resp->length++;
1349     len = need_block2 ? SZX_TO_BYTES(block.szx) : resp->max_size - resp->length;
1350
1351     result = print_wellknown(context, resp->data, &len, offset, query_filter);
1352     if ((result & COAP_PRINT_STATUS_ERROR) != 0)
1353     {
1354         debug("print_wellknown failed\n");
1355         goto error;
1356     }
1357
1358     resp->length += COAP_PRINT_OUTPUT_LENGTH(result);
1359     return resp;
1360
1361     error:
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;
1365     return resp;
1366 }
1367
1368 #define WANT_WKC(Pdu,Key)                   \
1369   (((Pdu)->hdr->code == COAP_REQUEST_GET) && is_wkc(Key))
1370
1371 void handle_request(coap_context_t *context, coap_queue_t *node, const char* responseData)
1372 {
1373
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;
1378     coap_key_t key;
1379
1380     coap_option_filter_clear(opt_filter);
1381
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);
1385
1386     if (!resource)
1387     {
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 */
1391
1392         switch (node->pdu->hdr->code)
1393         {
1394
1395             case COAP_REQUEST_GET:
1396                 if (is_wkc(key))
1397                 { /* GET request for .well-known/core */
1398                     info("create default response for %s\n", COAP_DEFAULT_URI_WELLKNOWN);
1399
1400                     response = wellknown_response(context, node->pdu);
1401
1402                 }
1403                 else
1404                 { /* GET request for any another resource, return 4.04 */
1405
1406                     debug(
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),
1409                             opt_filter);
1410                 }
1411                 break;
1412
1413             default: /* any other request type */
1414
1415                 debug(
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),
1419                             opt_filter);
1420         }
1421
1422         if (response && coap_send(context, &node->remote, response) == COAP_INVALID_TID)
1423         {
1424             warn("cannot send response for transaction %u\n", node->id);
1425         }
1426         coap_delete_pdu(response);
1427
1428         return;
1429     }
1430
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];
1435
1436     if (h)
1437     {
1438         debug(
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);
1443
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))
1447         {
1448             str token =
1449             { node->pdu->hdr->token_length, node->pdu->hdr->token };
1450
1451             h(context, resource, &node->remote, node->pdu, &token, response);
1452
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);
1459
1460             if (response->hdr->type != COAP_MESSAGE_NON
1461                     || (response->hdr->code >= 64 && !coap_is_mcast(&node->local)))
1462             {
1463
1464                 if (coap_send(context, &node->remote, response) == COAP_INVALID_TID)
1465                 {
1466                     debug("cannot send response for message %d\n", node->pdu->hdr->id);
1467                 }
1468             }
1469
1470             coap_delete_pdu(response);
1471         }
1472         else
1473         {
1474             warn("cannot generate response\r\n");
1475         }
1476     }
1477     else
1478     {
1479         if (WANT_WKC(node->pdu, key))
1480         {
1481             debug("create default response for %s\n", COAP_DEFAULT_URI_WELLKNOWN);
1482             response = wellknown_response(context, node->pdu);
1483         }
1484         else
1485             response = coap_new_error_response(node->pdu, COAP_RESPONSE_CODE(405), opt_filter);
1486
1487         if (!response || (coap_send(context, &node->remote, response) == COAP_INVALID_TID))
1488         {
1489             debug("cannot send response for transaction %u\n", node->id);
1490         }
1491         coap_delete_pdu(response);
1492     }
1493 }
1494
1495 static inline void handle_response(coap_context_t *context, coap_queue_t *sent, coap_queue_t *rcvd)
1496 {
1497
1498     /* Call application-specific reponse handler when available.  If
1499      * not, we must acknowledge confirmable messages. */
1500     if (context->response_handler)
1501     {
1502
1503         context->response_handler(context, &rcvd->remote, sent ? sent->pdu : NULL, rcvd->pdu,
1504                 rcvd->id);
1505     }
1506     else
1507     {
1508         /* send ACK if rcvd is confirmable (i.e. a separate response) */
1509         coap_send_ack(context, &rcvd->remote, rcvd->pdu);
1510     }
1511 }
1512
1513 static inline int
1514 #ifdef __GNUC__
1515 handle_locally(coap_context_t *context __attribute__ ((unused)),
1516         coap_queue_t *node __attribute__ ((unused)))
1517 {
1518 #else /* not a GCC */
1519     handle_locally(coap_context_t *context, coap_queue_t *node)
1520     {
1521 #endif /* GCC */
1522         /* this function can be used to check if node->pdu is really for us */
1523         return 1;
1524     }
1525
1526     /**
1527      * This function handles RST messages received for the message passed
1528      * in @p sent.
1529      */
1530     static void coap_handle_rst(coap_context_t *context, const coap_queue_t *sent)
1531     {
1532 #ifndef WITHOUT_OBSERVE
1533         coap_resource_t *r;
1534 #ifndef COAP_RESOURCES_NOHASH
1535         coap_resource_t *tmp;
1536 #endif
1537         str token =
1538         { 0, NULL };
1539
1540         /* remove observer for this resource, if any
1541          * get token from sent and try to find a matching resource. Uh!
1542          */
1543
1544         COAP_SET_STR(&token, sent->pdu->hdr->token_length, sent->pdu->hdr->token);
1545
1546 #ifndef WITH_CONTIKI
1547 #ifdef COAP_RESOURCES_NOHASH
1548         LL_FOREACH(context->resources, r)
1549         {
1550 #else
1551         HASH_ITER(hh, context->resources, r, tmp)
1552         {
1553 #endif
1554             coap_delete_observer(r, &sent->remote, &token);
1555             coap_cancel_all_messages(context, &sent->remote, token.s, token.length);
1556         }
1557 #else /* WITH_CONTIKI */
1558         r = (coap_resource_t *)resource_storage.mem;
1559         for (i = 0; i < resource_storage.num; ++i, ++r)
1560         {
1561             if (resource_storage.count[i])
1562             {
1563                 coap_delete_observer(r, &sent->remote, &token);
1564                 coap_cancel_all_messages(context, &sent->remote, token.s, token.length);
1565             }
1566         }
1567 #endif /* WITH_CONTIKI */
1568 #endif /* WITOUT_OBSERVE */
1569     }
1570
1571     void coap_dispatch(coap_context_t *context, const char* responseData)
1572     {
1573         coap_queue_t *rcvd = NULL, *sent = NULL;
1574         coap_pdu_t *response;
1575         coap_opt_filter_t opt_filter;
1576
1577         if (!context)
1578             return;
1579
1580         memset(opt_filter, 0, sizeof(coap_opt_filter_t));
1581
1582         while (context->recvqueue)
1583         {
1584             rcvd = context->recvqueue;
1585
1586             /* remove node from recvqueue */
1587             context->recvqueue = context->recvqueue->next;
1588             rcvd->next = NULL;
1589
1590             if (rcvd->pdu->hdr->version != COAP_DEFAULT_VERSION)
1591             {
1592                 debug("dropped packet with unknown version %u\n", rcvd->pdu->hdr->version);
1593                 goto cleanup;
1594             }
1595
1596             switch (rcvd->pdu->hdr->type)
1597             {
1598                 case COAP_MESSAGE_ACK:
1599                     /* find transaction in sendqueue to stop retransmission */
1600                     coap_remove_from_queue(&context->sendqueue, rcvd->id, &sent);
1601
1602                     if (rcvd->pdu->hdr->code == 0)
1603                         goto cleanup;
1604
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)
1609                     {
1610                         const str token =
1611                         { sent->pdu->hdr->token_length, sent->pdu->hdr->token };
1612                         coap_touch_observer(context, &sent->remote, &token);
1613                     }
1614                     break;
1615
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
1619                      * have. */
1620
1621                     coap_log(LOG_ALERT, "got RST for message %u\n", ntohs(rcvd->pdu->hdr->id));
1622
1623                     /* find transaction in sendqueue to stop retransmission */
1624                     coap_remove_from_queue(&context->sendqueue, rcvd->id, &sent);
1625
1626                     if (sent)
1627                         coap_handle_rst(context, sent);
1628                     goto cleanup;
1629
1630                 case COAP_MESSAGE_NON: /* check for unknown critical options */
1631                     if (coap_option_check_critical(context, rcvd->pdu, opt_filter) == 0)
1632                         goto cleanup;
1633                     break;
1634
1635                 case COAP_MESSAGE_CON: /* check for unknown critical options */
1636                     if (coap_option_check_critical(context, rcvd->pdu, opt_filter) == 0)
1637                     {
1638
1639                         /* FIXME: send response only if we have received a request. Otherwise,
1640                          * send RST. */
1641                         response = coap_new_error_response(rcvd->pdu, COAP_RESPONSE_CODE(402),
1642                                 opt_filter);
1643
1644                         if (!response)
1645                             warn("coap_dispatch: cannot create error reponse\n");
1646                         else
1647                         {
1648                             if (coap_send(context, &rcvd->remote, response) == COAP_INVALID_TID)
1649                             {
1650                                 warn("coap_dispatch: error sending reponse\n");
1651                             }
1652                             coap_delete_pdu(response);
1653                         }
1654
1655                         goto cleanup;
1656                     }
1657                     break;
1658             }
1659
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))
1663             {
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);
1668                 else
1669                 {
1670                     debug("dropped message with invalid code\n");
1671                     coap_send_message_type(context, &rcvd->remote, rcvd->pdu, COAP_MESSAGE_RST);
1672                 }
1673             }
1674
1675             cleanup: coap_delete_node(sent);
1676             coap_delete_node(rcvd);
1677         }
1678     }
1679
1680     int coap_can_exit(coap_context_t *context)
1681     {
1682         return !context || (context->recvqueue == NULL && context->sendqueue == NULL);
1683     }
1684
1685 #ifdef WITH_CONTIKI
1686
1687     /*---------------------------------------------------------------------------*/
1688     /* CoAP message retransmission */
1689     /*---------------------------------------------------------------------------*/
1690     PROCESS_THREAD(coap_retransmit_process, ev, data)
1691     {
1692         coap_tick_t now;
1693         coap_queue_t *nextpdu;
1694
1695         PROCESS_BEGIN();
1696
1697         debug("Started retransmit process\r\n");
1698
1699         while(1)
1700         {
1701             PROCESS_YIELD();
1702             if (ev == PROCESS_EVENT_TIMER)
1703             {
1704                 if (etimer_expired(&the_coap_context.retransmit_timer))
1705                 {
1706
1707                     nextpdu = coap_peek_next(&the_coap_context);
1708
1709                     coap_ticks(&now);
1710                     while (nextpdu && nextpdu->t <= now)
1711                     {
1712                         coap_retransmit(&the_coap_context, coap_pop_next(&the_coap_context));
1713                         nextpdu = coap_peek_next(&the_coap_context);
1714                     }
1715
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);
1719                 }
1720 #ifndef WITHOUT_OBSERVE
1721                 if (etimer_expired(&the_coap_context.notify_timer))
1722                 {
1723                     coap_check_notify(&the_coap_context);
1724                     etimer_reset(&the_coap_context.notify_timer);
1725                 }
1726 #endif /* WITHOUT_OBSERVE */
1727             }
1728         }
1729
1730         PROCESS_END();
1731     }
1732     /*---------------------------------------------------------------------------*/
1733
1734 #endif /* WITH_CONTIKI */
1735
1736 #ifdef WITH_LWIP
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
1746      *
1747      * also, this completely ignores COAP_RESOURCE_CHECK_TIME.
1748      * */
1749
1750     static void coap_retransmittimer_execute(void *arg)
1751     {
1752         coap_context_t *ctx = (coap_context_t*)arg;
1753         coap_tick_t now;
1754         coap_tick_t elapsed;
1755         coap_queue_t *nextinqueue;
1756
1757         ctx->timer_configured = 0;
1758
1759         coap_ticks(&now);
1760
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 */
1762
1763         nextinqueue = coap_peek_next(ctx);
1764         while (nextinqueue != NULL)
1765         {
1766             if (nextinqueue->t > elapsed)
1767             {
1768                 nextinqueue->t -= elapsed;
1769                 break;
1770             }
1771             else
1772             {
1773                 elapsed -= nextinqueue->t;
1774                 coap_retransmit(ctx, coap_pop_next(ctx));
1775                 nextinqueue = coap_peek_next(ctx);
1776             }
1777         }
1778
1779         ctx->sendqueue_basetime = now;
1780
1781         coap_retransmittimer_restart(ctx);
1782     }
1783
1784     static void coap_retransmittimer_restart(coap_context_t *ctx)
1785     {
1786         coap_tick_t now, elapsed, delay;
1787
1788         if (ctx->timer_configured)
1789         {
1790             printf("clearing\n");
1791             sys_untimeout(coap_retransmittimer_execute, (void*)ctx);
1792             ctx->timer_configured = 0;
1793         }
1794         if (ctx->sendqueue != NULL)
1795         {
1796             coap_ticks(&now);
1797             elapsed = now - ctx->sendqueue_basetime;
1798             if (ctx->sendqueue->t >= elapsed)
1799             {
1800                 delay = ctx->sendqueue->t - elapsed;
1801             }
1802             else
1803             {
1804                 /* a strange situation, but not completely impossible.
1805                  *
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.
1810                  *
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.
1814                  * */
1815                 delay = 0;
1816             }
1817
1818             printf("scheduling for %d ticks\n", delay);
1819             sys_timeout(delay, coap_retransmittimer_execute, (void*)ctx);
1820             ctx->timer_configured = 1;
1821         }
1822     }
1823 #endif