1 /* coap -- simple implementation of the Constrained Application Protocol (CoAP)
2 * as defined in draft-ietf-core-coap
4 * Copyright (C) 2010--2013 Olaf Bergmann <bergmann@tzi.org>
6 * This file is part of the CoAP library libcoap. Please see
7 * README for terms of use.
13 * @brief CoRE resource directory
15 * @see http://tools.ietf.org/id/draft-shelby-core-resource-directory
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>
39 #define COAP_RESOURCE_CHECK_TIME 2
41 #define RD_ROOT_STR ((unsigned char *)"rd")
42 #define RD_ROOT_SIZE 2
45 #define min(a,b) ((a) < (b) ? (a) : (b))
49 UT_hash_handle hh; /**< hash handle (for internal use only) */
50 coap_key_t key; /**< the actual key bytes for this resource */
52 size_t etag_len; /**< actual length of @c etag */
53 unsigned char etag[8]; /**< ETag for current description */
55 str data; /**< points to the resource description */
58 rd_t *resources = NULL;
63 rd = (rd_t *)coap_malloc(sizeof(rd_t));
65 memset(rd, 0, sizeof(rd_t));
73 coap_free(rd->data.s);
78 /* temporary storage for dynamic resource representations */
81 /* SIGINT handler: set quit to 1 for graceful termination */
83 handle_sigint(int signum) {
88 hnd_get_resource(coap_context_t *ctx, struct coap_resource_t *resource,
89 coap_address_t *peer, coap_pdu_t *request, str *token,
90 coap_pdu_t *response) {
94 HASH_FIND(hh, resources, resource->key, sizeof(coap_key_t), rd);
96 response->hdr->code = COAP_RESPONSE_CODE(205);
98 coap_add_option(response, COAP_OPTION_CONTENT_TYPE,
99 coap_encode_var_bytes(buf, COAP_MEDIATYPE_APPLICATION_LINK_FORMAT), buf);
101 if (rd && rd->etag_len)
102 coap_add_option(response, COAP_OPTION_ETAG, rd->etag_len, rd->etag);
104 if (rd && rd->data.s)
105 coap_add_data(response, rd->data.length, rd->data.s);
109 hnd_put_resource(coap_context_t *ctx, struct coap_resource_t *resource,
110 coap_address_t *peer, coap_pdu_t *request, str *token,
111 coap_pdu_t *response) {
113 response->hdr->code = COAP_RESPONSE_CODE(501);
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)
120 ? COAP_MESSAGE_ACK : COAP_MESSAGE_NON;
122 unsigned char code; /* result code */
126 HASH_FIND(hh, resources, resource->key, sizeof(coap_key_t), 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) {
133 if (coap_get_data(request, &tmp.length, &data)) {
135 tmp.s = (unsigned char *)coap_malloc(tmp.length);
137 debug("hnd_put_rd: cannot allocate storage for new rd\n");
138 code = COAP_RESPONSE_CODE(503);
142 coap_free(rd->data.s);
144 rd->data.length = tmp.length;
145 memcpy(rd->data.s, data, rd->data.length);
150 rd->etag_len = min(COAP_OPT_LENGTH(etag), sizeof(rd->etag));
151 memcpy(rd->etag, COAP_OPT_VALUE(etag), rd->etag_len);
154 code = COAP_RESPONSE_CODE(204);
155 /* FIXME: update lifetime */
159 code = COAP_RESPONSE_CODE(503);
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);
167 debug("cannot create response for message %d\n", request->hdr->id);
171 if (request->hdr->token_length)
172 coap_add_token(response, request->hdr->token_length, request->hdr->token);
174 if (coap_send(ctx, peer, response) == COAP_INVALID_TID) {
175 debug("hnd_get_rd: cannot send response for message %d\n",
178 coap_delete_pdu(response);
183 hnd_delete_resource(coap_context_t *ctx, struct coap_resource_t *resource,
184 coap_address_t *peer, coap_pdu_t *request, str *token,
185 coap_pdu_t *response) {
188 HASH_FIND(hh, resources, resource->key, sizeof(coap_key_t), rd);
190 HASH_DELETE(hh, resources, rd);
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);
197 response->hdr->code = COAP_RESPONSE_CODE(202);
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];
206 response->hdr->code = COAP_RESPONSE_CODE(205);
208 coap_add_option(response, COAP_OPTION_CONTENT_TYPE,
209 coap_encode_var_bytes(buf, COAP_MEDIATYPE_APPLICATION_LINK_FORMAT), buf);
211 coap_add_option(response, COAP_OPTION_MAXAGE,
212 coap_encode_var_bytes(buf, 0x2ffff), buf);
216 parse_param(unsigned char *search, size_t search_len,
217 unsigned char *data, size_t data_len, str *result) {
220 memset(result, 0, sizeof(str));
225 while (search_len <= data_len) {
227 /* handle parameter if found */
228 if (memcmp(search, data, search_len) == 0) {
230 data_len -= search_len;
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 != '=') {
238 if (data_len > 1 && result) {
239 /* value begins after '=' */
242 while (--data_len && *data != '&') {
243 ++data; result->length++;
251 /* otherwise proceed to next */
252 while (--data_len && *data++ != '&')
260 add_source_address(struct coap_resource_t *resource, coap_address_t *peer) {
265 buf = (char *)coap_malloc(BUFSIZE);
271 switch(peer->addr.sa.sa_family) {
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]);
298 if (peer->addr.sin6.sin6_port != htons(COAP_DEFAULT_PORT)) {
300 snprintf(buf + n, BUFSIZE - n, ":%d", peer->addr.sin6.sin6_port);
310 coap_add_attr(resource, (unsigned char *)"A", 1, (unsigned char *)buf, n, COAP_ATTR_FLAGS_RELEASE_VALUE);
316 make_rd(coap_address_t *peer, coap_pdu_t *pdu) {
319 coap_opt_iterator_t opt_iter;
325 debug("hnd_get_rd: cannot allocate storage for rd\n");
329 if (coap_get_data(pdu, &rd->data.length, &data)) {
330 rd->data.s = (unsigned char *)coap_malloc(rd->data.length);
332 debug("hnd_get_rd: cannot allocate storage for rd->data\n");
336 memcpy(rd->data.s, data, rd->data.length);
339 etag = coap_check_option(pdu, COAP_OPTION_ETAG, &opt_iter);
341 rd->etag_len = min(COAP_OPT_LENGTH(etag), sizeof(rd->etag));
342 memcpy(rd->etag, COAP_OPT_VALUE(etag), rd->etag_len);
349 hnd_post_rd(coap_context_t *ctx, struct coap_resource_t *resource,
350 coap_address_t *peer, coap_pdu_t *request, str *token,
351 coap_pdu_t *response) {
353 coap_opt_iterator_t opt_iter;
358 str h = {0, NULL}, ins = {0, NULL}, rt = {0, NULL}, lt = {0, NULL}; /* store query parameters */
361 loc = (unsigned char *)coap_malloc(LOCSIZE);
363 response->hdr->code = COAP_RESPONSE_CODE(500);
366 memcpy(loc, RD_ROOT_STR, RD_ROOT_SIZE);
368 loc_size = RD_ROOT_SIZE;
369 loc[loc_size++] = '/';
371 /* store query parameters for later use */
372 query = coap_check_option(request, COAP_OPTION_URI_QUERY, &opt_iter);
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), <);
380 parse_param((unsigned char *)"rt", 2,
381 COAP_OPT_VALUE(query), COAP_OPT_LENGTH(query), &rt);
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);
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);
395 } else { /* generate node identifier */
397 snprintf((char *)(loc + loc_size), LOCSIZE - loc_size - 1,
398 "%x", request->hdr->id);
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);
411 snprintf((char *)(loc + loc_size), LOCSIZE - loc_size - 1,
418 * - use lt to check expiration
421 r = coap_resource_init(loc, loc_size, COAP_RESOURCE_FLAGS_RELEASE_URI);
422 coap_register_handler(r, COAP_REQUEST_GET, hnd_get_resource);
423 coap_register_handler(r, COAP_REQUEST_PUT, hnd_put_resource);
424 coap_register_handler(r, COAP_REQUEST_DELETE, hnd_delete_resource);
427 buf = (unsigned char *)coap_malloc(ins.length + 2);
429 /* add missing quotes */
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);
438 buf = (unsigned char *)coap_malloc(rt.length + 2);
440 /* add missing quotes */
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);
448 add_source_address(r, peer);
452 rd = make_rd(peer, request);
454 coap_hash_path(loc, loc_size, rd->key);
455 HASH_ADD(hh, resources, key, sizeof(coap_key_t), rd);
457 /* FIXME: send error response and delete r */
461 coap_add_resource(ctx, r);
464 /* create response */
466 response->hdr->code = COAP_RESPONSE_CODE(201);
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);
474 nseg = coap_split_path(loc, loc_size, b, &buflen);
476 coap_add_option(response, COAP_OPTION_LOCATION_PATH,
477 COAP_OPT_LENGTH(b), COAP_OPT_VALUE(b));
478 b += COAP_OPT_SIZE(b);
484 init_resources(coap_context_t *ctx) {
487 r = coap_resource_init(RD_ROOT_STR, RD_ROOT_SIZE, 0);
488 coap_register_handler(r, COAP_REQUEST_GET, hnd_get_rd);
489 coap_register_handler(r, COAP_REQUEST_POST, hnd_post_rd);
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);
495 coap_add_resource(ctx, r);
500 usage( const char *program, const char *version) {
503 p = strrchr( program, '/' );
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 );
517 get_context(const char *node, const char *port) {
518 coap_context_t *ctx = NULL;
520 struct addrinfo hints;
521 struct addrinfo *result, *rp;
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;
528 s = getaddrinfo(node, port, &hints, &result);
530 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
534 /* iterate through results until success */
535 for (rp = result; rp != NULL; rp = rp->ai_next) {
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);
543 ctx = coap_new_context(&addr);
545 /* TODO: output address:port for successful binding */
551 fprintf(stderr, "no context available for interface '%s'\n", node);
554 freeaddrinfo(result);
559 join(coap_context_t *ctx, char *group_name) {
560 struct ipv6_mreq mreq;
561 struct addrinfo *reslocal = NULL, *resmulti = NULL, hints, *ainfo;
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;
569 result = getaddrinfo("::", NULL, &hints, &reslocal);
571 perror("join: cannot resolve link-local interface");
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;
584 memset(&hints, 0, sizeof(hints));
585 hints.ai_family = AF_INET6;
586 hints.ai_socktype = SOCK_DGRAM;
588 /* resolve the multicast group address */
589 result = getaddrinfo(group_name, NULL, &hints, &resmulti);
592 perror("join: cannot resolve multicast address");
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;
604 result = setsockopt( ctx->sockfd, IPPROTO_IPV6, IPV6_JOIN_GROUP,
605 (char *)&mreq, sizeof(mreq) );
607 perror("join: setsockopt");
610 freeaddrinfo(resmulti);
611 freeaddrinfo(reslocal);
617 main(int argc, char **argv) {
620 struct timeval tv, *timeout;
623 coap_queue_t *nextpdu;
624 char addr_str[NI_MAXHOST] = "::";
625 char port_str[NI_MAXSERV] = "5683";
628 coap_log_t log_level = LOG_WARNING;
630 while ((opt = getopt(argc, argv, "A:g:p:v:")) != -1) {
633 strncpy(addr_str, optarg, NI_MAXHOST-1);
634 addr_str[NI_MAXHOST - 1] = '\0';
640 strncpy(port_str, optarg, NI_MAXSERV-1);
641 port_str[NI_MAXSERV - 1] = '\0';
644 log_level = strtol(optarg, NULL, 10);
647 usage( argv[0], PACKAGE_VERSION );
652 coap_set_log_level(log_level);
654 ctx = get_context(addr_str, port_str);
663 signal(SIGINT, handle_sigint);
667 FD_SET( ctx->sockfd, &readfds );
669 nextpdu = coap_peek_next( ctx );
672 while ( nextpdu && nextpdu->t <= now ) {
673 coap_retransmit( ctx, coap_pop_next( ctx ) );
674 nextpdu = coap_peek_next( ctx );
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;
684 tv.tv_sec = COAP_RESOURCE_CHECK_TIME;
687 result = select( FD_SETSIZE, &readfds, 0, 0, timeout );
689 if ( result < 0 ) { /* error */
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 */
697 } else { /* timeout */
698 /* coap_check_resource_list( ctx ); */
702 coap_free_context( ctx );