libcoap 4.3.4
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-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 if (address->length >= sizeof(addrstr)) {
474 coap_log_warn("Host name too long (%zu > 255)\n", address->length);
475 return NULL;
476 }
477 memcpy(addrstr, address->s, address->length);
478 } else {
479 memcpy(addrstr, "localhost", 9);
480 }
481
482 memset((char *)&hints, 0, sizeof(hints));
483 hints.ai_socktype = 0;
484 hints.ai_family = AF_UNSPEC;
485 hints.ai_flags = ai_hints_flags;
486
487 error = getaddrinfo(addrstr, NULL, &hints, &res);
488
489 if (error != 0) {
490 coap_log_warn("getaddrinfo: %s\n", gai_strerror(error));
491 return NULL;
492 }
493
494 for (ainfo = res; ainfo != NULL; ainfo = ainfo->ai_next) {
495#if !defined(WITH_LWIP)
496 if (ainfo->ai_addrlen > (socklen_t)sizeof(info->addr.addr))
497 continue;
498#endif /* ! WITH_LWIP */
499
500 switch (ainfo->ai_family) {
501#if COAP_IPV4_SUPPORT
502 case AF_INET:
503#endif /* COAP_IPV4_SUPPORT */
504#if COAP_IPV6_SUPPORT
505 case AF_INET6:
506#endif /* COAP_IPV6_SUPPORT */
507 for (scheme = 0; scheme < COAP_URI_SCHEME_LAST; scheme++) {
508 if (scheme_hint_bits & (1 << scheme)) {
509 switch (scheme) {
511 proto = COAP_PROTO_UDP;
512 break;
515 continue;
516 proto = COAP_PROTO_DTLS;
517 break;
520 continue;
521 proto = COAP_PROTO_TCP;
522 break;
525 continue;
526 proto = COAP_PROTO_TLS;
527 break;
530 continue;
531 proto = COAP_PROTO_NONE;
532 break;
535 continue;
536 proto = COAP_PROTO_NONE;
537 break;
540 continue;
541 proto = COAP_PROTO_WS;
542 break;
545 continue;
546 proto = COAP_PROTO_WSS;
547 break;
549 default:
550 continue;
551 }
552
554 if (info == NULL) {
555 freeaddrinfo(res);
556 /* malloc failure - return what we have so far */
557 return info_list;
558 }
559
560 info->next = NULL;
561 info->scheme = scheme;
562 info->proto = proto;
563 coap_address_init(&info->addr);
564#if !defined(WITH_LWIP)
565 info->addr.size = (socklen_t)ainfo->ai_addrlen;
566 memcpy(&info->addr.addr, ainfo->ai_addr, ainfo->ai_addrlen);
567#else /* WITH_LWIP */
568 memset(&info->addr, 0, sizeof(info->addr));
569 switch (ainfo->ai_family) {
570#if COAP_IPV6_SUPPORT
571 struct sockaddr_in6 *sock6;
572#endif /* COAP_IPV6_SUPPORT */
573#if COAP_IPV4_SUPPORT
574 struct sockaddr_in *sock4;
575 case AF_INET:
576 sock4 = (struct sockaddr_in *)ainfo->ai_addr;
577 info->addr.port = ntohs(sock4->sin_port);
578 memcpy(&info->addr.addr, &sock4->sin_addr, 4);
579#if LWIP_IPV6
580 info->addr.addr.type = IPADDR_TYPE_V4;
581#endif /* LWIP_IPV6 */
582 break;
583#endif /* COAP_IPV4_SUPPORT */
584#if COAP_IPV6_SUPPORT
585 case AF_INET6:
586 sock6 = (struct sockaddr_in6 *)ainfo->ai_addr;
587 info->addr.port = ntohs(sock6->sin6_port);
588 memcpy(&info->addr.addr, &sock6->sin6_addr, 16);
589#if LWIP_IPV6 && LWIP_IPV4
590 info->addr.addr.type = IPADDR_TYPE_V6;
591#endif /* LWIP_IPV6 && LWIP_IPV4 */
592 break;
593#endif /* COAP_IPV6_SUPPORT */
594 default:
595 ;
596 }
597#endif /* WITH_LWIP */
598 switch (scheme) {
600 update_port(&info->addr, port, COAP_DEFAULT_PORT,
602 break;
604 update_port(&info->addr, secure_port, COAPS_DEFAULT_PORT,
606 break;
608 update_port(&info->addr, port, COAP_DEFAULT_PORT,
610 break;
612 update_port(&info->addr, secure_port, COAPS_DEFAULT_PORT,
614 break;
616 update_port(&info->addr, port, 80,
618 break;
620 update_port(&info->addr, secure_port, 443,
622 break;
624 update_port(&info->addr, ws_port, 80,
626 break;
628 update_port(&info->addr, ws_secure_port, 443,
630 break;
632 default:
633 break;
634 }
635
636 /* Check there are no duplications */
637 info_tmp = info_list;
638 while (info_tmp) {
639 if (info_tmp->proto == info->proto &&
640 info_tmp->scheme == info->scheme &&
641 coap_address_equals(&info_tmp->addr, &info->addr)) {
642 break;
643 }
644 info_tmp = info_tmp->next;
645 }
646
647 if (info_tmp) {
648 /* Duplicate */
650 } else {
651 /* Need to return in same order as getaddrinfo() */
652 if (!info_prev) {
653 info_list = info;
654 info_prev = info;
655 } else {
656 info_prev->next = info;
657 info_prev = info;
658 }
659 }
660 }
661 }
662 break;
663 default:
664 break;
665 }
666 }
667
668 freeaddrinfo(res);
669 return info_list;
670#else /* RIOT_VERSION */
671#if COAP_IPV6_SUPPORT
672#include "net/utils.h"
673 ipv6_addr_t addr_ipv6;
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 coap_proto_t proto = 0;
680 (void)ai_hints_flags;
681
682 if (netutils_get_ipv6(&addr_ipv6, &netif, (const char *)address->s) >= 0) {
683 for (scheme = 0; scheme < COAP_URI_SCHEME_LAST; scheme++) {
684 if (scheme_hint_bits & (1 << scheme)) {
685 switch (scheme) {
687 proto = COAP_PROTO_UDP;
688 break;
691 continue;
692 proto = COAP_PROTO_DTLS;
693 break;
696 continue;
697 proto = COAP_PROTO_TCP;
698 break;
701 continue;
702 proto = COAP_PROTO_TLS;
703 break;
706 continue;
707 proto = COAP_PROTO_NONE;
708 break;
711 continue;
712 proto = COAP_PROTO_NONE;
713 break;
716 continue;
717 proto = COAP_PROTO_WS;
718 break;
721 continue;
722 proto = COAP_PROTO_WSS;
723 break;
725 default:
726 continue;
727 }
728
730 if (info == NULL) {
731 /* malloc failure - return what we have so far */
732 return info_list;
733 }
734 info->next = NULL;
735 /* Need to return in same order as getaddrinfo() */
736 if (!info_prev) {
737 info_list = info;
738 info_prev = info;
739 } else {
740 info_prev->next = info;
741 info_prev = info;
742 }
743
744 info->scheme = scheme;
745 info->proto = proto;
746 coap_address_init(&info->addr);
747 info->addr.size = sizeof(struct sockaddr_in6);
748 info->addr.addr.sin6.sin6_family = AF_INET6;
749 memcpy(&info->addr.addr.sin6.sin6_addr, &addr_ipv6,
750 sizeof(info->addr.addr.sin6.sin6_addr));
751 info->addr.addr.sin6.sin6_scope_id = (uint32_t)netif_get_id(netif);
752 switch (scheme) {
754 update_port(&info->addr, port, COAP_DEFAULT_PORT,
756 break;
758 update_port(&info->addr, secure_port, COAPS_DEFAULT_PORT,
760 break;
762 update_port(&info->addr, port, COAP_DEFAULT_PORT,
764 break;
766 update_port(&info->addr, secure_port, COAPS_DEFAULT_PORT,
768 break;
770 update_port(&info->addr, port, ws_port,
772 break;
774 update_port(&info->addr, secure_port, ws_secure_port,
776 break;
778 default:
779 break;
780 }
781 }
782 }
783 return info_list;
784 }
785#endif /* COAP_IPV6_SUPPORT */
786 return NULL;
787#endif /* RIOT_VERSION */
788}
789#endif /* !WITH_CONTIKI */
790
791void
793 while (info) {
794 coap_addr_info_t *info_next = info->next;
795
797 info = info_next;
798 }
799}
800
801#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
802void
804#if defined(WITH_LWIP) || defined(WITH_CONTIKI)
805 memcpy(dst, src, sizeof(coap_address_t));
806#else
807 memset(dst, 0, sizeof(coap_address_t));
808 dst->size = src->size;
809#if COAP_IPV6_SUPPORT
810 if (src->addr.sa.sa_family == AF_INET6) {
811 dst->addr.sin6.sin6_family = src->addr.sin6.sin6_family;
812 dst->addr.sin6.sin6_addr = src->addr.sin6.sin6_addr;
813 dst->addr.sin6.sin6_port = src->addr.sin6.sin6_port;
814 dst->addr.sin6.sin6_scope_id = src->addr.sin6.sin6_scope_id;
815 }
816#endif /* COAP_IPV6_SUPPORT */
817#if COAP_IPV4_SUPPORT && COAP_IPV6_SUPPORT
818 else
819#endif /* COAP_IPV4_SUPPORT && COAP_IPV6_SUPPORT */
820#if COAP_IPV4_SUPPORT
821 if (src->addr.sa.sa_family == AF_INET) {
822 dst->addr.sin = src->addr.sin;
823 }
824#endif /* COAP_IPV4_SUPPORT */
825 else {
826 memcpy(&dst->addr, &src->addr, src->size);
827 }
828#endif
829}
830
831int
833 /* need to compare only relevant parts of sockaddr_in6 */
834 switch (a->addr.sa.sa_family) {
835#if COAP_IPV4_SUPPORT
836 case AF_INET:
837 return a->addr.sin.sin_addr.s_addr == INADDR_ANY;
838#endif /* COAP_IPV4_SUPPORT */
839#if COAP_IPV6_SUPPORT
840 case AF_INET6:
841 return memcmp(&in6addr_any,
842 &a->addr.sin6.sin6_addr,
843 sizeof(in6addr_any)) == 0;
844#endif /* COAP_IPV6_SUPPORT */
845 default:
846 ;
847 }
848
849 return 0;
850}
851#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)
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)
coap_resolve_type_t
coap_resolve_type_t values
@ COAP_RESOLVE_TYPE_LOCAL
local side of session
#define COAP_UNIX_PATH_MAX
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:404
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.
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