libcoap 4.3.4-develop-9f1418e
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
16#include "coap3/coap_internal.h"
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
295#if !defined(WITH_CONTIKI)
296static void
297update_port(coap_address_t *addr, uint16_t port, uint16_t default_port,
298 int update_port0) {
299 /* Client target port must be set if default of 0 */
300 if (port == 0 && update_port0)
301 port = default_port;
302
303 coap_address_set_port(addr, port);
304 return;
305}
306
307#ifdef HAVE_NETDB_H
308#include <netdb.h>
309#endif
310
311uint32_t
312coap_get_available_scheme_hint_bits(int have_pki_psk, int ws_check,
313 coap_proto_t use_unix_proto) {
314 uint32_t scheme_hint_bits = 0;
315 coap_uri_scheme_t scheme;
316
317 for (scheme = 0; scheme < COAP_URI_SCHEME_LAST; scheme++) {
318 switch (scheme) {
320 scheme_hint_bits |= 1 << scheme;
321 break;
323 if (!(coap_dtls_is_supported() && have_pki_psk))
324 continue;
325 scheme_hint_bits |= 1 << scheme;
326 break;
329 continue;
330 scheme_hint_bits |= 1 << scheme;
331 break;
333 if (!(coap_tls_is_supported() && have_pki_psk))
334 continue;
335 scheme_hint_bits |= 1 << scheme;
336 break;
338 if (!ws_check || !coap_ws_is_supported())
339 continue;
340 scheme_hint_bits |= 1 << scheme;
341 break;
343 if (!ws_check || !(coap_wss_is_supported() && have_pki_psk))
344 continue;
345 scheme_hint_bits |= 1 << scheme;
346 break;
350 default:
351 continue;
352 }
353 }
354
355 switch (use_unix_proto) {
356 /* For AF_UNIX, can only listen on a single endpoint */
357 case COAP_PROTO_UDP:
358 scheme_hint_bits = 1 << COAP_URI_SCHEME_COAP;
359 break;
360 case COAP_PROTO_TCP:
361 scheme_hint_bits = 1 << COAP_URI_SCHEME_COAP_TCP;
362 break;
363 case COAP_PROTO_DTLS:
364 scheme_hint_bits = 1 << COAP_URI_SCHEME_COAPS;
365 break;
366 case COAP_PROTO_TLS:
367 scheme_hint_bits = 1 << COAP_URI_SCHEME_COAPS_TCP;
368 break;
369 case COAP_PROTO_WS:
370 scheme_hint_bits = 1 << COAP_URI_SCHEME_COAP_WS;
371 break;
372 case COAP_PROTO_WSS:
373 scheme_hint_bits = 1 << COAP_URI_SCHEME_COAPS_WS;
374 break;
375 case COAP_PROTO_NONE: /* If use_unix_proto was not defined */
376 case COAP_PROTO_LAST:
377 default:
378 break;
379 }
380 return scheme_hint_bits;
381}
382
383static coap_addr_info_t *
385 coap_addr_info_t *info = NULL;
386 coap_proto_t proto = 0;
387
388 switch (scheme) {
390 proto = COAP_PROTO_UDP;
391 break;
394 return NULL;
395 proto = COAP_PROTO_DTLS;
396 break;
399 return NULL;
400 proto = COAP_PROTO_TCP;
401 break;
404 return NULL;
405 proto = COAP_PROTO_TLS;
406 break;
409 return NULL;
410 proto = COAP_PROTO_NONE;
411 break;
414 return NULL;
415 proto = COAP_PROTO_NONE;
416 break;
419 return NULL;
420 proto = COAP_PROTO_WS;
421 break;
424 return NULL;
425 proto = COAP_PROTO_WSS;
426 break;
428 default:
429 return NULL;
430 }
432 if (info == NULL)
433 return NULL;
434 info->next = NULL;
435 info->proto = proto;
436 info->scheme = scheme;
437
438 coap_address_init(&info->addr);
439 return info;
440}
441
442static void
444 uint16_t port, uint16_t secure_port, uint16_t ws_port,
445 uint16_t ws_secure_port,
446 coap_resolve_type_t type) {
447 switch (scheme) {
449 update_port(&info->addr, port, COAP_DEFAULT_PORT,
451 break;
453 update_port(&info->addr, secure_port, COAPS_DEFAULT_PORT,
455 break;
457 update_port(&info->addr, port, COAP_DEFAULT_PORT,
459 break;
461 update_port(&info->addr, secure_port, COAPS_DEFAULT_PORT,
463 break;
465 update_port(&info->addr, port, 80,
467 break;
469 update_port(&info->addr, secure_port, 443,
471 break;
473 update_port(&info->addr, ws_port, 80,
475 break;
477 update_port(&info->addr, ws_secure_port, 443,
479 break;
481 default:
482 break;
483 }
484}
485
488 uint16_t port,
489 uint16_t secure_port,
490 uint16_t ws_port,
491 uint16_t ws_secure_port,
492 int ai_hints_flags,
493 int scheme_hint_bits,
494 coap_resolve_type_t type) {
495#if !defined(RIOT_VERSION)
496
497 struct addrinfo *res, *ainfo;
498 struct addrinfo hints;
499 static char addrstr[256];
500 int error;
501 coap_addr_info_t *info = NULL;
502 coap_addr_info_t *info_prev = NULL;
503 coap_addr_info_t *info_list = NULL;
504 coap_addr_info_t *info_tmp;
505 coap_uri_scheme_t scheme;
506
507#if COAP_AF_UNIX_SUPPORT
508 if (address && coap_host_is_unix_domain(address)) {
509 /* There can only be one unique filename entry for AF_UNIX */
510 if (address->length >= COAP_UNIX_PATH_MAX) {
511 coap_log_err("Unix Domain host too long\n");
512 return NULL;
513 }
514 /* Need to chose the first defined one in scheme_hint_bits */
515 for (scheme = 0; scheme < COAP_URI_SCHEME_LAST; scheme++) {
516 if (scheme_hint_bits & (1 << scheme)) {
517 break;
518 }
519 }
520 if (scheme == COAP_URI_SCHEME_LAST) {
521 return NULL;
522 }
523 info = get_coap_addr_info(scheme);
524 if (info == NULL) {
525 return NULL;
526 }
527
528 if (!coap_address_set_unix_domain(&info->addr, address->s,
529 address->length)) {
531 return NULL;
532 }
533 return info;
534 }
535#endif /* COAP_AF_UNIX_SUPPORT */
536
537 memset(addrstr, 0, sizeof(addrstr));
538 if (address && address->length)
539 memcpy(addrstr, address->s, address->length);
540 else
541 memcpy(addrstr, "localhost", 9);
542
543 memset((char *)&hints, 0, sizeof(hints));
544 hints.ai_socktype = 0;
545 hints.ai_family = AF_UNSPEC;
546 hints.ai_flags = ai_hints_flags;
547
548 error = getaddrinfo(addrstr, NULL, &hints, &res);
549
550 if (error != 0) {
551 coap_log_warn("getaddrinfo: %s\n", gai_strerror(error));
552 return NULL;
553 }
554
555 for (ainfo = res; ainfo != NULL; ainfo = ainfo->ai_next) {
556#if !defined(WITH_LWIP)
557 if (ainfo->ai_addrlen > (socklen_t)sizeof(info->addr.addr))
558 continue;
559#endif /* ! WITH_LWIP */
560
561 switch (ainfo->ai_family) {
562#if COAP_IPV4_SUPPORT
563 case AF_INET:
564#endif /* COAP_IPV4_SUPPORT */
565#if COAP_IPV6_SUPPORT
566 case AF_INET6:
567#endif /* COAP_IPV6_SUPPORT */
568 for (scheme = 0; scheme < COAP_URI_SCHEME_LAST; scheme++) {
569 if (scheme_hint_bits & (1 << scheme)) {
570 info = get_coap_addr_info(scheme);
571 if (info == NULL) {
572 continue;
573 }
574
575#if !defined(WITH_LWIP)
576 info->addr.size = (socklen_t)ainfo->ai_addrlen;
577 memcpy(&info->addr.addr, ainfo->ai_addr, ainfo->ai_addrlen);
578#else /* WITH_LWIP */
579 memset(&info->addr, 0, sizeof(info->addr));
580 switch (ainfo->ai_family) {
581#if COAP_IPV6_SUPPORT
582 struct sockaddr_in6 *sock6;
583#endif /* COAP_IPV6_SUPPORT */
584#if COAP_IPV4_SUPPORT
585 struct sockaddr_in *sock4;
586 case AF_INET:
587 sock4 = (struct sockaddr_in *)ainfo->ai_addr;
588 info->addr.port = ntohs(sock4->sin_port);
589 memcpy(&info->addr.addr, &sock4->sin_addr, 4);
590#if LWIP_IPV6
591 info->addr.addr.type = IPADDR_TYPE_V4;
592#endif /* LWIP_IPV6 */
593 break;
594#endif /* COAP_IPV4_SUPPORT */
595#if COAP_IPV6_SUPPORT
596 case AF_INET6:
597 sock6 = (struct sockaddr_in6 *)ainfo->ai_addr;
598 info->addr.port = ntohs(sock6->sin6_port);
599 memcpy(&info->addr.addr, &sock6->sin6_addr, 16);
600#if LWIP_IPV6 && LWIP_IPV4
601 info->addr.addr.type = IPADDR_TYPE_V6;
602#endif /* LWIP_IPV6 && LWIP_IPV4 */
603 break;
604#endif /* COAP_IPV6_SUPPORT */
605 default:
606 ;
607 }
608#endif /* WITH_LWIP */
609 update_coap_addr_port(scheme, info, port, secure_port, ws_port,
610 ws_secure_port, type);
611
612 /* Check there are no duplications */
613 info_tmp = info_list;
614 while (info_tmp) {
615 if (info_tmp->proto == info->proto &&
616 info_tmp->scheme == info->scheme &&
617 coap_address_equals(&info_tmp->addr, &info->addr)) {
618 break;
619 }
620 info_tmp = info_tmp->next;
621 }
622
623 if (info_tmp) {
624 /* Duplicate */
626 } else {
627 /* Need to return in same order as getaddrinfo() */
628 if (!info_prev) {
629 info_list = info;
630 info_prev = info;
631 } else {
632 info_prev->next = info;
633 info_prev = info;
634 }
635 }
636 }
637 }
638 break;
639 default:
640 break;
641 }
642 }
643
644 freeaddrinfo(res);
645 return info_list;
646#else /* RIOT_VERSION */
647#include "net/utils.h"
648#if COAP_IPV6_SUPPORT
649 ipv6_addr_t addr_ipv6;
650#endif /* COAP_IPV6_SUPPORT */
651#if COAP_IPV4_SUPPORT
652 ipv4_addr_t addr_ipv4;
653#endif /* COAP_IPV4_SUPPORT */
654 netif_t *netif = NULL;
655 coap_addr_info_t *info = NULL;
656 coap_addr_info_t *info_prev = NULL;
657 coap_addr_info_t *info_list = NULL;
658 coap_uri_scheme_t scheme;
659 (void)ai_hints_flags;
660 int family = AF_UNSPEC;
661
662 if (address == NULL || address->length == 0) {
663 memset(&addr_ipv6, 0, sizeof(addr_ipv6));
664#if COAP_IPV6_SUPPORT
665 family = AF_INET6;
666#else /* ! COAP_IPV6_SUPPORT */
667 family = AF_INET;
668#endif /* ! COAP_IPV6_SUPPORT */
669 } else {
670#if COAP_IPV6_SUPPORT
671 if (netutils_get_ipv6(&addr_ipv6, &netif, (const char *)address->s) >= 0) {
672 family = AF_INET6;
673 }
674#endif /* COAP_IPV6_SUPPORT */
675#if COAP_IPV4_SUPPORT
676 if (family == AF_UNSPEC &&
677 netutils_get_ipv4(&addr_ipv4, (const char *)address->s) >= 0) {
678 family = AF_INET;
679 }
680#endif /* COAP_IPV4_SUPPORT */
681 if (family == AF_UNSPEC) {
682 coap_log_err("coap_resolve_address_info: Unable to parse '%s'\n", address->s);
683 return NULL;
684 }
685 }
686 for (scheme = 0; scheme < COAP_URI_SCHEME_LAST; scheme++) {
687 if (scheme_hint_bits & (1 << scheme)) {
688 info = get_coap_addr_info(scheme);
689 if (info == NULL) {
690 continue;
691 }
692
693 /* Need to return in same order as getaddrinfo() */
694 if (!info_prev) {
695 info_list = info;
696 info_prev = info;
697 } else {
698 info_prev->next = info;
699 info_prev = info;
700 }
701
702 switch (family) {
703#if COAP_IPV6_SUPPORT
704 case AF_INET6:
705 info->addr.riot.family = AF_INET6;
706 memcpy(&info->addr.riot.addr.ipv6, &addr_ipv6,
707 sizeof(info->addr.riot.addr.ipv6));
708 info->addr.riot.netif = netif ? (uint32_t)netif_get_id(netif) : 0;
709 break;
710#endif /* ! COAP_IPV6_SUPPORT */
711#if COAP_IPV4_SUPPORT
712 case AF_INET:
713 info->addr.riot.family = AF_INET;
714 memcpy(&info->addr.riot.addr.ipv4, &addr_ipv4,
715 sizeof(info->addr.riot.addr.ipv4));
716 break;
717#endif /* ! COAP_IPV4_SUPPORT */
718 default:
719 break;
720 }
721
722 update_coap_addr_port(scheme, info, port, secure_port, ws_port,
723 ws_secure_port, type);
724 }
725 }
726 return info_list;
727#endif /* RIOT_VERSION */
728}
729#endif /* !WITH_CONTIKI */
730
731void
733 while (info) {
734 coap_addr_info_t *info_next = info->next;
735
737 info = info_next;
738 }
739}
740
741#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI) && !defined(RIOT_VERSION)
742void
744#if defined(WITH_LWIP) || defined(WITH_CONTIKI)
745 memcpy(dst, src, sizeof(coap_address_t));
746#else
747 memset(dst, 0, sizeof(coap_address_t));
748 dst->size = src->size;
749#if COAP_IPV6_SUPPORT
750 if (src->addr.sa.sa_family == AF_INET6) {
751 dst->addr.sin6.sin6_family = src->addr.sin6.sin6_family;
752 dst->addr.sin6.sin6_addr = src->addr.sin6.sin6_addr;
753 dst->addr.sin6.sin6_port = src->addr.sin6.sin6_port;
754 dst->addr.sin6.sin6_scope_id = src->addr.sin6.sin6_scope_id;
755 }
756#endif /* COAP_IPV6_SUPPORT */
757#if COAP_IPV4_SUPPORT && COAP_IPV6_SUPPORT
758 else
759#endif /* COAP_IPV4_SUPPORT && COAP_IPV6_SUPPORT */
760#if COAP_IPV4_SUPPORT
761 if (src->addr.sa.sa_family == AF_INET) {
762 dst->addr.sin = src->addr.sin;
763 }
764#endif /* COAP_IPV4_SUPPORT */
765 else {
766 memcpy(&dst->addr, &src->addr, src->size);
767 }
768#endif
769}
770
771int
773 /* need to compare only relevant parts of sockaddr_in6 */
774 switch (a->addr.sa.sa_family) {
775#if COAP_IPV4_SUPPORT
776 case AF_INET:
777 return a->addr.sin.sin_addr.s_addr == INADDR_ANY;
778#endif /* COAP_IPV4_SUPPORT */
779#if COAP_IPV6_SUPPORT
780 case AF_INET6:
781 return memcmp(&in6addr_any,
782 &a->addr.sin6.sin6_addr,
783 sizeof(in6addr_any)) == 0;
784#endif /* COAP_IPV6_SUPPORT */
785 default:
786 ;
787 }
788
789 return 0;
790}
791#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).
Definition: coap_address.c:62
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.
Definition: coap_address.c:263
#define COAP_BCST_REFRESH_SECS
Definition: coap_address.c:154
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().
Definition: coap_address.c:732
int coap_is_af_unix(const coap_address_t *a)
Checks if given address a denotes a AF_UNIX address.
Definition: coap_address.c:109
int coap_is_bcast(const coap_address_t *a)
Checks if given address a denotes a broadcast address.
Definition: coap_address.c:164
void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
Definition: coap_address.c:253
int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
Definition: coap_address.c:119
int _coap_address_isany_impl(const coap_address_t *a)
Definition: coap_address.c:772
uint16_t coap_address_get_port(const coap_address_t *addr)
Returns the port from addr in host byte order.
Definition: coap_address.c:44
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...
Definition: coap_address.c:312
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...
Definition: coap_address.c:487
#define COAP_BCST_CNT
Definition: coap_address.c:149
void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
Definition: coap_address.c:743
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)
Definition: coap_address.c:443
int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
Compares given address objects a and b.
Definition: coap_address.c:81
static void update_port(coap_address_t *addr, uint16_t port, uint16_t default_port, int update_port0)
Definition: coap_address.c:297
static coap_addr_info_t * get_coap_addr_info(coap_uri_scheme_t scheme)
Definition: coap_address.c:384
coap_resolve_type_t
coap_resolve_type_t values
Definition: coap_address.h:205
@ COAP_RESOLVE_TYPE_LOCAL
local side of session
Definition: coap_address.h:206
#define COAP_UNIX_PATH_MAX
Definition: coap_address.h:140
Pulls together all the internal only header files.
@ COAP_STRING
Definition: coap_mem.h:38
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_tcp_is_supported(void)
Check whether TCP is available.
Definition: coap_tcp.c:20
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:386
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.
int coap_tls_is_supported(void)
Check whether TLS is available.
Definition: coap_notls.c:28
int coap_dtls_is_supported(void)
Check whether DTLS is available.
Definition: coap_notls.c:23
#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_ws_is_supported(void)
Check whether WebSockets is available.
Definition: coap_ws.c:932
int coap_wss_is_supported(void)
Check whether Secure WebSockets is available.
Definition: coap_ws.c:937
Resolved addresses information.
Definition: coap_address.h:179
coap_uri_scheme_t scheme
CoAP scheme to use.
Definition: coap_address.h:181
coap_proto_t proto
CoAP protocol to use.
Definition: coap_address.h:182
struct coap_addr_info_t * next
Next entry in the chain.
Definition: coap_address.h:180
coap_address_t addr
The address to connect / bind to.
Definition: coap_address.h:183
Multi-purpose address abstraction.
Definition: coap_address.h:148
socklen_t size
size of addr
Definition: coap_address.h:149
struct sockaddr_in sin
Definition: coap_address.h:152
struct coap_sockaddr_un cun
Definition: coap_address.h:154
struct sockaddr_in6 sin6
Definition: coap_address.h:153
struct sockaddr sa
Definition: coap_address.h:151
union coap_address_t::@0 addr
char sun_path[COAP_UNIX_PATH_MAX]
Definition: coap_address.h:144
sa_family_t sun_family
Definition: coap_address.h:143
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