From 2bfedb97756ed5086472ea5a3778f1aa8ef17f10 Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Fri, 18 Jan 2013 16:44:57 +0100 Subject: [PATCH] Klaus-encoding --- examples/client.c | 40 +++++++++++------------------------- examples/etsi_iot_01.c | 27 ++++--------------------- examples/rd.c | 24 ++++------------------ examples/server.c | 46 +++++++++++++++--------------------------- 4 files changed, 36 insertions(+), 101 deletions(-) diff --git a/examples/client.c b/examples/client.c index 00877382..f3580f37 100644 --- a/examples/client.c +++ b/examples/client.c @@ -1,6 +1,6 @@ /* coap-client -- simple CoAP client * - * Copyright (C) 2010--2012 Olaf Bergmann + * Copyright (C) 2010--2013 Olaf Bergmann * * This file is part of the CoAP library libcoap. Please see * README for terms of use. @@ -25,6 +25,9 @@ int flags = 0; +static unsigned char _token_data[8]; +str the_token = { 0, _token_data }; + #define FLAGS_BLOCK 0x01 static coap_list_t *optlist = NULL; @@ -162,13 +165,13 @@ clear_obs(coap_context_t *ctx, const coap_address_t *remote) { pdu = coap_new_request(ctx, COAP_REQUEST_GET, NULL); if (pdu) { + /* FIXME: add token */ /* add URI components from optlist */ for (option = optlist; option; option = option->next ) { switch (COAP_OPTION_KEY(*(coap_option *)option->data)) { case COAP_OPTION_URI_HOST : case COAP_OPTION_URI_PORT : case COAP_OPTION_URI_PATH : - case COAP_OPTION_TOKEN : case COAP_OPTION_URI_QUERY : coap_add_option ( pdu, COAP_OPTION_KEY(*(coap_option *)option->data), COAP_OPTION_LENGTH(*(coap_option *)option->data), @@ -255,27 +258,10 @@ get_block(coap_pdu_t *pdu, coap_opt_iterator_t *opt_iter) { ((Pdu)->hdr->code == COAP_RESPONSE_CODE(201) || \ (Pdu)->hdr->code == COAP_RESPONSE_CODE(204))) -int +inline int check_token(coap_pdu_t *received) { - coap_opt_iterator_t opt_iter; - coap_list_t *option; - str token1 = { 0, NULL }, token2 = { 0, NULL }; - - if (coap_check_option(received, COAP_OPTION_TOKEN, &opt_iter)) { - token1.s = COAP_OPT_VALUE(opt_iter.option); - token1.length = COAP_OPT_LENGTH(opt_iter.option); - } - - for (option = optlist; option; option = option->next) { - if (COAP_OPTION_KEY(*(coap_option *)option->data) == COAP_OPTION_TOKEN) { - token2.s = COAP_OPTION_DATA(*(coap_option *)option->data); - token2.length = COAP_OPTION_LENGTH(*(coap_option *)option->data); - break; - } - } - - return token1.length == token2.length && - memcmp(token1.s, token2.s, token1.length) == 0; + return received->hdr->token_length == the_token.length && + memcmp(received->hdr->token, the_token.s, the_token.length) == 0; } void @@ -360,7 +346,6 @@ message_handler(struct coap_context_t *ctx, case COAP_OPTION_URI_HOST : case COAP_OPTION_URI_PORT : case COAP_OPTION_URI_PATH : - case COAP_OPTION_TOKEN : case COAP_OPTION_URI_QUERY : coap_add_option ( pdu, COAP_OPTION_KEY(*(coap_option *)option->data), COAP_OPTION_LENGTH(*(coap_option *)option->data), @@ -431,7 +416,7 @@ usage( const char *program, const char *version) { program = ++p; fprintf( stderr, "%s v%s -- a small CoAP implementation\n" - "(c) 2010-2012 Olaf Bergmann \n\n" + "(c) 2010-2013 Olaf Bergmann \n\n" "usage: %s [-A type...] [-t type] [-b [num,]size] [-B seconds] [-e text]\n" "\t\t[-g group] [-m method] [-N] [-o file] [-P addr[:port]] [-p port]\n" "\t\t[-s duration] [-O num,text] [-T string] [-v num] URI\n\n" @@ -772,11 +757,10 @@ cmdline_proxy(char *arg) { return 1; } -void +inline void cmdline_token(char *arg) { - coap_insert( &optlist, new_option_node(COAP_OPTION_TOKEN, - strlen(arg), - (unsigned char *)arg), order_opts); + + strncpy((char *)the_token.s, arg, min(sizeof(_token_data), strlen(arg))); } void diff --git a/examples/etsi_iot_01.c b/examples/etsi_iot_01.c index 3d470f5f..0d10d04b 100644 --- a/examples/etsi_iot_01.c +++ b/examples/etsi_iot_01.c @@ -1,6 +1,6 @@ /* CoAP server for first ETSI CoAP plugtest, March 2012 * - * Copyright (C) 2012 Olaf Bergmann + * Copyright (C) 2012--2013 Olaf Bergmann * * This file is part of the CoAP library libcoap. Please see * README for terms of use. @@ -139,9 +139,6 @@ hnd_get_index(coap_context_t *ctx, struct coap_resource_t *resource, coap_add_option(response, COAP_OPTION_MAXAGE, coap_encode_var_bytes(buf, 0x2ffff), buf); - if (token->length) - coap_add_option(response, COAP_OPTION_TOKEN, token->length, token->s); - coap_add_data(response, strlen(INDEX), (unsigned char *)INDEX); } @@ -159,8 +156,6 @@ hnd_get_resource(coap_context_t *ctx, struct coap_resource_t *resource, if (!test_payload) { response->hdr->code = COAP_RESPONSE_CODE(500); - if (token->length) - coap_add_option(response, COAP_OPTION_TOKEN, token->length, token->s); return; } @@ -176,9 +171,6 @@ hnd_get_resource(coap_context_t *ctx, struct coap_resource_t *resource, coap_add_option(response, COAP_OPTION_ETAG, sizeof(etag), etag); } - if (token->length) - coap_add_option(response, COAP_OPTION_TOKEN, token->length, token->s); - if (request) { int res; @@ -241,8 +233,6 @@ hnd_delete_resource(coap_context_t *ctx, struct coap_resource_t *resource, coap_delete_resource(ctx, resource->key); response->hdr->code = COAP_RESPONSE_CODE(202); - if (token->length) - coap_add_option(response, COAP_OPTION_TOKEN, token->length, token->s); } void @@ -308,8 +298,6 @@ hnd_post_test(coap_context_t *ctx, struct coap_resource_t *resource, response->hdr->code = COAP_RESPONSE_CODE(201); } - if (token->length) - coap_add_option(response, COAP_OPTION_TOKEN, token->length, token->s); } void @@ -322,8 +310,6 @@ hnd_put_test(coap_context_t *ctx, struct coap_resource_t *resource, unsigned char *data; response->hdr->code = COAP_RESPONSE_CODE(204); - if (token->length) - coap_add_option(response, COAP_OPTION_TOKEN, token->length, token->s); coap_get_data(request, &len, &data); @@ -378,8 +364,6 @@ hnd_delete_test(coap_context_t *ctx, struct coap_resource_t *resource, #endif response->hdr->code = COAP_RESPONSE_CODE(202); - if (token->length) - coap_add_option(response, COAP_OPTION_TOKEN, token->length, token->s); } void @@ -397,9 +381,6 @@ hnd_get_query(coap_context_t *ctx, struct coap_resource_t *resource, coap_add_option(response, COAP_OPTION_CONTENT_TYPE, coap_encode_var_bytes(buf, COAP_MEDIATYPE_TEXT_PLAIN), buf); - if (token->length) - coap_add_option(response, COAP_OPTION_TOKEN, token->length, token->s); - coap_option_filter_clear(f); coap_option_setb(f, COAP_OPTION_URI_QUERY); @@ -492,12 +473,12 @@ check_async(coap_context_t *ctx, coap_tick_t now) { response->hdr->id = coap_new_message_id(ctx); + if (async->tokenlen) + coap_add_token(response, async->tokenlen, async->token); + coap_add_option(response, COAP_OPTION_CONTENT_TYPE, coap_encode_var_bytes(buf, COAP_MEDIATYPE_TEXT_PLAIN), buf); - if (async->tokenlen) - coap_add_option(response, COAP_OPTION_TOKEN, async->tokenlen, async->token); - coap_add_data(response, 4, (unsigned char *)"done"); if (coap_send(ctx, &async->peer, response) == COAP_INVALID_TID) { diff --git a/examples/rd.c b/examples/rd.c index 8e88cdf4..d5bf0b5b 100644 --- a/examples/rd.c +++ b/examples/rd.c @@ -1,7 +1,7 @@ /* coap -- simple implementation of the Constrained Application Protocol (CoAP) * as defined in draft-ietf-core-coap * - * Copyright (C) 2010--2012 Olaf Bergmann + * Copyright (C) 2010--2013 Olaf Bergmann * * This file is part of the CoAP library libcoap. Please see * README for terms of use. @@ -101,9 +101,6 @@ hnd_get_resource(coap_context_t *ctx, struct coap_resource_t *resource, if (rd && rd->etag_len) coap_add_option(response, COAP_OPTION_ETAG, rd->etag_len, rd->etag); - if (token->length) - coap_add_option(response, COAP_OPTION_TOKEN, token->length, token->s); - if (rd && rd->data.s) coap_add_data(response, rd->data.length, rd->data.s); } @@ -163,10 +160,7 @@ hnd_put_resource(coap_context_t *ctx, struct coap_resource_t *resource, } finish: - token = coap_check_option(request, COAP_OPTION_TOKEN, &opt_iter); - if (token) - size += COAP_OPT_SIZE(token); - + /* FIXME: do not create a new response but use the old one instead */ response = coap_pdu_init(type, code, request->hdr->id, size); if (!response) { @@ -174,9 +168,8 @@ hnd_put_resource(coap_context_t *ctx, struct coap_resource_t *resource, return; } - if (token) - coap_add_option(response, COAP_OPTION_TOKEN, - COAP_OPT_LENGTH(token), COAP_OPT_VALUE(token)); + if (request->hdr->token_length) + coap_add_token(response, request->hdr->token_length, request->hdr->token); if (coap_send(ctx, peer, response) == COAP_INVALID_TID) { debug("hnd_get_rd: cannot send response for message %d\n", @@ -202,9 +195,6 @@ hnd_delete_resource(coap_context_t *ctx, struct coap_resource_t *resource, coap_delete_resource(ctx, resource->key); response->hdr->code = COAP_RESPONSE_CODE(202); - - if (token->length) - coap_add_option(response, COAP_OPTION_TOKEN, token->length, token->s); } void @@ -220,9 +210,6 @@ hnd_get_rd(coap_context_t *ctx, struct coap_resource_t *resource, coap_add_option(response, COAP_OPTION_MAXAGE, coap_encode_var_bytes(buf, 0x2ffff), buf); - - if (token->length) - coap_add_option(response, COAP_OPTION_TOKEN, token->length, token->s); } int @@ -491,9 +478,6 @@ hnd_post_rd(coap_context_t *ctx, struct coap_resource_t *resource, b += COAP_OPT_SIZE(b); } } - - if (token->length) - coap_add_option(response, COAP_OPTION_TOKEN, token->length, token->s); } void diff --git a/examples/server.c b/examples/server.c index f474e0a4..cb681f90 100644 --- a/examples/server.c +++ b/examples/server.c @@ -1,7 +1,7 @@ /* coap -- simple implementation of the Constrained Application Protocol (CoAP) * as defined in draft-ietf-core-coap * - * Copyright (C) 2010,2011 Olaf Bergmann + * Copyright (C) 2010--2013 Olaf Bergmann * * This file is part of the CoAP library libcoap. Please see * README for terms of use. @@ -52,7 +52,7 @@ handle_sigint(int signum) { } #define INDEX "This is a test server made with libcoap (see http://libcoap.sf.net)\n" \ - "Copyright (C) 2010--2012 Olaf Bergmann \n\n" + "Copyright (C) 2010--2013 Olaf Bergmann \n\n" void hnd_get_index(coap_context_t *ctx, struct coap_resource_t *resource, @@ -68,9 +68,6 @@ hnd_get_index(coap_context_t *ctx, struct coap_resource_t *resource, coap_add_option(response, COAP_OPTION_MAXAGE, coap_encode_var_bytes(buf, 0x2ffff), buf); - if (token->length) - coap_add_option(response, COAP_OPTION_TOKEN, token->length, token->s); - coap_add_data(response, strlen(INDEX), (unsigned char *)INDEX); } @@ -90,28 +87,25 @@ hnd_get_time(coap_context_t *ctx, struct coap_resource_t *resource, response->hdr->code = my_clock_base ? COAP_RESPONSE_CODE(205) : COAP_RESPONSE_CODE(404); + if (request != NULL && + coap_check_option(request, COAP_OPTION_OBSERVE, &opt_iter)) { + coap_add_observer(resource, + peer, + token); + coap_add_option(response, COAP_OPTION_OBSERVE, 0, NULL); + } + if (resource->dirty == 1) + coap_add_option(response, COAP_OPTION_OBSERVE, + coap_encode_var_bytes(buf, ctx->observe), buf); + + if (my_clock_base) - coap_add_option(response, COAP_OPTION_CONTENT_TYPE, + coap_add_option(response, COAP_OPTION_CONTENT_FORMAT, coap_encode_var_bytes(buf, COAP_MEDIATYPE_TEXT_PLAIN), buf); coap_add_option(response, COAP_OPTION_MAXAGE, coap_encode_var_bytes(buf, 0x01), buf); - if (request != NULL && - coap_check_option(request, COAP_OPTION_SUBSCRIPTION, &opt_iter)) { - coap_add_observer(resource, - peer, - token); - coap_add_option(response, COAP_OPTION_SUBSCRIPTION, 0, NULL); - } - if (resource->dirty == 1) - coap_add_option(response, COAP_OPTION_SUBSCRIPTION, - coap_encode_var_bytes(buf, ctx->observe), buf); - - - if (token->length) - coap_add_option(response, COAP_OPTION_TOKEN, token->length, token->s); - if (my_clock_base) { /* calculate current time */ @@ -167,9 +161,6 @@ hnd_put_time(coap_context_t *ctx, struct coap_resource_t *resource, my_clock_base = my_clock_base * 10 + *data++; my_clock_base -= t / COAP_TICKS_PER_SECOND; } - - if (token->length) - coap_add_option(response, COAP_OPTION_TOKEN, token->length, token->s); } void @@ -180,9 +171,6 @@ hnd_delete_time(coap_context_t *ctx, struct coap_resource_t *resource, /* type = request->hdr->type == COAP_MESSAGE_CON */ /* ? COAP_MESSAGE_ACK : COAP_MESSAGE_NON; */ - - if (token->length) - coap_add_option(response, COAP_OPTION_TOKEN, token->length, token->s); } #ifndef WITHOUT_ASYNC @@ -226,8 +214,6 @@ check_async(coap_context_t *ctx, coap_tick_t now) { if (!async || now < async->created + (unsigned long)async->appdata) return; - size += async->tokenlen; - response = coap_pdu_init(async->flags & COAP_ASYNC_CONFIRM ? COAP_MESSAGE_CON : COAP_MESSAGE_NON, @@ -242,7 +228,7 @@ check_async(coap_context_t *ctx, coap_tick_t now) { response->hdr->id = coap_new_message_id(ctx); if (async->tokenlen) - coap_add_option(response, COAP_OPTION_TOKEN, async->tokenlen, async->token); + coap_add_token(response, async->tokenlen, async->token); coap_add_data(response, 4, (unsigned char *)"done");