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