diff --git a/examples/client.c b/examples/client.c index 90725708..c0b42168 100644 --- a/examples/client.c +++ b/examples/client.c @@ -46,7 +46,7 @@ static coap_list_t *optlist = NULL; * TODO: associate the resources with transaction id and make it expireable */ static coap_uri_t uri; static str proxy = { 0, NULL }; -static unsigned short proxy_port = COAP_DEFAULT_PORT; +static uint16_t proxy_port = COAP_DEFAULT_PORT; /* reading is done when this flag is set */ static int ready = 0; @@ -150,14 +150,14 @@ coap_new_request(coap_context_t *ctx, coap_list_t *opt; (void)ctx; - if ( ! ( pdu = coap_new_pdu(session) ) ) + if ( ! ( pdu = coap_new_pdu() ) ) return NULL; - pdu->hdr->type = msgtype; - pdu->hdr->id = coap_new_message_id(session); - pdu->hdr->code = m; + pdu->type = msgtype; + pdu->tid = coap_new_message_id(session); + pdu->code = m; - pdu->hdr->token_length = (uint16_t)the_token.length; + pdu->token_length = (uint8_t)the_token.length; if ( !coap_add_token(pdu, the_token.length, the_token.s)) { debug("cannot add token to request\n"); } @@ -311,8 +311,8 @@ resolve_address(const str *server, struct sockaddr *dst) { static inline int check_token(coap_pdu_t *received) { - return received->hdr->token_length == the_token.length && - memcmp(received->hdr->token, the_token.s, the_token.length) == 0; + return received->token_length == the_token.length && + memcmp(received->token, the_token.s, the_token.length) == 0; } static void @@ -334,7 +334,7 @@ message_handler(struct coap_context_t *ctx, #ifndef NDEBUG if (LOG_INFO <= coap_get_log_level()) { debug("** process incoming %d.%02d response:\n", - (received->hdr->code >> 5), received->hdr->code & 0x1F); + (received->code >> 5), received->code & 0x1F); coap_show_pdu(received); } #endif @@ -342,19 +342,19 @@ message_handler(struct coap_context_t *ctx, /* check if this is a response to our original request */ if (!check_token(received)) { /* drop if this was just some message, or send RST in case of notification */ - if (!sent && (received->hdr->type == COAP_MESSAGE_CON || - received->hdr->type == COAP_MESSAGE_NON)) + if (!sent && (received->type == COAP_MESSAGE_CON || + received->type == COAP_MESSAGE_NON)) coap_send_rst(session, received); return; } - if (received->hdr->type == COAP_MESSAGE_RST) { + if (received->type == COAP_MESSAGE_RST) { info("got RST\n"); return; } /* output the received data, if any */ - if (COAP_RESPONSE_CLASS(received->hdr->code) == 2) { + if (COAP_RESPONSE_CLASS(received->code) == 2) { /* set obs timer if we have successfully subscribed a resource */ if (sent && coap_check_option(received, COAP_OPTION_SUBSCRIPTION, &opt_iter)) { @@ -367,7 +367,7 @@ message_handler(struct coap_context_t *ctx, * both, Block1 and Block2 are present. */ block_opt = coap_check_option(received, COAP_OPTION_BLOCK2, &opt_iter); if (block_opt) { /* handle Block2 */ - unsigned short blktype = opt_iter.type; + uint16_t blktype = opt_iter.type; /* TODO: check if we are looking at the correct block number */ if (coap_get_data(received, &len, &databuf)) @@ -511,9 +511,9 @@ message_handler(struct coap_context_t *ctx, } else { /* no 2.05 */ /* check if an error was signaled and output payload if so */ - if (COAP_RESPONSE_CLASS(received->hdr->code) >= 4) { + if (COAP_RESPONSE_CLASS(received->code) >= 4) { fprintf(stderr, "%d.%02d", - (received->hdr->code >> 5), received->hdr->code & 0x1F); + (received->code >> 5), received->code & 0x1F); if (coap_get_data(received, &len, &databuf)) { fprintf(stderr, " "); while(len--) @@ -587,7 +587,7 @@ usage( const char *program, const char *version) { } static coap_list_t * -new_option_node(unsigned short key, size_t length, unsigned char *data) { +new_option_node(uint16_t key, size_t length, unsigned char *data) { coap_list_t *node; node = coap_malloc(sizeof(coap_list_t) + sizeof(coap_option) + length); @@ -611,7 +611,7 @@ typedef struct { } content_type_t; static void -cmdline_content_type(char *arg, unsigned short key) { +cmdline_content_type(char *arg, uint16_t key) { static content_type_t content_types[] = { { 0, "plain" }, { 0, "text/plain" }, @@ -673,7 +673,7 @@ cmdline_content_type(char *arg, unsigned short key) { } } -static unsigned short +static uint16_t get_default_port(const coap_uri_t *u) { return coap_uri_scheme_is_secure(u) ? COAPS_DEFAULT_PORT : COAP_DEFAULT_PORT; } @@ -738,10 +738,10 @@ cmdline_uri(char *arg, int create_uri_opts) { while (res--) { coap_insert(&optlist, new_option_node(COAP_OPTION_URI_PATH, - COAP_OPT_LENGTH(buf), - COAP_OPT_VALUE(buf))); + coap_opt_length(buf), + coap_opt_value(buf))); - buf += COAP_OPT_SIZE(buf); + buf += coap_opt_size(buf); } } @@ -753,10 +753,10 @@ cmdline_uri(char *arg, int create_uri_opts) { while (res--) { coap_insert(&optlist, new_option_node(COAP_OPTION_URI_QUERY, - COAP_OPT_LENGTH(buf), - COAP_OPT_VALUE(buf))); + coap_opt_length(buf), + coap_opt_value(buf))); - buf += COAP_OPT_SIZE(buf); + buf += coap_opt_size(buf); } } } @@ -766,7 +766,7 @@ cmdline_uri(char *arg, int create_uri_opts) { static int cmdline_blocksize(char *arg) { - unsigned short size; + uint16_t size; again: size = 0; @@ -791,7 +791,7 @@ cmdline_blocksize(char *arg) { static void set_blocksize(void) { static unsigned char buf[4]; /* hack: temporarily take encoded bytes */ - unsigned short opt; + uint16_t opt; unsigned int opt_length; if (method != COAP_REQUEST_DELETE) { @@ -1101,7 +1101,7 @@ main(int argc, char **argv) { int result = -1; coap_pdu_t *pdu; static str server; - unsigned short port = COAP_DEFAULT_PORT; + uint16_t port = COAP_DEFAULT_PORT; char port_str[NI_MAXSERV] = "0"; char node_str[NI_MAXHOST] = ""; int opt, res; diff --git a/examples/coap-rd.c b/examples/coap-rd.c index 7b27cee8..827f7229 100644 --- a/examples/coap-rd.c +++ b/examples/coap-rd.c @@ -101,7 +101,7 @@ hnd_get_resource(coap_context_t *ctx UNUSED_PARAM, HASH_FIND(hh, resources, resource->key, sizeof(coap_key_t), rd); - response->hdr->code = COAP_RESPONSE_CODE(205); + response->code = COAP_RESPONSE_CODE(205); coap_add_option(response, COAP_OPTION_CONTENT_TYPE, @@ -125,7 +125,7 @@ hnd_put_resource(coap_context_t *ctx UNUSED_PARAM, str *query UNUSED_PARAM, coap_pdu_t *response) { #if 1 - response->hdr->code = COAP_RESPONSE_CODE(501); + response->code = COAP_RESPONSE_CODE(501); #else /* FIXME */ coap_opt_iterator_t opt_iter; coap_opt_t *token, *etag; @@ -212,7 +212,7 @@ hnd_delete_resource(coap_context_t *ctx, * using coap_malloc() and must be released. */ coap_delete_resource(ctx, resource->key); - response->hdr->code = COAP_RESPONSE_CODE(202); + response->code = COAP_RESPONSE_CODE(202); } static void @@ -225,7 +225,7 @@ hnd_get_rd(coap_context_t *ctx UNUSED_PARAM, coap_pdu_t *response) { unsigned char buf[3]; - response->hdr->code = COAP_RESPONSE_CODE(205); + response->code = COAP_RESPONSE_CODE(205); coap_add_option(response, COAP_OPTION_CONTENT_TYPE, @@ -372,8 +372,8 @@ make_rd(coap_pdu_t *pdu) { etag = coap_check_option(pdu, COAP_OPTION_ETAG, &opt_iter); if (etag) { - rd->etag_len = min(COAP_OPT_LENGTH(etag), sizeof(rd->etag)); - memcpy(rd->etag, COAP_OPT_VALUE(etag), rd->etag_len); + rd->etag_len = min(coap_opt_length(etag), sizeof(rd->etag)); + memcpy(rd->etag, coap_opt_value(etag), rd->etag_len); } return rd; @@ -396,7 +396,7 @@ hnd_post_rd(coap_context_t *ctx, loc = (unsigned char *)coap_malloc(LOCSIZE); if (!loc) { - response->hdr->code = COAP_RESPONSE_CODE(500); + response->code = COAP_RESPONSE_CODE(500); return; } memcpy(loc, RD_ROOT_STR, RD_ROOT_SIZE); @@ -426,7 +426,7 @@ hnd_post_rd(coap_context_t *ctx, } else { /* generate node identifier */ loc_size += snprintf((char *)(loc + loc_size), LOCSIZE - loc_size - 1, - "%x", request->hdr->id); + "%x", request->tid); if (loc_size > 1) { if (ins.length) { @@ -505,7 +505,7 @@ hnd_post_rd(coap_context_t *ctx, /* create response */ - response->hdr->code = COAP_RESPONSE_CODE(201); + response->code = COAP_RESPONSE_CODE(201); { /* split path into segments and add Location-Path options */ unsigned char _b[LOCSIZE]; @@ -517,9 +517,9 @@ hnd_post_rd(coap_context_t *ctx, while (nseg--) { coap_add_option(response, COAP_OPTION_LOCATION_PATH, - COAP_OPT_LENGTH(b), - COAP_OPT_VALUE(b)); - b += COAP_OPT_SIZE(b); + coap_opt_length(b), + coap_opt_value(b)); + b += coap_opt_size(b); } } } diff --git a/examples/coap-server.c b/examples/coap-server.c index 48c4c6a0..2fc29385 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -79,7 +79,7 @@ hnd_get_index(coap_context_t *ctx UNUSED_PARAM, coap_pdu_t *response) { unsigned char buf[3]; - response->hdr->code = COAP_RESPONSE_CODE(205); + response->code = COAP_RESPONSE_CODE(205); coap_add_option(response, COAP_OPTION_CONTENT_TYPE, @@ -110,7 +110,7 @@ hnd_get_time(coap_context_t *ctx, * when query ?ticks is given. */ /* if my_clock_base was deleted, we pretend to have no such resource */ - response->hdr->code = + response->code = my_clock_base ? COAP_RESPONSE_CODE(205) : COAP_RESPONSE_CODE(404); if (coap_find_observer(resource, session, token)) { @@ -138,19 +138,13 @@ hnd_get_time(coap_context_t *ctx, if (query != NULL && memcmp(query->s, "ticks", min(5, query->length)) == 0) { /* output ticks */ - len = snprintf((char *)buf, - min(sizeof(buf), - response->max_size - response->length), - "%u", (unsigned int)now); + len = snprintf((char *)buf, sizeof(buf), "%u", (unsigned int)now); coap_add_data(response, len, buf); } else { /* output human-readable time */ struct tm *tmp; tmp = gmtime(&now); - len = strftime((char *)buf, - min(sizeof(buf), - response->max_size - response->length), - "%b %d %H:%M:%S", tmp); + len = strftime((char *)buf, sizeof(buf), "%b %d %H:%M:%S", tmp); coap_add_data(response, len, buf); } } @@ -174,7 +168,7 @@ hnd_put_time(coap_context_t *ctx UNUSED_PARAM, */ /* if my_clock_base was deleted, we pretend to have no such resource */ - response->hdr->code = + response->code = my_clock_base ? COAP_RESPONSE_CODE(204) : COAP_RESPONSE_CODE(201); coap_resource_set_dirty(resource, NULL); @@ -220,10 +214,10 @@ hnd_get_async(coap_context_t *ctx, size_t size; if (async) { - if (async->id != request->hdr->id) { + if (async->id != request->tid) { coap_opt_filter_t f; coap_option_filter_clear(f); - response->hdr->code = COAP_RESPONSE_CODE(503); + response->code = COAP_RESPONSE_CODE(503); } return; } @@ -249,7 +243,7 @@ check_async(coap_context_t *ctx, coap_pdu_t *response; coap_async_state_t *tmp; - size_t size = sizeof(coap_hdr_t) + 13; + size_t size = 13; if (!async || now < async->created + (unsigned long)async->appdata) return; @@ -265,7 +259,7 @@ check_async(coap_context_t *ctx, return; } - response->hdr->id = coap_new_message_id(async->session); + response->tid = coap_new_message_id(async->session); if (async->tokenlen) coap_add_token(response, async->tokenlen, async->token); @@ -274,7 +268,7 @@ check_async(coap_context_t *ctx, if (coap_send(async->session, response) == COAP_INVALID_TID) { debug("check_async: cannot send response for message %d\n", - ntohs(response->hdr->id)); + response->tid); } coap_remove_async(ctx, async->session, async->id, &tmp); coap_free_async(async); diff --git a/examples/contiki/coap-observer.c b/examples/contiki/coap-observer.c index da892575..7d612f01 100644 --- a/examples/contiki/coap-observer.c +++ b/examples/contiki/coap-observer.c @@ -156,9 +156,9 @@ PROCESS_THREAD(coap_server_process, ev, data) while (res--) { coap_add_option(request, COAP_OPTION_URI_PATH, - COAP_OPT_LENGTH(buf), COAP_OPT_VALUE(buf)); + coap_opt_length(buf), coap_opt_value(buf)); - buf += COAP_OPT_SIZE(buf); + buf += coap_opt_size(buf); } } diff --git a/examples/etsi_iot_01.c b/examples/etsi_iot_01.c index 153163fb..1f5d5284 100644 --- a/examples/etsi_iot_01.c +++ b/examples/etsi_iot_01.c @@ -131,7 +131,7 @@ hnd_get_index(coap_context_t *ctx, struct coap_resource_t *resource, coap_pdu_t *response) { unsigned char buf[3]; - response->hdr->code = COAP_RESPONSE_CODE(205); + response->code = COAP_RESPONSE_CODE(205); coap_add_option(response, COAP_OPTION_CONTENT_TYPE, coap_encode_var_bytes(buf, COAP_MEDIATYPE_TEXT_PLAIN), buf); @@ -154,12 +154,12 @@ hnd_get_resource(coap_context_t *ctx, struct coap_resource_t *resource, test_payload = coap_find_payload(resource->key); if (!test_payload) { - response->hdr->code = COAP_RESPONSE_CODE(500); + response->code = COAP_RESPONSE_CODE(500); return; } - response->hdr->code = COAP_RESPONSE_CODE(205); + response->code = COAP_RESPONSE_CODE(205); coap_add_option(response, COAP_OPTION_CONTENT_TYPE, coap_encode_var_bytes(buf, test_payload->media_type), buf); @@ -180,13 +180,13 @@ hnd_get_resource(coap_context_t *ctx, struct coap_resource_t *resource, switch (res) { case -2: /* illegal block */ - response->hdr->code = COAP_RESPONSE_CODE(400); + response->code = COAP_RESPONSE_CODE(400); goto error; case -1: /* should really not happen */ assert(0); /* fall through if assert is a no-op */ case -3: /* cannot handle request */ - response->hdr->code = COAP_RESPONSE_CODE(500); + response->code = COAP_RESPONSE_CODE(500); goto error; default: /* everything is good */ ; @@ -214,8 +214,8 @@ hnd_get_resource(coap_context_t *ctx, struct coap_resource_t *resource, error: coap_add_data(response, - strlen(coap_response_phrase(response->hdr->code)), - (unsigned char *)coap_response_phrase(response->hdr->code)); + strlen(coap_response_phrase(response->code)), + (unsigned char *)coap_response_phrase(response->code)); } /* DELETE handler for dynamic resources created by POST /test */ @@ -232,7 +232,7 @@ 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); + response->code = COAP_RESPONSE_CODE(202); } void @@ -260,7 +260,7 @@ hnd_post_test(coap_context_t *ctx, struct coap_resource_t *resource, uri = (coap_dynamic_uri_t *)coap_malloc(sizeof(coap_dynamic_uri_t) + l); if (!(test_payload && uri)) { coap_log(LOG_CRIT, "cannot allocate new resource under /test"); - response->hdr->code = COAP_RESPONSE_CODE(500); + response->code = COAP_RESPONSE_CODE(500); coap_free(test_payload); coap_free(uri); } else { @@ -291,12 +291,12 @@ hnd_post_test(coap_context_t *ctx, struct coap_resource_t *resource, while (res--) { coap_add_option(response, COAP_OPTION_LOCATION_PATH, - COAP_OPT_LENGTH(buf), COAP_OPT_VALUE(buf)); + coap_opt_length(buf), coap_opt_value(buf)); - buf += COAP_OPT_SIZE(buf); + buf += coap_opt_size(buf); } - response->hdr->code = COAP_RESPONSE_CODE(201); + response->code = COAP_RESPONSE_CODE(201); } } @@ -311,7 +311,7 @@ hnd_put_test(coap_context_t *ctx, struct coap_resource_t *resource, size_t len; unsigned char *data; - response->hdr->code = COAP_RESPONSE_CODE(204); + response->code = COAP_RESPONSE_CODE(204); coap_get_data(request, &len, &data); @@ -349,7 +349,7 @@ hnd_put_test(coap_context_t *ctx, struct coap_resource_t *resource, return; error: warn("cannot modify resource\n"); - response->hdr->code = COAP_RESPONSE_CODE(500); + response->code = COAP_RESPONSE_CODE(500); } void @@ -365,7 +365,7 @@ hnd_delete_test(coap_context_t *ctx, struct coap_resource_t *resource, payload->length = 0; #endif - response->hdr->code = COAP_RESPONSE_CODE(202); + response->code = COAP_RESPONSE_CODE(202); } void @@ -378,7 +378,7 @@ hnd_get_query(coap_context_t *ctx, struct coap_resource_t *resource, size_t len, L; unsigned char buf[70]; - response->hdr->code = COAP_RESPONSE_CODE(205); + response->code = COAP_RESPONSE_CODE(205); coap_add_option(response, COAP_OPTION_CONTENT_TYPE, coap_encode_var_bytes(buf, COAP_MEDIATYPE_TEXT_PLAIN), buf); @@ -416,10 +416,10 @@ hnd_get_separate(coap_context_t *ctx, struct coap_resource_t *resource, unsigned long delay = 5; if (async) { - if (async->id != request->hdr->id) { + if (async->id != request->tid) { coap_opt_filter_t f; coap_option_filter_clear(f); - response->hdr->code = COAP_RESPONSE_CODE(503); + response->code = COAP_RESPONSE_CODE(503); } return; } @@ -456,7 +456,7 @@ check_async(coap_context_t *ctx, coap_tick_t now) { coap_pdu_t *response; coap_async_state_t *tmp; unsigned char buf[2]; - size_t size = sizeof(coap_hdr_t) + 8; + size_t size = 8; if (!async || now < async->created + (unsigned long)async->appdata) return; @@ -474,7 +474,7 @@ check_async(coap_context_t *ctx, coap_tick_t now) { return; } - response->hdr->id = coap_new_message_id(ctx); + response->tid = coap_new_message_id(ctx); if (async->tokenlen) coap_add_token(response, async->tokenlen, async->token); @@ -486,7 +486,7 @@ check_async(coap_context_t *ctx, coap_tick_t now) { if (coap_send(ctx, &async->peer, response) == COAP_INVALID_TID) { debug("check_async: cannot send response for message %d\n", - ntohs(response->hdr->id)); + response->tid); } coap_remove_async(ctx, async->id, &tmp); diff --git a/examples/tiny.c b/examples/tiny.c index b309ce45..f83b1c26 100644 --- a/examples/tiny.c +++ b/examples/tiny.c @@ -32,9 +32,9 @@ make_pdu( unsigned int value ) { if ( ! ( pdu = coap_new_pdu() ) ) return NULL; - pdu->hdr->type = COAP_MESSAGE_NON; - pdu->hdr->code = COAP_REQUEST_POST; - pdu->hdr->id = htons(id++); + pdu->type = COAP_MESSAGE_NON; + pdu->code = COAP_REQUEST_POST; + pdu->tid = id++; enc = COAP_PSEUDOFP_ENCODE_8_4_DOWN(value,ls); coap_add_data( pdu, 1, &enc); diff --git a/include/coap/address.h b/include/coap/address.h index fb6188da..df0ba489 100644 --- a/include/coap/address.h +++ b/include/coap/address.h @@ -42,7 +42,7 @@ typedef struct coap_address_t { typedef struct coap_address_t { uip_ipaddr_t addr; - unsigned short port; + uint16_t port; } coap_address_t; #define _coap_address_equals_impl(A,B) \ diff --git a/include/coap/async.h b/include/coap/async.h index 469eceeb..2c4135f3 100644 --- a/include/coap/async.h +++ b/include/coap/async.h @@ -43,7 +43,7 @@ typedef struct coap_async_state_t { * asynchronous state object. */ void *appdata; - unsigned short message_id; /**< id of last message seen */ + uint16_t message_id; /**< id of last message seen */ coap_session_t *session; /**< transaction session */ coap_tid_t id; /**< transaction id */ struct coap_async_state_t *next; /**< internally used for linking */ diff --git a/include/coap/block.h b/include/coap/block.h index b89a3623..6f56a558 100644 --- a/include/coap/block.h +++ b/include/coap/block.h @@ -21,8 +21,7 @@ #ifndef COAP_MAX_BLOCK_SZX /** - * The largest value for the SZX component in a Block option. Note that - * 1 << (COAP_MAX_BLOCK_SZX + 4) should not exceed COAP_DEFAULT_PDU_SIZE. + * The largest value for the SZX component in a Block option. */ #define COAP_MAX_BLOCK_SZX 6 #endif /* COAP_MAX_BLOCK_SZX */ @@ -42,15 +41,15 @@ typedef struct { * returns @c NULL. */ #define COAP_OPT_BLOCK_LAST(opt) \ - (COAP_OPT_LENGTH(opt) ? (COAP_OPT_VALUE(opt) + (COAP_OPT_LENGTH(opt)-1)) : 0) + (coap_opt_length(opt) ? (coap_opt_const_value(opt) + (coap_opt_length(opt)-1)) : 0) /** Returns the value of the More-bit of a Block option @p opt. */ #define COAP_OPT_BLOCK_MORE(opt) \ - (COAP_OPT_LENGTH(opt) ? (*COAP_OPT_BLOCK_LAST(opt) & 0x08) : 0) + (coap_opt_length(opt) ? (*COAP_OPT_BLOCK_LAST(opt) & 0x08) : 0) /** Returns the value of the SZX-field of a Block option @p opt. */ #define COAP_OPT_BLOCK_SZX(opt) \ - (COAP_OPT_LENGTH(opt) ? (*COAP_OPT_BLOCK_LAST(opt) & 0x07) : 0) + (coap_opt_length(opt) ? (*COAP_OPT_BLOCK_LAST(opt) & 0x07) : 0) /** * Returns the value of field @c num in the given block option @p block_opt. @@ -62,7 +61,7 @@ unsigned int coap_opt_block_num(const coap_opt_t *block_opt); * bytes of data for a block size of 1 << (@p szx + 4). */ COAP_STATIC_INLINE int -coap_more_blocks(size_t data_len, unsigned int num, unsigned short szx) { +coap_more_blocks(size_t data_len, unsigned int num, uint16_t szx) { return ((num+1) << (szx + 4)) < data_len; } @@ -70,9 +69,9 @@ coap_more_blocks(size_t data_len, unsigned int num, unsigned short szx) { COAP_STATIC_INLINE void coap_opt_block_set_m(coap_opt_t *block_opt, int m) { if (m) - *(COAP_OPT_VALUE(block_opt) + (COAP_OPT_LENGTH(block_opt) - 1)) |= 0x08; + *(coap_opt_value(block_opt) + (coap_opt_length(block_opt) - 1)) |= 0x08; else - *(COAP_OPT_VALUE(block_opt) + (COAP_OPT_LENGTH(block_opt) - 1)) &= ~0x08; + *(coap_opt_value(block_opt) + (coap_opt_length(block_opt) - 1)) &= ~0x08; } /** @@ -88,7 +87,7 @@ coap_opt_block_set_m(coap_opt_t *block_opt, int m) { * * @return @c 1 on success, @c 0 otherwise. */ -int coap_get_block(coap_pdu_t *pdu, unsigned short type, coap_block_t *block); +int coap_get_block(coap_pdu_t *pdu, uint16_t type, coap_block_t *block); /** * Writes a block option of type @p type to message @p pdu. If the requested @@ -111,7 +110,7 @@ int coap_get_block(coap_pdu_t *pdu, unsigned short type, coap_block_t *block); * @return @c 1 on success, or a negative value on error. */ int coap_write_block_opt(coap_block_t *block, - unsigned short type, + uint16_t type, coap_pdu_t *pdu, size_t data_length); diff --git a/include/coap/coap_dtls.h b/include/coap/coap_dtls.h index 13d54fca..9bcf4a86 100644 --- a/include/coap/coap_dtls.h +++ b/include/coap/coap_dtls.h @@ -78,13 +78,28 @@ void coap_dtls_session_update_mtu(coap_session_t *session); * Send data to a DTLS peer. * * @param session The CoAP session - * @param pdu The CoAP PDU + * @param data pointer to data + * @param size number of bytes to send * @return 0 if this would be blocking, -1 if there is an error or the number of cleartext bytes sent */ int coap_dtls_send(coap_session_t *session, const uint8_t *data, size_t data_len); +/** + * Send data to a TLS peer. + * + * @param session The CoAP session + * @param data pointer to data + * @param size number of bytes to send + * @param flush set to non-zero if the message should be sent now + * @return 0 if this would be blocking, -1 if there is an error or the number of cleartext bytes sent + */ +int coap_tls_send(coap_session_t *session, + const uint8_t *data, + size_t data_len, + int flush); + /** * Check if timeout is handled per session or per context. * diff --git a/include/coap/coap_session.h b/include/coap/coap_session.h index 640ca082..7137a141 100644 --- a/include/coap/coap_session.h +++ b/include/coap/coap_session.h @@ -20,13 +20,8 @@ struct coap_queue_t; #define COAP_DEFAULT_SESSION_TIMEOUT 300 -typedef uint8_t coap_proto_t; -/** -* coap_proto_t values -*/ -#define COAP_PROTO_NONE 0 -#define COAP_PROTO_UDP 1 -#define COAP_PROTO_DTLS 2 +#define COAP_PROTO_NOT_RELIABLE(p) ((p)==COAP_PROTO_UDP || (p)==COAP_PROTO_DTLS) +#define COAP_PROTO_RELIABLE(p) ((p)==COAP_PROTO_TCP || (p)==COAP_PROTO_TLS) typedef uint8_t coap_session_type_t; /** @@ -191,6 +186,20 @@ coap_session_t *coap_new_client_session_psk( ssize_t coap_session_send(coap_session_t *session, const uint8_t *data, size_t datalen); +/** +* Send a pdu according to the session's protocol. This function returns +* the number of bytes that have been transmitted, or a value less than zero +* on error. +* +* @param session Session to send pdu on. +* @param pdu The pdu to send. +* +* @return The number of bytes written on success, or a value +* less than zero on error. +*/ +ssize_t coap_session_send_pdu(coap_session_t *session, coap_pdu_t *pdu); + + /** * Get session description. * diff --git a/include/coap/encode.h b/include/coap/encode.h index 5a4f28c0..582ad31b 100644 --- a/include/coap/encode.h +++ b/include/coap/encode.h @@ -16,6 +16,8 @@ # include #endif +#include + #define Nn 8 /* duplicate definition of N if built on sky motes */ #define ENCODE_HEADER_SIZE 4 #define HIBIT (1 << (Nn - 1)) @@ -47,13 +49,13 @@ extern int coap_flsll(long long i); * Decodes multiple-length byte sequences. buf points to an input byte sequence * of length len. Returns the decoded value. */ -unsigned int coap_decode_var_bytes(unsigned char *buf,unsigned int len); +unsigned int coap_decode_var_bytes(const uint8_t *buf, unsigned int len); /** * Encodes multiple-length byte sequences. buf points to an output buffer of * sufficient length to store the encoded bytes. val is the value to encode. * Returns the number of bytes used to encode val or 0 on error. */ -unsigned int coap_encode_var_bytes(unsigned char *buf, unsigned int val); +unsigned int coap_encode_var_bytes(uint8_t *buf, unsigned int val); #endif /* _COAP_ENCODE_H_ */ diff --git a/include/coap/net.h b/include/coap/net.h index cbe2dce4..2d4ebbd6 100644 --- a/include/coap/net.h +++ b/include/coap/net.h @@ -118,7 +118,7 @@ typedef struct coap_context_t { * value is set by coap_new_context() and is usually a random value. A new * message id can be created with coap_new_message_id(). */ - unsigned short message_id; + uint16_t message_id; /** * The next value to be used for Observe. This field is global for all @@ -241,12 +241,7 @@ void coap_context_set_psk( coap_context_t *ctx, const char *hint, */ COAP_STATIC_INLINE uint16_t coap_new_message_id(coap_session_t *session) { - session->tx_mid++; -#ifndef WITH_CONTIKI - return htons(session->tx_mid); -#else /* WITH_CONTIKI */ - return uip_htons(session->tx_mid); -#endif + return ++session->tx_mid; } /** diff --git a/include/coap/option.h b/include/coap/option.h index 5bb0e183..ba6147db 100644 --- a/include/coap/option.h +++ b/include/coap/option.h @@ -22,14 +22,14 @@ * Use byte-oriented access methods here because sliding a complex struct * coap_opt_t over the data buffer may cause bus error on certain platforms. */ -typedef unsigned char coap_opt_t; +typedef uint8_t coap_opt_t; #define PCHAR(p) ((coap_opt_t *)(p)) /** Representation of CoAP options. */ typedef struct { - unsigned short delta; + uint16_t delta; size_t length; - unsigned char *value; + uint8_t *value; } coap_option_t; /** @@ -59,9 +59,6 @@ size_t coap_opt_parse(const coap_opt_t *opt, */ size_t coap_opt_size(const coap_opt_t *opt); -/** @deprecated { Use coap_opt_size() instead. } */ -#define COAP_OPT_SIZE(opt) coap_opt_size(opt) - /** * Calculates the beginning of the PDU's option section. * @@ -76,7 +73,7 @@ coap_opt_t *options_start(coap_pdu_t *pdu); * @hideinitializer */ #define options_next(opt) \ - ((coap_opt_t *)((unsigned char *)(opt) + COAP_OPT_SIZE(opt))) + ((coap_opt_t *)((uint8_t *)(opt) + coap_opt_size(opt))) /** * @defgroup opt_filter Option Filters @@ -157,7 +154,7 @@ coap_option_filter_clear(coap_opt_filter_t f) { * * @return @c 1 if bit was set, @c 0 otherwise. */ -int coap_option_filter_set(coap_opt_filter_t filter, unsigned short type); +int coap_option_filter_set(coap_opt_filter_t filter, uint16_t type); /** * Clears the corresponding entry for @p type in @p filter. This @@ -169,7 +166,7 @@ int coap_option_filter_set(coap_opt_filter_t filter, unsigned short type); * * @return @c 1 if bit was set, @c 0 otherwise. */ -int coap_option_filter_unset(coap_opt_filter_t filter, unsigned short type); +int coap_option_filter_unset(coap_opt_filter_t filter, uint16_t type); /** * Checks if @p type is contained in @p filter. This function returns @@ -181,7 +178,7 @@ int coap_option_filter_unset(coap_opt_filter_t filter, unsigned short type); * * @return @c 1 if @p type was found, @c 0 otherwise, or @c -1 on error. */ -int coap_option_filter_get(const coap_opt_filter_t filter, unsigned short type); +int coap_option_filter_get(const coap_opt_filter_t filter, uint16_t type); /** * Sets the corresponding bit for @p type in @p filter. This function returns @c @@ -196,7 +193,7 @@ int coap_option_filter_get(const coap_opt_filter_t filter, unsigned short type); * @return @c 1 if bit was set, @c -1 otherwise. */ COAP_STATIC_INLINE int -coap_option_setb(coap_opt_filter_t filter, unsigned short type) { +coap_option_setb(coap_opt_filter_t filter, uint16_t type) { return coap_option_filter_set(filter, type) ? 1 : -1; } @@ -213,7 +210,7 @@ coap_option_setb(coap_opt_filter_t filter, unsigned short type) { * @return @c 1 if bit was set, @c -1 otherwise. */ COAP_STATIC_INLINE int -coap_option_clrb(coap_opt_filter_t filter, unsigned short type) { +coap_option_clrb(coap_opt_filter_t filter, uint16_t type) { return coap_option_filter_unset(filter, type) ? 1 : -1; } @@ -230,7 +227,7 @@ coap_option_clrb(coap_opt_filter_t filter, unsigned short type) { * @return @c 1 if bit was set, @c 0 if not, @c -1 on error. */ COAP_STATIC_INLINE int -coap_option_getb(const coap_opt_filter_t filter, unsigned short type) { +coap_option_getb(const coap_opt_filter_t filter, uint16_t type) { return coap_option_filter_get(filter, type); } @@ -252,7 +249,7 @@ coap_option_getb(const coap_opt_filter_t filter, unsigned short type) { */ typedef struct { size_t length; /**< remaining length of PDU */ - unsigned short type; /**< decoded option type */ + uint16_t 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 *next_option; /**< pointer to the unparsed next option */ @@ -311,7 +308,7 @@ coap_opt_t *coap_option_next(coap_opt_iterator_t *oi); * not found. */ coap_opt_t *coap_check_option(coap_pdu_t *pdu, - unsigned short type, + uint16_t type, coap_opt_iterator_t *oi); /** @@ -330,7 +327,7 @@ coap_opt_t *coap_check_option(coap_pdu_t *pdu, */ size_t coap_opt_setheader(coap_opt_t *opt, size_t maxlen, - unsigned short delta, + uint16_t delta, size_t length); /** @@ -350,8 +347,8 @@ size_t coap_opt_setheader(coap_opt_t *opt, */ size_t coap_opt_encode(coap_opt_t *opt, size_t n, - unsigned short delta, - const unsigned char *val, + uint16_t delta, + const uint8_t *val, size_t length); /** @@ -364,14 +361,7 @@ size_t coap_opt_encode(coap_opt_t *opt, * * @return The number of bytes read or @c 0 on error. */ -unsigned short coap_opt_delta(const coap_opt_t *opt); - -/** @deprecated { Use coap_opt_delta() instead. } */ -#define COAP_OPT_DELTA(opt) coap_opt_delta(opt) - -/** @deprecated { Use coap_opt_encode() instead. } */ -#define COAP_OPT_SETDELTA(opt,val) \ - coap_opt_encode((opt), COAP_DEFAULT_PDU_SIZE, (val), NULL, 0) +uint16_t coap_opt_delta(const coap_opt_t *opt); /** * Returns the length of the given option. @p opt must point to an option jump @@ -386,10 +376,7 @@ unsigned short coap_opt_delta(const coap_opt_t *opt); * * @return The option's length or @c 0 when undefined. */ -unsigned short coap_opt_length(const coap_opt_t *opt); - -/** @deprecated { Use coap_opt_length() instead. } */ -#define COAP_OPT_LENGTH(opt) coap_opt_length(opt) +uint16_t coap_opt_length(const coap_opt_t *opt); /** * Returns a pointer to the value of the given option. @p opt must point to an @@ -400,10 +387,12 @@ unsigned short coap_opt_length(const coap_opt_t *opt); * * @return A pointer to the option value or @c NULL on error. */ -unsigned char *coap_opt_value(coap_opt_t *opt); +uint8_t *coap_opt_value(coap_opt_t *opt); -/** @deprecated { Use coap_opt_value() instead. } */ -#define COAP_OPT_VALUE(opt) coap_opt_value((coap_opt_t *)opt) +COAP_STATIC_INLINE const uint8_t * +coap_opt_const_value( const coap_opt_t *opt ) { + return coap_opt_value((coap_opt_t*)opt); +} /** @} */ diff --git a/include/coap/pdu.h b/include/coap/pdu.h index 6e5a3004..5f19db20 100644 --- a/include/coap/pdu.h +++ b/include/coap/pdu.h @@ -23,12 +23,14 @@ struct coap_session_t; #include #endif -#define COAP_DEFAULT_PORT 5683 /* CoAP default UDP port */ -#define COAPS_DEFAULT_PORT 5684 /* CoAP default UDP port for secure transmission */ +#include + +#define COAP_DEFAULT_PORT 5683 /* CoAP default UDP/TCP port */ +#define COAPS_DEFAULT_PORT 5684 /* CoAP default UDP/TCP port for secure transmission */ #define COAP_DEFAULT_MAX_AGE 60 /* default maximum object lifetime in seconds */ -#ifndef COAP_DEFAULT_PDU_SIZE -#define COAP_DEFAULT_PDU_SIZE 1152 /* default maximum size of a CoAP PDU */ -#endif /* COAP_DEFAULT_PDU_SIZE */ +#ifndef COAP_DEFAULT_MTU +#define COAP_DEFAULT_MTU 1152 +#endif /* COAP_DEFAULT_MTU */ #define COAP_DEFAULT_VERSION 1 /* version of CoAP supported */ #define COAP_DEFAULT_SCHEME "coap" /* the default scheme for CoAP URIs */ @@ -146,6 +148,24 @@ char *coap_response_phrase(unsigned char code); #endif #define COAP_RESPONSE_X_242 COAP_RESPONSE_CODE(402) /* Critical Option not supported */ +#define COAP_SIGNALING_CODE(N) (((N)/100 << 5) | (N)%100) +#define COAP_SIGNALING_CSM COAP_SIGNALING_CODE(701) +#define COAP_SIGNALING_PING COAP_SIGNALING_CODE(702) +#define COAP_SIGNALING_PONG COAP_SIGNALING_CODE(703) +#define COAP_SIGNALING_RELEASE COAP_SIGNALING_CODE(704) +#define COAP_SIGNALING_ABORT COAP_SIGNALING_CODE(705) + +/* Applies to COAP_SIGNALING_CSM */ +#define COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE 2 +#define COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER 4 +/* Applies to COAP_SIGNALING_PING / COAP_SIGNALING_PONG */ +#define COAP_SIGNALING_OPTION_CUSTODY 2 +/* Applies to COAP_SIGNALING_RELEASE */ +#define COAP_SIGNALING_OPTION_ALTERNATIVE_ADDRESS 2 +#define COAP_SIGNALING_OPTION_HOLD_OFF 4 +/* Applies to COAP_SIGNALING_ABORT */ +#define COAP_SIGNALING_OPTION_BAD_CSM_OPTION 2 + /* CoAP media type encoding */ #define COAP_MEDIATYPE_TEXT_PLAIN 0 /* text/plain (UTF-8) */ @@ -190,46 +210,6 @@ typedef int coap_tid_t; #define COAP_PDU_DELAYED -3 -#if defined(_MSC_VER) -#include -#pragma pack( push, 1 ) -typedef struct { - uint16_t token_length : 4; /* length of Token */ - uint16_t type : 2; /* type flag */ - uint16_t version : 2; /* protocol version */ - uint16_t code : 8; /* request method (value 1--10) or response - code (value 40-255) */ - uint16_t id; /* transaction id (network byte order!) */ - uint8_t token[]; /* the actual token, if any */ -} coap_hdr_t; -#pragma pack( pop ) -#elif defined(WORDS_BIGENDIAN) -typedef struct { - unsigned int version:2; /* protocol version */ - unsigned int type:2; /* type flag */ - unsigned int token_length:4; /* length of Token */ - unsigned int code:8; /* request method (value 1--10) or response - code (value 40-255) */ - unsigned short id; /* message id */ - unsigned char token[]; /* the actual token, if any */ -} coap_hdr_t; -#else -typedef struct { - unsigned int token_length:4; /* length of Token */ - unsigned int type:2; /* type flag */ - unsigned int version:2; /* protocol version */ - unsigned int code:8; /* request method (value 1--10) or response - code (value 40-255) */ - unsigned short id; /* transaction id (network byte order!) */ - unsigned char token[]; /* the actual token, if any */ -} coap_hdr_t; -#endif - -#define COAP_MESSAGE_IS_EMPTY(MSG) ((MSG)->code == 0) -#define COAP_MESSAGE_IS_REQUEST(MSG) (!COAP_MESSAGE_IS_EMPTY(MSG) \ - && ((MSG)->code < 32)) -#define COAP_MESSAGE_IS_RESPONSE(MSG) ((MSG)->code >= 64) - #define COAP_OPT_LONG 0x0F /* OC == 0b1111 indicates that the option list * in a CoAP message is limited by 0b11110000 * marker */ @@ -244,7 +224,7 @@ typedef struct { * structure (see macro COAP_OPTION_DATA). */ typedef struct { - unsigned short key; /* the option key (no delta coding) */ + uint16_t key; /* the option key (no delta coding) */ unsigned int length; } coap_option; @@ -253,35 +233,47 @@ typedef struct { #define COAP_OPTION_DATA(option) ((unsigned char *)&(option) + sizeof(coap_option)) /** - * Header structure for CoAP PDUs + * structure for CoAP PDUs + * token, if any, follows the fixed size header, then options until + * payload marker (0xff), then the payload if stored inline. + * Memory layout is: + * <---header--->|<---token---><---options--->0xff<---payload---> + * header is addressed with a negative offset to token, its maximum size is + * max_hdr_size. + * options starts at token + token_length + * payload starts at data, its length is used_size - (data - token) */ typedef struct coap_pdu_t { - size_t max_size; /**< allocated storage for options and data */ - coap_hdr_t *hdr; /**< Address of the first byte of the CoAP message. - * This may or may not equal (coap_hdr_t*)(pdu+1) - * depending on the memory management - * implementation. */ - unsigned short max_delta; /**< highest option number */ - unsigned short length; /**< PDU length (including header, options, data) */ - unsigned char *data; /**< payload */ - -#ifdef WITH_LWIP + uint8_t type; /**< message type */ + uint8_t code; /**< request method (value 1--10) or response code (value 40-255) */ + uint8_t max_hdr_size; /**< space reserved for protocol-specific header */ + uint8_t token_length; /**< length of Token */ + uint16_t tid; /**< transaction id, if any, in regular host byte order */ + uint16_t max_delta; /**< highest option number */ + size_t max_size; /**< allocated storage for token, options and payload */ + size_t used_size; /**< used bytes of storage for token, options and payload */ + uint8_t *token; /**< first byte of token, if any, or options */ + uint8_t *data; /**< first byte of payload, if any */ +#ifdef LWIP struct pbuf *pbuf; /**< lwIP PBUF. The package data will always reside - * inside the pbuf's payload, but this pointer - * has to be kept because no exact offset can be - * given. This field must not be accessed from - * outside, because the pbuf's reference count - * is checked to be 1 when the pbuf is assigned - * to the pdu, and the pbuf stays exclusive to - * this pdu. */ + * inside the pbuf's payload, but this pointer + * has to be kept because no exact offset can be + * given. This field must not be accessed from + * outside, because the pbuf's reference count + * is checked to be 1 when the pbuf is assigned + * to the pdu, and the pbuf stays exclusive to + * this pdu. */ #endif } coap_pdu_t; -/** - * Options in coap_pdu_t are accessed with the macro COAP_OPTION. - */ -#define COAP_OPTION(node) ((coap_option *)(node)->options) +#define COAP_PDU_IS_EMPTY(pdu) ((pdu)->code == 0) +#define COAP_PDU_IS_REQUEST(pdu) (!COAP_PDU_IS_EMPTY(pdu) && (pdu)->code < 32) +#define COAP_PDU_IS_RESPONSE(pdu) ((pdu)->code >= 64 && (pdu)->code < 224) +#define COAP_PDU_IS_SIGNALING(pdu) ((pdu)->code >= 224) + +#define COAP_PDU_MAX_UDP_HEADER_SIZE 4 +#define COAP_PDU_MAX_TCP_HEADER_SIZE 6 #ifdef WITH_LWIP /** @@ -302,9 +294,20 @@ typedef struct coap_pdu_t { coap_pdu_t * coap_pdu_from_pbuf(struct pbuf *pbuf); #endif +typedef uint8_t coap_proto_t; /** - * Creates a new CoAP PDU of given @p size (must be large enough to hold the - * basic CoAP message header (coap_hdr_t). The function returns a pointer to the +* coap_proto_t values +*/ +#define COAP_PROTO_NONE 0 +#define COAP_PROTO_UDP 1 +#define COAP_PROTO_DTLS 2 +#define COAP_PROTO_TCP 3 +#define COAP_PROTO_TLS 4 + + +/** + * Creates a new CoAP PDU with at least enough storage space for the given + * @p size maximum message size. The function returns a pointer to the * node coap_pdu_t object on success, or @c NULL on error. The storage allocated * for the result must be released with coap_delete_pdu() if coap_send() is not * called. @@ -312,16 +315,14 @@ coap_pdu_t * coap_pdu_from_pbuf(struct pbuf *pbuf); * @param type The type of the PDU (one of COAP_MESSAGE_CON, COAP_MESSAGE_NON, * COAP_MESSAGE_ACK, COAP_MESSAGE_RST). * @param code The message code. - * @param id The message id to set or COAP_INVALID_TID if unknown. - * @param size The number of bytes to allocate for the actual message. + * @param tid The transcation id to set or 0 if unknown / not applicable. + * @param size The number of bytes to allocate for the message, or zero to use + * a reasonable start size. * * @return A pointer to the new PDU object or @c NULL on error. */ coap_pdu_t * -coap_pdu_init(unsigned char type, - unsigned char code, - unsigned short id, - size_t size); +coap_pdu_init(uint8_t type, uint8_t code, uint16_t tid, size_t size); /** * Clears any contents from @p pdu and resets @c version field, @c @@ -333,11 +334,8 @@ void coap_pdu_clear(coap_pdu_t *pdu, size_t size); /** * Creates a new CoAP PDU. - * - * @deprecated This function allocates the maximum storage for each - * PDU. Use coap_pdu_init() instead. */ -coap_pdu_t *coap_new_pdu(struct coap_session_t *session); +coap_pdu_t *coap_new_pdu(); /** * Dispose of an CoAP PDU and frees associated storage. @@ -349,21 +347,78 @@ coap_pdu_t *coap_new_pdu(struct coap_session_t *session); void coap_delete_pdu(coap_pdu_t *); /** - * Parses @p data into the CoAP PDU structure given in @p result. +* Interprets @p data to determine the number of bytes in the header. +* This function returns @c 0 on error or a number greater than zero on success. +* +* @param proto Session's protocol +* @param data The first byte of raw data to parse as CoAP PDU. +* +* @return A value greater than zero on success or @c 0 on error. +*/ +size_t coap_pdu_parse_header_size(coap_proto_t proto, + const uint8_t *data); + +/** + * Parses @p data to extract the message size. + * @p length must be at least coap_pdu_parse_header_size(proto, data). * This function returns @c 0 on error or a number greater than zero on success. * + * @param proto Session's protocol * @param data The raw data to parse as CoAP PDU. * @param length The actual size of @p data. - * @param result The PDU structure to fill. Note that the structure must - * provide space for at least @p length bytes to hold the - * entire CoAP PDU. * * @return A value greater than zero on success or @c 0 on error. */ -int coap_pdu_parse(unsigned char *data, - size_t length, - coap_pdu_t *result); +size_t coap_pdu_parse_size(coap_proto_t proto, + const uint8_t *data, + size_t length); +/** + * Parses @p data into the CoAP PDU structure given in @p result. + * This function returns @c 0 on error or a number greater than zero on + * success. + * This function only parses the header, token and options, up to the + * payload start marker. + * The payload itself is not copied, only its start position is returned. + * It need not be present in the supplied data. + * coap_add_data() or coap_add_data_after() must be called later to add the + * data to the pdu if the payload is not empty. + * + * @param proto Session's protocol + * @param data The raw data to parse as CoAP PDU. + * @param length The actual size of @p data. + * @param pdu The PDU structure to fill. Note that the structure must + * provide space to hold at least the token and options + * part of the message. + * @param offset Position of payload inside data or 0 if message has an + * empty payload. + * + * @return 1 on success or @c 0 on error. + */ +int coap_pdu_parse_opt(coap_proto_t proto, + const uint8_t *data, + size_t length, + coap_pdu_t *pdu, + size_t *offset); + +/** +* Parses @p data into the CoAP PDU structure given in @p result. +* The target pdu must be large enough to +* This function returns @c 0 on error or a number greater than zero on success. +* +* @param proto Session's protocol +* @param data The raw data to parse as CoAP PDU. +* @param length The actual size of @p data. +* @param pdu The PDU structure to fill. Note that the structure must +* provide space to hold at least the token and options +* part of the message. +* +* @return 1 on success or @c 0 on error. +*/ +int coap_pdu_parse(coap_proto_t proto, + const uint8_t *data, + size_t length, + coap_pdu_t *pdu); /** * Adds token of length @p len to @p pdu. * Adding the token destroys any following contents of the pdu. Hence options @@ -379,7 +434,7 @@ int coap_pdu_parse(unsigned char *data, */ int coap_add_token(coap_pdu_t *pdu, size_t len, - const unsigned char *data); + const uint8_t *data); /** * Adds option of given type to pdu that is passed as first @@ -390,9 +445,9 @@ int coap_add_token(coap_pdu_t *pdu, * called. This function returns the number of bytes written or @c 0 on error. */ size_t coap_add_option(coap_pdu_t *pdu, - unsigned short type, - unsigned int len, - const unsigned char *data); + uint16_t type, + size_t len, + const uint8_t *data); /** * Adds option of given type to pdu that is passed as first parameter, but does @@ -401,9 +456,9 @@ size_t coap_add_option(coap_pdu_t *pdu, * address to which the option data has to be written before the PDU can be * sent, or @c NULL on error. */ -unsigned char *coap_add_option_later(coap_pdu_t *pdu, - unsigned short type, - unsigned int len); +uint8_t *coap_add_option_later(coap_pdu_t *pdu, + uint16_t type, + size_t len); /** * Adds given data to the pdu that is passed as first parameter. Note that the @@ -411,8 +466,17 @@ unsigned char *coap_add_option_later(coap_pdu_t *pdu, * only once per PDU, otherwise the result is undefined. */ int coap_add_data(coap_pdu_t *pdu, - unsigned int len, - const unsigned char *data); + size_t len, + const uint8_t *data); + +/** + * Adds given data to the pdu that is passed as first parameter but does not + * copyt it. Note that the PDU's data is destroyed by coap_add_option(). + * coap_add_data() must be have been called once for this PDU, otherwise the + * result is undefined. + * The actual data must be copied at the returned location. + */ +uint8_t *coap_add_data_after(coap_pdu_t *pdu, size_t len); /** * Retrieves the length and data pointer of specified PDU. Returns 0 on error or @@ -421,6 +485,15 @@ int coap_add_data(coap_pdu_t *pdu, */ int coap_get_data(coap_pdu_t *pdu, size_t *len, - unsigned char **data); + uint8_t **data); + +/** + * Compose the protocol specific header for the specified PDU. + * @param pdu A newly composed PDU. + * @param proto The target wire protocol. + * @return Number of header bytes prepended before pdu->token or 0 on error. + */ + +size_t coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto); #endif /* _COAP_PDU_H_ */ diff --git a/include/coap/prng.h b/include/coap/prng.h index 1ed08055..4daed54d 100644 --- a/include/coap/prng.h +++ b/include/coap/prng.h @@ -30,7 +30,7 @@ */ COAP_STATIC_INLINE int contiki_prng_impl(unsigned char *buf, size_t len) { - unsigned short v = random_rand(); + uint16_t v = random_rand(); while (len > sizeof(v)) { memcpy(buf, &v, sizeof(v)); len -= sizeof(v); @@ -43,7 +43,7 @@ contiki_prng_impl(unsigned char *buf, size_t len) { } #define prng(Buf,Length) contiki_prng_impl((Buf), (Length)) -#define prng_init(Value) random_init((unsigned short)(Value)) +#define prng_init(Value) random_init((uint16_t)(Value)) #elif defined(WITH_LWIP) && defined(LWIP_RAND) COAP_STATIC_INLINE int lwip_prng_impl(unsigned char *buf, size_t len) { diff --git a/include/coap/uri.h b/include/coap/uri.h index 798d6678..30e0e1db 100644 --- a/include/coap/uri.h +++ b/include/coap/uri.h @@ -10,6 +10,8 @@ #ifndef _COAP_URI_H_ #define _COAP_URI_H_ +#include + #include "hashkey.h" #include "str.h" struct coap_pdu_t; @@ -32,7 +34,7 @@ enum coap_uri_scheme_t { */ typedef struct { str host; /**< host part of the URI */ - unsigned short port; /**< The port in host byte order */ + uint16_t port; /**< The port in host byte order */ str path; /**< Beginning of the first path segment. Use coap_split_path() to create Uri-Path options */ str query; /**< The query part if present */ diff --git a/libcoap-1.sym b/libcoap-1.sym index 20bef026..afcda40c 100644 --- a/libcoap-1.sym +++ b/libcoap-1.sym @@ -113,6 +113,7 @@ coap_package_version coap_packet_get_memmapped coap_packet_set_addr coap_pdu_clear +coap_pdu_encode_header coap_pdu_init coap_pdu_parse coap_peek_next diff --git a/src/async.c b/src/async.c index 0460f4d3..74d8117b 100644 --- a/src/async.c +++ b/src/async.c @@ -35,7 +35,7 @@ coap_async_state_t * coap_register_async(coap_context_t *context, coap_session_t *session, coap_pdu_t *request, unsigned char flags, void *data) { coap_async_state_t *s; - coap_tid_t id = ntohs( request->hdr->id ); + coap_tid_t id = request->tid; SEARCH_PAIR(context->async_state,s,session,session,id,id); @@ -48,26 +48,26 @@ coap_register_async(coap_context_t *context, coap_session_t *session, /* store information for handling the asynchronous task */ s = (coap_async_state_t *)coap_malloc(sizeof(coap_async_state_t) + - request->hdr->token_length); + request->token_length); if (!s) { coap_log(LOG_CRIT, "coap_register_async: insufficient memory\n"); return NULL; } - memset(s, 0, sizeof(coap_async_state_t) + request->hdr->token_length); + memset(s, 0, sizeof(coap_async_state_t) + request->token_length); /* set COAP_ASYNC_CONFIRM according to request's type */ s->flags = flags & ~COAP_ASYNC_CONFIRM; - if (request->hdr->type == COAP_MESSAGE_CON) + if (request->type == COAP_MESSAGE_CON) s->flags |= COAP_ASYNC_CONFIRM; s->appdata = data; s->session = coap_session_reference( session ); s->id = id; - if (request->hdr->token_length) { - s->tokenlen = request->hdr->token_length; - memcpy(s->token, request->hdr->token, request->hdr->token_length); + if (request->token_length) { + s->tokenlen = request->token_length; + memcpy(s->token, request->token, request->token_length); } coap_touch_async(s); diff --git a/src/block.c b/src/block.c index d8e25e75..932a3599 100644 --- a/src/block.c +++ b/src/block.c @@ -16,10 +16,6 @@ #include "debug.h" #include "block.h" -#if (COAP_DEFAULT_PDU_SIZE - 6) < (1 << (COAP_MAX_BLOCK_SZX + 4)) -#error "COAP_MAX_BLOCK_SZX too large" -#endif - #ifndef min #define min(a,b) ((a) < (b) ? (a) : (b)) #endif @@ -28,7 +24,7 @@ unsigned int coap_opt_block_num(const coap_opt_t *block_opt) { unsigned int num = 0; - unsigned short len; + uint16_t len; len = coap_opt_length(block_opt); @@ -37,15 +33,15 @@ coap_opt_block_num(const coap_opt_t *block_opt) { } if (len > 1) { - num = coap_decode_var_bytes(COAP_OPT_VALUE(block_opt), - COAP_OPT_LENGTH(block_opt) - 1); + num = coap_decode_var_bytes(coap_opt_const_value(block_opt), + coap_opt_length(block_opt) - 1); } return (num << 4) | ((*COAP_OPT_BLOCK_LAST(block_opt) & 0xF0) >> 4); } int -coap_get_block(coap_pdu_t *pdu, unsigned short type, coap_block_t *block) { +coap_get_block(coap_pdu_t *pdu, uint16_t type, coap_block_t *block) { coap_opt_iterator_t opt_iter; coap_opt_t *option; @@ -73,7 +69,7 @@ coap_get_block(coap_pdu_t *pdu, unsigned short type, coap_block_t *block) { } int -coap_write_block_opt(coap_block_t *block, unsigned short type, +coap_write_block_opt(coap_block_t *block, uint16_t type, coap_pdu_t *pdu, size_t data_length) { size_t start, want, avail; unsigned char buf[4]; @@ -86,7 +82,7 @@ coap_write_block_opt(coap_block_t *block, unsigned short type, return -2; } - avail = pdu->max_size - pdu->length - 4; + avail = pdu->max_size - pdu->used_size - 4; want = (size_t)1 << (block->szx + 4); /* check if entire block fits in message */ diff --git a/src/coap_io_lwip.c b/src/coap_io_lwip.c index 636a4145..36bbb370 100644 --- a/src/coap_io_lwip.c +++ b/src/coap_io_lwip.c @@ -80,7 +80,7 @@ coap_endpoint_t *coap_new_endpoint(const coap_address_t *addr, int flags) { goto error; } - result->default_mtu = COAP_DEFAULT_PDU_SIZE; + result->default_mtu = COAP_DEFAULT_MTU; return result; diff --git a/src/coap_openssl.c b/src/coap_openssl.c index 1aae0266..0bfd3bca 100644 --- a/src/coap_openssl.c +++ b/src/coap_openssl.c @@ -350,7 +350,7 @@ void *coap_dtls_new_context(struct coap_context_t *coap_context) { SSL_set_bio(context->ssl, bio, bio); SSL_set_app_data(context->ssl, NULL); SSL_set_options(context->ssl, SSL_OP_COOKIE_EXCHANGE); - SSL_set_mtu(context->ssl, COAP_DEFAULT_PDU_SIZE); + SSL_set_mtu(context->ssl, COAP_DEFAULT_MTU); } return context; diff --git a/src/coap_session.c b/src/coap_session.c index 37b5edf3..e20a7da8 100644 --- a/src/coap_session.c +++ b/src/coap_session.c @@ -72,7 +72,7 @@ coap_make_session(coap_proto_t proto, coap_session_type_t type, const coap_addre if (endpoint) session->mtu = endpoint->default_mtu; else - session->mtu = COAP_DEFAULT_PDU_SIZE; + session->mtu = COAP_DEFAULT_MTU; if (proto == COAP_PROTO_DTLS) { session->tls_overhead = 29; if (session->tls_overhead >= session->mtu) { @@ -112,7 +112,7 @@ void coap_session_free(coap_session_t *session) { coap_free(session->psk_key); LL_FOREACH_SAFE(session->sendqueue, q, tmp) { - if (q->pdu->hdr->type==COAP_MESSAGE_CON && session->context->nack_handler) + if (q->pdu->type==COAP_MESSAGE_CON && session->context->nack_handler) session->context->nack_handler(session->context, session, q->pdu, session->proto == COAP_PROTO_DTLS ? COAP_NACK_TLS_FAILED : COAP_NACK_NOT_DELIVERABLE, q->id); coap_delete_node(q); } @@ -168,9 +168,9 @@ coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu, node = coap_new_node(); if (node == NULL) return COAP_INVALID_TID; - node->id = ntohs(pdu->hdr->id); + node->id = pdu->tid; node->pdu = pdu; - if (pdu->hdr->type == COAP_MESSAGE_CON) { + if (pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto) ) { uint8_t r; prng(&r, sizeof(r)); /* add timeout in range [ACK_TIMEOUT...ACK_TIMEOUT * ACK_RANDOM_FACTOR] */ @@ -200,12 +200,9 @@ void coap_session_connected(coap_session_t *session) { coap_queue_t *q = session->sendqueue; session->sendqueue = q->next; q->next = NULL; - debug("** %s tid=%d: transmitted after delay\n", coap_session_str(session), (int)ntohs(q->pdu->hdr->id)); - if (session->proto == COAP_PROTO_DTLS) - bytes_written = coap_dtls_send(session, (const uint8_t*)q->pdu->hdr, q->pdu->length); - else - bytes_written = coap_session_send(session, (const uint8_t*)q->pdu->hdr, q->pdu->length); - if (q->pdu->hdr->type == COAP_MESSAGE_CON) { + debug("** %s tid=%d: transmitted after delay\n", coap_session_str(session), (int)q->pdu->tid); + bytes_written = coap_session_send_pdu(session, q->pdu); + if (q->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) { if (coap_wait_ack(session->context, session, q) >= 0) q = NULL; } @@ -229,7 +226,7 @@ void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reaso session->sendqueue = q->next; q->next = NULL; debug("** %s tid=%d: not transmitted after delay\n", coap_session_str(session), q->id); - if (q->pdu->hdr->type == COAP_MESSAGE_CON) { + if (q->pdu->type==COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) { if (coap_wait_ack(session->context, session, q) >= 0) q = NULL; } @@ -254,8 +251,11 @@ void coap_session_reset(coap_session_t *session) { session->sendqueue = q->next; q->next = NULL; debug("** %s tid=%d: not transmitted after delay\n", coap_session_str(session), (int)q->id); - if (q->pdu->hdr->type == COAP_MESSAGE_CON && session->context->nack_handler) + if ( q->pdu->type == COAP_MESSAGE_CON + && COAP_PROTO_NOT_RELIABLE(session->proto) + && session->context->nack_handler ) { session->context->nack_handler(session->context, session, q->pdu, COAP_NACK_NOT_DELIVERABLE, q->id); + } coap_delete_node(q); } } @@ -486,7 +486,7 @@ coap_new_endpoint(coap_context_t *context, const coap_address_t *listen_addr, co ep->hello.endpoint = ep; } - ep->default_mtu = COAP_DEFAULT_PDU_SIZE; + ep->default_mtu = COAP_DEFAULT_MTU; LL_PREPEND(context->endpoint, ep); return ep; diff --git a/src/coap_tinydtls.c b/src/coap_tinydtls.c index ab581cda..12d26807 100644 --- a/src/coap_tinydtls.c +++ b/src/coap_tinydtls.c @@ -124,7 +124,7 @@ static int dtls_event(struct dtls_context_t *dtls_context, session_t *dtls_session, dtls_alert_level_t level, - unsigned short code) { + uint16_t code) { (void)dtls_context; (void)dtls_session; diff --git a/src/debug.c b/src/debug.c index 06849287..572f1e3f 100644 --- a/src/debug.c +++ b/src/debug.c @@ -264,7 +264,7 @@ coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, size_t le /** Returns a textual description of the message type @p t. */ static const char * -msg_type_string(unsigned short t) { +msg_type_string(uint16_t t) { static char *types[] = { "CON", "NON", "ACK", "RST", "???" }; return types[min(t, sizeof(types)/sizeof(char *) - 1)]; @@ -272,13 +272,17 @@ msg_type_string(unsigned short t) { /** Returns a textual description of the method or response code. */ static const char * -msg_code_string(unsigned short c) { - static char *methods[] = { "0.00", "GET", "POST", "PUT", "DELETE", - "FETCH", "PATCH", "iPATCH" }; +msg_code_string(uint16_t c) { + static const char *methods[] = { "0.00", "GET", "POST", "PUT", "DELETE", + "FETCH", "PATCH", "iPATCH" }; + static const char *signals[] = { "7.00", "CSM", "Ping", "Pong", "Release", + "Abort" }; static char buf[5]; - if (c < sizeof(methods)/sizeof(char *)) { + if (c < sizeof(methods)/sizeof(const char *)) { return methods[c]; + } else if ( c>=224 && c-224 < sizeof(signals)/sizeof(const char *) ) { + return signals[c-224]; } else { snprintf(buf, sizeof(buf), "%u.%02u", c >> 5, c & 0x1f); return buf; @@ -386,7 +390,7 @@ is_binary(int content_format) { void coap_show_pdu(const coap_pdu_t *pdu) { - unsigned char buf[COAP_DEFAULT_PDU_SIZE]; /* need some space for output creation */ + unsigned char buf[1024]; /* need some space for output creation */ size_t buf_len = 0; /* takes the number of bytes written to buf */ int encode = 0, have_options = 0, i; coap_opt_iterator_t opt_iter; @@ -396,11 +400,11 @@ coap_show_pdu(const coap_pdu_t *pdu) { unsigned char *data; fprintf(COAP_DEBUG_FD, "v:%d t:%s c:%s i:%04x {", - pdu->hdr->version, msg_type_string(pdu->hdr->type), - msg_code_string(pdu->hdr->code), ntohs(pdu->hdr->id)); + COAP_DEFAULT_VERSION, msg_type_string(pdu->type), + msg_code_string(pdu->code), pdu->tid); - for (i = 0; i < pdu->hdr->token_length; i++) { - fprintf(COAP_DEBUG_FD, "%02x", pdu->hdr->token[i]); + for (i = 0; i < pdu->token_length; i++) { + fprintf(COAP_DEBUG_FD, "%02x", pdu->token[i]); } fprintf(COAP_DEBUG_FD, "}"); @@ -417,8 +421,8 @@ coap_show_pdu(const coap_pdu_t *pdu) { switch (opt_iter.type) { case COAP_OPTION_CONTENT_FORMAT: - content_format = (int)coap_decode_var_bytes(COAP_OPT_VALUE(option), - COAP_OPT_LENGTH(option)); + content_format = (int)coap_decode_var_bytes(coap_opt_value(option), + coap_opt_length(option)); buf_len = print_content_format(content_format, buf, sizeof(buf)); break; @@ -440,8 +444,8 @@ coap_show_pdu(const coap_pdu_t *pdu) { case COAP_OPTION_SIZE1: /* show values as unsigned decimal value */ buf_len = snprintf((char *)buf, sizeof(buf), "%u", - coap_decode_var_bytes(COAP_OPT_VALUE(option), - COAP_OPT_LENGTH(option))); + coap_decode_var_bytes(coap_opt_value(option), + coap_opt_length(option))); break; default: @@ -457,8 +461,8 @@ coap_show_pdu(const coap_pdu_t *pdu) { encode = 1; } - buf_len = print_readable(COAP_OPT_VALUE(option), - COAP_OPT_LENGTH(option), + buf_len = print_readable(coap_opt_value(option), + coap_opt_length(option), buf, sizeof(buf), encode); } diff --git a/src/encode.c b/src/encode.c index 78b3a142..3d5d960c 100644 --- a/src/encode.c +++ b/src/encode.c @@ -31,7 +31,7 @@ int coap_flsll(long long i) #endif unsigned int -coap_decode_var_bytes(unsigned char *buf,unsigned int len) { +coap_decode_var_bytes(const uint8_t *buf,unsigned int len) { unsigned int i, n = 0; for (i = 0; i < len; ++i) n = (n << 8) + buf[i]; @@ -40,7 +40,7 @@ coap_decode_var_bytes(unsigned char *buf,unsigned int len) { } unsigned int -coap_encode_var_bytes(unsigned char *buf, unsigned int val) { +coap_encode_var_bytes(uint8_t *buf, unsigned int val) { unsigned int n, i; for (n = 0, i = val; i && n < sizeof(val); ++n) diff --git a/src/net.c b/src/net.c index 3fccad12..2d997157 100644 --- a/src/net.c +++ b/src/net.c @@ -113,7 +113,7 @@ #endif /** creates a Qx.frac from fval */ -#define Q(frac,fval) ((unsigned short)(((1 << (frac)) * (fval)))) +#define Q(frac,fval) ((uint16_t)(((1 << (frac)) * (fval)))) /** creates a Qx.FRAC_BITS from COAP_DEFAULT_ACK_RANDOM_FACTOR */ #define ACK_RANDOM_FACTOR \ @@ -476,7 +476,7 @@ coap_new_context( } /* initialize message id */ - prng((unsigned char *)&c->message_id, sizeof(unsigned short)); + prng((unsigned char *)&c->message_id, sizeof(uint16_t)); if (listen_addr) { coap_endpoint_t *endpoint = coap_new_endpoint(c, listen_addr, COAP_PROTO_UDP); @@ -620,15 +620,52 @@ coap_send_ack(coap_session_t *session, coap_pdu_t *request) { coap_pdu_t *response; coap_tid_t result = COAP_INVALID_TID; - if (request && request->hdr->type == COAP_MESSAGE_CON) { - response = coap_pdu_init(COAP_MESSAGE_ACK, 0, request->hdr->id, - sizeof(coap_pdu_t)); + if (request && request->type == COAP_MESSAGE_CON && + COAP_PROTO_NOT_RELIABLE(session->proto)) { + response = coap_pdu_init(COAP_MESSAGE_ACK, 0, request->tid, 0); if (response) result = coap_send(session, response); } return result; } +int coap_tls_send(coap_session_t *session, + const uint8_t *data, + size_t data_len, + int flush ) +{ + return -1; +} + +ssize_t +coap_session_send_pdu(coap_session_t *session, coap_pdu_t *pdu) { + ssize_t bytes_written = -1; + size_t header_size = coap_pdu_encode_header(pdu, session->proto); + if (!header_size) + return -1; + switch(session->proto) { + case COAP_PROTO_UDP: + bytes_written = coap_session_send(session, pdu->token - header_size, + pdu->used_size + header_size); + break; + case COAP_PROTO_DTLS: + bytes_written = coap_dtls_send(session, pdu->token - header_size, + pdu->used_size + header_size); + break; + case COAP_PROTO_TCP: + bytes_written = coap_session_send(session, pdu->token - header_size, + pdu->used_size + header_size); + break; + case COAP_PROTO_TLS: + bytes_written = coap_tls_send(session, pdu->token - header_size, + pdu->used_size + header_size, 1); + break; + default: + break; + } + return bytes_written; +} + static ssize_t coap_send_pdu(coap_session_t *session, coap_pdu_t *pdu, coap_queue_t *node) { ssize_t bytes_written; @@ -650,7 +687,7 @@ coap_send_pdu(coap_session_t *session, coap_pdu_t *pdu, coap_queue_t *node) { * FIXME: If No-Response option indicates interest, these responses * must not be dropped. */ if (coap_is_mcast(&session->local_addr) && - COAP_RESPONSE_CLASS(pdu->hdr->code) > 2) { + COAP_RESPONSE_CLASS(pdu->code) > 2) { return COAP_DROPPED_RESPONSE; } @@ -668,12 +705,7 @@ coap_send_pdu(coap_session_t *session, coap_pdu_t *pdu, coap_queue_t *node) { if (session->state != COAP_SESSION_STATE_ESTABLISHED) return coap_session_delay_pdu(session, pdu, node); - /* Pass to DTLS module for sending if local_interface is for secure - * communication. */ - if (session->proto == COAP_PROTO_DTLS) - bytes_written = coap_dtls_send(session, (const uint8_t*)pdu->hdr, pdu->length); - else - bytes_written = coap_session_send(session, (const uint8_t*)pdu->hdr, pdu->length); + bytes_written = coap_session_send_pdu(session, pdu); #endif /* WITH_LWIP */ @@ -704,7 +736,7 @@ coap_send_message_type(coap_session_t *session, coap_pdu_t *request, unsigned ch coap_tid_t result = COAP_INVALID_TID; if (request) { - response = coap_pdu_init(type, 0, request->hdr->id, sizeof(coap_pdu_t)); + response = coap_pdu_init(type, 0, request->tid, 0); if (response) result = coap_send(session, response); } @@ -806,15 +838,15 @@ coap_send(coap_session_t *session, coap_pdu_t *pdu) { ssize_t bytes_written = coap_send_pdu(session, pdu, NULL); if (bytes_written == COAP_PDU_DELAYED) - return ntohs(pdu->hdr->id); + return pdu->tid; if (bytes_written < 0) { coap_delete_pdu(pdu); return (coap_tid_t)bytes_written; } - if (pdu->hdr->type != COAP_MESSAGE_CON) { - coap_tid_t id = ntohs(pdu->hdr->id); + if (pdu->type != COAP_MESSAGE_CON || COAP_PROTO_RELIABLE(session->proto)) { + coap_tid_t id = pdu->tid; coap_delete_pdu(pdu); return id; } @@ -825,7 +857,7 @@ coap_send(coap_session_t *session, coap_pdu_t *pdu) { return COAP_INVALID_TID; } - node->id = ntohs(pdu->hdr->id); + node->id = pdu->tid; node->pdu = pdu; prng(&r, sizeof(r)); /* add timeout in range [ACK_TIMEOUT...ACK_TIMEOUT * ACK_RANDOM_FACTOR] */ @@ -885,19 +917,22 @@ coap_retransmit(coap_context_t *context, coap_queue_t *node) { #ifndef WITHOUT_OBSERVE /* Check if subscriptions exist that should be canceled after COAP_MAX_NOTIFY_FAILURES */ - if (node->pdu->hdr->code >= 64) { + if (node->pdu->code >= 64) { str token = { 0, NULL }; - token.length = node->pdu->hdr->token_length; - token.s = node->pdu->hdr->token; + token.length = node->pdu->token_length; + token.s = node->pdu->token; coap_handle_failed_notify(context, node->session, &token); } #endif /* WITHOUT_OBSERVE */ /* And finally delete the node */ - if (node->pdu->hdr->type == COAP_MESSAGE_CON && context->nack_handler) + if ( node->pdu->type == COAP_MESSAGE_CON + && COAP_PROTO_NOT_RELIABLE(node->session->proto) + && context->nack_handler) { context->nack_handler(context, node->session, node->pdu, COAP_NACK_TOO_MANY_RETRIES, node->id); + } coap_delete_node(node); return COAP_INVALID_TID; } @@ -1041,14 +1076,8 @@ coap_handle_message(coap_context_t *ctx, coap_session_t *session, enum result_t { RESULT_OK, RESULT_ERR_EARLY, RESULT_ERR }; int result = RESULT_ERR_EARLY; - if (msg_len < sizeof(coap_hdr_t)) { - debug("coap_handle_message: discarded invalid frame\n"); - goto error_early; - } - - /* check version identifier */ - if (((*msg >> 6) & 0x03) != COAP_DEFAULT_VERSION) { - debug("coap_handle_message: unknown protocol version %d\n", (*msg >> 6) & 0x03); + if ( COAP_PROTO_RELIABLE(session->proto) ) { + warn( "tcp not implemented\n" ); goto error_early; } @@ -1063,13 +1092,13 @@ coap_handle_message(coap_context_t *ctx, coap_session_t *session, #ifdef WITH_LWIP node->pdu = coap_pdu_from_pbuf(coap_packet_extract_pbuf(packet)); #else - node->pdu = coap_pdu_init(0, 0, 0, msg_len); + node->pdu = coap_pdu_init(0, 0, 0, msg_len - 4); #endif if (!node->pdu) { goto error; } - if (!coap_pdu_parse(msg, msg_len, node->pdu)) { + if (!coap_pdu_parse(session->proto, msg, msg_len, node->pdu)) { warn("discard malformed PDU\n"); goto error; } @@ -1078,7 +1107,7 @@ coap_handle_message(coap_context_t *ctx, coap_session_t *session, /* and add new node to receive queue */ node->session = coap_session_reference(session); - node->id = ntohs(node->pdu->hdr->id); + node->id = node->pdu->tid; #ifndef NDEBUG if (LOG_DEBUG <= coap_get_log_level()) { @@ -1166,8 +1195,12 @@ coap_cancel_session_messages(coap_context_t *context, coap_session_t *session, q = context->sendqueue; context->sendqueue = q->next; debug("** %s tid=%d: removed\n", coap_session_str(session), q->id); - if (q->pdu->hdr->type == COAP_MESSAGE_CON && context->nack_handler) + if ( q->pdu->type == COAP_MESSAGE_CON + && context->nack_handler + && COAP_PROTO_NOT_RELIABLE(session->proto) + ) { context->nack_handler(context, session, q->pdu, reason, q->id); + } coap_delete_node(q); } @@ -1181,8 +1214,12 @@ coap_cancel_session_messages(coap_context_t *context, coap_session_t *session, if (q->session == session) { p->next = q->next; debug("** %s tid=%d: removed\n", coap_session_str(session), q->id); - if (q->pdu->hdr->type == COAP_MESSAGE_CON && context->nack_handler) + if ( q->pdu->type == COAP_MESSAGE_CON + && context->nack_handler + && COAP_PROTO_NOT_RELIABLE(session->proto) + ) { context->nack_handler(context, session, q->pdu, reason, q->id); + } coap_delete_node(q); q = p->next; } else { @@ -1201,8 +1238,8 @@ coap_cancel_all_messages(coap_context_t *context, coap_session_t *session, while (context->sendqueue && context->sendqueue->session == session && token_match(token, token_length, - context->sendqueue->pdu->hdr->token, - context->sendqueue->pdu->hdr->token_length)) { + context->sendqueue->pdu->token, + context->sendqueue->pdu->token_length)) { q = context->sendqueue; context->sendqueue = q->next; debug("** %s tid=%d: removed\n", coap_session_str(session), q->id); @@ -1219,7 +1256,7 @@ coap_cancel_all_messages(coap_context_t *context, coap_session_t *session, while (q) { if (q->session == session && token_match(token, token_length, - q->pdu->hdr->token, q->pdu->hdr->token_length)) { + q->pdu->token, q->pdu->token_length)) { p->next = q->next; debug("** %s tid=%d: removed\n", coap_session_str(session), q->id); coap_delete_node(q); @@ -1244,10 +1281,10 @@ coap_new_error_response(coap_pdu_t *request, unsigned char code, coap_opt_filter_t opts) { coap_opt_iterator_t opt_iter; coap_pdu_t *response; - size_t size = sizeof(coap_hdr_t) + request->hdr->token_length; + size_t size = request->token_length; unsigned char type; coap_opt_t *option; - unsigned short opt_type = 0; /* used for calculating delta-storage */ + uint16_t opt_type = 0; /* used for calculating delta-storage */ #if COAP_ERROR_PHRASE_LENGTH > 0 char *phrase = coap_response_phrase(code); @@ -1260,7 +1297,7 @@ coap_new_error_response(coap_pdu_t *request, unsigned char code, assert(request); /* cannot send ACK if original request was not confirmable */ - type = request->hdr->type == COAP_MESSAGE_CON + type = request->type == COAP_MESSAGE_CON ? COAP_MESSAGE_ACK : COAP_MESSAGE_NON; @@ -1276,7 +1313,7 @@ coap_new_error_response(coap_pdu_t *request, unsigned char code, value might grow. */ while ((option = coap_option_next(&opt_iter))) { - unsigned short delta = opt_iter.type - opt_type; + uint16_t delta = opt_iter.type - opt_type; /* calculate space required to encode (opt_iter.type - opt_type) */ if (delta < 13) { size++; @@ -1305,11 +1342,11 @@ coap_new_error_response(coap_pdu_t *request, unsigned char code, } /* Now create the response and fill with options and payload data. */ - response = coap_pdu_init(type, code, request->hdr->id, size); + response = coap_pdu_init(type, code, request->tid, size); if (response) { /* copy token */ - if (!coap_add_token(response, request->hdr->token_length, - request->hdr->token)) { + if (!coap_add_token(response, request->token_length, + request->token)) { debug("cannot add token to error response\n"); coap_delete_pdu(response); return NULL; @@ -1319,8 +1356,8 @@ coap_new_error_response(coap_pdu_t *request, unsigned char code, coap_option_iterator_init(request, &opt_iter, opts); while ((option = coap_option_next(&opt_iter))) { coap_add_option(response, opt_iter.type, - COAP_OPT_LENGTH(option), - COAP_OPT_VALUE(option)); + coap_opt_length(option), + coap_opt_value(option)); } #if COAP_ERROR_PHRASE_LENGTH > 0 @@ -1368,17 +1405,17 @@ coap_wellknown_response(coap_context_t *context, coap_session_t *session, coap_opt_t *query_filter; size_t offset = 0; - resp = coap_pdu_init(request->hdr->type == COAP_MESSAGE_CON + resp = coap_pdu_init(request->type == COAP_MESSAGE_CON ? COAP_MESSAGE_ACK : COAP_MESSAGE_NON, COAP_RESPONSE_CODE(205), - request->hdr->id, coap_session_max_pdu_size(session)); + request->tid, coap_session_max_pdu_size(session)); if (!resp) { debug("coap_wellknown_response: cannot create PDU\n"); return NULL; } - if (!coap_add_token(resp, request->hdr->token_length, request->hdr->token)) { + if (!coap_add_token(resp, request->token_length, request->token)) { debug("coap_wellknown_response: cannot add token\n"); goto error; } @@ -1390,8 +1427,8 @@ coap_wellknown_response(coap_context_t *context, coap_session_t *session, if (wkc_len == 0) { debug("coap_wellknown_response: undefined resource\n"); /* set error code 4.00 Bad Request*/ - resp->hdr->code = COAP_RESPONSE_CODE(400); - resp->length = sizeof(coap_hdr_t) + resp->hdr->token_length; + resp->code = COAP_RESPONSE_CODE(400); + resp->used_size = resp->token_length; return resp; } @@ -1400,7 +1437,7 @@ coap_wellknown_response(coap_context_t *context, coap_session_t *session, debug("create block\n"); offset = block.num << (block.szx + 4); if (block.szx > 6) { /* invalid, MUST lead to 4.00 Bad Request */ - resp->hdr->code = COAP_RESPONSE_CODE(400); + resp->code = COAP_RESPONSE_CODE(400); return resp; } else if (block.szx > COAP_MAX_BLOCK_SZX) { block.szx = COAP_MAX_BLOCK_SZX; @@ -1414,7 +1451,7 @@ coap_wellknown_response(coap_context_t *context, coap_session_t *session, * and data. We do this before adding the Content-Format option to * avoid sending error responses with that option but no actual * content. */ - if (resp->max_size <= (size_t)resp->length + 3) { + if (resp->max_size <= resp->used_size + 3) { debug("coap_wellknown_response: insufficient storage space\n"); goto error; } @@ -1428,9 +1465,9 @@ coap_wellknown_response(coap_context_t *context, coap_session_t *session, COAP_MEDIATYPE_APPLICATION_LINK_FORMAT), buf); /* check if Block2 option is required even if not requested */ - if (!need_block2 && (resp->max_size - (size_t)resp->length < wkc_len)) { - assert(resp->length <= resp->max_size); - const size_t payloadlen = resp->max_size - resp->length; + if (!need_block2 && ((size_t)resp->max_size - resp->used_size < wkc_len)) { + assert(resp->used_size <= resp->max_size); + const size_t payloadlen = resp->max_size - resp->used_size; /* yes, need block-wise transfer */ block.num = 0; block.m = 0; /* the M bit is set by coap_write_block_opt() */ @@ -1458,11 +1495,9 @@ coap_wellknown_response(coap_context_t *context, coap_session_t *session, /* Manually set payload of response to let print_wellknown() write, * into our buffer without copying data. */ - resp->data = (unsigned char *)resp->hdr + resp->length; - *resp->data = COAP_PAYLOAD_START; - resp->data++; - resp->length++; - len = need_block2 ? SZX_TO_BYTES(block.szx) : resp->max_size - resp->length; + resp->token[resp->used_size++] = COAP_PAYLOAD_START; + resp->data = resp->token + resp->used_size; + len = need_block2 ? SZX_TO_BYTES(block.szx) : resp->max_size - resp->used_size; result = coap_print_wellknown(context, resp->data, &len, offset, query_filter); if ((result & COAP_PRINT_STATUS_ERROR) != 0) { @@ -1470,19 +1505,13 @@ coap_wellknown_response(coap_context_t *context, coap_session_t *session, goto error; } - unsigned int new_resp_length = resp->length + COAP_PRINT_OUTPUT_LENGTH(result); - if (new_resp_length > USHRT_MAX) { - debug("coap_print_wellknown failed - print result too large\n"); - goto error; - } - - resp->length = (unsigned short)new_resp_length; + resp->used_size = resp->used_size + COAP_PRINT_OUTPUT_LENGTH(result); return resp; error: /* set error code 5.03 and remove all options and data from response */ - resp->hdr->code = COAP_RESPONSE_CODE(503); - resp->length = sizeof(coap_hdr_t) + resp->hdr->token_length; + resp->code = COAP_RESPONSE_CODE(503); + resp->used_size = resp->token_length; return resp; } @@ -1506,7 +1535,7 @@ coap_cancel(coap_context_t *context, const coap_queue_t *sent) { * get token from sent and try to find a matching resource. Uh! */ - COAP_SET_STR(&token, sent->pdu->hdr->token_length, sent->pdu->hdr->token); + COAP_SET_STR(&token, sent->pdu->token_length, sent->pdu->token); RESOURCES_ITER(context->resources, r) { num_cancelled += coap_delete_observer(r, sent->session, &token); @@ -1562,7 +1591,7 @@ no_response(coap_pdu_t *request, coap_pdu_t *response) { assert(request); assert(response); - if (COAP_RESPONSE_CLASS(response->hdr->code) > 0) { + if (COAP_RESPONSE_CLASS(response->code) > 0) { nores = coap_check_option(request, COAP_OPTION_NORESPONSE, &opt_iter); if (nores) { @@ -1573,7 +1602,7 @@ no_response(coap_pdu_t *request, coap_pdu_t *response) { * documentation). When a No-Response option is present and the * bit is not set, the sender explicitly indicates interest in * this response. */ - if (((1 << (COAP_RESPONSE_CLASS(response->hdr->code) - 1)) & val) > 0) { + if (((1 << (COAP_RESPONSE_CLASS(response->code) - 1)) & val) > 0) { return RESPONSE_DROP; } else { return RESPONSE_SEND; @@ -1588,7 +1617,7 @@ no_response(coap_pdu_t *request, coap_pdu_t *response) { } #define WANT_WKC(Pdu,Key) \ - (((Pdu)->hdr->code == COAP_REQUEST_GET) && is_wkc(Key)) + (((Pdu)->code == COAP_REQUEST_GET) && is_wkc(Key)) static void handle_request(coap_context_t *context, coap_queue_t *node) { @@ -1615,7 +1644,7 @@ handle_request(coap_context_t *context, coap_queue_t *node) { * response, otherwise, we return 4.04 */ if (is_wkc(key)) { /* request for .well-known/core */ - if (node->pdu->hdr->code == COAP_REQUEST_GET) { /* GET */ + if (node->pdu->code == COAP_REQUEST_GET) { /* GET */ info("create default response for %s\n", COAP_DEFAULT_URI_WELLKNOWN); response = coap_wellknown_response(context, node->session, node->pdu); } else { @@ -1645,25 +1674,25 @@ handle_request(coap_context_t *context, coap_queue_t *node) { } /* the resource was found, check if there is a registered handler */ - if ((size_t)node->pdu->hdr->code - 1 < + if ((size_t)node->pdu->code - 1 < sizeof(resource->handler) / sizeof(coap_method_handler_t)) - h = resource->handler[node->pdu->hdr->code - 1]; + h = resource->handler[node->pdu->code - 1]; if (h) { str *query = coap_get_query(node->pdu); int owns_query = 1; debug("call custom handler for resource 0x%02x%02x%02x%02x\n", key[0], key[1], key[2], key[3]); - response = coap_pdu_init(node->pdu->hdr->type == COAP_MESSAGE_CON + response = coap_pdu_init(node->pdu->type == COAP_MESSAGE_CON ? COAP_MESSAGE_ACK : COAP_MESSAGE_NON, - 0, node->pdu->hdr->id, coap_session_max_pdu_size(node->session)); + 0, node->pdu->tid, coap_session_max_pdu_size(node->session)); /* Implementation detail: coap_add_token() immediately returns 0 if response == NULL */ - if (coap_add_token(response, node->pdu->hdr->token_length, - node->pdu->hdr->token)) { - str token = { node->pdu->hdr->token_length, node->pdu->hdr->token }; + if (coap_add_token(response, node->pdu->token_length, + node->pdu->token)) { + str token = { node->pdu->token_length, node->pdu->token }; coap_opt_iterator_t opt_iter; coap_opt_t *observe = NULL; int observe_action = COAP_OBSERVE_CANCEL; @@ -1699,7 +1728,7 @@ handle_request(coap_context_t *context, coap_queue_t *node) { respond = no_response(node->pdu, response); if (respond != RESPONSE_DROP) { - if (observe && (COAP_RESPONSE_CLASS(response->hdr->code) > 2)) { + if (observe && (COAP_RESPONSE_CLASS(response->code) > 2)) { coap_log(LOG_DEBUG, "removed observer\n"); coap_delete_observer(resource, node->session, &token); } @@ -1708,21 +1737,21 @@ handle_request(coap_context_t *context, coap_queue_t *node) { * application handler made no changes to the response, then * this is an empty ACK with a token, which is a malformed * PDU */ - if ((response->hdr->type == COAP_MESSAGE_ACK) - && (response->hdr->code == 0)) { + if ((response->type == COAP_MESSAGE_ACK) + && (response->code == 0)) { /* Remove token from otherwise-empty acknowledgment PDU */ - response->hdr->token_length = 0; - response->length = sizeof(coap_hdr_t); + response->token_length = 0; + response->used_size = 0; } if ((respond == RESPONSE_SEND) || /* RESPOND_DEFAULT */ - (response->hdr->type != COAP_MESSAGE_NON || - (response->hdr->code >= 64 + (response->type != COAP_MESSAGE_NON || + (response->code >= 64 && !coap_mcast_interface(&node->local_if)))) { if (coap_send(node->session, response) == COAP_INVALID_TID) - debug("cannot send response for message %d\n", node->pdu->hdr->id); + debug("cannot send response for message %d\n", node->pdu->tid); } else { coap_delete_pdu(response); } @@ -1765,8 +1794,8 @@ handle_response(coap_context_t *context, * same token. */ coap_cancel_all_messages(context, rcvd->session, - rcvd->pdu->hdr->token, - rcvd->pdu->hdr->token_length); + rcvd->pdu->token, + rcvd->pdu->token_length); /* Call application-specific response handler when available. */ if (context->response_handler) { @@ -1793,20 +1822,20 @@ coap_dispatch(coap_context_t *context, coap_queue_t *rcvd) { /* goto cleanup; */ /* } */ - switch (rcvd->pdu->hdr->type) { + switch (rcvd->pdu->type) { case COAP_MESSAGE_ACK: /* find transaction in sendqueue to stop retransmission */ coap_remove_from_queue(&context->sendqueue, rcvd->session, rcvd->id, &sent); - if (rcvd->pdu->hdr->code == 0) + if (rcvd->pdu->code == 0) goto cleanup; /* if sent code was >= 64 the message might have been a * notification. Then, we must flag the observer to be alive * by setting obs->fail_cnt = 0. */ - if (sent && COAP_RESPONSE_CLASS(sent->pdu->hdr->code) == 2) { + if (sent && COAP_RESPONSE_CLASS(sent->pdu->code) == 2) { const str token = - { sent->pdu->hdr->token_length, sent->pdu->hdr->token }; + { sent->pdu->token_length, sent->pdu->token }; coap_touch_observer(context, sent->session, &token); } break; @@ -1817,9 +1846,9 @@ coap_dispatch(coap_context_t *context, coap_queue_t *rcvd) { * have. */ #ifndef WITH_CONTIKI - coap_log(LOG_ALERT, "got RST for message %u\n", ntohs(rcvd->pdu->hdr->id)); + coap_log(LOG_ALERT, "got RST for message %d\n", rcvd->pdu->tid); #else /* WITH_CONTIKI */ - coap_log(LOG_ALERT, "got RST for message %u\n", uip_ntohs(rcvd->pdu->hdr->id)); + coap_log(LOG_ALERT, "got RST for message %d\n", rcvd->pdu->tid)); #endif /* WITH_CONTIKI */ /* find transaction in sendqueue to stop retransmission */ @@ -1828,7 +1857,7 @@ coap_dispatch(coap_context_t *context, coap_queue_t *rcvd) { if (sent) coap_cancel(context, sent); - if(sent->pdu->hdr->type==COAP_MESSAGE_CON && context->nack_handler) + if(sent->pdu->type==COAP_MESSAGE_CON && context->nack_handler) context->nack_handler(context, sent->session, sent->pdu, COAP_NACK_RST, sent->id); goto cleanup; @@ -1860,14 +1889,14 @@ coap_dispatch(coap_context_t *context, coap_queue_t *rcvd) { /* Pass message to upper layer if a specific handler was * registered for a request that should be handled locally. */ - if (COAP_MESSAGE_IS_REQUEST(rcvd->pdu->hdr)) + if (COAP_PDU_IS_REQUEST(rcvd->pdu)) handle_request(context, rcvd); - else if (COAP_MESSAGE_IS_RESPONSE(rcvd->pdu->hdr)) + else if (COAP_PDU_IS_RESPONSE(rcvd->pdu)) handle_response(context, sent, rcvd); else { debug("dropped message with invalid code (%d.%02d)\n", - COAP_RESPONSE_CLASS(rcvd->pdu->hdr->code), - rcvd->pdu->hdr->code & 0x1f); + COAP_RESPONSE_CLASS(rcvd->pdu->code), + rcvd->pdu->code & 0x1f); if (!coap_is_mcast(&rcvd->session->local_addr)) { coap_send_message_type(rcvd->session, diff --git a/src/option.c b/src/option.c index 90f906b2..cd3c65e0 100644 --- a/src/option.c +++ b/src/option.c @@ -24,14 +24,9 @@ coap_opt_t * options_start(coap_pdu_t *pdu) { - - if (pdu && pdu->hdr && - (pdu->hdr->token + pdu->hdr->token_length - < (unsigned char *)pdu->hdr + pdu->length)) { - - coap_opt_t *opt = pdu->hdr->token + pdu->hdr->token_length; + if (pdu && pdu->token_length < pdu->used_size) { + coap_opt_t *opt = pdu->token + pdu->token_length; return (*opt == COAP_PAYLOAD_START) ? NULL : opt; - } else return NULL; } @@ -121,21 +116,18 @@ coap_opt_iterator_t * coap_option_iterator_init(coap_pdu_t *pdu, coap_opt_iterator_t *oi, const coap_opt_filter_t filter) { assert(pdu); - assert(pdu->hdr); + assert(pdu->token); assert(oi); memset(oi, 0, sizeof(coap_opt_iterator_t)); - oi->next_option = (unsigned char *)pdu->hdr + sizeof(coap_hdr_t) - + pdu->hdr->token_length; - if ((unsigned char *)pdu->hdr + pdu->length <= oi->next_option) { + oi->next_option = pdu->token + pdu->token_length; + if (pdu->token + pdu->used_size <= oi->next_option) { oi->bad = 1; return NULL; } - assert((sizeof(coap_hdr_t) + pdu->hdr->token_length) <= pdu->length); - - oi->length = pdu->length - (sizeof(coap_hdr_t) + pdu->hdr->token_length); + oi->length = pdu->used_size - pdu->token_length; if (filter) { memcpy(oi->filter, filter, sizeof(coap_opt_filter_t)); @@ -207,7 +199,7 @@ coap_option_next(coap_opt_iterator_t *oi) { } coap_opt_t * -coap_check_option(coap_pdu_t *pdu, unsigned short type, +coap_check_option(coap_pdu_t *pdu, uint16_t type, coap_opt_iterator_t *oi) { coap_opt_filter_t f; @@ -219,9 +211,9 @@ coap_check_option(coap_pdu_t *pdu, unsigned short type, return coap_option_next(oi); } -unsigned short +uint16_t coap_opt_delta(const coap_opt_t *opt) { - unsigned short n; + uint16_t n; n = (*opt++ & 0xf0) >> 4; @@ -248,9 +240,9 @@ coap_opt_delta(const coap_opt_t *opt) { return n; } -unsigned short +uint16_t coap_opt_length(const coap_opt_t *opt) { - unsigned short length; + uint16_t length; length = *opt & 0x0f; switch (*opt & 0xf0) { @@ -330,7 +322,7 @@ coap_opt_size(const coap_opt_t *opt) { size_t coap_opt_setheader(coap_opt_t *opt, size_t maxlen, - unsigned short delta, size_t length) { + uint16_t delta, size_t length) { size_t skip = 0; assert(opt); @@ -384,8 +376,8 @@ coap_opt_setheader(coap_opt_t *opt, size_t maxlen, } size_t -coap_opt_encode(coap_opt_t *opt, size_t maxlen, unsigned short delta, - const unsigned char *val, size_t length) { +coap_opt_encode(coap_opt_t *opt, size_t maxlen, uint16_t delta, + const uint8_t *val, size_t length) { size_t l = 1; l = coap_opt_setheader(opt, maxlen, delta, length); @@ -424,7 +416,7 @@ typedef struct { /** Returns true iff @p type denotes an option type larger than 255. */ COAP_STATIC_INLINE int -is_long_option(unsigned short type) { return type > 255; } +is_long_option(uint16_t type) { return type > 255; } /** Operation specifiers for coap_filter_op(). */ enum filter_op_t { FILTER_SET, FILTER_CLEAR, FILTER_GET }; @@ -450,7 +442,7 @@ enum filter_op_t { FILTER_SET, FILTER_CLEAR, FILTER_GET }; */ static int coap_option_filter_op(coap_opt_filter_t filter, - unsigned short type, + uint16_t type, enum filter_op_t op) { size_t index = 0; opt_filter *of = (opt_filter *)filter; @@ -509,17 +501,17 @@ coap_option_filter_op(coap_opt_filter_t filter, } int -coap_option_filter_set(coap_opt_filter_t filter, unsigned short type) { +coap_option_filter_set(coap_opt_filter_t filter, uint16_t type) { return coap_option_filter_op(filter, type, FILTER_SET); } int -coap_option_filter_unset(coap_opt_filter_t filter, unsigned short type) { +coap_option_filter_unset(coap_opt_filter_t filter, uint16_t type) { return coap_option_filter_op(filter, type, FILTER_CLEAR); } int -coap_option_filter_get(const coap_opt_filter_t filter, unsigned short type) { +coap_option_filter_get(const coap_opt_filter_t filter, uint16_t type) { /* Ugly cast to make the const go away (FILTER_GET wont change filter * but as _set and _unset do, the function does not take a const). */ return coap_option_filter_op((uint16_t *)filter, type, FILTER_GET); diff --git a/src/pdu.c b/src/pdu.c index fa37c784..6eb05340 100644 --- a/src/pdu.c +++ b/src/pdu.c @@ -37,109 +37,95 @@ void coap_pdu_clear(coap_pdu_t *pdu, size_t size) { assert(pdu); - -#ifdef WITH_LWIP - /* the pdu itself is not wiped as opposed to the other implementations, - * because we have to rely on the pbuf to be set there. */ - pdu->hdr = pdu->pbuf->payload; -#else + assert(pdu->token); + assert(pdu->max_hdr_size >= COAP_PDU_MAX_UDP_HEADER_SIZE); + pdu->type = 0; + pdu->code = 0; + pdu->token_length = 0; + pdu->tid = 0; pdu->max_delta = 0; - pdu->data = NULL; -#endif - memset(pdu->hdr, 0, size); pdu->max_size = size; - pdu->hdr->version = COAP_DEFAULT_VERSION; - - /* data is NULL unless explicitly set by coap_add_data() */ - pdu->length = sizeof(coap_hdr_t); + pdu->used_size = 0; + pdu->data = NULL; } #ifdef WITH_LWIP coap_pdu_t * -coap_pdu_from_pbuf(struct pbuf *pbuf) +coap_pdu_from_pbuf( struct pbuf *pbuf ) { + coap_pdu_t *pdu; + if (pbuf == NULL) return NULL; LWIP_ASSERT("Can only deal with contiguous PBUFs", pbuf->tot_len == pbuf->len); LWIP_ASSERT("coap_read needs to receive an exclusive copy of the incoming pbuf", pbuf->ref == 1); - coap_pdu_t *result = coap_malloc_type(COAP_PDU, sizeof(coap_pdu_t)); - if (!result) { - pbuf_free(pbuf); - return NULL; + pdu = coap_malloc_type(COAP_PDU, sizeof(coap_pdu_t) ); + if (!pdu) { + pbuf_free(pbuf); + return NULL; } - memset(result, 0, sizeof(coap_pdu_t)); + pdu->max_hdr_size = COAP_PDU_MAX_UDP_HEADER_SIZE; + pdu->pbuf = pbuf; + pdu->token = pbuf->payload + pdu->max_hdr_size; + coap_pdu_clear(pdu, pbuf->tot_len - pdu->max_hdr_size); - result->max_size = pbuf->tot_len; - result->length = pbuf->tot_len; - result->hdr = pbuf->payload; - result->pbuf = pbuf; - - return result; + return pdu; } #endif coap_pdu_t * -coap_pdu_init(unsigned char type, unsigned char code, - unsigned short id, size_t size) { +coap_pdu_init(uint8_t type, uint8_t code, uint16_t tid, size_t size) { coap_pdu_t *pdu; -#ifdef WITH_LWIP - struct pbuf *p; -#endif + uint8_t *buf; #ifdef WITH_CONTIKI - assert(size <= COAP_RXBUFFER_SIZE); - /* Size must be large enough to fit the header. */ - if (size < sizeof(coap_hdr_t) || size > COAP_RXBUFFER_SIZE) + if (size == 0) + size = COAP_RXBUFFER_SIZE - COAP_PDU_MAX_UDP_HEADER_SIZE; + assert(size <= COAP_RXBUFFER_SIZE - COAP_PDU_MAX_UDP_HEADER_SIZE); + if (size > COAP_RXBUFFER_SIZE - COAP_PDU_MAX_UDP_HEADER_SIZE) return NULL; #else - assert(size <= 16384); - /* Size must be large enough to fit the header. */ - if (size < sizeof(coap_hdr_t) || size > 16384) - return NULL; + if (size == 0) + size = 256; #endif - /* size must be large enough for hdr */ -#if !defined(WITH_LWIP) pdu = coap_malloc_type(COAP_PDU, sizeof(coap_pdu_t)); if (!pdu) return NULL; - pdu->hdr = coap_malloc_type(COAP_PDU_BUF, size); - if (pdu->hdr == NULL) { - coap_free_type(COAP_PDU, pdu); - pdu = NULL; - } -#else /* WITH_LWIP */ - pdu = (coap_pdu_t*)coap_malloc_type(COAP_PDU, sizeof(coap_pdu_t)); - if (!pdu) return NULL; - p = pbuf_alloc(PBUF_TRANSPORT, size, PBUF_RAM); - if (p == NULL) { - coap_free_type(COAP_PDU, pdu); - pdu = NULL; - } -#endif /* WITH_LWIP */ - if (pdu) { -#ifdef WITH_LWIP - pdu->pbuf = p; + +#if defined(WITH_CONTIKI) || defined(WITH_LWIP) + pdu->max_hdr_size = COAP_PDU_MAX_UDP_HEADER_SIZE; +#else + pdu->max_hdr_size = COAP_PDU_MAX_TCP_HEADER_SIZE; #endif - coap_pdu_clear(pdu, size); - pdu->hdr->id = id; - pdu->hdr->type = type; - pdu->hdr->code = code; - } + +#ifdef WITH_LWIP + pdu->pbuf = pbuf_alloc(PBUF_TRANSPORT, size + pdu->max_hdr_size, PBUF_RAM); + if (pdu->pbuf == NULL) { + coap_free_type(COAP_PDU, pdu); + pdu = NULL; + } + pdu->token = pdu->pbuf->payload + pdu->max_hdr_size; + size = pbuf->tot_len - pdu->max_hdr_size; +#else /* WITH_LWIP */ + buf = coap_malloc_type(COAP_PDU_BUF, size + pdu->max_hdr_size); + if (buf == NULL) { + coap_free_type(COAP_PDU, pdu); + pdu = NULL; + } + pdu->token = buf + pdu->max_hdr_size; +#endif /* WITH_LWIP */ + coap_pdu_clear(pdu, size); + pdu->tid = tid; + pdu->type = type; + pdu->code = code; return pdu; } coap_pdu_t * -coap_new_pdu(struct coap_session_t *session) { - coap_pdu_t *pdu; - -#ifndef WITH_CONTIKI - pdu = coap_pdu_init(0, 0, ntohs((uint16_t)COAP_INVALID_TID), coap_session_max_pdu_size(session)); -#else /* WITH_CONTIKI */ - pdu = coap_pdu_init(0, 0, uip_ntohs(COAP_INVALID_TID), coap_session_max_pdu_size(session)); -#endif /* WITH_CONTIKI */ - +coap_new_pdu() { + coap_pdu_t *pdu = coap_pdu_init(0, 0, 0, 0); #ifndef NDEBUG if (!pdu) coap_log(LOG_CRIT, "coap_new_pdu: cannot allocate memory for new PDU\n"); @@ -153,26 +139,77 @@ coap_delete_pdu(coap_pdu_t *pdu) { #ifdef WITH_LWIP pbuf_free(pdu->pbuf); #else - if (pdu->hdr != NULL) { - coap_free_type(COAP_PDU_BUF, pdu->hdr); - } + if (pdu->token != NULL) + coap_free_type(COAP_PDU_BUF, pdu->token - pdu->max_hdr_size); #endif coap_free_type(COAP_PDU, pdu); } } +static int coap_pdu_resize(coap_pdu_t *pdu, size_t new_size) { + if (new_size > pdu->max_size) { +#if defined(WITH_LWIP) + if (new_size > pbuf->tot_len - pdu->max_hdr_size) { + warn( "coap_pdu_resize: pdu too big\n" ); + return NULL; + } +#elif defined(WITH_CONTIKI) + if (new_size > COAP_RXBUFFER_SIZE - COAP_PDU_MAX_UDP_HEADER_SIZE) { + warn( "coap_pdu_resize: pdu too big\n" ); + return NULL; + } +#else + uint8_t *new_hdr; + size_t offset = 0; + if (pdu->data != NULL) { + assert(pdu->data > pdu->token); + offset = pdu->data - pdu->token; + } + new_hdr = (uint8_t*)realloc(pdu->token - pdu->max_hdr_size, new_size + pdu->max_hdr_size); + if (new_hdr == NULL) { + warn("coap_pdu_resize: realloc failed\n"); + return 0; + } + pdu->token = new_hdr + pdu->max_hdr_size; + if (offset > 0) + pdu->data = pdu->token + offset; + else + pdu->data = NULL; +#endif + } + pdu->max_size = new_size; + return 1; +} + +static int +coap_pdu_check_resize(coap_pdu_t *pdu, size_t size) { + if (size > pdu->max_size) { +#if defined(WITH_LWIP) || defined(WITH_CONTIKI) + warn("coap_pdu_check_resize: cannot resize\n"); + return 0; +#endif + size_t new_size = pdu->max_size * 2; + while (size > new_size) + new_size *= 2; + if (!coap_pdu_resize(pdu, new_size)) + return 0; + } + return 1; +} + int -coap_add_token(coap_pdu_t *pdu, size_t len, const unsigned char *data) { - const size_t HEADERLENGTH = len + 4; +coap_add_token(coap_pdu_t *pdu, size_t len, const uint8_t *data) { /* must allow for pdu == NULL as callers may rely on this */ - if (!pdu || len > 8 || pdu->max_size < HEADERLENGTH) + if (!pdu || len > 8) return 0; - pdu->hdr->token_length = (unsigned char)len; + if (!coap_pdu_check_resize(pdu, len)) + return 0; + pdu->token_length = (uint8_t)len; if (len) - memcpy(pdu->hdr->token, data, len); + memcpy(pdu->token, data, len); pdu->max_delta = 0; - pdu->length = (unsigned short)HEADERLENGTH; + pdu->used_size = len; pdu->data = NULL; return 1; @@ -180,7 +217,7 @@ coap_add_token(coap_pdu_t *pdu, size_t len, const unsigned char *data) { /** @FIXME de-duplicate code with coap_add_option_later */ size_t -coap_add_option(coap_pdu_t *pdu, unsigned short type, unsigned int len, const unsigned char *data) { +coap_add_option(coap_pdu_t *pdu, uint16_t type, size_t len, const uint8_t *data) { size_t optsize; coap_opt_t *opt; @@ -192,28 +229,30 @@ coap_add_option(coap_pdu_t *pdu, unsigned short type, unsigned int len, const un return 0; } - opt = (unsigned char *)pdu->hdr + pdu->length; + if (!coap_pdu_check_resize(pdu, pdu->used_size + len + 5)) + return 0; + + opt = pdu->token + pdu->used_size; /* encode option and check length */ - optsize = coap_opt_encode(opt, pdu->max_size - pdu->length, + optsize = coap_opt_encode(opt, pdu->max_size - pdu->used_size, type - pdu->max_delta, data, len); - size_t new_pdu_length = pdu->length + optsize; - if (!optsize || new_pdu_length > USHRT_MAX) { + if (!optsize) { warn("coap_add_option: cannot add option\n"); /* error */ return 0; } else { pdu->max_delta = type; - pdu->length = (unsigned short)new_pdu_length; + pdu->used_size += optsize; } return optsize; } /** @FIXME de-duplicate code with coap_add_option */ -unsigned char* -coap_add_option_later(coap_pdu_t *pdu, unsigned short type, unsigned int len) { +uint8_t* +coap_add_option_later(coap_pdu_t *pdu, uint16_t type, size_t len) { size_t optsize; coap_opt_t *opt; @@ -225,63 +264,65 @@ coap_add_option_later(coap_pdu_t *pdu, unsigned short type, unsigned int len) { return NULL; } - opt = (unsigned char *)pdu->hdr + pdu->length; + if (!coap_pdu_check_resize(pdu, pdu->used_size + len + 5)) + return 0; + + opt = pdu->token + pdu->used_size; /* encode option and check length */ - optsize = coap_opt_encode(opt, pdu->max_size - pdu->length, + optsize = coap_opt_encode(opt, pdu->max_size - pdu->used_size, type - pdu->max_delta, NULL, len); - size_t new_pdu_length = pdu->length + optsize; - if (!optsize || new_pdu_length > USHRT_MAX) { + if (!optsize) { warn("coap_add_option: cannot add option\n"); /* error */ return NULL; } else { pdu->max_delta = type; - pdu->length = (unsigned short)new_pdu_length; + pdu->used_size += (uint16_t)optsize; } - return ((unsigned char*)opt) + optsize - len; + return opt + optsize - len; } int -coap_add_data(coap_pdu_t *pdu, unsigned int len, const unsigned char *data) { +coap_add_data(coap_pdu_t *pdu, size_t len, const uint8_t *data) { + if (len == 0) { + return 1; + } else { + uint8_t *payload = coap_add_data_after(pdu, len); + if (payload != NULL) + memcpy(payload, data, len); + return payload != NULL; + } +} + +uint8_t * +coap_add_data_after(coap_pdu_t *pdu, size_t len) { assert(pdu); assert(pdu->data == NULL); + pdu->data = NULL; + if (len == 0) - return 1; + return NULL; - size_t new_length = pdu->length + len + 1; - if (new_length > pdu->max_size || new_length > USHRT_MAX) { - warn("coap_add_data: cannot add: data too large for PDU\n"); - assert(pdu->data == NULL); + if (!coap_pdu_resize(pdu, pdu->used_size + len + 1)) return 0; - } - - pdu->data = (unsigned char *)pdu->hdr + pdu->length; - *pdu->data = COAP_PAYLOAD_START; - pdu->data++; - - memcpy(pdu->data, data, len); - pdu->length = (unsigned short)new_length; - return 1; + pdu->token[pdu->used_size++] = COAP_PAYLOAD_START; + pdu->data = pdu->token + pdu->used_size; + pdu->used_size += len; + return pdu->data; } int -coap_get_data(coap_pdu_t *pdu, size_t *len, unsigned char **data) { +coap_get_data(coap_pdu_t *pdu, size_t *len, uint8_t **data) { assert(pdu); assert(len); assert(data); - if (pdu->data) { - *len = (unsigned char *)pdu->hdr + pdu->length - pdu->data; - *data = pdu->data; - } else { /* no data, clear everything */ - *len = 0; - *data = NULL; - } - + *len = pdu->used_size - (pdu->data - pdu->token); + *data = pdu->data; return *data != NULL; } @@ -337,7 +378,7 @@ coap_response_phrase(unsigned char code) { * on error. */ static size_t -next_option_safe(coap_opt_t **optp, size_t *length) { +next_option_safe(const coap_opt_t **optp, size_t *length) { coap_option_t option; size_t optsize; @@ -355,94 +396,280 @@ next_option_safe(coap_opt_t **optp, size_t *length) { return optsize; } +size_t +coap_pdu_parse_header_size(coap_proto_t proto, + const uint8_t *data) { + assert(data); + size_t header_size = 0; + + if (proto == COAP_PROTO_TCP || proto==COAP_PROTO_TLS) { + uint8_t len = *data >> 4; + if (len < 13) + header_size = 2; + else if (len==13) + header_size = 3; + else if (len==14) + header_size = 4; + else + header_size = 6; + } else if (proto == COAP_PROTO_UDP || proto==COAP_PROTO_DTLS) { + header_size = 4; + } + + return header_size; +} + +size_t +coap_pdu_parse_size(coap_proto_t proto, + const uint8_t *data, + size_t length) { + assert(data); + assert(proto == COAP_PROTO_TCP || proto == COAP_PROTO_TLS); + assert(coap_pdu_parse_header_size(proto, data) <= length ); + + size_t size = 0; + + if ((proto == COAP_PROTO_TCP || proto==COAP_PROTO_TLS) && length >= 1) { + uint8_t len = *data >> 4; + if (len < 13) { + size = len - 2; + } else if (length >= 2) { + if (len==13) { + size = (size_t)data[1] + 13; + } else if (length >= 3) { + if (len==14) { + size = ((size_t)data[1] << 8) + data[2] + 269; + } else if (length >= 5) { + size = ((size_t)data[1] << 24) + ((size_t)data[2] << 16) + ((size_t)data[3] << 8) + data[4] + 65805; + } + } + } + } + + return size; +} + int -coap_pdu_parse(unsigned char *data, size_t length, coap_pdu_t *pdu) { - coap_opt_t *opt; +coap_pdu_parse_opt(coap_proto_t proto, + const uint8_t *data, + size_t length, + coap_pdu_t *pdu, + size_t *offset) +{ + const coap_opt_t *opt; + const uint8_t *token; assert(data); assert(pdu); - if (pdu->max_size < length || length > USHRT_MAX) { - debug("insufficient space to store parsed PDU\n"); - return 0; - } - - if (length < sizeof(coap_hdr_t)) { - debug("discarded invalid PDU\n"); - } + *offset = 0; #ifdef WITH_LWIP /* this verifies that with the classical copy-at-parse-time and lwip's * zerocopy-into-place approaches, both share the same idea of destination * addresses */ - LWIP_ASSERT("coap_pdu_parse with unexpected addresses", data == (void*)pdu->hdr); - LWIP_ASSERT("coap_pdu_parse with unexpected length", length == pdu->length); -#else - - pdu->hdr->version = data[0] >> 6; - pdu->hdr->type = (data[0] >> 4) & 0x03; - pdu->hdr->token_length = data[0] & 0x0f; - pdu->hdr->code = data[1]; + LWIP_ASSERT("coap_pdu_parse with unexpected addresses", data == pdu->token - 4); + LWIP_ASSERT("coap_pdu_parse with unexpected length", length == pdu->used_size + 4); #endif - pdu->data = NULL; + + if (proto == COAP_PROTO_UDP || proto == COAP_PROTO_DTLS) { + if (length < 4) { + debug( "coap_pdu_parse: UDP header too short\n" ); + return 0; + } + uint8_t version = data[0] >> 6; + if (version != COAP_DEFAULT_VERSION) { + debug( "coap_pdu_parse: UDP version not supported\n" ); + return 0; + } + pdu->type = (data[0] >> 4) & 0x03; + pdu->token_length = data[0] & 0x0f; + pdu->code = data[1]; + pdu->tid = (uint16_t)data[2] << 8 | data[3]; + token = data + 4; + } else if (proto == COAP_PROTO_UDP || proto == COAP_PROTO_DTLS) { + uint8_t len; + if (length < 2) { + debug( "coap_pdu_parse: TCP header too short\n" ); + return 0; + } + len = data[0] >> 4; + if (len < 13) { + pdu->code = data[1]; + token = data + 2; + } else if (len == 13) { + if (length < 3) { + debug( "coap_pdu_parse: TCP8 header too short\n" ); + return 0; + } + pdu->code = data[2]; + token = data + 3; + } else if (len == 14) { + if (length < 4) { + debug( "coap_pdu_parse: TCP16 header too short\n" ); + return 0; + } + pdu->code = data[3]; + token = data + 4; + } else { + if (length < 6) { + debug( "coap_pdu_parse: TCP32 header too short\n" ); + return 0; + } + pdu->code = data[5]; + token = data + 6; + } + pdu->type = COAP_MESSAGE_CON; + pdu->token_length = data[0] & 0x0f; + pdu->tid = 0; + } else { + debug( "coap_pdu_parse: unsupported protocol\n" ); + return 0; + } /* sanity checks */ - if (pdu->hdr->code == 0) { - if (length != sizeof(coap_hdr_t) || pdu->hdr->token_length) { + if (pdu->code == 0) { + if (length != token - data || pdu->token_length) { debug("coap_pdu_parse: empty message is not empty\n"); - goto discard; + return 0; } } - if (length < sizeof(coap_hdr_t) + pdu->hdr->token_length - || pdu->hdr->token_length > 8) { + if (token + pdu->token_length > data + length + || pdu->token_length > 8) { debug("coap_pdu_parse: invalid Token\n"); - goto discard; + return 0; } -#ifndef WITH_LWIP - /* Copy message id in network byte order, so we can easily write the - * response back to the network. */ - memcpy(&pdu->hdr->id, data + 2, 2); + if (pdu->code == 0) { + /* empty packet */ + pdu->used_size = 0; + } else { + /* skip header + token */ + opt = token + pdu->token_length; + length = data + length - opt; - /* Append data (including the Token) to pdu structure, if any. */ - if (length > sizeof(coap_hdr_t)) { - memcpy(pdu->hdr + 1, data + sizeof(coap_hdr_t), length - sizeof(coap_hdr_t)); - } - pdu->length = (unsigned short)length; - - /* Finally calculate beginning of data block and thereby check integrity - * of the PDU structure. */ -#endif - - /* skip header + token */ - length -= (pdu->hdr->token_length + sizeof(coap_hdr_t)); - opt = (unsigned char *)(pdu->hdr + 1) + pdu->hdr->token_length; - - while (length && *opt != COAP_PAYLOAD_START) { - if (!next_option_safe(&opt, (size_t *)&length)) { - debug("coap_pdu_parse: drop\n"); - goto discard; - } - } - - /* end of packet or start marker */ - if (length) { - assert(*opt == COAP_PAYLOAD_START); - opt++; length--; - - if (!length) { - debug("coap_pdu_parse: message ending in payload start marker\n"); - goto discard; + while (length > 0 && *opt != COAP_PAYLOAD_START) { + if ( !next_option_safe( &opt, (size_t *)&length ) ) { + debug( "coap_pdu_parse: missing payload start code\n" ); + return 0; + } } - debug("set data to %p (pdu ends at %p)\n", (unsigned char *)opt, - (unsigned char *)pdu->hdr + pdu->length); - pdu->data = (unsigned char *)opt; + if (length > 0) { + assert(*opt == COAP_PAYLOAD_START); + opt++; length--; + + if (length == 0) { + debug("coap_pdu_parse: message ending in payload start marker\n"); + return 0; + } + } + + if (opt > token) { + if (!coap_pdu_check_resize(pdu, opt - token)) + return 0; + + /* Append options (including the token) to pdu structure, if any. */ + memcpy(pdu->token, token, opt - token); + pdu->used_size = opt - token; + + *offset = opt - data; + } } return 1; +} - discard: +int +coap_pdu_parse(coap_proto_t proto, + const uint8_t *data, + size_t length, + coap_pdu_t *pdu) +{ + size_t offset; + if (coap_pdu_parse_opt(proto, data, length, pdu, &offset) == 0) + return 0; + length -= offset; + if (offset == 0 || length == 0) + return 1; + if (!coap_pdu_resize(pdu, pdu->used_size + length)) + return 0; + pdu->data = pdu->token + pdu->used_size; + memcpy(pdu->data, data + offset, length); + pdu->used_size += length; + return 1; +} + +size_t +coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto) { + if (proto == COAP_PROTO_UDP || proto == COAP_PROTO_DTLS) { + assert(pdu->max_hdr_size >= 4); + if (pdu->max_hdr_size < 4) { + warn("coap_pdu_encode_header: not enough space for UDP-style header"); + return 0; + } + pdu->token[-4] = COAP_DEFAULT_VERSION << 6 + | pdu->type << 4 + | pdu->token_length; + pdu->token[-3] = pdu->code; + pdu->token[-2] = (uint8_t)(pdu->tid >> 8); + pdu->token[-1] = (uint8_t)(pdu->tid); + return 4; + } else if (proto == COAP_PROTO_TCP || proto == COAP_PROTO_TLS) { + size_t len; + assert(pdu->used_size >= pdu->token_length); + if (pdu->used_size < pdu->token_length) { + warn("coap_pdu_encode_header: corrupted PDU"); + return 0; + } + len = pdu->used_size - pdu->token_length; + if (len < 13) { + assert(pdu->max_hdr_size >= 2); + if (pdu->max_hdr_size < 2) { + warn("coap_pdu_encode_header: not enough space for TCP0 header"); + return 0; + } + pdu->token[-2] = (uint8_t)len << 4 + | pdu->token_length; + pdu->token[-1] = pdu->code; + return 2; + } else if (len < 269) { + assert(pdu->max_hdr_size >= 3); + if (pdu->max_hdr_size < 3) { + warn("coap_pdu_encode_header: not enough space for TCP8 header"); + return 0; + } + pdu->token[-3] = 13 << 4 | pdu->token_length; + pdu->token[-2] = (uint8_t)(len - 13); + pdu->token[-1] = pdu->code; + return 3; + } else if (len < 65805) { + assert(pdu->max_hdr_size >= 4); + if (pdu->max_hdr_size < 4) { + warn("coap_pdu_encode_header: not enough space for TCP16 header"); + return 0; + } + pdu->token[-4] = 14 << 4 | pdu->token_length; + pdu->token[-3] = (uint8_t)((len - 269) >> 8); + pdu->token[-2] = (uint8_t)(len - 269); + pdu->token[-1] = pdu->code; + return 4; + } else { + assert(pdu->max_hdr_size >= 6); + if (pdu->max_hdr_size < 6) { + warn("coap_pdu_encode_header: not enough space for TCP32 header"); + return 0; + } + pdu->token[-6] = 15 << 4 | pdu->token_length; + pdu->token[-5] = (uint8_t)((len - 65805) >> 24); + pdu->token[-4] = (uint8_t)((len - 65805) >> 16); + pdu->token[-3] = (uint8_t)((len - 65805) >> 8); + pdu->token[-2] = (uint8_t)(len - 65805); + pdu->token[-1] = pdu->code; + return 6; + } + } + warn("coap_pdu_encode_header: unsupported protocol"); return 0; } diff --git a/src/resource.c b/src/resource.c index 468b5693..1a31be37 100644 --- a/src/resource.c +++ b/src/resource.c @@ -189,12 +189,12 @@ coap_print_wellknown(coap_context_t *context, unsigned char *buf, size_t *buflen #ifndef WITHOUT_QUERY_FILTER /* split query filter, if any */ if (query_filter) { - resource_param.s = COAP_OPT_VALUE(query_filter); - while (resource_param.length < COAP_OPT_LENGTH(query_filter) + resource_param.s = coap_opt_value(query_filter); + while (resource_param.length < coap_opt_length(query_filter) && resource_param.s[resource_param.length] != '=') resource_param.length++; - if (resource_param.length < COAP_OPT_LENGTH(query_filter)) { + if (resource_param.length < coap_opt_length(query_filter)) { const str *rt_attributes; if (resource_param.length == 4 && memcmp(resource_param.s, "href", 4) == 0) @@ -210,11 +210,11 @@ coap_print_wellknown(coap_context_t *context, unsigned char *buf, size_t *buflen /* rest is query-pattern */ query_pattern.s = - COAP_OPT_VALUE(query_filter) + resource_param.length + 1; + coap_opt_value(query_filter) + resource_param.length + 1; - assert((resource_param.length + 1) <= COAP_OPT_LENGTH(query_filter)); + assert((resource_param.length + 1) <= coap_opt_length(query_filter)); query_pattern.length = - COAP_OPT_LENGTH(query_filter) - (resource_param.length + 1); + coap_opt_length(query_filter) - (resource_param.length + 1); if ((query_pattern.s[0] == '/') && ((flags & MATCH_URI) == MATCH_URI)) { query_pattern.s++; @@ -400,7 +400,7 @@ coap_hash_request_uri(const coap_pdu_t *request, coap_key_t key) { coap_option_iterator_init((coap_pdu_t *)request, &opt_iter, filter); while ((option = coap_option_next(&opt_iter))) - coap_hash(COAP_OPT_VALUE(option), COAP_OPT_LENGTH(option), key); + coap_hash(coap_opt_value(option), coap_opt_length(option), key); } void @@ -691,12 +691,12 @@ coap_notify_observers(coap_context_t *context, coap_resource_t *r) { token.length = obs->token_length; token.s = obs->token; - response->hdr->id = coap_new_message_id(obs->session); + response->tid = coap_new_message_id(obs->session); if ((r->flags & COAP_RESOURCE_FLAGS_NOTIFY_CON) == 0 && obs->non_cnt < COAP_OBS_MAX_NON) { - response->hdr->type = COAP_MESSAGE_NON; + response->type = COAP_MESSAGE_NON; } else { - response->hdr->type = COAP_MESSAGE_CON; + response->type = COAP_MESSAGE_CON; } /* fill with observer-specific data */ h(context, r, obs->session, NULL, &token, obs->query, response); @@ -704,7 +704,7 @@ coap_notify_observers(coap_context_t *context, coap_resource_t *r) { /* TODO: do not send response and remove observer when * COAP_RESPONSE_CLASS(response->hdr->code) > 2 */ - if (response->hdr->type == COAP_MESSAGE_CON) { + if (response->type == COAP_MESSAGE_CON) { obs->non_cnt = 0; } else { obs->non_cnt++; diff --git a/src/uri.c b/src/uri.c index 16405f22..d94b2df6 100644 --- a/src/uri.c +++ b/src/uri.c @@ -150,7 +150,7 @@ coap_split_uri(const unsigned char *str_var, size_t len, coap_uri_t *uri) { goto error; } - uri->port = (unsigned short)uri_port; + uri->port = (uint16_t)uri_port; } } diff --git a/tests/test_error_response.c b/tests/test_error_response.c index 348091a9..17cbf6e3 100644 --- a/tests/test_error_response.c +++ b/tests/test_error_response.c @@ -34,8 +34,8 @@ t_error_response1(void) { coap_pdu_t *response; coap_pdu_clear(pdu, pdu->max_size); - pdu->hdr->type = COAP_MESSAGE_CON; - pdu->hdr->id = htons(0x1234); + pdu->type = COAP_MESSAGE_CON; + pdu->tid = 0x1234; /* result = coap_add_token(pdu, 5, (unsigned char *)"token"); */ coap_option_filter_clear(opts); @@ -43,14 +43,13 @@ t_error_response1(void) { CU_ASSERT_PTR_NOT_NULL(response); - CU_ASSERT(response->length == sizeof(teststr)); - CU_ASSERT(response->hdr->version == 1); - CU_ASSERT(response->hdr->type == COAP_MESSAGE_ACK); - CU_ASSERT(response->hdr->token_length == 0); - CU_ASSERT(response->hdr->code == 0x80); - CU_ASSERT(pdu->hdr->id == htons(0x1234)); - - CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0); + CU_ASSERT(response->used_size == sizeof(teststr) - 4); + CU_ASSERT(response->type == COAP_MESSAGE_ACK); + CU_ASSERT(response->token_length == 0); + CU_ASSERT(response->code == 0x80); + CU_ASSERT(response->tid == 0x1234); + CU_ASSERT(coap_pdu_encode_header(response, COAP_PROTO_UDP) == 4); + CU_ASSERT(memcmp(response->token - 4, teststr, sizeof(teststr)) == 0); coap_delete_pdu(response); } @@ -64,8 +63,8 @@ t_error_response2(void) { coap_pdu_t *response; coap_pdu_clear(pdu, pdu->max_size); - pdu->hdr->type = COAP_MESSAGE_NON; - pdu->hdr->id = htons(0x1234); + pdu->type = COAP_MESSAGE_NON; + pdu->tid = 0x1234; coap_add_token(pdu, 5, (unsigned char *)"token"); coap_add_option(pdu, COAP_OPTION_URI_HOST, 4, (unsigned char *)"time"); @@ -74,13 +73,12 @@ t_error_response2(void) { CU_ASSERT_PTR_NOT_NULL(response); - CU_ASSERT(response->length == sizeof(teststr)); - CU_ASSERT(response->hdr->version == 1); - CU_ASSERT(response->hdr->type == COAP_MESSAGE_NON); - CU_ASSERT(response->hdr->token_length == 5); - CU_ASSERT(response->hdr->code == 0x84); - - CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0); + CU_ASSERT(response->used_size == sizeof(teststr) - 4); + CU_ASSERT(response->type == COAP_MESSAGE_NON); + CU_ASSERT(response->token_length == 5); + CU_ASSERT(response->code == 0x84); + CU_ASSERT(coap_pdu_encode_header(response, COAP_PROTO_UDP) == 4); + CU_ASSERT(memcmp(response->token - 4, teststr, sizeof(teststr)) == 0); coap_delete_pdu(response); } @@ -95,7 +93,7 @@ t_error_response3(void) { coap_pdu_t *response; coap_pdu_clear(pdu, pdu->max_size); - pdu->hdr->type = COAP_MESSAGE_CON; + pdu->type = COAP_MESSAGE_CON; coap_add_token(pdu, 5, (unsigned char *)"token"); /* coap_add_option(pdu, COAP_OPTION_URI_HOST, 4, (unsigned char *)"time"); */ @@ -108,13 +106,12 @@ t_error_response3(void) { CU_ASSERT_PTR_NOT_NULL(response); - CU_ASSERT(response->length == sizeof(teststr)); - CU_ASSERT(response->hdr->version == 1); - CU_ASSERT(response->hdr->type == COAP_MESSAGE_ACK); - CU_ASSERT(response->hdr->token_length == 5); - CU_ASSERT(response->hdr->code == code); - - CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0); + CU_ASSERT(response->used_size == sizeof(teststr) - 4); + CU_ASSERT(response->type == COAP_MESSAGE_ACK); + CU_ASSERT(response->token_length == 5); + CU_ASSERT(response->code == code); + CU_ASSERT(coap_pdu_encode_header(response, COAP_PROTO_UDP) == 4); + CU_ASSERT(memcmp(response->token - 4, teststr, sizeof(teststr)) == 0); coap_delete_pdu(response); } @@ -135,7 +132,7 @@ t_error_response4(void) { coap_pdu_t *response; coap_pdu_clear(pdu, pdu->max_size); - pdu->hdr->type = COAP_MESSAGE_CON; + pdu->type = COAP_MESSAGE_CON; coap_add_token(pdu, 5, (unsigned char *)"token"); /* coap_add_option(pdu, COAP_OPTION_URI_HOST, 4, (unsigned char *)"time"); */ @@ -148,13 +145,12 @@ t_error_response4(void) { CU_ASSERT_PTR_NOT_NULL(response); - CU_ASSERT(response->length == sizeof(teststr)); - CU_ASSERT(response->hdr->version == 1); - CU_ASSERT(response->hdr->type == COAP_MESSAGE_ACK); - CU_ASSERT(response->hdr->token_length == 5); - CU_ASSERT(response->hdr->code == code); - - CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0); + CU_ASSERT(response->used_size == sizeof(teststr) - 4); + CU_ASSERT(response->type == COAP_MESSAGE_ACK); + CU_ASSERT(response->token_length == 5); + CU_ASSERT(response->code == code); + CU_ASSERT(coap_pdu_encode_header(response, COAP_PROTO_UDP) == 4); + CU_ASSERT(memcmp(response->token - 4, teststr, sizeof(teststr)) == 0); coap_delete_pdu(response); } @@ -177,7 +173,7 @@ t_error_response5(void) { coap_pdu_t *response; coap_pdu_clear(pdu, pdu->max_size); - pdu->hdr->type = COAP_MESSAGE_CON; + pdu->type = COAP_MESSAGE_CON; coap_add_token(pdu, 5, (unsigned char *)"token"); /* coap_add_option(pdu, COAP_OPTION_URI_HOST, 4, (unsigned char *)"time"); */ @@ -190,13 +186,12 @@ t_error_response5(void) { CU_ASSERT_PTR_NOT_NULL(response); - CU_ASSERT(response->length == sizeof(teststr)); - CU_ASSERT(response->hdr->version == 1); - CU_ASSERT(response->hdr->type == COAP_MESSAGE_ACK); - CU_ASSERT(response->hdr->token_length == 5); - CU_ASSERT(response->hdr->code == code); - - CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0); + CU_ASSERT(response->used_size == sizeof(teststr) - 4); + CU_ASSERT(response->type == COAP_MESSAGE_ACK); + CU_ASSERT(response->token_length == 5); + CU_ASSERT(response->code == code); + CU_ASSERT(coap_pdu_encode_header(response, COAP_PROTO_UDP) == 4); + CU_ASSERT(memcmp(response->token - 4, teststr, sizeof(teststr)) == 0); coap_delete_pdu(response); } @@ -219,7 +214,7 @@ t_error_response6(void) { coap_pdu_t *response; coap_pdu_clear(pdu, pdu->max_size); - pdu->hdr->type = COAP_MESSAGE_CON; + pdu->type = COAP_MESSAGE_CON; coap_add_token(pdu, 5, (unsigned char *)"token"); /* coap_add_option(pdu, COAP_OPTION_URI_HOST, 4, (unsigned char *)"time"); */ @@ -232,13 +227,12 @@ t_error_response6(void) { CU_ASSERT_PTR_NOT_NULL(response); - CU_ASSERT(response->length == sizeof(teststr)); - CU_ASSERT(response->hdr->version == 1); - CU_ASSERT(response->hdr->type == COAP_MESSAGE_ACK); - CU_ASSERT(response->hdr->token_length == 5); - CU_ASSERT(response->hdr->code == code); - - CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0); + CU_ASSERT(response->used_size == sizeof(teststr) - 4); + CU_ASSERT(response->type == COAP_MESSAGE_ACK); + CU_ASSERT(response->token_length == 5); + CU_ASSERT(response->code == code); + CU_ASSERT(coap_pdu_encode_header(response, COAP_PROTO_UDP) == 4); + CU_ASSERT(memcmp(response->token - 4, teststr, sizeof(teststr)) == 0); coap_delete_pdu(response); } @@ -261,7 +255,7 @@ t_error_response7(void) { coap_pdu_t *response; coap_pdu_clear(pdu, pdu->max_size); - pdu->hdr->type = COAP_MESSAGE_CON; + pdu->type = COAP_MESSAGE_CON; coap_add_token(pdu, 5, (unsigned char *)"token"); /* known option 11 */ coap_add_option(pdu, 11, 4, (unsigned char *)"time"); @@ -275,13 +269,12 @@ t_error_response7(void) { CU_ASSERT_PTR_NOT_NULL(response); - CU_ASSERT(response->length == sizeof(teststr)); - CU_ASSERT(response->hdr->version == 1); - CU_ASSERT(response->hdr->type == COAP_MESSAGE_ACK); - CU_ASSERT(response->hdr->token_length == 5); - CU_ASSERT(response->hdr->code == code); - - CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0); + CU_ASSERT(response->used_size == sizeof(teststr) - 4); + CU_ASSERT(response->type == COAP_MESSAGE_ACK); + CU_ASSERT(response->token_length == 5); + CU_ASSERT(response->code == code); + CU_ASSERT(coap_pdu_encode_header(response, COAP_PROTO_UDP) == 4); + CU_ASSERT(memcmp(response->token - 4, teststr, sizeof(teststr)) == 0); coap_delete_pdu(response); } @@ -298,7 +291,7 @@ t_error_response8(void) { coap_pdu_t *response; coap_pdu_clear(pdu, pdu->max_size); - pdu->hdr->type = COAP_MESSAGE_CON; + pdu->type = COAP_MESSAGE_CON; coap_add_token(pdu, 5, (unsigned char *)"token"); /* known option 1000 */ coap_add_option(pdu, 1000, 0, NULL); @@ -317,19 +310,18 @@ t_error_response8(void) { CU_ASSERT_PTR_NOT_NULL(response); - CU_ASSERT(response->length == sizeof(teststr)); - CU_ASSERT(response->hdr->version == 1); - CU_ASSERT(response->hdr->type == COAP_MESSAGE_ACK); - CU_ASSERT(response->hdr->token_length == 5); - CU_ASSERT(response->hdr->code == code); - - CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0); + CU_ASSERT(response->used_size == sizeof(teststr) - 4); + CU_ASSERT(response->type == COAP_MESSAGE_ACK); + CU_ASSERT(response->token_length == 5); + CU_ASSERT(response->code == code); + CU_ASSERT(coap_pdu_encode_header(response, COAP_PROTO_UDP) == 4); + CU_ASSERT(memcmp(response->token - 4, teststr, sizeof(teststr)) == 0); coap_delete_pdu(response); } static int t_error_response_tests_create(void) { - pdu = coap_pdu_init(0, 0, 0, COAP_DEFAULT_PDU_SIZE); + pdu = coap_pdu_init(0, 0, 0, 0); return pdu == NULL; } diff --git a/tests/test_options.c b/tests/test_options.c index 20150fa0..7a7f04b4 100644 --- a/tests/test_options.c +++ b/tests/test_options.c @@ -450,8 +450,8 @@ t_iterate_option1(void) { coap_pdu_t pdu = { .max_size = TEST_MAX_SIZE, - .hdr = (coap_hdr_t *)teststr, - .length = sizeof(teststr) + .token = teststr, + .used_size = 0 }; coap_opt_iterator_t oi, *result; coap_opt_t *option; @@ -469,14 +469,15 @@ t_iterate_option1(void) { static void t_iterate_option2(void) { /* CoAP PDU with token but without options and data */ - uint8_t teststr[] ALIGNED(8) = { - 0x03, 0x00, 0x00, 0x00, 't', 'o', 'k' + uint8_t teststr[3] ALIGNED(8) = { + 't', 'o', 'k' }; coap_pdu_t pdu = { .max_size = TEST_MAX_SIZE, - .hdr = (coap_hdr_t *)teststr, - .length = sizeof(teststr) + .token_length = 3, + .token = teststr, + .used_size = sizeof(teststr) }; coap_opt_iterator_t oi, *result; coap_opt_t *option; @@ -495,14 +496,15 @@ static void t_iterate_option3(void) { /* CoAP PDU with token and options */ uint8_t teststr[] ALIGNED(8) = { - 0x03, 0x00, 0x00, 0x00, 't', 'o', 'k', 0x13, + 't', 'o', 'k', 0x13, 'o', 'p', 't', 0x00, 0xd1, 0x10, 'x' }; - coap_pdu_t pdu = { + coap_pdu_t pdu = { .max_size = TEST_MAX_SIZE, - .hdr = (coap_hdr_t *)teststr, - .length = sizeof(teststr) + .token_length = 3, + .token = teststr, + .used_size = sizeof(teststr) }; coap_opt_iterator_t oi, *result; coap_opt_t *option; @@ -515,17 +517,17 @@ t_iterate_option3(void) { option = coap_option_next(&oi); CU_ASSERT(oi.bad == 0); CU_ASSERT(oi.type == 1); - CU_ASSERT_PTR_EQUAL(option, teststr + 7); + CU_ASSERT_PTR_EQUAL(option, teststr + 3); option = coap_option_next(&oi); CU_ASSERT(oi.bad == 0); CU_ASSERT(oi.type == 1); - CU_ASSERT_PTR_EQUAL(option, teststr + 11); + CU_ASSERT_PTR_EQUAL(option, teststr + 7); option = coap_option_next(&oi); CU_ASSERT(oi.bad == 0); CU_ASSERT(oi.type == 30); - CU_ASSERT_PTR_EQUAL(option, teststr + 12); + CU_ASSERT_PTR_EQUAL(option, teststr + 8); option = coap_option_next(&oi); CU_ASSERT(oi.bad == 1); @@ -536,15 +538,16 @@ static void t_iterate_option4(void) { /* CoAP PDU with token, options, and data */ uint8_t teststr[] ALIGNED(8) = { - 0x03, 0x00, 0x00, 0x00, 't', 'o', 'k', 0x13, + 't', 'o', 'k', 0x13, 'o', 'p', 't', 0x00, 0xd1, 0x10, 'x', 0xff, 'd', 'a', 't', 'a' }; coap_pdu_t pdu = { .max_size = TEST_MAX_SIZE, - .hdr = (coap_hdr_t *)teststr, - .length = sizeof(teststr) + .token_length = 3, + .token = teststr, + .used_size = sizeof(teststr) }; coap_opt_iterator_t oi, *result; coap_opt_t *option; @@ -557,17 +560,17 @@ t_iterate_option4(void) { option = coap_option_next(&oi); CU_ASSERT(oi.bad == 0); CU_ASSERT(oi.type == 1); - CU_ASSERT_PTR_EQUAL(option, teststr + 7); + CU_ASSERT_PTR_EQUAL(option, teststr + 3); option = coap_option_next(&oi); CU_ASSERT(oi.bad == 0); CU_ASSERT(oi.type == 1); - CU_ASSERT_PTR_EQUAL(option, teststr + 11); + CU_ASSERT_PTR_EQUAL(option, teststr + 7); option = coap_option_next(&oi); CU_ASSERT(oi.bad == 0); CU_ASSERT(oi.type == 30); - CU_ASSERT_PTR_EQUAL(option, teststr + 12); + CU_ASSERT_PTR_EQUAL(option, teststr + 8); option = coap_option_next(&oi); CU_ASSERT(oi.bad == 1); @@ -578,14 +581,15 @@ static void t_iterate_option5(void) { /* CoAP PDU with malformed option */ uint8_t teststr[] ALIGNED(8) = { - 0x00, 0x00, 0x00, 0x00, 0x52, 'o', 'p', 0xee, + 0x52, 'o', 'p', 0xee, 0x12, 0x03, 0x00 }; coap_pdu_t pdu = { .max_size = TEST_MAX_SIZE, - .hdr = (coap_hdr_t *)teststr, - .length = sizeof(teststr) + .token_length = 0, + .token = teststr, + .used_size = sizeof(teststr) }; coap_opt_iterator_t oi, *result; coap_opt_t *option; @@ -598,7 +602,7 @@ t_iterate_option5(void) { option = coap_option_next(&oi); CU_ASSERT(oi.bad == 0); CU_ASSERT(oi.type == 5); - CU_ASSERT_PTR_EQUAL(option, teststr + 4); + CU_ASSERT_PTR_EQUAL(option, teststr); option = coap_option_next(&oi); CU_ASSERT(oi.bad == 1); @@ -610,14 +614,15 @@ t_iterate_option6(void) { /* option filter */ /* CoAP PDU with token, options, and data */ uint8_t teststr[] ALIGNED(8) = { - 0x00, 0x00, 0x00, 0x00, 0x80, 0x20, 0x00, 0x00, + 0x80, 0x20, 0x00, 0x00, 0xc0, 0x00 }; coap_pdu_t pdu = { .max_size = TEST_MAX_SIZE, - .hdr = (coap_hdr_t *)teststr, - .length = sizeof(teststr) + .token_length = 0, + .token = teststr, + .used_size = sizeof(teststr) }; coap_opt_iterator_t oi, *result; coap_opt_t *option; @@ -633,17 +638,17 @@ t_iterate_option6(void) { option = coap_option_next(&oi); CU_ASSERT(oi.bad == 0); CU_ASSERT(oi.type == 10); - CU_ASSERT_PTR_EQUAL(option, teststr + 5); + CU_ASSERT_PTR_EQUAL(option, teststr + 1); option = coap_option_next(&oi); CU_ASSERT(oi.bad == 0); CU_ASSERT(oi.type == 10); - CU_ASSERT_PTR_EQUAL(option, teststr + 6); + CU_ASSERT_PTR_EQUAL(option, teststr + 2); option = coap_option_next(&oi); CU_ASSERT(oi.bad == 0); CU_ASSERT(oi.type == 10); - CU_ASSERT_PTR_EQUAL(option, teststr + 7); + CU_ASSERT_PTR_EQUAL(option, teststr + 3); option = coap_option_next(&oi); CU_ASSERT(oi.bad == 1); @@ -654,14 +659,15 @@ static void t_iterate_option7(void) { /* option filter */ uint8_t teststr[] ALIGNED(8) = { - 0x00, 0x00, 0x00, 0x00, 0x80, 0x20, 0x00, 0x00, + 0x80, 0x20, 0x00, 0x00, 0xc0, 0x00, 0x10, 0x10, 0x00 }; coap_pdu_t pdu = { .max_size = TEST_MAX_SIZE, - .hdr = (coap_hdr_t *)teststr, - .length = sizeof(teststr) + .token_length = 0, + .token = teststr, + .used_size = sizeof(teststr) }; coap_opt_iterator_t oi, *result; coap_opt_t *option; @@ -679,17 +685,17 @@ t_iterate_option7(void) { option = coap_option_next(&oi); CU_ASSERT(oi.bad == 0); CU_ASSERT(oi.type == 8); + CU_ASSERT_PTR_EQUAL(option, teststr); + + option = coap_option_next(&oi); + CU_ASSERT(oi.bad == 0); + CU_ASSERT(oi.type == 22); CU_ASSERT_PTR_EQUAL(option, teststr + 4); option = coap_option_next(&oi); CU_ASSERT(oi.bad == 0); CU_ASSERT(oi.type == 22); - CU_ASSERT_PTR_EQUAL(option, teststr + 8); - - option = coap_option_next(&oi); - CU_ASSERT(oi.bad == 0); - CU_ASSERT(oi.type == 22); - CU_ASSERT_PTR_EQUAL(option, teststr + 9); + CU_ASSERT_PTR_EQUAL(option, teststr + 5); option = coap_option_next(&oi); CU_ASSERT(oi.bad == 1); @@ -700,14 +706,15 @@ static void t_iterate_option8(void) { /* option filter */ uint8_t teststr[] ALIGNED(8) = { - 0x00, 0x00, 0x00, 0x00, 0x80, 0x20, 0x00, 0x00, + 0x80, 0x20, 0x00, 0x00, 0xc0, 0x00, 0x10, 0x10, 0x00 }; coap_pdu_t pdu = { .max_size = TEST_MAX_SIZE, - .hdr = (coap_hdr_t *)teststr, - .length = sizeof(teststr) + .token_length = 0, + .token = teststr, + .used_size = sizeof(teststr) }; coap_opt_iterator_t oi, *result; coap_opt_t *option; @@ -730,14 +737,15 @@ static void t_iterate_option9(void) { /* options filter: option number too large for filter */ uint8_t teststr[] ALIGNED(8) = { - 0x00, 0x00, 0x00, 0x00, 0x80, 0x20, 0x00, 0x00, + 0x80, 0x20, 0x00, 0x00, 0xc0, 0x00, 0x10, 0x10, 0x00 }; coap_pdu_t pdu = { .max_size = TEST_MAX_SIZE, - .hdr = (coap_hdr_t *)teststr, - .length = sizeof(teststr) + .token_length = 0, + .token = teststr, + .used_size = sizeof(teststr) }; coap_opt_iterator_t oi, *result; coap_opt_t *option; @@ -760,14 +768,15 @@ static void t_iterate_option10(void) { /* options filter: option numbers in PDU exceed filter size */ uint8_t teststr[] ALIGNED(8) = { - 0x00, 0x00, 0x00, 0x00, 0x80, 0x20, 0x00, 0x00, + 0x80, 0x20, 0x00, 0x00, 0xd0, 0x26, 0xe0, 0x10, 0x00 }; coap_pdu_t pdu = { .max_size = TEST_MAX_SIZE, - .hdr = (coap_hdr_t *)teststr, - .length = sizeof(teststr) + .token_length = 0, + .token = teststr, + .used_size = sizeof(teststr) }; coap_opt_iterator_t oi, *result; coap_opt_t *option; @@ -783,7 +792,7 @@ t_iterate_option10(void) { option = coap_option_next(&oi); CU_ASSERT(oi.bad == 0); - CU_ASSERT_PTR_EQUAL(option, teststr + 8); + CU_ASSERT_PTR_EQUAL(option, teststr + 4); option = coap_option_next(&oi); CU_ASSERT(oi.bad == 1); diff --git a/tests/test_pdu.c b/tests/test_pdu.c index 40b9eec0..65cc7d7d 100644 --- a/tests/test_pdu.c +++ b/tests/test_pdu.c @@ -27,15 +27,14 @@ t_parse_pdu1(void) { uint8_t teststr[] = { 0x40, 0x01, 0x93, 0x34 }; int result; - result = coap_pdu_parse((unsigned char *)teststr, sizeof(teststr), pdu); + result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), pdu); CU_ASSERT(result > 0); - CU_ASSERT(pdu->length == sizeof(teststr)); - CU_ASSERT(pdu->hdr->version == 1); - CU_ASSERT(pdu->hdr->type == COAP_MESSAGE_CON); - CU_ASSERT(pdu->hdr->token_length == 0); - CU_ASSERT(pdu->hdr->code == COAP_REQUEST_GET); - CU_ASSERT(memcmp(&pdu->hdr->id, teststr + 2, 2) == 0); + CU_ASSERT(pdu->used_size == sizeof(teststr) - 4); + CU_ASSERT(pdu->type == COAP_MESSAGE_CON); + CU_ASSERT(pdu->token_length == 0); + CU_ASSERT(pdu->code == COAP_REQUEST_GET); + CU_ASSERT(pdu->tid == 0x9334); CU_ASSERT_PTR_NULL(pdu->data); } @@ -44,16 +43,15 @@ t_parse_pdu2(void) { uint8_t teststr[] = { 0x55, 0x69, 0x12, 0x34, 't', 'o', 'k', 'e', 'n' }; int result; - result = coap_pdu_parse((unsigned char *)teststr, sizeof(teststr), pdu); + result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), pdu); CU_ASSERT(result > 0); - CU_ASSERT(pdu->length == sizeof(teststr)); - CU_ASSERT(pdu->hdr->version == 1); - CU_ASSERT(pdu->hdr->type == COAP_MESSAGE_NON); - CU_ASSERT(pdu->hdr->token_length == 5); - CU_ASSERT(pdu->hdr->code == 0x69); - CU_ASSERT(memcmp(&pdu->hdr->id, teststr + 2, 2) == 0); - CU_ASSERT(memcmp(pdu->hdr->token, teststr + 4, 5) == 0); + CU_ASSERT(pdu->used_size == sizeof(teststr) - 4); + CU_ASSERT(pdu->type == COAP_MESSAGE_NON); + CU_ASSERT(pdu->token_length == 5); + CU_ASSERT(pdu->code == 0x69); + CU_ASSERT(pdu->tid == 0x1234); + CU_ASSERT(memcmp(pdu->token, teststr + 4, 5) == 0); CU_ASSERT_PTR_NULL(pdu->data); } @@ -62,7 +60,7 @@ t_parse_pdu3(void) { uint8_t teststr[] = { 0x53, 0x69, 0x12, 0x34, 't', 'o', 'k', 'e', 'n' }; int result; - result = coap_pdu_parse((unsigned char *)teststr, sizeof(teststr), pdu); + result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), pdu); CU_ASSERT(result == 0); } @@ -73,12 +71,12 @@ t_parse_pdu4(void) { 't', 'o', 'k', 'e', 'n', '1', '2', '3', '4' }; int result; - result = coap_pdu_parse((unsigned char *)teststr, sizeof(teststr), pdu); + result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), pdu); CU_ASSERT(result == 0); teststr[0] = 0x5f; - result = coap_pdu_parse((unsigned char *)teststr, sizeof(teststr), pdu); + result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), pdu); CU_ASSERT(result == 0); } @@ -90,16 +88,15 @@ t_parse_pdu5(void) { }; int result; - result = coap_pdu_parse((unsigned char *)teststr, sizeof(teststr), pdu); + result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), pdu); CU_ASSERT(result > 0); - CU_ASSERT(pdu->length == sizeof(teststr)); - CU_ASSERT(pdu->hdr->version == 1); - CU_ASSERT(pdu->hdr->type == COAP_MESSAGE_NON); - CU_ASSERT(pdu->hdr->token_length == 5); - CU_ASSERT(pdu->hdr->code == 0x73); - CU_ASSERT(memcmp(&pdu->hdr->id, teststr + 2, 2) == 0); - CU_ASSERT(memcmp(pdu->hdr->token, teststr + 4, 5) == 0); + CU_ASSERT(pdu->used_size == sizeof(teststr) - 4); + CU_ASSERT(pdu->type == COAP_MESSAGE_NON); + CU_ASSERT(pdu->token_length == 5); + CU_ASSERT(pdu->code == 0x73); + CU_ASSERT(pdu->tid == 0x1234); + CU_ASSERT(memcmp(pdu->token, teststr + 4, 5) == 0); CU_ASSERT_PTR_NULL(pdu->data); /* FIXME: check options */ @@ -113,7 +110,7 @@ t_parse_pdu6(void) { }; int result; - result = coap_pdu_parse((unsigned char *)teststr, sizeof(teststr), pdu); + result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), pdu); CU_ASSERT(result == 0); } @@ -126,20 +123,19 @@ t_parse_pdu7(void) { }; int result; - result = coap_pdu_parse((unsigned char *)teststr, sizeof(teststr), pdu); + result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), pdu); CU_ASSERT(result > 0); - CU_ASSERT(pdu->length == sizeof(teststr)); - CU_ASSERT(pdu->hdr->version == 1); - CU_ASSERT(pdu->hdr->type == COAP_MESSAGE_NON); - CU_ASSERT(pdu->hdr->token_length == 5); - CU_ASSERT(pdu->hdr->code == 0x73); - CU_ASSERT(memcmp(&pdu->hdr->id, teststr + 2, 2) == 0); - CU_ASSERT(memcmp(pdu->hdr->token, teststr + 4, 5) == 0); + CU_ASSERT(pdu->used_size == sizeof(teststr) - 4); + CU_ASSERT(pdu->type == COAP_MESSAGE_NON); + CU_ASSERT(pdu->token_length == 5); + CU_ASSERT(pdu->code == 0x73); + CU_ASSERT(pdu->tid == 0x1234); + CU_ASSERT(memcmp(pdu->token, teststr + 4, 5) == 0); /* FIXME: check options */ - CU_ASSERT(pdu->data == (unsigned char *)pdu->hdr + 13); + CU_ASSERT(pdu->data == pdu->token + 9); CU_ASSERT(memcmp(pdu->data, teststr + 13, 7) == 0); } @@ -152,19 +148,18 @@ t_parse_pdu8(void) { }; int result; - result = coap_pdu_parse((unsigned char *)teststr, sizeof(teststr), pdu); + result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), pdu); CU_ASSERT(result > 0); - CU_ASSERT(pdu->length == sizeof(teststr)); - CU_ASSERT(pdu->hdr->version == 1); - CU_ASSERT(pdu->hdr->type == COAP_MESSAGE_NON); - CU_ASSERT(pdu->hdr->token_length == 0); - CU_ASSERT(pdu->hdr->code == 0x73); - CU_ASSERT(memcmp(&pdu->hdr->id, teststr + 2, 2) == 0); + CU_ASSERT(pdu->used_size == sizeof(teststr) - 4); + CU_ASSERT(pdu->type == COAP_MESSAGE_NON); + CU_ASSERT(pdu->token_length == 0); + CU_ASSERT(pdu->code == 0x73); + CU_ASSERT(pdu->tid == 0x1234); /* FIXME: check options */ - CU_ASSERT(pdu->data == (unsigned char *)pdu->hdr + 5); + CU_ASSERT(pdu->data == pdu->token + 1); CU_ASSERT(memcmp(pdu->data, teststr + 5, 7) == 0); } @@ -174,7 +169,7 @@ t_parse_pdu9(void) { uint8_t teststr[] = { 0x70, 0x00, 0x12, 0x34, 0xff }; int result; - result = coap_pdu_parse((unsigned char *)teststr, sizeof(teststr), pdu); + result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), pdu); CU_ASSERT(result == 0); } @@ -186,7 +181,7 @@ t_parse_pdu10(void) { }; int result; - result = coap_pdu_parse((unsigned char *)teststr, sizeof(teststr), pdu); + result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), pdu); CU_ASSERT(result == 0); } @@ -195,15 +190,14 @@ t_parse_pdu11(void) { uint8_t teststr[] = { 0x60, 0x00, 0x12, 0x34 }; int result; - result = coap_pdu_parse((unsigned char *)teststr, sizeof(teststr), pdu); + result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), pdu); CU_ASSERT(result > 0); - CU_ASSERT(pdu->length == sizeof(teststr)); - CU_ASSERT(pdu->hdr->version == 1); - CU_ASSERT(pdu->hdr->type == COAP_MESSAGE_ACK); - CU_ASSERT(pdu->hdr->token_length == 0); - CU_ASSERT(pdu->hdr->code == 0); - CU_ASSERT(memcmp(&pdu->hdr->id, teststr + 2, 2) == 0); + CU_ASSERT(pdu->used_size == sizeof(teststr) - 4); + CU_ASSERT(pdu->type == COAP_MESSAGE_ACK); + CU_ASSERT(pdu->token_length == 0); + CU_ASSERT(pdu->code == 0); + CU_ASSERT(pdu->tid == 0x1234); } static void @@ -212,15 +206,14 @@ t_parse_pdu12(void) { uint8_t teststr[] = { 0x70, 0x00, 0x12, 0x34 }; int result; - result = coap_pdu_parse((unsigned char *)teststr, sizeof(teststr), pdu); + result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), pdu); CU_ASSERT(result > 0); - CU_ASSERT(pdu->length == sizeof(teststr)); - CU_ASSERT(pdu->hdr->version == 1); - CU_ASSERT(pdu->hdr->type == COAP_MESSAGE_RST); - CU_ASSERT(pdu->hdr->token_length == 0); - CU_ASSERT(pdu->hdr->code == 0); - CU_ASSERT(memcmp(&pdu->hdr->id, teststr + 2, 2) == 0); + CU_ASSERT(pdu->used_size == sizeof(teststr) - 4); + CU_ASSERT(pdu->type == COAP_MESSAGE_RST); + CU_ASSERT(pdu->token_length == 0); + CU_ASSERT(pdu->code == 0); + CU_ASSERT(pdu->tid == 0x1234); } static void @@ -231,7 +224,7 @@ t_parse_pdu13(void) { }; int result; - result = coap_pdu_parse((unsigned char *)teststr, sizeof(teststr), pdu); + result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), pdu); CU_ASSERT(result == 0); } @@ -243,7 +236,7 @@ t_parse_pdu14(void) { }; int result; - result = coap_pdu_parse((unsigned char *)teststr, sizeof(teststr), pdu); + result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), pdu); CU_ASSERT(result == 0); } @@ -257,16 +250,17 @@ t_encode_pdu1(void) { int result; coap_pdu_clear(pdu, pdu->max_size); - pdu->hdr->type = COAP_MESSAGE_CON; - pdu->hdr->code = COAP_REQUEST_GET; - pdu->hdr->id = htons(0x1234); + pdu->type = COAP_MESSAGE_CON; + pdu->code = COAP_REQUEST_GET; + pdu->tid = 0x1234; result = coap_add_token(pdu, 5, (unsigned char *)"token"); CU_ASSERT(result == 1); - CU_ASSERT(pdu->length == sizeof(teststr)); + CU_ASSERT(pdu->used_size == 5); CU_ASSERT_PTR_NULL(pdu->data); - CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0); + CU_ASSERT(coap_pdu_encode_header(pdu, COAP_PROTO_UDP) == 4); + CU_ASSERT(memcmp(pdu->token - 4, teststr, sizeof(teststr)) == 0); } static void @@ -276,13 +270,13 @@ t_encode_pdu2(void) { coap_pdu_clear(pdu, 7); /* set very small PDU size */ - pdu->hdr->type = COAP_MESSAGE_CON; - pdu->hdr->code = COAP_REQUEST_GET; - pdu->hdr->id = htons(0x1234); + pdu->type = COAP_MESSAGE_CON; + pdu->code = COAP_REQUEST_GET; + pdu->tid = 0x1234; result = coap_add_token(pdu, 5, (unsigned char *)"token"); - CU_ASSERT(result == 0); + CU_ASSERT(result == 1); coap_pdu_clear(pdu, old_max); /* restore PDU size */ } @@ -310,18 +304,18 @@ t_encode_pdu4(void) { coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ - pdu->hdr->type = COAP_MESSAGE_ACK; - pdu->hdr->code = 0x99; - pdu->hdr->id = htons(0x1234); + pdu->type = COAP_MESSAGE_ACK; + pdu->code = 0x99; + pdu->tid = 0x1234; - CU_ASSERT(pdu->length == 4); + CU_ASSERT(pdu->used_size == 0); result = coap_add_option(pdu, COAP_OPTION_URI_HOST, 18, (unsigned char *)"fancyproxy.coap.me"); CU_ASSERT(result == 20); CU_ASSERT(pdu->max_delta == 3); - CU_ASSERT(pdu->length == 24); + CU_ASSERT(pdu->used_size == 20); CU_ASSERT_PTR_NULL(pdu->data); result = coap_add_option(pdu, COAP_OPTION_URI_PATH, @@ -329,25 +323,25 @@ t_encode_pdu4(void) { CU_ASSERT(result == 5); CU_ASSERT(pdu->max_delta == 11); - CU_ASSERT(pdu->length == 29); + CU_ASSERT(pdu->used_size == 25); CU_ASSERT_PTR_NULL(pdu->data); result = coap_add_option(pdu, COAP_OPTION_URI_PATH, 0, NULL); CU_ASSERT(result == 1); CU_ASSERT(pdu->max_delta == 11); - CU_ASSERT(pdu->length == 30); + CU_ASSERT(pdu->used_size == 26); CU_ASSERT_PTR_NULL(pdu->data); result = coap_add_option(pdu, 8000, 8, (unsigned char *)"fancyopt"); CU_ASSERT(result == 11); CU_ASSERT(pdu->max_delta == 8000); - CU_ASSERT(pdu->length == 41); + CU_ASSERT(pdu->used_size == 37); CU_ASSERT_PTR_NULL(pdu->data); - CU_ASSERT(pdu->length == sizeof(teststr)); - CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0); + CU_ASSERT(coap_pdu_encode_header(pdu, COAP_PROTO_UDP) == 4); + CU_ASSERT(memcmp(pdu->token - 4, teststr, sizeof(teststr)) == 0); } static void @@ -361,22 +355,22 @@ t_encode_pdu5(void) { coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ - pdu->hdr->type = COAP_MESSAGE_ACK; - pdu->hdr->code = COAP_RESPONSE_CODE(404); - pdu->hdr->id = htons(0x1234); + pdu->type = COAP_MESSAGE_ACK; + pdu->code = COAP_RESPONSE_CODE(404); + pdu->tid = 0x1234; - CU_ASSERT(pdu->length == 4); + CU_ASSERT(pdu->used_size == 0); result = coap_add_token(pdu, 8, (unsigned char *)"12345678"); - CU_ASSERT(pdu->length == 12); + CU_ASSERT(pdu->used_size == 8); result = coap_add_option(pdu, COAP_OPTION_IF_MATCH, 8, (unsigned char *)"ABCDEFGH"); CU_ASSERT(result == 9); CU_ASSERT(pdu->max_delta == 1); - CU_ASSERT(pdu->length == 21); + CU_ASSERT(pdu->used_size == 17); CU_ASSERT_PTR_NULL(pdu->data); result = coap_add_option(pdu, COAP_OPTION_ACCEPT, @@ -384,11 +378,11 @@ t_encode_pdu5(void) { CU_ASSERT(result == 3); CU_ASSERT(pdu->max_delta == 17); - CU_ASSERT(pdu->length == 24); + CU_ASSERT(pdu->used_size == 20); CU_ASSERT_PTR_NULL(pdu->data); - CU_ASSERT(pdu->length == sizeof(teststr)); - CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0); + CU_ASSERT(coap_pdu_encode_header(pdu, COAP_PROTO_UDP) == 4); + CU_ASSERT(memcmp(pdu->token - 4, teststr, sizeof(teststr)) == 0); } static void @@ -399,17 +393,18 @@ t_encode_pdu6(void) { }; coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ - pdu->hdr->type = COAP_MESSAGE_NON; - pdu->hdr->code = COAP_REQUEST_POST; - pdu->hdr->id = htons(0x1234); + pdu->type = COAP_MESSAGE_NON; + pdu->code = COAP_REQUEST_POST; + pdu->tid = 0x1234; - CU_ASSERT(pdu->length == 4); + CU_ASSERT(pdu->used_size == 0); CU_ASSERT_PTR_NULL(pdu->data); coap_add_data(pdu, 8, (unsigned char *)"12345678"); - CU_ASSERT(pdu->length == sizeof(teststr)); - CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0); + CU_ASSERT(pdu->used_size == 9); + CU_ASSERT(coap_pdu_encode_header(pdu, COAP_PROTO_UDP) == 4); + CU_ASSERT(memcmp(pdu->token - 4, teststr, sizeof(teststr)) == 0); } static void @@ -419,20 +414,20 @@ t_encode_pdu7(void) { int result; coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ - pdu->hdr->type = COAP_MESSAGE_CON; - pdu->hdr->code = COAP_RESPONSE_CODE(203); - pdu->hdr->id = htons(0x1234); + pdu->type = COAP_MESSAGE_CON; + pdu->code = COAP_RESPONSE_CODE(203); + pdu->tid = 0x1234; - CU_ASSERT(pdu->length == 4); + CU_ASSERT(pdu->used_size == 0); result = coap_add_data(pdu, 0, NULL); CU_ASSERT(result > 0); - CU_ASSERT(pdu->length == 4); + CU_ASSERT(pdu->used_size == 0); CU_ASSERT_PTR_NULL(pdu->data); - CU_ASSERT(pdu->length == sizeof(teststr)); - CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0); + CU_ASSERT(coap_pdu_encode_header(pdu, COAP_PROTO_UDP) == 4); + CU_ASSERT(memcmp(pdu->token - 4, teststr, sizeof(teststr)) == 0); } static void @@ -442,11 +437,11 @@ t_encode_pdu8(void) { int result; coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ - pdu->hdr->type = COAP_MESSAGE_CON; - pdu->hdr->code = COAP_RESPONSE_CODE(203); - pdu->hdr->id = htons(0x1234); + pdu->type = COAP_MESSAGE_CON; + pdu->code = COAP_RESPONSE_CODE(203); + pdu->tid = 0x1234; - CU_ASSERT(pdu->length == 4); + CU_ASSERT(pdu->used_size == 0); result = coap_add_token(pdu, 2, (unsigned char *)"\x00\x01"); @@ -455,11 +450,11 @@ t_encode_pdu8(void) { result = coap_add_data(pdu, 1, (unsigned char *)"\0"); CU_ASSERT(result > 0); - CU_ASSERT(pdu->length == 8); - CU_ASSERT(pdu->data == (unsigned char *)pdu->hdr + 7); + CU_ASSERT(pdu->used_size == 4); + CU_ASSERT(pdu->data == pdu->token + 3); - CU_ASSERT(pdu->length == sizeof(teststr)); - CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0); + CU_ASSERT(coap_pdu_encode_header(pdu, COAP_PROTO_UDP) == 4); + CU_ASSERT(memcmp(pdu->token - 4, teststr, sizeof(teststr)) == 0); } static void @@ -475,24 +470,24 @@ t_encode_pdu9(void) { coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ - pdu->hdr->type = COAP_MESSAGE_ACK; - pdu->hdr->code = COAP_RESPONSE_CODE(204); - pdu->hdr->id = htons(0x1234); + pdu->type = COAP_MESSAGE_ACK; + pdu->code = COAP_RESPONSE_CODE(204); + pdu->tid = 0x1234; - CU_ASSERT(pdu->length == 4); + CU_ASSERT(pdu->used_size == 0); result = coap_add_option(pdu, COAP_OPTION_ETAG, 8, (unsigned char *)"someetag"); CU_ASSERT(result == 9); CU_ASSERT(pdu->max_delta == 4); - CU_ASSERT(pdu->length == 13); + CU_ASSERT(pdu->used_size == 9); CU_ASSERT_PTR_NULL(pdu->data); result = coap_add_option(pdu, COAP_OPTION_IF_NONE_MATCH, 0, NULL); CU_ASSERT(result == 1); CU_ASSERT(pdu->max_delta == 5); - CU_ASSERT(pdu->length == 14); + CU_ASSERT(pdu->used_size == 10); CU_ASSERT_PTR_NULL(pdu->data); result = coap_add_option(pdu, COAP_OPTION_PROXY_URI, @@ -500,17 +495,17 @@ t_encode_pdu9(void) { CU_ASSERT(result == 20); CU_ASSERT(pdu->max_delta == 35); - CU_ASSERT(pdu->length == 34); + CU_ASSERT(pdu->used_size == 30); CU_ASSERT_PTR_NULL(pdu->data); result = coap_add_data(pdu, 4, (unsigned char *)"data"); CU_ASSERT(result > 0); - CU_ASSERT(pdu->length == 39); - CU_ASSERT(pdu->data == (unsigned char *)pdu->hdr + 35); + CU_ASSERT(pdu->used_size == 35); + CU_ASSERT(pdu->data == pdu->token + 31); - CU_ASSERT(pdu->length == sizeof(teststr)); - CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0); + CU_ASSERT(coap_pdu_encode_header(pdu, COAP_PROTO_UDP) == 4); + CU_ASSERT(memcmp(pdu->token - 4, teststr, sizeof(teststr)) == 0); } static void @@ -557,11 +552,11 @@ t_encode_pdu10(void) { coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ - pdu->hdr->type = COAP_MESSAGE_ACK; - pdu->hdr->code = COAP_RESPONSE_CODE(204); - pdu->hdr->id = htons(0x1234); + pdu->type = COAP_MESSAGE_ACK; + pdu->code = COAP_RESPONSE_CODE(204); + pdu->tid = 0x1234; - CU_ASSERT(pdu->length == 4); + CU_ASSERT(pdu->used_size == 0); result = coap_add_token(pdu, 2, (unsigned char *)"\0\0"); @@ -571,7 +566,7 @@ t_encode_pdu10(void) { CU_ASSERT(result == 257); CU_ASSERT(pdu->max_delta == 8); - CU_ASSERT(pdu->length == 263); + CU_ASSERT(pdu->used_size == 259); CU_ASSERT_PTR_NULL(pdu->data); result = coap_add_option(pdu, COAP_OPTION_LOCATION_PATH, 14, @@ -579,7 +574,7 @@ t_encode_pdu10(void) { CU_ASSERT(result == 16); CU_ASSERT(pdu->max_delta == 8); - CU_ASSERT(pdu->length == 279); + CU_ASSERT(pdu->used_size == 275); CU_ASSERT_PTR_NULL(pdu->data); result = coap_add_option(pdu, COAP_OPTION_LOCATION_QUERY, @@ -587,17 +582,17 @@ t_encode_pdu10(void) { CU_ASSERT(result == 2); CU_ASSERT(pdu->max_delta == 20); - CU_ASSERT(pdu->length == 281); + CU_ASSERT(pdu->used_size == 277); CU_ASSERT_PTR_NULL(pdu->data); result = coap_add_data(pdu, 4, (unsigned char *)"data"); CU_ASSERT(result > 0); - CU_ASSERT(pdu->length == 286); - CU_ASSERT(pdu->data == (unsigned char *)pdu->hdr + 282); + CU_ASSERT(pdu->used_size == 282); + CU_ASSERT(pdu->data == pdu->token + 278); - CU_ASSERT(pdu->length == sizeof(teststr)); - CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0); + CU_ASSERT(coap_pdu_encode_header(pdu, COAP_PROTO_UDP) == 4); + CU_ASSERT(memcmp(pdu->token - 4, teststr, sizeof(teststr)) == 0); } static void @@ -614,15 +609,16 @@ t_encode_pdu11(void) { result = coap_add_data(pdu, 10, (unsigned char *)"0123456789"); coap_set_log_level(level); - CU_ASSERT(result == 0); - CU_ASSERT(pdu->data == NULL); + CU_ASSERT(result == 1); + CU_ASSERT(pdu->data != NULL); + CU_ASSERT(pdu->used_size == 11); pdu->max_size = old_max; } static int t_pdu_tests_create(void) { - pdu = coap_pdu_init(0, 0, 0, COAP_DEFAULT_PDU_SIZE); + pdu = coap_pdu_init(0, 0, 0, 0); return pdu == NULL; } diff --git a/tests/test_uri.c b/tests/test_uri.c index c5069718..2dfa58f0 100644 --- a/tests/test_uri.c +++ b/tests/test_uri.c @@ -325,7 +325,7 @@ t_parse_uri12(void) { static void t_parse_uri13(void) { uint8_t teststr[] ALIGNED(8) = { - 0x00, 0x00, 0x00, 0x00, 0x80, 0x03, 'f', 'o', + 0x80, 0x03, 'f', 'o', 'o', 0x3b, '.', 'w', 'e', 'l', 'l', '-', 'k', 'n', 'o', 'w', 'n', 0x04, 'c', 'o', 'r', 'e' @@ -333,8 +333,9 @@ t_parse_uri13(void) { coap_pdu_t pdu = { .max_size = sizeof(teststr), - .hdr = (coap_hdr_t *)teststr, - .length = sizeof(teststr) + .token_length = 0, + .token = teststr, + .used_size = sizeof(teststr) }; coap_key_t key; diff --git a/tests/test_wellknown.c b/tests/test_wellknown.c index a4936267..9afc3f72 100644 --- a/tests/test_wellknown.c +++ b/tests/test_wellknown.c @@ -134,12 +134,12 @@ t_wellknown3(void) { coap_print_status_t result; int j; coap_resource_t *r; - static char uris[2 * COAP_DEFAULT_PDU_SIZE]; + static char uris[2 * 1024]; unsigned char *uribuf = (unsigned char *)uris; unsigned char buf[40]; size_t buflen = sizeof(buf); size_t offset; - const unsigned short num_resources = (sizeof(uris) / TEST_URI_LEN) - 1; + const uint16_t num_resources = (sizeof(uris) / TEST_URI_LEN) - 1; /* , (TEST_URI_LEN + 4 chars) */ for (j = 0; j < num_resources; j++) { @@ -175,7 +175,7 @@ t_wellknown4(void) { CU_ASSERT(block.num == 0); CU_ASSERT(block.m == 1); CU_ASSERT(1 << (block.szx + 4) - == (unsigned char *)response->hdr + response->length - response->data); + == response->token + response->used_size - response->data); coap_delete_pdu(response); } @@ -207,7 +207,7 @@ t_wellknown5(void) { CU_ASSERT(block.num == inblock.num); CU_ASSERT(block.m == 1); CU_ASSERT(1 << (block.szx + 4) - == (unsigned char *)response->hdr + response->length - response->data); + == response->token + response->used_size - response->data); coap_delete_pdu(response); } @@ -222,9 +222,9 @@ t_wellknown6(void) { do { coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ - pdu->hdr->type = COAP_MESSAGE_NON; - pdu->hdr->code = COAP_REQUEST_GET; - pdu->hdr->id = htons(0x1234); + pdu->type = COAP_MESSAGE_NON; + pdu->code = COAP_REQUEST_GET; + pdu->tid = 0x1234; CU_ASSERT_PTR_NOT_NULL(pdu); @@ -269,7 +269,7 @@ t_wkc_tests_create(void) { /* add resources to coap context */ if (ctx && pdu) { coap_resource_t *r; - static char _buf[2 * COAP_DEFAULT_PDU_SIZE]; + static char _buf[2 * 1024]; unsigned char *buf = (unsigned char *)_buf; int i; diff --git a/win32/testdriver/testdriver.vcxproj.user b/win32/testdriver/testdriver.vcxproj.user index b496dc39..3ab94306 100644 --- a/win32/testdriver/testdriver.vcxproj.user +++ b/win32/testdriver/testdriver.vcxproj.user @@ -1,25 +1,23 @@  - - + PATH=..\lib WindowsLocalDebugger - $(ProjectDir)..\Cunit + $(ProjectDir) - + PATH=..\lib WindowsLocalDebugger - $(ProjectDir)..\Cunit + $(ProjectDir) - - + PATH=..\lib WindowsLocalDebugger - $(ProjectDir)..\Cunit + $(ProjectDir) - + PATH=..\lib WindowsLocalDebugger - $(ProjectDir)..\Cunit + $(ProjectDir) \ No newline at end of file