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