libcoap 4.3.5
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 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#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(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].req_list);
42 }
43 coap_free_type(COAP_STRING, context->proxy_list);
44}
45
46/*
47 * return 1 if there is a future expire time, else 0.
48 * update tim_rem with remaining value if return is 1.
49 */
50int
52 coap_tick_t *tim_rem) {
53 size_t i;
54 int ret = 0;
55
56 *tim_rem = -1;
57 for (i = 0; i < context->proxy_list_count; i++) {
58 coap_proxy_list_t *proxy_list = &context->proxy_list[i];
59
60 if (proxy_list->ongoing && proxy_list->idle_timeout_ticks) {
61 if (proxy_list->last_used + proxy_list->idle_timeout_ticks <= now) {
62 /* Drop session to upstream server */
64 proxy_list->ongoing = NULL;
65 } else {
66 if (*tim_rem > proxy_list->last_used + proxy_list->idle_timeout_ticks - now) {
67 *tim_rem = proxy_list->last_used + proxy_list->idle_timeout_ticks - now;
68 }
69 ret = 1;
70 }
71 }
72 }
73 return ret;
74}
75
76static int
77coap_get_uri_proxy_scheme_info(const coap_pdu_t *request,
78 coap_opt_t *opt,
79 coap_uri_t *uri,
80 coap_string_t **uri_path,
81 coap_string_t **uri_query) {
82
83 const char *opt_val = (const char *)coap_opt_value(opt);
84 int opt_len = coap_opt_length(opt);
85 coap_opt_iterator_t opt_iter;
86
87 if (opt_len == 9 &&
88 strncasecmp(opt_val, "coaps+tcp", 9) == 0) {
91 } else if (opt_len == 8 &&
92 strncasecmp(opt_val, "coap+tcp", 8) == 0) {
95 } else if (opt_len == 5 &&
96 strncasecmp(opt_val, "coaps", 5) == 0) {
99 } else if (opt_len == 4 &&
100 strncasecmp(opt_val, "coap", 4) == 0) {
102 uri->port = COAP_DEFAULT_PORT;
103 } else if (opt_len == 7 &&
104 strncasecmp(opt_val, "coap+ws", 7) == 0) {
106 uri->port = 80;
107 } else if (opt_len == 8 &&
108 strncasecmp(opt_val, "coaps+ws", 8) == 0) {
110 uri->port = 443;
111 } else {
112 coap_log_warn("Unsupported Proxy Scheme '%*.*s'\n",
113 opt_len, opt_len, opt_val);
114 return 0;
115 }
116
117 opt = coap_check_option(request, COAP_OPTION_URI_HOST, &opt_iter);
118 if (opt) {
119 uri->host.length = coap_opt_length(opt);
120 uri->host.s = coap_opt_value(opt);
121 } else {
122 coap_log_warn("Proxy Scheme requires Uri-Host\n");
123 return 0;
124 }
125 opt = coap_check_option(request, COAP_OPTION_URI_PORT, &opt_iter);
126 if (opt) {
127 uri->port =
129 coap_opt_length(opt));
130 }
131 *uri_path = coap_get_uri_path(request);
132 if (*uri_path) {
133 uri->path.s = (*uri_path)->s;
134 uri->path.length = (*uri_path)->length;
135 } else {
136 uri->path.s = NULL;
137 uri->path.length = 0;
138 }
139 *uri_query = coap_get_query(request);
140 if (*uri_query) {
141 uri->query.s = (*uri_query)->s;
142 uri->query.length = (*uri_query)->length;
143 } else {
144 uri->query.s = NULL;
145 uri->query.length = 0;
146 }
147 return 1;
148}
149
150int
152
153 /* Sanity check that the connection can be forwarded on */
154 switch (scheme) {
157 coap_log_warn("Proxy URI http or https not supported\n");
158 return 0;
160 break;
162 if (!coap_dtls_is_supported()) {
163 coap_log_warn("coaps URI scheme not supported for proxy\n");
164 return 0;
165 }
166 break;
168 if (!coap_tcp_is_supported()) {
169 coap_log_warn("coap+tcp URI scheme not supported for proxy\n");
170 return 0;
171 }
172 break;
174 if (!coap_tls_is_supported()) {
175 coap_log_warn("coaps+tcp URI scheme not supported for proxy\n");
176 return 0;
177 }
178 break;
180 if (!coap_ws_is_supported()) {
181 coap_log_warn("coap+ws URI scheme not supported for proxy\n");
182 return 0;
183 }
184 break;
186 if (!coap_wss_is_supported()) {
187 coap_log_warn("coaps+ws URI scheme not supported for proxy\n");
188 return 0;
189 }
190 break;
192 default:
193 coap_log_warn("%d URI scheme not supported\n", scheme);
194 return 0;
195 }
196 return 1;
197}
198
199static coap_proxy_list_t *
200coap_proxy_get_session(coap_session_t *session, const coap_pdu_t *request,
201 coap_pdu_t *response,
202 coap_proxy_server_list_t *server_list,
203 coap_proxy_server_t *server_use) {
204 size_t i;
205 coap_proxy_list_t *new_proxy_list;
206 coap_proxy_list_t *proxy_list = session->context->proxy_list;
207 size_t proxy_list_count = session->context->proxy_list_count;
208
209 coap_opt_iterator_t opt_iter;
210 coap_opt_t *proxy_scheme;
211 coap_opt_t *proxy_uri;
212 coap_string_t *uri_path = NULL;
213 coap_string_t *uri_query = NULL;
214
215 /* Round robin the defined next server list (which usually is just one */
216 server_list->next_entry++;
217 if (server_list->next_entry >= server_list->entry_count)
218 server_list->next_entry = 0;
219
220 memcpy(server_use, &server_list->entry[server_list->next_entry], sizeof(*server_use));
221
222 switch (server_list->type) {
226 /* Nothing else needs to be done */
227 break;
231 /* Need to get actual server from CoAP options */
232 /*
233 * See if Proxy-Scheme
234 */
235 proxy_scheme = coap_check_option(request, COAP_OPTION_PROXY_SCHEME, &opt_iter);
236 if (proxy_scheme) {
237 if (!coap_get_uri_proxy_scheme_info(request, proxy_scheme, &server_use->uri, &uri_path,
238 &uri_query)) {
239 response->code = COAP_RESPONSE_CODE(505);
240 return NULL;
241 }
242 }
243 /*
244 * See if Proxy-Uri
245 */
246 proxy_uri = coap_check_option(request, COAP_OPTION_PROXY_URI, &opt_iter);
247 if (proxy_uri) {
248 coap_log_info("Proxy URI '%.*s'\n",
249 (int)coap_opt_length(proxy_uri),
250 (const char *)coap_opt_value(proxy_uri));
252 coap_opt_length(proxy_uri),
253 &server_use->uri) < 0) {
254 /* Need to return a 5.05 RFC7252 Section 5.7.2 */
255 coap_log_warn("Proxy URI not decodable\n");
256 response->code = COAP_RESPONSE_CODE(505);
257 return NULL;
258 }
259 }
260
261 if (!(proxy_scheme || proxy_uri)) {
262 response->code = COAP_RESPONSE_CODE(404);
263 return NULL;
264 }
265
266 if (server_use->uri.host.length == 0) {
267 /* Ongoing connection not well formed */
268 response->code = COAP_RESPONSE_CODE(505);
269 return NULL;
270 }
271
273 response->code = COAP_RESPONSE_CODE(505);
274 return NULL;
275 }
276 break;
277 default:
278 assert(0);
279 return NULL;
280 }
281
282 /* See if we are already connected to the Server */
283 for (i = 0; i < proxy_list_count; i++) {
284 if (coap_string_equal(&proxy_list[i].uri.host, &server_use->uri.host) &&
285 proxy_list[i].uri.port == server_use->uri.port &&
286 proxy_list[i].uri.scheme == server_use->uri.scheme) {
287 if (!server_list->track_client_session) {
288 coap_ticks(&proxy_list[i].last_used);
289 return &proxy_list[i];
290 } else {
291 if (proxy_list[i].incoming == session) {
292 coap_ticks(&proxy_list[i].last_used);
293 return &proxy_list[i];
294 }
295 }
296 }
297 }
298
299 /* Need to create a new forwarding mapping */
300 new_proxy_list = coap_realloc_type(COAP_STRING, proxy_list, (i+1)*sizeof(proxy_list[0]));
301
302 if (new_proxy_list == NULL) {
303 response->code = COAP_RESPONSE_CODE(500);
304 return NULL;
305 }
306 session->context->proxy_list = proxy_list = new_proxy_list;
307 memset(&proxy_list[i], 0, sizeof(proxy_list[i]));
308
309 proxy_list[i].uri = server_use->uri;
310 if (server_list->track_client_session) {
311 proxy_list[i].incoming = session;
312 }
313 session->context->proxy_list_count++;
314 proxy_list[i].idle_timeout_ticks = server_list->idle_timeout_secs * COAP_TICKS_PER_SECOND;
315 coap_ticks(&proxy_list[i].last_used);
316 return &proxy_list[i];
317}
318
319void
320coap_proxy_remove_association(coap_session_t *session, int send_failure) {
321
322 size_t i;
323 size_t j;
324 coap_proxy_list_t *proxy_list = session->context->proxy_list;
325 size_t proxy_list_count = session->context->proxy_list_count;
326
327 for (i = 0; i < proxy_list_count; i++) {
328 /* Check for incoming match */
329 for (j = 0; j < proxy_list[i].req_count; j++) {
330 if (proxy_list[i].req_list[j].incoming == session) {
331 coap_delete_pdu(proxy_list[i].req_list[j].pdu);
332 coap_delete_bin_const(proxy_list[i].req_list[j].token_used);
333 coap_delete_cache_key(proxy_list[i].req_list[j].cache_key);
334 if (proxy_list[i].req_count-j > 1) {
335 memmove(&proxy_list[i].req_list[j], &proxy_list[i].req_list[j+1],
336 (proxy_list[i].req_count-j-1) * sizeof(proxy_list[i].req_list[0]));
337 }
338 proxy_list[i].req_count--;
339 break;
340 }
341 }
342 if (proxy_list[i].incoming == session) {
343 /* Only if there is a one-to-one tracking */
344 coap_session_release_lkd(proxy_list[i].ongoing);
345 break;
346 }
347
348 /* Check for outgoing match */
349 if (proxy_list[i].ongoing == session) {
350 coap_session_t *ongoing;
351
352 for (j = 0; j < proxy_list[i].req_count; j++) {
353 if (send_failure) {
354 coap_pdu_t *response;
355 coap_bin_const_t l_token;
356
357 /* Need to send back a gateway failure */
358 response = coap_pdu_init(proxy_list[i].req_list[j].pdu->type,
360 coap_new_message_id_lkd(proxy_list[i].incoming),
361 coap_session_max_pdu_size_lkd(proxy_list[i].incoming));
362 if (!response) {
363 coap_log_info("PDU creation issue\n");
364 goto cleanup;
365 }
366
367 l_token = coap_pdu_get_token(proxy_list[i].req_list[j].pdu);
368 if (!coap_add_token(response, l_token.length,
369 l_token.s)) {
370 coap_log_debug("Cannot add token to incoming proxy response PDU\n");
371 }
372
373 if (coap_send_lkd(proxy_list[i].incoming, response) == COAP_INVALID_MID) {
374 coap_log_info("Failed to send PDU with 5.02 gateway issue\n");
375 }
376cleanup:
377 coap_delete_pdu(proxy_list[i].req_list[j].pdu);
378 coap_delete_bin_const(proxy_list[i].req_list[j].token_used);
379 coap_delete_cache_key(proxy_list[i].req_list[j].cache_key);
380 }
381 }
382 ongoing = proxy_list[i].ongoing;
383 coap_free_type(COAP_STRING, proxy_list[i].req_list);
384 if (proxy_list_count-i > 1) {
385 memmove(&proxy_list[i],
386 &proxy_list[i+1],
387 (proxy_list_count-i-1) * sizeof(proxy_list[0]));
388 }
389 session->context->proxy_list_count--;
391 break;
392 }
393 }
394}
395
396static coap_proxy_list_t *
397coap_proxy_get_ongoing_session(coap_session_t *session,
398 const coap_pdu_t *request,
399 coap_pdu_t *response,
400 coap_proxy_server_list_t *server_list,
401 coap_proxy_server_t *server_use) {
402
403 coap_address_t dst;
404 coap_proto_t proto;
405 coap_addr_info_t *info_list = NULL;
406 coap_proxy_list_t *proxy_entry;
407 coap_context_t *context = session->context;
408 static char client_sni[256];
409
410 proxy_entry = coap_proxy_get_session(session, request, response, server_list, server_use);
411 if (!proxy_entry) {
412 /* Response code should be set */
413 return NULL;
414 }
415
416 if (!proxy_entry->ongoing) {
417 /* Need to create a new session */
418
419 /* resolve destination address where data should be sent */
420 info_list = coap_resolve_address_info(&server_use->uri.host,
421 server_use->uri.port,
422 server_use->uri.port,
423 server_use->uri.port,
424 server_use->uri.port,
425 0,
426 1 << server_use->uri.scheme,
428
429 if (info_list == NULL) {
430 response->code = COAP_RESPONSE_CODE(502);
432 return NULL;
433 }
434 proto = info_list->proto;
435 memcpy(&dst, &info_list->addr, sizeof(dst));
436 coap_free_address_info(info_list);
437
438 snprintf(client_sni, sizeof(client_sni), "%*.*s", (int)server_use->uri.host.length,
439 (int)server_use->uri.host.length, server_use->uri.host.s);
440
441 switch (server_use->uri.scheme) {
445#if COAP_OSCORE_SUPPORT
446 if (server_use->oscore_conf) {
447 proxy_entry->ongoing =
448 coap_new_client_session_oscore_lkd(context, NULL, &dst,
449 proto, server_use->oscore_conf);
450 } else {
451#endif /* COAP_OSCORE_SUPPORT */
452 proxy_entry->ongoing =
453 coap_new_client_session_lkd(context, NULL, &dst, proto);
454#if COAP_OSCORE_SUPPORT
455 }
456#endif /* COAP_OSCORE_SUPPORT */
457 break;
461#if COAP_OSCORE_SUPPORT
462 if (server_use->oscore_conf) {
463 if (server_use->dtls_pki) {
464 server_use->dtls_pki->client_sni = client_sni;
465 proxy_entry->ongoing =
466 coap_new_client_session_oscore_pki_lkd(context, NULL, &dst,
467 proto, server_use->dtls_pki, server_use->oscore_conf);
468 } else if (server_use->dtls_cpsk) {
469 server_use->dtls_cpsk->client_sni = client_sni;
470 proxy_entry->ongoing =
471 coap_new_client_session_oscore_psk_lkd(context, NULL, &dst,
472 proto, server_use->dtls_cpsk, server_use->oscore_conf);
473 } else {
474 coap_log_warn("Proxy: (D)TLS not configured for secure session\n");
475 }
476 } else {
477#endif /* COAP_OSCORE_SUPPORT */
478 /* Not doing OSCORE */
479 if (server_use->dtls_pki) {
480 server_use->dtls_pki->client_sni = client_sni;
481 proxy_entry->ongoing =
482 coap_new_client_session_pki_lkd(context, NULL, &dst,
483 proto, server_use->dtls_pki);
484 } else if (server_use->dtls_cpsk) {
485 server_use->dtls_cpsk->client_sni = client_sni;
486 proxy_entry->ongoing =
487 coap_new_client_session_psk2_lkd(context, NULL, &dst,
488 proto, server_use->dtls_cpsk);
489 } else {
490 coap_log_warn("Proxy: (D)TLS not configured for secure session\n");
491 }
492#if COAP_OSCORE_SUPPORT
493 }
494#endif /* COAP_OSCORE_SUPPORT */
495 break;
499 default:
500 assert(0);
501 break;
502 }
503 if (proxy_entry->ongoing == NULL) {
504 response->code = COAP_RESPONSE_CODE(505);
506 return NULL;
507 }
508 }
509
510 return proxy_entry;
511}
512
513static void
514coap_proxy_release_body_data(coap_session_t *session COAP_UNUSED,
515 void *app_ptr) {
516 coap_delete_binary(app_ptr);
517}
518
519int COAP_API
521 const coap_pdu_t *request,
522 coap_pdu_t *response,
523 coap_resource_t *resource,
524 coap_cache_key_t *cache_key,
525 coap_proxy_server_list_t *server_list) {
526 int ret;
527
528 coap_lock_lock(session->context, return 0);
529 ret = coap_proxy_forward_request_lkd(session,
530 request,
531 response,
532 resource,
533 cache_key,
534 server_list);
535 coap_lock_unlock(session->context);
536 return ret;
537}
538
539int
541 const coap_pdu_t *request,
542 coap_pdu_t *response,
543 coap_resource_t *resource,
544 coap_cache_key_t *cache_key,
545 coap_proxy_server_list_t *server_list) {
546 coap_proxy_list_t *proxy_entry;
547 size_t size;
548 size_t offset;
549 size_t total;
550 coap_binary_t *body_data = NULL;
551 const uint8_t *data;
552 coap_pdu_t *pdu = NULL;
553 coap_bin_const_t r_token = coap_pdu_get_token(request);
554 uint8_t token[8];
555 size_t token_len;
556 coap_proxy_req_t *new_req_list;
557 coap_optlist_t *optlist = NULL;
558 coap_opt_t *option;
559 coap_opt_iterator_t opt_iter;
560 coap_proxy_server_t server_use;
561
562 /* Set up ongoing session (if not already done) */
563
564 proxy_entry = coap_proxy_get_ongoing_session(session, request, response,
565 server_list, &server_use);
566 if (!proxy_entry)
567 /* response code already set */
568 return 0;
569
570 /* Need to save the request pdu entry */
571 new_req_list = coap_realloc_type(COAP_STRING, proxy_entry->req_list,
572 (proxy_entry->req_count + 1)*sizeof(coap_proxy_req_t));
573
574 if (new_req_list == NULL) {
575 goto failed;
576 }
577 proxy_entry->req_list = new_req_list;
578 /* Get a new token for ongoing session */
579 coap_session_new_token(proxy_entry->ongoing, &token_len, token);
580 new_req_list[proxy_entry->req_count].token_used = coap_new_bin_const(token, token_len);
581 if (new_req_list[proxy_entry->req_count].token_used == NULL) {
582 goto failed;
583 }
584 new_req_list[proxy_entry->req_count].pdu = coap_pdu_duplicate_lkd(request, session,
585 r_token.length, r_token.s, NULL);
586 if (new_req_list[proxy_entry->req_count].pdu == NULL) {
587 coap_delete_bin_const(new_req_list[proxy_entry->req_count].token_used);
588 new_req_list[proxy_entry->req_count].token_used = NULL;
589 goto failed;
590 }
591 new_req_list[proxy_entry->req_count].resource = resource;
592 new_req_list[proxy_entry->req_count].incoming = session;
593 new_req_list[proxy_entry->req_count].cache_key = cache_key;
594 proxy_entry->req_count++;
595
596 switch (server_list->type) {
600 /*
601 * Need to replace Proxy-Uri with Uri-Host (and Uri-Port)
602 * or strip out Proxy-Scheme.
603 */
604
605 /*
606 * Build up the ongoing PDU that we are going to send
607 */
608 pdu = coap_pdu_init(request->type, request->code,
609 coap_new_message_id_lkd(proxy_entry->ongoing),
611 if (!pdu) {
612 goto failed;
613 }
614
615 if (!coap_add_token(pdu, token_len, token)) {
616 goto failed;
617 }
618
619 if (!coap_uri_into_optlist(&server_use.uri,
620 &proxy_entry->ongoing->addr_info.remote,
621 &optlist, 1)) {
622 coap_log_err("Failed to create options for URI\n");
623 goto failed;
624 }
625
626 /* Copy the remaining options across */
627 coap_option_iterator_init(request, &opt_iter, COAP_OPT_ALL);
628 while ((option = coap_option_next(&opt_iter))) {
629 switch (opt_iter.number) {
631 break;
633 break;
638 /* These are not passed on */
639 break;
640 default:
641 coap_insert_optlist(&optlist,
642 coap_new_optlist(opt_iter.number,
643 coap_opt_length(option),
644 coap_opt_value(option)));
645 break;
646 }
647 }
648
649 /* Update pdu with options */
650 coap_add_optlist_pdu(pdu, &optlist);
651 coap_delete_optlist(optlist);
652 break;
656 default:
657 /*
658 * Duplicate request PDU for onward transmission (with new token).
659 */
660 pdu = coap_pdu_duplicate_lkd(request, proxy_entry->ongoing, token_len, token, NULL);
661 if (!pdu) {
662 coap_log_debug("proxy: PDU generation error\n");
663 goto failed;
664 }
665 break;
666 }
667
668 if (coap_get_data_large(request, &size, &data, &offset, &total)) {
669 /* COAP_BLOCK_SINGLE_BODY is set, so single body should be given */
670 assert(size == total);
671 /*
672 * Need to take a copy of the data as request PDU may go away before
673 * all data is transmitted.
674 */
675 body_data = coap_new_binary(total);
676 if (!body_data) {
677 coap_log_debug("proxy: body build memory error\n");
678 goto failed;
679 }
680 memcpy(body_data->s, data, size);
681 if (!coap_add_data_large_request_lkd(proxy_entry->ongoing, pdu, total, data,
682 coap_proxy_release_body_data, body_data)) {
683 coap_log_debug("proxy: add data error\n");
684 goto failed;
685 }
686 }
687
688 if (coap_send_lkd(proxy_entry->ongoing, pdu) == COAP_INVALID_MID) {
689 pdu = NULL;
690 coap_log_debug("proxy: upstream PDU send error\n");
691 goto failed;
692 }
693
694 /*
695 * Do not update the response code (hence empty ACK) as will be sending
696 * separate response when response comes back from upstream server
697 */
698
699 return 1;
700
701failed:
702 response->code = COAP_RESPONSE_CODE(500);
703 coap_delete_pdu(pdu);
704 return 0;
705}
706
709 const coap_pdu_t *received,
710 coap_cache_key_t **cache_key) {
711 int ret;
712
713 coap_lock_lock(session->context, return 0);
715 received,
716 cache_key);
717 coap_lock_unlock(session->context);
718 return ret;
719}
720
723 const coap_pdu_t *received,
724 coap_cache_key_t **cache_key) {
725 coap_pdu_t *pdu = NULL;
726 coap_session_t *incoming = NULL;
727 size_t i;
728 size_t j = 0;
729 size_t size;
730 const uint8_t *data;
731 coap_optlist_t *optlist = NULL;
732 coap_opt_t *option;
733 coap_opt_iterator_t opt_iter;
734 size_t offset;
735 size_t total;
736 coap_proxy_list_t *proxy_entry = NULL;
737 uint16_t media_type = COAP_MEDIATYPE_TEXT_PLAIN;
738 int maxage = -1;
739 uint64_t etag = 0;
740 coap_pdu_code_t rcv_code = coap_pdu_get_code(received);
741 coap_bin_const_t rcv_token = coap_pdu_get_token(received);
742 coap_bin_const_t req_token;
743 coap_binary_t *body_data = NULL;
744 coap_pdu_t *req_pdu;
745 coap_proxy_list_t *proxy_list = session->context->proxy_list;
746 size_t proxy_list_count = session->context->proxy_list_count;
747 coap_resource_t *resource;
748 struct coap_proxy_req_t *proxy_req = NULL;
749
750 for (i = 0; i < proxy_list_count; i++) {
751 proxy_entry = &proxy_list[i];
752 for (j = 0; j < proxy_entry->req_count; j++) {
753 if (coap_binary_equal(&rcv_token, proxy_entry->req_list[j].token_used)) {
754 proxy_req = &proxy_entry->req_list[j];
755 break;
756 }
757 }
758 if (j != proxy_entry->req_count) {
759 break;
760 }
761 }
762 if (i == proxy_list_count) {
763 coap_log_warn("Unknown proxy ongoing session response received\n");
764 return COAP_RESPONSE_OK;
765 }
766
767 req_pdu = proxy_req->pdu;
768 req_token = coap_pdu_get_token(req_pdu);
769 resource = proxy_req->resource;
770 incoming = proxy_req->incoming;
771
772 coap_log_debug("** process upstream incoming %d.%02d response:\n",
773 COAP_RESPONSE_CLASS(rcv_code), rcv_code & 0x1F);
774
775 if (coap_get_data_large(received, &size, &data, &offset, &total)) {
776 /* COAP_BLOCK_SINGLE_BODY is set, so single body should be given */
777 assert(size == total);
778 body_data = coap_new_binary(total);
779 if (!body_data) {
780 coap_log_debug("body build memory error\n");
781 goto remove_match;
782 }
783 memcpy(body_data->s, data, size);
784 data = body_data->s;
785 }
786
787 /*
788 * Build up the ongoing PDU that we are going to send to proxy originator
789 * as separate response
790 */
791 pdu = coap_pdu_init(req_pdu->type, rcv_code,
794 if (!pdu) {
795 coap_log_debug("Failed to create ongoing proxy response PDU\n");
796 goto remove_match;
797 }
798
799 if (!coap_add_token(pdu, req_token.length, req_token.s)) {
800 coap_log_debug("cannot add token to ongoing proxy response PDU\n");
801 }
802
803 /*
804 * Copy the options across, skipping those needed for
805 * coap_add_data_response_large()
806 */
807 coap_option_iterator_init(received, &opt_iter, COAP_OPT_ALL);
808 while ((option = coap_option_next(&opt_iter))) {
809 switch (opt_iter.number) {
811 media_type = coap_decode_var_bytes(coap_opt_value(option),
812 coap_opt_length(option));
813 break;
815 maxage = coap_decode_var_bytes(coap_opt_value(option),
816 coap_opt_length(option));
817 break;
818 case COAP_OPTION_ETAG:
820 coap_opt_length(option));
821 break;
825 break;
826 default:
827 coap_insert_optlist(&optlist,
828 coap_new_optlist(opt_iter.number,
829 coap_opt_length(option),
830 coap_opt_value(option)));
831 break;
832 }
833 }
834 coap_add_optlist_pdu(pdu, &optlist);
835 coap_delete_optlist(optlist);
836
837 if (size > 0) {
838 coap_string_t *l_query = coap_get_query(req_pdu);
839
841 l_query,
842 media_type, maxage, etag, size, data,
843 coap_proxy_release_body_data,
844 body_data);
845 body_data = NULL;
846 coap_delete_string(l_query);
847 }
848
849 if (cache_key)
850 *cache_key = proxy_req->cache_key;
851
853
854remove_match:
855 option = coap_check_option(received, COAP_OPTION_OBSERVE, &opt_iter);
856 /* Need to remove matching token entry (apart from on Observe response) */
857 if (option == NULL && proxy_entry->req_count) {
858 coap_delete_pdu(proxy_entry->req_list[j].pdu);
860 /* Do not delete cache key here - caller's responsibility */
861 proxy_entry->req_count--;
862 if (proxy_entry->req_count-j > 0) {
863 memmove(&proxy_entry->req_list[j], &proxy_entry->req_list[j+1],
864 (proxy_entry->req_count-j) * sizeof(proxy_entry->req_list[0]));
865 }
866 }
867 coap_delete_binary(body_data);
868 return COAP_RESPONSE_OK;
869}
870
871#else /* ! COAP_PROXY_SUPPORT */
872
873int
875 return 0;
876}
877
878COAP_API int
880 const coap_pdu_t *request,
881 coap_pdu_t *response,
884 coap_proxy_server_list_t *server_list) {
885 (void)session;
886 (void)request;
887 (void)resource;
888 (void)cache_key;
889 (void)server_list;
890 response->code = COAP_RESPONSE_CODE(500);
891 return 0;
892}
893
896 const coap_pdu_t *received,
898 (void)session;
899 (void)received;
900 (void)cache_key;
901 return COAP_RESPONSE_OK;
902}
903
904int
906 (void)scheme;
907 return 0;
908}
909#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
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.
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 ...
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:214
#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:1565
#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:179
#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:340
#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:97
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:864
#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:1589
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:895
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:905
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:879
@ 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: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:874
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.
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(.
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