diff --git a/coap.h b/coap.h index aa378114..1a392109 100644 --- a/coap.h +++ b/coap.h @@ -1,17 +1,17 @@ /* coap.h -- main header file for CoAP stack * * Copyright (C) 2010 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. diff --git a/configure.in b/configure.in index 3673d53d..fd5e335b 100644 --- a/configure.in +++ b/configure.in @@ -2,23 +2,23 @@ # Process this file with autoconf to produce a configure script. # # Copyright (C) 2010 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. -AC_PREREQ([2.67]) -AC_INIT([libcoap], [0.07]) +AC_PREREQ([2.65]) +AC_INIT([libcoap], [0.08]) AC_CONFIG_SRCDIR([coap.h]) # Checks for programs. diff --git a/debug.c b/debug.c index d01dd7ed..e5574feb 100644 --- a/debug.c +++ b/debug.c @@ -1,17 +1,17 @@ /* debug.c -- debug utilities * * Copyright (C) 2010 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. @@ -34,7 +34,7 @@ void debug(char *format, ...) { if ( strftime(timebuf,sizeof(timebuf), "%b %d %H:%M:%S", tmp) ) printf("%s ", timebuf); - + va_start(ap, format); vprintf(format, ap); va_end(ap); diff --git a/debug.h b/debug.h index f04903a1..6ed6e93d 100644 --- a/debug.h +++ b/debug.h @@ -1,17 +1,17 @@ /* debug.h -- debug utilities * * Copyright (C) 2010 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. diff --git a/encode.c b/encode.c index e6fe3bd2..fac9e4aa 100644 --- a/encode.c +++ b/encode.c @@ -1,17 +1,17 @@ /* encode.c -- encoding and decoding of CoAP data types * * Copyright (C) 2010 Olaf Bergmann , Carsten Bormann - * + * * 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. @@ -35,7 +35,7 @@ unsigned int coap_decode_var_bytes(unsigned char *buf,unsigned int len) { unsigned int i, n = 0; for (i = 0; i < len; ++i) - n = (n << 8) + buf[i]; + n = (n << 8) + buf[i]; return n; } @@ -45,7 +45,7 @@ coap_encode_var_bytes(unsigned char *buf, unsigned int val) { int n, i = val >> 1; for (n = 0; i; n++) /* FIXME: coap_fls() */ i >>= 1; - + for (i = n / 8 + 1; i; --i) { buf[i-1] = val & 0xff; val >>= 8; diff --git a/encode.h b/encode.h index e7f2b8b6..286ea2da 100644 --- a/encode.h +++ b/encode.h @@ -1,17 +1,17 @@ /* encode.h -- encoding and decoding of CoAP data types * * Copyright (C) 2010 Olaf Bergmann , Carsten Bormann - * + * * 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. @@ -46,13 +46,13 @@ extern int coap_fls(unsigned int i); #define COAP_PSEUDOFP_ENCODE_8_4_DOWN(v,ls) (v < HIBIT ? v : (ls = coap_fls(v) - N, (v >> ls) & MMASK) + ls) #define COAP_PSEUDOFP_ENCODE_8_4_UP(v,ls,s) (v < HIBIT ? v : (ls = coap_fls(v) - N, (s = (((v + ((1<> ls) & MMASK)), s == 0 ? HIBIT + ls + 1 : s + ls)) -/** +/** * Decodes multiple-length byte sequences. buf points to an input byte * sequence of length len. Returns the decoded value. */ unsigned int coap_decode_var_bytes(unsigned char *buf,unsigned int len); -/** +/** * Encodes multiple-length byte sequences. buf points to an output * buffer of sufficient length to store the encoded bytes. val is * the value to encode. Returns the number of bytes used to encode diff --git a/examples/client.c b/examples/client.c index 8d9f51e1..2dadab01 100644 --- a/examples/client.c +++ b/examples/client.c @@ -1,17 +1,17 @@ /* coap-client -- simple CoAP client * * Copyright (C) 2010 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. @@ -48,7 +48,7 @@ typedef unsigned char method_t; method_t method = 1; /* the method we are using in our requests */ extern unsigned int -print_readable( const unsigned char *data, unsigned int len, +print_readable( const unsigned char *data, unsigned int len, unsigned char *result, unsigned int buflen ); int @@ -59,7 +59,7 @@ append_to_file(const char *filename, const unsigned char *data, size_t len) { perror("append_to_file: fopen"); return -1; } - + do { written = fwrite(data, 1, len, file); len -= written; @@ -72,7 +72,7 @@ append_to_file(const char *filename, const unsigned char *data, size_t len) { coap_pdu_t * new_ack( coap_context_t *ctx, coap_queue_t *node ) { coap_pdu_t *pdu = coap_new_pdu(); - + if (pdu) { pdu->hdr->type = COAP_MESSAGE_ACK; pdu->hdr->code = 0; @@ -105,8 +105,8 @@ coap_new_request( method_t m, coap_list_t *options ) { pdu->hdr->code = m; for (opt = options; opt; opt = opt->next) { - coap_add_option( pdu, COAP_OPTION_KEY(*(coap_option *)opt->data), - COAP_OPTION_LENGTH(*(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) ); } @@ -119,7 +119,7 @@ coap_new_request( method_t m, coap_list_t *options ) { return pdu; } -void +void send_request( coap_context_t *ctx, coap_pdu_t *pdu, const char *server, unsigned short port ) { struct addrinfo *res, *ainfo; struct addrinfo hints; @@ -152,7 +152,7 @@ send_request( coap_context_t *ctx, coap_pdu_t *pdu, const char *server, unsign goto leave; } } - + leave: freeaddrinfo(res); } @@ -160,8 +160,8 @@ send_request( coap_context_t *ctx, coap_pdu_t *pdu, const char *server, unsign #define COAP_OPT_BLOCK_LAST(opt) ( COAP_OPT_VALUE(*block) + (COAP_OPT_LENGTH(*block) - 1) ) #define COAP_OPT_BLOCK_MORE(opt) ( *COAP_OPT_LAST(*block) & 0x08 ) #define COAP_OPT_BLOCK_SIZE(opt) ( *COAP_OPT_LAST(*block) & 0x07 ) - -unsigned int + +unsigned int _read_blk_nr(coap_opt_t *opt) { unsigned int i, nr=0; for ( i = COAP_OPT_LENGTH(*opt); i; --i) { @@ -171,7 +171,7 @@ _read_blk_nr(coap_opt_t *opt) { } #define COAP_OPT_BLOCK_NR(opt) _read_blk_nr(&opt) -void +void message_handler( coap_context_t *ctx, coap_queue_t *node, void *data) { coap_pdu_t *pdu = NULL; coap_opt_t *block, *ct, *sub; @@ -190,7 +190,7 @@ message_handler( coap_context_t *ctx, coap_queue_t *node, void *data) { debug("dropped packet with unknown version %u\n", node->pdu->hdr->version); return; } - + if ( node->pdu->hdr->code < COAP_RESPONSE_100 && node->pdu->hdr->type == COAP_MESSAGE_CON ) { /* send 500 response */ pdu = new_response( ctx, node, COAP_RESPONSE_500 ); @@ -216,32 +216,32 @@ message_handler( coap_context_t *ctx, coap_queue_t *node, void *data) { append_to_file( "coap.out", databuf, len ); } - if ( (blocknr & 0x08) ) { + if ( (blocknr & 0x08) ) { /* more bit is set */ printf("found the M bit, block size is %u, block nr. %u\n", - blocknr & 0x07, + blocknr & 0x07, (blocknr & 0xf0) << blocknr & 0x07); - + /* need to acknowledge if message was asyncronous */ if ( node->pdu->hdr->type == COAP_MESSAGE_CON ) { - pdu = new_ack( ctx, node ); - + pdu = new_ack( ctx, node ); + if ( pdu && coap_send( ctx, &node->remote, pdu ) == COAP_INVALID_TID ) { debug("message_handler: error sending reponse"); coap_delete_pdu(pdu); return; - } + } } - + /* create pdu with request for next block */ pdu = coap_new_request( method, NULL ); /* first, create bare PDU w/o any option */ if ( pdu ) { 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 ); if ( ct ) { - coap_add_option( pdu, COAP_OPTION_CONTENT_TYPE, + coap_add_option( pdu, COAP_OPTION_CONTENT_TYPE, COAP_OPT_LENGTH(*ct),COAP_OPT_VALUE(*ct) ); } @@ -251,7 +251,7 @@ message_handler( coap_context_t *ctx, coap_queue_t *node, void *data) { case COAP_OPTION_URI_AUTHORITY : case COAP_OPTION_URI_PATH : case COAP_OPTION_URI_QUERY : - coap_add_option ( pdu, COAP_OPTION_KEY(*(coap_option *)option->data), + coap_add_option ( pdu, COAP_OPTION_KEY(*(coap_option *)option->data), COAP_OPTION_LENGTH(*(coap_option *)option->data), COAP_OPTION_DATA(*(coap_option *)option->data) ); break; @@ -259,17 +259,17 @@ message_handler( coap_context_t *ctx, coap_queue_t *node, void *data) { ; /* skip other options */ } } - + /* finally add updated block option from response */ - coap_add_option ( pdu, COAP_OPTION_BLOCK, + coap_add_option ( pdu, COAP_OPTION_BLOCK, coap_encode_var_bytes(buf, blocknr + ( 1 << 4) ), buf); - + if ( coap_send_confirmed( ctx, &node->remote, pdu ) == COAP_INVALID_TID ) { debug("message_handler: error sending reponse"); coap_delete_pdu(pdu); } return; - } + } } } @@ -277,7 +277,7 @@ message_handler( coap_context_t *ctx, coap_queue_t *node, void *data) { default: ; } - + /* acknowledge if requested */ if ( !pdu && node->pdu->hdr->type == COAP_MESSAGE_CON ) { pdu = new_ack( ctx, node ); @@ -287,18 +287,18 @@ message_handler( coap_context_t *ctx, coap_queue_t *node, void *data) { if ( pdu && coap_send( ctx, &node->remote, pdu ) == COAP_INVALID_TID ) { debug("message_handler: error sending reponse"); coap_delete_pdu(pdu); - } + } /* our job is done, we can exit at any time */ sub = coap_check_option( node->pdu, COAP_OPTION_SUBSCRIPTION ); if ( sub ) { - debug("message_handler: Subscription-Lifetime is %d\n", + debug("message_handler: Subscription-Lifetime is %d\n", COAP_PSEUDOFP_DECODE_8_4(*COAP_OPT_VALUE(*sub))); } ready = !sub || COAP_PSEUDOFP_DECODE_8_4(*COAP_OPT_VALUE(*sub)) == 0; } -void +void usage( const char *program, const char *version) { const char *p; @@ -344,7 +344,7 @@ join( coap_context_t *ctx, char *group_name ){ /* get the first suitable interface identifier */ for (ainfo = reslocal; ainfo != NULL; ainfo = ainfo->ai_next) { if ( ainfo->ai_family == AF_INET6 ) { - mreq.ipv6mr_interface = + mreq.ipv6mr_interface = ((struct sockaddr_in6 *)ainfo->ai_addr)->sin6_scope_id; break; } @@ -364,32 +364,32 @@ join( coap_context_t *ctx, char *group_name ){ for (ainfo = resmulti; ainfo != NULL; ainfo = ainfo->ai_next) { if ( ainfo->ai_family == AF_INET6 ) { - mreq.ipv6mr_multiaddr = + mreq.ipv6mr_multiaddr = ((struct sockaddr_in6 *)ainfo->ai_addr)->sin6_addr; break; } } - + result = setsockopt( ctx->sockfd, IPPROTO_IPV6, IPV6_JOIN_GROUP, (char *)&mreq, sizeof(mreq) ); - if ( result < 0 ) + if ( result < 0 ) perror("join: setsockopt"); finish: freeaddrinfo(resmulti); freeaddrinfo(reslocal); - + return result; } -int +int order_opts(void *a, void *b) { if (!a || !b) return a < b ? -1 : 1; if (COAP_OPTION_KEY(*(coap_option *)a) < COAP_OPTION_KEY(*(coap_option *)b)) return -1; - + return COAP_OPTION_KEY(*(coap_option *)a) == COAP_OPTION_KEY(*(coap_option *)b); } @@ -400,28 +400,28 @@ new_option_node(unsigned short key, unsigned int length, unsigned char *data) { coap_list_t *node; option = coap_malloc(sizeof(coap_option) + length); - if ( !option ) + if ( !option ) goto error; - + COAP_OPTION_KEY(*option) = key; COAP_OPTION_LENGTH(*option) = length; memcpy(COAP_OPTION_DATA(*option), data, length); - /* we can pass NULL here as delete function since option is released automatically */ + /* we can pass NULL here as delete function since option is released automatically */ node = coap_new_listnode(option, NULL); - if ( node ) + if ( node ) return node; error: perror("new_option_node: malloc"); coap_free( option ); - return NULL; + return NULL; } void cmdline_content_type(char *arg, unsigned short key) { - static char *content_types[] = + static char *content_types[] = { "plain", "xml", "csv", "html", "","","","","","","","","","","","","","","","","", "gif", "jpeg", "png", "tiff", "audio", "video", "","","","","","","","","","","","","", "link", "axml", "binary", "rdf", "soap", "atom", "xmpp", "exi", @@ -434,9 +434,9 @@ cmdline_content_type(char *arg, unsigned short key) { while (q && *q) { p = strchr(q, ','); - for (i=0; content_types[i] && - strncmp(q,content_types[i], p ? p-q : strlen(q)) != 0 ; - ++i) + for (i=0; content_types[i] && + strncmp(q,content_types[i], p ? p-q : strlen(q)) != 0 ; + ++i) ; if (content_types[i]) { @@ -454,7 +454,7 @@ cmdline_content_type(char *arg, unsigned short key) { if (valcnt) { node = new_option_node(key, valcnt, value); - if (node) + if (node) coap_insert( &optlist, node, order_opts ); } } @@ -465,17 +465,17 @@ cmdline_uri(char *arg) { #if 0 /* need authority only for proxy requests */ if (uri.na.length) - coap_insert( &optlist, new_option_node(COAP_OPTION_URI_AUTHORITY, + coap_insert( &optlist, new_option_node(COAP_OPTION_URI_AUTHORITY, uri.na.length, uri.na.s), order_opts); #endif if (uri.path.length) - coap_insert( &optlist, new_option_node(COAP_OPTION_URI_PATH, + coap_insert( &optlist, new_option_node(COAP_OPTION_URI_PATH, uri.path.length, uri.path.s), order_opts); if (uri.query.length) - coap_insert( &optlist, new_option_node(COAP_OPTION_URI_QUERY, + coap_insert( &optlist, new_option_node(COAP_OPTION_URI_QUERY, uri.query.length, uri.query.s), order_opts); } @@ -484,7 +484,7 @@ void cmdline_blocksize(char *arg) { static unsigned char buf[4]; /* hack: temporarily take encoded bytes */ unsigned int blocksize = atoi(arg); - + if ( COAP_MAX_PDU_SIZE < blocksize + sizeof(coap_hdr_t) ) { fprintf(stderr, "W: skipped invalid blocksize\n"); return; @@ -492,7 +492,7 @@ cmdline_blocksize(char *arg) { /* use only last three bits and clear M-bit */ blocksize = (coap_fls(blocksize >> 4) - 1) & 0x07; - coap_insert( &optlist, new_option_node(COAP_OPTION_BLOCK, + coap_insert( &optlist, new_option_node(COAP_OPTION_BLOCK, coap_encode_var_bytes(buf, blocksize), buf), order_opts); } @@ -520,7 +520,7 @@ cmdline_proxy(char *arg) { void cmdline_token(char *arg) { coap_insert( &optlist, new_option_node(COAP_OPTION_TOKEN, - strlen(arg), + strlen(arg), (unsigned char *)arg), order_opts); } @@ -531,13 +531,13 @@ cmdline_input_from_file(char *filename, str *buf) { int result = 1; struct stat statbuf; - if (!filename || !buf) - return 0; + if (!filename || !buf) + return 0; if (filename[0] == '-' && !filename[1]) { /* read from stdin */ buf->length = 20000; buf->s = (unsigned char *)coap_malloc(buf->length); - if (!buf->s) + if (!buf->s) return 0; inputfile = stdin; @@ -550,9 +550,9 @@ cmdline_input_from_file(char *filename, str *buf) { buf->length = statbuf.st_size; buf->s = (unsigned char *)coap_malloc(buf->length); - if (!buf->s) + if (!buf->s) return 0; - + inputfile = fopen(filename, "r"); if ( !inputfile ) { perror("cmdline_input_from_file: fopen"); @@ -562,7 +562,7 @@ cmdline_input_from_file(char *filename, str *buf) { } len = fread(buf->s, 1, buf->length, inputfile); - + if (len < buf->length) { if (ferror(inputfile) != 0) { perror("cmdline_input_from_file: fread"); @@ -573,8 +573,8 @@ cmdline_input_from_file(char *filename, str *buf) { } else { buf->length = len; } - } - + } + if (inputfile != stdin) fclose(inputfile); @@ -583,17 +583,17 @@ cmdline_input_from_file(char *filename, str *buf) { method_t cmdline_method(char *arg) { - static char *methods[] = + static char *methods[] = { 0, "get", "post", "put", "delete", 0}; unsigned char i; - for (i=1; methods[i] && strcasecmp(arg,methods[i]) != 0 ; ++i) + for (i=1; methods[i] && strcasecmp(arg,methods[i]) != 0 ; ++i) ; - + return i; /* note that we do not prevent illegal methods */ } -int +int main(int argc, char **argv) { coap_context_t *ctx; fd_set readfds; @@ -680,22 +680,22 @@ main(int argc, char **argv) { if (*server.s == '[') { /* IPv6 address reference */ p = ++server.s; --server.length; - - while ( p - server.s < server.length && *p != ']' ) + + while ( p - server.s < server.length && *p != ']' ) ++p; if (*p == ']') *p++ = '\0'; /* port starts here */ } else { /* IPv4 address or hostname */ p = server.s; - while ( p - server.s < server.length && *p != ':' ) + while ( p - server.s < server.length && *p != ':' ) ++p; } - + if (*p == ':') { /* port starts here */ *p++ = '\0'; port = 0; - + /* set port */ while( p - server.s < server.length && isdigit(*p) ) { port = port * 10 + ( *p - '0' ); @@ -708,9 +708,9 @@ main(int argc, char **argv) { send_request( ctx, pdu, server.length ? (char *)server.s : "::1", port ); while ( !(ready && coap_can_exit(ctx)) ) { - FD_ZERO(&readfds); + FD_ZERO(&readfds); FD_SET( ctx->sockfd, &readfds ); - + nextpdu = coap_peek_next( ctx ); time(&now); @@ -723,11 +723,11 @@ main(int argc, char **argv) { tv.tv_usec = 0; tv.tv_sec = nextpdu->t - now; timeout = &tv; - } else + } else timeout = NULL; /* no timeout otherwise */ result = select( ctx->sockfd + 1, &readfds, 0, 0, timeout ); - + if ( result < 0 ) { /* error */ perror("select"); } else if ( result > 0 ) { /* read from socket */ diff --git a/examples/server.c b/examples/server.c index f2a886ce..5aed9f3b 100644 --- a/examples/server.c +++ b/examples/server.c @@ -2,17 +2,17 @@ * as defined in draft-ietf-core-coap-01 * * Copyright (C) 2010 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. @@ -59,7 +59,7 @@ } /* temporary storage for dynamic resource representations */ -static char resource_buf[20000]; +static char resource_buf[20000]; static int quit = 0; /* SIGINT handler: set quit to 1 for graceful termination */ @@ -94,7 +94,7 @@ add_contents( coap_pdu_t *pdu, unsigned int mediatype, unsigned int len, unsigne unsigned char ct = COAP_MEDIATYPE_APPLICATION_LINK_FORMAT; if (!pdu) return; - + /* add content-encoding */ coap_add_option(pdu, COAP_OPTION_CONTENT_TYPE, 1, &ct); @@ -131,26 +131,26 @@ mediatype_matches(coap_pdu_t *pdu, unsigned char mediatype) { if ( COAP_OPT_VALUE(*accept)[t] == mediatype ) return 1; } - + return 0; } /* Check if provided path name is a valid CoAP URI path. */ -int +int is_valid(char *prefix, unsigned char *path, unsigned int length) { - enum { START, PATH, DOT, DOTDOT } state; + enum { START, PATH, DOT, DOTDOT } state; if (!path || length < strlen(prefix) || strncmp((char *)path, prefix, strlen(prefix)) != 0) return 0; - + path += strlen(prefix); length -= strlen(prefix); if ( length && *path == '/' ) { state = START; ++path; --length; - } else + } else state = PATH; while (length) { @@ -193,12 +193,12 @@ strnlen(const char *p, size_t maxlen) { for (n = 0; n < maxlen && p[n]; n++) ; return n; -} +} #endif -int -resource_wellknown(coap_context_t *ctx, coap_resource_t *resource, - unsigned char *mediatype, unsigned int offset, +int +resource_wellknown(coap_context_t *ctx, coap_resource_t *resource, + unsigned char *mediatype, unsigned int offset, unsigned char *buf, unsigned int *buflen, int *finished); @@ -228,7 +228,7 @@ handle_get(coap_context_t *ctx, coap_queue_t *node, void *data) { pdu = new_response(ctx, node, COAP_RESPONSE_200); if ( !pdu ) return NULL; - + add_contents( pdu, COAP_MEDIATYPE_TEXT_PLAIN, sizeof(INDEX) - 1, (unsigned char *)INDEX ); goto ok; } @@ -245,7 +245,7 @@ handle_get(coap_context_t *ctx, coap_queue_t *node, void *data) { if ( !resource ) { pdu = new_response(ctx, node, COAP_RESPONSE_404); if (tok) { - coap_add_option(pdu, COAP_OPTION_TOKEN, + coap_add_option(pdu, COAP_OPTION_TOKEN, COAP_OPT_LENGTH(*tok), COAP_OPT_VALUE(*tok)); } @@ -253,7 +253,7 @@ 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) && !mediatype_matches(node->pdu, resource->mediatype) ) { debug("media type mismatch\n"); return new_response(ctx, node, COAP_RESPONSE_415); @@ -261,7 +261,7 @@ handle_get(coap_context_t *ctx, coap_queue_t *node, void *data) { block = coap_check_option(node->pdu, COAP_OPTION_BLOCK); if ( block ) { - blk = coap_decode_var_bytes(COAP_OPT_VALUE(*block), + blk = coap_decode_var_bytes(COAP_OPT_VALUE(*block), COAP_OPT_LENGTH(*block)); blklen = 16 << (blk & 0x07); } else { @@ -274,17 +274,17 @@ handle_get(coap_context_t *ctx, coap_queue_t *node, void *data) { if ( resource->data ) { mediatype = resource->mediatype; - code = resource->data(&uri, &mediatype, - (blk & ~0x0f) << (blk & 0x07), buf, &blklen, + code = resource->data(&uri, &mediatype, + (blk & ~0x0f) << (blk & 0x07), buf, &blklen, &finished); } else { /* check if the well-known URI was requested */ - if (memcmp(uri.path.s, COAP_DEFAULT_URI_WELLKNOWN, + if (memcmp(uri.path.s, COAP_DEFAULT_URI_WELLKNOWN, MIN(uri.path.length, sizeof(COAP_DEFAULT_URI_WELLKNOWN) - 1)) == 0) { mediatype = resource->mediatype; code = resource_wellknown(ctx, resource, &mediatype, - (blk & ~0x0f) << (blk & 0x07), buf, &blklen, + (blk & ~0x0f) << (blk & 0x07), buf, &blklen, &finished); } else { /* no callback available, set code, blklen and finished manually @@ -297,21 +297,21 @@ handle_get(coap_context_t *ctx, coap_queue_t *node, void *data) { if ( !(pdu = new_response(ctx, node, code)) ) return NULL; - - if ( blklen > 0 ) { + + if ( blklen > 0 ) { /* add content-type */ - if ( mediatype != COAP_MEDIATYPE_ANY ) + if ( mediatype != COAP_MEDIATYPE_ANY ) coap_add_option(pdu, COAP_OPTION_CONTENT_TYPE, 1, &mediatype); /* set Max-age option unless resource->maxage is zero */ if (resource->maxage) { - coap_add_option(pdu, COAP_OPTION_MAXAGE, + coap_add_option(pdu, COAP_OPTION_MAXAGE, coap_encode_var_bytes(optbuf, resource->maxage), optbuf); } /* set Etag option unless resource->etag is zero */ if (*resource->etag) { - coap_add_option(pdu, COAP_OPTION_ETAG, + coap_add_option(pdu, COAP_OPTION_ETAG, strnlen((char *)resource->etag,4), resource->etag); } @@ -321,17 +321,17 @@ handle_get(coap_context_t *ctx, coap_queue_t *node, void *data) { duration = COAP_PSEUDOFP_DECODE_8_4(*COAP_OPT_VALUE(*sub)); debug("*** add subscription for %d seconds\n", duration); enc = COAP_PSEUDOFP_ENCODE_8_4_DOWN(duration, ls); - coap_add_option(pdu, COAP_OPTION_SUBSCRIPTION, 1, &enc); - + coap_add_option(pdu, COAP_OPTION_SUBSCRIPTION, 1, &enc); + /* refresh only if already subscribed */ - subscription = - coap_find_subscription(ctx, coap_uri_hash(&uri), &(node->remote), + subscription = + coap_find_subscription(ctx, coap_uri_hash(&uri), &(node->remote), 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), time(NULL)+duration); if (subscription) { if (token.length) { @@ -349,7 +349,7 @@ handle_get(coap_context_t *ctx, coap_queue_t *node, void *data) { } if (tok) { - coap_add_option(pdu, COAP_OPTION_TOKEN, + coap_add_option(pdu, COAP_OPTION_TOKEN, COAP_OPT_LENGTH(*tok), COAP_OPT_VALUE(*tok)); } @@ -359,7 +359,7 @@ handle_get(coap_context_t *ctx, coap_queue_t *node, void *data) { if ( block || !finished ) { blk = (blk & ~0x08) | (!finished << 3); /* add block option to PDU */ - coap_add_option(pdu, COAP_OPTION_BLOCK, + coap_add_option(pdu, COAP_OPTION_BLOCK, coap_encode_var_bytes(optbuf, blk), optbuf); } @@ -372,7 +372,7 @@ handle_get(coap_context_t *ctx, coap_queue_t *node, void *data) { /* FIXME: handle this case -- must send 500 or something */ } } else if (tok) { - coap_add_option(pdu, COAP_OPTION_TOKEN, + coap_add_option(pdu, COAP_OPTION_TOKEN, COAP_OPT_LENGTH(*tok), COAP_OPT_VALUE(*tok)); } @@ -381,7 +381,7 @@ handle_get(coap_context_t *ctx, coap_queue_t *node, void *data) { return pdu; } -int +int write_file(char *filename, unsigned char *text, int length) { FILE *file; ssize_t written; @@ -420,7 +420,7 @@ handle_put(coap_context_t *ctx, coap_queue_t *node, void *data) { if ( !(pdu = new_response(ctx, node, COAP_RESPONSE_200)) ) return NULL; - + /* create a zero-terminated string */ memcpy(filename, uri.path.s, uri.path.length); filename[uri.path.length] = '\0'; @@ -429,13 +429,13 @@ handle_put(coap_context_t *ctx, coap_queue_t *node, void *data) { written = write_file(filename, node->pdu->data, length); /* set etag from file's modification time (byte-order does not care) */ - if ( (stat(filename, &statbuf) == 0) && + if ( (stat(filename, &statbuf) == 0) && S_ISREG(statbuf.st_mode) ) { memcpy(resource->etag, &statbuf.st_mtime, sizeof(resource->etag)); } else { /* clear etag */ *resource->etag = 0; } - + if (written < length) return new_response(ctx, node, COAP_RESPONSE_500); @@ -445,9 +445,9 @@ handle_put(coap_context_t *ctx, coap_queue_t *node, void *data) { return pdu; } -int -resource_from_file(coap_uri_t *uri, - unsigned char *mediatype, unsigned int offset, +int +resource_from_file(coap_uri_t *uri, + unsigned char *mediatype, unsigned int offset, unsigned char *buf, unsigned int *buflen, int *finished); @@ -470,7 +470,7 @@ handle_post(coap_context_t *ctx, coap_queue_t *node, void *data) { return handle_put(ctx, node, data); /* create new resource */ - if ( !(r = coap_malloc( sizeof(coap_resource_t) ))) + 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); @@ -479,9 +479,9 @@ handle_post(coap_context_t *ctx, coap_queue_t *node, void *data) { * file system. We restrict the storage area to DATASINK_PREFIX. */ memset(name, 0, sizeof(name)); - namelen = snprintf(name, sizeof(name)-1, DATASINK_PREFIX "%lX", + namelen = snprintf(name, sizeof(name)-1, DATASINK_PREFIX "%lX", coap_uri_hash(&uri)); - + r->uri = coap_new_uri((unsigned char *)name, namelen); r->name = coap_new_string(uri.path.length); if (r->name) { @@ -493,14 +493,14 @@ handle_post(coap_context_t *ctx, coap_queue_t *node, void *data) { r->dirty = 1; r->writable = 1; r->data = resource_from_file; - + /* we know that r->uri.s is zero-terminated */ - length = + length = (unsigned char *)node->pdu->hdr + node->pdu->length - node->pdu->data; written = write_file((char *)r->uri->path.s, node->pdu->data, length); /* set etag from file's modification time (byte-order does not care) */ - if ( (stat((char *)r->uri->path.s, &statbuf) == 0) && + if ( (stat((char *)r->uri->path.s, &statbuf) == 0) && S_ISREG(statbuf.st_mode) ) { memcpy(r->etag, &statbuf.st_mtime, sizeof(r->etag)); } else { /* clear etag */ @@ -512,19 +512,19 @@ handle_post(coap_context_t *ctx, coap_queue_t *node, void *data) { return new_response(ctx, node, COAP_RESPONSE_500); } else coap_add_resource(ctx, r); - + /* create the response */ 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, 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); - if (tok) - coap_add_option(pdu, COAP_OPTION_TOKEN, + if (tok) + coap_add_option(pdu, COAP_OPTION_TOKEN, COAP_OPT_LENGTH(*tok), COAP_OPT_VALUE(*tok)); return pdu; @@ -544,7 +544,7 @@ handle_delete(coap_context_t *ctx, coap_queue_t *node, void *data) { r = coap_get_resource(ctx, &uri); if (!r) return new_response(ctx, node, COAP_RESPONSE_404); - + if (!r->writable) { debug("tried to remove resource that is read-only\n"); pdu = new_response(ctx, node, COAP_RESPONSE_400); @@ -554,31 +554,31 @@ handle_delete(coap_context_t *ctx, coap_queue_t *node, void *data) { if (FILENAME_MAX < uri.path.length) return new_response(ctx, node, COAP_RESPONSE_500); - + memcpy(filename, uri.path.s, uri.path.length); filename[uri.path.length] = '\0'; debug("unlink %s\n", filename); unlink(filename); - /* create the response */ + /* create the response */ pdu = new_response(ctx, node, COAP_RESPONSE_200); if (uri.na.length) - coap_add_option(pdu, COAP_OPTION_URI_AUTHORITY, + 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, + coap_add_option(pdu, COAP_OPTION_URI_PATH, uri.path.length, uri.path.s); - + tok = coap_check_option(node->pdu, COAP_OPTION_TOKEN); - if (tok) - coap_add_option(pdu, COAP_OPTION_TOKEN, + 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, + coap_add_option(pdu, COAP_OPTION_URI_QUERY, uri.query.length, uri.query.s); coap_delete_resource(ctx, coap_uri_hash(&uri)); @@ -586,7 +586,7 @@ handle_delete(coap_context_t *ctx, coap_queue_t *node, void *data) { return pdu; } -void +void message_handler(coap_context_t *ctx, coap_queue_t *node, void *data) { coap_pdu_t *pdu = NULL; @@ -599,7 +599,7 @@ message_handler(coap_context_t *ctx, coap_queue_t *node, void *data) { debug("dropped packet with unknown version %u\n", node->pdu->hdr->version); return; } - + switch (node->pdu->hdr->code) { case COAP_REQUEST_GET : pdu = handle_get(ctx, node, data); @@ -640,7 +640,7 @@ message_handler(coap_context_t *ctx, coap_queue_t *node, void *data) { } -void +void usage( const char *program, const char *version) { const char *p; @@ -677,7 +677,7 @@ join( coap_context_t *ctx, char *group_name ){ /* get the first suitable interface identifier */ for (ainfo = reslocal; ainfo != NULL; ainfo = ainfo->ai_next) { if ( ainfo->ai_family == AF_INET6 ) { - mreq.ipv6mr_interface = + mreq.ipv6mr_interface = ((struct sockaddr_in6 *)ainfo->ai_addr)->sin6_scope_id; break; } @@ -697,34 +697,34 @@ join( coap_context_t *ctx, char *group_name ){ for (ainfo = resmulti; ainfo != NULL; ainfo = ainfo->ai_next) { if ( ainfo->ai_family == AF_INET6 ) { - mreq.ipv6mr_multiaddr = + mreq.ipv6mr_multiaddr = ((struct sockaddr_in6 *)ainfo->ai_addr)->sin6_addr; break; } } - + result = setsockopt( ctx->sockfd, IPPROTO_IPV6, IPV6_JOIN_GROUP, (char *)&mreq, sizeof(mreq) ); - if ( result < 0 ) + if ( result < 0 ) perror("join: setsockopt"); finish: freeaddrinfo(resmulti); freeaddrinfo(reslocal); - + return result; } -int +int print_link(coap_resource_t *resource, unsigned char *buf, size_t buflen) { size_t n = 0; assert(resource); assert(buf); - if (buflen < resource->uri->path.length + 3) + if (buflen < resource->uri->path.length + 3) return -1; - + /* FIXME: calculate maximum length and return if longer than buflen */ buf[n++] = '<'; buf[n++] = '/'; @@ -755,21 +755,21 @@ print_link(coap_resource_t *resource, unsigned char *buf, size_t buflen) { n += snprintf((char *)(buf + n), buflen - n, " (read-only)"); } - + buf[n++] = '"'; } return n; } -int +int resource_wellknown(coap_context_t *ctx, - coap_resource_t *resource, - unsigned char *mediatype, unsigned int offset, + coap_resource_t *resource, + unsigned char *mediatype, unsigned int offset, unsigned char *buf, unsigned int *buflen, int *finished) { #define RESOURCE_BUFLEN 4000 - static unsigned char resources[RESOURCE_BUFLEN]; + static unsigned char resources[RESOURCE_BUFLEN]; size_t maxlen = 0; int n; coap_list_t *node; @@ -784,7 +784,7 @@ resource_wellknown(coap_context_t *ctx, debug("resource description too long, truncating\n"); resources[maxlen] = '\0'; break; - } + } maxlen += n; if (node->next) /* check if another entry follows */ @@ -801,7 +801,7 @@ resource_wellknown(coap_context_t *ctx, *mediatype = COAP_MEDIATYPE_APPLICATION_LINK_FORMAT; break; default : - *buflen = 0; + *buflen = 0; return COAP_RESPONSE_415; } @@ -817,9 +817,9 @@ resource_wellknown(coap_context_t *ctx, return COAP_RESPONSE_200; } -int -resource_time(coap_uri_t *uri, - unsigned char *mediatype, unsigned int offset, +int +resource_time(coap_uri_t *uri, + unsigned char *mediatype, unsigned int offset, unsigned char *buf, unsigned int *buflen, int *finished) { static unsigned char b[400]; @@ -864,9 +864,9 @@ resource_time(coap_uri_t *uri, return COAP_RESPONSE_200; } -int -resource_lipsum(coap_uri_t *uri, - unsigned char *mediatype, unsigned int offset, +int +resource_lipsum(coap_uri_t *uri, + unsigned char *mediatype, unsigned int offset, unsigned char *buf, unsigned int *buflen, int *finished) { static unsigned char verylargebuf[] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris fermentum, lacus elementum venenatis aliquet, tortor risus laoreet sapien, a vulputate libero dolor ut odio. Vivamus congue elementum fringilla. Suspendisse porttitor, lectus sed gravida volutpat, dolor magna gravida massa, id fermentum lectus mi quis erat. Suspendisse lacinia, libero in euismod bibendum, magna nisi tempus lacus, eu suscipit augue nisi vel nulla. Praesent gravida lacus nec elit vestibulum sit amet rhoncus dui fringilla. Quisque diam lacus, ullamcorper non consectetur vitae, pellentesque eget lectus. Vestibulum velit nulla, venenatis vel mattis at, scelerisque nec mauris. Nulla facilisi. Mauris vel erat mi. Morbi et nulla nibh, vitae cursus eros. In convallis, magna egestas dictum porttitor, diam magna sagittis nisi, rhoncus tincidunt ligula felis sed mauris. Pellentesque pulvinar ante id velit convallis in porttitor justo imperdiet. Curabitur viverra placerat tincidunt. Vestibulum justo lacus, sollicitudin in facilisis vel, tempus nec erat. Duis varius viverra aliquet. In tempor varius elit vel pharetra. Sed mattis, quam in pulvinar ullamcorper, est ipsum tempor dui, at fringilla magna sem in sapien. Phasellus sollicitudin ornare sem, nec porta libero tempus vitae. Maecenas posuere pulvinar dictum. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Cras eros mauris, pulvinar tempor facilisis ut, condimentum in magna. Nullam eget ipsum sit amet lacus massa nunc."; @@ -895,9 +895,9 @@ resource_lipsum(coap_uri_t *uri, return COAP_RESPONSE_200; } -int -_resource_from_dir(char *filename, - unsigned char *mediatype, unsigned int offset, +int +_resource_from_dir(char *filename, + unsigned char *mediatype, unsigned int offset, unsigned char *buf, unsigned int *buflen, int *finished) { DIR *dir; @@ -937,7 +937,7 @@ _resource_from_dir(char *filename, continue; } - if (pos + overhead + namelen * 2 > sizeof(resource_buf) - 1) + if (pos + overhead + namelen * 2 > sizeof(resource_buf) - 1) break; /* broken */ if ( pos + overhead + namelen * 2 < offset) { @@ -946,11 +946,11 @@ _resource_from_dir(char *filename, pos += sprintf(resource_buf + pos, ";n=\"%s\",", filename, dirent->d_name, dirent->d_name); } - - if ( pos > offset + *buflen ) + + if ( pos > offset + *buflen ) break; } - + if (errno != 0) goto error; @@ -965,7 +965,7 @@ _resource_from_dir(char *filename, if ( (offset < pos) && (pos <= offset + *buflen) ) { *buflen = pos - offset - 1; *finished = 1; - } else + } else *finished = 0; memcpy(buf, resource_buf + offset, *buflen); @@ -979,9 +979,9 @@ _resource_from_dir(char *filename, return COAP_RESPONSE_500; } -int -resource_from_file(coap_uri_t *uri, - unsigned char *mediatype, unsigned int offset, +int +resource_from_file(coap_uri_t *uri, + unsigned char *mediatype, unsigned int offset, unsigned char *buf, unsigned int *buflen, int *finished) { static char filename[FILENAME_MAX+1]; @@ -1020,7 +1020,7 @@ resource_from_file(coap_uri_t *uri, code = COAP_RESPONSE_404; goto error; } - + if ( offset > statbuf.st_size ) { code = COAP_RESPONSE_400; goto error; @@ -1039,16 +1039,16 @@ resource_from_file(coap_uri_t *uri, code = COAP_RESPONSE_500; goto error; } - + *buflen = fread(buf, 1, *buflen, file); fclose(file); - + *finished = offset + *buflen >= statbuf.st_size; return COAP_RESPONSE_200; - error: - *buflen = 0; + error: + *buflen = 0; *finished = 1; return code; } @@ -1063,7 +1063,7 @@ resource_from_file(coap_uri_t *uri, memcpy((r)->name->s, (st), (r)->name->length); \ } -void +void init_resources(coap_context_t *ctx) { static const char *u_lipsum = "/lipsum"; static const char *d_lipsum = "some large text to test buffer sizes ( marks its end)"; @@ -1075,7 +1075,7 @@ init_resources(coap_context_t *ctx) { static const char *d_data = "POSTed data is stored here"; coap_resource_t *r; - if ( !(r = coap_malloc( sizeof(coap_resource_t) ))) + if ( !(r = coap_malloc( sizeof(coap_resource_t) ))) return; memset(r, 0, sizeof(coap_resource_t)); @@ -1086,7 +1086,7 @@ init_resources(coap_context_t *ctx) { r->writable = 0; coap_add_resource( ctx, r ); - if ( !(r = coap_malloc( sizeof(coap_resource_t) ))) + if ( !(r = coap_malloc( sizeof(coap_resource_t) ))) return; memset(r, 0, sizeof(coap_resource_t)); @@ -1099,7 +1099,7 @@ init_resources(coap_context_t *ctx) { r->maxage = 1209600; /* two weeks */ coap_add_resource( ctx, r ); - if ( !(r = coap_malloc( sizeof(coap_resource_t) ))) + if ( !(r = coap_malloc( sizeof(coap_resource_t) ))) return; memset(r, 0, sizeof(coap_resource_t)); @@ -1112,7 +1112,7 @@ init_resources(coap_context_t *ctx) { r->maxage = 1; coap_add_resource( ctx, r ); - if ( !(r = coap_malloc( sizeof(coap_resource_t) ))) + if ( !(r = coap_malloc( sizeof(coap_resource_t) ))) return; memset(r, 0, sizeof(coap_resource_t)); @@ -1125,7 +1125,7 @@ init_resources(coap_context_t *ctx) { write_file("filestorage",(unsigned char *)"initial text", 12); coap_add_resource( ctx, r ); - if ( !(r = coap_malloc( sizeof(coap_resource_t) ))) + if ( !(r = coap_malloc( sizeof(coap_resource_t) ))) return; memset(r, 0, sizeof(coap_resource_t)); @@ -1139,7 +1139,7 @@ init_resources(coap_context_t *ctx) { coap_add_resource(ctx, r); } -int +int main(int argc, char **argv) { coap_context_t *ctx; fd_set readfds; @@ -1178,9 +1178,9 @@ main(int argc, char **argv) { signal(SIGINT, handle_sigint); while ( !quit ) { - FD_ZERO(&readfds); + FD_ZERO(&readfds); FD_SET( ctx->sockfd, &readfds ); - + nextpdu = coap_peek_next( ctx ); time(&now); @@ -1200,7 +1200,7 @@ main(int argc, char **argv) { timeout = &tv; } result = select( FD_SETSIZE, &readfds, 0, 0, timeout ); - + if ( result < 0 ) { /* error */ if (errno != EINTR) perror("select"); @@ -1208,7 +1208,7 @@ main(int argc, char **argv) { if ( FD_ISSET( ctx->sockfd, &readfds ) ) { coap_read( ctx ); /* read received data */ coap_dispatch( ctx ); /* and dispatch PDUs from receivequeue */ - } + } } else { /* timeout */ coap_check_resource_list( ctx ); coap_check_subscriptions( ctx ); diff --git a/examples/tiny.c b/examples/tiny.c index 298c0f6c..d4be9b07 100644 --- a/examples/tiny.c +++ b/examples/tiny.c @@ -1,17 +1,17 @@ /* tiny -- tiny sender * * Copyright (C) 2010 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. @@ -58,7 +58,7 @@ make_pdu( unsigned int value ) { return pdu; } -void +void usage( const char *program ) { const char *p; @@ -73,7 +73,7 @@ usage( const char *program ) { program, program ); } -int +int main(int argc, char **argv) { coap_context_t *ctx; struct timeval tv; @@ -97,7 +97,7 @@ main(int argc, char **argv) { dst.sin6_port = htons( COAP_DEFAULT_PORT ); if ( IN6_IS_ADDR_MULTICAST(&dst.sin6_addr) ) { - /* set socket options for multicast */ + /* set socket options for multicast */ if ( setsockopt( ctx->sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (char *)&hops, sizeof(hops) ) < 0 ) @@ -106,7 +106,7 @@ main(int argc, char **argv) { } while ( 1 ) { - + if (! (pdu = make_pdu( rand() & 0xfff ) ) ) return -1; @@ -115,7 +115,7 @@ main(int argc, char **argv) { tv.tv_sec = 5; tv.tv_usec = 0; select( 0, 0, 0, 0, &tv ); - + } coap_free_context( ctx ); diff --git a/list.c b/list.c index 8523fb00..99c92d75 100644 --- a/list.c +++ b/list.c @@ -1,17 +1,17 @@ /* list.c -- CoAP list structures * * Copyright (C) 2010 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. @@ -29,7 +29,7 @@ coap_insert(coap_list_t **queue, coap_list_t *node, coap_list_t *p, *q; if ( !queue || !node ) return 0; - + /* set queue head if empty */ if ( !*queue ) { *queue = node; @@ -49,29 +49,29 @@ coap_insert(coap_list_t **queue, coap_list_t *node, p = q; q = q->next; } while ( q && order( node->data, q->data ) >= 0); - + /* insert new item */ node->next = q; p->next = node; return 1; } -int +int coap_delete(coap_list_t *node) { - if ( !node ) + if ( !node ) return 0; - if ( node->delete ) + if ( node->delete ) node->delete( node->data ); coap_free( node->data ); - coap_free( node ); + coap_free( node ); return 1; } void coap_delete_list(coap_list_t *queue) { - if ( !queue ) + if ( !queue ) return; coap_delete_list( queue->next ); diff --git a/list.h b/list.h index a2d124d3..2e39063d 100644 --- a/list.h +++ b/list.h @@ -1,17 +1,17 @@ /* list.h -- CoAP list structures * * Copyright (C) 2010 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. @@ -37,7 +37,7 @@ typedef struct coap_linkedlistnode coap_list_t; * Adds node to given queue, ordered by specified order function. Returns 1 * when insert was successful, 0 otherwise. */ -int coap_insert(coap_list_t **queue, coap_list_t *node, +int coap_insert(coap_list_t **queue, coap_list_t *node, int (*order)(void *, void *) ); /* destroys specified node */ diff --git a/mem.h b/mem.h index 8879d63d..6369efe8 100644 --- a/mem.h +++ b/mem.h @@ -2,17 +2,17 @@ * Currently, this is just a dummy for malloc/free * * Copyright (C) 2010 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. diff --git a/net.c b/net.c index f9796470..c443f2f3 100644 --- a/net.c +++ b/net.c @@ -1,17 +1,17 @@ /* net.c -- CoAP network interface * * Copyright (C) 2010 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. @@ -49,13 +49,13 @@ extern int snprintf(char *str, size_t size, const char *format, ...); ** some functions for debugging ************************************************************************/ -void -for_each_option(coap_pdu_t *pdu, +void +for_each_option(coap_pdu_t *pdu, void (*f)(coap_opt_t *, unsigned char, unsigned int, const unsigned char *) ) { unsigned char cnt; coap_opt_t *opt; unsigned char opt_code = 0; - + if (! pdu ) return; @@ -70,7 +70,7 @@ for_each_option(coap_pdu_t *pdu, unsigned int -print_readable( const unsigned char *data, unsigned int len, +print_readable( const unsigned char *data, unsigned int len, unsigned char *result, unsigned int buflen, int encode_always ) { static unsigned char hex[] = "0123456789ABCDEF"; unsigned int cnt = 0; @@ -83,27 +83,27 @@ print_readable( const unsigned char *data, unsigned int len, *result++ = '\\'; *result++ = 'x'; *result++ = hex[(*data & 0xf0) >> 4]; - *result++ = hex[*data & 0x0f ]; + *result++ = hex[*data & 0x0f ]; cnt += 4; - } else + } else break; } ++data; --len; } - + *result = '\0'; return cnt; } -void +void show( coap_opt_t *opt, unsigned char type, unsigned int len, const unsigned char *data ) { static unsigned char buf[COAP_MAX_PDU_SIZE]; print_readable( data, len, buf, COAP_MAX_PDU_SIZE, 0 ); printf(" %d:'%s'", type, buf ); } -void +void show_data( coap_pdu_t *pdu ) { static unsigned char buf[COAP_MAX_PDU_SIZE]; unsigned int len = (int)( (unsigned char *)pdu->hdr + pdu->length - pdu->data ); @@ -138,7 +138,7 @@ coap_insert_node(coap_queue_t **queue, coap_queue_t *node, coap_queue_t *p, *q; if ( !queue || !node ) return 0; - + /* set queue head if empty */ if ( !*queue ) { *queue = node; @@ -158,27 +158,27 @@ coap_insert_node(coap_queue_t **queue, coap_queue_t *node, p = q; q = q->next; } while ( q && order( node, q ) >= 0 ); - + /* insert new item */ node->next = q; p->next = node; return 1; } -int +int coap_delete_node(coap_queue_t *node) { - if ( !node ) + if ( !node ) return 0; coap_free( node->pdu ); - coap_free( node ); + coap_free( node ); return 1; } void coap_delete_all(coap_queue_t *queue) { - if ( !queue ) + if ( !queue ) return; coap_delete_all( queue->next ); @@ -208,7 +208,7 @@ coap_peek_next( coap_context_t *context ) { coap_queue_t * coap_pop_next( coap_context_t *context ) { - coap_queue_t *next; + coap_queue_t *next; if ( !context || !context->sendqueue ) return NULL; @@ -240,7 +240,7 @@ coap_new_context(in_port_t port) { perror("coap_new_context: socket"); goto onerror; } - + if ( setsockopt( c->sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse) ) < 0 ) perror("setsockopt SO_REUSEADDR"); @@ -259,16 +259,16 @@ coap_new_context(in_port_t port) { perror("coap_new_context: bind"); goto onerror; } - + do { addr.sin6_port = htons( ++port ); } while (bind (c->sockfd, (struct sockaddr *)&addr, sizeof addr) < 0); - } - + } + return c; onerror: - if ( c->sockfd >= 0 ) + if ( c->sockfd >= 0 ) close ( c->sockfd ); coap_free( c ); return NULL; @@ -308,9 +308,9 @@ coap_send_impl( coap_context_t *context, const struct sockaddr_in6 *dst, coap_pd coap_show_pdu( pdu ); #endif - bytes_written = sendto( context->sockfd, pdu->hdr, pdu->length, 0, + bytes_written = sendto( context->sockfd, pdu->hdr, pdu->length, 0, (const struct sockaddr *)dst, sizeof( *dst )); - + if ( free_pdu ) coap_delete_pdu( pdu ); @@ -331,7 +331,7 @@ int _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; @@ -372,7 +372,7 @@ coap_retransmit( coap_context_t *context, coap_queue_t *node ) { node->retransmit_cnt, ntohs(node->pdu->hdr->id)); return coap_send_impl( context, &node->remote, node->pdu, 0 ); - } + } /* no more retransmissions, remove node from system */ @@ -385,10 +385,10 @@ coap_retransmit( coap_context_t *context, coap_queue_t *node ) { int _order_transaction_id( coap_queue_t *lhs, coap_queue_t *rhs ) { return ( lhs && rhs && lhs->pdu && rhs->pdu && - ( lhs->pdu->hdr->id < lhs->pdu->hdr->id ) ) - ? -1 + ( lhs->pdu->hdr->id < lhs->pdu->hdr->id ) ) + ? -1 : 1; -} +} int coap_read( coap_context_t *ctx ) { @@ -413,17 +413,17 @@ coap_read( coap_context_t *ctx ) { } if ( bytes_read < sizeof(coap_hdr_t) ) { - debug("coap_read: discarded invalid frame\n" ); + debug("coap_read: discarded invalid frame\n" ); return -1; } if ( pdu->version != COAP_DEFAULT_VERSION ) { - debug("coap_read: unknown protocol version\n" ); + debug("coap_read: unknown protocol version\n" ); return -1; } node = coap_new_node(); - if ( !node ) + if ( !node ) return -1; node->pdu = coap_new_pdu(); @@ -438,7 +438,7 @@ coap_read( coap_context_t *ctx ) { /* "parse" received PDU by filling pdu structure */ memcpy( node->pdu->hdr, buf, bytes_read ); node->pdu->length = bytes_read; - + /* finally calculate beginning of data block */ options_end( node->pdu, &opt ); @@ -449,7 +449,7 @@ coap_read( coap_context_t *ctx ) { /* and add new node to receive queue */ coap_insert_node( &ctx->recvqueue, node, _order_transaction_id ); - + #ifndef NDEBUG if ( inet_ntop(src.sin6_family, &src.sin6_addr, addr, INET6_ADDRSTRLEN) == 0 ) { perror("coap_read: inet_ntop"); @@ -470,7 +470,7 @@ coap_remove_transaction( coap_queue_t **queue, coap_tid_t id ) { return 0; /* replace queue head if PDU's time is less than head's time */ - + q = *queue; if ( id == q->pdu->hdr->id ) { /* found transaction */ *queue = q->next; @@ -484,16 +484,16 @@ coap_remove_transaction( coap_queue_t **queue, coap_tid_t id ) { p = q; q = q->next; } while ( q && id == q->pdu->hdr->id ); - + if ( q ) { /* found transaction */ p->next = q->next; coap_delete_node( q ); debug("*** removed transaction %u\n", ntohs(id)); - return 1; + return 1; } return 0; - + } coap_queue_t * @@ -506,9 +506,9 @@ coap_find_transaction(coap_queue_t *queue, coap_tid_t id) { return queue; } return NULL; -} +} -void +void coap_dispatch( coap_context_t *context ) { coap_queue_t *node, *sent; coap_uri_t uri; @@ -519,7 +519,7 @@ coap_dispatch( coap_context_t *context ) { #define MAXMSG 60 static unsigned char msg[MAXMSG]; - if ( !context ) + if ( !context ) return; while ( context->recvqueue ) { @@ -541,10 +541,10 @@ coap_dispatch( coap_context_t *context ) { fprintf(stderr, "* got RST for transaction %u\n", ntohs(node->pdu->hdr->id) ); sent = coap_find_transaction(context->sendqueue, node->pdu->hdr->id); - if (sent && coap_get_request_uri(sent->pdu, &uri)) { + if (sent && coap_get_request_uri(sent->pdu, &uri)) { /* The easy way: we still have the transaction that has caused * the trouble.*/ - + coap_delete_subscription(context, coap_uri_hash(&uri), &node->remote); } else { /* ?? */ @@ -554,7 +554,7 @@ 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_check_critical(node->pdu, &opt) != 0 ) goto cleanup; break; case COAP_MESSAGE_CON : /* check for unknown critical options */ @@ -571,34 +571,34 @@ coap_dispatch( coap_context_t *context ) { /* 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 ) == + + if ( coap_send( context, &node->remote, response ) == COAP_INVALID_TID ) { debug("coap_dispatch: error sending reponse"); coap_delete_pdu(response); } - } + } goto cleanup; } break; } - + /* pass message to upper layer if a specific handler was registered */ - if ( context->msg_handler ) + if ( context->msg_handler ) context->msg_handler( context, node, NULL ); - + cleanup: coap_delete_node( node ); - } + } } -void +void coap_register_message_handler( coap_context_t *context, coap_message_handler_t handler) { context->msg_handler = (void (*)( void *, coap_queue_t *, void *)) handler; } -int +int coap_can_exit( coap_context_t *context ) { return !context || (context->recvqueue == NULL && context->sendqueue == NULL); } diff --git a/net.h b/net.h index 838c4b3f..bd03fb33 100644 --- a/net.h +++ b/net.h @@ -1,17 +1,17 @@ /* net.h -- CoAP network interface * * Copyright (C) 2010 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. @@ -32,7 +32,7 @@ struct coap_listnode { time_t t; /* when to send PDU for the next time */ unsigned char retransmit_cnt; /* retransmission counter, will be removed when zero */ - + struct sockaddr_in6 remote; /* remote address */ coap_pdu_t *pdu; /* the CoAP PDU to send */ @@ -41,7 +41,7 @@ struct coap_listnode { typedef struct coap_listnode coap_queue_t; /* adds node to given queue, ordered by specified order function */ -int coap_insert_node(coap_queue_t **queue, coap_queue_t *node, +int coap_insert_node(coap_queue_t **queue, coap_queue_t *node, int (*order)(coap_queue_t *, coap_queue_t *node) ); /* destroys specified node */ @@ -64,13 +64,13 @@ typedef struct { typedef void (*coap_message_handler_t)( coap_context_t *, coap_queue_t *, void *); -/** +/** * Registers a new message handler that is called whenever a new PDU * was received. Note that the transactions are handled on the lower * layer previously to stop retransmissions, e.g. */ void coap_register_message_handler( coap_context_t *context, coap_message_handler_t handler); -/** +/** * Registers a new handler function that is called when a RST message * has been received. */ diff --git a/pdu.c b/pdu.c index 504053a4..25841535 100644 --- a/pdu.c +++ b/pdu.c @@ -1,17 +1,17 @@ /* pdu.c -- CoAP message structure * * Copyright (C) 2010 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. @@ -34,14 +34,14 @@ coap_new_pdu() { perror("new_pdu: malloc"); return NULL; } - + /* initialize PDU */ memset(pdu, 0, sizeof(coap_pdu_t) + COAP_MAX_PDU_SIZE ); pdu->hdr = (coap_hdr_t *) ( (unsigned char *)pdu + sizeof(coap_pdu_t) ); pdu->hdr->version = COAP_DEFAULT_VERSION; pdu->hdr->id = ntohs( COAP_INVALID_TID ); - /* data points after the header; when options are added, the data + /* data points after the header; when options are added, the data pointer is moved to the back */ pdu->length = sizeof(coap_hdr_t); pdu->data = (unsigned char *)pdu->hdr + pdu->length; @@ -49,28 +49,28 @@ coap_new_pdu() { return pdu; } -void +void coap_delete_pdu(coap_pdu_t *pdu) { coap_free( pdu ); } #define options_start(p) ((coap_opt_t *) ( (unsigned char *)p->hdr + sizeof ( coap_hdr_t ) )) -int +int coap_add_option(coap_pdu_t *pdu, unsigned char type, unsigned int len, const unsigned char *data) { unsigned char cnt; coap_opt_t *opt; unsigned char opt_code = 0; - if (!pdu) + if (!pdu) return -1; /* get last option from pdu to calculate the delta */ - + opt = options_start( pdu ); for ( cnt = pdu->hdr->optcnt; cnt; --cnt ) { opt_code += COAP_OPT_DELTA(*opt); - opt = (coap_opt_t *)( (unsigned char *)opt + COAP_OPT_SIZE(*opt) ); + opt = (coap_opt_t *)( (unsigned char *)opt + COAP_OPT_SIZE(*opt) ); } if ( type < opt_code ) { @@ -94,13 +94,13 @@ coap_add_option(coap_pdu_t *pdu, unsigned char type, unsigned int len, const uns COAP_OPT_SETDELTA( *opt, (COAP_OPTION_NOOP * (cnt+1)) - opt_code ); opt_code += COAP_OPT_DELTA(*opt); - opt = (coap_opt_t *)( (unsigned char *)opt + COAP_OPT_SIZE(*opt) ); + opt = (coap_opt_t *)( (unsigned char *)opt + COAP_OPT_SIZE(*opt) ); } /* here, the actual option is added (delta <= 15) */ pdu->hdr->optcnt += 1; COAP_OPT_SETDELTA( *opt, type - opt_code ); - + COAP_OPT_SETLENGTH( *opt, len ); memcpy(COAP_OPT_VALUE(*opt), data, len); pdu->data = (unsigned char *)COAP_OPT_VALUE(*opt) + len ; @@ -115,21 +115,21 @@ coap_check_option(coap_pdu_t *pdu, unsigned char type) { coap_opt_t *opt; unsigned char opt_code = 0; - if (!pdu) + if (!pdu) return NULL; /* get last option from pdu to calculate the delta */ - + opt = options_start( pdu ); for ( cnt = pdu->hdr->optcnt; cnt && opt_code < type; --cnt ) { opt_code += COAP_OPT_DELTA(*opt); - + /* check if current option is the one we are looking for */ if (type == opt_code) return opt; /* yes, return */ /* goto next option */ - opt = (coap_opt_t *)( (unsigned char *)opt + COAP_OPT_SIZE(*opt) ); + opt = (coap_opt_t *)( (unsigned char *)opt + COAP_OPT_SIZE(*opt) ); } return NULL; @@ -140,15 +140,15 @@ coap_check_critical(coap_pdu_t *pdu, coap_opt_t **option) { unsigned char cnt; unsigned char opt_code = 0; - if (!pdu) + if (!pdu) goto success; /* get last option from pdu to calculate the delta */ - + *option = options_start( pdu ); for ( cnt = pdu->hdr->optcnt; cnt; --cnt ) { opt_code += COAP_OPT_DELTA(**option); - + /* check if current option is critical */ if (opt_code & 0x01) { switch (opt_code) { /* skip known options */ @@ -160,14 +160,14 @@ coap_check_critical(coap_pdu_t *pdu, coap_opt_t **option) { case COAP_OPTION_URI_QUERY : break; default: /* return first unknown critical option */ - fprintf(stderr, + fprintf(stderr, "coap_check_critical: unknown critical option %d\n", opt_code); return opt_code; } } /* goto next option */ - *option = (coap_opt_t *)( (unsigned char *)*option + COAP_OPT_SIZE(**option) ); + *option = (coap_opt_t *)( (unsigned char *)*option + COAP_OPT_SIZE(**option) ); } success: @@ -175,11 +175,11 @@ coap_check_critical(coap_pdu_t *pdu, coap_opt_t **option) { return 0; } -int +int coap_add_data(coap_pdu_t *pdu, unsigned int len, const unsigned char *data) { if ( !pdu ) return 0; - + if ( pdu->length + len > COAP_MAX_PDU_SIZE ) { #ifndef NDEBUG fprintf(stderr, "coap_add_data: cannot add: data too large for PDU\n"); @@ -192,12 +192,12 @@ coap_add_data(coap_pdu_t *pdu, unsigned int len, const unsigned char *data) { return 1; } -int +int coap_get_data(coap_pdu_t *pdu, unsigned int *len, unsigned char **data) { if ( !pdu ) return 0; - if ( pdu->data < (unsigned char *)pdu->hdr + pdu->length ) { + if ( pdu->data < (unsigned char *)pdu->hdr + pdu->length ) { /* pdu contains data */ *len = (unsigned char *)pdu->hdr + pdu->length - pdu->data; @@ -206,33 +206,33 @@ coap_get_data(coap_pdu_t *pdu, unsigned int *len, unsigned char **data) { *len = 0; *data = NULL; } - + return 1; } -int +int coap_get_request_uri(coap_pdu_t *pdu, coap_uri_t *result) { coap_opt_t *opt; - + if (!pdu || !result) return 0; memset(result, 0, sizeof(*result)); - if ((opt = coap_check_option(pdu, COAP_OPTION_URI_AUTHORITY))) + if ((opt = coap_check_option(pdu, COAP_OPTION_URI_AUTHORITY))) COAP_SET_STR(&result->na, COAP_OPT_LENGTH(*opt), COAP_OPT_VALUE(*opt)); - if ((opt = coap_check_option(pdu, COAP_OPTION_URI_PATH))) + if ((opt = coap_check_option(pdu, COAP_OPTION_URI_PATH))) COAP_SET_STR(&result->path, COAP_OPT_LENGTH(*opt), COAP_OPT_VALUE(*opt)); - if ((opt = coap_check_option(pdu, COAP_OPTION_URI_QUERY))) + if ((opt = coap_check_option(pdu, COAP_OPTION_URI_QUERY))) COAP_SET_STR(&result->query, COAP_OPT_LENGTH(*opt), COAP_OPT_VALUE(*opt)); return 1; } #if 0 -int +int coap_encode_pdu(coap_pdu_t *pdu) { } diff --git a/pdu.h b/pdu.h index 9193c5e6..3d56d9e1 100644 --- a/pdu.h +++ b/pdu.h @@ -1,17 +1,17 @@ /* pdu.h -- CoAP message structure * * Copyright (C) 2010 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. @@ -118,7 +118,7 @@ /* CoAP transaction id */ /*typedef unsigned short coap_tid_t; */ -typedef int coap_tid_t; +typedef int coap_tid_t; #define COAP_INVALID_TID -1 #ifdef WORDS_BIGENDIAN @@ -196,7 +196,7 @@ typedef union { /** * Structures for more convenient handling of options. (To be used with ordered - * coap_list_t.) The option's data will be added to the end of the coap_option + * coap_list_t.) The option's data will be added to the end of the coap_option * structure (see macro COAP_OPTION_DATA). */ typedef struct { @@ -234,7 +234,7 @@ typedef struct { /** Options in coap_pdu_t are accessed with the macro COAP_OPTION. */ #define COAP_OPTION(node) ((coap_option *)(node)->options) -/** +/** * Creates a new CoAP PDU. The object is created on the heap and must be released * using delete_pdu(); */ @@ -246,15 +246,15 @@ void coap_delete_pdu(coap_pdu_t *); int coap_encode_pdu(coap_pdu_t *); #endif -/** - * Adds option of given type to pdu that is passed as first parameter. coap_add_option() +/** + * Adds option of given type to pdu that is passed as first parameter. coap_add_option() * destroys the PDU's data, so coap_add_data must be called after all options have been * added. */ int coap_add_option(coap_pdu_t *pdu, unsigned char type, unsigned int len, const unsigned char *data); coap_opt_t *coap_check_option(coap_pdu_t *pdu, unsigned char type); -/** +/** * Checks for critical options that we do not know, as requests * containing unknown critical options must be discarded. The function * returns a pointer to the first unknown critical option in the given @@ -264,8 +264,8 @@ coap_opt_t *coap_check_option(coap_pdu_t *pdu, unsigned char type); */ int coap_check_critical(coap_pdu_t *pdu, coap_opt_t **option); -/** - * Adds given data to the pdu that is passed as first parameter. Note that the PDU's +/** + * Adds given data to the pdu that is passed as first parameter. Note that the PDU's * data is destroyed by coap_add_option(). */ int coap_add_data(coap_pdu_t *pdu, unsigned int len, const unsigned char *data); @@ -278,9 +278,9 @@ int coap_add_data(coap_pdu_t *pdu, unsigned int len, const unsigned char *data); int coap_get_data(coap_pdu_t *pdu, unsigned int *len, unsigned char **data); /** - * Fills the given coap_uri_t object with the request URI components from + * Fills the given coap_uri_t object with the request URI components from * the PDU. - * @param pdu the PDU + * @param pdu the PDU * @param result the URI object to update * @return 1 if result has been updated, 0 otherwise, i.e. in case of error */ diff --git a/str.c b/str.c index 8196447c..341e6057 100644 --- a/str.c +++ b/str.c @@ -1,17 +1,17 @@ /* str.c -- strings to be used in the CoAP library * * Copyright (C) 2010 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. @@ -28,7 +28,7 @@ str *coap_new_string(size_t size) { perror("coap_new_string: malloc"); return NULL; } - + memset(s, 0, sizeof(str)); s->s = ((unsigned char *)s) + sizeof(str); return s; diff --git a/str.h b/str.h index a564e77b..75d8d0e1 100644 --- a/str.h +++ b/str.h @@ -1,17 +1,17 @@ /* str.h -- strings to be used in the CoAP library * * Copyright (C) 2010 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. @@ -32,7 +32,7 @@ typedef struct { /** * Returns a new string object with at least size bytes storage * allocated. The string must be released using coap_delete_string(); - */ + */ str *coap_new_string(size_t size); /** Deletes the given string and releases any memory allocated. */ diff --git a/subscribe.c b/subscribe.c index d50ccfa3..bb7a2e77 100644 --- a/subscribe.c +++ b/subscribe.c @@ -1,18 +1,18 @@ -/* subscribe.c -- subscription handling for CoAP +/* subscribe.c -- subscription handling for CoAP * see draft-hartke-coap-observe-01 * * Copyright (C) 2010 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. @@ -30,7 +30,7 @@ #define HMASK (ULONG_MAX >> 1) void -notify(coap_context_t *context, coap_resource_t *res, +notify(coap_context_t *context, coap_resource_t *res, coap_subscription_t *sub, unsigned int duration, int code) { coap_pdu_t *pdu; int ls, finished=0; @@ -49,28 +49,28 @@ notify(coap_context_t *context, coap_resource_t *res, /* 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, + coap_add_option (pdu, COAP_OPTION_URI_AUTHORITY, + res->uri->na.length, res->uri->na.s ); if (res->uri->path.length) - coap_add_option (pdu, COAP_OPTION_URI_PATH, - res->uri->path.length, + coap_add_option (pdu, COAP_OPTION_URI_PATH, + res->uri->path.length, res->uri->path.s); d = COAP_PSEUDOFP_ENCODE_8_4_DOWN(duration, ls); - + coap_add_option ( pdu, COAP_OPTION_SUBSCRIPTION, 1, &d ); if (sub->token.length) { - coap_add_option (pdu, COAP_OPTION_TOKEN, - sub->token.length, - sub->token.s); + coap_add_option (pdu, COAP_OPTION_TOKEN, + sub->token.length, + sub->token.s); } - + if (res->uri->query.length) - coap_add_option (pdu, COAP_OPTION_URI_QUERY, - res->uri->query.length, + coap_add_option (pdu, COAP_OPTION_URI_QUERY, + res->uri->query.length, res->uri->query.s ); if (res->data) { @@ -82,26 +82,26 @@ notify(coap_context_t *context, coap_resource_t *res, /* TODO: add block option if not finished */ /* TODO: add mediatype */ } - + #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)); } #endif - if ( pdu && coap_send_confirmed(context, + if ( pdu && coap_send_confirmed(context, &sub->subscriber, pdu ) == COAP_INVALID_TID ) { debug("coap_check_resource_list: error sending notification\n"); coap_delete_pdu(pdu); - } + } } -void +void coap_check_resource_list(coap_context_t *context) { coap_list_t *res, *sub; coap_key_t key; time_t now; - if ( !context || !context->resources /* || !context->subscribers */) + if ( !context || !context->resources /* || !context->subscribers */) return; time(&now); @@ -113,7 +113,7 @@ coap_check_resource_list(coap_context_t *context) { for (sub = context->subscriptions; sub; sub = sub->next) { if ( COAP_SUBSCRIPTION(sub)->resource == key ) { /* notify subscriber */ - notify(context, COAP_RESOURCE(res), COAP_SUBSCRIPTION(sub), + notify(context, COAP_RESOURCE(res), COAP_SUBSCRIPTION(sub), COAP_SUBSCRIPTION(sub)->expires - now, COAP_RESPONSE_200); } @@ -135,7 +135,7 @@ coap_get_resource_from_key(coap_context_t *ctx, coap_key_t key) { return COAP_RESOURCE(node); } } - + return NULL; } @@ -144,14 +144,14 @@ coap_get_resource(coap_context_t *ctx, coap_uri_t *uri) { return uri ? coap_get_resource_from_key(ctx, coap_uri_hash(uri)) : NULL; } -void +void coap_check_subscriptions(coap_context_t *context) { time_t now; coap_list_t *node; #ifndef NDEBUG char addr[INET6_ADDRSTRLEN]; #endif - + if ( !context ) return; @@ -161,14 +161,14 @@ coap_check_subscriptions(coap_context_t *context) { 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)); } #endif #if 0 - notify(context, - coap_get_resource_from_key(context, COAP_SUBSCRIPTION(node)->resource), - COAP_SUBSCRIPTION(node), + notify(context, + coap_get_resource_from_key(context, COAP_SUBSCRIPTION(node)->resource), + COAP_SUBSCRIPTION(node), 0, COAP_RESPONSE_400); #endif context->subscriptions = node->next; @@ -184,11 +184,11 @@ coap_free_resource(void *res) { coap_delete_string(((coap_resource_t *)res)->name); } } - -coap_key_t + +coap_key_t _hash(coap_key_t init, const char *s) { int c; - + if ( s ) while ( (c = *s++) ) { init = ((init << 7) + init) + c; @@ -197,7 +197,7 @@ _hash(coap_key_t init, const char *s) { return init & HMASK; } -coap_key_t +coap_key_t _hash2(coap_key_t init, const unsigned char *s, unsigned int len) { if ( len && !s ) return COAP_INVALID_HASHKEY; @@ -205,16 +205,16 @@ _hash2(coap_key_t init, const unsigned char *s, unsigned int len) { while ( len-- ) { init = ((init << 7) + init) + *s++; } - + return init & HMASK; } - + coap_key_t coap_uri_hash(const coap_uri_t *uri) { return uri ? _hash2(0, uri->path.s, uri->path.length) : COAP_INVALID_HASHKEY; } -coap_key_t +coap_key_t coap_add_resource(coap_context_t *context, coap_resource_t *resource) { coap_list_t *node; @@ -234,11 +234,11 @@ coap_add_resource(coap_context_t *context, coap_resource_t *resource) { return coap_uri_hash( resource->uri ); } - + /** * Deletes the resource that is identified by key. Returns 1 if the resource was - * removed, 0 on error (e.g. if no such resource exists). + * removed, 0 on error (e.g. if no such resource exists). */ int coap_delete_resource(coap_context_t *context, coap_key_t key) { @@ -247,7 +247,7 @@ coap_delete_resource(coap_context_t *context, coap_key_t key) { if (!context || key == COAP_INVALID_HASHKEY) return 0; - for (prev = NULL, node = context->resources; node; + for (prev = NULL, node = context->resources; node; prev = node, node = node->next) { if (coap_uri_hash(COAP_RESOURCE(node)->uri) == key) { debug("removed key %lu (%s)\n",key,COAP_RESOURCE(node)->uri->path.s); @@ -260,7 +260,7 @@ coap_delete_resource(coap_context_t *context, coap_key_t key) { return 1; } } - return 0; + return 0; } coap_subscription_t * @@ -297,33 +297,33 @@ coap_list_push_first(coap_list_t **list, void *data, void (*delete)(void *) ) { } return node; -} +} int _order_subscription(void *a, void *b) { - if ( !a || !b ) + if ( !a || !b ) return a < b ? -1 : 1; - + return ((coap_subscription_t *)a)->expires < ((coap_subscription_t *)b)->expires ? -1 : 1; } -coap_key_t +coap_key_t coap_subscription_hash(coap_subscription_t *subscription) { if ( !subscription ) return COAP_INVALID_HASHKEY; - return _hash2( subscription->resource, (unsigned char *)&subscription->subscriber, + return _hash2( subscription->resource, (unsigned char *)&subscription->subscriber, sizeof(subscription->subscriber) ); } -coap_key_t +coap_key_t coap_add_subscription(coap_context_t *context, coap_subscription_t *subscription) { coap_list_t *node; if ( !context || !subscription ) return COAP_INVALID_HASHKEY; - - if ( !(node = coap_new_listnode(subscription, NULL)) ) + + if ( !(node = coap_new_listnode(subscription, NULL)) ) return COAP_INVALID_HASHKEY; if ( !coap_insert(&context->subscriptions, node, _order_subscription ) ) { @@ -331,11 +331,11 @@ coap_add_subscription(coap_context_t *context, return COAP_INVALID_HASHKEY; } - return coap_subscription_hash(subscription); + return coap_subscription_hash(subscription); } coap_subscription_t * -coap_find_subscription(coap_context_t *context, +coap_find_subscription(coap_context_t *context, coap_key_t hashkey, struct sockaddr_in6 *subscriber, str *token) { @@ -349,35 +349,35 @@ coap_find_subscription(coap_context_t *context, if (token) { /* do not proceed if tokens do not match */ if (token->length != COAP_SUBSCRIPTION(node)->token.length || - memcmp(token->s, COAP_SUBSCRIPTION(node)->token.s, + memcmp(token->s, COAP_SUBSCRIPTION(node)->token.s, token->length) != 0) continue; } if (subscriber->sin6_port == COAP_SUBSCRIPTION(node)->subscriber.sin6_port - && memcmp(&subscriber->sin6_addr, + && memcmp(&subscriber->sin6_addr, &COAP_SUBSCRIPTION(node)->subscriber.sin6_addr, sizeof(struct in6_addr)) == 0) return COAP_SUBSCRIPTION(node); } } - return NULL; + return NULL; } -int +int coap_delete_subscription(coap_context_t *context, - coap_key_t key, + coap_key_t key, struct sockaddr_in6 *subscriber) { coap_list_t *prev, *node; if (!context || !subscriber || key == COAP_INVALID_HASHKEY) return 0; - for (prev = NULL, node = context->subscriptions; node; + for (prev = NULL, node = context->subscriptions; node; prev = node, node = node->next) { if (COAP_SUBSCRIPTION(node)->resource == key) { if (subscriber->sin6_port == COAP_SUBSCRIPTION(node)->subscriber.sin6_port - && memcmp(&subscriber->sin6_addr, + && memcmp(&subscriber->sin6_addr, &COAP_SUBSCRIPTION(node)->subscriber.sin6_addr, sizeof(struct in6_addr)) == 0) { @@ -394,5 +394,5 @@ coap_delete_subscription(coap_context_t *context, } } } - return 0; + return 0; } diff --git a/subscribe.h b/subscribe.h index bf05ae7e..ac888247 100644 --- a/subscribe.h +++ b/subscribe.h @@ -1,18 +1,18 @@ -/* subscribe.h -- subscription handling for CoAP +/* subscribe.h -- subscription handling for CoAP * see draft-hartke-coap-observe-01 * * Copyright (C) 2010 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. @@ -41,19 +41,19 @@ typedef struct { unsigned int writable:1; /* set to 1 if resource can be changed using PUT */ /* cache-control */ - unsigned char etag[4]; /* version identifier for this resource + unsigned char etag[4]; /* version identifier for this resource * (zero terminated, first byte is zero if not set). */ unsigned int maxage; /* maximum cache time (zero means no Max-age option) */ - /** + /** * Callback function that copies the resource representation into the provided data - * buffer (PDU payload). finished is set to 1 to indicate that this was the last block + * buffer (PDU payload). finished is set to 1 to indicate that this was the last block * of buflen data for this resource representation, 0 means that data is not finished - * and a subsequent call with offset updated by buflen would yield more data (i.e. + * and a subsequent call with offset updated by buflen would yield more data (i.e. * the M-bit of CoAP's block option must be set if offset and buflen are selected * accordingly. - * When called, buflen must be set to the maximum length of buf that is to be filled - * with the mediatype representation of the resource identified by uri. + * When called, buflen must be set to the maximum length of buf that is to be filled + * with the mediatype representation of the resource identified by uri. * The mediatype must be set to the requested mediatype of COAP_MEDIATYPE_ANY if * none was given. On return, the mediatype will be set to the type that is * actually used. @@ -89,33 +89,33 @@ coap_key_t coap_add_resource(coap_context_t *context, coap_resource_t *); /** * Deletes the resource that is identified by key. Returns 1 if the resource was - * removed, 0 on error (e.g. if no such resource exists). + * removed, 0 on error (e.g. if no such resource exists). */ int coap_delete_resource(coap_context_t *context, coap_key_t key); /** * Creates a new subscription object filled with the given data. The storage * allocated for this object must be released using coap_free(). */ -coap_subscription_t *coap_new_subscription(coap_context_t *context, +coap_subscription_t *coap_new_subscription(coap_context_t *context, const coap_uri_t *resource, const struct sockaddr_in6 *subscriber, time_t expiry); /** - * Adds the given subsription object to the observer list. + * Adds the given subsription object to the observer list. * @param context The CoAP context * @param subscription A new subscription oobject created with coap_new_subscription() * @return A unique hash key for this resource or COAP_INVALID_HASHKEY on error. * The storage allocated for the subscription object is released when it is - * removed from the subscription list, unless the function has returned - * COAP_INVALID_HASHKEY. In this case, the storage must be released by the + * removed from the subscription list, unless the function has returned + * COAP_INVALID_HASHKEY. In this case, the storage must be released by the * caller of this function. */ -coap_key_t coap_add_subscription(coap_context_t *context, +coap_key_t coap_add_subscription(coap_context_t *context, coap_subscription_t *subscription); /** - * Returns the subscription from subscriber for the resource identified + * Returns the subscription from subscriber for the resource identified * by hashkey. When token is not NULL the subscription must have the * same token. * @param context The CoAP context @@ -125,7 +125,7 @@ coap_key_t coap_add_subscription(coap_context_t *context, * subscriber to identify its subscription. * @return The requested subscription object or NULL when not found. */ -coap_subscription_t * coap_find_subscription(coap_context_t *context, +coap_subscription_t * coap_find_subscription(coap_context_t *context, coap_key_t hashkey, struct sockaddr_in6 *subscriber, str *token); @@ -133,10 +133,10 @@ coap_subscription_t * coap_find_subscription(coap_context_t *context, * Removes a subscription from the subscription list stored in context and * releases the storage that was allocated for this subscription. * @param context The CoAP context. - * @param haskey The unique key that identifies the subscription to remove. + * @param haskey The unique key that identifies the subscription to remove. * @return 1 if a subscription was removed, 0 otherwise. */ -int coap_delete_subscription(coap_context_t *context, +int coap_delete_subscription(coap_context_t *context, coap_key_t hashkey, struct sockaddr_in6 *subscriber); diff --git a/uri.c b/uri.c index 481f61c9..e5f78b11 100644 --- a/uri.c +++ b/uri.c @@ -1,17 +1,17 @@ /* uri.c -- helper functions for URI treatment * * Copyright (C) 2010 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. @@ -31,25 +31,25 @@ #endif int -coap_split_uri(unsigned char *str, coap_uri_t *uri) { +coap_split_uri(unsigned char *str_var, coap_uri_t *uri) { unsigned char *p; - if ( !str || !uri ) + if ( !str_var || !uri ) return -1; memset( uri, 0, sizeof(coap_uri_t) ); /* find scheme */ - p = str; + p = str_var; while ( isalnum(*p) ) ++p; if ( *p != ':' ) { /* no scheme, reset p */ - p = str; + p = str_var; } else { /* scheme found, check if it is "coap" */ - if (memcmp(str, COAP_DEFAULT_SCHEME, - MIN(p - str, sizeof(COAP_DEFAULT_SCHEME) - 1)) != 0) { - debug("unknown URI scheme '%s'\n",str); + if (memcmp(str_var, COAP_DEFAULT_SCHEME, + MIN(p - str_var, sizeof(COAP_DEFAULT_SCHEME) - 1)) != 0) { + debug("unknown URI scheme '%s'\n",str_var); return -1; } *p++ = '\0'; @@ -59,60 +59,60 @@ coap_split_uri(unsigned char *str, coap_uri_t *uri) { p += 2; uri->na.s = p; - /* skip NA and port so that p and str finally point to path */ - while ( *p && *p != '/' && *p != '?') + /* skip NA and port so that p and str_var finally point to path */ + while ( *p && *p != '/' && *p != '?') ++p; - + uri->na.length = p - uri->na.s; - str = p; + str_var = p; #if 0 /* split server address and port */ if ( *uri->na == '[' ) { /* IPv6 address reference */ p = ++uri->na; - while ( *p && *p != ']' ) + while ( *p && *p != ']' ) ++p; *p++ = '\0'; } else { /* IPv4 address or hostname */ p = uri->na; - while ( *p && *p != ':' ) + while ( *p && *p != ':' ) ++p; } - + if ( *p == ':' ) { /* handle port */ *p++ = '\0'; uri->port = p; } #endif - } else - str = p; + } else + str_var = p; - /* str now points to the path or query if path is empty*/ + /* str_var now points to the path or query if path is empty*/ } /* split path and query */ - - if ( *str == '\0' ) + + if ( *str_var == '\0' ) return 0; - if (*str != '?') { - if (*str == '/') /* skip leading '/' */ - *str++ = '\0'; - uri->path.s = str; + if (*str_var != '?') { + if (*str_var == '/') /* skip leading '/' */ + *str_var++ = '\0'; + uri->path.s = str_var; } - while (*str && *str != '?') - str++; + while (*str_var && *str_var != '?') + str_var++; - if (*str == '?') { - *str++ = '\0'; + if (*str_var == '?') { + *str_var++ = '\0'; - if (*str) { - uri->query.s = str; + if (*str_var) { + uri->query.s = str_var; uri->query.length = strlen((char *)uri->query.s); } - } + } if (uri->path.s) uri->path.length = strlen((char *)uri->path.s); @@ -127,7 +127,7 @@ coap_new_uri(const unsigned char *uri, unsigned int length) { unsigned char *result = coap_malloc(length + 1 + sizeof(coap_uri_t)); if ( !result ) return NULL; - + memcpy(URI_DATA(result), uri, length); URI_DATA(result)[length] = '\0'; /* make it zero-terminated */ @@ -139,10 +139,10 @@ coap_uri_t * coap_clone_uri(const coap_uri_t *uri) { coap_uri_t *result; - if ( !uri ) + if ( !uri ) return NULL; - result = (coap_uri_t *)coap_malloc( uri->query.length + uri->na.length + + result = (coap_uri_t *)coap_malloc( uri->query.length + uri->na.length + uri->path.length + sizeof(coap_uri_t) + 1); if ( !result ) diff --git a/uri.h b/uri.h index a5970179..44459ccc 100644 --- a/uri.h +++ b/uri.h @@ -1,17 +1,17 @@ /* uri.h -- helper functions for URI treatment * * Copyright (C) 2010 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. @@ -30,22 +30,22 @@ typedef struct { /** * Splits given URI into pieces and fills the specified uri object accordingly. - * URI parts that are not available will be set to NULL in uri. The function + * URI parts that are not available will be set to NULL in uri. The function * returns -1 on error, 0 on success. Note that the passed str will be altered. */ int coap_split_uri(unsigned char *str, coap_uri_t *uri); /** * Creates a new coap_uri_t object from the specified URI. Returns the new - * object or NULL on error. The memory allocated by the new coap_uri_t - * must be released using coap_free(). + * object or NULL on error. The memory allocated by the new coap_uri_t + * must be released using coap_free(). * @param uri The URI path to copy. * @para length The length of uri. * @return New URI object or NULL on error. */ coap_uri_t *coap_new_uri(const unsigned char *uri, unsigned int length); -/** +/** * Clones the specified coap_uri_t object. Thie function allocates sufficient * memory to hold the coap_uri_t structure and its contents. The object must * be released with coap_free(). */