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