libcoap 4.3.5-develop-fcba074
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-2026 Jon Shallow <supjps-libcoap@jpshallow.com>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 *
7 * This file is part of the CoAP library libcoap. Please see
8 * README for terms of use.
9 */
10
15
17
18#if COAP_PROXY_SUPPORT
19#include <stdio.h>
20#ifndef INET6_ADDRSTRLEN
21#define INET6_ADDRSTRLEN 46
22#endif
23
24#if COAP_CLIENT_SUPPORT == 0
25#error For Proxy support, COAP_CLIENT_SUPPORT must be set
26#endif
27#if COAP_SERVER_SUPPORT == 0
28#error For Proxy support, COAP_SERVER_SUPPORT must be set
29#endif
30
31#ifdef _WIN32
32#define strcasecmp _stricmp
33#define strncasecmp _strnicmp
34#endif
35
36int
38 return 1;
39}
40
41void
42coap_proxy_log_entry(coap_session_t *incoming, const coap_pdu_t *pdu,
43 coap_bin_const_t *upstream_token, const char *type) {
45 coap_string_t *url;
46
47 url = coap_get_uri_path(pdu);
48 if (url) {
49 coap_opt_iterator_t opt_iter;
50 uint8_t addr[INET6_ADDRSTRLEN + 8];
51 char scratch_d[24];
52 char scratch_u[24];
53 size_t size;
54 size_t i;
55
56 scratch_d[0] = '\000';
57 for (i = 0; i < pdu->actual_token.length; i++) {
58 size = strlen(scratch_d);
59 snprintf(&scratch_d[size], sizeof(scratch_d)-size,
60 "%02x", pdu->actual_token.s[i]);
61 }
62 scratch_u[0] = '\000';
63 if (upstream_token) {
64 for (i = 0; i < upstream_token->length; i++) {
65 size = strlen(scratch_u);
66 snprintf(&scratch_u[size], sizeof(scratch_u)-size,
67 "%02x", upstream_token->s[i]);
68 }
69 }
70 (void)coap_print_addr(coap_session_get_addr_remote(incoming), addr, sizeof(addr));
71 coap_log_debug(" proxy %-4s %s {%s}-{%s} \"%s\"%s\n", type, addr, scratch_u, scratch_d,
72 url->s, coap_check_option(pdu, COAP_OPTION_OBSERVE, &opt_iter) ? " Observe" : "");
74 }
75 }
76}
77
78void
79coap_proxy_del_req(coap_proxy_entry_t *proxy_entry, coap_proxy_req_t *proxy_req) {
80 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used, "del");
81
82 coap_delete_pdu_lkd(proxy_req->pdu);
83 coap_delete_bin_const(proxy_req->token_used);
84 coap_delete_cache_key(proxy_req->cache_key);
85 if (proxy_req->proxy_cache) {
87 coap_string_t *url;
88
89 url = coap_get_uri_path(proxy_req->proxy_cache->req_pdu);
90 coap_log_debug("***%s: Removing Proxy Cache \"%s\" (Active %d)\n",
91 coap_session_str(proxy_req->incoming),
92 url ? (char *)url->s : "???",
93 proxy_req->proxy_cache->ref);
95 }
96 assert(proxy_req->proxy_cache->ref);
97 proxy_req->proxy_cache->ref--;
98 if (proxy_req->proxy_cache->ref == 0) {
99 PROXY_CACHE_DELETE(proxy_entry->rsp_cache, proxy_req->proxy_cache);
100 coap_delete_pdu_lkd(proxy_req->proxy_cache->req_pdu);
101 coap_delete_pdu_lkd(proxy_req->proxy_cache->rsp_pdu);
102 coap_free_type(COAP_STRING, proxy_req->proxy_cache);
103 proxy_req->proxy_cache = NULL;
104 }
105 if (proxy_req->doing_observe) {
106 assert(proxy_req->incoming->ref_proxy_subs);
107 proxy_req->incoming->ref_proxy_subs--;
108 proxy_req->doing_observe = 0;
109 }
110 }
111 /* To prevent potential loops */
112 proxy_req->incoming = NULL;
113
114 LL_DELETE(proxy_entry->proxy_req, proxy_req);
115 coap_free_type(COAP_STRING, proxy_req);
116}
117
118static void
119coap_proxy_cleanup_entry(coap_proxy_entry_t *proxy_entry, coap_pdu_code_t failure_code) {
120 coap_proxy_req_t *proxy_req, *treq;
121
122 LL_FOREACH_SAFE(proxy_entry->proxy_req, proxy_req, treq) {
123 if (failure_code) {
124 coap_pdu_t *response;
125 coap_bin_const_t l_token;
126
127 /* Need to send back a gateway failure as per failure_code */
128 response = coap_pdu_init(proxy_req->pdu->type,
129 failure_code,
130 coap_new_message_id_lkd(proxy_req->incoming),
131 coap_session_max_pdu_size_lkd(proxy_req->incoming));
132 if (!response) {
133 coap_log_info("PDU creation issue\n");
134 goto cleanup;
135 }
136
137 l_token = coap_pdu_get_token(proxy_req->pdu);
138 if (!coap_add_token(response, l_token.length,
139 l_token.s)) {
140 coap_log_debug("Cannot add token to incoming proxy response PDU\n");
141 }
142
143 if (coap_send_lkd(proxy_req->incoming, response) == COAP_INVALID_MID) {
144 coap_log_info("Failed to send PDU with %u.%02u proxy issue\n",
145 COAP_RESPONSE_CLASS(failure_code), failure_code & 0x1f);
146 }
147 }
148cleanup:
149 coap_proxy_del_req(proxy_entry, proxy_req);
150 }
151 coap_free_type(COAP_STRING, proxy_entry->uri_host_keep);
152}
153
154void
155coap_proxy_cleanup(coap_context_t *context) {
156 size_t i;
157
158 for (i = 0; i < context->proxy_list_count; i++) {
159 /* All sessions have now been closed down */
160 coap_log_debug("proxy_entry %p cleaned up\n",
161 (void *)&context->proxy_list[i]);
162 coap_proxy_cleanup_entry(&context->proxy_list[i], 0);
163 }
164 coap_free_type(COAP_STRING, context->proxy_list);
165}
166
167static int
168coap_proxy_check_observe(coap_proxy_entry_t *proxy_entry) {
169 if (proxy_entry && proxy_entry->ongoing) {
170 /* Need to see if there are any Observes active */
171 coap_lg_crcv_t *lg_crcv;
172
173 LL_FOREACH(proxy_entry->ongoing->lg_crcv, lg_crcv) {
174 if (lg_crcv->observe_set) {
175 return 1;
176 }
177 }
178 }
179 return 0;
180}
181
182/*
183 * Return 1 if there is a future expire time, else 0.
184 * Update tim_rem with remaining value if return is 1.
185 */
186int
187coap_proxy_check_timeouts(coap_context_t *context, coap_tick_t now,
188 coap_tick_t *tim_rem) {
189 size_t i;
190 int ret = 0;
191
192 *tim_rem = COAP_MAX_DELAY_TICKS;
193 for (i = 0; i < context->proxy_list_count; i++) {
194 coap_proxy_entry_t *proxy_entry = &context->proxy_list[i];
195
196 if (coap_proxy_check_observe(proxy_entry))
197 continue;
198
199 if (proxy_entry->ongoing) {
200 if (proxy_entry->req_start_ticks) {
201 coap_tick_t timeout_time;
202
203 timeout_time = proxy_entry->req_start_ticks + COAP_MAX_TRANSMIT_WAIT_TICKS(proxy_entry->ongoing);
204 if (timeout_time <= now) {
205 /* Forward request has timed out */
206 coap_queue_t *sent = NULL;
207
208 coap_remove_from_queue(&context->sendqueue, proxy_entry->ongoing,
209 proxy_entry->mid, NULL, &sent);
210 if (sent) {
212 }
213 if (coap_proxy_remove_association(proxy_entry->ongoing, COAP_RESPONSE_CODE(504))) {
214 i--;
215 }
216 } else {
217 if (*tim_rem > timeout_time - now) {
218 *tim_rem = timeout_time - now;
219 ret = 1;
220 }
221 }
222 } else if (proxy_entry->idle_timeout_ticks) {
223 if (proxy_entry->last_used + proxy_entry->idle_timeout_ticks <= now) {
224 /* Drop session to upstream server (which may remove proxy entry) */
225 if (coap_proxy_remove_association(proxy_entry->ongoing, 0)) {
226 i--;
227 }
228 } else {
229 if (*tim_rem > proxy_entry->last_used + proxy_entry->idle_timeout_ticks - now) {
230 *tim_rem = proxy_entry->last_used + proxy_entry->idle_timeout_ticks - now;
231 ret = 1;
232 }
233 }
234 }
235 }
236 }
237 return ret;
238}
239
240static int
241coap_get_uri_proxy_scheme_info(const coap_pdu_t *request,
242 coap_opt_t *opt,
243 coap_uri_t *uri) {
244 const char *opt_val = (const char *)coap_opt_value(opt);
245 int opt_len = coap_opt_length(opt);
246 coap_opt_iterator_t opt_iter;
247
248 if (opt_len == 9 &&
249 strncasecmp(opt_val, "coaps+tcp", 9) == 0) {
252 } else if (opt_len == 8 &&
253 strncasecmp(opt_val, "coap+tcp", 8) == 0) {
255 uri->port = COAP_DEFAULT_PORT;
256 } else if (opt_len == 5 &&
257 strncasecmp(opt_val, "coaps", 5) == 0) {
260 } else if (opt_len == 4 &&
261 strncasecmp(opt_val, "coap", 4) == 0) {
263 uri->port = COAP_DEFAULT_PORT;
264 } else if (opt_len == 7 &&
265 strncasecmp(opt_val, "coap+ws", 7) == 0) {
267 uri->port = 80;
268 } else if (opt_len == 8 &&
269 strncasecmp(opt_val, "coaps+ws", 8) == 0) {
271 uri->port = 443;
272 } else {
273 coap_log_warn("Unsupported Proxy Scheme '%*.*s'\n",
274 opt_len, opt_len, opt_val);
275 return 0;
276 }
277
278 opt = coap_check_option(request, COAP_OPTION_URI_HOST, &opt_iter);
279 if (opt) {
280 uri->host.length = coap_opt_length(opt);
281 uri->host.s = coap_opt_value(opt);
282 } else {
283 uri->host.s = NULL;
284 uri->host.length = 0;
285 coap_log_warn("Proxy Scheme requires Uri-Host\n");
286 return 0;
287 }
288 opt = coap_check_option(request, COAP_OPTION_URI_PORT, &opt_iter);
289 if (opt) {
290 uri->port =
292 coap_opt_length(opt));
293 }
294 return 1;
295}
296
297int
299
300 /* Sanity check that the connection can be forwarded on */
301 switch (scheme) {
304 coap_log_warn("Proxy URI http or https not supported\n");
305 return 0;
307 break;
309 if (!coap_dtls_is_supported()) {
310 coap_log_warn("coaps URI scheme not supported for proxy\n");
311 return 0;
312 }
313 break;
315 if (!coap_tcp_is_supported()) {
316 coap_log_warn("coap+tcp URI scheme not supported for proxy\n");
317 return 0;
318 }
319 break;
321 if (!coap_tls_is_supported()) {
322 coap_log_warn("coaps+tcp URI scheme not supported for proxy\n");
323 return 0;
324 }
325 break;
327 if (!coap_ws_is_supported()) {
328 coap_log_warn("coap+ws URI scheme not supported for proxy\n");
329 return 0;
330 }
331 break;
333 if (!coap_wss_is_supported()) {
334 coap_log_warn("coaps+ws URI scheme not supported for proxy\n");
335 return 0;
336 }
337 break;
339 default:
340 coap_log_warn("%d URI scheme not supported\n", scheme);
341 return 0;
342 }
343 return 1;
344}
345
346static coap_proxy_t
347coap_proxy_map_type(coap_proxy_t proxy_type) {
348 if ((proxy_type & COAP_PROXY_NEW_MASK) == 0) {
349 /* Old version - update to bitwise version */
350 switch ((int)proxy_type) {
352 proxy_type = COAP_PROXY_REV;
353 break;
356 break;
358 proxy_type = COAP_PROXY_FWD_STATIC;
359 break;
362 break;
364 /* Mcast was always let through */
366 break;
368 /* Mcast was always let through */
370 break;
371 default:
372 coap_log_warn("Proxy old proxy_type 0x%u unknown\n", proxy_type);
373 proxy_type = COAP_PROXY_FWD_DYNAMIC;
374 break;
375 }
376 }
377 return proxy_type;
378}
379
380static coap_proxy_entry_t *
381coap_proxy_get_add_list_entry(coap_session_t *incoming, coap_session_t *ongoing,
382 coap_uri_t uri, coap_proxy_server_list_t *server_list,
383 int allow_add) {
384 if (!incoming && !ongoing)
385 return NULL;
386 coap_context_t *context = incoming ? incoming->context : ongoing->context;
387 coap_proxy_entry_t *proxy_list = context->proxy_list;
388 size_t proxy_list_count = context->proxy_list_count;
389 coap_proxy_entry_t *new_proxy_list;
390 size_t i;
391
392 /* See if we are already connected to the Server */
393 for (i = 0; i < proxy_list_count; i++) {
394 if (coap_string_equal(&proxy_list[i].uri.host, &uri.host) &&
395 proxy_list[i].uri.port == uri.port &&
396 proxy_list[i].uri.scheme == uri.scheme) {
397 if (!proxy_list[i].track_client_session && context->proxy_response_cb) {
398 coap_ticks(&proxy_list[i].last_used);
399 return &proxy_list[i];
400 } else {
401 if (proxy_list[i].incoming == incoming) {
402 coap_ticks(&proxy_list[i].last_used);
403 return &proxy_list[i];
404 }
405 }
406 }
407 }
408 if ((server_list->type & COAP_PROXY_DYN_DEFINED) && !allow_add)
409 return NULL;
410
411 /* Need to create a new forwarding mapping */
412 new_proxy_list = coap_realloc_type(COAP_STRING, proxy_list,
413 (proxy_list_count+1)*sizeof(proxy_list[0]));
414
415 if (new_proxy_list == NULL) {
416 return NULL;
417 }
418 context->proxy_list = proxy_list = new_proxy_list;
419 memset(&proxy_list[proxy_list_count], 0, sizeof(proxy_list[proxy_list_count]));
420
421 /* Keep a copy of the host as server_use->uri pointed to will be going away */
422 proxy_list[proxy_list_count].uri = uri;
423 proxy_list[proxy_list_count].uri_host_keep = coap_malloc_type(COAP_STRING,
424 uri.host.length);
425 if (!proxy_list[proxy_list_count].uri_host_keep) {
426 return NULL;
427 }
428 memcpy(proxy_list[proxy_list_count].uri_host_keep, uri.host.s, uri.host.length);
429 proxy_list[proxy_list_count].uri.host.s = proxy_list[proxy_list_count].uri_host_keep;
430 /* Unset uri parts which point to going away information */
431 proxy_list[proxy_list_count].uri.path.s = NULL;
432 proxy_list[proxy_list_count].uri.path.length = 0;
433 proxy_list[proxy_list_count].uri.query.s = NULL;
434 proxy_list[proxy_list_count].uri.query.length = 0;
435
436 proxy_list[proxy_list_count].ongoing = ongoing;
437 proxy_list[proxy_list_count].idle_timeout_ticks = server_list->idle_timeout_secs *
439 proxy_list[proxy_list_count].track_client_session = server_list->track_client_session;
440 coap_ticks(&proxy_list[proxy_list_count].last_used);
441 if (incoming) {
442 incoming->proxy_entry = &proxy_list[proxy_list_count];
443 if (server_list->track_client_session)
444 proxy_list[proxy_list_count].incoming = incoming;
445 incoming->proxy_entry = &proxy_list[proxy_list_count];
446 }
447
448 context->proxy_list_count++;
449 return &proxy_list[proxy_list_count];
450}
451
452static coap_proxy_entry_t *
453coap_proxy_get_session(coap_session_t *session, const coap_pdu_t *request,
454 coap_pdu_t *response,
455 coap_proxy_server_list_t *server_list,
456 coap_proxy_server_t *server_use, int *proxy_entry_created) {
457 size_t i;
458 coap_proxy_entry_t *proxy_entry;
459 coap_proxy_entry_t *proxy_list = session->context->proxy_list;
460 size_t proxy_list_count = session->context->proxy_list_count;
461 coap_opt_iterator_t opt_iter;
462 coap_opt_t *proxy_scheme;
463 coap_opt_t *proxy_uri;
464 coap_proxy_t proxy_type;
465
466 *proxy_entry_created = 0;
467
468 /*
469 * Maintain server stickability. server_use not needed as there is
470 * ongoing session in place.
471 */
472 if (session->proxy_entry) {
473 for (i = 0; i < proxy_list_count; i++) {
474 if (&proxy_list[i] == session->proxy_entry) {
475 if (session->proxy_entry->ongoing) {
476 memset(server_use, 0, sizeof(*server_use));
477 return session->proxy_entry;
478 }
479 }
480 }
481 }
482
483 /* Round robin the defined next server list (which usually is just one */
484 server_list->next_entry++;
485 if (server_list->next_entry >= server_list->entry_count)
486 server_list->next_entry = 0;
487
488 if (server_list->entry_count) {
489 memcpy(server_use, &server_list->entry[server_list->next_entry], sizeof(*server_use));
490 } else {
491 memset(server_use, 0, sizeof(*server_use));
492 }
493
494 proxy_type = coap_proxy_map_type(server_list->type);
495
496 if ((proxy_type & COAP_PROXY_NEW_MASK) == COAP_PROXY_FWD_DYNAMIC) {
497 /* Need to get actual server from CoAP Proxy-Uri or Proxy-Scheme options */
498 /*
499 * See if Proxy-Scheme
500 */
501 proxy_scheme = coap_check_option(request, COAP_OPTION_PROXY_SCHEME, &opt_iter);
502 if (proxy_scheme) {
503 if (!coap_get_uri_proxy_scheme_info(request, proxy_scheme, &server_use->uri)) {
504 response->code = COAP_RESPONSE_CODE(505);
505 return NULL;
506 }
507 }
508 /*
509 * See if Proxy-Uri
510 */
511 proxy_uri = coap_check_option(request, COAP_OPTION_PROXY_URI, &opt_iter);
512 if (proxy_uri) {
513 coap_log_info("Proxy URI '%.*s'\n",
514 (int)coap_opt_length(proxy_uri),
515 (const char *)coap_opt_value(proxy_uri));
517 coap_opt_length(proxy_uri),
518 &server_use->uri) < 0) {
519 /* Need to return a 5.05 RFC7252 Section 5.7.2 */
520 coap_log_warn("Proxy URI not decodable\n");
521 response->code = COAP_RESPONSE_CODE(505);
522 return NULL;
523 }
524 }
525
526 if (!(proxy_scheme || proxy_uri)) {
527 response->code = COAP_RESPONSE_CODE(404);
528 return NULL;
529 }
530 }
531
532 if (server_use->uri.host.length == 0) {
533 /* Ongoing connection not well formed */
534 response->code = COAP_RESPONSE_CODE(505);
535 return NULL;
536 }
537
539 response->code = COAP_RESPONSE_CODE(505);
540 return NULL;
541 }
542
543 /* Check if need to create a new forwarding mapping */
544 proxy_entry = coap_proxy_get_add_list_entry(session, NULL, server_use->uri, server_list, 0);
545
546 if (proxy_entry == NULL) {
547 response->code = COAP_RESPONSE_CODE(500);
548 return NULL;
549 }
550
551 *proxy_entry_created = 1;
552 return proxy_entry;
553}
554
555int
556coap_proxy_remove_association(coap_session_t *session, coap_pdu_code_t failure_code) {
557
558 size_t i;
559 coap_proxy_entry_t *proxy_list = session->context->proxy_list;
560 size_t proxy_list_count = session->context->proxy_list_count;
561
562 for (i = 0; i < proxy_list_count; i++) {
563 coap_proxy_entry_t *proxy_entry = &proxy_list[i];
564 coap_proxy_req_t *proxy_req;
565
566 /* Check for incoming match */
567retry:
568 LL_FOREACH(proxy_entry->proxy_req, proxy_req) {
569 if (proxy_req->incoming == session) {
570 coap_proxy_del_req(proxy_entry, proxy_req);
571 goto retry;
572 }
573 }
574 if (proxy_entry->incoming == session) {
575 /*
576 * One-to-one tracking (track_client_session): release this entry's
577 * upstream session. session may be tracked by several entries, so
578 * keep going.
579 */
580 coap_session_t *ongoing = proxy_entry->ongoing;
581
582 proxy_entry->ongoing = NULL;
584 continue;
585 }
586
587 /* Check for outgoing match */
588 if (proxy_entry->ongoing == session) {
589 coap_session_t *ongoing;
590
591 coap_proxy_cleanup_entry(proxy_entry, failure_code);
592 ongoing = proxy_entry->ongoing;
593 coap_log_debug("* %s: proxy_entry %p released (rem count = % " PRIdS ")\n",
594 coap_session_str(ongoing),
595 (void *)proxy_entry,
596 session->context->proxy_list_count - 1);
597 if (proxy_list_count-i > 1) {
598 memmove(&proxy_list[i],
599 &proxy_list[i+1],
600 (proxy_list_count-i-1) * sizeof(proxy_list[0]));
601 }
602 session->context->proxy_list_count--;
604 return 1;
605 }
606 }
607 return 0;
608}
609
610static coap_proxy_entry_t *
611coap_proxy_get_ongoing_session(coap_session_t *session,
612 const coap_pdu_t *request,
613 coap_pdu_t *response,
614 coap_proxy_server_list_t *server_list) {
615
616 coap_address_t dst;
617 coap_proto_t proto;
618 coap_addr_info_t *info_list = NULL;
619 coap_proxy_entry_t *proxy_entry;
620 coap_context_t *context = session->context;
621 static char client_sni[256];
622 coap_proxy_server_t server_use;
623 int proxy_entry_created;
624
625 proxy_entry = coap_proxy_get_session(session, request, response, server_list,
626 &server_use, &proxy_entry_created);
627 if (!proxy_entry) {
628 /* Error response code already set */
629 return NULL;
630 }
631
632 if (!proxy_entry->ongoing) {
633 /* Need to create a new session */
634 coap_address_t *local_addr = NULL;
635
636 /* resolve destination address where data should be sent */
637 info_list = coap_resolve_address_info_lkd(&server_use.uri.host,
638 server_use.uri.port,
639 server_use.uri.port,
640 server_use.uri.port,
641 server_use.uri.port,
642 0,
643 1 << server_use.uri.scheme,
645
646 if (info_list == NULL) {
647 response->code = COAP_RESPONSE_CODE(502);
648 coap_proxy_remove_association(session, 0);
649 return NULL;
650 }
651 proto = info_list->proto;
652 memcpy(&dst, &info_list->addr, sizeof(dst));
653 coap_free_address_info(info_list);
654 if (coap_is_mcast(&dst)) {
655 coap_proxy_t proxy_type = coap_proxy_map_type(server_list->type);
656
657 if ((proxy_type & COAP_PROXY_NEW_MASK) == COAP_PROXY_FWD_DYNAMIC &&
658 (proxy_type & COAP_PROXY_BIT_MCAST) == 0) {
659 response->code = COAP_RESPONSE_CODE(501);
660 coap_proxy_remove_association(session, 0);
661 coap_log_debug("* %s: mcast proxy forwarding not enabled\n",
662 coap_session_str(session));
663 return NULL;
664 }
665 if (server_use.uri.scheme != COAP_URI_SCHEME_COAP) {
666 response->code = COAP_RESPONSE_CODE(501);
667 coap_proxy_remove_association(session, 0);
668 coap_log_debug("* %s: mcast proxy forwarding only supported for 'coap'\n",
669 coap_session_str(session));
670 return NULL;
671 }
672 }
673
674#if COAP_AF_UNIX_SUPPORT
675 coap_address_t bind_addr;
676 if (coap_is_af_unix(&dst)) {
677 char buf[COAP_UNIX_PATH_MAX];
678 coap_tick_t now;
679
680 /* Need a unique 'client' address */
681 coap_ticks(&now);
682 snprintf(buf, COAP_UNIX_PATH_MAX,
683 "/tmp/coap-pr-cl-%" PRIu64, (uint64_t)now);
684 if (!coap_address_set_unix_domain(&bind_addr, (const uint8_t *)buf,
685 strlen(buf))) {
686 fprintf(stderr, "coap_address_set_unix_domain: %s: failed\n",
687 buf);
688 remove(buf);
689 return NULL;
690 }
691 (void)remove(buf);
692 local_addr = &bind_addr;
693 }
694#endif /* COAP_AF_UNIX_SUPPORT */
695
696 snprintf(client_sni, sizeof(client_sni), "%*.*s", (int)server_use.uri.host.length,
697 (int)server_use.uri.host.length, server_use.uri.host.s);
698
699 switch (server_use.uri.scheme) {
703#if COAP_OSCORE_SUPPORT
704 if (server_use.oscore_conf) {
705 proxy_entry->ongoing =
706 coap_new_client_session_oscore3_lkd(context, local_addr, &dst,
707 proto, server_use.oscore_conf,
708 NULL, NULL, NULL);
709 } else {
710#endif /* COAP_OSCORE_SUPPORT */
711 proxy_entry->ongoing =
712 coap_new_client_session3_lkd(context, local_addr, &dst, proto,
713 NULL, NULL, NULL);
714#if COAP_OSCORE_SUPPORT
715 }
716#endif /* COAP_OSCORE_SUPPORT */
717 break;
721#if COAP_OSCORE_SUPPORT
722 if (server_use.oscore_conf) {
723 if (server_use.dtls_pki) {
724 server_use.dtls_pki->client_sni = client_sni;
725 proxy_entry->ongoing =
726 coap_new_client_session_oscore_pki3_lkd(context, local_addr, &dst,
727 proto, server_use.dtls_pki,
728 server_use.oscore_conf,
729 NULL, NULL, NULL);
730 } else if (server_use.dtls_cpsk) {
731 server_use.dtls_cpsk->client_sni = client_sni;
732 proxy_entry->ongoing =
733 coap_new_client_session_oscore_psk3_lkd(context, local_addr, &dst,
734 proto, server_use.dtls_cpsk,
735 server_use.oscore_conf,
736 NULL, NULL, NULL);
737 } else {
738 coap_log_warn("Proxy: (D)TLS not configured for secure session\n");
739 }
740 } else {
741#endif /* COAP_OSCORE_SUPPORT */
742 /* Not doing OSCORE */
743 if (server_use.dtls_pki) {
744 server_use.dtls_pki->client_sni = client_sni;
745 proxy_entry->ongoing =
746 coap_new_client_session_pki3_lkd(context, local_addr, &dst,
747 proto, server_use.dtls_pki,
748 NULL, NULL, NULL);
749 } else if (server_use.dtls_cpsk) {
750 server_use.dtls_cpsk->client_sni = client_sni;
751 proxy_entry->ongoing =
752 coap_new_client_session_psk3_lkd(context, local_addr, &dst,
753 proto, server_use.dtls_cpsk,
754 NULL, NULL, NULL);
755 } else {
756 /* Using client anonymous PKI */
757 proxy_entry->ongoing =
758 coap_new_client_session3_lkd(context, local_addr, &dst, proto,
759 NULL, NULL, NULL);
760 }
761#if COAP_OSCORE_SUPPORT
762 }
763#endif /* COAP_OSCORE_SUPPORT */
764 break;
768 default:
769 assert(0);
770 break;
771 }
772 if (proxy_entry->ongoing == NULL) {
773 response->code = COAP_RESPONSE_CODE(505);
774 coap_proxy_remove_association(session, 0);
775 return NULL;
776 }
777 if (proxy_entry_created) {
778 coap_log_debug("* %s: proxy_entry %.*s:%d %p created (tot count = % " PRIdS ")\n",
779 coap_session_str(proxy_entry->ongoing),
780 (int)proxy_entry->uri.host.length, proxy_entry->uri.host.s,
781 proxy_entry->uri.port,
782 (void *)proxy_entry,
783 session->context->proxy_list_count);
784 }
785 } else if (proxy_entry->ongoing->session_failed) {
786 if (!coap_session_reconnect(proxy_entry->ongoing)) {
787 /* Server is not yet back up */
788 return NULL;
789 }
790 }
791
792 return proxy_entry;
793}
794
795static void
796coap_proxy_release_body_data(coap_session_t *session COAP_UNUSED,
797 void *app_ptr) {
798 coap_delete_binary(app_ptr);
799}
800
801static coap_proxy_req_t *
802coap_proxy_get_req(coap_proxy_entry_t *proxy_entry, coap_proxy_cache_t *proxy_cache,
803 coap_session_t *session) {
804 coap_proxy_req_t *proxy_req;
805
806 LL_FOREACH(proxy_entry->proxy_req, proxy_req) {
807 if (proxy_req->incoming == session && proxy_req->proxy_cache == proxy_cache) {
808 return proxy_req;
809 }
810 }
811 return NULL;
812}
813
814static void
815coap_proxy_free_response_data(coap_session_t *session COAP_UNUSED, void *app_ptr) {
816 coap_delete_bin_const(app_ptr);
817}
818
819static coap_response_t
820coap_proxy_call_response_handler(coap_session_t *session, const coap_pdu_t *sent,
821 coap_pdu_t *rcvd, coap_bin_const_t *token,
822 coap_proxy_req_t *proxy_req, int replace_mid,
823 int remove_observe) {
825 coap_pdu_t *resp_pdu;
826 coap_pdu_t *fwd_pdu = NULL;
827 size_t size;
828 size_t offset;
829 size_t total;
830 const uint8_t *data;
831 coap_string_t *l_query = NULL;
832 coap_opt_filter_t opt_filter;
833 coap_opt_iterator_t opt_iter;
834 int rcvd_created = 0;
835
836 coap_option_filter_clear(&opt_filter);
837 if (!coap_option_check_critical(session, rcvd, &opt_filter, COAP_CRIT_PROXY)) {
838 /* Need to send back a 5.02 response */
839 resp_pdu = coap_new_error_response(proxy_req->pdu, COAP_RESPONSE_CODE(502), &opt_filter);
840 if (resp_pdu) {
841 rcvd_created = 1;
842 rcvd = resp_pdu;
843 }
844 }
845
846 if (rcvd->code == COAP_RESPONSE_CODE(508) && proxy_req->resource &&
847 proxy_req->resource == session->context->unknown_resource &&
848 proxy_req->resource->is_reverse_proxy && proxy_req && proxy_req->pdu &&
849 coap_check_option(rcvd, COAP_OPTION_HOP_LIMIT, &opt_iter)) {
850 /* Need to hide a 5.08 response if reverse proxy say resource not available */
851 resp_pdu = coap_new_error_response(proxy_req->pdu, COAP_RESPONSE_CODE(404), NULL);
852 if (resp_pdu) {
853 rcvd_created = 1;
854 rcvd = resp_pdu;
855 }
856 }
857
858 if (remove_observe) {
859 coap_opt_filter_t drop_options;
860
861 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
863 /* Correct the token */
864 resp_pdu = coap_pdu_duplicate_lkd(rcvd, session, token->length, token->s,
865 &drop_options, COAP_BOOL_FALSE);
866 } else {
867 /* Correct the token */
868 resp_pdu = coap_pdu_duplicate_lkd(rcvd, session, token->length, token->s,
870 }
871 if (!resp_pdu)
872 goto failed;
873
874 assert(proxy_req);
875 /* In case change in type of request over the proxy */
876 if (proxy_req->pdu)
877 resp_pdu->type = proxy_req->pdu->type;
878
879 if (COAP_PROTO_NOT_RELIABLE(session->proto))
880 resp_pdu->max_size = coap_session_max_pdu_size_lkd(session);
881
882 if (replace_mid)
883 resp_pdu->mid = sent->mid;
884
885 proxy_req->mid = resp_pdu->mid;
886
887 /* We sent early ACK, and this is an ACK, need to convert it to CON */
888 if (COAP_PROTO_NOT_RELIABLE(session->proto) && resp_pdu->type == COAP_MESSAGE_ACK) {
889 resp_pdu->type = COAP_MESSAGE_CON;
890 }
891
892 if (coap_get_data_large(rcvd, &size, &data, &offset, &total)) {
893 uint16_t media_type = 0;
894 int maxage = -1;
895 uint64_t etag = 0;
896 coap_opt_t *option;
897 coap_bin_const_t *body;
898
899 /* COAP_BLOCK_SINGLE_BODY is set, so single body should be given */
900 assert(size == total);
901
902 body = coap_new_bin_const(data, size);
903 if (!body) {
904 coap_log_debug("coap_proxy_call_response_handler: copy data error\n");
905 goto failed;
906 }
907 option = coap_check_option(rcvd, COAP_OPTION_CONTENT_FORMAT, &opt_iter);
908 if (option) {
909 media_type = coap_decode_var_bytes(coap_opt_value(option),
910 coap_opt_length(option));
911 }
912 option = coap_check_option(rcvd, COAP_OPTION_MAXAGE, &opt_iter);
913 if (option) {
914 maxage = coap_decode_var_bytes(coap_opt_value(option),
915 coap_opt_length(option));
916 }
917 option = coap_check_option(rcvd, COAP_OPTION_ETAG, &opt_iter);
918 if (option) {
920 coap_opt_length(option));
921 }
922 if (sent)
923 l_query = coap_get_query(sent);
924 if (!coap_add_data_large_response_lkd(proxy_req->resource, session, sent,
925 resp_pdu,
926 l_query,
927 media_type, maxage, etag, body->length,
928 body->s,
929 coap_proxy_free_response_data,
930 body)) {
931 coap_log_debug("coap_proxy_call_response_handler: add data error\n");
932 goto failed;
933 }
934 }
936 session->context->proxy_response_cb(session,
937 sent,
938 resp_pdu,
939 proxy_req->cache_key),
940 /* context is being freed off */
941 goto failed);
942 if (fwd_pdu) {
943 ret = COAP_RESPONSE_OK;
944 if (coap_send_lkd(session, fwd_pdu) == COAP_INVALID_MID) {
945 ret = COAP_RESPONSE_FAIL;
946 }
947 if (fwd_pdu != resp_pdu) {
948 /* Application created a new PDU */
949 coap_delete_pdu_lkd(resp_pdu);
950 }
951 } else {
952failed:
953 ret = COAP_RESPONSE_FAIL;
954 coap_delete_pdu_lkd(resp_pdu);
955 }
956 if (rcvd_created)
958 coap_delete_string(l_query);
959 return ret;
960}
961
962int COAP_API
964 const coap_pdu_t *request,
965 coap_pdu_t *response,
966 coap_resource_t *resource,
967 coap_cache_key_t *cache_key,
968 coap_proxy_server_list_t *server_list) {
969 int ret;
970
971 coap_lock_lock(return 0);
972 ret = coap_proxy_forward_request_lkd(session,
973 request,
974 response,
975 resource,
976 cache_key,
977 server_list);
979 return ret;
980}
981
982/* https://rfc-editor.org/rfc/rfc7641#section-3.6 */
983static const uint16_t coap_proxy_ignore_options[] = { COAP_OPTION_ETAG,
988 };
989
990int
991coap_proxy_forward_request_lkd(coap_session_t *session,
992 const coap_pdu_t *request,
993 coap_pdu_t *response,
994 coap_resource_t *resource,
995 coap_cache_key_t *cache_key,
996 coap_proxy_server_list_t *server_list) {
997 coap_proxy_entry_t *proxy_entry;
998 size_t size;
999 size_t offset;
1000 size_t total;
1001 coap_binary_t *body_data = NULL;
1002 const uint8_t *data;
1003 coap_pdu_t *pdu = NULL;
1004 coap_bin_const_t r_token = coap_pdu_get_token(request);
1005 uint8_t token[8];
1006 size_t token_len;
1007 coap_proxy_req_t *proxy_req = NULL;
1008 coap_optlist_t *optlist = NULL;
1009 coap_opt_t *option;
1010 coap_opt_iterator_t opt_iter;
1011 coap_uri_t uri;
1012 coap_opt_t *obs_opt = coap_check_option(request,
1014 &opt_iter);
1015 coap_proxy_cache_t *proxy_cache = NULL;
1016 coap_proxy_t proxy_type;
1017 coap_tick_t now;
1018 uint32_t new_maxage;
1019 uint8_t buf[4];
1020
1021 coap_ticks(&now);
1022
1023 /* Set up ongoing session (if not already done) */
1024 proxy_entry = coap_proxy_get_ongoing_session(session, request, response,
1025 server_list);
1026 if (!proxy_entry) {
1027 /* Error response code already set */
1028 return 0;
1029 }
1030 coap_proxy_log_entry(session, request, NULL, "req");
1031
1032 /* Is this a observe cached request? */
1033 if (obs_opt && session->context->proxy_response_cb) {
1034 coap_cache_key_t *cache_key_l;
1035
1036 cache_key_l = coap_cache_derive_key_w_ignore(session, request,
1038 coap_proxy_ignore_options,
1039 sizeof(coap_proxy_ignore_options)/sizeof(coap_proxy_ignore_options[0]));
1040 if (!cache_key_l) {
1041 response->code = COAP_RESPONSE_CODE(505);
1042 return 0;
1043 }
1044 PROXY_CACHE_FIND(proxy_entry->rsp_cache, cache_key_l, proxy_cache);
1045 coap_delete_cache_key(cache_key_l);
1046 }
1047
1048 if (proxy_cache) {
1049 proxy_req = coap_proxy_get_req(proxy_entry, proxy_cache, session);
1050 if (proxy_req) {
1051 if (obs_opt) {
1052 int observe_action;
1053
1054 observe_action = coap_decode_var_bytes(coap_opt_value(obs_opt),
1055 coap_opt_length(obs_opt));
1056
1057 if (observe_action == COAP_OBSERVE_CANCEL) {
1058 if (proxy_req->doing_observe) {
1059 assert(proxy_req->incoming->ref_proxy_subs);
1060 proxy_req->incoming->ref_proxy_subs--;
1061 proxy_req->doing_observe = 0;
1062 }
1063 if (proxy_entry->proxy_req && !proxy_entry->proxy_req->next &&
1064 proxy_req->token_used) {
1065 coap_binary_t tmp;
1066
1067 coap_log_debug("coap_proxy_forward_request: Using coap_cancel_observe() to do Proxy OBSERVE cancellation\n");
1068 /* Unfortunately need to change the ptr type to be r/w */
1069 memcpy(&tmp.s, &proxy_req->token_used->s, sizeof(tmp.s));
1070 tmp.length = proxy_req->token_used->length;
1071 coap_cancel_observe_lkd(proxy_entry->ongoing, &tmp, COAP_MESSAGE_CON);
1072 /* Let the upstream cancellation be the response */
1073 return 1;
1074 } else {
1075 obs_opt = 0;
1076 goto return_cached_info;
1077 }
1078 } else if (observe_action == COAP_OBSERVE_ESTABLISH) {
1079 /* Client must be re-registering */
1080 if (proxy_cache && (proxy_cache->expire + COAP_TICKS_PER_SECOND) < now) {
1081 /* Need to get an updated rsp_pdu */
1082 coap_proxy_del_req(proxy_entry, proxy_req);
1083 proxy_req = NULL;
1084 proxy_cache = NULL;
1085 } else {
1086 goto return_cached_info;
1087 }
1088 }
1089 } else {
1090 if (proxy_cache && (proxy_cache->expire + COAP_TICKS_PER_SECOND) < now) {
1091 /* Need to get an updated rsp_pdu */
1092 coap_proxy_del_req(proxy_entry, proxy_req);
1093 proxy_req = NULL;
1094 proxy_cache = NULL;
1095 } else {
1096 goto return_cached_info;
1097 }
1098 }
1099 }
1100 }
1101
1102 if (!proxy_req) {
1103 proxy_req = coap_malloc_type(COAP_STRING, sizeof(coap_proxy_req_t));
1104 if (proxy_req == NULL) {
1105 goto failed;
1106 }
1107 memset(proxy_req, 0, sizeof(coap_proxy_req_t));
1108 LL_PREPEND(proxy_entry->proxy_req, proxy_req);
1109
1110 proxy_req->pdu = coap_const_pdu_reference_lkd(request);
1111 proxy_req->resource = resource;
1112 proxy_req->incoming = session;
1113 proxy_req->cache_key = cache_key;
1114 proxy_req->proxy_cache = proxy_cache;
1115
1116 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used, "add");
1117 }
1118
1119 if (proxy_cache) {
1120 coap_bin_const_t j_token;
1121
1122 if (obs_opt)
1123 proxy_cache->ref++;
1124 coap_delete_bin_const(proxy_req->token_used);
1125 j_token = coap_pdu_get_token(proxy_cache->rsp_pdu);
1126 proxy_req->token_used = coap_new_bin_const(j_token.s, j_token.length);
1127 if (proxy_req->token_used == NULL) {
1128 goto failed;
1129 }
1130 goto return_cached_info;
1131 }
1132
1133 /* Get a new token for ongoing session */
1134 coap_session_new_token(proxy_entry->ongoing, &token_len, token);
1135 coap_delete_bin_const(proxy_req->token_used);
1136 proxy_req->token_used = coap_new_bin_const(token, token_len);
1137 if (proxy_req->token_used == NULL) {
1138 goto failed;
1139 }
1140 coap_pdu_release_lkd(proxy_req->pdu);
1141 proxy_req->pdu = coap_const_pdu_reference_lkd(request);
1142
1143 /* Need to create the ongoing request pdu entry */
1144 proxy_type = coap_proxy_map_type(server_list->type);
1145 if (proxy_type & COAP_PROXY_BIT_STRIP) {
1146 /*
1147 * Need to replace Proxy-Uri with Uri-Host (and Uri-Port)
1148 * and strip out Proxy-Scheme.
1149 */
1150
1151 /*
1152 * Build up the ongoing PDU that we are going to send
1153 */
1154 pdu = coap_pdu_init(request->type, request->code,
1155 coap_new_message_id_lkd(proxy_entry->ongoing),
1156 coap_session_max_pdu_size_lkd(proxy_entry->ongoing));
1157 if (!pdu) {
1158 goto failed;
1159 }
1160
1161 if (coap_is_mcast(&proxy_entry->ongoing->addr_info.remote)) {
1162 pdu->type = COAP_MESSAGE_NON;
1163 }
1164
1165 if (!coap_add_token(pdu, token_len, token)) {
1166 goto failed;
1167 }
1168
1169 /* Copy the remaining options across */
1170 coap_option_iterator_init(request, &opt_iter, COAP_OPT_ALL);
1171 while ((option = coap_option_next(&opt_iter))) {
1172 switch (opt_iter.number) {
1175 coap_opt_length(option),
1176 &uri) < 0) {
1177 /* Need to return a 5.05 RFC7252 Section 5.7.2 */
1178 coap_log_warn("Proxy URI not decodable\n");
1179 goto failed;
1180 }
1181 if (!coap_uri_into_optlist(&uri, NULL, &optlist, 0)) {
1182 coap_log_err("Failed to create options for URI\n");
1183 goto failed;
1184 }
1185 break;
1187 break;
1188 case COAP_OPTION_BLOCK1:
1189 case COAP_OPTION_BLOCK2:
1192 /* These are not passed on */
1193 break;
1196 break;
1197 default:
1198 coap_insert_optlist(&optlist,
1199 coap_new_optlist(opt_iter.number,
1200 coap_opt_length(option),
1201 coap_opt_value(option)));
1202 break;
1203 }
1204 }
1205
1206 /* Update pdu with options */
1207 coap_add_optlist_pdu(pdu, &optlist);
1208 coap_delete_optlist(optlist);
1209 optlist = NULL;
1210 } else {
1211 /*
1212 * Duplicate request PDU for onward transmission (with new token).
1213 */
1214 pdu = coap_pdu_duplicate_lkd(request, proxy_entry->ongoing, token_len,
1215 token, NULL, COAP_BOOL_FALSE);
1216 if (!pdu) {
1217 coap_log_debug("proxy: PDU generation error\n");
1218 goto failed;
1219 }
1220 if (COAP_PROTO_NOT_RELIABLE(session->proto))
1221 pdu->max_size = coap_session_max_pdu_size_lkd(proxy_entry->ongoing);
1222 }
1223
1224 if (coap_get_data_large(request, &size, &data, &offset, &total)) {
1225 /* COAP_BLOCK_SINGLE_BODY is set, so single body should be given */
1226 assert(size == total);
1227 /*
1228 * Need to take a copy of the data as request PDU may go away before
1229 * all data is transmitted.
1230 */
1231 body_data = coap_new_binary(total);
1232 if (!body_data) {
1233 coap_log_debug("proxy: body build memory error\n");
1234 goto failed;
1235 }
1236 memcpy(body_data->s, data, size);
1237 data = body_data->s;
1238 if (!coap_add_data_large_request_lkd(proxy_entry->ongoing, pdu, total, data,
1239 coap_proxy_release_body_data, body_data)) {
1240 coap_log_debug("proxy: add data error\n");
1241 goto failed;
1242 }
1243 }
1244
1245 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used, "fwd");
1246 proxy_entry->mid = coap_send_lkd(proxy_entry->ongoing, pdu);
1247 if (proxy_entry->mid == COAP_INVALID_MID) {
1248 pdu = NULL;
1249 coap_log_debug("proxy: upstream PDU send error\n");
1250 goto failed;
1251 }
1252 if (request->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(proxy_entry->ongoing->proto)) {
1253 proxy_entry->req_start_ticks = now;
1254 }
1255
1256 /*
1257 * Do not update the response code (hence empty ACK) as will be sending
1258 * separate response when response comes back from upstream server
1259 */
1260
1261 return 1;
1262
1263failed:
1264 response->code = COAP_RESPONSE_CODE(500);
1265 coap_delete_optlist(optlist);
1267 return 0;
1268
1269return_cached_info:
1270 if (obs_opt && !proxy_req->doing_observe) {
1271 int observe_action;
1272
1273 observe_action = coap_decode_var_bytes(coap_opt_value(obs_opt),
1274 coap_opt_length(obs_opt));
1275
1276 if (observe_action == COAP_OBSERVE_ESTABLISH) {
1277 proxy_req->doing_observe = 1;
1278 proxy_req->incoming->ref_proxy_subs++;
1279 }
1280 }
1281 coap_log_debug("* %s: Using Proxy Cache (Active %d)\n",
1282 coap_session_str(session),
1283 proxy_cache->ref);
1284 coap_proxy_log_entry(session, request, &proxy_cache->rsp_pdu->actual_token, "rspc");
1285 if (now < proxy_cache->expire) {
1286 new_maxage = (uint32_t)((proxy_cache->expire - now) / COAP_TICKS_PER_SECOND);
1287 if (new_maxage == 0)
1288 new_maxage = 1;
1289 } else {
1290 new_maxage = 1;
1291 }
1292 coap_update_option(proxy_cache->rsp_pdu,
1294 coap_encode_var_safe(buf, sizeof(buf), new_maxage), buf);
1295 coap_proxy_call_response_handler(session, request, proxy_cache->rsp_pdu,
1296 &r_token, proxy_req, 1, obs_opt ? 0 : 1);
1297 if (!obs_opt)
1298 coap_proxy_del_req(proxy_entry, proxy_req);
1299 return 1;
1300}
1301
1302coap_proxy_req_t *
1303coap_proxy_map_outgoing_request(coap_session_t *ongoing,
1304 const coap_pdu_t *received,
1305 coap_proxy_entry_t **u_proxy_entry) {
1306 coap_proxy_entry_t *proxy_list = ongoing->context->proxy_list;
1307 size_t proxy_list_count = ongoing->context->proxy_list_count;
1308 size_t i;
1309 coap_bin_const_t rcv_token = coap_pdu_get_token(received);
1310 coap_proxy_entry_t *proxy_entry = NULL;
1311 coap_proxy_req_t *proxy_req;
1312
1313 for (i = 0; i < proxy_list_count; i++) {
1314 proxy_entry = &proxy_list[i];
1315 if (proxy_entry->ongoing == ongoing) {
1316 LL_FOREACH(proxy_entry->proxy_req, proxy_req) {
1317 if (coap_binary_equal(&rcv_token, proxy_req->token_used)) {
1318 coap_ticks(&proxy_entry->last_used);
1319 if (u_proxy_entry)
1320 *u_proxy_entry = proxy_entry;
1321 return proxy_req;
1322 }
1323 }
1324 }
1325 }
1327 char scratch[24];
1328 size_t size;
1329
1330 scratch[0] = '\000';
1331 for (i = 0; i < rcv_token.length; i++) {
1332 size = strlen(scratch);
1333 snprintf(&scratch[size], sizeof(scratch)-size,
1334 "%02x", rcv_token.s[i]);
1335 }
1336 coap_log_debug(" proxy token to match {%s}\n", scratch);
1337 for (i = 0; i < proxy_list_count; i++) {
1338 proxy_entry = &proxy_list[i];
1339 LL_FOREACH(proxy_entry->proxy_req, proxy_req) {
1340 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used, "miss");
1341 }
1342 }
1343 }
1344 return NULL;
1345}
1346
1349 const coap_pdu_t *received,
1350 coap_cache_key_t **cache_key) {
1351 int ret;
1352
1353 coap_lock_lock(return 0);
1354 ret = coap_proxy_forward_response_lkd(session,
1355 received,
1356 cache_key);
1358 return ret;
1359}
1360
1362coap_proxy_forward_response_lkd(coap_session_t *session,
1363 const coap_pdu_t *received,
1364 coap_cache_key_t **cache_key) {
1365 coap_pdu_t *pdu = NULL;
1366 coap_session_t *incoming = NULL;
1367 size_t size;
1368 const uint8_t *data;
1369 coap_optlist_t *optlist = NULL;
1370 coap_opt_t *option;
1371 coap_opt_iterator_t opt_iter;
1372 size_t offset;
1373 size_t total;
1374 coap_proxy_entry_t *proxy_entry = NULL;
1375 uint16_t media_type = COAP_MEDIATYPE_TEXT_PLAIN;
1376 int maxage = -1;
1377 uint64_t etag = 0;
1378 coap_pdu_code_t rcv_code = coap_pdu_get_code(received);
1379 coap_bin_const_t req_token;
1380 coap_binary_t *body_data = NULL;
1381 coap_pdu_t *req_pdu;
1382 coap_resource_t *resource;
1383 coap_proxy_req_t *proxy_req = NULL;
1384
1385 proxy_req = coap_proxy_map_outgoing_request(session, received, &proxy_entry);
1386 if (!proxy_req || proxy_req->incoming->server_list) {
1387 coap_log_warn("Unknown proxy ongoing session response received - ignored\n");
1388 return COAP_RESPONSE_OK;
1389 }
1390
1391 proxy_entry->req_start_ticks = 0;
1392 req_pdu = proxy_req->pdu;
1393 req_token = coap_pdu_get_token(req_pdu);
1394 resource = proxy_req->resource;
1395 incoming = proxy_req->incoming;
1396
1397 coap_log_debug("** process upstream incoming %d.%02d response:\n",
1398 COAP_RESPONSE_CLASS(rcv_code), rcv_code & 0x1F);
1399
1400 if (coap_get_data_large(received, &size, &data, &offset, &total)) {
1401 /* COAP_BLOCK_SINGLE_BODY is set, so single body should be given */
1402 assert(size == total);
1403 body_data = coap_new_binary(total);
1404 if (!body_data) {
1405 coap_log_debug("body build memory error\n");
1406 goto remove_match;
1407 }
1408 memcpy(body_data->s, data, size);
1409 data = body_data->s;
1410 }
1411
1412 /*
1413 * Build up the ongoing PDU that we are going to send to proxy originator
1414 * as separate response
1415 */
1416 pdu = coap_pdu_init(req_pdu->type, rcv_code,
1417 coap_new_message_id_lkd(incoming),
1419 if (!pdu) {
1420 coap_log_debug("Failed to create ongoing proxy response PDU\n");
1421 goto remove_match;
1422 }
1423
1424 if (!coap_add_token(pdu, req_token.length, req_token.s)) {
1425 coap_log_debug("cannot add token to ongoing proxy response PDU\n");
1426 }
1427
1428 /*
1429 * Copy the options across, skipping those needed for
1430 * coap_add_data_response_large()
1431 */
1432 coap_option_iterator_init(received, &opt_iter, COAP_OPT_ALL);
1433 while ((option = coap_option_next(&opt_iter))) {
1434 switch (opt_iter.number) {
1436 media_type = coap_decode_var_bytes(coap_opt_value(option),
1437 coap_opt_length(option));
1438 break;
1439 case COAP_OPTION_MAXAGE:
1440 maxage = coap_decode_var_bytes(coap_opt_value(option),
1441 coap_opt_length(option));
1442 break;
1443 case COAP_OPTION_ETAG:
1445 coap_opt_length(option));
1446 break;
1447 case COAP_OPTION_BLOCK2:
1449 case COAP_OPTION_SIZE2:
1450 break;
1451 default:
1452 coap_insert_optlist(&optlist,
1453 coap_new_optlist(opt_iter.number,
1454 coap_opt_length(option),
1455 coap_opt_value(option)));
1456 break;
1457 }
1458 }
1459 coap_add_optlist_pdu(pdu, &optlist);
1460 coap_delete_optlist(optlist);
1461
1462 if (size > 0) {
1463 coap_string_t *l_query = coap_get_query(req_pdu);
1464
1465 coap_add_data_large_response_lkd(resource, incoming, req_pdu, pdu,
1466 l_query,
1467 media_type, maxage, etag, size, data,
1468 coap_proxy_release_body_data,
1469 body_data);
1470 body_data = NULL;
1471 coap_delete_string(l_query);
1472 }
1473
1474 if (cache_key)
1475 *cache_key = proxy_req->cache_key;
1476
1477 coap_send_lkd(incoming, pdu);
1478
1479remove_match:
1480 option = coap_check_option(received, COAP_OPTION_OBSERVE, &opt_iter);
1481 /* Need to remove matching token entry (apart from an Observe response) */
1482 if (option == NULL) {
1483 if (proxy_entry->proxy_req) {
1484 /* Do not delete cache key here - caller's responsibility */
1485 proxy_req->cache_key = NULL;
1486 coap_proxy_del_req(proxy_entry, proxy_req);
1487 }
1488 } else if (!proxy_req->doing_observe) {
1489 option = coap_check_option(proxy_req->pdu, COAP_OPTION_OBSERVE, &opt_iter);
1490 if (option &&
1493 proxy_req->doing_observe = 1;
1494 proxy_req->incoming->ref_proxy_subs++;
1495 }
1496 }
1497 coap_delete_binary(body_data);
1498 return COAP_RESPONSE_OK;
1499}
1500
1501void
1502coap_proxy_process_incoming(coap_session_t *session,
1503 coap_pdu_t *rcvd,
1504 void *body_data, coap_proxy_req_t *proxy_req,
1505 coap_proxy_entry_t *proxy_entry) {
1506 coap_opt_t *obs_opt;
1507 coap_opt_t *option;
1508 coap_opt_iterator_t opt_iter;
1510 coap_bin_const_t token;
1511 int can_cache = 0;
1512
1513 if (COAP_RESPONSE_CLASS(rcvd->code) == 2) {
1514 switch ((int)rcvd->code) {
1515 case COAP_RESPONSE_CODE(201):
1516 case COAP_RESPONSE_CODE(202):
1517 case COAP_RESPONSE_CODE(203):
1518 case COAP_RESPONSE_CODE(204):
1519 case COAP_RESPONSE_CODE(205):
1520 can_cache = 1;
1521 break;
1522 /* 2.13 should be handled by block transfer logic */
1523 case COAP_RESPONSE_CODE(213):
1524 default:
1525 break;
1526 }
1527 }
1528 obs_opt = coap_check_option(rcvd, COAP_OPTION_OBSERVE, &opt_iter);
1529
1530 /* See if we are doing proxy caching */
1531 if (can_cache && obs_opt) {
1532 coap_proxy_cache_t *proxy_cache;
1533 coap_cache_key_t *cache_key_l;
1534 coap_tick_t now;
1535 uint64_t expire;
1536
1537 /* Need to cache the response */
1538 if (proxy_req->proxy_cache) {
1539 coap_delete_pdu_lkd(proxy_req->proxy_cache->rsp_pdu);
1540 proxy_cache = proxy_req->proxy_cache;
1541 } else {
1542 proxy_cache = coap_malloc_type(COAP_STRING, sizeof(coap_proxy_cache_t));
1543 if (proxy_cache == NULL) {
1544 goto cache_fail;
1545 }
1546 memset(proxy_cache, 0, sizeof(coap_proxy_cache_t));
1547 cache_key_l = coap_cache_derive_key_w_ignore(session, proxy_req->pdu,
1549 coap_proxy_ignore_options,
1550 sizeof(coap_proxy_ignore_options)/sizeof(coap_proxy_ignore_options[0]));
1551 if (!cache_key_l) {
1552 coap_free_type(COAP_STRING, proxy_cache);
1553 goto cache_fail;
1554 }
1555 memcpy(&proxy_cache->cache_req, cache_key_l,
1556 sizeof(proxy_cache->cache_req));
1557 coap_delete_cache_key(cache_key_l);
1558
1559 proxy_cache->req_pdu = coap_pdu_reference_lkd(proxy_req->pdu);
1560 proxy_req->proxy_cache = proxy_cache;
1561
1562 PROXY_CACHE_ADD(proxy_entry->rsp_cache, proxy_cache);
1563 proxy_cache->ref++;
1564 }
1565 if (rcvd->body_data) {
1566 /* More data than that held in the PDU */
1567 const uint8_t *data;
1568 size_t data_len;
1569
1570 proxy_cache->rsp_pdu = coap_pdu_duplicate_lkd(rcvd,
1571 session,
1572 rcvd->actual_token.length,
1573 rcvd->actual_token.s,
1575 if (proxy_cache->rsp_pdu) {
1576 if (coap_get_data(rcvd, &data_len, &data)) {
1577 coap_binary_t *copy;
1578
1579 copy = coap_new_binary(data_len);
1580 if (copy) {
1581 memcpy(copy->s, data, data_len);
1582 proxy_cache->rsp_pdu->data_free = copy;
1583 proxy_cache->rsp_pdu->body_data = copy->s;
1584 proxy_cache->rsp_pdu->body_length = copy->length;
1585 proxy_cache->rsp_pdu->body_total = copy->length;
1586 } else {
1587 proxy_cache->rsp_pdu->body_data = NULL;
1588 proxy_cache->rsp_pdu->body_length = 0;
1589 proxy_cache->rsp_pdu->body_total = 0;
1590 }
1591 }
1592 }
1593 } else {
1594 /* The simple case */
1595 proxy_cache->rsp_pdu = coap_pdu_reference_lkd(rcvd);
1596 }
1597 option = coap_check_option(rcvd, COAP_OPTION_ETAG, &opt_iter);
1598 if (option) {
1599 proxy_cache->etag = coap_decode_var_bytes8(coap_opt_value(option),
1600 coap_opt_length(option));
1601 } else {
1602 proxy_cache->etag = 0;
1603 }
1604 coap_ticks(&now);
1605 option = coap_check_option(rcvd, COAP_OPTION_MAXAGE, &opt_iter);
1606 if (option) {
1607 expire = coap_decode_var_bytes(coap_opt_value(option),
1608 coap_opt_length(option));
1609 } else {
1610 /* Default is 60 seconds */
1611 expire = 60;
1612 }
1613 proxy_cache->expire = now + expire * COAP_TICKS_PER_SECOND;
1614
1615 /* Update all the cache listeners */
1616 LL_FOREACH(proxy_entry->proxy_req, proxy_req) {
1617 if (proxy_req->proxy_cache == proxy_cache) {
1618 if (!proxy_req->doing_observe) {
1619 coap_opt_t *req_obs;
1620
1621 req_obs = coap_check_option(proxy_req->pdu, COAP_OPTION_OBSERVE, &opt_iter);
1622 if (req_obs &&
1625 proxy_req->doing_observe = 1;
1626 proxy_req->incoming->ref_proxy_subs++;
1627 }
1628 }
1629 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used, "rsp");
1630 token = coap_pdu_get_token(proxy_req->pdu);
1631 if (coap_proxy_call_response_handler(proxy_req->incoming, proxy_req->pdu,
1632 rcvd, &token,
1633 proxy_req, 0, 0) == COAP_RESPONSE_OK) {
1634 /* At least one success */
1635 ret = COAP_RESPONSE_OK;
1636 }
1637 }
1638 }
1639 goto finish;
1640 }
1641cache_fail:
1642 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used, "rspn");
1643 token = coap_pdu_get_token(proxy_req->pdu);
1644 ret = coap_proxy_call_response_handler(proxy_req->incoming, proxy_req->pdu,
1645 rcvd, &token, proxy_req, 0, 0);
1646 if (!obs_opt)
1647 coap_proxy_del_req(proxy_entry, proxy_req);
1648
1649finish:
1650 if (ret == COAP_RESPONSE_FAIL) {
1651 coap_send_rst_lkd(session, rcvd);
1653 } else {
1654 coap_send_ack_lkd(session, rcvd);
1656 }
1657 coap_free_type(COAP_STRING, body_data);
1658}
1659
1660/*
1661 */
1663coap_proxy_local_write(coap_session_t *session, coap_pdu_t *pdu) {
1664 coap_pdu_t *response = NULL;
1665 coap_resource_t *resource;
1667
1668 resource = session->context->unknown_resource ?
1669 session->context->unknown_resource :
1670 session->context->proxy_uri_resource;
1671 if (!resource) {
1672 coap_log_err("coap_proxy_local_write: Unknown or Proxy resource not defined\n");
1673 goto fail;
1674 }
1675
1676 response = coap_pdu_init(pdu->type == COAP_MESSAGE_CON ?
1678 0, pdu->mid, coap_session_max_pdu_size_lkd(session));
1679 if (!response) {
1680 coap_log_err("coap_proxy_local_write: Could not create response PDU\n");
1681 goto fail;
1682 }
1683 response->session = session;
1684
1685 if (!coap_add_token(response, pdu->actual_token.length,
1686 pdu->actual_token.s)) {
1687 goto fail;
1688 }
1689
1690 coap_log_debug("* %s: internal: sent %4" PRIdS " bytes\n",
1691 coap_session_str(session),
1692 pdu->used_size + coap_pdu_encode_header(pdu, session->proto));
1694
1695 mid = pdu->mid;
1696 if (!coap_proxy_forward_request_lkd(session, pdu, response, resource,
1697 NULL, session->server_list)) {
1698 coap_log_debug("coap_proxy_local_write: Failed to forward PDU\n");
1699 mid = COAP_INVALID_MID;
1700 }
1701fail:
1702 coap_delete_pdu_lkd(response);
1704 return mid;
1705}
1706
1709 coap_proxy_server_list_t *server_list) {
1710 coap_session_t *session;
1711
1712 coap_lock_lock(return NULL);
1713 session = coap_new_client_session_proxy_lkd(ctx, server_list);
1715 return session;
1716}
1717
1719coap_new_client_session_proxy_lkd(coap_context_t *ctx,
1720 coap_proxy_server_list_t *server_list) {
1721 coap_session_t *session;
1722 coap_addr_info_t *info_list = NULL;
1723 coap_str_const_t remote;
1724
1726
1727#if COAP_IPV6_SUPPORT
1728 remote.s = (const uint8_t *)"::1";
1729#elif COAP_IPV4_SUPPORT
1730 remote.s = (const uint8_t *)"127.0.0.1";
1731#else /* !COAP_IPV6_SUPPORT && ! COAP_IPV4_SUPPORT */
1732 coap_log_warn("coap_new_client_session_proxy: No IPv4 or IPv6 support\n");
1733 return NULL;
1734#endif /* !COAP_IPV6_SUPPORT && ! COAP_IPV4_SUPPORT */
1735 remote.length = strlen((const char *)remote.s);
1736 /* resolve internal remote address where proxy session is 'connecting' to */
1737 info_list = coap_resolve_address_info_lkd(&remote, 0, 0, 0, 0,
1738 0,
1741 if (!info_list) {
1742 coap_log_warn("coap_new_client_session_proxy: Unable to resolve IP address\n");
1743 return NULL;
1744 }
1745
1746 session = coap_new_client_session3_lkd(ctx, NULL, &info_list->addr, COAP_PROTO_UDP,
1747 NULL, NULL, NULL);
1748
1749 if (session) {
1750 session->server_list = server_list;
1751 }
1752 coap_free_address_info(info_list);
1753 return session;
1754}
1755
1757coap_proxy_fwd_add_client_session(coap_session_t *session, const char *use_ip,
1758 uint16_t use_port,
1759 coap_proxy_server_list_t *server_list) {
1760 coap_proxy_entry_t *ret;
1761
1762 coap_lock_lock(return 0);
1763 ret = coap_proxy_fwd_add_client_session_lkd(session, use_ip, use_port, server_list);
1765 return ret;
1766}
1767
1769coap_proxy_fwd_add_client_session_lkd(coap_session_t *session, const char *use_ip,
1770 uint16_t use_port,
1771 coap_proxy_server_list_t *server_list) {
1772#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
1773 char addr[INET6_ADDRSTRLEN];
1774#else /* WITH_LWIP || WITH_CONTIKI */
1775 char addr[40];
1776#endif /* WITH_LWIP || WITH_CONTIKI */
1777 coap_proxy_entry_t *proxy_entry;
1778 coap_uri_t uri;
1779
1780 if (!use_ip && !coap_print_ip_addr(&session->addr_info.remote, addr, sizeof(addr))) {
1781 return NULL;
1782 }
1783 if (!use_ip)
1784 use_ip = addr;
1785
1786 memset(&uri, 0, sizeof(uri));
1787 uri.port = use_port;
1788 switch (session->proto) {
1789 case COAP_PROTO_UDP:
1791 if (uri.port == 0)
1792 uri.port = 5683;
1793 break;
1794 case COAP_PROTO_DTLS:
1796 if (uri.port == 0)
1797 uri.port = 5684;
1798 break;
1799 case COAP_PROTO_TCP:
1801 if (uri.port == 0)
1802 uri.port = 5683;
1803 break;
1804 case COAP_PROTO_TLS:
1806 if (uri.port == 0)
1807 uri.port = 5684;
1808 break;
1809 case COAP_PROTO_WS:
1811 if (uri.port == 0)
1812 uri.port = 80;
1813 break;
1814 case COAP_PROTO_WSS:
1816 if (uri.port == 0)
1817 uri.port = 443;
1818 break;
1819 case COAP_PROTO_LAST:
1820 case COAP_PROTO_NONE:
1821 default:
1822 return NULL;
1823 }
1824 if (memcmp(use_ip, "::ffff:", 7) == 0) {
1825 /* IPv4 in IPv6 format */
1826 uri.host.s = (const uint8_t *)&use_ip[7];
1827 uri.host.length = strlen(&use_ip[7]);
1828 } else {
1829 uri.host.s = (const uint8_t *)use_ip;
1830 uri.host.length = strlen(use_ip);
1831 }
1832 if (!uri.host.length)
1833 return NULL;
1834
1835 proxy_entry = coap_proxy_get_add_list_entry(NULL, session, uri, server_list, 1);
1836 if (proxy_entry) {
1837 coap_log_debug("* %s: proxy_entry %.*s:%d %p created (tot count = % " PRIdS ")\n",
1838 coap_session_str(proxy_entry->ongoing),
1839 (int)proxy_entry->uri.host.length, proxy_entry->uri.host.s,
1840 proxy_entry->uri.port,
1841 (void *)proxy_entry,
1842 session->context->proxy_list_count);
1843 proxy_entry->idle_timeout_ticks = 0;
1844 proxy_entry->req_start_ticks = 0;
1845 proxy_entry->track_client_session = 0;
1847 return proxy_entry;
1848 }
1849 return NULL;
1850}
1851
1852void
1853coap_delete_proxy_subscriber(coap_session_t *session, coap_bin_const_t *token,
1854 coap_mid_t mid, coap_proxy_subs_delete_t type) {
1855 /* Need to check is there is a proxy subscription active and delete it */
1856 coap_context_t *context = session->context;
1857 size_t i;
1858
1859 for (i = 0; i < context->proxy_list_count; i++) {
1860 coap_proxy_entry_t *proxy_entry = &context->proxy_list[i];
1861 coap_proxy_req_t *proxy_req, *treq;
1862
1863 LL_FOREACH_SAFE(proxy_entry->proxy_req, proxy_req, treq) {
1864 if (proxy_req->incoming == session) {
1865 coap_bin_const_t req_token;
1866 int match = 0;
1867
1868 req_token = coap_pdu_get_token(proxy_req->pdu);
1869 switch (type) {
1870 case COAP_PROXY_SUBS_ALL:
1871 match = 1;
1872 break;
1873 case COAP_PROXY_SUBS_TOKEN:
1874 if (token && coap_binary_equal(token, &req_token))
1875 match = 1;
1876 break;
1877 case COAP_PROXY_SUBS_MID:
1878 if (proxy_req->mid == mid)
1879 match = 1;
1880 break;
1881 default:
1882 break;
1883 }
1884
1885 if (match && proxy_entry->proxy_req && ! proxy_entry->proxy_req->next &&
1886 proxy_req->token_used) {
1887 coap_binary_t tmp;
1888
1889 coap_log_debug("coap_delete_proxy_subscriber: Using coap_cancel_observe() to do Proxy OBSERVE cancellation\n");
1890 /* Unfortunately need to change the ptr type to be r/w */
1891 memcpy(&tmp.s, &proxy_req->token_used->s, sizeof(tmp.s));
1892 tmp.length = proxy_req->token_used->length;
1893 coap_cancel_observe_lkd(proxy_entry->ongoing, &tmp, COAP_MESSAGE_CON);
1894 }
1895 coap_proxy_del_req(proxy_entry, proxy_req);
1896 }
1897 }
1898 }
1899}
1900
1901#else /* ! COAP_PROXY_SUPPORT */
1902
1903int
1905 return 0;
1906}
1907
1908COAP_API int
1910 const coap_pdu_t *request,
1911 coap_pdu_t *response,
1912 coap_resource_t *resource,
1913 coap_cache_key_t *cache_key,
1914 coap_proxy_server_list_t *server_list) {
1915 (void)session;
1916 (void)request;
1917 (void)resource;
1918 (void)cache_key;
1919 (void)server_list;
1920 response->code = COAP_RESPONSE_CODE(500);
1921 return 0;
1922}
1923
1926 const coap_pdu_t *received,
1927 coap_cache_key_t **cache_key) {
1928 (void)session;
1929 (void)received;
1930 (void)cache_key;
1931 return COAP_RESPONSE_OK;
1932}
1933
1934int
1936 (void)scheme;
1937 return 0;
1938}
1939#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.
int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
@ COAP_RESOLVE_TYPE_REMOTE
remote side of session
#define COAP_UNIX_PATH_MAX
#define INET6_ADDRSTRLEN
Definition coap_debug.c:234
struct coap_lg_crcv_t coap_lg_crcv_t
struct coap_cache_key_t coap_cache_key_t
struct coap_proxy_entry_t coap_proxy_entry_t
Proxy information.
struct coap_resource_t coap_resource_t
#define PRIdS
#define PRIu64
Library specific build wrapper for coap_internal.h.
#define COAP_API
@ COAP_STRING
Definition coap_mem.h:33
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().
#define NULL
Definition coap_option.h:30
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
@ COAP_OPTION_OBSERVE
Definition coap_option.h:75
@ COAP_OPTION_ETAG
Definition coap_option.h:73
@ COAP_OPTION_MAXAGE
Definition coap_option.h:83
@ COAP_OPTION_SIZE2
Definition coap_option.h:92
@ COAP_OPTION_Q_BLOCK2
Definition coap_option.h:93
@ COAP_OPTION_PROXY_SCHEME
Definition coap_option.h:95
@ COAP_OPTION_HOP_LIMIT
Definition coap_option.h:85
@ COAP_OPTION_URI_PORT
Definition coap_option.h:76
@ COAP_OPTION_URI_HOST
Definition coap_option.h:72
@ COAP_OPTION_BLOCK2
Definition coap_option.h:90
@ COAP_OPTION_RTAG
Definition coap_option.h:99
@ COAP_OPTION_BLOCK1
Definition coap_option.h:91
@ COAP_OPTION_Q_BLOCK1
Definition coap_option.h:87
@ COAP_OPTION_OSCORE
Definition coap_option.h:78
@ COAP_OPTION_CONTENT_FORMAT
Definition coap_option.h:80
@ COAP_OPTION_PROXY_URI
Definition coap_option.h:94
coap_uri_scheme_t
The scheme specifiers.
Definition coap_uri.h:30
@ COAP_URI_SCHEME_COAPS_WS
Definition coap_uri.h:38
@ COAP_URI_SCHEME_COAPS_TCP
Definition coap_uri.h:34
@ COAP_URI_SCHEME_COAPS
Definition coap_uri.h:32
@ COAP_URI_SCHEME_COAP_TCP
Definition coap_uri.h:33
@ COAP_URI_SCHEME_COAP_WS
Definition coap_uri.h:37
@ COAP_URI_SCHEME_HTTPS
Definition coap_uri.h:36
@ COAP_URI_SCHEME_COAP
Definition coap_uri.h:31
@ COAP_URI_SCHEME_LAST
Definition coap_uri.h:39
@ COAP_URI_SCHEME_HTTP
Definition coap_uri.h:35
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:1235
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:1642
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:1252
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:40
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition coap_time.h:149
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition coap_time.h:164
#define COAP_MAX_DELAY_TICKS
Definition coap_time.h:231
uint16_t coap_new_message_id_lkd(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
int coap_delete_node_lkd(coap_queue_t *node)
Destroys specified node.
Definition coap_net.c:206
int coap_option_check_critical(coap_session_t *session, coap_pdu_t *pdu, coap_opt_filter_t *unknown, coap_crit_type_t is_proxy)
Verifies that pdu contains no unknown critical options, duplicate options or the options defined as R...
Definition coap_net.c:1033
coap_addr_info_t * coap_resolve_address_info_lkd(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...
int coap_remove_from_queue(coap_queue_t **queue, coap_session_t *session, coap_mid_t mid, coap_bin_const_t *token, coap_queue_t **node)
This function removes the element with given mid from the list given list.
Definition coap_net.c:3280
@ COAP_CRIT_PROXY
coap_pdu_t * coap_new_error_response(const coap_pdu_t *request, coap_pdu_code_t code, coap_opt_filter_t *opts)
Creates a new ACK PDU with specified error code.
Definition coap_net.c:3461
coap_response_t
Definition coap_net.h:51
void coap_ticks(coap_tick_t *t)
Returns the current value of an internal tick counter.
Definition coap_time.c:90
@ COAP_RESPONSE_FAIL
Response not liked - send CoAP RST packet.
Definition coap_net.h:52
@ COAP_RESPONSE_OK
Response is fine.
Definition coap_net.h:53
unsigned int coap_encode_var_safe(uint8_t *buf, size_t length, unsigned int val)
Encodes multiple-length byte sequences.
Definition coap_encode.c:47
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:71
#define coap_lock_callback_ret_release(r, func, failed)
Dummy for no thread-safe code.
#define coap_lock_unlock()
Dummy for no thread-safe code.
#define coap_lock_check_locked()
Dummy for no thread-safe code.
#define coap_lock_lock(failed)
Dummy for no thread-safe code.
#define coap_log_debug(...)
Definition coap_debug.h:126
coap_log_t coap_get_log_level(void)
Get the current logging level.
Definition coap_debug.c:103
void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu)
Display the contents of the specified pdu.
Definition coap_debug.c:813
size_t coap_print_addr(const coap_address_t *addr, unsigned char *buf, size_t len)
Print the address into the defined buffer.
Definition coap_debug.c:241
const char * coap_session_str(const coap_session_t *session)
Get session description.
const char * coap_print_ip_addr(const coap_address_t *addr, char *buf, size_t len)
Print the IP address into the defined buffer.
Definition coap_debug.c:424
#define coap_log_info(...)
Definition coap_debug.h:114
#define coap_log_warn(...)
Definition coap_debug.h:108
#define coap_log_err(...)
Definition coap_debug.h:102
@ COAP_LOG_DEBUG
Definition coap_debug.h:64
#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(coap_option_num_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 ...
void coap_option_filter_clear(coap_opt_filter_t *filter)
Clears filter filter.
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.
int coap_option_filter_set(coap_opt_filter_t *filter, coap_option_num_t option)
Sets the corresponding entry for number in filter.
coap_session_t * coap_new_client_session_oscore3_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, void *app_data, coap_app_data_free_callback_t callback, coap_str_const_t *ws_host)
Creates a new client session to the designated server, protecting the data using OSCORE,...
coap_session_t * coap_new_client_session_oscore_psk3_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, void *app_data, coap_app_data_free_callback_t callback, coap_str_const_t *ws_host)
Creates a new client session to the designated server, with PSK credentials protecting the data using...
coap_session_t * coap_new_client_session_oscore_pki3_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, void *app_data, coap_app_data_free_callback_t callback, coap_str_const_t *ws_host)
Creates a new client session to the designated server, with PKI credentials protecting the data using...
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:1773
void coap_delete_pdu_lkd(coap_pdu_t *pdu)
Dispose of an CoAP PDU and free off associated storage.
Definition coap_pdu.c:197
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, coap_bool_t expand_opt_abb)
Duplicate an existing PDU.
Definition coap_pdu.c:237
size_t coap_update_option(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Updates existing first option of given number in the pdu with the new data.
Definition coap_pdu.c:801
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:1635
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:1781
COAP_STATIC_INLINE void coap_pdu_release_lkd(coap_pdu_t *pdu)
coap_pdu_code_t coap_pdu_get_code(const coap_pdu_t *pdu)
Gets the PDU code associated with pdu.
Definition coap_pdu.c:1731
#define COAP_DEFAULT_PORT
Definition coap_pdu.h:39
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition coap_pdu.h:184
#define COAP_RESPONSE_CODE(N)
Definition coap_pdu.h:96
#define COAP_RESPONSE_CLASS(C)
Definition coap_pdu.h:99
coap_proto_t
CoAP protocol types Note: coap_layers_coap[] needs updating if extended.
Definition coap_pdu.h:234
coap_pdu_code_t
Set of codes available for a PDU.
Definition coap_pdu.h:248
#define COAP_MEDIATYPE_TEXT_PLAIN
Definition coap_pdu.h:134
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:417
#define COAPS_DEFAULT_PORT
Definition coap_pdu.h:40
int coap_get_data(const coap_pdu_t *pdu, size_t *len, const uint8_t **data)
Retrieves the length and data pointer of specified PDU.
Definition coap_pdu.c:966
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:104
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:974
#define COAP_INVALID_MID
Indicates an invalid message id.
Definition coap_pdu.h:187
coap_bin_const_t coap_pdu_get_token(const coap_pdu_t *pdu)
Gets the token associated with pdu.
Definition coap_pdu.c:1755
@ COAP_BOOL_FALSE
Definition coap_pdu.h:295
@ COAP_PROTO_WS
Definition coap_pdu.h:240
@ COAP_PROTO_DTLS
Definition coap_pdu.h:237
@ COAP_PROTO_UDP
Definition coap_pdu.h:236
@ COAP_PROTO_NONE
Definition coap_pdu.h:235
@ COAP_PROTO_TLS
Definition coap_pdu.h:239
@ COAP_PROTO_WSS
Definition coap_pdu.h:241
@ COAP_PROTO_TCP
Definition coap_pdu.h:238
@ COAP_PROTO_LAST
Definition coap_pdu.h:242
@ COAP_MESSAGE_NON
Definition coap_pdu.h:72
@ COAP_MESSAGE_ACK
Definition coap_pdu.h:73
@ COAP_MESSAGE_CON
Definition coap_pdu.h:71
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_proxy_t
coap_proxy_t Proxy definitions.
Definition coap_proxy.h:48
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.
#define COAP_PROXY_NEW_MASK
Space used in coap_proxy_t for new types.
Definition coap_proxy.h:92
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_proxy_entry_t * coap_proxy_fwd_add_client_session(coap_session_t *session, const char *use_ip, uint16_t use_port, coap_proxy_server_list_t *server_list)
Add a previously setup proxy-client session to use the proxy logic for forwarding subsequent dynamic ...
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_DIRECT_STRIP
Old - do not use - Act as a forward-dynamic proxy, strip out proxy options.
Definition coap_proxy.h:57
@ COAP_PROXY_BIT_STRIP
If COAP_PROXY_BIT_STRIP set, then remove any Proxy-Uri or Proxy-Scheme as the PDU is forwarded,...
Definition coap_proxy.h:71
@ COAP_PROXY_REVERSE_STRIP
Old - do not use - Act as a reverse proxy, strip out proxy options.
Definition coap_proxy.h:51
@ COAP_PROXY_FWD_STATIC
forward-static proxy.
Definition coap_proxy.h:67
@ COAP_PROXY_REV
reverse proxy.
Definition coap_proxy.h:66
@ COAP_PROXY_REVERSE
Old - do not use - Act as a reverse proxy.
Definition coap_proxy.h:50
@ COAP_PROXY_DIRECT
Old - do not use - Act as a forward-dynamic proxy.
Definition coap_proxy.h:56
@ COAP_PROXY_DYN_DEFINED
If COAP_PROXY_DYN_DEFINED set, then no new dynamic upstream servers will get automatically added.
Definition coap_proxy.h:82
@ COAP_PROXY_FORWARD_STRIP
Old - do not use - Act as a forward-static proxy, strip out proxy options.
Definition coap_proxy.h:54
@ COAP_PROXY_FWD_DYNAMIC
forward-dynamic proxy.
Definition coap_proxy.h:68
@ COAP_PROXY_FORWARD
Old - do not use - Act as a forward-static proxy.
Definition coap_proxy.h:53
@ COAP_PROXY_BIT_MCAST
If COAP_PROXY_BIT_MCAST set, then upstream servers can be multicast, else only unicast.
Definition coap_proxy.h:76
coap_session_t * coap_new_client_session_pki3_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, void *app_data, coap_app_data_free_callback_t callback, coap_str_const_t *ws_host)
Creates a new client session to the designated server, with PKI credentials along with app_data infor...
#define COAP_MAX_TRANSMIT_WAIT_TICKS(s)
coap_session_t * coap_new_client_session_psk3_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, void *app_data, coap_app_data_free_callback_t callback, coap_str_const_t *ws_host)
Creates a new client session to the designated server, with PSK credentials along with app_data infor...
int coap_session_reconnect(coap_session_t *session)
Close the current session (if not already closed) and reconnect to server (client session only).
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.
coap_session_t * coap_new_client_session3_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, void *app_data, coap_app_data_free_callback_t callback, coap_str_const_t *ws_host)
Creates a new client session to the designated server, with PSK credentials along with app_data infor...
int coap_session_set_type_client_lkd(coap_session_t *session, int report_changed)
Set the session type to client.
#define COAP_PROTO_NOT_RELIABLE(p)
void coap_session_new_token(coap_session_t *session, size_t *len, uint8_t *data)
Creates a new token for use.
const coap_address_t * coap_session_get_addr_remote(const coap_session_t *session)
Get the remote IP address and port from the session.
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:130
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:81
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:119
void coap_delete_binary(coap_binary_t *s)
Deletes the given coap_binary_t object and releases any memory allocated.
Definition coap_str.c:114
#define coap_binary_equal(binary1, binary2)
Compares the two binary data for equality.
Definition coap_str.h:222
#define coap_string_equal(string1, string2)
Compares the two strings for equality.
Definition coap_str.h:208
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
Definition coap_str.c:50
int coap_tcp_is_supported(void)
Check whether TCP is available.
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:938
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:943
coap_string_t * coap_get_uri_path(const coap_pdu_t *request)
Extract uri_path string from request PDU.
Definition coap_uri.c:1190
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:357
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:380
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:1111
#define COAP_UNUSED
Definition libcoap.h:74
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:58
Multi-purpose address abstraction.
CoAP binary data definition with const data.
Definition coap_str.h:65
size_t length
length of binary data
Definition coap_str.h:66
const uint8_t * s
read-only binary data
Definition coap_str.h:67
CoAP binary data definition.
Definition coap_str.h:57
size_t length
length of binary data
Definition coap_str.h:58
uint8_t * s
binary data
Definition coap_str.h:59
The CoAP stack's global state is stored in a coap_context_t object.
coap_queue_t * sendqueue
char * client_sni
If not NULL, SNI to use in client TLS setup.
Definition coap_dtls.h:445
char * client_sni
If not NULL, SNI to use in client TLS setup.
Definition coap_dtls.h:376
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
size_t max_size
maximum size for token, options and payload, or zero for variable size pdu
const uint8_t * body_data
Holds ptr to re-assembled data or NULL.
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:107
coap_proxy_server_t * entry
Set of servers to connect to.
Definition coap_proxy.h:102
coap_proxy_t type
The proxy type and option controlling bits (if old type used, will get mapped into new + bits.
Definition coap_proxy.h:105
unsigned int idle_timeout_secs
Proxy upstream session idle timeout (0 is no timeout).
Definition coap_proxy.h:110
size_t next_entry
Next server to use (% entry_count).
Definition coap_proxy.h:104
size_t entry_count
The number of servers in entry list.
Definition coap_proxy.h:103
coap_dtls_pki_t * dtls_pki
PKI configuration to use if not NULL.
Definition coap_proxy.h:96
coap_oscore_conf_t * oscore_conf
OSCORE configuration if not NULL.
Definition coap_proxy.h:98
coap_uri_t uri
host and port define the server, scheme method
Definition coap_proxy.h:95
coap_dtls_cpsk_t * dtls_cpsk
PSK configuration to use if not NULL.
Definition coap_proxy.h:97
Queue entry.
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_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:47
const uint8_t * s
read-only string data
Definition coap_str.h:49
size_t length
length of string
Definition coap_str.h:48
CoAP string data definition.
Definition coap_str.h:39
uint8_t * s
string data
Definition coap_str.h:41
Representation of parsed URI.
Definition coap_uri.h:70
enum coap_uri_scheme_t scheme
The parsed scheme specifier.
Definition coap_uri.h:82
uint16_t port
The port in host byte order.
Definition coap_uri.h:72
coap_str_const_t host
The host part of the URI.
Definition coap_uri.h:71