39 #include <sys/ioctl.h> 40 #include <sys/types.h> 41 #include <sys/socket.h> 42 #include <netinet/in.h> 43 #include <arpa/inet.h> 58 #define LOCALHOST_IPV4 "127.0.0.1" 59 #define LOCALHOST_IPV6 "::1" 61 #define NETLINK_BUFSIZE 16384 67 setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, (
void *)&on,
sizeof(on));
80 if (addr1->
family == AF_INET) {
81 addrlen =
sizeof(
struct in_addr);
83 if (addr1->
family == AF_INET6) {
84 addrlen =
sizeof(
struct in6_addr);
88 if (memcmp(addr1->
addr, addr2->
addr, addrlen) == 0)
96 const struct sockaddr *sa)
102 if (totem_ip->
family != sa->sa_family) {
106 switch (totem_ip->
family) {
108 res = (memcmp(totem_ip->
addr,
109 &((
const struct sockaddr_in *)sa)->sin_addr,
sizeof(
struct in_addr)) == 0);
112 res = (memcmp(totem_ip->
addr,
113 &((
const struct sockaddr_in6 *)sa)->sin6_addr,
sizeof(
struct in6_addr)) == 0);
146 memcpy (&addr, ip_addr->
addr, sizeof (uint32_t));
148 if (ip_addr->
family == AF_INET) {
150 if ((addr >> 28) != 0xE) {
163 struct in_addr ipv4_a1;
164 struct in_addr ipv4_a2;
165 struct in6_addr ipv6_a1;
166 struct in6_addr ipv6_a2;
172 memcpy (&family, &totemip_a->
family, sizeof (
unsigned short));
174 if (family == AF_INET) {
175 memcpy (&ipv4_a1, totemip_a->
addr, sizeof (
struct in_addr));
176 memcpy (&ipv4_a2, totemip_b->
addr, sizeof (
struct in_addr));
177 if (ipv4_a1.s_addr == ipv4_a2.s_addr) {
180 if (htonl(ipv4_a1.s_addr) < htonl(ipv4_a2.s_addr)) {
186 if (family == AF_INET6) {
190 memcpy (&ipv6_a1, totemip_a->
addr, sizeof (
struct in6_addr));
191 memcpy (&ipv6_a2, totemip_b->
addr, sizeof (
struct in6_addr));
192 for (i = 0; i < 16; i++) {
193 int res = ipv6_a1.s6_addr[i] -
212 const char *addr_text;
216 if (family == AF_INET) {
218 if (inet_pton(family, addr_text, (
char *)&localhost->
nodeid) <= 0) {
225 if (inet_pton(family, addr_text, (
char *)localhost->
addr) <= 0)
244 static char buf[INET6_ADDRSTRLEN];
248 switch (sa->sa_family) {
250 inet_ntop(sa->sa_family, &((
struct sockaddr_in *)(sa))->sin_addr, buf,
254 inet_ntop(sa->sa_family, &((
struct sockaddr_in6 *)(sa))->sin6_addr, buf,
266 static char buf[INET6_ADDRSTRLEN];
268 return (inet_ntop(addr->
family, addr->
addr, buf,
sizeof(buf)));
273 uint16_t port,
struct sockaddr_storage *saddr,
int *addrlen)
277 if (ip_addr->
family == AF_INET) {
278 struct sockaddr_in *sin = (
struct sockaddr_in *)saddr;
280 memset(sin, 0,
sizeof(
struct sockaddr_in));
281 #ifdef HAVE_SOCK_SIN_LEN 282 sin->sin_len =
sizeof(
struct sockaddr_in);
284 sin->sin_family = ip_addr->
family;
285 sin->sin_port = ntohs(port);
286 memcpy(&sin->sin_addr, ip_addr->
addr,
sizeof(
struct in_addr));
287 *addrlen =
sizeof(
struct sockaddr_in);
291 if (ip_addr->
family == AF_INET6) {
292 struct sockaddr_in6 *sin = (
struct sockaddr_in6 *)saddr;
294 memset(sin, 0,
sizeof(
struct sockaddr_in6));
295 #ifdef HAVE_SOCK_SIN6_LEN 296 sin->sin6_len =
sizeof(
struct sockaddr_in6);
298 sin->sin6_family = ip_addr->
family;
299 sin->sin6_port = ntohs(port);
300 sin->sin6_scope_id = 2;
301 memcpy(&sin->sin6_addr, ip_addr->
addr,
sizeof(
struct in6_addr));
303 *addrlen =
sizeof(
struct sockaddr_in6);
317 struct addrinfo *ainfo;
318 struct addrinfo *ainfo_iter;
319 struct addrinfo *ainfo_ipv4;
320 struct addrinfo *ainfo_ipv6;
321 struct addrinfo *ainfo_final;
322 struct addrinfo ahints;
323 struct sockaddr_in *sa;
324 struct sockaddr_in6 *sa6;
329 memset(&ahints, 0,
sizeof(ahints));
330 ahints.ai_socktype = SOCK_DGRAM;
331 ahints.ai_protocol = IPPROTO_UDP;
333 ai_family = AF_UNSPEC;
336 switch (ip_version) {
342 ai_family = AF_INET6;
353 ahints.ai_family = ai_family;
355 ret = getaddrinfo(addr, NULL, &ahints, &ainfo);
357 if (ret == 0 && ai_family == AF_UNSPEC) {
358 ainfo_ipv4 = ainfo_ipv6 = NULL;
363 for (ainfo_iter = ainfo; ainfo_iter != NULL; ainfo_iter = ainfo_iter->ai_next) {
364 if (ainfo_iter->ai_family == AF_INET && ainfo_ipv4 == NULL) {
365 ainfo_ipv4 = ainfo_iter;
368 if (ainfo_iter->ai_family == AF_INET6 && ainfo_ipv6 == NULL) {
369 ainfo_ipv6 = ainfo_iter;
374 if (ainfo_ipv6 != NULL) {
375 ainfo_final = ainfo_ipv6;
377 ainfo_final = ainfo_ipv4;
380 if (ainfo_ipv4 != NULL) {
381 ainfo_final = ainfo_ipv4;
383 ainfo_final = ainfo_ipv6;
386 }
else if (ret == 0) {
392 if (ainfo_final == NULL) {
397 if (debug_ip_family == 0) {
402 debug_ip_family, addr);
408 totemip->
family = ainfo_final->ai_family;
409 if (ainfo_final->ai_family == AF_INET) {
410 sa = (
struct sockaddr_in *)ainfo_final->ai_addr;
411 memcpy(totemip->
addr, &sa->sin_addr,
sizeof(
struct in_addr));
414 sa6 = (
struct sockaddr_in6 *)ainfo_final->ai_addr;
415 memcpy(totemip->
addr, &sa6->sin6_addr,
sizeof(
struct in6_addr));
433 ip_addr->
family = saddr->ss_family;
436 if (saddr->ss_family == AF_INET) {
437 const struct sockaddr_in *sin = (
const struct sockaddr_in *)saddr;
439 memcpy(ip_addr->
addr, &sin->sin_addr,
sizeof(
struct in_addr));
443 if (saddr->ss_family == AF_INET6) {
444 const struct sockaddr_in6 *sin
445 = (
const struct sockaddr_in6 *)saddr;
447 memcpy(ip_addr->
addr, &sin->sin6_addr,
sizeof(
struct in6_addr));
456 struct ifaddrs *ifap, *ifa;
459 if (getifaddrs(&ifap) != 0)
464 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
465 if (ifa->ifa_addr == NULL || ifa->ifa_netmask == NULL)
468 if ((ifa->ifa_addr->sa_family != AF_INET && ifa->ifa_addr->sa_family != AF_INET6) ||
469 (ifa->ifa_netmask->sa_family != AF_INET && ifa->ifa_netmask->sa_family != AF_INET6 &&
470 ifa->ifa_netmask->sa_family != 0))
473 if (ifa->ifa_netmask->sa_family == 0) {
474 ifa->ifa_netmask->sa_family = ifa->ifa_addr->sa_family;
478 if (if_addr == NULL) {
479 goto error_free_ifaddrs;
482 qb_list_init(&if_addr->
list);
488 if_addr->
name = strdup(ifa->ifa_name);
489 if (if_addr->
name == NULL) {
490 goto error_free_addr;
495 goto error_free_addr_name;
500 goto error_free_addr_name;
503 qb_list_add_tail(&if_addr->
list, addrs);
510 error_free_addr_name:
525 struct qb_list_head *list, *tmp_iter;
527 qb_list_for_each_safe(list, tmp_iter, addrs) {
531 qb_list_del(&if_addr->
list);
543 struct qb_list_head addrs;
544 struct qb_list_head *list;
550 int exact_match_found = 0;
551 int net_match_found = 0;
560 qb_list_for_each(list, &addrs) {
568 switch (bindnet->
family) {
570 addr_len =
sizeof(
struct in_addr);
573 addr_len =
sizeof(
struct in6_addr);
584 exact_match_found = 1;
587 for (si = 0; si < addr_len; si++) {
592 if (exact_match_found || (!net_match_found &&
totemip_equal(&bn_netaddr, &if_netaddr))) {
601 if (exact_match_found) {
612 #define TOTEMIP_UDP_HEADER_SIZE 8 613 #define TOTEMIP_IPV4_HEADER_SIZE 20 614 #define TOTEMIP_IPV6_HEADER_SIZE 40 631 return (header_size);
#define TOTEMIP_IPV4_HEADER_SIZE
The totem_ip_address struct.
unsigned char addr[TOTEMIP_ADDRLEN]
void totemip_copy(struct totem_ip_address *addr1, const struct totem_ip_address *addr2)
unsigned char addr[TOTEMIP_ADDRLEN]
int totemip_localhost(int family, struct totem_ip_address *localhost)
#define log_printf(level, format, args...)
#define totemip_nosigpipe(s)
const char * totemip_print(const struct totem_ip_address *addr)
int32_t totemip_is_mcast(struct totem_ip_address *ip_addr)
int totemip_localhost_check(const struct totem_ip_address *addr)
void totemip_freeifaddrs(struct qb_list_head *addrs)
int totemip_totemip_to_sockaddr_convert(struct totem_ip_address *ip_addr, uint16_t port, struct sockaddr_storage *saddr, int *addrlen)
int totemip_getifaddrs(struct qb_list_head *addrs)
int totemip_iface_check(struct totem_ip_address *bindnet, struct totem_ip_address *boundto, int *interface_up, int *interface_num, int mask_high_bit)
size_t totemip_udpip_header_size(int family)
const char * totemip_sa_print(const struct sockaddr *sa)
#define LOGSYS_LEVEL_DEBUG
struct totem_ip_address mask_addr
int totemip_compare(const void *a, const void *b)
#define swab32(x)
The swab32 macro.
int totemip_parse(struct totem_ip_address *totemip, const char *addr, enum totem_ip_version_enum ip_version)
int totemip_equal(const struct totem_ip_address *addr1, const struct totem_ip_address *addr2)
#define swab16(x)
The swab16 macro.
int totemip_sockaddr_to_totemip_convert(const struct sockaddr_storage *saddr, struct totem_ip_address *ip_addr)
#define TOTEMIP_IPV6_HEADER_SIZE
int totemip_sa_equal(const struct totem_ip_address *totem_ip, const struct sockaddr *sa)
#define TOTEMIP_UDP_HEADER_SIZE
struct totem_ip_address ip_addr
void totemip_copy_endian_convert(struct totem_ip_address *addr1, const struct totem_ip_address *addr2)