libcoap 4.3.5b
Loading...
Searching...
No Matches
coap_proxy.c
Go to the documentation of this file.
1/* coap_proxy.c -- helper functions for proxy handling
2 *
3 * Copyright (C) 2024-2025 Jon Shallow <supjps-libcoap@jpshallow.com>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 *
7 * This file is part of the CoAP library libcoap. Please see
8 * README for terms of use.
9 */
10
15
17
18#if COAP_PROXY_SUPPORT
19#include <stdio.h>
20
21#ifdef _WIN32
22#define strcasecmp _stricmp
23#define strncasecmp _strnicmp
24#endif
25
26int
28 return 1;
29}
30
31void
33 size_t i;
34 size_t j;
35
36 for (i = 0; i < context->proxy_list_count; i++) {
37 for (j = 0; j < context->proxy_list[i].req_count; j++) {
38 coap_delete_pdu_lkd(context->proxy_list[i].req_list[j].pdu);
39 coap_delete_cache_key(context->proxy_list[i].req_list[j].cache_key);
40 }
41 coap_free_type(COAP_STRING, context->proxy_list[i].uri_host_keep);
42 coap_free_type(COAP_STRING, context->proxy_list[i].req_list);
43 }
44 coap_free_type(COAP_STRING, context->proxy_list);
45}
46
47/*
48 * return 1 if there is a future expire time, else 0.
49 * update tim_rem with remaining value if return is 1.
50 */
51int
53 coap_tick_t *tim_rem) {
54 size_t i;
55 int ret = 0;
56
57 *tim_rem = -1;
58 for (i = 0; i < context->proxy_list_count; i++) {
59 coap_proxy_list_t *proxy_list = &context->proxy_list[i];
60
61 if (proxy_list->ongoing && proxy_list->idle_timeout_ticks) {
62 if (proxy_list->last_used + proxy_list->idle_timeout_ticks <= now) {
63 /* Drop session to upstream server */
65 proxy_list->ongoing = NULL;
66 } else {
67 if (*tim_rem > proxy_list->last_used + proxy_list->idle_timeout_ticks - now) {
68 *tim_rem = proxy_list->last_used + proxy_list->idle_timeout_ticks - now;
69 }
70 ret = 1;
71 }
72 }
73 }
74 return ret;
75}
76
77static int
78coap_get_uri_proxy_scheme_info(const coap_pdu_t *request,
79 coap_opt_t *opt,
80 coap_uri_t *uri,
81 coap_string_t **uri_path,
82 coap_string_t **uri_query) {
83
84 const char *opt_val = (const char *)coap_opt_value(opt);
85 int opt_len = coap_opt_length(opt);
86 coap_opt_iterator_t opt_iter;
87
88 if (opt_len == 9 &&
89 strncasecmp(opt_val, "coaps+tcp", 9) == 0) {
92 } else if (opt_len == 8 &&
93 strncasecmp(opt_val, "coap+tcp", 8) == 0) {
96 } else if (opt_len == 5 &&
97 strncasecmp(opt_val, "coaps", 5) == 0) {
100 } else if (opt_len == 4 &&
101 strncasecmp(opt_val, "coap", 4) == 0) {
103 uri->port = COAP_DEFAULT_PORT;
104 } else if (opt_len == 7 &&
105 strncasecmp(opt_val, "coap+ws", 7) == 0) {
107 uri->port = 80;
108 } else if (opt_len == 8 &&
109 strncasecmp(opt_val, "coaps+ws", 8) == 0) {
111 uri->port = 443;
112 } else {
113 coap_log_warn("Unsupported Proxy Scheme '%*.*s'\n",
114 opt_len, opt_len, opt_val);
115 return 0;
116 }
117
118 opt = coap_check_option(request, COAP_OPTION_URI_HOST, &opt_iter);
119 if (opt) {
120 uri->host.length = coap_opt_length(opt);
121 uri->host.s = coap_opt_value(opt);
122 } else {
123 coap_log_warn("Proxy Scheme requires Uri-Host\n");
124 return 0;
125 }
126 opt = coap_check_option(request, COAP_OPTION_URI_PORT, &opt_iter);
127 if (opt) {
128 uri->port =
130 coap_opt_length(opt));
131 }
132 *uri_path = coap_get_uri_path(request);
133 if (*uri_path) {
134 uri->path.s = (*uri_path)->s;
135 uri->path.length = (*uri_path)->length;
136 } else {
137 uri->path.s = NULL;
138 uri->path.length = 0;
139 }
140 *uri_query = coap_get_query(request);
141 if (*uri_query) {
142 uri->query.s = (*uri_query)->s;
143 uri->query.length = (*uri_query)->length;
144 } else {
145 uri->query.s = NULL;
146 uri->query.length = 0;
147 }
148 return 1;
149}
150
151int
153
154 /* Sanity check that the connection can be forwarded on */
155 switch (scheme) {
158 coap_log_warn("Proxy URI http or https not supported\n");
159 return 0;
161 break;
163 if (!coap_dtls_is_supported()) {
164 coap_log_warn("coaps URI scheme not supported for proxy\n");
165 return 0;
166 }
167 break;
169 if (!coap_tcp_is_supported()) {
170 coap_log_warn("coap+tcp URI scheme not supported for proxy\n");
171 return 0;
172 }
173 break;
175 if (!coap_tls_is_supported()) {
176 coap_log_warn("coaps+tcp URI scheme not supported for proxy\n");
177 return 0;
178 }
179 break;
181 if (!coap_ws_is_supported()) {
182 coap_log_warn("coap+ws URI scheme not supported for proxy\n");
183 return 0;
184 }
185 break;
187 if (!coap_wss_is_supported()) {
188 coap_log_warn("coaps+ws URI scheme not supported for proxy\n");
189 return 0;
190 }
191 break;
193 default:
194 coap_log_warn("%d URI scheme not supported\n", scheme);
195 return 0;
196 }
197 return 1;
198}
199
200static coap_proxy_list_t *
201coap_proxy_get_session(coap_session_t *session, const coap_pdu_t *request,
202 coap_pdu_t *response,
203 coap_proxy_server_list_t *server_list,
204 coap_proxy_server_t *server_use) {
205 size_t i;
206 coap_proxy_list_t *new_proxy_list;
207 coap_proxy_list_t *proxy_list = session->context->proxy_list;
208 size_t proxy_list_count = session->context->proxy_list_count;
209
210 coap_opt_iterator_t opt_iter;
211 coap_opt_t *proxy_scheme;
212 coap_opt_t *proxy_uri;
213 coap_string_t *uri_path = NULL;
214 coap_string_t *uri_query = NULL;
215
216 /* Round robin the defined next server list (which usually is just one */
217 server_list->next_entry++;
218 if (server_list->next_entry >= server_list->entry_count)
219 server_list->next_entry = 0;
220
221 memcpy(server_use, &server_list->entry[server_list->next_entry], sizeof(*server_use));
222
223 switch (server_list->type) {
228 /* Nothing else needs to be done */
229 break;
232 /* Need to get actual server from CoAP options */
233 /*
234 * See if Proxy-Scheme
235 */
236 proxy_scheme = coap_check_option(request, COAP_OPTION_PROXY_SCHEME, &opt_iter);
237 if (proxy_scheme) {
238 if (!coap_get_uri_proxy_scheme_info(request, proxy_scheme, &server_use->uri, &uri_path,
239 &uri_query)) {
240 response->code = COAP_RESPONSE_CODE(505);
241 return NULL;
242 }
243 }
244 /*
245 * See if Proxy-Uri
246 */
247 proxy_uri = coap_check_option(request, COAP_OPTION_PROXY_URI, &opt_iter);
248 if (proxy_uri) {
249 coap_log_info("Proxy URI '%.*s'\n",
250 (int)coap_opt_length(proxy_uri),
251 (const char *)coap_opt_value(proxy_uri));
253 coap_opt_length(proxy_uri),
254 &server_use->uri) < 0) {
255 /* Need to return a 5.05 RFC7252 Section 5.7.2 */
256 coap_log_warn("Proxy URI not decodable\n");
257 response->code = COAP_RESPONSE_CODE(505);
258 return NULL;
259 }
260 }
261
262 if (!(proxy_scheme || proxy_uri)) {
263 response->code = COAP_RESPONSE_CODE(404);
264 return NULL;
265 }
266
267 if (server_use->uri.host.length == 0) {
268 /* Ongoing connection not well formed */
269 response->code = COAP_RESPONSE_CODE(505);
270 return NULL;
271 }
272
274 response->code = COAP_RESPONSE_CODE(505);
275 return NULL;
276 }
277 break;
278 default:
279 assert(0);
280 return NULL;
281 }
282
283 /* See if we are already connected to the Server */
284 for (i = 0; i < proxy_list_count; i++) {
285 if (coap_string_equal(&proxy_list[i].uri.host, &server_use->uri.host) &&
286 proxy_list[i].uri.port == server_use->uri.port &&
287 proxy_list[i].uri.scheme == server_use->uri.scheme) {
288 if (!server_list->track_client_session) {
289 coap_ticks(&proxy_list[i].last_used);
290 return &proxy_list[i];
291 } else {
292 if (proxy_list[i].incoming == session) {
293 coap_ticks(&proxy_list[i].last_used);
294 return &proxy_list[i];
295 }
296 }
297 }
298 }
299
300 /* Need to create a new forwarding mapping */
301 new_proxy_list = coap_realloc_type(COAP_STRING, proxy_list, (i+1)*sizeof(proxy_list[0]));
302
303 if (new_proxy_list == NULL) {
304 response->code = COAP_RESPONSE_CODE(500);
305 return NULL;
306 }
307 session->context->proxy_list = proxy_list = new_proxy_list;
308 memset(&proxy_list[i], 0, sizeof(proxy_list[i]));
309
310 proxy_list[i].uri = server_use->uri;
312 server_use->uri.host.length);
313 if (!proxy_list[i].uri_host_keep) {
314 return NULL;
315 }
316 if (server_use->uri.host.length) {
317 memcpy(proxy_list[i].uri_host_keep, server_use->uri.host.s, server_use->uri.host.length);
318 }
319 proxy_list[proxy_list_count].uri.host.s = proxy_list[proxy_list_count].uri_host_keep;
320
321 if (server_list->track_client_session) {
322 proxy_list[i].incoming = session;
323 }
324 session->context->proxy_list_count++;
325 proxy_list[i].idle_timeout_ticks = server_list->idle_timeout_secs * COAP_TICKS_PER_SECOND;
326 coap_ticks(&proxy_list[i].last_used);
327 return &proxy_list[i];
328}
329
330void
331coap_proxy_remove_association(coap_session_t *session, int send_failure) {
332
333 size_t i;
334 size_t j;
335 coap_proxy_list_t *proxy_list = session->context->proxy_list;
336 size_t proxy_list_count = session->context->proxy_list_count;
337
338 for (i = 0; i < proxy_list_count; i++) {
339 /* Check for incoming match */
340 for (j = 0; j < proxy_list[i].req_count; j++) {
341 if (proxy_list[i].req_list[j].incoming == session) {
342 coap_delete_pdu_lkd(proxy_list[i].req_list[j].pdu);
343 coap_delete_bin_const(proxy_list[i].req_list[j].token_used);
344 coap_delete_cache_key(proxy_list[i].req_list[j].cache_key);
345 if (proxy_list[i].req_count-j > 1) {
346 memmove(&proxy_list[i].req_list[j], &proxy_list[i].req_list[j+1],
347 (proxy_list[i].req_count-j-1) * sizeof(proxy_list[i].req_list[0]));
348 }
349 proxy_list[i].req_count--;
350 break;
351 }
352 }
353 if (proxy_list[i].incoming == session) {
354 /* Only if there is a one-to-one tracking */
355 coap_session_release_lkd(proxy_list[i].ongoing);
356 break;
357 }
358
359 /* Check for outgoing match */
360 if (proxy_list[i].ongoing == session) {
361 coap_session_t *ongoing;
362
363 for (j = 0; j < proxy_list[i].req_count; j++) {
364 if (send_failure) {
365 coap_pdu_t *response;
366 coap_bin_const_t l_token;
367
368 /* Need to send back a gateway failure */
369 response = coap_pdu_init(proxy_list[i].req_list[j].pdu->type,
371 coap_new_message_id_lkd(proxy_list[i].incoming),
372 coap_session_max_pdu_size_lkd(proxy_list[i].incoming));
373 if (!response) {
374 coap_log_info("PDU creation issue\n");
375 goto cleanup;
376 }
377
378 l_token = coap_pdu_get_token(proxy_list[i].req_list[j].pdu);
379 if (!coap_add_token(response, l_token.length,
380 l_token.s)) {
381 coap_log_debug("Cannot add token to incoming proxy response PDU\n");
382 }
383
384 if (coap_send_lkd(proxy_list[i].incoming, response) == COAP_INVALID_MID) {
385 coap_log_info("Failed to send PDU with 5.02 gateway issue\n");
386 }
387cleanup:
388 coap_delete_pdu_lkd(proxy_list[i].req_list[j].pdu);
389 coap_delete_bin_const(proxy_list[i].req_list[j].token_used);
390 coap_delete_cache_key(proxy_list[i].req_list[j].cache_key);
391 }
392 }
393 ongoing = proxy_list[i].ongoing;
394 coap_free_type(COAP_STRING, proxy_list[i].req_list);
395 if (proxy_list_count-i > 1) {
396 memmove(&proxy_list[i],
397 &proxy_list[i+1],
398 (proxy_list_count-i-1) * sizeof(proxy_list[0]));
399 }
400 session->context->proxy_list_count--;
402 break;
403 }
404 }
405}
406
407static coap_proxy_list_t *
408coap_proxy_get_ongoing_session(coap_session_t *session,
409 const coap_pdu_t *request,
410 coap_pdu_t *response,
411 coap_proxy_server_list_t *server_list,
412 coap_proxy_server_t *server_use) {
413
414 coap_address_t dst;
415 coap_proto_t proto;
416 coap_addr_info_t *info_list = NULL;
417 coap_proxy_list_t *proxy_entry;
418 coap_context_t *context = session->context;
419 static char client_sni[256];
420
421 proxy_entry = coap_proxy_get_session(session, request, response, server_list, server_use);
422 if (!proxy_entry) {
423 /* Response code should be set */
424 return NULL;
425 }
426
427 if (!proxy_entry->ongoing) {
428 /* Need to create a new session */
429
430 /* resolve destination address where data should be sent */
431 info_list = coap_resolve_address_info(&server_use->uri.host,
432 server_use->uri.port,
433 server_use->uri.port,
434 server_use->uri.port,
435 server_use->uri.port,
436 0,
437 1 << server_use->uri.scheme,
439
440 if (info_list == NULL) {
441 response->code = COAP_RESPONSE_CODE(502);
443 return NULL;
444 }
445 proto = info_list->proto;
446 memcpy(&dst, &info_list->addr, sizeof(dst));
447 coap_free_address_info(info_list);
448
449 snprintf(client_sni, sizeof(client_sni), "%*.*s", (int)server_use->uri.host.length,
450 (int)server_use->uri.host.length, server_use->uri.host.s);
451
452 switch (server_use->uri.scheme) {
456#if COAP_OSCORE_SUPPORT
457 if (server_use->oscore_conf) {
458 proxy_entry->ongoing =
459 coap_new_client_session_oscore_lkd(context, NULL, &dst,
460 proto, server_use->oscore_conf);
461 } else {
462#endif /* COAP_OSCORE_SUPPORT */
463 proxy_entry->ongoing =
464 coap_new_client_session_lkd(context, NULL, &dst, proto);
465#if COAP_OSCORE_SUPPORT
466 }
467#endif /* COAP_OSCORE_SUPPORT */
468 break;
472#if COAP_OSCORE_SUPPORT
473 if (server_use->oscore_conf) {
474 if (server_use->dtls_pki) {
475 server_use->dtls_pki->client_sni = client_sni;
476 proxy_entry->ongoing =
477 coap_new_client_session_oscore_pki_lkd(context, NULL, &dst,
478 proto, server_use->dtls_pki, server_use->oscore_conf);
479 } else if (server_use->dtls_cpsk) {
480 server_use->dtls_cpsk->client_sni = client_sni;
481 proxy_entry->ongoing =
482 coap_new_client_session_oscore_psk_lkd(context, NULL, &dst,
483 proto, server_use->dtls_cpsk, server_use->oscore_conf);
484 } else {
485 coap_log_warn("Proxy: (D)TLS not configured for secure session\n");
486 }
487 } else {
488#endif /* COAP_OSCORE_SUPPORT */
489 /* Not doing OSCORE */
490 if (server_use->dtls_pki) {
491 server_use->dtls_pki->client_sni = client_sni;
492 proxy_entry->ongoing =
493 coap_new_client_session_pki_lkd(context, NULL, &dst,
494 proto, server_use->dtls_pki);
495 } else if (server_use->dtls_cpsk) {
496 server_use->dtls_cpsk->client_sni = client_sni;
497 proxy_entry->ongoing =
498 coap_new_client_session_psk2_lkd(context, NULL, &dst,
499 proto, server_use->dtls_cpsk);
500 } else {
501 coap_log_warn("Proxy: (D)TLS not configured for secure session\n");
502 }
503#if COAP_OSCORE_SUPPORT
504 }
505#endif /* COAP_OSCORE_SUPPORT */
506 break;
510 default:
511 assert(0);
512 break;
513 }
514 if (proxy_entry->ongoing == NULL) {
515 response->code = COAP_RESPONSE_CODE(505);
517 return NULL;
518 }
519 }
520
521 return proxy_entry;
522}
523
524static void
525coap_proxy_release_body_data(coap_session_t *session COAP_UNUSED,
526 void *app_ptr) {
527 coap_delete_binary(app_ptr);
528}
529
530int COAP_API
532 const coap_pdu_t *request,
533 coap_pdu_t *response,
534 coap_resource_t *resource,
535 coap_cache_key_t *cache_key,
536 coap_proxy_server_list_t *server_list) {
537 int ret;
538
539 coap_lock_lock(session->context, return 0);
540 ret = coap_proxy_forward_request_lkd(session,
541 request,
542 response,
543 resource,
544 cache_key,
545 server_list);
546 coap_lock_unlock(session->context);
547 return ret;
548}
549
550int
552 const coap_pdu_t *request,
553 coap_pdu_t *response,
554 coap_resource_t *resource,
555 coap_cache_key_t *cache_key,
556 coap_proxy_server_list_t *server_list) {
557 coap_proxy_list_t *proxy_entry;
558 size_t size;
559 size_t offset;
560 size_t total;
561 coap_binary_t *body_data = NULL;
562 const uint8_t *data;
563 coap_pdu_t *pdu = NULL;
564 coap_bin_const_t r_token = coap_pdu_get_token(request);
565 uint8_t token[8];
566 size_t token_len;
567 coap_proxy_req_t *new_req_list;
568 coap_optlist_t *optlist = NULL;
569 coap_opt_t *option;
570 coap_opt_iterator_t opt_iter;
571 coap_proxy_server_t server_use;
572
573 /* Set up ongoing session (if not already done) */
574
575 proxy_entry = coap_proxy_get_ongoing_session(session, request, response,
576 server_list, &server_use);
577 if (!proxy_entry)
578 /* response code already set */
579 return 0;
580
581 /* Need to save the request pdu entry */
582 new_req_list = coap_realloc_type(COAP_STRING, proxy_entry->req_list,
583 (proxy_entry->req_count + 1)*sizeof(coap_proxy_req_t));
584
585 if (new_req_list == NULL) {
586 goto failed;
587 }
588 proxy_entry->req_list = new_req_list;
589 /* Get a new token for ongoing session */
590 coap_session_new_token(proxy_entry->ongoing, &token_len, token);
591 new_req_list[proxy_entry->req_count].token_used = coap_new_bin_const(token, token_len);
592 if (new_req_list[proxy_entry->req_count].token_used == NULL) {
593 goto failed;
594 }
595 new_req_list[proxy_entry->req_count].pdu = coap_pdu_duplicate_lkd(request, session,
596 r_token.length, r_token.s, NULL);
597 if (new_req_list[proxy_entry->req_count].pdu == NULL) {
598 coap_delete_bin_const(new_req_list[proxy_entry->req_count].token_used);
599 new_req_list[proxy_entry->req_count].token_used = NULL;
600 goto failed;
601 }
602 new_req_list[proxy_entry->req_count].resource = resource;
603 new_req_list[proxy_entry->req_count].incoming = session;
604 new_req_list[proxy_entry->req_count].cache_key = cache_key;
605 proxy_entry->req_count++;
606
607 switch (server_list->type) {
611 /*
612 * Need to replace Proxy-Uri with Uri-Host (and Uri-Port)
613 * or strip out Proxy-Scheme.
614 */
615
616 /*
617 * Build up the ongoing PDU that we are going to send
618 */
619 pdu = coap_pdu_init(request->type, request->code,
620 coap_new_message_id_lkd(proxy_entry->ongoing),
622 if (!pdu) {
623 goto failed;
624 }
625
626 if (!coap_add_token(pdu, token_len, token)) {
627 goto failed;
628 }
629
630 if (!coap_uri_into_optlist(&server_use.uri,
631 &proxy_entry->ongoing->addr_info.remote,
632 &optlist, 1)) {
633 coap_log_err("Failed to create options for URI\n");
634 goto failed;
635 }
636
637 /* Copy the remaining options across */
638 coap_option_iterator_init(request, &opt_iter, COAP_OPT_ALL);
639 while ((option = coap_option_next(&opt_iter))) {
640 coap_uri_t proxy_uri;
641
642 switch (opt_iter.number) {
644 coap_log_info("Proxy URI '%.*s'\n",
645 (int)coap_opt_length(option),
646 (const char *)coap_opt_value(option));
648 coap_opt_length(option),
649 &proxy_uri) < 0) {
650 goto failed;
651 }
652 if (!coap_uri_into_optlist(&proxy_uri,
653 &proxy_entry->ongoing->addr_info.remote,
654 &optlist, 0)) {
655 coap_log_err("Failed to create options for URI\n");
656 goto failed;
657 }
658 break;
660 break;
665 /* These are not passed on */
666 break;
667 default:
668 coap_insert_optlist(&optlist,
669 coap_new_optlist(opt_iter.number,
670 coap_opt_length(option),
671 coap_opt_value(option)));
672 break;
673 }
674 }
675
676 /* Update pdu with options */
677 coap_add_optlist_pdu(pdu, &optlist);
678 coap_delete_optlist(optlist);
679 break;
683 default:
684 /*
685 * Duplicate request PDU for onward transmission (with new token).
686 */
687 pdu = coap_pdu_duplicate_lkd(request, proxy_entry->ongoing, token_len, token, NULL);
688 if (!pdu) {
689 coap_log_debug("proxy: PDU generation error\n");
690 goto failed;
691 }
692 break;
693 }
694
695 if (coap_get_data_large(request, &size, &data, &offset, &total)) {
696 /* COAP_BLOCK_SINGLE_BODY is set, so single body should be given */
697 assert(size == total);
698 /*
699 * Need to take a copy of the data as request PDU may go away before
700 * all data is transmitted.
701 */
702 body_data = coap_new_binary(total);
703 if (!body_data) {
704 coap_log_debug("proxy: body build memory error\n");
705 goto failed;
706 }
707 memcpy(body_data->s, data, size);
708 if (!coap_add_data_large_request_lkd(proxy_entry->ongoing, pdu, total, data,
709 coap_proxy_release_body_data, body_data)) {
710 coap_log_debug("proxy: add data error\n");
711 goto failed;
712 }
713 }
714
715 if (coap_send_lkd(proxy_entry->ongoing, pdu) == COAP_INVALID_MID) {
716 pdu = NULL;
717 coap_log_debug("proxy: upstream PDU send error\n");
718 goto failed;
719 }
720
721 /*
722 * Do not update the response code (hence empty ACK) as will be sending
723 * separate response when response comes back from upstream server
724 */
725
726 return 1;
727
728failed:
729 response->code = COAP_RESPONSE_CODE(500);
731 return 0;
732}
733
736 const coap_pdu_t *received,
737 coap_cache_key_t **cache_key) {
738 int ret;
739
740 coap_lock_lock(session->context, return 0);
742 received,
743 cache_key);
744 coap_lock_unlock(session->context);
745 return ret;
746}
747
750 const coap_pdu_t *received,
751 coap_cache_key_t **cache_key) {
752 coap_pdu_t *pdu = NULL;
753 coap_session_t *incoming = NULL;
754 size_t i;
755 size_t j = 0;
756 size_t size;
757 const uint8_t *data;
758 coap_optlist_t *optlist = NULL;
759 coap_opt_t *option;
760 coap_opt_iterator_t opt_iter;
761 size_t offset;
762 size_t total;
763 coap_proxy_list_t *proxy_entry = NULL;
764 uint16_t media_type = COAP_MEDIATYPE_TEXT_PLAIN;
765 int maxage = -1;
766 uint64_t etag = 0;
767 coap_pdu_code_t rcv_code = coap_pdu_get_code(received);
768 coap_bin_const_t rcv_token = coap_pdu_get_token(received);
769 coap_bin_const_t req_token;
770 coap_binary_t *body_data = NULL;
771 coap_pdu_t *req_pdu;
772 coap_proxy_list_t *proxy_list = session->context->proxy_list;
773 size_t proxy_list_count = session->context->proxy_list_count;
774 coap_resource_t *resource;
775 struct coap_proxy_req_t *proxy_req = NULL;
776
777 for (i = 0; i < proxy_list_count; i++) {
778 proxy_entry = &proxy_list[i];
779 for (j = 0; j < proxy_entry->req_count; j++) {
780 if (coap_binary_equal(&rcv_token, proxy_entry->req_list[j].token_used)) {
781 proxy_req = &proxy_entry->req_list[j];
782 break;
783 }
784 }
785 if (j != proxy_entry->req_count) {
786 break;
787 }
788 }
789 if (i == proxy_list_count) {
790 coap_log_warn("Unknown proxy ongoing session response received\n");
791 return COAP_RESPONSE_OK;
792 }
793
794 req_pdu = proxy_req->pdu;
795 req_token = coap_pdu_get_token(req_pdu);
796 resource = proxy_req->resource;
797 incoming = proxy_req->incoming;
798
799 coap_log_debug("** process upstream incoming %d.%02d response:\n",
800 COAP_RESPONSE_CLASS(rcv_code), rcv_code & 0x1F);
801
802 if (coap_get_data_large(received, &size, &data, &offset, &total)) {
803 /* COAP_BLOCK_SINGLE_BODY is set, so single body should be given */
804 assert(size == total);
805 body_data = coap_new_binary(total);
806 if (!body_data) {
807 coap_log_debug("body build memory error\n");
808 goto remove_match;
809 }
810 memcpy(body_data->s, data, size);
811 data = body_data->s;
812 }
813
814 /*
815 * Build up the ongoing PDU that we are going to send to proxy originator
816 * as separate response
817 */
818 pdu = coap_pdu_init(req_pdu->type, rcv_code,
821 if (!pdu) {
822 coap_log_debug("Failed to create ongoing proxy response PDU\n");
823 goto remove_match;
824 }
825
826 if (!coap_add_token(pdu, req_token.length, req_token.s)) {
827 coap_log_debug("cannot add token to ongoing proxy response PDU\n");
828 }
829
830 /*
831 * Copy the options across, skipping those needed for
832 * coap_add_data_response_large()
833 */
834 coap_option_iterator_init(received, &opt_iter, COAP_OPT_ALL);
835 while ((option = coap_option_next(&opt_iter))) {
836 switch (opt_iter.number) {
838 media_type = coap_decode_var_bytes(coap_opt_value(option),
839 coap_opt_length(option));
840 break;
842 maxage = coap_decode_var_bytes(coap_opt_value(option),
843 coap_opt_length(option));
844 break;
845 case COAP_OPTION_ETAG:
847 coap_opt_length(option));
848 break;
852 break;
853 default:
854 coap_insert_optlist(&optlist,
855 coap_new_optlist(opt_iter.number,
856 coap_opt_length(option),
857 coap_opt_value(option)));
858 break;
859 }
860 }
861 coap_add_optlist_pdu(pdu, &optlist);
862 coap_delete_optlist(optlist);
863
864 if (size > 0) {
865 coap_string_t *l_query = coap_get_query(req_pdu);
866
868 l_query,
869 media_type, maxage, etag, size, data,
870 coap_proxy_release_body_data,
871 body_data);
872 body_data = NULL;
873 coap_delete_string(l_query);
874 }
875
876 if (cache_key)
877 *cache_key = proxy_req->cache_key;
878
880
881remove_match:
882 option = coap_check_option(received, COAP_OPTION_OBSERVE, &opt_iter);
883 /* Need to remove matching token entry (apart from on Observe response) */
884 if (option == NULL && proxy_entry->req_count) {
885 coap_delete_pdu_lkd(proxy_entry->req_list[j].pdu);
887 /* Do not delete cache key here - caller's responsibility */
888 proxy_entry->req_count--;
889 if (proxy_entry->req_count-j > 0) {
890 memmove(&proxy_entry->req_list[j], &proxy_entry->req_list[j+1],
891 (proxy_entry->req_count-j) * sizeof(proxy_entry->req_list[0]));
892 }
893 }
894 coap_delete_binary(body_data);
895 return COAP_RESPONSE_OK;
896}
897
898#else /* ! COAP_PROXY_SUPPORT */
899
900int
902 return 0;
903}
904
905COAP_API int
907 const coap_pdu_t *request,
908 coap_pdu_t *response,
911 coap_proxy_server_list_t *server_list) {
912 (void)session;
913 (void)request;
914 (void)resource;
915 (void)cache_key;
916 (void)server_list;
917 response->code = COAP_RESPONSE_CODE(500);
918 return 0;
919}
920
923 const coap_pdu_t *received,
925 (void)session;
926 (void)received;
927 (void)cache_key;
928 return COAP_RESPONSE_OK;
929}
930
931int
933 (void)scheme;
934 return 0;
935}
936#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().
coap_addr_info_t * coap_resolve_address_info(const coap_str_const_t *address, uint16_t port, uint16_t secure_port, uint16_t ws_port, uint16_t ws_secure_port, int ai_hints_flags, int scheme_hint_bits, coap_resolve_type_t type)
Resolve the specified address into a set of coap_address_t that can be used to bind() (local) or conn...
@ COAP_RESOLVE_TYPE_REMOTE
remote side of session
struct coap_resource_t coap_resource_t
Library specific build wrapper for coap_internal.h.
#define COAP_API
@ COAP_STRING
Definition coap_mem.h:39
void * coap_realloc_type(coap_memory_tag_t type, void *p, size_t size)
Reallocates a chunk p of bytes created by coap_malloc_type() or coap_realloc_type() and returns a poi...
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
Definition coap_option.h:26
coap_uri_scheme_t
The scheme specifiers.
Definition coap_uri.h:28
@ COAP_URI_SCHEME_COAPS_WS
Definition coap_uri.h:36
@ COAP_URI_SCHEME_COAPS_TCP
Definition coap_uri.h:32
@ COAP_URI_SCHEME_COAPS
Definition coap_uri.h:30
@ COAP_URI_SCHEME_COAP_TCP
Definition coap_uri.h:31
@ COAP_URI_SCHEME_COAP_WS
Definition coap_uri.h:35
@ COAP_URI_SCHEME_HTTPS
Definition coap_uri.h:34
@ COAP_URI_SCHEME_COAP
Definition coap_uri.h:29
@ COAP_URI_SCHEME_LAST
Definition coap_uri.h:37
@ COAP_URI_SCHEME_HTTP
Definition coap_uri.h:33
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:1354
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.
coap_optlist_t * coap_new_optlist(uint16_t number, size_t length, const uint8_t *data)
Create a new optlist entry.
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
coap_opt_iterator_t * coap_option_iterator_init(const coap_pdu_t *pdu, coap_opt_iterator_t *oi, const coap_opt_filter_t *filter)
Initializes the given option iterator oi to point to the beginning of the pdu's option list.
void coap_delete_optlist(coap_optlist_t *queue)
Removes all entries from the optlist_chain, freeing off their memory usage.
#define COAP_OPT_ALL
Pre-defined filter that includes all options.
int coap_add_optlist_pdu(coap_pdu_t *pdu, coap_optlist_t **options)
The current optlist of optlist_chain is first sorted (as per RFC7272 ordering requirements) and then ...
coap_opt_t * coap_check_option(const coap_pdu_t *pdu, coap_option_num_t number, coap_opt_iterator_t *oi)
Retrieves the first option of number number from pdu.
int coap_insert_optlist(coap_optlist_t **head, coap_optlist_t *node)
Adds optlist to the given optlist_chain.
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
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 ...
void coap_delete_pdu_lkd(coap_pdu_t *pdu)
Dispose of an CoAP PDU and free off associated storage.
Definition coap_pdu.c:188
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:228
#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:1586
#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
#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 Note: coap_layers_coap[] needs updating if extended.
Definition coap_pdu.h:313
coap_pdu_code_t
Set of codes available for a PDU.
Definition coap_pdu.h:327
#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:356
#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:885
#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:1610
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:922
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:932
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:906
@ 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.
void coap_session_release_lkd(coap_session_t *session)
Decrement reference counter on a session.
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:121
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:901
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:1008
int coap_split_proxy_uri(const uint8_t *str_var, size_t len, coap_uri_t *uri)
Parses a given string into URI components.
Definition coap_uri.c:299
int coap_uri_into_optlist(const coap_uri_t *uri, const coap_address_t *dst, coap_optlist_t **optlist_chain, int create_port_host_opt)
Takes a coap_uri_t and then adds CoAP options into the optlist_chain.
Definition coap_uri.c:322
coap_string_t * coap_get_query(const coap_pdu_t *request)
Extract query string from request PDU according to escape rules in 6.5.8.
Definition coap_uri.c:957
#define COAP_UNUSED
Definition libcoap.h:70
Resolved addresses information.
coap_proto_t proto
CoAP protocol to use.
coap_address_t addr
The address to connect / bind to.
coap_address_t remote
remote address and port
Definition coap_io.h:56
Multi-purpose address abstraction.
CoAP binary data definition with const data.
Definition coap_str.h:64
size_t length
length of binary data
Definition coap_str.h:65
const uint8_t * s
read-only binary data
Definition coap_str.h:66
CoAP binary data definition.
Definition coap_str.h:56
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.
coap_option_num_t number
decoded option number
Representation of chained list of CoAP options to install.
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(.
uint8_t * uri_host_keep
memory for uri.host
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 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