From 3f5a797776f60e6d5d3c926ae9e937761981e659 Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Thu, 1 Oct 2015 19:27:50 +0200 Subject: [PATCH] net.c: fixed critical option check The limitation of COAP_OPT_FILTER_SHORT to six entries made the critical option check fail e.g. for Block2. This change hard-codes the built-in critical options in coap_option_check_critical() so that up to COAP_OPT_FILTER_SHORT short options and COAP_OPT_FILTER_LONG long options can be registered dynamically. Please note that registering critical options that are already known (i.e. listed in pdu.h and checked in coap_option_check_critical()) is a waste of storage space. --- src/net.c | 50 +++++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/src/net.c b/src/net.c index 15de94da..41726ce2 100644 --- a/src/net.c +++ b/src/net.c @@ -379,19 +379,6 @@ coap_new_context( /* initialize message id */ prng((unsigned char *)&c->message_id, sizeof(unsigned short)); - /* register the critical options that we know */ - coap_register_option(c, COAP_OPTION_IF_MATCH); - coap_register_option(c, COAP_OPTION_URI_HOST); - coap_register_option(c, COAP_OPTION_IF_NONE_MATCH); - coap_register_option(c, COAP_OPTION_URI_PORT); - coap_register_option(c, COAP_OPTION_URI_PATH); - coap_register_option(c, COAP_OPTION_URI_QUERY); - coap_register_option(c, COAP_OPTION_ACCEPT); - coap_register_option(c, COAP_OPTION_PROXY_URI); - coap_register_option(c, COAP_OPTION_PROXY_SCHEME); - coap_register_option(c, COAP_OPTION_BLOCK2); - coap_register_option(c, COAP_OPTION_BLOCK1); - c->endpoint = coap_new_endpoint(listen_addr, COAP_ENDPOINT_NOSEC); #ifdef WITH_LWIP c->endpoint->context = c; @@ -471,17 +458,34 @@ coap_option_check_critical(coap_context_t *ctx, * the largest known option, we know that everything beyond is * bad. */ - if (opt_iter.type & 0x01 && - coap_option_getb(ctx->known_options, opt_iter.type) < 1) { - debug("unknown critical option %d\n", opt_iter.type); - - ok = 0; - - /* When opt_iter.type is beyond our known option range, - * coap_option_setb() will return -1 and we are safe to leave - * this loop. */ - if (coap_option_setb(unknown, opt_iter.type) == -1) + if (opt_iter.type & 0x01) { + /* first check the built-in critical options */ + switch (opt_iter.type) { + case COAP_OPTION_IF_MATCH: + case COAP_OPTION_URI_HOST: + case COAP_OPTION_IF_NONE_MATCH: + case COAP_OPTION_URI_PORT: + case COAP_OPTION_URI_PATH: + case COAP_OPTION_URI_QUERY: + case COAP_OPTION_ACCEPT: + case COAP_OPTION_PROXY_URI: + case COAP_OPTION_PROXY_SCHEME: + case COAP_OPTION_BLOCK2: + case COAP_OPTION_BLOCK1: break; + default: + if (coap_option_filter_get(ctx->known_options, opt_iter.type) <= 0) { + debug("unknown critical option %d\n", opt_iter.type); + ok = 0; + + /* When opt_iter.type is beyond our known option range, + * coap_option_filter_set() will return -1 and we are safe to leave + * this loop. */ + if (coap_option_filter_set(unknown, opt_iter.type) == -1) { + break; + } + } + } } }