18 #elif HAVE_SYS_UNISTD_H
19 #include <sys/unistd.h>
21 #include <sys/types.h>
22 #ifdef HAVE_SYS_SOCKET_H
23 #include <sys/socket.h>
25 #ifdef HAVE_NETINET_IN_H
26 #include <netinet/in.h>
28 #ifdef HAVE_ARPA_INET_H
29 #include <arpa/inet.h>
33 #include <lwip/pbuf.h>
35 #include <lwip/timers.h>
48 #if defined(WITH_POSIX)
64 #include <lwip/memp.h>
66 static void coap_retransmittimer_execute(
void *arg);
76 memp_free(MEMP_COAP_NODE, node);
82 # define DEBUG DEBUG_PRINT
86 #include "net/uip-debug.h"
88 clock_time_t clock_offset;
90 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
91 #define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[UIP_LLIPH_LEN])
93 void coap_resources_init();
94 void coap_pdu_resources_init();
96 unsigned char initialized = 0;
101 PROCESS(coap_retransmit_process,
"message retransmit process");
110 memb_free(&node_storage, node);
122 static void received_package(
void *arg,
struct udp_pcb *upcb,
struct pbuf *p, ip_addr_t *addr, u16_t port)
126 LWIP_ASSERT(
"pending_package was not cleared.", context->pending_package == NULL);
128 context->pending_package = p;
129 context->pending_address.addr = addr->addr;
130 context->pending_port = port;
144 unsigned int result = 0;
159 while (q && (t + q->
t < (coap_tick_t)delta)) {
168 q->
t = (coap_tick_t)delta - t;
182 if ( !queue || !node )
193 if (node->
t < q->
t) {
205 }
while (q && q->
t <= node->
t);
222 coap_free_node(node);
239 node = coap_malloc_node();
243 coap_log(LOG_WARNING,
"coap_new_node: malloc\n");
248 memset(node, 0,
sizeof *node );
276 #ifdef COAP_DEFAULT_WKC_HASHKEY
278 #define is_wkc(Key) \
279 (memcmp((Key), COAP_DEFAULT_WKC_HASHKEY, sizeof(coap_key_t)) == 0)
286 static unsigned char _initialized = 0;
291 return memcmp(k, wkc,
sizeof(
coap_key_t)) == 0;
297 const coap_address_t *listen_addr) {
313 coap_log(LOG_EMERG,
"no listen address specified\n");
321 prng_init((
unsigned long)listen_addr ^ clock_offset);
327 coap_log(LOG_EMERG,
"coap_init: malloc:\n");
333 coap_resources_init();
334 coap_pdu_resources_init();
336 c = &the_coap_context;
359 c->sockfd = socket(listen_addr->addr.sa.sa_family, SOCK_DGRAM, 0);
360 if ( c->sockfd < 0 ) {
362 coap_log(LOG_EMERG,
"coap_new_context: socket\n");
367 if ( setsockopt( c->sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse,
sizeof(reuse) ) < 0 ) {
369 coap_log(LOG_WARNING,
"setsockopt SO_REUSEADDR\n");
373 if (bind(c->sockfd, &listen_addr->addr.sa, listen_addr->size) < 0) {
375 coap_log(LOG_EMERG,
"coap_new_context: bind\n");
383 if ( c->sockfd >= 0 )
390 c->conn = udp_new(NULL, 0, NULL);
391 udp_bind(c->conn, listen_addr->port);
393 process_start(&coap_retransmit_process, (
char *)c);
395 PROCESS_CONTEXT_BEGIN(&coap_retransmit_process);
396 #ifndef WITHOUT_OBSERVE
400 etimer_set(&the_coap_context.retransmit_timer, 0xFFFF);
401 PROCESS_CONTEXT_END(&coap_retransmit_process);
407 LWIP_ASSERT(
"Failed to allocate PCB for CoAP", c->pcb != NULL);
409 udp_recv(c->pcb, received_package, (
void*)c);
410 udp_bind(c->pcb, &listen_addr->addr, listen_addr->port);
412 c->timer_configured = 0;
420 #if defined(WITH_POSIX) || defined(WITH_LWIP)
422 #ifndef COAP_RESOURCES_NOHASH
434 coap_retransmittimer_restart(context);
437 #if defined(WITH_POSIX) || defined(WITH_LWIP)
438 #ifdef COAP_RESOURCES_NOHASH
449 close( context->sockfd );
453 udp_remove(context->pcb);
454 memp_free(MEMP_COAP_CONTEXT, context);
480 if (opt_iter.
type & 0x01 &&
482 debug(
"unknown critical option %d\n", opt_iter.
type);
508 switch (peer->addr.sa.sa_family) {
510 coap_hash((
const unsigned char *)&peer->addr.sa, peer->size, h);
513 coap_hash((
const unsigned char *)&peer->addr.sin6.sin6_port,
514 sizeof(peer->addr.sin6.sin6_port), h);
515 coap_hash((
const unsigned char *)&peer->addr.sin6.sin6_addr,
516 sizeof(peer->addr.sin6.sin6_addr), h);
522 #if defined(WITH_LWIP) || defined(WITH_CONTIKI)
524 coap_hash((
const unsigned char *)&peer->port,
sizeof(peer->port), h);
525 coap_hash((
const unsigned char *)&peer->addr,
sizeof(peer->addr), h);
528 coap_hash((
const unsigned char *)&pdu->
hdr->
id,
sizeof(
unsigned short), h);
530 *
id = ((h[0] << 8) | h[1]) ^ ((h[2] << 8) | h[3]);
535 const coap_address_t *
dst,
544 result =
coap_send(context, dst, response);
555 const coap_address_t *
dst,
557 ssize_t bytes_written;
560 if ( !context || !dst || !pdu )
563 bytes_written = sendto( context->sockfd, pdu->
hdr, pdu->
length, 0,
564 &dst->addr.sa, dst->size);
566 if (bytes_written >= 0) {
569 coap_log(LOG_CRIT,
"coap_send: sendto\n");
579 const coap_address_t *dst,
583 if ( !context || !dst || !pdu )
587 uip_udp_packet_sendto(context->conn, pdu->
hdr, pdu->
length,
588 &dst->addr, dst->port);
598 const coap_address_t *dst,
605 if ( !context || !dst || !pdu )
610 data_backup = pdu->
data;
617 LWIP_ASSERT(
"The PDU header is not where it is expected", pdu->
hdr == p->payload +
sizeof(
coap_pdu_t));
622 debug(
"coap_send_impl: pbuf_header failed\n");
629 pbuf_realloc(p, pdu->
length);
631 udp_sendto(context->pcb, p,
632 &dst->addr, dst->port);
634 pbuf_header(p, -(ptrdiff_t)((uint8_t*)pdu - (uint8_t*)p->payload) -
sizeof(
coap_pdu_t));
637 LWIP_ASSERT(
"Cannot undo pbuf_header", err == 0);
640 LWIP_ASSERT(
"PDU not restored", p->payload == pdu);
645 pdu->
data = data_backup;
654 const coap_address_t *dst,
656 return coap_send_impl(context, dst, pdu);
662 const coap_address_t *dst,
673 result =
coap_send(context, dst, response);
682 const coap_address_t *dst,
684 unsigned char type) {
691 result =
coap_send(context, dst, response);
700 const coap_address_t *dst,
708 debug(
"coap_send_confirmed: insufficient memory\n");
712 node->
id = coap_send_impl(context, dst, pdu);
714 debug(
"coap_send_confirmed: error sending pdu\n");
715 coap_free_node(node);
719 prng((
unsigned char *)&r,
sizeof(r));
724 ((COAP_TICKS_PER_SECOND * (r & 0xFF)) >> 8);
726 memcpy(&node->
remote, dst,
sizeof(coap_address_t));
750 coap_retransmittimer_restart(context);
761 PROCESS_CONTEXT_BEGIN(&coap_retransmit_process);
762 etimer_set(&context->retransmit_timer, nextpdu->
t);
763 PROCESS_CONTEXT_END(&coap_retransmit_process);
772 if ( !context || !node )
782 coap_retransmittimer_restart(context);
785 debug(
"** retransmission #%d of transaction %d\n",
788 node->
id = coap_send_impl(context, &node->
remote, node->
pdu);
795 debug(
"** removed transaction %d\n", ntohs(node->
id));
798 #ifndef WITHOUT_OBSERVE
802 str token = { 0, NULL };
823 if (opt && opt < maxpos) {
824 if (((*opt & 0x0f) < 0x0f) || (opt + 1 < maxpos))
835 #if defined(WITH_LWIP) || defined(WITH_CONTIKI)
839 ssize_t bytes_read = -1;
840 coap_address_t src,
dst;
847 LWIP_ASSERT(
"No package pending", ctx->pending_package != NULL);
848 LWIP_ASSERT(
"Can only deal with contiguous PBUFs to read the initial details", ctx->pending_package->tot_len == ctx->pending_package->len);
849 buf = ctx->pending_package->payload;
857 bytes_read = recvfrom(ctx->sockfd, buf,
sizeof(buf), 0,
858 &src.addr.sa, &src.size);
862 uip_ipaddr_copy(&src.addr, &UIP_IP_BUF->srcipaddr);
863 src.port = UIP_UDP_BUF->srcport;
864 uip_ipaddr_copy(&dst.addr, &UIP_IP_BUF->destipaddr);
865 dst.port = UIP_UDP_BUF->destport;
867 bytes_read = uip_datalen();
868 ((
char *)uip_appdata)[bytes_read] = 0;
869 PRINTF(
"Server received %d bytes from [", (
int)bytes_read);
870 PRINT6ADDR(&src.addr);
871 PRINTF(
"]:%d\n", uip_ntohs(src.port));
876 src.addr.addr = ctx->pending_address.addr;
877 src.port = ctx->pending_port;
878 bytes_read = ctx->pending_package->tot_len;
881 if ( bytes_read < 0 ) {
882 warn(
"coap_read: recvfrom");
886 if ( (
size_t)bytes_read <
sizeof(
coap_hdr_t) ) {
887 debug(
"coap_read: discarded invalid frame\n" );
892 debug(
"coap_read: unknown protocol version\n" );
901 node->
pdu = coap_pdu_from_pbuf(ctx->pending_package);
902 ctx->pending_package = NULL;
910 memcpy(&node->
local, &dst,
sizeof(coap_address_t));
911 memcpy(&node->
remote, &src,
sizeof(coap_address_t));
914 warn(
"discard malformed PDU");
924 #ifndef INET6_ADDRSTRLEN
925 #define INET6_ADDRSTRLEN 40
930 debug(
"** received %d bytes from %s:\n", (
int)bytes_read, addr);
945 pbuf_free(ctx->pending_package);
946 ctx->pending_package = NULL;
955 if ( !queue || !*queue)
960 if (
id == (*queue)->id ) {
962 *queue = (*queue)->
next;
964 (*queue)->
t += (*node)->t;
966 (*node)->next = NULL;
968 debug(
"*** removed transaction %u\n",
id);
977 }
while ( q &&
id != q->
id );
987 debug(
"*** removed transaction %u\n",
id);
997 const unsigned char *b,
size_t blen) {
998 return alen == blen && (alen == 0 || memcmp(a, b, alen) == 0);
1003 const unsigned char *token,
size_t token_length) {
1008 debug(
"cancel_all_messages\n");
1016 debug(
"**** removed transaction %d\n", ntohs(q->
pdu->
hdr->
id));
1032 debug(
"**** removed transaction %d\n", ntohs(q->
pdu->
hdr->
id));
1044 while (queue && queue->
id !=
id)
1045 queue = queue->
next;
1058 unsigned short opt_type = 0;
1065 size += strlen(phrase) + 1;
1087 unsigned short delta = opt_iter.
type - opt_type;
1091 }
else if (delta < 269) {
1101 switch (*option & 0x0f) {
1112 opt_type = opt_iter.
type;
1121 debug(
"cannot add token to error response\n");
1133 #if COAP_ERROR_PHRASE_LENGTH > 0
1136 coap_add_data(response, strlen(phrase), (
unsigned char *)phrase);
1147 static inline size_t
1149 unsigned char buf[1];
1154 warn(
"cannot determine length of /.well-known/core\n");
1158 debug(
"get_wkc_len: print_wellknown() returned %zu\n", len);
1163 #define SZX_TO_BYTES(SZX) ((size_t)(1 << ((SZX) + 4)))
1169 size_t len, wkc_len;
1170 unsigned char buf[2];
1172 int need_block2 = 0;
1183 debug(
"wellknown_response: cannot create PDU\n");
1188 debug(
"wellknown_response: cannot add token\n");
1197 offset = block.
num << (block.
szx + 4);
1198 if (block.
szx > 6) {
1203 block.
num = offset >> (block.
szx + 4);
1214 debug(
"wellknown_response: insufficient storage space\n");
1227 if (!need_block2 && (resp->
max_size - (
size_t)resp->
length < wkc_len)) {
1235 if (block.
szx == 0) {
1236 debug(
"wellknown_response: message to small even for szx == 0\n");
1249 debug(
"wellknown_response: cannot add Block2 option\n");
1265 debug(
"print_wellknown failed\n");
1279 #define WANT_WKC(Pdu,Key) \
1280 (((Pdu)->hdr->code == COAP_REQUEST_GET) && is_wkc(Key))
1310 debug(
"GET for unknown resource 0x%02x%02x%02x%02x, return 4.04\n",
1311 key[0], key[1], key[2], key[3]);
1320 debug(
"unhandled request for unknown resource 0x%02x%02x%02x%02x\r\n",
1321 key[0], key[1], key[2], key[3]);
1328 warn(
"cannot send response for transaction %u\n", node->
id);
1341 debug(
"call custom handler for resource 0x%02x%02x%02x%02x\n",
1342 key[0], key[1], key[2], key[3]);
1354 h(context, resource, &node->
remote,
1355 node->
pdu, &token, response);
1356 if (response->
hdr->
type != COAP_MESSAGE_NON ||
1360 debug(
"cannot send response for message %d\n", node->
pdu->
hdr->
id);
1366 warn(
"cannot generate response\r\n");
1378 debug(
"cannot send response for transaction %u\n", node->
id);
1393 rcvd->
pdu, rcvd->
id);
1417 #ifndef WITHOUT_OBSERVE
1419 #ifndef COAP_RESOURCES_NOHASH
1422 str token = { 0, NULL };
1430 #ifndef WITH_CONTIKI
1431 #ifdef COAP_RESOURCES_NOHASH
1441 for (i = 0; i < resource_storage.num; ++i, ++r) {
1442 if (resource_storage.count[i]) {
1487 { sent->pdu->hdr->token_length, sent->pdu->hdr->token };
1497 coap_log(LOG_ALERT,
"got RST for message %u\n", ntohs(rcvd->
pdu->
hdr->
id));
1520 warn(
"coap_dispatch: cannot create error reponse\n");
1524 warn(
"coap_dispatch: error sending reponse\n");
1542 debug(
"dropped message with invalid code\n");
1571 debug(
"Started retransmit process\r\n");
1575 if (ev == PROCESS_EVENT_TIMER) {
1576 if (etimer_expired(&the_coap_context.retransmit_timer)) {
1581 while (nextpdu && nextpdu->
t <= now) {
1587 etimer_set(&the_coap_context.retransmit_timer,
1588 nextpdu ? nextpdu->
t - now : 0xFFFF);
1590 #ifndef WITHOUT_OBSERVE
1591 if (etimer_expired(&the_coap_context.notify_timer)) {
1593 etimer_reset(&the_coap_context.notify_timer);
1619 static void coap_retransmittimer_execute(
void *arg)
1623 coap_tick_t elapsed;
1626 ctx->timer_configured = 0;
1633 while (nextinqueue != NULL)
1635 if (nextinqueue->
t > elapsed) {
1636 nextinqueue->
t -= elapsed;
1639 elapsed -= nextinqueue->
t;
1647 coap_retransmittimer_restart(ctx);
1652 coap_tick_t now, elapsed, delay;
1654 if (ctx->timer_configured)
1656 printf(
"clearing\n");
1657 sys_untimeout(coap_retransmittimer_execute, (
void*)ctx);
1658 ctx->timer_configured = 0;
1681 printf(
"scheduling for %d ticks\n", delay);
1682 sys_timeout(delay, coap_retransmittimer_execute, (
void*)ctx);
1683 ctx->timer_configured = 1;