41#include <sys/socket.h>
42#include <netinet/in.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);
138 memcpy (&
addr, ip_addr->
addr, sizeof (uint32_t));
140 if (ip_addr->
family == AF_INET) {
142 if ((
addr >> 28) != 0xE) {
155 struct in_addr ipv4_a1;
156 struct in_addr ipv4_a2;
157 struct in6_addr ipv6_a1;
158 struct in6_addr ipv6_a2;
164 memcpy (&
family, &totemip_a->
family, sizeof (
unsigned short));
167 memcpy (&ipv4_a1, totemip_a->
addr, sizeof (
struct in_addr));
168 memcpy (&ipv4_a2, totemip_b->
addr, sizeof (
struct in_addr));
169 if (ipv4_a1.s_addr == ipv4_a2.s_addr) {
172 if (htonl(ipv4_a1.s_addr) < htonl(ipv4_a2.s_addr)) {
182 memcpy (&ipv6_a1, totemip_a->
addr, sizeof (
struct in6_addr));
183 memcpy (&ipv6_a2, totemip_b->
addr, sizeof (
struct in6_addr));
184 for (i = 0; i < 16; i++) {
185 int res = ipv6_a1.s6_addr[i] -
204 const char *addr_text;
210 if (inet_pton(
family, addr_text, (
char *)&localhost->
nodeid) <= 0) {
217 if (inet_pton(
family, addr_text, (
char *)localhost->
addr) <= 0)
236 static char buf[INET6_ADDRSTRLEN];
240 switch (sa->sa_family) {
242 inet_ntop(sa->sa_family, &((
struct sockaddr_in *)(sa))->sin_addr, buf,
246 inet_ntop(sa->sa_family, &((
struct sockaddr_in6 *)(sa))->sin6_addr, buf,
258 static char buf[INET6_ADDRSTRLEN];
260 return (inet_ntop(
addr->family,
addr->addr, buf,
sizeof(buf)));
265 uint16_t port,
struct sockaddr_storage *saddr,
int *addrlen)
269 if (ip_addr->
family == AF_INET) {
270 struct sockaddr_in *sin = (
struct sockaddr_in *)saddr;
272 memset(sin, 0,
sizeof(
struct sockaddr_in));
273#ifdef HAVE_SOCK_SIN_LEN
274 sin->sin_len =
sizeof(
struct sockaddr_in);
276 sin->sin_family = ip_addr->
family;
277 sin->sin_port = ntohs(port);
278 memcpy(&sin->sin_addr, ip_addr->
addr,
sizeof(
struct in_addr));
279 *addrlen =
sizeof(
struct sockaddr_in);
283 if (ip_addr->
family == AF_INET6) {
284 struct sockaddr_in6 *sin = (
struct sockaddr_in6 *)saddr;
286 memset(sin, 0,
sizeof(
struct sockaddr_in6));
287#ifdef HAVE_SOCK_SIN6_LEN
288 sin->sin6_len =
sizeof(
struct sockaddr_in6);
290 sin->sin6_family = ip_addr->
family;
291 sin->sin6_port = ntohs(port);
292 sin->sin6_scope_id = 2;
293 memcpy(&sin->sin6_addr, ip_addr->
addr,
sizeof(
struct in6_addr));
295 *addrlen =
sizeof(
struct sockaddr_in6);
309 struct addrinfo *ainfo;
310 struct addrinfo *ainfo_iter;
311 struct addrinfo *ainfo_ipv4;
312 struct addrinfo *ainfo_ipv6;
313 struct addrinfo *ainfo_final;
314 struct addrinfo ahints;
315 struct sockaddr_in *sa;
316 struct sockaddr_in6 *sa6;
321 memset(&ahints, 0,
sizeof(ahints));
322 ahints.ai_socktype = SOCK_DGRAM;
323 ahints.ai_protocol = IPPROTO_UDP;
325 ai_family = AF_UNSPEC;
328 switch (ip_version) {
334 ai_family = AF_INET6;
345 ahints.ai_family = ai_family;
347 ret = getaddrinfo(
addr, NULL, &ahints, &ainfo);
349 if (ret == 0 && ai_family == AF_UNSPEC) {
350 ainfo_ipv4 = ainfo_ipv6 = NULL;
355 for (ainfo_iter = ainfo; ainfo_iter != NULL; ainfo_iter = ainfo_iter->ai_next) {
356 if (ainfo_iter->ai_family == AF_INET && ainfo_ipv4 == NULL) {
357 ainfo_ipv4 = ainfo_iter;
360 if (ainfo_iter->ai_family == AF_INET6 && ainfo_ipv6 == NULL) {
361 ainfo_ipv6 = ainfo_iter;
366 if (ainfo_ipv6 != NULL) {
367 ainfo_final = ainfo_ipv6;
369 ainfo_final = ainfo_ipv4;
372 if (ainfo_ipv4 != NULL) {
373 ainfo_final = ainfo_ipv4;
375 ainfo_final = ainfo_ipv6;
378 }
else if (ret == 0) {
384 if (ainfo_final == NULL) {
389 if (debug_ip_family == 0) {
394 debug_ip_family,
addr);
400 totemip->
family = ainfo_final->ai_family;
401 if (ainfo_final->ai_family == AF_INET) {
402 sa = (
struct sockaddr_in *)ainfo_final->ai_addr;
403 memcpy(totemip->
addr, &sa->sin_addr,
sizeof(
struct in_addr));
406 sa6 = (
struct sockaddr_in6 *)ainfo_final->ai_addr;
407 memcpy(totemip->
addr, &sa6->sin6_addr,
sizeof(
struct in6_addr));
425 ip_addr->
family = saddr->ss_family;
428 if (saddr->ss_family == AF_INET) {
429 const struct sockaddr_in *sin = (
const struct sockaddr_in *)saddr;
431 memcpy(ip_addr->
addr, &sin->sin_addr,
sizeof(
struct in_addr));
435 if (saddr->ss_family == AF_INET6) {
436 const struct sockaddr_in6 *sin
437 = (
const struct sockaddr_in6 *)saddr;
439 memcpy(ip_addr->
addr, &sin->sin6_addr,
sizeof(
struct in6_addr));
448 struct ifaddrs *ifap, *ifa;
451 if (getifaddrs(&ifap) != 0)
456 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
457 if (ifa->ifa_addr == NULL || ifa->ifa_netmask == NULL)
460 if ((ifa->ifa_addr->sa_family != AF_INET && ifa->ifa_addr->sa_family != AF_INET6) ||
461 (ifa->ifa_netmask->sa_family != AF_INET && ifa->ifa_netmask->sa_family != AF_INET6 &&
462 ifa->ifa_netmask->sa_family != 0))
465 if (ifa->ifa_netmask->sa_family == 0) {
466 ifa->ifa_netmask->sa_family = ifa->ifa_addr->sa_family;
470 if (if_addr == NULL) {
471 goto error_free_ifaddrs;
474 qb_list_init(&if_addr->
list);
480 if_addr->
name = strdup(ifa->ifa_name);
481 if (if_addr->
name == NULL) {
482 goto error_free_addr;
487 goto error_free_addr_name;
492 goto error_free_addr_name;
495 qb_list_add_tail(&if_addr->
list, addrs);
517 struct qb_list_head *list, *tmp_iter;
519 qb_list_for_each_safe(list, tmp_iter, addrs) {
523 qb_list_del(&if_addr->
list);
535 struct qb_list_head addrs;
536 struct qb_list_head *list;
542 int exact_match_found = 0;
543 int net_match_found = 0;
552 qb_list_for_each(list, &addrs) {
560 switch (bindnet->
family) {
562 addr_len =
sizeof(
struct in_addr);
565 addr_len =
sizeof(
struct in6_addr);
576 exact_match_found = 1;
579 for (si = 0; si < addr_len; si++) {
584 if (exact_match_found || (!net_match_found &&
totemip_equal(&bn_netaddr, &if_netaddr))) {
593 if (exact_match_found) {
604#define TOTEMIP_UDP_HEADER_SIZE 8
605#define TOTEMIP_IPV4_HEADER_SIZE 20
606#define TOTEMIP_IPV6_HEADER_SIZE 40
623 return (header_size);
unsigned char addr[TOTEMIP_ADDRLEN]
#define log_printf(level, format, args...)
#define LOGSYS_LEVEL_DEBUG
The totem_ip_address struct.
unsigned char addr[TOTEMIP_ADDRLEN]
struct totem_ip_address mask_addr
struct totem_ip_address ip_addr
#define TOTEMIP_UDP_HEADER_SIZE
const char * totemip_sa_print(const struct sockaddr *sa)
int totemip_sockaddr_to_totemip_convert(const struct sockaddr_storage *saddr, struct totem_ip_address *ip_addr)
int totemip_localhost_check(const struct totem_ip_address *addr)
int totemip_parse(struct totem_ip_address *totemip, const char *addr, enum totem_ip_version_enum ip_version)
int totemip_sa_equal(const struct totem_ip_address *totem_ip, const struct sockaddr *sa)
#define TOTEMIP_IPV6_HEADER_SIZE
void totemip_freeifaddrs(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)
int totemip_getifaddrs(struct qb_list_head *addrs)
int totemip_equal(const struct totem_ip_address *addr1, const struct totem_ip_address *addr2)
void totemip_copy(struct totem_ip_address *addr1, const struct totem_ip_address *addr2)
size_t totemip_udpip_header_size(int family)
int totemip_compare(const void *a, const void *b)
int totemip_totemip_to_sockaddr_convert(struct totem_ip_address *ip_addr, uint16_t port, struct sockaddr_storage *saddr, int *addrlen)
const char * totemip_print(const struct totem_ip_address *addr)
int32_t totemip_is_mcast(struct totem_ip_address *ip_addr)
#define TOTEMIP_IPV4_HEADER_SIZE
int totemip_localhost(int family, struct totem_ip_address *localhost)
#define totemip_nosigpipe(s)