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