Memory Leak of capi context resolved
[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 #elif WITH_CONTIKI
328     coap_context_t *c =NULL;
329     if (initialized)
330     {
331         return NULL;
332     }
333 #elif WITH_LWIP
334     coap_context_t *c = memp_malloc(MEMP_COAP_CONTEXT);
335 #endif /* WITH_POSIX */
336     if (!listen_addr)
337     {
338         coap_log(LOG_EMERG, "no listen address specified\n");
339 #if defined(WITH_POSIX)
340         coap_free_context(c);
341 #elif WITH_LWIP
342         memp_free(c);
343 #endif
344         return NULL;
345     }
346
347     coap_clock_init();
348 #ifdef WITH_LWIP
349     prng_init(LWIP_RAND());
350 #else /* WITH_LWIP */
351     prng_init((unsigned long)listen_addr ^ clock_offset);
352 #endif /* WITH_LWIP */
353
354 #ifndef WITH_CONTIKI
355     if (!c)
356     {
357 #ifndef NDEBUG
358         coap_log(LOG_EMERG, "coap_init: malloc:\n");
359 #endif
360         return NULL;
361     }
362 #endif /* not WITH_CONTIKI */
363 #ifdef WITH_CONTIKI
364     coap_resources_init();
365     coap_pdu_resources_init();
366
367     c = &the_coap_context;
368     initialized = 1;
369 #endif /* WITH_CONTIKI */
370
371     memset(c, 0, sizeof(coap_context_t));
372
373     /* initialize message id */
374     prng((unsigned char *)&c->message_id, sizeof(unsigned short));
375
376     /* register the critical options that we know */
377     coap_register_option(c, COAP_OPTION_IF_MATCH);
378     coap_register_option(c, COAP_OPTION_URI_HOST);
379     coap_register_option(c, COAP_OPTION_IF_NONE_MATCH);
380     coap_register_option(c, COAP_OPTION_URI_PORT);
381     coap_register_option(c, COAP_OPTION_URI_PATH);
382     coap_register_option(c, COAP_OPTION_URI_QUERY);
383     coap_register_option(c, COAP_OPTION_ACCEPT);
384     coap_register_option(c, COAP_OPTION_PROXY_URI);
385     coap_register_option(c, COAP_OPTION_PROXY_SCHEME);
386     coap_register_option(c, COAP_OPTION_BLOCK2);
387     coap_register_option(c, COAP_OPTION_BLOCK1);
388
389 #if defined(WITH_POSIX) || defined(WITH_ARDUINO)
390     c->sockfd = socket(listen_addr->addr.sa.sa_family, SOCK_DGRAM, 0);
391     if ( c->sockfd < 0 )
392     {
393 #ifndef NDEBUG
394         coap_log(LOG_EMERG, "coap_new_context: socket\n");
395 #endif /* WITH_POSIX */
396         goto onerror;
397     }
398
399     if ( setsockopt( c->sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse) ) < 0 )
400     {
401 #ifndef NDEBUG
402         coap_log(LOG_WARNING, "setsockopt SO_REUSEADDR\n");
403 #endif
404     }
405
406     if (bind(c->sockfd, &listen_addr->addr.sa, listen_addr->size) < 0)
407     {
408 #ifndef NDEBUG
409         coap_log(LOG_EMERG, "coap_new_context: bind\n");
410 #endif
411         goto onerror;
412     }
413
414     return c;
415
416     onerror:
417     if ( c->sockfd >= 0 )
418     close ( c->sockfd );
419     coap_free( c );
420     return NULL;
421
422 #endif /* WITH_POSIX || WITH_ARDUINO */
423 #ifdef WITH_CONTIKI
424     c->conn = udp_new(NULL, 0, NULL);
425     udp_bind(c->conn, listen_addr->port);
426
427     process_start(&coap_retransmit_process, (char *)c);
428
429     PROCESS_CONTEXT_BEGIN(&coap_retransmit_process);
430 #ifndef WITHOUT_OBSERVE
431     etimer_set(&c->notify_timer, COAP_RESOURCE_CHECK_TIME * COAP_TICKS_PER_SECOND);
432 #endif /* WITHOUT_OBSERVE */
433     /* the retransmit timer must be initialized to some large value */
434     etimer_set(&the_coap_context.retransmit_timer, 0xFFFF);
435     PROCESS_CONTEXT_END(&coap_retransmit_process);
436     return c;
437 #endif /* WITH_CONTIKI */
438 #ifdef WITH_LWIP
439     c->pcb = udp_new();
440     /* hard assert: this is not expected to fail dynamically */
441     LWIP_ASSERT("Failed to allocate PCB for CoAP", c->pcb != NULL);
442
443     udp_recv(c->pcb, received_package, (void*)c);
444     udp_bind(c->pcb, &listen_addr->addr, listen_addr->port);
445
446     c->timer_configured = 0;
447
448     return c;
449 #endif
450 }
451
452 void coap_free_context(coap_context_t *context)
453 {
454 #if defined(WITH_POSIX) || defined(WITH_LWIP)
455     coap_resource_t *res;
456 #ifndef COAP_RESOURCES_NOHASH
457     coap_resource_t *rtmp;
458 #endif
459 #endif /* WITH_POSIX || WITH_LWIP */
460     if (!context)
461         return;
462
463     coap_delete_all(context->recvqueue);
464     coap_delete_all(context->sendqueue);
465
466 #ifdef WITH_LWIP
467     context->sendqueue = NULL;
468     coap_retransmittimer_restart(context);
469 #endif
470
471 #if defined(WITH_POSIX) || defined(WITH_LWIP) || defined(WITH_ARDUINO)
472 #ifdef COAP_RESOURCES_NOHASH
473     LL_FOREACH(context->resources, res)
474     {
475 #else
476         HASH_ITER(hh, context->resources, res, rtmp)
477         {
478 #endif
479             coap_delete_resource(context, res->key);
480         }
481 #endif /* WITH_POSIX || WITH_LWIP */
482
483 #ifdef WITH_POSIX
484     /* coap_delete_list(context->subscriptions); */
485     close( context->sockfd );
486     coap_free( context );
487 #endif
488 #ifdef WITH_LWIP
489     udp_remove(context->pcb);
490     memp_free(MEMP_COAP_CONTEXT, context);
491 #endif
492 #ifdef WITH_CONTIKI
493     memset(&the_coap_context, 0, sizeof(coap_context_t));
494     initialized = 0;
495 #endif /* WITH_CONTIKI */
496 }
497 #endif //ifndef WITH_ARDUINO
498 int coap_option_check_critical(coap_context_t *ctx, coap_pdu_t *pdu, coap_opt_filter_t unknown)
499 {
500
501     coap_opt_iterator_t opt_iter;
502     int ok = 1;
503
504     coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL, coap_udp);
505
506     while (coap_option_next(&opt_iter))
507     {
508
509         /* The following condition makes use of the fact that
510          * coap_option_getb() returns -1 if type exceeds the bit-vector
511          * filter. As the vector is supposed to be large enough to hold
512          * the largest known option, we know that everything beyond is
513          * bad.
514          */
515         if (opt_iter.type & 0x01 && coap_option_getb(ctx->known_options, opt_iter.type) < 1)
516         {
517             debug("unknown critical option %d\n", opt_iter.type);
518
519             ok = 0;
520
521             /* When opt_iter.type is beyond our known option range,
522              * coap_option_setb() will return -1 and we are safe to leave
523              * this loop. */
524             if (coap_option_setb(unknown, opt_iter.type) == -1)
525                 break;
526         }
527     }
528
529     return ok;
530 }
531
532 void coap_transaction_id(const coap_address_t *peer, const coap_pdu_t *pdu, coap_tid_t *id)
533 {
534     coap_key_t h;
535
536     memset(h, 0, sizeof(coap_key_t));
537
538     /* Compare the complete address structure in case of IPv4. For IPv6,
539      * we need to look at the transport address only. */
540
541 #ifdef WITH_POSIX
542     switch (peer->addr.sa.sa_family)
543     {
544         case AF_INET:
545         coap_hash((const unsigned char *)&peer->addr.sa, peer->size, h);
546         break;
547         case AF_INET6:
548         coap_hash((const unsigned char *)&peer->addr.sin6.sin6_port,
549                 sizeof(peer->addr.sin6.sin6_port), h);
550         coap_hash((const unsigned char *)&peer->addr.sin6.sin6_addr,
551                 sizeof(peer->addr.sin6.sin6_addr), h);
552         break;
553         default:
554         return;
555     }
556 #endif
557
558 #ifdef WITH_ARDUINO
559     coap_hash((const unsigned char *)peer->addr, peer->size, h);
560 #endif /* WITH_ARDUINO */
561
562 #if defined(WITH_LWIP) || defined(WITH_CONTIKI)
563     /* FIXME: with lwip, we can do better */
564     coap_hash((const unsigned char *)&peer->port, sizeof(peer->port), h);
565     coap_hash((const unsigned char *)&peer->addr, sizeof(peer->addr), h);
566 #endif /* WITH_LWIP || WITH_CONTIKI */
567
568     coap_hash((const unsigned char *)&pdu->hdr->coap_hdr_udp_t.id, sizeof(unsigned short), h);
569
570     *id = ((h[0] << 8) | h[1]) ^ ((h[2] << 8) | h[3]);
571 }
572
573 coap_tid_t coap_send_ack(coap_context_t *context, const coap_address_t *dst, coap_pdu_t *request)
574 {
575     coap_pdu_t *response;
576     coap_tid_t result = COAP_INVALID_TID;
577
578     if (request && request->hdr->coap_hdr_udp_t.type == COAP_MESSAGE_CON)
579     {
580         response = coap_pdu_init(COAP_MESSAGE_ACK, 0, request->hdr->coap_hdr_udp_t.id,
581                                  sizeof(coap_pdu_t), coap_udp);
582         if (response)
583         {
584             result = coap_send(context, dst, response);
585             coap_delete_pdu(response);
586         }
587     }
588     return result;
589 }
590
591 #if defined(WITH_ARDUINO)
592 coap_tid_t
593 coap_send_impl(coap_context_t *context,
594                const coap_address_t *dst,
595                coap_pdu_t *pdu)
596 {
597     return 0;
598 }
599 #endif
600
601 #if defined(WITH_POSIX)
602 /* releases space allocated by PDU if free_pdu is set */
603 coap_tid_t
604 coap_send_impl(coap_context_t *context,
605         const coap_address_t *dst,
606         coap_pdu_t *pdu)
607 {
608     ssize_t bytes_written;
609     coap_tid_t id = COAP_INVALID_TID;
610
611     if ( !context || !dst || !pdu )
612     return id;
613
614     bytes_written = sendto( context->sockfd, pdu->hdr, pdu->length, 0,
615             &dst->addr.sa, dst->size);
616
617     if (bytes_written >= 0)
618     {
619         coap_transaction_id(dst, pdu, &id);
620     }
621     else
622     {
623         coap_log(LOG_CRIT, "coap_send: sendto\n");
624     }
625
626     return id;
627 }
628 #endif /* WITH_POSIX || WITH_ARDUINO */
629 #ifdef WITH_CONTIKI
630 /* releases space allocated by PDU if free_pdu is set */
631 coap_tid_t
632 coap_send_impl(coap_context_t *context,
633         const coap_address_t *dst,
634         coap_pdu_t *pdu)
635 {
636     coap_tid_t id = COAP_INVALID_TID;
637
638     if ( !context || !dst || !pdu )
639     return id;
640
641     /* FIXME: is there a way to check if send was successful? */
642     uip_udp_packet_sendto(context->conn, pdu->hdr, pdu->length,
643             &dst->addr, dst->port);
644
645     coap_transaction_id(dst, pdu, &id);
646
647     return id;
648 }
649 #endif /* WITH_CONTIKI */
650 #ifdef WITH_LWIP
651 coap_tid_t
652 coap_send_impl(coap_context_t *context,
653         const coap_address_t *dst,
654         coap_pdu_t *pdu)
655 {
656     coap_tid_t id = COAP_INVALID_TID;
657     struct pbuf *p;
658     uint8_t err;
659     char *data_backup;
660
661     if ( !context || !dst || !pdu )
662     {
663         return id;
664     }
665
666     data_backup = pdu->data;
667
668     /* FIXME: we can't check this here with the existing infrastructure, but we
669      * should actually check that the pdu is not held by anyone but us. the
670      * respective pbuf is already exclusively owned by the pdu. */
671
672     p = pdu->pbuf;
673     LWIP_ASSERT("The PDU header is not where it is expected", pdu->hdr == p->payload + sizeof(coap_pdu_t));
674
675     err = pbuf_header(p, -sizeof(coap_pdu_t));
676     if (err)
677     {
678         debug("coap_send_impl: pbuf_header failed\n");
679         pbuf_free(p);
680         return id;
681     }
682
683     coap_transaction_id(dst, pdu, &id);
684
685     pbuf_realloc(p, pdu->length);
686
687     udp_sendto(context->pcb, p,
688             &dst->addr, dst->port);
689
690     pbuf_header(p, -(ptrdiff_t)((uint8_t*)pdu - (uint8_t*)p->payload) - sizeof(coap_pdu_t)); /* FIXME hack around udp_sendto not restoring; see http://lists.gnu.org/archive/html/lwip-users/2013-06/msg00008.html. for udp over ip over ethernet, this was -42; as we're doing ppp too, this has to be calculated generically */
691
692     err = pbuf_header(p, sizeof(coap_pdu_t));
693     LWIP_ASSERT("Cannot undo pbuf_header", err == 0);
694
695     /* restore destroyed pdu data */
696     LWIP_ASSERT("PDU not restored", p->payload == pdu);
697     pdu->max_size = p->tot_len - sizeof(coap_pdu_t); /* reduced after pbuf_realloc */
698     pdu->hdr = p->payload + sizeof(coap_pdu_t);
699     pdu->max_delta = 0; /* won't be used any more */
700     pdu->length = pdu->max_size;
701     pdu->data = data_backup;
702     pdu->pbuf = p;
703
704     return id;
705 }
706 #endif /* WITH_LWIP */
707
708 coap_tid_t coap_send(coap_context_t *context, const coap_address_t *dst, coap_pdu_t *pdu)
709 {
710 #ifndef WITH_ARDUINO
711     return coap_send_impl(context, dst, pdu);
712 #endif
713 }
714
715 coap_tid_t coap_send_error(coap_context_t *context, coap_pdu_t *request, const coap_address_t *dst,
716         unsigned char code, coap_opt_filter_t opts)
717 {
718     coap_pdu_t *response;
719     coap_tid_t result = COAP_INVALID_TID;
720
721     assert(request);
722     assert(dst);
723
724     response = coap_new_error_response(request, code, opts);
725     if (response)
726     {
727         result = coap_send(context, dst, response);
728         coap_delete_pdu(response);
729     }
730
731     return result;
732 }
733
734 coap_tid_t coap_send_message_type(coap_context_t *context, const coap_address_t *dst,
735         coap_pdu_t *request, unsigned char type)
736 {
737     coap_pdu_t *response;
738     coap_tid_t result = COAP_INVALID_TID;
739
740     if (request)
741     {
742         response = coap_pdu_init(type, 0, request->hdr->coap_hdr_udp_t.id,
743                                  sizeof(coap_pdu_t), coap_udp);
744         if (response)
745         {
746             result = coap_send(context, dst, response);
747             coap_delete_pdu(response);
748         }
749     }
750     return result;
751 }
752
753 coap_tid_t coap_send_confirmed(coap_context_t *context, const coap_address_t *dst, coap_pdu_t *pdu)
754 {
755     coap_queue_t *node;
756     coap_tick_t now;
757     int r;
758
759     node = coap_new_node();
760     if (!node)
761     {
762         debug("coap_send_confirmed: insufficient memory\n");
763         return COAP_INVALID_TID;
764     }
765
766     node->id = coap_send_impl(context, dst, pdu);
767     if (COAP_INVALID_TID == node->id)
768     {
769         debug("coap_send_confirmed: error sending pdu\n");
770         coap_free_node(node);
771         return COAP_INVALID_TID;
772     }
773
774     prng((unsigned char *)&r, sizeof(r));
775
776     /* add randomized RESPONSE_TIMEOUT to determine retransmission timeout */
777     node->timeout = COAP_DEFAULT_RESPONSE_TIMEOUT * COAP_TICKS_PER_SECOND
778             + (COAP_DEFAULT_RESPONSE_TIMEOUT >> 1) * ((COAP_TICKS_PER_SECOND * (r & 0xFF)) >> 8);
779
780     memcpy(&node->remote, dst, sizeof(coap_address_t));
781     node->pdu = pdu;
782
783     /* Set timer for pdu retransmission. If this is the first element in
784      * the retransmission queue, the base time is set to the current
785      * time and the retransmission time is node->timeout. If there is
786      * already an entry in the sendqueue, we must check if this node is
787      * to be retransmitted earlier. Therefore, node->timeout is first
788      * normalized to the base time and then inserted into the queue with
789      * an adjusted relative time.
790      */coap_ticks(&now);
791     if (context->sendqueue == NULL)
792     {
793         node->t = node->timeout;
794         context->sendqueue_basetime = now;
795     }
796     else
797     {
798         /* make node->t relative to context->sendqueue_basetime */
799         node->t = (now - context->sendqueue_basetime) + node->timeout;
800     }
801
802     coap_insert_node(&context->sendqueue, node);
803
804 #ifdef WITH_LWIP
805     if (node == context->sendqueue) /* don't bother with timer stuff if there are earlier retransmits */
806     coap_retransmittimer_restart(context);
807 #endif
808
809 #ifdef WITH_CONTIKI
810     { /* (re-)initialize retransmission timer */
811         coap_queue_t *nextpdu;
812
813         nextpdu = coap_peek_next(context);
814         assert(nextpdu); /* we have just inserted a node */
815
816         /* must set timer within the context of the retransmit process */
817         PROCESS_CONTEXT_BEGIN(&coap_retransmit_process);
818         etimer_set(&context->retransmit_timer, nextpdu->t);
819         PROCESS_CONTEXT_END(&coap_retransmit_process);
820     }
821 #endif /* WITH_CONTIKI */
822
823     return node->id;
824 }
825
826 coap_tid_t coap_retransmit(coap_context_t *context, coap_queue_t *node)
827 {
828     if (!context || !node)
829         return COAP_INVALID_TID;
830
831     /* re-initialize timeout when maximum number of retransmissions are not reached yet */
832     if (node->retransmit_cnt < COAP_DEFAULT_MAX_RETRANSMIT)
833     {
834         node->retransmit_cnt++;
835         node->t = node->timeout << node->retransmit_cnt;
836         coap_insert_node(&context->sendqueue, node);
837 #ifdef WITH_LWIP
838         /* don't bother with timer stuff if there are earlier retransmits */
839         if (node == context->sendqueue)
840         coap_retransmittimer_restart(context);
841 #endif
842
843         debug(
844                 "** retransmission #%d of transaction %d\n", node->retransmit_cnt,
845                 ntohs(node->pdu->hdr->coap_hdr_udp_t.id));
846
847         node->id = coap_send_impl(context, &node->remote, node->pdu);
848         return node->id;
849     }
850
851     /* no more retransmissions, remove node from system */
852
853 #if !defined(WITH_CONTIKI) && !defined(WITH_ARDUINO)
854     debug("** removed transaction %d\n", ntohs(node->id));
855 #endif
856
857 #ifndef WITHOUT_OBSERVE
858     /* Check if subscriptions exist that should be canceled after
859      COAP_MAX_NOTIFY_FAILURES */
860     if (node->pdu->hdr->coap_hdr_udp_t.code >= 64)
861     {
862         str token =
863         { 0, NULL };
864
865         token.length = node->pdu->hdr->coap_hdr_udp_t.token_length;
866         token.s = node->pdu->hdr->coap_hdr_udp_t.token;
867
868         coap_handle_failed_notify(context, &node->remote, &token);
869     }
870 #endif /* WITHOUT_OBSERVE */
871
872     /* And finally delete the node */
873     coap_delete_node(node);
874     return COAP_INVALID_TID;
875 }
876
877 /**
878  * Checks if @p opt fits into the message that ends with @p maxpos.
879  * This function returns @c 1 on success, or @c 0 if the option @p opt
880  * would exceed @p maxpos.
881  */
882 static inline int check_opt_size(coap_opt_t *opt, unsigned char *maxpos)
883 {
884     if (opt && opt < maxpos)
885     {
886         if (((*opt & 0x0f) < 0x0f) || (opt + 1 < maxpos))
887             return opt + COAP_OPT_SIZE(opt) < maxpos;
888     }
889     return 0;
890 }
891
892 #ifndef WITH_ARDUINO
893 int coap_read(coap_context_t *ctx)
894 {
895 #if defined(WITH_POSIX)
896     static char buf[COAP_MAX_PDU_SIZE];
897 #endif
898 #if defined(WITH_LWIP) || defined(WITH_CONTIKI)
899     char *buf;
900 #endif
901     coap_hdr_t *pdu;
902     ssize_t bytes_read = -1;
903     coap_address_t src, dst;
904     coap_queue_t *node;
905
906 #ifdef WITH_CONTIKI
907     buf = uip_appdata;
908 #endif /* WITH_CONTIKI */
909 #ifdef WITH_LWIP
910     LWIP_ASSERT("No package pending", ctx->pending_package != NULL);
911     LWIP_ASSERT("Can only deal with contiguous PBUFs to read the initial details", ctx->pending_package->tot_len == ctx->pending_package->len);
912     buf = ctx->pending_package->payload;
913 #endif /* WITH_LWIP */
914
915     pdu = (coap_hdr_t *) buf;
916
917     coap_address_init(&src);
918
919 #if defined(WITH_POSIX)
920     bytes_read = recvfrom(ctx->sockfd, buf, sizeof(buf), 0, &src.addr.sa, &src.size);
921
922 #endif /* WITH_POSIX || WITH_ARDUINO */
923 #ifdef WITH_CONTIKI
924     if(uip_newdata())
925     {
926         uip_ipaddr_copy(&src.addr, &UIP_IP_BUF->srcipaddr);
927         src.port = UIP_UDP_BUF->srcport;
928         uip_ipaddr_copy(&dst.addr, &UIP_IP_BUF->destipaddr);
929         dst.port = UIP_UDP_BUF->destport;
930
931         bytes_read = uip_datalen();
932         ((char *)uip_appdata)[bytes_read] = 0;
933         PRINTF("Server received %d bytes from [", (int)bytes_read);
934         PRINT6ADDR(&src.addr);
935         PRINTF("]:%d\n", uip_ntohs(src.port));
936     }
937 #endif /* WITH_CONTIKI */
938 #ifdef WITH_LWIP
939     /* FIXME: use lwip address operation functions */
940     src.addr.addr = ctx->pending_address.addr;
941     src.port = ctx->pending_port;
942     bytes_read = ctx->pending_package->tot_len;
943 #endif /* WITH_LWIP */
944
945     if (bytes_read < 0)
946     {
947         warn("coap_read: recvfrom");
948         goto error_early;
949     }
950
951     if ((size_t) bytes_read < sizeof(coap_hdr_t))
952     {
953         debug("coap_read: discarded invalid frame\n");
954         goto error_early;
955     }
956
957     if (pdu->coap_hdr_udp_t.version != COAP_DEFAULT_VERSION)
958     {
959         debug("coap_read: unknown protocol version\n");
960         goto error_early;
961     }
962
963     node = coap_new_node();
964     if (!node)
965         goto error_early;
966
967 #ifdef WITH_LWIP
968     node->pdu = coap_pdu_from_pbuf(ctx->pending_package);
969     ctx->pending_package = NULL;
970 #else
971     node->pdu = coap_pdu_init(0, 0, 0, bytes_read, coap_udp);
972 #endif
973     if (!node->pdu)
974         goto error;
975
976     coap_ticks(&node->t);
977     memcpy(&node->local, &dst, sizeof(coap_address_t));
978     memcpy(&node->remote, &src, sizeof(coap_address_t));
979
980     if (!coap_pdu_parse((unsigned char *) buf, bytes_read, node->pdu, coap_udp))
981     {
982         warn("discard malformed PDU");
983         goto error;
984     }
985
986     /* and add new node to receive queue */
987     coap_transaction_id(&node->remote, node->pdu, &node->id);
988     coap_insert_node(&ctx->recvqueue, node);
989
990 #ifndef NDEBUG
991     if (LOG_DEBUG <= coap_get_log_level())
992     {
993 #ifndef INET6_ADDRSTRLEN
994 #define INET6_ADDRSTRLEN 40
995 #endif
996         unsigned char addr[INET6_ADDRSTRLEN + 8];
997
998         if (coap_print_addr(&src, addr, INET6_ADDRSTRLEN + 8))
999             debug("** received %d bytes from %s:\n", (int)bytes_read, addr);
1000
1001         coap_show_pdu(node->pdu);
1002     }
1003 #endif
1004
1005     return 0;
1006
1007     error:
1008     /* FIXME: send back RST? */
1009     coap_delete_node(node);
1010     return -1;
1011     error_early:
1012 #ifdef WITH_LWIP
1013     /* even if there was an error, clean up */
1014     pbuf_free(ctx->pending_package);
1015     ctx->pending_package = NULL;
1016 #endif
1017     return -1;
1018 }
1019 #endif //#ifndef WITH_ARDUINO
1020
1021 int coap_remove_from_queue(coap_queue_t **queue, coap_tid_t id, coap_queue_t **node)
1022 {
1023     coap_queue_t *p, *q;
1024
1025     if (!queue || !*queue)
1026         return 0;
1027
1028     /* replace queue head if PDU's time is less than head's time */
1029
1030     if (id == (*queue)->id)
1031     { /* found transaction */
1032         *node = *queue;
1033         *queue = (*queue)->next;
1034         if (*queue)
1035         { /* adjust relative time of new queue head */
1036             (*queue)->t += (*node)->t;
1037         }
1038         (*node)->next = NULL;
1039         /* coap_delete_node( q ); */
1040         debug("*** removed transaction %u\n", id);
1041         return 1;
1042     }
1043
1044     /* search transaction to remove (only first occurence will be removed) */
1045     q = *queue;
1046     do
1047     {
1048         p = q;
1049         q = q->next;
1050     } while (q && id != q->id);
1051
1052     if (q)
1053     { /* found transaction */
1054         p->next = q->next;
1055         if (p->next)
1056         { /* must update relative time of p->next */
1057             p->next->t += q->t;
1058         }
1059         q->next = NULL;
1060         *node = q;
1061         /* coap_delete_node( q ); */
1062         debug("*** removed transaction %u\n", id);
1063         return 1;
1064     }
1065
1066     return 0;
1067
1068 }
1069
1070 static inline int token_match(const unsigned char *a, size_t alen, const unsigned char *b,
1071         size_t blen)
1072 {
1073     return alen == blen && (alen == 0 || memcmp(a, b, alen) == 0);
1074 }
1075
1076 void coap_cancel_all_messages(coap_context_t *context, const coap_address_t *dst,
1077         const unsigned char *token, size_t token_length)
1078 {
1079     /* cancel all messages in sendqueue that are for dst
1080      * and use the specified token */
1081     coap_queue_t *p, *q;
1082
1083     debug("cancel_all_messages\n");
1084     while (context->sendqueue && coap_address_equals(dst, &context->sendqueue->remote)
1085             && token_match(token, token_length, context->sendqueue->pdu->hdr->coap_hdr_udp_t.token,
1086                     context->sendqueue->pdu->hdr->coap_hdr_udp_t.token_length))
1087     {
1088         q = context->sendqueue;
1089         context->sendqueue = q->next;
1090         debug("**** removed transaction %d\n", ntohs(q->pdu->hdr->coap_hdr_udp_t.id));
1091         coap_delete_node(q);
1092     }
1093
1094     if (!context->sendqueue)
1095         return;
1096
1097     p = context->sendqueue;
1098     q = p->next;
1099
1100     /* when q is not NULL, it does not match (dst, token), so we can skip it */
1101     while (q)
1102     {
1103         if (coap_address_equals(dst, &q->remote)
1104                 && token_match(token, token_length, q->pdu->hdr->coap_hdr_udp_t.token,
1105                                q->pdu->hdr->coap_hdr_udp_t.token_length))
1106         {
1107             p->next = q->next;
1108             debug("**** removed transaction %d\n", ntohs(q->pdu->hdr->coap_hdr_udp_t.id));
1109             coap_delete_node(q);
1110             q = p->next;
1111         }
1112         else
1113         {
1114             p = q;
1115             q = q->next;
1116         }
1117     }
1118 }
1119
1120 coap_queue_t *
1121 coap_find_transaction(coap_queue_t *queue, coap_tid_t id)
1122 {
1123     while (queue && queue->id != id)
1124         queue = queue->next;
1125
1126     return queue;
1127 }
1128
1129 coap_pdu_t *
1130 coap_new_error_response(coap_pdu_t *request, unsigned char code, coap_opt_filter_t opts)
1131 {
1132     coap_opt_iterator_t opt_iter;
1133     coap_pdu_t *response;
1134     size_t size = sizeof(coap_hdr_t) + request->hdr->coap_hdr_udp_t.token_length;
1135     int type;
1136     coap_opt_t *option;
1137     unsigned short opt_type = 0; /* used for calculating delta-storage */
1138
1139 #if COAP_ERROR_PHRASE_LENGTH > 0
1140     char *phrase = coap_response_phrase(code);
1141
1142     /* Need some more space for the error phrase and payload start marker */
1143     if (phrase)
1144         size += strlen(phrase) + 1;
1145 #endif
1146
1147     assert(request);
1148
1149     /* cannot send ACK if original request was not confirmable */
1150     type = request->hdr->coap_hdr_udp_t.type == COAP_MESSAGE_CON ? COAP_MESSAGE_ACK : COAP_MESSAGE_NON;
1151
1152     /* Estimate how much space we need for options to copy from
1153      * request. We always need the Token, for 4.02 the unknown critical
1154      * options must be included as well. */
1155     coap_option_clrb(opts, COAP_OPTION_CONTENT_TYPE); /* we do not want this */
1156
1157     coap_option_iterator_init(request, &opt_iter, opts, coap_udp);
1158
1159     /* Add size of each unknown critical option. As known critical
1160      options as well as elective options are not copied, the delta
1161      value might grow.
1162      */
1163     while ((option = coap_option_next(&opt_iter)))
1164     {
1165         unsigned short delta = opt_iter.type - opt_type;
1166         /* calculate space required to encode (opt_iter.type - opt_type) */
1167         if (delta < 13)
1168         {
1169             size++;
1170         }
1171         else if (delta < 269)
1172         {
1173             size += 2;
1174         }
1175         else
1176         {
1177             size += 3;
1178         }
1179
1180         /* add coap_opt_length(option) and the number of additional bytes
1181          * required to encode the option length */
1182
1183         size += coap_opt_length(option);
1184         switch (*option & 0x0f)
1185         {
1186             case 0x0e:
1187                 size++;
1188                 /* fall through */
1189             case 0x0d:
1190                 size++;
1191                 break;
1192             default:
1193                 ;
1194         }
1195
1196         opt_type = opt_iter.type;
1197     }
1198
1199     /* Now create the response and fill with options and payload data. */
1200     response = coap_pdu_init(type, code, request->hdr->coap_hdr_udp_t.id, size, coap_udp);
1201     if (response)
1202     {
1203         /* copy token */
1204         if (!coap_add_token(response, request->hdr->coap_hdr_udp_t.token_length,
1205                             request->hdr->coap_hdr_udp_t.token, coap_udp))
1206         {
1207             debug("cannot add token to error response\n");
1208             coap_delete_pdu(response);
1209             return NULL;
1210         }
1211
1212         /* copy all options */
1213         coap_option_iterator_init(request, &opt_iter, opts, coap_udp);
1214         while ((option = coap_option_next(&opt_iter)))
1215             coap_add_option(response, opt_iter.type, COAP_OPT_LENGTH(option),
1216                     COAP_OPT_VALUE(option), coap_udp);
1217
1218 #if COAP_ERROR_PHRASE_LENGTH > 0
1219         /* note that diagnostic messages do not need a Content-Format option. */
1220         if (phrase)
1221             coap_add_data(response, strlen(phrase), (unsigned char *) phrase);
1222 #endif
1223     }
1224
1225     return response;
1226 }
1227
1228 /**
1229  * Quick hack to determine the size of the resource description for
1230  * .well-known/core.
1231  */
1232 static inline size_t get_wkc_len(coap_context_t *context, coap_opt_t *query_filter)
1233 {
1234     unsigned char buf[1];
1235     size_t len = 0;
1236
1237     if (print_wellknown(context, buf, &len, UINT_MAX, query_filter) & COAP_PRINT_STATUS_ERROR)
1238     {
1239         warn("cannot determine length of /.well-known/core\n");
1240         return 0;
1241     }
1242
1243     debug("get_wkc_len: print_wellknown() returned %zu\n", len);
1244
1245     return len;
1246 }
1247
1248 #define SZX_TO_BYTES(SZX) ((size_t)(1 << ((SZX) + 4)))
1249
1250 coap_pdu_t *
1251 wellknown_response(coap_context_t *context, coap_pdu_t *request)
1252 {
1253     coap_pdu_t *resp;
1254     coap_opt_iterator_t opt_iter;
1255     size_t len, wkc_len;
1256     unsigned char buf[2];
1257     int result = 0;
1258     int need_block2 = 0; /* set to 1 if Block2 option is required */
1259     coap_block_t block;
1260     coap_opt_t *query_filter;
1261     size_t offset = 0;
1262
1263     resp = coap_pdu_init(
1264             request->hdr->coap_hdr_udp_t.type == COAP_MESSAGE_CON ? COAP_MESSAGE_ACK : COAP_MESSAGE_NON,
1265             COAP_RESPONSE_CODE(205),
1266             request->hdr->coap_hdr_udp_t.id, COAP_MAX_PDU_SIZE, coap_udp);
1267     if (!resp)
1268     {
1269         debug("wellknown_response: cannot create PDU\n");
1270         return NULL;
1271     }
1272
1273     if (!coap_add_token(resp, request->hdr->coap_hdr_udp_t.token_length,
1274                         request->hdr->coap_hdr_udp_t.token, coap_udp))
1275     {
1276         debug("wellknown_response: cannot add token\n");
1277         goto error;
1278     }
1279
1280     query_filter = coap_check_option(request, COAP_OPTION_URI_QUERY, &opt_iter);
1281     wkc_len = get_wkc_len(context, query_filter);
1282
1283     /* check whether the request contains the Block2 option */
1284     if (coap_get_block(request, COAP_OPTION_BLOCK2, &block))
1285     {
1286         offset = block.num << (block.szx + 4);
1287         if (block.szx > 6)
1288         { /* invalid, MUST lead to 4.00 Bad Request */
1289             resp->hdr->coap_hdr_udp_t.code = COAP_RESPONSE_CODE(400);
1290             return resp;
1291         }
1292         else if (block.szx > COAP_MAX_BLOCK_SZX)
1293         {
1294             block.szx = COAP_MAX_BLOCK_SZX;
1295             block.num = offset >> (block.szx + 4);
1296         }
1297
1298         need_block2 = 1;
1299     }
1300
1301     /* Check if there is sufficient space to add Content-Format option
1302      * and data. We do this before adding the Content-Format option to
1303      * avoid sending error responses with that option but no actual
1304      * content. */
1305     if (resp->max_size <= (size_t) resp->length + 3)
1306     {
1307         debug("wellknown_response: insufficient storage space\n");
1308         goto error;
1309     }
1310
1311     /* Add Content-Format. As we have checked for available storage,
1312      * nothing should go wrong here. */
1313     assert(coap_encode_var_bytes(buf, COAP_MEDIATYPE_APPLICATION_LINK_FORMAT) == 1);
1314     coap_add_option(resp, COAP_OPTION_CONTENT_FORMAT,
1315             coap_encode_var_bytes(buf, COAP_MEDIATYPE_APPLICATION_LINK_FORMAT), buf, coap_udp);
1316
1317     /* check if Block2 option is required even if not requested */
1318     if (!need_block2 && (resp->max_size - (size_t) resp->length < wkc_len))
1319     {
1320         assert(resp->length <= resp->max_size);
1321         const size_t payloadlen = resp->max_size - resp->length;
1322         /* yes, need block-wise transfer */
1323         block.num = 0;
1324         block.m = 0; /* the M bit is set by coap_write_block_opt() */
1325         block.szx = COAP_MAX_BLOCK_SZX;
1326         while (payloadlen < SZX_TO_BYTES(block.szx))
1327         {
1328             if (block.szx == 0)
1329             {
1330                 debug("wellknown_response: message to small even for szx == 0\n");
1331                 goto error;
1332             }
1333             else
1334             {
1335                 block.szx--;
1336             }
1337         }
1338
1339         need_block2 = 1;
1340     }
1341
1342     /* write Block2 option if necessary */
1343     if (need_block2)
1344     {
1345         if (coap_write_block_opt(&block, COAP_OPTION_BLOCK2, resp, wkc_len) < 0)
1346         {
1347             debug("wellknown_response: cannot add Block2 option\n");
1348             goto error;
1349         }
1350     }
1351
1352     /* Manually set payload of response to let print_wellknown() write,
1353      * into our buffer without copying data. */
1354
1355     resp->data = (unsigned char *) resp->hdr + resp->length;
1356     *resp->data = COAP_PAYLOAD_START;
1357     resp->data++;
1358     resp->length++;
1359     len = need_block2 ? SZX_TO_BYTES(block.szx) : resp->max_size - resp->length;
1360
1361     result = print_wellknown(context, resp->data, &len, offset, query_filter);
1362     if ((result & COAP_PRINT_STATUS_ERROR) != 0)
1363     {
1364         debug("print_wellknown failed\n");
1365         goto error;
1366     }
1367
1368     resp->length += COAP_PRINT_OUTPUT_LENGTH(result);
1369     return resp;
1370
1371     error:
1372     /* set error code 5.03 and remove all options and data from response */
1373     resp->hdr->coap_hdr_udp_t.code = COAP_RESPONSE_CODE(503);
1374     resp->length = sizeof(coap_hdr_t) + resp->hdr->coap_hdr_udp_t.token_length;
1375     return resp;
1376 }
1377
1378 #define WANT_WKC(Pdu,Key)                   \
1379   (((Pdu)->hdr->coap_hdr_udp_t.code == COAP_REQUEST_GET) && is_wkc(Key))
1380
1381 void handle_request(coap_context_t *context, coap_queue_t *node, const char* responseData)
1382 {
1383
1384     coap_method_handler_t h = NULL;
1385     coap_pdu_t *response = NULL;
1386     coap_opt_filter_t opt_filter;
1387     coap_resource_t *resource;
1388     coap_key_t key;
1389
1390     coap_option_filter_clear(opt_filter);
1391
1392     /* try to find the resource from the request URI */
1393     coap_hash_request_uri(node->pdu, key);
1394     resource = coap_get_resource_from_key(context, key);
1395
1396     if (!resource)
1397     {
1398         /* The resource was not found. Check if the request URI happens to
1399          * be the well-known URI. In that case, we generate a default
1400          * response, otherwise, we return 4.04 */
1401
1402         switch (node->pdu->hdr->coap_hdr_udp_t.code)
1403         {
1404
1405             case COAP_REQUEST_GET:
1406                 if (is_wkc(key))
1407                 { /* GET request for .well-known/core */
1408                     info("create default response for %s\n", COAP_DEFAULT_URI_WELLKNOWN);
1409
1410                     response = wellknown_response(context, node->pdu);
1411
1412                 }
1413                 else
1414                 { /* GET request for any another resource, return 4.04 */
1415
1416                     debug(
1417                             "GET for unknown resource 0x%02x%02x%02x%02x, return 4.04\n", key[0], key[1], key[2], key[3]);
1418                     response = coap_new_error_response(node->pdu, COAP_RESPONSE_CODE(404),
1419                             opt_filter);
1420                 }
1421                 break;
1422
1423             default: /* any other request type */
1424
1425                 debug(
1426                         "unhandled request for unknown resource 0x%02x%02x%02x%02x\r\n", key[0], key[1], key[2], key[3]);
1427                 if (!coap_is_mcast(&node->local))
1428                     response = coap_new_error_response(node->pdu, COAP_RESPONSE_CODE(405),
1429                             opt_filter);
1430         }
1431
1432         if (response && coap_send(context, &node->remote, response) == COAP_INVALID_TID)
1433         {
1434             warn("cannot send response for transaction %u\n", node->id);
1435         }
1436         coap_delete_pdu(response);
1437
1438         return;
1439     }
1440
1441     /* the resource was found, check if there is a registered handler */
1442     if ((size_t) node->pdu->hdr->coap_hdr_udp_t.code - 1
1443             < sizeof(resource->handler) / sizeof(coap_method_handler_t))
1444         h = resource->handler[node->pdu->hdr->coap_hdr_udp_t.code - 1];
1445
1446     if (h)
1447     {
1448         debug(
1449                 "call custom handler for resource 0x%02x%02x%02x%02x\n", key[0], key[1], key[2], key[3]);
1450         response = coap_pdu_init(
1451                 node->pdu->hdr->coap_hdr_udp_t.type ==
1452                         COAP_MESSAGE_CON ? COAP_MESSAGE_ACK : COAP_MESSAGE_NON,
1453                 0, node->pdu->hdr->coap_hdr_udp_t.id, COAP_MAX_PDU_SIZE, coap_udp);
1454
1455         /* Implementation detail: coap_add_token() immediately returns 0
1456          if response == NULL */
1457         if (coap_add_token(response, node->pdu->hdr->coap_hdr_udp_t.token_length,
1458                            node->pdu->hdr->coap_hdr_udp_t.token, coap_udp))
1459         {
1460             str token =
1461             { node->pdu->hdr->coap_hdr_udp_t.token_length, node->pdu->hdr->coap_hdr_udp_t.token };
1462
1463             h(context, resource, &node->remote, node->pdu, &token, response);
1464
1465             unsigned char buf[3];
1466             response->hdr->coap_hdr_udp_t.code = COAP_RESPONSE_CODE(205);
1467             coap_add_option(response, COAP_OPTION_CONTENT_TYPE,
1468                     coap_encode_var_bytes(buf, COAP_MEDIATYPE_TEXT_PLAIN), buf, coap_udp);
1469             coap_add_option(response, COAP_OPTION_MAXAGE, coap_encode_var_bytes(buf, 0x2ffff), buf, coap_udp);
1470             coap_add_data(response, strlen(responseData), (unsigned char *) responseData);
1471
1472             if (response->hdr->coap_hdr_udp_t.type != COAP_MESSAGE_NON
1473                     || (response->hdr->coap_hdr_udp_t.code >= 64 && !coap_is_mcast(&node->local)))
1474             {
1475
1476                 if (coap_send(context, &node->remote, response) == COAP_INVALID_TID)
1477                 {
1478                     debug("cannot send response for message %d\n", node->pdu->hdr->coap_hdr_udp_t.id);
1479                 }
1480             }
1481
1482             coap_delete_pdu(response);
1483         }
1484         else
1485         {
1486             warn("cannot generate response\r\n");
1487         }
1488     }
1489     else
1490     {
1491         if (WANT_WKC(node->pdu, key))
1492         {
1493             debug("create default response for %s\n", COAP_DEFAULT_URI_WELLKNOWN);
1494             response = wellknown_response(context, node->pdu);
1495         }
1496         else
1497             response = coap_new_error_response(node->pdu, COAP_RESPONSE_CODE(405), opt_filter);
1498
1499         if (!response || (coap_send(context, &node->remote, response) == COAP_INVALID_TID))
1500         {
1501             debug("cannot send response for transaction %u\n", node->id);
1502         }
1503         coap_delete_pdu(response);
1504     }
1505 }
1506
1507 static inline void handle_response(coap_context_t *context, coap_queue_t *sent, coap_queue_t *rcvd)
1508 {
1509
1510     /* Call application-specific reponse handler when available.  If
1511      * not, we must acknowledge confirmable messages. */
1512     if (context->response_handler)
1513     {
1514
1515         context->response_handler(context, &rcvd->remote, sent ? sent->pdu : NULL, rcvd->pdu,
1516                 rcvd->id);
1517     }
1518     else
1519     {
1520         /* send ACK if rcvd is confirmable (i.e. a separate response) */
1521         coap_send_ack(context, &rcvd->remote, rcvd->pdu);
1522     }
1523 }
1524
1525 static inline int
1526 #ifdef __GNUC__
1527 handle_locally(coap_context_t *context __attribute__ ((unused)),
1528         coap_queue_t *node __attribute__ ((unused)))
1529 {
1530 #else /* not a GCC */
1531     handle_locally(coap_context_t *context, coap_queue_t *node)
1532     {
1533 #endif /* GCC */
1534         /* this function can be used to check if node->pdu is really for us */
1535         return 1;
1536     }
1537
1538     /**
1539      * This function handles RST messages received for the message passed
1540      * in @p sent.
1541      */
1542     static void coap_handle_rst(coap_context_t *context, const coap_queue_t *sent)
1543     {
1544 #ifndef WITHOUT_OBSERVE
1545         coap_resource_t *r;
1546 #ifndef COAP_RESOURCES_NOHASH
1547         coap_resource_t *tmp;
1548 #endif
1549         str token =
1550         { 0, NULL };
1551
1552         /* remove observer for this resource, if any
1553          * get token from sent and try to find a matching resource. Uh!
1554          */
1555
1556         COAP_SET_STR(&token, sent->pdu->hdr->coap_hdr_udp_t.token_length,
1557                      sent->pdu->hdr->coap_hdr_udp_t.token);
1558
1559 #ifndef WITH_CONTIKI
1560 #ifdef COAP_RESOURCES_NOHASH
1561         LL_FOREACH(context->resources, r)
1562         {
1563 #else
1564         HASH_ITER(hh, context->resources, r, tmp)
1565         {
1566 #endif
1567             coap_delete_observer(r, &sent->remote, &token);
1568             coap_cancel_all_messages(context, &sent->remote, token.s, token.length);
1569         }
1570 #else /* WITH_CONTIKI */
1571         r = (coap_resource_t *)resource_storage.mem;
1572         for (i = 0; i < resource_storage.num; ++i, ++r)
1573         {
1574             if (resource_storage.count[i])
1575             {
1576                 coap_delete_observer(r, &sent->remote, &token);
1577                 coap_cancel_all_messages(context, &sent->remote, token.s, token.length);
1578             }
1579         }
1580 #endif /* WITH_CONTIKI */
1581 #endif /* WITOUT_OBSERVE */
1582     }
1583
1584     void coap_dispatch(coap_context_t *context, const char* responseData)
1585     {
1586         coap_queue_t *rcvd = NULL, *sent = NULL;
1587         coap_pdu_t *response;
1588         coap_opt_filter_t opt_filter;
1589
1590         if (!context)
1591             return;
1592
1593         memset(opt_filter, 0, sizeof(coap_opt_filter_t));
1594
1595         while (context->recvqueue)
1596         {
1597             rcvd = context->recvqueue;
1598
1599             /* remove node from recvqueue */
1600             context->recvqueue = context->recvqueue->next;
1601             rcvd->next = NULL;
1602
1603             if (rcvd->pdu->hdr->coap_hdr_udp_t.version != COAP_DEFAULT_VERSION)
1604             {
1605                 debug("dropped packet with unknown version %u\n", rcvd->pdu->hdr->coap_hdr_udp_t.version);
1606                 goto cleanup;
1607             }
1608
1609             switch (rcvd->pdu->hdr->coap_hdr_udp_t.type)
1610             {
1611                 case COAP_MESSAGE_ACK:
1612                     /* find transaction in sendqueue to stop retransmission */
1613                     coap_remove_from_queue(&context->sendqueue, rcvd->id, &sent);
1614
1615                     if (rcvd->pdu->hdr->coap_hdr_udp_t.code == 0)
1616                         goto cleanup;
1617
1618                     /* FIXME: if sent code was >= 64 the message might have been a
1619                      * notification. Then, we must flag the observer to be alive
1620                      * by setting obs->fail_cnt = 0. */
1621                     if (sent && COAP_RESPONSE_CLASS(sent->pdu->hdr->coap_hdr_udp_t.code) == 2)
1622                     {
1623                         const str token =
1624                         { sent->pdu->hdr->coap_hdr_udp_t.token_length,
1625                           sent->pdu->hdr->coap_hdr_udp_t.token };
1626
1627                         coap_touch_observer(context, &sent->remote, &token);
1628                     }
1629                     break;
1630
1631                 case COAP_MESSAGE_RST:
1632                     /* We have sent something the receiver disliked, so we remove
1633                      * not only the transaction but also the subscriptions we might
1634                      * have. */
1635
1636                     coap_log(LOG_ALERT, "got RST for message %u\n",
1637                              ntohs(rcvd->pdu->hdr->coap_hdr_udp_t.id));
1638
1639                     /* find transaction in sendqueue to stop retransmission */
1640                     coap_remove_from_queue(&context->sendqueue, rcvd->id, &sent);
1641
1642                     if (sent)
1643                         coap_handle_rst(context, sent);
1644                     goto cleanup;
1645
1646                 case COAP_MESSAGE_NON: /* check for unknown critical options */
1647                     if (coap_option_check_critical(context, rcvd->pdu, opt_filter) == 0)
1648                         goto cleanup;
1649                     break;
1650
1651                 case COAP_MESSAGE_CON: /* check for unknown critical options */
1652                     if (coap_option_check_critical(context, rcvd->pdu, opt_filter) == 0)
1653                     {
1654
1655                         /* FIXME: send response only if we have received a request. Otherwise,
1656                          * send RST. */
1657                         response = coap_new_error_response(rcvd->pdu, COAP_RESPONSE_CODE(402),
1658                                 opt_filter);
1659
1660                         if (!response)
1661                             warn("coap_dispatch: cannot create error reponse\n");
1662                         else
1663                         {
1664                             if (coap_send(context, &rcvd->remote, response) == COAP_INVALID_TID)
1665                             {
1666                                 warn("coap_dispatch: error sending reponse\n");
1667                             }
1668                             coap_delete_pdu(response);
1669                         }
1670
1671                         goto cleanup;
1672                     }
1673                     break;
1674             }
1675
1676             /* Pass message to upper layer if a specific handler was
1677              * registered for a request that should be handled locally. */
1678             if (handle_locally(context, rcvd))
1679             {
1680                 if (COAP_MESSAGE_IS_REQUEST(rcvd->pdu->hdr->coap_hdr_udp_t))
1681                     handle_request(context, rcvd, responseData);
1682                 else if (COAP_MESSAGE_IS_RESPONSE(rcvd->pdu->hdr->coap_hdr_udp_t))
1683                     handle_response(context, sent, rcvd);
1684                 else
1685                 {
1686                     debug("dropped message with invalid code\n");
1687                     coap_send_message_type(context, &rcvd->remote, rcvd->pdu, COAP_MESSAGE_RST);
1688                 }
1689             }
1690
1691             cleanup: coap_delete_node(sent);
1692             coap_delete_node(rcvd);
1693         }
1694     }
1695
1696     int coap_can_exit(coap_context_t *context)
1697     {
1698         return !context || (context->recvqueue == NULL && context->sendqueue == NULL);
1699     }
1700
1701 #ifdef WITH_CONTIKI
1702
1703     /*---------------------------------------------------------------------------*/
1704     /* CoAP message retransmission */
1705     /*---------------------------------------------------------------------------*/
1706     PROCESS_THREAD(coap_retransmit_process, ev, data)
1707     {
1708         coap_tick_t now;
1709         coap_queue_t *nextpdu;
1710
1711         PROCESS_BEGIN();
1712
1713         debug("Started retransmit process\r\n");
1714
1715         while(1)
1716         {
1717             PROCESS_YIELD();
1718             if (ev == PROCESS_EVENT_TIMER)
1719             {
1720                 if (etimer_expired(&the_coap_context.retransmit_timer))
1721                 {
1722
1723                     nextpdu = coap_peek_next(&the_coap_context);
1724
1725                     coap_ticks(&now);
1726                     while (nextpdu && nextpdu->t <= now)
1727                     {
1728                         coap_retransmit(&the_coap_context, coap_pop_next(&the_coap_context));
1729                         nextpdu = coap_peek_next(&the_coap_context);
1730                     }
1731
1732                     /* need to set timer to some value even if no nextpdu is available */
1733                     etimer_set(&the_coap_context.retransmit_timer,
1734                             nextpdu ? nextpdu->t - now : 0xFFFF);
1735                 }
1736 #ifndef WITHOUT_OBSERVE
1737                 if (etimer_expired(&the_coap_context.notify_timer))
1738                 {
1739                     coap_check_notify(&the_coap_context);
1740                     etimer_reset(&the_coap_context.notify_timer);
1741                 }
1742 #endif /* WITHOUT_OBSERVE */
1743             }
1744         }
1745
1746         PROCESS_END();
1747     }
1748     /*---------------------------------------------------------------------------*/
1749
1750 #endif /* WITH_CONTIKI */
1751
1752 #ifdef WITH_LWIP
1753     /* FIXME: retransmits that are not required any more due to incoming packages
1754      * do *not* get cleared at the moment, the wakeup when the transmission is due
1755      * is silently accepted. this is mainly due to the fact that the required
1756      * checks are similar in two places in the code (when receiving ACK and RST)
1757      * and that they cause more than one patch chunk, as it must be first checked
1758      * whether the sendqueue item to be dropped is the next one pending, and later
1759      * the restart function has to be called. nothing insurmountable, but it can
1760      * also be implemented when things have stabilized, and the performance
1761      * penality is minimal
1762      *
1763      * also, this completely ignores COAP_RESOURCE_CHECK_TIME.
1764      * */
1765
1766     static void coap_retransmittimer_execute(void *arg)
1767     {
1768         coap_context_t *ctx = (coap_context_t*)arg;
1769         coap_tick_t now;
1770         coap_tick_t elapsed;
1771         coap_queue_t *nextinqueue;
1772
1773         ctx->timer_configured = 0;
1774
1775         coap_ticks(&now);
1776
1777         elapsed = now - ctx->sendqueue_basetime; /* that's positive for sure, and unless we haven't been called for a complete wrapping cycle, did not wrap */
1778
1779         nextinqueue = coap_peek_next(ctx);
1780         while (nextinqueue != NULL)
1781         {
1782             if (nextinqueue->t > elapsed)
1783             {
1784                 nextinqueue->t -= elapsed;
1785                 break;
1786             }
1787             else
1788             {
1789                 elapsed -= nextinqueue->t;
1790                 coap_retransmit(ctx, coap_pop_next(ctx));
1791                 nextinqueue = coap_peek_next(ctx);
1792             }
1793         }
1794
1795         ctx->sendqueue_basetime = now;
1796
1797         coap_retransmittimer_restart(ctx);
1798     }
1799
1800     static void coap_retransmittimer_restart(coap_context_t *ctx)
1801     {
1802         coap_tick_t now, elapsed, delay;
1803
1804         if (ctx->timer_configured)
1805         {
1806             printf("clearing\n");
1807             sys_untimeout(coap_retransmittimer_execute, (void*)ctx);
1808             ctx->timer_configured = 0;
1809         }
1810         if (ctx->sendqueue != NULL)
1811         {
1812             coap_ticks(&now);
1813             elapsed = now - ctx->sendqueue_basetime;
1814             if (ctx->sendqueue->t >= elapsed)
1815             {
1816                 delay = ctx->sendqueue->t - elapsed;
1817             }
1818             else
1819             {
1820                 /* a strange situation, but not completely impossible.
1821                  *
1822                  * this happens, for example, right after
1823                  * coap_retransmittimer_execute, when a retransmission
1824                  * was *just not yet* due, and the clock ticked before
1825                  * our coap_ticks was called.
1826                  *
1827                  * not trying to retransmit anything now, as it might
1828                  * cause uncontrollable recursion; let's just try again
1829                  * with the next main loop run.
1830                  * */
1831                 delay = 0;
1832             }
1833
1834             printf("scheduling for %d ticks\n", delay);
1835             sys_timeout(delay, coap_retransmittimer_execute, (void*)ctx);
1836             ctx->timer_configured = 1;
1837         }
1838     }
1839 #endif