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