libcoap 4.3.4
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-2023 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)
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 b_ipv4[bcst_cnt].s_addr = ((struct sockaddr_in *)ife->ifa_broadaddr)->sin_addr.s_addr;
222 bcst_cnt++;
223 }
224 ife = ife->ifa_next;
225 }
226 if (ife) {
227 coap_log_warn("coap_is_bcst: Insufficient space for broadcast addresses\n");
228 }
229 freeifaddrs(ifa);
230 }
231 for (i = 0; i < bcst_cnt; i++) {
232 if (ipv4.s_addr == b_ipv4[i].s_addr)
233 return 1;
234 }
235#endif /* HAVE_IFADDRS_H */
236 return 0;
237#endif /* COAP_IPV4_SUPPORT */
238}
239
240#endif /* !defined(WITH_CONTIKI) && !defined(WITH_LWIP) */
241
242void
244 assert(addr);
245 memset(addr, 0, sizeof(coap_address_t));
246#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
247 /* lwip and Contiki have constant address sizes and don't need the .size part */
248 addr->size = sizeof(addr->addr);
249#endif
250}
251
252int
254 const uint8_t *host, size_t host_len) {
255#if COAP_AF_UNIX_SUPPORT
256 size_t i;
257 size_t ofs = 0;
258
259 coap_address_init(addr);
260 addr->addr.cun.sun_family = AF_UNIX;
261 for (i = 0; i < host_len; i++) {
262 if ((host_len - i) >= 3 && host[i] == '%' && host[i+1] == '2' &&
263 (host[i+2] == 'F' || host[i+2] == 'f')) {
264 addr->addr.cun.sun_path[ofs++] = '/';
265 i += 2;
266 } else {
267 addr->addr.cun.sun_path[ofs++] = host[i];
268 }
269 if (ofs == COAP_UNIX_PATH_MAX)
270 break;
271 }
272 if (ofs < COAP_UNIX_PATH_MAX)
273 addr->addr.cun.sun_path[ofs] = '\000';
274 else
275 addr->addr.cun.sun_path[ofs-1] = '\000';
276 return 1;
277#else /* ! COAP_AF_UNIX_SUPPORT */
278 (void)addr;
279 (void)host;
280 (void)host_len;
281 return 0;
282#endif /* ! COAP_AF_UNIX_SUPPORT */
283}
284
285#if !defined(WITH_CONTIKI)
286static void
287update_port(coap_address_t *addr, uint16_t port, uint16_t default_port,
288 int update_port0) {
289 /* Client target port must be set if default of 0 */
290 if (port == 0 && update_port0)
291 port = default_port;
292
293 coap_address_set_port(addr, port);
294 return;
295}
296
297#ifdef HAVE_NETDB_H
298#include <netdb.h>
299#endif
300
301uint32_t
302coap_get_available_scheme_hint_bits(int have_pki_psk, int ws_check,
303 coap_proto_t use_unix_proto) {
304 uint32_t scheme_hint_bits = 0;
305 coap_uri_scheme_t scheme;
306
307 for (scheme = 0; scheme < COAP_URI_SCHEME_LAST; scheme++) {
308 switch (scheme) {
310 scheme_hint_bits |= 1 << scheme;
311 break;
313 if (!(coap_dtls_is_supported() && have_pki_psk))
314 continue;
315 scheme_hint_bits |= 1 << scheme;
316 break;
319 continue;
320 scheme_hint_bits |= 1 << scheme;
321 break;
323 if (!(coap_tls_is_supported() && have_pki_psk))
324 continue;
325 scheme_hint_bits |= 1 << scheme;
326 break;
328 if (!ws_check || !coap_ws_is_supported())
329 continue;
330 scheme_hint_bits |= 1 << scheme;
331 break;
333 if (!ws_check || !(coap_wss_is_supported() && have_pki_psk))
334 continue;
335 scheme_hint_bits |= 1 << scheme;
336 break;
340 default:
341 continue;
342 }
343 }
344
345 switch (use_unix_proto) {
346 /* For AF_UNIX, can only listen on a single endpoint */
347 case COAP_PROTO_UDP:
348 scheme_hint_bits = 1 << COAP_URI_SCHEME_COAP;
349 break;
350 case COAP_PROTO_TCP:
351 scheme_hint_bits = 1 << COAP_URI_SCHEME_COAP_TCP;
352 break;
353 case COAP_PROTO_DTLS:
354 scheme_hint_bits = 1 << COAP_URI_SCHEME_COAPS;
355 break;
356 case COAP_PROTO_TLS:
357 scheme_hint_bits = 1 << COAP_URI_SCHEME_COAPS_TCP;
358 break;
359 case COAP_PROTO_WS:
360 scheme_hint_bits = 1 << COAP_URI_SCHEME_COAP_WS;
361 break;
362 case COAP_PROTO_WSS:
363 scheme_hint_bits = 1 << COAP_URI_SCHEME_COAPS_WS;
364 break;
365 case COAP_PROTO_NONE: /* If use_unix_proto was not defined */
366 case COAP_PROTO_LAST:
367 default:
368 break;
369 }
370 return scheme_hint_bits;
371}
372
375 uint16_t port,
376 uint16_t secure_port,
377 uint16_t ws_port,
378 uint16_t ws_secure_port,
379 int ai_hints_flags,
380 int scheme_hint_bits,
381 coap_resolve_type_t type) {
382#if !defined(RIOT_VERSION)
383
384 struct addrinfo *res, *ainfo;
385 struct addrinfo hints;
386 static char addrstr[256];
387 int error;
388 coap_addr_info_t *info = NULL;
389 coap_addr_info_t *info_prev = NULL;
390 coap_addr_info_t *info_list = NULL;
391 coap_addr_info_t *info_tmp;
392 coap_uri_scheme_t scheme;
393 coap_proto_t proto = 0;
394
395#if COAP_AF_UNIX_SUPPORT
396 if (address && coap_host_is_unix_domain(address)) {
397 /* There can only be one unique filename entry for AF_UNIX */
398 if (address->length >= COAP_UNIX_PATH_MAX) {
399 coap_log_err("Unix Domain host too long\n");
400 return NULL;
401 }
402 /* Need to chose the first defined one in scheme_hint_bits */
403 for (scheme = 0; scheme < COAP_URI_SCHEME_LAST; scheme++) {
404 if (scheme_hint_bits & (1 << scheme)) {
405 break;
406 }
407 }
408 if (scheme == COAP_URI_SCHEME_LAST) {
409 return NULL;
410 }
411 switch (scheme) {
413 proto = COAP_PROTO_UDP;
414 break;
417 return NULL;
418 proto = COAP_PROTO_DTLS;
419 break;
422 return NULL;
423 proto = COAP_PROTO_TCP;
424 break;
427 return NULL;
428 proto = COAP_PROTO_TLS;
429 break;
432 return NULL;
433 proto = COAP_PROTO_NONE;
434 break;
437 return NULL;
438 proto = COAP_PROTO_NONE;
439 break;
442 return NULL;
443 proto = COAP_PROTO_WS;
444 break;
447 return NULL;
448 proto = COAP_PROTO_WSS;
449 break;
451 default:
452 return NULL;
453 }
455 if (info == NULL)
456 return NULL;
457 info->next = NULL;
458 info->proto = proto;
459 info->scheme = scheme;
460
461 coap_address_init(&info->addr);
462 if (!coap_address_set_unix_domain(&info->addr, address->s,
463 address->length)) {
465 return NULL;
466 }
467 return info;
468 }
469#endif /* COAP_AF_UNIX_SUPPORT */
470
471 memset(addrstr, 0, sizeof(addrstr));
472 if (address && address->length)
473 memcpy(addrstr, address->s, address->length);
474 else
475 memcpy(addrstr, "localhost", 9);
476
477 memset((char *)&hints, 0, sizeof(hints));
478 hints.ai_socktype = 0;
479 hints.ai_family = AF_UNSPEC;
480 hints.ai_flags = ai_hints_flags;
481
482 error = getaddrinfo(addrstr, NULL, &hints, &res);
483
484 if (error != 0) {
485 coap_log_warn("getaddrinfo: %s\n", gai_strerror(error));
486 return NULL;
487 }
488
489 for (ainfo = res; ainfo != NULL; ainfo = ainfo->ai_next) {
490#if !defined(WITH_LWIP)
491 if (ainfo->ai_addrlen > (socklen_t)sizeof(info->addr.addr))
492 continue;
493#endif /* ! WITH_LWIP */
494
495 switch (ainfo->ai_family) {
496#if COAP_IPV4_SUPPORT
497 case AF_INET:
498#endif /* COAP_IPV4_SUPPORT */
499#if COAP_IPV6_SUPPORT
500 case AF_INET6:
501#endif /* COAP_IPV6_SUPPORT */
502 for (scheme = 0; scheme < COAP_URI_SCHEME_LAST; scheme++) {
503 if (scheme_hint_bits & (1 << scheme)) {
504 switch (scheme) {
506 proto = COAP_PROTO_UDP;
507 break;
510 continue;
511 proto = COAP_PROTO_DTLS;
512 break;
515 continue;
516 proto = COAP_PROTO_TCP;
517 break;
520 continue;
521 proto = COAP_PROTO_TLS;
522 break;
525 continue;
526 proto = COAP_PROTO_NONE;
527 break;
530 continue;
531 proto = COAP_PROTO_NONE;
532 break;
535 continue;
536 proto = COAP_PROTO_WS;
537 break;
540 continue;
541 proto = COAP_PROTO_WSS;
542 break;
544 default:
545 continue;
546 }
547
549 if (info == NULL) {
550 freeaddrinfo(res);
551 /* malloc failure - return what we have so far */
552 return info_list;
553 }
554
555 info->next = NULL;
556 info->scheme = scheme;
557 info->proto = proto;
558 coap_address_init(&info->addr);
559#if !defined(WITH_LWIP)
560 info->addr.size = (socklen_t)ainfo->ai_addrlen;
561 memcpy(&info->addr.addr, ainfo->ai_addr, ainfo->ai_addrlen);
562#else /* WITH_LWIP */
563 memset(&info->addr, 0, sizeof(info->addr));
564 switch (ainfo->ai_family) {
565#if COAP_IPV6_SUPPORT
566 struct sockaddr_in6 *sock6;
567#endif /* COAP_IPV6_SUPPORT */
568#if COAP_IPV4_SUPPORT
569 struct sockaddr_in *sock4;
570 case AF_INET:
571 sock4 = (struct sockaddr_in *)ainfo->ai_addr;
572 info->addr.port = ntohs(sock4->sin_port);
573 memcpy(&info->addr.addr, &sock4->sin_addr, 4);
574#if LWIP_IPV6
575 info->addr.addr.type = IPADDR_TYPE_V4;
576#endif /* LWIP_IPV6 */
577 break;
578#endif /* COAP_IPV4_SUPPORT */
579#if COAP_IPV6_SUPPORT
580 case AF_INET6:
581 sock6 = (struct sockaddr_in6 *)ainfo->ai_addr;
582 info->addr.port = ntohs(sock6->sin6_port);
583 memcpy(&info->addr.addr, &sock6->sin6_addr, 16);
584#if LWIP_IPV6 && LWIP_IPV4
585 info->addr.addr.type = IPADDR_TYPE_V6;
586#endif /* LWIP_IPV6 && LWIP_IPV4 */
587 break;
588#endif /* COAP_IPV6_SUPPORT */
589 default:
590 ;
591 }
592#endif /* WITH_LWIP */
593 switch (scheme) {
595 update_port(&info->addr, port, COAP_DEFAULT_PORT,
597 break;
599 update_port(&info->addr, secure_port, COAPS_DEFAULT_PORT,
601 break;
603 update_port(&info->addr, port, COAP_DEFAULT_PORT,
605 break;
607 update_port(&info->addr, secure_port, COAPS_DEFAULT_PORT,
609 break;
611 update_port(&info->addr, port, 80,
613 break;
615 update_port(&info->addr, secure_port, 443,
617 break;
619 update_port(&info->addr, ws_port, 80,
621 break;
623 update_port(&info->addr, ws_secure_port, 443,
625 break;
627 default:
628 break;
629 }
630
631 /* Check there are no duplications */
632 info_tmp = info_list;
633 while (info_tmp) {
634 if (info_tmp->proto == info->proto &&
635 info_tmp->scheme == info->scheme &&
636 coap_address_equals(&info_tmp->addr, &info->addr)) {
637 break;
638 }
639 info_tmp = info_tmp->next;
640 }
641
642 if (info_tmp) {
643 /* Duplicate */
645 } else {
646 /* Need to return in same order as getaddrinfo() */
647 if (!info_prev) {
648 info_list = info;
649 info_prev = info;
650 } else {
651 info_prev->next = info;
652 info_prev = info;
653 }
654 }
655 }
656 }
657 break;
658 default:
659 break;
660 }
661 }
662
663 freeaddrinfo(res);
664 return info_list;
665#else /* RIOT_VERSION */
666#if COAP_IPV6_SUPPORT
667#include "net/utils.h"
668 ipv6_addr_t addr_ipv6;
669 netif_t *netif = NULL;
670 coap_addr_info_t *info = NULL;
671 coap_addr_info_t *info_prev = NULL;
672 coap_addr_info_t *info_list = NULL;
673 coap_uri_scheme_t scheme;
674 coap_proto_t proto = 0;
675 (void)ai_hints_flags;
676
677 if (netutils_get_ipv6(&addr_ipv6, &netif, (const char *)address->s) >= 0) {
678 for (scheme = 0; scheme < COAP_URI_SCHEME_LAST; scheme++) {
679 if (scheme_hint_bits & (1 << scheme)) {
680 switch (scheme) {
682 proto = COAP_PROTO_UDP;
683 break;
686 continue;
687 proto = COAP_PROTO_DTLS;
688 break;
691 continue;
692 proto = COAP_PROTO_TCP;
693 break;
696 continue;
697 proto = COAP_PROTO_TLS;
698 break;
701 continue;
702 proto = COAP_PROTO_NONE;
703 break;
706 continue;
707 proto = COAP_PROTO_NONE;
708 break;
711 continue;
712 proto = COAP_PROTO_WS;
713 break;
716 continue;
717 proto = COAP_PROTO_WSS;
718 break;
720 default:
721 continue;
722 }
723
725 if (info == NULL) {
726 /* malloc failure - return what we have so far */
727 return info_list;
728 }
729 info->next = NULL;
730 /* Need to return in same order as getaddrinfo() */
731 if (!info_prev) {
732 info_list = info;
733 info_prev = info;
734 } else {
735 info_prev->next = info;
736 info_prev = info;
737 }
738
739 info->scheme = scheme;
740 info->proto = proto;
741 coap_address_init(&info->addr);
742 info->addr.size = sizeof(struct sockaddr_in6);
743 info->addr.addr.sin6.sin6_family = AF_INET6;
744 memcpy(&info->addr.addr.sin6.sin6_addr, &addr_ipv6,
745 sizeof(info->addr.addr.sin6.sin6_addr));
746 info->addr.addr.sin6.sin6_scope_id = (uint32_t)netif_get_id(netif);
747 switch (scheme) {
749 update_port(&info->addr, port, COAP_DEFAULT_PORT,
751 break;
753 update_port(&info->addr, secure_port, COAPS_DEFAULT_PORT,
755 break;
757 update_port(&info->addr, port, COAP_DEFAULT_PORT,
759 break;
761 update_port(&info->addr, secure_port, COAPS_DEFAULT_PORT,
763 break;
765 update_port(&info->addr, port, ws_port,
767 break;
769 update_port(&info->addr, secure_port, ws_secure_port,
771 break;
773 default:
774 break;
775 }
776 }
777 }
778 return info_list;
779 }
780#endif /* COAP_IPV6_SUPPORT */
781 return NULL;
782#endif /* RIOT_VERSION */
783}
784#endif /* !WITH_CONTIKI */
785
786void
788 while (info) {
789 coap_addr_info_t *info_next = info->next;
790
792 info = info_next;
793 }
794}
795
796#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
797void
799#if defined(WITH_LWIP) || defined(WITH_CONTIKI)
800 memcpy(dst, src, sizeof(coap_address_t));
801#else
802 memset(dst, 0, sizeof(coap_address_t));
803 dst->size = src->size;
804#if COAP_IPV6_SUPPORT
805 if (src->addr.sa.sa_family == AF_INET6) {
806 dst->addr.sin6.sin6_family = src->addr.sin6.sin6_family;
807 dst->addr.sin6.sin6_addr = src->addr.sin6.sin6_addr;
808 dst->addr.sin6.sin6_port = src->addr.sin6.sin6_port;
809 dst->addr.sin6.sin6_scope_id = src->addr.sin6.sin6_scope_id;
810 }
811#endif /* COAP_IPV6_SUPPORT */
812#if COAP_IPV4_SUPPORT && COAP_IPV6_SUPPORT
813 else
814#endif /* COAP_IPV4_SUPPORT && COAP_IPV6_SUPPORT */
815#if COAP_IPV4_SUPPORT
816 if (src->addr.sa.sa_family == AF_INET) {
817 dst->addr.sin = src->addr.sin;
818 }
819#endif /* COAP_IPV4_SUPPORT */
820 else {
821 memcpy(&dst->addr, &src->addr, src->size);
822 }
823#endif
824}
825
826int
828 /* need to compare only relevant parts of sockaddr_in6 */
829 switch (a->addr.sa.sa_family) {
830#if COAP_IPV4_SUPPORT
831 case AF_INET:
832 return a->addr.sin.sin_addr.s_addr == INADDR_ANY;
833#endif /* COAP_IPV4_SUPPORT */
834#if COAP_IPV6_SUPPORT
835 case AF_INET6:
836 return memcmp(&in6addr_any,
837 &a->addr.sin6.sin6_addr,
838 sizeof(in6addr_any)) == 0;
839#endif /* COAP_IPV6_SUPPORT */
840 default:
841 ;
842 }
843
844 return 0;
845}
846#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:253
#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:787
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:243
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:827
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:302
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:374
#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:798
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:287
coap_resolve_type_t
coap_resolve_type_t values
Definition: coap_address.h:166
@ COAP_RESOLVE_TYPE_LOCAL
local side of session
Definition: coap_address.h:167
#define COAP_UNIX_PATH_MAX
Definition: coap_address.h:101
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:37
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:144
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition: coap_time.h:159
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:304
#define COAPS_DEFAULT_PORT
Definition: coap_pdu.h:38
@ COAP_PROTO_WS
Definition: coap_pdu.h:310
@ COAP_PROTO_DTLS
Definition: coap_pdu.h:307
@ COAP_PROTO_UDP
Definition: coap_pdu.h:306
@ COAP_PROTO_NONE
Definition: coap_pdu.h:305
@ COAP_PROTO_TLS
Definition: coap_pdu.h:309
@ COAP_PROTO_WSS
Definition: coap_pdu.h:311
@ COAP_PROTO_TCP
Definition: coap_pdu.h:308
@ COAP_PROTO_LAST
Definition: coap_pdu.h:312
int coap_ws_is_supported(void)
Check whether WebSockets is available.
Definition: coap_ws.c:935
int coap_wss_is_supported(void)
Check whether Secure WebSockets is available.
Definition: coap_ws.c:940
Resolved addresses information.
Definition: coap_address.h:140
coap_uri_scheme_t scheme
CoAP scheme to use.
Definition: coap_address.h:142
coap_proto_t proto
CoAP protocol to use.
Definition: coap_address.h:143
struct coap_addr_info_t * next
Next entry in the chain.
Definition: coap_address.h:141
coap_address_t addr
The address to connect / bind to.
Definition: coap_address.h:144
Multi-purpose address abstraction.
Definition: coap_address.h:109
socklen_t size
size of addr
Definition: coap_address.h:110
struct sockaddr_in sin
Definition: coap_address.h:113
struct coap_sockaddr_un cun
Definition: coap_address.h:115
struct sockaddr_in6 sin6
Definition: coap_address.h:114
struct sockaddr sa
Definition: coap_address.h:112
union coap_address_t::@0 addr
char sun_path[COAP_UNIX_PATH_MAX]
Definition: coap_address.h:105
sa_family_t sun_family
Definition: coap_address.h:104
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