libcoap 4.3.5-develop-79e5224
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 /* Only if there is a one-to-one tracking */
576 coap_session_t *ongoing = proxy_entry->ongoing;
577
578 proxy_entry->ongoing = NULL;
580 return 0;
581 }
582
583 /* Check for outgoing match */
584 if (proxy_entry->ongoing == session) {
585 coap_session_t *ongoing;
586
587 coap_proxy_cleanup_entry(proxy_entry, failure_code);
588 ongoing = proxy_entry->ongoing;
589 coap_log_debug("* %s: proxy_entry %p released (rem count = % " PRIdS ")\n",
590 coap_session_str(ongoing),
591 (void *)proxy_entry,
592 session->context->proxy_list_count - 1);
593 if (proxy_list_count-i > 1) {
594 memmove(&proxy_list[i],
595 &proxy_list[i+1],
596 (proxy_list_count-i-1) * sizeof(proxy_list[0]));
597 }
598 session->context->proxy_list_count--;
600 return 1;
601 }
602 }
603 return 0;
604}
605
606static coap_proxy_entry_t *
607coap_proxy_get_ongoing_session(coap_session_t *session,
608 const coap_pdu_t *request,
609 coap_pdu_t *response,
610 coap_proxy_server_list_t *server_list) {
611
612 coap_address_t dst;
613 coap_proto_t proto;
614 coap_addr_info_t *info_list = NULL;
615 coap_proxy_entry_t *proxy_entry;
616 coap_context_t *context = session->context;
617 static char client_sni[256];
618 coap_proxy_server_t server_use;
619 int proxy_entry_created;
620
621 proxy_entry = coap_proxy_get_session(session, request, response, server_list,
622 &server_use, &proxy_entry_created);
623 if (!proxy_entry) {
624 /* Error response code already set */
625 return NULL;
626 }
627
628 if (!proxy_entry->ongoing) {
629 /* Need to create a new session */
630 coap_address_t *local_addr = NULL;
631
632 /* resolve destination address where data should be sent */
633 info_list = coap_resolve_address_info_lkd(&server_use.uri.host,
634 server_use.uri.port,
635 server_use.uri.port,
636 server_use.uri.port,
637 server_use.uri.port,
638 0,
639 1 << server_use.uri.scheme,
641
642 if (info_list == NULL) {
643 response->code = COAP_RESPONSE_CODE(502);
644 coap_proxy_remove_association(session, 0);
645 return NULL;
646 }
647 proto = info_list->proto;
648 memcpy(&dst, &info_list->addr, sizeof(dst));
649 coap_free_address_info(info_list);
650 if (coap_is_mcast(&dst)) {
651 coap_proxy_t proxy_type = coap_proxy_map_type(server_list->type);
652
653 if ((proxy_type & COAP_PROXY_NEW_MASK) == COAP_PROXY_FWD_DYNAMIC &&
654 (proxy_type & COAP_PROXY_BIT_MCAST) == 0) {
655 response->code = COAP_RESPONSE_CODE(501);
656 coap_proxy_remove_association(session, 0);
657 coap_log_debug("* %s: mcast proxy forwarding not enabled\n",
658 coap_session_str(session));
659 return NULL;
660 }
661 if (server_use.uri.scheme != COAP_URI_SCHEME_COAP) {
662 response->code = COAP_RESPONSE_CODE(501);
663 coap_proxy_remove_association(session, 0);
664 coap_log_debug("* %s: mcast proxy forwarding only supported for 'coap'\n",
665 coap_session_str(session));
666 return NULL;
667 }
668 }
669
670#if COAP_AF_UNIX_SUPPORT
671 coap_address_t bind_addr;
672 if (coap_is_af_unix(&dst)) {
673 char buf[COAP_UNIX_PATH_MAX];
674 coap_tick_t now;
675
676 /* Need a unique 'client' address */
677 coap_ticks(&now);
678 snprintf(buf, COAP_UNIX_PATH_MAX,
679 "/tmp/coap-pr-cl-%" PRIu64, (uint64_t)now);
680 if (!coap_address_set_unix_domain(&bind_addr, (const uint8_t *)buf,
681 strlen(buf))) {
682 fprintf(stderr, "coap_address_set_unix_domain: %s: failed\n",
683 buf);
684 remove(buf);
685 return NULL;
686 }
687 (void)remove(buf);
688 local_addr = &bind_addr;
689 }
690#endif /* COAP_AF_UNIX_SUPPORT */
691
692 snprintf(client_sni, sizeof(client_sni), "%*.*s", (int)server_use.uri.host.length,
693 (int)server_use.uri.host.length, server_use.uri.host.s);
694
695 switch (server_use.uri.scheme) {
699#if COAP_OSCORE_SUPPORT
700 if (server_use.oscore_conf) {
701 proxy_entry->ongoing =
702 coap_new_client_session_oscore3_lkd(context, local_addr, &dst,
703 proto, server_use.oscore_conf,
704 NULL, NULL, NULL);
705 } else {
706#endif /* COAP_OSCORE_SUPPORT */
707 proxy_entry->ongoing =
708 coap_new_client_session3_lkd(context, local_addr, &dst, proto,
709 NULL, NULL, NULL);
710#if COAP_OSCORE_SUPPORT
711 }
712#endif /* COAP_OSCORE_SUPPORT */
713 break;
717#if COAP_OSCORE_SUPPORT
718 if (server_use.oscore_conf) {
719 if (server_use.dtls_pki) {
720 server_use.dtls_pki->client_sni = client_sni;
721 proxy_entry->ongoing =
722 coap_new_client_session_oscore_pki3_lkd(context, local_addr, &dst,
723 proto, server_use.dtls_pki,
724 server_use.oscore_conf,
725 NULL, NULL, NULL);
726 } else if (server_use.dtls_cpsk) {
727 server_use.dtls_cpsk->client_sni = client_sni;
728 proxy_entry->ongoing =
729 coap_new_client_session_oscore_psk3_lkd(context, local_addr, &dst,
730 proto, server_use.dtls_cpsk,
731 server_use.oscore_conf,
732 NULL, NULL, NULL);
733 } else {
734 coap_log_warn("Proxy: (D)TLS not configured for secure session\n");
735 }
736 } else {
737#endif /* COAP_OSCORE_SUPPORT */
738 /* Not doing OSCORE */
739 if (server_use.dtls_pki) {
740 server_use.dtls_pki->client_sni = client_sni;
741 proxy_entry->ongoing =
742 coap_new_client_session_pki3_lkd(context, local_addr, &dst,
743 proto, server_use.dtls_pki,
744 NULL, NULL, NULL);
745 } else if (server_use.dtls_cpsk) {
746 server_use.dtls_cpsk->client_sni = client_sni;
747 proxy_entry->ongoing =
748 coap_new_client_session_psk3_lkd(context, local_addr, &dst,
749 proto, server_use.dtls_cpsk,
750 NULL, NULL, NULL);
751 } else {
752 /* Using client anonymous PKI */
753 proxy_entry->ongoing =
754 coap_new_client_session3_lkd(context, local_addr, &dst, proto,
755 NULL, NULL, NULL);
756 }
757#if COAP_OSCORE_SUPPORT
758 }
759#endif /* COAP_OSCORE_SUPPORT */
760 break;
764 default:
765 assert(0);
766 break;
767 }
768 if (proxy_entry->ongoing == NULL) {
769 response->code = COAP_RESPONSE_CODE(505);
770 coap_proxy_remove_association(session, 0);
771 return NULL;
772 }
773 if (proxy_entry_created) {
774 coap_log_debug("* %s: proxy_entry %.*s:%d %p created (tot count = % " PRIdS ")\n",
775 coap_session_str(proxy_entry->ongoing),
776 (int)proxy_entry->uri.host.length, proxy_entry->uri.host.s,
777 proxy_entry->uri.port,
778 (void *)proxy_entry,
779 session->context->proxy_list_count);
780 }
781 } else if (proxy_entry->ongoing->session_failed) {
782 if (!coap_session_reconnect(proxy_entry->ongoing)) {
783 /* Server is not yet back up */
784 return NULL;
785 }
786 }
787
788 return proxy_entry;
789}
790
791static void
792coap_proxy_release_body_data(coap_session_t *session COAP_UNUSED,
793 void *app_ptr) {
794 coap_delete_binary(app_ptr);
795}
796
797static coap_proxy_req_t *
798coap_proxy_get_req(coap_proxy_entry_t *proxy_entry, coap_proxy_cache_t *proxy_cache,
799 coap_session_t *session) {
800 coap_proxy_req_t *proxy_req;
801
802 LL_FOREACH(proxy_entry->proxy_req, proxy_req) {
803 if (proxy_req->incoming == session && proxy_req->proxy_cache == proxy_cache) {
804 return proxy_req;
805 }
806 }
807 return NULL;
808}
809
810static void
811coap_proxy_free_response_data(coap_session_t *session COAP_UNUSED, void *app_ptr) {
812 coap_delete_bin_const(app_ptr);
813}
814
815static coap_response_t
816coap_proxy_call_response_handler(coap_session_t *session, const coap_pdu_t *sent,
817 coap_pdu_t *rcvd, coap_bin_const_t *token,
818 coap_proxy_req_t *proxy_req, int replace_mid,
819 int remove_observe) {
821 coap_pdu_t *resp_pdu;
822 coap_pdu_t *fwd_pdu = NULL;
823 size_t size;
824 size_t offset;
825 size_t total;
826 const uint8_t *data;
827 coap_string_t *l_query = NULL;
828 coap_opt_filter_t opt_filter;
829
830 coap_option_filter_clear(&opt_filter);
831 if (!coap_option_check_critical(session, rcvd, &opt_filter, COAP_CRIT_PROXY)) {
832 /* Need to send back a 5.02 response */
833 coap_send_error_lkd(session, rcvd, COAP_RESPONSE_CODE(502), &opt_filter);
834 return COAP_RESPONSE_FAIL;
835 }
836
837 if (remove_observe) {
838 coap_opt_filter_t drop_options;
839
840 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
842 /* Correct the token */
843 resp_pdu = coap_pdu_duplicate_lkd(rcvd, session, token->length, token->s,
844 &drop_options, COAP_BOOL_FALSE);
845 } else {
846 /* Correct the token */
847 resp_pdu = coap_pdu_duplicate_lkd(rcvd, session, token->length, token->s,
849 }
850 if (!resp_pdu)
851 return COAP_RESPONSE_FAIL;
852
853 assert(proxy_req);
854 /* In case change in type of request over the proxy */
855 if (proxy_req->pdu)
856 resp_pdu->type = proxy_req->pdu->type;
857
858 if (COAP_PROTO_NOT_RELIABLE(session->proto))
859 resp_pdu->max_size = coap_session_max_pdu_size_lkd(session);
860
861 if (replace_mid)
862 resp_pdu->mid = sent->mid;
863
864 proxy_req->mid = resp_pdu->mid;
865
866 /* We sent early ACK, and this is an ACK, need to convert it to CON */
867 if (COAP_PROTO_NOT_RELIABLE(session->proto) && resp_pdu->type == COAP_MESSAGE_ACK) {
868 resp_pdu->type = COAP_MESSAGE_CON;
869 }
870
871 if (coap_get_data_large(rcvd, &size, &data, &offset, &total)) {
872 uint16_t media_type = 0;
873 int maxage = -1;
874 uint64_t etag = 0;
875 coap_opt_t *option;
876 coap_opt_iterator_t opt_iter;
877 coap_bin_const_t *body;
878
879 /* COAP_BLOCK_SINGLE_BODY is set, so single body should be given */
880 assert(size == total);
881
882 body = coap_new_bin_const(data, size);
883 if (!body) {
884 coap_log_debug("coap_proxy_call_response_handler: copy data error\n");
885 goto failed;
886 }
887 option = coap_check_option(rcvd, COAP_OPTION_CONTENT_FORMAT, &opt_iter);
888 if (option) {
889 media_type = coap_decode_var_bytes(coap_opt_value(option),
890 coap_opt_length(option));
891 }
892 option = coap_check_option(rcvd, COAP_OPTION_MAXAGE, &opt_iter);
893 if (option) {
894 maxage = coap_decode_var_bytes(coap_opt_value(option),
895 coap_opt_length(option));
896 }
897 option = coap_check_option(rcvd, COAP_OPTION_ETAG, &opt_iter);
898 if (option) {
900 coap_opt_length(option));
901 }
902 if (sent)
903 l_query = coap_get_query(sent);
904 if (!coap_add_data_large_response_lkd(proxy_req->resource, session, sent,
905 resp_pdu,
906 l_query,
907 media_type, maxage, etag, body->length,
908 body->s,
909 coap_proxy_free_response_data,
910 body)) {
911 coap_log_debug("coap_proxy_call_response_handler: add data error\n");
912 goto failed;
913 }
914 }
916 session->context->proxy_response_cb(session,
917 sent,
918 resp_pdu,
919 proxy_req->cache_key),
920 /* context is being freed off */
921 goto failed);
922 if (fwd_pdu) {
923 ret = COAP_RESPONSE_OK;
924 if (coap_send_lkd(session, fwd_pdu) == COAP_INVALID_MID) {
925 ret = COAP_RESPONSE_FAIL;
926 }
927 if (fwd_pdu != resp_pdu) {
928 /* Application created a new PDU */
929 coap_delete_pdu_lkd(resp_pdu);
930 }
931 } else {
932failed:
933 ret = COAP_RESPONSE_FAIL;
934 coap_delete_pdu_lkd(resp_pdu);
935 }
936 coap_delete_string(l_query);
937 return ret;
938}
939
940int COAP_API
942 const coap_pdu_t *request,
943 coap_pdu_t *response,
944 coap_resource_t *resource,
945 coap_cache_key_t *cache_key,
946 coap_proxy_server_list_t *server_list) {
947 int ret;
948
949 coap_lock_lock(return 0);
950 ret = coap_proxy_forward_request_lkd(session,
951 request,
952 response,
953 resource,
954 cache_key,
955 server_list);
957 return ret;
958}
959
960/* https://rfc-editor.org/rfc/rfc7641#section-3.6 */
961static const uint16_t coap_proxy_ignore_options[] = { COAP_OPTION_ETAG,
966 };
967
968int
969coap_proxy_forward_request_lkd(coap_session_t *session,
970 const coap_pdu_t *request,
971 coap_pdu_t *response,
972 coap_resource_t *resource,
973 coap_cache_key_t *cache_key,
974 coap_proxy_server_list_t *server_list) {
975 coap_proxy_entry_t *proxy_entry;
976 size_t size;
977 size_t offset;
978 size_t total;
979 coap_binary_t *body_data = NULL;
980 const uint8_t *data;
981 coap_pdu_t *pdu = NULL;
982 coap_bin_const_t r_token = coap_pdu_get_token(request);
983 uint8_t token[8];
984 size_t token_len;
985 coap_proxy_req_t *proxy_req = NULL;
986 coap_optlist_t *optlist = NULL;
987 coap_opt_t *option;
988 coap_opt_iterator_t opt_iter;
989 coap_uri_t uri;
990 coap_opt_t *obs_opt = coap_check_option(request,
992 &opt_iter);
993 coap_proxy_cache_t *proxy_cache = NULL;
994 coap_proxy_t proxy_type;
995 coap_tick_t now;
996 uint32_t new_maxage;
997 uint8_t buf[4];
998
999 coap_ticks(&now);
1000
1001 /* Set up ongoing session (if not already done) */
1002 proxy_entry = coap_proxy_get_ongoing_session(session, request, response,
1003 server_list);
1004 if (!proxy_entry) {
1005 /* Error response code already set */
1006 return 0;
1007 }
1008 coap_proxy_log_entry(session, request, NULL, "req");
1009
1010 /* Is this a observe cached request? */
1011 if (obs_opt && session->context->proxy_response_cb) {
1012 coap_cache_key_t *cache_key_l;
1013
1014 cache_key_l = coap_cache_derive_key_w_ignore(session, request,
1016 coap_proxy_ignore_options,
1017 sizeof(coap_proxy_ignore_options)/sizeof(coap_proxy_ignore_options[0]));
1018 if (!cache_key_l) {
1019 response->code = COAP_RESPONSE_CODE(505);
1020 return 0;
1021 }
1022 PROXY_CACHE_FIND(proxy_entry->rsp_cache, cache_key_l, proxy_cache);
1023 coap_delete_cache_key(cache_key_l);
1024 }
1025
1026 if (proxy_cache) {
1027 proxy_req = coap_proxy_get_req(proxy_entry, proxy_cache, session);
1028 if (proxy_req) {
1029 if (obs_opt) {
1030 int observe_action;
1031
1032 observe_action = coap_decode_var_bytes(coap_opt_value(obs_opt),
1033 coap_opt_length(obs_opt));
1034
1035 if (observe_action == COAP_OBSERVE_CANCEL) {
1036 if (proxy_req->doing_observe) {
1037 assert(proxy_req->incoming->ref_proxy_subs);
1038 proxy_req->incoming->ref_proxy_subs--;
1039 proxy_req->doing_observe = 0;
1040 }
1041 if (proxy_entry->proxy_req && !proxy_entry->proxy_req->next &&
1042 proxy_req->token_used) {
1043 coap_binary_t tmp;
1044
1045 coap_log_debug("coap_proxy_forward_request: Using coap_cancel_observe() to do Proxy OBSERVE cancellation\n");
1046 /* Unfortunately need to change the ptr type to be r/w */
1047 memcpy(&tmp.s, &proxy_req->token_used->s, sizeof(tmp.s));
1048 tmp.length = proxy_req->token_used->length;
1049 coap_cancel_observe_lkd(proxy_entry->ongoing, &tmp, COAP_MESSAGE_CON);
1050 /* Let the upstream cancellation be the response */
1051 return 1;
1052 } else {
1053 obs_opt = 0;
1054 goto return_cached_info;
1055 }
1056 } else if (observe_action == COAP_OBSERVE_ESTABLISH) {
1057 /* Client must be re-registering */
1058 if (proxy_cache && (proxy_cache->expire + COAP_TICKS_PER_SECOND) < now) {
1059 /* Need to get an updated rsp_pdu */
1060 coap_proxy_del_req(proxy_entry, proxy_req);
1061 proxy_req = NULL;
1062 proxy_cache = NULL;
1063 } else {
1064 goto return_cached_info;
1065 }
1066 }
1067 } else {
1068 if (proxy_cache && (proxy_cache->expire + COAP_TICKS_PER_SECOND) < now) {
1069 /* Need to get an updated rsp_pdu */
1070 coap_proxy_del_req(proxy_entry, proxy_req);
1071 proxy_req = NULL;
1072 proxy_cache = NULL;
1073 } else {
1074 goto return_cached_info;
1075 }
1076 }
1077 }
1078 }
1079
1080 if (!proxy_req) {
1081 proxy_req = coap_malloc_type(COAP_STRING, sizeof(coap_proxy_req_t));
1082 if (proxy_req == NULL) {
1083 goto failed;
1084 }
1085 memset(proxy_req, 0, sizeof(coap_proxy_req_t));
1086 LL_PREPEND(proxy_entry->proxy_req, proxy_req);
1087
1088 proxy_req->pdu = coap_const_pdu_reference_lkd(request);
1089 proxy_req->resource = resource;
1090 proxy_req->incoming = session;
1091 proxy_req->cache_key = cache_key;
1092 proxy_req->proxy_cache = proxy_cache;
1093
1094 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used, "add");
1095 }
1096
1097 if (proxy_cache) {
1098 coap_bin_const_t j_token;
1099
1100 if (obs_opt)
1101 proxy_cache->ref++;
1102 coap_delete_bin_const(proxy_req->token_used);
1103 j_token = coap_pdu_get_token(proxy_cache->rsp_pdu);
1104 proxy_req->token_used = coap_new_bin_const(j_token.s, j_token.length);
1105 if (proxy_req->token_used == NULL) {
1106 goto failed;
1107 }
1108 goto return_cached_info;
1109 }
1110
1111 /* Get a new token for ongoing session */
1112 coap_session_new_token(proxy_entry->ongoing, &token_len, token);
1113 coap_delete_bin_const(proxy_req->token_used);
1114 proxy_req->token_used = coap_new_bin_const(token, token_len);
1115 if (proxy_req->token_used == NULL) {
1116 goto failed;
1117 }
1118 coap_pdu_release_lkd(proxy_req->pdu);
1119 proxy_req->pdu = coap_const_pdu_reference_lkd(request);
1120
1121 /* Need to create the ongoing request pdu entry */
1122 proxy_type = coap_proxy_map_type(server_list->type);
1123 if (proxy_type & COAP_PROXY_BIT_STRIP) {
1124 /*
1125 * Need to replace Proxy-Uri with Uri-Host (and Uri-Port)
1126 * and strip out Proxy-Scheme.
1127 */
1128
1129 /*
1130 * Build up the ongoing PDU that we are going to send
1131 */
1132 pdu = coap_pdu_init(request->type, request->code,
1133 coap_new_message_id_lkd(proxy_entry->ongoing),
1134 coap_session_max_pdu_size_lkd(proxy_entry->ongoing));
1135 if (!pdu) {
1136 goto failed;
1137 }
1138
1139 if (coap_is_mcast(&proxy_entry->ongoing->addr_info.remote)) {
1140 pdu->type = COAP_MESSAGE_NON;
1141 }
1142
1143 if (!coap_add_token(pdu, token_len, token)) {
1144 goto failed;
1145 }
1146
1147 /* Copy the remaining options across */
1148 coap_option_iterator_init(request, &opt_iter, COAP_OPT_ALL);
1149 while ((option = coap_option_next(&opt_iter))) {
1150 switch (opt_iter.number) {
1153 coap_opt_length(option),
1154 &uri) < 0) {
1155 /* Need to return a 5.05 RFC7252 Section 5.7.2 */
1156 coap_log_warn("Proxy URI not decodable\n");
1157 goto failed;
1158 }
1159 if (!coap_uri_into_optlist(&uri, NULL, &optlist, 0)) {
1160 coap_log_err("Failed to create options for URI\n");
1161 goto failed;
1162 }
1163 break;
1165 break;
1166 case COAP_OPTION_BLOCK1:
1167 case COAP_OPTION_BLOCK2:
1170 /* These are not passed on */
1171 break;
1174 break;
1175 default:
1176 coap_insert_optlist(&optlist,
1177 coap_new_optlist(opt_iter.number,
1178 coap_opt_length(option),
1179 coap_opt_value(option)));
1180 break;
1181 }
1182 }
1183
1184 /* Update pdu with options */
1185 coap_add_optlist_pdu(pdu, &optlist);
1186 coap_delete_optlist(optlist);
1187 optlist = NULL;
1188 } else {
1189 /*
1190 * Duplicate request PDU for onward transmission (with new token).
1191 */
1192 pdu = coap_pdu_duplicate_lkd(request, proxy_entry->ongoing, token_len,
1193 token, NULL, COAP_BOOL_FALSE);
1194 if (!pdu) {
1195 coap_log_debug("proxy: PDU generation error\n");
1196 goto failed;
1197 }
1198 if (COAP_PROTO_NOT_RELIABLE(session->proto))
1199 pdu->max_size = coap_session_max_pdu_size_lkd(proxy_entry->ongoing);
1200 }
1201
1202 if (coap_get_data_large(request, &size, &data, &offset, &total)) {
1203 /* COAP_BLOCK_SINGLE_BODY is set, so single body should be given */
1204 assert(size == total);
1205 /*
1206 * Need to take a copy of the data as request PDU may go away before
1207 * all data is transmitted.
1208 */
1209 body_data = coap_new_binary(total);
1210 if (!body_data) {
1211 coap_log_debug("proxy: body build memory error\n");
1212 goto failed;
1213 }
1214 memcpy(body_data->s, data, size);
1215 if (!coap_add_data_large_request_lkd(proxy_entry->ongoing, pdu, total, data,
1216 coap_proxy_release_body_data, body_data)) {
1217 coap_log_debug("proxy: add data error\n");
1218 goto failed;
1219 }
1220 }
1221
1222 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used, "fwd");
1223 proxy_entry->mid = coap_send_lkd(proxy_entry->ongoing, pdu);
1224 if (proxy_entry->mid == COAP_INVALID_MID) {
1225 pdu = NULL;
1226 coap_log_debug("proxy: upstream PDU send error\n");
1227 goto failed;
1228 }
1229 if (request->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(proxy_entry->ongoing->proto)) {
1230 proxy_entry->req_start_ticks = now;
1231 }
1232
1233 /*
1234 * Do not update the response code (hence empty ACK) as will be sending
1235 * separate response when response comes back from upstream server
1236 */
1237
1238 return 1;
1239
1240failed:
1241 response->code = COAP_RESPONSE_CODE(500);
1242 coap_delete_optlist(optlist);
1244 return 0;
1245
1246return_cached_info:
1247 if (obs_opt && !proxy_req->doing_observe) {
1248 int observe_action;
1249
1250 observe_action = coap_decode_var_bytes(coap_opt_value(obs_opt),
1251 coap_opt_length(obs_opt));
1252
1253 if (observe_action == COAP_OBSERVE_ESTABLISH) {
1254 proxy_req->doing_observe = 1;
1255 proxy_req->incoming->ref_proxy_subs++;
1256 }
1257 }
1258 coap_log_debug("* %s: Using Proxy Cache (Active %d)\n",
1259 coap_session_str(session),
1260 proxy_cache->ref);
1261 coap_proxy_log_entry(session, request, &proxy_cache->rsp_pdu->actual_token, "rspc");
1262 if (now < proxy_cache->expire) {
1263 new_maxage = (uint32_t)((proxy_cache->expire - now) / COAP_TICKS_PER_SECOND);
1264 if (new_maxage == 0)
1265 new_maxage = 1;
1266 } else {
1267 new_maxage = 1;
1268 }
1269 coap_update_option(proxy_cache->rsp_pdu,
1271 coap_encode_var_safe(buf, sizeof(buf), new_maxage), buf);
1272 coap_proxy_call_response_handler(session, request, proxy_cache->rsp_pdu,
1273 &r_token, proxy_req, 1, obs_opt ? 0 : 1);
1274 if (!obs_opt)
1275 coap_proxy_del_req(proxy_entry, proxy_req);
1276 return 1;
1277}
1278
1279coap_proxy_req_t *
1280coap_proxy_map_outgoing_request(coap_session_t *ongoing,
1281 const coap_pdu_t *received,
1282 coap_proxy_entry_t **u_proxy_entry) {
1283 coap_proxy_entry_t *proxy_list = ongoing->context->proxy_list;
1284 size_t proxy_list_count = ongoing->context->proxy_list_count;
1285 size_t i;
1286 coap_bin_const_t rcv_token = coap_pdu_get_token(received);
1287 coap_proxy_entry_t *proxy_entry = NULL;
1288 coap_proxy_req_t *proxy_req;
1289
1290 for (i = 0; i < proxy_list_count; i++) {
1291 proxy_entry = &proxy_list[i];
1292 if (proxy_entry->ongoing == ongoing) {
1293 LL_FOREACH(proxy_entry->proxy_req, proxy_req) {
1294 if (coap_binary_equal(&rcv_token, proxy_req->token_used)) {
1295 coap_ticks(&proxy_entry->last_used);
1296 if (u_proxy_entry)
1297 *u_proxy_entry = proxy_entry;
1298 return proxy_req;
1299 }
1300 }
1301 }
1302 }
1304 char scratch[24];
1305 size_t size;
1306
1307 scratch[0] = '\000';
1308 for (i = 0; i < rcv_token.length; i++) {
1309 size = strlen(scratch);
1310 snprintf(&scratch[size], sizeof(scratch)-size,
1311 "%02x", rcv_token.s[i]);
1312 }
1313 coap_log_debug(" proxy token to match {%s}\n", scratch);
1314 for (i = 0; i < proxy_list_count; i++) {
1315 proxy_entry = &proxy_list[i];
1316 LL_FOREACH(proxy_entry->proxy_req, proxy_req) {
1317 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used, "miss");
1318 }
1319 }
1320 }
1321 return NULL;
1322}
1323
1326 const coap_pdu_t *received,
1327 coap_cache_key_t **cache_key) {
1328 int ret;
1329
1330 coap_lock_lock(return 0);
1331 ret = coap_proxy_forward_response_lkd(session,
1332 received,
1333 cache_key);
1335 return ret;
1336}
1337
1339coap_proxy_forward_response_lkd(coap_session_t *session,
1340 const coap_pdu_t *received,
1341 coap_cache_key_t **cache_key) {
1342 coap_pdu_t *pdu = NULL;
1343 coap_session_t *incoming = NULL;
1344 size_t size;
1345 const uint8_t *data;
1346 coap_optlist_t *optlist = NULL;
1347 coap_opt_t *option;
1348 coap_opt_iterator_t opt_iter;
1349 size_t offset;
1350 size_t total;
1351 coap_proxy_entry_t *proxy_entry = NULL;
1352 uint16_t media_type = COAP_MEDIATYPE_TEXT_PLAIN;
1353 int maxage = -1;
1354 uint64_t etag = 0;
1355 coap_pdu_code_t rcv_code = coap_pdu_get_code(received);
1356 coap_bin_const_t req_token;
1357 coap_binary_t *body_data = NULL;
1358 coap_pdu_t *req_pdu;
1359 coap_resource_t *resource;
1360 coap_proxy_req_t *proxy_req = NULL;
1361
1362 proxy_req = coap_proxy_map_outgoing_request(session, received, &proxy_entry);
1363 if (!proxy_req || proxy_req->incoming->server_list) {
1364 coap_log_warn("Unknown proxy ongoing session response received - ignored\n");
1365 return COAP_RESPONSE_OK;
1366 }
1367
1368 proxy_entry->req_start_ticks = 0;
1369 req_pdu = proxy_req->pdu;
1370 req_token = coap_pdu_get_token(req_pdu);
1371 resource = proxy_req->resource;
1372 incoming = proxy_req->incoming;
1373
1374 coap_log_debug("** process upstream incoming %d.%02d response:\n",
1375 COAP_RESPONSE_CLASS(rcv_code), rcv_code & 0x1F);
1376
1377 if (coap_get_data_large(received, &size, &data, &offset, &total)) {
1378 /* COAP_BLOCK_SINGLE_BODY is set, so single body should be given */
1379 assert(size == total);
1380 body_data = coap_new_binary(total);
1381 if (!body_data) {
1382 coap_log_debug("body build memory error\n");
1383 goto remove_match;
1384 }
1385 memcpy(body_data->s, data, size);
1386 data = body_data->s;
1387 }
1388
1389 /*
1390 * Build up the ongoing PDU that we are going to send to proxy originator
1391 * as separate response
1392 */
1393 pdu = coap_pdu_init(req_pdu->type, rcv_code,
1394 coap_new_message_id_lkd(incoming),
1396 if (!pdu) {
1397 coap_log_debug("Failed to create ongoing proxy response PDU\n");
1398 goto remove_match;
1399 }
1400
1401 if (!coap_add_token(pdu, req_token.length, req_token.s)) {
1402 coap_log_debug("cannot add token to ongoing proxy response PDU\n");
1403 }
1404
1405 /*
1406 * Copy the options across, skipping those needed for
1407 * coap_add_data_response_large()
1408 */
1409 coap_option_iterator_init(received, &opt_iter, COAP_OPT_ALL);
1410 while ((option = coap_option_next(&opt_iter))) {
1411 switch (opt_iter.number) {
1413 media_type = coap_decode_var_bytes(coap_opt_value(option),
1414 coap_opt_length(option));
1415 break;
1416 case COAP_OPTION_MAXAGE:
1417 maxage = coap_decode_var_bytes(coap_opt_value(option),
1418 coap_opt_length(option));
1419 break;
1420 case COAP_OPTION_ETAG:
1422 coap_opt_length(option));
1423 break;
1424 case COAP_OPTION_BLOCK2:
1426 case COAP_OPTION_SIZE2:
1427 break;
1428 default:
1429 coap_insert_optlist(&optlist,
1430 coap_new_optlist(opt_iter.number,
1431 coap_opt_length(option),
1432 coap_opt_value(option)));
1433 break;
1434 }
1435 }
1436 coap_add_optlist_pdu(pdu, &optlist);
1437 coap_delete_optlist(optlist);
1438
1439 if (size > 0) {
1440 coap_string_t *l_query = coap_get_query(req_pdu);
1441
1442 coap_add_data_large_response_lkd(resource, incoming, req_pdu, pdu,
1443 l_query,
1444 media_type, maxage, etag, size, data,
1445 coap_proxy_release_body_data,
1446 body_data);
1447 body_data = NULL;
1448 coap_delete_string(l_query);
1449 }
1450
1451 if (cache_key)
1452 *cache_key = proxy_req->cache_key;
1453
1454 coap_send_lkd(incoming, pdu);
1455
1456remove_match:
1457 option = coap_check_option(received, COAP_OPTION_OBSERVE, &opt_iter);
1458 /* Need to remove matching token entry (apart from an Observe response) */
1459 if (option == NULL) {
1460 if (proxy_entry->proxy_req) {
1461 /* Do not delete cache key here - caller's responsibility */
1462 proxy_req->cache_key = NULL;
1463 coap_proxy_del_req(proxy_entry, proxy_req);
1464 }
1465 } else if (!proxy_req->doing_observe) {
1466 option = coap_check_option(proxy_req->pdu, COAP_OPTION_OBSERVE, &opt_iter);
1467 if (option &&
1470 proxy_req->doing_observe = 1;
1471 proxy_req->incoming->ref_proxy_subs++;
1472 }
1473 }
1474 coap_delete_binary(body_data);
1475 return COAP_RESPONSE_OK;
1476}
1477
1478void
1479coap_proxy_process_incoming(coap_session_t *session,
1480 coap_pdu_t *rcvd,
1481 void *body_data, coap_proxy_req_t *proxy_req,
1482 coap_proxy_entry_t *proxy_entry) {
1483 coap_opt_t *obs_opt;
1484 coap_opt_t *option;
1485 coap_opt_iterator_t opt_iter;
1487 coap_bin_const_t token;
1488 int can_cache = 0;
1489
1490 if (COAP_RESPONSE_CLASS(rcvd->code) == 2) {
1491 switch ((int)rcvd->code) {
1492 case COAP_RESPONSE_CODE(201):
1493 case COAP_RESPONSE_CODE(202):
1494 case COAP_RESPONSE_CODE(203):
1495 case COAP_RESPONSE_CODE(204):
1496 case COAP_RESPONSE_CODE(205):
1497 can_cache = 1;
1498 break;
1499 /* 2.13 should be handled by block transfer logic */
1500 case COAP_RESPONSE_CODE(213):
1501 default:
1502 break;
1503 }
1504 }
1505 obs_opt = coap_check_option(rcvd, COAP_OPTION_OBSERVE, &opt_iter);
1506
1507 /* See if we are doing proxy caching */
1508 if (can_cache && obs_opt) {
1509 coap_proxy_cache_t *proxy_cache;
1510 coap_cache_key_t *cache_key_l;
1511 coap_tick_t now;
1512 uint64_t expire;
1513
1514 /* Need to cache the response */
1515 if (proxy_req->proxy_cache) {
1516 coap_delete_pdu_lkd(proxy_req->proxy_cache->rsp_pdu);
1517 proxy_cache = proxy_req->proxy_cache;
1518 } else {
1519 proxy_cache = coap_malloc_type(COAP_STRING, sizeof(coap_proxy_cache_t));
1520 if (proxy_cache == NULL) {
1521 goto cache_fail;
1522 }
1523 memset(proxy_cache, 0, sizeof(coap_proxy_cache_t));
1524 cache_key_l = coap_cache_derive_key_w_ignore(session, proxy_req->pdu,
1526 coap_proxy_ignore_options,
1527 sizeof(coap_proxy_ignore_options)/sizeof(coap_proxy_ignore_options[0]));
1528 if (!cache_key_l) {
1529 coap_free_type(COAP_STRING, proxy_cache);
1530 goto cache_fail;
1531 }
1532 memcpy(&proxy_cache->cache_req, cache_key_l,
1533 sizeof(proxy_cache->cache_req));
1534 coap_delete_cache_key(cache_key_l);
1535
1536 proxy_cache->req_pdu = coap_pdu_reference_lkd(proxy_req->pdu);
1537 proxy_req->proxy_cache = proxy_cache;
1538
1539 PROXY_CACHE_ADD(proxy_entry->rsp_cache, proxy_cache);
1540 proxy_cache->ref++;
1541 }
1542 if (rcvd->body_data) {
1543 /* More data than that held in the PDU */
1544 const uint8_t *data;
1545 size_t data_len;
1546
1547 proxy_cache->rsp_pdu = coap_pdu_duplicate_lkd(rcvd,
1548 session,
1549 rcvd->actual_token.length,
1550 rcvd->actual_token.s,
1552 if (proxy_cache->rsp_pdu) {
1553 if (coap_get_data(rcvd, &data_len, &data)) {
1554 coap_binary_t *copy;
1555
1556 copy = coap_new_binary(data_len);
1557 if (copy) {
1558 memcpy(copy->s, data, data_len);
1559 proxy_cache->rsp_pdu->data_free = copy;
1560 proxy_cache->rsp_pdu->body_data = copy->s;
1561 proxy_cache->rsp_pdu->body_length = copy->length;
1562 proxy_cache->rsp_pdu->body_total = copy->length;
1563 } else {
1564 proxy_cache->rsp_pdu->body_data = NULL;
1565 proxy_cache->rsp_pdu->body_length = 0;
1566 proxy_cache->rsp_pdu->body_total = 0;
1567 }
1568 }
1569 }
1570 } else {
1571 /* The simple case */
1572 proxy_cache->rsp_pdu = coap_pdu_reference_lkd(rcvd);
1573 }
1574 option = coap_check_option(rcvd, COAP_OPTION_ETAG, &opt_iter);
1575 if (option) {
1576 proxy_cache->etag = coap_decode_var_bytes8(coap_opt_value(option),
1577 coap_opt_length(option));
1578 } else {
1579 proxy_cache->etag = 0;
1580 }
1581 coap_ticks(&now);
1582 option = coap_check_option(rcvd, COAP_OPTION_MAXAGE, &opt_iter);
1583 if (option) {
1584 expire = coap_decode_var_bytes(coap_opt_value(option),
1585 coap_opt_length(option));
1586 } else {
1587 /* Default is 60 seconds */
1588 expire = 60;
1589 }
1590 proxy_cache->expire = now + expire * COAP_TICKS_PER_SECOND;
1591
1592 /* Update all the cache listeners */
1593 LL_FOREACH(proxy_entry->proxy_req, proxy_req) {
1594 if (proxy_req->proxy_cache == proxy_cache) {
1595 if (!proxy_req->doing_observe) {
1596 coap_opt_t *req_obs;
1597
1598 req_obs = coap_check_option(proxy_req->pdu, COAP_OPTION_OBSERVE, &opt_iter);
1599 if (req_obs &&
1602 proxy_req->doing_observe = 1;
1603 proxy_req->incoming->ref_proxy_subs++;
1604 }
1605 }
1606 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used, "rsp");
1607 token = coap_pdu_get_token(proxy_req->pdu);
1608 if (coap_proxy_call_response_handler(proxy_req->incoming, proxy_req->pdu,
1609 rcvd, &token,
1610 proxy_req, 0, 0) == COAP_RESPONSE_OK) {
1611 /* At least one success */
1612 ret = COAP_RESPONSE_OK;
1613 }
1614 }
1615 }
1616 goto finish;
1617 }
1618cache_fail:
1619 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used, "rspn");
1620 token = coap_pdu_get_token(proxy_req->pdu);
1621 ret = coap_proxy_call_response_handler(proxy_req->incoming, proxy_req->pdu,
1622 rcvd, &token, proxy_req, 0, 0);
1623 if (!obs_opt)
1624 coap_proxy_del_req(proxy_entry, proxy_req);
1625
1626finish:
1627 if (ret == COAP_RESPONSE_FAIL) {
1628 coap_send_rst_lkd(session, rcvd);
1630 } else {
1631 coap_send_ack_lkd(session, rcvd);
1633 }
1634 coap_free_type(COAP_STRING, body_data);
1635}
1636
1637/*
1638 */
1640coap_proxy_local_write(coap_session_t *session, coap_pdu_t *pdu) {
1641 coap_pdu_t *response = NULL;
1642 coap_resource_t *resource;
1644
1645 resource = session->context->unknown_resource ?
1646 session->context->unknown_resource :
1647 session->context->proxy_uri_resource;
1648 if (!resource) {
1649 coap_log_err("coap_proxy_local_write: Unknown or Proxy resource not defined\n");
1650 goto fail;
1651 }
1652
1653 response = coap_pdu_init(pdu->type == COAP_MESSAGE_CON ?
1655 0, pdu->mid, coap_session_max_pdu_size_lkd(session));
1656 if (!response) {
1657 coap_log_err("coap_proxy_local_write: Could not create response PDU\n");
1658 goto fail;
1659 }
1660 response->session = session;
1661
1662 if (!coap_add_token(response, pdu->actual_token.length,
1663 pdu->actual_token.s)) {
1664 goto fail;
1665 }
1666
1667 coap_log_debug("* %s: internal: sent %4" PRIdS " bytes\n",
1668 coap_session_str(session),
1669 pdu->used_size + coap_pdu_encode_header(pdu, session->proto));
1671
1672 mid = pdu->mid;
1673 if (!coap_proxy_forward_request_lkd(session, pdu, response, resource,
1674 NULL, session->server_list)) {
1675 coap_log_debug("coap_proxy_local_write: Failed to forward PDU\n");
1676 mid = COAP_INVALID_MID;
1677 }
1678fail:
1679 coap_delete_pdu_lkd(response);
1681 return mid;
1682}
1683
1686 coap_proxy_server_list_t *server_list) {
1687 coap_session_t *session;
1688
1689 coap_lock_lock(return NULL);
1690 session = coap_new_client_session_proxy_lkd(ctx, server_list);
1692 return session;
1693}
1694
1696coap_new_client_session_proxy_lkd(coap_context_t *ctx,
1697 coap_proxy_server_list_t *server_list) {
1698 coap_session_t *session;
1699 coap_addr_info_t *info_list = NULL;
1700 coap_str_const_t remote;
1701
1703
1704#if COAP_IPV6_SUPPORT
1705 remote.s = (const uint8_t *)"::1";
1706#elif COAP_IPV4_SUPPORT
1707 remote.s = (const uint8_t *)"127.0.0.1";
1708#else /* !COAP_IPV6_SUPPORT && ! COAP_IPV4_SUPPORT */
1709 coap_log_warn("coap_new_client_session_proxy: No IPv4 or IPv6 support\n");
1710 return NULL;
1711#endif /* !COAP_IPV6_SUPPORT && ! COAP_IPV4_SUPPORT */
1712 remote.length = strlen((const char *)remote.s);
1713 /* resolve internal remote address where proxy session is 'connecting' to */
1714 info_list = coap_resolve_address_info_lkd(&remote, 0, 0, 0, 0,
1715 0,
1718 if (!info_list) {
1719 coap_log_warn("coap_new_client_session_proxy: Unable to resolve IP address\n");
1720 return NULL;
1721 }
1722
1723 session = coap_new_client_session3_lkd(ctx, NULL, &info_list->addr, COAP_PROTO_UDP,
1724 NULL, NULL, NULL);
1725
1726 if (session) {
1727 session->server_list = server_list;
1728 }
1729 coap_free_address_info(info_list);
1730 return session;
1731}
1732
1734coap_proxy_fwd_add_client_session(coap_session_t *session, const char *use_ip,
1735 uint16_t use_port,
1736 coap_proxy_server_list_t *server_list) {
1737 coap_proxy_entry_t *ret;
1738
1739 coap_lock_lock(return 0);
1740 ret = coap_proxy_fwd_add_client_session_lkd(session, use_ip, use_port, server_list);
1742 return ret;
1743}
1744
1746coap_proxy_fwd_add_client_session_lkd(coap_session_t *session, const char *use_ip,
1747 uint16_t use_port,
1748 coap_proxy_server_list_t *server_list) {
1749#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
1750 char addr[INET6_ADDRSTRLEN];
1751#else /* WITH_LWIP || WITH_CONTIKI */
1752 char addr[40];
1753#endif /* WITH_LWIP || WITH_CONTIKI */
1754 coap_proxy_entry_t *proxy_entry;
1755 coap_uri_t uri;
1756
1757 if (!use_ip && !coap_print_ip_addr(&session->addr_info.remote, addr, sizeof(addr))) {
1758 return NULL;
1759 }
1760 if (!use_ip)
1761 use_ip = addr;
1762
1763 memset(&uri, 0, sizeof(uri));
1764 uri.port = use_port;
1765 switch (session->proto) {
1766 case COAP_PROTO_UDP:
1768 if (uri.port == 0)
1769 uri.port = 5683;
1770 break;
1771 case COAP_PROTO_DTLS:
1773 if (uri.port == 0)
1774 uri.port = 5684;
1775 break;
1776 case COAP_PROTO_TCP:
1778 if (uri.port == 0)
1779 uri.port = 5683;
1780 break;
1781 case COAP_PROTO_TLS:
1783 if (uri.port == 0)
1784 uri.port = 5684;
1785 break;
1786 case COAP_PROTO_WS:
1788 if (uri.port == 0)
1789 uri.port = 80;
1790 break;
1791 case COAP_PROTO_WSS:
1793 if (uri.port == 0)
1794 uri.port = 443;
1795 break;
1796 case COAP_PROTO_LAST:
1797 case COAP_PROTO_NONE:
1798 default:
1799 return NULL;
1800 }
1801 if (memcmp(use_ip, "::ffff:", 7) == 0) {
1802 /* IPv4 in IPv6 format */
1803 uri.host.s = (const uint8_t *)&use_ip[7];
1804 uri.host.length = strlen(&use_ip[7]);
1805 } else {
1806 uri.host.s = (const uint8_t *)use_ip;
1807 uri.host.length = strlen(use_ip);
1808 }
1809 if (!uri.host.length)
1810 return NULL;
1811
1812 proxy_entry = coap_proxy_get_add_list_entry(NULL, session, uri, server_list, 1);
1813 if (proxy_entry) {
1814 coap_log_debug("* %s: proxy_entry %.*s:%d %p created (tot count = % " PRIdS ")\n",
1815 coap_session_str(proxy_entry->ongoing),
1816 (int)proxy_entry->uri.host.length, proxy_entry->uri.host.s,
1817 proxy_entry->uri.port,
1818 (void *)proxy_entry,
1819 session->context->proxy_list_count);
1820 proxy_entry->idle_timeout_ticks = 0;
1821 proxy_entry->req_start_ticks = 0;
1822 proxy_entry->track_client_session = 0;
1824 return proxy_entry;
1825 }
1826 return NULL;
1827}
1828
1829void
1830coap_delete_proxy_subscriber(coap_session_t *session, coap_bin_const_t *token,
1831 coap_mid_t mid, coap_proxy_subs_delete_t type) {
1832 /* Need to check is there is a proxy subscription active and delete it */
1833 coap_context_t *context = session->context;
1834 size_t i;
1835
1836 for (i = 0; i < context->proxy_list_count; i++) {
1837 coap_proxy_entry_t *proxy_entry = &context->proxy_list[i];
1838 coap_proxy_req_t *proxy_req, *treq;
1839
1840 LL_FOREACH_SAFE(proxy_entry->proxy_req, proxy_req, treq) {
1841 if (proxy_req->incoming == session) {
1842 coap_bin_const_t req_token;
1843 int match = 0;
1844
1845 req_token = coap_pdu_get_token(proxy_req->pdu);
1846 switch (type) {
1847 case COAP_PROXY_SUBS_ALL:
1848 match = 1;
1849 break;
1850 case COAP_PROXY_SUBS_TOKEN:
1851 if (token && coap_binary_equal(token, &req_token))
1852 match = 1;
1853 break;
1854 case COAP_PROXY_SUBS_MID:
1855 if (proxy_req->mid == mid)
1856 match = 1;
1857 break;
1858 default:
1859 break;
1860 }
1861
1862 if (match && proxy_entry->proxy_req && ! proxy_entry->proxy_req->next &&
1863 proxy_req->token_used) {
1864 coap_binary_t tmp;
1865
1866 coap_log_debug("coap_delete_proxy_subscriber: Using coap_cancel_observe() to do Proxy OBSERVE cancellation\n");
1867 /* Unfortunately need to change the ptr type to be r/w */
1868 memcpy(&tmp.s, &proxy_req->token_used->s, sizeof(tmp.s));
1869 tmp.length = proxy_req->token_used->length;
1870 coap_cancel_observe_lkd(proxy_entry->ongoing, &tmp, COAP_MESSAGE_CON);
1871 }
1872 coap_proxy_del_req(proxy_entry, proxy_req);
1873 }
1874 }
1875 }
1876}
1877
1878#else /* ! COAP_PROXY_SUPPORT */
1879
1880int
1882 return 0;
1883}
1884
1885COAP_API int
1887 const coap_pdu_t *request,
1888 coap_pdu_t *response,
1889 coap_resource_t *resource,
1890 coap_cache_key_t *cache_key,
1891 coap_proxy_server_list_t *server_list) {
1892 (void)session;
1893 (void)request;
1894 (void)resource;
1895 (void)cache_key;
1896 (void)server_list;
1897 response->code = COAP_RESPONSE_CODE(500);
1898 return 0;
1899}
1900
1903 const coap_pdu_t *received,
1904 coap_cache_key_t **cache_key) {
1905 (void)session;
1906 (void)received;
1907 (void)cache_key;
1908 return COAP_RESPONSE_OK;
1909}
1910
1911int
1913 (void)scheme;
1914 return 0;
1915}
1916#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_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:1205
coap_mid_t coap_send_error_lkd(coap_session_t *session, const coap_pdu_t *request, coap_pdu_code_t code, coap_opt_filter_t *opts)
Sends an error response with code code for request request to dst.
Definition coap_net.c:1302
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:1612
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:1222
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:1016
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:3219
@ COAP_CRIT_PROXY
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:1760
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:1622
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:1768
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:1718
#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:1742
@ 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:1182
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:351
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:374
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:1103
#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