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