libcoap 4.3.5-develop-17c3fee
Loading...
Searching...
No Matches
coap_proxy.c
Go to the documentation of this file.
1/* coap_proxy.c -- helper functions for proxy handling
2 *
3 * Copyright (C) 2024-2025 Jon Shallow <supjps-libcoap@jpshallow.com>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 *
7 * This file is part of the CoAP library libcoap. Please see
8 * README for terms of use.
9 */
10
17
18#if COAP_PROXY_SUPPORT
19#include <stdio.h>
20#ifndef INET6_ADDRSTRLEN
21#define INET6_ADDRSTRLEN 46
22#endif
23
24#if COAP_CLIENT_SUPPORT == 0
25#error For Proxy support, COAP_CLIENT_SUPPORT must be set
26#endif
27#if COAP_SERVER_SUPPORT == 0
28#error For Proxy support, COAP_SERVER_SUPPORT must be set
29#endif
30
31#ifdef _WIN32
32#define strcasecmp _stricmp
33#define strncasecmp _strnicmp
34#endif
35
36int
38 return 1;
39}
40
41static void
42coap_proxy_log_entry(coap_session_t *incoming, const coap_pdu_t *pdu,
43 coap_bin_const_t *upstream_token, const char *type) {
45 coap_string_t *url;
46
47 url = coap_get_uri_path(pdu);
48 if (url) {
49 coap_opt_iterator_t opt_iter;
50 uint8_t addr[INET6_ADDRSTRLEN + 8];
51 char scratch_d[24];
52 char scratch_u[24];
53 size_t size;
54 size_t i;
55
56 scratch_d[0] = '\000';
57 for (i = 0; i < pdu->actual_token.length; i++) {
58 size = strlen(scratch_d);
59 snprintf(&scratch_d[size], sizeof(scratch_d)-size,
60 "%02x", pdu->actual_token.s[i]);
61 }
62 scratch_u[0] = '\000';
63 if (upstream_token) {
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]);
68 }
69 }
70 coap_print_addr(coap_session_get_addr_remote(incoming), addr, sizeof(addr));
71 coap_log_debug(" proxy %-4s %s {%s}-{%s} \"%s\"%s\n", type, addr, scratch_u, scratch_d,
72 url->s, coap_check_option(pdu, COAP_OPTION_OBSERVE, &opt_iter) ? " Observe" : "");
74 }
75 }
76}
77
78void
79coap_proxy_del_req(coap_proxy_list_t *proxy_entry, coap_proxy_req_t *proxy_req) {
80 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used, "del");
81
82 coap_delete_pdu_lkd(proxy_req->pdu);
83 coap_delete_bin_const(proxy_req->token_used);
84 coap_delete_cache_key(proxy_req->cache_key);
85 if (proxy_req->proxy_cache) {
87 coap_string_t *url;
88
89 url = coap_get_uri_path(proxy_req->proxy_cache->req_pdu);
90 coap_log_debug("***%s: Removing Proxy Cache \"%s\" (Active %d)\n",
91 coap_session_str(proxy_req->incoming),
92 url ? (char *)url->s : "???",
93 proxy_req->proxy_cache->ref);
95 }
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);
100 coap_delete_pdu_lkd(proxy_req->proxy_cache->req_pdu);
101 coap_delete_pdu_lkd(proxy_req->proxy_cache->rsp_pdu);
102 coap_free_type(COAP_STRING, proxy_req->proxy_cache);
103 proxy_req->proxy_cache = NULL;
104 }
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;
109 }
110 }
111 /* To prevent potential loops */
112 proxy_req->incoming = NULL;
113
114 LL_DELETE(proxy_entry->proxy_req, proxy_req);
115 coap_free_type(COAP_STRING, proxy_req);
116}
117
118static void
119coap_proxy_cleanup_entry(coap_proxy_list_t *proxy_entry, int send_failure) {
120 coap_proxy_req_t *proxy_req, *treq;
121
122 LL_FOREACH_SAFE(proxy_entry->proxy_req, proxy_req, treq) {
123 if (send_failure) {
124 coap_pdu_t *response;
125 coap_bin_const_t l_token;
126
127 /* Need to send back a gateway failure */
128 response = coap_pdu_init(proxy_req->pdu->type,
130 coap_new_message_id_lkd(proxy_entry->incoming),
131 coap_session_max_pdu_size_lkd(proxy_entry->incoming));
132 if (!response) {
133 coap_log_info("PDU creation issue\n");
134 goto cleanup;
135 }
136
137 l_token = coap_pdu_get_token(proxy_req->pdu);
138 if (!coap_add_token(response, l_token.length,
139 l_token.s)) {
140 coap_log_debug("Cannot add token to incoming proxy response PDU\n");
141 }
142
143 if (coap_send_lkd(proxy_entry->incoming, response) == COAP_INVALID_MID) {
144 coap_log_info("Failed to send PDU with 5.02 gateway issue\n");
145 }
146 }
147cleanup:
148 coap_proxy_del_req(proxy_entry, proxy_req);
149 }
150 coap_free_type(COAP_STRING, proxy_entry->uri_host_keep);
151}
152
153void
154coap_proxy_cleanup(coap_context_t *context) {
155 size_t i;
156
157 for (i = 0; i < context->proxy_list_count; i++) {
158 /* All sessions have now been closed down */
159 coap_log_debug("proxy_entry %p cleaned up\n",
160 (void *)&context->proxy_list[i]);
161 coap_proxy_cleanup_entry(&context->proxy_list[i], 0);
162 }
163 coap_free_type(COAP_STRING, context->proxy_list);
164}
165
166static int
167coap_proxy_check_observe(coap_proxy_list_t *proxy_entry) {
168 if (proxy_entry && proxy_entry->ongoing) {
169 /* Need to see if there are any Observes active */
170 coap_lg_crcv_t *lg_crcv;
171
172 LL_FOREACH(proxy_entry->ongoing->lg_crcv, lg_crcv) {
173 if (lg_crcv->observe_set) {
174 return 1;
175 }
176 }
177 }
178 return 0;
179}
180
181/*
182 * Return 1 if there is a future expire time, else 0.
183 * Update tim_rem with remaining value if return is 1.
184 */
185int
186coap_proxy_check_timeouts(coap_context_t *context, coap_tick_t now,
187 coap_tick_t *tim_rem) {
188 size_t i;
189 int ret = 0;
190
191 *tim_rem = COAP_MAX_DELAY_TICKS;
192 for (i = 0; i < context->proxy_list_count; i++) {
193 coap_proxy_list_t *proxy_entry = &context->proxy_list[i];
194
195 if (coap_proxy_check_observe(proxy_entry))
196 continue;
197
198 if (proxy_entry->ongoing && proxy_entry->idle_timeout_ticks) {
199 if (proxy_entry->last_used + proxy_entry->idle_timeout_ticks <= now) {
200 /* Drop session to upstream server (which may remove proxy entry) */
201 if (coap_proxy_remove_association(proxy_entry->ongoing, 0))
202 i--;
203 } else {
204 if (*tim_rem > proxy_entry->last_used + proxy_entry->idle_timeout_ticks - now) {
205 *tim_rem = proxy_entry->last_used + proxy_entry->idle_timeout_ticks - now;
206 ret = 1;
207 }
208 }
209 }
210 }
211 return ret;
212}
213
214static int
215coap_get_uri_proxy_scheme_info(const coap_pdu_t *request,
216 coap_opt_t *opt,
217 coap_uri_t *uri) {
218 const char *opt_val = (const char *)coap_opt_value(opt);
219 int opt_len = coap_opt_length(opt);
220 coap_opt_iterator_t opt_iter;
221
222 if (opt_len == 9 &&
223 strncasecmp(opt_val, "coaps+tcp", 9) == 0) {
226 } else if (opt_len == 8 &&
227 strncasecmp(opt_val, "coap+tcp", 8) == 0) {
229 uri->port = COAP_DEFAULT_PORT;
230 } else if (opt_len == 5 &&
231 strncasecmp(opt_val, "coaps", 5) == 0) {
234 } else if (opt_len == 4 &&
235 strncasecmp(opt_val, "coap", 4) == 0) {
237 uri->port = COAP_DEFAULT_PORT;
238 } else if (opt_len == 7 &&
239 strncasecmp(opt_val, "coap+ws", 7) == 0) {
241 uri->port = 80;
242 } else if (opt_len == 8 &&
243 strncasecmp(opt_val, "coaps+ws", 8) == 0) {
245 uri->port = 443;
246 } else {
247 coap_log_warn("Unsupported Proxy Scheme '%*.*s'\n",
248 opt_len, opt_len, opt_val);
249 return 0;
250 }
251
252 opt = coap_check_option(request, COAP_OPTION_URI_HOST, &opt_iter);
253 if (opt) {
254 uri->host.length = coap_opt_length(opt);
255 uri->host.s = coap_opt_value(opt);
256 } else {
257 uri->host.s = NULL;
258 uri->host.length = 0;
259 coap_log_warn("Proxy Scheme requires Uri-Host\n");
260 return 0;
261 }
262 opt = coap_check_option(request, COAP_OPTION_URI_PORT, &opt_iter);
263 if (opt) {
264 uri->port =
266 coap_opt_length(opt));
267 }
268 return 1;
269}
270
271int
273
274 /* Sanity check that the connection can be forwarded on */
275 switch (scheme) {
278 coap_log_warn("Proxy URI http or https not supported\n");
279 return 0;
281 break;
283 if (!coap_dtls_is_supported()) {
284 coap_log_warn("coaps URI scheme not supported for proxy\n");
285 return 0;
286 }
287 break;
289 if (!coap_tcp_is_supported()) {
290 coap_log_warn("coap+tcp URI scheme not supported for proxy\n");
291 return 0;
292 }
293 break;
295 if (!coap_tls_is_supported()) {
296 coap_log_warn("coaps+tcp URI scheme not supported for proxy\n");
297 return 0;
298 }
299 break;
301 if (!coap_ws_is_supported()) {
302 coap_log_warn("coap+ws URI scheme not supported for proxy\n");
303 return 0;
304 }
305 break;
307 if (!coap_wss_is_supported()) {
308 coap_log_warn("coaps+ws URI scheme not supported for proxy\n");
309 return 0;
310 }
311 break;
313 default:
314 coap_log_warn("%d URI scheme not supported\n", scheme);
315 return 0;
316 }
317 return 1;
318}
319
320static coap_proxy_list_t *
321coap_proxy_get_session(coap_session_t *session, const coap_pdu_t *request,
322 coap_pdu_t *response,
323 coap_proxy_server_list_t *server_list,
324 coap_proxy_server_t *server_use, int *proxy_entry_created) {
325 size_t i;
326 coap_proxy_list_t *new_proxy_list;
327 coap_proxy_list_t *proxy_list = session->context->proxy_list;
328 size_t proxy_list_count = session->context->proxy_list_count;
329 coap_opt_iterator_t opt_iter;
330 coap_opt_t *proxy_scheme;
331 coap_opt_t *proxy_uri;
332
333 *proxy_entry_created = 0;
334
335 /*
336 * Maintain server stickability. server_use not needed as there is
337 * ongoing session in place.
338 */
339 if (session->proxy_entry) {
340 for (i = 0; i < proxy_list_count; i++) {
341 if (&proxy_list[i] == session->proxy_entry) {
342 if (session->proxy_entry->ongoing) {
343 memset(server_use, 0, sizeof(*server_use));
344 return session->proxy_entry;
345 }
346 }
347 }
348 }
349
350 /* Round robin the defined next server list (which usually is just one */
351 server_list->next_entry++;
352 if (server_list->next_entry >= server_list->entry_count)
353 server_list->next_entry = 0;
354
355 if (server_list->entry_count) {
356 memcpy(server_use, &server_list->entry[server_list->next_entry], sizeof(*server_use));
357 } else {
358 memset(server_use, 0, sizeof(*server_use));
359 }
360
361 switch (server_list->type) {
366 /* Nothing else needs to be done here */
367 break;
370 /* Need to get actual server from CoAP Proxy-Uri or Proxy-Scheme options */
371 /*
372 * See if Proxy-Scheme
373 */
374 proxy_scheme = coap_check_option(request, COAP_OPTION_PROXY_SCHEME, &opt_iter);
375 if (proxy_scheme) {
376 if (!coap_get_uri_proxy_scheme_info(request, proxy_scheme, &server_use->uri)) {
377 response->code = COAP_RESPONSE_CODE(505);
378 return NULL;
379 }
380 }
381 /*
382 * See if Proxy-Uri
383 */
384 proxy_uri = coap_check_option(request, COAP_OPTION_PROXY_URI, &opt_iter);
385 if (proxy_uri) {
386 coap_log_info("Proxy URI '%.*s'\n",
387 (int)coap_opt_length(proxy_uri),
388 (const char *)coap_opt_value(proxy_uri));
390 coap_opt_length(proxy_uri),
391 &server_use->uri) < 0) {
392 /* Need to return a 5.05 RFC7252 Section 5.7.2 */
393 coap_log_warn("Proxy URI not decodable\n");
394 response->code = COAP_RESPONSE_CODE(505);
395 return NULL;
396 }
397 }
398
399 if (!(proxy_scheme || proxy_uri)) {
400 response->code = COAP_RESPONSE_CODE(404);
401 return NULL;
402 }
403 break;
404 default:
405 assert(0);
406 return NULL;
407 }
408
409 if (server_use->uri.host.length == 0) {
410 /* Ongoing connection not well formed */
411 response->code = COAP_RESPONSE_CODE(505);
412 return NULL;
413 }
414
416 response->code = COAP_RESPONSE_CODE(505);
417 return NULL;
418 }
419
420 /* See if we are already connected to the Server */
421 for (i = 0; i < proxy_list_count; i++) {
422 if (coap_string_equal(&proxy_list[i].uri.host, &server_use->uri.host) &&
423 proxy_list[i].uri.port == server_use->uri.port &&
424 proxy_list[i].uri.scheme == server_use->uri.scheme) {
425 if (!server_list->track_client_session && session->context->proxy_response_handler) {
426 coap_ticks(&proxy_list[i].last_used);
427 return &proxy_list[i];
428 } else {
429 if (proxy_list[i].incoming == session) {
430 coap_ticks(&proxy_list[i].last_used);
431 return &proxy_list[i];
432 }
433 }
434 }
435 }
436
437 /* Need to create a new forwarding mapping */
438 new_proxy_list = coap_realloc_type(COAP_STRING, proxy_list, (i+1)*sizeof(proxy_list[0]));
439
440 if (new_proxy_list == NULL) {
441 response->code = COAP_RESPONSE_CODE(500);
442 return NULL;
443 }
444 session->context->proxy_list = proxy_list = new_proxy_list;
445 memset(&proxy_list[i], 0, sizeof(proxy_list[i]));
446
447 /* Keep a copy of the host as server_use->uri pointed to will be going away */
448 proxy_list[i].uri = server_use->uri;
449 proxy_list[i].uri_host_keep = coap_malloc_type(COAP_STRING,
450 server_use->uri.host.length);
451 if (!proxy_list[i].uri_host_keep) {
452 response->code = COAP_RESPONSE_CODE(500);
453 return NULL;
454 }
455 memcpy(proxy_list[i].uri_host_keep, server_use->uri.host.s,
456 server_use->uri.host.length);
457 proxy_list[i].uri.host.s = proxy_list[i].uri_host_keep;
458 /* Unset uri parts which point to going away information */
459 proxy_list[i].uri.path.s = NULL;
460 proxy_list[i].uri.path.length = 0;
461 proxy_list[i].uri.query.s = NULL;
462 proxy_list[i].uri.query.length = 0;
463
464 if (server_list->track_client_session) {
465 proxy_list[i].incoming = session;
466 }
467 *proxy_entry_created = 1;
468 session->context->proxy_list_count++;
469 proxy_list[i].idle_timeout_ticks = server_list->idle_timeout_secs * COAP_TICKS_PER_SECOND;
470 coap_ticks(&proxy_list[i].last_used);
471 session->proxy_entry = &proxy_list[i];
472 return &proxy_list[i];
473}
474
475int
476coap_proxy_remove_association(coap_session_t *session, int send_failure) {
477
478 size_t i;
479 coap_proxy_list_t *proxy_list = session->context->proxy_list;
480 size_t proxy_list_count = session->context->proxy_list_count;
481
482 for (i = 0; i < proxy_list_count; i++) {
483 coap_proxy_list_t *proxy_entry = &proxy_list[i];
484 coap_proxy_req_t *proxy_req;
485
486 /* Check for incoming match */
487retry:
488 LL_FOREACH(proxy_entry->proxy_req, proxy_req) {
489 if (proxy_req->incoming == session) {
490 coap_proxy_del_req(proxy_entry, proxy_req);
491 goto retry;
492 }
493 }
494 if (proxy_entry->incoming == session) {
495 /* Only if there is a one-to-one tracking */
496 coap_session_t *ongoing = proxy_entry->ongoing;
497
498 proxy_entry->ongoing = NULL;
500 return 0;
501 }
502
503 /* Check for outgoing match */
504 if (proxy_entry->ongoing == session) {
505 coap_session_t *ongoing;
506
507 coap_proxy_cleanup_entry(proxy_entry, send_failure);
508 ongoing = proxy_entry->ongoing;
509 coap_log_debug("* %s: proxy_entry %p released (rem count = %zd)\n",
510 coap_session_str(ongoing),
511 (void *)proxy_entry,
512 session->context->proxy_list_count - 1);
513 if (proxy_list_count-i > 1) {
514 memmove(&proxy_list[i],
515 &proxy_list[i+1],
516 (proxy_list_count-i-1) * sizeof(proxy_list[0]));
517 }
518 session->context->proxy_list_count--;
520 return 1;
521 }
522 }
523 return 0;
524}
525
526static coap_proxy_list_t *
527coap_proxy_get_ongoing_session(coap_session_t *session,
528 const coap_pdu_t *request,
529 coap_pdu_t *response,
530 coap_proxy_server_list_t *server_list) {
531
532 coap_address_t dst;
533 coap_proto_t proto;
534 coap_addr_info_t *info_list = NULL;
535 coap_proxy_list_t *proxy_entry;
536 coap_context_t *context = session->context;
537 static char client_sni[256];
538 coap_proxy_server_t server_use;
539 int proxy_entry_created;
540
541 proxy_entry = coap_proxy_get_session(session, request, response, server_list,
542 &server_use, &proxy_entry_created);
543 if (!proxy_entry) {
544 /* Error response code already set */
545 return NULL;
546 }
547
548 if (!proxy_entry->ongoing) {
549 /* Need to create a new session */
550 coap_address_t *local_addr = NULL;
551
552 /* resolve destination address where data should be sent */
553 info_list = coap_resolve_address_info(&server_use.uri.host,
554 server_use.uri.port,
555 server_use.uri.port,
556 server_use.uri.port,
557 server_use.uri.port,
558 0,
559 1 << server_use.uri.scheme,
561
562 if (info_list == NULL) {
563 response->code = COAP_RESPONSE_CODE(502);
564 coap_proxy_remove_association(session, 0);
565 return NULL;
566 }
567 proto = info_list->proto;
568 memcpy(&dst, &info_list->addr, sizeof(dst));
569 coap_free_address_info(info_list);
570
571#if COAP_AF_UNIX_SUPPORT
572 coap_address_t bind_addr;
573 if (coap_is_af_unix(&dst)) {
574 char buf[COAP_UNIX_PATH_MAX];
575 coap_tick_t now;
576
577 /* Need a unique 'client' address */
578 coap_ticks(&now);
579 snprintf(buf, COAP_UNIX_PATH_MAX,
580 "/tmp/coap-pr-cl-%" PRIu64, (uint64_t)now);
581 if (!coap_address_set_unix_domain(&bind_addr, (const uint8_t *)buf,
582 strlen(buf))) {
583 fprintf(stderr, "coap_address_set_unix_domain: %s: failed\n",
584 buf);
585 remove(buf);
586 return NULL;
587 }
588 (void)remove(buf);
589 local_addr = &bind_addr;
590 }
591#endif /* COAP_AF_UNIX_SUPPORT */
592
593 snprintf(client_sni, sizeof(client_sni), "%*.*s", (int)server_use.uri.host.length,
594 (int)server_use.uri.host.length, server_use.uri.host.s);
595
596 switch (server_use.uri.scheme) {
600#if COAP_OSCORE_SUPPORT
601 if (server_use.oscore_conf) {
602 proxy_entry->ongoing =
603 coap_new_client_session_oscore_lkd(context, local_addr, &dst,
604 proto, server_use.oscore_conf);
605 } else {
606#endif /* COAP_OSCORE_SUPPORT */
607 proxy_entry->ongoing =
608 coap_new_client_session_lkd(context, local_addr, &dst, proto);
609#if COAP_OSCORE_SUPPORT
610 }
611#endif /* COAP_OSCORE_SUPPORT */
612 break;
616#if COAP_OSCORE_SUPPORT
617 if (server_use.oscore_conf) {
618 if (server_use.dtls_pki) {
619 server_use.dtls_pki->client_sni = client_sni;
620 proxy_entry->ongoing =
621 coap_new_client_session_oscore_pki_lkd(context, local_addr, &dst,
622 proto, server_use.dtls_pki, server_use.oscore_conf);
623 } else if (server_use.dtls_cpsk) {
624 server_use.dtls_cpsk->client_sni = client_sni;
625 proxy_entry->ongoing =
626 coap_new_client_session_oscore_psk_lkd(context, local_addr, &dst,
627 proto, server_use.dtls_cpsk, server_use.oscore_conf);
628 } else {
629 coap_log_warn("Proxy: (D)TLS not configured for secure session\n");
630 }
631 } else {
632#endif /* COAP_OSCORE_SUPPORT */
633 /* Not doing OSCORE */
634 if (server_use.dtls_pki) {
635 server_use.dtls_pki->client_sni = client_sni;
636 proxy_entry->ongoing =
637 coap_new_client_session_pki_lkd(context, local_addr, &dst,
638 proto, server_use.dtls_pki);
639 } else if (server_use.dtls_cpsk) {
640 server_use.dtls_cpsk->client_sni = client_sni;
641 proxy_entry->ongoing =
642 coap_new_client_session_psk2_lkd(context, local_addr, &dst,
643 proto, server_use.dtls_cpsk);
644 } else {
645 /* Using client anonymous PKI */
646 proxy_entry->ongoing =
647 coap_new_client_session_lkd(context, local_addr, &dst, proto);
648 }
649#if COAP_OSCORE_SUPPORT
650 }
651#endif /* COAP_OSCORE_SUPPORT */
652 break;
656 default:
657 assert(0);
658 break;
659 }
660 if (proxy_entry->ongoing == NULL) {
661 response->code = COAP_RESPONSE_CODE(505);
662 coap_proxy_remove_association(session, 0);
663 return NULL;
664 }
665 if (proxy_entry_created) {
666 coap_log_debug("* %s: proxy_entry %p created (tot count = %zd)\n",
667 coap_session_str(proxy_entry->ongoing),
668 (void *)proxy_entry,
669 session->context->proxy_list_count);
670 }
671 } else if (proxy_entry->ongoing->session_failed) {
672 if (!coap_session_reconnect(proxy_entry->ongoing)) {
673 /* Server is not yet back up */
674 return NULL;
675 }
676 }
677
678 return proxy_entry;
679}
680
681static void
682coap_proxy_release_body_data(coap_session_t *session COAP_UNUSED,
683 void *app_ptr) {
684 coap_delete_binary(app_ptr);
685}
686
687static coap_proxy_req_t *
688coap_proxy_get_req(coap_proxy_list_t *proxy_entry, coap_proxy_cache_t *proxy_cache,
689 coap_session_t *session) {
690 coap_proxy_req_t *proxy_req;
691
692 LL_FOREACH(proxy_entry->proxy_req, proxy_req) {
693 if (proxy_req->incoming == session && proxy_req->proxy_cache == proxy_cache) {
694 return proxy_req;
695 }
696 }
697 return NULL;
698}
699
700static void
701coap_proxy_free_response_data(coap_session_t *session COAP_UNUSED, void *app_ptr) {
702 coap_delete_bin_const(app_ptr);
703}
704
705static coap_response_t
706coap_proxy_call_response_handler(coap_session_t *session, const coap_pdu_t *sent,
707 coap_pdu_t *rcvd, coap_bin_const_t *token,
708 coap_proxy_req_t *proxy_req, int replace_mid,
709 int remove_observe) {
711 coap_pdu_t *resp_pdu;
712 coap_pdu_t *fwd_pdu = NULL;
713 size_t size;
714 size_t offset;
715 size_t total;
716 const uint8_t *data;
717 coap_string_t *l_query = NULL;
718
719 if (remove_observe) {
720 coap_opt_filter_t drop_options;
721
722 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
724 /* Correct the token */
725 resp_pdu = coap_pdu_duplicate_lkd(rcvd, session, token->length, token->s,
726 &drop_options);
727 } else {
728 /* Correct the token */
729 resp_pdu = coap_pdu_duplicate_lkd(rcvd, session, token->length, token->s,
730 NULL);
731 }
732 if (!resp_pdu)
733 return COAP_RESPONSE_FAIL;
734
735 if (COAP_PROTO_NOT_RELIABLE(session->proto))
736 resp_pdu->max_size = coap_session_max_pdu_size_lkd(session);
737
738 if (replace_mid)
739 resp_pdu->mid = sent->mid;
740
741 proxy_req->mid = resp_pdu->mid;
742
743 if (coap_get_data_large(rcvd, &size, &data, &offset, &total)) {
744 uint16_t media_type = 0;
745 int maxage = -1;
746 uint64_t etag = 0;
747 coap_opt_t *option;
748 coap_opt_iterator_t opt_iter;
749 coap_bin_const_t *body;
750
751 /* COAP_BLOCK_SINGLE_BODY is set, so single body should be given */
752 assert(size == total);
753
754 body = coap_new_bin_const(data, size);
755 if (!body) {
756 coap_log_debug("coap_proxy_call_response_handler: copy data error\n");
757 goto failed;
758 }
759 option = coap_check_option(rcvd, COAP_OPTION_CONTENT_FORMAT, &opt_iter);
760 if (option) {
761 media_type = coap_decode_var_bytes(coap_opt_value(option),
762 coap_opt_length(option));
763 }
764 option = coap_check_option(rcvd, COAP_OPTION_MAXAGE, &opt_iter);
765 if (option) {
766 maxage = coap_decode_var_bytes(coap_opt_value(option),
767 coap_opt_length(option));
768 }
769 option = coap_check_option(rcvd, COAP_OPTION_ETAG, &opt_iter);
770 if (option) {
772 coap_opt_length(option));
773 }
774 if (sent)
775 l_query = coap_get_query(sent);
776 if (!coap_add_data_large_response_lkd(proxy_req->resource, session, sent,
777 resp_pdu,
778 l_query,
779 media_type, maxage, etag, body->length,
780 body->s,
781 coap_proxy_free_response_data,
782 body)) {
783 coap_log_debug("coap_proxy_call_response_handler: add data error\n");
784 goto failed;
785 }
786 }
788 session->context->proxy_response_handler(session,
789 sent,
790 resp_pdu,
791 proxy_req->cache_key),
792 /* context is being freed off */
793 goto failed);
794 if (fwd_pdu) {
795 ret = COAP_RESPONSE_OK;
796 if (coap_send_lkd(session, fwd_pdu) == COAP_INVALID_MID) {
797 ret = COAP_RESPONSE_FAIL;
798 }
799 if (fwd_pdu != resp_pdu) {
800 /* Application created a new PDU */
801 coap_delete_pdu_lkd(resp_pdu);
802 }
803 } else {
804failed:
805 ret = COAP_RESPONSE_FAIL;
806 coap_delete_pdu_lkd(resp_pdu);
807 }
808 coap_delete_string(l_query);
809 return ret;
810}
811
812int COAP_API
814 const coap_pdu_t *request,
815 coap_pdu_t *response,
816 coap_resource_t *resource,
817 coap_cache_key_t *cache_key,
818 coap_proxy_server_list_t *server_list) {
819 int ret;
820
821 coap_lock_lock(return 0);
822 ret = coap_proxy_forward_request_lkd(session,
823 request,
824 response,
825 resource,
826 cache_key,
827 server_list);
829 return ret;
830}
831
832/* https://rfc-editor.org/rfc/rfc7641#section-3.6 */
833static const uint16_t coap_proxy_ignore_options[] = { COAP_OPTION_ETAG,
838 };
839
840int
841coap_proxy_forward_request_lkd(coap_session_t *session,
842 const coap_pdu_t *request,
843 coap_pdu_t *response,
844 coap_resource_t *resource,
845 coap_cache_key_t *cache_key,
846 coap_proxy_server_list_t *server_list) {
847 coap_proxy_list_t *proxy_entry;
848 size_t size;
849 size_t offset;
850 size_t total;
851 coap_binary_t *body_data = NULL;
852 const uint8_t *data;
853 coap_pdu_t *pdu = NULL;
854 coap_bin_const_t r_token = coap_pdu_get_token(request);
855 uint8_t token[8];
856 size_t token_len;
857 coap_proxy_req_t *proxy_req = NULL;
858 coap_optlist_t *optlist = NULL;
859 coap_opt_t *option;
860 coap_opt_iterator_t opt_iter;
861 coap_uri_t uri;
862 coap_opt_t *obs_opt = coap_check_option(request,
864 &opt_iter);
865 coap_proxy_cache_t *proxy_cache = NULL;
866
867 /* Set up ongoing session (if not already done) */
868 proxy_entry = coap_proxy_get_ongoing_session(session, request, response,
869 server_list);
870 if (!proxy_entry) {
871 /* Error response code already set */
872 return 0;
873 }
874 coap_proxy_log_entry(session, request, NULL, "req");
875
876 /* Is this a observe cached request? */
877 if (obs_opt && session->context->proxy_response_handler) {
878 coap_cache_key_t *cache_key_l;
879
880 cache_key_l = coap_cache_derive_key_w_ignore(session, request,
882 coap_proxy_ignore_options,
883 sizeof(coap_proxy_ignore_options)/sizeof(coap_proxy_ignore_options[0]));
884 if (!cache_key_l) {
885 response->code = COAP_RESPONSE_CODE(505);
886 return 0;
887 }
888 PROXY_CACHE_FIND(proxy_entry->rsp_cache, cache_key_l, proxy_cache);
889 coap_delete_cache_key(cache_key_l);
890 }
891
892 if (proxy_cache) {
893 proxy_req = coap_proxy_get_req(proxy_entry, proxy_cache, session);
894 if (proxy_req) {
895 coap_tick_t now;
896
897 if (obs_opt) {
898 int observe_action;
899
900 observe_action = coap_decode_var_bytes(coap_opt_value(obs_opt),
901 coap_opt_length(obs_opt));
902
903 if (observe_action == COAP_OBSERVE_CANCEL) {
904 if (proxy_req->doing_observe) {
905 assert(proxy_req->incoming->ref_proxy_subs);
906 proxy_req->incoming->ref_proxy_subs--;
907 proxy_req->doing_observe = 0;
908 }
909 if (proxy_entry->proxy_req && !proxy_entry->proxy_req->next &&
910 proxy_req->token_used) {
911 coap_binary_t tmp;
912
913 coap_log_debug("coap_proxy_forward_request: Using coap_cancel_observe() to do Proxy OBSERVE cancellation\n");
914 /* Unfortunately need to change the ptr type to be r/w */
915 memcpy(&tmp.s, &proxy_req->token_used->s, sizeof(tmp.s));
916 tmp.length = proxy_req->token_used->length;
917 coap_cancel_observe_lkd(proxy_entry->ongoing, &tmp, COAP_MESSAGE_CON);
918 /* Let the upstream cancellation be the response */
919 return 1;
920 } else {
921 obs_opt = 0;
922 goto return_cached_info;
923 }
924 } else if (observe_action == COAP_OBSERVE_ESTABLISH) {
925 /* Client must be re-registering */
926 coap_ticks(&now);
927 if (proxy_cache && (proxy_cache->expire + COAP_TICKS_PER_SECOND) < now) {
928 /* Need to get an updated rsp_pdu */
929 coap_proxy_del_req(proxy_entry, proxy_req);
930 proxy_req = NULL;
931 proxy_cache = NULL;
932 } else {
933 goto return_cached_info;
934 }
935 }
936 } else {
937 coap_ticks(&now);
938 if (proxy_cache && (proxy_cache->expire + COAP_TICKS_PER_SECOND) < now) {
939 /* Need to get an updated rsp_pdu */
940 coap_proxy_del_req(proxy_entry, proxy_req);
941 proxy_req = NULL;
942 proxy_cache = NULL;
943 } else {
944 goto return_cached_info;
945 }
946 }
947 }
948 }
949
950 if (!proxy_req) {
951 proxy_req = coap_malloc_type(COAP_STRING, sizeof(coap_proxy_req_t));
952 if (proxy_req == NULL) {
953 goto failed;
954 }
955 memset(proxy_req, 0, sizeof(coap_proxy_req_t));
956 LL_PREPEND(proxy_entry->proxy_req, proxy_req);
957
958 proxy_req->pdu = coap_const_pdu_reference_lkd(request);
959 proxy_req->resource = resource;
960 proxy_req->incoming = session;
961 proxy_req->cache_key = cache_key;
962 proxy_req->proxy_cache = proxy_cache;
963
964 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used, "add");
965 }
966
967 if (proxy_cache) {
968 coap_bin_const_t j_token;
969
970 if (obs_opt)
971 proxy_cache->ref++;
972 coap_delete_bin_const(proxy_req->token_used);
973 j_token = coap_pdu_get_token(proxy_cache->rsp_pdu);
974 proxy_req->token_used = coap_new_bin_const(j_token.s, j_token.length);
975 if (proxy_req->token_used == NULL) {
976 goto failed;
977 }
978 goto return_cached_info;
979 }
980
981 /* Get a new token for ongoing session */
982 coap_session_new_token(proxy_entry->ongoing, &token_len, token);
983 coap_delete_bin_const(proxy_req->token_used);
984 proxy_req->token_used = coap_new_bin_const(token, token_len);
985 if (proxy_req->token_used == NULL) {
986 goto failed;
987 }
988 coap_pdu_release_lkd(proxy_req->pdu);
989 proxy_req->pdu = coap_const_pdu_reference_lkd(request);
990
991 /* Need to create the ongoing request pdu entry */
992 switch (server_list->type) {
996 /*
997 * Need to replace Proxy-Uri with Uri-Host (and Uri-Port)
998 * and strip out Proxy-Scheme.
999 */
1000
1001 /*
1002 * Build up the ongoing PDU that we are going to send
1003 */
1004 pdu = coap_pdu_init(request->type, request->code,
1005 coap_new_message_id_lkd(proxy_entry->ongoing),
1006 coap_session_max_pdu_size_lkd(proxy_entry->ongoing));
1007 if (!pdu) {
1008 goto failed;
1009 }
1010
1011 if (!coap_add_token(pdu, token_len, token)) {
1012 goto failed;
1013 }
1014
1015 /* Copy the remaining options across */
1016 coap_option_iterator_init(request, &opt_iter, COAP_OPT_ALL);
1017 while ((option = coap_option_next(&opt_iter))) {
1018 switch (opt_iter.number) {
1021 coap_opt_length(option),
1022 &uri) < 0) {
1023 /* Need to return a 5.05 RFC7252 Section 5.7.2 */
1024 coap_log_warn("Proxy URI not decodable\n");
1026 return 0;
1027 }
1028 if (!coap_uri_into_optlist(&uri, NULL, &optlist, 0)) {
1029 coap_log_err("Failed to create options for URI\n");
1030 goto failed;
1031 }
1032 break;
1034 break;
1035 case COAP_OPTION_BLOCK1:
1036 case COAP_OPTION_BLOCK2:
1039 /* These are not passed on */
1040 break;
1043 break;
1044 default:
1045 coap_insert_optlist(&optlist,
1046 coap_new_optlist(opt_iter.number,
1047 coap_opt_length(option),
1048 coap_opt_value(option)));
1049 break;
1050 }
1051 }
1052
1053 /* Update pdu with options */
1054 coap_add_optlist_pdu(pdu, &optlist);
1055 coap_delete_optlist(optlist);
1056 break;
1057 case COAP_PROXY_REVERSE:
1060 default:
1061 /*
1062 * Duplicate request PDU for onward transmission (with new token).
1063 */
1064 pdu = coap_pdu_duplicate_lkd(request, proxy_entry->ongoing, token_len, token, NULL);
1065 if (!pdu) {
1066 coap_log_debug("proxy: PDU generation error\n");
1067 goto failed;
1068 }
1069 if (COAP_PROTO_NOT_RELIABLE(session->proto))
1070 pdu->max_size = coap_session_max_pdu_size_lkd(proxy_entry->ongoing);
1071 break;
1072 }
1073
1074 if (coap_get_data_large(request, &size, &data, &offset, &total)) {
1075 /* COAP_BLOCK_SINGLE_BODY is set, so single body should be given */
1076 assert(size == total);
1077 /*
1078 * Need to take a copy of the data as request PDU may go away before
1079 * all data is transmitted.
1080 */
1081 body_data = coap_new_binary(total);
1082 if (!body_data) {
1083 coap_log_debug("proxy: body build memory error\n");
1084 goto failed;
1085 }
1086 memcpy(body_data->s, data, size);
1087 if (!coap_add_data_large_request_lkd(proxy_entry->ongoing, pdu, total, data,
1088 coap_proxy_release_body_data, body_data)) {
1089 coap_log_debug("proxy: add data error\n");
1090 goto failed;
1091 }
1092 }
1093
1094 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used, "fwd");
1095 if (coap_send_lkd(proxy_entry->ongoing, pdu) == COAP_INVALID_MID) {
1096 pdu = NULL;
1097 coap_log_debug("proxy: upstream PDU send error\n");
1098 goto failed;
1099 }
1100
1101 /*
1102 * Do not update the response code (hence empty ACK) as will be sending
1103 * separate response when response comes back from upstream server
1104 */
1105
1106 return 1;
1107
1108failed:
1109 response->code = COAP_RESPONSE_CODE(500);
1111 return 0;
1112
1113return_cached_info:
1114 if (obs_opt && !proxy_req->doing_observe) {
1115 int observe_action;
1116
1117 observe_action = coap_decode_var_bytes(coap_opt_value(obs_opt),
1118 coap_opt_length(obs_opt));
1119
1120 if (observe_action == COAP_OBSERVE_ESTABLISH) {
1121 proxy_req->doing_observe = 1;
1122 proxy_req->incoming->ref_proxy_subs++;
1123 }
1124 }
1125 coap_log_debug("* %s: Using Proxy Cache (Active %d)\n",
1126 coap_session_str(session),
1127 proxy_cache->ref);
1128 coap_proxy_log_entry(session, request, &proxy_cache->rsp_pdu->actual_token, "rspc");
1129 coap_proxy_call_response_handler(session, request, proxy_cache->rsp_pdu,
1130 &r_token, proxy_req, 1, obs_opt ? 0 : 1);
1131 if (!obs_opt)
1132 coap_proxy_del_req(proxy_entry, proxy_req);
1133 return 1;
1134}
1135
1136coap_proxy_req_t *
1137coap_proxy_map_outgoing_request(coap_session_t *ongoing,
1138 const coap_pdu_t *received,
1139 coap_proxy_list_t **u_proxy_entry) {
1140 coap_proxy_list_t *proxy_list = ongoing->context->proxy_list;
1141 size_t proxy_list_count = ongoing->context->proxy_list_count;
1142 size_t i;
1143 coap_bin_const_t rcv_token = coap_pdu_get_token(received);
1144 coap_proxy_list_t *proxy_entry = NULL;
1145 coap_proxy_req_t *proxy_req;
1146
1147 for (i = 0; i < proxy_list_count; i++) {
1148 proxy_entry = &proxy_list[i];
1149 if (proxy_entry->ongoing == ongoing) {
1150 LL_FOREACH(proxy_entry->proxy_req, proxy_req) {
1151 if (coap_binary_equal(&rcv_token, proxy_req->token_used)) {
1152 coap_ticks(&proxy_entry->last_used);
1153 if (u_proxy_entry)
1154 *u_proxy_entry = proxy_entry;
1155 return proxy_req;
1156 }
1157 }
1158 }
1159 }
1161 char scratch[24];
1162 size_t size;
1163
1164 scratch[0] = '\000';
1165 for (i = 0; i < rcv_token.length; i++) {
1166 size = strlen(scratch);
1167 snprintf(&scratch[size], sizeof(scratch)-size,
1168 "%02x", rcv_token.s[i]);
1169 }
1170 coap_log_debug(" proxy token to match {%s}\n", scratch);
1171 for (i = 0; i < proxy_list_count; i++) {
1172 proxy_entry = &proxy_list[i];
1173 LL_FOREACH(proxy_entry->proxy_req, proxy_req) {
1174 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used, "miss");
1175 }
1176 }
1177 }
1178 return NULL;
1179}
1180
1183 const coap_pdu_t *received,
1184 coap_cache_key_t **cache_key) {
1185 int ret;
1186
1187 coap_lock_lock(return 0);
1188 ret = coap_proxy_forward_response_lkd(session,
1189 received,
1190 cache_key);
1192 return ret;
1193}
1194
1196coap_proxy_forward_response_lkd(coap_session_t *session,
1197 const coap_pdu_t *received,
1198 coap_cache_key_t **cache_key) {
1199 coap_pdu_t *pdu = NULL;
1200 coap_session_t *incoming = NULL;
1201 size_t size;
1202 const uint8_t *data;
1203 coap_optlist_t *optlist = NULL;
1204 coap_opt_t *option;
1205 coap_opt_iterator_t opt_iter;
1206 size_t offset;
1207 size_t total;
1208 coap_proxy_list_t *proxy_entry = NULL;
1209 uint16_t media_type = COAP_MEDIATYPE_TEXT_PLAIN;
1210 int maxage = -1;
1211 uint64_t etag = 0;
1212 coap_pdu_code_t rcv_code = coap_pdu_get_code(received);
1213 coap_bin_const_t req_token;
1214 coap_binary_t *body_data = NULL;
1215 coap_pdu_t *req_pdu;
1216 coap_resource_t *resource;
1217 coap_proxy_req_t *proxy_req = NULL;
1218
1219 proxy_req = coap_proxy_map_outgoing_request(session, received, &proxy_entry);
1220 if (!proxy_req || proxy_req->incoming->server_list) {
1221 coap_log_warn("Unknown proxy ongoing session response received - ignored\n");
1222 return COAP_RESPONSE_OK;
1223 }
1224
1225 req_pdu = proxy_req->pdu;
1226 req_token = coap_pdu_get_token(req_pdu);
1227 resource = proxy_req->resource;
1228 incoming = proxy_req->incoming;
1229
1230 coap_log_debug("** process upstream incoming %d.%02d response:\n",
1231 COAP_RESPONSE_CLASS(rcv_code), rcv_code & 0x1F);
1232
1233 if (coap_get_data_large(received, &size, &data, &offset, &total)) {
1234 /* COAP_BLOCK_SINGLE_BODY is set, so single body should be given */
1235 assert(size == total);
1236 body_data = coap_new_binary(total);
1237 if (!body_data) {
1238 coap_log_debug("body build memory error\n");
1239 goto remove_match;
1240 }
1241 memcpy(body_data->s, data, size);
1242 data = body_data->s;
1243 }
1244
1245 /*
1246 * Build up the ongoing PDU that we are going to send to proxy originator
1247 * as separate response
1248 */
1249 pdu = coap_pdu_init(req_pdu->type, rcv_code,
1250 coap_new_message_id_lkd(incoming),
1252 if (!pdu) {
1253 coap_log_debug("Failed to create ongoing proxy response PDU\n");
1254 goto remove_match;
1255 }
1256
1257 if (!coap_add_token(pdu, req_token.length, req_token.s)) {
1258 coap_log_debug("cannot add token to ongoing proxy response PDU\n");
1259 }
1260
1261 /*
1262 * Copy the options across, skipping those needed for
1263 * coap_add_data_response_large()
1264 */
1265 coap_option_iterator_init(received, &opt_iter, COAP_OPT_ALL);
1266 while ((option = coap_option_next(&opt_iter))) {
1267 switch (opt_iter.number) {
1269 media_type = coap_decode_var_bytes(coap_opt_value(option),
1270 coap_opt_length(option));
1271 break;
1272 case COAP_OPTION_MAXAGE:
1273 maxage = coap_decode_var_bytes(coap_opt_value(option),
1274 coap_opt_length(option));
1275 break;
1276 case COAP_OPTION_ETAG:
1278 coap_opt_length(option));
1279 break;
1280 case COAP_OPTION_BLOCK2:
1282 case COAP_OPTION_SIZE2:
1283 break;
1284 default:
1285 coap_insert_optlist(&optlist,
1286 coap_new_optlist(opt_iter.number,
1287 coap_opt_length(option),
1288 coap_opt_value(option)));
1289 break;
1290 }
1291 }
1292 coap_add_optlist_pdu(pdu, &optlist);
1293 coap_delete_optlist(optlist);
1294
1295 if (size > 0) {
1296 coap_string_t *l_query = coap_get_query(req_pdu);
1297
1298 coap_add_data_large_response_lkd(resource, incoming, req_pdu, pdu,
1299 l_query,
1300 media_type, maxage, etag, size, data,
1301 coap_proxy_release_body_data,
1302 body_data);
1303 body_data = NULL;
1304 coap_delete_string(l_query);
1305 }
1306
1307 if (cache_key)
1308 *cache_key = proxy_req->cache_key;
1309
1310 coap_send_lkd(incoming, pdu);
1311
1312remove_match:
1313 option = coap_check_option(received, COAP_OPTION_OBSERVE, &opt_iter);
1314 /* Need to remove matching token entry (apart from an Observe response) */
1315 if (option == NULL) {
1316 if (proxy_entry->proxy_req) {
1317 /* Do not delete cache key here - caller's responsibility */
1318 proxy_req->cache_key = NULL;
1319 coap_proxy_del_req(proxy_entry, proxy_req);
1320 }
1321 } else if (!proxy_req->doing_observe) {
1322 option = coap_check_option(proxy_req->pdu, COAP_OPTION_OBSERVE, &opt_iter);
1323 if (option &&
1326 proxy_req->doing_observe = 1;
1327 proxy_req->incoming->ref_proxy_subs++;
1328 }
1329 }
1330 coap_delete_binary(body_data);
1331 return COAP_RESPONSE_OK;
1332}
1333
1334void
1335coap_proxy_process_incoming(coap_session_t *session,
1336 coap_pdu_t *rcvd,
1337 void *body_data, coap_proxy_req_t *proxy_req,
1338 coap_proxy_list_t *proxy_entry) {
1339 coap_opt_t *obs_opt;
1340 coap_opt_t *option;
1341 coap_opt_iterator_t opt_iter;
1343 coap_bin_const_t token;
1344
1345 obs_opt = coap_check_option(rcvd, COAP_OPTION_OBSERVE, &opt_iter);
1346
1347 /* See if we are doing proxy caching */
1348 if (obs_opt) {
1349 coap_proxy_cache_t *proxy_cache;
1350 coap_cache_key_t *cache_key_l;
1351 coap_tick_t now;
1352 uint64_t expire;
1353
1354 /* Need to cache the response */
1355 if (proxy_req->proxy_cache) {
1356 coap_delete_pdu_lkd(proxy_req->proxy_cache->rsp_pdu);
1357 proxy_cache = proxy_req->proxy_cache;
1358 } else {
1359 proxy_cache = coap_malloc_type(COAP_STRING, sizeof(coap_proxy_cache_t));
1360 if (proxy_cache == NULL) {
1361 goto cache_fail;
1362 }
1363 memset(proxy_cache, 0, sizeof(coap_proxy_cache_t));
1364 cache_key_l = coap_cache_derive_key_w_ignore(session, proxy_req->pdu,
1366 coap_proxy_ignore_options,
1367 sizeof(coap_proxy_ignore_options)/sizeof(coap_proxy_ignore_options[0]));
1368 if (!cache_key_l) {
1369 coap_free_type(COAP_STRING, proxy_cache);
1370 goto cache_fail;
1371 }
1372 memcpy(&proxy_cache->cache_req, cache_key_l,
1373 sizeof(proxy_cache->cache_req));
1374 coap_delete_cache_key(cache_key_l);
1375
1376 proxy_cache->req_pdu = coap_pdu_reference_lkd(proxy_req->pdu);
1377 proxy_req->proxy_cache = proxy_cache;
1378
1379 PROXY_CACHE_ADD(proxy_entry->rsp_cache, proxy_cache);
1380 proxy_cache->ref++;
1381 }
1382 proxy_cache->rsp_pdu = coap_pdu_reference_lkd(rcvd);
1383 option = coap_check_option(rcvd, COAP_OPTION_ETAG, &opt_iter);
1384 if (option) {
1385 proxy_cache->etag = coap_decode_var_bytes8(coap_opt_value(option),
1386 coap_opt_length(option));
1387 } else {
1388 proxy_cache->etag = 0;
1389 }
1390 coap_ticks(&now);
1391 option = coap_check_option(rcvd, COAP_OPTION_MAXAGE, &opt_iter);
1392 if (option) {
1393 expire = coap_decode_var_bytes(coap_opt_value(option),
1394 coap_opt_length(option));
1395 } else {
1396 /* Default is 60 seconds */
1397 expire = 60;
1398 }
1399 proxy_cache->expire = now + expire * COAP_TICKS_PER_SECOND;
1400
1401 /* Update all the cache listeners */
1402 LL_FOREACH(proxy_entry->proxy_req, proxy_req) {
1403 if (proxy_req->proxy_cache == proxy_cache) {
1404 if (!proxy_req->doing_observe) {
1405 coap_opt_t *req_obs;
1406
1407 req_obs = coap_check_option(proxy_req->pdu, COAP_OPTION_OBSERVE, &opt_iter);
1408 if (req_obs &&
1411 proxy_req->doing_observe = 1;
1412 proxy_req->incoming->ref_proxy_subs++;
1413 }
1414 }
1415 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used, "rsp");
1416 token = coap_pdu_get_token(proxy_req->pdu);
1417 if (coap_proxy_call_response_handler(proxy_req->incoming, proxy_req->pdu,
1418 rcvd, &token,
1419 proxy_req, 0, 0) == COAP_RESPONSE_OK) {
1420 /* At least one success */
1421 ret = COAP_RESPONSE_OK;
1422 }
1423 }
1424 }
1425 goto finish;
1426 }
1427cache_fail:
1428 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used, "rspn");
1429 token = coap_pdu_get_token(proxy_req->pdu);
1430 ret = coap_proxy_call_response_handler(proxy_req->incoming, proxy_req->pdu,
1431 rcvd, &token, proxy_req, 0, 0);
1432 if (!obs_opt)
1433 coap_proxy_del_req(proxy_entry, proxy_req);
1434
1435finish:
1436 if (ret == COAP_RESPONSE_FAIL && rcvd->type != COAP_MESSAGE_ACK) {
1437 coap_send_rst_lkd(session, rcvd);
1439 } else {
1440 coap_send_ack_lkd(session, rcvd);
1442 }
1443 coap_free_type(COAP_STRING, body_data);
1444}
1445
1446/*
1447 */
1449coap_proxy_local_write(coap_session_t *session, coap_pdu_t *pdu) {
1450 coap_pdu_t *response = NULL;
1451 coap_resource_t *resource;
1453
1454 resource = session->context->unknown_resource ?
1455 session->context->unknown_resource :
1456 session->context->proxy_uri_resource;
1457 if (!resource) {
1458 coap_log_err("coap_proxy_local_write: Unknown or Proxy resource not defined\n");
1459 goto fail;
1460 }
1461
1462 response = coap_pdu_init(pdu->type == COAP_MESSAGE_CON ?
1464 0, pdu->mid, coap_session_max_pdu_size_lkd(session));
1465 if (!response) {
1466 coap_log_err("coap_proxy_local_write: Could not create response PDU\n");
1467 goto fail;
1468 }
1469 response->session = session;
1470
1471 if (!coap_add_token(response, pdu->actual_token.length,
1472 pdu->actual_token.s)) {
1473 goto fail;
1474 }
1475
1476 coap_log_debug("* %s: internal: sent %4zd bytes\n",
1477 coap_session_str(session),
1478 pdu->used_size + coap_pdu_encode_header(pdu, session->proto));
1480
1481 mid = pdu->mid;
1482 if (!coap_proxy_forward_request_lkd(session, pdu, response, resource,
1483 NULL, session->server_list)) {
1484 coap_log_debug("coap_proxy_local_write: Failed to forward PDU\n");
1485 mid = COAP_INVALID_MID;
1486 }
1487fail:
1488 coap_delete_pdu_lkd(response);
1490 return mid;
1491}
1492
1495 coap_proxy_server_list_t *server_list) {
1496 coap_session_t *session;
1497
1498 coap_lock_lock(return NULL);
1499 session = coap_new_client_session_proxy_lkd(ctx, server_list);
1501 return session;
1502}
1503
1505coap_new_client_session_proxy_lkd(coap_context_t *ctx,
1506 coap_proxy_server_list_t *server_list) {
1507 coap_session_t *session;
1508 coap_addr_info_t *info_list = NULL;
1509 coap_str_const_t remote;
1510
1512
1513#if COAP_IPV6_SUPPORT
1514 remote.s = (const uint8_t *)"::1";
1515#elif COAP_IPV4_SUPPORT
1516 remote.s = (const uint8_t *)"127.0.0.1";
1517#else /* !COAP_IPV6_SUPPORT && ! COAP_IPV4_SUPPORT */
1518 coap_log_warn("coap_new_client_session_proxy: No IPv4 or IPv6 support\n");
1519 return NULL;
1520#endif /* !COAP_IPV6_SUPPORT && ! COAP_IPV4_SUPPORT */
1521 remote.length = strlen((const char *)remote.s);
1522 /* resolve internal remote address where proxy session is 'connecting' to */
1523 info_list = coap_resolve_address_info(&remote, 0, 0, 0, 0,
1524 0,
1527 if (!info_list) {
1528 coap_log_warn("coap_new_client_session_proxy: Unable to resolve IP address\n");
1529 return NULL;
1530 }
1531
1532 session = coap_new_client_session_lkd(ctx, NULL, &info_list->addr, COAP_PROTO_UDP);
1533
1534 if (session) {
1535 session->server_list = server_list;
1536 }
1537 coap_free_address_info(info_list);
1538 return session;
1539}
1540
1541void
1542coap_delete_proxy_subscriber(coap_session_t *session, coap_bin_const_t *token,
1543 coap_mid_t mid, coap_proxy_subs_delete_t type) {
1544 /* Need to check is there is a proxy subscription active and delete it */
1545 coap_context_t *context = session->context;
1546 size_t i;
1547
1548 for (i = 0; i < context->proxy_list_count; i++) {
1549 coap_proxy_list_t *proxy_entry = &context->proxy_list[i];
1550 coap_proxy_req_t *proxy_req, *treq;
1551
1552 LL_FOREACH_SAFE(proxy_entry->proxy_req, proxy_req, treq) {
1553 if (proxy_req->incoming == session) {
1554 coap_bin_const_t req_token;
1555 int match = 0;
1556
1557 req_token = coap_pdu_get_token(proxy_req->pdu);
1558 switch (type) {
1559 case COAP_PROXY_SUBS_ALL:
1560 match = 1;
1561 break;
1562 case COAP_PROXY_SUBS_TOKEN:
1563 if (token && coap_binary_equal(token, &req_token))
1564 match = 1;
1565 break;
1566 case COAP_PROXY_SUBS_MID:
1567 if (proxy_req->mid == mid)
1568 match = 1;
1569 break;
1570 default:
1571 break;
1572 }
1573
1574 if (match && proxy_entry->proxy_req && ! proxy_entry->proxy_req->next &&
1575 proxy_req->token_used) {
1576 coap_binary_t tmp;
1577
1578 coap_log_debug("coap_delete_proxy_subscriber: Using coap_cancel_observe() to do Proxy OBSERVE cancellation\n");
1579 /* Unfortunately need to change the ptr type to be r/w */
1580 memcpy(&tmp.s, &proxy_req->token_used->s, sizeof(tmp.s));
1581 tmp.length = proxy_req->token_used->length;
1582 coap_cancel_observe_lkd(proxy_entry->ongoing, &tmp, COAP_MESSAGE_CON);
1583 }
1584 coap_proxy_del_req(proxy_entry, proxy_req);
1585 }
1586 }
1587 }
1588}
1589
1590#else /* ! COAP_PROXY_SUPPORT */
1591
1592int
1594 return 0;
1595}
1596
1597COAP_API int
1599 const coap_pdu_t *request,
1600 coap_pdu_t *response,
1601 coap_resource_t *resource,
1602 coap_cache_key_t *cache_key,
1603 coap_proxy_server_list_t *server_list) {
1604 (void)session;
1605 (void)request;
1606 (void)resource;
1607 (void)cache_key;
1608 (void)server_list;
1609 response->code = COAP_RESPONSE_CODE(500);
1610 return 0;
1611}
1612
1615 const coap_pdu_t *received,
1616 coap_cache_key_t **cache_key) {
1617 (void)session;
1618 (void)received;
1619 (void)cache_key;
1620 return COAP_RESPONSE_OK;
1621}
1622
1623int
1625 (void)scheme;
1626 return 0;
1627}
1628#endif /* ! COAP_PROXY_SUPPORT */
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.
coap_addr_info_t * coap_resolve_address_info(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...
@ COAP_RESOLVE_TYPE_REMOTE
remote side of session
#define COAP_UNIX_PATH_MAX
#define INET6_ADDRSTRLEN
Definition coap_debug.c:232
struct coap_proxy_list_t coap_proxy_list_t
Proxy information.
#define PRIu64
Library specific build wrapper for coap_internal.h.
#define COAP_API
@ COAP_STRING
Definition coap_mem.h:39
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...
Definition coap_option.h:26
coap_uri_scheme_t
The scheme specifiers.
Definition coap_uri.h:28
@ COAP_URI_SCHEME_COAPS_WS
Definition coap_uri.h:36
@ COAP_URI_SCHEME_COAPS_TCP
Definition coap_uri.h:32
@ COAP_URI_SCHEME_COAPS
Definition coap_uri.h:30
@ COAP_URI_SCHEME_COAP_TCP
Definition coap_uri.h:31
@ COAP_URI_SCHEME_COAP_WS
Definition coap_uri.h:35
@ COAP_URI_SCHEME_HTTPS
Definition coap_uri.h:34
@ COAP_URI_SCHEME_COAP
Definition coap_uri.h:29
@ COAP_URI_SCHEME_LAST
Definition coap_uri.h:37
@ COAP_URI_SCHEME_HTTP
Definition coap_uri.h:33
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.
Definition coap_net.c:1070
coap_mid_t coap_send_lkd(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
Definition coap_net.c:1470
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.
Definition coap_net.c:1085
int coap_add_data_large_response_lkd(coap_resource_t *resource, coap_session_t *session, const coap_pdu_t *request, coap_pdu_t *response, const coap_string_t *query, uint16_t media_type, int maxage, uint64_t etag, size_t length, const uint8_t *data, coap_release_large_data_t release_func, void *app_ptr)
Associates given data with the response pdu that is passed as fourth parameter.
int coap_add_data_large_request_lkd(coap_session_t *session, coap_pdu_t *pdu, size_t length, const uint8_t *data, coap_release_large_data_t release_func, void *app_ptr)
Associates given data with the pdu that is passed as second parameter.
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
Definition coap_cache.h:38
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition coap_time.h:143
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition coap_time.h:158
#define COAP_MAX_DELAY_TICKS
Definition coap_time.h:221
uint16_t coap_new_message_id_lkd(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
coap_response_t
Definition coap_net.h:48
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
@ COAP_RESPONSE_FAIL
Response not liked - send CoAP RST packet.
Definition coap_net.h:49
@ COAP_RESPONSE_OK
Response is fine.
Definition coap_net.h:50
unsigned int coap_decode_var_bytes(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
Definition coap_encode.c:38
uint64_t coap_decode_var_bytes8(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
Definition coap_encode.c:67
#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(...)
Definition coap_debug.h:120
coap_log_t coap_get_log_level(void)
Get the current logging level.
Definition coap_debug.c:101
void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu)
Display the contents of the specified pdu.
Definition coap_debug.c:784
size_t coap_print_addr(const coap_address_t *addr, unsigned char *buf, size_t len)
Print the address into the defined buffer.
Definition coap_debug.c:239
const char * coap_session_str(const coap_session_t *session)
Get session description.
#define coap_log_info(...)
Definition coap_debug.h:108
#define coap_log_warn(...)
Definition coap_debug.h:102
#define coap_log_err(...)
Definition coap_debug.h:96
@ COAP_LOG_DEBUG
Definition coap_debug.h:58
#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(uint16_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 ...
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_oscore_psk_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)
Creates a new client session to the designated server with PSK credentials as well as protecting the ...
coap_session_t * coap_new_client_session_oscore_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)
Creates a new client session to the designated server, protecting the data using OSCORE.
coap_session_t * coap_new_client_session_oscore_pki_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)
Creates a new client session to the designated server with PKI credentials as well as protecting the ...
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() ...
Definition coap_pdu.c:1651
void coap_delete_pdu_lkd(coap_pdu_t *pdu)
Dispose of an CoAP PDU and free off associated storage.
Definition coap_pdu.c:190
size_t coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto)
Compose the protocol specific header for the specified PDU.
Definition coap_pdu.c:1513
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)
Duplicate an existing PDU.
Definition coap_pdu.c:230
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_...
Definition coap_pdu.c:1659
COAP_STATIC_INLINE void coap_pdu_release_lkd(coap_pdu_t *pdu)
#define COAP_OPTION_URI_HOST
Definition coap_pdu.h:120
coap_pdu_code_t coap_pdu_get_code(const coap_pdu_t *pdu)
Gets the PDU code associated with pdu.
Definition coap_pdu.c:1609
#define COAP_OPTION_BLOCK2
Definition coap_pdu.h:138
#define COAP_OPTION_CONTENT_FORMAT
Definition coap_pdu.h:128
#define COAP_OPTION_SIZE2
Definition coap_pdu.h:140
#define COAP_OPTION_BLOCK1
Definition coap_pdu.h:139
#define COAP_OPTION_Q_BLOCK1
Definition coap_pdu.h:135
#define COAP_OPTION_PROXY_SCHEME
Definition coap_pdu.h:143
#define COAP_DEFAULT_PORT
Definition coap_pdu.h:37
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition coap_pdu.h:264
#define COAP_RESPONSE_CODE(N)
Definition coap_pdu.h:161
#define COAP_RESPONSE_CLASS(C)
Definition coap_pdu.h:164
coap_proto_t
CoAP protocol types.
Definition coap_pdu.h:313
coap_pdu_code_t
Set of codes available for a PDU.
Definition coap_pdu.h:327
#define COAP_OPTION_OSCORE
Definition coap_pdu.h:126
#define COAP_MEDIATYPE_TEXT_PLAIN
Definition coap_pdu.h:214
int coap_add_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds token of length len to pdu.
Definition coap_pdu.c:356
#define COAP_OPTION_Q_BLOCK2
Definition coap_pdu.h:141
#define COAPS_DEFAULT_PORT
Definition coap_pdu.h:38
#define COAP_OPTION_RTAG
Definition coap_pdu.h:147
#define COAP_OPTION_URI_PORT
Definition coap_pdu.h:124
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.
Definition coap_pdu.c:99
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.
Definition coap_pdu.c:883
#define COAP_INVALID_MID
Indicates an invalid message id.
Definition coap_pdu.h:267
#define COAP_OPTION_MAXAGE
Definition coap_pdu.h:131
#define COAP_OPTION_ETAG
Definition coap_pdu.h:121
#define COAP_OPTION_PROXY_URI
Definition coap_pdu.h:142
#define COAP_OPTION_OBSERVE
Definition coap_pdu.h:123
coap_bin_const_t coap_pdu_get_token(const coap_pdu_t *pdu)
Gets the token associated with pdu.
Definition coap_pdu.c:1633
@ COAP_PROTO_UDP
Definition coap_pdu.h:315
@ COAP_MESSAGE_NON
Definition coap_pdu.h:70
@ COAP_MESSAGE_ACK
Definition coap_pdu.h:71
@ COAP_MESSAGE_CON
Definition coap_pdu.h:69
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_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.
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_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_REVERSE_STRIP
Act as a reverse proxy, strip out proxy options.
Definition coap_proxy.h:29
@ COAP_PROXY_FORWARD_DYNAMIC
Act as a forward-dynamic proxy using the request's Proxy-Uri or Proxy-Scheme options to determine ser...
Definition coap_proxy.h:34
@ COAP_PROXY_REVERSE
Act as a reverse proxy.
Definition coap_proxy.h:28
@ COAP_PROXY_FORWARD_STATIC
Act as a forward-static proxy.
Definition coap_proxy.h:31
@ COAP_PROXY_FORWARD_DYNAMIC_STRIP
Act as a forward-dynamic proxy, strip out proxy options.
Definition coap_proxy.h:38
@ COAP_PROXY_FORWARD_STATIC_STRIP
Act as a forward-static proxy, strip out proxy options.
Definition coap_proxy.h:32
coap_session_t * coap_new_client_session_psk2_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)
Creates a new client session to the designated server with PSK credentials.
int coap_session_reconnect(coap_session_t *session)
Close the current session (if not already closed) and reconnect to server (client session only).
coap_session_t * coap_new_client_session_pki_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)
Creates a new client session to the designated server with PKI credentials.
coap_session_t * coap_new_client_session_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto)
Creates a new client session to the designated server.
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.
#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.
Definition coap_str.c:120
coap_binary_t * coap_new_binary(size_t size)
Returns a new binary object with at least size bytes storage allocated.
Definition coap_str.c:77
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...
Definition coap_str.c:110
void coap_delete_binary(coap_binary_t *s)
Deletes the given coap_binary_t object and releases any memory allocated.
Definition coap_str.c:105
#define coap_binary_equal(binary1, binary2)
Compares the two binary data for equality.
Definition coap_str.h:211
#define coap_string_equal(string1, string2)
Compares the two strings for equality.
Definition coap_str.h:197
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
Definition coap_str.c:46
int coap_cancel_observe_lkd(coap_session_t *session, coap_binary_t *token, coap_pdu_type_t message_type)
Cancel an observe that is being tracked by the client large receive logic.
int coap_tcp_is_supported(void)
Check whether TCP is available.
Definition coap_tcp.c:29
int coap_tls_is_supported(void)
Check whether TLS is available.
Definition coap_notls.c:41
int coap_ws_is_supported(void)
Check whether WebSockets is available.
Definition coap_ws.c:933
int coap_dtls_is_supported(void)
Check whether DTLS is available.
Definition coap_notls.c:36
int coap_proxy_is_supported(void)
Check whether Proxy code is available.
int coap_wss_is_supported(void)
Check whether Secure WebSockets is available.
Definition coap_ws.c:938
coap_string_t * coap_get_uri_path(const coap_pdu_t *request)
Extract uri_path string from request PDU.
Definition coap_uri.c:990
int coap_split_proxy_uri(const uint8_t *str_var, size_t len, coap_uri_t *uri)
Parses a given string into URI components.
Definition coap_uri.c:281
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.
Definition coap_uri.c:304
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.
Definition coap_uri.c:939
#define COAP_UNUSED
Definition libcoap.h:70
Resolved addresses information.
coap_proto_t proto
CoAP protocol to use.
coap_address_t addr
The address to connect / bind to.
Multi-purpose address abstraction.
CoAP binary data definition with const data.
Definition coap_str.h:64
size_t length
length of binary data
Definition coap_str.h:65
const uint8_t * s
read-only binary data
Definition coap_str.h:66
CoAP binary data definition.
Definition coap_str.h:56
size_t length
length of binary data
Definition coap_str.h:57
uint8_t * s
binary data
Definition coap_str.h:58
The CoAP stack's global state is stored in a coap_context_t object.
coap_resource_t * proxy_uri_resource
can be used for handling proxy URI resources
coap_resource_t * unknown_resource
can be used for handling unknown resources
char * client_sni
If not NULL, SNI to use in client TLS setup.
Definition coap_dtls.h:437
char * client_sni
If not NULL, SNI to use in client TLS setup.
Definition coap_dtls.h:368
Structure to hold large body (many blocks) client receive information.
uint8_t observe_set
Set if this is an observe receive PDU.
Iterator to run through PDU options.
coap_option_num_t number
decoded option number
Representation of chained list of CoAP options to install.
structure for CoAP PDUs
size_t max_size
maximum size for token, options and payload, or zero for variable size pdu
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 ...
Definition coap_proxy.h:59
coap_proxy_server_t * entry
Set of servers to connect to.
Definition coap_proxy.h:55
coap_proxy_t type
The proxy type.
Definition coap_proxy.h:58
unsigned int idle_timeout_secs
Proxy upstream session idle timeout (0 is no timeout).
Definition coap_proxy.h:62
size_t next_entry
Next server to use (% entry_count)
Definition coap_proxy.h:57
size_t entry_count
The number of servers in entry list.
Definition coap_proxy.h:56
coap_dtls_pki_t * dtls_pki
PKI configuration to use if not NULL.
Definition coap_proxy.h:49
coap_oscore_conf_t * oscore_conf
OSCORE configuration if not NULL.
Definition coap_proxy.h:51
coap_uri_t uri
host and port define the server, scheme method
Definition coap_proxy.h:48
coap_dtls_cpsk_t * dtls_cpsk
PSK configuration to use if not NULL.
Definition coap_proxy.h:50
Abstraction of resource that can be attached to coap_context_t.
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
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.
Definition coap_str.h:46
const uint8_t * s
read-only string data
Definition coap_str.h:48
size_t length
length of string
Definition coap_str.h:47
CoAP string data definition.
Definition coap_str.h:38
uint8_t * s
string data
Definition coap_str.h:40
Representation of parsed URI.
Definition coap_uri.h:68
enum coap_uri_scheme_t scheme
The parsed scheme specifier.
Definition coap_uri.h:80
uint16_t port
The port in host byte order.
Definition coap_uri.h:70
coap_str_const_t host
The host part of the URI.
Definition coap_uri.h:69