libcoap 4.3.5-develop-daa4e05
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
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
38static void
39coap_proxy_del_req(coap_proxy_list_t *proxy_entry, coap_proxy_req_t *proxy_req) {
40 size_t i;
41
42 coap_delete_pdu_lkd(proxy_req->pdu);
43 coap_delete_bin_const(proxy_req->token_used);
44 coap_delete_cache_key(proxy_req->cache_key);
45 if (proxy_req->proxy_cache) {
46 assert(proxy_req->proxy_cache->ref);
47 proxy_req->proxy_cache->ref--;
48 if (proxy_req->proxy_cache->ref == 0) {
49 PROXY_CACHE_DELETE(proxy_entry->rsp_cache, proxy_req->proxy_cache);
50 coap_delete_pdu_lkd(proxy_req->proxy_cache->req_pdu);
51 coap_delete_pdu_lkd(proxy_req->proxy_cache->rsp_pdu);
52 coap_free_type(COAP_STRING, proxy_req->proxy_cache);
53 proxy_req->proxy_cache = NULL;
54 }
55 }
56
57 for (i = 0; i < proxy_entry->req_count; i++) {
58 if (&proxy_entry->req_list[i] == proxy_req) {
59 if (proxy_entry->req_count > 1) {
60 memmove(&proxy_entry->req_list[i], &proxy_entry->req_list[i+1],
61 (proxy_entry->req_count-i-1) * sizeof(proxy_entry->req_list[0]));
62 }
63 proxy_entry->req_count--;
64 break;
65 }
66 }
67}
68
69static void
70coap_proxy_cleanup_entry(coap_proxy_list_t *proxy_entry, int send_failure) {
71 size_t i;
72
73 for (i = 0; i < proxy_entry->req_count; i++) {
74 coap_proxy_req_t *proxy_req = &proxy_entry->req_list[i];
75
76 if (send_failure) {
77 coap_pdu_t *response;
78 coap_bin_const_t l_token;
79
80 /* Need to send back a gateway failure */
81 response = coap_pdu_init(proxy_req->pdu->type,
83 coap_new_message_id_lkd(proxy_entry->incoming),
84 coap_session_max_pdu_size_lkd(proxy_entry->incoming));
85 if (!response) {
86 coap_log_info("PDU creation issue\n");
87 goto cleanup;
88 }
89
90 l_token = coap_pdu_get_token(proxy_req->pdu);
91 if (!coap_add_token(response, l_token.length,
92 l_token.s)) {
93 coap_log_debug("Cannot add token to incoming proxy response PDU\n");
94 }
95
96 if (coap_send_lkd(proxy_entry->incoming, response) == COAP_INVALID_MID) {
97 coap_log_info("Failed to send PDU with 5.02 gateway issue\n");
98 }
99 }
100cleanup:
101 coap_proxy_del_req(proxy_entry, proxy_req);
102 }
103 coap_free_type(COAP_STRING, proxy_entry->req_list);
104 coap_free_type(COAP_STRING, proxy_entry->uri_host_keep);
105}
106
107void
108coap_proxy_cleanup(coap_context_t *context) {
109 size_t i;
110
111 for (i = 0; i < context->proxy_list_count; i++) {
112 /* All sessions have now been closed down */
113 coap_log_debug("proxy_entry %p cleaned up\n",
114 (void *)&context->proxy_list[i]);
115 coap_proxy_cleanup_entry(&context->proxy_list[i], 0);
116 }
117 coap_free_type(COAP_STRING, context->proxy_list);
118}
119
120static int
121coap_proxy_check_observe(coap_proxy_list_t *proxy_entry) {
122 if (proxy_entry && proxy_entry->ongoing) {
123 /* Need to see if there are any Observes active */
124 coap_lg_crcv_t *lg_crcv;
125
126 LL_FOREACH(proxy_entry->ongoing->lg_crcv, lg_crcv) {
127 if (lg_crcv->observe_set) {
128 return 1;
129 }
130 }
131 }
132 return 0;
133}
134
135/*
136 * Return 1 if there is a future expire time, else 0.
137 * Update tim_rem with remaining value if return is 1.
138 */
139int
140coap_proxy_check_timeouts(coap_context_t *context, coap_tick_t now,
141 coap_tick_t *tim_rem) {
142 size_t i;
143 int ret = 0;
144
145 *tim_rem = COAP_MAX_DELAY_TICKS;
146 for (i = 0; i < context->proxy_list_count; i++) {
147 coap_proxy_list_t *proxy_entry = &context->proxy_list[i];
148
149 if (coap_proxy_check_observe(proxy_entry))
150 continue;
151
152 if (proxy_entry->ongoing && proxy_entry->idle_timeout_ticks) {
153 if (proxy_entry->last_used + proxy_entry->idle_timeout_ticks <= now) {
154 /* Drop session to upstream server (which may remove proxy entry) */
155 if (coap_proxy_remove_association(proxy_entry->ongoing, 0))
156 i--;
157 } else {
158 if (*tim_rem > proxy_entry->last_used + proxy_entry->idle_timeout_ticks - now) {
159 *tim_rem = proxy_entry->last_used + proxy_entry->idle_timeout_ticks - now;
160 ret = 1;
161 }
162 }
163 }
164 }
165 return ret;
166}
167
168static int
169coap_get_uri_proxy_scheme_info(const coap_pdu_t *request,
170 coap_opt_t *opt,
171 coap_uri_t *uri) {
172 const char *opt_val = (const char *)coap_opt_value(opt);
173 int opt_len = coap_opt_length(opt);
174 coap_opt_iterator_t opt_iter;
175
176 if (opt_len == 9 &&
177 strncasecmp(opt_val, "coaps+tcp", 9) == 0) {
180 } else if (opt_len == 8 &&
181 strncasecmp(opt_val, "coap+tcp", 8) == 0) {
183 uri->port = COAP_DEFAULT_PORT;
184 } else if (opt_len == 5 &&
185 strncasecmp(opt_val, "coaps", 5) == 0) {
188 } else if (opt_len == 4 &&
189 strncasecmp(opt_val, "coap", 4) == 0) {
191 uri->port = COAP_DEFAULT_PORT;
192 } else if (opt_len == 7 &&
193 strncasecmp(opt_val, "coap+ws", 7) == 0) {
195 uri->port = 80;
196 } else if (opt_len == 8 &&
197 strncasecmp(opt_val, "coaps+ws", 8) == 0) {
199 uri->port = 443;
200 } else {
201 coap_log_warn("Unsupported Proxy Scheme '%*.*s'\n",
202 opt_len, opt_len, opt_val);
203 return 0;
204 }
205
206 opt = coap_check_option(request, COAP_OPTION_URI_HOST, &opt_iter);
207 if (opt) {
208 uri->host.length = coap_opt_length(opt);
209 uri->host.s = coap_opt_value(opt);
210 } else {
211 uri->host.s = NULL;
212 uri->host.length = 0;
213 coap_log_warn("Proxy Scheme requires Uri-Host\n");
214 return 0;
215 }
216 opt = coap_check_option(request, COAP_OPTION_URI_PORT, &opt_iter);
217 if (opt) {
218 uri->port =
220 coap_opt_length(opt));
221 }
222 return 1;
223}
224
225int
227
228 /* Sanity check that the connection can be forwarded on */
229 switch (scheme) {
232 coap_log_warn("Proxy URI http or https not supported\n");
233 return 0;
235 break;
237 if (!coap_dtls_is_supported()) {
238 coap_log_warn("coaps URI scheme not supported for proxy\n");
239 return 0;
240 }
241 break;
243 if (!coap_tcp_is_supported()) {
244 coap_log_warn("coap+tcp URI scheme not supported for proxy\n");
245 return 0;
246 }
247 break;
249 if (!coap_tls_is_supported()) {
250 coap_log_warn("coaps+tcp URI scheme not supported for proxy\n");
251 return 0;
252 }
253 break;
255 if (!coap_ws_is_supported()) {
256 coap_log_warn("coap+ws URI scheme not supported for proxy\n");
257 return 0;
258 }
259 break;
261 if (!coap_wss_is_supported()) {
262 coap_log_warn("coaps+ws URI scheme not supported for proxy\n");
263 return 0;
264 }
265 break;
267 default:
268 coap_log_warn("%d URI scheme not supported\n", scheme);
269 return 0;
270 }
271 return 1;
272}
273
274static coap_proxy_list_t *
275coap_proxy_get_session(coap_session_t *session, const coap_pdu_t *request,
276 coap_pdu_t *response,
277 coap_proxy_server_list_t *server_list,
278 coap_proxy_server_t *server_use, int *proxy_entry_created) {
279 size_t i;
280 coap_proxy_list_t *new_proxy_list;
281 coap_proxy_list_t *proxy_list = session->context->proxy_list;
282 size_t proxy_list_count = session->context->proxy_list_count;
283 coap_opt_iterator_t opt_iter;
284 coap_opt_t *proxy_scheme;
285 coap_opt_t *proxy_uri;
286
287 *proxy_entry_created = 0;
288
289 /*
290 * Maintain server stickability. server_use not needed as there is
291 * ongoing session in place.
292 */
293 if (session->proxy_entry) {
294 for (i = 0; i < proxy_list_count; i++) {
295 if (&proxy_list[i] == session->proxy_entry) {
296 if (session->proxy_entry->ongoing) {
297 memset(server_use, 0, sizeof(*server_use));
298 return session->proxy_entry;
299 }
300 }
301 }
302 }
303
304 /* Round robin the defined next server list (which usually is just one */
305 server_list->next_entry++;
306 if (server_list->next_entry >= server_list->entry_count)
307 server_list->next_entry = 0;
308
309 if (server_list->entry_count) {
310 memcpy(server_use, &server_list->entry[server_list->next_entry], sizeof(*server_use));
311 } else {
312 memset(server_use, 0, sizeof(*server_use));
313 }
314
315 switch (server_list->type) {
320 /* Nothing else needs to be done here */
321 break;
324 /* Need to get actual server from CoAP Proxy-Uri or Proxy-Scheme options */
325 /*
326 * See if Proxy-Scheme
327 */
328 proxy_scheme = coap_check_option(request, COAP_OPTION_PROXY_SCHEME, &opt_iter);
329 if (proxy_scheme) {
330 if (!coap_get_uri_proxy_scheme_info(request, proxy_scheme, &server_use->uri)) {
331 response->code = COAP_RESPONSE_CODE(505);
332 return NULL;
333 }
334 }
335 /*
336 * See if Proxy-Uri
337 */
338 proxy_uri = coap_check_option(request, COAP_OPTION_PROXY_URI, &opt_iter);
339 if (proxy_uri) {
340 coap_log_info("Proxy URI '%.*s'\n",
341 (int)coap_opt_length(proxy_uri),
342 (const char *)coap_opt_value(proxy_uri));
344 coap_opt_length(proxy_uri),
345 &server_use->uri) < 0) {
346 /* Need to return a 5.05 RFC7252 Section 5.7.2 */
347 coap_log_warn("Proxy URI not decodable\n");
348 response->code = COAP_RESPONSE_CODE(505);
349 return NULL;
350 }
351 }
352
353 if (!(proxy_scheme || proxy_uri)) {
354 response->code = COAP_RESPONSE_CODE(404);
355 return NULL;
356 }
357 break;
358 default:
359 assert(0);
360 return NULL;
361 }
362
363 if (server_use->uri.host.length == 0) {
364 /* Ongoing connection not well formed */
365 response->code = COAP_RESPONSE_CODE(505);
366 return NULL;
367 }
368
370 response->code = COAP_RESPONSE_CODE(505);
371 return NULL;
372 }
373
374 /* See if we are already connected to the Server */
375 for (i = 0; i < proxy_list_count; i++) {
376 if (coap_string_equal(&proxy_list[i].uri.host, &server_use->uri.host) &&
377 proxy_list[i].uri.port == server_use->uri.port &&
378 proxy_list[i].uri.scheme == server_use->uri.scheme) {
379 if (!server_list->track_client_session && session->context->proxy_response_handler) {
380 coap_ticks(&proxy_list[i].last_used);
381 return &proxy_list[i];
382 } else {
383 if (proxy_list[i].incoming == session) {
384 coap_ticks(&proxy_list[i].last_used);
385 return &proxy_list[i];
386 }
387 }
388 }
389 }
390
391 /* Need to create a new forwarding mapping */
392 new_proxy_list = coap_realloc_type(COAP_STRING, proxy_list, (i+1)*sizeof(proxy_list[0]));
393
394 if (new_proxy_list == NULL) {
395 response->code = COAP_RESPONSE_CODE(500);
396 return NULL;
397 }
398 session->context->proxy_list = proxy_list = new_proxy_list;
399 memset(&proxy_list[i], 0, sizeof(proxy_list[i]));
400
401 /* Keep a copy of the host as server_use->uri pointed to will be going away */
402 proxy_list[i].uri = server_use->uri;
403 proxy_list[i].uri_host_keep = coap_malloc_type(COAP_STRING,
404 server_use->uri.host.length);
405 if (!proxy_list[i].uri_host_keep) {
406 response->code = COAP_RESPONSE_CODE(500);
407 return NULL;
408 }
409 memcpy(proxy_list[i].uri_host_keep, server_use->uri.host.s,
410 server_use->uri.host.length);
411 proxy_list[i].uri.host.s = proxy_list[i].uri_host_keep;
412 /* Unset uri parts which point to going away information */
413 proxy_list[i].uri.path.s = NULL;
414 proxy_list[i].uri.path.length = 0;
415 proxy_list[i].uri.query.s = NULL;
416 proxy_list[i].uri.query.length = 0;
417
418 if (server_list->track_client_session) {
419 proxy_list[i].incoming = session;
420 }
421 *proxy_entry_created = 1;
422 session->context->proxy_list_count++;
423 proxy_list[i].idle_timeout_ticks = server_list->idle_timeout_secs * COAP_TICKS_PER_SECOND;
424 coap_ticks(&proxy_list[i].last_used);
425 session->proxy_entry = &proxy_list[i];
426 return &proxy_list[i];
427}
428
429int
430coap_proxy_remove_association(coap_session_t *session, int send_failure) {
431
432 size_t i;
433 size_t j;
434 coap_proxy_list_t *proxy_list = session->context->proxy_list;
435 size_t proxy_list_count = session->context->proxy_list_count;
436
437 for (i = 0; i < proxy_list_count; i++) {
438 coap_proxy_list_t *proxy_entry = &proxy_list[i];
439
440 /* Check for incoming match */
441 for (j = 0; j < proxy_entry->req_count; j++) {
442 coap_proxy_req_t *proxy_req = &proxy_entry->req_list[j];
443
444 if (proxy_req->incoming == session) {
445 coap_proxy_del_req(proxy_entry, proxy_req);
446 break;
447 }
448 }
449 if (proxy_entry->incoming == session) {
450 /* Only if there is a one-to-one tracking */
451 coap_session_t *ongoing = proxy_entry->ongoing;
452
453 proxy_entry->ongoing = NULL;
455 return 0;
456 }
457
458 /* Check for outgoing match */
459 if (proxy_entry->ongoing == session) {
460 coap_session_t *ongoing;
461
462 coap_proxy_cleanup_entry(proxy_entry, send_failure);
463 ongoing = proxy_entry->ongoing;
464 coap_log_debug("* %s: proxy_entry %p released (rem count = %zd)\n",
465 coap_session_str(ongoing),
466 (void *)proxy_entry,
467 session->context->proxy_list_count - 1);
468 if (proxy_list_count-i > 1) {
469 memmove(&proxy_list[i],
470 &proxy_list[i+1],
471 (proxy_list_count-i-1) * sizeof(proxy_list[0]));
472 }
473 session->context->proxy_list_count--;
475 return 1;
476 }
477 }
478 return 0;
479}
480
481static coap_proxy_list_t *
482coap_proxy_get_ongoing_session(coap_session_t *session,
483 const coap_pdu_t *request,
484 coap_pdu_t *response,
485 coap_proxy_server_list_t *server_list) {
486
487 coap_address_t dst;
488 coap_proto_t proto;
489 coap_addr_info_t *info_list = NULL;
490 coap_proxy_list_t *proxy_entry;
491 coap_context_t *context = session->context;
492 static char client_sni[256];
493 coap_proxy_server_t server_use;
494 int proxy_entry_created;
495
496 proxy_entry = coap_proxy_get_session(session, request, response, server_list,
497 &server_use, &proxy_entry_created);
498 if (!proxy_entry) {
499 /* Error response code already set */
500 return NULL;
501 }
502
503 if (!proxy_entry->ongoing) {
504 /* Need to create a new session */
505 coap_address_t *local_addr = NULL;
506
507 /* resolve destination address where data should be sent */
508 info_list = coap_resolve_address_info(&server_use.uri.host,
509 server_use.uri.port,
510 server_use.uri.port,
511 server_use.uri.port,
512 server_use.uri.port,
513 0,
514 1 << server_use.uri.scheme,
516
517 if (info_list == NULL) {
518 response->code = COAP_RESPONSE_CODE(502);
519 coap_proxy_remove_association(session, 0);
520 return NULL;
521 }
522 proto = info_list->proto;
523 memcpy(&dst, &info_list->addr, sizeof(dst));
524 coap_free_address_info(info_list);
525
526#if COAP_AF_UNIX_SUPPORT
527 coap_address_t bind_addr;
528 if (coap_is_af_unix(&dst)) {
529 char buf[COAP_UNIX_PATH_MAX];
530 coap_tick_t now;
531
532 /* Need a unique 'client' address */
533 coap_ticks(&now);
534 snprintf(buf, COAP_UNIX_PATH_MAX,
535 "/tmp/coap-pr-cl-%" PRIu64, (uint64_t)now);
536 if (!coap_address_set_unix_domain(&bind_addr, (const uint8_t *)buf,
537 strlen(buf))) {
538 fprintf(stderr, "coap_address_set_unix_domain: %s: failed\n",
539 buf);
540 remove(buf);
541 return NULL;
542 }
543 (void)remove(buf);
544 local_addr = &bind_addr;
545 }
546#endif /* COAP_AF_UNIX_SUPPORT */
547
548 snprintf(client_sni, sizeof(client_sni), "%*.*s", (int)server_use.uri.host.length,
549 (int)server_use.uri.host.length, server_use.uri.host.s);
550
551 switch (server_use.uri.scheme) {
555#if COAP_OSCORE_SUPPORT
556 if (server_use.oscore_conf) {
557 proxy_entry->ongoing =
558 coap_new_client_session_oscore_lkd(context, local_addr, &dst,
559 proto, server_use.oscore_conf);
560 } else {
561#endif /* COAP_OSCORE_SUPPORT */
562 proxy_entry->ongoing =
563 coap_new_client_session_lkd(context, local_addr, &dst, proto);
564#if COAP_OSCORE_SUPPORT
565 }
566#endif /* COAP_OSCORE_SUPPORT */
567 break;
571#if COAP_OSCORE_SUPPORT
572 if (server_use.oscore_conf) {
573 if (server_use.dtls_pki) {
574 server_use.dtls_pki->client_sni = client_sni;
575 proxy_entry->ongoing =
576 coap_new_client_session_oscore_pki_lkd(context, local_addr, &dst,
577 proto, server_use.dtls_pki, server_use.oscore_conf);
578 } else if (server_use.dtls_cpsk) {
579 server_use.dtls_cpsk->client_sni = client_sni;
580 proxy_entry->ongoing =
581 coap_new_client_session_oscore_psk_lkd(context, local_addr, &dst,
582 proto, server_use.dtls_cpsk, server_use.oscore_conf);
583 } else {
584 coap_log_warn("Proxy: (D)TLS not configured for secure session\n");
585 }
586 } else {
587#endif /* COAP_OSCORE_SUPPORT */
588 /* Not doing OSCORE */
589 if (server_use.dtls_pki) {
590 server_use.dtls_pki->client_sni = client_sni;
591 proxy_entry->ongoing =
592 coap_new_client_session_pki_lkd(context, local_addr, &dst,
593 proto, server_use.dtls_pki);
594 } else if (server_use.dtls_cpsk) {
595 server_use.dtls_cpsk->client_sni = client_sni;
596 proxy_entry->ongoing =
597 coap_new_client_session_psk2_lkd(context, local_addr, &dst,
598 proto, server_use.dtls_cpsk);
599 } else {
600 /* Using client anonymous PKI */
601 proxy_entry->ongoing =
602 coap_new_client_session_lkd(context, local_addr, &dst, proto);
603 }
604#if COAP_OSCORE_SUPPORT
605 }
606#endif /* COAP_OSCORE_SUPPORT */
607 break;
611 default:
612 assert(0);
613 break;
614 }
615 if (proxy_entry->ongoing == NULL) {
616 response->code = COAP_RESPONSE_CODE(505);
617 coap_proxy_remove_association(session, 0);
618 return NULL;
619 }
620 if (proxy_entry_created) {
621 coap_log_debug("* %s: proxy_entry %p created (tot count = %zd)\n",
622 coap_session_str(proxy_entry->ongoing),
623 (void *)proxy_entry,
624 session->context->proxy_list_count);
625 }
626 } else if (proxy_entry->ongoing->session_failed) {
627 if (!coap_session_reconnect(proxy_entry->ongoing)) {
628 /* Server is not yet back up */
629 return NULL;
630 }
631 }
632
633 return proxy_entry;
634}
635
636static void
637coap_proxy_release_body_data(coap_session_t *session COAP_UNUSED,
638 void *app_ptr) {
639 coap_delete_binary(app_ptr);
640}
641
642static coap_proxy_req_t *
643coap_proxy_get_req(coap_proxy_list_t *proxy_entry, coap_session_t *session) {
644 size_t i;
645
646 for (i = 0; i < proxy_entry->req_count; i++) {
647 if (proxy_entry->req_list[i].incoming == session) {
648 return &proxy_entry->req_list[i];
649 }
650 }
651 return NULL;
652}
653
654static void
655coap_proxy_free_response_data(coap_session_t *session COAP_UNUSED, void *app_ptr) {
656 coap_delete_bin_const(app_ptr);
657}
658
659static coap_response_t
660coap_proxy_call_response_handler(coap_session_t *session, const coap_pdu_t *sent,
661 coap_pdu_t *rcvd, coap_bin_const_t *token,
662 coap_proxy_req_t *proxy_req, int replace_mid) {
664 coap_pdu_t *resp_pdu;
665 coap_pdu_t *fwd_pdu = NULL;
666 size_t size;
667 size_t offset;
668 size_t total;
669 const uint8_t *data;
670 coap_string_t *l_query = NULL;
671
672 /* Correct the token */
673 resp_pdu = coap_pdu_duplicate_lkd(rcvd, session, token->length, token->s, NULL);
674 if (!resp_pdu)
675 return COAP_RESPONSE_FAIL;
676
677 if (replace_mid)
678 resp_pdu->mid = rcvd->mid;
679 if (coap_get_data_large(rcvd, &size, &data, &offset, &total)) {
680 uint16_t media_type = 0;
681 int maxage = -1;
682 uint64_t etag = 0;
683 coap_opt_t *option;
684 coap_opt_iterator_t opt_iter;
685 coap_bin_const_t *body;
686
687 /* COAP_BLOCK_SINGLE_BODY is set, so single body should be given */
688 assert(size == total);
689
690 body = coap_new_bin_const(data, size);
691 if (!body) {
692 coap_log_debug("coap_proxy_call_response_handler: copy data error\n");
693 goto failed;
694 }
695 option = coap_check_option(rcvd, COAP_OPTION_CONTENT_FORMAT, &opt_iter);
696 if (option) {
697 media_type = coap_decode_var_bytes(coap_opt_value(option),
698 coap_opt_length(option));
699 }
700 option = coap_check_option(rcvd, COAP_OPTION_MAXAGE, &opt_iter);
701 if (option) {
702 maxage = coap_decode_var_bytes(coap_opt_value(option),
703 coap_opt_length(option));
704 }
705 option = coap_check_option(rcvd, COAP_OPTION_ETAG, &opt_iter);
706 if (option) {
708 coap_opt_length(option));
709 }
710 if (sent)
711 l_query = coap_get_query(sent);
712 if (!coap_add_data_large_response_lkd(proxy_req->resource, session, sent,
713 resp_pdu,
714 l_query,
715 media_type, maxage, etag, body->length,
716 body->s,
717 coap_proxy_free_response_data,
718 body)) {
719 coap_log_debug("coap_proxy_call_response_handler: add data error\n");
720 goto failed;
721 }
722 }
723 coap_lock_callback_ret_release(fwd_pdu, session->context,
724 session->context->proxy_response_handler(session,
725 sent,
726 resp_pdu,
727 proxy_req->cache_key),
728 /* context is being freed off */
729 goto failed);
730 if (fwd_pdu) {
731 ret = COAP_RESPONSE_OK;
732 if (coap_send_lkd(session, fwd_pdu) == COAP_INVALID_MID) {
733 ret = COAP_RESPONSE_FAIL;
734 }
735 if (fwd_pdu != resp_pdu) {
736 /* Application created a new PDU */
737 coap_delete_pdu_lkd(resp_pdu);
738 }
739 } else {
740failed:
741 ret = COAP_RESPONSE_FAIL;
742 coap_delete_pdu_lkd(resp_pdu);
743 }
744 coap_delete_string(l_query);
745 return ret;
746}
747
748int COAP_API
750 const coap_pdu_t *request,
751 coap_pdu_t *response,
752 coap_resource_t *resource,
753 coap_cache_key_t *cache_key,
754 coap_proxy_server_list_t *server_list) {
755 int ret;
756
757 coap_lock_lock(session->context, return 0);
758 ret = coap_proxy_forward_request_lkd(session,
759 request,
760 response,
761 resource,
762 cache_key,
763 server_list);
764 coap_lock_unlock(session->context);
765 return ret;
766}
767
768/* https://rfc-editor.org/rfc/rfc7641#section-3.6 */
769static const uint16_t coap_proxy_ignore_options[] = { COAP_OPTION_ETAG,
774 };
775
776int
777coap_proxy_forward_request_lkd(coap_session_t *session,
778 const coap_pdu_t *request,
779 coap_pdu_t *response,
780 coap_resource_t *resource,
781 coap_cache_key_t *cache_key,
782 coap_proxy_server_list_t *server_list) {
783 coap_proxy_list_t *proxy_entry;
784 size_t size;
785 size_t offset;
786 size_t total;
787 coap_binary_t *body_data = NULL;
788 const uint8_t *data;
789 coap_pdu_t *pdu = NULL;
790 coap_bin_const_t r_token = coap_pdu_get_token(request);
791 uint8_t token[8];
792 size_t token_len;
793 coap_proxy_req_t *proxy_req = NULL;
794 coap_optlist_t *optlist = NULL;
795 coap_opt_t *option;
796 coap_opt_iterator_t opt_iter;
797 coap_uri_t uri;
798 coap_opt_t *obs_opt = coap_check_option(request,
800 &opt_iter);
801 coap_proxy_cache_t *proxy_cache = NULL;
802
803 /* Set up ongoing session (if not already done) */
804 proxy_entry = coap_proxy_get_ongoing_session(session, request, response,
805 server_list);
806 if (!proxy_entry) {
807 /* Error response code already set */
808 return 0;
809 }
810
811 /* Is this a observe cached request? */
812 if (obs_opt && session->context->proxy_response_handler) {
813 coap_cache_key_t *cache_key_l;
814 coap_tick_t now;
815
816 cache_key_l = coap_cache_derive_key_w_ignore(session, request,
818 coap_proxy_ignore_options,
819 sizeof(coap_proxy_ignore_options)/sizeof(coap_proxy_ignore_options[0]));
820 if (!cache_key_l) {
821 response->code = COAP_RESPONSE_CODE(505);
822 return 0;
823 }
824 PROXY_CACHE_FIND(proxy_entry->rsp_cache, cache_key_l, proxy_cache);
825 coap_delete_cache_key(cache_key_l);
826 coap_ticks(&now);
827 if (proxy_cache && (proxy_cache->expire + COAP_TICKS_PER_SECOND) < now) {
828 /* Need to get an updated rsp_pdu */
829 proxy_cache = NULL;
830 }
831 }
832
833 if (proxy_cache) {
834 proxy_req = coap_proxy_get_req(proxy_entry, session);
835 if (proxy_req) {
836 if (obs_opt) {
837 int observe_action;
838
839 observe_action = coap_decode_var_bytes(coap_opt_value(obs_opt),
840 coap_opt_length(obs_opt));
841
842 if (observe_action == COAP_OBSERVE_CANCEL) {
843 assert(proxy_cache->ref);
844 proxy_cache->ref--;
845 if (proxy_cache->ref > 0) {
846 goto return_cached_info;
847 }
848 proxy_cache = NULL;
849 if (proxy_req->proxy_cache->ref == 0) {
850 PROXY_CACHE_DELETE(proxy_entry->rsp_cache, proxy_req->proxy_cache);
851 coap_delete_pdu_lkd(proxy_req->proxy_cache->req_pdu);
852 coap_delete_pdu_lkd(proxy_req->proxy_cache->rsp_pdu);
853 coap_free_type(COAP_STRING, proxy_req->proxy_cache);
854 proxy_req->proxy_cache = NULL;
855 }
856 /* Last user of proxy_cache. Need to de-register upstream */
857 } else if (observe_action == COAP_OBSERVE_ESTABLISH) {
858 /* Client must be re-registering */
859 goto return_cached_info;
860 }
861 } else {
862 goto return_cached_info;
863 }
864 }
865 }
866
867 if (!proxy_req) {
868 coap_proxy_req_t *new_req_list;
869
870 new_req_list = coap_realloc_type(COAP_STRING, proxy_entry->req_list,
871 (proxy_entry->req_count + 1)*sizeof(coap_proxy_req_t));
872
873 if (new_req_list == NULL) {
874 goto failed;
875 }
876 proxy_entry->req_list = new_req_list;
877 proxy_req = &new_req_list[proxy_entry->req_count];
878 memset(proxy_req, 0, sizeof(coap_proxy_req_t));
879
880 /* Get a new token for ongoing session */
881 coap_session_new_token(proxy_entry->ongoing, &token_len, token);
882 proxy_req->token_used = coap_new_bin_const(token, token_len);
883 if (proxy_req->token_used == NULL) {
884 goto failed;
885 }
886 proxy_req->pdu = coap_const_pdu_reference_lkd(request);
887 proxy_req->resource = resource;
888 proxy_req->incoming = session;
889 proxy_req->cache_key = cache_key;
890 proxy_req->proxy_cache = proxy_cache;
891 proxy_entry->req_count++;
892 } else if (obs_opt) {
893 /* Need to reuse token */
894 memcpy(token, proxy_req->token_used->s, proxy_req->token_used->length);
895 token_len = proxy_req->token_used->length;
896 } else {
897 /* Need to refresh used token */
898 coap_session_new_token(proxy_entry->ongoing, &token_len, token);
899 coap_delete_bin_const(proxy_req->token_used);
900 proxy_req->token_used = coap_new_bin_const(token, token_len);
901 if (proxy_req->token_used == NULL) {
902 goto failed;
903 }
904 }
905
906 if (proxy_cache) {
907 if (obs_opt)
908 proxy_cache->ref++;
909 goto return_cached_info;
910 }
911
912 /* Need to save the request pdu entry */
913 switch (server_list->type) {
917 /*
918 * Need to replace Proxy-Uri with Uri-Host (and Uri-Port)
919 * and strip out Proxy-Scheme.
920 */
921
922 /*
923 * Build up the ongoing PDU that we are going to send
924 */
925 pdu = coap_pdu_init(request->type, request->code,
926 coap_new_message_id_lkd(proxy_entry->ongoing),
927 coap_session_max_pdu_size_lkd(proxy_entry->ongoing));
928 if (!pdu) {
929 goto failed;
930 }
931
932 if (!coap_add_token(pdu, token_len, token)) {
933 goto failed;
934 }
935
936 /* Copy the remaining options across */
937 coap_option_iterator_init(request, &opt_iter, COAP_OPT_ALL);
938 while ((option = coap_option_next(&opt_iter))) {
939 switch (opt_iter.number) {
942 coap_opt_length(option),
943 &uri) < 0) {
944 /* Need to return a 5.05 RFC7252 Section 5.7.2 */
945 coap_log_warn("Proxy URI not decodable\n");
947 return 0;
948 }
949 if (!coap_uri_into_optlist(&uri, NULL, &optlist, 0)) {
950 coap_log_err("Failed to create options for URI\n");
951 goto failed;
952 }
953 break;
955 break;
960 /* These are not passed on */
961 break;
964 break;
965 default:
966 coap_insert_optlist(&optlist,
967 coap_new_optlist(opt_iter.number,
968 coap_opt_length(option),
969 coap_opt_value(option)));
970 break;
971 }
972 }
973
974 /* Update pdu with options */
975 coap_add_optlist_pdu(pdu, &optlist);
976 coap_delete_optlist(optlist);
977 break;
981 default:
982 /*
983 * Duplicate request PDU for onward transmission (with new token).
984 */
985 pdu = coap_pdu_duplicate_lkd(request, proxy_entry->ongoing, token_len, token, NULL);
986 if (!pdu) {
987 coap_log_debug("proxy: PDU generation error\n");
988 goto failed;
989 }
990 break;
991 }
992
993 if (coap_get_data_large(request, &size, &data, &offset, &total)) {
994 /* COAP_BLOCK_SINGLE_BODY is set, so single body should be given */
995 assert(size == total);
996 /*
997 * Need to take a copy of the data as request PDU may go away before
998 * all data is transmitted.
999 */
1000 body_data = coap_new_binary(total);
1001 if (!body_data) {
1002 coap_log_debug("proxy: body build memory error\n");
1003 goto failed;
1004 }
1005 memcpy(body_data->s, data, size);
1006 if (!coap_add_data_large_request_lkd(proxy_entry->ongoing, pdu, total, data,
1007 coap_proxy_release_body_data, body_data)) {
1008 coap_log_debug("proxy: add data error\n");
1009 goto failed;
1010 }
1011 }
1012
1013 if (coap_send_lkd(proxy_entry->ongoing, pdu) == COAP_INVALID_MID) {
1014 pdu = NULL;
1015 coap_log_debug("proxy: upstream PDU send error\n");
1016 goto failed;
1017 }
1018
1019 /*
1020 * Do not update the response code (hence empty ACK) as will be sending
1021 * separate response when response comes back from upstream server
1022 */
1023
1024 return 1;
1025
1026failed:
1027 response->code = COAP_RESPONSE_CODE(500);
1029 return 0;
1030
1031return_cached_info:
1032 coap_proxy_call_response_handler(session, request, proxy_cache->rsp_pdu,
1033 &r_token, proxy_req, 1);
1034 if (!obs_opt)
1035 coap_proxy_del_req(proxy_entry, proxy_req);
1036 return 1;
1037}
1038
1039struct coap_proxy_req_t *
1040coap_proxy_map_outgoing_request(coap_session_t *ongoing,
1041 const coap_pdu_t *received,
1042 coap_proxy_list_t **proxy_entry) {
1043 coap_proxy_list_t *proxy_list = ongoing->context->proxy_list;
1044 size_t proxy_list_count = ongoing->context->proxy_list_count;
1045 size_t i;
1046 size_t j;
1047 coap_bin_const_t rcv_token = coap_pdu_get_token(received);
1048 coap_proxy_list_t *l_proxy_entry = NULL;
1049
1050 for (i = 0; i < proxy_list_count; i++) {
1051 l_proxy_entry = &proxy_list[i];
1052 if (l_proxy_entry->ongoing == ongoing) {
1053 for (j = 0; j < l_proxy_entry->req_count; j++) {
1054 if (coap_binary_equal(&rcv_token, l_proxy_entry->req_list[j].token_used)) {
1055 coap_ticks(&l_proxy_entry->last_used);
1056 if (proxy_entry)
1057 *proxy_entry = l_proxy_entry;
1058 return &l_proxy_entry->req_list[j];
1059 }
1060 }
1061 }
1062 }
1063 return NULL;
1064}
1065
1068 const coap_pdu_t *received,
1069 coap_cache_key_t **cache_key) {
1070 int ret;
1071
1072 coap_lock_lock(session->context, return 0);
1073 ret = coap_proxy_forward_response_lkd(session,
1074 received,
1075 cache_key);
1076 coap_lock_unlock(session->context);
1077 return ret;
1078}
1079
1081coap_proxy_forward_response_lkd(coap_session_t *session,
1082 const coap_pdu_t *received,
1083 coap_cache_key_t **cache_key) {
1084 coap_pdu_t *pdu = NULL;
1085 coap_session_t *incoming = NULL;
1086 size_t size;
1087 const uint8_t *data;
1088 coap_optlist_t *optlist = NULL;
1089 coap_opt_t *option;
1090 coap_opt_iterator_t opt_iter;
1091 size_t offset;
1092 size_t total;
1093 coap_proxy_list_t *proxy_entry = NULL;
1094 uint16_t media_type = COAP_MEDIATYPE_TEXT_PLAIN;
1095 int maxage = -1;
1096 uint64_t etag = 0;
1097 coap_pdu_code_t rcv_code = coap_pdu_get_code(received);
1098 coap_bin_const_t req_token;
1099 coap_binary_t *body_data = NULL;
1100 coap_pdu_t *req_pdu;
1101 coap_resource_t *resource;
1102 struct coap_proxy_req_t *proxy_req = NULL;
1103
1104 proxy_req = coap_proxy_map_outgoing_request(session, received, &proxy_entry);
1105 if (!proxy_req || proxy_req->incoming->server_list) {
1106 coap_log_warn("Unknown proxy ongoing session response received - ignored\n");
1107 return COAP_RESPONSE_OK;
1108 }
1109
1110 req_pdu = proxy_req->pdu;
1111 req_token = coap_pdu_get_token(req_pdu);
1112 resource = proxy_req->resource;
1113 incoming = proxy_req->incoming;
1114
1115 coap_log_debug("** process upstream incoming %d.%02d response:\n",
1116 COAP_RESPONSE_CLASS(rcv_code), rcv_code & 0x1F);
1117
1118 if (coap_get_data_large(received, &size, &data, &offset, &total)) {
1119 /* COAP_BLOCK_SINGLE_BODY is set, so single body should be given */
1120 assert(size == total);
1121 body_data = coap_new_binary(total);
1122 if (!body_data) {
1123 coap_log_debug("body build memory error\n");
1124 goto remove_match;
1125 }
1126 memcpy(body_data->s, data, size);
1127 data = body_data->s;
1128 }
1129
1130 /*
1131 * Build up the ongoing PDU that we are going to send to proxy originator
1132 * as separate response
1133 */
1134 pdu = coap_pdu_init(req_pdu->type, rcv_code,
1135 coap_new_message_id_lkd(incoming),
1137 if (!pdu) {
1138 coap_log_debug("Failed to create ongoing proxy response PDU\n");
1139 goto remove_match;
1140 }
1141
1142 if (!coap_add_token(pdu, req_token.length, req_token.s)) {
1143 coap_log_debug("cannot add token to ongoing proxy response PDU\n");
1144 }
1145
1146 /*
1147 * Copy the options across, skipping those needed for
1148 * coap_add_data_response_large()
1149 */
1150 coap_option_iterator_init(received, &opt_iter, COAP_OPT_ALL);
1151 while ((option = coap_option_next(&opt_iter))) {
1152 switch (opt_iter.number) {
1154 media_type = coap_decode_var_bytes(coap_opt_value(option),
1155 coap_opt_length(option));
1156 break;
1157 case COAP_OPTION_MAXAGE:
1158 maxage = coap_decode_var_bytes(coap_opt_value(option),
1159 coap_opt_length(option));
1160 break;
1161 case COAP_OPTION_ETAG:
1163 coap_opt_length(option));
1164 break;
1165 case COAP_OPTION_BLOCK2:
1167 case COAP_OPTION_SIZE2:
1168 break;
1169 default:
1170 coap_insert_optlist(&optlist,
1171 coap_new_optlist(opt_iter.number,
1172 coap_opt_length(option),
1173 coap_opt_value(option)));
1174 break;
1175 }
1176 }
1177 coap_add_optlist_pdu(pdu, &optlist);
1178 coap_delete_optlist(optlist);
1179
1180 if (size > 0) {
1181 coap_string_t *l_query = coap_get_query(req_pdu);
1182
1183 coap_add_data_large_response_lkd(resource, incoming, req_pdu, pdu,
1184 l_query,
1185 media_type, maxage, etag, size, data,
1186 coap_proxy_release_body_data,
1187 body_data);
1188 body_data = NULL;
1189 coap_delete_string(l_query);
1190 }
1191
1192 if (cache_key)
1193 *cache_key = proxy_req->cache_key;
1194
1195 coap_send_lkd(incoming, pdu);
1196
1197remove_match:
1198 option = coap_check_option(received, COAP_OPTION_OBSERVE, &opt_iter);
1199 /* Need to remove matching token entry (apart from an Observe response) */
1200 if (option == NULL && proxy_entry->req_count) {
1201 /* Do not delete cache key here - caller's responsibility */
1202 proxy_req->cache_key = NULL;
1203 coap_proxy_del_req(proxy_entry, proxy_req);
1204 }
1205 coap_delete_binary(body_data);
1206 return COAP_RESPONSE_OK;
1207}
1208
1209void
1210coap_proxy_process_incoming(coap_session_t *session,
1211 coap_pdu_t *rcvd,
1212 void *body_data, coap_proxy_req_t *proxy_req,
1213 coap_proxy_list_t *proxy_entry) {
1214 coap_opt_t *obs_opt;
1215 coap_opt_t *option;
1216 coap_opt_iterator_t opt_iter;
1218 coap_bin_const_t token;
1219
1220 obs_opt = coap_check_option(rcvd, COAP_OPTION_OBSERVE, &opt_iter);
1221
1222 /* See if we are doing proxy caching */
1223 if (obs_opt) {
1224 coap_proxy_cache_t *proxy_cache;
1225 coap_cache_key_t *cache_key_l;
1226 coap_tick_t now;
1227 uint64_t expire;
1228 size_t i;
1229
1230 /* Need to cache the response */
1231 if (proxy_req->proxy_cache) {
1232 coap_delete_pdu_lkd(proxy_req->proxy_cache->rsp_pdu);
1233 proxy_cache = proxy_req->proxy_cache;
1234 } else {
1235 proxy_cache = coap_malloc_type(COAP_STRING, sizeof(coap_proxy_cache_t));
1236 if (proxy_cache == NULL) {
1237 goto cache_fail;
1238 }
1239 memset(proxy_cache, 0, sizeof(coap_proxy_cache_t));
1240 cache_key_l = coap_cache_derive_key_w_ignore(session, proxy_req->pdu,
1242 coap_proxy_ignore_options,
1243 sizeof(coap_proxy_ignore_options)/sizeof(coap_proxy_ignore_options[0]));
1244 if (!cache_key_l) {
1245 coap_free_type(COAP_STRING, proxy_cache);
1246 goto cache_fail;
1247 }
1248 memcpy(&proxy_cache->cache_req, cache_key_l,
1249 sizeof(proxy_cache->cache_req));
1250 coap_delete_cache_key(cache_key_l);
1251
1252 proxy_cache->req_pdu = coap_pdu_reference_lkd(proxy_req->pdu);
1253 proxy_req->proxy_cache = proxy_cache;
1254
1255 PROXY_CACHE_ADD(proxy_entry->rsp_cache, proxy_cache);
1256 proxy_cache->ref++;
1257 }
1258 proxy_cache->rsp_pdu = coap_pdu_reference_lkd(rcvd);
1259 option = coap_check_option(rcvd, COAP_OPTION_ETAG, &opt_iter);
1260 if (option) {
1261 proxy_cache->etag = coap_decode_var_bytes8(coap_opt_value(option),
1262 coap_opt_length(option));
1263 } else {
1264 proxy_cache->etag = 0;
1265 }
1266 coap_ticks(&now);
1267 option = coap_check_option(rcvd, COAP_OPTION_MAXAGE, &opt_iter);
1268 if (option) {
1269 expire = coap_decode_var_bytes(coap_opt_value(option),
1270 coap_opt_length(option));
1271 } else {
1272 /* Default is 60 seconds */
1273 expire = 60;
1274 }
1275 proxy_cache->expire = now + expire * COAP_TICKS_PER_SECOND;
1276
1277 /* Update all the cache listeners */
1278 for (i = 0; i < proxy_entry->req_count; i++) {
1279 if (proxy_entry->req_list[i].proxy_cache == proxy_cache) {
1280 proxy_req = &proxy_entry->req_list[i];
1281 token = coap_pdu_get_token(proxy_req->pdu);
1282 if (coap_proxy_call_response_handler(proxy_req->incoming, proxy_req->pdu,
1283 rcvd, &token,
1284 proxy_req, 0) == COAP_RESPONSE_OK) {
1285 /* At least one success */
1286 ret = COAP_RESPONSE_OK;
1287 }
1288 }
1289 }
1290 goto finish;
1291 }
1292cache_fail:
1293 token = coap_pdu_get_token(proxy_req->pdu);
1294 ret = coap_proxy_call_response_handler(proxy_req->incoming, proxy_req->pdu,
1295 rcvd, &token, proxy_req, 0);
1296
1297finish:
1298 if (ret == COAP_RESPONSE_FAIL && rcvd->type != COAP_MESSAGE_ACK) {
1299 coap_send_rst_lkd(session, rcvd);
1301 } else {
1302 coap_send_ack_lkd(session, rcvd);
1304 }
1305 coap_free_type(COAP_STRING, body_data);
1306}
1307
1308/*
1309 */
1311coap_proxy_local_write(coap_session_t *session, coap_pdu_t *pdu) {
1312 coap_pdu_t *response = NULL;
1313 coap_resource_t *resource;
1315
1316 resource = session->context->unknown_resource ?
1317 session->context->unknown_resource :
1318 session->context->proxy_uri_resource;
1319 if (!resource) {
1320 coap_log_err("coap_proxy_local_write: Unknown or Proxy resource not defined\n");
1321 goto fail;
1322 }
1323
1324 response = coap_pdu_init(pdu->type == COAP_MESSAGE_CON ?
1326 0, pdu->mid, coap_session_max_pdu_size_lkd(session));
1327 if (!response) {
1328 coap_log_err("coap_proxy_local_write: Could not create response PDU\n");
1329 goto fail;
1330 }
1331 response->session = session;
1332
1333 if (!coap_add_token(response, pdu->actual_token.length,
1334 pdu->actual_token.s)) {
1335 goto fail;
1336 }
1337
1338 coap_log_debug("* %s: internal: sent %4zd bytes\n",
1339 coap_session_str(session),
1340 pdu->used_size + coap_pdu_encode_header(pdu, session->proto));
1342
1343 mid = pdu->mid;
1344 if (!coap_proxy_forward_request_lkd(session, pdu, response, resource,
1345 NULL, session->server_list)) {
1346 coap_log_debug("coap_proxy_local_write: Failed to forward PDU\n");
1347 mid = COAP_INVALID_MID;
1348 }
1349fail:
1350 coap_delete_pdu_lkd(response);
1352 return mid;
1353}
1354
1357 coap_proxy_server_list_t *server_list) {
1358 coap_session_t *session;
1359
1360 coap_lock_lock(ctx, return NULL);
1361 session = coap_new_client_session_proxy_lkd(ctx, server_list);
1362 coap_lock_unlock(ctx);
1363 return session;
1364}
1365
1367coap_new_client_session_proxy_lkd(coap_context_t *ctx,
1368 coap_proxy_server_list_t *server_list) {
1369 coap_session_t *session;
1370 coap_addr_info_t *info_list = NULL;
1371 coap_str_const_t remote;
1372
1374
1375#if COAP_IPV6_SUPPORT
1376 remote.s = (const uint8_t *)"::1";
1377#elif COAP_IPV4_SUPPORT
1378 remote.s = (const uint8_t *)"127.0.0.1";
1379#else /* !COAP_IPV6_SUPPORT && ! COAP_IPV4_SUPPORT */
1380 coap_log_warn("coap_new_client_session_proxy: No IPv4 or IPv6 support\n");
1381 return NULL;
1382#endif /* !COAP_IPV6_SUPPORT && ! COAP_IPV4_SUPPORT */
1383 remote.length = strlen((const char *)remote.s);
1384 /* resolve internal remote address where proxy session is 'connecting' to */
1385 info_list = coap_resolve_address_info(&remote, 0, 0, 0, 0,
1386 0,
1389 if (!info_list) {
1390 coap_log_warn("coap_new_client_session_proxy: Unable to resolve IP address\n");
1391 return NULL;
1392 }
1393
1394 session = coap_new_client_session_lkd(ctx, NULL, &info_list->addr, COAP_PROTO_UDP);
1395
1396 if (session) {
1397 session->server_list = server_list;
1398 }
1399 coap_free_address_info(info_list);
1400 return session;
1401}
1402
1403#else /* ! COAP_PROXY_SUPPORT */
1404
1405int
1407 return 0;
1408}
1409
1410COAP_API int
1412 const coap_pdu_t *request,
1413 coap_pdu_t *response,
1414 coap_resource_t *resource,
1415 coap_cache_key_t *cache_key,
1416 coap_proxy_server_list_t *server_list) {
1417 (void)session;
1418 (void)request;
1419 (void)resource;
1420 (void)cache_key;
1421 (void)server_list;
1422 response->code = COAP_RESPONSE_CODE(500);
1423 return 0;
1424}
1425
1428 const coap_pdu_t *received,
1429 coap_cache_key_t **cache_key) {
1430 (void)session;
1431 (void)received;
1432 (void)cache_key;
1433 return COAP_RESPONSE_OK;
1434}
1435
1436int
1438 (void)scheme;
1439 return 0;
1440}
1441#endif /* ! COAP_PROXY_SUPPORT */
int coap_address_set_unix_domain(coap_address_t *addr, const uint8_t *host, size_t host_len)
Copy the parsed unix domain host into coap_address_t structure translating %2F into / on the way.
void coap_free_address_info(coap_addr_info_t *info)
Free off the one or more linked sets of coap_addr_info_t returned from coap_resolve_address_info().
int coap_is_af_unix(const coap_address_t *a)
Checks if given address a denotes a AF_UNIX address.
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
#define COAP_UNIX_PATH_MAX
struct coap_proxy_list_t coap_proxy_list_t
Proxy information.
#define PRIu64
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
coap_mid_t coap_send_rst_lkd(coap_session_t *session, const coap_pdu_t *request)
Sends an RST message with code 0 for the specified request to dst.
Definition coap_net.c:1040
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:1428
coap_mid_t coap_send_ack_lkd(coap_session_t *session, const coap_pdu_t *request)
Sends an ACK message with code 0 for the specified request to dst.
Definition coap_net.c:1055
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.
coap_cache_key_t * coap_cache_derive_key_w_ignore(const coap_session_t *session, const coap_pdu_t *pdu, coap_cache_session_based_t session_based, const uint16_t *ignore_options, size_t ignore_count)
Calculates a cache-key for the given CoAP PDU.
@ COAP_CACHE_NOT_SESSION_BASED
Definition coap_cache.h:38
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
#define COAP_MAX_DELAY_TICKS
Definition coap_time.h:221
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_FAIL
Response not liked - send CoAP RST packet.
Definition coap_net.h:49
@ 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_callback_ret_release(r, c, func, failed)
Dummy for no thread-safe code.
#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_lock_check_locked(c)
Dummy for no thread-safe code.
#define coap_log_debug(...)
Definition coap_debug.h:120
void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu)
Display the contents of the specified pdu.
Definition coap_debug.c:784
const char * coap_session_str(const coap_session_t *session)
Get session description.
#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_LOG_DEBUG
Definition coap_debug.h:58
#define COAP_OBSERVE_CANCEL
The value COAP_OBSERVE_CANCEL in a GET/FETCH request option COAP_OPTION_OBSERVE indicates that the ob...
#define COAP_OBSERVE_ESTABLISH
The value COAP_OBSERVE_ESTABLISH in a GET/FETCH request option COAP_OPTION_OBSERVE indicates a new ob...
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
coap_optlist_t * coap_new_optlist(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_reference_lkd(coap_pdu_t *pdu)
Increment reference counter on a pdu to stop it prematurely getting freed off when coap_delete_pdu() ...
Definition coap_pdu.c:1623
void coap_delete_pdu_lkd(coap_pdu_t *pdu)
Dispose of an CoAP PDU and free off associated storage.
Definition coap_pdu.c:190
size_t coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto)
Compose the protocol specific header for the specified PDU.
Definition coap_pdu.c:1485
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:230
coap_pdu_t * coap_const_pdu_reference_lkd(const coap_pdu_t *pdu)
Increment reference counter on a const pdu to stop it prematurely getting freed off when coap_delete_...
Definition coap_pdu.c:1631
#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:1581
#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
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition coap_pdu.h:263
#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_OPTION_OSCORE
Definition coap_pdu.h:126
#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_RTAG
Definition coap_pdu.h:146
#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:880
#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:1605
@ COAP_PROTO_UDP
Definition coap_pdu.h:314
@ COAP_MESSAGE_NON
Definition coap_pdu.h:70
@ COAP_MESSAGE_ACK
Definition coap_pdu.h:71
@ COAP_MESSAGE_CON
Definition coap_pdu.h:69
COAP_API coap_session_t * coap_new_client_session_proxy(coap_context_t *context, coap_proxy_server_list_t *server_list)
Creates a new client session to use the proxy logic going to the defined upstream server.
COAP_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.
int coap_verify_proxy_scheme_supported(coap_uri_scheme_t scheme)
Verify that the CoAP Scheme is supported for an ongoing proxy connection.
COAP_API coap_response_t coap_proxy_forward_response(coap_session_t *session, const coap_pdu_t *received, coap_cache_key_t **cache_key)
Forward the returning response back to the appropriate client.
@ COAP_PROXY_REVERSE_STRIP
Act as a reverse proxy, strip out proxy options.
Definition coap_proxy.h:29
@ COAP_PROXY_FORWARD_DYNAMIC
Act as a forward-dynamic proxy using the request's Proxy-Uri or Proxy-Scheme options to determine ser...
Definition coap_proxy.h:34
@ COAP_PROXY_REVERSE
Act as a reverse proxy.
Definition coap_proxy.h:28
@ COAP_PROXY_FORWARD_STATIC
Act as a forward-static proxy.
Definition coap_proxy.h:31
@ COAP_PROXY_FORWARD_DYNAMIC_STRIP
Act as a forward-dynamic proxy, strip out proxy options.
Definition coap_proxy.h:38
@ COAP_PROXY_FORWARD_STATIC_STRIP
Act as a forward-static proxy, strip out proxy options.
Definition coap_proxy.h:32
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.
int coap_session_reconnect(coap_session_t *session)
Close the current session (if not already closed) and reconnect to server (client session only).
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:29
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.
int coap_wss_is_supported(void)
Check whether Secure WebSockets is available.
Definition coap_ws.c:938
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.
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.
coap_resource_t * proxy_uri_resource
can be used for handling proxy URI resources
coap_resource_t * unknown_resource
can be used for handling unknown resources
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
Structure to hold large body (many blocks) client receive information.
uint8_t observe_set
Set if this is an observe receive PDU.
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_bin_const_t actual_token
Actual token in pdu.
coap_mid_t mid
message id, if any, in regular host byte order
size_t used_size
used bytes of storage for token, options and payload
coap_session_t * session
Session responsible for PDU or NULL.
coap_pdu_type_t type
message type
int track_client_session
If 1, track individual connections to upstream server, else 0 for all clients to be multiplexed over ...
Definition coap_proxy.h:59
coap_proxy_server_t * entry
Set of servers to connect to.
Definition coap_proxy.h:55
coap_proxy_t type
The proxy type.
Definition coap_proxy.h:58
unsigned int idle_timeout_secs
Proxy upstream session idle timeout (0 is no timeout).
Definition coap_proxy.h:62
size_t next_entry
Next server to use (% entry_count)
Definition coap_proxy.h:57
size_t entry_count
The number of servers in entry list.
Definition coap_proxy.h:56
coap_dtls_pki_t * dtls_pki
PKI configuration to use if not NULL.
Definition coap_proxy.h:49
coap_oscore_conf_t * oscore_conf
OSCORE configuration if not NULL.
Definition coap_proxy.h:51
coap_uri_t uri
host and port define the server, scheme method
Definition coap_proxy.h:48
coap_dtls_cpsk_t * dtls_cpsk
PSK configuration to use if not NULL.
Definition coap_proxy.h:50
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_proto_t proto
protocol used
coap_response_t last_con_handler_res
The result of calling the response handler of the last CON.
coap_context_t * context
session's context
CoAP string data definition with const data.
Definition coap_str.h:46
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
uint16_t port
The port in host byte order.
Definition coap_uri.h:70
coap_str_const_t host
The host part of the URI.
Definition coap_uri.h:69