libcoap 4.3.5-develop-19cef11
coap_proxy.c
Go to the documentation of this file.
1/* coap_proxy.c -- helper functions for proxy handling
2 *
3 * Copyright (C) 2024 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
21#if COAP_CLIENT_SUPPORT == 0
22#error For Proxy support, COAP_CLIENT_SUPPORT must be set
23#endif
24#if COAP_SERVER_SUPPORT == 0
25#error For Proxy support, COAP_SERVER_SUPPORT must be set
26#endif
27
28#ifdef _WIN32
29#define strcasecmp _stricmp
30#define strncasecmp _strnicmp
31#endif
32
33int
35 return 1;
36}
37
38void
40 size_t i;
41 size_t j;
42
43 for (i = 0; i < context->proxy_list_count; i++) {
44 for (j = 0; j < context->proxy_list[i].req_count; j++) {
45 coap_delete_pdu(context->proxy_list[i].req_list[j].pdu);
46 coap_delete_cache_key(context->proxy_list[i].req_list[j].cache_key);
47 }
48 coap_free_type(COAP_STRING, context->proxy_list[i].req_list);
49 }
50 coap_free_type(COAP_STRING, context->proxy_list);
51}
52
53/*
54 * return 1 if there is a future expire time, else 0.
55 * update tim_rem with remaining value if return is 1.
56 */
57int
59 coap_tick_t *tim_rem) {
60 size_t i;
61 int ret = 0;
62
63 *tim_rem = -1;
64 for (i = 0; i < context->proxy_list_count; i++) {
65 coap_proxy_list_t *proxy_list = &context->proxy_list[i];
66
67 if (proxy_list->ongoing && proxy_list->idle_timeout_ticks) {
68 if (proxy_list->last_used + proxy_list->idle_timeout_ticks <= now) {
69 /* Drop session to upstream server */
71 proxy_list->ongoing = NULL;
72 } else {
73 if (*tim_rem > proxy_list->last_used + proxy_list->idle_timeout_ticks - now) {
74 *tim_rem = proxy_list->last_used + proxy_list->idle_timeout_ticks - now;
75 }
76 ret = 1;
77 }
78 }
79 }
80 return ret;
81}
82
83static int
84coap_get_uri_proxy_scheme_info(const coap_pdu_t *request,
85 coap_opt_t *opt,
86 coap_uri_t *uri,
87 coap_string_t **uri_path,
88 coap_string_t **uri_query) {
89
90 const char *opt_val = (const char *)coap_opt_value(opt);
91 int opt_len = coap_opt_length(opt);
92 coap_opt_iterator_t opt_iter;
93
94 if (opt_len == 9 &&
95 strncasecmp(opt_val, "coaps+tcp", 9) == 0) {
98 } else if (opt_len == 8 &&
99 strncasecmp(opt_val, "coap+tcp", 8) == 0) {
101 uri->port = COAP_DEFAULT_PORT;
102 } else if (opt_len == 5 &&
103 strncasecmp(opt_val, "coaps", 5) == 0) {
106 } else if (opt_len == 4 &&
107 strncasecmp(opt_val, "coap", 4) == 0) {
109 uri->port = COAP_DEFAULT_PORT;
110 } else if (opt_len == 7 &&
111 strncasecmp(opt_val, "coap+ws", 7) == 0) {
113 uri->port = 80;
114 } else if (opt_len == 8 &&
115 strncasecmp(opt_val, "coaps+ws", 8) == 0) {
117 uri->port = 443;
118 } else {
119 coap_log_warn("Unsupported Proxy Scheme '%*.*s'\n",
120 opt_len, opt_len, opt_val);
121 return 0;
122 }
123
124 opt = coap_check_option(request, COAP_OPTION_URI_HOST, &opt_iter);
125 if (opt) {
126 uri->host.length = coap_opt_length(opt);
127 uri->host.s = coap_opt_value(opt);
128 } else {
129 coap_log_warn("Proxy Scheme requires Uri-Host\n");
130 return 0;
131 }
132 opt = coap_check_option(request, COAP_OPTION_URI_PORT, &opt_iter);
133 if (opt) {
134 uri->port =
136 coap_opt_length(opt));
137 }
138 *uri_path = coap_get_uri_path(request);
139 if (*uri_path) {
140 uri->path.s = (*uri_path)->s;
141 uri->path.length = (*uri_path)->length;
142 } else {
143 uri->path.s = NULL;
144 uri->path.length = 0;
145 }
146 *uri_query = coap_get_query(request);
147 if (*uri_query) {
148 uri->query.s = (*uri_query)->s;
149 uri->query.length = (*uri_query)->length;
150 } else {
151 uri->query.s = NULL;
152 uri->query.length = 0;
153 }
154 return 1;
155}
156
157int
159
160 /* Sanity check that the connection can be forwarded on */
161 switch (scheme) {
164 coap_log_warn("Proxy URI http or https not supported\n");
165 return 0;
167 break;
169 if (!coap_dtls_is_supported()) {
170 coap_log_warn("coaps URI scheme not supported for proxy\n");
171 return 0;
172 }
173 break;
175 if (!coap_tcp_is_supported()) {
176 coap_log_warn("coap+tcp URI scheme not supported for proxy\n");
177 return 0;
178 }
179 break;
181 if (!coap_tls_is_supported()) {
182 coap_log_warn("coaps+tcp URI scheme not supported for proxy\n");
183 return 0;
184 }
185 break;
187 if (!coap_ws_is_supported()) {
188 coap_log_warn("coap+ws URI scheme not supported for proxy\n");
189 return 0;
190 }
191 break;
193 if (!coap_wss_is_supported()) {
194 coap_log_warn("coaps+ws URI scheme not supported for proxy\n");
195 return 0;
196 }
197 break;
199 default:
200 coap_log_warn("%d URI scheme not supported\n", scheme);
201 return 0;
202 }
203 return 1;
204}
205
206static coap_proxy_list_t *
207coap_proxy_get_session(coap_session_t *session, const coap_pdu_t *request,
208 coap_pdu_t *response,
209 coap_proxy_server_list_t *server_list,
210 coap_proxy_server_t *server_use) {
211 size_t i;
212 coap_proxy_list_t *new_proxy_list;
213 coap_proxy_list_t *proxy_list = session->context->proxy_list;
214 size_t proxy_list_count = session->context->proxy_list_count;
215
216 coap_opt_iterator_t opt_iter;
217 coap_opt_t *proxy_scheme;
218 coap_opt_t *proxy_uri;
219 coap_string_t *uri_path = NULL;
220 coap_string_t *uri_query = NULL;
221
222 /* Round robin the defined next server list (which usually is just one */
223 server_list->next_entry++;
224 if (server_list->next_entry >= server_list->entry_count)
225 server_list->next_entry = 0;
226
227 memcpy(server_use, &server_list->entry[server_list->next_entry], sizeof(*server_use));
228
229 switch (server_list->type) {
233 /* Nothing else needs to be done */
234 break;
238 /* Need to get actual server from CoAP options */
239 /*
240 * See if Proxy-Scheme
241 */
242 proxy_scheme = coap_check_option(request, COAP_OPTION_PROXY_SCHEME, &opt_iter);
243 if (proxy_scheme) {
244 if (!coap_get_uri_proxy_scheme_info(request, proxy_scheme, &server_use->uri, &uri_path,
245 &uri_query)) {
246 response->code = COAP_RESPONSE_CODE(505);
247 return NULL;
248 }
249 }
250 /*
251 * See if Proxy-Uri
252 */
253 proxy_uri = coap_check_option(request, COAP_OPTION_PROXY_URI, &opt_iter);
254 if (proxy_uri) {
255 coap_log_info("Proxy URI '%.*s'\n",
256 (int)coap_opt_length(proxy_uri),
257 (const char *)coap_opt_value(proxy_uri));
259 coap_opt_length(proxy_uri),
260 &server_use->uri) < 0) {
261 /* Need to return a 5.05 RFC7252 Section 5.7.2 */
262 coap_log_warn("Proxy URI not decodable\n");
263 response->code = COAP_RESPONSE_CODE(505);
264 return NULL;
265 }
266 }
267
268 if (!(proxy_scheme || proxy_uri)) {
269 response->code = COAP_RESPONSE_CODE(404);
270 return NULL;
271 }
272
273 if (server_use->uri.host.length == 0) {
274 /* Ongoing connection not well formed */
275 response->code = COAP_RESPONSE_CODE(505);
276 return NULL;
277 }
278
280 response->code = COAP_RESPONSE_CODE(505);
281 return NULL;
282 }
283 break;
284 default:
285 assert(0);
286 return NULL;
287 }
288
289 /* See if we are already connected to the Server */
290 for (i = 0; i < proxy_list_count; i++) {
291 if (coap_string_equal(&proxy_list[i].uri.host, &server_use->uri.host) &&
292 proxy_list[i].uri.port == server_use->uri.port &&
293 proxy_list[i].uri.scheme == server_use->uri.scheme) {
294 if (!server_list->track_client_session) {
295 coap_ticks(&proxy_list[i].last_used);
296 return &proxy_list[i];
297 } else {
298 if (proxy_list[i].incoming == session) {
299 coap_ticks(&proxy_list[i].last_used);
300 return &proxy_list[i];
301 }
302 }
303 }
304 }
305
306 /* Need to create a new forwarding mapping */
307 new_proxy_list = coap_realloc_type(COAP_STRING, proxy_list, (i+1)*sizeof(proxy_list[0]));
308
309 if (new_proxy_list == NULL) {
310 response->code = COAP_RESPONSE_CODE(500);
311 return NULL;
312 }
313 session->context->proxy_list = proxy_list = new_proxy_list;
314 memset(&proxy_list[i], 0, sizeof(proxy_list[i]));
315
316 proxy_list[i].uri = server_use->uri;
317 if (server_list->track_client_session) {
318 proxy_list[i].incoming = session;
319 }
320 session->context->proxy_list_count++;
321 proxy_list[i].idle_timeout_ticks = server_list->idle_timeout_secs * COAP_TICKS_PER_SECOND;
322 coap_ticks(&proxy_list[i].last_used);
323 return &proxy_list[i];
324}
325
326void
327coap_proxy_remove_association(coap_session_t *session, int send_failure) {
328
329 size_t i;
330 size_t j;
331 coap_proxy_list_t *proxy_list = session->context->proxy_list;
332 size_t proxy_list_count = session->context->proxy_list_count;
333
334 for (i = 0; i < proxy_list_count; i++) {
335 /* Check for incoming match */
336 for (j = 0; j < proxy_list[i].req_count; j++) {
337 if (proxy_list[i].req_list[j].incoming == session) {
338 coap_delete_pdu(proxy_list[i].req_list[j].pdu);
339 coap_delete_bin_const(proxy_list[i].req_list[j].token_used);
340 coap_delete_cache_key(proxy_list[i].req_list[j].cache_key);
341 if (proxy_list[i].req_count-j > 1) {
342 memmove(&proxy_list[i].req_list[j], &proxy_list[i].req_list[j+1],
343 (proxy_list[i].req_count-j-1) * sizeof(proxy_list[i].req_list[0]));
344 }
345 proxy_list[i].req_count--;
346 break;
347 }
348 }
349 if (proxy_list[i].incoming == session) {
350 /* Only if there is a one-to-one tracking */
351 coap_session_release_lkd(proxy_list[i].ongoing);
352 break;
353 }
354
355 /* Check for outgoing match */
356 if (proxy_list[i].ongoing == session) {
357 coap_session_t *ongoing;
358
359 for (j = 0; j < proxy_list[i].req_count; j++) {
360 if (send_failure) {
361 coap_pdu_t *response;
362 coap_bin_const_t l_token;
363
364 /* Need to send back a gateway failure */
365 response = coap_pdu_init(proxy_list[i].req_list[j].pdu->type,
367 coap_new_message_id_lkd(proxy_list[i].incoming),
368 coap_session_max_pdu_size_lkd(proxy_list[i].incoming));
369 if (!response) {
370 coap_log_info("PDU creation issue\n");
371 goto cleanup;
372 }
373
374 l_token = coap_pdu_get_token(proxy_list[i].req_list[j].pdu);
375 if (!coap_add_token(response, l_token.length,
376 l_token.s)) {
377 coap_log_debug("Cannot add token to incoming proxy response PDU\n");
378 }
379
380 if (coap_send_lkd(proxy_list[i].incoming, response) == COAP_INVALID_MID) {
381 coap_log_info("Failed to send PDU with 5.02 gateway issue\n");
382 }
383cleanup:
384 coap_delete_pdu(proxy_list[i].req_list[j].pdu);
385 coap_delete_bin_const(proxy_list[i].req_list[j].token_used);
386 coap_delete_cache_key(proxy_list[i].req_list[j].cache_key);
387 }
388 }
389 ongoing = proxy_list[i].ongoing;
390 coap_free_type(COAP_STRING, proxy_list[i].req_list);
391 if (proxy_list_count-i > 1) {
392 memmove(&proxy_list[i],
393 &proxy_list[i+1],
394 (proxy_list_count-i-1) * sizeof(proxy_list[0]));
395 }
396 session->context->proxy_list_count--;
398 break;
399 }
400 }
401}
402
403static coap_proxy_list_t *
404coap_proxy_get_ongoing_session(coap_session_t *session,
405 const coap_pdu_t *request,
406 coap_pdu_t *response,
407 coap_proxy_server_list_t *server_list,
408 coap_proxy_server_t *server_use) {
409
410 coap_address_t dst;
411 coap_proto_t proto;
412 coap_addr_info_t *info_list = NULL;
413 coap_proxy_list_t *proxy_entry;
414 coap_context_t *context = session->context;
415 static char client_sni[256];
416
417 proxy_entry = coap_proxy_get_session(session, request, response, server_list, server_use);
418 if (!proxy_entry) {
419 /* Response code should be set */
420 return NULL;
421 }
422
423 if (!proxy_entry->ongoing) {
424 /* Need to create a new session */
425
426 /* resolve destination address where data should be sent */
427 info_list = coap_resolve_address_info(&server_use->uri.host,
428 server_use->uri.port,
429 server_use->uri.port,
430 server_use->uri.port,
431 server_use->uri.port,
432 0,
433 1 << server_use->uri.scheme,
435
436 if (info_list == NULL) {
437 response->code = COAP_RESPONSE_CODE(502);
439 return NULL;
440 }
441 proto = info_list->proto;
442 memcpy(&dst, &info_list->addr, sizeof(dst));
443 coap_free_address_info(info_list);
444
445 snprintf(client_sni, sizeof(client_sni), "%*.*s", (int)server_use->uri.host.length,
446 (int)server_use->uri.host.length, server_use->uri.host.s);
447
448 switch (server_use->uri.scheme) {
452#if COAP_OSCORE_SUPPORT
453 if (server_use->oscore_conf) {
454 proxy_entry->ongoing =
455 coap_new_client_session_oscore_lkd(context, NULL, &dst,
456 proto, server_use->oscore_conf);
457 } else {
458#endif /* COAP_OSCORE_SUPPORT */
459 proxy_entry->ongoing =
460 coap_new_client_session_lkd(context, NULL, &dst, proto);
461#if COAP_OSCORE_SUPPORT
462 }
463#endif /* COAP_OSCORE_SUPPORT */
464 break;
468#if COAP_OSCORE_SUPPORT
469 if (server_use->oscore_conf) {
470 if (server_use->dtls_pki) {
471 server_use->dtls_pki->client_sni = client_sni;
472 proxy_entry->ongoing =
473 coap_new_client_session_oscore_pki_lkd(context, NULL, &dst,
474 proto, server_use->dtls_pki, server_use->oscore_conf);
475 } else if (server_use->dtls_cpsk) {
476 server_use->dtls_cpsk->client_sni = client_sni;
477 proxy_entry->ongoing =
478 coap_new_client_session_oscore_psk_lkd(context, NULL, &dst,
479 proto, server_use->dtls_cpsk, server_use->oscore_conf);
480 } else {
481 coap_log_warn("Proxy: (D)TLS not configured for secure session\n");
482 }
483 } else {
484#endif /* COAP_OSCORE_SUPPORT */
485 /* Not doing OSCORE */
486 if (server_use->dtls_pki) {
487 server_use->dtls_pki->client_sni = client_sni;
488 proxy_entry->ongoing =
489 coap_new_client_session_pki_lkd(context, NULL, &dst,
490 proto, server_use->dtls_pki);
491 } else if (server_use->dtls_cpsk) {
492 server_use->dtls_cpsk->client_sni = client_sni;
493 proxy_entry->ongoing =
494 coap_new_client_session_psk2_lkd(context, NULL, &dst,
495 proto, server_use->dtls_cpsk);
496 } else {
497 coap_log_warn("Proxy: (D)TLS not configured for secure session\n");
498 }
499#if COAP_OSCORE_SUPPORT
500 }
501#endif /* COAP_OSCORE_SUPPORT */
502 break;
506 default:
507 assert(0);
508 break;
509 }
510 if (proxy_entry->ongoing == NULL) {
511 response->code = COAP_RESPONSE_CODE(505);
513 return NULL;
514 }
515 }
516
517 return proxy_entry;
518}
519
520static void
521coap_proxy_release_body_data(coap_session_t *session COAP_UNUSED,
522 void *app_ptr) {
523 coap_delete_binary(app_ptr);
524}
525
526int COAP_API
528 const coap_pdu_t *request,
529 coap_pdu_t *response,
530 coap_resource_t *resource,
531 coap_cache_key_t *cache_key,
532 coap_proxy_server_list_t *server_list) {
533 int ret;
534
535 coap_lock_lock(session->context, return 0);
536 ret = coap_proxy_forward_request_lkd(session,
537 request,
538 response,
539 resource,
540 cache_key,
541 server_list);
542 coap_lock_unlock(session->context);
543 return ret;
544}
545
546int
548 const coap_pdu_t *request,
549 coap_pdu_t *response,
550 coap_resource_t *resource,
551 coap_cache_key_t *cache_key,
552 coap_proxy_server_list_t *server_list) {
553 coap_proxy_list_t *proxy_entry;
554 size_t size;
555 size_t offset;
556 size_t total;
557 coap_binary_t *body_data = NULL;
558 const uint8_t *data;
559 coap_pdu_t *pdu = NULL;
560 coap_bin_const_t r_token = coap_pdu_get_token(request);
561 uint8_t token[8];
562 size_t token_len;
563 coap_proxy_req_t *new_req_list;
564 coap_optlist_t *optlist = NULL;
565 coap_opt_t *option;
566 coap_opt_iterator_t opt_iter;
567 coap_proxy_server_t server_use;
568
569 /* Set up ongoing session (if not already done) */
570
571 proxy_entry = coap_proxy_get_ongoing_session(session, request, response,
572 server_list, &server_use);
573 if (!proxy_entry)
574 /* response code already set */
575 return 0;
576
577 /* Need to save the request pdu entry */
578 new_req_list = coap_realloc_type(COAP_STRING, proxy_entry->req_list,
579 (proxy_entry->req_count + 1)*sizeof(coap_proxy_req_t));
580
581 if (new_req_list == NULL) {
582 goto failed;
583 }
584 proxy_entry->req_list = new_req_list;
585 /* Get a new token for ongoing session */
586 coap_session_new_token(proxy_entry->ongoing, &token_len, token);
587 new_req_list[proxy_entry->req_count].token_used = coap_new_bin_const(token, token_len);
588 if (new_req_list[proxy_entry->req_count].token_used == NULL) {
589 goto failed;
590 }
591 new_req_list[proxy_entry->req_count].pdu = coap_pdu_duplicate_lkd(request, session,
592 r_token.length, r_token.s, NULL);
593 if (new_req_list[proxy_entry->req_count].pdu == NULL) {
594 coap_delete_bin_const(new_req_list[proxy_entry->req_count].token_used);
595 new_req_list[proxy_entry->req_count].token_used = NULL;
596 goto failed;
597 }
598 new_req_list[proxy_entry->req_count].resource = resource;
599 new_req_list[proxy_entry->req_count].incoming = session;
600 new_req_list[proxy_entry->req_count].cache_key = cache_key;
601 proxy_entry->req_count++;
602
603 switch (server_list->type) {
607 /*
608 * Need to replace Proxy-Uri with Uri-Host (and Uri-Port)
609 * or strip out Proxy-Scheme.
610 */
611
612 /*
613 * Build up the ongoing PDU that we are going to send
614 */
615 pdu = coap_pdu_init(request->type, request->code,
616 coap_new_message_id_lkd(proxy_entry->ongoing),
618 if (!pdu) {
619 goto failed;
620 }
621
622 if (!coap_add_token(pdu, token_len, token)) {
623 goto failed;
624 }
625
626 if (!coap_uri_into_optlist(&server_use.uri,
627 &proxy_entry->ongoing->addr_info.remote,
628 &optlist, 1)) {
629 coap_log_err("Failed to create options for URI\n");
630 goto failed;
631 }
632
633 /* Copy the remaining options across */
634 coap_option_iterator_init(request, &opt_iter, COAP_OPT_ALL);
635 while ((option = coap_option_next(&opt_iter))) {
636 switch (opt_iter.number) {
638 break;
640 break;
645 /* These are not passed on */
646 break;
647 default:
648 coap_insert_optlist(&optlist,
649 coap_new_optlist(opt_iter.number,
650 coap_opt_length(option),
651 coap_opt_value(option)));
652 break;
653 }
654 }
655
656 /* Update pdu with options */
657 coap_add_optlist_pdu(pdu, &optlist);
658 coap_delete_optlist(optlist);
659 break;
663 default:
664 /*
665 * Duplicate request PDU for onward transmission (with new token).
666 */
667 pdu = coap_pdu_duplicate_lkd(request, proxy_entry->ongoing, token_len, token, NULL);
668 if (!pdu) {
669 coap_log_debug("proxy: PDU generation error\n");
670 goto failed;
671 }
672 break;
673 }
674
675 if (coap_get_data_large(request, &size, &data, &offset, &total)) {
676 /* COAP_BLOCK_SINGLE_BODY is set, so single body should be given */
677 assert(size == total);
678 /*
679 * Need to take a copy of the data as request PDU may go away before
680 * all data is transmitted.
681 */
682 body_data = coap_new_binary(total);
683 if (!body_data) {
684 coap_log_debug("proxy: body build memory error\n");
685 goto failed;
686 }
687 memcpy(body_data->s, data, size);
688 if (!coap_add_data_large_request_lkd(proxy_entry->ongoing, pdu, total, data,
689 coap_proxy_release_body_data, body_data)) {
690 coap_log_debug("proxy: add data error\n");
691 goto failed;
692 }
693 }
694
695 if (coap_send_lkd(proxy_entry->ongoing, pdu) == COAP_INVALID_MID) {
696 pdu = NULL;
697 coap_log_debug("proxy: upstream PDU send error\n");
698 goto failed;
699 }
700
701 /*
702 * Do not update the response code (hence empty ACK) as will be sending
703 * separate response when response comes back from upstream server
704 */
705
706 return 1;
707
708failed:
709 response->code = COAP_RESPONSE_CODE(500);
710 coap_delete_pdu(pdu);
711 return 0;
712}
713
716 const coap_pdu_t *received,
717 coap_cache_key_t **cache_key) {
718 int ret;
719
720 coap_lock_lock(session->context, return 0);
722 received,
723 cache_key);
724 coap_lock_unlock(session->context);
725 return ret;
726}
727
730 const coap_pdu_t *received,
731 coap_cache_key_t **cache_key) {
732 coap_pdu_t *pdu = NULL;
733 coap_session_t *incoming = NULL;
734 size_t i;
735 size_t j = 0;
736 size_t size;
737 const uint8_t *data;
738 coap_optlist_t *optlist = NULL;
739 coap_opt_t *option;
740 coap_opt_iterator_t opt_iter;
741 size_t offset;
742 size_t total;
743 coap_proxy_list_t *proxy_entry = NULL;
744 uint16_t media_type = COAP_MEDIATYPE_TEXT_PLAIN;
745 int maxage = -1;
746 uint64_t etag = 0;
747 coap_pdu_code_t rcv_code = coap_pdu_get_code(received);
748 coap_bin_const_t rcv_token = coap_pdu_get_token(received);
749 coap_bin_const_t req_token;
750 coap_binary_t *body_data = NULL;
751 coap_pdu_t *req_pdu;
752 coap_proxy_list_t *proxy_list = session->context->proxy_list;
753 size_t proxy_list_count = session->context->proxy_list_count;
754 coap_resource_t *resource;
755 struct coap_proxy_req_t *proxy_req = NULL;
756
757 for (i = 0; i < proxy_list_count; i++) {
758 proxy_entry = &proxy_list[i];
759 for (j = 0; j < proxy_entry->req_count; j++) {
760 if (coap_binary_equal(&rcv_token, proxy_entry->req_list[j].token_used)) {
761 proxy_req = &proxy_entry->req_list[j];
762 break;
763 }
764 }
765 if (j != proxy_entry->req_count) {
766 break;
767 }
768 }
769 if (i == proxy_list_count) {
770 coap_log_warn("Unknown proxy ongoing session response received\n");
771 return COAP_RESPONSE_OK;
772 }
773
774 req_pdu = proxy_req->pdu;
775 req_token = coap_pdu_get_token(req_pdu);
776 resource = proxy_req->resource;
777 incoming = proxy_req->incoming;
778
779 coap_log_debug("** process upstream incoming %d.%02d response:\n",
780 COAP_RESPONSE_CLASS(rcv_code), rcv_code & 0x1F);
781
782 if (coap_get_data_large(received, &size, &data, &offset, &total)) {
783 /* COAP_BLOCK_SINGLE_BODY is set, so single body should be given */
784 assert(size == total);
785 body_data = coap_new_binary(total);
786 if (!body_data) {
787 coap_log_debug("body build memory error\n");
788 goto remove_match;
789 }
790 memcpy(body_data->s, data, size);
791 data = body_data->s;
792 }
793
794 /*
795 * Build up the ongoing PDU that we are going to send to proxy originator
796 * as separate response
797 */
798 pdu = coap_pdu_init(req_pdu->type, rcv_code,
801 if (!pdu) {
802 coap_log_debug("Failed to create ongoing proxy response PDU\n");
803 goto remove_match;
804 }
805
806 if (!coap_add_token(pdu, req_token.length, req_token.s)) {
807 coap_log_debug("cannot add token to ongoing proxy response PDU\n");
808 }
809
810 /*
811 * Copy the options across, skipping those needed for
812 * coap_add_data_response_large()
813 */
814 coap_option_iterator_init(received, &opt_iter, COAP_OPT_ALL);
815 while ((option = coap_option_next(&opt_iter))) {
816 switch (opt_iter.number) {
818 media_type = coap_decode_var_bytes(coap_opt_value(option),
819 coap_opt_length(option));
820 break;
822 maxage = coap_decode_var_bytes(coap_opt_value(option),
823 coap_opt_length(option));
824 break;
825 case COAP_OPTION_ETAG:
827 coap_opt_length(option));
828 break;
832 break;
833 default:
834 coap_insert_optlist(&optlist,
835 coap_new_optlist(opt_iter.number,
836 coap_opt_length(option),
837 coap_opt_value(option)));
838 break;
839 }
840 }
841 coap_add_optlist_pdu(pdu, &optlist);
842 coap_delete_optlist(optlist);
843
844 if (size > 0) {
845 coap_string_t *l_query = coap_get_query(req_pdu);
846
848 l_query,
849 media_type, maxage, etag, size, data,
850 coap_proxy_release_body_data,
851 body_data);
852 body_data = NULL;
853 coap_delete_string(l_query);
854 }
855
856 if (cache_key)
857 *cache_key = proxy_req->cache_key;
858
860
861remove_match:
862 option = coap_check_option(received, COAP_OPTION_OBSERVE, &opt_iter);
863 /* Need to remove matching token entry (apart from on Observe response) */
864 if (option == NULL && proxy_entry->req_count) {
865 coap_delete_pdu(proxy_entry->req_list[j].pdu);
867 /* Do not delete cache key here - caller's responsibility */
868 proxy_entry->req_count--;
869 if (proxy_entry->req_count-j > 0) {
870 memmove(&proxy_entry->req_list[j], &proxy_entry->req_list[j+1],
871 (proxy_entry->req_count-j) * sizeof(proxy_entry->req_list[0]));
872 }
873 }
874 coap_delete_binary(body_data);
875 return COAP_RESPONSE_OK;
876}
877
878#else /* ! COAP_PROXY_SUPPORT */
879
880int
882 return 0;
883}
884
885COAP_API int
887 const coap_pdu_t *request,
888 coap_pdu_t *response,
891 coap_proxy_server_list_t *server_list) {
892 (void)session;
893 (void)request;
894 (void)resource;
895 (void)cache_key;
896 (void)server_list;
897 response->code = COAP_RESPONSE_CODE(500);
898 return 0;
899}
900
903 const coap_pdu_t *received,
905 (void)session;
906 (void)received;
907 (void)cache_key;
908 return COAP_RESPONSE_OK;
909}
910
911int
913 (void)scheme;
914 return 0;
915}
916#endif /* ! COAP_PROXY_SUPPORT */
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().
Definition: coap_address.c:790
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...
Definition: coap_address.c:486
@ COAP_RESOLVE_TYPE_REMOTE
remote side of session
Definition: coap_address.h:207
Library specific build wrapper for coap_internal.h.
#define COAP_API
@ COAP_STRING
Definition: coap_mem.h:39
void * coap_realloc_type(coap_memory_tag_t type, void *p, size_t size)
Reallocates a chunk p of bytes created by coap_malloc_type() or coap_realloc_type() and returns a poi...
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
Definition: coap_option.h:26
coap_uri_scheme_t
The scheme specifiers.
Definition: coap_uri.h:28
@ COAP_URI_SCHEME_COAPS_WS
Definition: coap_uri.h:36
@ COAP_URI_SCHEME_COAPS_TCP
Definition: coap_uri.h:32
@ COAP_URI_SCHEME_COAPS
Definition: coap_uri.h:30
@ COAP_URI_SCHEME_COAP_TCP
Definition: coap_uri.h:31
@ COAP_URI_SCHEME_COAP_WS
Definition: coap_uri.h:35
@ COAP_URI_SCHEME_HTTPS
Definition: coap_uri.h:34
@ COAP_URI_SCHEME_COAP
Definition: coap_uri.h:29
@ COAP_URI_SCHEME_LAST
Definition: coap_uri.h:37
@ COAP_URI_SCHEME_HTTP
Definition: coap_uri.h:33
int coap_proxy_check_timeouts(coap_context_t *context, coap_tick_t now, coap_tick_t *tim_rem)
Idle timeout inactive proxy sessions as well as return in tim_rem the time to remaining to timeout th...
coap_response_t coap_proxy_forward_response_lkd(coap_session_t *session, const coap_pdu_t *received, coap_cache_key_t **cache_key)
Forward the returning response back to the appropriate client.
void coap_proxy_cleanup(coap_context_t *context)
Close down proxy tracking, releasing any memory used.
void coap_proxy_remove_association(coap_session_t *session, int send_failure)
int coap_proxy_forward_request_lkd(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.
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:1356
int coap_add_data_large_response_lkd(coap_resource_t *resource, coap_session_t *session, const coap_pdu_t *request, coap_pdu_t *response, const coap_string_t *query, uint16_t media_type, int maxage, uint64_t etag, size_t length, const uint8_t *data, coap_release_large_data_t release_func, void *app_ptr)
Associates given data with the response pdu that is passed as fourth parameter.
int coap_add_data_large_request_lkd(coap_session_t *session, coap_pdu_t *pdu, size_t length, const uint8_t *data, coap_release_large_data_t release_func, void *app_ptr)
Associates given data with the pdu that is passed as second parameter.
void coap_delete_cache_key(coap_cache_key_t *cache_key)
Delete the cache-key.
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.h:143
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition: coap_time.h:158
uint16_t coap_new_message_id_lkd(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
coap_response_t
Definition: coap_net.h:48
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
@ COAP_RESPONSE_OK
Response is fine.
Definition: coap_net.h:50
unsigned int coap_decode_var_bytes(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
Definition: coap_encode.c:38
uint64_t coap_decode_var_bytes8(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
Definition: coap_encode.c:67
#define coap_lock_unlock(c)
Dummy for no thread-safe code.
#define coap_lock_lock(c, failed)
Dummy for no thread-safe code.
#define coap_log_debug(...)
Definition: coap_debug.h:120
#define coap_log_info(...)
Definition: coap_debug.h:108
#define coap_log_warn(...)
Definition: coap_debug.h:102
#define coap_log_err(...)
Definition: coap_debug.h:96
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
Definition: coap_option.c:153
coap_optlist_t * coap_new_optlist(uint16_t number, size_t length, const uint8_t *data)
Create a new optlist entry.
Definition: coap_option.c:511
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
Definition: coap_option.c:212
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.
Definition: coap_option.c:117
void coap_delete_optlist(coap_optlist_t *queue)
Removes all entries from the optlist_chain, freeing off their memory usage.
Definition: coap_option.c:592
#define COAP_OPT_ALL
Pre-defined filter that includes all options.
Definition: coap_option.h:108
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 ...
Definition: coap_option.c:551
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.
Definition: coap_option.c:199
int coap_insert_optlist(coap_optlist_t **head, coap_optlist_t *node)
Adds optlist to the given optlist_chain.
Definition: coap_option.c:571
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
Definition: coap_option.c:249
coap_session_t * coap_new_client_session_oscore_psk_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_cpsk_t *psk_data, coap_oscore_conf_t *oscore_conf)
Creates a new client session to the designated server with PSK credentials as well as protecting the ...
coap_session_t * coap_new_client_session_oscore_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_oscore_conf_t *oscore_conf)
Creates a new client session to the designated server, protecting the data using OSCORE.
coap_session_t * coap_new_client_session_oscore_pki_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_pki_t *pki_data, coap_oscore_conf_t *oscore_conf)
Creates a new client session to the designated server with PKI credentials as well as protecting the ...
coap_pdu_t * coap_pdu_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:223
#define COAP_OPTION_URI_HOST
Definition: coap_pdu.h:120
coap_pdu_code_t coap_pdu_get_code(const coap_pdu_t *pdu)
Gets the PDU code associated with pdu.
Definition: coap_pdu.c:1574
#define COAP_OPTION_BLOCK2
Definition: coap_pdu.h:137
#define COAP_OPTION_CONTENT_FORMAT
Definition: coap_pdu.h:128
#define COAP_OPTION_SIZE2
Definition: coap_pdu.h:139
#define COAP_OPTION_BLOCK1
Definition: coap_pdu.h:138
#define COAP_OPTION_Q_BLOCK1
Definition: coap_pdu.h:135
#define COAP_OPTION_PROXY_SCHEME
Definition: coap_pdu.h:142
#define COAP_DEFAULT_PORT
Definition: coap_pdu.h:37
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
Definition: coap_pdu.c:183
#define COAP_RESPONSE_CODE(N)
Definition: coap_pdu.h:160
#define COAP_RESPONSE_CLASS(C)
Definition: coap_pdu.h:163
coap_proto_t
CoAP protocol types.
Definition: coap_pdu.h:312
coap_pdu_code_t
Set of codes available for a PDU.
Definition: coap_pdu.h:326
#define COAP_MEDIATYPE_TEXT_PLAIN
Definition: coap_pdu.h:213
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:349
#define COAP_OPTION_Q_BLOCK2
Definition: coap_pdu.h:140
#define COAPS_DEFAULT_PORT
Definition: coap_pdu.h:38
#define COAP_OPTION_URI_PORT
Definition: coap_pdu.h:124
coap_pdu_t * coap_pdu_init(coap_pdu_type_t type, coap_pdu_code_t code, coap_mid_t mid, size_t size)
Creates a new CoAP PDU with at least enough storage space for the given size maximum message size.
Definition: coap_pdu.c:99
int coap_get_data_large(const coap_pdu_t *pdu, size_t *len, const uint8_t **data, size_t *offset, size_t *total)
Retrieves the data from a PDU, with support for large bodies of data that spans multiple PDUs.
Definition: coap_pdu.c:873
#define COAP_INVALID_MID
Indicates an invalid message id.
Definition: coap_pdu.h:266
#define COAP_OPTION_MAXAGE
Definition: coap_pdu.h:131
#define COAP_OPTION_ETAG
Definition: coap_pdu.h:121
#define COAP_OPTION_PROXY_URI
Definition: coap_pdu.h:141
#define COAP_OPTION_OBSERVE
Definition: coap_pdu.h:123
coap_bin_const_t coap_pdu_get_token(const coap_pdu_t *pdu)
Gets the token associated with pdu.
Definition: coap_pdu.c:1598
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.
Definition: coap_proxy.c:902
int coap_verify_proxy_scheme_supported(coap_uri_scheme_t scheme)
Verify that the CoAP Scheme is supported for an ongoing proxy connection.
Definition: coap_proxy.c:912
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.
Definition: coap_proxy.c:886
@ COAP_PROXY_DIRECT_STRIP
Act as a direct proxy, strip out proxy options.
Definition: coap_proxy.h:33
@ COAP_PROXY_REVERSE_STRIP
Act as a reverse proxy, strip out proxy options.
Definition: coap_proxy.h:29
@ COAP_PROXY_REVERSE
Act as a reverse proxy.
Definition: coap_proxy.h:28
@ COAP_PROXY_DIRECT
Act as a direct proxy.
Definition: coap_proxy.h:32
@ COAP_PROXY_FORWARD_STRIP
Act as a forward proxy, strip out proxy options.
Definition: coap_proxy.h:31
@ COAP_PROXY_FORWARD
Act as a forward proxy.
Definition: coap_proxy.h:30
coap_session_t * coap_new_client_session_psk2_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_cpsk_t *setup_data)
Creates a new client session to the designated server with PSK credentials.
coap_session_t * coap_new_client_session_pki_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_pki_t *setup_data)
Creates a new client session to the designated server with PKI credentials.
coap_session_t * coap_new_client_session_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto)
Creates a new client session to the designated server.
size_t coap_session_max_pdu_size_lkd(const coap_session_t *session)
Get maximum acceptable PDU size.
Definition: coap_session.c:655
void coap_session_release_lkd(coap_session_t *session)
Decrement reference counter on a session.
Definition: coap_session.c:376
void coap_session_new_token(coap_session_t *session, size_t *len, uint8_t *data)
Creates a new token for use.
void coap_delete_bin_const(coap_bin_const_t *s)
Deletes the given const binary data and releases any memory allocated.
Definition: coap_str.c:120
coap_binary_t * coap_new_binary(size_t size)
Returns a new binary object with at least size bytes storage allocated.
Definition: coap_str.c:77
coap_bin_const_t * coap_new_bin_const(const uint8_t *data, size_t size)
Take the specified byte array (text) and create a coap_bin_const_t * Returns a new const binary objec...
Definition: coap_str.c:110
void coap_delete_binary(coap_binary_t *s)
Deletes the given coap_binary_t object and releases any memory allocated.
Definition: coap_str.c:105
#define coap_binary_equal(binary1, binary2)
Compares the two binary data for equality.
Definition: coap_str.h:211
#define coap_string_equal(string1, string2)
Compares the two strings for equality.
Definition: coap_str.h:197
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
Definition: coap_str.c:46
int coap_tcp_is_supported(void)
Check whether TCP is available.
Definition: coap_tcp.c:20
int coap_tls_is_supported(void)
Check whether TLS is available.
Definition: coap_notls.c:41
int coap_ws_is_supported(void)
Check whether WebSockets is available.
Definition: coap_ws.c:933
int coap_dtls_is_supported(void)
Check whether DTLS is available.
Definition: coap_notls.c:36
int coap_proxy_is_supported(void)
Check whether Proxy code is available.
Definition: coap_proxy.c:881
int coap_wss_is_supported(void)
Check whether Secure WebSockets is available.
Definition: coap_ws.c:938
coap_string_t * coap_get_uri_path(const coap_pdu_t *request)
Extract uri_path string from request PDU.
Definition: coap_uri.c:985
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:276
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:299
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:934
#define COAP_UNUSED
Definition: libcoap.h:70
Resolved addresses information.
Definition: coap_address.h:179
coap_proto_t proto
CoAP protocol to use.
Definition: coap_address.h:182
coap_address_t addr
The address to connect / bind to.
Definition: coap_address.h:183
coap_address_t remote
remote address and port
Definition: coap_io.h:56
Multi-purpose address abstraction.
Definition: coap_address.h:148
CoAP binary data definition with const data.
Definition: coap_str.h:64
size_t length
length of binary data
Definition: coap_str.h:65
const uint8_t * s
read-only binary data
Definition: coap_str.h:66
CoAP binary data definition.
Definition: coap_str.h:56
uint8_t * s
binary data
Definition: coap_str.h:58
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:437
char * client_sni
If not NULL, SNI to use in client TLS setup.
Definition: coap_dtls.h:368
Iterator to run through PDU options.
Definition: coap_option.h:168
coap_option_num_t number
decoded option number
Definition: coap_option.h:170
Representation of chained list of CoAP options to install.
Definition: coap_option.h:325
structure for CoAP PDUs
coap_pdu_code_t code
request method (value 1–31) or response code (value 64-255)
coap_pdu_type_t type
message type
coap_tick_t idle_timeout_ticks
Idle timeout (0 == no timeout)
coap_session_t * incoming
Incoming session (used if client tracking(.
coap_proxy_req_t * req_list
Incoming list of request info.
coap_tick_t last_used
Last time entry was used.
coap_uri_t uri
URI info for connection.
coap_session_t * ongoing
Ongoing session.
size_t req_count
Count of incoming request info.
coap_bin_const_t * token_used
coap_cache_key_t * cache_key
coap_session_t * incoming
coap_resource_t * resource
int track_client_session
If 1, track individual connections to upstream server, else 0.
Definition: coap_proxy.h:48
coap_proxy_server_t * entry
Set of servers to connect to.
Definition: coap_proxy.h:44
coap_proxy_t type
The proxy type.
Definition: coap_proxy.h:47
unsigned int idle_timeout_secs
Proxy session idle timeout (0 is no timeout)
Definition: coap_proxy.h:50
size_t next_entry
Next server to us (% entry_count)
Definition: coap_proxy.h:46
size_t entry_count
The number of servers.
Definition: coap_proxy.h:45
coap_dtls_pki_t * dtls_pki
PKI configuration to use if not NULL.
Definition: coap_proxy.h:38
coap_oscore_conf_t * oscore_conf
OSCORE configuration if not NULL.
Definition: coap_proxy.h:40
coap_uri_t uri
host and port define the server, scheme method
Definition: coap_proxy.h:37
coap_dtls_cpsk_t * dtls_cpsk
PSK configuration to use if not NULL.
Definition: coap_proxy.h:39
Abstraction of resource that can be attached to coap_context_t.
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
coap_addr_tuple_t addr_info
remote/local address info
coap_context_t * context
session's context
const uint8_t * s
read-only string data
Definition: coap_str.h:48
size_t length
length of string
Definition: coap_str.h:47
CoAP string data definition.
Definition: coap_str.h:38
Representation of parsed URI.
Definition: coap_uri.h:65
enum coap_uri_scheme_t scheme
The parsed scheme specifier.
Definition: coap_uri.h:77
coap_str_const_t path
The complete path if present or {0, NULL}.
Definition: coap_uri.h:68
uint16_t port
The port in host byte order.
Definition: coap_uri.h:67
coap_str_const_t query
The complete query if present or {0, NULL}.
Definition: coap_uri.h:72
coap_str_const_t host
The host part of the URI.
Definition: coap_uri.h:66