libcoap 4.3.5-develop-4fa3dfa
Loading...
Searching...
No Matches
coap_address.c
Go to the documentation of this file.
1/* coap_address.c -- representation of network addresses
2 *
3 * Copyright (C) 2015-2016,2019-2025 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
17
18#if !defined(WITH_CONTIKI) && !defined(WITH_LWIP) && !defined(RIOT_VERSION)
19#ifndef __ZEPHYR__
20#ifdef HAVE_ARPA_INET_H
21#include <arpa/inet.h>
22#endif
23#ifdef HAVE_NETINET_IN_H
24#include <netinet/in.h>
25#endif
26#ifdef HAVE_SYS_SOCKET_H
27#include <sys/socket.h>
28#endif
29#ifdef HAVE_NET_IF_H
30#include <net/if.h>
31#endif
32#ifdef HAVE_IFADDRS_H
33#include <ifaddrs.h>
34#endif
35#ifdef HAVE_WS2TCPIP_H
36#include <ws2tcpip.h>
37#endif
38#else /* __ZEPHYR__ */
39#ifndef IN_MULTICAST
40#define IN_MULTICAST(a) ((((long int) (a)) & 0xf0000000) == 0xe0000000)
41#endif
42#ifndef IN6_IS_ADDR_MULTICAST
43#define IN6_IS_ADDR_MULTICAST(a) ((a)->s6_addr[0] == 0xff)
44#endif
45#ifndef IN6_IS_ADDR_V4MAPPED
46#define IN6_IS_ADDR_V4MAPPED(a) \
47 ((((a)->s6_addr32[0]) == 0) && (((a)->s6_addr32[1]) == 0) && \
48 (((a)->s6_addr32[2]) == htonl(0xffff)))
49#endif
50#endif /* __ZEPHYR__ */
51
52#ifdef RIOT_VERSION
53/* FIXME */
54#define IN_MULTICAST(Address) (0)
55#endif /* RIOT_VERSION */
56
57uint16_t
59 assert(addr != NULL);
60 switch (addr->addr.sa.sa_family) {
61#if COAP_IPV4_SUPPORT
62 case AF_INET:
63 return ntohs(addr->addr.sin.sin_port);
64#endif /* COAP_IPV4_SUPPORT */
65#if COAP_IPV6_SUPPORT
66 case AF_INET6:
67 return ntohs(addr->addr.sin6.sin6_port);
68#endif /* COAP_IPV6_SUPPORT */
69 default: /* undefined */
70 ;
71 }
72 return 0;
73}
74
75void
77 assert(addr != NULL);
78 switch (addr->addr.sa.sa_family) {
79#if COAP_IPV4_SUPPORT
80 case AF_INET:
81 addr->addr.sin.sin_port = htons(port);
82 break;
83#endif /* COAP_IPV4_SUPPORT */
84#if COAP_IPV6_SUPPORT
85 case AF_INET6:
86 addr->addr.sin6.sin6_port = htons(port);
87 break;
88#endif /* COAP_IPV6_SUPPORT */
89 default: /* undefined */
90 ;
91 }
92}
93
94int
96 assert(a);
97 assert(b);
98
99 if (a->size != b->size || a->addr.sa.sa_family != b->addr.sa.sa_family)
100 return 0;
101
102 /* need to compare only relevant parts of sockaddr_in6 */
103 switch (a->addr.sa.sa_family) {
104#if COAP_IPV4_SUPPORT
105 case AF_INET:
106 return a->addr.sin.sin_port == b->addr.sin.sin_port &&
107 memcmp(&a->addr.sin.sin_addr, &b->addr.sin.sin_addr,
108 sizeof(struct in_addr)) == 0;
109#endif /* COAP_IPV4_SUPPORT */
110#if COAP_IPV6_SUPPORT
111 case AF_INET6:
112 return a->addr.sin6.sin6_port == b->addr.sin6.sin6_port &&
113 memcmp(&a->addr.sin6.sin6_addr, &b->addr.sin6.sin6_addr,
114 sizeof(struct in6_addr)) == 0;
115#endif /* COAP_IPV6_SUPPORT */
116#if COAP_AF_UNIX_SUPPORT
117 case AF_UNIX:
118 return memcmp(&a->addr.cun.sun_path, &b->addr.cun.sun_path,
119 sizeof(a->addr.cun.sun_path)) == 0;
120#endif /* COAP_AF_UNIX_SUPPORT */
121 default: /* fall through and signal error */
122 ;
123 }
124 return 0;
125}
126
127int
129#if COAP_AF_UNIX_SUPPORT
130 return a->addr.sa.sa_family == AF_UNIX;
131#else /* ! COAP_AF_UNIX_SUPPORT */
132 (void)a;
133 return 0;
134#endif /* ! COAP_AF_UNIX_SUPPORT */
135}
136
137int
139 if (!a)
140 return 0;
141
142 /* Treat broadcast in same way as multicast */
143 if (coap_is_bcast(a))
144 return 1;
145
146 switch (a->addr.sa.sa_family) {
147#if COAP_IPV4_SUPPORT
148 case AF_INET:
149 return IN_MULTICAST(ntohl(a->addr.sin.sin_addr.s_addr));
150#endif /* COAP_IPV4_SUPPORT */
151#if COAP_IPV6_SUPPORT
152 case AF_INET6:
153#if COAP_IPV4_SUPPORT
154 return IN6_IS_ADDR_MULTICAST(&a->addr.sin6.sin6_addr) ||
155 (IN6_IS_ADDR_V4MAPPED(&a->addr.sin6.sin6_addr) &&
156 IN_MULTICAST(ntohl(a->addr.sin6.sin6_addr.s6_addr[12])));
157#else /* ! COAP_IPV4_SUPPORT */
158 return a->addr.sin6.sin6_addr.s6_addr[0] == 0xff;
159#endif /* ! COAP_IPV4_SUPPORT */
160#endif /* COAP_IPV6_SUPPORT */
161 default: /* fall through and signal not multicast */
162 ;
163 }
164 return 0;
165}
166
167#ifndef COAP_BCST_CNT
168#define COAP_BCST_CNT 15
169#endif /* COAP_BCST_CNT */
170
171/* How frequently to refresh the list of valid IPv4 broadcast addresses */
172#ifndef COAP_BCST_REFRESH_SECS
173#define COAP_BCST_REFRESH_SECS 30
174#endif /* COAP_BCST_REFRESH_SECS */
175
176#if COAP_IPV4_SUPPORT && defined(HAVE_IFADDRS_H) && !defined(__ZEPHYR__)
177static int bcst_cnt = -1;
178static coap_tick_t last_refresh;
179static struct in_addr b_ipv4[COAP_BCST_CNT];
180#endif /* COAP_IPV4_SUPPORT && HAVE_IFADDRS_H && !defined(__ZEPHYR__) */
181
182int
184#if COAP_IPV4_SUPPORT
185 struct in_addr ipv4;
186#if defined(HAVE_IFADDRS_H) && !defined(__ZEPHYR__)
187 int i;
188 coap_tick_t now;
189#endif /* HAVE_IFADDRS_H && !defined(__ZEPHYR__) */
190#endif /* COAP_IPV4_SUPPORT */
191
192 if (!a)
193 return 0;
194
195 switch (a->addr.sa.sa_family) {
196#if COAP_IPV4_SUPPORT
197 case AF_INET:
198 ipv4.s_addr = a->addr.sin.sin_addr.s_addr;
199 break;
200#endif /* COAP_IPV4_SUPPORT */
201#if COAP_IPV6_SUPPORT
202 case AF_INET6:
203#if COAP_IPV4_SUPPORT
204 if (IN6_IS_ADDR_V4MAPPED(&a->addr.sin6.sin6_addr)) {
205 memcpy(&ipv4, &a->addr.sin6.sin6_addr.s6_addr[12], sizeof(ipv4));
206 break;
207 }
208#endif /* COAP_IPV4_SUPPORT */
209 /* IPv6 does not support broadcast */
210 return 0;
211#endif /* COAP_IPV6_SUPPORT */
212 default:
213 return 0;
214 }
215#if COAP_IPV4_SUPPORT
216#ifndef INADDR_BROADCAST
217#define INADDR_BROADCAST ((uint32_t)0xffffffffUL)
218#endif /* !INADDR_BROADCAST */
219 if (ipv4.s_addr == INADDR_BROADCAST)
220 return 1;
221
222#if defined(HAVE_IFADDRS_H) && !defined(__ZEPHYR__)
223 coap_ticks(&now);
224 if (bcst_cnt == -1 ||
225 (now - last_refresh) > (COAP_BCST_REFRESH_SECS * COAP_TICKS_PER_SECOND)) {
226 /* Determine the list of broadcast interfaces */
227 struct ifaddrs *ifa = NULL;
228 struct ifaddrs *ife;
229
230 if (getifaddrs(&ifa) != 0) {
231 coap_log_warn("coap_is_bcst: Cannot determine any broadcast addresses\n");
232 return 0;
233 }
234 bcst_cnt = 0;
235 last_refresh = now;
236 ife = ifa;
237 while (ife && bcst_cnt < COAP_BCST_CNT) {
238 if (ife->ifa_addr && ife->ifa_addr->sa_family == AF_INET &&
239 ife->ifa_flags & IFF_BROADCAST) {
240 struct in_addr netmask;
241
242 /*
243 * Sometimes the broadcast IP is set to the IP address, even though
244 * netmask is not set to 0xffffffff, so unsafe to use ifa_broadaddr.
245 */
246 netmask.s_addr = ((struct sockaddr_in *)ife->ifa_netmask)->sin_addr.s_addr;
247 if (netmask.s_addr != 0xffffffff) {
248 b_ipv4[bcst_cnt].s_addr = ((struct sockaddr_in *)ife->ifa_addr)->sin_addr.s_addr |
249 ~netmask.s_addr;
250 bcst_cnt++;
251 }
252 }
253 ife = ife->ifa_next;
254 }
255 if (ife) {
256 coap_log_warn("coap_is_bcst: Insufficient space for broadcast addresses\n");
257 }
258 freeifaddrs(ifa);
259 }
260 for (i = 0; i < bcst_cnt; i++) {
261 if (ipv4.s_addr == b_ipv4[i].s_addr)
262 return 1;
263 }
264#endif /* HAVE_IFADDRS_H && !defined(__ZEPHYR__) */
265 return 0;
266#endif /* COAP_IPV4_SUPPORT */
267}
268
269#endif /* !defined(WITH_CONTIKI) && !defined(WITH_LWIP) */
270
271void
273 assert(addr);
274 memset(addr, 0, sizeof(coap_address_t));
275#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI) && !defined(RIOT_VERSION)
276 /* lwip and Contiki have constant address sizes and don't need the .size part */
277 addr->size = sizeof(addr->addr);
278#endif
279}
280
281int
283 const uint8_t *host, size_t host_len) {
284#if COAP_AF_UNIX_SUPPORT
285 size_t i;
286 size_t ofs = 0;
287
288 coap_address_init(addr);
289 addr->addr.cun.sun_family = AF_UNIX;
290 for (i = 0; i < host_len; i++) {
291 if ((host_len - i) >= 3 && host[i] == '%' && host[i+1] == '2' &&
292 (host[i+2] == 'F' || host[i+2] == 'f')) {
293 addr->addr.cun.sun_path[ofs++] = '/';
294 i += 2;
295 } else {
296 addr->addr.cun.sun_path[ofs++] = host[i];
297 }
298 if (ofs == COAP_UNIX_PATH_MAX)
299 break;
300 }
301 if (ofs < COAP_UNIX_PATH_MAX)
302 addr->addr.cun.sun_path[ofs] = '\000';
303 else
304 addr->addr.cun.sun_path[ofs-1] = '\000';
305 return 1;
306#else /* ! COAP_AF_UNIX_SUPPORT */
307 (void)addr;
308 (void)host;
309 (void)host_len;
310 return 0;
311#endif /* ! COAP_AF_UNIX_SUPPORT */
312}
313
314static void
315update_port(coap_address_t *addr, uint16_t port, uint16_t default_port,
316 int update_port0) {
317 /* Client target port must be set if default of 0 */
318 if (port == 0 && update_port0)
319 port = default_port;
320
321 coap_address_set_port(addr, port);
322 return;
323}
324
325#ifdef HAVE_NETDB_H
326#include <netdb.h>
327#endif
328
329uint32_t
330coap_get_available_scheme_hint_bits(int have_pki_psk, int ws_check,
331 coap_proto_t use_unix_proto) {
332 uint32_t scheme_hint_bits = 0;
333 coap_uri_scheme_t scheme;
334
335 for (scheme = 0; scheme < COAP_URI_SCHEME_LAST; scheme++) {
336 switch (scheme) {
338 scheme_hint_bits |= 1 << scheme;
339 break;
341 if (!(coap_dtls_is_supported() && have_pki_psk))
342 continue;
343 scheme_hint_bits |= 1 << scheme;
344 break;
347 continue;
348 scheme_hint_bits |= 1 << scheme;
349 break;
351 if (!(coap_tls_is_supported() && have_pki_psk))
352 continue;
353 scheme_hint_bits |= 1 << scheme;
354 break;
356 if (!ws_check || !coap_ws_is_supported())
357 continue;
358 scheme_hint_bits |= 1 << scheme;
359 break;
361 if (!ws_check || !(coap_wss_is_supported() && have_pki_psk))
362 continue;
363 scheme_hint_bits |= 1 << scheme;
364 break;
368 default:
369 continue;
370 }
371 }
372
373 switch (use_unix_proto) {
374 /* For AF_UNIX, can only listen on a single endpoint */
375 case COAP_PROTO_UDP:
376 scheme_hint_bits = 1 << COAP_URI_SCHEME_COAP;
377 break;
378 case COAP_PROTO_TCP:
379 scheme_hint_bits = 1 << COAP_URI_SCHEME_COAP_TCP;
380 break;
381 case COAP_PROTO_DTLS:
382 scheme_hint_bits = 1 << COAP_URI_SCHEME_COAPS;
383 break;
384 case COAP_PROTO_TLS:
385 scheme_hint_bits = 1 << COAP_URI_SCHEME_COAPS_TCP;
386 break;
387 case COAP_PROTO_WS:
388 scheme_hint_bits = 1 << COAP_URI_SCHEME_COAP_WS;
389 break;
390 case COAP_PROTO_WSS:
391 scheme_hint_bits = 1 << COAP_URI_SCHEME_COAPS_WS;
392 break;
393 case COAP_PROTO_NONE: /* If use_unix_proto was not defined */
394 case COAP_PROTO_LAST:
395 default:
396 break;
397 }
398 return scheme_hint_bits;
399}
400
401static coap_addr_info_t *
403 coap_addr_info_t *info = NULL;
404 coap_proto_t proto = 0;
405
406 switch (scheme) {
408 proto = COAP_PROTO_UDP;
409 break;
412 return NULL;
413 proto = COAP_PROTO_DTLS;
414 break;
417 return NULL;
418 proto = COAP_PROTO_TCP;
419 break;
422 return NULL;
423 proto = COAP_PROTO_TLS;
424 break;
427 return NULL;
428 proto = COAP_PROTO_NONE;
429 break;
432 return NULL;
433 proto = COAP_PROTO_NONE;
434 break;
437 return NULL;
438 proto = COAP_PROTO_WS;
439 break;
442 return NULL;
443 proto = COAP_PROTO_WSS;
444 break;
446 default:
447 return NULL;
448 }
450 if (info == NULL)
451 return NULL;
452 info->next = NULL;
453 info->proto = proto;
454 info->scheme = scheme;
455
456 coap_address_init(&info->addr);
457 return info;
458}
459
460static void
462 uint16_t port, uint16_t secure_port, uint16_t ws_port,
463 uint16_t ws_secure_port,
464 coap_resolve_type_t type) {
465 switch (scheme) {
467 update_port(&info->addr, port, COAP_DEFAULT_PORT,
469 break;
471 update_port(&info->addr, secure_port, COAPS_DEFAULT_PORT,
473 break;
475 update_port(&info->addr, port, COAP_DEFAULT_PORT,
477 break;
479 update_port(&info->addr, secure_port, COAPS_DEFAULT_PORT,
481 break;
483 update_port(&info->addr, port, 80,
485 break;
487 update_port(&info->addr, secure_port, 443,
489 break;
491 update_port(&info->addr, ws_port, 80,
493 break;
495 update_port(&info->addr, ws_secure_port, 443,
497 break;
499 default:
500 break;
501 }
502}
503
506 uint16_t port,
507 uint16_t secure_port,
508 uint16_t ws_port,
509 uint16_t ws_secure_port,
510 int ai_hints_flags,
511 int scheme_hint_bits,
512 coap_resolve_type_t type) {
513#if !defined(RIOT_VERSION) && !defined(WITH_CONTIKI)
514
515 struct addrinfo *res, *ainfo;
516 struct addrinfo hints;
517 static char addrstr[256];
518 int error;
519 coap_addr_info_t *info = NULL;
520 coap_addr_info_t *info_prev = NULL;
521 coap_addr_info_t *info_list = NULL;
522 coap_addr_info_t *info_tmp;
523 coap_uri_scheme_t scheme;
524
525#if COAP_AF_UNIX_SUPPORT
526 if (address && coap_host_is_unix_domain(address)) {
527 /* There can only be one unique filename entry for AF_UNIX */
528 if (address->length >= COAP_UNIX_PATH_MAX) {
529 coap_log_err("Unix Domain host too long\n");
530 return NULL;
531 }
532 /* Need to chose the first defined one in scheme_hint_bits */
533 for (scheme = 0; scheme < COAP_URI_SCHEME_LAST; scheme++) {
534 if (scheme_hint_bits & (1 << scheme)) {
535 break;
536 }
537 }
538 if (scheme == COAP_URI_SCHEME_LAST) {
539 return NULL;
540 }
541 info = get_coap_addr_info(scheme);
542 if (info == NULL) {
543 return NULL;
544 }
545
546 if (!coap_address_set_unix_domain(&info->addr, address->s,
547 address->length)) {
549 return NULL;
550 }
551 return info;
552 }
553#endif /* COAP_AF_UNIX_SUPPORT */
554
555 memset(addrstr, 0, sizeof(addrstr));
556 if (address && address->length)
557 memcpy(addrstr, address->s, address->length);
558 else
559 memcpy(addrstr, "localhost", 9);
560
561 memset((char *)&hints, 0, sizeof(hints));
562 hints.ai_socktype = 0;
563 hints.ai_family = AF_UNSPEC;
564 hints.ai_flags = ai_hints_flags;
565
566 error = getaddrinfo(addrstr, NULL, &hints, &res);
567
568 if (error != 0) {
569 coap_log_warn("getaddrinfo: %s: %s\n", addrstr, gai_strerror(error));
570 return NULL;
571 }
572
573 for (ainfo = res; ainfo != NULL; ainfo = ainfo->ai_next) {
574#if !defined(WITH_LWIP)
575 if (ainfo->ai_addrlen > (socklen_t)sizeof(info->addr.addr))
576 continue;
577#endif /* ! WITH_LWIP */
578
579 switch (ainfo->ai_family) {
580#if COAP_IPV4_SUPPORT
581 case AF_INET:
582#endif /* COAP_IPV4_SUPPORT */
583#if COAP_IPV6_SUPPORT
584 case AF_INET6:
585#endif /* COAP_IPV6_SUPPORT */
586 for (scheme = 0; scheme < COAP_URI_SCHEME_LAST; scheme++) {
587 if (scheme_hint_bits & (1 << scheme)) {
588 info = get_coap_addr_info(scheme);
589 if (info == NULL) {
590 continue;
591 }
592
593#if !defined(WITH_LWIP)
594 info->addr.size = (socklen_t)ainfo->ai_addrlen;
595 memcpy(&info->addr.addr, ainfo->ai_addr, ainfo->ai_addrlen);
596#else /* WITH_LWIP */
597 memset(&info->addr, 0, sizeof(info->addr));
598 switch (ainfo->ai_family) {
599#if COAP_IPV6_SUPPORT
600 struct sockaddr_in6 *sock6;
601#endif /* COAP_IPV6_SUPPORT */
602#if COAP_IPV4_SUPPORT
603 struct sockaddr_in *sock4;
604 case AF_INET:
605 sock4 = (struct sockaddr_in *)ainfo->ai_addr;
606 info->addr.port = ntohs(sock4->sin_port);
607 memcpy(&info->addr.addr, &sock4->sin_addr, 4);
608#if LWIP_IPV6
609 info->addr.addr.type = IPADDR_TYPE_V4;
610#endif /* LWIP_IPV6 */
611 break;
612#endif /* COAP_IPV4_SUPPORT */
613#if COAP_IPV6_SUPPORT
614 case AF_INET6:
615 sock6 = (struct sockaddr_in6 *)ainfo->ai_addr;
616 info->addr.port = ntohs(sock6->sin6_port);
617 memcpy(&info->addr.addr, &sock6->sin6_addr, 16);
618#if LWIP_IPV6 && LWIP_IPV4
619 info->addr.addr.type = IPADDR_TYPE_V6;
620#endif /* LWIP_IPV6 && LWIP_IPV4 */
621 break;
622#endif /* COAP_IPV6_SUPPORT */
623 default:
624 ;
625 }
626#endif /* WITH_LWIP */
627 update_coap_addr_port(scheme, info, port, secure_port, ws_port,
628 ws_secure_port, type);
629
630 /* Check there are no duplications */
631 info_tmp = info_list;
632 while (info_tmp) {
633 if (info_tmp->proto == info->proto &&
634 info_tmp->scheme == info->scheme &&
635 coap_address_equals(&info_tmp->addr, &info->addr)) {
636 break;
637 }
638 info_tmp = info_tmp->next;
639 }
640
641 if (info_tmp) {
642 /* Duplicate */
644 } else {
645 /* Need to return in same order as getaddrinfo() */
646 if (!info_prev) {
647 info_list = info;
648 info_prev = info;
649 } else {
650 info_prev->next = info;
651 info_prev = info;
652 }
653 }
654 }
655 }
656 break;
657 default:
658 break;
659 }
660 }
661
662 freeaddrinfo(res);
663 return info_list;
664
665#elif defined(RIOT_VERSION)
666
667#include "net/utils.h"
668#if COAP_IPV6_SUPPORT
669 ipv6_addr_t addr_ipv6;
670#endif /* COAP_IPV6_SUPPORT */
671#if COAP_IPV4_SUPPORT
672 ipv4_addr_t addr_ipv4;
673#endif /* COAP_IPV4_SUPPORT */
674 netif_t *netif = NULL;
675 coap_addr_info_t *info = NULL;
676 coap_addr_info_t *info_prev = NULL;
677 coap_addr_info_t *info_list = NULL;
678 coap_uri_scheme_t scheme;
679 (void)ai_hints_flags;
680 int family = AF_UNSPEC;
681
682 if (address == NULL || address->length == 0) {
683 memset(&addr_ipv6, 0, sizeof(addr_ipv6));
684#if COAP_IPV6_SUPPORT
685 family = AF_INET6;
686#else /* ! COAP_IPV6_SUPPORT */
687 family = AF_INET;
688#endif /* ! COAP_IPV6_SUPPORT */
689 } else {
690#if COAP_IPV6_SUPPORT
691 if (netutils_get_ipv6(&addr_ipv6, &netif, (const char *)address->s) >= 0) {
692 family = AF_INET6;
693 }
694#endif /* COAP_IPV6_SUPPORT */
695#if COAP_IPV4_SUPPORT
696 if (family == AF_UNSPEC &&
697 netutils_get_ipv4(&addr_ipv4, (const char *)address->s) >= 0) {
698 family = AF_INET;
699 }
700#endif /* COAP_IPV4_SUPPORT */
701 if (family == AF_UNSPEC) {
702 coap_log_err("coap_resolve_address_info: Unable to parse '%s'\n", address->s);
703 return NULL;
704 }
705 }
706 for (scheme = 0; scheme < COAP_URI_SCHEME_LAST; scheme++) {
707 if (scheme_hint_bits & (1 << scheme)) {
708 info = get_coap_addr_info(scheme);
709 if (info == NULL) {
710 continue;
711 }
712
713 /* Need to return in same order as getaddrinfo() */
714 if (!info_prev) {
715 info_list = info;
716 info_prev = info;
717 } else {
718 info_prev->next = info;
719 info_prev = info;
720 }
721
722 switch (family) {
723#if COAP_IPV6_SUPPORT
724 case AF_INET6:
725 info->addr.riot.family = AF_INET6;
726 memcpy(&info->addr.riot.addr.ipv6, &addr_ipv6,
727 sizeof(info->addr.riot.addr.ipv6));
728 info->addr.riot.netif = netif ? (uint32_t)netif_get_id(netif) : 0;
729 break;
730#endif /* ! COAP_IPV6_SUPPORT */
731#if COAP_IPV4_SUPPORT
732 case AF_INET:
733 info->addr.riot.family = AF_INET;
734 memcpy(&info->addr.riot.addr.ipv4, &addr_ipv4,
735 sizeof(info->addr.riot.addr.ipv4));
736 break;
737#endif /* ! COAP_IPV4_SUPPORT */
738 default:
739 break;
740 }
741
742 update_coap_addr_port(scheme, info, port, secure_port, ws_port,
743 ws_secure_port, type);
744 }
745 }
746 return info_list;
747
748#elif defined(WITH_CONTIKI)
749
750#include <os/net/ipv6/uiplib.h>
751 uip_ipaddr_t addr_ip;
752 coap_addr_info_t *info = NULL;
753 coap_addr_info_t *info_prev = NULL;
754 coap_addr_info_t *info_list = NULL;
755 coap_uri_scheme_t scheme;
756 int parsed_ip = 0;
757
758 (void)ai_hints_flags;
759
760 if (address == NULL || address->length == 0) {
761 memset(&addr_ip, 0, sizeof(addr_ip));
762 } else {
763#if COAP_IPV6_SUPPORT
764 if (uiplib_ip6addrconv((const char *)address->s, (uip_ip6addr_t *)&addr_ip) > 0) {
765 parsed_ip = 1;
766 }
767#endif /* COAP_IPV6_SUPPORT */
768#if COAP_IPV4_SUPPORT
769 if (family == AF_UNSPEC &&
770 uiplib_ip4addrconv((const char *)address->s, (uip_ip4addr_t *)&addr_ip) > 0) {
771 parsed_ip = 1;
772 }
773#endif /* COAP_IPV4_SUPPORT */
774 if (!parsed_ip) {
775 coap_log_err("coap_resolve_address_info: Unable to parse '%s'\n", address->s);
776 return NULL;
777 }
778 }
779 for (scheme = 0; scheme < COAP_URI_SCHEME_LAST; scheme++) {
780 if (scheme_hint_bits & (1 << scheme)) {
781 info = get_coap_addr_info(scheme);
782 if (info == NULL) {
783 continue;
784 }
785
786 /* Need to return in same order as getaddrinfo() */
787 if (!info_prev) {
788 info_list = info;
789 info_prev = info;
790 } else {
791 info_prev->next = info;
792 info_prev = info;
793 }
794
795 memcpy(&info->addr.addr, &addr_ip, sizeof(info->addr.addr));
796
797 update_coap_addr_port(scheme, info, port, secure_port, ws_port,
798 ws_secure_port, type);
799 }
800 }
801 return info_list;
802#else
803#bad OS type not supported
804 return NULL;
805#endif
806}
807
808void
810 while (info) {
811 coap_addr_info_t *info_next = info->next;
812
814 info = info_next;
815 }
816}
817
818#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI) && !defined(RIOT_VERSION)
819void
821#if defined(WITH_LWIP) || defined(WITH_CONTIKI)
822 memcpy(dst, src, sizeof(coap_address_t));
823#else
824 memset(dst, 0, sizeof(coap_address_t));
825 dst->size = src->size;
826#if COAP_IPV6_SUPPORT
827 if (src->addr.sa.sa_family == AF_INET6) {
828 dst->addr.sin6.sin6_family = src->addr.sin6.sin6_family;
829 dst->addr.sin6.sin6_addr = src->addr.sin6.sin6_addr;
830 dst->addr.sin6.sin6_port = src->addr.sin6.sin6_port;
831 dst->addr.sin6.sin6_scope_id = src->addr.sin6.sin6_scope_id;
832 }
833#endif /* COAP_IPV6_SUPPORT */
834#if COAP_IPV4_SUPPORT && COAP_IPV6_SUPPORT
835 else
836#endif /* COAP_IPV4_SUPPORT && COAP_IPV6_SUPPORT */
837#if COAP_IPV4_SUPPORT
838 if (src->addr.sa.sa_family == AF_INET) {
839 dst->addr.sin = src->addr.sin;
840 }
841#endif /* COAP_IPV4_SUPPORT */
842 else {
843 memcpy(&dst->addr, &src->addr, src->size);
844 }
845#endif
846}
847
848int
850 /* need to compare only relevant parts of sockaddr_in6 */
851 switch (a->addr.sa.sa_family) {
852#if COAP_IPV4_SUPPORT
853 case AF_INET:
854 return a->addr.sin.sin_addr.s_addr == INADDR_ANY;
855#endif /* COAP_IPV4_SUPPORT */
856#if COAP_IPV6_SUPPORT
857 case AF_INET6:
858 return memcmp(&in6addr_any,
859 &a->addr.sin6.sin6_addr,
860 sizeof(in6addr_any)) == 0;
861#endif /* COAP_IPV6_SUPPORT */
862 default:
863 ;
864 }
865
866 return 0;
867}
868#endif /* ! WITH_LWIP && ! WITH_CONTIKI */
void coap_address_set_port(coap_address_t *addr, uint16_t port)
Set the port field of addr to port (in host byte order).
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.
#define COAP_BCST_REFRESH_SECS
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_bcast(const coap_address_t *a)
Checks if given address a denotes a broadcast address.
void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
int _coap_address_isany_impl(const coap_address_t *a)
uint16_t coap_address_get_port(const coap_address_t *addr)
Returns the port from addr in host byte order.
uint32_t coap_get_available_scheme_hint_bits(int have_pki_psk, int ws_check, coap_proto_t use_unix_proto)
Determine and set up scheme_hint_bits for a server that can be used in a call to coap_resolve_address...
coap_addr_info_t * coap_resolve_address_info(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...
#define COAP_BCST_CNT
void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
static void update_coap_addr_port(coap_uri_scheme_t scheme, coap_addr_info_t *info, uint16_t port, uint16_t secure_port, uint16_t ws_port, uint16_t ws_secure_port, coap_resolve_type_t type)
int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
Compares given address objects a and b.
static void update_port(coap_address_t *addr, uint16_t port, uint16_t default_port, int update_port0)
static coap_addr_info_t * get_coap_addr_info(coap_uri_scheme_t scheme)
coap_resolve_type_t
coap_resolve_type_t values
@ COAP_RESOLVE_TYPE_LOCAL
local side of session
#define COAP_UNIX_PATH_MAX
Library specific build wrapper for coap_internal.h.
@ COAP_STRING
Definition coap_mem.h:39
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().
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:389
coap_uri_scheme_t
The scheme specifiers.
Definition coap_uri.h:28
@ COAP_URI_SCHEME_COAPS_WS
Definition coap_uri.h:36
@ COAP_URI_SCHEME_COAPS_TCP
Definition coap_uri.h:32
@ COAP_URI_SCHEME_COAPS
Definition coap_uri.h:30
@ COAP_URI_SCHEME_COAP_TCP
Definition coap_uri.h:31
@ COAP_URI_SCHEME_COAP_WS
Definition coap_uri.h:35
@ COAP_URI_SCHEME_HTTPS
Definition coap_uri.h:34
@ COAP_URI_SCHEME_COAP
Definition coap_uri.h:29
@ COAP_URI_SCHEME_LAST
Definition coap_uri.h:37
@ COAP_URI_SCHEME_HTTP
Definition coap_uri.h:33
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition coap_time.h:143
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition coap_time.h:158
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
#define coap_log_warn(...)
Definition coap_debug.h:102
#define coap_log_err(...)
Definition coap_debug.h:96
#define COAP_DEFAULT_PORT
Definition coap_pdu.h:37
coap_proto_t
CoAP protocol types.
Definition coap_pdu.h:313
#define COAPS_DEFAULT_PORT
Definition coap_pdu.h:38
@ COAP_PROTO_WS
Definition coap_pdu.h:319
@ COAP_PROTO_DTLS
Definition coap_pdu.h:316
@ COAP_PROTO_UDP
Definition coap_pdu.h:315
@ COAP_PROTO_NONE
Definition coap_pdu.h:314
@ COAP_PROTO_TLS
Definition coap_pdu.h:318
@ COAP_PROTO_WSS
Definition coap_pdu.h:320
@ COAP_PROTO_TCP
Definition coap_pdu.h:317
@ COAP_PROTO_LAST
Definition coap_pdu.h:321
int coap_tcp_is_supported(void)
Check whether TCP is available.
Definition coap_tcp.c:29
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:933
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:938
Resolved addresses information.
coap_uri_scheme_t scheme
CoAP scheme to use.
coap_proto_t proto
CoAP protocol to use.
struct coap_addr_info_t * next
Next entry in the chain.
coap_address_t addr
The address to connect / bind to.
Multi-purpose address abstraction.
socklen_t size
size of addr
struct sockaddr_in sin
struct coap_sockaddr_un cun
struct sockaddr_in6 sin6
struct sockaddr sa
union coap_address_t::@0 addr
char sun_path[COAP_UNIX_PATH_MAX]
sa_family_t sun_family
CoAP string data definition with const data.
Definition coap_str.h:46
const uint8_t * s
read-only string data
Definition coap_str.h:48
size_t length
length of string
Definition coap_str.h:47