libcoap 4.3.5-develop-d80fa14
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-2026 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 (void)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_t
321coap_proxy_map_type(coap_proxy_t proxy_type) {
322 if ((proxy_type & COAP_PROXY_NEW_MASK) == 0) {
323 /* Old version - update to bitwise version */
324 switch ((int)proxy_type) {
326 proxy_type = COAP_PROXY_REV;
327 break;
330 break;
332 proxy_type = COAP_PROXY_FWD_STATIC;
333 break;
336 break;
338 /* Mcast was always let through */
340 break;
342 /* Mcast was always let through */
344 break;
345 default:
346 coap_log_warn("Proxy old proxy_type 0x%u unknow\n", proxy_type);
347 proxy_type = COAP_PROXY_FWD_DYNAMIC;
348 break;
349 }
350 }
351 return proxy_type;
352}
353
354static coap_proxy_list_t *
355coap_proxy_get_session(coap_session_t *session, const coap_pdu_t *request,
356 coap_pdu_t *response,
357 coap_proxy_server_list_t *server_list,
358 coap_proxy_server_t *server_use, int *proxy_entry_created) {
359 size_t i;
360 coap_proxy_list_t *new_proxy_list;
361 coap_proxy_list_t *proxy_list = session->context->proxy_list;
362 size_t proxy_list_count = session->context->proxy_list_count;
363 coap_opt_iterator_t opt_iter;
364 coap_opt_t *proxy_scheme;
365 coap_opt_t *proxy_uri;
366 coap_proxy_t proxy_type;
367
368 *proxy_entry_created = 0;
369
370 /*
371 * Maintain server stickability. server_use not needed as there is
372 * ongoing session in place.
373 */
374 if (session->proxy_entry) {
375 for (i = 0; i < proxy_list_count; i++) {
376 if (&proxy_list[i] == session->proxy_entry) {
377 if (session->proxy_entry->ongoing) {
378 memset(server_use, 0, sizeof(*server_use));
379 return session->proxy_entry;
380 }
381 }
382 }
383 }
384
385 /* Round robin the defined next server list (which usually is just one */
386 server_list->next_entry++;
387 if (server_list->next_entry >= server_list->entry_count)
388 server_list->next_entry = 0;
389
390 if (server_list->entry_count) {
391 memcpy(server_use, &server_list->entry[server_list->next_entry], sizeof(*server_use));
392 } else {
393 memset(server_use, 0, sizeof(*server_use));
394 }
395
396 proxy_type = coap_proxy_map_type(server_list->type);
397
398 if ((proxy_type & COAP_PROXY_NEW_MASK) == COAP_PROXY_FWD_DYNAMIC) {
399 /* Need to get actual server from CoAP Proxy-Uri or Proxy-Scheme options */
400 /*
401 * See if Proxy-Scheme
402 */
403 proxy_scheme = coap_check_option(request, COAP_OPTION_PROXY_SCHEME, &opt_iter);
404 if (proxy_scheme) {
405 if (!coap_get_uri_proxy_scheme_info(request, proxy_scheme, &server_use->uri)) {
406 response->code = COAP_RESPONSE_CODE(505);
407 return NULL;
408 }
409 }
410 /*
411 * See if Proxy-Uri
412 */
413 proxy_uri = coap_check_option(request, COAP_OPTION_PROXY_URI, &opt_iter);
414 if (proxy_uri) {
415 coap_log_info("Proxy URI '%.*s'\n",
416 (int)coap_opt_length(proxy_uri),
417 (const char *)coap_opt_value(proxy_uri));
419 coap_opt_length(proxy_uri),
420 &server_use->uri) < 0) {
421 /* Need to return a 5.05 RFC7252 Section 5.7.2 */
422 coap_log_warn("Proxy URI not decodable\n");
423 response->code = COAP_RESPONSE_CODE(505);
424 return NULL;
425 }
426 }
427
428 if (!(proxy_scheme || proxy_uri)) {
429 response->code = COAP_RESPONSE_CODE(404);
430 return NULL;
431 }
432 }
433
434 if (server_use->uri.host.length == 0) {
435 /* Ongoing connection not well formed */
436 response->code = COAP_RESPONSE_CODE(505);
437 return NULL;
438 }
439
441 response->code = COAP_RESPONSE_CODE(505);
442 return NULL;
443 }
444
445 /* See if we are already connected to the Server */
446 for (i = 0; i < proxy_list_count; i++) {
447 if (coap_string_equal(&proxy_list[i].uri.host, &server_use->uri.host) &&
448 proxy_list[i].uri.port == server_use->uri.port &&
449 proxy_list[i].uri.scheme == server_use->uri.scheme) {
450 if (!server_list->track_client_session && session->context->proxy_response_cb) {
451 coap_ticks(&proxy_list[i].last_used);
452 return &proxy_list[i];
453 } else {
454 if (proxy_list[i].incoming == session) {
455 coap_ticks(&proxy_list[i].last_used);
456 return &proxy_list[i];
457 }
458 }
459 }
460 }
461
462 /* Need to create a new forwarding mapping */
463 new_proxy_list = coap_realloc_type(COAP_STRING, proxy_list, (i+1)*sizeof(proxy_list[0]));
464
465 if (new_proxy_list == NULL) {
466 response->code = COAP_RESPONSE_CODE(500);
467 return NULL;
468 }
469 session->context->proxy_list = proxy_list = new_proxy_list;
470 memset(&proxy_list[i], 0, sizeof(proxy_list[i]));
471
472 /* Keep a copy of the host as server_use->uri pointed to will be going away */
473 proxy_list[i].uri = server_use->uri;
474 proxy_list[i].uri_host_keep = coap_malloc_type(COAP_STRING,
475 server_use->uri.host.length);
476 if (!proxy_list[i].uri_host_keep) {
477 response->code = COAP_RESPONSE_CODE(500);
478 return NULL;
479 }
480 memcpy(proxy_list[i].uri_host_keep, server_use->uri.host.s,
481 server_use->uri.host.length);
482 proxy_list[i].uri.host.s = proxy_list[i].uri_host_keep;
483 /* Unset uri parts which point to going away information */
484 proxy_list[i].uri.path.s = NULL;
485 proxy_list[i].uri.path.length = 0;
486 proxy_list[i].uri.query.s = NULL;
487 proxy_list[i].uri.query.length = 0;
488
489 if (server_list->track_client_session) {
490 proxy_list[i].incoming = session;
491 }
492 *proxy_entry_created = 1;
493 session->context->proxy_list_count++;
494 proxy_list[i].idle_timeout_ticks = server_list->idle_timeout_secs * COAP_TICKS_PER_SECOND;
495 coap_ticks(&proxy_list[i].last_used);
496 session->proxy_entry = &proxy_list[i];
497 return &proxy_list[i];
498}
499
500int
501coap_proxy_remove_association(coap_session_t *session, int send_failure) {
502
503 size_t i;
504 coap_proxy_list_t *proxy_list = session->context->proxy_list;
505 size_t proxy_list_count = session->context->proxy_list_count;
506
507 for (i = 0; i < proxy_list_count; i++) {
508 coap_proxy_list_t *proxy_entry = &proxy_list[i];
509 coap_proxy_req_t *proxy_req;
510
511 /* Check for incoming match */
512retry:
513 LL_FOREACH(proxy_entry->proxy_req, proxy_req) {
514 if (proxy_req->incoming == session) {
515 coap_proxy_del_req(proxy_entry, proxy_req);
516 goto retry;
517 }
518 }
519 if (proxy_entry->incoming == session) {
520 /* Only if there is a one-to-one tracking */
521 coap_session_t *ongoing = proxy_entry->ongoing;
522
523 proxy_entry->ongoing = NULL;
525 return 0;
526 }
527
528 /* Check for outgoing match */
529 if (proxy_entry->ongoing == session) {
530 coap_session_t *ongoing;
531
532 coap_proxy_cleanup_entry(proxy_entry, send_failure);
533 ongoing = proxy_entry->ongoing;
534 coap_log_debug("* %s: proxy_entry %p released (rem count = % " PRIdS ")\n",
535 coap_session_str(ongoing),
536 (void *)proxy_entry,
537 session->context->proxy_list_count - 1);
538 if (proxy_list_count-i > 1) {
539 memmove(&proxy_list[i],
540 &proxy_list[i+1],
541 (proxy_list_count-i-1) * sizeof(proxy_list[0]));
542 }
543 session->context->proxy_list_count--;
545 return 1;
546 }
547 }
548 return 0;
549}
550
551static coap_proxy_list_t *
552coap_proxy_get_ongoing_session(coap_session_t *session,
553 const coap_pdu_t *request,
554 coap_pdu_t *response,
555 coap_proxy_server_list_t *server_list) {
556
557 coap_address_t dst;
558 coap_proto_t proto;
559 coap_addr_info_t *info_list = NULL;
560 coap_proxy_list_t *proxy_entry;
561 coap_context_t *context = session->context;
562 static char client_sni[256];
563 coap_proxy_server_t server_use;
564 int proxy_entry_created;
565
566 proxy_entry = coap_proxy_get_session(session, request, response, server_list,
567 &server_use, &proxy_entry_created);
568 if (!proxy_entry) {
569 /* Error response code already set */
570 return NULL;
571 }
572
573 if (!proxy_entry->ongoing) {
574 /* Need to create a new session */
575 coap_address_t *local_addr = NULL;
576
577 /* resolve destination address where data should be sent */
578 info_list = coap_resolve_address_info(&server_use.uri.host,
579 server_use.uri.port,
580 server_use.uri.port,
581 server_use.uri.port,
582 server_use.uri.port,
583 0,
584 1 << server_use.uri.scheme,
586
587 if (info_list == NULL) {
588 response->code = COAP_RESPONSE_CODE(502);
589 coap_proxy_remove_association(session, 0);
590 return NULL;
591 }
592 proto = info_list->proto;
593 memcpy(&dst, &info_list->addr, sizeof(dst));
594 coap_free_address_info(info_list);
595 if (coap_is_mcast(&dst)) {
596 coap_proxy_t proxy_type = coap_proxy_map_type(server_list->type);
597
598 if ((proxy_type & COAP_PROXY_NEW_MASK) == COAP_PROXY_FWD_DYNAMIC &&
599 (proxy_type & COAP_PROXY_BIT_MCAST) == 0) {
600 response->code = COAP_RESPONSE_CODE(501);
601 coap_proxy_remove_association(session, 0);
602 coap_log_debug("* %s: mcast proxy forwarding not enabled\n",
603 coap_session_str(session));
604 return NULL;
605 }
606 if (server_use.uri.scheme != COAP_URI_SCHEME_COAP) {
607 response->code = COAP_RESPONSE_CODE(501);
608 coap_proxy_remove_association(session, 0);
609 coap_log_debug("* %s: mcast proxy forwarding only supported for 'coap'\n",
610 coap_session_str(session));
611 return NULL;
612 }
613 }
614
615#if COAP_AF_UNIX_SUPPORT
616 coap_address_t bind_addr;
617 if (coap_is_af_unix(&dst)) {
618 char buf[COAP_UNIX_PATH_MAX];
619 coap_tick_t now;
620
621 /* Need a unique 'client' address */
622 coap_ticks(&now);
623 snprintf(buf, COAP_UNIX_PATH_MAX,
624 "/tmp/coap-pr-cl-%" PRIu64, (uint64_t)now);
625 if (!coap_address_set_unix_domain(&bind_addr, (const uint8_t *)buf,
626 strlen(buf))) {
627 fprintf(stderr, "coap_address_set_unix_domain: %s: failed\n",
628 buf);
629 remove(buf);
630 return NULL;
631 }
632 (void)remove(buf);
633 local_addr = &bind_addr;
634 }
635#endif /* COAP_AF_UNIX_SUPPORT */
636
637 snprintf(client_sni, sizeof(client_sni), "%*.*s", (int)server_use.uri.host.length,
638 (int)server_use.uri.host.length, server_use.uri.host.s);
639
640 switch (server_use.uri.scheme) {
644#if COAP_OSCORE_SUPPORT
645 if (server_use.oscore_conf) {
646 proxy_entry->ongoing =
647 coap_new_client_session_oscore3_lkd(context, local_addr, &dst,
648 proto, server_use.oscore_conf,
649 NULL, NULL, NULL);
650 } else {
651#endif /* COAP_OSCORE_SUPPORT */
652 proxy_entry->ongoing =
653 coap_new_client_session3_lkd(context, local_addr, &dst, proto,
654 NULL, NULL, NULL);
655#if COAP_OSCORE_SUPPORT
656 }
657#endif /* COAP_OSCORE_SUPPORT */
658 break;
662#if COAP_OSCORE_SUPPORT
663 if (server_use.oscore_conf) {
664 if (server_use.dtls_pki) {
665 server_use.dtls_pki->client_sni = client_sni;
666 proxy_entry->ongoing =
667 coap_new_client_session_oscore_pki3_lkd(context, local_addr, &dst,
668 proto, server_use.dtls_pki,
669 server_use.oscore_conf,
670 NULL, NULL, NULL);
671 } else if (server_use.dtls_cpsk) {
672 server_use.dtls_cpsk->client_sni = client_sni;
673 proxy_entry->ongoing =
674 coap_new_client_session_oscore_psk3_lkd(context, local_addr, &dst,
675 proto, server_use.dtls_cpsk,
676 server_use.oscore_conf,
677 NULL, NULL, NULL);
678 } else {
679 coap_log_warn("Proxy: (D)TLS not configured for secure session\n");
680 }
681 } else {
682#endif /* COAP_OSCORE_SUPPORT */
683 /* Not doing OSCORE */
684 if (server_use.dtls_pki) {
685 server_use.dtls_pki->client_sni = client_sni;
686 proxy_entry->ongoing =
687 coap_new_client_session_pki3_lkd(context, local_addr, &dst,
688 proto, server_use.dtls_pki,
689 NULL, NULL, NULL);
690 } else if (server_use.dtls_cpsk) {
691 server_use.dtls_cpsk->client_sni = client_sni;
692 proxy_entry->ongoing =
693 coap_new_client_session_psk3_lkd(context, local_addr, &dst,
694 proto, server_use.dtls_cpsk,
695 NULL, NULL, NULL);
696 } else {
697 /* Using client anonymous PKI */
698 proxy_entry->ongoing =
699 coap_new_client_session3_lkd(context, local_addr, &dst, proto,
700 NULL, NULL, NULL);
701 }
702#if COAP_OSCORE_SUPPORT
703 }
704#endif /* COAP_OSCORE_SUPPORT */
705 break;
709 default:
710 assert(0);
711 break;
712 }
713 if (proxy_entry->ongoing == NULL) {
714 response->code = COAP_RESPONSE_CODE(505);
715 coap_proxy_remove_association(session, 0);
716 return NULL;
717 }
718 if (proxy_entry_created) {
719 coap_log_debug("* %s: proxy_entry %p created (tot count = % " PRIdS ")\n",
720 coap_session_str(proxy_entry->ongoing),
721 (void *)proxy_entry,
722 session->context->proxy_list_count);
723 }
724 } else if (proxy_entry->ongoing->session_failed) {
725 if (!coap_session_reconnect(proxy_entry->ongoing)) {
726 /* Server is not yet back up */
727 return NULL;
728 }
729 }
730
731 return proxy_entry;
732}
733
734static void
735coap_proxy_release_body_data(coap_session_t *session COAP_UNUSED,
736 void *app_ptr) {
737 coap_delete_binary(app_ptr);
738}
739
740static coap_proxy_req_t *
741coap_proxy_get_req(coap_proxy_list_t *proxy_entry, coap_proxy_cache_t *proxy_cache,
742 coap_session_t *session) {
743 coap_proxy_req_t *proxy_req;
744
745 LL_FOREACH(proxy_entry->proxy_req, proxy_req) {
746 if (proxy_req->incoming == session && proxy_req->proxy_cache == proxy_cache) {
747 return proxy_req;
748 }
749 }
750 return NULL;
751}
752
753static void
754coap_proxy_free_response_data(coap_session_t *session COAP_UNUSED, void *app_ptr) {
755 coap_delete_bin_const(app_ptr);
756}
757
758static coap_response_t
759coap_proxy_call_response_handler(coap_session_t *session, const coap_pdu_t *sent,
760 coap_pdu_t *rcvd, coap_bin_const_t *token,
761 coap_proxy_req_t *proxy_req, int replace_mid,
762 int remove_observe) {
764 coap_pdu_t *resp_pdu;
765 coap_pdu_t *fwd_pdu = NULL;
766 size_t size;
767 size_t offset;
768 size_t total;
769 const uint8_t *data;
770 coap_string_t *l_query = NULL;
771
772 if (remove_observe) {
773 coap_opt_filter_t drop_options;
774
775 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
777 /* Correct the token */
778 resp_pdu = coap_pdu_duplicate_lkd(rcvd, session, token->length, token->s,
779 &drop_options);
780 } else {
781 /* Correct the token */
782 resp_pdu = coap_pdu_duplicate_lkd(rcvd, session, token->length, token->s,
783 NULL);
784 }
785 if (!resp_pdu)
786 return COAP_RESPONSE_FAIL;
787
788 assert(proxy_req);
789 /* Incase change in type of request over the proxy */
790 if (proxy_req->pdu)
791 resp_pdu->type = proxy_req->pdu->type;
792
793 if (COAP_PROTO_NOT_RELIABLE(session->proto))
794 resp_pdu->max_size = coap_session_max_pdu_size_lkd(session);
795
796 if (replace_mid)
797 resp_pdu->mid = sent->mid;
798
799 proxy_req->mid = resp_pdu->mid;
800
801 /* If sent early ACK, and this is an ACK, need to convert it to CON */
802 if (COAP_PROTO_NOT_RELIABLE(session->proto) && resp_pdu->type == COAP_MESSAGE_ACK &&
804 resp_pdu->type = COAP_MESSAGE_CON;
805 }
806
807 if (coap_get_data_large(rcvd, &size, &data, &offset, &total)) {
808 uint16_t media_type = 0;
809 int maxage = -1;
810 uint64_t etag = 0;
811 coap_opt_t *option;
812 coap_opt_iterator_t opt_iter;
813 coap_bin_const_t *body;
814
815 /* COAP_BLOCK_SINGLE_BODY is set, so single body should be given */
816 assert(size == total);
817
818 body = coap_new_bin_const(data, size);
819 if (!body) {
820 coap_log_debug("coap_proxy_call_response_handler: copy data error\n");
821 goto failed;
822 }
823 option = coap_check_option(rcvd, COAP_OPTION_CONTENT_FORMAT, &opt_iter);
824 if (option) {
825 media_type = coap_decode_var_bytes(coap_opt_value(option),
826 coap_opt_length(option));
827 }
828 option = coap_check_option(rcvd, COAP_OPTION_MAXAGE, &opt_iter);
829 if (option) {
830 maxage = coap_decode_var_bytes(coap_opt_value(option),
831 coap_opt_length(option));
832 }
833 option = coap_check_option(rcvd, COAP_OPTION_ETAG, &opt_iter);
834 if (option) {
836 coap_opt_length(option));
837 }
838 if (sent)
839 l_query = coap_get_query(sent);
840 if (!coap_add_data_large_response_lkd(proxy_req->resource, session, sent,
841 resp_pdu,
842 l_query,
843 media_type, maxage, etag, body->length,
844 body->s,
845 coap_proxy_free_response_data,
846 body)) {
847 coap_log_debug("coap_proxy_call_response_handler: add data error\n");
848 goto failed;
849 }
850 }
852 session->context->proxy_response_cb(session,
853 sent,
854 resp_pdu,
855 proxy_req->cache_key),
856 /* context is being freed off */
857 goto failed);
858 if (fwd_pdu) {
859 ret = COAP_RESPONSE_OK;
860 if (coap_send_lkd(session, fwd_pdu) == COAP_INVALID_MID) {
861 ret = COAP_RESPONSE_FAIL;
862 }
863 if (fwd_pdu != resp_pdu) {
864 /* Application created a new PDU */
865 coap_delete_pdu_lkd(resp_pdu);
866 }
867 } else {
868failed:
869 ret = COAP_RESPONSE_FAIL;
870 coap_delete_pdu_lkd(resp_pdu);
871 }
872 coap_delete_string(l_query);
873 return ret;
874}
875
876int COAP_API
878 const coap_pdu_t *request,
879 coap_pdu_t *response,
880 coap_resource_t *resource,
881 coap_cache_key_t *cache_key,
882 coap_proxy_server_list_t *server_list) {
883 int ret;
884
885 coap_lock_lock(return 0);
886 ret = coap_proxy_forward_request_lkd(session,
887 request,
888 response,
889 resource,
890 cache_key,
891 server_list);
893 return ret;
894}
895
896/* https://rfc-editor.org/rfc/rfc7641#section-3.6 */
897static const uint16_t coap_proxy_ignore_options[] = { COAP_OPTION_ETAG,
902 };
903
904int
905coap_proxy_forward_request_lkd(coap_session_t *session,
906 const coap_pdu_t *request,
907 coap_pdu_t *response,
908 coap_resource_t *resource,
909 coap_cache_key_t *cache_key,
910 coap_proxy_server_list_t *server_list) {
911 coap_proxy_list_t *proxy_entry;
912 size_t size;
913 size_t offset;
914 size_t total;
915 coap_binary_t *body_data = NULL;
916 const uint8_t *data;
917 coap_pdu_t *pdu = NULL;
918 coap_bin_const_t r_token = coap_pdu_get_token(request);
919 uint8_t token[8];
920 size_t token_len;
921 coap_proxy_req_t *proxy_req = NULL;
922 coap_optlist_t *optlist = NULL;
923 coap_opt_t *option;
924 coap_opt_iterator_t opt_iter;
925 coap_uri_t uri;
926 coap_opt_t *obs_opt = coap_check_option(request,
928 &opt_iter);
929 coap_proxy_cache_t *proxy_cache = NULL;
930 coap_proxy_t proxy_type;
931
932 /* Set up ongoing session (if not already done) */
933 proxy_entry = coap_proxy_get_ongoing_session(session, request, response,
934 server_list);
935 if (!proxy_entry) {
936 /* Error response code already set */
937 return 0;
938 }
939 coap_proxy_log_entry(session, request, NULL, "req");
940
941 /* Is this a observe cached request? */
942 if (obs_opt && session->context->proxy_response_cb) {
943 coap_cache_key_t *cache_key_l;
944
945 cache_key_l = coap_cache_derive_key_w_ignore(session, request,
947 coap_proxy_ignore_options,
948 sizeof(coap_proxy_ignore_options)/sizeof(coap_proxy_ignore_options[0]));
949 if (!cache_key_l) {
950 response->code = COAP_RESPONSE_CODE(505);
951 return 0;
952 }
953 PROXY_CACHE_FIND(proxy_entry->rsp_cache, cache_key_l, proxy_cache);
954 coap_delete_cache_key(cache_key_l);
955 }
956
957 if (proxy_cache) {
958 proxy_req = coap_proxy_get_req(proxy_entry, proxy_cache, session);
959 if (proxy_req) {
960 coap_tick_t now;
961
962 if (obs_opt) {
963 int observe_action;
964
965 observe_action = coap_decode_var_bytes(coap_opt_value(obs_opt),
966 coap_opt_length(obs_opt));
967
968 if (observe_action == COAP_OBSERVE_CANCEL) {
969 if (proxy_req->doing_observe) {
970 assert(proxy_req->incoming->ref_proxy_subs);
971 proxy_req->incoming->ref_proxy_subs--;
972 proxy_req->doing_observe = 0;
973 }
974 if (proxy_entry->proxy_req && !proxy_entry->proxy_req->next &&
975 proxy_req->token_used) {
976 coap_binary_t tmp;
977
978 coap_log_debug("coap_proxy_forward_request: Using coap_cancel_observe() to do Proxy OBSERVE cancellation\n");
979 /* Unfortunately need to change the ptr type to be r/w */
980 memcpy(&tmp.s, &proxy_req->token_used->s, sizeof(tmp.s));
981 tmp.length = proxy_req->token_used->length;
982 coap_cancel_observe_lkd(proxy_entry->ongoing, &tmp, COAP_MESSAGE_CON);
983 /* Let the upstream cancellation be the response */
984 return 1;
985 } else {
986 obs_opt = 0;
987 goto return_cached_info;
988 }
989 } else if (observe_action == COAP_OBSERVE_ESTABLISH) {
990 /* Client must be re-registering */
991 coap_ticks(&now);
992 if (proxy_cache && (proxy_cache->expire + COAP_TICKS_PER_SECOND) < now) {
993 /* Need to get an updated rsp_pdu */
994 coap_proxy_del_req(proxy_entry, proxy_req);
995 proxy_req = NULL;
996 proxy_cache = NULL;
997 } else {
998 goto return_cached_info;
999 }
1000 }
1001 } else {
1002 coap_ticks(&now);
1003 if (proxy_cache && (proxy_cache->expire + COAP_TICKS_PER_SECOND) < now) {
1004 /* Need to get an updated rsp_pdu */
1005 coap_proxy_del_req(proxy_entry, proxy_req);
1006 proxy_req = NULL;
1007 proxy_cache = NULL;
1008 } else {
1009 goto return_cached_info;
1010 }
1011 }
1012 }
1013 }
1014
1015 if (!proxy_req) {
1016 proxy_req = coap_malloc_type(COAP_STRING, sizeof(coap_proxy_req_t));
1017 if (proxy_req == NULL) {
1018 goto failed;
1019 }
1020 memset(proxy_req, 0, sizeof(coap_proxy_req_t));
1021 LL_PREPEND(proxy_entry->proxy_req, proxy_req);
1022
1023 proxy_req->pdu = coap_const_pdu_reference_lkd(request);
1024 proxy_req->resource = resource;
1025 proxy_req->incoming = session;
1026 proxy_req->cache_key = cache_key;
1027 proxy_req->proxy_cache = proxy_cache;
1028
1029 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used, "add");
1030 }
1031
1032 if (proxy_cache) {
1033 coap_bin_const_t j_token;
1034
1035 if (obs_opt)
1036 proxy_cache->ref++;
1037 coap_delete_bin_const(proxy_req->token_used);
1038 j_token = coap_pdu_get_token(proxy_cache->rsp_pdu);
1039 proxy_req->token_used = coap_new_bin_const(j_token.s, j_token.length);
1040 if (proxy_req->token_used == NULL) {
1041 goto failed;
1042 }
1043 goto return_cached_info;
1044 }
1045
1046 /* Get a new token for ongoing session */
1047 coap_session_new_token(proxy_entry->ongoing, &token_len, token);
1048 coap_delete_bin_const(proxy_req->token_used);
1049 proxy_req->token_used = coap_new_bin_const(token, token_len);
1050 if (proxy_req->token_used == NULL) {
1051 goto failed;
1052 }
1053 coap_pdu_release_lkd(proxy_req->pdu);
1054 proxy_req->pdu = coap_const_pdu_reference_lkd(request);
1055
1056 /* Need to create the ongoing request pdu entry */
1057 proxy_type = coap_proxy_map_type(server_list->type);
1058 if (proxy_type & COAP_PROXY_BIT_STRIP) {
1059 /*
1060 * Need to replace Proxy-Uri with Uri-Host (and Uri-Port)
1061 * and strip out Proxy-Scheme.
1062 */
1063
1064 /*
1065 * Build up the ongoing PDU that we are going to send
1066 */
1067 pdu = coap_pdu_init(request->type, request->code,
1068 coap_new_message_id_lkd(proxy_entry->ongoing),
1069 coap_session_max_pdu_size_lkd(proxy_entry->ongoing));
1070 if (!pdu) {
1071 goto failed;
1072 }
1073
1074 if (coap_is_mcast(&proxy_entry->ongoing->addr_info.remote)) {
1075 pdu->type = COAP_MESSAGE_NON;
1076 }
1077
1078 if (!coap_add_token(pdu, token_len, token)) {
1079 goto failed;
1080 }
1081
1082 /* Copy the remaining options across */
1083 coap_option_iterator_init(request, &opt_iter, COAP_OPT_ALL);
1084 while ((option = coap_option_next(&opt_iter))) {
1085 switch (opt_iter.number) {
1088 coap_opt_length(option),
1089 &uri) < 0) {
1090 /* Need to return a 5.05 RFC7252 Section 5.7.2 */
1091 coap_log_warn("Proxy URI not decodable\n");
1093 return 0;
1094 }
1095 if (!coap_uri_into_optlist(&uri, NULL, &optlist, 0)) {
1096 coap_log_err("Failed to create options for URI\n");
1097 goto failed;
1098 }
1099 break;
1101 break;
1102 case COAP_OPTION_BLOCK1:
1103 case COAP_OPTION_BLOCK2:
1106 /* These are not passed on */
1107 break;
1110 break;
1111 default:
1112 coap_insert_optlist(&optlist,
1113 coap_new_optlist(opt_iter.number,
1114 coap_opt_length(option),
1115 coap_opt_value(option)));
1116 break;
1117 }
1118 }
1119
1120 /* Update pdu with options */
1121 coap_add_optlist_pdu(pdu, &optlist);
1122 coap_delete_optlist(optlist);
1123 } else {
1124 /*
1125 * Duplicate request PDU for onward transmission (with new token).
1126 */
1127 pdu = coap_pdu_duplicate_lkd(request, proxy_entry->ongoing, token_len, token, NULL);
1128 if (!pdu) {
1129 coap_log_debug("proxy: PDU generation error\n");
1130 goto failed;
1131 }
1132 if (COAP_PROTO_NOT_RELIABLE(session->proto))
1133 pdu->max_size = coap_session_max_pdu_size_lkd(proxy_entry->ongoing);
1134 }
1135
1136 if (coap_get_data_large(request, &size, &data, &offset, &total)) {
1137 /* COAP_BLOCK_SINGLE_BODY is set, so single body should be given */
1138 assert(size == total);
1139 /*
1140 * Need to take a copy of the data as request PDU may go away before
1141 * all data is transmitted.
1142 */
1143 body_data = coap_new_binary(total);
1144 if (!body_data) {
1145 coap_log_debug("proxy: body build memory error\n");
1146 goto failed;
1147 }
1148 memcpy(body_data->s, data, size);
1149 if (!coap_add_data_large_request_lkd(proxy_entry->ongoing, pdu, total, data,
1150 coap_proxy_release_body_data, body_data)) {
1151 coap_log_debug("proxy: add data error\n");
1152 goto failed;
1153 }
1154 }
1155
1156 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used, "fwd");
1157 if (coap_send_lkd(proxy_entry->ongoing, pdu) == COAP_INVALID_MID) {
1158 pdu = NULL;
1159 coap_log_debug("proxy: upstream PDU send error\n");
1160 goto failed;
1161 }
1162
1163 /*
1164 * Do not update the response code (hence empty ACK) as will be sending
1165 * separate response when response comes back from upstream server
1166 */
1167
1168 return 1;
1169
1170failed:
1171 response->code = COAP_RESPONSE_CODE(500);
1173 return 0;
1174
1175return_cached_info:
1176 if (obs_opt && !proxy_req->doing_observe) {
1177 int observe_action;
1178
1179 observe_action = coap_decode_var_bytes(coap_opt_value(obs_opt),
1180 coap_opt_length(obs_opt));
1181
1182 if (observe_action == COAP_OBSERVE_ESTABLISH) {
1183 proxy_req->doing_observe = 1;
1184 proxy_req->incoming->ref_proxy_subs++;
1185 }
1186 }
1187 coap_log_debug("* %s: Using Proxy Cache (Active %d)\n",
1188 coap_session_str(session),
1189 proxy_cache->ref);
1190 coap_proxy_log_entry(session, request, &proxy_cache->rsp_pdu->actual_token, "rspc");
1191 coap_proxy_call_response_handler(session, request, proxy_cache->rsp_pdu,
1192 &r_token, proxy_req, 1, obs_opt ? 0 : 1);
1193 if (!obs_opt)
1194 coap_proxy_del_req(proxy_entry, proxy_req);
1195 return 1;
1196}
1197
1198coap_proxy_req_t *
1199coap_proxy_map_outgoing_request(coap_session_t *ongoing,
1200 const coap_pdu_t *received,
1201 coap_proxy_list_t **u_proxy_entry) {
1202 coap_proxy_list_t *proxy_list = ongoing->context->proxy_list;
1203 size_t proxy_list_count = ongoing->context->proxy_list_count;
1204 size_t i;
1205 coap_bin_const_t rcv_token = coap_pdu_get_token(received);
1206 coap_proxy_list_t *proxy_entry = NULL;
1207 coap_proxy_req_t *proxy_req;
1208
1209 for (i = 0; i < proxy_list_count; i++) {
1210 proxy_entry = &proxy_list[i];
1211 if (proxy_entry->ongoing == ongoing) {
1212 LL_FOREACH(proxy_entry->proxy_req, proxy_req) {
1213 if (coap_binary_equal(&rcv_token, proxy_req->token_used)) {
1214 coap_ticks(&proxy_entry->last_used);
1215 if (u_proxy_entry)
1216 *u_proxy_entry = proxy_entry;
1217 return proxy_req;
1218 }
1219 }
1220 }
1221 }
1223 char scratch[24];
1224 size_t size;
1225
1226 scratch[0] = '\000';
1227 for (i = 0; i < rcv_token.length; i++) {
1228 size = strlen(scratch);
1229 snprintf(&scratch[size], sizeof(scratch)-size,
1230 "%02x", rcv_token.s[i]);
1231 }
1232 coap_log_debug(" proxy token to match {%s}\n", scratch);
1233 for (i = 0; i < proxy_list_count; i++) {
1234 proxy_entry = &proxy_list[i];
1235 LL_FOREACH(proxy_entry->proxy_req, proxy_req) {
1236 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used, "miss");
1237 }
1238 }
1239 }
1240 return NULL;
1241}
1242
1245 const coap_pdu_t *received,
1246 coap_cache_key_t **cache_key) {
1247 int ret;
1248
1249 coap_lock_lock(return 0);
1250 ret = coap_proxy_forward_response_lkd(session,
1251 received,
1252 cache_key);
1254 return ret;
1255}
1256
1258coap_proxy_forward_response_lkd(coap_session_t *session,
1259 const coap_pdu_t *received,
1260 coap_cache_key_t **cache_key) {
1261 coap_pdu_t *pdu = NULL;
1262 coap_session_t *incoming = NULL;
1263 size_t size;
1264 const uint8_t *data;
1265 coap_optlist_t *optlist = NULL;
1266 coap_opt_t *option;
1267 coap_opt_iterator_t opt_iter;
1268 size_t offset;
1269 size_t total;
1270 coap_proxy_list_t *proxy_entry = NULL;
1271 uint16_t media_type = COAP_MEDIATYPE_TEXT_PLAIN;
1272 int maxage = -1;
1273 uint64_t etag = 0;
1274 coap_pdu_code_t rcv_code = coap_pdu_get_code(received);
1275 coap_bin_const_t req_token;
1276 coap_binary_t *body_data = NULL;
1277 coap_pdu_t *req_pdu;
1278 coap_resource_t *resource;
1279 coap_proxy_req_t *proxy_req = NULL;
1280
1281 proxy_req = coap_proxy_map_outgoing_request(session, received, &proxy_entry);
1282 if (!proxy_req || proxy_req->incoming->server_list) {
1283 coap_log_warn("Unknown proxy ongoing session response received - ignored\n");
1284 return COAP_RESPONSE_OK;
1285 }
1286
1287 req_pdu = proxy_req->pdu;
1288 req_token = coap_pdu_get_token(req_pdu);
1289 resource = proxy_req->resource;
1290 incoming = proxy_req->incoming;
1291
1292 coap_log_debug("** process upstream incoming %d.%02d response:\n",
1293 COAP_RESPONSE_CLASS(rcv_code), rcv_code & 0x1F);
1294
1295 if (coap_get_data_large(received, &size, &data, &offset, &total)) {
1296 /* COAP_BLOCK_SINGLE_BODY is set, so single body should be given */
1297 assert(size == total);
1298 body_data = coap_new_binary(total);
1299 if (!body_data) {
1300 coap_log_debug("body build memory error\n");
1301 goto remove_match;
1302 }
1303 memcpy(body_data->s, data, size);
1304 data = body_data->s;
1305 }
1306
1307 /*
1308 * Build up the ongoing PDU that we are going to send to proxy originator
1309 * as separate response
1310 */
1311 pdu = coap_pdu_init(req_pdu->type, rcv_code,
1312 coap_new_message_id_lkd(incoming),
1314 if (!pdu) {
1315 coap_log_debug("Failed to create ongoing proxy response PDU\n");
1316 goto remove_match;
1317 }
1318
1319 if (!coap_add_token(pdu, req_token.length, req_token.s)) {
1320 coap_log_debug("cannot add token to ongoing proxy response PDU\n");
1321 }
1322
1323 /*
1324 * Copy the options across, skipping those needed for
1325 * coap_add_data_response_large()
1326 */
1327 coap_option_iterator_init(received, &opt_iter, COAP_OPT_ALL);
1328 while ((option = coap_option_next(&opt_iter))) {
1329 switch (opt_iter.number) {
1331 media_type = coap_decode_var_bytes(coap_opt_value(option),
1332 coap_opt_length(option));
1333 break;
1334 case COAP_OPTION_MAXAGE:
1335 maxage = coap_decode_var_bytes(coap_opt_value(option),
1336 coap_opt_length(option));
1337 break;
1338 case COAP_OPTION_ETAG:
1340 coap_opt_length(option));
1341 break;
1342 case COAP_OPTION_BLOCK2:
1344 case COAP_OPTION_SIZE2:
1345 break;
1346 default:
1347 coap_insert_optlist(&optlist,
1348 coap_new_optlist(opt_iter.number,
1349 coap_opt_length(option),
1350 coap_opt_value(option)));
1351 break;
1352 }
1353 }
1354 coap_add_optlist_pdu(pdu, &optlist);
1355 coap_delete_optlist(optlist);
1356
1357 if (size > 0) {
1358 coap_string_t *l_query = coap_get_query(req_pdu);
1359
1360 coap_add_data_large_response_lkd(resource, incoming, req_pdu, pdu,
1361 l_query,
1362 media_type, maxage, etag, size, data,
1363 coap_proxy_release_body_data,
1364 body_data);
1365 body_data = NULL;
1366 coap_delete_string(l_query);
1367 }
1368
1369 if (cache_key)
1370 *cache_key = proxy_req->cache_key;
1371
1372 coap_send_lkd(incoming, pdu);
1373
1374remove_match:
1375 option = coap_check_option(received, COAP_OPTION_OBSERVE, &opt_iter);
1376 /* Need to remove matching token entry (apart from an Observe response) */
1377 if (option == NULL) {
1378 if (proxy_entry->proxy_req) {
1379 /* Do not delete cache key here - caller's responsibility */
1380 proxy_req->cache_key = NULL;
1381 coap_proxy_del_req(proxy_entry, proxy_req);
1382 }
1383 } else if (!proxy_req->doing_observe) {
1384 option = coap_check_option(proxy_req->pdu, COAP_OPTION_OBSERVE, &opt_iter);
1385 if (option &&
1388 proxy_req->doing_observe = 1;
1389 proxy_req->incoming->ref_proxy_subs++;
1390 }
1391 }
1392 coap_delete_binary(body_data);
1393 return COAP_RESPONSE_OK;
1394}
1395
1396void
1397coap_proxy_process_incoming(coap_session_t *session,
1398 coap_pdu_t *rcvd,
1399 void *body_data, coap_proxy_req_t *proxy_req,
1400 coap_proxy_list_t *proxy_entry) {
1401 coap_opt_t *obs_opt;
1402 coap_opt_t *option;
1403 coap_opt_iterator_t opt_iter;
1405 coap_bin_const_t token;
1406
1407 obs_opt = coap_check_option(rcvd, COAP_OPTION_OBSERVE, &opt_iter);
1408
1409 /* See if we are doing proxy caching */
1410 if (obs_opt) {
1411 coap_proxy_cache_t *proxy_cache;
1412 coap_cache_key_t *cache_key_l;
1413 coap_tick_t now;
1414 uint64_t expire;
1415
1416 /* Need to cache the response */
1417 if (proxy_req->proxy_cache) {
1418 coap_delete_pdu_lkd(proxy_req->proxy_cache->rsp_pdu);
1419 proxy_cache = proxy_req->proxy_cache;
1420 } else {
1421 proxy_cache = coap_malloc_type(COAP_STRING, sizeof(coap_proxy_cache_t));
1422 if (proxy_cache == NULL) {
1423 goto cache_fail;
1424 }
1425 memset(proxy_cache, 0, sizeof(coap_proxy_cache_t));
1426 cache_key_l = coap_cache_derive_key_w_ignore(session, proxy_req->pdu,
1428 coap_proxy_ignore_options,
1429 sizeof(coap_proxy_ignore_options)/sizeof(coap_proxy_ignore_options[0]));
1430 if (!cache_key_l) {
1431 coap_free_type(COAP_STRING, proxy_cache);
1432 goto cache_fail;
1433 }
1434 memcpy(&proxy_cache->cache_req, cache_key_l,
1435 sizeof(proxy_cache->cache_req));
1436 coap_delete_cache_key(cache_key_l);
1437
1438 proxy_cache->req_pdu = coap_pdu_reference_lkd(proxy_req->pdu);
1439 proxy_req->proxy_cache = proxy_cache;
1440
1441 PROXY_CACHE_ADD(proxy_entry->rsp_cache, proxy_cache);
1442 proxy_cache->ref++;
1443 }
1444 if (rcvd->body_data) {
1445 /* More data than that held in the PDU */
1446 const uint8_t *data;
1447 size_t data_len;
1448
1449 proxy_cache->rsp_pdu = coap_pdu_duplicate_lkd(rcvd,
1450 session,
1451 rcvd->actual_token.length,
1452 rcvd->actual_token.s,
1453 NULL);
1454 if (proxy_cache->rsp_pdu) {
1455 if (coap_get_data(rcvd, &data_len, &data)) {
1456 coap_binary_t *copy;
1457
1458 copy = coap_new_binary(data_len);
1459 if (copy) {
1460 memcpy(copy->s, data, data_len);
1461 proxy_cache->rsp_pdu->data_free = copy;
1462 proxy_cache->rsp_pdu->body_data = copy->s;
1463 proxy_cache->rsp_pdu->body_length = copy->length;
1464 proxy_cache->rsp_pdu->body_total = copy->length;
1465 } else {
1466 proxy_cache->rsp_pdu->body_data = NULL;
1467 proxy_cache->rsp_pdu->body_length = 0;
1468 proxy_cache->rsp_pdu->body_total = 0;
1469 }
1470 }
1471 }
1472 } else {
1473 /* The simple case */
1474 proxy_cache->rsp_pdu = coap_pdu_reference_lkd(rcvd);
1475 }
1476 option = coap_check_option(rcvd, COAP_OPTION_ETAG, &opt_iter);
1477 if (option) {
1478 proxy_cache->etag = coap_decode_var_bytes8(coap_opt_value(option),
1479 coap_opt_length(option));
1480 } else {
1481 proxy_cache->etag = 0;
1482 }
1483 coap_ticks(&now);
1484 option = coap_check_option(rcvd, COAP_OPTION_MAXAGE, &opt_iter);
1485 if (option) {
1486 expire = coap_decode_var_bytes(coap_opt_value(option),
1487 coap_opt_length(option));
1488 } else {
1489 /* Default is 60 seconds */
1490 expire = 60;
1491 }
1492 proxy_cache->expire = now + expire * COAP_TICKS_PER_SECOND;
1493
1494 /* Update all the cache listeners */
1495 LL_FOREACH(proxy_entry->proxy_req, proxy_req) {
1496 if (proxy_req->proxy_cache == proxy_cache) {
1497 if (!proxy_req->doing_observe) {
1498 coap_opt_t *req_obs;
1499
1500 req_obs = coap_check_option(proxy_req->pdu, COAP_OPTION_OBSERVE, &opt_iter);
1501 if (req_obs &&
1504 proxy_req->doing_observe = 1;
1505 proxy_req->incoming->ref_proxy_subs++;
1506 }
1507 }
1508 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used, "rsp");
1509 token = coap_pdu_get_token(proxy_req->pdu);
1510 if (coap_proxy_call_response_handler(proxy_req->incoming, proxy_req->pdu,
1511 rcvd, &token,
1512 proxy_req, 0, 0) == COAP_RESPONSE_OK) {
1513 /* At least one success */
1514 ret = COAP_RESPONSE_OK;
1515 }
1516 }
1517 }
1518 goto finish;
1519 }
1520cache_fail:
1521 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used, "rspn");
1522 token = coap_pdu_get_token(proxy_req->pdu);
1523 ret = coap_proxy_call_response_handler(proxy_req->incoming, proxy_req->pdu,
1524 rcvd, &token, proxy_req, 0, 0);
1525 if (!obs_opt)
1526 coap_proxy_del_req(proxy_entry, proxy_req);
1527
1528finish:
1529 if (ret == COAP_RESPONSE_FAIL && rcvd->type != COAP_MESSAGE_ACK) {
1530 coap_send_rst_lkd(session, rcvd);
1532 } else {
1533 coap_send_ack_lkd(session, rcvd);
1535 }
1536 coap_free_type(COAP_STRING, body_data);
1537}
1538
1539/*
1540 */
1542coap_proxy_local_write(coap_session_t *session, coap_pdu_t *pdu) {
1543 coap_pdu_t *response = NULL;
1544 coap_resource_t *resource;
1546
1547 resource = session->context->unknown_resource ?
1548 session->context->unknown_resource :
1549 session->context->proxy_uri_resource;
1550 if (!resource) {
1551 coap_log_err("coap_proxy_local_write: Unknown or Proxy resource not defined\n");
1552 goto fail;
1553 }
1554
1555 response = coap_pdu_init(pdu->type == COAP_MESSAGE_CON ?
1557 0, pdu->mid, coap_session_max_pdu_size_lkd(session));
1558 if (!response) {
1559 coap_log_err("coap_proxy_local_write: Could not create response PDU\n");
1560 goto fail;
1561 }
1562 response->session = session;
1563
1564 if (!coap_add_token(response, pdu->actual_token.length,
1565 pdu->actual_token.s)) {
1566 goto fail;
1567 }
1568
1569 coap_log_debug("* %s: internal: sent %4" PRIdS " bytes\n",
1570 coap_session_str(session),
1571 pdu->used_size + coap_pdu_encode_header(pdu, session->proto));
1573
1574 mid = pdu->mid;
1575 if (!coap_proxy_forward_request_lkd(session, pdu, response, resource,
1576 NULL, session->server_list)) {
1577 coap_log_debug("coap_proxy_local_write: Failed to forward PDU\n");
1578 mid = COAP_INVALID_MID;
1579 }
1580fail:
1581 coap_delete_pdu_lkd(response);
1583 return mid;
1584}
1585
1588 coap_proxy_server_list_t *server_list) {
1589 coap_session_t *session;
1590
1591 coap_lock_lock(return NULL);
1592 session = coap_new_client_session_proxy_lkd(ctx, server_list);
1594 return session;
1595}
1596
1598coap_new_client_session_proxy_lkd(coap_context_t *ctx,
1599 coap_proxy_server_list_t *server_list) {
1600 coap_session_t *session;
1601 coap_addr_info_t *info_list = NULL;
1602 coap_str_const_t remote;
1603
1605
1606#if COAP_IPV6_SUPPORT
1607 remote.s = (const uint8_t *)"::1";
1608#elif COAP_IPV4_SUPPORT
1609 remote.s = (const uint8_t *)"127.0.0.1";
1610#else /* !COAP_IPV6_SUPPORT && ! COAP_IPV4_SUPPORT */
1611 coap_log_warn("coap_new_client_session_proxy: No IPv4 or IPv6 support\n");
1612 return NULL;
1613#endif /* !COAP_IPV6_SUPPORT && ! COAP_IPV4_SUPPORT */
1614 remote.length = strlen((const char *)remote.s);
1615 /* resolve internal remote address where proxy session is 'connecting' to */
1616 info_list = coap_resolve_address_info(&remote, 0, 0, 0, 0,
1617 0,
1620 if (!info_list) {
1621 coap_log_warn("coap_new_client_session_proxy: Unable to resolve IP address\n");
1622 return NULL;
1623 }
1624
1625 session = coap_new_client_session3_lkd(ctx, NULL, &info_list->addr, COAP_PROTO_UDP,
1626 NULL, NULL, NULL);
1627
1628 if (session) {
1629 session->server_list = server_list;
1630 }
1631 coap_free_address_info(info_list);
1632 return session;
1633}
1634
1635void
1636coap_delete_proxy_subscriber(coap_session_t *session, coap_bin_const_t *token,
1637 coap_mid_t mid, coap_proxy_subs_delete_t type) {
1638 /* Need to check is there is a proxy subscription active and delete it */
1639 coap_context_t *context = session->context;
1640 size_t i;
1641
1642 for (i = 0; i < context->proxy_list_count; i++) {
1643 coap_proxy_list_t *proxy_entry = &context->proxy_list[i];
1644 coap_proxy_req_t *proxy_req, *treq;
1645
1646 LL_FOREACH_SAFE(proxy_entry->proxy_req, proxy_req, treq) {
1647 if (proxy_req->incoming == session) {
1648 coap_bin_const_t req_token;
1649 int match = 0;
1650
1651 req_token = coap_pdu_get_token(proxy_req->pdu);
1652 switch (type) {
1653 case COAP_PROXY_SUBS_ALL:
1654 match = 1;
1655 break;
1656 case COAP_PROXY_SUBS_TOKEN:
1657 if (token && coap_binary_equal(token, &req_token))
1658 match = 1;
1659 break;
1660 case COAP_PROXY_SUBS_MID:
1661 if (proxy_req->mid == mid)
1662 match = 1;
1663 break;
1664 default:
1665 break;
1666 }
1667
1668 if (match && proxy_entry->proxy_req && ! proxy_entry->proxy_req->next &&
1669 proxy_req->token_used) {
1670 coap_binary_t tmp;
1671
1672 coap_log_debug("coap_delete_proxy_subscriber: Using coap_cancel_observe() to do Proxy OBSERVE cancellation\n");
1673 /* Unfortunately need to change the ptr type to be r/w */
1674 memcpy(&tmp.s, &proxy_req->token_used->s, sizeof(tmp.s));
1675 tmp.length = proxy_req->token_used->length;
1676 coap_cancel_observe_lkd(proxy_entry->ongoing, &tmp, COAP_MESSAGE_CON);
1677 }
1678 coap_proxy_del_req(proxy_entry, proxy_req);
1679 }
1680 }
1681 }
1682}
1683
1684#else /* ! COAP_PROXY_SUPPORT */
1685
1686int
1688 return 0;
1689}
1690
1691COAP_API int
1693 const coap_pdu_t *request,
1694 coap_pdu_t *response,
1695 coap_resource_t *resource,
1696 coap_cache_key_t *cache_key,
1697 coap_proxy_server_list_t *server_list) {
1698 (void)session;
1699 (void)request;
1700 (void)resource;
1701 (void)cache_key;
1702 (void)server_list;
1703 response->code = COAP_RESPONSE_CODE(500);
1704 return 0;
1705}
1706
1709 const coap_pdu_t *received,
1710 coap_cache_key_t **cache_key) {
1711 (void)session;
1712 (void)received;
1713 (void)cache_key;
1714 return COAP_RESPONSE_OK;
1715}
1716
1717int
1719 (void)scheme;
1720 return 0;
1721}
1722#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.
int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast 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:234
struct coap_proxy_list_t coap_proxy_list_t
Proxy information.
#define PRIdS
#define PRIu64
Library specific build wrapper for coap_internal.h.
#define COAP_API
@ COAP_STRING
Definition coap_mem.h:34
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:30
coap_uri_scheme_t
The scheme specifiers.
Definition coap_uri.h:32
@ COAP_URI_SCHEME_COAPS_WS
Definition coap_uri.h:40
@ COAP_URI_SCHEME_COAPS_TCP
Definition coap_uri.h:36
@ COAP_URI_SCHEME_COAPS
Definition coap_uri.h:34
@ COAP_URI_SCHEME_COAP_TCP
Definition coap_uri.h:35
@ COAP_URI_SCHEME_COAP_WS
Definition coap_uri.h:39
@ COAP_URI_SCHEME_HTTPS
Definition coap_uri.h:38
@ COAP_URI_SCHEME_COAP
Definition coap_uri.h:33
@ COAP_URI_SCHEME_LAST
Definition coap_uri.h:41
@ COAP_URI_SCHEME_HTTP
Definition coap_uri.h:37
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:1080
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:1484
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:1095
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.
#define COAP_BLOCK_CACHE_RESPONSE
Definition coap_block.h:73
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:42
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition coap_time.h:151
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition coap_time.h:166
#define COAP_MAX_DELAY_TICKS
Definition coap_time.h:233
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:54
void coap_ticks(coap_tick_t *t)
Returns the current value of an internal tick counter.
Definition coap_time.c:90
@ COAP_RESPONSE_FAIL
Response not liked - send CoAP RST packet.
Definition coap_net.h:55
@ COAP_RESPONSE_OK
Response is fine.
Definition coap_net.h:56
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:70
#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:126
coap_log_t coap_get_log_level(void)
Get the current logging level.
Definition coap_debug.c:103
void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu)
Display the contents of the specified pdu.
Definition coap_debug.c:793
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:241
const char * coap_session_str(const coap_session_t *session)
Get session description.
#define coap_log_info(...)
Definition coap_debug.h:114
#define coap_log_warn(...)
Definition coap_debug.h:108
#define coap_log_err(...)
Definition coap_debug.h:102
@ COAP_LOG_DEBUG
Definition coap_debug.h:64
#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_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() ...
Definition coap_pdu.c:1661
void coap_delete_pdu_lkd(coap_pdu_t *pdu)
Dispose of an CoAP PDU and free off associated storage.
Definition coap_pdu.c:194
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:1523
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:234
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:1669
COAP_STATIC_INLINE void coap_pdu_release_lkd(coap_pdu_t *pdu)
#define COAP_OPTION_URI_HOST
Definition coap_pdu.h:124
coap_pdu_code_t coap_pdu_get_code(const coap_pdu_t *pdu)
Gets the PDU code associated with pdu.
Definition coap_pdu.c:1619
#define COAP_OPTION_BLOCK2
Definition coap_pdu.h:142
#define COAP_OPTION_CONTENT_FORMAT
Definition coap_pdu.h:132
#define COAP_OPTION_SIZE2
Definition coap_pdu.h:144
#define COAP_OPTION_BLOCK1
Definition coap_pdu.h:143
#define COAP_OPTION_Q_BLOCK1
Definition coap_pdu.h:139
#define COAP_OPTION_PROXY_SCHEME
Definition coap_pdu.h:147
#define COAP_DEFAULT_PORT
Definition coap_pdu.h:41
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition coap_pdu.h:268
#define COAP_RESPONSE_CODE(N)
Definition coap_pdu.h:165
#define COAP_RESPONSE_CLASS(C)
Definition coap_pdu.h:168
coap_proto_t
CoAP protocol types Note: coap_layers_coap[] needs updating if extended.
Definition coap_pdu.h:318
coap_pdu_code_t
Set of codes available for a PDU.
Definition coap_pdu.h:332
#define COAP_OPTION_OSCORE
Definition coap_pdu.h:130
#define COAP_MEDIATYPE_TEXT_PLAIN
Definition coap_pdu.h:218
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:366
#define COAP_OPTION_Q_BLOCK2
Definition coap_pdu.h:145
#define COAPS_DEFAULT_PORT
Definition coap_pdu.h:42
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.
Definition coap_pdu.c:885
#define COAP_OPTION_RTAG
Definition coap_pdu.h:151
#define COAP_OPTION_URI_PORT
Definition coap_pdu.h:128
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:102
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:893
#define COAP_INVALID_MID
Indicates an invalid message id.
Definition coap_pdu.h:271
#define COAP_OPTION_MAXAGE
Definition coap_pdu.h:135
#define COAP_OPTION_ETAG
Definition coap_pdu.h:125
#define COAP_OPTION_PROXY_URI
Definition coap_pdu.h:146
#define COAP_OPTION_OBSERVE
Definition coap_pdu.h:127
coap_bin_const_t coap_pdu_get_token(const coap_pdu_t *pdu)
Gets the token associated with pdu.
Definition coap_pdu.c:1643
@ COAP_PROTO_UDP
Definition coap_pdu.h:320
@ COAP_MESSAGE_NON
Definition coap_pdu.h:74
@ COAP_MESSAGE_ACK
Definition coap_pdu.h:75
@ COAP_MESSAGE_CON
Definition coap_pdu.h:73
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.
Definition coap_proxy.h:48
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.
Definition coap_proxy.h:85
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_DIRECT_STRIP
Old - do not use - Act as a forward-dynamic proxy, strip out proxy options.
Definition coap_proxy.h:57
@ COAP_PROXY_BIT_STRIP
If COAP_PROXY_BIT_STRIP set, then remove any Proxy-Uri or Proxy-Scheme, else leave them.
Definition coap_proxy.h:71
@ COAP_PROXY_REVERSE_STRIP
Old - do not use - Act as a reverse proxy, strip out proxy options.
Definition coap_proxy.h:51
@ COAP_PROXY_FWD_STATIC
forward-static proxy.
Definition coap_proxy.h:67
@ COAP_PROXY_REV
reverse proxy.
Definition coap_proxy.h:66
@ COAP_PROXY_REVERSE
Old - do not use - Act as a reverse proxy.
Definition coap_proxy.h:50
@ COAP_PROXY_DIRECT
Old - do not use - Act as a forward-dynamic proxy.
Definition coap_proxy.h:56
@ COAP_PROXY_FORWARD_STRIP
Old - do not use - Act as a forward-static proxy, strip out proxy options.
Definition coap_proxy.h:54
@ COAP_PROXY_FWD_DYNAMIC
forward-dynamic proxy.
Definition coap_proxy.h:68
@ COAP_PROXY_FORWARD
Old - do not use - Act as a forward-static proxy.
Definition coap_proxy.h:53
@ COAP_PROXY_BIT_MCAST
If COAP_PROXY_BIT_MCAST set, then upstream servers can be multicast, else only unicast.
Definition coap_proxy.h:76
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...
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...
#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:214
#define coap_string_equal(string1, string2)
Compares the two strings for equality.
Definition coap_str.h:200
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.
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:934
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:939
coap_string_t * coap_get_uri_path(const coap_pdu_t *request)
Extract uri_path string from request PDU.
Definition coap_uri.c:1020
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:299
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:322
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:969
#define COAP_UNUSED
Definition libcoap.h:74
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:67
size_t length
length of binary data
Definition coap_str.h:68
const uint8_t * s
read-only binary data
Definition coap_str.h:69
CoAP binary data definition.
Definition coap_str.h:59
size_t length
length of binary data
Definition coap_str.h:60
uint8_t * s
binary data
Definition coap_str.h:61
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:441
char * client_sni
If not NULL, SNI to use in client TLS setup.
Definition coap_dtls.h:372
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
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 ...
Definition coap_proxy.h:100
coap_proxy_server_t * entry
Set of servers to connect to.
Definition coap_proxy.h:95
coap_proxy_t type
The proxy type and option controlling bits (if old type used, will get mapped into new + bits.
Definition coap_proxy.h:98
unsigned int idle_timeout_secs
Proxy upstream session idle timeout (0 is no timeout).
Definition coap_proxy.h:103
size_t next_entry
Next server to use (% entry_count)
Definition coap_proxy.h:97
size_t entry_count
The number of servers in entry list.
Definition coap_proxy.h:96
coap_dtls_pki_t * dtls_pki
PKI configuration to use if not NULL.
Definition coap_proxy.h:89
coap_oscore_conf_t * oscore_conf
OSCORE configuration if not NULL.
Definition coap_proxy.h:91
coap_uri_t uri
host and port define the server, scheme method
Definition coap_proxy.h:88
coap_dtls_cpsk_t * dtls_cpsk
PSK configuration to use if not NULL.
Definition coap_proxy.h:90
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...
uint32_t block_mode
Zero or more COAP_BLOCK_ or'd options.
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:49
const uint8_t * s
read-only string data
Definition coap_str.h:51
size_t length
length of string
Definition coap_str.h:50
CoAP string data definition.
Definition coap_str.h:41
uint8_t * s
string data
Definition coap_str.h:43
Representation of parsed URI.
Definition coap_uri.h:72
enum coap_uri_scheme_t scheme
The parsed scheme specifier.
Definition coap_uri.h:84
uint16_t port
The port in host byte order.
Definition coap_uri.h:74
coap_str_const_t host
The host part of the URI.
Definition coap_uri.h:73