39 static int ba2str(
const BTH_ADDR *ba,
char *str);
40 static int str2ba(
const char *str, BTH_ADDR *ba);
41 static int bachk(
const char *str);
43 #elif _POSIX_C_SOURCE >= 200112L 68 #if defined(_WIN32) || (defined(__linux__) \ 69 && defined(HAVE_BLUETOOTH_BLUETOOTH_H) \ 70 && defined(HAVE_BLUETOOTH_RFCOMM_H)) 80 WSASetLastError(WSAEINVAL);
84 const SOCKADDR_BTH *addr_bth = (
const SOCKADDR_BTH *)&addr->
addr;
85 if (
__unlikely(addr_bth->addressFamily != AF_BTH)) {
86 WSASetLastError(WSAEAFNOSUPPORT);
91 *port = addr_bth->port == BT_PORT_ANY ? 0 : addr_bth->port;
92 if (ba &&
__unlikely(ba2str(&addr_bth->btAddr, ba) < 0))
100 const struct sockaddr_rc *addr_rc =
101 (
const struct sockaddr_rc *)&addr->
addr;
102 if (
__unlikely(addr_rc->rc_family != AF_BLUETOOTH)) {
103 errno = EAFNOSUPPORT;
108 *port = btohs(addr_rc->rc_channel);
109 if (ba &&
__unlikely(ba2str(&addr_rc->rc_bdaddr, ba) < 0))
121 memset(addr, 0,
sizeof(*addr));
123 addr->
addrlen =
sizeof(SOCKADDR_BTH);
124 SOCKADDR_BTH *addr_bth = (SOCKADDR_BTH *)&addr->
addr;
126 addr_bth->addressFamily = AF_BTH;
127 addr_bth->port = port ? (ULONG)port : BT_PORT_ANY;
128 addr_bth->btAddr = 0;
130 if (
__unlikely(str2ba(ba, &addr_bth->btAddr) < 0))
134 addr->
addrlen =
sizeof(
struct sockaddr_rc);
135 struct sockaddr_rc *addr_rc = (
struct sockaddr_rc *)&addr->
addr;
137 addr_rc->rc_family = AF_BLUETOOTH;
138 addr_rc->rc_channel = htobs(port);
140 if (
__unlikely(str2ba(ba, &addr_rc->rc_bdaddr) < 0))
143 bacpy(&addr_rc->rc_bdaddr, BDADDR_ANY);
157 WSASetLastError(WSAEINVAL);
161 const SOCKADDR_BTH *addr_bth = (
const SOCKADDR_BTH *)&addr->
addr;
162 if (
__unlikely(addr_bth->addressFamily != AF_BTH)) {
163 WSASetLastError(WSAEAFNOSUPPORT);
168 *port = addr_bth->port == BT_PORT_ANY ? 0 : addr_bth->port;
170 for (
int i = 0; i < 6; i++)
171 ba[i] = (addr_bth->btAddr >> (7 - i) * 8) & 0xff;
179 const struct sockaddr_rc *addr_rc =
180 (
const struct sockaddr_rc *)&addr->
addr;
181 if (
__unlikely(addr_rc->rc_family != AF_BLUETOOTH)) {
182 errno = EAFNOSUPPORT;
187 *port = btohs(addr_rc->rc_channel);
189 memcpy(ba, &addr_rc->rc_bdaddr, 6);
200 memset(addr, 0,
sizeof(*addr));
202 addr->
addrlen =
sizeof(SOCKADDR_BTH);
203 SOCKADDR_BTH *addr_bth = (SOCKADDR_BTH *)&addr->
addr;
205 addr_bth->addressFamily = AF_BTH;
206 addr_bth->port = port ? (ULONG)port : BT_PORT_ANY;
207 addr_bth->btAddr = 0;
209 for (
int i = 0; i < 6; i++)
210 addr_bth->btAddr |= (BTH_ADDR)ba[i] << (7 - i) * 8;
213 addr->
addrlen =
sizeof(
struct sockaddr_rc);
214 struct sockaddr_rc *addr_rc = (
struct sockaddr_rc *)&addr->
addr;
216 addr_rc->rc_family = AF_BLUETOOTH;
217 addr_rc->rc_channel = htobs(port);
219 memcpy(&addr_rc->rc_bdaddr, ba, 6);
221 bacpy(&addr_rc->rc_bdaddr, BDADDR_ANY);
230 memset(addr, 0,
sizeof(*addr));
232 addr->
addrlen =
sizeof(SOCKADDR_BTH);
233 SOCKADDR_BTH *addr_bth = (SOCKADDR_BTH *)&addr->
addr;
235 addr_bth->addressFamily = AF_BTH;
236 addr_bth->port = port ? (ULONG)port : BT_PORT_ANY;
237 addr_bth->btAddr = (BTH_ADDR)0xffffff000000ull;
239 addr->
addrlen =
sizeof(
struct sockaddr_rc);
240 struct sockaddr_rc *addr_rc = (
struct sockaddr_rc *)&addr->
addr;
242 addr_rc->rc_family = AF_BLUETOOTH;
243 addr_rc->rc_channel = htobs(port);
244 bacpy(&addr_rc->rc_bdaddr, BDADDR_LOCAL);
249 #endif // _WIN32 || (__linux__ && HAVE_BLUETOOTH_BLUETOOTH_H && HAVE_BLUETOOTH_RFCOMM_H) 252 #if defined(_WIN32) || _POSIX_C_SOURCE >= 200112L 264 const struct sockaddr_in *addr_in =
265 (
const struct sockaddr_in *)&addr->
addr;
266 if (
__unlikely(addr_in->sin_family != AF_INET)) {
272 *port = ntohs(addr_in->sin_port);
274 if (ip &&
__unlikely(!inet_ntop(AF_INET, (
void *)&addr_in->sin_addr, ip,
287 memset(addr, 0,
sizeof(*addr));
288 addr->
addrlen =
sizeof(
struct sockaddr_in);
289 struct sockaddr_in *addr_in = (
struct sockaddr_in *)&addr->
addr;
291 addr_in->sin_family = AF_INET;
292 addr_in->sin_port = htons(port);
294 if (
__unlikely(inet_pton(AF_INET, ip, &addr_in->sin_addr) != 1))
297 addr_in->sin_addr.s_addr = htonl(INADDR_ANY);
313 const struct sockaddr_in *addr_in =
314 (
const struct sockaddr_in *)&addr->
addr;
315 if (
__unlikely(addr_in->sin_family != AF_INET)) {
321 *port = ntohs(addr_in->sin_port);
323 memcpy(ip, &addr_in->sin_addr.s_addr, 4);
333 memset(addr, 0,
sizeof(*addr));
334 addr->
addrlen =
sizeof(
struct sockaddr_in);
335 struct sockaddr_in *addr_in = (
struct sockaddr_in *)&addr->
addr;
337 addr_in->sin_family = AF_INET;
338 addr_in->sin_port = htons(port);
340 memcpy(&addr_in->sin_addr.s_addr, ip, 4);
342 addr_in->sin_addr.s_addr = htonl(INADDR_ANY);
350 memset(addr, 0,
sizeof(*addr));
351 addr->
addrlen =
sizeof(
struct sockaddr_in);
352 struct sockaddr_in *addr_in = (
struct sockaddr_in *)&addr->
addr;
354 addr_in->sin_family = AF_INET;
355 addr_in->sin_port = htons(port);
356 addr_in->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
364 memset(addr, 0,
sizeof(*addr));
365 addr->
addrlen =
sizeof(
struct sockaddr_in);
366 struct sockaddr_in *addr_in = (
struct sockaddr_in *)&addr->
addr;
368 addr_in->sin_family = AF_INET;
369 addr_in->sin_port = htons(port);
370 addr_in->sin_addr.s_addr = htonl(INADDR_BROADCAST);
383 const struct sockaddr_in6 *addr_in6 =
384 (
const struct sockaddr_in6 *)&addr->
addr;
385 if (
__unlikely(addr_in6->sin6_family != AF_INET6)) {
391 *port = ntohs(addr_in6->sin6_port);
393 if (ip &&
__unlikely(!inet_ntop(AF_INET6, (
void *)&addr_in6->sin6_addr,
406 memset(addr, 0,
sizeof(*addr));
407 addr->
addrlen =
sizeof(
struct sockaddr_in6);
408 struct sockaddr_in6 *addr_in6 = (
struct sockaddr_in6 *)&addr->
addr;
410 addr_in6->sin6_family = AF_INET6;
411 addr_in6->sin6_port = htons(port);
414 if (
__unlikely(inet_pton(AF_INET6, ip, &addr_in6->sin6_addr)
419 addr_in6->sin6_addr = in6addr_any;
435 const struct sockaddr_in6 *addr_in6 =
436 (
const struct sockaddr_in6 *)&addr->
addr;
437 if (
__unlikely(addr_in6->sin6_family != AF_INET6)) {
443 *port = ntohs(addr_in6->sin6_port);
445 memcpy(ip, &addr_in6->sin6_addr.s6_addr, 16);
455 memset(addr, 0,
sizeof(*addr));
456 addr->
addrlen =
sizeof(
struct sockaddr_in6);
457 struct sockaddr_in6 *addr_in6 = (
struct sockaddr_in6 *)&addr->
addr;
459 addr_in6->sin6_family = AF_INET6;
460 addr_in6->sin6_port = htons(port);
462 memcpy(&addr_in6->sin6_addr.s6_addr, ip, 16);
464 addr_in6->sin6_addr = in6addr_any;
472 memset(addr, 0,
sizeof(*addr));
473 addr->
addrlen =
sizeof(
struct sockaddr_in6);
474 struct sockaddr_in6 *addr_in6 = (
struct sockaddr_in6 *)&addr->
addr;
476 addr_in6->sin6_family = AF_INET6;
477 addr_in6->sin6_port = htons(port);
478 addr_in6->sin6_addr = in6addr_loopback;
481 #endif // _WIN32 || _POSIX_C_SOURCE >= 200112L 483 #if _POSIX_C_SOURCE >= 200112L 495 const struct sockaddr_un *addr_un =
496 (
const struct sockaddr_un *)&addr->
addr;
497 if (
__unlikely(addr_un->sun_family != AF_UNIX)) {
498 errno = EAFNOSUPPORT;
514 memset(addr, 0,
sizeof(*addr));
515 addr->
addrlen =
sizeof(
struct sockaddr_un);
516 struct sockaddr_un *addr_un = (
struct sockaddr_un *)&addr->
addr;
518 addr_un->sun_family = AF_UNIX;
520 size_t n =
MIN(strlen(path),
sizeof(addr_un->sun_path) - 1);
521 strncpy(addr_un->sun_path, path, n);
522 addr_un->sun_path[n] =
'\0';
525 #endif // _POSIX_C_SOURCE >= 200112L 527 #if defined(_WIN32) || _POSIX_C_SOURCE >= 200112L 534 switch (((
const struct sockaddr *)&addr->
addr)->sa_family) {
537 #elif defined(__linux__) && defined(HAVE_BLUETOOTH_BLUETOOTH_H) \ 538 && defined(HAVE_BLUETOOTH_RFCOMM_H) 543 #if _POSIX_C_SOURCE >= 200112L 560 switch (((
const struct sockaddr *)&addr->
addr)->sa_family) {
564 WSASetLastError(WSAEINVAL);
567 const SOCKADDR_BTH *addr_bth =
568 (
const SOCKADDR_BTH *)&addr->
addr;
571 *port = addr_bth->port == BT_PORT_ANY
572 ? 0 : addr_bth->port;
576 #elif defined(__linux__) && defined(HAVE_BLUETOOTH_BLUETOOTH_H) \ 577 && defined(HAVE_BLUETOOTH_RFCOMM_H) 581 < (
int)
sizeof(
struct sockaddr_rc))) {
586 const struct sockaddr_rc *addr_rc =
587 (
const struct sockaddr_rc *)&addr->
addr;
589 *port = btohs(addr_rc->rc_channel);
596 < (
int)
sizeof(
struct sockaddr_in))) {
601 const struct sockaddr_in *addr_in =
602 (
const struct sockaddr_in *)&addr->
addr;
604 *port = ntohs(addr_in->sin_port);
610 < (
int)
sizeof(
struct sockaddr_in6))) {
615 const struct sockaddr_in6 *addr_in6 =
616 (
const struct sockaddr_in6 *)&addr->
addr;
618 *port = ntohs(addr_in6->sin6_port);
635 switch (((
struct sockaddr *)&addr->
addr)->sa_family) {
639 WSASetLastError(WSAEINVAL);
642 ((SOCKADDR_BTH *)&addr->
addr)->port =
643 port ? (ULONG)port : BT_PORT_ANY;
646 #elif defined(__linux__) && defined(HAVE_BLUETOOTH_BLUETOOTH_H) \ 647 && defined(HAVE_BLUETOOTH_RFCOMM_H) 651 < (
int)
sizeof(
struct sockaddr_rc))) {
656 ((
struct sockaddr_rc *)&addr->
addr)->rc_channel = htobs(port);
662 < (
int)
sizeof(
struct sockaddr_in))) {
667 ((
struct sockaddr_in *)&addr->
addr)->sin_port = htons(port);
672 < (
int)
sizeof(
struct sockaddr_in6))) {
677 ((
struct sockaddr_in6 *)&addr->
addr)->sin6_port = htons(port);
691 switch (((
const struct sockaddr *)&addr->
addr)->sa_family) {
695 const struct sockaddr_in *addr_in =
696 (
const struct sockaddr_in *)&addr->
addr;
697 return ntohl(addr_in->sin_addr.s_addr) == INADDR_LOOPBACK;
702 < (
int)
sizeof(
struct sockaddr_in6)))
705 const struct sockaddr_in6 *addr_in6 =
706 (
const struct sockaddr_in6 *)&addr->
addr;
707 return !memcmp(&addr_in6->sin6_addr, &in6addr_any,
708 sizeof(in6addr_any));
722 switch (((
const struct sockaddr *)&addr->
addr)->sa_family) {
726 const struct sockaddr_in *addr_in =
727 (
const struct sockaddr_in *)&addr->
addr;
728 return ntohl(addr_in->sin_addr.s_addr) == INADDR_BROADCAST;
742 switch (((
const struct sockaddr *)&addr->
addr)->sa_family) {
746 const struct sockaddr_in *addr_in =
747 (
const struct sockaddr_in *)&addr->
addr;
748 return (ntohl(addr_in->sin_addr.s_addr) >> 28) == 0xe;
753 < (
int)
sizeof(
struct sockaddr_in6)))
756 const struct sockaddr_in6 *addr_in6 =
757 (
const struct sockaddr_in6 *)&addr->
addr;
758 return addr_in6->sin6_addr.s6_addr[0] == 0xff;
766 const char *servname,
const struct io_addrinfo *hints)
773 struct addrinfo ai_hints = { .ai_family = AF_UNSPEC };
778 case IO_SOCK_IPV6: ai_hints.ai_family = AF_INET6;
break;
779 default: ecode = EAI_FAMILY;
break;
782 switch (hints->
type) {
785 case IO_SOCK_DGRAM: ai_hints.ai_socktype = SOCK_DGRAM;
break;
786 default: ecode = EAI_SOCKTYPE;
break;
790 struct addrinfo *res = NULL;
792 ecode = getaddrinfo(nodename, servname, &ai_hints, &res);
804 case EAI_SYSTEM:
break;
812 for (
struct addrinfo *ai = res; ai; ai = ai->ai_next) {
814 switch (ai->ai_family) {
821 switch (ai->ai_socktype) {
827 if (ninfo++ < maxinfo) {
828 memset(info, 0,
sizeof(*info));
832 memcpy(&info->
addr.
addr, ai->ai_addr, ai->ai_addrlen);
842 #endif // _WIN32 || _POSIX_C_SOURCE >= 200112L 847 ba2str(
const BTH_ADDR *ba,
char *str)
849 return sprintf(str,
"%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
850 (
int)((*ba >> 40) & 0xff), (
int)((*ba >> 32) & 0xff),
851 (
int)((*ba >> 24) & 0xff), (
int)((*ba >> 16) & 0xff),
852 ((
int)(*ba >> 8) & 0xff), (
int)(*ba & 0xff));
856 str2ba(
const char *str, BTH_ADDR *ba)
863 for (
int i = 5; i >= 0; i--, str += 3)
864 *ba |= (BTH_ADDR)strtol(str, NULL, 16) << (i * 8);
870 bachk(
const char *str)
int io_addr_is_multicast(const io_addr_t *addr)
Returns 1 if the network address is a multicast address, and 0 if not.
The intended socket type was not recognized.
void io_addr_set_ipv4_n(io_addr_t *addr, const uint8_t ip[4], int port)
Initializes a network address from an IPv4 address and port number.
int io_addr_get_ipv4_a(const io_addr_t *addr, char *ip, int *port)
Obtains an IPv4 address and port number from a network address.
This header file is part of the C11 and POSIX compatibility library; it includes <string.h> and defines any missing functionality.
void io_addr_set_unix(io_addr_t *addr, const char *path)
Initializes a network address from a UNIX domain socket path name.
This header file is part of the utilities library; it contains the comparison function definitions...
void io_addr_set_ipv4_broadcast(io_addr_t *addr, int port)
Initializes a network address with the IPv4 broadcast address and a port number.
A datagram-oriented, typically connectionless-mode, socket type.
void set_errnum(errnum_t errnum)
Sets the current (thread-specific) platform-independent error number to errnum.
The address family was not recognized or the address length was invalid for the specified family...
int io_addr_is_loopback(const io_addr_t *addr)
Returns 1 if the network address is a loopback address, and 0 if not.
An opaque network address type.
void io_addr_set_ipv6_n(io_addr_t *addr, const uint8_t ip[16], int port)
Initializes a network address from an IPv6 address and port number.
#define MIN(a, b)
Returns the minimum of a and b.
A network address info structure.
#define IO_ADDR_UNIX_STRLEN
The maximum number of bytes required to hold the text representation of a UNIX domain socket path nam...
int io_get_addrinfo(int maxinfo, struct io_addrinfo *info, const char *nodename, const char *servname, const struct io_addrinfo *hints)
Obtains a list of network addresses corresponding to a host and/or service name.
This header file is part of the utilities library; it contains the native and platform-independent er...
#define __likely(x)
Indicates to the compiler that the expression is most-likely true.
Address family not supported.
The flags had an invalid value.
int io_addr_is_broadcast(const io_addr_t *addr)
Returns 1 if the network address is a broadcast address, and 0 if not.
A non-recoverable error occurred.
int io_addr_get_unix(const io_addr_t *addr, char *path)
Obtains a UNIX domain socket path name from a network address.
#define IO_ADDR_IPV4_STRLEN
The maximum number of bytes required to hold the text representation of an IPv4 internet address...
void io_addr_set_rfcomm_n(io_addr_t *addr, const uint8_t ba[6], int port)
Initializes a network address from an RFCOMM Bluetooth device address and port number.
int addrlen
The size (in bytes) of addr.
int domain
The domain of the socket (only IO_SOCK_IPV4 and IO_SOCK_IPV6 are supported).
int io_addr_set_port(io_addr_t *addr, int port)
Initializes the port number of an IPv4 or IPv6 network address.
int io_addr_set_ipv4_a(io_addr_t *addr, const char *ip, int port)
Initializes a network address from an IPv4 address and port number.
int io_addr_get_domain(const io_addr_t *addr)
Obtains the domain of a network address.
This header file is part of the I/O library; it contains the network socket declarations.
int io_addr_get_ipv6_n(const io_addr_t *addr, uint8_t ip[16], int *port)
Obtains an IPv6 address and port number from a network address.
int io_addr_set_ipv6_a(io_addr_t *addr, const char *ip, int port)
Initializes a network address from an IPv6 address and port number.
int io_addr_get_rfcomm_a(const io_addr_t *addr, char *ba, int *port)
Obtains an RFCOMM Bluetooth device address and port number from a network address.
#define __unlikely(x)
Indicates to the compiler that the expression is most-likely false.
This is the internal header file of the I/O library.
int io_addr_set_rfcomm_a(io_addr_t *addr, const char *ba, int port)
Initializes a network address from an RFCOMM Bluetooth device address and port number.
#define IO_ADDR_IPV6_STRLEN
The maximum number of bytes required to hold the text representation of an IPv6 internet address...
int io_addr_get_ipv4_n(const io_addr_t *addr, uint8_t ip[4], int *port)
Obtains an IPv4 address and port number from a network address.
void io_addr_set_ipv4_loopback(io_addr_t *addr, int port)
Initializes a network address with the IPv4 loopback address and a port number.
This header file is part of the C11 and POSIX compatibility library; it includes <stdio.h> and defines any missing functionality.
int io_addr_get_ipv6_a(const io_addr_t *addr, char *ip, int *port)
Obtains an IPv6 address and port number from a network address.
io_addr_t addr
The network address.
A stream-oriented connection-mode socket type.
A UNIX domain socket (only supported on POSIX platforms).
This header file is part of the C11 and POSIX compatibility library; it includes <stdlib.h> and defines any missing functionality.
The service passed was not recognized for the specified socket type.
The name does not resolve for the supplied parameters.
int io_addr_get_rfcomm_n(const io_addr_t *addr, uint8_t ba[6], int *port)
Obtains an RFCOMM Bluetooth device address and port number from a network address.
The name could not be resolved at this time.
void io_addr_set_rfcomm_local(io_addr_t *addr, int port)
Initializes a network address with the local Bluetooth (RFCOMM) device address (FF:FF:FF:00:00:00) an...
void io_addr_set_ipv6_loopback(io_addr_t *addr, int port)
Initializes a network address with the IPv6 loopback address and a port number.
There was a memory allocation failure.
int type
The type of the socket (either IO_SOCK_STREAM or IO_SOCK_DGRAM).
int io_addr_get_port(const io_addr_t *addr, int *port)
Obtains the port number of an IPv4 or IPv6 network address.
This header file is part of the I/O library; it contains the network address declarations.
union __io_addr::@2 addr
The network address.
int io_addr_cmp(const void *p1, const void *p2)
Compares two network addresses.