Merge pull request #639 from mrdeep1/obf_pdu_t

coap_pdu_t: Obfuscate coap_pdu_t for applications
This commit is contained in:
obgm authored and GitHub committed 2021-04-20 15:22:08 +02:00
commit 04c641f7f0
42 files changed
+1456 -749

No files matched your search

+2 -8
View File
@@ -31,8 +31,9 @@ EXTRA_DIST = \
include/coap$(LIBCOAP_API_VERSION)/coap_async_internal.h \
include/coap$(LIBCOAP_API_VERSION)/coap_block_internal.h \
include/coap$(LIBCOAP_API_VERSION)/coap_cache_internal.h \
include/coap$(LIBCOAP_API_VERSION)/coap_net_internal.h \
include/coap$(LIBCOAP_API_VERSION)/coap_io_internal.h \
include/coap$(LIBCOAP_API_VERSION)/coap_net_internal.h \
include/coap$(LIBCOAP_API_VERSION)/coap_pdu_internal.h \
include/coap$(LIBCOAP_API_VERSION)/coap_resource_internal.h \
include/coap$(LIBCOAP_API_VERSION)/coap_session_internal.h \
include/coap$(LIBCOAP_API_VERSION)/coap_subscribe_internal.h \
@@ -165,8 +166,6 @@ libcoap_@LIBCOAP_NAME_SUFFIX@_la_LDFLAGS = \
## Collect symbols here we want to ignore while building the helper files
## libcoap-$(LIBCOAP_API_VERSION).{map,sym} for the linker.
CTAGS_IGNORE=-I " \
coap_delete_all_async \
coap_delete_all \
coap_dtls_context_check_keys_enabled \
coap_dtls_context_set_pki \
coap_dtls_context_set_pki_root_cas \
@@ -188,17 +187,12 @@ coap_dtls_send \
coap_dtls_session_update_mtu \
coap_dtls_shutdown \
coap_dtls_startup \
coap_insert_option \
coap_pdu_check_resize \
coap_pdu_from_pbuf \
coap_remove_option \
coap_tls_free_session \
coap_tls_new_client_session \
coap_tls_new_server_session \
coap_tls_read \
coap_tls_write \
coap_update_option \
coap_update_token \
"
# This helper is called by libcoap-$(LIBCOAP_API_VERSION).{map,sym} to see if
+1
View File
@@ -844,6 +844,7 @@ man/coap_io.txt
man/coap_keepalive.txt
man/coap_logging.txt
man/coap_observe.txt
man/coap_pdu_access.txt
man/coap_pdu_setup.txt
man/coap_recovery.txt
man/coap_resource.txt
+15 -16
View File
@@ -193,13 +193,9 @@ coap_new_request(coap_context_t *ctx,
coap_pdu_t *pdu;
(void)ctx;
if (!(pdu = coap_new_pdu(session)))
if (!(pdu = coap_new_pdu(msgtype, m, session)))
return NULL;
pdu->type = msgtype;
pdu->mid = coap_new_message_id(session);
pdu->code = m;
if ( !coap_add_token(pdu, the_token.length, the_token.s)) {
coap_log(LOG_DEBUG, "cannot add token to request\n");
}
@@ -266,8 +262,10 @@ resolve_address(const coap_str_const_t *server, struct sockaddr *dst) {
static inline int
check_token(coap_pdu_t *received) {
return received->token_length == the_token.length &&
memcmp(received->token, the_token.s, the_token.length) == 0;
coap_bin_const_t token = coap_pdu_get_token(received);
return token.length == the_token.length &&
memcmp(token.s, the_token.s, the_token.length) == 0;
}
static int
@@ -324,30 +322,32 @@ message_handler(coap_context_t *ctx COAP_UNUSED,
const uint8_t *databuf;
size_t offset;
size_t total;
coap_pdu_code_t rcv_code = coap_pdu_get_code(received);
coap_pdu_type_t rcv_type = coap_pdu_get_type(received);
coap_log(LOG_DEBUG, "** process incoming %d.%02d response:\n",
COAP_RESPONSE_CLASS(received->code), received->code & 0x1F);
COAP_RESPONSE_CLASS(rcv_code), rcv_code & 0x1F);
if (coap_get_log_level() < LOG_DEBUG)
coap_show_pdu(LOG_INFO, received);
/* 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->type == COAP_MESSAGE_CON ||
received->type == COAP_MESSAGE_NON)) {
if (!sent && (rcv_type == COAP_MESSAGE_CON ||
rcv_type == COAP_MESSAGE_NON)) {
/* Cause a CoAP RST to be sent */
return COAP_RESPONSE_FAIL;
}
return COAP_RESPONSE_OK;
}
if (received->type == COAP_MESSAGE_RST) {
if (rcv_type == COAP_MESSAGE_RST) {
coap_log(LOG_INFO, "got RST\n");
return COAP_RESPONSE_OK;
}
/* output the received data, if any */
if (COAP_RESPONSE_CLASS(received->code) == 2) {
if (COAP_RESPONSE_CLASS(rcv_code) == 2) {
/* set obs timer if we have successfully subscribed a resource */
if (doing_observe && !obs_started &&
@@ -387,11 +387,10 @@ message_handler(coap_context_t *ctx COAP_UNUSED,
return COAP_RESPONSE_OK;
}
} else { /* no 2.05 */
/* check if an error was signaled and output payload if so */
if (COAP_RESPONSE_CLASS(received->code) >= 4) {
fprintf(stderr, "%d.%02d",
(received->code >> 5), received->code & 0x1F);
if (COAP_RESPONSE_CLASS(rcv_code) >= 4) {
fprintf(stderr, "%d.%02d", COAP_RESPONSE_CLASS(rcv_code),
rcv_code & 0x1F);
if (coap_get_data_large(received, &len, &databuf, &offset, &total)) {
fprintf(stderr, " ");
while(len--) {
+9 -9
View File
@@ -125,7 +125,7 @@ hnd_get_resource(coap_context_t *ctx COAP_UNUSED,
if (uri_path)
HASH_FIND(hh, resources, uri_path->s, uri_path->length, rd);
response->code = COAP_RESPONSE_CODE_CONTENT;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);
coap_add_option(response,
COAP_OPTION_CONTENT_TYPE,
@@ -149,7 +149,7 @@ hnd_put_resource(coap_context_t *ctx COAP_UNUSED,
coap_string_t *query COAP_UNUSED,
coap_pdu_t *response) {
#if 1
response->code = COAP_RESPONSE_CODE_NOT_IMPLEMENTED;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_IMPLEMENTED);
#else /* FIXME */
coap_opt_iterator_t opt_iter;
coap_opt_t *token, *etag;
@@ -159,7 +159,7 @@ hnd_put_resource(coap_context_t *ctx COAP_UNUSED,
? COAP_MESSAGE_ACK : COAP_MESSAGE_NON;
rd_t *rd = NULL;
unsigned char code; /* result code */
unsigned char *data;
const uint8_t *data;
coap_string_t tmp;
HASH_FIND(hh, resources, resource->uri_path.s, resource->uri_path.length, rd);
@@ -240,7 +240,7 @@ hnd_delete_resource(coap_context_t *ctx,
* using coap_malloc() and must be released. */
coap_delete_resource(ctx, resource);
response->code = COAP_RESPONSE_CODE_DELETED;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_DELETED);
}
static void
@@ -253,7 +253,7 @@ hnd_get_rd(coap_context_t *ctx COAP_UNUSED,
coap_pdu_t *response) {
unsigned char buf[3];
response->code = COAP_RESPONSE_CODE_CONTENT;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);
coap_add_option(response,
COAP_OPTION_CONTENT_TYPE,
@@ -378,7 +378,7 @@ add_source_address(coap_resource_t *resource,
static rd_t *
make_rd(coap_pdu_t *pdu) {
rd_t *rd;
unsigned char *data;
const uint8_t *data;
coap_opt_iterator_t opt_iter;
coap_opt_t *etag;
@@ -427,7 +427,7 @@ hnd_post_rd(coap_context_t *ctx,
loc = (unsigned char *)coap_malloc(LOCSIZE);
if (!loc) {
response->code = COAP_RESPONSE_CODE_INTERNAL_ERROR;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_INTERNAL_ERROR);
return;
}
memcpy(loc, RD_ROOT_STR, RD_ROOT_SIZE);
@@ -457,7 +457,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->mid);
"%x", coap_pdu_get_mid(request));
if (loc_size > 1) {
if (ins.length) {
@@ -540,7 +540,7 @@ hnd_post_rd(coap_context_t *ctx,
/* create response */
response->code = COAP_RESPONSE_CODE_CREATED;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CREATED);
{ /* split path into segments and add Location-Path options */
unsigned char _b[LOCSIZE];
+60 -55
View File
@@ -230,7 +230,7 @@ hnd_get_index(coap_context_t *ctx COAP_UNUSED,
coap_string_t *query COAP_UNUSED,
coap_pdu_t *response) {
response->code = COAP_RESPONSE_CODE_CONTENT;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);
coap_add_data_large_response(resource, session, request, response,
token, query, COAP_MEDIATYPE_TEXT_PLAIN,
0x2ffff, 0, strlen(INDEX),
@@ -267,14 +267,14 @@ hnd_get_time(coap_context_t *ctx COAP_UNUSED,
tmp = gmtime(&now);
if (!tmp) {
/* If 'now' is not valid */
response->code = COAP_RESPONSE_CODE_NOT_FOUND;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_FOUND);
return;
}
else {
len = strftime((char *)buf, sizeof(buf), "%b %d %H:%M:%S", tmp);
}
}
response->code = COAP_RESPONSE_CODE_CONTENT;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);
coap_add_data_large_response(resource, session, request, response, token,
query, COAP_MEDIATYPE_TEXT_PLAIN, 1, 0,
len,
@@ -282,7 +282,7 @@ hnd_get_time(coap_context_t *ctx COAP_UNUSED,
}
else {
/* if my_clock_base was deleted, we pretend to have no such resource */
response->code = COAP_RESPONSE_CODE_NOT_FOUND;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_FOUND);
}
}
@@ -296,7 +296,7 @@ hnd_put_time(coap_context_t *ctx COAP_UNUSED,
coap_pdu_t *response) {
coap_tick_t t;
size_t size;
unsigned char *data;
const uint8_t *data;
/* FIXME: re-set my_clock_base to clock_offset if my_clock_base == 0
* and request is empty. When not empty, set to value in request payload
@@ -304,8 +304,8 @@ hnd_put_time(coap_context_t *ctx COAP_UNUSED,
*/
/* if my_clock_base was deleted, we pretend to have no such resource */
response->code =
my_clock_base ? COAP_RESPONSE_CODE_CHANGED : COAP_RESPONSE_CODE_CREATED;
coap_pdu_set_code(response, my_clock_base ? COAP_RESPONSE_CODE_CHANGED :
COAP_RESPONSE_CODE_CREATED);
coap_resource_notify_observers(resource, NULL);
@@ -324,7 +324,7 @@ hnd_put_time(coap_context_t *ctx COAP_UNUSED,
/* Sanity check input value */
if (!gmtime(&my_clock_base)) {
unsigned char buf[3];
response->code = COAP_RESPONSE_CODE_BAD_REQUEST;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_BAD_REQUEST);
coap_add_option(response,
COAP_OPTION_CONTENT_FORMAT,
coap_encode_var_safe(buf, sizeof(buf),
@@ -381,7 +381,7 @@ hnd_get_async(coap_context_t *context,
delay = delay * 10 + (*p - '0');
if (delay == 0) {
coap_log(LOG_INFO, "async: delay of 0 not supported\n");
response->code = COAP_RESPONSE_CODE_BAD_REQUEST;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_BAD_REQUEST);
return;
}
}
@@ -390,7 +390,7 @@ hnd_get_async(coap_context_t *context,
request,
COAP_TICKS_PER_SECOND * delay);
if (async == NULL) {
response->code = COAP_RESPONSE_CODE_SERVICE_UNAVAILABLE;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_SERVICE_UNAVAILABLE);
return;
}
/*
@@ -405,7 +405,7 @@ hnd_get_async(coap_context_t *context,
coap_add_data_large_response(resource, session, request, response, token,
query, COAP_MEDIATYPE_TEXT_PLAIN, -1, 0, 4,
(const uint8_t *)"done", NULL, NULL);
response->code = COAP_RESPONSE_CODE_CONTENT;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);
/* async is automatically removed by libcoap on return from this handler */
}
@@ -441,7 +441,7 @@ hnd_get_example_data(coap_context_t *ctx COAP_UNUSED,
}
example_data_value = alloc_resource_data(value);
}
response->code = COAP_RESPONSE_CODE_CONTENT;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);
body = reference_resource_data(example_data_value);
coap_add_data_large_response(resource, session, request, response, token,
query, example_data_media_type, -1, 0,
@@ -520,7 +520,7 @@ hnd_put_example_data(coap_context_t *ctx COAP_UNUSED,
coap_log(LOG_WARNING,
"No cache entry available for the non-first BLOCK\n");
}
response->code = COAP_RESPONSE_CODE_INTERNAL_ERROR;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_INTERNAL_ERROR);
return;
}
@@ -539,7 +539,7 @@ hnd_put_example_data(coap_context_t *ctx COAP_UNUSED,
}
else {
/* Give us the next block response */
response->code = COAP_RESPONSE_CODE_CONTINUE;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTINUE);
return;
}
}
@@ -553,17 +553,17 @@ hnd_put_example_data(coap_context_t *ctx COAP_UNUSED,
if (example_data_value) {
/* pre-existed response */
response->code = COAP_RESPONSE_CODE_CHANGED;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CHANGED);
/* Need to de-reference as value may be in use elsewhere */
release_resource_data(session, example_data_value);
}
else
/* just generated response */
response->code = COAP_RESPONSE_CODE_CREATED;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CREATED);
example_data_value = alloc_resource_data(data_so_far);
if (!example_data_value) {
response->code = COAP_RESPONSE_CODE_INTERNAL_ERROR;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_INTERNAL_ERROR);
return;
}
if ((option = coap_check_option(request, COAP_OPTION_CONTENT_FORMAT,
@@ -790,7 +790,7 @@ get_proxy_session(coap_session_t *session, coap_pdu_t *response,
new_proxy_list = realloc(proxy_list, (i+1)*sizeof(proxy_list[0]));
if (new_proxy_list == NULL) {
response->code = COAP_RESPONSE_CODE_INTERNAL_ERROR;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_INTERNAL_ERROR);
return NULL;
}
proxy_list = new_proxy_list;
@@ -798,7 +798,7 @@ get_proxy_session(coap_session_t *session, coap_pdu_t *response,
if (token) {
proxy_list[i].token = coap_new_binary(token->length);
if (!proxy_list[i].token) {
response->code = COAP_RESPONSE_CODE_INTERNAL_ERROR;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_INTERNAL_ERROR);
return NULL;
}
memcpy(proxy_list[i].token->s, token->s, token->length);
@@ -809,7 +809,7 @@ get_proxy_session(coap_session_t *session, coap_pdu_t *response,
if (query) {
proxy_list[i].query = coap_new_string(query->length);
if (!proxy_list[i].query) {
response->code = COAP_RESPONSE_CODE_INTERNAL_ERROR;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_INTERNAL_ERROR);
return NULL;
}
memcpy(proxy_list[i].query->s, query->s, query->length);
@@ -907,7 +907,7 @@ get_ongoing_proxy_session(coap_context_t *ctx, coap_session_t *session,
scheme = uri->scheme;
}
if (resolve_address(&server, &dst.addr.sa) < 0) {
response->code = COAP_RESPONSE_CODE_BAD_GATEWAY;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_BAD_GATEWAY);
remove_proxy_association(session, 0);
return NULL;
}
@@ -966,7 +966,7 @@ get_ongoing_proxy_session(coap_context_t *ctx, coap_session_t *session,
break;
}
if (new_proxy_list->ongoing == NULL) {
response->code = COAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED);
remove_proxy_association(session, 0);
return NULL;
}
@@ -1019,7 +1019,8 @@ hnd_proxy_uri(coap_context_t *ctx,
if (opt) {
if (!get_uri_proxy_scheme_info(request, opt, &uri, &uri_path,
&uri_query)) {
response->code = COAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED;
coap_pdu_set_code(response,
COAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED);
goto cleanup;
}
proxy_scheme_option = 1;
@@ -1037,24 +1038,25 @@ hnd_proxy_uri(coap_context_t *ctx,
&uri) < 0) {
/* Need to return a 5.05 RFC7252 Section 5.7.2 */
coap_log(LOG_WARNING, "Proxy URI not decodable\n");
response->code = COAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED;
coap_pdu_set_code(response,
COAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED);
goto cleanup;
}
}
if (!(proxy_scheme_option || proxy_uri)) {
response->code = COAP_RESPONSE_CODE_NOT_FOUND;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_FOUND);
goto cleanup;
}
if (uri.host.length == 0) {
/* Ongoing connection not well formed */
response->code = COAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED);
goto cleanup;
}
if (!verify_proxy_scheme_supported(uri.scheme)) {
response->code = COAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED);
goto cleanup;
}
@@ -1063,6 +1065,7 @@ hnd_proxy_uri(coap_context_t *ctx,
uri.scheme == COAP_URI_SCHEME_COAPS ||
uri.scheme == COAP_URI_SCHEME_COAP_TCP ||
uri.scheme == COAP_URI_SCHEME_COAPS_TCP) {
coap_pdu_code_t req_code = coap_pdu_get_code(request);
tmp_proxy_list = get_proxy_session(session, response, token, query);
if (!tmp_proxy_list)
@@ -1081,7 +1084,7 @@ hnd_proxy_uri(coap_context_t *ctx,
goto cleanup;
}
if (offset + size != total) {
response->code = COAP_RESPONSE_CODE_CONTINUE;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTINUE);
goto cleanup;
}
data = body_data->s;
@@ -1096,17 +1099,17 @@ hnd_proxy_uri(coap_context_t *ctx,
/*
* Build up the ongoing PDU that we are going to send
*/
pdu = coap_pdu_init(request->type, request->code,
pdu = coap_pdu_init(coap_pdu_get_type(request), req_code,
coap_new_message_id(ongoing),
coap_session_max_pdu_size(session));
if (!pdu) {
response->code = COAP_RESPONSE_CODE_INTERNAL_ERROR;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_INTERNAL_ERROR);
goto cleanup;
}
if (token && !coap_add_token(pdu, token->length, token->s)) {
coap_log(LOG_DEBUG, "cannot add token to proxy request\n");
response->code = COAP_RESPONSE_CODE_INTERNAL_ERROR;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_INTERNAL_ERROR);
coap_delete_pdu(pdu);
goto cleanup;
}
@@ -1164,7 +1167,7 @@ hnd_proxy_uri(coap_context_t *ctx,
/* Copy the remaining options across */
coap_option_iterator_init(request, &opt_iter, COAP_OPT_ALL);
while ((option = coap_option_next(&opt_iter))) {
switch (opt_iter.type) {
switch (opt_iter.number) {
case COAP_OPTION_PROXY_URI:
case COAP_OPTION_PROXY_SCHEME:
case COAP_OPTION_URI_PATH:
@@ -1178,7 +1181,7 @@ hnd_proxy_uri(coap_context_t *ctx,
break;
default:
coap_insert_optlist(&optlist,
coap_new_optlist(opt_iter.type,
coap_new_optlist(opt_iter.number,
coap_opt_length(option),
coap_opt_value(option)));
break;
@@ -1245,7 +1248,7 @@ hnd_delete(coap_context_t *ctx,
/* get the uri_path */
uri_path = coap_get_uri_path(request);
if (!uri_path) {
response->code = COAP_RESPONSE_CODE_NOT_FOUND;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_FOUND);
return;
}
@@ -1267,7 +1270,7 @@ hnd_delete(coap_context_t *ctx,
/* Dynamic resource no longer required - delete it */
coap_delete_resource(ctx, resource);
coap_delete_string(uri_path);
response->code = COAP_RESPONSE_CODE_DELETED;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_DELETED);
}
/*
@@ -1296,7 +1299,7 @@ hnd_get(coap_context_t *ctx COAP_UNUSED,
uri_path = coap_resource_get_uri_path(resource);
if (!uri_path) {
response->code = COAP_RESPONSE_CODE_NOT_FOUND;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_FOUND);
return;
}
@@ -1306,13 +1309,13 @@ hnd_get(coap_context_t *ctx COAP_UNUSED,
}
}
if (i == dynamic_count) {
response->code = COAP_RESPONSE_CODE_NOT_FOUND;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_FOUND);
return;
}
resource_entry = &dynamic_entry[i];
response->code = COAP_RESPONSE_CODE_CONTENT;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);
body = reference_resource_data(resource_entry->value);
coap_add_data_large_response(resource, session, request, response, token,
query, resource_entry->media_type, -1, 0,
@@ -1351,7 +1354,7 @@ hnd_put(coap_context_t *ctx COAP_UNUSED,
/* get the uri_path */
uri_path = coap_get_uri_path(request);
if (!uri_path) {
response->code = COAP_RESPONSE_CODE_NOT_FOUND;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_FOUND);
return;
}
@@ -1366,7 +1369,7 @@ hnd_put(coap_context_t *ctx COAP_UNUSED,
if (i == dynamic_count) {
if (dynamic_count >= support_dynamic) {
/* Should have been caught in hnd_unknown_put() */
response->code = COAP_RESPONSE_CODE_NOT_ACCEPTABLE;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_ACCEPTABLE);
coap_delete_string(uri_path);
return;
}
@@ -1378,7 +1381,7 @@ hnd_put(coap_context_t *ctx COAP_UNUSED,
dynamic_entry[i].value = NULL;
dynamic_entry[i].resource = resource;
dynamic_entry[i].created = 1;
response->code = COAP_RESPONSE_CODE_CREATED;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CREATED);
if ((option = coap_check_option(request, COAP_OPTION_CONTENT_FORMAT,
&opt_iter)) != NULL) {
dynamic_entry[i].media_type =
@@ -1401,14 +1404,14 @@ hnd_put(coap_context_t *ctx COAP_UNUSED,
0);
} else {
dynamic_count--;
response->code = COAP_RESPONSE_CODE_INTERNAL_ERROR;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_INTERNAL_ERROR);
coap_delete_string(uri_path);
return;
}
} else {
/* Need to do this as coap_get_uri_path() created it */
coap_delete_string(uri_path);
response->code = COAP_RESPONSE_CODE_CHANGED;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CHANGED);
}
resource_entry = &dynamic_entry[i];
@@ -1451,7 +1454,7 @@ hnd_put(coap_context_t *ctx COAP_UNUSED,
coap_log(LOG_WARNING,
"No cache entry available for the non-first BLOCK\n");
}
response->code = COAP_RESPONSE_CODE_INTERNAL_ERROR;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_INTERNAL_ERROR);
return;
}
@@ -1486,7 +1489,7 @@ hnd_put(coap_context_t *ctx COAP_UNUSED,
coap_cache_set_app_data(cache_entry, NULL, NULL);
}
else {
response->code = COAP_RESPONSE_CODE_CONTINUE;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTINUE);
return;
}
}
@@ -1501,17 +1504,17 @@ hnd_put(coap_context_t *ctx COAP_UNUSED,
release_resource_data(session, resource_entry->value);
transient_value = alloc_resource_data(data_so_far);
if (!transient_value) {
response->code = COAP_RESPONSE_CODE_INTERNAL_ERROR;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_INTERNAL_ERROR);
return;
}
resource_entry->value = transient_value;
if (resource_entry->created) {
resource_entry->created = 0;
response->code = COAP_RESPONSE_CODE_CREATED;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CREATED);
}
else {
response->code = COAP_RESPONSE_CODE_CHANGED;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CHANGED);
coap_resource_notify_observers(resource_entry->resource, NULL);
}
@@ -1545,14 +1548,14 @@ hnd_unknown_put(coap_context_t *ctx,
/* check if creating a new resource is allowed */
if (dynamic_count >= support_dynamic) {
response->code = COAP_RESPONSE_CODE_NOT_ACCEPTABLE;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_ACCEPTABLE);
return;
}
/* get the uri_path - will get used by coap_resource_init() */
uri_path = coap_get_uri_path(request);
if (!uri_path) {
response->code = COAP_RESPONSE_CODE_NOT_FOUND;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_FOUND);
return;
}
@@ -1614,6 +1617,8 @@ proxy_message_handler(coap_context_t *ctx COAP_UNUSED,
uint16_t media_type = COAP_MEDIATYPE_TEXT_PLAIN;
int maxage = -1;
uint64_t etag = 0;
coap_pdu_code_t rcv_code = coap_pdu_get_code(received);
coap_bin_const_t rcv_token = coap_pdu_get_token(received);
for (i = 0; i < proxy_list_count; i++) {
if (proxy_list[i].ongoing == session) {
@@ -1628,7 +1633,7 @@ proxy_message_handler(coap_context_t *ctx COAP_UNUSED,
}
coap_log(LOG_DEBUG, "** process upstream incoming %d.%02d response:\n",
COAP_RESPONSE_CLASS(received->code), received->code & 0x1F);
COAP_RESPONSE_CLASS(rcv_code), rcv_code & 0x1F);
if (coap_get_log_level() < LOG_DEBUG)
coap_show_pdu(LOG_INFO, received);
@@ -1653,7 +1658,7 @@ proxy_message_handler(coap_context_t *ctx COAP_UNUSED,
/*
* Build up the ongoing PDU that we are going to send to proxy originator
*/
pdu = coap_pdu_init(received->type, received->code,
pdu = coap_pdu_init(coap_pdu_get_type(received), rcv_code,
coap_new_message_id(incoming),
coap_session_max_pdu_size(incoming));
if (!pdu) {
@@ -1661,7 +1666,7 @@ proxy_message_handler(coap_context_t *ctx COAP_UNUSED,
return COAP_RESPONSE_OK;
}
if (!coap_add_token(pdu, received->token_length, received->token)) {
if (!coap_add_token(pdu, rcv_token.length, rcv_token.s)) {
coap_log(LOG_DEBUG, "cannot add token to ongoing proxy response PDU\n");
}
@@ -1671,7 +1676,7 @@ proxy_message_handler(coap_context_t *ctx COAP_UNUSED,
*/
coap_option_iterator_init(received, &opt_iter, COAP_OPT_ALL);
while ((option = coap_option_next(&opt_iter))) {
switch (opt_iter.type) {
switch (opt_iter.number) {
case COAP_OPTION_CONTENT_FORMAT:
media_type = coap_decode_var_bytes(coap_opt_value (option),
coap_opt_length (option));
@@ -1689,7 +1694,7 @@ proxy_message_handler(coap_context_t *ctx COAP_UNUSED,
break;
default:
coap_insert_optlist(&optlist,
coap_new_optlist(opt_iter.type,
coap_new_optlist(opt_iter.number,
coap_opt_length(option),
coap_opt_value(option)));
break;
+2 -2
View File
@@ -118,7 +118,7 @@ hnd_get_time(coap_context_t *ctx, struct coap_resource_t *resource,
tmp = gmtime(&tnow);
if (!tmp) {
/* If 'tnow' is not valid */
response->code = COAP_RESPONSE_CODE_NOT_FOUND;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_FOUND);
return;
}
else {
@@ -132,7 +132,7 @@ hnd_get_time(coap_context_t *ctx, struct coap_resource_t *resource,
}
else {
/* if my_clock_base was deleted, we pretend to have no such resource */
response->code = COAP_RESPONSE_CODE_NOT_FOUND;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_FOUND);
}
}
+14 -14
View File
@@ -99,7 +99,7 @@ hnd_get_index(coap_context_t *ctx COAP_UNUSED,
coap_pdu_t *response) {
unsigned char buf[3];
response->code = COAP_RESPONSE_CODE_CONTENT;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);
coap_add_option(response, COAP_OPTION_CONTENT_TYPE,
coap_encode_var_safe(buf, sizeof(buf),
@@ -125,12 +125,12 @@ hnd_get_resource(coap_context_t *ctx COAP_UNUSED,
test_payload = coap_find_payload(resource);
if (!test_payload) {
response->code = COAP_RESPONSE_CODE_INTERNAL_ERROR;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_INTERNAL_ERROR);
return;
}
response->code = COAP_RESPONSE_CODE_CONTENT;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);
coap_add_data_blocked_response(resource, session, request, response, token,
test_payload->media_type, -1,
@@ -157,7 +157,7 @@ hnd_delete_resource(coap_context_t *ctx,
coap_delete_resource(ctx, resource);
response->code = COAP_RESPONSE_CODE_DELETED;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_DELETED);
}
static void
@@ -173,7 +173,7 @@ hnd_post_test(coap_context_t *ctx,
coap_payload_t *test_payload;
size_t len;
coap_str_const_t *uri;
unsigned char *data;
const uint8_t *data;
#define BUFSIZE 20
int res;
@@ -189,7 +189,7 @@ hnd_post_test(coap_context_t *ctx,
uri = coap_new_str_const(buf, strlen((char *)buf));
if (!(test_payload && uri)) {
coap_log(LOG_CRIT, "cannot allocate new resource under /test");
response->code = COAP_RESPONSE_CODE_INTERNAL_ERROR;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_INTERNAL_ERROR);
coap_free(test_payload);
coap_free(uri);
} else {
@@ -223,7 +223,7 @@ hnd_post_test(coap_context_t *ctx,
buf += coap_opt_size(buf);
}
response->code = COAP_RESPONSE_CODE_CREATED;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CREATED);
}
}
@@ -240,9 +240,9 @@ hnd_put_test(coap_context_t *ctx COAP_UNUSED,
coap_opt_t *option;
coap_payload_t *payload;
size_t len;
unsigned char *data;
const uint8_t *data;
response->code = COAP_RESPONSE_CODE_CHANGED;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CHANGED);
coap_get_data(request, &len, &data);
@@ -280,7 +280,7 @@ hnd_put_test(coap_context_t *ctx COAP_UNUSED,
return;
error:
coap_log(LOG_WARNING, "cannot modify resource\n");
response->code = COAP_RESPONSE_CODE_INTERNAL_ERROR;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_INTERNAL_ERROR);
}
static void
@@ -300,7 +300,7 @@ hnd_delete_test(coap_context_t *ctx COAP_UNUSED,
payload->length = 0;
#endif
response->code = COAP_RESPONSE_CODE_DELETED;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_DELETED);
}
static void
@@ -317,7 +317,7 @@ hnd_get_query(coap_context_t *ctx COAP_UNUSED,
size_t len, L;
unsigned char buf[70];
response->code = COAP_RESPONSE_CODE_CONTENT;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);
coap_add_option(response, COAP_OPTION_CONTENT_TYPE,
coap_encode_var_safe(buf, sizeof(buf),
@@ -393,7 +393,7 @@ hnd_get_separate(coap_context_t *ctx,
request,
COAP_TICKS_PER_SECOND * delay);
if (async == NULL) {
response->code = COAP_RESPONSE_CODE_SERVICE_UNAVAILABLE;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_SERVICE_UNAVAILABLE);
return;
}
/* Not setting response code will cause empty ACK to be sent
@@ -403,7 +403,7 @@ hnd_get_separate(coap_context_t *ctx,
/* This is the delayed response. Send back the appropriate data */
coap_add_data(response, 4, (const uint8_t *)"done");
response->code = COAP_RESPONSE_CODE_CONTENT;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);
/* async is automatically removed by libcoap */
}
+2 -5
View File
@@ -56,13 +56,10 @@ make_pdu( unsigned int value ) {
static unsigned char buf[20];
int len, ls;
if (!(pdu = coap_pdu_init(0, 0, 0, COAP_DEFAULT_MTU)))
if (!(pdu = coap_pdu_init(COAP_MESSAGE_NON, COAP_REQUEST_POST, id++,
COAP_DEFAULT_MTU)))
return NULL;
pdu->type = COAP_MESSAGE_NON;
pdu->code = COAP_REQUEST_POST;
pdu->mid = id++;
enc = COAP_PSEUDOFP_ENCODE_8_4_DOWN(value, &ls);
len = sprintf((char *)buf, "%c%u", enc, COAP_PSEUDOFP_DECODE_8_4(enc));
+13 -12
View File
@@ -81,22 +81,23 @@ coap_opt_block_set_m(coap_opt_t *block_opt, int m) {
#endif
/**
* Initializes @p block from @p pdu. @p type must be either COAP_OPTION_BLOCK1
* or COAP_OPTION_BLOCK2. When option @p type was found in @p pdu, @p block is
* Initializes @p block from @p pdu. @p number must be either COAP_OPTION_BLOCK1
* or COAP_OPTION_BLOCK2. When option @p number was found in @p pdu, @p block is
* initialized with values from this option and the function returns the value
* @c 1. Otherwise, @c 0 is returned.
*
* @param pdu The pdu to search for option @p type.
* @param type The option to search for (must be COAP_OPTION_BLOCK1 or
* COAP_OPTION_BLOCK2).
* @param block The block structure to initilize.
* @param pdu The pdu to search for option @p number.
* @param number The option number to search for (must be COAP_OPTION_BLOCK1 or
* COAP_OPTION_BLOCK2).
* @param block The block structure to initilize.
*
* @return @c 1 on success, @c 0 otherwise.
* @return @c 1 on success, @c 0 otherwise.
*/
int coap_get_block(coap_pdu_t *pdu, uint16_t type, coap_block_t *block);
int coap_get_block(coap_pdu_t *pdu, coap_option_num_t number,
coap_block_t *block);
/**
* Writes a block option of type @p type to message @p pdu. If the requested
* Writes a block option of type @p number to message @p pdu. If the requested
* block size is too large to fit in @p pdu, it is reduced accordingly. An
* exception is made for the final block when less space is required. The actual
* length of the resource is specified in @p data_length.
@@ -108,7 +109,7 @@ int coap_get_block(coap_pdu_t *pdu, uint16_t type, coap_block_t *block);
* @param block The block structure to use. On return, this object is
* updated according to the values that have been written to
* @p pdu.
* @param type COAP_OPTION_BLOCK1 or COAP_OPTION_BLOCK2.
* @param number COAP_OPTION_BLOCK1 or COAP_OPTION_BLOCK2.
* @param pdu The message where the block option should be written.
* @param data_length The length of the actual data that will be added the @p
* pdu by calling coap_add_block().
@@ -116,7 +117,7 @@ int coap_get_block(coap_pdu_t *pdu, uint16_t 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,
uint16_t type,
coap_option_num_t number,
coap_pdu_t *pdu,
size_t data_length);
@@ -346,7 +347,7 @@ void coap_context_set_block_mode(coap_context_t *context,
* else @c 0.
*/
int coap_cancel_observe(coap_session_t *session, coap_binary_t *token,
uint8_t message_type);
coap_pdu_type_t message_type);
/**@}*/
+1
View File
@@ -17,6 +17,7 @@
#ifndef COAP_BLOCK_INTERNAL_H_
#define COAP_BLOCK_INTERNAL_H_
#include "coap_pdu_internal.h"
#include "resource.h"
/**
+7 -8
View File
@@ -27,14 +27,6 @@ struct coap_dtls_pki_t;
struct coap_str_const_t;
struct coap_string_t;
/*
* Just to get rid of #include circular references
*
* When (If) the struct becomes opaque, then move it to the correct place
*
*/
typedef struct coap_pdu_t coap_pdu_t;
/*
* typedef all the opaque structures that are defined in coap_*_internal.h
*/
@@ -79,6 +71,13 @@ typedef struct coap_socket_t coap_socket_t;
typedef struct coap_context_t coap_context_t;
typedef struct coap_queue_t coap_queue_t;
/* ************* coap_pdu_internal.h ***************** */
/**
* PDU information.
*/
typedef struct coap_pdu_t coap_pdu_t;
/* ************* coap_resource_internal.h ***************** */
/*
+1
View File
@@ -52,6 +52,7 @@
#include "coap_cache_internal.h"
#include "coap_io_internal.h"
#include "coap_net_internal.h"
#include "coap_pdu_internal.h"
#include "coap_session_internal.h"
#include "coap_resource_internal.h"
#include "coap_session_internal.h"
+4 -4
View File
@@ -73,7 +73,7 @@ coap_socket_bind_udp(coap_socket_t *sock,
void coap_socket_close(coap_socket_t *sock);
ssize_t
coap_socket_send( coap_socket_t *sock, struct coap_session_t *session,
coap_socket_send( coap_socket_t *sock, coap_session_t *session,
const uint8_t *data, size_t data_len );
ssize_t
@@ -87,8 +87,8 @@ coap_epoll_ctl_mod(coap_socket_t *sock, uint32_t events, const char *func);
#ifdef WITH_LWIP
ssize_t
coap_socket_send_pdu( coap_socket_t *sock, struct coap_session_t *session,
struct coap_pdu_t *pdu );
coap_socket_send_pdu( coap_socket_t *sock, coap_session_t *session,
coap_pdu_t *pdu );
#endif
/**
@@ -103,7 +103,7 @@ coap_socket_send_pdu( coap_socket_t *sock, struct coap_session_t *session,
* @return The number of bytes written on success, or a value
* less than zero on error.
*/
ssize_t coap_network_send( coap_socket_t *sock, const struct coap_session_t *session, const uint8_t *data, size_t datalen );
ssize_t coap_network_send( coap_socket_t *sock, const coap_session_t *session, const uint8_t *data, size_t datalen );
/**
* Function interface for reading data. This function returns the number of
+298
View File
@@ -0,0 +1,298 @@
/*
* coap_pdu_internal.h -- CoAP PDU structure
*
* Copyright (C) 2010-2021 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
/**
* @file coap_pdu_internal.h
* @brief CoAP PDU internal information
*/
#ifndef COAP_COAP_PDU_INTERNAL_H_
#define COAP_COAP_PDU_INTERNAL_H_
#ifdef WITH_LWIP
#include <lwip/pbuf.h>
#endif
#include <stdint.h>
/**
* @defgroup pdu_internal PDU (Internal)
* CoAP PDU Structures, Enums and Functions that are not exposed to
* applications
* @{
*/
#define COAP_DEFAULT_VERSION 1 /* version of CoAP supported */
/* TCP Message format constants, do not modify */
#define COAP_MESSAGE_SIZE_OFFSET_TCP8 13
#define COAP_MESSAGE_SIZE_OFFSET_TCP16 269 /* 13 + 256 */
#define COAP_MESSAGE_SIZE_OFFSET_TCP32 65805 /* 269 + 65536 */
/* Derived message size limits */
#define COAP_MAX_MESSAGE_SIZE_TCP0 (COAP_MESSAGE_SIZE_OFFSET_TCP8-1) /* 12 */
#define COAP_MAX_MESSAGE_SIZE_TCP8 (COAP_MESSAGE_SIZE_OFFSET_TCP16-1) /* 268 */
#define COAP_MAX_MESSAGE_SIZE_TCP16 (COAP_MESSAGE_SIZE_OFFSET_TCP32-1) /* 65804 */
#define COAP_MAX_MESSAGE_SIZE_TCP32 (COAP_MESSAGE_SIZE_OFFSET_TCP32+0xFFFFFFFF)
#ifndef COAP_DEBUG_BUF_SIZE
#if defined(WITH_CONTIKI) || defined(WITH_LWIP)
#define COAP_DEBUG_BUF_SIZE 128
#else /* defined(WITH_CONTIKI) || defined(WITH_LWIP) */
/* 1024 derived from RFC7252 4.6. Message Size max payload */
#define COAP_DEBUG_BUF_SIZE (8 + 1024 * 2)
#endif /* defined(WITH_CONTIKI) || defined(WITH_LWIP) */
#endif /* COAP_DEBUG_BUF_SIZE */
#ifndef COAP_DEFAULT_MAX_PDU_RX_SIZE
#if defined(WITH_CONTIKI) || defined(WITH_LWIP)
#define COAP_DEFAULT_MAX_PDU_RX_SIZE (COAP_MAX_MESSAGE_SIZE_TCP16+4UL)
#else
/* 8 MiB max-message-size plus some space for options */
#define COAP_DEFAULT_MAX_PDU_RX_SIZE (8UL*1024*1024+256)
#endif
#endif /* COAP_DEFAULT_MAX_PDU_RX_SIZE */
/**
* Indicates that a response is suppressed. This will occur for error
* responses if the request was received via IP multicast.
*/
#define COAP_DROPPED_RESPONSE -2
#define COAP_PDU_DELAYED -3
#define COAP_PAYLOAD_START 0xFF /* payload marker */
#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
/**
* 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)
*/
struct coap_pdu_t {
coap_pdu_type_t type; /**< message type */
coap_pdu_code_t code; /**< request method (value 1--31) or response code
(value 64-255) */
coap_mid_t mid; /**< message id, if any, in regular host byte
order */
uint8_t max_hdr_size; /**< space reserved for protocol-specific header */
uint8_t hdr_size; /**< actual size used for protocol-specific
header */
uint8_t token_length; /**< length of Token */
uint16_t max_opt; /**< highest option number in PDU */
size_t alloc_size; /**< allocated storage for token, options and
payload */
size_t used_size; /**< used bytes of storage for token, options and
payload */
size_t max_size; /**< maximum size for token, options and payload,
or zero for variable size pdu */
uint8_t *token; /**< first byte of token, if any, or options */
uint8_t *data; /**< first byte of payload, if any */
#ifdef WITH_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. */
#endif
const uint8_t *body_data; /**< Holds ptr to re-assembled data or NULL */
size_t body_length; /**< Holds body data length */
size_t body_offset; /**< Holds body data offset */
size_t body_total; /**< Holds body data total size */
coap_lg_xmit_t *lg_xmit; /**< Holds ptr to lg_xmit if sending a set of
blocks */
};
/**
* Dynamically grows the size of @p pdu to @p new_size. The new size
* must not exceed the PDU's configure maximum size. On success, this
* function returns 1, otherwise 0.
*
* @param pdu The PDU to resize.
* @param new_size The new size in bytes.
* @return 1 if the operation succeeded, 0 otherwise.
*/
int coap_pdu_resize(coap_pdu_t *pdu, size_t new_size);
/**
* Dynamically grows the size of @p pdu to @p new_size if needed. The new size
* must not exceed the PDU's configured maximum size. On success, this
* function returns 1, otherwise 0.
*
* @param pdu The PDU to resize.
* @param new_size The new size in bytes.
* @return 1 if the operation succeeded, 0 otherwise.
*/
int coap_pdu_check_resize(coap_pdu_t *pdu, size_t new_size);
/**
* 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.
*
* @return A value greater than zero on success or @c 0 on error.
*/
size_t coap_pdu_parse_size(coap_proto_t proto,
const uint8_t *data,
size_t length);
/**
* Decode the protocol specific header for the specified PDU.
* @param pdu A newly received PDU.
* @param proto The target wire protocol.
* @return 1 for success or 0 on error.
*/
int coap_pdu_parse_header(coap_pdu_t *pdu, coap_proto_t proto);
/**
* Verify consistency in the given CoAP PDU structure and locate the data.
* This function returns @c 0 on error or a number greater than zero on
* success.
* This function only parses the token and options, up to the payload start
* marker.
*
* @param pdu The PDU structure to check.
*
* @return 1 on success or @c 0 on error.
*/
int coap_pdu_parse_opt(coap_pdu_t *pdu);
/**
* 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);
/**
* Clears any contents from @p pdu and resets @c used_size,
* and @c data pointers. @c max_size is set to @p size, any
* other field is set to @c 0. Note that @p pdu must be a valid
* pointer to a coap_pdu_t object created e.g. by coap_pdu_init().
*
* @param pdu The PDU to clear.
* @param size The maximum size of the PDU.
*/
void coap_pdu_clear(coap_pdu_t *pdu, size_t size);
/**
* Removes (first) option of given number from the @p pdu.
*
* @param pdu The PDU to remove the option from.
* @param number The number of the CoAP option to remove (first only removed).
*
* @return @c 1 if success else @c 0 if error.
*/
int coap_remove_option(coap_pdu_t *pdu, coap_option_num_t number);
/**
* Inserts option of given number in the @p pdu with the appropriate data.
* The option will be inserted in the appropriate place in the options in
* the pdu.
*
* @param pdu The PDU where the option is to be inserted.
* @param number The number of the new option.
* @param len The length of the new option.
* @param data The data of the new option.
*
* @return The overall length of the option or @c 0 on failure.
*/
size_t coap_insert_option(coap_pdu_t *pdu, coap_option_num_t number,
size_t len, const uint8_t *data);
/**
* Updates existing first option of given number in the @p pdu with the new
* data.
*
* @param pdu The PDU where the option is to be updated.
* @param number The number of the option to update (first only updated).
* @param len The length of the updated option.
* @param data The data of the updated option.
*
* @return The overall length of the updated option or @c 0 on failure.
*/
size_t coap_update_option(coap_pdu_t *pdu,
coap_option_num_t number,
size_t len,
const 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);
/**
* Updates token in @p pdu with length @p len and @p data.
* This function returns @c 0 on error or a value greater than zero on success.
*
* @param pdu The PDU where the token is to be updated.
* @param len The length of the new token.
* @param data The token to add.
*
* @return A value greater than zero on success, or @c 0 on error.
*/
int coap_update_token(coap_pdu_t *pdu,
size_t len,
const uint8_t *data);
/** @} */
#endif /* COAP_COAP_PDU_INTERNAL_H_ */
+7
View File
@@ -14,6 +14,12 @@
#ifndef COAP_SESSION_H_
#define COAP_SESSION_H_
/**
* @defgroup session Sessions
* API functions for CoAP Sessions
* @{
*/
/**
* Abstraction of a fixed point number that can be used where necessary instead
* of a float. 1,000 fractional bits equals one integer
@@ -361,6 +367,7 @@ void coap_endpoint_set_default_mtu(coap_endpoint_t *endpoint, unsigned mtu);
void coap_free_endpoint(coap_endpoint_t *ep);
/** @} */
/**
* @ingroup logging
+3 -3
View File
@@ -52,8 +52,8 @@ struct coap_subscription_t {
* sent (in that case, the resource's partially
* dirty flag is set too) */
unsigned int has_block2:1; /**< GET request had Block2 definition */
uint8_t code; /** request type code (GET/FETCH)*/
uint16_t mid; /**< message id, if any, in regular host byte order */
coap_pdu_code_t code; /** request type code (GET/FETCH)*/
coap_mid_t mid; /**< message id, if any, in regular host byte order */
coap_block_t block; /**< GET/FETCH request Block definition */
size_t token_length; /**< actual length of token */
unsigned char token[8]; /**< token used for subscription */
@@ -107,7 +107,7 @@ coap_subscription_t *coap_add_observer(coap_resource_t *resource,
coap_string_t *query,
int has_block2,
coap_block_t block2,
uint8_t code);
coap_pdu_code_t code);
/**
* Returns a subscription object for given @p peer.
+41 -34
View File
@@ -17,6 +17,8 @@
#include "bits.h"
typedef uint16_t coap_option_num_t;
/**
* 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.
@@ -105,50 +107,49 @@ typedef struct coap_opt_filter_t {
#define COAP_OPT_ALL NULL
/**
* Clears filter @p f.
* Clears filter @p filter.
*
* @param f The filter to clear.
* @param filter The filter to clear.
*/
COAP_STATIC_INLINE void
coap_option_filter_clear(coap_opt_filter_t *f) {
memset(f, 0, sizeof(coap_opt_filter_t));
}
void
coap_option_filter_clear(coap_opt_filter_t *filter);
/**
* Sets the corresponding entry for @p type in @p filter. This
* Sets the corresponding entry for @p number in @p filter. This
* function returns @c 1 if bit was set or @c 0 on error (i.e. when
* the given type does not fit in the filter).
* the given number does not fit in the filter).
*
* @param filter The filter object to change.
* @param type The type for which the bit should be set.
* @param number The option number for which the bit should be set.
*
* @return @c 1 if bit was set, @c 0 otherwise.
*/
int coap_option_filter_set(coap_opt_filter_t *filter, uint16_t type);
int coap_option_filter_set(coap_opt_filter_t *filter, coap_option_num_t number);
/**
* Clears the corresponding entry for @p type in @p filter. This
* Clears the corresponding entry for @p number in @p filter. This
* function returns @c 1 if bit was set or @c 0 on error (i.e. when
* the given type does not fit in the filter).
* the given number does not fit in the filter).
*
* @param filter The filter object to change.
* @param type The type that should be cleared from the filter.
* @param number The option number that should be cleared from the filter.
*
* @return @c 1 if bit was set, @c 0 otherwise.
*/
int coap_option_filter_unset(coap_opt_filter_t *filter, uint16_t type);
int coap_option_filter_unset(coap_opt_filter_t *filter,
coap_option_num_t number);
/**
* Checks if @p type is contained in @p filter. This function returns
* Checks if @p number is contained in @p filter. This function returns
* @c 1 if found, @c 0 if not, or @c -1 on error (i.e. when the given
* type does not fit in the filter).
* number does not fit in the filter).
*
* @param filter The filter object to search.
* @param type The type to search for.
* @param number The option number to search for.
*
* @return @c 1 if @p type was found, @c 0 otherwise, or @c -1 on error.
* @return @c 1 if @p number was found, @c 0 otherwise, or @c -1 on error.
*/
int coap_option_filter_get(coap_opt_filter_t *filter, uint16_t type);
int coap_option_filter_get(coap_opt_filter_t *filter, coap_option_num_t number);
/**
* Iterator to run through PDU options. This object must be
@@ -168,7 +169,7 @@ int coap_option_filter_get(coap_opt_filter_t *filter, uint16_t type);
*/
typedef struct {
size_t length; /**< remaining length of PDU */
uint16_t type; /**< decoded option type */
coap_option_num_t number; /**< decoded option number */
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 */
@@ -183,8 +184,8 @@ typedef struct {
*
* @param pdu The PDU the options of which should be walked through.
* @param oi An iterator object that will be initilized.
* @param filter An optional option type filter.
* With @p type != @c COAP_OPT_ALL, coap_option_next()
* @param filter An optional option number filter.
* With @p number != @c COAP_OPT_ALL, coap_option_next()
* will return only options matching this bitmask.
* Fence-post options @c 14, @c 28, @c 42, ... are always
* skipped.
@@ -199,12 +200,12 @@ coap_opt_iterator_t *coap_option_iterator_init(const coap_pdu_t *pdu,
* Updates the iterator @p oi to point to the next option. This function returns
* a pointer to that option or @c NULL if no more options exist. The contents of
* @p oi will be updated. In particular, @c oi->n specifies the current option's
* ordinal number (counted from @c 1), @c oi->type is the option's type code,
* and @c oi->option points to the beginning of the current option itself. When
* advanced past the last option, @c oi->option will be @c NULL.
* ordinal number (counted from @c 1), @c oi->number is the option's number
* value, and @c oi->option points to the beginning of the current option
* itself. When * advanced past the last option, @c oi->option will be @c NULL.
*
* Note that options are skipped whose corresponding bits in the filter
* specified with coap_option_iterator_init() are @c 0. Options with type codes
* specified with coap_option_iterator_init() are @c 0. Options with numbers
* that do not fit in this filter hence will always be returned.
*
* @param oi The option iterator to update.
@@ -214,20 +215,20 @@ coap_opt_iterator_t *coap_option_iterator_init(const coap_pdu_t *pdu,
coap_opt_t *coap_option_next(coap_opt_iterator_t *oi);
/**
* Retrieves the first option of type @p type from @p pdu. @p oi must point to a
* coap_opt_iterator_t object that will be initialized by this function to
* filter only options with code @p type. This function returns the first option
* with this type, or @c NULL if not found.
* Retrieves the first option of number @p number from @p pdu. @p oi must
* point to a coap_opt_iterator_t object that will be initialized by this
* function to filter only options with number @p number. This function returns
* the first option with this number, or @c NULL if not found.
*
* @param pdu The PDU to parse for options.
* @param type The option type code to search for.
* @param number The option number to search for.
* @param oi An iterator object to use.
*
* @return A pointer to the first option of type @p type, or @c NULL if
* @return A pointer to the first option of number @p number, or @c NULL if
* not found.
*/
coap_opt_t *coap_check_option(coap_pdu_t *pdu,
uint16_t type,
coap_opt_t *coap_check_option(const coap_pdu_t *pdu,
coap_option_num_t number,
coap_opt_iterator_t *oi);
/**
@@ -333,6 +334,12 @@ typedef struct coap_optlist_t {
/**
* Create a new optlist entry.
*
* Note: Where possible, the option data needs to be stripped of leading zeros
* (big endian) to reduce the amount of data needed in the PDU, as well as in
* some cases the maximum data size of an opton can be exceeded if not stripped
* and hence be illegal. This is done by using coap_encode_var_safe() or
* coap_encode_var_safe8().
*
* @param number The option number (COAP_OPTION_*)
* @param length The option length
* @param data The option value data
+184 -303
View File
@@ -24,6 +24,12 @@
#include <stdint.h>
/**
* @defgroup pdu PDU
* API functions for PDUs
* @{
*/
#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 */
@@ -35,36 +41,6 @@
#define COAP_DEFAULT_HOP_LIMIT 16
#endif /* COAP_DEFAULT_HOP_LIMIT */
/* TCP Message format constants, do not modify */
#define COAP_MESSAGE_SIZE_OFFSET_TCP8 13
#define COAP_MESSAGE_SIZE_OFFSET_TCP16 269 /* 13 + 256 */
#define COAP_MESSAGE_SIZE_OFFSET_TCP32 65805 /* 269 + 65536 */
/* Derived message size limits */
#define COAP_MAX_MESSAGE_SIZE_TCP0 (COAP_MESSAGE_SIZE_OFFSET_TCP8-1) /* 12 */
#define COAP_MAX_MESSAGE_SIZE_TCP8 (COAP_MESSAGE_SIZE_OFFSET_TCP16-1) /* 268 */
#define COAP_MAX_MESSAGE_SIZE_TCP16 (COAP_MESSAGE_SIZE_OFFSET_TCP32-1) /* 65804 */
#define COAP_MAX_MESSAGE_SIZE_TCP32 (COAP_MESSAGE_SIZE_OFFSET_TCP32+0xFFFFFFFF)
#ifndef COAP_DEFAULT_MAX_PDU_RX_SIZE
#if defined(WITH_CONTIKI) || defined(WITH_LWIP)
#define COAP_DEFAULT_MAX_PDU_RX_SIZE (COAP_MAX_MESSAGE_SIZE_TCP16+4UL)
#else
/* 8 MiB max-message-size plus some space for options */
#define COAP_DEFAULT_MAX_PDU_RX_SIZE (8UL*1024*1024+256)
#endif
#endif /* COAP_DEFAULT_MAX_PDU_RX_SIZE */
#ifndef COAP_DEBUG_BUF_SIZE
#if defined(WITH_CONTIKI) || defined(WITH_LWIP)
#define COAP_DEBUG_BUF_SIZE 128
#else /* defined(WITH_CONTIKI) || defined(WITH_LWIP) */
/* 1024 derived from RFC7252 4.6. Message Size max payload */
#define COAP_DEBUG_BUF_SIZE (8 + 1024 * 2)
#endif /* defined(WITH_CONTIKI) || defined(WITH_LWIP) */
#endif /* COAP_DEBUG_BUF_SIZE */
#define COAP_DEFAULT_VERSION 1 /* version of CoAP supported */
#define COAP_DEFAULT_SCHEME "coap" /* the default scheme for CoAP URIs */
/** well-known resources URI */
@@ -72,13 +48,19 @@
/* CoAP message types */
#define COAP_MESSAGE_CON 0 /* confirmable message (requires ACK/RST) */
#define COAP_MESSAGE_NON 1 /* non-confirmable message (one-shot message) */
#define COAP_MESSAGE_ACK 2 /* used to acknowledge confirmable messages */
#define COAP_MESSAGE_RST 3 /* indicates error in received messages */
/* CoAP request methods */
/**
* CoAP PDU message type definitions
*/
typedef enum coap_pdu_type_t {
COAP_MESSAGE_CON, /* 0 confirmable message (requires ACK/RST) */
COAP_MESSAGE_NON, /* 1 non-confirmable message (one-shot message) */
COAP_MESSAGE_ACK, /* 2 used to acknowledge confirmable messages */
COAP_MESSAGE_RST /* 3 indicates error in received messages */
} coap_pdu_type_t;
/**
* CoAP PDU Request methods
*/
typedef enum coap_request_t {
COAP_REQUEST_GET = 1,
COAP_REQUEST_POST, /* 2 */
@@ -90,7 +72,7 @@ typedef enum coap_request_t {
} coap_request_t;
/*
* CoAP option types (be sure to update coap_option_check_critical() and
* CoAP option numbers (be sure to update coap_option_check_critical() and
* coap_add_option() when adding options
*/
@@ -180,11 +162,14 @@ const char *coap_response_phrase(unsigned char code);
#endif /* SHORT_ERROR_RESPONSE */
#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)
typedef enum coap_pdu_signaling_proto_t {
COAP_SIGNALING_CSM = COAP_SIGNALING_CODE(701),
COAP_SIGNALING_PING = COAP_SIGNALING_CODE(702),
COAP_SIGNALING_PONG = COAP_SIGNALING_CODE(703),
COAP_SIGNALING_RELEASE = COAP_SIGNALING_CODE(704),
COAP_SIGNALING_ABORT = COAP_SIGNALING_CODE(705),
} coap_pdu_signaling_proto_t;
/* Applies to COAP_SIGNALING_CSM */
#define COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE 2
@@ -252,16 +237,6 @@ typedef int coap_mid_t;
*/
#define COAP_INVALID_TID COAP_INVALID_MID
/**
* Indicates that a response is suppressed. This will occur for error
* responses if the request was received via IP multicast.
*/
#define COAP_DROPPED_RESPONSE -2
#define COAP_PDU_DELAYED -3
#define COAP_PAYLOAD_START 0xFF /* payload marker */
/**
* @deprecated Use coap_optlist_t instead.
*
@@ -278,57 +253,6 @@ COAP_DEPRECATED typedef struct {
#define COAP_OPTION_LENGTH(option) (option).length
#define COAP_OPTION_DATA(option) ((unsigned char *)&(option) + sizeof(coap_option))
/**
* 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)
*/
struct coap_pdu_t {
uint8_t type; /**< message type */
uint8_t code; /**< request method (value 1--31) or response code (value 64-255) */
uint8_t max_hdr_size; /**< space reserved for protocol-specific header */
uint8_t hdr_size; /**< actual size used for protocol-specific header */
uint8_t token_length; /**< length of Token */
uint16_t mid; /**< message id, if any, in regular host byte order */
uint16_t max_opt; /**< highest option number in PDU */
size_t alloc_size; /**< allocated storage for token, options and payload */
size_t used_size; /**< used bytes of storage for token, options and payload */
size_t max_size; /**< maximum size for token, options and payload, or zero for variable size pdu */
uint8_t *token; /**< first byte of token, if any, or options */
uint8_t *data; /**< first byte of payload, if any */
#ifdef WITH_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. */
#endif
const uint8_t *body_data; /**< Holds ptr to re-assembled data or NULL */
size_t body_length; /**< Holds body data length */
size_t body_offset; /**< Holds body data offset */
size_t body_total; /**< Holds body data total size */
coap_lg_xmit_t *lg_xmit; /**< Holds ptr to lg_xmit if sending a set of
blocks */
};
#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
/**
* Creates a CoAP PDU from an lwIP @p pbuf, whose reference is passed on to this
@@ -348,53 +272,66 @@ struct coap_pdu_t {
coap_pdu_t * coap_pdu_from_pbuf(struct pbuf *pbuf);
#endif
typedef uint8_t coap_proto_t;
/**
* coap_proto_t values
* CoAP protocol types
*/
#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
typedef enum coap_proto_t {
COAP_PROTO_NONE = 0,
COAP_PROTO_UDP,
COAP_PROTO_DTLS,
COAP_PROTO_TCP,
COAP_PROTO_TLS,
} coap_proto_t;
/**
* @brief Set of response codes available for a response packet.
*
* To be used when creating a response.
* Set of codes available for a PDU.
*/
enum coap_response_code {
COAP_RESPONSE_CODE_OK = COAP_RESPONSE_CODE(200),
COAP_RESPONSE_CODE_CREATED = COAP_RESPONSE_CODE(201),
COAP_RESPONSE_CODE_DELETED = COAP_RESPONSE_CODE(202),
COAP_RESPONSE_CODE_VALID = COAP_RESPONSE_CODE(203),
COAP_RESPONSE_CODE_CHANGED = COAP_RESPONSE_CODE(204),
COAP_RESPONSE_CODE_CONTENT = COAP_RESPONSE_CODE(205),
COAP_RESPONSE_CODE_CONTINUE = COAP_RESPONSE_CODE(231),
COAP_RESPONSE_CODE_BAD_REQUEST = COAP_RESPONSE_CODE(400),
COAP_RESPONSE_CODE_UNAUTHORIZED = COAP_RESPONSE_CODE(401),
COAP_RESPONSE_CODE_BAD_OPTION = COAP_RESPONSE_CODE(402),
COAP_RESPONSE_CODE_FORBIDDEN = COAP_RESPONSE_CODE(403),
COAP_RESPONSE_CODE_NOT_FOUND = COAP_RESPONSE_CODE(404),
COAP_RESPONSE_CODE_NOT_ALLOWED = COAP_RESPONSE_CODE(405),
COAP_RESPONSE_CODE_NOT_ACCEPTABLE = COAP_RESPONSE_CODE(406),
COAP_RESPONSE_CODE_INCOMPLETE = COAP_RESPONSE_CODE(408),
COAP_RESPONSE_CODE_CONFLICT = COAP_RESPONSE_CODE(409),
COAP_RESPONSE_CODE_PRECONDITION_FAILED = COAP_RESPONSE_CODE(412),
COAP_RESPONSE_CODE_REQUEST_TOO_LARGE = COAP_RESPONSE_CODE(413),
COAP_RESPONSE_CODE_UNSUPPORTED_CONTENT_FORMAT =
COAP_RESPONSE_CODE(415),
COAP_RESPONSE_CODE_UNPROCESSABLE = COAP_RESPONSE_CODE(422),
COAP_RESPONSE_CODE_TOO_MANY_REQUESTS = COAP_RESPONSE_CODE(429),
COAP_RESPONSE_CODE_INTERNAL_ERROR = COAP_RESPONSE_CODE(500),
COAP_RESPONSE_CODE_NOT_IMPLEMENTED = COAP_RESPONSE_CODE(501),
COAP_RESPONSE_CODE_BAD_GATEWAY = COAP_RESPONSE_CODE(502),
COAP_RESPONSE_CODE_SERVICE_UNAVAILABLE = COAP_RESPONSE_CODE(503),
COAP_RESPONSE_CODE_GATEWAY_TIMEOUT = COAP_RESPONSE_CODE(504),
COAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED =
COAP_RESPONSE_CODE(505),
COAP_RESPONSE_CODE_HOP_LIMIT_REACHED = COAP_RESPONSE_CODE(508)
};
typedef enum coap_pdu_code_t {
COAP_EMPTY_CODE = 0,
COAP_REQUEST_CODE_GET = COAP_REQUEST_GET,
COAP_REQUEST_CODE_POST = COAP_REQUEST_POST,
COAP_REQUEST_CODE_PUT = COAP_REQUEST_PUT,
COAP_REQUEST_CODE_DELETE = COAP_REQUEST_DELETE,
COAP_REQUEST_CODE_FETCH = COAP_REQUEST_FETCH,
COAP_REQUEST_CODE_PATCH = COAP_REQUEST_PATCH,
COAP_REQUEST_CODE_IPATCH = COAP_REQUEST_IPATCH,
COAP_RESPONSE_CODE_OK = COAP_RESPONSE_CODE(200),
COAP_RESPONSE_CODE_CREATED = COAP_RESPONSE_CODE(201),
COAP_RESPONSE_CODE_DELETED = COAP_RESPONSE_CODE(202),
COAP_RESPONSE_CODE_VALID = COAP_RESPONSE_CODE(203),
COAP_RESPONSE_CODE_CHANGED = COAP_RESPONSE_CODE(204),
COAP_RESPONSE_CODE_CONTENT = COAP_RESPONSE_CODE(205),
COAP_RESPONSE_CODE_CONTINUE = COAP_RESPONSE_CODE(231),
COAP_RESPONSE_CODE_BAD_REQUEST = COAP_RESPONSE_CODE(400),
COAP_RESPONSE_CODE_UNAUTHORIZED = COAP_RESPONSE_CODE(401),
COAP_RESPONSE_CODE_BAD_OPTION = COAP_RESPONSE_CODE(402),
COAP_RESPONSE_CODE_FORBIDDEN = COAP_RESPONSE_CODE(403),
COAP_RESPONSE_CODE_NOT_FOUND = COAP_RESPONSE_CODE(404),
COAP_RESPONSE_CODE_NOT_ALLOWED = COAP_RESPONSE_CODE(405),
COAP_RESPONSE_CODE_NOT_ACCEPTABLE = COAP_RESPONSE_CODE(406),
COAP_RESPONSE_CODE_INCOMPLETE = COAP_RESPONSE_CODE(408),
COAP_RESPONSE_CODE_CONFLICT = COAP_RESPONSE_CODE(409),
COAP_RESPONSE_CODE_PRECONDITION_FAILED = COAP_RESPONSE_CODE(412),
COAP_RESPONSE_CODE_REQUEST_TOO_LARGE = COAP_RESPONSE_CODE(413),
COAP_RESPONSE_CODE_UNSUPPORTED_CONTENT_FORMAT = COAP_RESPONSE_CODE(415),
COAP_RESPONSE_CODE_UNPROCESSABLE = COAP_RESPONSE_CODE(422),
COAP_RESPONSE_CODE_TOO_MANY_REQUESTS = COAP_RESPONSE_CODE(429),
COAP_RESPONSE_CODE_INTERNAL_ERROR = COAP_RESPONSE_CODE(500),
COAP_RESPONSE_CODE_NOT_IMPLEMENTED = COAP_RESPONSE_CODE(501),
COAP_RESPONSE_CODE_BAD_GATEWAY = COAP_RESPONSE_CODE(502),
COAP_RESPONSE_CODE_SERVICE_UNAVAILABLE = COAP_RESPONSE_CODE(503),
COAP_RESPONSE_CODE_GATEWAY_TIMEOUT = COAP_RESPONSE_CODE(504),
COAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED = COAP_RESPONSE_CODE(505),
COAP_RESPONSE_CODE_HOP_LIMIT_REACHED = COAP_RESPONSE_CODE(508),
COAP_SIGNALING_CODE_CSM = COAP_SIGNALING_CSM,
COAP_SIGNALING_CODE_PING = COAP_SIGNALING_PING,
COAP_SIGNALING_CODE_PONG = COAP_SIGNALING_PONG,
COAP_SIGNALING_CODE_RELEASE = COAP_SIGNALING_RELEASE,
COAP_SIGNALING_CODE_ABORT = COAP_SIGNALING_ABORT
} coap_pdu_code_t;
/**
* Creates a new CoAP PDU with at least enough storage space for the given
@@ -405,54 +342,26 @@ enum coap_response_code {
*
* @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 mid The transcation id to set or 0 if unknown / not applicable.
* @param code The message code of the PDU.
* @param mid The message id to set or 0 if unknown / not applicable.
* @param size The maximum allowed number of byte for the message.
* @return A pointer to the new PDU object or @c NULL on error.
*/
coap_pdu_t *
coap_pdu_init(uint8_t type, uint8_t code, uint16_t mid, size_t size);
/**
* Dynamically grows the size of @p pdu to @p new_size. The new size
* must not exceed the PDU's configure maximum size. On success, this
* function returns 1, otherwise 0.
*
* @param pdu The PDU to resize.
* @param new_size The new size in bytes.
* @return 1 if the operation succeeded, 0 otherwise.
*/
int coap_pdu_resize(coap_pdu_t *pdu, size_t new_size);
/**
* Dynamically grows the size of @p pdu to @p new_size if needed. The new size
* must not exceed the PDU's configured maximum size. On success, this
* function returns 1, otherwise 0.
*
* Internal use only.
*
* @param pdu The PDU to resize.
* @param new_size The new size in bytes.
* @return 1 if the operation succeeded, 0 otherwise.
*/
int coap_pdu_check_resize(coap_pdu_t *pdu, size_t new_size);
/**
* Clears any contents from @p pdu and resets @c used_size,
* and @c data pointers. @c max_size is set to @p size, any
* other field is set to @c 0. Note that @p pdu must be a valid
* pointer to a coap_pdu_t object created e.g. by coap_pdu_init().
*/
void coap_pdu_clear(coap_pdu_t *pdu, size_t size);
coap_pdu_t *coap_pdu_init(coap_pdu_type_t type, coap_pdu_code_t code,
coap_mid_t mid, size_t size);
/**
* Creates a new CoAP PDU.
*
* @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 of the PDU.
* @param session The session that will be using this PDU
*
* @return The skeletal PDU or @c NULL if failure.
*/
coap_pdu_t *coap_new_pdu(const coap_session_t *session);
coap_pdu_t *coap_new_pdu(coap_pdu_type_t type, coap_pdu_code_t code,
coap_session_t *session);
/**
* Dispose of an CoAP PDU and frees associated storage.
@@ -484,73 +393,6 @@ coap_pdu_duplicate(const coap_pdu_t *old_pdu,
uint8_t *token,
coap_opt_filter_t *drop_options);
/**
* 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.
*
* @return A value greater than zero on success or @c 0 on error.
*/
size_t coap_pdu_parse_size(coap_proto_t proto,
const uint8_t *data,
size_t length);
/**
* Decode the protocol specific header for the specified PDU.
* @param pdu A newly received PDU.
* @param proto The target wire protocol.
* @return 1 for success or 0 on error.
*/
int coap_pdu_parse_header(coap_pdu_t *pdu, coap_proto_t proto);
/**
* Verify consistency in the given CoAP PDU structure and locate the data.
* This function returns @c 0 on error or a number greater than zero on
* success.
* This function only parses the token and options, up to the payload start
* marker.
*
* @param pdu The PDU structure to.
*
* @return 1 on success or @c 0 on error.
*/
int coap_pdu_parse_opt(coap_pdu_t *pdu);
/**
* 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
@@ -568,69 +410,42 @@ int coap_add_token(coap_pdu_t *pdu,
size_t len,
const uint8_t *data);
size_t coap_insert_option(coap_pdu_t *, uint16_t, size_t, const uint8_t *);
/**
* Updates token in @p pdu with length @p len and @p data.
* This function returns @c 0 on error or a value greater than zero on success.
*
* Internal use only
*
* @param pdu The PDU where the token is to be updated.
* @param len The length of the new token.
* @param data The token to add.
*
* @return A value greater than zero on success, or @c 0 on error.
*/
int coap_update_token(coap_pdu_t *pdu,
size_t len,
const uint8_t *data);
/**
* Adds option of given type to pdu that is passed as first
* Adds option of given number to pdu that is passed as first
* parameter.
* coap_add_option() destroys the PDU's data, so coap_add_data() must be called
* after all options have been added. As coap_add_token() destroys the options
* following the token, the token must be added before coap_add_option() is
* called. This function returns the number of bytes written or @c 0 on error.
*
* Note: Where possible, the option data needs to be stripped of leading zeros
* (big endian) to reduce the amount of data needed in the PDU, as well as in
* some cases the maximum data size of an opton can be exceeded if not stripped
* and hence be illegal. This is done by using coap_encode_var_safe() or
* coap_encode_var_safe8().
*
* @param pdu The PDU where the option is to be added.
* @param number The number of the new option.
* @param len The length of the new option.
* @param data The data of the new option.
*
* @return The overall length of the option or @c 0 on failure.
*/
size_t coap_add_option(coap_pdu_t *pdu,
uint16_t type,
coap_option_num_t number,
size_t len,
const uint8_t *data);
/**
* Removes option of given type from the @p pdu.
*
* Internal use only.
*/
int coap_remove_option(coap_pdu_t *pdu, uint16_t type);
/**
* Inserts option of given type in the @p pdu with the appropriate data.
* The option will be inserted in the appropriate place in the options in
* the pdu.
*
* Internal use only.
*/
size_t coap_insert_option(coap_pdu_t *pdu, uint16_t type,
size_t len, const uint8_t *data);
/**
* Updates existing first option of given type in the @p pdu with the new data.
*
* Internal use only.
*/
size_t coap_update_option(coap_pdu_t *pdu,
uint16_t type,
size_t len,
const uint8_t *data);
/**
* Adds given data to the pdu that is passed as first parameter. Note that the
* PDU's data is destroyed by coap_add_option(). coap_add_data() must be called
* only once per PDU, otherwise the result is undefined.
*
* @param pdu The PDU where the data is to be added.
* @param len The length of the data.
* @param data The data to add.
*
* @return @c 1 if success, else @c 0 if failure.
*/
int coap_add_data(coap_pdu_t *pdu,
size_t len,
@@ -642,6 +457,11 @@ int coap_add_data(coap_pdu_t *pdu,
* 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.
*
* @param pdu The PDU where the data is to be added.
* @param len The length of the data.
*
* @return Where to copy the data of len to, or @c NULL is error.
*/
uint8_t *coap_add_data_after(coap_pdu_t *pdu, size_t len);
@@ -649,10 +469,17 @@ 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
* 1 if *len and *data have correct values. Note that these values are destroyed
* with the pdu.
*
* @param pdu The specified PDU.
* @param len Returns the length of the current data
* @param data Returns the ptr to the current data
*
* @return @c 1 if len and data are correctly filled in, else
* @c 0 if there is no data.
*/
int coap_get_data(const coap_pdu_t *pdu,
size_t *len,
uint8_t **data);
const uint8_t **data);
/**
* Retrieves the data from a PDU, with support for large bodies of data that
@@ -678,12 +505,66 @@ int coap_get_data_large(const coap_pdu_t *pdu,
size_t *total);
/**
* 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.
* Gets the PDU code associated with @p pdu.
*
* @param pdu The PDU object.
*
* @return The PDU code.
*/
coap_pdu_code_t coap_pdu_get_code(const coap_pdu_t *pdu);
size_t coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto);
/**
* Sets the PDU code in the @p pdu.
*
* @param pdu The PDU object.
* @param code The code to set in the PDU.
*/
void coap_pdu_set_code(coap_pdu_t *pdu, coap_pdu_code_t code);
/**
* Gets the PDU type associated with @p pdu.
*
* @param pdu The PDU object.
*
* @return The PDU type.
*/
coap_pdu_type_t coap_pdu_get_type(const coap_pdu_t *pdu);
/**
* Sets the PDU type in the @p pdu.
*
* @param pdu The PDU object.
* @param type The type to set for the PDU.
*/
void coap_pdu_set_type(coap_pdu_t *pdu, coap_pdu_type_t type);
/**
* Gets the token associated with @p pdu.
*
* @param pdu The PDU object.
*
* @return The token information.
*/
coap_bin_const_t coap_pdu_get_token(const coap_pdu_t *pdu);
/**
* Gets the message id associated with @p pdu.
*
* @param pdu The PDU object.
*
* @return The message id.
*/
coap_mid_t coap_pdu_get_mid(const coap_pdu_t *pdu);
/**
* Sets the message id in the @p pdu.
*
* @param pdu The PDU object.
* @param mid The message id value to set in the PDU.
*
*/
void coap_pdu_set_mid(coap_pdu_t *pdu, coap_mid_t mid);
/** @} */
#endif /* COAP_PDU_H_ */
+2 -2
View File
@@ -157,7 +157,7 @@ int coap_split_query(const uint8_t *s,
* object returned by this function must be released with
* coap_delete_string.
*/
coap_string_t *coap_get_query(const struct coap_pdu_t *request);
coap_string_t *coap_get_query(const coap_pdu_t *request);
/**
* Extract uri_path string from request PDU
@@ -167,7 +167,7 @@ coap_string_t *coap_get_query(const struct coap_pdu_t *request);
* coap_string_t object returned by this function must be
* released with coap_delete_string.
*/
coap_string_t *coap_get_uri_path(const struct coap_pdu_t *request);
coap_string_t *coap_get_uri_path(const coap_pdu_t *request);
/** @} */
+8 -8
View File
@@ -122,6 +122,7 @@ global:
coap_opt_block_num;
coap_opt_encode;
coap_opt_encode_size;
coap_option_filter_clear;
coap_option_filter_get;
coap_option_filter_set;
coap_option_filter_unset;
@@ -134,16 +135,15 @@ global:
coap_opt_value;
coap_package_name;
coap_package_version;
coap_pdu_clear;
coap_pdu_duplicate;
coap_pdu_encode_header;
coap_pdu_get_code;
coap_pdu_get_mid;
coap_pdu_get_token;
coap_pdu_get_type;
coap_pdu_init;
coap_pdu_parse;
coap_pdu_parse_header;
coap_pdu_parse_header_size;
coap_pdu_parse_opt;
coap_pdu_parse_size;
coap_pdu_resize;
coap_pdu_set_code;
coap_pdu_set_mid;
coap_pdu_set_type;
coap_print_addr;
coap_print_link;
coap_prng;
+8 -8
View File
@@ -120,6 +120,7 @@ coap_new_uri
coap_opt_block_num
coap_opt_encode
coap_opt_encode_size
coap_option_filter_clear
coap_option_filter_get
coap_option_filter_set
coap_option_filter_unset
@@ -132,16 +133,15 @@ coap_opt_size
coap_opt_value
coap_package_name
coap_package_version
coap_pdu_clear
coap_pdu_duplicate
coap_pdu_encode_header
coap_pdu_get_code
coap_pdu_get_mid
coap_pdu_get_token
coap_pdu_get_type
coap_pdu_init
coap_pdu_parse
coap_pdu_parse_header
coap_pdu_parse_header_size
coap_pdu_parse_opt
coap_pdu_parse_size
coap_pdu_resize
coap_pdu_set_code
coap_pdu_set_mid
coap_pdu_set_type
coap_print_addr
coap_print_link
coap_prng
+13
View File
@@ -31,6 +31,7 @@ TXT3 = coap_async.txt \
coap_keepalive.txt \
coap_logging.txt \
coap_observe.txt \
coap_pdu_access.txt \
coap_pdu_setup.txt \
coap_recovery.txt \
coap_resource.txt \
@@ -90,6 +91,15 @@ install-man: install-man3 install-man5 install-man7
@echo ".so man3/coap_context.3" > coap_context_get_csm_timeout.3
@echo ".so man3/coap_logging.3" > coap_endpoint_str.3
@echo ".so man3/coap_logging.3" > coap_session_str.3
@echo ".so man3/coap_pdu_access.3" > coap_option_filter_set.3
@echo ".so man3/coap_pdu_access.3" > coap_option_filter_unset.3
@echo ".so man3/coap_pdu_access.3" > coap_option_iterator_init.3
@echo ".so man3/coap_pdu_access.3" > coap_option_next.3
@echo ".so man3/coap_pdu_access.3" > coap_pdu_get_code.3
@echo ".so man3/coap_pdu_access.3" > coap_pdu_get_mid.3
@echo ".so man3/coap_pdu_access.3" > coap_pdu_get_token.3
@echo ".so man3/coap_pdu_access.3" > coap_pdu_get_type.3
@echo ".so man3/coap_pdu_setup.3" > coap_delete_optlist.3
@echo ".so man3/coap_pdu_setup.3" > coap_encode_var_safe.3
@echo ".so man3/coap_pdu_setup.3" > coap_encode_var_safe8.3
@echo ".so man3/coap_pdu_setup.3" > coap_add_optlist_pdu.3
@@ -99,6 +109,9 @@ install-man: install-man3 install-man5 install-man7
@echo ".so man3/coap_pdu_setup.3" > coap_send.3
@echo ".so man3/coap_pdu_setup.3" > coap_split_path.3
@echo ".so man3/coap_pdu_setup.3" > coap_split_query.3
@echo ".so man3/coap_pdu_setup.3" > coap_pdu_set_mid.3
@echo ".so man3/coap_pdu_setup.3" > coap_pdu_set_code.3
@echo ".so man3/coap_pdu_setup.3" > coap_pdu_set_type.3
@echo ".so man3/coap_resource.3" > coap_resource_get_userdata.3
@echo ".so man3/coap_resource.3" > coap_resource_release_userdata_handler.3
@echo ".so man3/coap_resource.3" > coap_resource_get_uri_path.3
+2
View File
@@ -62,6 +62,8 @@ See
"RFC8323: CoAP (Constrained Application Protocol) over TCP, TLS, and WebSockets"
"RFC8613: Object Security for Constrained RESTful Environments (OSCORE)"
"RFC8768: Constrained Application Protocol (CoAP) Hop-Limit Option"
for further information.
+3 -3
View File
@@ -131,7 +131,7 @@ hnd_get_with_delay(coap_context_t *context,
delay = delay * 10 + (*p - '0');
if (delay == 0) {
coap_log(LOG_INFO, "async: delay of 0 not supported\n");
response->code = COAP_RESPONSE_CODE_BAD_REQUEST;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_BAD_REQUEST);
return;
}
}
@@ -140,7 +140,7 @@ hnd_get_with_delay(coap_context_t *context,
request,
COAP_TICKS_PER_SECOND * delay);
if (async == NULL) {
response->code = COAP_RESPONSE_CODE_SERVICE_UNAVAILABLE;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_SERVICE_UNAVAILABLE);
return;
}
/*
@@ -155,7 +155,7 @@ hnd_get_with_delay(coap_context_t *context,
coap_add_data_large_response(resource, session, request, response, token,
query, COAP_MEDIATYPE_TEXT_PLAIN, -1, 0, 4,
(const uint8_t *)"done", NULL, NULL);
response->code = COAP_RESPONSE_CODE_CONTENT;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);
/* async is automatically removed by libcoap on return from this handler */
}
+2 -2
View File
@@ -363,12 +363,12 @@ coap_string_t *query, coap_pdu_t *response) {
tmp = gmtime(&now);
if (!tmp) {
/* If 'now' is not valid */
response->code = COAP_RESPONSE_CODE_NOT_FOUND;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_FOUND);
return;
}
len = strftime((char *)buf, sizeof(buf), "%b %d %H:%M:%S", tmp);
}
response->code = COAP_RESPONSE_CODE_CONTENT;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);
/*
* Invoke coap_add_data_large_response() to do all the hard work.
* [A good practice, even though ins this case, the amount of data is small]
+5 -5
View File
@@ -268,7 +268,7 @@ hnd_put_example_data(coap_context_t *ctx,
coap_log(LOG_WARNING,
"No cache entry available for the non-first BLOCK\n");
}
response->code = COAP_RESPONSE_CODE_INTERNAL_ERROR;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_INTERNAL_ERROR);
return;
}
@@ -287,7 +287,7 @@ hnd_put_example_data(coap_context_t *ctx,
}
else {
/* Give us the next block response */
response->code = COAP_RESPONSE_CODE_CONTINUE;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTINUE);
return;
}
}
@@ -301,12 +301,12 @@ hnd_put_example_data(coap_context_t *ctx,
if (example_data_ptr) {
/* pre-existed response */
response->code = COAP_RESPONSE_CODE_CHANGED;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CHANGED);
coap_delete_binary(example_data_ptr);
}
else
/* just generated response */
response->code = COAP_RESPONSE_CODE_CREATED;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CREATED);
example_data_ptr = data_so_far;
if ((option = coap_check_option(request, COAP_OPTION_CONTENT_FORMAT,
@@ -319,7 +319,7 @@ hnd_put_example_data(coap_context_t *ctx,
example_data_media_type = COAP_MEDIATYPE_TEXT_PLAIN;
}
response->code = COAP_RESPONSE_CODE_CHANGED;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CHANGED);
coap_resource_notify_observers(resource, NULL);
}
+8 -6
View File
@@ -236,12 +236,12 @@ coap_string_t *query, coap_pdu_t *response) {
tmp = gmtime(&now);
if (!tmp) {
/* If 'now' is not valid */
response->code = COAP_RESPONSE_CODE_NOT_FOUND;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_FOUND);
return;
}
len = strftime((char *)buf, sizeof(buf), "%b %d %H:%M:%S", tmp);
}
response->code = COAP_RESPONSE_CODE_CONTENT;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);
/*
* Invoke coap_add_data_large_response() to do all the hard work.
*
@@ -281,25 +281,27 @@ coap_pdu_t *sent, coap_pdu_t *received, const coap_mid_t mid) {
(void)ctx;
(void)session;
(void)mid;
coap_pdu_type_t rcv_type = coap_pdu_get_type(received);
coap_pdu_code_t rcv_code = coap_pdu_get_code(received);
/* 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->type == COAP_MESSAGE_CON ||
received->type == COAP_MESSAGE_NON)) {
if (!sent && (rcv_type == COAP_MESSAGE_CON ||
rcv_type == COAP_MESSAGE_NON)) {
/* Cause a CoAP RST to be sent */
return COAP_RESPONSE_FAIL;
}
return COAP_RESPONSE_OK;
}
if (received->type == COAP_MESSAGE_RST) {
if (rcv_type == COAP_MESSAGE_RST) {
coap_log(LOG_INFO, "got RST\n");
return COAP_RESPONSE_OK;
}
/* Output the received data, if any */
if (COAP_RESPONSE_CLASS(received->code) == 2) {
if (COAP_RESPONSE_CLASS(rcv_code) == 2) {
/* Additional code to deal with the response */
}
+5 -5
View File
@@ -27,7 +27,7 @@ int _mode_);*
const coap_string_t *_query_);*
*int coap_cancel_observe(coap_session_t *_session_, coap_binary_t *_token_,
uint8_t _message_type_);*
coap_pdu_type_t _message_type_);*
For specific (D)TLS library support, link with
*-lcoap-@LIBCOAP_API_VERSION@-notls*, *-lcoap-@LIBCOAP_API_VERSION@-gnutls*,
@@ -167,12 +167,12 @@ coap_string_t *query, coap_pdu_t *response) {
tmp = gmtime(&now);
if (!tmp) {
/* If 'now' is not valid */
response->code = COAP_RESPONSE_CODE_NOT_FOUND;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_FOUND);
return;
}
len = strftime((char *)buf, sizeof(buf), "%b %d %H:%M:%S", tmp);
}
response->code = COAP_RESPONSE_CODE_CONTENT;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);
/*
* Invoke coap_add_data_blocked_response() to do all the hard work.
*
@@ -201,7 +201,7 @@ coap_string_t *query, coap_pdu_t *response) {
if (!uri_path) {
/* Unexpected Failure */
response->code = COAP_RESPONSE_CODE_BAD_REQUEST;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_BAD_REQUEST);
return;
}
@@ -215,7 +215,7 @@ coap_string_t *query, coap_pdu_t *response) {
/* Other resources code */
/* Failure response */
response->code = COAP_RESPONSE_CODE_BAD_REQUEST;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_BAD_REQUEST);
}
/* Initialize generic GET handler */
+356
View File
@@ -0,0 +1,356 @@
// -*- mode:doc; -*-
// vim: set syntax=asciidoc,tw=0:
coap_pdu_access(3)
=================
:doctype: manpage
:man source: coap_pdu_access
:man version: @PACKAGE_VERSION@
:man manual: libcoap Manual
NAME
----
coap_pdu_access,
coap_check_option,
coap_decode_var_bytes,
coap_decode_var_bytes8,
coap_get_data,
coap_opt_length,
coap_opt_value,
coap_option_filter_clear,
coap_option_filter_get,
coap_option_filter_set,
coap_option_filter_unset,
coap_option_iterator_init,
coap_option_next,
coap_pdu_get_code,
coap_pdu_get_mid,
coap_pdu_get_token,
coap_pdu_get_type
- Accessing CoAP PDUs
SYNOPSIS
--------
*#include <coap@LIBCOAP_API_VERSION@/coap.h>*
*coap_opt_t *coap_check_option(const coap_pdu_t *_pdu_,
coap_option_num_t _number_, coap_opt_iterator_t *_oi_);*
*unsigned int coap_decode_var_bytes(const uint8_t *_buf_, size_t _length_);*
*uint64_t coap_decode_var_bytes8(const uint8_t *_buf_, size_t _length_);*
*int coap_get_data(const coap_pdu_t *_pdu_, size_t *_length,
const uint8_t **_data_);*
*uint32_t coap_opt_length(const coap_opt_t *_opt_);*
*const uint8_t *coap_opt_value(const coap_opt_t *_opt_);*
*void coap_option_filter_clear(coap_opt_filter_t *_filter_);*
*int coap_option_filter_get(coap_opt_filter_t *_filter_,
coap_option_num_t _number_);*
*int coap_option_filter_set(coap_opt_filter_t *_filter_,
coap_option_num_t _number_)*;
*int coap_option_filter_unset(coap_opt_filter_t *_filter_,
coap_option_num_t _number_);*
*coap_opt_iterator_t *coap_option_iterator_init(const coap_pdu_t *_pdu_,
coap_opt_iterator_t *_oi_, const coap_opt_filter_t *_filter_);*
*coap_opt_t *coap_option_next(coap_opt_iterator_t *_oi_);*
*coap_pdu_code_t coap_pdu_get_code(const coap_pdu_t *_pdu_);*
*coap_mid_t coap_pdu_get_mid(const coap_pdu_t *_pdu_);*
*coap_bin_const_t coap_pdu_get_token(const coap_pdu_t *_pdu_);*
*coap_pdu_type_t coap_pdu_get_type(const coap_pdu_t *_pdu_);*
For specific (D)TLS library support, link with
*-lcoap-@LIBCOAP_API_VERSION@-notls*, *-lcoap-@LIBCOAP_API_VERSION@-gnutls*,
*-lcoap-@LIBCOAP_API_VERSION@-openssl*, *-lcoap-@LIBCOAP_API_VERSION@-mbedtls*
or *-lcoap-@LIBCOAP_API_VERSION@-tinydtls*. Otherwise, link with
*-lcoap-@LIBCOAP_API_VERSION@* to get the default (D)TLS library support.
DESCRIPTION
-----------
The CoAP PDU is of the form
--header--|--optional token--|--optional options--|--optional payload--
The terminology used is taken mainly from
https://tools.ietf.org/html/rfc7252#section-1.2
The following functions provide access to the information held within a PDU.
*Header:*
The *coap_pdu_get_type*() function returns one of the messages types below from
the PDU _pdu_.
[source, c]
----
COAP_MESSAGE_CON Type confirmable.
COAP_MESSAGE_NON Type non-confirmable.
COAP_MESSAGE_ACK Type acknowledge
COAP_MESSAGE_RST Type reset.
----
The *coap_pdu_get_code*() function returns one of the codes below from the
PDU _pdu_. It is possible that new codes are added in over time.
[source, c]
----
COAP_EMPTY_CODE 0.00
COAP_REQUEST_CODE_GET 0.01
COAP_REQUEST_CODE_POST 0.02
COAP_REQUEST_CODE_PUT 0.03
COAP_REQUEST_CODE_DELETE 0.04
COAP_REQUEST_CODE_FETCH 0.05
COAP_REQUEST_CODE_PATCH 0.06
COAP_REQUEST_CODE_IPATCH 0.07
COAP_RESPONSE_CODE_OK 2.00
COAP_RESPONSE_CODE_CREATED 2.01
COAP_RESPONSE_CODE_DELETED 2.02
COAP_RESPONSE_CODE_VALID 2.03
COAP_RESPONSE_CODE_CHANGED 2.04
COAP_RESPONSE_CODE_CONTENT 2.05
COAP_RESPONSE_CODE_CONTINUE 2.31
COAP_RESPONSE_CODE_BAD_REQUEST 4.00
COAP_RESPONSE_CODE_UNAUTHORIZED 4.01
COAP_RESPONSE_CODE_BAD_OPTION 4.02
COAP_RESPONSE_CODE_FORBIDDEN 4.03
COAP_RESPONSE_CODE_NOT_FOUND 4.04
COAP_RESPONSE_CODE_NOT_ALLOWED 4.05
COAP_RESPONSE_CODE_NOT_ACCEPTABLE 4.06
COAP_RESPONSE_CODE_INCOMPLETE 4.08
COAP_RESPONSE_CODE_CONFLICT 4.09
COAP_RESPONSE_CODE_PRECONDITION_FAILED 4.12
COAP_RESPONSE_CODE_REQUEST_TOO_LARGE 4.13
COAP_RESPONSE_CODE_UNSUPPORTED_CONTENT_FORMAT 4.15
COAP_RESPONSE_CODE_UNPROCESSABLE 4.22
COAP_RESPONSE_CODE_TOO_MANY_REQUESTS 4.29
COAP_RESPONSE_CODE_INTERNAL_ERROR 5.00
COAP_RESPONSE_CODE_NOT_IMPLEMENTED 5.01
COAP_RESPONSE_CODE_BAD_GATEWAY 5.02
COAP_RESPONSE_CODE_SERVICE_UNAVAILABLE 5.03
COAP_RESPONSE_CODE_GATEWAY_TIMEOUT 5.04
COAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED 5.05
COAP_RESPONSE_CODE_HOP_LIMIT_REACHED 5.08
COAP_SIGNALING_CODE_CSM 7.01
COAP_SIGNALING_CODE_PING 7.02
COAP_SIGNALING_CODE_PONG 7.03
COAP_SIGNALING_CODE_RELEASE 7.04
COAP_SIGNALING_CODE_ABORT 7.05
----
The *coap_pdu_get_mid*() returns the message id from the PDU _pdu_.
*Token:*
The *coap_pdu_get_token*() returns the token information held in the PDU _pdu_
which may be zero length.
*Options:*
The following is the current list of options with their numeric value
----
/*
* The C, U, and N flags indicate the properties
* Critical, Unsafe, and NoCacheKey, respectively.
* If U is set, then N has no meaning as per
* https://tools.ietf.org/html/rfc7252#section-5.10
* and is set to a -.
* Separately, R is for the options that can be repeated
*
* The least significant byte of the option is set as followed
* as per https://tools.ietf.org/html/rfc7252#section-5.4.6
*
* 0 1 2 3 4 5 6 7
* --+---+---+---+---+---+---+---+
* | NoCacheKey| U | C |
* --+---+---+---+---+---+---+---+
*
* https://tools.ietf.org/html/rfc8613#section-4 goes on to define E, I and U
* properties Encrypted and Integrity Protected, Integrity Protected Only and
* Unprotected respectively. Integrity Protected Only is not currently used.
*
* An Option is tagged with CUNREIU with any of the letters replaced with _ if
* not set, or - for N if U is set (see above) for aiding understanding of the
* Option.
*/
COAP_OPTION_IF_MATCH 1 /* C__RE__, opaque, 0-8 B, RFC7252 */
COAP_OPTION_URI_HOST 3 /* CU-___U, String, 1-255 B, RFC7252 */
COAP_OPTION_ETAG 4 /* ___RE__, opaque, 1-8 B, RFC7252 */
COAP_OPTION_IF_NONE_MATCH 5 /* C___E__, empty, 0 B, RFC7252 */
COAP_OPTION_OBSERVE 6 /* _U-_E_U, empty/uint, 0 B/0-3 B, RFC7641 */
COAP_OPTION_URI_PORT 7 /* CU-___U, uint, 0-2 B, RFC7252 */
COAP_OPTION_LOCATION_PATH 8 /* ___RE__, String, 0-255 B, RFC7252 */
COAP_OPTION_OSCORE 9 /* C_____U, *, 0-255 B, RFC8613 */
COAP_OPTION_URI_PATH 11 /* CU-RE__, String, 0-255 B, RFC7252 */
COAP_OPTION_CONTENT_FORMAT 12 /* ____E__, uint, 0-2 B, RFC7252 */
/* COAP_OPTION_MAXAGE default 60 seconds if not set */
COAP_OPTION_MAXAGE 14 /* _U-_E_U, uint, 0-4 B, RFC7252 */
COAP_OPTION_URI_QUERY 15 /* CU-RE__, String, 1-255 B, RFC7252 */
COAP_OPTION_HOP_LIMIT 16 /* ______U, uint, 1 B, RFC8768 */
COAP_OPTION_ACCEPT 17 /* C___E__, uint, 0-2 B, RFC7252 */
COAP_OPTION_LOCATION_QUERY 20 /* ___RE__, String, 0-255 B, RFC7252 */
COAP_OPTION_BLOCK2 23 /* CU-_E_U, uint, 0-3 B, RFC7959 */
COAP_OPTION_BLOCK1 27 /* CU-_E_U, uint, 0-3 B, RFC7959 */
COAP_OPTION_SIZE2 28 /* __N_E_U, uint, 0-4 B, RFC7959 */
COAP_OPTION_PROXY_URI 35 /* CU-___U, String, 1-1034 B, RFC7252 */
COAP_OPTION_PROXY_SCHEME 39 /* CU-___U, String, 1-255 B, RFC7252 */
COAP_OPTION_SIZE1 60 /* __N_E_U, uint, 0-4 B, RFC7252 */
COAP_OPTION_NORESPONSE 258 /* _U-_E_U, uint, 0-1 B, RFC7967 */
----
See FURTHER INFORMATION as to how to get the latest list.
The *coap_check_option*() function is used to check whether the specified option
_number_ is in the PDU _pdu_. The option iterator _oi_ is used as an internal
storage location while iterating through the options looking for the specific
_number_. If the _number_ is repeated in the _pdu_, only the first occurrence
will be returned. If the option is not found, NULL is returned.
Alternatively, the *coap_option_iterator_init*() function can be used to
initialize option iterator _oi_, applying a filter _filter_ to indicate which
options are to be ignored when iterating through the options. The _filter_ can
be NULL (or COAP_OPT_ALL) if all of the options are required. Then this is
followed by using the *coap_option_next*() function in a loop to return all
the appropriate options until NULL is returned - indicating the end of
available the options. See EXAMPLES below for further information.
To set up the filter, the following 4 functions are available.
The *coap_option_filter_clear*() function initializes _filter_ to have no
options set.
The *coap_option_filter_get*() function is used to check whether option _number_
is set in _filter_.
The *coap_option_filter_set*() function is used to set option _number_ in
_filter_.
The *coap_option_filter_unset*() function is used to remove option _number_ in
_filter_.
The *coap_opt_length*() function returns the length of the option _opt_.
The *coap_opt_val*() function returns a pointer to the start of the data for
the option.
The *coap_decode_var_bytes*() function will decode an option up to 4 bytes
long from _buf_ and _length_ into an unsigned 32bit number.
The *coap_decode_var_bytes*8() function will decode an option up to 8 bytes
long from _buf_ and _length_ into an unsigned 64bit number.
*Payload:*
The *coap_get_data*() function is used abstract from the _pdu_
information about the received data by updating _length_ with the length of
data available, and _data_ with a pointer to where the data is located.
*NOTE:* This function has been updated by *coap_get_data_large*() when large
transfers may take place. See coap_block(3).
EXAMPLES
--------
*Abstract information from PDU*
[source, c]
----
#include <coap@LIBCOAP_API_VERSION@/coap.h>
static void
get_pdu_information(coap_pdu_t *pdu) {
int ret;
/* Header variables */
coap_pdu_type_t pdu_type;
coap_pdu_code_t pdu_code;
coap_mid_t pdu_mid;
/* Token variables */
coap_bin_const_t pdu_token;
/* Option variables */
coap_opt_iterator_t opt_iter;
coap_opt_t *option;
coap_opt_filter_t ignore_options;
/* Data payload variables */
size_t pdu_data_length;
const uint8_t *pdu_data;
size_t pdu_data_offset;
size_t pdu_data_total_length;
/* Pull in the header information */
pdu_type = coap_pdu_get_type(pdu);
pdu_code = coap_pdu_get_code(pdu);
pdu_mid = coap_pdu_get_mid(pdu);
/* Pull in the token information */
pdu_token = coap_pdu_get_token(pdu);
/* Pull in the option information */
/* Specific option check */
option = coap_check_option(pdu, COAP_OPTION_OBSERVE, &opt_iter);
if (option) {
uint32_t value = coap_decode_var_bytes(coap_opt_value(option),
coap_opt_length(option));
coap_log(LOG_INFO, "Option OBSERVE, value %u\n", value);
}
/* Iterate through all options */
coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL);
while ((option = coap_option_next(&opt_iter))) {
coap_log(LOG_INFO, "A: Option %d, Length %u\n",
opt_iter.number, coap_opt_length(option));
}
/* Iterate through options, some ignored */
coap_option_filter_clear(&ignore_options);
coap_option_filter_set(&ignore_options, COAP_OPTION_OBSERVE);
coap_option_iterator_init(pdu, &opt_iter, &ignore_options);
while ((option = coap_option_next(&opt_iter))) {
coap_log(LOG_INFO, "I: Option %d, Length %u\n",
opt_iter.number, coap_opt_length(option));
}
/* Pull in the payload information */
ret = coap_get_data(pdu, &pdu_data_length, &pdu_data);
/* or */
ret = coap_get_data_large(pdu, &pdu_data_length, &pdu_data,
&pdu_data_offset, &pdu_data_total_length);
}
----
SEE ALSO
--------
*coap_block*(3) and *coap_pdu_setup*(3)
FURTHER INFORMATION
-------------------
See
"RFC7252: The Constrained Application Protocol (CoAP)"
"RFC8613: Object Security for Constrained RESTful Environments (OSCORE)"
for further information.
See https://www.iana.org/assignments/core-parameters/core-parameters.xhtml#option-numbers
for the current set of defined CoAP Options.
BUGS
----
Please report bugs on the mailing list for libcoap:
libcoap-developers@lists.sourceforge.net or raise an issue on GitHub at
https://github.com/obgm/libcoap/issues
AUTHORS
-------
The libcoap project <libcoap-developers@lists.sourceforge.net>
+169 -77
View File
@@ -11,6 +11,7 @@ coap_pdu_setup(3)
NAME
----
coap_pdu_setup,
coap_new_pdu,
coap_pdu_init,
coap_session_init_token,
coap_session_new_token,
@@ -26,15 +27,21 @@ coap_add_data,
coap_add_data_blocked_response,
coap_send,
coap_split_path,
coap_split_query
- Work with CoAP PDUs
coap_split_query,
coap_pdu_set_mid,
coap_pdu_set_code,
coap_pdu_set_type
- Setting up CoAP PDUs
SYNOPSIS
--------
*#include <coap@LIBCOAP_API_VERSION@/coap.h>*
*coap_pdu_t *coap_pdu_init(uint8_t _type_, uint8_t _code_,
uint16_t _message_id_, size_t _max_size_);*
*coap_pdu_t *coap_new_pdu(coap_pdu_type_t _type_, coap_pdu_code_t _code_,
coap_session_t *_session_);*
*coap_pdu_t *coap_pdu_init(coap_pdu_type_t _type_, coap_pdu_code_t _code_,
coap_mid_t _message_id_, size_t _max_size_);*
*void coap_session_init_token(coap_session_t *_session_, size_t _length_,
const uint8_t *_token_);*
@@ -79,6 +86,12 @@ size_t *_buflen_);*
*int coap_split_query(const uint8_t *_query_, size_t _length_,
uint8_t *_buffer_, size_t *_buflen_);*
*void coap_pdu_set_mid(coap_pdu_t *_pdu_, coap_mid_t _mid_);*
*void coap_pdu_set_code(coap_pdu_t *_pdu_, coap_pdu_code_t _code_);*
*void coap_pdu_set_type(coap_pdu_t *_pdu_, coap_pdu_type_t _type_);*
For specific (D)TLS library support, link with
*-lcoap-@LIBCOAP_API_VERSION@-notls*, *-lcoap-@LIBCOAP_API_VERSION@-gnutls*,
*-lcoap-@LIBCOAP_API_VERSION@-openssl*, *-lcoap-@LIBCOAP_API_VERSION@-mbedtls*
@@ -91,6 +104,9 @@ The CoAP PDU is of the form
--header--|--optional token--|--optional options--|--optional payload--
The terminology used is taken mainly from
https://tools.ietf.org/html/rfc7252#section-1.2
The PDU must be built in the correct order, from left to right. In particular,
the options need to be added in the correct numerical option order as they are
stored in the PDU using relative numeric offsets from the previous option
@@ -107,50 +123,105 @@ Typically with a server, the response PDU, with the optional token already added
in, will already be created before the response handler is called, and the
response PDU will need to be updated as appropriate starting with the optional
options. Note that updating the response pdu's code variable will cause the
response pdu to get transmitted. If code does not get updated, then the
response pdu is freed off by the underlying library.
response pdu to get transmitted. If code does not get updated, and the PDU is
of type CONFIRMABLE, then the response PDU is transmitted as an empty ACK
packet. The response pdu is always freed off by the underlying library.
For handling situations where the data to be transmitted does not fit into a
single packet, see coap_block(3).
The *coap_pdu_init*() function returns a newly created _PDU_ of type
_coap_pdu_t_*. _type_ is one of the following
*Create and Header:*
[horizontal]
*COAP_MESSAGE_CON*::
Set the _PDU_ to be of type confirmable.
*COAP_MESSAGE_NON*::
Set the _PDU_ to be of type non-confirmable.
*COAP_MESSAGE_ACK*::
Set the _PDU_ to be of type acknowledge (for internal use).
*COAP_MESSAGE_RST*::
Set the _PDU_ to be of type reset.
The *coap_new_pdu*() function returns a newly created PDU of type
_coap_pdu_t_*. The _type_ is one of the following
[source, c]
----
COAP_MESSAGE_CON Set the _PDU_ to be of type confirmable.
COAP_MESSAGE_NON Set the _PDU_ to be of type non-confirmable.
COAP_MESSAGE_ACK Set the _PDU_ to be of type acknowledge (for internal use).
COAP_MESSAGE_RST Set the _PDU_ to be of type reset.
----
The _code_ is one of the following
[horizontal]
*COAP_REQUEST_GET*::
Set the _PDU_ request to be of type GET.
*COAP_REQUEST_POST*::
Set the _PDU_ request to be of type POST.
*COAP_REQUEST_PUT*::
Set the _PDU_ request to be of type PUT.
*COAP_REQUEST_DELETE*::
Set the _PDU_ request to be of type DELETE.
*COAP_REQUEST_FETCH*::
Set the _PDU_ request to be of type FETCH.
*COAP_REQUEST_PATCH*::
Set the _PDU_ request to be of type PATCH.
*COAP_REQUEST_IPATCH*::
Set the _PDU_ request to be of type IPATCH.
[source, c]
----
COAP_EMPTY_CODE 0.00
COAP_REQUEST_CODE_GET 0.01
COAP_REQUEST_CODE_POST 0.02
COAP_REQUEST_CODE_PUT 0.03
COAP_REQUEST_CODE_DELETE 0.04
COAP_REQUEST_CODE_FETCH 0.05
COAP_REQUEST_CODE_PATCH 0.06
COAP_REQUEST_CODE_IPATCH 0.07
COAP_RESPONSE_CODE_OK 2.00
COAP_RESPONSE_CODE_CREATED 2.01
COAP_RESPONSE_CODE_DELETED 2.02
COAP_RESPONSE_CODE_VALID 2.03
COAP_RESPONSE_CODE_CHANGED 2.04
COAP_RESPONSE_CODE_CONTENT 2.05
COAP_RESPONSE_CODE_CONTINUE 2.31
COAP_RESPONSE_CODE_BAD_REQUEST 4.00
COAP_RESPONSE_CODE_UNAUTHORIZED 4.01
COAP_RESPONSE_CODE_BAD_OPTION 4.02
COAP_RESPONSE_CODE_FORBIDDEN 4.03
COAP_RESPONSE_CODE_NOT_FOUND 4.04
COAP_RESPONSE_CODE_NOT_ALLOWED 4.05
COAP_RESPONSE_CODE_NOT_ACCEPTABLE 4.06
COAP_RESPONSE_CODE_INCOMPLETE 4.08
COAP_RESPONSE_CODE_CONFLICT 4.09
COAP_RESPONSE_CODE_PRECONDITION_FAILED 4.12
COAP_RESPONSE_CODE_REQUEST_TOO_LARGE 4.13
COAP_RESPONSE_CODE_UNSUPPORTED_CONTENT_FORMAT 4.15
COAP_RESPONSE_CODE_UNPROCESSABLE 4.22
COAP_RESPONSE_CODE_TOO_MANY_REQUESTS 4.29
COAP_RESPONSE_CODE_INTERNAL_ERROR 5.00
COAP_RESPONSE_CODE_NOT_IMPLEMENTED 5.01
COAP_RESPONSE_CODE_BAD_GATEWAY 5.02
COAP_RESPONSE_CODE_SERVICE_UNAVAILABLE 5.03
COAP_RESPONSE_CODE_GATEWAY_TIMEOUT 5.04
COAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED 5.05
COAP_RESPONSE_CODE_HOP_LIMIT_REACHED 5.08
COAP_SIGNALING_CODE_CSM 7.01
COAP_SIGNALING_CODE_PING 7.02
COAP_SIGNALING_CODE_PONG 7.03
COAP_SIGNALING_CODE_RELEASE 7.04
COAP_SIGNALING_CODE_ABORT 7.05
----
and _session_ is used to set up other default values.
The *coap_pdu_init*() function does the same work as *coap_new_pdu*() but gives
the additional ability to define the default values for _message_id_ and
_max_size_ that *coap_new_pdu*() creates.
The _message_id_ must be unique per request (which is not the same as the
token), and must not be reused within EXCHANGE_LIFETIME (usually 247 seconds).
To automate this, the function coap_new_message_id(session) should be called.
At the CoAP protocol level, requests and responses are matched by _message_id_
which is why it needs to be unique. At the application level, for "separate"
responses, the initial empty ACK response matches the _message_id_ of the
request (handled by libcoap) but the actual response has the same token as the
request and this must be used for the match. For "piggybacked" responses the
token must still be used as the valid match for request and response. and the
_message_id_ just happens to match (but unsafe in case the server is sending
back a "separate" response).
The _max_size_ parameter defines the maximum size of a _PDU_ and is usually
determined by calling coap_session_max_pdu_size(session);
The *coap_pdu_set_mid*() function is used to set the message id _mid_ in the
PDU _pdu_.
The *coap_pdu_set_code*() function is used to set the code _code_ in the PDU
_pdu_.
The *coap_pdu_set_type*() function is used to set the _type_ of the PDU _pdu_.
*Token:*
The *coap_session_init_token*() function is used to initialize the starting
_token_ of _length_ for the _session_.
@@ -166,11 +237,22 @@ Adding the token must be done before any options or data are added.
This function must only be called once per _pdu_, and must not be called
in the appropriate response handler.
If a token is not added, then the token in the PDU is zero length, but still a
valid token which is used for matching. The exception is an empty ACK packet.
*Options:*
The *coap_new_optlist*() function returns a newly created _optlist_ entry of
type _coap_optlist_t_*. The _number_ specifies which CoAP option is to be
used, and is one of the COAP_OPTION_* definitions. The _length_ is the length
of the data of the option, and _data_ points to the content of the option.
*NOTE:* Where possible, the option data needs to be stripped of leading zeros
(big endian) to reduce the amount of data needed in the PDU, as well as in
some cases the maximum data size of an opton can be exceeded if not stripped
and hence be illegal. This is done by using coap_encode_var_safe() or
coap_encode_var_safe8().
The following is the current list of options with their numeric value
----
/*
@@ -191,36 +273,36 @@ The following is the current list of options with their numeric value
*
* https://tools.ietf.org/html/rfc8613#section-4 goes on to define E, I and U
* properties Encrypted and Integrity Protected, Integrity Protected Only and
* Unprotected respectively. Integretity Protected Only is not currently used.
* Unprotected respectively. Integrity Protected Only is not currently used.
*
* An Option is tagged with CUNREIU with any of the letters replaced with _ if
* not set, or - for N if U is set (see above) for aiding understanding of the
* Option.
*/
#define COAP_OPTION_IF_MATCH 1 /* C__RE__, opaque, 0-8 B, RFC7252 */
#define COAP_OPTION_URI_HOST 3 /* CU-___U, String, 1-255 B, RFC7252 */
#define COAP_OPTION_ETAG 4 /* ___RE__, opaque, 1-8 B, RFC7252 */
#define COAP_OPTION_IF_NONE_MATCH 5 /* C___E__, empty, 0 B, RFC7252 */
#define COAP_OPTION_OBSERVE 6 /* _U-_E_U, empty/uint, 0 B/0-3 B, RFC7641 */
#define COAP_OPTION_URI_PORT 7 /* CU-___U, uint, 0-2 B, RFC7252 */
#define COAP_OPTION_LOCATION_PATH 8 /* ___RE__, String, 0-255 B, RFC7252 */
#define COAP_OPTION_OSCORE 9 /* C_____U, *, 0-255 B, RFC8613 */
#define COAP_OPTION_URI_PATH 11 /* CU-RE__, String, 0-255 B, RFC7252 */
#define COAP_OPTION_CONTENT_FORMAT 12 /* ____E__, uint, 0-2 B, RFC7252 */
COAP_OPTION_IF_MATCH 1 /* C__RE__, opaque, 0-8 B, RFC7252 */
COAP_OPTION_URI_HOST 3 /* CU-___U, String, 1-255 B, RFC7252 */
COAP_OPTION_ETAG 4 /* ___RE__, opaque, 1-8 B, RFC7252 */
ION_IF_NONE_MATCH 5 /* C___E__, empty, 0 B, RFC7252 */
COAP_OPTION_OBSERVE 6 /* _U-_E_U, empty/uint, 0 B/0-3 B, RFC7641 */
COAP_OPTION_URI_PORT 7 /* CU-___U, uint, 0-2 B, RFC7252 */
COAP_OPTION_LOCATION_PATH 8 /* ___RE__, String, 0-255 B, RFC7252 */
COAP_OPTION_OSCORE 9 /* C_____U, *, 0-255 B, RFC8613 */
COAP_OPTION_URI_PATH 11 /* CU-RE__, String, 0-255 B, RFC7252 */
COAP_OPTION_CONTENT_FORMAT 12 /* ____E__, uint, 0-2 B, RFC7252 */
/* COAP_OPTION_MAXAGE default 60 seconds if not set */
#define COAP_OPTION_MAXAGE 14 /* _U-_E_U, uint, 0-4 B, RFC7252 */
#define COAP_OPTION_URI_QUERY 15 /* CU-RE__, String, 1-255 B, RFC7252 */
#define COAP_OPTION_HOP_LIMIT 16 /* ______U, uint, 1 B, RFC8768 */
#define COAP_OPTION_ACCEPT 17 /* C___E__, uint, 0-2 B, RFC7252 */
#define COAP_OPTION_LOCATION_QUERY 20 /* ___RE__, String, 0-255 B, RFC7252 */
#define COAP_OPTION_BLOCK2 23 /* CU-_E_U, uint, 0-3 B, RFC7959 */
#define COAP_OPTION_BLOCK1 27 /* CU-_E_U, uint, 0-3 B, RFC7959 */
#define COAP_OPTION_SIZE2 28 /* __N_E_U, uint, 0-4 B, RFC7959 */
#define COAP_OPTION_PROXY_URI 35 /* CU-___U, String, 1-1034 B, RFC7252 */
#define COAP_OPTION_PROXY_SCHEME 39 /* CU-___U, String, 1-255 B, RFC7252 */
#define COAP_OPTION_SIZE1 60 /* __N_E_U, uint, 0-4 B, RFC7252 */
#define COAP_OPTION_NORESPONSE 258 /* _U-_E_U, uint, 0-1 B, RFC7967 */
COAP_OPTION_MAXAGE 14 /* _U-_E_U, uint, 0-4 B, RFC7252 */
COAP_OPTION_URI_QUERY 15 /* CU-RE__, String, 1-255 B, RFC7252 */
COAP_OPTION_HOP_LIMIT 16 /* ______U, uint, 1 B, RFC8768 */
COAP_OPTION_ACCEPT 17 /* C___E__, uint, 0-2 B, RFC7252 */
COAP_OPTION_LOCATION_QUERY 20 /* ___RE__, String, 0-255 B, RFC7252 */
COAP_OPTION_BLOCK2 23 /* CU-_E_U, uint, 0-3 B, RFC7959 */
COAP_OPTION_BLOCK1 27 /* CU-_E_U, uint, 0-3 B, RFC7959 */
COAP_OPTION_SIZE2 28 /* __N_E_U, uint, 0-4 B, RFC7959 */
COAP_OPTION_PROXY_URI 35 /* CU-___U, String, 1-1034 B, RFC7252 */
COAP_OPTION_PROXY_SCHEME 39 /* CU-___U, String, 1-255 B, RFC7252 */
COAP_OPTION_SIZE1 60 /* __N_E_U, uint, 0-4 B, RFC7252 */
COAP_OPTION_NORESPONSE 258 /* _U-_E_U, uint, 0-1 B, RFC7967 */
----
See FURTHER INFORMATION as to how to get the latest list.
@@ -244,6 +326,32 @@ with _data_ of length _length_ to the PDU _pdu_.
It is important that options are added to the _pdu_ with _number_ either
being the same or greater than the previous option _number_ that was added.
*NOTE:* Where possible, the option data needs to be stripped of leading zeros
(big endian) to reduce the amount of data needed in the PDU, as well as in
some cases the maximum data size of an opton can be exceeded if not stripped
and hence be illegal. This is done by using coap_encode_var_safe() or
coap_encode_var_safe8().
The *coap_encode_var_safe*() function encodes _value_ into _buffer_ which has
a size of _size_ in bytes. Normally, the _buffer_ size should be at least
the sizeof(int) bytes unless you definitely know less space is required.
The *coap_encode_var_safe8*() function encodes 8 byte _value_ into _buffer_
which has a size of _size_ in bytes. Normally, the _buffer_ size should be at
least 8 bytes unless you definitely know less space is required.
The *coap_split_path*() function splits up _path_ of length _length_ and
places the result in _buffer_ which has a size of _buflen_. _buflen_ needs
to be preset with the size of _buffer_ before the function call, and then
_buflen_ is updated with the actual size of _buffer_ used.
The *coap_split_query*() function splits up _query_ of length _length_ and
places the result in _buffer_ which has a size of _buflen_. _buflen_ needs
to be preset with the size of _buffer_ before the function call, and then
_buflen_ is updated with the actual size of _buffer_ used.
*Payload Data:*
The *coap_add_data*() function adds in the specified payload _data_ of length
_length_ to the PDU _pdu_. Adding the payload data must be done after any
token or options are added. This function must only be called once per _pdu_.
@@ -267,31 +375,15 @@ by this function.
*NOTE:* This function has been superseded by *coap_add_data_response_large*().
See coap_block(3).
The *coap_encode_var_safe*() function encodes _value_ into _buffer_ which has
a size of _size_ in bytes. Normally, the _buffer_ size should be at least
the sizeof(int) bytes unless you definitely know less space is required.
The *coap_encode_var_safe8*() function encodes 8 byte _value_ into _buffer_
which has a size of _size_ in bytes. Normally, the _buffer_ size should be at
least 8 bytes unless you definitely know less space is required.
*Transmit:*
The *coap_send*() function is used to initiate the transmission of the _pdu_
associated with the _session_.
The *coap_split_path*() function splits up _path_ of length _length_ and
places the result in _buffer_ which has a size of _buflen_. _buflen_ needs
to be preset with the size of _buffer_ before the function call, and then
_buflen_ is updated with the actual size of _buffer_ used.
The *coap_split_query*() function splits up _query_ of length _length_ and
places the result in _buffer_ which has a size of _buflen_. _buflen_ needs
to be preset with the size of _buffer_ before the function call, and then
_buflen_ is updated with the actual size of _buffer_ used.
RETURN VALUES
-------------
The *coap_pdu_init*() function returns a newly created _PDU_ or NULL if there
is a malloc or parameter failure.
The *coap_new_pdu*() and *coap_pdu_init*() function returns a newly created
_PDU_ or NULL if there is a malloc or parameter failure.
The *coap_new_optlist*() function returns a newly created _optlist_ or NULL
if there is a malloc failure.
@@ -448,12 +540,12 @@ coap_string_t *query, coap_pdu_t *response) {
tmp = gmtime(&now);
if (!tmp) {
/* If 'now' is not valid */
response->code = COAP_RESPONSE_CODE_NOT_FOUND;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_FOUND);
return;
}
len = strftime((char *)buf, sizeof(buf), "%b %d %H:%M:%S", tmp);
}
response->code = COAP_RESPONSE_CODE_CONTENT;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);
/*
* Invoke coap_add_data_blocked_response() to do all the hard work.
*
@@ -475,7 +567,7 @@ coap_string_t *query, coap_pdu_t *response) {
SEE ALSO
--------
*coap_block*(3), *coap_observe*(3) and *coap_resource*(3)
*coap_block*(3), *coap_observe*(3), *coap_pdu_access*(3) and *coap_resource*(3)
FURTHER INFORMATION
-------------------
+15 -14
View File
@@ -207,7 +207,7 @@ coap_string_t *query, coap_pdu_t *response) {
(void)token;
(void)query;
response->code = COAP_RESPONSE_CODE_CONTENT;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);
coap_add_option(response,
COAP_OPTION_CONTENT_TYPE,
@@ -221,7 +221,7 @@ coap_string_t *query, coap_pdu_t *response) {
coap_add_data(response, strlen(INDEX), (const uint8_t *)INDEX);
response->code = COAP_RESPONSE_CODE_CONTENT;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);
}
static void
@@ -238,7 +238,7 @@ coap_string_t *query, coap_pdu_t *response) {
/* .. code .. */
response->code = COAP_RESPONSE_CODE_DELETED;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_DELETED);
}
static void
@@ -256,7 +256,7 @@ coap_string_t *query, coap_pdu_t *response) {
/* .. code .. */
response->code = COAP_RESPONSE_CODE_CONTENT;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);
}
static void
@@ -274,7 +274,7 @@ coap_string_t *query, coap_pdu_t *response) {
/* .. code .. */
response->code = COAP_RESPONSE_CODE_CHANGED;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CHANGED);
}
static void
@@ -340,7 +340,7 @@ coap_string_t *query, coap_pdu_t *response) {
/* Dynamic resource no longer required - delete it */
coap_delete_resource(ctx, resource);
response->code = COAP_RESPONSE_CODE_DELETED;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_DELETED);
}
/* Regular GET handler - used by resources created by the
@@ -371,7 +371,7 @@ coap_string_t *query, coap_pdu_t *response) {
/* .. code .. */
response->code = COAP_RESPONSE_CODE_CONTENT;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);
}
/* Regular PUT handler - used by resources created by the
@@ -398,7 +398,7 @@ coap_string_t *query, coap_pdu_t *response) {
/* get the uri_path */
put_uri_path = coap_get_uri_path(request);
if (!put_uri_path) {
response->code = COAP_RESPONSE_CODE_NOT_FOUND;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_FOUND);
return;
}
coap_get_data_large(request, &length, &data, &offset, &total);
@@ -409,11 +409,11 @@ coap_string_t *query, coap_pdu_t *response) {
coap_delete_string(put_uri_path);
if (length + offset < total)
response->code = COAP_RESPONSE_CODE_CONTINUE;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTINUE);
else if (new_resource)
response->code = COAP_RESPONSE_CODE_CREATED;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CREATED);
else
response->code = COAP_RESPONSE_CODE_CHANGED;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CHANGED);
}
static int
@@ -436,6 +436,7 @@ coap_string_t *query, coap_pdu_t *response) {
/* Remove (void) definition if variable is used */
(void)ctx;
(void)resource;
coap_pdu_code_t req_code = coap_pdu_get_code(request);
coap_resource_t *r;
coap_string_t *uri_path;
@@ -443,13 +444,13 @@ coap_string_t *query, coap_pdu_t *response) {
/* get the uri_path - which will get used by coap_resource_init() */
uri_path = coap_get_uri_path(request);
if (!uri_path) {
response->code = COAP_RESPONSE_CODE_NOT_FOUND;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_FOUND);
return;
}
/* Check if new URI Path is valid */
if (!check_url_fn (uri_path, request->code)) {
response->code = COAP_RESPONSE_CODE_NOT_FOUND;
if (!check_url_fn (uri_path, req_code)) {
coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_FOUND);
coap_delete_string(uri_path);
return;
}
+1 -1
View File
@@ -38,7 +38,7 @@ coap_register_async(coap_context_t *context, coap_session_t *session,
coap_async_t *s;
coap_mid_t mid = request->mid;
size_t len;
uint8_t *data;
const uint8_t *data;
if (!COAP_PDU_IS_REQUEST(request))
return NULL;
+14 -14
View File
@@ -32,14 +32,14 @@ coap_opt_block_num(const coap_opt_t *block_opt) {
}
int
coap_get_block(coap_pdu_t *pdu, uint16_t type, coap_block_t *block) {
coap_get_block(coap_pdu_t *pdu, coap_option_num_t number, coap_block_t *block) {
coap_opt_iterator_t opt_iter;
coap_opt_t *option;
assert(block);
memset(block, 0, sizeof(coap_block_t));
if (pdu && (option = coap_check_option(pdu, type, &opt_iter)) != NULL) {
if (pdu && (option = coap_check_option(pdu, number, &opt_iter)) != NULL) {
unsigned int num;
block->szx = COAP_OPT_BLOCK_SZX(option);
@@ -60,7 +60,7 @@ coap_get_block(coap_pdu_t *pdu, uint16_t type, coap_block_t *block) {
}
int
coap_write_block_opt(coap_block_t *block, uint16_t type,
coap_write_block_opt(coap_block_t *block, coap_option_num_t number,
coap_pdu_t *pdu, size_t data_length) {
size_t start, want, avail;
unsigned char buf[4];
@@ -111,10 +111,10 @@ coap_write_block_opt(coap_block_t *block, uint16_t type,
}
/* to re-encode the block option */
coap_add_option(pdu, type, coap_encode_var_safe(buf, sizeof(buf),
((block->num << 4) |
(block->m << 3) |
block->szx)),
coap_add_option(pdu, number, coap_encode_var_safe(buf, sizeof(buf),
((block->num << 4) |
(block->m << 3) |
block->szx)),
buf);
return 1;
@@ -190,7 +190,7 @@ coap_add_data_blocked_response(coap_resource_t *resource,
buf);
}
coap_add_option(response, COAP_OPTION_CONTENT_TYPE,
coap_add_option(response, COAP_OPTION_CONTENT_FORMAT,
coap_encode_var_safe(buf, sizeof(buf),
media_type),
buf);
@@ -291,7 +291,7 @@ full_match(const uint8_t *a, size_t alen,
int
coap_cancel_observe(coap_session_t *session, coap_binary_t *token,
uint8_t type) {
coap_pdu_type_t type) {
coap_lg_crcv_t *cq;
assert(session);
@@ -1047,7 +1047,7 @@ coap_handle_request_send_block(coap_session_t *session,
coap_option_iterator_init(pdu, &opt_b_iter, COAP_OPT_ALL);
while ((option = coap_option_next(&opt_b_iter))) {
unsigned int num;
if (opt_b_iter.type != p->option)
if (opt_b_iter.number != p->option)
continue;
num = coap_opt_block_num(option);
if (num > 0xFFFFF) /* 20 bits max for num */
@@ -1098,9 +1098,9 @@ coap_handle_request_send_block(coap_session_t *session,
*/
coap_option_iterator_init(&p->pdu, &opt_iter, COAP_OPT_ALL);
while ((option = coap_option_next(&opt_iter))) {
if (opt_iter.type == COAP_OPTION_OBSERVE && block.num != 0)
if (opt_iter.number == COAP_OPTION_OBSERVE && block.num != 0)
continue;
if (!coap_add_option(response, opt_iter.type,
if (!coap_add_option(response, opt_iter.number,
coap_opt_length(option),
coap_opt_value(option))) {
goto internal_issue;
@@ -1654,7 +1654,7 @@ coap_handle_response_get_block(coap_context_t *context,
if (COAP_RESPONSE_CLASS(rcvd->code) == 2) {
size_t length;
uint8_t *data;
const uint8_t *data;
coap_opt_t *size_opt = coap_check_option(rcvd, COAP_OPTION_SIZE2,
&opt_iter);
size_t size2 = size_opt ?
@@ -1936,7 +1936,7 @@ fail_resp:
if (block.num != 0) {
/* Assume random access and just give the single response to app */
size_t length;
uint8_t *data;
const uint8_t *data;
size_t chunk = (size_t)1 << (block.szx + 4);
coap_get_data(rcvd, &length, &data);
+3 -3
View File
@@ -87,7 +87,7 @@ coap_cache_derive_key(const coap_session_t *session,
}
}
while ((option = coap_option_next(&opt_iter))) {
if (is_cache_key(session->context, opt_iter.type)) {
if (is_cache_key(session->context, opt_iter.number)) {
if (!coap_digest_update(dctx, option, coap_opt_size(option))) {
coap_digest_free(dctx);
return NULL;
@@ -97,9 +97,9 @@ coap_cache_derive_key(const coap_session_t *session,
/* The body of a FETCH payload is part of the cache key,
* see https://tools.ietf.org/html/rfc8132#section-2 */
if (pdu->code == COAP_REQUEST_FETCH) {
if (pdu->code == COAP_REQUEST_CODE_FETCH) {
size_t len;
uint8_t *data;
const uint8_t *data;
if (coap_get_data(pdu, &len, &data)) {
if (!coap_digest_update(dctx, data, len)) {
coap_digest_free(dctx);
+16 -16
View File
@@ -519,7 +519,7 @@ coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu) {
coap_opt_t *option;
int content_format = -1;
size_t data_len;
unsigned char *data;
const uint8_t *data;
uint32_t opt_len;
const uint8_t* opt_val;
size_t outbuflen = 0;
@@ -558,7 +558,7 @@ coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu) {
snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, ",");
}
if (pdu->code == COAP_SIGNALING_CSM) switch(opt_iter.type) {
if (pdu->code == COAP_SIGNALING_CODE_CSM) switch(opt_iter.number) {
case COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE:
buf_len = snprintf((char *)buf, sizeof(buf), "%u",
coap_decode_var_bytes(coap_opt_value(option),
@@ -567,10 +567,10 @@ coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu) {
default:
buf_len = 0;
break;
} else if (pdu->code == COAP_SIGNALING_PING
|| pdu->code == COAP_SIGNALING_PONG) {
} else if (pdu->code == COAP_SIGNALING_CODE_PING
|| pdu->code == COAP_SIGNALING_CODE_PONG) {
buf_len = 0;
} else if (pdu->code == COAP_SIGNALING_RELEASE) switch(opt_iter.type) {
} else if (pdu->code == COAP_SIGNALING_CODE_RELEASE) switch(opt_iter.number) {
case COAP_SIGNALING_OPTION_ALTERNATIVE_ADDRESS:
buf_len = print_readable(coap_opt_value(option),
coap_opt_length(option),
@@ -584,7 +584,7 @@ coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu) {
default:
buf_len = 0;
break;
} else if (pdu->code == COAP_SIGNALING_ABORT) switch(opt_iter.type) {
} else if (pdu->code == COAP_SIGNALING_CODE_ABORT) switch(opt_iter.number) {
case COAP_SIGNALING_OPTION_BAD_CSM_OPTION:
buf_len = snprintf((char *)buf, sizeof(buf), "%u",
coap_decode_var_bytes(coap_opt_value(option),
@@ -593,7 +593,7 @@ coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu) {
default:
buf_len = 0;
break;
} else switch (opt_iter.type) {
} else switch (opt_iter.number) {
case COAP_OPTION_CONTENT_FORMAT:
case COAP_OPTION_ACCEPT:
content_format = (int)coap_decode_var_bytes(coap_opt_value(option),
@@ -639,13 +639,13 @@ coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu) {
break;
default:
/* generic output function for all other option types */
if (opt_iter.type == COAP_OPTION_URI_PATH ||
opt_iter.type == COAP_OPTION_PROXY_URI ||
opt_iter.type == COAP_OPTION_URI_HOST ||
opt_iter.type == COAP_OPTION_LOCATION_PATH ||
opt_iter.type == COAP_OPTION_LOCATION_QUERY ||
opt_iter.type == COAP_OPTION_PROXY_SCHEME ||
opt_iter.type == COAP_OPTION_URI_QUERY) {
if (opt_iter.number == COAP_OPTION_URI_PATH ||
opt_iter.number == COAP_OPTION_PROXY_URI ||
opt_iter.number == COAP_OPTION_URI_HOST ||
opt_iter.number == COAP_OPTION_LOCATION_PATH ||
opt_iter.number == COAP_OPTION_LOCATION_QUERY ||
opt_iter.number == COAP_OPTION_PROXY_SCHEME ||
opt_iter.number == COAP_OPTION_URI_QUERY) {
encode = 0;
} else {
encode = 1;
@@ -658,7 +658,7 @@ coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu) {
outbuflen = strlen(outbuf);
snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen,
" %s:%.*s", msg_option_string(pdu->code, opt_iter.type),
" %s:%.*s", msg_option_string(pdu->code, opt_iter.number),
(int)buf_len, buf);
}
@@ -672,7 +672,7 @@ coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu) {
if (is_binary(content_format) || !isprint(data[0])) {
size_t keep_data_len = data_len;
uint8_t *keep_data = data;
const uint8_t *keep_data = data;
outbuflen = strlen(outbuf);
snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen,
+2 -2
View File
@@ -355,7 +355,7 @@ void coap_session_send_csm(coap_session_t *session) {
session->partial_write = 0;
if (session->mtu == 0)
session->mtu = COAP_DEFAULT_MTU; /* base value */
pdu = coap_pdu_init(COAP_MESSAGE_CON, COAP_SIGNALING_CSM, 0, 20);
pdu = coap_pdu_init(COAP_MESSAGE_CON, COAP_SIGNALING_CODE_CSM, 0, 20);
if ( pdu == NULL
|| coap_add_option(pdu, COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE,
coap_encode_var_safe(buf, sizeof(buf),
@@ -386,7 +386,7 @@ coap_mid_t coap_session_send_ping(coap_session_t *session) {
}
#if !COAP_DISABLE_TCP
else {
ping = coap_pdu_init(COAP_MESSAGE_CON, COAP_SIGNALING_PING, 0, 1);
ping = coap_pdu_init(COAP_MESSAGE_CON, COAP_SIGNALING_CODE_PING, 0, 1);
}
#endif /* !COAP_DISABLE_TCP */
if (!ping)
+28 -23
View File
@@ -712,9 +712,9 @@ coap_option_check_critical(coap_context_t *ctx,
* the largest known option, we know that everything beyond is
* bad.
*/
if (opt_iter.type & 0x01) {
if (opt_iter.number & 0x01) {
/* first check the built-in critical options */
switch (opt_iter.type) {
switch (opt_iter.number) {
case COAP_OPTION_IF_MATCH:
case COAP_OPTION_URI_HOST:
case COAP_OPTION_IF_NONE_MATCH:
@@ -728,14 +728,14 @@ coap_option_check_critical(coap_context_t *ctx,
case COAP_OPTION_BLOCK1:
break;
default:
if (coap_option_filter_get(&ctx->known_options, opt_iter.type) <= 0) {
coap_log(LOG_DEBUG, "unknown critical option %d\n", opt_iter.type);
if (coap_option_filter_get(&ctx->known_options, opt_iter.number) <= 0) {
coap_log(LOG_DEBUG, "unknown critical option %d\n", opt_iter.number);
ok = 0;
/* When opt_iter.type is beyond our known option range,
/* When opt_iter.number is beyond our known option range,
* coap_option_filter_set() will return -1 and we are safe to leave
* this loop. */
if (coap_option_filter_set(&unknown, opt_iter.type) == -1) {
if (coap_option_filter_set(&unknown, opt_iter.number) == -1) {
break;
}
}
@@ -785,6 +785,7 @@ coap_session_send_pdu(coap_session_t *session, coap_pdu_t *pdu) {
pdu->used_size + pdu->hdr_size);
#endif /* !COAP_DISABLE_TCP */
break;
case COAP_PROTO_NONE:
default:
break;
}
@@ -1088,7 +1089,7 @@ coap_send_large(coap_session_t *session, coap_pdu_t *pdu) {
*/
if (observe_action != -1 || have_block1 ||
((pdu->type == COAP_MESSAGE_NON || COAP_PROTO_RELIABLE(session->proto)) &&
COAP_PDU_IS_REQUEST(pdu) && pdu->code != COAP_REQUEST_DELETE)) {
COAP_PDU_IS_REQUEST(pdu) && pdu->code != COAP_REQUEST_CODE_DELETE)) {
/* See if this token is already in use for large body responses */
LL_FOREACH(session->lg_crcv, lg_crcv) {
if (token_match(pdu->token, pdu->token_length,
@@ -1509,6 +1510,9 @@ coap_write_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now
);
#endif /* !COAP_DISABLE_TCP */
break;
case COAP_PROTO_NONE:
case COAP_PROTO_UDP:
case COAP_PROTO_DTLS:
default:
bytes_written = -1;
break;
@@ -2057,7 +2061,7 @@ coap_new_error_response(coap_pdu_t *request, unsigned char code,
size_t size = request->token_length;
unsigned char type;
coap_opt_t *option;
uint16_t opt_type = 0; /* used for calculating delta-storage */
coap_option_num_t opt_num = 0; /* used for calculating delta-storage */
#if COAP_ERROR_PHRASE_LENGTH > 0
const char *phrase;
@@ -2100,8 +2104,8 @@ coap_new_error_response(coap_pdu_t *request, unsigned char code,
value might grow.
*/
while ((option = coap_option_next(&opt_iter))) {
uint16_t delta = opt_iter.type - opt_type;
/* calculate space required to encode (opt_iter.type - opt_type) */
uint16_t delta = opt_iter.number - opt_num;
/* calculate space required to encode (opt_iter.number - opt_num) */
if (delta < 13) {
size++;
} else if (delta < 269) {
@@ -2125,7 +2129,7 @@ coap_new_error_response(coap_pdu_t *request, unsigned char code,
;
}
opt_type = opt_iter.type;
opt_num = opt_iter.number;
}
/* Now create the response and fill with options and payload data. */
@@ -2142,7 +2146,7 @@ coap_new_error_response(coap_pdu_t *request, unsigned char code,
/* copy all options */
coap_option_iterator_init(request, &opt_iter, opts);
while ((option = coap_option_next(&opt_iter))) {
coap_add_option(response, opt_iter.type,
coap_add_option(response, opt_iter.number,
coap_opt_length(option),
coap_opt_value(option));
}
@@ -2655,7 +2659,7 @@ handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu
if (coap_string_equal(uri_path, &coap_default_uri_wellknown)) {
/* request for .well-known/core */
if (pdu->code == COAP_REQUEST_GET) { /* GET */
if (pdu->code == COAP_REQUEST_CODE_GET) { /* GET */
coap_log(LOG_INFO, "create default response for %s\n",
COAP_DEFAULT_URI_WELLKNOWN);
response = coap_wellknown_response(context, session, pdu);
@@ -2686,7 +2690,7 @@ handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu
* handler to be observed
*/
resource = context->unknown_resource;
} else if (pdu->code == COAP_REQUEST_DELETE) {
} else if (pdu->code == COAP_REQUEST_CODE_DELETE) {
/*
* Request for DELETE on non-existant resource (RFC7252: 5.8.4. DELETE)
*/
@@ -2754,7 +2758,8 @@ handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu
/* check for Observe option RFC7641 and RFC8132 */
if (resource->observable &&
(pdu->code == COAP_REQUEST_GET || pdu->code == COAP_REQUEST_FETCH)) {
(pdu->code == COAP_REQUEST_CODE_GET ||
pdu->code == COAP_REQUEST_CODE_FETCH)) {
observe = coap_check_option(pdu, COAP_OPTION_OBSERVE, &opt_iter);
if (observe) {
observe_action =
@@ -2930,19 +2935,19 @@ handle_signaling(coap_context_t *context, coap_session_t *session,
coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL);
if (pdu->code == COAP_SIGNALING_CSM) {
if (pdu->code == COAP_SIGNALING_CODE_CSM) {
while ((option = coap_option_next(&opt_iter))) {
if (opt_iter.type == COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE) {
if (opt_iter.number == COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE) {
coap_session_set_mtu(session, coap_decode_var_bytes(coap_opt_value(option),
coap_opt_length(option)));
} else if (opt_iter.type == COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER) {
} else if (opt_iter.number == COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER) {
session->csm_block_supported = 1;
}
}
if (session->state == COAP_SESSION_STATE_CSM)
coap_session_connected(session);
} else if (pdu->code == COAP_SIGNALING_PING) {
coap_pdu_t *pong = coap_pdu_init(COAP_MESSAGE_CON, COAP_SIGNALING_PONG, 0, 1);
} else if (pdu->code == COAP_SIGNALING_CODE_PING) {
coap_pdu_t *pong = coap_pdu_init(COAP_MESSAGE_CON, COAP_SIGNALING_CODE_PONG, 0, 1);
if (context->ping_handler) {
context->ping_handler(context, session, pdu, pdu->mid);
}
@@ -2950,13 +2955,13 @@ handle_signaling(coap_context_t *context, coap_session_t *session,
coap_add_option(pong, COAP_SIGNALING_OPTION_CUSTODY, 0, NULL);
coap_send(session, pong);
}
} else if (pdu->code == COAP_SIGNALING_PONG) {
} else if (pdu->code == COAP_SIGNALING_CODE_PONG) {
session->last_pong = session->last_rx_tx;
if (context->pong_handler) {
context->pong_handler(context, session, pdu, pdu->mid);
}
} else if (pdu->code == COAP_SIGNALING_RELEASE
|| pdu->code == COAP_SIGNALING_ABORT) {
} else if (pdu->code == COAP_SIGNALING_CODE_RELEASE
|| pdu->code == COAP_SIGNALING_CODE_ABORT) {
coap_session_disconnected(session, COAP_NACK_RST);
}
}
+33 -30
View File
@@ -169,7 +169,7 @@ coap_option_next(coap_opt_iterator_t *oi) {
oi->next_option += optsize;
oi->length -= optsize;
oi->type += option.delta;
oi->number += option.delta;
} else { /* current option is malformed */
oi->bad = 1;
return NULL;
@@ -181,7 +181,7 @@ coap_option_next(coap_opt_iterator_t *oi) {
* - the filter is too small for the current option number
*/
if (!oi->filtered ||
(b = coap_option_filter_get(&oi->filter, oi->type)) > 0)
(b = coap_option_filter_get(&oi->filter, oi->number)) > 0)
break;
else if (b < 0) { /* filter too small, cannot proceed */
oi->bad = 1;
@@ -193,12 +193,12 @@ coap_option_next(coap_opt_iterator_t *oi) {
}
coap_opt_t *
coap_check_option(coap_pdu_t *pdu, uint16_t type,
coap_check_option(const coap_pdu_t *pdu, coap_option_num_t number,
coap_opt_iterator_t *oi) {
coap_opt_filter_t f;
coap_option_filter_clear(&f);
coap_option_filter_set(&f, type);
coap_option_filter_set(&f, number);
coap_option_iterator_init(pdu, oi, &f);
@@ -396,46 +396,46 @@ coap_opt_encode(coap_opt_t *opt, size_t maxlen, uint16_t delta,
#define SHORT_MASK \
(~LONG_MASK & ((1 << (COAP_OPT_FILTER_LONG + COAP_OPT_FILTER_SHORT)) - 1))
/** Returns true iff @p type denotes an option type larger than 255. */
/** Returns true iff @p number denotes an option number larger than 255. */
COAP_STATIC_INLINE int
is_long_option(uint16_t type) { return type > 255; }
is_long_option(coap_option_num_t number) { return number > 255; }
/** Operation specifiers for coap_filter_op(). */
enum filter_op_t { FILTER_SET, FILTER_CLEAR, FILTER_GET };
/**
* Applies @p op on @p filter with respect to @p type. The following
* Applies @p op on @p filter with respect to @p number. The following
* operations are defined:
*
* FILTER_SET: Store @p type into an empty slot in @p filter. Returns
* FILTER_SET: Store @p number into an empty slot in @p filter. Returns
* @c 1 on success, or @c 0 if no spare slot was available.
*
* FILTER_CLEAR: Remove @p type from filter if it exists.
* FILTER_CLEAR: Remove @p number from filter if it exists.
*
* FILTER_GET: Search for @p type in @p filter. Returns @c 1 if found,
* FILTER_GET: Search for @p number in @p filter. Returns @c 1 if found,
* or @c 0 if not found.
*
* @param filter The filter object.
* @param type The option type to set, get or clear in @p filter.
* @param op The operation to apply to @p filter and @p type.
* @param number The option number to set, get or clear in @p filter.
* @param op The operation to apply to @p filter and @p number.
*
* @return 1 on success, and 0 when FILTER_GET yields no
* hit or no free slot is available to store @p type with FILTER_SET.
* hit or no free slot is available to store @p number with FILTER_SET.
*/
static int
coap_option_filter_op(coap_opt_filter_t *filter,
uint16_t type,
coap_option_num_t number,
enum filter_op_t op) {
size_t lindex = 0;
coap_opt_filter_t *of = filter;
uint16_t nr, mask = 0;
if (is_long_option(type)) {
if (is_long_option(number)) {
mask = LONG_MASK;
for (nr = 1; lindex < COAP_OPT_FILTER_LONG; nr <<= 1, lindex++) {
if (((of->mask & nr) > 0) && (of->long_opts[lindex] == type)) {
if (((of->mask & nr) > 0) && (of->long_opts[lindex] == number)) {
if (op == FILTER_CLEAR) {
of->mask &= ~nr;
}
@@ -449,7 +449,7 @@ coap_option_filter_op(coap_opt_filter_t *filter,
for (nr = 1 << COAP_OPT_FILTER_LONG; lindex < COAP_OPT_FILTER_SHORT;
nr <<= 1, lindex++) {
if (((of->mask & nr) > 0) && (of->short_opts[lindex] == (type & 0xff))) {
if (((of->mask & nr) > 0) && (of->short_opts[lindex] == (number & 0xff))) {
if (op == FILTER_CLEAR) {
of->mask &= ~nr;
}
@@ -459,7 +459,7 @@ coap_option_filter_op(coap_opt_filter_t *filter,
}
}
/* type was not found, so there is nothing to do if op is CLEAR or GET */
/* number was not found, so there is nothing to do if op is CLEAR or GET */
if ((op == FILTER_CLEAR) || (op == FILTER_GET)) {
return 0;
}
@@ -471,10 +471,10 @@ coap_option_filter_op(coap_opt_filter_t *filter,
return 0;
}
if (is_long_option(type)) {
of->long_opts[lindex - 1] = type;
if (is_long_option(number)) {
of->long_opts[lindex - 1] = number;
} else {
of->short_opts[lindex - COAP_OPT_FILTER_LONG - 1] = (uint8_t)type;
of->short_opts[lindex - COAP_OPT_FILTER_LONG - 1] = (uint8_t)number;
}
of->mask |= 1 << (lindex - 1);
@@ -482,21 +482,24 @@ coap_option_filter_op(coap_opt_filter_t *filter,
return 1;
}
int
coap_option_filter_set(coap_opt_filter_t *filter, uint16_t type) {
return coap_option_filter_op(filter, type, FILTER_SET);
void
coap_option_filter_clear(coap_opt_filter_t *filter) {
memset(filter, 0, sizeof(coap_opt_filter_t));
}
int
coap_option_filter_unset(coap_opt_filter_t *filter, uint16_t type) {
return coap_option_filter_op(filter, type, FILTER_CLEAR);
coap_option_filter_set(coap_opt_filter_t *filter, coap_option_num_t option) {
return coap_option_filter_op(filter, option, FILTER_SET);
}
int
coap_option_filter_get(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(filter, type, FILTER_GET);
coap_option_filter_unset(coap_opt_filter_t *filter, coap_option_num_t option) {
return coap_option_filter_op(filter, option, FILTER_CLEAR);
}
int
coap_option_filter_get(coap_opt_filter_t *filter, coap_option_num_t option) {
return coap_option_filter_op(filter, option, FILTER_GET);
}
coap_optlist_t *
+84 -42
View File
@@ -82,9 +82,14 @@ coap_pdu_from_pbuf( struct pbuf *pbuf )
#endif
coap_pdu_t *
coap_pdu_init(uint8_t type, uint8_t code, uint16_t mid, size_t size) {
coap_pdu_init(coap_pdu_type_t type, coap_pdu_code_t code, coap_mid_t mid,
size_t size) {
coap_pdu_t *pdu;
assert(type <= 0x3);
assert(code <= 0xff);
assert(mid >= 0 && mid <= 0xffff);
pdu = coap_malloc_type(COAP_PDU, sizeof(coap_pdu_t));
if (!pdu) return NULL;
@@ -122,8 +127,10 @@ coap_pdu_init(uint8_t type, uint8_t code, uint16_t mid, size_t size) {
}
coap_pdu_t *
coap_new_pdu(const coap_session_t *session) {
coap_pdu_t *pdu = coap_pdu_init(0, 0, 0, coap_session_max_pdu_size(session));
coap_new_pdu(coap_pdu_type_t type, coap_pdu_code_t code,
coap_session_t *session) {
coap_pdu_t *pdu = coap_pdu_init(type, code, coap_new_message_id(session),
coap_session_max_pdu_size(session));
if (!pdu)
coap_log(LOG_CRIT, "coap_new_pdu: cannot allocate memory for new PDU\n");
return pdu;
@@ -179,9 +186,9 @@ coap_pdu_duplicate(const coap_pdu_t *old_pdu,
coap_option_iterator_init(old_pdu, &opt_iter, COAP_OPT_ALL);
while ((option = coap_option_next(&opt_iter))) {
if (drop_options && coap_option_filter_get(drop_options, opt_iter.type))
if (drop_options && coap_option_filter_get(drop_options, opt_iter.number))
continue;
if (!coap_add_option(pdu, opt_iter.type,
if (!coap_add_option(pdu, opt_iter.number,
coap_opt_length(option),
coap_opt_value(option)))
goto fail;
@@ -302,7 +309,7 @@ coap_update_token(coap_pdu_t *pdu, size_t len, const uint8_t *data) {
}
int
coap_remove_option(coap_pdu_t *pdu, uint16_t type) {
coap_remove_option(coap_pdu_t *pdu, coap_option_num_t number) {
coap_opt_iterator_t opt_iter;
coap_opt_t *option;
coap_opt_t *next_option = NULL;
@@ -313,7 +320,7 @@ coap_remove_option(coap_pdu_t *pdu, uint16_t type) {
/* Need to locate where in current options to remove this one */
coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL);
while ((option = coap_option_next(&opt_iter))) {
if (opt_iter.type == type) {
if (opt_iter.number == number) {
/* Found option to delete */
break;
}
@@ -356,7 +363,7 @@ coap_remove_option(coap_pdu_t *pdu, uint16_t type) {
/* Need to rediscover this and next options */
coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL);
while ((option = coap_option_next(&opt_iter))) {
if (opt_iter.type == type) {
if (opt_iter.number == number) {
/* Found option to delete */
break;
}
@@ -402,36 +409,36 @@ coap_remove_option(coap_pdu_t *pdu, uint16_t type) {
}
size_t
coap_insert_option(coap_pdu_t *pdu, uint16_t type, size_t len,
coap_insert_option(coap_pdu_t *pdu, coap_option_num_t number, size_t len,
const uint8_t *data) {
coap_opt_iterator_t opt_iter;
coap_opt_t *option;
uint16_t prev_type = 0;
uint16_t prev_number = 0;
size_t shift;
size_t opt_delta;
coap_option_t decode;
size_t shrink = 0;
if (type >= pdu->max_opt)
return coap_add_option(pdu, type, len, data);
if (number >= pdu->max_opt)
return coap_add_option(pdu, number, len, data);
/* Need to locate where in current options to insert this one */
coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL);
while ((option = coap_option_next(&opt_iter))) {
if (opt_iter.type > type) {
if (opt_iter.number > number) {
/* Found where to insert */
break;
}
prev_type = opt_iter.type;
prev_number = opt_iter.number;
}
assert(option != NULL);
/* size of option inc header to insert */
shift = coap_opt_encode_size(type - prev_type, len);
shift = coap_opt_encode_size(number - prev_number, len);
/* size of next option (header may shrink in size as delta changes */
if (!coap_opt_parse(option, opt_iter.length + 5, &decode))
return 0;
opt_delta = opt_iter.type - type;
opt_delta = opt_iter.number - number;
if (!coap_pdu_check_resize(pdu,
pdu->used_size + shift - shrink))
@@ -441,7 +448,7 @@ coap_insert_option(coap_pdu_t *pdu, uint16_t type, size_t len,
/* Need to locate where in current options to insert this one */
coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL);
while ((option = coap_option_next(&opt_iter))) {
if (opt_iter.type > type) {
if (opt_iter.number > number) {
/* Found where to insert */
break;
}
@@ -481,7 +488,7 @@ coap_insert_option(coap_pdu_t *pdu, uint16_t type, size_t len,
memmove(&option[shift], &option[shrink],
pdu->used_size - (option - pdu->token) - shrink);
if (!coap_opt_encode(option, pdu->alloc_size - pdu->used_size,
type - prev_type, data, len))
number - prev_number, data, len))
return 0;
pdu->used_size += shift - shrink;
@@ -491,7 +498,7 @@ coap_insert_option(coap_pdu_t *pdu, uint16_t type, size_t len,
}
size_t
coap_update_option(coap_pdu_t *pdu, uint16_t type, size_t len,
coap_update_option(coap_pdu_t *pdu, coap_option_num_t number, size_t len,
const uint8_t *data) {
coap_opt_iterator_t opt_iter;
coap_opt_t *option;
@@ -499,9 +506,9 @@ coap_update_option(coap_pdu_t *pdu, uint16_t type, size_t len,
size_t new_length = 0;
size_t old_length = 0;
option = coap_check_option(pdu, type, &opt_iter);
option = coap_check_option(pdu, number, &opt_iter);
if (!option)
return coap_insert_option(pdu, type, len, data);
return coap_insert_option(pdu, number, len, data);
old_length = coap_opt_parse(option, (size_t)-1, &decode);
if (old_length == 0)
@@ -513,7 +520,7 @@ coap_update_option(coap_pdu_t *pdu, uint16_t type, size_t len,
pdu->used_size + new_length - old_length))
return 0;
/* Possible a re-size took place with a realloc() */
option = coap_check_option(pdu, type, &opt_iter);
option = coap_check_option(pdu, number, &opt_iter);
}
if (new_length != old_length)
@@ -530,18 +537,17 @@ coap_update_option(coap_pdu_t *pdu, uint16_t type, size_t len,
return 1;
}
/* FIXME: de-duplicate code with coap_add_option_later */
size_t
coap_add_option(coap_pdu_t *pdu, uint16_t type, size_t len,
coap_add_option(coap_pdu_t *pdu, coap_option_num_t number, size_t len,
const uint8_t *data) {
size_t optsize;
coap_opt_t *opt;
assert(pdu);
if (type == pdu->max_opt) {
if (number == pdu->max_opt) {
/* Validate that the option is repeatable */
switch (type) {
switch (number) {
/* Ignore list of genuine repeatable */
case COAP_OPTION_IF_MATCH:
case COAP_OPTION_ETAG:
@@ -551,14 +557,16 @@ coap_add_option(coap_pdu_t *pdu, uint16_t type, size_t len,
case COAP_OPTION_LOCATION_QUERY:
break;
default:
coap_log(LOG_INFO, "Option %d is not defined as repeatable\n", type);
coap_log(LOG_INFO, "Option number %d is not defined as repeatable\n",
number);
/* Accepting it after warning as there may be user defineable options */
break;
}
}
if (COAP_PDU_IS_REQUEST(pdu) &&
(type == COAP_OPTION_PROXY_URI || type == COAP_OPTION_PROXY_SCHEME)) {
(number == COAP_OPTION_PROXY_URI ||
number == COAP_OPTION_PROXY_SCHEME)) {
/*
* Need to check whether there is a hop-limit option. If not, it needs
* to be inserted by default (RFC 8768).
@@ -572,13 +580,13 @@ coap_add_option(coap_pdu_t *pdu, uint16_t type, size_t len,
}
}
if (type < pdu->max_opt) {
if (number < pdu->max_opt) {
coap_log(LOG_DEBUG,
"coap_add_option: options are not in correct order\n");
return coap_insert_option(pdu, type, len, data);
return coap_insert_option(pdu, number, len, data);
}
optsize = coap_opt_encode_size(type - pdu->max_opt, len);
optsize = coap_opt_encode_size(number - pdu->max_opt, len);
if (!coap_pdu_check_resize(pdu,
pdu->used_size + optsize))
return 0;
@@ -596,14 +604,14 @@ coap_add_option(coap_pdu_t *pdu, uint16_t type, size_t len,
/* encode option and check length */
optsize = coap_opt_encode(opt, pdu->alloc_size - pdu->used_size,
type - pdu->max_opt, data, len);
number - pdu->max_opt, data, len);
if (!optsize) {
coap_log(LOG_WARNING, "coap_add_option: cannot add option\n");
/* error */
return 0;
} else {
pdu->max_opt = type;
pdu->max_opt = number;
pdu->used_size += optsize;
}
@@ -641,17 +649,11 @@ coap_add_data_after(coap_pdu_t *pdu, size_t len) {
}
int
coap_get_data(const coap_pdu_t *pdu, size_t *len, uint8_t **data) {
coap_get_data(const coap_pdu_t *pdu, size_t *len, const uint8_t **data) {
size_t offset;
size_t total;
const uint8_t **cdata = NULL;
#ifdef _WIN32
#pragma warning( disable : 4090 )
#endif
/* Really, coap_get_data() should be using a const */
memcpy(&cdata, &data, sizeof(cdata));
return coap_get_data_large(pdu, len, cdata, &offset, &total);
return coap_get_data_large(pdu, len, data, &offset, &total);
}
int
@@ -705,9 +707,12 @@ error_desc_t coap_error[] = {
{ COAP_RESPONSE_CODE(405), "Method Not Allowed" },
{ COAP_RESPONSE_CODE(406), "Not Acceptable" },
{ COAP_RESPONSE_CODE(408), "Request Entity Incomplete" },
{ COAP_RESPONSE_CODE(409), "Conflict" },
{ COAP_RESPONSE_CODE(412), "Precondition Failed" },
{ COAP_RESPONSE_CODE(413), "Request Entity Too Large" },
{ COAP_RESPONSE_CODE(415), "Unsupported Content-Format" },
{ COAP_RESPONSE_CODE(422), "Unprocessable" },
{ COAP_RESPONSE_CODE(429), "Too Many Requests" },
{ COAP_RESPONSE_CODE(500), "Internal Server Error" },
{ COAP_RESPONSE_CODE(501), "Not Implemented" },
{ COAP_RESPONSE_CODE(502), "Bad Gateway" },
@@ -848,7 +853,7 @@ coap_pdu_parse_header(coap_pdu_t *pdu, coap_proto_t proto) {
static int
coap_pdu_parse_opt_csm(coap_pdu_t *pdu, uint16_t len) {
switch (pdu->code) {
switch ((coap_pdu_signaling_proto_t)pdu->code) {
case COAP_SIGNALING_CSM:
switch (pdu->max_opt) {
case COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE:
@@ -1194,3 +1199,40 @@ coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto) {
}
return pdu->hdr_size;
}
coap_pdu_code_t
coap_pdu_get_code(const coap_pdu_t *pdu) {
return pdu->code;
}
void
coap_pdu_set_code(coap_pdu_t *pdu, coap_pdu_code_t code) {
assert(code <= 0xff);
pdu->code = code;
}
coap_pdu_type_t coap_pdu_get_type(const coap_pdu_t *pdu) {
return pdu->type;
}
void coap_pdu_set_type(coap_pdu_t *pdu, coap_pdu_type_t type) {
assert(type <= 0x3);
pdu->type = type;
}
coap_bin_const_t coap_pdu_get_token(const coap_pdu_t *pdu) {
coap_bin_const_t token;
token.length = pdu->token_length;
token.s = pdu->token;
return token;
}
coap_mid_t coap_pdu_get_mid(const coap_pdu_t *pdu) {
return pdu->mid;
}
void coap_pdu_set_mid(coap_pdu_t *pdu, coap_mid_t mid) {
assert(mid >= 0 && mid <= 0xffff);
pdu->mid = mid;
}
+1 -1
View File
@@ -723,7 +723,7 @@ coap_add_observer(coap_resource_t *resource,
coap_string_t *query,
int has_block2,
coap_block_t block,
uint8_t code) {
coap_pdu_code_t code) {
coap_subscription_t *s;
assert( session );
+13 -13
View File
@@ -536,17 +536,17 @@ t_iterate_option3(void) {
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 0);
CU_ASSERT(oi.type == 1);
CU_ASSERT(oi.number == 1);
CU_ASSERT_PTR_EQUAL(option, teststr + 3);
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 0);
CU_ASSERT(oi.type == 1);
CU_ASSERT(oi.number == 1);
CU_ASSERT_PTR_EQUAL(option, teststr + 7);
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 0);
CU_ASSERT(oi.type == 30);
CU_ASSERT(oi.number == 30);
CU_ASSERT_PTR_EQUAL(option, teststr + 8);
option = coap_option_next(&oi);
@@ -579,17 +579,17 @@ t_iterate_option4(void) {
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 0);
CU_ASSERT(oi.type == 1);
CU_ASSERT(oi.number == 1);
CU_ASSERT_PTR_EQUAL(option, teststr + 3);
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 0);
CU_ASSERT(oi.type == 1);
CU_ASSERT(oi.number == 1);
CU_ASSERT_PTR_EQUAL(option, teststr + 7);
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 0);
CU_ASSERT(oi.type == 30);
CU_ASSERT(oi.number == 30);
CU_ASSERT_PTR_EQUAL(option, teststr + 8);
option = coap_option_next(&oi);
@@ -621,7 +621,7 @@ t_iterate_option5(void) {
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 0);
CU_ASSERT(oi.type == 5);
CU_ASSERT(oi.number == 5);
CU_ASSERT_PTR_EQUAL(option, teststr);
option = coap_option_next(&oi);
@@ -657,17 +657,17 @@ t_iterate_option6(void) {
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 0);
CU_ASSERT(oi.type == 10);
CU_ASSERT(oi.number == 10);
CU_ASSERT_PTR_EQUAL(option, teststr + 1);
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 0);
CU_ASSERT(oi.type == 10);
CU_ASSERT(oi.number == 10);
CU_ASSERT_PTR_EQUAL(option, teststr + 2);
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 0);
CU_ASSERT(oi.type == 10);
CU_ASSERT(oi.number == 10);
CU_ASSERT_PTR_EQUAL(option, teststr + 3);
option = coap_option_next(&oi);
@@ -704,17 +704,17 @@ t_iterate_option7(void) {
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 0);
CU_ASSERT(oi.type == 8);
CU_ASSERT(oi.number == 8);
CU_ASSERT_PTR_EQUAL(option, teststr);
option = coap_option_next(&oi);
CU_ASSERT(oi.bad == 0);
CU_ASSERT(oi.type == 22);
CU_ASSERT(oi.number == 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(oi.number == 22);
CU_ASSERT_PTR_EQUAL(option, teststr + 5);
option = coap_option_next(&oi);
+1 -1
View File
@@ -31,7 +31,7 @@ t_parse_pdu1(void) {
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->code == COAP_REQUEST_CODE_GET);
CU_ASSERT(pdu->mid == 0x9334);
CU_ASSERT_PTR_NULL(pdu->data);
}