Applied patches from Markus Becker (thanks!)

* 0001-Trailing-whitespace-cleanup.patch
* 0001-Went-back-to-2.65-of-autoconf.patch
* 0002-Fix-compiler-warning-for-str.patch
* added several files to Makefile target .gitignore
This commit is contained in:
Olaf Bergmann committed 2010-11-22 13:01:00 +01:00
1 parent 23fc3b0246
commit 758b7f775e
22 files changed
+474 -474

No files matched your search

+76 -76
View File
@@ -1,17 +1,17 @@
/* coap-client -- simple CoAP client
*
* Copyright (C) 2010 Olaf Bergmann <bergmann@tzi.org>
*
*
* 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 */
+112 -112
View File
@@ -2,17 +2,17 @@
* as defined in draft-ietf-core-coap-01
*
* Copyright (C) 2010 Olaf Bergmann <bergmann@tzi.org>
*
*
* 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.<EOT>";
@@ -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, "</%s/%s>;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 (<EOT> 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 );
+8 -8
View File
@@ -1,17 +1,17 @@
/* tiny -- tiny sender
*
* Copyright (C) 2010 Olaf Bergmann <bergmann@tzi.org>
*
*
* 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 );