From a360c4633ecc8a41e82ce6ba89cde4b42aab79aa Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Sat, 24 Jul 2010 20:03:35 +0200 Subject: [PATCH] added token option --- examples/client.c | 18 +++++++-- main.c | 40 ++++++++++++++++--- net.c | 33 +--------------- subscribe.c | 99 +++++++++++++++++++++++++++++++++++------------ subscribe.h | 21 +++++++++- 5 files changed, 144 insertions(+), 67 deletions(-) diff --git a/examples/client.c b/examples/client.c index 0f8d88b2..05282157 100644 --- a/examples/client.c +++ b/examples/client.c @@ -283,14 +283,16 @@ usage( const char *program, const char *version) { fprintf( stderr, "%s v%s -- a small CoAP implementation\n" "(c) 2010 Olaf Bergmann \n\n" - "usage: %s [-b num] [-g group] [-p port] [-s num] [-t type...] URI\n\n" + "usage: %s [-b num] [-g group] [-m method] [-p port] [-s num] [-t type...] [-T string] URI\n\n" "\tURI can be an absolute or relative coap URI,\n" "\t-b size\t\tblock size to be used in GET/PUT/POST requests\n" "\t \t\t(value must be a mulitple of 16 not larger than 2048)\n" "\t-g group\tjoin the given multicast group\n" + "\t-m method\trequest method (get|put|post|delete)\n" "\t-p port\t\tlisten on specified port\n" "\t-s duration\t\tsubscribe for given duration [s]\n" - "\t-t type\t\taccepted content type (multiple occurrences allowed)\n", + "\t-t type\t\taccepted content type (multiple occurrences allowed)\n" + "\t-T token\t\tinclude specified token\n", program, version, program ); } @@ -462,6 +464,13 @@ cmdline_subscribe(char *arg) { 1, &duration), order_opts ); } +void +cmdline_token(char *arg) { + coap_insert( &optlist, new_option_node(COAP_OPTION_TOKEN, + strlen(arg), + (unsigned char *)arg), order_opts); +} + method_t cmdline_method(char *arg) { static char *methods[] = @@ -489,7 +498,7 @@ main(int argc, char **argv) { int opt; char *group = NULL; - while ((opt = getopt(argc, argv, "b:g:m:p:s:t:")) != -1) { + while ((opt = getopt(argc, argv, "b:g:m:p:s:t:T:")) != -1) { switch (opt) { case 'b' : cmdline_blocksize(optarg); @@ -509,6 +518,9 @@ main(int argc, char **argv) { case 't' : cmdline_content_type(optarg); break; + case 'T' : + cmdline_token(optarg); + break; default: usage( argv[0], VERSION ); exit( 1 ); diff --git a/main.c b/main.c index 9729fc13..d7c1b2e0 100644 --- a/main.c +++ b/main.c @@ -105,12 +105,14 @@ handle_get(coap_context_t *ctx, coap_queue_t *node, void *data) { coap_pdu_t *pdu; coap_uri_t uri; coap_resource_t *resource; - coap_opt_t *block, *ct, *sub; + coap_opt_t *block, *ct, *tok, *sub; + str token; unsigned int blklen, blk; int code, finished = 1; unsigned int ls; unsigned char duration, enc; unsigned char mediatype = COAP_MEDIATYPE_ANY; + coap_subscription_t *subscription; static unsigned char buf[COAP_MAX_PDU_SIZE]; static unsigned char optbuf[4]; @@ -180,10 +182,38 @@ handle_get(coap_context_t *ctx, coap_queue_t *node, void *data) { enc = COAP_PSEUDOFP_ENCODE_8_4_DOWN(duration, ls); coap_add_option(pdu, COAP_OPTION_SUBSCRIPTION, 1, &enc); - /* TODO: refresh only if already subscribed */ - coap_add_subscription(ctx, - coap_new_subscription(ctx, &uri, &(node->remote), - time(NULL)+duration)); + /* refresh only if already subscribed */ + token.length = 0; + tok = coap_check_option(node->pdu, COAP_OPTION_TOKEN); + if (tok) { + COAP_SET_STR(&token, COAP_OPT_LENGTH(*tok), COAP_OPT_VALUE(*tok)); + coap_add_option(pdu, COAP_OPTION_TOKEN, + COAP_OPT_LENGTH(*tok), + COAP_OPT_VALUE(*tok)); + } + + 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), + time(NULL)+duration); + if (subscription) { + if (token.length) { + /* TODO: copy token into subscription->token */ + subscription->token.s = (unsigned char *)coap_malloc(token.length); + if (subscription->token.s) { + subscription->token.length = token.length; + memcpy(subscription->token.s, token.s, token.length); + } + /* FIXME: else error? */ + } + coap_add_subscription(ctx, subscription); + } + } } /* add a block option when it has been requested explicitly or diff --git a/net.c b/net.c index 2ba16570..7a6bdde2 100644 --- a/net.c +++ b/net.c @@ -494,37 +494,6 @@ coap_find_transaction(coap_queue_t *queue, coap_tid_t id) { return NULL; } -int -coap_remove_subscription(coap_context_t *context, - 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; - 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, - &COAP_SUBSCRIPTION(node)->subscriber.sin6_addr, - sizeof(struct in6_addr)) == 0) { - - if (!prev) { - context->subscriptions = node->next; - coap_delete(node); - } else { - prev->next = node->next; - coap_delete(node); - } - return 1; - } - } - } - return 0; -} - void coap_dispatch( coap_context_t *context ) { coap_queue_t *node, *sent; @@ -552,7 +521,7 @@ coap_dispatch( coap_context_t *context ) { /* The easy way: we still have the transaction that has caused * the trouble.*/ - coap_remove_subscription(context, coap_uri_hash(&uri), &node->remote); + coap_delete_subscription(context, coap_uri_hash(&uri), &node->remote); } else { /* ?? */ } diff --git a/subscribe.c b/subscribe.c index 8a625a6b..9bc4c34b 100644 --- a/subscribe.c +++ b/subscribe.c @@ -34,24 +34,30 @@ notify(coap_context_t *context, coap_resource_t *res, /* FIXME: content-type and data (how about block?) */ if (res->uri->scheme.length) - coap_add_option ( pdu, COAP_OPTION_URI_SCHEME, - res->uri->scheme.length, - res->uri->scheme.s ); + coap_add_option (pdu, COAP_OPTION_URI_SCHEME, + res->uri->scheme.length, + res->uri->scheme.s ); if (res->uri->na.length) - coap_add_option ( pdu, COAP_OPTION_URI_AUTHORITY, - res->uri->na.length, - res->uri->na.s ); + 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, - res->uri->path.s); - + 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); + } + if (res->data) { length = (unsigned char *)pdu->hdr + COAP_MAX_PDU_SIZE - pdu->data; ct = res->mediatype; @@ -236,6 +242,8 @@ coap_new_subscription(coap_context_t *context, const coap_uri_t *resource, result->expires = expiry; memcpy( &result->subscriber, subscriber, sizeof(struct sockaddr_in6) ); + memset(&result->token, 0, sizeof(str)); + return result; } @@ -292,24 +300,65 @@ coap_add_subscription(coap_context_t *context, return coap_subscription_hash(subscription); } -int -coap_delete_subscription(coap_context_t *context, coap_key_t hashkey) { - coap_list_t *prev, *node; - - node = context->subscriptions; - if (COAP_SUBSCRIPTION(node)->resource == hashkey) { - context->subscriptions = context->subscriptions->next; - coap_delete(node); - return 1; - } +coap_subscription_t * +coap_find_subscription(coap_context_t *context, + coap_key_t hashkey, + struct sockaddr_in6 *subscriber, + str *token) { + coap_list_t *node; - for (prev = node, node = context->subscriptions; node; prev = node, node = node->next) { + if (!context || !subscriber || hashkey == COAP_INVALID_HASHKEY) + return NULL; + + for (node = context->subscriptions; node; node = node->next) { if (COAP_SUBSCRIPTION(node)->resource == hashkey) { - prev->next = node->next; - coap_delete(node); - return 1; + + 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, + token->length) != 0) + continue; + } + + if (subscriber->sin6_port == COAP_SUBSCRIPTION(node)->subscriber.sin6_port + && memcmp(&subscriber->sin6_addr, + &COAP_SUBSCRIPTION(node)->subscriber.sin6_addr, + sizeof(struct in6_addr)) == 0) + return COAP_SUBSCRIPTION(node); } } + return NULL; +} - return 0; +int +coap_delete_subscription(coap_context_t *context, + 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; + 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, + &COAP_SUBSCRIPTION(node)->subscriber.sin6_addr, + sizeof(struct in6_addr)) == 0) { + + if (!prev) { + context->subscriptions = node->next; + coap_free(COAP_SUBSCRIPTION(node)->token.s); + coap_delete(node); + } else { + prev->next = node->next; + coap_free(COAP_SUBSCRIPTION(node)->token.s); + coap_delete(node); + } + return 1; + } + } + } + return 0; } diff --git a/subscribe.h b/subscribe.h index 966b1a46..1dd67ed9 100644 --- a/subscribe.h +++ b/subscribe.h @@ -48,6 +48,7 @@ typedef struct { time_t expires; /* expiry time of subscription */ struct sockaddr_in6 subscriber; /* subscriber's address */ + str token; /* subscription token */ } coap_subscription_t; #define COAP_RESOURCE(node) ((coap_resource_t *)(node)->data) @@ -93,7 +94,22 @@ coap_subscription_t *coap_new_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 + * by hashkey. When token is not NULL the subscription must have the + * same token. + * @param context The CoAP context + * @param hashkey The unique key that identifies the subscription + * @param subscriber The subscriber's transport address + * @param token If not NULL, this specifies a token given by the + * 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_key_t hashkey, + struct sockaddr_in6 *subscriber, + str *token); +/** * 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. @@ -101,7 +117,8 @@ coap_key_t coap_add_subscription(coap_context_t *context, * @return 1 if a subscription was removed, 0 otherwise. */ int coap_delete_subscription(coap_context_t *context, - coap_key_t hashkey); + coap_key_t hashkey, + struct sockaddr_in6 *subscriber); /** Returns a unique hash for the specified URI or COAP_INVALID_HASHKEY on error. */ coap_key_t coap_uri_hash(const coap_uri_t *uri);