libcoap  4.1.1
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
rd.c
Go to the documentation of this file.
1 /* coap -- simple implementation of the Constrained Application Protocol (CoAP)
2  * as defined in draft-ietf-core-coap
3  *
4  * Copyright (C) 2010--2013 Olaf Bergmann <bergmann@tzi.org>
5  *
6  * This file is part of the CoAP library libcoap. Please see
7  * README for terms of use.
8  */
9 
10 
18 #include <string.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <ctype.h>
23 #include <sys/select.h>
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <netdb.h>
29 #include <sys/stat.h>
30 #include <dirent.h>
31 #include <errno.h>
32 #include <signal.h>
33 
34 #include "config.h"
35 #include "utlist.h"
36 #include "resource.h"
37 #include "coap.h"
38 
39 #define COAP_RESOURCE_CHECK_TIME 2
40 
41 #define RD_ROOT_STR ((unsigned char *)"rd")
42 #define RD_ROOT_SIZE 2
43 
44 #ifndef min
45 #define min(a,b) ((a) < (b) ? (a) : (b))
46 #endif
47 
48 typedef struct rd_t {
52  size_t etag_len;
53  unsigned char etag[8];
56 } rd_t;
57 
58 rd_t *resources = NULL;
59 
60 inline rd_t *
61 rd_new() {
62  rd_t *rd;
63  rd = (rd_t *)coap_malloc(sizeof(rd_t));
64  if (rd)
65  memset(rd, 0, sizeof(rd_t));
66 
67  return rd;
68 }
69 
70 inline void
72  if (rd) {
73  coap_free(rd->data.s);
74  coap_free(rd);
75  }
76 }
77 
78 /* temporary storage for dynamic resource representations */
79 static int quit = 0;
80 
81 /* SIGINT handler: set quit to 1 for graceful termination */
82 void
83 handle_sigint(int signum) {
84  quit = 1;
85 }
86 
87 void
89  coap_address_t *peer, coap_pdu_t *request, str *token,
90  coap_pdu_t *response) {
91  rd_t *rd = NULL;
92  unsigned char buf[3];
93 
94  HASH_FIND(hh, resources, resource->key, sizeof(coap_key_t), rd);
95 
96  response->hdr->code = COAP_RESPONSE_CODE(205);
97 
100 
101  if (rd && rd->etag_len)
102  coap_add_option(response, COAP_OPTION_ETAG, rd->etag_len, rd->etag);
103 
104  if (rd && rd->data.s)
105  coap_add_data(response, rd->data.length, rd->data.s);
106 }
107 
108 void
110  coap_address_t *peer, coap_pdu_t *request, str *token,
111  coap_pdu_t *response) {
112 #if 1
113  response->hdr->code = COAP_RESPONSE_CODE(501);
114 #else /* FIXME */
115  coap_opt_iterator_t opt_iter;
116  coap_opt_t *token, *etag;
117  coap_pdu_t *response;
118  size_t size = sizeof(coap_hdr_t);
119  int type = (request->hdr->type == COAP_MESSAGE_CON)
121  rd_t *rd = NULL;
122  unsigned char code; /* result code */
123  unsigned char *data;
124  str tmp;
125 
126  HASH_FIND(hh, resources, resource->key, sizeof(coap_key_t), rd);
127  if (rd) {
128  /* found resource object, now check Etag */
129  etag = coap_check_option(request, COAP_OPTION_ETAG, &opt_iter);
130  if (!etag || (COAP_OPT_LENGTH(etag) != rd->etag_len)
131  || memcmp(COAP_OPT_VALUE(etag), rd->etag, rd->etag_len) != 0) {
132 
133  if (coap_get_data(request, &tmp.length, &data)) {
134 
135  tmp.s = (unsigned char *)coap_malloc(tmp.length);
136  if (!tmp.s) {
137  debug("hnd_put_rd: cannot allocate storage for new rd\n");
138  code = COAP_RESPONSE_CODE(503);
139  goto finish;
140  }
141 
142  coap_free(rd->data.s);
143  rd->data.s = tmp.s;
144  rd->data.length = tmp.length;
145  memcpy(rd->data.s, data, rd->data.length);
146  }
147  }
148 
149  if (etag) {
150  rd->etag_len = min(COAP_OPT_LENGTH(etag), sizeof(rd->etag));
151  memcpy(rd->etag, COAP_OPT_VALUE(etag), rd->etag_len);
152  }
153 
154  code = COAP_RESPONSE_CODE(204);
155  /* FIXME: update lifetime */
156 
157  } else {
158 
159  code = COAP_RESPONSE_CODE(503);
160  }
161 
162  finish:
163  /* FIXME: do not create a new response but use the old one instead */
164  response = coap_pdu_init(type, code, request->hdr->id, size);
165 
166  if (!response) {
167  debug("cannot create response for message %d\n", request->hdr->id);
168  return;
169  }
170 
171  if (request->hdr->token_length)
172  coap_add_token(response, request->hdr->token_length, request->hdr->token);
173 
174  if (coap_send(ctx, peer, response) == COAP_INVALID_TID) {
175  debug("hnd_get_rd: cannot send response for message %d\n",
176  request->hdr->id);
177  }
178  coap_delete_pdu(response);
179 #endif
180 }
181 
182 void
184  coap_address_t *peer, coap_pdu_t *request, str *token,
185  coap_pdu_t *response) {
186  rd_t *rd = NULL;
187 
188  HASH_FIND(hh, resources, resource->key, sizeof(coap_key_t), rd);
189  if (rd) {
190  HASH_DELETE(hh, resources, rd);
191  rd_delete(rd);
192  }
193  /* FIXME: link attributes for resource have been created dynamically
194  * using coap_malloc() and must be released. */
195  coap_delete_resource(ctx, resource->key);
196 
197  response->hdr->code = COAP_RESPONSE_CODE(202);
198 }
199 
200 void
201 hnd_get_rd(coap_context_t *ctx, struct coap_resource_t *resource,
202  coap_address_t *peer, coap_pdu_t *request, str *token,
203  coap_pdu_t *response) {
204  unsigned char buf[3];
205 
206  response->hdr->code = COAP_RESPONSE_CODE(205);
207 
210 
212  coap_encode_var_bytes(buf, 0x2ffff), buf);
213 }
214 
215 int
216 parse_param(unsigned char *search, size_t search_len,
217  unsigned char *data, size_t data_len, str *result) {
218 
219  if (result)
220  memset(result, 0, sizeof(str));
221 
222  if (!search_len)
223  return 0;
224 
225  while (search_len <= data_len) {
226 
227  /* handle parameter if found */
228  if (memcmp(search, data, search_len) == 0) {
229  data += search_len;
230  data_len -= search_len;
231 
232  /* key is only valid if we are at end of string or delimiter follows */
233  if (!data_len || *data == '=' || *data == '&') {
234  while (data_len && *data != '=') {
235  ++data; --data_len;
236  }
237 
238  if (data_len > 1 && result) {
239  /* value begins after '=' */
240 
241  result->s = ++data;
242  while (--data_len && *data != '&') {
243  ++data; result->length++;
244  }
245  }
246 
247  return 1;
248  }
249  }
250 
251  /* otherwise proceed to next */
252  while (--data_len && *data++ != '&')
253  ;
254  }
255 
256  return 0;
257 }
258 
259 void
260 add_source_address(struct coap_resource_t *resource, coap_address_t *peer) {
261 #define BUFSIZE 64
262  char *buf;
263  size_t n = 1;
264 
265  buf = (char *)coap_malloc(BUFSIZE);
266  if (!buf)
267  return;
268 
269  buf[0] = '"';
270 
271  switch(peer->addr.sa.sa_family) {
272 
273  case AF_INET:
274  /* FIXME */
275  break;
276 
277  case AF_INET6:
278  n += snprintf(buf + n, BUFSIZE - n,
279  "[%02x%02x:%02x%02x:%02x%02x:%02x%02x" \
280  ":%02x%02x:%02x%02x:%02x%02x:%02x%02x]",
281  peer->addr.sin6.sin6_addr.s6_addr[0],
282  peer->addr.sin6.sin6_addr.s6_addr[1],
283  peer->addr.sin6.sin6_addr.s6_addr[2],
284  peer->addr.sin6.sin6_addr.s6_addr[3],
285  peer->addr.sin6.sin6_addr.s6_addr[4],
286  peer->addr.sin6.sin6_addr.s6_addr[5],
287  peer->addr.sin6.sin6_addr.s6_addr[6],
288  peer->addr.sin6.sin6_addr.s6_addr[7],
289  peer->addr.sin6.sin6_addr.s6_addr[8],
290  peer->addr.sin6.sin6_addr.s6_addr[9],
291  peer->addr.sin6.sin6_addr.s6_addr[10],
292  peer->addr.sin6.sin6_addr.s6_addr[11],
293  peer->addr.sin6.sin6_addr.s6_addr[12],
294  peer->addr.sin6.sin6_addr.s6_addr[13],
295  peer->addr.sin6.sin6_addr.s6_addr[14],
296  peer->addr.sin6.sin6_addr.s6_addr[15]);
297 
298  if (peer->addr.sin6.sin6_port != htons(COAP_DEFAULT_PORT)) {
299  n +=
300  snprintf(buf + n, BUFSIZE - n, ":%d", peer->addr.sin6.sin6_port);
301  }
302  break;
303  default:
304  ;
305  }
306 
307  if (n < BUFSIZE)
308  buf[n++] = '"';
309 
310  coap_add_attr(resource, (unsigned char *)"A", 1, (unsigned char *)buf, n, COAP_ATTR_FLAGS_RELEASE_VALUE);
311 #undef BUFSIZE
312 }
313 
314 
315 rd_t *
316 make_rd(coap_address_t *peer, coap_pdu_t *pdu) {
317  rd_t *rd;
318  unsigned char *data;
319  coap_opt_iterator_t opt_iter;
320  coap_opt_t *etag;
321 
322  rd = rd_new();
323 
324  if (!rd) {
325  debug("hnd_get_rd: cannot allocate storage for rd\n");
326  return NULL;
327  }
328 
329  if (coap_get_data(pdu, &rd->data.length, &data)) {
330  rd->data.s = (unsigned char *)coap_malloc(rd->data.length);
331  if (!rd->data.s) {
332  debug("hnd_get_rd: cannot allocate storage for rd->data\n");
333  rd_delete(rd);
334  return NULL;
335  }
336  memcpy(rd->data.s, data, rd->data.length);
337  }
338 
339  etag = coap_check_option(pdu, COAP_OPTION_ETAG, &opt_iter);
340  if (etag) {
341  rd->etag_len = min(COAP_OPT_LENGTH(etag), sizeof(rd->etag));
342  memcpy(rd->etag, COAP_OPT_VALUE(etag), rd->etag_len);
343  }
344 
345  return rd;
346 }
347 
348 void
350  coap_address_t *peer, coap_pdu_t *request, str *token,
351  coap_pdu_t *response) {
352  coap_resource_t *r;
353  coap_opt_iterator_t opt_iter;
354  coap_opt_t *query;
355 #define LOCSIZE 68
356  unsigned char *loc;
357  size_t loc_size;
358  str h = {0, NULL}, ins = {0, NULL}, rt = {0, NULL}, lt = {0, NULL}; /* store query parameters */
359  unsigned char *buf;
360 
361  loc = (unsigned char *)coap_malloc(LOCSIZE);
362  if (!loc) {
363  response->hdr->code = COAP_RESPONSE_CODE(500);
364  return;
365  }
366  memcpy(loc, RD_ROOT_STR, RD_ROOT_SIZE);
367 
368  loc_size = RD_ROOT_SIZE;
369  loc[loc_size++] = '/';
370 
371  /* store query parameters for later use */
372  query = coap_check_option(request, COAP_OPTION_URI_QUERY, &opt_iter);
373  if (query) {
374  parse_param((unsigned char *)"h", 1,
375  COAP_OPT_VALUE(query), COAP_OPT_LENGTH(query), &h);
376  parse_param((unsigned char *)"ins", 3,
377  COAP_OPT_VALUE(query), COAP_OPT_LENGTH(query), &ins);
378  parse_param((unsigned char *)"lt", 2,
379  COAP_OPT_VALUE(query), COAP_OPT_LENGTH(query), &lt);
380  parse_param((unsigned char *)"rt", 2,
381  COAP_OPT_VALUE(query), COAP_OPT_LENGTH(query), &rt);
382  }
383 
384  if (h.length) { /* client has specified a node name */
385  memcpy(loc + loc_size, h.s, min(h.length, LOCSIZE - loc_size - 1));
386  loc_size += min(h.length, LOCSIZE - loc_size - 1);
387 
388  if (ins.length && loc_size > 1) {
389  loc[loc_size++] = '-';
390  memcpy((char *)(loc + loc_size),
391  ins.s, min(ins.length, LOCSIZE - loc_size - 1));
392  loc_size += min(ins.length, LOCSIZE - loc_size - 1);
393  }
394 
395  } else { /* generate node identifier */
396  loc_size +=
397  snprintf((char *)(loc + loc_size), LOCSIZE - loc_size - 1,
398  "%x", request->hdr->id);
399 
400  if (loc_size > 1) {
401  if (ins.length) {
402  loc[loc_size++] = '-';
403  memcpy((char *)(loc + loc_size),
404  ins.s, min(ins.length, LOCSIZE - loc_size - 1));
405  loc_size += min(ins.length, LOCSIZE - loc_size - 1);
406  } else {
407  coap_tick_t now;
408  coap_ticks(&now);
409 
410  loc_size +=
411  snprintf((char *)(loc + loc_size), LOCSIZE - loc_size - 1,
412  "-%x", now);
413  }
414  }
415  }
416 
417  /* TODO:
418  * - use lt to check expiration
419  */
420 
425 
426  if (ins.s) {
427  buf = (unsigned char *)coap_malloc(ins.length + 2);
428  if (buf) {
429  /* add missing quotes */
430  buf[0] = '"';
431  memcpy(buf + 1, ins.s, ins.length);
432  buf[ins.length + 1] = '"';
433  coap_add_attr(r, (unsigned char *)"ins", 3, buf, ins.length + 2, COAP_ATTR_FLAGS_RELEASE_VALUE);
434  }
435  }
436 
437  if (rt.s) {
438  buf = (unsigned char *)coap_malloc(rt.length + 2);
439  if (buf) {
440  /* add missing quotes */
441  buf[0] = '"';
442  memcpy(buf + 1, rt.s, rt.length);
443  buf[rt.length + 1] = '"';
444  coap_add_attr(r, (unsigned char *)"rt", 2, buf, rt.length + 2, COAP_ATTR_FLAGS_RELEASE_VALUE);
445  }
446  }
447 
448  add_source_address(r, peer);
449 
450  {
451  rd_t *rd;
452  rd = make_rd(peer, request);
453  if (rd) {
454  coap_hash_path(loc, loc_size, rd->key);
455  HASH_ADD(hh, resources, key, sizeof(coap_key_t), rd);
456  } else {
457  /* FIXME: send error response and delete r */
458  }
459  }
460 
461  coap_add_resource(ctx, r);
462 
463 
464  /* create response */
465 
466  response->hdr->code = COAP_RESPONSE_CODE(201);
467 
468  { /* split path into segments and add Location-Path options */
469  unsigned char _b[LOCSIZE];
470  unsigned char *b = _b;
471  size_t buflen = sizeof(_b);
472  int nseg;
473 
474  nseg = coap_split_path(loc, loc_size, b, &buflen);
475  while (nseg--) {
478  b += COAP_OPT_SIZE(b);
479  }
480  }
481 }
482 
483 void
485  coap_resource_t *r;
486 
490 
491  coap_add_attr(r, (unsigned char *)"ct", 2, (unsigned char *)"40", 2, 0);
492  coap_add_attr(r, (unsigned char *)"rt", 2, (unsigned char *)"\"core-rd\"", 9, 0);
493  coap_add_attr(r, (unsigned char *)"ins", 2, (unsigned char *)"\"default\"", 9, 0);
494 
495  coap_add_resource(ctx, r);
496 
497 }
498 
499 void
500 usage( const char *program, const char *version) {
501  const char *p;
502 
503  p = strrchr( program, '/' );
504  if ( p )
505  program = ++p;
506 
507  fprintf( stderr, "%s v%s -- CoRE Resource Directory implementation\n"
508  "(c) 2011-2012 Olaf Bergmann <bergmann@tzi.org>\n\n"
509  "usage: %s [-A address] [-p port]\n\n"
510  "\t-A address\tinterface address to bind to\n"
511  "\t-p port\t\tlisten on specified port\n"
512  "\t-v num\t\tverbosity level (default: 3)\n",
513  program, version, program );
514 }
515 
517 get_context(const char *node, const char *port) {
518  coap_context_t *ctx = NULL;
519  int s;
520  struct addrinfo hints;
521  struct addrinfo *result, *rp;
522 
523  memset(&hints, 0, sizeof(struct addrinfo));
524  hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
525  hints.ai_socktype = SOCK_DGRAM; /* Coap uses UDP */
526  hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
527 
528  s = getaddrinfo(node, port, &hints, &result);
529  if ( s != 0 ) {
530  fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
531  return NULL;
532  }
533 
534  /* iterate through results until success */
535  for (rp = result; rp != NULL; rp = rp->ai_next) {
536  coap_address_t addr;
537 
538  if (rp->ai_addrlen <= sizeof(addr.addr)) {
539  coap_address_init(&addr);
540  addr.size = rp->ai_addrlen;
541  memcpy(&addr.addr, rp->ai_addr, rp->ai_addrlen);
542 
543  ctx = coap_new_context(&addr);
544  if (ctx) {
545  /* TODO: output address:port for successful binding */
546  goto finish;
547  }
548  }
549  }
550 
551  fprintf(stderr, "no context available for interface '%s'\n", node);
552 
553  finish:
554  freeaddrinfo(result);
555  return ctx;
556 }
557 
558 int
559 join(coap_context_t *ctx, char *group_name) {
560  struct ipv6_mreq mreq;
561  struct addrinfo *reslocal = NULL, *resmulti = NULL, hints, *ainfo;
562  int result = -1;
563 
564  /* we have to resolve the link-local interface to get the interface id */
565  memset(&hints, 0, sizeof(hints));
566  hints.ai_family = AF_INET6;
567  hints.ai_socktype = SOCK_DGRAM;
568 
569  result = getaddrinfo("::", NULL, &hints, &reslocal);
570  if ( result < 0 ) {
571  perror("join: cannot resolve link-local interface");
572  goto finish;
573  }
574 
575  /* get the first suitable interface identifier */
576  for (ainfo = reslocal; ainfo != NULL; ainfo = ainfo->ai_next) {
577  if ( ainfo->ai_family == AF_INET6 ) {
578  mreq.ipv6mr_interface =
579  ((struct sockaddr_in6 *)ainfo->ai_addr)->sin6_scope_id;
580  break;
581  }
582  }
583 
584  memset(&hints, 0, sizeof(hints));
585  hints.ai_family = AF_INET6;
586  hints.ai_socktype = SOCK_DGRAM;
587 
588  /* resolve the multicast group address */
589  result = getaddrinfo(group_name, NULL, &hints, &resmulti);
590 
591  if ( result < 0 ) {
592  perror("join: cannot resolve multicast address");
593  goto finish;
594  }
595 
596  for (ainfo = resmulti; ainfo != NULL; ainfo = ainfo->ai_next) {
597  if ( ainfo->ai_family == AF_INET6 ) {
598  mreq.ipv6mr_multiaddr =
599  ((struct sockaddr_in6 *)ainfo->ai_addr)->sin6_addr;
600  break;
601  }
602  }
603 
604  result = setsockopt( ctx->sockfd, IPPROTO_IPV6, IPV6_JOIN_GROUP,
605  (char *)&mreq, sizeof(mreq) );
606  if ( result < 0 )
607  perror("join: setsockopt");
608 
609  finish:
610  freeaddrinfo(resmulti);
611  freeaddrinfo(reslocal);
612 
613  return result;
614 }
615 
616 int
617 main(int argc, char **argv) {
618  coap_context_t *ctx;
619  fd_set readfds;
620  struct timeval tv, *timeout;
621  int result;
622  coap_tick_t now;
623  coap_queue_t *nextpdu;
624  char addr_str[NI_MAXHOST] = "::";
625  char port_str[NI_MAXSERV] = "5683";
626  char *group = NULL;
627  int opt;
628  coap_log_t log_level = LOG_WARNING;
629 
630  while ((opt = getopt(argc, argv, "A:g:p:v:")) != -1) {
631  switch (opt) {
632  case 'A' :
633  strncpy(addr_str, optarg, NI_MAXHOST-1);
634  addr_str[NI_MAXHOST - 1] = '\0';
635  break;
636  case 'g' :
637  group = optarg;
638  break;
639  case 'p' :
640  strncpy(port_str, optarg, NI_MAXSERV-1);
641  port_str[NI_MAXSERV - 1] = '\0';
642  break;
643  case 'v' :
644  log_level = strtol(optarg, NULL, 10);
645  break;
646  default:
647  usage( argv[0], PACKAGE_VERSION );
648  exit( 1 );
649  }
650  }
651 
652  coap_set_log_level(log_level);
653 
654  ctx = get_context(addr_str, port_str);
655  if (!ctx)
656  return -1;
657 
658  if (group)
659  join(ctx, group);
660 
661  init_resources(ctx);
662 
663  signal(SIGINT, handle_sigint);
664 
665  while ( !quit ) {
666  FD_ZERO(&readfds);
667  FD_SET( ctx->sockfd, &readfds );
668 
669  nextpdu = coap_peek_next( ctx );
670 
671  coap_ticks(&now);
672  while ( nextpdu && nextpdu->t <= now ) {
673  coap_retransmit( ctx, coap_pop_next( ctx ) );
674  nextpdu = coap_peek_next( ctx );
675  }
676 
677  if ( nextpdu && nextpdu->t <= now + COAP_RESOURCE_CHECK_TIME ) {
678  /* set timeout if there is a pdu to send before our automatic timeout occurs */
679  tv.tv_usec = ((nextpdu->t - now) % COAP_TICKS_PER_SECOND) * 1000000 / COAP_TICKS_PER_SECOND;
680  tv.tv_sec = (nextpdu->t - now) / COAP_TICKS_PER_SECOND;
681  timeout = &tv;
682  } else {
683  tv.tv_usec = 0;
684  tv.tv_sec = COAP_RESOURCE_CHECK_TIME;
685  timeout = &tv;
686  }
687  result = select( FD_SETSIZE, &readfds, 0, 0, timeout );
688 
689  if ( result < 0 ) { /* error */
690  if (errno != EINTR)
691  perror("select");
692  } else if ( result > 0 ) { /* read from socket */
693  if ( FD_ISSET( ctx->sockfd, &readfds ) ) {
694  coap_read( ctx ); /* read received data */
695  coap_dispatch( ctx ); /* and dispatch PDUs from receivequeue */
696  }
697  } else { /* timeout */
698  /* coap_check_resource_list( ctx ); */
699  }
700  }
701 
702  coap_free_context( ctx );
703 
704  return 0;
705 }
void hnd_get_resource(coap_context_t *ctx, struct coap_resource_t *resource, coap_address_t *peer, coap_pdu_t *request, str *token, coap_pdu_t *response)
Definition: rd.c:88
#define COAP_REQUEST_POST
Definition: pdu.h:51
unsigned char token[]
Definition: pdu.h:174
unsigned char coap_key_t[4]
Definition: hashkey.h:19
#define COAP_RESPONSE_CODE(N)
Definition: pdu.h:92
void coap_dispatch(coap_context_t *context)
Dispatches the PDUs from the receive queue in given context.
Definition: net.c:1452
int main(int argc, char **argv)
Definition: rd.c:617
void rd_delete(rd_t *rd)
Definition: rd.c:71
void hnd_post_rd(coap_context_t *ctx, struct coap_resource_t *resource, coap_address_t *peer, coap_pdu_t *request, str *token, coap_pdu_t *response)
Definition: rd.c:349
coap_context_t * get_context(const char *node, const char *port)
Definition: rd.c:517
coap_key_t key
the actual key bytes for this resource
Definition: rd.c:50
static int quit
Definition: rd.c:79
int coap_delete_resource(coap_context_t *context, coap_key_t key)
Deletes a resource identified by key.
Definition: resource.c:451
unsigned short id
Definition: pdu.h:173
#define COAP_REQUEST_GET
Definition: pdu.h:50
int coap_add_token(coap_pdu_t *pdu, size_t len, const unsigned char *data)
Adds token of length len to pdu.
Definition: pdu.c:157
coap_opt_t * coap_check_option(coap_pdu_t *pdu, unsigned char type, coap_opt_iterator_t *oi)
Retrieves the first option of type type from pdu.
Definition: option.c:207
size_t etag_len
actual length of etag
Definition: rd.c:52
#define COAP_MESSAGE_NON
Definition: pdu.h:44
#define COAP_OPTION_ETAG
Definition: pdu.h:59
short coap_log_t
Definition: debug.h:24
int coap_get_data(coap_pdu_t *pdu, size_t *len, unsigned char **data)
Retrieves the length and data pointer of specified PDU.
Definition: pdu.c:261
#define COAP_OPTION_MAXAGE
Definition: pdu.h:66
#define coap_malloc(size)
Definition: mem.h:15
size_t length
Definition: str.h:15
struct rd_t rd_t
rd_t * make_rd(coap_address_t *peer, coap_pdu_t *pdu)
Definition: rd.c:316
#define COAP_OPTION_CONTENT_TYPE
Definition: pdu.h:65
coap_hdr_t * hdr
Definition: pdu.h:209
str data
points to the resource description
Definition: rd.c:55
coap_context_t * coap_new_context(const coap_address_t *listen_addr)
Creates a new coap_context_t object that will hold the CoAP stack status.
Definition: net.c:296
#define RD_ROOT_STR
Definition: rd.c:41
coap_queue_t * coap_peek_next(coap_context_t *context)
Returns the next pdu to send without removing from sendqeue.
Definition: net.c:253
void hnd_delete_resource(coap_context_t *ctx, struct coap_resource_t *resource, coap_address_t *peer, coap_pdu_t *request, str *token, coap_pdu_t *response)
Definition: rd.c:183
#define LOCSIZE
#define debug(...)
Definition: debug.h:55
coap_tick_t t
when to send PDU for the next time
Definition: net.h:54
#define HASH_DELETE(hh, head, delptr)
Definition: uthash.h:194
#define COAP_INVALID_TID
Definition: pdu.h:156
coap_attr_t * coap_add_attr(coap_resource_t *resource, const unsigned char *name, size_t nlen, const unsigned char *val, size_t vlen, int flags)
Registers a new attribute with the given resource.
Definition: resource.c:341
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
unsigned int code
Definition: pdu.h:172
#define coap_free(size)
Definition: mem.h:16
Header structure for CoAP PDUs.
Definition: pdu.h:206
coap_key_t key
the actual key bytes for this resource
Definition: resource.h:75
#define COAP_ATTR_FLAGS_RELEASE_VALUE
Definition: resource.h:50
void usage(const char *program, const char *version)
Definition: rd.c:500
unsigned int coap_encode_var_bytes(unsigned char *buf, unsigned int val)
Encodes multiple-length byte sequences.
Definition: encode.c:34
static void coap_register_handler(coap_resource_t *resource, unsigned char method, coap_method_handler_t handler)
Registers the specified handler as message handler for the request type method.
Definition: resource.h:227
void coap_add_resource(coap_context_t *context, coap_resource_t *resource)
Registers the given resource for context.
Definition: resource.c:440
int coap_split_path(const unsigned char *s, size_t length, unsigned char *buf, size_t *buflen)
Splits the given URI path into segments.
Definition: uri.c:354
Iterator to run through PDU options.
Definition: option.h:169
#define COAP_MESSAGE_CON
Definition: pdu.h:43
#define COAP_MEDIATYPE_APPLICATION_LINK_FORMAT
Definition: pdu.h:142
size_t coap_add_option(coap_pdu_t *pdu, unsigned short type, unsigned int len, const unsigned char *data)
de-duplicate code with coap_add_option_later
Definition: pdu.c:175
generic resource handling
void add_source_address(struct coap_resource_t *resource, coap_address_t *peer)
Definition: rd.c:260
void hnd_put_resource(coap_context_t *ctx, struct coap_resource_t *resource, coap_address_t *peer, coap_pdu_t *request, str *token, coap_pdu_t *response)
Definition: rd.c:109
#define COAP_MESSAGE_ACK
Definition: pdu.h:45
#define COAP_OPT_SIZE(opt)
Definition: option.h:65
int coap_add_data(coap_pdu_t *pdu, unsigned int len, const unsigned char *data)
Adds given data to the pdu that is passed as first parameter.
Definition: pdu.c:238
#define COAP_REQUEST_DELETE
Definition: pdu.h:53
#define min(a, b)
Definition: rd.c:45
coap_tid_t coap_send(coap_context_t *context, const coap_address_t *dst, coap_pdu_t *pdu)
Sends a non-confirmed CoAP message to given destination.
Definition: net.c:653
#define COAP_DEFAULT_PORT
Definition: pdu.h:24
int join(coap_context_t *ctx, char *group_name)
Definition: rd.c:559
int parse_param(unsigned char *search, size_t search_len, unsigned char *data, size_t data_len, str *result)
Definition: rd.c:216
#define COAP_OPTION_LOCATION_PATH
Definition: pdu.h:62
void coap_delete_pdu(coap_pdu_t *pdu)
Definition: pdu.c:143
static void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
Definition: address.h:134
coap_pdu_t * coap_pdu_init(unsigned char type, unsigned char code, unsigned short id, size_t size)
Creates a new CoAP PDU of given size (must be large enough to hold the basic CoAP message header (coa...
Definition: pdu.c:82
rd_t * resources
Definition: rd.c:58
int coap_hash_path(const unsigned char *path, size_t len, coap_key_t key)
Calculates a hash over the given path and stores the result in key.
Definition: uri.c:454
Definition: str.h:14
#define BUFSIZE
void coap_free_context(coap_context_t *context)
Definition: net.c:419
#define COAP_OPT_LENGTH(opt)
Definition: option.h:305
unsigned int token_length
Definition: pdu.h:169
coap_resource_t * coap_resource_init(const unsigned char *uri, size_t len, int flags)
Creates a new resource object and initializes the link field to the string of length len...
Definition: resource.c:307
void hnd_get_rd(coap_context_t *ctx, struct coap_resource_t *resource, coap_address_t *peer, coap_pdu_t *request, str *token, coap_pdu_t *response)
Definition: rd.c:201
#define HASH_ADD(hh, head, fieldname, keylen_in, add)
Definition: uthash.h:149
void coap_set_log_level(coap_log_t level)
Sets the log level to the specified value.
Definition: debug.c:46
Definition: rd.c:48
#define COAP_OPTION_URI_QUERY
Definition: pdu.h:67
unsigned int type
Definition: pdu.h:170
unsigned char coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
Definition: option.h:26
#define COAP_REQUEST_PUT
Definition: pdu.h:52
#define PACKAGE_VERSION
Definition: config.h:108
unsigned char etag[8]
ETag for current description.
Definition: rd.c:53
rd_t * rd_new()
Definition: rd.c:61
int coap_read(coap_context_t *ctx)
Reads data from the network and tries to parse as CoAP PDU.
Definition: net.c:831
void handle_sigint(int signum)
Definition: rd.c:83
#define RD_ROOT_SIZE
Definition: rd.c:42
#define HASH_FIND(hh, head, keyptr, keylen, out)
Definition: uthash.h:84
unsigned char * s
Definition: str.h:16
UT_hash_handle hh
hash handle (for internal use only)
Definition: rd.c:49
#define COAP_RESOURCE_FLAGS_RELEASE_URI
Definition: resource.h:59
coap_tid_t coap_retransmit(coap_context_t *context, coap_queue_t *node)
Handles retransmissions of confirmable messages.
Definition: net.c:771
coap_queue_t * coap_pop_next(coap_context_t *context)
Returns the next pdu to send and removes it from the sendqeue.
Definition: net.c:261
void init_resources(coap_context_t *ctx)
Definition: rd.c:484
#define COAP_OPT_VALUE(opt)
Definition: option.h:318
The CoAP stack's global state is stored in a coap_context_t object.
Definition: net.h:97
#define COAP_RESOURCE_CHECK_TIME
Definition: rd.c:39