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