libcoap 4.3.5-develop-ce032db
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
15
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
41void
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_entry_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_entry_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_entry_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_entry_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_entry_t *
355coap_proxy_get_add_list_entry(coap_session_t *incoming, coap_session_t *ongoing,
356 coap_uri_t uri, coap_proxy_server_list_t *server_list,
357 int allow_add) {
358 coap_context_t *context = incoming ? incoming->context : ongoing->context;
359 coap_proxy_entry_t *proxy_list = context->proxy_list;
360 size_t proxy_list_count = context->proxy_list_count;
361 coap_proxy_entry_t *new_proxy_list;
362 size_t i;
363
364 /* See if we are already connected to the Server */
365 for (i = 0; i < proxy_list_count; i++) {
366 if (coap_string_equal(&proxy_list[i].uri.host, &uri.host) &&
367 proxy_list[i].uri.port == uri.port &&
368 proxy_list[i].uri.scheme == uri.scheme) {
369 if (!proxy_list[i].track_client_session && context->proxy_response_cb) {
370 coap_ticks(&proxy_list[i].last_used);
371 return &proxy_list[i];
372 } else {
373 if (proxy_list[i].incoming == incoming) {
374 coap_ticks(&proxy_list[i].last_used);
375 return &proxy_list[i];
376 }
377 }
378 }
379 }
380 if ((server_list->type & COAP_PROXY_DYN_DEFINED) && !allow_add)
381 return NULL;
382
383 /* Need to create a new forwarding mapping */
384 new_proxy_list = coap_realloc_type(COAP_STRING, proxy_list,
385 (proxy_list_count+1)*sizeof(proxy_list[0]));
386
387 if (new_proxy_list == NULL) {
388 return NULL;
389 }
390 context->proxy_list = proxy_list = new_proxy_list;
391 memset(&proxy_list[proxy_list_count], 0, sizeof(proxy_list[proxy_list_count]));
392
393 /* Keep a copy of the host as server_use->uri pointed to will be going away */
394 proxy_list[proxy_list_count].uri = uri;
395 proxy_list[proxy_list_count].uri_host_keep = coap_malloc_type(COAP_STRING,
396 uri.host.length);
397 if (!proxy_list[proxy_list_count].uri_host_keep) {
398 return NULL;
399 }
400 memcpy(proxy_list[proxy_list_count].uri_host_keep, uri.host.s, uri.host.length);
401 proxy_list[proxy_list_count].uri.host.s = proxy_list[proxy_list_count].uri_host_keep;
402 /* Unset uri parts which point to going away information */
403 proxy_list[proxy_list_count].uri.path.s = NULL;
404 proxy_list[proxy_list_count].uri.path.length = 0;
405 proxy_list[proxy_list_count].uri.query.s = NULL;
406 proxy_list[proxy_list_count].uri.query.length = 0;
407
408 proxy_list[proxy_list_count].ongoing = ongoing;
409 proxy_list[proxy_list_count].idle_timeout_ticks = server_list->idle_timeout_secs *
411 proxy_list[proxy_list_count].track_client_session = server_list->track_client_session;
412 coap_ticks(&proxy_list[proxy_list_count].last_used);
413 if (incoming) {
414 incoming->proxy_entry = &proxy_list[proxy_list_count];
415 if (server_list->track_client_session)
416 proxy_list[proxy_list_count].incoming = incoming;
417 incoming->proxy_entry = &proxy_list[proxy_list_count];
418 }
419
420 context->proxy_list_count++;
421 return &proxy_list[proxy_list_count];
422}
423
424static coap_proxy_entry_t *
425coap_proxy_get_session(coap_session_t *session, const coap_pdu_t *request,
426 coap_pdu_t *response,
427 coap_proxy_server_list_t *server_list,
428 coap_proxy_server_t *server_use, int *proxy_entry_created) {
429 size_t i;
430 coap_proxy_entry_t *proxy_entry;
431 coap_proxy_entry_t *proxy_list = session->context->proxy_list;
432 size_t proxy_list_count = session->context->proxy_list_count;
433 coap_opt_iterator_t opt_iter;
434 coap_opt_t *proxy_scheme;
435 coap_opt_t *proxy_uri;
436 coap_proxy_t proxy_type;
437
438 *proxy_entry_created = 0;
439
440 /*
441 * Maintain server stickability. server_use not needed as there is
442 * ongoing session in place.
443 */
444 if (session->proxy_entry) {
445 for (i = 0; i < proxy_list_count; i++) {
446 if (&proxy_list[i] == session->proxy_entry) {
447 if (session->proxy_entry->ongoing) {
448 memset(server_use, 0, sizeof(*server_use));
449 return session->proxy_entry;
450 }
451 }
452 }
453 }
454
455 /* Round robin the defined next server list (which usually is just one */
456 server_list->next_entry++;
457 if (server_list->next_entry >= server_list->entry_count)
458 server_list->next_entry = 0;
459
460 if (server_list->entry_count) {
461 memcpy(server_use, &server_list->entry[server_list->next_entry], sizeof(*server_use));
462 } else {
463 memset(server_use, 0, sizeof(*server_use));
464 }
465
466 proxy_type = coap_proxy_map_type(server_list->type);
467
468 if ((proxy_type & COAP_PROXY_NEW_MASK) == COAP_PROXY_FWD_DYNAMIC) {
469 /* Need to get actual server from CoAP Proxy-Uri or Proxy-Scheme options */
470 /*
471 * See if Proxy-Scheme
472 */
473 proxy_scheme = coap_check_option(request, COAP_OPTION_PROXY_SCHEME, &opt_iter);
474 if (proxy_scheme) {
475 if (!coap_get_uri_proxy_scheme_info(request, proxy_scheme, &server_use->uri)) {
476 response->code = COAP_RESPONSE_CODE(505);
477 return NULL;
478 }
479 }
480 /*
481 * See if Proxy-Uri
482 */
483 proxy_uri = coap_check_option(request, COAP_OPTION_PROXY_URI, &opt_iter);
484 if (proxy_uri) {
485 coap_log_info("Proxy URI '%.*s'\n",
486 (int)coap_opt_length(proxy_uri),
487 (const char *)coap_opt_value(proxy_uri));
489 coap_opt_length(proxy_uri),
490 &server_use->uri) < 0) {
491 /* Need to return a 5.05 RFC7252 Section 5.7.2 */
492 coap_log_warn("Proxy URI not decodable\n");
493 response->code = COAP_RESPONSE_CODE(505);
494 return NULL;
495 }
496 }
497
498 if (!(proxy_scheme || proxy_uri)) {
499 response->code = COAP_RESPONSE_CODE(404);
500 return NULL;
501 }
502 }
503
504 if (server_use->uri.host.length == 0) {
505 /* Ongoing connection not well formed */
506 response->code = COAP_RESPONSE_CODE(505);
507 return NULL;
508 }
509
511 response->code = COAP_RESPONSE_CODE(505);
512 return NULL;
513 }
514
515 /* Check if need to create a new forwarding mapping */
516 proxy_entry = coap_proxy_get_add_list_entry(session, NULL, server_use->uri, server_list, 0);
517
518 if (proxy_entry == NULL) {
519 response->code = COAP_RESPONSE_CODE(500);
520 return NULL;
521 }
522
523 *proxy_entry_created = 1;
524 return proxy_entry;
525}
526
527int
528coap_proxy_remove_association(coap_session_t *session, int send_failure) {
529
530 size_t i;
531 coap_proxy_entry_t *proxy_list = session->context->proxy_list;
532 size_t proxy_list_count = session->context->proxy_list_count;
533
534 for (i = 0; i < proxy_list_count; i++) {
535 coap_proxy_entry_t *proxy_entry = &proxy_list[i];
536 coap_proxy_req_t *proxy_req;
537
538 /* Check for incoming match */
539retry:
540 LL_FOREACH(proxy_entry->proxy_req, proxy_req) {
541 if (proxy_req->incoming == session) {
542 coap_proxy_del_req(proxy_entry, proxy_req);
543 goto retry;
544 }
545 }
546 if (proxy_entry->incoming == session) {
547 /* Only if there is a one-to-one tracking */
548 coap_session_t *ongoing = proxy_entry->ongoing;
549
550 proxy_entry->ongoing = NULL;
552 return 0;
553 }
554
555 /* Check for outgoing match */
556 if (proxy_entry->ongoing == session) {
557 coap_session_t *ongoing;
558
559 coap_proxy_cleanup_entry(proxy_entry, send_failure);
560 ongoing = proxy_entry->ongoing;
561 coap_log_debug("* %s: proxy_entry %p released (rem count = % " PRIdS ")\n",
562 coap_session_str(ongoing),
563 (void *)proxy_entry,
564 session->context->proxy_list_count - 1);
565 if (proxy_list_count-i > 1) {
566 memmove(&proxy_list[i],
567 &proxy_list[i+1],
568 (proxy_list_count-i-1) * sizeof(proxy_list[0]));
569 }
570 session->context->proxy_list_count--;
572 return 1;
573 }
574 }
575 return 0;
576}
577
578static coap_proxy_entry_t *
579coap_proxy_get_ongoing_session(coap_session_t *session,
580 const coap_pdu_t *request,
581 coap_pdu_t *response,
582 coap_proxy_server_list_t *server_list) {
583
584 coap_address_t dst;
585 coap_proto_t proto;
586 coap_addr_info_t *info_list = NULL;
587 coap_proxy_entry_t *proxy_entry;
588 coap_context_t *context = session->context;
589 static char client_sni[256];
590 coap_proxy_server_t server_use;
591 int proxy_entry_created;
592
593 proxy_entry = coap_proxy_get_session(session, request, response, server_list,
594 &server_use, &proxy_entry_created);
595 if (!proxy_entry) {
596 /* Error response code already set */
597 return NULL;
598 }
599
600 if (!proxy_entry->ongoing) {
601 /* Need to create a new session */
602 coap_address_t *local_addr = NULL;
603
604 /* resolve destination address where data should be sent */
605 info_list = coap_resolve_address_info_lkd(&server_use.uri.host,
606 server_use.uri.port,
607 server_use.uri.port,
608 server_use.uri.port,
609 server_use.uri.port,
610 0,
611 1 << server_use.uri.scheme,
613
614 if (info_list == NULL) {
615 response->code = COAP_RESPONSE_CODE(502);
616 coap_proxy_remove_association(session, 0);
617 return NULL;
618 }
619 proto = info_list->proto;
620 memcpy(&dst, &info_list->addr, sizeof(dst));
621 coap_free_address_info(info_list);
622 if (coap_is_mcast(&dst)) {
623 coap_proxy_t proxy_type = coap_proxy_map_type(server_list->type);
624
625 if ((proxy_type & COAP_PROXY_NEW_MASK) == COAP_PROXY_FWD_DYNAMIC &&
626 (proxy_type & COAP_PROXY_BIT_MCAST) == 0) {
627 response->code = COAP_RESPONSE_CODE(501);
628 coap_proxy_remove_association(session, 0);
629 coap_log_debug("* %s: mcast proxy forwarding not enabled\n",
630 coap_session_str(session));
631 return NULL;
632 }
633 if (server_use.uri.scheme != COAP_URI_SCHEME_COAP) {
634 response->code = COAP_RESPONSE_CODE(501);
635 coap_proxy_remove_association(session, 0);
636 coap_log_debug("* %s: mcast proxy forwarding only supported for 'coap'\n",
637 coap_session_str(session));
638 return NULL;
639 }
640 }
641
642#if COAP_AF_UNIX_SUPPORT
643 coap_address_t bind_addr;
644 if (coap_is_af_unix(&dst)) {
645 char buf[COAP_UNIX_PATH_MAX];
646 coap_tick_t now;
647
648 /* Need a unique 'client' address */
649 coap_ticks(&now);
650 snprintf(buf, COAP_UNIX_PATH_MAX,
651 "/tmp/coap-pr-cl-%" PRIu64, (uint64_t)now);
652 if (!coap_address_set_unix_domain(&bind_addr, (const uint8_t *)buf,
653 strlen(buf))) {
654 fprintf(stderr, "coap_address_set_unix_domain: %s: failed\n",
655 buf);
656 remove(buf);
657 return NULL;
658 }
659 (void)remove(buf);
660 local_addr = &bind_addr;
661 }
662#endif /* COAP_AF_UNIX_SUPPORT */
663
664 snprintf(client_sni, sizeof(client_sni), "%*.*s", (int)server_use.uri.host.length,
665 (int)server_use.uri.host.length, server_use.uri.host.s);
666
667 switch (server_use.uri.scheme) {
671#if COAP_OSCORE_SUPPORT
672 if (server_use.oscore_conf) {
673 proxy_entry->ongoing =
674 coap_new_client_session_oscore3_lkd(context, local_addr, &dst,
675 proto, server_use.oscore_conf,
676 NULL, NULL, NULL);
677 } else {
678#endif /* COAP_OSCORE_SUPPORT */
679 proxy_entry->ongoing =
680 coap_new_client_session3_lkd(context, local_addr, &dst, proto,
681 NULL, NULL, NULL);
682#if COAP_OSCORE_SUPPORT
683 }
684#endif /* COAP_OSCORE_SUPPORT */
685 break;
689#if COAP_OSCORE_SUPPORT
690 if (server_use.oscore_conf) {
691 if (server_use.dtls_pki) {
692 server_use.dtls_pki->client_sni = client_sni;
693 proxy_entry->ongoing =
694 coap_new_client_session_oscore_pki3_lkd(context, local_addr, &dst,
695 proto, server_use.dtls_pki,
696 server_use.oscore_conf,
697 NULL, NULL, NULL);
698 } else if (server_use.dtls_cpsk) {
699 server_use.dtls_cpsk->client_sni = client_sni;
700 proxy_entry->ongoing =
701 coap_new_client_session_oscore_psk3_lkd(context, local_addr, &dst,
702 proto, server_use.dtls_cpsk,
703 server_use.oscore_conf,
704 NULL, NULL, NULL);
705 } else {
706 coap_log_warn("Proxy: (D)TLS not configured for secure session\n");
707 }
708 } else {
709#endif /* COAP_OSCORE_SUPPORT */
710 /* Not doing OSCORE */
711 if (server_use.dtls_pki) {
712 server_use.dtls_pki->client_sni = client_sni;
713 proxy_entry->ongoing =
714 coap_new_client_session_pki3_lkd(context, local_addr, &dst,
715 proto, server_use.dtls_pki,
716 NULL, NULL, NULL);
717 } else if (server_use.dtls_cpsk) {
718 server_use.dtls_cpsk->client_sni = client_sni;
719 proxy_entry->ongoing =
720 coap_new_client_session_psk3_lkd(context, local_addr, &dst,
721 proto, server_use.dtls_cpsk,
722 NULL, NULL, NULL);
723 } else {
724 /* Using client anonymous PKI */
725 proxy_entry->ongoing =
726 coap_new_client_session3_lkd(context, local_addr, &dst, proto,
727 NULL, NULL, NULL);
728 }
729#if COAP_OSCORE_SUPPORT
730 }
731#endif /* COAP_OSCORE_SUPPORT */
732 break;
736 default:
737 assert(0);
738 break;
739 }
740 if (proxy_entry->ongoing == NULL) {
741 response->code = COAP_RESPONSE_CODE(505);
742 coap_proxy_remove_association(session, 0);
743 return NULL;
744 }
745 if (proxy_entry_created) {
746 coap_log_debug("* %s: proxy_entry %.*s:%d %p created (tot count = % " PRIdS ")\n",
747 coap_session_str(proxy_entry->ongoing),
748 (int)proxy_entry->uri.host.length, proxy_entry->uri.host.s,
749 proxy_entry->uri.port,
750 (void *)proxy_entry,
751 session->context->proxy_list_count);
752 }
753 } else if (proxy_entry->ongoing->session_failed) {
754 if (!coap_session_reconnect(proxy_entry->ongoing)) {
755 /* Server is not yet back up */
756 return NULL;
757 }
758 }
759
760 return proxy_entry;
761}
762
763static void
764coap_proxy_release_body_data(coap_session_t *session COAP_UNUSED,
765 void *app_ptr) {
766 coap_delete_binary(app_ptr);
767}
768
769static coap_proxy_req_t *
770coap_proxy_get_req(coap_proxy_entry_t *proxy_entry, coap_proxy_cache_t *proxy_cache,
771 coap_session_t *session) {
772 coap_proxy_req_t *proxy_req;
773
774 LL_FOREACH(proxy_entry->proxy_req, proxy_req) {
775 if (proxy_req->incoming == session && proxy_req->proxy_cache == proxy_cache) {
776 return proxy_req;
777 }
778 }
779 return NULL;
780}
781
782static void
783coap_proxy_free_response_data(coap_session_t *session COAP_UNUSED, void *app_ptr) {
784 coap_delete_bin_const(app_ptr);
785}
786
787static coap_response_t
788coap_proxy_call_response_handler(coap_session_t *session, const coap_pdu_t *sent,
789 coap_pdu_t *rcvd, coap_bin_const_t *token,
790 coap_proxy_req_t *proxy_req, int replace_mid,
791 int remove_observe) {
793 coap_pdu_t *resp_pdu;
794 coap_pdu_t *fwd_pdu = NULL;
795 size_t size;
796 size_t offset;
797 size_t total;
798 const uint8_t *data;
799 coap_string_t *l_query = NULL;
800 coap_opt_filter_t opt_filter;
801
802 coap_option_filter_clear(&opt_filter);
803 if (!coap_option_check_critical(session, rcvd, &opt_filter, COAP_CRIT_PROXY)) {
804 /* Need to send back a 5.02 response */
805 coap_send_error_lkd(session, rcvd, COAP_RESPONSE_CODE(502), &opt_filter);
806 return COAP_RESPONSE_FAIL;
807 }
808
809 if (remove_observe) {
810 coap_opt_filter_t drop_options;
811
812 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
814 /* Correct the token */
815 resp_pdu = coap_pdu_duplicate_lkd(rcvd, session, token->length, token->s,
816 &drop_options, COAP_BOOL_FALSE);
817 } else {
818 /* Correct the token */
819 resp_pdu = coap_pdu_duplicate_lkd(rcvd, session, token->length, token->s,
821 }
822 if (!resp_pdu)
823 return COAP_RESPONSE_FAIL;
824
825 assert(proxy_req);
826 /* Incase change in type of request over the proxy */
827 if (proxy_req->pdu)
828 resp_pdu->type = proxy_req->pdu->type;
829
830 if (COAP_PROTO_NOT_RELIABLE(session->proto))
831 resp_pdu->max_size = coap_session_max_pdu_size_lkd(session);
832
833 if (replace_mid)
834 resp_pdu->mid = sent->mid;
835
836 proxy_req->mid = resp_pdu->mid;
837
838 /* If sent early ACK, and this is an ACK, need to convert it to CON */
839 if (COAP_PROTO_NOT_RELIABLE(session->proto) && resp_pdu->type == COAP_MESSAGE_ACK &&
841 resp_pdu->type = COAP_MESSAGE_CON;
842 }
843
844 if (coap_get_data_large(rcvd, &size, &data, &offset, &total)) {
845 uint16_t media_type = 0;
846 int maxage = -1;
847 uint64_t etag = 0;
848 coap_opt_t *option;
849 coap_opt_iterator_t opt_iter;
850 coap_bin_const_t *body;
851
852 /* COAP_BLOCK_SINGLE_BODY is set, so single body should be given */
853 assert(size == total);
854
855 body = coap_new_bin_const(data, size);
856 if (!body) {
857 coap_log_debug("coap_proxy_call_response_handler: copy data error\n");
858 goto failed;
859 }
860 option = coap_check_option(rcvd, COAP_OPTION_CONTENT_FORMAT, &opt_iter);
861 if (option) {
862 media_type = coap_decode_var_bytes(coap_opt_value(option),
863 coap_opt_length(option));
864 }
865 option = coap_check_option(rcvd, COAP_OPTION_MAXAGE, &opt_iter);
866 if (option) {
867 maxage = coap_decode_var_bytes(coap_opt_value(option),
868 coap_opt_length(option));
869 }
870 option = coap_check_option(rcvd, COAP_OPTION_ETAG, &opt_iter);
871 if (option) {
873 coap_opt_length(option));
874 }
875 if (sent)
876 l_query = coap_get_query(sent);
877 if (!coap_add_data_large_response_lkd(proxy_req->resource, session, sent,
878 resp_pdu,
879 l_query,
880 media_type, maxage, etag, body->length,
881 body->s,
882 coap_proxy_free_response_data,
883 body)) {
884 coap_log_debug("coap_proxy_call_response_handler: add data error\n");
885 goto failed;
886 }
887 }
889 session->context->proxy_response_cb(session,
890 sent,
891 resp_pdu,
892 proxy_req->cache_key),
893 /* context is being freed off */
894 goto failed);
895 if (fwd_pdu) {
896 ret = COAP_RESPONSE_OK;
897 if (coap_send_lkd(session, fwd_pdu) == COAP_INVALID_MID) {
898 ret = COAP_RESPONSE_FAIL;
899 }
900 if (fwd_pdu != resp_pdu) {
901 /* Application created a new PDU */
902 coap_delete_pdu_lkd(resp_pdu);
903 }
904 } else {
905failed:
906 ret = COAP_RESPONSE_FAIL;
907 coap_delete_pdu_lkd(resp_pdu);
908 }
909 coap_delete_string(l_query);
910 return ret;
911}
912
913int COAP_API
915 const coap_pdu_t *request,
916 coap_pdu_t *response,
917 coap_resource_t *resource,
918 coap_cache_key_t *cache_key,
919 coap_proxy_server_list_t *server_list) {
920 int ret;
921
922 coap_lock_lock(return 0);
923 ret = coap_proxy_forward_request_lkd(session,
924 request,
925 response,
926 resource,
927 cache_key,
928 server_list);
930 return ret;
931}
932
933/* https://rfc-editor.org/rfc/rfc7641#section-3.6 */
934static const uint16_t coap_proxy_ignore_options[] = { COAP_OPTION_ETAG,
939 };
940
941int
942coap_proxy_forward_request_lkd(coap_session_t *session,
943 const coap_pdu_t *request,
944 coap_pdu_t *response,
945 coap_resource_t *resource,
946 coap_cache_key_t *cache_key,
947 coap_proxy_server_list_t *server_list) {
948 coap_proxy_entry_t *proxy_entry;
949 size_t size;
950 size_t offset;
951 size_t total;
952 coap_binary_t *body_data = NULL;
953 const uint8_t *data;
954 coap_pdu_t *pdu = NULL;
955 coap_bin_const_t r_token = coap_pdu_get_token(request);
956 uint8_t token[8];
957 size_t token_len;
958 coap_proxy_req_t *proxy_req = NULL;
959 coap_optlist_t *optlist = NULL;
960 coap_opt_t *option;
961 coap_opt_iterator_t opt_iter;
962 coap_uri_t uri;
963 coap_opt_t *obs_opt = coap_check_option(request,
965 &opt_iter);
966 coap_proxy_cache_t *proxy_cache = NULL;
967 coap_proxy_t proxy_type;
968 coap_tick_t now;
969 uint32_t new_maxage;
970 uint8_t buf[4];
971
972 coap_ticks(&now);
973
974 /* Set up ongoing session (if not already done) */
975 proxy_entry = coap_proxy_get_ongoing_session(session, request, response,
976 server_list);
977 if (!proxy_entry) {
978 /* Error response code already set */
979 return 0;
980 }
981 coap_proxy_log_entry(session, request, NULL, "req");
982
983 /* Is this a observe cached request? */
984 if (obs_opt && session->context->proxy_response_cb) {
985 coap_cache_key_t *cache_key_l;
986
987 cache_key_l = coap_cache_derive_key_w_ignore(session, request,
989 coap_proxy_ignore_options,
990 sizeof(coap_proxy_ignore_options)/sizeof(coap_proxy_ignore_options[0]));
991 if (!cache_key_l) {
992 response->code = COAP_RESPONSE_CODE(505);
993 return 0;
994 }
995 PROXY_CACHE_FIND(proxy_entry->rsp_cache, cache_key_l, proxy_cache);
996 coap_delete_cache_key(cache_key_l);
997 }
998
999 if (proxy_cache) {
1000 proxy_req = coap_proxy_get_req(proxy_entry, proxy_cache, session);
1001 if (proxy_req) {
1002 if (obs_opt) {
1003 int observe_action;
1004
1005 observe_action = coap_decode_var_bytes(coap_opt_value(obs_opt),
1006 coap_opt_length(obs_opt));
1007
1008 if (observe_action == COAP_OBSERVE_CANCEL) {
1009 if (proxy_req->doing_observe) {
1010 assert(proxy_req->incoming->ref_proxy_subs);
1011 proxy_req->incoming->ref_proxy_subs--;
1012 proxy_req->doing_observe = 0;
1013 }
1014 if (proxy_entry->proxy_req && !proxy_entry->proxy_req->next &&
1015 proxy_req->token_used) {
1016 coap_binary_t tmp;
1017
1018 coap_log_debug("coap_proxy_forward_request: Using coap_cancel_observe() to do Proxy OBSERVE cancellation\n");
1019 /* Unfortunately need to change the ptr type to be r/w */
1020 memcpy(&tmp.s, &proxy_req->token_used->s, sizeof(tmp.s));
1021 tmp.length = proxy_req->token_used->length;
1022 coap_cancel_observe_lkd(proxy_entry->ongoing, &tmp, COAP_MESSAGE_CON);
1023 /* Let the upstream cancellation be the response */
1024 return 1;
1025 } else {
1026 obs_opt = 0;
1027 goto return_cached_info;
1028 }
1029 } else if (observe_action == COAP_OBSERVE_ESTABLISH) {
1030 /* Client must be re-registering */
1031 if (proxy_cache && (proxy_cache->expire + COAP_TICKS_PER_SECOND) < now) {
1032 /* Need to get an updated rsp_pdu */
1033 coap_proxy_del_req(proxy_entry, proxy_req);
1034 proxy_req = NULL;
1035 proxy_cache = NULL;
1036 } else {
1037 goto return_cached_info;
1038 }
1039 }
1040 } else {
1041 if (proxy_cache && (proxy_cache->expire + COAP_TICKS_PER_SECOND) < now) {
1042 /* Need to get an updated rsp_pdu */
1043 coap_proxy_del_req(proxy_entry, proxy_req);
1044 proxy_req = NULL;
1045 proxy_cache = NULL;
1046 } else {
1047 goto return_cached_info;
1048 }
1049 }
1050 }
1051 }
1052
1053 if (!proxy_req) {
1054 proxy_req = coap_malloc_type(COAP_STRING, sizeof(coap_proxy_req_t));
1055 if (proxy_req == NULL) {
1056 goto failed;
1057 }
1058 memset(proxy_req, 0, sizeof(coap_proxy_req_t));
1059 LL_PREPEND(proxy_entry->proxy_req, proxy_req);
1060
1061 proxy_req->pdu = coap_const_pdu_reference_lkd(request);
1062 proxy_req->resource = resource;
1063 proxy_req->incoming = session;
1064 proxy_req->cache_key = cache_key;
1065 proxy_req->proxy_cache = proxy_cache;
1066
1067 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used, "add");
1068 }
1069
1070 if (proxy_cache) {
1071 coap_bin_const_t j_token;
1072
1073 if (obs_opt)
1074 proxy_cache->ref++;
1075 coap_delete_bin_const(proxy_req->token_used);
1076 j_token = coap_pdu_get_token(proxy_cache->rsp_pdu);
1077 proxy_req->token_used = coap_new_bin_const(j_token.s, j_token.length);
1078 if (proxy_req->token_used == NULL) {
1079 goto failed;
1080 }
1081 goto return_cached_info;
1082 }
1083
1084 /* Get a new token for ongoing session */
1085 coap_session_new_token(proxy_entry->ongoing, &token_len, token);
1086 coap_delete_bin_const(proxy_req->token_used);
1087 proxy_req->token_used = coap_new_bin_const(token, token_len);
1088 if (proxy_req->token_used == NULL) {
1089 goto failed;
1090 }
1091 coap_pdu_release_lkd(proxy_req->pdu);
1092 proxy_req->pdu = coap_const_pdu_reference_lkd(request);
1093
1094 /* Need to create the ongoing request pdu entry */
1095 proxy_type = coap_proxy_map_type(server_list->type);
1096 if (proxy_type & COAP_PROXY_BIT_STRIP) {
1097 /*
1098 * Need to replace Proxy-Uri with Uri-Host (and Uri-Port)
1099 * and strip out Proxy-Scheme.
1100 */
1101
1102 /*
1103 * Build up the ongoing PDU that we are going to send
1104 */
1105 pdu = coap_pdu_init(request->type, request->code,
1106 coap_new_message_id_lkd(proxy_entry->ongoing),
1107 coap_session_max_pdu_size_lkd(proxy_entry->ongoing));
1108 if (!pdu) {
1109 goto failed;
1110 }
1111
1112 if (coap_is_mcast(&proxy_entry->ongoing->addr_info.remote)) {
1113 pdu->type = COAP_MESSAGE_NON;
1114 }
1115
1116 if (!coap_add_token(pdu, token_len, token)) {
1117 goto failed;
1118 }
1119
1120 /* Copy the remaining options across */
1121 coap_option_iterator_init(request, &opt_iter, COAP_OPT_ALL);
1122 while ((option = coap_option_next(&opt_iter))) {
1123 switch (opt_iter.number) {
1126 coap_opt_length(option),
1127 &uri) < 0) {
1128 /* Need to return a 5.05 RFC7252 Section 5.7.2 */
1129 coap_log_warn("Proxy URI not decodable\n");
1130 goto failed;
1131 }
1132 if (!coap_uri_into_optlist(&uri, NULL, &optlist, 0)) {
1133 coap_log_err("Failed to create options for URI\n");
1134 goto failed;
1135 }
1136 break;
1138 break;
1139 case COAP_OPTION_BLOCK1:
1140 case COAP_OPTION_BLOCK2:
1143 /* These are not passed on */
1144 break;
1147 break;
1148 default:
1149 coap_insert_optlist(&optlist,
1150 coap_new_optlist(opt_iter.number,
1151 coap_opt_length(option),
1152 coap_opt_value(option)));
1153 break;
1154 }
1155 }
1156
1157 /* Update pdu with options */
1158 coap_add_optlist_pdu(pdu, &optlist);
1159 coap_delete_optlist(optlist);
1160 optlist = NULL;
1161 } else {
1162 /*
1163 * Duplicate request PDU for onward transmission (with new token).
1164 */
1165 pdu = coap_pdu_duplicate_lkd(request, proxy_entry->ongoing, token_len,
1166 token, NULL, COAP_BOOL_FALSE);
1167 if (!pdu) {
1168 coap_log_debug("proxy: PDU generation error\n");
1169 goto failed;
1170 }
1171 if (COAP_PROTO_NOT_RELIABLE(session->proto))
1172 pdu->max_size = coap_session_max_pdu_size_lkd(proxy_entry->ongoing);
1173 }
1174
1175 if (coap_get_data_large(request, &size, &data, &offset, &total)) {
1176 /* COAP_BLOCK_SINGLE_BODY is set, so single body should be given */
1177 assert(size == total);
1178 /*
1179 * Need to take a copy of the data as request PDU may go away before
1180 * all data is transmitted.
1181 */
1182 body_data = coap_new_binary(total);
1183 if (!body_data) {
1184 coap_log_debug("proxy: body build memory error\n");
1185 goto failed;
1186 }
1187 memcpy(body_data->s, data, size);
1188 if (!coap_add_data_large_request_lkd(proxy_entry->ongoing, pdu, total, data,
1189 coap_proxy_release_body_data, body_data)) {
1190 coap_log_debug("proxy: add data error\n");
1191 goto failed;
1192 }
1193 }
1194
1195 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used, "fwd");
1196 if (coap_send_lkd(proxy_entry->ongoing, pdu) == COAP_INVALID_MID) {
1197 pdu = NULL;
1198 coap_log_debug("proxy: upstream PDU send error\n");
1199 goto failed;
1200 }
1201
1202 /*
1203 * Do not update the response code (hence empty ACK) as will be sending
1204 * separate response when response comes back from upstream server
1205 */
1206
1207 return 1;
1208
1209failed:
1210 response->code = COAP_RESPONSE_CODE(500);
1211 coap_delete_optlist(optlist);
1213 return 0;
1214
1215return_cached_info:
1216 if (obs_opt && !proxy_req->doing_observe) {
1217 int observe_action;
1218
1219 observe_action = coap_decode_var_bytes(coap_opt_value(obs_opt),
1220 coap_opt_length(obs_opt));
1221
1222 if (observe_action == COAP_OBSERVE_ESTABLISH) {
1223 proxy_req->doing_observe = 1;
1224 proxy_req->incoming->ref_proxy_subs++;
1225 }
1226 }
1227 coap_log_debug("* %s: Using Proxy Cache (Active %d)\n",
1228 coap_session_str(session),
1229 proxy_cache->ref);
1230 coap_proxy_log_entry(session, request, &proxy_cache->rsp_pdu->actual_token, "rspc");
1231 if (now < proxy_cache->expire) {
1232 new_maxage = (uint32_t)((proxy_cache->expire - now) / COAP_TICKS_PER_SECOND);
1233 if (new_maxage == 0)
1234 new_maxage = 1;
1235 } else {
1236 new_maxage = 1;
1237 }
1238 coap_update_option(proxy_cache->rsp_pdu,
1240 coap_encode_var_safe(buf, sizeof(buf), new_maxage), buf);
1241 coap_proxy_call_response_handler(session, request, proxy_cache->rsp_pdu,
1242 &r_token, proxy_req, 1, obs_opt ? 0 : 1);
1243 if (!obs_opt)
1244 coap_proxy_del_req(proxy_entry, proxy_req);
1245 return 1;
1246}
1247
1248coap_proxy_req_t *
1249coap_proxy_map_outgoing_request(coap_session_t *ongoing,
1250 const coap_pdu_t *received,
1251 coap_proxy_entry_t **u_proxy_entry) {
1252 coap_proxy_entry_t *proxy_list = ongoing->context->proxy_list;
1253 size_t proxy_list_count = ongoing->context->proxy_list_count;
1254 size_t i;
1255 coap_bin_const_t rcv_token = coap_pdu_get_token(received);
1256 coap_proxy_entry_t *proxy_entry = NULL;
1257 coap_proxy_req_t *proxy_req;
1258
1259 for (i = 0; i < proxy_list_count; i++) {
1260 proxy_entry = &proxy_list[i];
1261 if (proxy_entry->ongoing == ongoing) {
1262 LL_FOREACH(proxy_entry->proxy_req, proxy_req) {
1263 if (coap_binary_equal(&rcv_token, proxy_req->token_used)) {
1264 coap_ticks(&proxy_entry->last_used);
1265 if (u_proxy_entry)
1266 *u_proxy_entry = proxy_entry;
1267 return proxy_req;
1268 }
1269 }
1270 }
1271 }
1273 char scratch[24];
1274 size_t size;
1275
1276 scratch[0] = '\000';
1277 for (i = 0; i < rcv_token.length; i++) {
1278 size = strlen(scratch);
1279 snprintf(&scratch[size], sizeof(scratch)-size,
1280 "%02x", rcv_token.s[i]);
1281 }
1282 coap_log_debug(" proxy token to match {%s}\n", scratch);
1283 for (i = 0; i < proxy_list_count; i++) {
1284 proxy_entry = &proxy_list[i];
1285 LL_FOREACH(proxy_entry->proxy_req, proxy_req) {
1286 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used, "miss");
1287 }
1288 }
1289 }
1290 return NULL;
1291}
1292
1295 const coap_pdu_t *received,
1296 coap_cache_key_t **cache_key) {
1297 int ret;
1298
1299 coap_lock_lock(return 0);
1300 ret = coap_proxy_forward_response_lkd(session,
1301 received,
1302 cache_key);
1304 return ret;
1305}
1306
1308coap_proxy_forward_response_lkd(coap_session_t *session,
1309 const coap_pdu_t *received,
1310 coap_cache_key_t **cache_key) {
1311 coap_pdu_t *pdu = NULL;
1312 coap_session_t *incoming = NULL;
1313 size_t size;
1314 const uint8_t *data;
1315 coap_optlist_t *optlist = NULL;
1316 coap_opt_t *option;
1317 coap_opt_iterator_t opt_iter;
1318 size_t offset;
1319 size_t total;
1320 coap_proxy_entry_t *proxy_entry = NULL;
1321 uint16_t media_type = COAP_MEDIATYPE_TEXT_PLAIN;
1322 int maxage = -1;
1323 uint64_t etag = 0;
1324 coap_pdu_code_t rcv_code = coap_pdu_get_code(received);
1325 coap_bin_const_t req_token;
1326 coap_binary_t *body_data = NULL;
1327 coap_pdu_t *req_pdu;
1328 coap_resource_t *resource;
1329 coap_proxy_req_t *proxy_req = NULL;
1330
1331 proxy_req = coap_proxy_map_outgoing_request(session, received, &proxy_entry);
1332 if (!proxy_req || proxy_req->incoming->server_list) {
1333 coap_log_warn("Unknown proxy ongoing session response received - ignored\n");
1334 return COAP_RESPONSE_OK;
1335 }
1336
1337 req_pdu = proxy_req->pdu;
1338 req_token = coap_pdu_get_token(req_pdu);
1339 resource = proxy_req->resource;
1340 incoming = proxy_req->incoming;
1341
1342 coap_log_debug("** process upstream incoming %d.%02d response:\n",
1343 COAP_RESPONSE_CLASS(rcv_code), rcv_code & 0x1F);
1344
1345 if (coap_get_data_large(received, &size, &data, &offset, &total)) {
1346 /* COAP_BLOCK_SINGLE_BODY is set, so single body should be given */
1347 assert(size == total);
1348 body_data = coap_new_binary(total);
1349 if (!body_data) {
1350 coap_log_debug("body build memory error\n");
1351 goto remove_match;
1352 }
1353 memcpy(body_data->s, data, size);
1354 data = body_data->s;
1355 }
1356
1357 /*
1358 * Build up the ongoing PDU that we are going to send to proxy originator
1359 * as separate response
1360 */
1361 pdu = coap_pdu_init(req_pdu->type, rcv_code,
1362 coap_new_message_id_lkd(incoming),
1364 if (!pdu) {
1365 coap_log_debug("Failed to create ongoing proxy response PDU\n");
1366 goto remove_match;
1367 }
1368
1369 if (!coap_add_token(pdu, req_token.length, req_token.s)) {
1370 coap_log_debug("cannot add token to ongoing proxy response PDU\n");
1371 }
1372
1373 /*
1374 * Copy the options across, skipping those needed for
1375 * coap_add_data_response_large()
1376 */
1377 coap_option_iterator_init(received, &opt_iter, COAP_OPT_ALL);
1378 while ((option = coap_option_next(&opt_iter))) {
1379 switch (opt_iter.number) {
1381 media_type = coap_decode_var_bytes(coap_opt_value(option),
1382 coap_opt_length(option));
1383 break;
1384 case COAP_OPTION_MAXAGE:
1385 maxage = coap_decode_var_bytes(coap_opt_value(option),
1386 coap_opt_length(option));
1387 break;
1388 case COAP_OPTION_ETAG:
1390 coap_opt_length(option));
1391 break;
1392 case COAP_OPTION_BLOCK2:
1394 case COAP_OPTION_SIZE2:
1395 break;
1396 default:
1397 coap_insert_optlist(&optlist,
1398 coap_new_optlist(opt_iter.number,
1399 coap_opt_length(option),
1400 coap_opt_value(option)));
1401 break;
1402 }
1403 }
1404 coap_add_optlist_pdu(pdu, &optlist);
1405 coap_delete_optlist(optlist);
1406
1407 if (size > 0) {
1408 coap_string_t *l_query = coap_get_query(req_pdu);
1409
1410 coap_add_data_large_response_lkd(resource, incoming, req_pdu, pdu,
1411 l_query,
1412 media_type, maxage, etag, size, data,
1413 coap_proxy_release_body_data,
1414 body_data);
1415 body_data = NULL;
1416 coap_delete_string(l_query);
1417 }
1418
1419 if (cache_key)
1420 *cache_key = proxy_req->cache_key;
1421
1422 coap_send_lkd(incoming, pdu);
1423
1424remove_match:
1425 option = coap_check_option(received, COAP_OPTION_OBSERVE, &opt_iter);
1426 /* Need to remove matching token entry (apart from an Observe response) */
1427 if (option == NULL) {
1428 if (proxy_entry->proxy_req) {
1429 /* Do not delete cache key here - caller's responsibility */
1430 proxy_req->cache_key = NULL;
1431 coap_proxy_del_req(proxy_entry, proxy_req);
1432 }
1433 } else if (!proxy_req->doing_observe) {
1434 option = coap_check_option(proxy_req->pdu, COAP_OPTION_OBSERVE, &opt_iter);
1435 if (option &&
1438 proxy_req->doing_observe = 1;
1439 proxy_req->incoming->ref_proxy_subs++;
1440 }
1441 }
1442 coap_delete_binary(body_data);
1443 return COAP_RESPONSE_OK;
1444}
1445
1446void
1447coap_proxy_process_incoming(coap_session_t *session,
1448 coap_pdu_t *rcvd,
1449 void *body_data, coap_proxy_req_t *proxy_req,
1450 coap_proxy_entry_t *proxy_entry) {
1451 coap_opt_t *obs_opt;
1452 coap_opt_t *option;
1453 coap_opt_iterator_t opt_iter;
1455 coap_bin_const_t token;
1456 int can_cache = 0;
1457
1458 if (COAP_RESPONSE_CLASS(rcvd->code) == 2) {
1459 switch ((int)rcvd->code) {
1460 case COAP_RESPONSE_CODE(201):
1461 case COAP_RESPONSE_CODE(202):
1462 case COAP_RESPONSE_CODE(203):
1463 case COAP_RESPONSE_CODE(204):
1464 case COAP_RESPONSE_CODE(205):
1465 can_cache = 1;
1466 break;
1467 /* 2.13 should be handled by block transfer logic */
1468 case COAP_RESPONSE_CODE(213):
1469 default:
1470 break;
1471 }
1472 }
1473 obs_opt = coap_check_option(rcvd, COAP_OPTION_OBSERVE, &opt_iter);
1474
1475 /* See if we are doing proxy caching */
1476 if (can_cache && obs_opt) {
1477 coap_proxy_cache_t *proxy_cache;
1478 coap_cache_key_t *cache_key_l;
1479 coap_tick_t now;
1480 uint64_t expire;
1481
1482 /* Need to cache the response */
1483 if (proxy_req->proxy_cache) {
1484 coap_delete_pdu_lkd(proxy_req->proxy_cache->rsp_pdu);
1485 proxy_cache = proxy_req->proxy_cache;
1486 } else {
1487 proxy_cache = coap_malloc_type(COAP_STRING, sizeof(coap_proxy_cache_t));
1488 if (proxy_cache == NULL) {
1489 goto cache_fail;
1490 }
1491 memset(proxy_cache, 0, sizeof(coap_proxy_cache_t));
1492 cache_key_l = coap_cache_derive_key_w_ignore(session, proxy_req->pdu,
1494 coap_proxy_ignore_options,
1495 sizeof(coap_proxy_ignore_options)/sizeof(coap_proxy_ignore_options[0]));
1496 if (!cache_key_l) {
1497 coap_free_type(COAP_STRING, proxy_cache);
1498 goto cache_fail;
1499 }
1500 memcpy(&proxy_cache->cache_req, cache_key_l,
1501 sizeof(proxy_cache->cache_req));
1502 coap_delete_cache_key(cache_key_l);
1503
1504 proxy_cache->req_pdu = coap_pdu_reference_lkd(proxy_req->pdu);
1505 proxy_req->proxy_cache = proxy_cache;
1506
1507 PROXY_CACHE_ADD(proxy_entry->rsp_cache, proxy_cache);
1508 proxy_cache->ref++;
1509 }
1510 if (rcvd->body_data) {
1511 /* More data than that held in the PDU */
1512 const uint8_t *data;
1513 size_t data_len;
1514
1515 proxy_cache->rsp_pdu = coap_pdu_duplicate_lkd(rcvd,
1516 session,
1517 rcvd->actual_token.length,
1518 rcvd->actual_token.s,
1520 if (proxy_cache->rsp_pdu) {
1521 if (coap_get_data(rcvd, &data_len, &data)) {
1522 coap_binary_t *copy;
1523
1524 copy = coap_new_binary(data_len);
1525 if (copy) {
1526 memcpy(copy->s, data, data_len);
1527 proxy_cache->rsp_pdu->data_free = copy;
1528 proxy_cache->rsp_pdu->body_data = copy->s;
1529 proxy_cache->rsp_pdu->body_length = copy->length;
1530 proxy_cache->rsp_pdu->body_total = copy->length;
1531 } else {
1532 proxy_cache->rsp_pdu->body_data = NULL;
1533 proxy_cache->rsp_pdu->body_length = 0;
1534 proxy_cache->rsp_pdu->body_total = 0;
1535 }
1536 }
1537 }
1538 } else {
1539 /* The simple case */
1540 proxy_cache->rsp_pdu = coap_pdu_reference_lkd(rcvd);
1541 }
1542 option = coap_check_option(rcvd, COAP_OPTION_ETAG, &opt_iter);
1543 if (option) {
1544 proxy_cache->etag = coap_decode_var_bytes8(coap_opt_value(option),
1545 coap_opt_length(option));
1546 } else {
1547 proxy_cache->etag = 0;
1548 }
1549 coap_ticks(&now);
1550 option = coap_check_option(rcvd, COAP_OPTION_MAXAGE, &opt_iter);
1551 if (option) {
1552 expire = coap_decode_var_bytes(coap_opt_value(option),
1553 coap_opt_length(option));
1554 } else {
1555 /* Default is 60 seconds */
1556 expire = 60;
1557 }
1558 proxy_cache->expire = now + expire * COAP_TICKS_PER_SECOND;
1559
1560 /* Update all the cache listeners */
1561 LL_FOREACH(proxy_entry->proxy_req, proxy_req) {
1562 if (proxy_req->proxy_cache == proxy_cache) {
1563 if (!proxy_req->doing_observe) {
1564 coap_opt_t *req_obs;
1565
1566 req_obs = coap_check_option(proxy_req->pdu, COAP_OPTION_OBSERVE, &opt_iter);
1567 if (req_obs &&
1570 proxy_req->doing_observe = 1;
1571 proxy_req->incoming->ref_proxy_subs++;
1572 }
1573 }
1574 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used, "rsp");
1575 token = coap_pdu_get_token(proxy_req->pdu);
1576 if (coap_proxy_call_response_handler(proxy_req->incoming, proxy_req->pdu,
1577 rcvd, &token,
1578 proxy_req, 0, 0) == COAP_RESPONSE_OK) {
1579 /* At least one success */
1580 ret = COAP_RESPONSE_OK;
1581 }
1582 }
1583 }
1584 goto finish;
1585 }
1586cache_fail:
1587 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used, "rspn");
1588 token = coap_pdu_get_token(proxy_req->pdu);
1589 ret = coap_proxy_call_response_handler(proxy_req->incoming, proxy_req->pdu,
1590 rcvd, &token, proxy_req, 0, 0);
1591 if (!obs_opt)
1592 coap_proxy_del_req(proxy_entry, proxy_req);
1593
1594finish:
1595 if (ret == COAP_RESPONSE_FAIL && rcvd->type != COAP_MESSAGE_ACK) {
1596 coap_send_rst_lkd(session, rcvd);
1598 } else {
1599 coap_send_ack_lkd(session, rcvd);
1601 }
1602 coap_free_type(COAP_STRING, body_data);
1603}
1604
1605/*
1606 */
1608coap_proxy_local_write(coap_session_t *session, coap_pdu_t *pdu) {
1609 coap_pdu_t *response = NULL;
1610 coap_resource_t *resource;
1612
1613 resource = session->context->unknown_resource ?
1614 session->context->unknown_resource :
1615 session->context->proxy_uri_resource;
1616 if (!resource) {
1617 coap_log_err("coap_proxy_local_write: Unknown or Proxy resource not defined\n");
1618 goto fail;
1619 }
1620
1621 response = coap_pdu_init(pdu->type == COAP_MESSAGE_CON ?
1623 0, pdu->mid, coap_session_max_pdu_size_lkd(session));
1624 if (!response) {
1625 coap_log_err("coap_proxy_local_write: Could not create response PDU\n");
1626 goto fail;
1627 }
1628 response->session = session;
1629
1630 if (!coap_add_token(response, pdu->actual_token.length,
1631 pdu->actual_token.s)) {
1632 goto fail;
1633 }
1634
1635 coap_log_debug("* %s: internal: sent %4" PRIdS " bytes\n",
1636 coap_session_str(session),
1637 pdu->used_size + coap_pdu_encode_header(pdu, session->proto));
1639
1640 mid = pdu->mid;
1641 if (!coap_proxy_forward_request_lkd(session, pdu, response, resource,
1642 NULL, session->server_list)) {
1643 coap_log_debug("coap_proxy_local_write: Failed to forward PDU\n");
1644 mid = COAP_INVALID_MID;
1645 }
1646fail:
1647 coap_delete_pdu_lkd(response);
1649 return mid;
1650}
1651
1654 coap_proxy_server_list_t *server_list) {
1655 coap_session_t *session;
1656
1657 coap_lock_lock(return NULL);
1658 session = coap_new_client_session_proxy_lkd(ctx, server_list);
1660 return session;
1661}
1662
1664coap_new_client_session_proxy_lkd(coap_context_t *ctx,
1665 coap_proxy_server_list_t *server_list) {
1666 coap_session_t *session;
1667 coap_addr_info_t *info_list = NULL;
1668 coap_str_const_t remote;
1669
1671
1672#if COAP_IPV6_SUPPORT
1673 remote.s = (const uint8_t *)"::1";
1674#elif COAP_IPV4_SUPPORT
1675 remote.s = (const uint8_t *)"127.0.0.1";
1676#else /* !COAP_IPV6_SUPPORT && ! COAP_IPV4_SUPPORT */
1677 coap_log_warn("coap_new_client_session_proxy: No IPv4 or IPv6 support\n");
1678 return NULL;
1679#endif /* !COAP_IPV6_SUPPORT && ! COAP_IPV4_SUPPORT */
1680 remote.length = strlen((const char *)remote.s);
1681 /* resolve internal remote address where proxy session is 'connecting' to */
1682 info_list = coap_resolve_address_info_lkd(&remote, 0, 0, 0, 0,
1683 0,
1686 if (!info_list) {
1687 coap_log_warn("coap_new_client_session_proxy: Unable to resolve IP address\n");
1688 return NULL;
1689 }
1690
1691 session = coap_new_client_session3_lkd(ctx, NULL, &info_list->addr, COAP_PROTO_UDP,
1692 NULL, NULL, NULL);
1693
1694 if (session) {
1695 session->server_list = server_list;
1696 }
1697 coap_free_address_info(info_list);
1698 return session;
1699}
1700
1702coap_proxy_fwd_add_client_session(coap_session_t *session, const char *use_ip,
1703 uint16_t use_port,
1704 coap_proxy_server_list_t *server_list) {
1705 coap_proxy_entry_t *ret;
1706
1707 coap_lock_lock(return 0);
1708 ret = coap_proxy_fwd_add_client_session_lkd(session, use_ip, use_port, server_list);
1710 return ret;
1711}
1712
1714coap_proxy_fwd_add_client_session_lkd(coap_session_t *session, const char *use_ip,
1715 uint16_t use_port,
1716 coap_proxy_server_list_t *server_list) {
1717#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
1718 char addr[INET6_ADDRSTRLEN];
1719#else /* WITH_LWIP || WITH_CONTIKI */
1720 char addr[40];
1721#endif /* WITH_LWIP || WITH_CONTIKI */
1722 coap_proxy_entry_t *proxy_entry;
1723 coap_uri_t uri;
1724
1725 if (!use_ip && !coap_print_ip_addr(&session->addr_info.remote, addr, sizeof(addr))) {
1726 return NULL;
1727 }
1728 if (!use_ip)
1729 use_ip = addr;
1730
1731 memset(&uri, 0, sizeof(uri));
1732 uri.port = use_port;
1733 switch (session->proto) {
1734 case COAP_PROTO_UDP:
1736 if (uri.port == 0)
1737 uri.port = 5683;
1738 break;
1739 case COAP_PROTO_DTLS:
1741 if (uri.port == 0)
1742 uri.port = 5684;
1743 break;
1744 case COAP_PROTO_TCP:
1746 if (uri.port == 0)
1747 uri.port = 5683;
1748 break;
1749 case COAP_PROTO_TLS:
1751 if (uri.port == 0)
1752 uri.port = 5684;
1753 break;
1754 case COAP_PROTO_WS:
1756 if (uri.port == 0)
1757 uri.port = 80;
1758 break;
1759 case COAP_PROTO_WSS:
1761 if (uri.port == 0)
1762 uri.port = 443;
1763 break;
1764 case COAP_PROTO_LAST:
1765 case COAP_PROTO_NONE:
1766 default:
1767 return NULL;
1768 }
1769 if (memcmp(use_ip, "::ffff:", 7) == 0) {
1770 /* IPv4 in IPv6 format */
1771 uri.host.s = (const uint8_t *)&use_ip[7];
1772 uri.host.length = strlen(&use_ip[7]);
1773 } else {
1774 uri.host.s = (const uint8_t *)use_ip;
1775 uri.host.length = strlen(use_ip);
1776 }
1777 if (!uri.host.length)
1778 return NULL;
1779
1780 proxy_entry = coap_proxy_get_add_list_entry(NULL, session, uri, server_list, 1);
1781 if (proxy_entry) {
1782 coap_log_debug("* %s: proxy_entry %.*s:%d %p created (tot count = % " PRIdS ")\n",
1783 coap_session_str(proxy_entry->ongoing),
1784 (int)proxy_entry->uri.host.length, proxy_entry->uri.host.s,
1785 proxy_entry->uri.port,
1786 (void *)proxy_entry,
1787 session->context->proxy_list_count);
1788 proxy_entry->idle_timeout_ticks = 0;
1789 proxy_entry->track_client_session = 0;
1791 return proxy_entry;
1792 }
1793 return NULL;
1794}
1795
1796void
1797coap_delete_proxy_subscriber(coap_session_t *session, coap_bin_const_t *token,
1798 coap_mid_t mid, coap_proxy_subs_delete_t type) {
1799 /* Need to check is there is a proxy subscription active and delete it */
1800 coap_context_t *context = session->context;
1801 size_t i;
1802
1803 for (i = 0; i < context->proxy_list_count; i++) {
1804 coap_proxy_entry_t *proxy_entry = &context->proxy_list[i];
1805 coap_proxy_req_t *proxy_req, *treq;
1806
1807 LL_FOREACH_SAFE(proxy_entry->proxy_req, proxy_req, treq) {
1808 if (proxy_req->incoming == session) {
1809 coap_bin_const_t req_token;
1810 int match = 0;
1811
1812 req_token = coap_pdu_get_token(proxy_req->pdu);
1813 switch (type) {
1814 case COAP_PROXY_SUBS_ALL:
1815 match = 1;
1816 break;
1817 case COAP_PROXY_SUBS_TOKEN:
1818 if (token && coap_binary_equal(token, &req_token))
1819 match = 1;
1820 break;
1821 case COAP_PROXY_SUBS_MID:
1822 if (proxy_req->mid == mid)
1823 match = 1;
1824 break;
1825 default:
1826 break;
1827 }
1828
1829 if (match && proxy_entry->proxy_req && ! proxy_entry->proxy_req->next &&
1830 proxy_req->token_used) {
1831 coap_binary_t tmp;
1832
1833 coap_log_debug("coap_delete_proxy_subscriber: Using coap_cancel_observe() to do Proxy OBSERVE cancellation\n");
1834 /* Unfortunately need to change the ptr type to be r/w */
1835 memcpy(&tmp.s, &proxy_req->token_used->s, sizeof(tmp.s));
1836 tmp.length = proxy_req->token_used->length;
1837 coap_cancel_observe_lkd(proxy_entry->ongoing, &tmp, COAP_MESSAGE_CON);
1838 }
1839 coap_proxy_del_req(proxy_entry, proxy_req);
1840 }
1841 }
1842 }
1843}
1844
1845#else /* ! COAP_PROXY_SUPPORT */
1846
1847int
1849 return 0;
1850}
1851
1852COAP_API int
1854 const coap_pdu_t *request,
1855 coap_pdu_t *response,
1856 coap_resource_t *resource,
1857 coap_cache_key_t *cache_key,
1858 coap_proxy_server_list_t *server_list) {
1859 (void)session;
1860 (void)request;
1861 (void)resource;
1862 (void)cache_key;
1863 (void)server_list;
1864 response->code = COAP_RESPONSE_CODE(500);
1865 return 0;
1866}
1867
1870 const coap_pdu_t *received,
1871 coap_cache_key_t **cache_key) {
1872 (void)session;
1873 (void)received;
1874 (void)cache_key;
1875 return COAP_RESPONSE_OK;
1876}
1877
1878int
1880 (void)scheme;
1881 return 0;
1882}
1883#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_RESOLVE_TYPE_REMOTE
remote side of session
#define COAP_UNIX_PATH_MAX
#define INET6_ADDRSTRLEN
Definition coap_debug.c:234
struct coap_lg_crcv_t coap_lg_crcv_t
struct coap_cache_key_t coap_cache_key_t
struct coap_proxy_entry_t coap_proxy_entry_t
Proxy information.
struct coap_resource_t coap_resource_t
#define PRIdS
#define PRIu64
Library specific build wrapper for coap_internal.h.
#define COAP_API
@ COAP_STRING
Definition coap_mem.h:33
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().
#define NULL
Definition coap_option.h:30
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
@ COAP_OPTION_OBSERVE
Definition coap_option.h:75
@ COAP_OPTION_ETAG
Definition coap_option.h:73
@ COAP_OPTION_MAXAGE
Definition coap_option.h:83
@ COAP_OPTION_SIZE2
Definition coap_option.h:92
@ COAP_OPTION_Q_BLOCK2
Definition coap_option.h:93
@ COAP_OPTION_PROXY_SCHEME
Definition coap_option.h:95
@ COAP_OPTION_URI_PORT
Definition coap_option.h:76
@ COAP_OPTION_URI_HOST
Definition coap_option.h:72
@ COAP_OPTION_BLOCK2
Definition coap_option.h:90
@ COAP_OPTION_RTAG
Definition coap_option.h:99
@ COAP_OPTION_BLOCK1
Definition coap_option.h:91
@ COAP_OPTION_Q_BLOCK1
Definition coap_option.h:87
@ COAP_OPTION_OSCORE
Definition coap_option.h:78
@ COAP_OPTION_CONTENT_FORMAT
Definition coap_option.h:80
@ COAP_OPTION_PROXY_URI
Definition coap_option.h:94
coap_uri_scheme_t
The scheme specifiers.
Definition coap_uri.h:30
@ COAP_URI_SCHEME_COAPS_WS
Definition coap_uri.h:38
@ COAP_URI_SCHEME_COAPS_TCP
Definition coap_uri.h:34
@ COAP_URI_SCHEME_COAPS
Definition coap_uri.h:32
@ COAP_URI_SCHEME_COAP_TCP
Definition coap_uri.h:33
@ COAP_URI_SCHEME_COAP_WS
Definition coap_uri.h:37
@ COAP_URI_SCHEME_HTTPS
Definition coap_uri.h:36
@ COAP_URI_SCHEME_COAP
Definition coap_uri.h:31
@ COAP_URI_SCHEME_LAST
Definition coap_uri.h:39
@ COAP_URI_SCHEME_HTTP
Definition coap_uri.h:35
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:1209
coap_mid_t coap_send_error_lkd(coap_session_t *session, const coap_pdu_t *request, coap_pdu_code_t code, coap_opt_filter_t *opts)
Sends an error response with code code for request request to dst.
Definition coap_net.c:1304
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:1614
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:1224
#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:40
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition coap_time.h:149
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition coap_time.h:164
#define COAP_MAX_DELAY_TICKS
Definition coap_time.h:231
uint16_t coap_new_message_id_lkd(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
int coap_option_check_critical(coap_session_t *session, coap_pdu_t *pdu, coap_opt_filter_t *unknown, coap_crit_type_t is_proxy)
Verifies that pdu contains no unknown critical options, duplicate options or the options defined as R...
Definition coap_net.c:1020
coap_addr_info_t * coap_resolve_address_info_lkd(const coap_str_const_t *address, uint16_t port, uint16_t secure_port, uint16_t ws_port, uint16_t ws_secure_port, int ai_hints_flags, int scheme_hint_bits, coap_resolve_type_t type)
Resolve the specified address into a set of coap_address_t that can be used to bind() (local) or conn...
@ COAP_CRIT_PROXY
coap_response_t
Definition coap_net.h:51
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:52
@ COAP_RESPONSE_OK
Response is fine.
Definition coap_net.h:53
unsigned int coap_encode_var_safe(uint8_t *buf, size_t length, unsigned int val)
Encodes multiple-length byte sequences.
Definition coap_encode.c:47
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:71
#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:812
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.
const char * coap_print_ip_addr(const coap_address_t *addr, char *buf, size_t len)
Print the IP address into the defined buffer.
Definition coap_debug.c:424
#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(coap_option_num_t number, size_t length, const uint8_t *data)
Create a new optlist entry.
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
coap_opt_iterator_t * coap_option_iterator_init(const coap_pdu_t *pdu, coap_opt_iterator_t *oi, const coap_opt_filter_t *filter)
Initializes the given option iterator oi to point to the beginning of the pdu's option list.
void coap_delete_optlist(coap_optlist_t *queue)
Removes all entries from the optlist_chain, freeing off their memory usage.
#define COAP_OPT_ALL
Pre-defined filter that includes all options.
int coap_add_optlist_pdu(coap_pdu_t *pdu, coap_optlist_t **options)
The current optlist of optlist_chain is first sorted (as per RFC7272 ordering requirements) and then ...
void coap_option_filter_clear(coap_opt_filter_t *filter)
Clears filter filter.
coap_opt_t * coap_check_option(const coap_pdu_t *pdu, coap_option_num_t number, coap_opt_iterator_t *oi)
Retrieves the first option of number number from pdu.
int coap_insert_optlist(coap_optlist_t **head, coap_optlist_t *node)
Adds optlist to the given optlist_chain.
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
int coap_option_filter_set(coap_opt_filter_t *filter, coap_option_num_t option)
Sets the corresponding entry for number in filter.
coap_session_t * coap_new_client_session_oscore3_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_oscore_conf_t *oscore_conf, void *app_data, coap_app_data_free_callback_t callback, coap_str_const_t *ws_host)
Creates a new client session to the designated server, protecting the data using OSCORE,...
coap_session_t * coap_new_client_session_oscore_psk3_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_cpsk_t *psk_data, coap_oscore_conf_t *oscore_conf, void *app_data, coap_app_data_free_callback_t callback, coap_str_const_t *ws_host)
Creates a new client session to the designated server, with PSK credentials protecting the data using...
coap_session_t * coap_new_client_session_oscore_pki3_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_pki_t *pki_data, coap_oscore_conf_t *oscore_conf, void *app_data, coap_app_data_free_callback_t callback, coap_str_const_t *ws_host)
Creates a new client session to the designated server, with PKI credentials protecting the data using...
coap_pdu_t * coap_pdu_reference_lkd(coap_pdu_t *pdu)
Increment reference counter on a pdu to stop it prematurely getting freed off when coap_delete_pdu() ...
Definition coap_pdu.c:1741
void coap_delete_pdu_lkd(coap_pdu_t *pdu)
Dispose of an CoAP PDU and free off associated storage.
Definition coap_pdu.c:197
coap_pdu_t * coap_pdu_duplicate_lkd(const coap_pdu_t *old_pdu, coap_session_t *session, size_t token_length, const uint8_t *token, coap_opt_filter_t *drop_options, coap_bool_t expand_opt_abb)
Duplicate an existing PDU.
Definition coap_pdu.c:237
size_t coap_update_option(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Updates existing first option of given number in the pdu with the new data.
Definition coap_pdu.c:791
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:1603
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:1749
COAP_STATIC_INLINE void coap_pdu_release_lkd(coap_pdu_t *pdu)
coap_pdu_code_t coap_pdu_get_code(const coap_pdu_t *pdu)
Gets the PDU code associated with pdu.
Definition coap_pdu.c:1699
#define COAP_DEFAULT_PORT
Definition coap_pdu.h:39
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition coap_pdu.h:184
#define COAP_RESPONSE_CODE(N)
Definition coap_pdu.h:96
#define COAP_RESPONSE_CLASS(C)
Definition coap_pdu.h:99
coap_proto_t
CoAP protocol types Note: coap_layers_coap[] needs updating if extended.
Definition coap_pdu.h:234
coap_pdu_code_t
Set of codes available for a PDU.
Definition coap_pdu.h:248
#define COAP_MEDIATYPE_TEXT_PLAIN
Definition coap_pdu.h:134
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:413
#define COAPS_DEFAULT_PORT
Definition coap_pdu.h:40
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:947
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:104
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:955
#define COAP_INVALID_MID
Indicates an invalid message id.
Definition coap_pdu.h:187
coap_bin_const_t coap_pdu_get_token(const coap_pdu_t *pdu)
Gets the token associated with pdu.
Definition coap_pdu.c:1723
@ COAP_BOOL_FALSE
Definition coap_pdu.h:295
@ COAP_PROTO_WS
Definition coap_pdu.h:240
@ COAP_PROTO_DTLS
Definition coap_pdu.h:237
@ COAP_PROTO_UDP
Definition coap_pdu.h:236
@ COAP_PROTO_NONE
Definition coap_pdu.h:235
@ COAP_PROTO_TLS
Definition coap_pdu.h:239
@ COAP_PROTO_WSS
Definition coap_pdu.h:241
@ COAP_PROTO_TCP
Definition coap_pdu.h:238
@ COAP_PROTO_LAST
Definition coap_pdu.h:242
@ COAP_MESSAGE_NON
Definition coap_pdu.h:72
@ COAP_MESSAGE_ACK
Definition coap_pdu.h:73
@ COAP_MESSAGE_CON
Definition coap_pdu.h:71
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:92
int coap_verify_proxy_scheme_supported(coap_uri_scheme_t scheme)
Verify that the CoAP Scheme is supported for an ongoing proxy connection.
COAP_API coap_proxy_entry_t * coap_proxy_fwd_add_client_session(coap_session_t *session, const char *use_ip, uint16_t use_port, coap_proxy_server_list_t *server_list)
Add a previously setup proxy-client session to use the proxy logic for forwarding subsequent dynamic ...
COAP_API coap_response_t coap_proxy_forward_response(coap_session_t *session, const coap_pdu_t *received, coap_cache_key_t **cache_key)
Forward the returning response back to the appropriate client.
@ COAP_PROXY_DIRECT_STRIP
Old - do not use - Act as a forward-dynamic proxy, strip out proxy options.
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_DYN_DEFINED
If COAP_PROXY_DYN_DEFINED set, then no new dynamic upstream servers will get automatically added.
Definition coap_proxy.h:82
@ 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...
int coap_session_set_type_client_lkd(coap_session_t *session, int report_changed)
Set the session type to client.
#define COAP_PROTO_NOT_RELIABLE(p)
void coap_session_new_token(coap_session_t *session, size_t *len, uint8_t *data)
Creates a new token for use.
const coap_address_t * coap_session_get_addr_remote(const coap_session_t *session)
Get the remote IP address and port from the session.
void coap_delete_bin_const(coap_bin_const_t *s)
Deletes the given const binary data and releases any memory allocated.
Definition coap_str.c:130
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:81
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:119
void coap_delete_binary(coap_binary_t *s)
Deletes the given coap_binary_t object and releases any memory allocated.
Definition coap_str.c:114
#define coap_binary_equal(binary1, binary2)
Compares the two binary data for equality.
Definition coap_str.h:222
#define coap_string_equal(string1, string2)
Compares the two strings for equality.
Definition coap_str.h:208
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
Definition coap_str.c:50
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:936
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:941
coap_string_t * coap_get_uri_path(const coap_pdu_t *request)
Extract uri_path string from request PDU.
Definition coap_uri.c:1182
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:351
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:374
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:1103
#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.
coap_address_t remote
remote address and port
Definition coap_io.h:58
Multi-purpose address abstraction.
CoAP binary data definition with const data.
Definition coap_str.h:65
size_t length
length of binary data
Definition coap_str.h:66
const uint8_t * s
read-only binary data
Definition coap_str.h:67
CoAP binary data definition.
Definition coap_str.h:57
size_t length
length of binary data
Definition coap_str.h:58
uint8_t * s
binary data
Definition coap_str.h:59
The CoAP stack's global state is stored in a coap_context_t object.
char * client_sni
If not NULL, SNI to use in client TLS setup.
Definition coap_dtls.h:445
char * client_sni
If not NULL, SNI to use in client TLS setup.
Definition coap_dtls.h:376
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:107
coap_proxy_server_t * entry
Set of servers to connect to.
Definition coap_proxy.h:102
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:105
unsigned int idle_timeout_secs
Proxy upstream session idle timeout (0 is no timeout).
Definition coap_proxy.h:110
size_t next_entry
Next server to use (% entry_count).
Definition coap_proxy.h:104
size_t entry_count
The number of servers in entry list.
Definition coap_proxy.h:103
coap_dtls_pki_t * dtls_pki
PKI configuration to use if not NULL.
Definition coap_proxy.h:96
coap_oscore_conf_t * oscore_conf
OSCORE configuration if not NULL.
Definition coap_proxy.h:98
coap_uri_t uri
host and port define the server, scheme method
Definition coap_proxy.h:95
coap_dtls_cpsk_t * dtls_cpsk
PSK configuration to use if not NULL.
Definition coap_proxy.h:97
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_addr_tuple_t addr_info
remote/local address info
coap_proto_t proto
protocol used
coap_response_t last_con_handler_res
The result of calling the response handler of the last CON.
coap_context_t * context
session's context
CoAP string data definition with const data.
Definition coap_str.h:47
const uint8_t * s
read-only string data
Definition coap_str.h:49
size_t length
length of string
Definition coap_str.h:48
CoAP string data definition.
Definition coap_str.h:39
uint8_t * s
string data
Definition coap_str.h:41
Representation of parsed URI.
Definition coap_uri.h:70
enum coap_uri_scheme_t scheme
The parsed scheme specifier.
Definition coap_uri.h:82
uint16_t port
The port in host byte order.
Definition coap_uri.h:72
coap_str_const_t host
The host part of the URI.
Definition coap_uri.h:71