libcoap 4.3.5-develop-5347a62
Loading...
Searching...
No Matches
coap_uri.c
Go to the documentation of this file.
1/* coap_uri.c -- helper functions for URI treatment
2 *
3 * Copyright (C) 2010--2012,2015-2016,2022-2026 Olaf Bergmann <bergmann@tzi.org>
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 defined(HAVE_LIMITS_H)
19#include <limits.h>
20#endif
21
22#include <stdint.h>
23#include <stdio.h>
24#include <string.h>
25#include <ctype.h>
26
27#ifdef _WIN32
28#define strcasecmp _stricmp
29#define strncasecmp _strnicmp
30#endif
31
34
46COAP_STATIC_INLINE const uint8_t *
47strnchr(const uint8_t *s, size_t len, unsigned char c) {
48 while (len && *s++ != c)
49 --len;
50
51 return len ? s : NULL;
52}
53
58
64 { "http", 80, 1, COAP_URI_SCHEME_HTTP },
65 { "https", 443, 1, COAP_URI_SCHEME_HTTPS },
66 { "coap+ws", 80, 0, COAP_URI_SCHEME_COAP_WS },
67 { "coaps+ws", 443, 0, COAP_URI_SCHEME_COAPS_WS }
68};
69
70/*
71 * Returns 0 All OK
72 * -1 Insufficient / Invalid parameters
73 * -2 No '://'
74 * -3 Ipv6 definition error or no host defined after scheme://
75 * -4 Invalid port value
76 * -5 Port defined for Unix domain or LLC host
77 * -6 Hostname > 255 chars
78 * -7 Invalid LLC address
79 */
80static int
81coap_split_uri_sub(const uint8_t *str_var,
82 size_t len,
83 coap_uri_t *uri,
84 coap_uri_check_t check_proxy) {
85 const uint8_t *p, *q;
86 int res = 0;
87 size_t i;
88 int is_unix_domain = 0;
89 int is_llc = 0;
90
91 if (!str_var || !uri || len == 0)
92 return -1;
93
94 memset(uri, 0, sizeof(coap_uri_t));
96
97 /* search for scheme */
98 p = str_var;
99 if (*p == '/') {
100 /* no scheme, host or port */
101 if (check_proxy == COAP_URI_CHECK_PROXY) {
102 /* Must have ongoing host if proxy definition */
103 return -1;
104 }
105 q = p;
106 goto path;
107 }
108
109 /* find scheme terminating :// */
110 while (len >= 3 && !(p[0] == ':' && p[1] == '/' && p[2] == '/')) {
111 ++p;
112 --len;
113 }
114 if (len < 3) {
115 /* scheme not defined with a :// terminator */
116 res = -2;
117 goto error;
118 }
119 for (i = 0; i < COAP_URI_SCHEME_LAST; i++) {
120 if ((p - str_var) == (int)strlen(coap_uri_scheme[i].name) &&
121 strncasecmp((const char *)str_var, coap_uri_scheme[i].name, p - str_var) == 0) {
122 if (check_proxy != COAP_URI_CHECK_PROXY && coap_uri_scheme[i].proxy_only) {
123 coap_log_err("%.*s URI scheme not enabled (not a proxy)\n",
124 (int)(p - str_var), str_var);
125 return -1;
126 }
127 uri->scheme = coap_uri_scheme[i].scheme;
128 uri->port = coap_uri_scheme[i].port;
129 break;
130 }
131 }
132 if (i == COAP_URI_SCHEME_LAST) {
133 /* scheme unknown */
134 coap_log_err("%.*s URI scheme unknown\n", (int)(p - str_var), str_var);
135 res = -1;
136 goto error;
137 }
138 switch (uri->scheme) {
140 break;
142 if (!coap_dtls_is_supported()) {
143 coap_log_err("coaps URI scheme not supported in this version of libcoap\n");
144 return -1;
145 }
146 break;
148 if (!coap_tcp_is_supported()) {
149 coap_log_err("coap+tcp URI scheme not supported in this version of libcoap\n");
150 return -1;
151 }
152 break;
154 if (!coap_tls_is_supported()) {
155 coap_log_err("coaps+tcp URI scheme not supported in this version of libcoap\n");
156 return -1;
157 }
158 break;
160 if (!coap_ws_is_supported()) {
161 coap_log_err("coap+ws URI scheme not supported in this version of libcoap\n");
162 return -1;
163 }
164 break;
166 if (!coap_wss_is_supported()) {
167 coap_log_err("coaps+ws URI scheme not supported in this version of libcoap\n");
168 return -1;
169 }
170 break;
173 /* Not proxy, caught above. For proxy, assume app is doing CoAP <> HTTP mapping. */
174 break;
176 default:
177 coap_log_warn("Unsupported URI type %d\n", uri->scheme);
178 return -1;
179 }
180 /* skip :// */
181 p += 3;
182 len -= 3;
183
184 /* p points to beginning of Uri-Host */
185 q = p;
186
187 if (len && *p == '[') {
188 /* IPv6 address reference or Unix domain */
189 ++p;
190 ++q;
191 --len;
192
193 while (len && *q != ']') {
194 ++q;
195 --len;
196 }
197
198 if (!len || *q != ']' || p == q) {
199 res = -3;
200 goto error;
201 }
202
203 if ((int)(q - p) > 255) {
204 coap_log_warn("Host name length too long (%d > 255)\n", (int)(q - p));
205 res = -6;
206 goto error;
207 }
208
209 COAP_SET_STR(&uri->host, q - p, p);
210 ++q;
211 --len;
212#if COAP_AF_LLC_SUPPORT
213 } else if ((len >= LLC_HOST_LEN) && strncmp((const char *)p, "llc[", 4) == 0) {
214 unsigned long sap = 0;
215
216 is_llc = 1;
217
218 while (len && *q != ']') {
219 ++q;
220 --len;
221 }
222
223 if (!len || *q != ']' || p == q) {
224 res = -7;
225 goto error;
226 }
227
228 ++q;
229 --len;
230
231 if (!len || *q != ':') {
232 coap_log_warn("LLC SAP missing in URI\n");
233 res = -7;
234 goto error;
235 }
236
237 ++q;
238 --len;
239
240 while (len && isxdigit(*q)) {
241 if (*q >= 'a' && *q <= 'f')
242 sap = sap * 16 + (*q - 'a' + 10);
243 else if (*q >= 'A' && *q <= 'F')
244 sap = sap * 16 + (*q - 'A' + 10);
245
246 ++q;
247 --len;
248 }
249
250 if (sap > UINT8_MAX) {
251 coap_log_warn("LLC SAP invalid (%lu > 255)\n", sap);
252 res = -7;
253 goto error;
254 }
255
256 COAP_SET_STR(&uri->host, q - p, p);
257#endif /* COAP_AF_LLC_SUPPORT */
258 } else {
259 /* IPv4 address, FQDN or Unix domain socket */
260 if (len >= 3 && p[0] == '%' && p[1] == '2' &&
261 (p[2] == 'F' || p[2] == 'f')) {
262 /* Unix domain definition */
263 uri->port = 0;
264 is_unix_domain = 1;
265 }
266 while (len && *q != ':' && *q != '/' && *q != '?') {
267 ++q;
268 --len;
269 }
270
271 if (p == q) {
272 res = -3;
273 goto error;
274 }
275
276 if ((int)(q - p) > 255) {
277 coap_log_warn("Host name length too long (%d > 255)\n", (int)(q - p));
278 res = -6;
279 goto error;
280 }
281
282 COAP_SET_STR(&uri->host, q - p, p);
283 }
284
285 /* check for Uri-Port (invalid for Unix) */
286 if (len && *q == ':') {
287 if (is_unix_domain || is_llc) {
288 res = -5;
289 goto error;
290 }
291 p = ++q;
292 --len;
293
294 while (len && isdigit(*q)) {
295 ++q;
296 --len;
297 }
298
299 if (p < q) { /* explicit port number given */
300 long uri_port = 0;
301
302 while ((p < q) && (uri_port <= UINT16_MAX))
303 uri_port = uri_port * 10 + (*p++ - '0');
304
305 /* check if port number is in allowed range */
306 if (uri_port > UINT16_MAX) {
307 coap_log_warn("Port number too big (%ld > 65535)\n", uri_port);
308 res = -4;
309 goto error;
310 }
311
312 uri->port = (uint16_t)uri_port;
313 }
314 }
315
316path: /* q points just past the authority, or to a relative URI's leading '/' */
317
318 if (!len)
319 goto end;
320
321 if (*q == '/') {
322 p = ++q;
323 --len;
324
325 while (len && *q != '?') {
326 ++q;
327 --len;
328 }
329
330 if (p < q) {
331 COAP_SET_STR(&uri->path, q - p, p);
332 }
333 }
334
335 /* Uri_Query */
336 if (len && *q == '?') {
337 ++q;
338 --len;
339 COAP_SET_STR(&uri->query, len, q);
340 len = 0;
341 }
342
343end:
344 return len ? -1 : 0;
345
346error:
347 return res;
348}
349
350int
351coap_split_uri(const uint8_t *str_var, size_t len, coap_uri_t *uri) {
352 return coap_split_uri_sub(str_var, len, uri, COAP_URI_CHECK_URI);
353}
354
355int
356coap_split_proxy_uri(const uint8_t *str_var, size_t len, coap_uri_t *uri) {
357 return coap_split_uri_sub(str_var, len, uri, COAP_URI_CHECK_PROXY);
358}
359
360static void
362 size_t i;
363
364 for (i = 0; i < optlist->length; i++) {
365 if (optlist->data[i] >= 'A' && optlist->data[i] <= 'Z') {
366 optlist->data[i] += 'a' - 'A';
367 }
368 }
369}
370
371int
373 coap_optlist_t **optlist_chain, int create_port_host_opt,
374 uint8_t *_buf COAP_UNUSED, size_t buflen COAP_UNUSED) {
375 return !coap_uri_into_optlist(uri, dst, optlist_chain, create_port_host_opt) ? -1 : 0;
376}
377
378int
380 coap_optlist_t **optlist_chain, int create_port_host_opt) {
381 return coap_uri_into_optlist_abbrev(uri, dst, optlist_chain, create_port_host_opt, NULL, 0);
382}
383
384int
386 coap_optlist_t **optlist_chain, int create_port_host_opt,
387 coap_upa_abbrev_t *mapping, uint32_t count) {
388 if (create_port_host_opt && !coap_host_is_unix_domain(&uri->host)) {
389 int add_option = 0;
390
391 if (dst && uri->host.length) {
392#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
393 char addr[INET6_ADDRSTRLEN];
394#else /* WITH_LWIP || WITH_CONTIKI */
395 char addr[40];
396#endif /* WITH_LWIP || WITH_CONTIKI */
397 coap_optlist_t *optlist;
398
399 /* Add in Uri-Host if not match (need to strip off %iface) */
400 size_t uri_host_len = uri->host.length;
401 const uint8_t *cp = uri->host.s;
402
403 /* Unfortunately not null terminated */
404 for (size_t i = 0; i < uri_host_len; i++) {
405 if (cp[i] == '%') {
406 /* %iface specified in host name */
407 uri_host_len = i;
408 break;
409 }
410 }
411
412 if (coap_print_ip_addr(dst, addr, sizeof(addr)) &&
413 (strlen(addr) != uri_host_len ||
414 strncasecmp(addr, (const char *)uri->host.s, uri_host_len) != 0)) {
415 /* add Uri-Host */
416 optlist = coap_new_optlist(COAP_OPTION_URI_HOST, uri_host_len,
417 uri->host.s);
418 if (!optlist)
419 return 0;
420 if (!coap_host_is_unix_domain(&uri->host)) {
421 coap_replace_percents(optlist);
423 }
424 if (!coap_insert_optlist(optlist_chain, optlist)) {
425 return 0;
426 }
427 }
428 }
429 /* Add in UriPort if not default */
430 switch ((int)uri->scheme) {
433 if (uri->port != 80)
434 add_option = 1;
435 break;
438 if (uri->port != 443)
439 add_option = 1;
440 break;
441 default:
444 add_option = 1;
445 break;
446 }
447 if (add_option) {
448 uint8_t tbuf[4];
449
450 coap_insert_optlist(optlist_chain,
452 coap_encode_var_safe(tbuf, 4,
453 (uri->port & 0xffff)),
454 tbuf));
455 }
456 }
457
458 if (uri->path.length) {
460 optlist_chain, mapping, count))
461 return 0;
462 }
463
464 if (uri->query.length) {
466 optlist_chain))
467 return 0;
468 }
469 return 1;
470}
471
472int
474 if (host->length >= 3 && host->s[0] == '%' &&
475 host->s[1] == '2' &&
476 (host->s[2] == 'F' || host->s[2] == 'f')) {
477 return 1;
478 }
479 if (host->length >= 1 && host->s[0] == '/')
480 return 1;
481 return 0;
482}
483
484int
486#if COAP_AF_LLC_SUPPORT
487 size_t length;
488 const uint8_t *s = NULL;
489 int i = 0;
490
491 length = host->length;
492 if (length != LLC_HOST_LEN)
493 return 0;
494
495 s = host->s;
496
497 if (strncmp((const char *)s, "llc[", 4) != 0)
498 return 0;
499
500 length -= 4;
501 s += 4;
502
503 while (length) {
504 if (!isxdigit(s[0]) || !isxdigit(s[1]))
505 return 0;
506
507 length -= 2;
508 s += 2;
509
510 i++;
511
512 if (length <= 0 || i >= 6)
513 break;
514
515 if (s[0] != ':')
516 return 0;
517
518 length--;
519 s++;
520 }
521
522 if (s[0] != ']' || s[1] != ':' || !isxdigit(s[2]) || !isxdigit(s[3]))
523 return 0;
524
525 return 1;
526#else /* COAP_AF_LLC_SUPPORT */
527 (void)host;
528 return 0;
529#endif /* COAP_AF_LLC_SUPPORT */
530}
531
539#define hexchar_to_dec(c) ((c) & 0x40 ? ((c) & 0x0F) + 9 : ((c) & 0x0F))
540
553static void
554decode_segment(const uint8_t *seg, size_t length, unsigned char *buf) {
555
556 while (length) {
557 length--;
558
559 if (*seg == '%') {
560 if (length < 2)
561 return;
562 *buf = (hexchar_to_dec(seg[1]) << 4) + hexchar_to_dec(seg[2]);
563
564 seg += 2;
565 length -= 2;
566 } else {
567 *buf = *seg;
568 }
569
570 ++buf;
571 ++seg;
572 }
573}
574
580static int
581check_segment(const uint8_t *s, size_t length, size_t *segment_size) {
582 size_t n = 0;
583
584 while (length) {
585 if (*s == '%') {
586 if (length < 3 || !(isxdigit(s[1]) && isxdigit(s[2])))
587 return -1;
588
589 s += 2;
590 length -= 2;
591 }
592
593 ++s;
594 ++n;
595 --length;
596 }
597
598 *segment_size = n;
599
600 return 0;
601}
602
623static int
624make_decoded_option(const uint8_t *s, size_t length,
625 unsigned char *buf, size_t buflen, size_t *optionsize) {
626 int res;
627 size_t segmentlen;
628 size_t written;
629
630 if (!buflen) {
631 coap_log_debug("make_decoded_option(): buflen is 0!\n");
632 return -1;
633 }
634
635 res = check_segment(s, length, &segmentlen);
636 if (res < 0)
637 return -1;
638
639 /* write option header using delta 0 and length res */
640 written = coap_opt_setheader(buf, buflen, 0, segmentlen);
641
642 assert(written <= buflen);
643
644 if (!written) /* encoding error */
645 return -1;
646
647 buf += written; /* advance past option type/length */
648 buflen -= written;
649
650 if (buflen < segmentlen) {
651 coap_log_debug("buffer too small for option\n");
652 return -1;
653 }
654
655 decode_segment(s, length, buf);
656
657 *optionsize = written + segmentlen;
658
659 return 0;
660}
661
662
663#ifndef min
664#define min(a,b) ((a) < (b) ? (a) : (b))
665#endif
666
667typedef void (*segment_handler_t)(const uint8_t *, size_t, void *);
668
674int
675coap_check_dots(const uint8_t *s, size_t len) {
676 uint8_t p;
677
678 if (!len)
679 return 0;
680
681 p = *s;
682
683 /* Check 'first' char */
684 if (p == '%' && len >=3) {
685 if (s[1] == '2' && (s[2] == 'E' || s[2] == 'e')) {
686 s += 2;
687 len -= 2;
688 }
689 p = '.';
690 }
691 if (p != '.')
692 return 0;
693 if (len == 1)
694 return 1;
695
696 /* Check 'second' char, first is '.' */
697 s++;
698 len--;
699 assert(len);
700 p = *s;
701 if (p == '%' && len >=3) {
702 if (s[1] == '2' && (s[2] == 'E' || s[2] == 'e')) {
703 len -= 2;
704 }
705 p = '.';
706 }
707 if (p != '.')
708 return 0;
709 if (len == 1)
710 return 2;
711
712 return 0;
713}
714
720
721static void
722backup_segment(void *data) {
723 struct cnt_str *state = (struct cnt_str *)data;
724 int i;
725 uint8_t *buf;
726
727 if (state->n == 0)
728 return;
729
730 state->n--;
731 buf = state->base_buf.s;
732 for (i = 0; i < state->n; i++) {
734 }
735 state->buf.s = buf;
736 state->buf.length = state->base_buf.length - (buf - state->base_buf.s);
737}
738
750static size_t
751coap_split_path_impl(const uint8_t *path, size_t len,
752 segment_handler_t h, void *data) {
753 const uint8_t *p, *q;
754 size_t length = len;
755 int num_dots;
756
757 p = q = path;
758 while (length > 0 && !strnchr((const uint8_t *)"?#", 2, *q)) {
759 if (*q == '/') {
760 /* start new segment */
761 num_dots = coap_check_dots(p, q - p);
762 switch (num_dots) {
763 case 1:
764 /* drop segment */
765 break;
766 case 2:
767 /* backup segment */
768 backup_segment(data);
769 break;
770 case 0:
771 default:
772 /* add segment */
773 h(p, q - p, data);
774 break;
775 }
776
777 p = q + 1;
778 }
779
780 q++;
781 length--;
782 }
783
784 /* write last segment */
785 num_dots = coap_check_dots(p, q - p);
786 switch (num_dots) {
787 case 1:
788 /* drop segment */
789 break;
790 case 2:
791 /* backup segment */
792 backup_segment(data);
793 break;
794 case 0:
795 default:
796 /* add segment */
797 h(p, q - p, data);
798 break;
799 }
800
801 return q - path;
802}
803
804static void
805write_option(const uint8_t *s, size_t len, void *data) {
806 struct cnt_str *state = (struct cnt_str *)data;
807 int res;
808 size_t optionsize;
809 assert(state);
810
811 res = make_decoded_option(s, len, state->buf.s, state->buf.length, &optionsize);
812 if (res == 0) {
813 state->buf.s += optionsize;
814 state->buf.length -= optionsize;
815 state->n++;
816 }
817}
818
819int
820coap_split_path(const uint8_t *s, size_t length,
821 unsigned char *buf, size_t *buflen) {
822 struct cnt_str tmp = { { *buflen, buf }, { *buflen, buf }, 0 };
823
824 coap_split_path_impl(s, length, write_option, &tmp);
825
826 *buflen = *buflen - tmp.buf.length;
827
828 return tmp.n;
829}
830
831void
833 size_t i;
834 size_t o = 0;
835
836 for (i = 0; i < optlist->length; i++) {
837 if (optlist->data[i] == '%' && optlist->length - i >= 3) {
838 optlist->data[o] = (hexchar_to_dec(optlist->data[i+1]) << 4) +
839 hexchar_to_dec(optlist->data[i+2]);
840 i+= 2;
841 } else if (o != i) {
842 optlist->data[o] = optlist->data[i];
843 }
844 o++;
845 }
846 optlist->length = o;
847}
848
849static void
851 coap_optlist_t *last = NULL;
852 coap_optlist_t *cur = *optlist_begin;
853
854 if (!cur)
855 return;
856
857 while (cur) {
858 if (!cur->next)
859 break;
860 last = cur;
861 cur = cur->next;
862 }
864 if (last) {
865 last->next = NULL;
866 } else {
867 *optlist_begin = NULL;
868 }
869}
870
871int
872coap_path_into_optlist(const uint8_t *s, size_t length, coap_option_num_t optnum,
873 coap_optlist_t **optlist_chain) {
874 return coap_path_into_optlist_abbrev(s, length, optnum, optlist_chain, NULL, 0);
875}
876
877int
878coap_path_into_optlist_abbrev(const uint8_t *s, size_t length, coap_option_num_t optnum,
879 coap_optlist_t **optlist_chain, coap_upa_abbrev_t *mapping,
880 uint32_t count) {
881 const uint8_t *p = s;
882 coap_optlist_t *optlist;
883 int num_dots;
884 coap_optlist_t **optlist_start = optlist_chain;
885
886 /* A ".." can remove only segments appended by this call. */
887 while (*optlist_start)
888 optlist_start = &(*optlist_start)->next;
889
890 if (length > 0 && mapping) {
891 uint32_t i;
892 uint8_t buf[4];
893
894 for (i = 0; i < count; i++) {
895 if (strlen(mapping[i].upa_path) == length &&
896 memcmp(mapping[i].upa_path, s, length) == 0) {
897 /* add in as Uri_path-Abbrev */
899 coap_encode_var_safe(buf, sizeof(buf), mapping[i].upa_value), buf);
900 if (!coap_insert_optlist(optlist_chain, optlist)) {
901 return 0;
902 }
903 return 1;
904 }
905 }
906 }
907 while (length > 0 && !strnchr((const uint8_t *)"?#", 2, *s)) {
908 if (*s == '/') { /* start of new path element */
909 /* start new segment */
910 num_dots = coap_check_dots(p, s - p);
911 switch (num_dots) {
912 case 1:
913 /* drop segment */
914 break;
915 case 2:
916 /* backup segment */
917 backup_optlist(optlist_start);
918 break;
919 case 0:
920 default:
921 /* add segment */
922 optlist = coap_new_optlist(optnum, s - p, p);
923 if (!optlist) {
924 return 0;
925 }
926 coap_replace_percents(optlist);
927 if (!coap_insert_optlist(optlist_chain, optlist)) {
928 return 0;
929 }
930 break;
931 }
932 p = s + 1;
933 }
934 s++;
935 length--;
936
937 }
938 /* add last path element */
939 num_dots = coap_check_dots(p, s - p);
940 switch (num_dots) {
941 case 1:
942 /* drop segment */
943 break;
944 case 2:
945 /* backup segment */
946 backup_optlist(optlist_start);
947 break;
948 case 0:
949 default:
950 /* add segment */
951 optlist = coap_new_optlist(optnum, s - p, p);
952 if (!optlist) {
953 return 0;
954 }
955 coap_replace_percents(optlist);
956 if (!coap_insert_optlist(optlist_chain, optlist)) {
957 return 0;
958 }
959 break;
960 }
961 return 1;
962}
963
964int
965coap_split_query(const uint8_t *s, size_t length,
966 unsigned char *buf, size_t *buflen) {
967 struct cnt_str tmp = { { *buflen, buf }, { *buflen, buf }, 0 };
968 const uint8_t *p;
969
970 p = s;
971 while (length > 0 && *s != '#') {
972 if (*s == '&') { /* start new query element */
973 write_option(p, s - p, &tmp);
974 p = s + 1;
975 }
976
977 s++;
978 length--;
979 }
980
981 /* write last query element */
982 write_option(p, s - p, &tmp);
983
984 *buflen = *buflen - tmp.buf.length;
985 return tmp.n;
986}
987
988int
989coap_query_into_optlist(const uint8_t *s, size_t length, coap_option_num_t optnum,
990 coap_optlist_t **optlist_chain) {
991 const uint8_t *p = s;
992 coap_optlist_t *optlist;
993
994 while (length > 0 && *s != '#') {
995 if (*s == '&') { /* start of new query element */
996 /* add previous query element */
997 optlist = coap_new_optlist(optnum, s - p, p);
998 if (!optlist) {
999 return 0;
1000 }
1001 coap_replace_percents(optlist);
1002 if (!coap_insert_optlist(optlist_chain, optlist)) {
1003 return 0;
1004 }
1005 p = s + 1;
1006 }
1007 s++;
1008 length--;
1009 }
1010 /* add last query element */
1011 optlist = coap_new_optlist(optnum, s - p, p);
1012 if (!optlist) {
1013 return 0;
1014 }
1015 coap_replace_percents(optlist);
1016 if (!coap_insert_optlist(optlist_chain, optlist)) {
1017 return 0;
1018 }
1019 return 1;
1020}
1021
1022#define URI_DATA(uriobj) ((unsigned char *)(uriobj) + sizeof(coap_uri_t))
1023
1024coap_uri_t *
1025coap_new_uri(const uint8_t *uri, unsigned int length) {
1026 uint8_t *result;
1027 coap_uri_t *out_uri;
1028
1029 out_uri = (coap_uri_t *)coap_malloc_type(COAP_STRING, length + 1 + sizeof(coap_uri_t));
1030
1031 if (!out_uri)
1032 return NULL;
1033
1034 result = (uint8_t *)out_uri;
1035 memcpy(URI_DATA(result), uri, length);
1036 URI_DATA(result)[length] = '\0'; /* make it zero-terminated */
1037
1038 if (coap_split_uri(URI_DATA(result), length, out_uri) < 0) {
1039 coap_free_type(COAP_STRING, out_uri);
1040 return NULL;
1041 }
1042 return out_uri;
1043}
1044
1045coap_uri_t *
1047 coap_uri_t *result;
1048 uint8_t *p;
1049
1050 if (!uri)
1051 return NULL;
1052
1053 result = (coap_uri_t *)coap_malloc_type(COAP_STRING, uri->query.length + uri->host.length +
1054 uri->path.length + sizeof(coap_uri_t) + 1);
1055
1056 if (!result)
1057 return NULL;
1058
1059 memset(result, 0, sizeof(coap_uri_t));
1060
1061 result->port = uri->port;
1062 result->scheme = uri->scheme;
1063
1064 if (uri->host.length) {
1065 result->host.s = p = URI_DATA(result);
1066 result->host.length = uri->host.length;
1067
1068 memcpy(p, uri->host.s, uri->host.length);
1069 }
1070
1071 if (uri->path.length) {
1072 result->path.s = p = URI_DATA(result) + uri->host.length;
1073 result->path.length = uri->path.length;
1074
1075 memcpy(p, uri->path.s, uri->path.length);
1076 }
1077
1078 if (uri->query.length) {
1079 result->query.s = p = URI_DATA(result) + uri->host.length + uri->path.length;
1080 result->query.length = uri->query.length;
1081
1082 memcpy(p, uri->query.s, uri->query.length);
1083 }
1084
1085 return result;
1086}
1087
1088void
1092
1093static int
1094is_unescaped_in_path(const uint8_t c) {
1095 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
1096 (c >= '0' && c <= '9') || c == '-' || c == '.' || c == '_' ||
1097 c == '~' || c == '!' || c == '$' || c == '\'' || c == '(' ||
1098 c == ')' || c == '*' || c == '+' || c == ',' || c == ';' ||
1099 c=='=' || c==':' || c=='@' || c == '&';
1100}
1101
1102static int
1103is_unescaped_in_query(const uint8_t c) {
1104 return c != '&' && (is_unescaped_in_path(c) || c == '/' || c == '?');
1105}
1106
1109 coap_opt_iterator_t opt_iter;
1111 coap_opt_t *q;
1112 coap_string_t *query = NULL;
1113 size_t length = 0;
1114 int have_query = 0;
1115 static const uint8_t hex[] = "0123456789ABCDEF";
1116
1119 coap_option_iterator_init(request, &opt_iter, &f);
1120 while ((q = coap_option_next(&opt_iter))) {
1121 uint16_t seg_len = coap_opt_length(q), i;
1122 const uint8_t *seg= coap_opt_value(q);
1123 have_query = 1;
1124 for (i = 0; i < seg_len; i++) {
1125 if (is_unescaped_in_query(seg[i]))
1126 length += 1;
1127 else
1128 length += 3;
1129 }
1130 length += 1;
1131 }
1132 if (length > 0)
1133 length -= 1;
1134 /* Allocate and return a string whenever at least one Uri-Query option is
1135 present, even when every option is empty and the encoded length is zero.
1136 A single empty Uri-Query then yields an empty string rather than NULL,
1137 which is reserved for "no Uri-Query option at all". */
1138 if (have_query) {
1139 query = coap_new_string(length);
1140 if (query) {
1141 unsigned char *s = query->s;
1142 int first = 1;
1143 query->length = length;
1144 coap_option_iterator_init(request, &opt_iter, &f);
1145 while ((q = coap_option_next(&opt_iter))) {
1146 /* Write the '&' separator before every Uri-Query option except the
1147 first. An empty Uri-Query option is a distinct zero-length segment
1148 (just as "&&" is not "&"), so the separators around it must still
1149 be emitted. Keying this off the option index rather than off
1150 whether any byte has been written yet is what keeps leading empty
1151 options from being dropped, and keeps the buffer fully written so
1152 no uninitialised tail is exposed to callers. */
1153 if (!first)
1154 *s++ = '&';
1155 first = 0;
1156 uint16_t seg_len = coap_opt_length(q), i;
1157 const uint8_t *seg= coap_opt_value(q);
1158 for (i = 0; i < seg_len; i++) {
1159 if (is_unescaped_in_query(seg[i])) {
1160 *s++ = seg[i];
1161 } else {
1162 *s++ = '%';
1163 *s++ = hex[seg[i]>>4];
1164 *s++ = hex[seg[i]&0x0F];
1165 }
1166 }
1167 }
1168 }
1169 }
1170 return query;
1171}
1172
1173const char *
1175 while (chain) {
1176 coap_upa_chain_t *next = chain->next;
1177
1178 if (chain->upa_value == value) {
1179 return chain->upa_path;
1180 }
1181 chain = next;
1182 }
1183 return NULL;
1184}
1185
1186int
1187coap_map_uri_path_abbrev(coap_upa_chain_t *chain, const char *path, size_t length,
1188 uint32_t *value) {
1189 while (chain) {
1190 coap_upa_chain_t *next = chain->next;
1191
1192 if (strlen(chain->upa_path) == length && memcmp(chain->upa_path, path, length) == 0) {
1193 *value = chain->upa_value;
1194 return 1;
1195 }
1196 chain = next;
1197 }
1198 return 0;
1199}
1200
1203 coap_opt_iterator_t opt_iter;
1205 coap_opt_t *q;
1206 coap_string_t *uri_path = NULL;
1207 size_t length = 0;
1208 static const uint8_t hex[] = "0123456789ABCDEF";
1209
1210 q = coap_check_option(request, COAP_OPTION_URI_PATH_ABB, &opt_iter);
1211 if (q) {
1212 uint32_t value;
1213 const char *exp;
1214 if (coap_check_option(request, COAP_OPTION_PROXY_URI, &opt_iter) ||
1215 coap_check_option(request, COAP_OPTION_URI_PATH, &opt_iter)) {
1216 return NULL;
1217 }
1220 if (exp) {
1221 uri_path = coap_new_string(strlen(exp));
1222 if (uri_path) {
1223 memcpy(uri_path->s, exp, strlen(exp));
1224 }
1225 return uri_path;
1226 }
1227 return NULL;
1228 }
1229 q = coap_check_option(request, COAP_OPTION_PROXY_URI, &opt_iter);
1230 if (q) {
1231 coap_uri_t uri;
1232
1234 coap_opt_length(q), &uri) < 0) {
1235 return NULL;
1236 }
1237 uri_path = coap_new_string(uri.path.length);
1238 if (uri_path && uri.path.length) {
1239 memcpy(uri_path->s, uri.path.s, uri.path.length);
1240 }
1241 return uri_path;
1242 }
1243
1246 coap_option_iterator_init(request, &opt_iter, &f);
1247 while ((q = coap_option_next(&opt_iter))) {
1248 uint16_t seg_len = coap_opt_length(q), i;
1249 const uint8_t *seg = coap_opt_value(q);
1250 for (i = 0; i < seg_len; i++) {
1251 if (is_unescaped_in_path(seg[i]))
1252 length += 1;
1253 else
1254 length += 3;
1255 }
1256 /* bump for the leading "/" */
1257 length += 1;
1258 }
1259 /* The first entry does not have a leading "/" */
1260 if (length > 0)
1261 length -= 1;
1262
1263 /* if 0, either no URI_PATH Option, or the first one was empty */
1264 uri_path = coap_new_string(length);
1265 if (uri_path) {
1266 uri_path->length = length;
1267 unsigned char *s = uri_path->s;
1268 int n = 0;
1269 coap_option_iterator_init(request, &opt_iter, &f);
1270 while ((q = coap_option_next(&opt_iter))) {
1271 if (n++) {
1272 *s++ = '/';
1273 }
1274 uint16_t seg_len = coap_opt_length(q), i;
1275 const uint8_t *seg= coap_opt_value(q);
1276 for (i = 0; i < seg_len; i++) {
1277 if (is_unescaped_in_path(seg[i])) {
1278 *s++ = seg[i];
1279 } else {
1280 *s++ = '%';
1281 *s++ = hex[seg[i]>>4];
1282 *s++ = hex[seg[i]&0x0F];
1283 }
1284 }
1285 }
1286 }
1287 return uri_path;
1288}
1289
1290void
1292 while (chain) {
1293 coap_upa_chain_t *next = chain->next;
1294
1296 chain = next;
1297 }
1298}
1299
1300static coap_upa_chain_t *
1302 uint32_t i;
1303 coap_upa_chain_t *chain = NULL;
1304 coap_upa_chain_t *next;
1305
1306 for (i = 0; i < count; i++) {
1307 next = coap_malloc_type(COAP_STRING, sizeof(coap_upa_chain_t) + strlen(list[i].upa_path) + 1);
1308 if (next == NULL)
1309 goto cleanup;
1310
1311 next->upa_value = list[i].upa_value;
1312 next->upa_path = (char *)next + sizeof(coap_upa_chain_t);
1313 strcpy(next->upa_path, list[i].upa_path);
1314 next->next = chain;
1315 chain = next;
1316 }
1317 return chain;
1318
1319cleanup:
1320 coap_delete_upa_chain(chain);
1321 return NULL;
1322}
1323
1324void
1331
1332void
#define INET6_ADDRSTRLEN
Definition coap_debug.c:259
Library specific build wrapper for coap_internal.h.
@ COAP_STRING
Definition coap_mem.h:33
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().
size_t coap_opt_size(const coap_opt_t *opt)
Returns the size of the given option, taking into account a possible option jump.
#define NULL
Definition coap_option.h:30
uint16_t coap_option_num_t
Definition coap_option.h:37
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_URI_PORT
Definition coap_option.h:76
@ COAP_OPTION_URI_HOST
Definition coap_option.h:72
@ COAP_OPTION_URI_PATH
Definition coap_option.h:79
@ COAP_OPTION_URI_QUERY
Definition coap_option.h:84
@ COAP_OPTION_PROXY_URI
Definition coap_option.h:94
@ COAP_OPTION_URI_PATH_ABB
Definition coap_option.h:81
static void coap_replace_upper_lower(coap_optlist_t *optlist)
Definition coap_uri.c:361
int coap_uri_into_options(const coap_uri_t *uri, const coap_address_t *dst, coap_optlist_t **optlist_chain, int create_port_host_opt, uint8_t *_buf COAP_UNUSED, size_t buflen COAP_UNUSED)
Definition coap_uri.c:372
static void backup_optlist(coap_optlist_t **optlist_begin)
Definition coap_uri.c:850
static void decode_segment(const uint8_t *seg, size_t length, unsigned char *buf)
Decodes percent-encoded characters while copying the string seg of size length to buf.
Definition coap_uri.c:554
static coap_upa_chain_t * coap_build_upa_chain(coap_upa_abbrev_t *list, uint32_t count)
Definition coap_uri.c:1301
static int is_unescaped_in_query(const uint8_t c)
Definition coap_uri.c:1103
COAP_STATIC_INLINE const uint8_t * strnchr(const uint8_t *s, size_t len, unsigned char c)
A length-safe version of strchr().
Definition coap_uri.c:47
static int check_segment(const uint8_t *s, size_t length, size_t *segment_size)
Runs through the given path (or query) segment and checks if percent-encodings are correct.
Definition coap_uri.c:581
static void write_option(const uint8_t *s, size_t len, void *data)
Definition coap_uri.c:805
static void backup_segment(void *data)
Definition coap_uri.c:722
void coap_delete_uri(coap_uri_t *uri)
Removes the specified coap_uri_t object.
Definition coap_uri.c:1089
static size_t coap_split_path_impl(const uint8_t *path, size_t len, segment_handler_t h, void *data)
Splits the given string into segments.
Definition coap_uri.c:751
static int coap_split_uri_sub(const uint8_t *str_var, size_t len, coap_uri_t *uri, coap_uri_check_t check_proxy)
Definition coap_uri.c:81
#define hexchar_to_dec(c)
Calculates decimal value from hexadecimal ASCII character given in c.
Definition coap_uri.c:539
static int make_decoded_option(const uint8_t *s, size_t length, unsigned char *buf, size_t buflen, size_t *optionsize)
Writes a coap option from given string s to buf.
Definition coap_uri.c:624
int coap_host_is_llc(const coap_str_const_t *host)
Determines from the host whether this is an LLC socket request.
Definition coap_uri.c:485
static int is_unescaped_in_path(const uint8_t c)
Definition coap_uri.c:1094
coap_uri_check_t
Definition coap_uri.c:54
@ COAP_URI_CHECK_URI
Definition coap_uri.c:55
@ COAP_URI_CHECK_PROXY
Definition coap_uri.c:56
coap_uri_t * coap_clone_uri(const coap_uri_t *uri)
Clones the specified coap_uri_t object.
Definition coap_uri.c:1046
#define URI_DATA(uriobj)
Definition coap_uri.c:1022
coap_uri_t * coap_new_uri(const uint8_t *uri, unsigned int length)
Creates a new coap_uri_t object from the specified URI.
Definition coap_uri.c:1025
int coap_host_is_unix_domain(const coap_str_const_t *host)
Determines from the host whether this is a Unix Domain socket request.
Definition coap_uri.c:473
void(* segment_handler_t)(const uint8_t *, size_t, void *)
Definition coap_uri.c:667
static int coap_uri_scheme_is_secure(const coap_uri_t *uri)
Definition coap_uri.h:92
@ 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
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
#define coap_lock_unlock()
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
const char * coap_print_ip_addr(const coap_address_t *addr, char *buf, size_t len)
Print the IP address into the defined buffer.
Definition coap_debug.c:449
#define coap_log_warn(...)
Definition coap_debug.h:108
#define coap_log_err(...)
Definition coap_debug.h:102
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.
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.
size_t coap_opt_setheader(coap_opt_t *opt, size_t maxlen, uint16_t delta, size_t length)
Encodes the given delta and length values into opt.
#define COAP_DEFAULT_PORT
Definition coap_pdu.h:39
#define COAPS_DEFAULT_PORT
Definition coap_pdu.h:40
#define COAP_SET_STR(st, l, v)
Definition coap_str.h:52
coap_string_t * coap_new_string(size_t size)
Returns a new string object with at least size+1 bytes storage allocated.
Definition coap_str.c:21
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:977
int coap_dtls_is_supported(void)
Check whether DTLS is available.
Definition coap_notls.c:36
int coap_wss_is_supported(void)
Check whether Secure WebSockets is available.
Definition coap_ws.c:982
coap_string_t * coap_get_uri_path(const coap_pdu_t *request)
Extract uri_path string from request PDU.
Definition coap_uri.c:1202
int coap_split_path(const uint8_t *s, size_t length, unsigned char *buf, size_t *buflen)
Splits the given URI path into segments.
Definition coap_uri.c:820
void coap_upa_client_fallback(coap_upa_abbrev_t *list, uint32_t count)
Define a Path to use if an Uri-Path-Abbrev option fails and the client is to retry the request using ...
Definition coap_uri.c:1325
void coap_upa_server_mapping(coap_upa_abbrev_t *list, uint32_t count)
Define a Path to use on receipt of an Uri-Path-Abbrev option value.
Definition coap_uri.c:1333
int coap_query_into_optlist(const uint8_t *s, size_t length, coap_option_num_t optnum, coap_optlist_t **optlist_chain)
Splits the given URI query into '&' separate segments, and then adds the Uri-Query / Location-Query o...
Definition coap_uri.c:989
int coap_split_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_path_into_optlist(const uint8_t *s, size_t length, coap_option_num_t optnum, coap_optlist_t **optlist_chain)
Splits the given URI path into '/' separate segments, and then adds the Uri-Path / Location-Path opti...
Definition coap_uri.c:872
int coap_split_proxy_uri(const uint8_t *str_var, size_t len, coap_uri_t *uri)
Parses a given string into URI components.
Definition coap_uri.c:356
int coap_uri_into_optlist_abbrev(const coap_uri_t *uri, const coap_address_t *dst, coap_optlist_t **optlist_chain, int create_port_host_opt, coap_upa_abbrev_t *mapping, uint32_t count)
Takes a coap_uri_t and then adds CoAP options into the optlist_chain.
Definition coap_uri.c:385
int coap_uri_into_optlist(const coap_uri_t *uri, const coap_address_t *dst, coap_optlist_t **optlist_chain, int create_port_host_opt)
Takes a coap_uri_t and then adds CoAP options into the optlist_chain.
Definition coap_uri.c:379
int coap_split_query(const uint8_t *s, size_t length, unsigned char *buf, size_t *buflen)
Splits the given URI query into segments.
Definition coap_uri.c:965
int coap_path_into_optlist_abbrev(const uint8_t *s, size_t length, coap_option_num_t optnum, coap_optlist_t **optlist_chain, coap_upa_abbrev_t *mapping, uint32_t count)
Splits the given URI path into '/' separate segments, and then adds the Uri-Path / Location-Path opti...
Definition coap_uri.c:878
coap_string_t * coap_get_query(const coap_pdu_t *request)
Extract query string from request PDU according to escape rules in 6.5.8.
Definition coap_uri.c:1108
int coap_map_uri_path_abbrev(coap_upa_chain_t *chain, const char *path, size_t length, uint32_t *value)
Definition coap_uri.c:1187
int coap_check_dots(const uint8_t *s, size_t len)
Checks if path segment s consists of one or two dots.
Definition coap_uri.c:675
void coap_delete_upa_chain(coap_upa_chain_t *chain)
Clean up a UPA chain.
Definition coap_uri.c:1291
coap_uri_info_t coap_uri_scheme[COAP_URI_SCHEME_LAST]
Definition coap_uri.c:59
coap_upa_chain_t * coap_upa_server_mapping_chain
Definition coap_uri.c:33
const char * coap_map_abbrev_uri_path(coap_upa_chain_t *chain, uint32_t value)
Determine the expanded Uri-Path-Abbrev option value.
Definition coap_uri.c:1174
void coap_replace_percents(coap_optlist_t *optlist)
replace any % hex definitions with the actual character.
Definition coap_uri.c:832
coap_upa_chain_t * coap_upa_client_fallback_chain
Definition coap_uri.c:32
#define COAP_UNUSED
Definition libcoap.h:74
#define COAP_STATIC_INLINE
Definition libcoap.h:57
int n
Definition coap_uri.c:718
coap_string_t base_buf
Definition coap_uri.c:716
coap_string_t buf
Definition coap_uri.c:717
Multi-purpose address abstraction.
Iterator to run through PDU options.
Representation of chained list of CoAP options to install.
size_t length
the option value length
uint8_t * data
the option data
struct coap_optlist_t * next
next entry in the optlist chain
structure for CoAP PDUs
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
size_t length
length of string
Definition coap_str.h:40
uint32_t upa_value
The Uri-Path-Abbrev option value.
Definition coap_uri.h:86
const char * upa_path
The Uri-Path-Abbrev option path representation (withouot the leading '/').
Definition coap_uri.h:87
uint32_t upa_value
The Uri-Path-Abbrev option value.
struct coap_upa_chain_t * next
Next entry in the chain.
char * upa_path
The Uri-Path-Abbrev option path representation (withouot the leading '/').
Representation of parsed URI.
Definition coap_uri.h:70
enum coap_uri_scheme_t scheme
The parsed scheme specifier.
Definition coap_uri.h:82
coap_str_const_t path
The complete path if present or {0, NULL}.
Definition coap_uri.h:73
uint16_t port
The port in host byte order.
Definition coap_uri.h:72
coap_str_const_t query
The complete query if present or {0, NULL}.
Definition coap_uri.h:77
coap_str_const_t host
The host part of the URI.
Definition coap_uri.h:71