20#ifndef INET6_ADDRSTRLEN
21#define INET6_ADDRSTRLEN 46
24#if COAP_CLIENT_SUPPORT == 0
25#error For Proxy support, COAP_CLIENT_SUPPORT must be set
27#if COAP_SERVER_SUPPORT == 0
28#error For Proxy support, COAP_SERVER_SUPPORT must be set
32#define strcasecmp _stricmp
33#define strncasecmp _strnicmp
56 scratch_d[0] =
'\000';
58 size = strlen(scratch_d);
59 snprintf(&scratch_d[size],
sizeof(scratch_d)-size,
62 scratch_u[0] =
'\000';
64 for (i = 0; i < upstream_token->
length; i++) {
65 size = strlen(scratch_u);
66 snprintf(&scratch_u[size],
sizeof(scratch_u)-size,
67 "%02x", upstream_token->
s[i]);
71 coap_log_debug(
" proxy %-4s %s {%s}-{%s} \"%s\"%s\n", type, addr, scratch_u, scratch_d,
80 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used,
"del");
85 if (proxy_req->proxy_cache) {
92 url ? (
char *)url->
s :
"???",
93 proxy_req->proxy_cache->ref);
96 assert(proxy_req->proxy_cache->ref);
97 proxy_req->proxy_cache->ref--;
98 if (proxy_req->proxy_cache->ref == 0) {
99 PROXY_CACHE_DELETE(proxy_entry->rsp_cache, proxy_req->proxy_cache);
103 proxy_req->proxy_cache =
NULL;
105 if (proxy_req->doing_observe) {
106 assert(proxy_req->incoming->ref_proxy_subs);
107 proxy_req->incoming->ref_proxy_subs--;
108 proxy_req->doing_observe = 0;
112 proxy_req->incoming =
NULL;
114 LL_DELETE(proxy_entry->proxy_req, proxy_req);
120 coap_proxy_req_t *proxy_req, *treq;
122 LL_FOREACH_SAFE(proxy_entry->proxy_req, proxy_req, treq) {
140 coap_log_debug(
"Cannot add token to incoming proxy response PDU\n");
144 coap_log_info(
"Failed to send PDU with %u.%02u proxy issue\n",
149 coap_proxy_del_req(proxy_entry, proxy_req);
158 for (i = 0; i < context->proxy_list_count; i++) {
161 (
void *)&context->proxy_list[i]);
162 coap_proxy_cleanup_entry(&context->proxy_list[i], 0);
169 if (proxy_entry && proxy_entry->ongoing) {
173 LL_FOREACH(proxy_entry->ongoing->lg_crcv, lg_crcv) {
174 if (lg_crcv->observe_set) {
193 for (i = 0; i < context->proxy_list_count; i++) {
196 if (coap_proxy_check_observe(proxy_entry))
199 if (proxy_entry->ongoing) {
200 if (proxy_entry->req_start_ticks) {
204 if (timeout_time <= now) {
209 proxy_entry->mid,
NULL, &sent);
217 if (*tim_rem > timeout_time - now) {
218 *tim_rem = timeout_time - now;
222 }
else if (proxy_entry->idle_timeout_ticks) {
223 if (proxy_entry->last_used + proxy_entry->idle_timeout_ticks <= now) {
225 if (coap_proxy_remove_association(proxy_entry->ongoing, 0)) {
229 if (*tim_rem > proxy_entry->last_used + proxy_entry->idle_timeout_ticks - now) {
230 *tim_rem = proxy_entry->last_used + proxy_entry->idle_timeout_ticks - now;
241coap_get_uri_proxy_scheme_info(
const coap_pdu_t *request,
249 strncasecmp(opt_val,
"coaps+tcp", 9) == 0) {
252 }
else if (opt_len == 8 &&
253 strncasecmp(opt_val,
"coap+tcp", 8) == 0) {
256 }
else if (opt_len == 5 &&
257 strncasecmp(opt_val,
"coaps", 5) == 0) {
260 }
else if (opt_len == 4 &&
261 strncasecmp(opt_val,
"coap", 4) == 0) {
264 }
else if (opt_len == 7 &&
265 strncasecmp(opt_val,
"coap+ws", 7) == 0) {
268 }
else if (opt_len == 8 &&
269 strncasecmp(opt_val,
"coaps+ws", 8) == 0) {
274 opt_len, opt_len, opt_val);
316 coap_log_warn(
"coap+tcp URI scheme not supported for proxy\n");
322 coap_log_warn(
"coaps+tcp URI scheme not supported for proxy\n");
328 coap_log_warn(
"coap+ws URI scheme not supported for proxy\n");
334 coap_log_warn(
"coaps+ws URI scheme not supported for proxy\n");
350 switch ((
int)proxy_type) {
372 coap_log_warn(
"Proxy old proxy_type 0x%u unknown\n", proxy_type);
384 if (!incoming && !ongoing)
388 size_t proxy_list_count = context->proxy_list_count;
393 for (i = 0; i < proxy_list_count; i++) {
395 proxy_list[i].uri.port == uri.
port &&
396 proxy_list[i].uri.scheme == uri.
scheme) {
397 if (!proxy_list[i].track_client_session && context->proxy_response_cb) {
399 return &proxy_list[i];
401 if (proxy_list[i].incoming == incoming) {
403 return &proxy_list[i];
413 (proxy_list_count+1)*
sizeof(proxy_list[0]));
415 if (new_proxy_list ==
NULL) {
418 context->proxy_list = proxy_list = new_proxy_list;
419 memset(&proxy_list[proxy_list_count], 0,
sizeof(proxy_list[proxy_list_count]));
422 proxy_list[proxy_list_count].uri = uri;
425 if (!proxy_list[proxy_list_count].uri_host_keep) {
428 memcpy(proxy_list[proxy_list_count].uri_host_keep, uri.
host.
s, uri.
host.
length);
429 proxy_list[proxy_list_count].uri.host.s = proxy_list[proxy_list_count].uri_host_keep;
431 proxy_list[proxy_list_count].uri.path.s =
NULL;
432 proxy_list[proxy_list_count].uri.path.length = 0;
433 proxy_list[proxy_list_count].uri.query.s =
NULL;
434 proxy_list[proxy_list_count].uri.query.length = 0;
436 proxy_list[proxy_list_count].ongoing = ongoing;
437 proxy_list[proxy_list_count].idle_timeout_ticks = server_list->
idle_timeout_secs *
440 coap_ticks(&proxy_list[proxy_list_count].last_used);
442 incoming->proxy_entry = &proxy_list[proxy_list_count];
444 proxy_list[proxy_list_count].incoming = incoming;
445 incoming->proxy_entry = &proxy_list[proxy_list_count];
448 context->proxy_list_count++;
449 return &proxy_list[proxy_list_count];
460 size_t proxy_list_count = session->
context->proxy_list_count;
466 *proxy_entry_created = 0;
468 proxy_type = coap_proxy_map_type(server_list->
type);
476 if (session->proxy_entry &&
478 for (i = 0; i < proxy_list_count; i++) {
479 if (&proxy_list[i] == session->proxy_entry) {
480 if (session->proxy_entry->ongoing) {
481 memset(server_use, 0,
sizeof(*server_use));
482 return session->proxy_entry;
494 memcpy(server_use, &server_list->
entry[server_list->
next_entry],
sizeof(*server_use));
496 memset(server_use, 0,
sizeof(*server_use));
506 if (!coap_get_uri_proxy_scheme_info(request, proxy_scheme, &server_use->
uri)) {
521 &server_use->
uri) < 0) {
529 if (!(proxy_scheme || proxy_uri)) {
547 proxy_entry = coap_proxy_get_add_list_entry(session,
NULL, server_use->
uri, server_list, 0);
549 if (proxy_entry ==
NULL) {
554 *proxy_entry_created = 1;
563 size_t proxy_list_count = session->
context->proxy_list_count;
565 for (i = 0; i < proxy_list_count; i++) {
567 coap_proxy_req_t *proxy_req;
571 LL_FOREACH(proxy_entry->proxy_req, proxy_req) {
572 if (proxy_req->incoming == session) {
573 coap_proxy_del_req(proxy_entry, proxy_req);
577 if (proxy_entry->incoming == session) {
585 proxy_entry->ongoing =
NULL;
591 if (proxy_entry->ongoing == session) {
594 coap_proxy_cleanup_entry(proxy_entry, failure_code);
595 ongoing = proxy_entry->ongoing;
599 session->
context->proxy_list_count - 1);
600 if (proxy_list_count-i > 1) {
601 memmove(&proxy_list[i],
603 (proxy_list_count-i-1) *
sizeof(proxy_list[0]));
605 session->
context->proxy_list_count--;
624 static char client_sni[256];
626 int proxy_entry_created;
628 proxy_entry = coap_proxy_get_session(session, request, response, server_list,
629 &server_use, &proxy_entry_created);
635 if (!proxy_entry->ongoing) {
649 if (info_list ==
NULL) {
651 coap_proxy_remove_association(session, 0);
654 proto = info_list->
proto;
655 memcpy(&dst, &info_list->
addr,
sizeof(dst));
663 coap_proxy_remove_association(session, 0);
670 coap_proxy_remove_association(session, 0);
671 coap_log_debug(
"* %s: mcast proxy forwarding only supported for 'coap'\n",
677#if COAP_AF_UNIX_SUPPORT
686 "/tmp/coap-pr-cl-%" PRIu64, (uint64_t)now);
689 fprintf(stderr,
"coap_address_set_unix_domain: %s: failed\n",
695 local_addr = &bind_addr;
699 snprintf(client_sni,
sizeof(client_sni),
"%*.*s", (
int)server_use.
uri.
host.
length,
706#if COAP_OSCORE_SUPPORT
708 proxy_entry->ongoing =
714 proxy_entry->ongoing =
717#if COAP_OSCORE_SUPPORT
724#if COAP_OSCORE_SUPPORT
728 proxy_entry->ongoing =
735 proxy_entry->ongoing =
741 coap_log_warn(
"Proxy: (D)TLS not configured for secure session\n");
748 proxy_entry->ongoing =
754 proxy_entry->ongoing =
760 proxy_entry->ongoing =
764#if COAP_OSCORE_SUPPORT
775 if (proxy_entry->ongoing ==
NULL) {
777 coap_proxy_remove_association(session, 0);
780 if (proxy_entry_created) {
783 (
int)proxy_entry->uri.host.length, proxy_entry->uri.host.s,
784 proxy_entry->uri.port,
786 session->
context->proxy_list_count);
788 }
else if (proxy_entry->ongoing->session_failed) {
804static coap_proxy_req_t *
807 coap_proxy_req_t *proxy_req;
809 LL_FOREACH(proxy_entry->proxy_req, proxy_req) {
810 if (proxy_req->incoming == session && proxy_req->proxy_cache == proxy_cache) {
825 coap_proxy_req_t *proxy_req,
int replace_mid,
826 int remove_observe) {
837 int rcvd_created = 0;
850 proxy_req->resource == session->
context->unknown_resource &&
851 proxy_req->resource->is_reverse_proxy && proxy_req && proxy_req->pdu &&
861 if (remove_observe) {
880 resp_pdu->
type = proxy_req->pdu->type;
886 resp_pdu->
mid = sent->
mid;
888 proxy_req->mid = resp_pdu->
mid;
896 uint16_t media_type = 0;
903 assert(size == total);
907 coap_log_debug(
"coap_proxy_call_response_handler: copy data error\n");
927 if (!coap_add_data_large_response_lkd(proxy_req->resource, session, sent,
930 media_type, maxage, etag, body->
length,
932 coap_proxy_free_response_data,
934 coap_log_debug(
"coap_proxy_call_response_handler: add data error\n");
939 session->
context->proxy_response_cb(session,
942 proxy_req->cache_key),
950 if (fwd_pdu != resp_pdu) {
975 ret = coap_proxy_forward_request_lkd(session,
1005 const uint8_t *data;
1010 coap_proxy_req_t *proxy_req =
NULL;
1018 coap_proxy_cache_t *proxy_cache =
NULL;
1021 uint32_t new_maxage;
1027 proxy_entry = coap_proxy_get_ongoing_session(session, request, response,
1033 coap_proxy_log_entry(session, request,
NULL,
"req");
1036 if (obs_opt && session->
context->proxy_response_cb) {
1041 coap_proxy_ignore_options,
1042 sizeof(coap_proxy_ignore_options)/
sizeof(coap_proxy_ignore_options[0]));
1047 PROXY_CACHE_FIND(proxy_entry->rsp_cache, cache_key_l, proxy_cache);
1052 proxy_req = coap_proxy_get_req(proxy_entry, proxy_cache, session);
1061 if (proxy_req->doing_observe) {
1062 assert(proxy_req->incoming->ref_proxy_subs);
1063 proxy_req->incoming->ref_proxy_subs--;
1064 proxy_req->doing_observe = 0;
1066 if (proxy_entry->proxy_req && !proxy_entry->proxy_req->next &&
1067 proxy_req->token_used) {
1070 coap_log_debug(
"coap_proxy_forward_request: Using coap_cancel_observe() to do Proxy OBSERVE cancellation\n");
1072 memcpy(&tmp.
s, &proxy_req->token_used->s,
sizeof(tmp.
s));
1073 tmp.
length = proxy_req->token_used->length;
1079 goto return_cached_info;
1085 coap_proxy_del_req(proxy_entry, proxy_req);
1089 goto return_cached_info;
1095 coap_proxy_del_req(proxy_entry, proxy_req);
1099 goto return_cached_info;
1107 if (proxy_req ==
NULL) {
1110 memset(proxy_req, 0,
sizeof(coap_proxy_req_t));
1111 LL_PREPEND(proxy_entry->proxy_req, proxy_req);
1114 proxy_req->resource = resource;
1115 proxy_req->incoming = session;
1116 proxy_req->cache_key = cache_key;
1117 proxy_req->proxy_cache = proxy_cache;
1119 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used,
"add");
1130 if (proxy_req->token_used ==
NULL) {
1133 goto return_cached_info;
1140 if (proxy_req->token_used ==
NULL) {
1147 proxy_type = coap_proxy_map_type(server_list->
type);
1164 if (
coap_is_mcast(&proxy_entry->ongoing->addr_info.remote)) {
1175 switch (opt_iter.
number) {
1229 assert(size == total);
1239 memcpy(body_data->
s, data, size);
1240 data = body_data->
s;
1241 if (!coap_add_data_large_request_lkd(proxy_entry->ongoing, pdu, total, data,
1242 coap_proxy_release_body_data, body_data)) {
1248 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used,
"fwd");
1249 proxy_entry->mid =
coap_send_lkd(proxy_entry->ongoing, pdu);
1256 proxy_entry->req_start_ticks = now;
1273 if (obs_opt && !proxy_req->doing_observe) {
1280 proxy_req->doing_observe = 1;
1281 proxy_req->incoming->ref_proxy_subs++;
1287 coap_proxy_log_entry(session, request, &proxy_cache->rsp_pdu->actual_token,
"rspc");
1288 if (now < proxy_cache->expire) {
1290 if (new_maxage == 0)
1298 coap_proxy_call_response_handler(session, request, proxy_cache->rsp_pdu,
1299 &r_token, proxy_req, 1, obs_opt ? 0 : 1);
1301 coap_proxy_del_req(proxy_entry, proxy_req);
1310 size_t proxy_list_count = ongoing->
context->proxy_list_count;
1314 coap_proxy_req_t *proxy_req;
1316 for (i = 0; i < proxy_list_count; i++) {
1317 proxy_entry = &proxy_list[i];
1318 if (proxy_entry->ongoing == ongoing) {
1319 LL_FOREACH(proxy_entry->proxy_req, proxy_req) {
1323 *u_proxy_entry = proxy_entry;
1333 scratch[0] =
'\000';
1334 for (i = 0; i < rcv_token.
length; i++) {
1335 size = strlen(scratch);
1336 snprintf(&scratch[size],
sizeof(scratch)-size,
1337 "%02x", rcv_token.
s[i]);
1340 for (i = 0; i < proxy_list_count; i++) {
1341 proxy_entry = &proxy_list[i];
1342 LL_FOREACH(proxy_entry->proxy_req, proxy_req) {
1343 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used,
"miss");
1357 ret = coap_proxy_forward_response_lkd(session,
1371 const uint8_t *data;
1386 coap_proxy_req_t *proxy_req =
NULL;
1388 proxy_req = coap_proxy_map_outgoing_request(session, received, &proxy_entry);
1389 if (!proxy_req || proxy_req->incoming->server_list) {
1390 coap_log_warn(
"Unknown proxy ongoing session response received - ignored\n");
1394 proxy_entry->req_start_ticks = 0;
1395 req_pdu = proxy_req->pdu;
1397 resource = proxy_req->resource;
1398 incoming = proxy_req->incoming;
1400 coap_log_debug(
"** process upstream incoming %d.%02d response:\n",
1405 assert(size == total);
1411 memcpy(body_data->
s, data, size);
1412 data = body_data->
s;
1428 coap_log_debug(
"cannot add token to ongoing proxy response PDU\n");
1437 switch (opt_iter.
number) {
1468 coap_add_data_large_response_lkd(resource, incoming, req_pdu, pdu,
1470 media_type, maxage, etag, size, data,
1471 coap_proxy_release_body_data,
1478 *cache_key = proxy_req->cache_key;
1485 if (option ==
NULL) {
1486 if (proxy_entry->proxy_req) {
1488 proxy_req->cache_key =
NULL;
1489 coap_proxy_del_req(proxy_entry, proxy_req);
1491 }
else if (!proxy_req->doing_observe) {
1496 proxy_req->doing_observe = 1;
1497 proxy_req->incoming->ref_proxy_subs++;
1507 void *body_data, coap_proxy_req_t *proxy_req,
1517 switch ((
int)rcvd->
code) {
1534 if (can_cache && obs_opt) {
1535 coap_proxy_cache_t *proxy_cache;
1541 if (proxy_req->proxy_cache) {
1543 proxy_cache = proxy_req->proxy_cache;
1546 if (proxy_cache ==
NULL) {
1549 memset(proxy_cache, 0,
sizeof(coap_proxy_cache_t));
1552 coap_proxy_ignore_options,
1553 sizeof(coap_proxy_ignore_options)/
sizeof(coap_proxy_ignore_options[0]));
1558 memcpy(&proxy_cache->cache_req, cache_key_l,
1559 sizeof(proxy_cache->cache_req));
1563 proxy_req->proxy_cache = proxy_cache;
1565 PROXY_CACHE_ADD(proxy_entry->rsp_cache, proxy_cache);
1570 const uint8_t *data;
1578 if (proxy_cache->rsp_pdu) {
1584 memcpy(copy->
s, data, data_len);
1585 proxy_cache->rsp_pdu->data_free = copy;
1586 proxy_cache->rsp_pdu->body_data = copy->
s;
1587 proxy_cache->rsp_pdu->body_length = copy->
length;
1588 proxy_cache->rsp_pdu->body_total = copy->
length;
1590 proxy_cache->rsp_pdu->body_data =
NULL;
1591 proxy_cache->rsp_pdu->body_length = 0;
1592 proxy_cache->rsp_pdu->body_total = 0;
1605 proxy_cache->etag = 0;
1619 LL_FOREACH(proxy_entry->proxy_req, proxy_req) {
1620 if (proxy_req->proxy_cache == proxy_cache) {
1621 if (!proxy_req->doing_observe) {
1628 proxy_req->doing_observe = 1;
1629 proxy_req->incoming->ref_proxy_subs++;
1632 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used,
"rsp");
1634 if (coap_proxy_call_response_handler(proxy_req->incoming, proxy_req->pdu,
1645 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used,
"rspn");
1647 ret = coap_proxy_call_response_handler(proxy_req->incoming, proxy_req->pdu,
1648 rcvd, &token, proxy_req, 0, 0);
1650 coap_proxy_del_req(proxy_entry, proxy_req);
1671 resource = session->
context->unknown_resource ?
1672 session->
context->unknown_resource :
1673 session->
context->proxy_uri_resource;
1675 coap_log_err(
"coap_proxy_local_write: Unknown or Proxy resource not defined\n");
1683 coap_log_err(
"coap_proxy_local_write: Could not create response PDU\n");
1699 if (!coap_proxy_forward_request_lkd(session, pdu, response, resource,
1700 NULL, session->server_list)) {
1701 coap_log_debug(
"coap_proxy_local_write: Failed to forward PDU\n");
1716 session = coap_new_client_session_proxy_lkd(ctx, server_list);
1730#if COAP_IPV6_SUPPORT
1731 remote.
s = (
const uint8_t *)
"::1";
1732#elif COAP_IPV4_SUPPORT
1733 remote.
s = (
const uint8_t *)
"127.0.0.1";
1735 coap_log_warn(
"coap_new_client_session_proxy: No IPv4 or IPv6 support\n");
1738 remote.
length = strlen((
const char *)remote.
s);
1745 coap_log_warn(
"coap_new_client_session_proxy: Unable to resolve IP address\n");
1753 session->server_list = server_list;
1766 ret = coap_proxy_fwd_add_client_session_lkd(session, use_ip, use_port, server_list);
1772coap_proxy_fwd_add_client_session_lkd(
coap_session_t *session,
const char *use_ip,
1775#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
1789 memset(&uri, 0,
sizeof(uri));
1790 uri.
port = use_port;
1791 switch (session->
proto) {
1827 if (memcmp(use_ip,
"::ffff:", 7) == 0) {
1829 uri.
host.
s = (
const uint8_t *)&use_ip[7];
1832 uri.
host.
s = (
const uint8_t *)use_ip;
1838 proxy_entry = coap_proxy_get_add_list_entry(
NULL, session, uri, server_list, 1);
1842 (
int)proxy_entry->uri.host.length, proxy_entry->uri.host.s,
1843 proxy_entry->uri.port,
1844 (
void *)proxy_entry,
1845 session->
context->proxy_list_count);
1846 proxy_entry->idle_timeout_ticks = 0;
1847 proxy_entry->req_start_ticks = 0;
1848 proxy_entry->track_client_session = 0;
1857 coap_mid_t mid, coap_proxy_subs_delete_t type) {
1862 for (i = 0; i < context->proxy_list_count; i++) {
1864 coap_proxy_req_t *proxy_req, *treq;
1866 LL_FOREACH_SAFE(proxy_entry->proxy_req, proxy_req, treq) {
1867 if (proxy_req->incoming == session) {
1873 case COAP_PROXY_SUBS_ALL:
1876 case COAP_PROXY_SUBS_TOKEN:
1880 case COAP_PROXY_SUBS_MID:
1881 if (proxy_req->mid == mid)
1888 if (match && proxy_entry->proxy_req && ! proxy_entry->proxy_req->next &&
1889 proxy_req->token_used) {
1892 coap_log_debug(
"coap_delete_proxy_subscriber: Using coap_cancel_observe() to do Proxy OBSERVE cancellation\n");
1894 memcpy(&tmp.
s, &proxy_req->token_used->s,
sizeof(tmp.
s));
1895 tmp.
length = proxy_req->token_used->length;
1898 coap_proxy_del_req(proxy_entry, proxy_req);
int coap_address_set_unix_domain(coap_address_t *addr, const uint8_t *host, size_t host_len)
Copy the parsed unix domain host into coap_address_t structure translating %2F into / on the way.
void coap_free_address_info(coap_addr_info_t *info)
Free off the one or more linked sets of coap_addr_info_t returned from coap_resolve_address_info().
int coap_is_af_unix(const coap_address_t *a)
Checks if given address a denotes a AF_UNIX address.
int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
@ COAP_RESOLVE_TYPE_REMOTE
remote side of session
#define COAP_UNIX_PATH_MAX
struct coap_lg_crcv_t coap_lg_crcv_t
struct coap_cache_key_t coap_cache_key_t
struct coap_proxy_entry_t coap_proxy_entry_t
Proxy information.
struct coap_resource_t coap_resource_t
Library specific build wrapper for coap_internal.h.
void * coap_realloc_type(coap_memory_tag_t type, void *p, size_t size)
Reallocates a chunk p of bytes created by coap_malloc_type() or coap_realloc_type() and returns a poi...
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
@ COAP_OPTION_PROXY_SCHEME
@ COAP_OPTION_CONTENT_FORMAT
coap_uri_scheme_t
The scheme specifiers.
@ COAP_URI_SCHEME_COAPS_WS
@ COAP_URI_SCHEME_COAPS_TCP
@ COAP_URI_SCHEME_COAP_TCP
@ COAP_URI_SCHEME_COAP_WS
coap_mid_t coap_send_rst_lkd(coap_session_t *session, const coap_pdu_t *request)
Sends an RST message with code 0 for the specified request to dst.
coap_mid_t coap_send_lkd(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
coap_mid_t coap_send_ack_lkd(coap_session_t *session, const coap_pdu_t *request)
Sends an ACK message with code 0 for the specified request to dst.
void coap_delete_cache_key(coap_cache_key_t *cache_key)
Delete the cache-key.
coap_cache_key_t * coap_cache_derive_key_w_ignore(const coap_session_t *session, const coap_pdu_t *pdu, coap_cache_session_based_t session_based, const uint16_t *ignore_options, size_t ignore_count)
Calculates a cache-key for the given CoAP PDU.
@ COAP_CACHE_NOT_SESSION_BASED
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
#define COAP_MAX_DELAY_TICKS
uint16_t coap_new_message_id_lkd(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
int coap_delete_node_lkd(coap_queue_t *node)
Destroys specified node.
int coap_option_check_critical(coap_session_t *session, coap_pdu_t *pdu, coap_opt_filter_t *unknown, coap_crit_type_t is_proxy)
Verifies that pdu contains no unknown critical options, duplicate options or the options defined as R...
coap_addr_info_t * coap_resolve_address_info_lkd(const coap_str_const_t *address, uint16_t port, uint16_t secure_port, uint16_t ws_port, uint16_t ws_secure_port, int ai_hints_flags, int scheme_hint_bits, coap_resolve_type_t type)
Resolve the specified address into a set of coap_address_t that can be used to bind() (local) or conn...
int coap_remove_from_queue(coap_queue_t **queue, coap_session_t *session, coap_mid_t mid, coap_bin_const_t *token, coap_queue_t **node)
This function removes the element with given mid from the list given list.
coap_pdu_t * coap_new_error_response(const coap_pdu_t *request, coap_pdu_code_t code, coap_opt_filter_t *opts)
Creates a new ACK PDU with specified error code.
void coap_ticks(coap_tick_t *t)
Returns the current value of an internal tick counter.
@ COAP_RESPONSE_FAIL
Response not liked - send CoAP RST packet.
@ COAP_RESPONSE_OK
Response is fine.
unsigned int coap_encode_var_safe(uint8_t *buf, size_t length, unsigned int val)
Encodes multiple-length byte sequences.
unsigned int coap_decode_var_bytes(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
uint64_t coap_decode_var_bytes8(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
#define coap_lock_callback_ret_release(r, func, failed)
Dummy for no thread-safe code.
#define coap_lock_unlock()
Dummy for no thread-safe code.
#define coap_lock_check_locked()
Dummy for no thread-safe code.
#define coap_lock_lock(failed)
Dummy for no thread-safe code.
#define coap_log_debug(...)
coap_log_t coap_get_log_level(void)
Get the current logging level.
void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu)
Display the contents of the specified pdu.
size_t coap_print_addr(const coap_address_t *addr, unsigned char *buf, size_t len)
Print the address into the defined buffer.
const char * coap_session_str(const coap_session_t *session)
Get session description.
const char * coap_print_ip_addr(const coap_address_t *addr, char *buf, size_t len)
Print the IP address into the defined buffer.
#define coap_log_info(...)
#define coap_log_warn(...)
#define coap_log_err(...)
#define COAP_OBSERVE_CANCEL
The value COAP_OBSERVE_CANCEL in a GET/FETCH request option COAP_OPTION_OBSERVE indicates that the ob...
#define COAP_OBSERVE_ESTABLISH
The value COAP_OBSERVE_ESTABLISH in a GET/FETCH request option COAP_OPTION_OBSERVE indicates a new ob...
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
coap_optlist_t * coap_new_optlist(coap_option_num_t number, size_t length, const uint8_t *data)
Create a new optlist entry.
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
coap_opt_iterator_t * coap_option_iterator_init(const coap_pdu_t *pdu, coap_opt_iterator_t *oi, const coap_opt_filter_t *filter)
Initializes the given option iterator oi to point to the beginning of the pdu's option list.
void coap_delete_optlist(coap_optlist_t *queue)
Removes all entries from the optlist_chain, freeing off their memory usage.
#define COAP_OPT_ALL
Pre-defined filter that includes all options.
int coap_add_optlist_pdu(coap_pdu_t *pdu, coap_optlist_t **options)
The current optlist of optlist_chain is first sorted (as per RFC7272 ordering requirements) and then ...
void coap_option_filter_clear(coap_opt_filter_t *filter)
Clears filter filter.
coap_opt_t * coap_check_option(const coap_pdu_t *pdu, coap_option_num_t number, coap_opt_iterator_t *oi)
Retrieves the first option of number number from pdu.
int coap_insert_optlist(coap_optlist_t **head, coap_optlist_t *node)
Adds optlist to the given optlist_chain.
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
int coap_option_filter_set(coap_opt_filter_t *filter, coap_option_num_t option)
Sets the corresponding entry for number in filter.
coap_session_t * coap_new_client_session_oscore3_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_oscore_conf_t *oscore_conf, void *app_data, coap_app_data_free_callback_t callback, coap_str_const_t *ws_host)
Creates a new client session to the designated server, protecting the data using OSCORE,...
coap_session_t * coap_new_client_session_oscore_psk3_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_cpsk_t *psk_data, coap_oscore_conf_t *oscore_conf, void *app_data, coap_app_data_free_callback_t callback, coap_str_const_t *ws_host)
Creates a new client session to the designated server, with PSK credentials protecting the data using...
coap_session_t * coap_new_client_session_oscore_pki3_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_pki_t *pki_data, coap_oscore_conf_t *oscore_conf, void *app_data, coap_app_data_free_callback_t callback, coap_str_const_t *ws_host)
Creates a new client session to the designated server, with PKI credentials protecting the data using...
coap_pdu_t * coap_pdu_reference_lkd(coap_pdu_t *pdu)
Increment reference counter on a pdu to stop it prematurely getting freed off when coap_delete_pdu() ...
void coap_delete_pdu_lkd(coap_pdu_t *pdu)
Dispose of an CoAP PDU and free off associated storage.
coap_pdu_t * coap_pdu_duplicate_lkd(const coap_pdu_t *old_pdu, coap_session_t *session, size_t token_length, const uint8_t *token, coap_opt_filter_t *drop_options, coap_bool_t expand_opt_abb)
Duplicate an existing PDU.
size_t coap_update_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 pdu with the new data.
size_t coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto)
Compose the protocol specific header for the specified PDU.
coap_pdu_t * coap_const_pdu_reference_lkd(const coap_pdu_t *pdu)
Increment reference counter on a const pdu to stop it prematurely getting freed off when coap_delete_...
COAP_STATIC_INLINE void coap_pdu_release_lkd(coap_pdu_t *pdu)
coap_pdu_code_t coap_pdu_get_code(const coap_pdu_t *pdu)
Gets the PDU code associated with pdu.
#define COAP_DEFAULT_PORT
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
#define COAP_RESPONSE_CODE(N)
#define COAP_RESPONSE_CLASS(C)
coap_proto_t
CoAP protocol types Note: coap_layers_coap[] needs updating if extended.
coap_pdu_code_t
Set of codes available for a PDU.
#define COAP_MEDIATYPE_TEXT_PLAIN
int coap_add_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds token of length len to pdu.
#define COAPS_DEFAULT_PORT
int coap_get_data(const coap_pdu_t *pdu, size_t *len, const uint8_t **data)
Retrieves the length and data pointer of specified PDU.
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 with at least enough storage space for the given size maximum message size.
int coap_get_data_large(const coap_pdu_t *pdu, size_t *len, const uint8_t **data, size_t *offset, size_t *total)
Retrieves the data from a PDU, with support for large bodies of data that spans multiple PDUs.
#define COAP_INVALID_MID
Indicates an invalid message id.
coap_bin_const_t coap_pdu_get_token(const coap_pdu_t *pdu)
Gets the token associated with pdu.
COAP_API coap_session_t * coap_new_client_session_proxy(coap_context_t *context, coap_proxy_server_list_t *server_list)
Creates a new client session to use the proxy logic going to the defined upstream server.
coap_proxy_t
coap_proxy_t Proxy definitions.
COAP_API int coap_proxy_forward_request(coap_session_t *session, const coap_pdu_t *request, coap_pdu_t *response, coap_resource_t *resource, coap_cache_key_t *cache_key, coap_proxy_server_list_t *server_list)
Forward incoming request upstream to the next proxy/server.
#define COAP_PROXY_NEW_MASK
Space used in coap_proxy_t for new types.
int coap_verify_proxy_scheme_supported(coap_uri_scheme_t scheme)
Verify that the CoAP Scheme is supported for an ongoing proxy connection.
COAP_API coap_proxy_entry_t * coap_proxy_fwd_add_client_session(coap_session_t *session, const char *use_ip, uint16_t use_port, coap_proxy_server_list_t *server_list)
Add a previously setup proxy-client session to use the proxy logic for forwarding subsequent dynamic ...
COAP_API coap_response_t coap_proxy_forward_response(coap_session_t *session, const coap_pdu_t *received, coap_cache_key_t **cache_key)
Forward the returning response back to the appropriate client.
@ COAP_PROXY_DIRECT_STRIP
Old - do not use - Act as a forward-dynamic proxy, strip out proxy options.
@ COAP_PROXY_BIT_STRIP
If COAP_PROXY_BIT_STRIP set, then remove any Proxy-Uri or Proxy-Scheme as the PDU is forwarded,...
@ COAP_PROXY_REVERSE_STRIP
Old - do not use - Act as a reverse proxy, strip out proxy options.
@ COAP_PROXY_FWD_STATIC
forward-static proxy.
@ COAP_PROXY_REV
reverse proxy.
@ COAP_PROXY_REVERSE
Old - do not use - Act as a reverse proxy.
@ COAP_PROXY_DIRECT
Old - do not use - Act as a forward-dynamic proxy.
@ COAP_PROXY_DYN_DEFINED
If COAP_PROXY_DYN_DEFINED set, then no new dynamic upstream servers will get automatically added.
@ COAP_PROXY_FORWARD_STRIP
Old - do not use - Act as a forward-static proxy, strip out proxy options.
@ COAP_PROXY_FWD_DYNAMIC
forward-dynamic proxy.
@ COAP_PROXY_FORWARD
Old - do not use - Act as a forward-static proxy.
@ COAP_PROXY_BIT_MCAST
If COAP_PROXY_BIT_MCAST set, then upstream servers can be multicast, else only unicast.
coap_session_t * coap_new_client_session_pki3_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_pki_t *setup_data, void *app_data, coap_app_data_free_callback_t callback, coap_str_const_t *ws_host)
Creates a new client session to the designated server, with PKI credentials along with app_data infor...
#define COAP_MAX_TRANSMIT_WAIT_TICKS(s)
coap_session_t * coap_new_client_session_psk3_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_cpsk_t *setup_data, void *app_data, coap_app_data_free_callback_t callback, coap_str_const_t *ws_host)
Creates a new client session to the designated server, with PSK credentials along with app_data infor...
int coap_session_reconnect(coap_session_t *session)
Close the current session (if not already closed) and reconnect to server (client session only).
size_t coap_session_max_pdu_size_lkd(const coap_session_t *session)
Get maximum acceptable PDU size.
void coap_session_release_lkd(coap_session_t *session)
Decrement reference counter on a session.
coap_session_t * coap_new_client_session3_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, void *app_data, coap_app_data_free_callback_t callback, coap_str_const_t *ws_host)
Creates a new client session to the designated server, with PSK credentials along with app_data infor...
int coap_session_set_type_client_lkd(coap_session_t *session, int report_changed)
Set the session type to client.
#define COAP_PROTO_NOT_RELIABLE(p)
void coap_session_new_token(coap_session_t *session, size_t *len, uint8_t *data)
Creates a new token for use.
const coap_address_t * coap_session_get_addr_remote(const coap_session_t *session)
Get the remote IP address and port from the session.
void coap_delete_bin_const(coap_bin_const_t *s)
Deletes the given const binary data and releases any memory allocated.
coap_binary_t * coap_new_binary(size_t size)
Returns a new binary object with at least size bytes storage allocated.
coap_bin_const_t * coap_new_bin_const(const uint8_t *data, size_t size)
Take the specified byte array (text) and create a coap_bin_const_t * Returns a new const binary objec...
void coap_delete_binary(coap_binary_t *s)
Deletes the given coap_binary_t object and releases any memory allocated.
#define coap_binary_equal(binary1, binary2)
Compares the two binary data for equality.
#define coap_string_equal(string1, string2)
Compares the two strings for equality.
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
int coap_tcp_is_supported(void)
Check whether TCP is available.
int coap_tls_is_supported(void)
Check whether TLS is available.
int coap_ws_is_supported(void)
Check whether WebSockets is available.
int coap_dtls_is_supported(void)
Check whether DTLS is available.
int coap_proxy_is_supported(void)
Check whether Proxy code is available.
int coap_wss_is_supported(void)
Check whether Secure WebSockets is available.
coap_string_t * coap_get_uri_path(const coap_pdu_t *request)
Extract uri_path string from request PDU.
int coap_split_proxy_uri(const uint8_t *str_var, size_t len, coap_uri_t *uri)
Parses a given string into URI components.
int coap_uri_into_optlist(const coap_uri_t *uri, const coap_address_t *dst, coap_optlist_t **optlist_chain, int create_port_host_opt)
Takes a coap_uri_t and then adds CoAP options into the optlist_chain.
coap_string_t * coap_get_query(const coap_pdu_t *request)
Extract query string from request PDU according to escape rules in 6.5.8.
Resolved addresses information.
coap_proto_t proto
CoAP protocol to use.
coap_address_t addr
The address to connect / bind to.
coap_address_t remote
remote address and port
Multi-purpose address abstraction.
CoAP binary data definition with const data.
size_t length
length of binary data
const uint8_t * s
read-only binary data
CoAP binary data definition.
size_t length
length of binary data
The CoAP stack's global state is stored in a coap_context_t object.
char * client_sni
If not NULL, SNI to use in client TLS setup.
char * client_sni
If not NULL, SNI to use in client TLS setup.
Iterator to run through PDU options.
coap_option_num_t number
decoded option number
Representation of chained list of CoAP options to install.
size_t max_size
maximum size for token, options and payload, or zero for variable size pdu
const uint8_t * body_data
Holds ptr to re-assembled data or NULL.
coap_pdu_code_t code
request method (value 1–31) or response code (value 64-255)
coap_bin_const_t actual_token
Actual token in pdu.
coap_mid_t mid
message id, if any, in regular host byte order
size_t used_size
used bytes of storage for token, options and payload
coap_session_t * session
Session responsible for PDU or NULL.
coap_pdu_type_t type
message type
int track_client_session
If 1, track individual connections to upstream server, else 0 for all clients to be multiplexed over ...
coap_proxy_server_t * entry
Set of servers to connect to.
coap_proxy_t type
The proxy type and option controlling bits (if old type used, will get mapped into new + bits.
unsigned int idle_timeout_secs
Proxy upstream session idle timeout (0 is no timeout).
size_t next_entry
Next server to use (% entry_count).
size_t entry_count
The number of servers in entry list.
coap_dtls_pki_t * dtls_pki
PKI configuration to use if not NULL.
coap_oscore_conf_t * oscore_conf
OSCORE configuration if not NULL.
coap_uri_t uri
host and port define the server, scheme method
coap_dtls_cpsk_t * dtls_cpsk
PSK configuration to use if not NULL.
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
coap_addr_tuple_t addr_info
remote/local address info
coap_proto_t proto
protocol used
coap_response_t last_con_handler_res
The result of calling the response handler of the last CON.
coap_context_t * context
session's context
CoAP string data definition with const data.
const uint8_t * s
read-only string data
size_t length
length of string
CoAP string data definition.
Representation of parsed URI.
enum coap_uri_scheme_t scheme
The parsed scheme specifier.
uint16_t port
The port in host byte order.
coap_str_const_t host
The host part of the URI.