more fixes for option iterator

Renamed option in struct coap_opt_iterator_t to next_option to detect
errors.
This commit is contained in:
Olaf Bergmann committed 2013-01-21 17:25:36 +01:00
1 parent 7852aa66a5
commit 8b8c3d9ac9
6 files changed
+41 -30

No files matched your search

+5
View File
@@ -1,3 +1,8 @@
2013-01-21 Olaf Bergmann <bergmann@tzi.org>
* option.h: renamed option field in coap_opt_iterator_t to
next_option to detect erroneous use in old code
2013-01-18 Olaf Bergmann <bergmann@tzi.org>
* configure.in: new option --with-tests to enable unit tests
+5 -4
View File
@@ -21,15 +21,16 @@
int
coap_get_block(coap_pdu_t *pdu, unsigned short type, coap_block_t *block) {
coap_opt_iterator_t opt_iter;
coap_opt_t *option;
assert(block);
memset(block, 0, sizeof(coap_block_t));
if (pdu && coap_check_option(pdu, type, &opt_iter)) {
block->szx = COAP_OPT_BLOCK_SZX(opt_iter.option);
if (COAP_OPT_BLOCK_MORE(opt_iter.option))
if (pdu && (option = coap_check_option(pdu, type, &opt_iter))) {
block->szx = COAP_OPT_BLOCK_SZX(option);
if (COAP_OPT_BLOCK_MORE(option))
block->m = 1;
block->num = COAP_OPT_BLOCK_NUM(opt_iter.option);
block->num = COAP_OPT_BLOCK_NUM(option);
return 1;
}
+13 -10
View File
@@ -240,6 +240,7 @@ hnd_post_test(coap_context_t *ctx, struct coap_resource_t *resource,
coap_address_t *peer, coap_pdu_t *request, str *token,
coap_pdu_t *response) {
coap_opt_iterator_t opt_iter;
coap_opt_t *option;
coap_payload_t *test_payload;
size_t len;
size_t l = 6 + sizeof(void *);
@@ -276,10 +277,10 @@ hnd_post_test(coap_context_t *ctx, struct coap_resource_t *resource,
coap_register_handler(r, COAP_REQUEST_DELETE, hnd_delete_resource);
/* set media_type if available */
if (coap_check_option(request, COAP_OPTION_CONTENT_TYPE, &opt_iter)) {
option = coap_check_option(request, COAP_OPTION_CONTENT_TYPE, &opt_iter);
if (option) {
test_payload->media_type =
coap_decode_var_bytes(COAP_OPT_VALUE(opt_iter.option),
COAP_OPT_LENGTH(opt_iter.option));
coap_decode_var_bytes(COAP_OPT_VALUE(option), COAP_OPT_LENGTH(option));
}
coap_add_resource(ctx, r);
@@ -305,6 +306,7 @@ hnd_put_test(coap_context_t *ctx, struct coap_resource_t *resource,
coap_address_t *peer, coap_pdu_t *request, str *token,
coap_pdu_t *response) {
coap_opt_iterator_t opt_iter;
coap_opt_t *option;
coap_payload_t *payload;
size_t len;
unsigned char *data;
@@ -331,11 +333,11 @@ hnd_put_test(coap_context_t *ctx, struct coap_resource_t *resource,
payload->length = len;
memcpy(payload->data, data, len);
if (coap_check_option(request, COAP_OPTION_CONTENT_TYPE, &opt_iter)) {
option = coap_check_option(request, COAP_OPTION_CONTENT_TYPE, &opt_iter);
if (option) {
/* set media type given in request */
payload->media_type =
coap_decode_var_bytes(COAP_OPT_VALUE(opt_iter.option),
COAP_OPT_LENGTH(opt_iter.option));
coap_decode_var_bytes(COAP_OPT_VALUE(option), COAP_OPT_LENGTH(option));
} else {
/* set default value */
payload->media_type = COAP_MEDIATYPE_TEXT_PLAIN;
@@ -409,6 +411,7 @@ hnd_get_separate(coap_context_t *ctx, struct coap_resource_t *resource,
coap_address_t *peer, coap_pdu_t *request, str *token,
coap_pdu_t *response) {
coap_opt_iterator_t opt_iter;
coap_opt_t *option;
coap_opt_filter_t f;
unsigned long delay = 5;
@@ -427,13 +430,13 @@ hnd_get_separate(coap_context_t *ctx, struct coap_resource_t *resource,
coap_option_iterator_init(request, &opt_iter, f);
while (coap_option_next(&opt_iter)) {
if (strncmp("delay=", (char *)COAP_OPT_VALUE(opt_iter.option), 6) == 0) {
while ((option = coap_option_next(&opt_iter))) {
if (strncmp("delay=", (char *)COAP_OPT_VALUE(option), 6) == 0) {
int i;
unsigned long d = 0;
for (i = 6; i < COAP_OPT_LENGTH(opt_iter.option); ++i)
d = d * 10 + COAP_OPT_VALUE(opt_iter.option)[i] - '0';
for (i = 6; i < COAP_OPT_LENGTH(option); ++i)
d = d * 10 + COAP_OPT_VALUE(option)[i] - '0';
/* don't allow delay to be less than COAP_RESOURCE_CHECK_TIME*/
delay = d < COAP_RESOURCE_CHECK_TIME_SEC
+9 -6
View File
@@ -76,6 +76,7 @@ hnd_get_time(coap_context_t *ctx, struct coap_resource_t *resource,
coap_address_t *peer, coap_pdu_t *request, str *token,
coap_pdu_t *response) {
coap_opt_iterator_t opt_iter;
coap_opt_t *option;
unsigned char buf[40];
size_t len;
time_t now;
@@ -114,9 +115,9 @@ hnd_get_time(coap_context_t *ctx, struct coap_resource_t *resource,
now = my_clock_base + (t / COAP_TICKS_PER_SECOND);
if (request != NULL
&& coap_check_option(request, COAP_OPTION_URI_QUERY, &opt_iter)
&& memcmp(COAP_OPT_VALUE(opt_iter.option), "ticks",
min(5, COAP_OPT_LENGTH(opt_iter.option))) == 0) {
&& (option = coap_check_option(request, COAP_OPTION_URI_QUERY, &opt_iter))
&& memcmp(COAP_OPT_VALUE(option), "ticks",
min(5, COAP_OPT_LENGTH(option))) == 0) {
/* output ticks */
len = snprintf((char *)buf,
min(sizeof(buf), response->max_size - response->length),
@@ -182,6 +183,7 @@ hnd_get_async(coap_context_t *ctx, struct coap_resource_t *resource,
coap_address_t *peer, coap_pdu_t *request, str *token,
coap_pdu_t *response) {
coap_opt_iterator_t opt_iter;
coap_opt_t *option;
unsigned long delay = 5;
size_t size;
@@ -194,11 +196,12 @@ hnd_get_async(coap_context_t *ctx, struct coap_resource_t *resource,
return;
}
if (coap_check_option(request, COAP_OPTION_URI_QUERY, &opt_iter)) {
unsigned char *p = COAP_OPT_VALUE(opt_iter.option);
option = coap_check_option(request, COAP_OPTION_URI_QUERY, &opt_iter);
if (option) {
unsigned char *p = COAP_OPT_VALUE(option);
delay = 0;
for (size = COAP_OPT_LENGTH(opt_iter.option); size; --size, ++p)
for (size = COAP_OPT_LENGTH(option); size; --size, ++p)
delay = delay * 10 + (*p - '0');
}
+7 -8
View File
@@ -1,7 +1,7 @@
/*
* option.c -- helpers for handling options in CoAP PDUs
*
* Copyright (C) 2010,2011 Olaf Bergmann <bergmann@tzi.org>
* Copyright (C) 2010-2013 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see
* README for terms of use.
@@ -123,9 +123,9 @@ coap_option_iterator_init(coap_pdu_t *pdu, coap_opt_iterator_t *oi,
memset(oi, 0, sizeof(coap_opt_iterator_t));
oi->option = (unsigned char *)pdu->hdr + sizeof(coap_hdr_t)
oi->next_option = (unsigned char *)pdu->hdr + sizeof(coap_hdr_t)
+ pdu->hdr->token_length;
if ((unsigned char *)pdu->hdr + pdu->length <= oi->option) {
if ((unsigned char *)pdu->hdr + pdu->length <= oi->next_option) {
oi->bad = 1;
return NULL;
}
@@ -146,7 +146,7 @@ opt_finished(coap_opt_iterator_t *oi) {
assert(oi);
if (oi->bad || oi->length == 0 ||
!oi->option || *oi->option == COAP_PAYLOAD_START) {
!oi->next_option || *oi->next_option == COAP_PAYLOAD_START) {
oi->bad = 1;
}
@@ -169,15 +169,15 @@ coap_option_next(coap_opt_iterator_t *oi) {
/* oi->option always points to the next option to deliver; as
* opt_finished() filters out any bad conditions, we can assume that
* oi->option is valid. */
current_opt = oi->option;
current_opt = oi->next_option;
/* Advance internal pointer to next option, skipping any option that
* is not included in oi->filter. */
optsize = coap_opt_parse(oi->option, oi->length, &option);
optsize = coap_opt_parse(oi->next_option, oi->length, &option);
if (optsize) {
assert(optsize <= oi->length);
oi->option += optsize;
oi->next_option += optsize;
oi->length -= optsize;
oi->type += option.delta;
@@ -207,7 +207,6 @@ coap_opt_t *
coap_check_option(coap_pdu_t *pdu, unsigned char type,
coap_opt_iterator_t *oi) {
coap_opt_filter_t f;
coap_opt_t *option;
coap_option_filter_clear(f);
coap_option_setb(f, type);
+2 -2
View File
@@ -1,7 +1,7 @@
/*
* option.h -- helpers for handling options in CoAP PDUs
*
* Copyright (C) 2010,2011 Olaf Bergmann <bergmann@tzi.org>
* Copyright (C) 2010-2013 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see
* README for terms of use.
@@ -171,7 +171,7 @@ typedef struct {
unsigned short type; /**< decoded option type */
unsigned int bad:1; /**< iterator object is ok if not set */
unsigned int filtered:1; /**< denotes whether or not filter is used */
coap_opt_t *option; /**< pointer to the current option */
coap_opt_t *next_option; /**< pointer to the unparsed next option */
coap_opt_filter_t filter; /**< option filter */
} coap_opt_iterator_t;