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