diff --git a/configure.in b/configure.in index 1e92a3a3..c40860f8 100644 --- a/configure.in +++ b/configure.in @@ -1,24 +1,13 @@ # -*- Autoconf -*- # Process this file with autoconf to produce a configure script. # -# Copyright (C) 2010 Olaf Bergmann +# Copyright (C) 2010,2011 Olaf Bergmann # -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# This file is part of the CoAP library libcoap. Please see +# README for terms of use. AC_PREREQ([2.65]) -AC_INIT([libcoap], [0.1.9]) +AC_INIT([libcoap], [0.2.0]) AC_CONFIG_SRCDIR([coap.h]) # Checks for programs. @@ -35,6 +24,12 @@ AC_CHECK_LIB([coap], [coap_new_pdu]) AC_SEARCH_LIBS([gethostbyname], [nsl]) AC_SEARCH_LIBS([socket], [socket]) +# configuration options that may change compile flags +AC_ARG_WITH(debug, + [AS_HELP_STRING([--without-debug],[disable all debug output and assertions])], + [CPPFLAGS="${CPPFLAGS} -DNDEBUG"], + []) + # Checks for header files. AC_CHECK_HEADERS([assert.h arpa/inet.h limits.h netdb.h netinet/in.h stdlib.h string.h strings.h sys/socket.h unistd.h]) diff --git a/debug.c b/debug.c index e5574feb..a1f74154 100644 --- a/debug.c +++ b/debug.c @@ -1,20 +1,9 @@ /* debug.c -- debug utilities * - * Copyright (C) 2010 Olaf Bergmann + * Copyright (C) 2010,2011 Olaf Bergmann * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * This file is part of the CoAP library libcoap. Please see + * README for terms of use. */ #include diff --git a/debug.h b/debug.h index 6ed6e93d..88f74066 100644 --- a/debug.h +++ b/debug.h @@ -1,20 +1,9 @@ /* debug.h -- debug utilities * - * Copyright (C) 2010 Olaf Bergmann + * Copyright (C) 2010,2011 Olaf Bergmann * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * This file is part of the CoAP library libcoap. Please see + * README for terms of use. */ #ifndef _COAP_DEBUG_H_ diff --git a/encode.c b/encode.c index fac9e4aa..223b079d 100644 --- a/encode.c +++ b/encode.c @@ -1,20 +1,9 @@ /* encode.c -- encoding and decoding of CoAP data types * - * Copyright (C) 2010 Olaf Bergmann , Carsten Bormann + * Copyright (C) 2010,2011 Olaf Bergmann * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * This file is part of the CoAP library libcoap. Please see + * README for terms of use. */ #ifndef NDEBUG diff --git a/encode.h b/encode.h index 286ea2da..53d69243 100644 --- a/encode.h +++ b/encode.h @@ -1,20 +1,9 @@ /* encode.h -- encoding and decoding of CoAP data types * - * Copyright (C) 2010 Olaf Bergmann , Carsten Bormann + * Copyright (C) 2010,2011 Olaf Bergmann * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * This file is part of the CoAP library libcoap. Please see + * README for terms of use. */ #ifndef _COAP_ENCODE_H_ diff --git a/examples/client.c b/examples/client.c index 66c7429c..b7f5b157 100644 --- a/examples/client.c +++ b/examples/client.c @@ -105,8 +105,6 @@ coap_new_request( method_t m, coap_list_t *options ) { buf, BUFSIZE); while (res--) { - printf("add option %d\n", COAP_OPTION_KEY(*(coap_option *)opt->data)); - coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *)opt->data), COAP_OPT_LENGTH(*(coap_opt_t *)buf), COAP_OPT_VALUE(*(coap_opt_t *)buf)); @@ -120,8 +118,6 @@ coap_new_request( method_t m, coap_list_t *options ) { buf, BUFSIZE); while (res--) { - printf("add option %d\n", COAP_OPTION_KEY(*(coap_option *)opt->data)); - coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *)opt->data), COAP_OPT_LENGTH(*(coap_opt_t *)buf), COAP_OPT_VALUE(*(coap_opt_t *)buf)); @@ -130,7 +126,6 @@ coap_new_request( method_t m, coap_list_t *options ) { } break; default: - printf("add option %d\n", COAP_OPTION_KEY(*(coap_option *)opt->data)); coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *)opt->data), COAP_OPTION_LENGTH(*(coap_option *)opt->data), COAP_OPTION_DATA(*(coap_option *)opt->data)); @@ -203,6 +198,7 @@ void message_handler( coap_context_t *ctx, coap_queue_t *node, void *data) { coap_pdu_t *pdu = NULL; coap_opt_t *block, *ct, *sub; + coap_opt_iterator_t opt_iter; unsigned int blocknr; unsigned char buf[4]; coap_list_t *option; @@ -230,7 +226,7 @@ message_handler( coap_context_t *ctx, coap_queue_t *node, void *data) { switch (node->pdu->hdr->code) { case COAP_RESPONSE_200: /* got some data, check if block option is set */ - block = coap_check_option( node->pdu, COAP_OPTION_BLOCK ); + block = coap_check_option(node->pdu, COAP_OPTION_BLOCK, &opt_iter); if ( !block ) { /* There is no block option set, just read the data and we are done. */ if ( coap_get_data( node->pdu, &len, &databuf ) ) { @@ -273,7 +269,7 @@ message_handler( coap_context_t *ctx, coap_queue_t *node, void *data) { pdu->hdr->id = node->pdu->hdr->id; /* copy transaction id from response */ /* get content type from response */ - ct = coap_check_option( node->pdu, COAP_OPTION_CONTENT_TYPE ); + ct = coap_check_option(node->pdu, COAP_OPTION_CONTENT_TYPE, &opt_iter); if ( ct ) { coap_add_option( pdu, COAP_OPTION_CONTENT_TYPE, COAP_OPT_LENGTH(*ct),COAP_OPT_VALUE(*ct) ); @@ -331,7 +327,7 @@ message_handler( coap_context_t *ctx, coap_queue_t *node, void *data) { } /* our job is done, we can exit at any time */ - sub = coap_check_option( node->pdu, COAP_OPTION_SUBSCRIPTION ); + sub = coap_check_option(node->pdu, COAP_OPTION_SUBSCRIPTION, &opt_iter); #ifndef NDEBUG if ( sub ) { debug("message_handler: Subscription-Lifetime is %d\n", diff --git a/examples/server.c b/examples/server.c index 3f13c41f..3be466fa 100644 --- a/examples/server.c +++ b/examples/server.c @@ -1,21 +1,10 @@ /* coap -- simple implementation of the Constrained Application Protocol (CoAP) - * as defined in draft-ietf-core-coap-01 + * as defined in draft-ietf-core-coap * - * Copyright (C) 2010 Olaf Bergmann + * Copyright (C) 2010,2011 Olaf Bergmann * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * This file is part of the CoAP library libcoap. Please see + * README for terms of use. */ #include @@ -91,12 +80,13 @@ new_response( coap_context_t *ctx, coap_queue_t *node, unsigned int code ) { void add_contents( coap_pdu_t *pdu, unsigned int mediatype, unsigned int len, unsigned char *data ) { - unsigned char ct = COAP_MEDIATYPE_APPLICATION_LINK_FORMAT; + unsigned char ct[2]; if (!pdu) return; /* add content-encoding */ - coap_add_option(pdu, COAP_OPTION_CONTENT_TYPE, 1, &ct); + coap_add_option(pdu, COAP_OPTION_CONTENT_TYPE, + coap_encode_var_bytes(ct, mediatype), ct); /* TODO: handle fragmentation (check result code) */ coap_add_data(pdu, len, data); @@ -118,19 +108,19 @@ coap_next_option(coap_pdu_t *pdu, coap_opt_t *opt) { int mediatype_matches(coap_pdu_t *pdu, unsigned char mediatype) { - coap_opt_t *accept; - int t; + coap_opt_iterator_t opt_iter; - if ( mediatype == COAP_MEDIATYPE_ANY || - (accept = coap_check_option(pdu, COAP_OPTION_ACCEPT)) == NULL) + /* FIXME: handle Accept.Only vs. Accept.Prefer, see draft-bormann-coap-misc-08 */ + if (mediatype == COAP_MEDIATYPE_ANY || + coap_check_option(pdu, COAP_OPTION_ACCEPT, &opt_iter) == NULL) return 1; - /* Check the byte sequence in the option value for any occurence of - * mediatype. */ - for (t = 0; t < COAP_OPT_LENGTH(*accept); ++t) { - if ( COAP_OPT_VALUE(*accept)[t] == mediatype ) - return 1; - } + do { + if (coap_decode_var_bytes(COAP_OPT_VALUE(*opt_iter.option), + COAP_OPT_LENGTH(*opt_iter.option)) + == mediatype) + return 1; + } while (coap_option_next(&opt_iter) && opt_iter.type == COAP_OPTION_ACCEPT); return 0; } @@ -210,6 +200,7 @@ handle_get(coap_context_t *ctx, coap_queue_t *node, void *data) { coap_uri_t uri; coap_resource_t *resource; coap_opt_t *block, *tok, *sub; + coap_opt_iterator_t opt_iter; str token; unsigned int blklen, blk; int code, finished = 1; @@ -235,7 +226,7 @@ handle_get(coap_context_t *ctx, coap_queue_t *node, void *data) { /* add token to any response */ token.length = 0; - tok = coap_check_option(node->pdu, COAP_OPTION_TOKEN); + tok = coap_check_option(node->pdu, COAP_OPTION_TOKEN, &opt_iter); if (tok) { COAP_SET_STR(&token, COAP_OPT_LENGTH(*tok), COAP_OPT_VALUE(*tok)); } @@ -253,13 +244,13 @@ handle_get(coap_context_t *ctx, coap_queue_t *node, void *data) { } /* check if requested mediatypes match */ - if ( coap_check_option(node->pdu, COAP_OPTION_ACCEPT) + if (coap_check_option(node->pdu, COAP_OPTION_ACCEPT, &opt_iter) && !mediatype_matches(node->pdu, resource->mediatype) ) { debug("media type mismatch\n"); return new_response(ctx, node, COAP_RESPONSE_415); } - block = coap_check_option(node->pdu, COAP_OPTION_BLOCK); + block = coap_check_option(node->pdu, COAP_OPTION_BLOCK, &opt_iter); if ( block ) { blk = coap_decode_var_bytes(COAP_OPT_VALUE(*block), COAP_OPT_LENGTH(*block)); @@ -301,7 +292,8 @@ handle_get(coap_context_t *ctx, coap_queue_t *node, void *data) { if ( blklen > 0 ) { /* add content-type */ if ( mediatype != COAP_MEDIATYPE_ANY ) - coap_add_option(pdu, COAP_OPTION_CONTENT_TYPE, 1, &mediatype); + coap_add_option(pdu, COAP_OPTION_CONTENT_TYPE, + coap_encode_var_bytes(optbuf, mediatype), optbuf); /* set Max-age option unless resource->maxage is zero */ if (resource->maxage) { @@ -316,7 +308,7 @@ handle_get(coap_context_t *ctx, coap_queue_t *node, void *data) { } /* handle subscription if requested */ - sub = coap_check_option( node->pdu, COAP_OPTION_SUBSCRIPTION ); + sub = coap_check_option(node->pdu, COAP_OPTION_SUBSCRIPTION, &opt_iter); if ( sub ) { duration = COAP_PSEUDOFP_DECODE_8_4(*COAP_OPT_VALUE(*sub)); debug("*** add subscription for %d seconds\n", duration); @@ -325,13 +317,16 @@ handle_get(coap_context_t *ctx, coap_queue_t *node, void *data) { /* refresh only if already subscribed */ subscription = - coap_find_subscription(ctx, coap_uri_hash(&uri), &(node->remote), + coap_find_subscription(ctx, coap_uri_hash(&uri), + &node->remote.addr.sa, tok ? &token : NULL); if (subscription) { /* refresh existing subscription */ subscription->expires = time(NULL)+duration; } else { /* add new subscription */ - subscription = coap_new_subscription(ctx, &uri, &(node->remote), + subscription = coap_new_subscription(ctx, &uri, + &node->remote.addr.sa, + node->remote.size, time(NULL)+duration); if (subscription) { if (token.length) { @@ -402,6 +397,7 @@ coap_pdu_t * handle_put(coap_context_t *ctx, coap_queue_t *node, void *data) { coap_uri_t uri; coap_opt_t *tok; + coap_opt_iterator_t opt_iter; coap_pdu_t *pdu; coap_resource_t *resource; ssize_t written, length; @@ -439,7 +435,7 @@ handle_put(coap_context_t *ctx, coap_queue_t *node, void *data) { if (written < length) return new_response(ctx, node, COAP_RESPONSE_500); - tok = coap_check_option(node->pdu, COAP_OPTION_CONTENT_TYPE); + tok = coap_check_option(node->pdu, COAP_OPTION_CONTENT_TYPE, &opt_iter); resource->mediatype = tok ? *COAP_OPT_VALUE(*tok) : COAP_MEDIATYPE_ANY; resource->dirty = 1; /* mark for notification of observers */ return pdu; @@ -457,6 +453,7 @@ handle_post(coap_context_t *ctx, coap_queue_t *node, void *data) { coap_pdu_t *pdu; coap_resource_t *r; coap_opt_t *tok; + coap_opt_iterator_t opt_iter; ssize_t written, length; int namelen; char name[60]; @@ -473,7 +470,7 @@ handle_post(coap_context_t *ctx, coap_queue_t *node, void *data) { if ( !(r = coap_malloc( sizeof(coap_resource_t) ))) return new_response(ctx, node, COAP_RESPONSE_500); - tok = coap_check_option(node->pdu, COAP_OPTION_CONTENT_TYPE); + tok = coap_check_option(node->pdu, COAP_OPTION_CONTENT_TYPE, &opt_iter); /* Create a new resource to store the given contents in the local * file system. We restrict the storage area to DATASINK_PREFIX. @@ -517,12 +514,12 @@ handle_post(coap_context_t *ctx, coap_queue_t *node, void *data) { pdu = new_response(ctx, node, COAP_RESPONSE_201); /* add location header */ - coap_add_option(pdu, COAP_OPTION_LOCATION, + coap_add_option(pdu, COAP_OPTION_LOCATION_PATH, namelen, (unsigned char *)name); /* we do not need the request URI, only a token, if specified */ - tok = coap_check_option(node->pdu, COAP_OPTION_TOKEN); + tok = coap_check_option(node->pdu, COAP_OPTION_TOKEN, &opt_iter); if (tok) coap_add_option(pdu, COAP_OPTION_TOKEN, COAP_OPT_LENGTH(*tok), COAP_OPT_VALUE(*tok)); @@ -536,6 +533,7 @@ handle_delete(coap_context_t *ctx, coap_queue_t *node, void *data) { coap_pdu_t *pdu; coap_resource_t *r; coap_opt_t *tok; + coap_opt_iterator_t opt_iter; static char filename[FILENAME_MAX+1]; if ( !coap_get_request_uri( node->pdu, &uri ) ) @@ -562,25 +560,13 @@ handle_delete(coap_context_t *ctx, coap_queue_t *node, void *data) { unlink(filename); /* create the response */ - pdu = new_response(ctx, node, COAP_RESPONSE_200); + pdu = new_response(ctx, node, COAP_RESPONSE_CODE(202)); - if (uri.na.length) - coap_add_option(pdu, COAP_OPTION_URI_AUTHORITY, - uri.na.length, uri.na.s); - - if (uri.path.length) - coap_add_option(pdu, COAP_OPTION_URI_PATH, - uri.path.length, uri.path.s); - - tok = coap_check_option(node->pdu, COAP_OPTION_TOKEN); + tok = coap_check_option(node->pdu, COAP_OPTION_TOKEN, &opt_iter); if (tok) coap_add_option(pdu, COAP_OPTION_TOKEN, COAP_OPT_LENGTH(*tok), COAP_OPT_VALUE(*tok)); - if (uri.query.length) - coap_add_option(pdu, COAP_OPTION_URI_QUERY, - uri.query.length, uri.query.s); - coap_delete_resource(ctx, coap_uri_hash(&uri)); return pdu; @@ -624,16 +610,16 @@ message_handler(coap_context_t *ctx, coap_queue_t *node, void *data) { break; default: if ( node->pdu->hdr->type == COAP_MESSAGE_CON ) { - if ( node->pdu->hdr->code >= COAP_RESPONSE_100 ) - pdu = new_rst( ctx, node, COAP_RESPONSE_500 ); - else { + if ( node->pdu->hdr->code < 32 ) { /* it is a request */ debug("request method not implemented: %u\n", node->pdu->hdr->code); pdu = new_rst( ctx, node, COAP_RESPONSE_405 ); - } + } else /* it is a response */ + pdu = new_rst( ctx, node, COAP_RESPONSE_500 ); } } - if ( pdu && coap_send( ctx, &node->remote, pdu ) == COAP_INVALID_TID ) { + if (pdu && coap_send(ctx, &node->remote.addr.sa, node->remote.size, pdu) + == COAP_INVALID_TID) { debug("message_handler: error sending reponse"); coap_delete_pdu(pdu); } @@ -649,7 +635,7 @@ usage( const char *program, const char *version) { program = ++p; fprintf( stderr, "%s v%s -- a small CoAP implementation\n" - "(c) 2010 Olaf Bergmann \n\n" + "(c) 2010,2011 Olaf Bergmann \n\n" "usage: %s [-A address] [-g group] [-p port]\n\n" "\t-A address\tinterface address to bind to\n" "\t-g group\tjoin the given multicast group\n" @@ -741,11 +727,11 @@ print_link(coap_resource_t *resource, unsigned char *buf, size_t buflen) { } if (resource->name) { - if (buflen - n < resource->name->length + 5) /* include trailing quote */ + if (buflen - n < resource->name->length + 9) /* include trailing quote */ return -1; - memcpy(buf + n, ";n=\"", 4); - n += 4; + memcpy(buf + n, ";title=\"", 8); + n += 8; memcpy(buf + n, resource->name->s, resource->name->length); n += resource->name->length; @@ -759,6 +745,19 @@ print_link(coap_resource_t *resource, unsigned char *buf, size_t buflen) { buf[n++] = '"'; } +#if 0 + if (resource->rt) { + if (buflen - n < resource->rt->length + 6) /* include trailing quote */ + return -1; + + memcpy(buf + n, ";rt=\"", 5); + n += 5; + memcpy(buf + n, resource->rt->s, resource->rt->length); + n += resource->rt->length; + buf[n++] = '"'; + } + #endif + return n; } @@ -843,7 +842,6 @@ resource_time(coap_uri_t *uri, *mediatype = COAP_MEDIATYPE_TEXT_PLAIN; maxlen = strftime(resource_buf, sizeof(b), "%b %d %H:%M:%S", tlocal); break; - case COAP_MEDIATYPE_TEXT_XML : case COAP_MEDIATYPE_APPLICATION_XML : maxlen = strftime(resource_buf, sizeof(b), "\n %Y-%m-%d\n \n %S\n", tlocal); break; @@ -912,8 +910,6 @@ _resource_from_dir(char *filename, case COAP_MEDIATYPE_APPLICATION_LINK_FORMAT: *mediatype = COAP_MEDIATYPE_APPLICATION_LINK_FORMAT; break; - /* case COAP_MEDIATYPE_TEXT_PLAIN: */ - /* break; */ default: *buflen = 0; return COAP_RESPONSE_415; @@ -1056,6 +1052,7 @@ resource_from_file(coap_uri_t *uri, #define RESOURCE_SET_URI(r,st) \ (r)->uri = coap_new_uri((const unsigned char *)(st), strlen(st)); +/** \bug memory leak as the new string is not released by coap_delete() ! */ #define RESOURCE_SET_DESC(r,st) \ (r)->name = coap_new_string(strlen(st)); \ if ((r)->name) { \ @@ -1179,10 +1176,10 @@ main(int argc, char **argv) { fd_set readfds; struct timeval tv, *timeout; int result; - time_t now; + coap_tick_t now; coap_queue_t *nextpdu; char addr_str[NI_MAXHOST] = "::"; - char port_str[NI_MAXSERV] = "61616"; + char port_str[NI_MAXSERV] = "5683"; int opt; char *group = NULL; @@ -1223,7 +1220,7 @@ main(int argc, char **argv) { nextpdu = coap_peek_next( ctx ); - time(&now); + coap_ticks(&now); while ( nextpdu && nextpdu->t <= now ) { coap_retransmit( ctx, coap_pop_next( ctx ) ); nextpdu = coap_peek_next( ctx ); @@ -1231,8 +1228,8 @@ main(int argc, char **argv) { if ( nextpdu && nextpdu->t <= now + COAP_RESOURCE_CHECK_TIME ) { /* set timeout if there is a pdu to send before our automatic timeout occurs */ - tv.tv_usec = 0; - tv.tv_sec = nextpdu->t - now; + tv.tv_usec = ((nextpdu->t - now) % COAP_TICKS_PER_SECOND) << 10; + tv.tv_sec = (nextpdu->t - now) / COAP_TICKS_PER_SECOND; timeout = &tv; } else { tv.tv_usec = 0; diff --git a/examples/tiny.c b/examples/tiny.c index d8968b68..b309ce45 100644 --- a/examples/tiny.c +++ b/examples/tiny.c @@ -1,20 +1,9 @@ /* tiny -- tiny sender * - * Copyright (C) 2010 Olaf Bergmann + * Copyright (C) 2010,2011 Olaf Bergmann * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * This file is part of the CoAP library libcoap. Please see + * README for terms of use. */ #include @@ -145,7 +134,7 @@ main(int argc, char **argv) { if (! (pdu = make_pdu( rand() & 0xfff ) ) ) return -1; - coap_send( ctx, &dst, pdu ); + coap_send( ctx, (struct sockaddr *)&dst, sizeof(dst), pdu ); tv.tv_sec = 5; tv.tv_usec = 0; diff --git a/list.c b/list.c index 0b4acc11..a1ef71fc 100644 --- a/list.c +++ b/list.c @@ -1,20 +1,9 @@ /* list.c -- CoAP list structures * - * Copyright (C) 2010 Olaf Bergmann + * Copyright (C) 2010,2011 Olaf Bergmann * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * This file is part of the CoAP library libcoap. Please see + * README for terms of use. */ #include diff --git a/list.h b/list.h index 26a3a972..6cfa33fa 100644 --- a/list.h +++ b/list.h @@ -1,20 +1,9 @@ /* list.h -- CoAP list structures * - * Copyright (C) 2010 Olaf Bergmann + * Copyright (C) 2010,2011 Olaf Bergmann * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * This file is part of the CoAP library libcoap. Please see + * README for terms of use. */ #ifndef _COAP_LIST_H_ diff --git a/mem.h b/mem.h index 6369efe8..a84405ee 100644 --- a/mem.h +++ b/mem.h @@ -1,21 +1,10 @@ /* mem.h -- CoAP memory handling * Currently, this is just a dummy for malloc/free * - * Copyright (C) 2010 Olaf Bergmann + * Copyright (C) 2010,2011 Olaf Bergmann * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * This file is part of the CoAP library libcoap. Please see + * README for terms of use. */ #ifndef _COAP_MEM_H_ diff --git a/net.c b/net.c index 7132acf8..9f74203c 100644 --- a/net.c +++ b/net.c @@ -1,25 +1,15 @@ /* net.c -- CoAP network interface * - * Copyright (C) 2010 Olaf Bergmann + * Copyright (C) 2010,2011 Olaf Bergmann * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * This file is part of the CoAP library libcoap. Please see + * README for terms of use. */ #include #include #include +#include #include #include #include @@ -30,12 +20,11 @@ #include "mem.h" #include "str.h" #include "subscribe.h" +#include "option.h" #include "net.h" extern int snprintf(char *str, size_t size, const char *format, ...); -#define options_start(p) ((coap_opt_t *) ( (unsigned char *)p->hdr + sizeof ( coap_hdr_t ) )) - #define options_end(p, opt) { \ unsigned char opt_code = 0, cnt; \ *opt = options_start( node->pdu ); \ @@ -49,6 +38,7 @@ extern int snprintf(char *str, size_t size, const char *format, ...); ** some functions for debugging ************************************************************************/ +#ifndef NDEBUG void for_each_option(coap_pdu_t *pdu, void (*f)(coap_opt_t *, unsigned char, unsigned int, const unsigned char *) ) { @@ -129,6 +119,7 @@ coap_show_pdu( coap_pdu_t *pdu ) { printf("\n"); fflush(stdout); } +#endif /* NDEBUG */ /************************************************************************/ @@ -190,7 +181,9 @@ coap_queue_t * coap_new_node() { coap_queue_t *node = coap_malloc ( sizeof *node ); if ( ! node ) { +#ifndef NDEBUG perror ("coap_new_node: malloc"); +#endif return NULL; } @@ -233,23 +226,41 @@ coap_new_context(const struct sockaddr *listen_addr, size_t addr_size) { srand( getpid() ^ time(&now) ); if ( !c ) { +#ifndef NDEBUG perror("coap_init: malloc:"); +#endif return NULL; } memset(c, 0, sizeof( coap_context_t ) ); + /* register the critical options that we know */ + coap_register_option(c, COAP_OPTION_CONTENT_TYPE); + coap_register_option(c, COAP_OPTION_PROXY_URI); + coap_register_option(c, COAP_OPTION_URI_HOST); + coap_register_option(c, COAP_OPTION_URI_PORT); + coap_register_option(c, COAP_OPTION_URI_PATH); + coap_register_option(c, COAP_OPTION_TOKEN); + coap_register_option(c, COAP_OPTION_URI_QUERY); + c->sockfd = socket(listen_addr->sa_family, SOCK_DGRAM, 0); if ( c->sockfd < 0 ) { +#ifndef NDEBUG perror("coap_new_context: socket"); +#endif goto onerror; } - if ( setsockopt( c->sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse) ) < 0 ) + if ( setsockopt( c->sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse) ) < 0 ) { +#ifndef NDEBUG perror("setsockopt SO_REUSEADDR"); +#endif + } if ( bind (c->sockfd, listen_addr, addr_size) < 0 ) { +#ifndef NDEBUG perror("coap_new_context: bind"); +#endif goto onerror; } @@ -275,35 +286,61 @@ coap_free_context( coap_context_t *context ) { coap_free( context ); } +int +coap_option_check_critical(coap_context_t *ctx, + coap_pdu_t *pdu, + coap_opt_filter_t unknown) { + + coap_opt_iterator_t opt_iter; + int ok = 1; + + coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL); + + while (coap_option_next(&opt_iter)) { + + /* The following condition makes use of the fact that + * coap_option_getb() returns -1 if type exceeds the bit-vector + * filter. As the vector is supposed to be large enough to hold + * the largest known option, we know that everything beyond is + * bad. + */ + if (opt_iter.type & 0x01 && + coap_option_getb(ctx->known_options, opt_iter.type) < 1) { + debug("unknown critical option %d\n", opt_iter.type); + + ok = 0; + + /* When opt_iter.type is beyond our known option range, + * coap_option_setb() will return -1 and we are safe to leave + * this loop. */ + if (coap_option_setb(unknown, opt_iter.type) == -1) + break; + } + } + + return ok; +} + /* releases space allocated by PDU if free_pdu is set */ coap_tid_t -coap_send_impl( coap_context_t *context, const struct sockaddr_in6 *dst, coap_pdu_t *pdu, - int free_pdu ) { +coap_send_impl( coap_context_t *context, + const struct sockaddr *dst, socklen_t dstlen, + coap_pdu_t *pdu, int free_pdu ) { ssize_t bytes_written; -#ifndef NDEBUG - char addr[INET6_ADDRSTRLEN];/* buffer space for textual represenation of destination address */ -#endif if ( !context || !dst || !pdu ) return COAP_INVALID_TID; -#ifndef NDEBUG - if ( inet_ntop(dst->sin6_family, &dst->sin6_addr, addr, INET6_ADDRSTRLEN) == 0 ) { - perror("coap_send_impl: inet_ntop"); - } else { - fprintf(stderr,"send to [%s]:%d:\n ",addr,ntohs(dst->sin6_port)); - } - coap_show_pdu( pdu ); -#endif - bytes_written = sendto( context->sockfd, pdu->hdr, pdu->length, 0, - (const struct sockaddr *)dst, sizeof( *dst )); + dst, dstlen); if ( free_pdu ) coap_delete_pdu( pdu ); if ( bytes_written < 0 ) { +#ifndef NDEBUG perror("coap_send: sendto"); +#endif return COAP_INVALID_TID; } @@ -311,8 +348,10 @@ coap_send_impl( coap_context_t *context, const struct sockaddr_in6 *dst, coap_pd } coap_tid_t -coap_send( coap_context_t *context, const struct sockaddr_in6 *dst, coap_pdu_t *pdu ) { - return coap_send_impl( context, dst, pdu, 1 ); +coap_send( coap_context_t *context, + const struct sockaddr *dst, socklen_t dstlen, + coap_pdu_t *pdu ) { + return coap_send_impl( context, dst, dstlen, pdu, 1 ); } int @@ -320,18 +359,37 @@ _order_timestamp( coap_queue_t *lhs, coap_queue_t *rhs ) { return lhs && rhs && ( lhs->t < rhs->t ) ? -1 : 1; } -coap_tid_t -coap_send_confirmed( coap_context_t *context, const struct sockaddr_in6 *dst, coap_pdu_t *pdu ) { - coap_queue_t *node; +void +coap_ticks(coap_tick_t *t) { + struct timeval tv; + gettimeofday(&tv, NULL); + *t = tv.tv_sec * COAP_TICKS_PER_SECOND + + (tv.tv_usec >> 10) % COAP_TICKS_PER_SECOND; +} - /* send once, and enter into message queue for retransmission unless - * retransmission counter is reached */ +coap_tid_t +coap_send_confirmed( coap_context_t *context, + const struct sockaddr *dst, socklen_t addrlen, + coap_pdu_t *pdu ) { + coap_queue_t *node; + int r; + + if (addrlen > sizeof(struct sockaddr_in6)) + return COAP_INVALID_TID; node = coap_new_node(); - time(&node->t); - node->t += 1; /* 1 == 1 << 0 == 1 << retransmit_cnt */ - memcpy( &node->remote, dst, sizeof( struct sockaddr_in6 ) ); + r = rand(); + coap_ticks(&node->t); + + /* add randomized RESPONSE_TIMEOUT to determine retransmission timeout */ + node->timeout = COAP_DEFAULT_RESPONSE_TIMEOUT * COAP_TICKS_PER_SECOND + + (COAP_DEFAULT_RESPONSE_TIMEOUT >> 1) * + ((COAP_TICKS_PER_SECOND * (r & 0xFF)) >> 8); + node->t += node->timeout; + + node->remote.size = addrlen; + memcpy( &node->remote.addr.sa, dst, addrlen ); node->pdu = pdu; if ( !coap_insert_node( &context->sendqueue, node, _order_timestamp ) ) { @@ -342,7 +400,7 @@ coap_send_confirmed( coap_context_t *context, const struct sockaddr_in6 *dst, co return COAP_INVALID_TID; } - return coap_send_impl( context, dst, pdu, 0 ); + return coap_send_impl( context, dst, addrlen, pdu, 0 ); } coap_tid_t @@ -353,13 +411,14 @@ coap_retransmit( coap_context_t *context, coap_queue_t *node ) { /* re-initialize timeout when maximum number of retransmissions are not reached yet */ if ( node->retransmit_cnt < COAP_DEFAULT_MAX_RETRANSMIT ) { node->retransmit_cnt++; - node->t += ( 1 << node->retransmit_cnt ); + node->t += ( node->timeout << node->retransmit_cnt ); coap_insert_node( &context->sendqueue, node, _order_timestamp ); debug("** retransmission #%d of transaction %d\n", node->retransmit_cnt, ntohs(node->pdu->hdr->id)); - return coap_send_impl( context, &node->remote, node->pdu, 0 ); + return coap_send_impl( context, &node->remote.addr.sa, node->remote.size, + node->pdu, 0 ); } /* no more retransmissions, remove node from system */ @@ -410,6 +469,11 @@ coap_read( coap_context_t *ctx ) { return -1; } + if (addrsize > sizeof(struct sockaddr_in6)) { + debug("coap_read: cannote store remote address\n" ); + return -1; + } + node = coap_new_node(); if ( !node ) return -1; @@ -420,8 +484,9 @@ coap_read( coap_context_t *ctx ) { return -1; } - time( &node->t ); - memcpy( &node->remote, &src, sizeof( src ) ); + coap_ticks( &node->t ); + node->remote.size = addrsize; + memcpy( &node->remote.addr.sa, &src, addrsize ); /* "parse" received PDU by filling pdu structure */ memcpy( node->pdu->hdr, buf, bytes_read ); @@ -500,16 +565,14 @@ void coap_dispatch( coap_context_t *context ) { coap_queue_t *node, *sent; coap_uri_t uri; - coap_opt_t *opt; - int type; coap_pdu_t *response; - size_t msglen; -#define MAXMSG 60 - static unsigned char msg[MAXMSG]; + coap_opt_filter_t unknown; if ( !context ) return; + memset(unknown, 0, sizeof(coap_opt_filter_t)); + while ( context->recvqueue ) { node = context->recvqueue; @@ -533,7 +596,7 @@ coap_dispatch( coap_context_t *context ) { /* The easy way: we still have the transaction that has caused * the trouble.*/ - coap_delete_subscription(context, coap_uri_hash(&uri), &node->remote); + coap_delete_subscription(context, coap_uri_hash(&uri), &node->remote.addr.sa); } else { /* ?? */ } @@ -542,26 +605,33 @@ coap_dispatch( coap_context_t *context ) { coap_remove_transaction( &context->sendqueue, node->pdu->hdr->id ); break; case COAP_MESSAGE_NON : /* check for unknown critical options */ - if ( coap_check_critical(node->pdu, &opt) != 0 ) + if (coap_option_check_critical(context, node->pdu, unknown) == 0) goto cleanup; break; case COAP_MESSAGE_CON : /* check for unknown critical options */ - /* get type and option */ - type = coap_check_critical(node->pdu, &opt); + if (coap_option_check_critical(context, node->pdu, unknown) == 0) { + coap_opt_iterator_t opt_iter; - if ( type != 0 ) { /* send error response if unknown */ response = coap_new_pdu(); if (response) { response->hdr->type = COAP_MESSAGE_RST; - response->hdr->code = COAP_RESPONSE_X_242; + response->hdr->code = COAP_RESPONSE_CODE(402); response->hdr->id = node->pdu->hdr->id; - /* add rejected option to payload */ - msglen = snprintf((char *)msg, MAXMSG-1, "Unknown critical option %d", type); - coap_add_data(response, msglen, msg); - - if ( coap_send( context, &node->remote, response ) == - COAP_INVALID_TID ) { + coap_option_iterator_init(node->pdu, &opt_iter, unknown); + + while (coap_option_next(&opt_iter)) { + if (opt_iter.type & 0x01) { + + coap_add_option(response, opt_iter.type, + COAP_OPT_LENGTH(*opt_iter.option), + COAP_OPT_VALUE(*opt_iter.option)); + } + } + + if ( coap_send( context, &node->remote.addr.sa, node->remote.size, + response ) + == COAP_INVALID_TID ) { debug("coap_dispatch: error sending reponse"); coap_delete_pdu(response); } diff --git a/net.h b/net.h index a7e9ce23..d45a094d 100644 --- a/net.h +++ b/net.h @@ -1,20 +1,9 @@ /* net.h -- CoAP network interface * - * Copyright (C) 2010 Olaf Bergmann + * Copyright (C) 2010,2011 Olaf Bergmann * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * This file is part of the CoAP library libcoap. Please see + * README for terms of use. */ #ifndef _COAP_NET_H_ @@ -25,17 +14,31 @@ #include #include +#include "option.h" #include "pdu.h" +typedef unsigned int coap_tick_t; + +/** multi-purpose address abstraction */ +typedef struct { + socklen_t size; /**< size of addr */ + union { + struct sockaddr sa; + struct sockaddr_in sin; + struct sockaddr_in6 sin6; + } addr; +} coap_address_t; + struct coap_listnode { struct coap_listnode *next; - time_t t; /* when to send PDU for the next time */ + coap_tick_t t; /* when to send PDU for the next time */ unsigned char retransmit_cnt; /* retransmission counter, will be removed when zero */ + unsigned int timeout; /* the randomized timeout value */ - struct sockaddr_in6 remote; /* remote address */ + coap_address_t remote; /**< remote address */ - coap_pdu_t *pdu; /* the CoAP PDU to send */ + coap_pdu_t *pdu; /**< the CoAP PDU to send */ }; typedef struct coap_listnode coap_queue_t; @@ -55,6 +58,7 @@ coap_queue_t *coap_new_node(); /* The CoAP stack's global state is stored in a coap_context_t object */ typedef struct { + coap_opt_filter_t known_options; coap_list_t *resources, *subscriptions; /* FIXME: make these hash tables */ coap_queue_t *sendqueue, *recvqueue; /* FIXME make these coap_list_t */ int sockfd; /* send/receive socket */ @@ -76,6 +80,18 @@ void coap_register_message_handler( coap_context_t *context, coap_message_handle */ void coap_register_error_handler( coap_context_t *context, coap_message_handler_t handler); +/** + * Registers the option type @p type with the given context object @p + * ctx. + * + * @param ctx The context to use. + * @param type The option type to register. + */ +inline static void +coap_register_option(coap_context_t *ctx, unsigned char type) { + coap_option_setb(ctx->known_options, type); +} + /* Returns the next pdu to send without removing from sendqeue. */ coap_queue_t *coap_peek_next( coap_context_t *context ); @@ -90,17 +106,37 @@ void coap_free_context( coap_context_t *context ); /** - * Sends a confirmed CoAP message to given destination. The memory that is allocated by pdu will - * be released by coap_send_confirmed(). The caller must not make any assumption on the lifetime - * of pdu. + * Sends a confirmed CoAP message to given destination. The memory + * that is allocated by pdu will be released by + * coap_send_confirmed(). The caller must not make any assumption on + * the lifetime of pdu. + * + * @param context The CoAP context to use. + * @param dst The address to send to. + * @param addrlen The actual length of @p dst + * @param pdu The CoAP PDU to send. + * @return The message id of the sent message or @c COAP_INVALID_TID on error. */ -coap_tid_t coap_send_confirmed( coap_context_t *context, const struct sockaddr_in6 *dst, coap_pdu_t *pdu ); +coap_tid_t coap_send_confirmed( coap_context_t *context, + const struct sockaddr *dst, + socklen_t addrlen, + coap_pdu_t *pdu ); /** - * Sends a non-confirmed CoAP message to given destination. The memory that is allocated by pdu will - * be released by coap_send(). The caller must not make any assumption on the lifetime of pdu. + * Sends a non-confirmed CoAP message to given destination. The memory + * that is allocated by pdu will be released by coap_send(). The + * caller must not make any assumption on the lifetime of pdu. + * + * @param context The CoAP context to use. + * @param dst The address to send to. + * @param addrlen The actual length of @p dst + * @param pdu The CoAP PDU to send. + * @return The message id of the sent message or @c COAP_INVALID_TID on error. */ -coap_tid_t coap_send( coap_context_t *context, const struct sockaddr_in6 *dst, coap_pdu_t *pdu ); +coap_tid_t coap_send( coap_context_t *context, + const struct sockaddr *dst, + socklen_t addrlen, + coap_pdu_t *pdu ); /** Handles retransmissions of confirmable messages */ coap_tid_t coap_retransmit( coap_context_t *context, coap_queue_t *node ); @@ -129,4 +165,48 @@ void coap_dispatch( coap_context_t *context ); /** Returns 1 if there are no messages to send or to dispatch in the context's queues. */ int coap_can_exit( coap_context_t *context ); +#define COAP_TICKS_PER_SECOND 1024 + +/** + * Returns the current value of an internal tick counter. The counter + * counts \c COAP_TICKS_PER_SECOND ticks every second. + */ +void coap_ticks(coap_tick_t *); + +/** + * Verifies that @p pdu contains no unknown critical options. Options + * must be registered at @p ctx, using the function + * coap_register_option(). A basic set of options is registered + * automatically by coap_new_context(). This function returns @c 1 if + * @p pdu is ok, @c 0 otherwise. The given filter object @p unknown + * will be updated with the unknown options. As only @c COAP_MAX_OPT + * options can be signalled this way, remaining options must be + * examined manually. + * + * @code + coap_opt_filter_t f = COAP_OPT_NONE; + coap_opt_iterator_t opt_iter; + + if (coap_option_check_critical(ctx, pdu, f) == 0) { + coap_option_iterator_init(pdu, &opt_iter, f); + + while (coap_option_next(&opt_iter)) { + if (opt_iter.type & 0x01) { + ... handle unknown critical option in opt_iter ... + } + } + } + * @endcode + * + * @param ctx The context where all known options are registered. + * @param pdu The PDU to check. + * @param unknown The output filter that will be updated to indicate the + * unknown critical options found in @p pdu. + * + * @return @c 1 if everything was ok, @c 0 otherwise. + */ +int coap_option_check_critical(coap_context_t *ctx, + coap_pdu_t *pdu, + coap_opt_filter_t unknown); + #endif /* _COAP_NET_H_ */ diff --git a/str.c b/str.c index 341e6057..fe847cf1 100644 --- a/str.c +++ b/str.c @@ -1,20 +1,9 @@ /* str.c -- strings to be used in the CoAP library * - * Copyright (C) 2010 Olaf Bergmann + * Copyright (C) 2010,2011 Olaf Bergmann * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * This file is part of the CoAP library libcoap. Please see + * README for terms of use. */ #include diff --git a/str.h b/str.h index 75d8d0e1..7c10ce8a 100644 --- a/str.h +++ b/str.h @@ -1,20 +1,9 @@ /* str.h -- strings to be used in the CoAP library * - * Copyright (C) 2010 Olaf Bergmann + * Copyright (C) 2010,2011 Olaf Bergmann * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * This file is part of the CoAP library libcoap. Please see + * README for terms of use. */ #ifndef _COAP_STR_H_ diff --git a/subscribe.c b/subscribe.c index 9c98f470..4e78a695 100644 --- a/subscribe.c +++ b/subscribe.c @@ -1,21 +1,10 @@ /* subscribe.c -- subscription handling for CoAP * see draft-hartke-coap-observe-01 * - * Copyright (C) 2010 Olaf Bergmann + * Copyright (C) 2010,2011 Olaf Bergmann * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * This file is part of the CoAP library libcoap. Please see + * README for terms of use. */ #include @@ -48,10 +37,10 @@ notify(coap_context_t *context, coap_resource_t *res, pdu->hdr->code = code; /* FIXME: content-type and data (how about block?) */ - if (res->uri->na.length) - coap_add_option (pdu, COAP_OPTION_URI_AUTHORITY, - res->uri->na.length, - res->uri->na.s ); + if (res->uri->host.length) + coap_add_option (pdu, COAP_OPTION_URI_HOST, + res->uri->host.length, + res->uri->host.s ); if (res->uri->path.length) coap_add_option (pdu, COAP_OPTION_URI_PATH, @@ -84,13 +73,18 @@ notify(coap_context_t *context, coap_resource_t *res, } #ifndef NDEBUG - if ( inet_ntop(AF_INET6, &(sub->subscriber.sin6_addr), addr, INET6_ADDRSTRLEN) ) { - debug("*** notify for %s to [%s]:%d\n", res->uri->path.s, addr, ntohs(sub->subscriber.sin6_port)); + if ( inet_ntop(sub->subscriber.addr.sa.sa_family, + &sub->subscriber.addr, addr, sizeof(addr)) ) { + debug("*** notify for %s to [%s]\n", res->uri->path.s, addr); } #endif - if ( pdu && coap_send_confirmed(context, - &sub->subscriber, pdu ) == COAP_INVALID_TID ) { + if (pdu && coap_send_confirmed(context, + &sub->subscriber.addr.sa, + sub->subscriber.size, pdu) + == COAP_INVALID_TID) { +#ifndef NDEBUG debug("coap_check_resource_list: error sending notification\n"); +#endif coap_delete_pdu(pdu); } } @@ -160,9 +154,11 @@ coap_check_subscriptions(coap_context_t *context) { node = context->subscriptions; while ( node && COAP_SUBSCRIPTION(node)->expires < now ) { #ifndef NDEBUG - if ( inet_ntop(AF_INET6, &(COAP_SUBSCRIPTION(node)->subscriber.sin6_addr), addr, INET6_ADDRSTRLEN) ) { - - debug("** removed expired subscription from [%s]:%d\n", addr, ntohs(COAP_SUBSCRIPTION(node)->subscriber.sin6_port)); + if (inet_ntop(COAP_SUBSCRIPTION(node)->subscriber.addr.sa.sa_family, + &COAP_SUBSCRIPTION(node)->subscriber.addr, + addr, sizeof(addr))) { + + debug("** removed expired subscription from [%s]\n", addr); } #endif #if 0 @@ -250,7 +246,9 @@ coap_delete_resource(coap_context_t *context, coap_key_t key) { for (prev = NULL, node = context->resources; node; prev = node, node = node->next) { if (coap_uri_hash(COAP_RESOURCE(node)->uri) == key) { +#ifndef NDEBUG debug("removed key %lu (%s)\n",key,COAP_RESOURCE(node)->uri->path.s); +#endif if (!prev) context->resources = node->next; else @@ -265,16 +263,16 @@ coap_delete_resource(coap_context_t *context, coap_key_t key) { coap_subscription_t * coap_new_subscription(coap_context_t *context, const coap_uri_t *resource, - const struct sockaddr_in6 *subscriber, time_t expiry) { + const struct sockaddr *addr, socklen_t addrlen, time_t expiry) { coap_subscription_t *result; - if ( !context || !resource || !subscriber + if ( !context || !resource || !addr || !(result = coap_malloc(sizeof(coap_subscription_t)))) return NULL; result->resource = coap_uri_hash(resource); result->expires = expiry; - memcpy( &result->subscriber, subscriber, sizeof(struct sockaddr_in6) ); + memcpy(&result->subscriber.addr.sa, addr, addrlen); memset(&result->token, 0, sizeof(str)); @@ -337,13 +335,18 @@ coap_add_subscription(coap_context_t *context, coap_subscription_t * coap_find_subscription(coap_context_t *context, coap_key_t hashkey, - struct sockaddr_in6 *subscriber, + struct sockaddr *addr, str *token) { +#if 0 coap_list_t *node; +#endif - if (!context || !subscriber || hashkey == COAP_INVALID_HASHKEY) + if (!context || !addr || hashkey == COAP_INVALID_HASHKEY) return NULL; + /* FIXME: I do not like the way subscriptions work right now. To be fixed. */ + +#if 0 for (node = context->subscriptions; node; node = node->next) { if (COAP_SUBSCRIPTION(node)->resource == hashkey) { @@ -361,18 +364,24 @@ coap_find_subscription(coap_context_t *context, return COAP_SUBSCRIPTION(node); } } +#endif return NULL; } int coap_delete_subscription(coap_context_t *context, coap_key_t key, - struct sockaddr_in6 *subscriber) { + struct sockaddr *addr) { +#if 0 coap_list_t *prev, *node; +#endif - if (!context || !subscriber || key == COAP_INVALID_HASHKEY) + if (!context || !addr || key == COAP_INVALID_HASHKEY) return 0; + /* FIXME: I do not like the way subscriptions work right now. To be fixed. */ + +#if 0 for (prev = NULL, node = context->subscriptions; node; prev = node, node = node->next) { if (COAP_SUBSCRIPTION(node)->resource == key) { @@ -394,5 +403,7 @@ coap_delete_subscription(coap_context_t *context, } } } +#endif + return 0; } diff --git a/subscribe.h b/subscribe.h index ac888247..fae539c8 100644 --- a/subscribe.h +++ b/subscribe.h @@ -1,21 +1,10 @@ /* subscribe.h -- subscription handling for CoAP * see draft-hartke-coap-observe-01 * - * Copyright (C) 2010 Olaf Bergmann + * Copyright (C) 2010,2011 Olaf Bergmann * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * This file is part of the CoAP library libcoap. Please see + * README for terms of use. */ @@ -67,8 +56,9 @@ typedef struct { coap_key_t resource; /* hash key for subscribed resource */ time_t expires; /* expiry time of subscription */ - struct sockaddr_in6 subscriber; /* subscriber's address */ - str token; /* subscription token */ + coap_address_t subscriber; /**< subscriber's address */ + + str token; /**< subscription token */ } coap_subscription_t; #define COAP_RESOURCE(node) ((coap_resource_t *)(node)->data) @@ -98,7 +88,8 @@ int coap_delete_resource(coap_context_t *context, coap_key_t key); * allocated for this object must be released using coap_free(). */ coap_subscription_t *coap_new_subscription(coap_context_t *context, const coap_uri_t *resource, - const struct sockaddr_in6 *subscriber, + const struct sockaddr *subscriber, + socklen_t addrlen, time_t expiry); /** @@ -127,7 +118,7 @@ coap_key_t coap_add_subscription(coap_context_t *context, */ coap_subscription_t * coap_find_subscription(coap_context_t *context, coap_key_t hashkey, - struct sockaddr_in6 *subscriber, + struct sockaddr *subscriber, str *token); /** * Removes a subscription from the subscription list stored in context and @@ -138,7 +129,7 @@ coap_subscription_t * coap_find_subscription(coap_context_t *context, */ int coap_delete_subscription(coap_context_t *context, coap_key_t hashkey, - struct sockaddr_in6 *subscriber); + struct sockaddr *subscriber); /** Returns a unique hash for the specified URI or COAP_INVALID_HASHKEY on error. */ coap_key_t coap_uri_hash(const coap_uri_t *uri);