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)
119 memcpy (&addr, ip_addr->
addr, sizeof (uint32_t));
121 if (ip_addr->
family == AF_INET) {
123 if ((addr >> 28) != 0xE) {
136 struct in_addr ipv4_a1;
137 struct in_addr ipv4_a2;
138 struct in6_addr ipv6_a1;
139 struct in6_addr ipv6_a2;
145 memcpy (&family, &totemip_a->
family, sizeof (
unsigned short));
147 if (family == AF_INET) {
148 memcpy (&ipv4_a1, totemip_a->
addr, sizeof (
struct in_addr));
149 memcpy (&ipv4_a2, totemip_b->
addr, sizeof (
struct in_addr));
150 if (ipv4_a1.s_addr == ipv4_a2.s_addr) {
153 if (htonl(ipv4_a1.s_addr) < htonl(ipv4_a2.s_addr)) {
159 if (family == AF_INET6) {
163 memcpy (&ipv6_a1, totemip_a->
addr, sizeof (
struct in6_addr));
164 memcpy (&ipv6_a2, totemip_b->
addr, sizeof (
struct in6_addr));
165 for (i = 0; i < 16; i++) {
166 int res = ipv6_a1.s6_addr[i] -
185 const char *addr_text;
189 if (family == AF_INET) {
191 if (inet_pton(family, addr_text, (
char *)&localhost->
nodeid) <= 0) {
198 if (inet_pton(family, addr_text, (
char *)localhost->
addr) <= 0)
217 static char buf[INET6_ADDRSTRLEN];
221 switch (sa->sa_family) {
223 inet_ntop(sa->sa_family, &((
struct sockaddr_in *)(sa))->sin_addr, buf,
227 inet_ntop(sa->sa_family, &((
struct sockaddr_in6 *)(sa))->sin6_addr, buf,
239 static char buf[INET6_ADDRSTRLEN];
241 return (inet_ntop(addr->
family, addr->
addr, buf,
sizeof(buf)));
246 uint16_t port,
struct sockaddr_storage *saddr,
int *addrlen)
250 if (ip_addr->
family == AF_INET) {
251 struct sockaddr_in *sin = (
struct sockaddr_in *)saddr;
253 memset(sin, 0,
sizeof(
struct sockaddr_in));
254 #ifdef HAVE_SOCK_SIN_LEN 255 sin->sin_len =
sizeof(
struct sockaddr_in);
257 sin->sin_family = ip_addr->
family;
258 sin->sin_port = ntohs(port);
259 memcpy(&sin->sin_addr, ip_addr->
addr,
sizeof(
struct in_addr));
260 *addrlen =
sizeof(
struct sockaddr_in);
264 if (ip_addr->
family == AF_INET6) {
265 struct sockaddr_in6 *sin = (
struct sockaddr_in6 *)saddr;
267 memset(sin, 0,
sizeof(
struct sockaddr_in6));
268 #ifdef HAVE_SOCK_SIN6_LEN 269 sin->sin6_len =
sizeof(
struct sockaddr_in6);
271 sin->sin6_family = ip_addr->
family;
272 sin->sin6_port = ntohs(port);
273 sin->sin6_scope_id = 2;
274 memcpy(&sin->sin6_addr, ip_addr->
addr,
sizeof(
struct in6_addr));
276 *addrlen =
sizeof(
struct sockaddr_in6);
290 struct addrinfo *ainfo;
291 struct addrinfo *ainfo_iter;
292 struct addrinfo *ainfo_ipv4;
293 struct addrinfo *ainfo_ipv6;
294 struct addrinfo *ainfo_final;
295 struct addrinfo ahints;
296 struct sockaddr_in *sa;
297 struct sockaddr_in6 *sa6;
302 memset(&ahints, 0,
sizeof(ahints));
303 ahints.ai_socktype = SOCK_DGRAM;
304 ahints.ai_protocol = IPPROTO_UDP;
306 ai_family = AF_UNSPEC;
309 switch (ip_version) {
315 ai_family = AF_INET6;
326 ahints.ai_family = ai_family;
328 ret = getaddrinfo(addr, NULL, &ahints, &ainfo);
330 if (ret == 0 && ai_family == AF_UNSPEC) {
331 ainfo_ipv4 = ainfo_ipv6 = NULL;
336 for (ainfo_iter = ainfo; ainfo_iter != NULL; ainfo_iter = ainfo_iter->ai_next) {
337 if (ainfo_iter->ai_family == AF_INET && ainfo_ipv4 == NULL) {
338 ainfo_ipv4 = ainfo_iter;
341 if (ainfo_iter->ai_family == AF_INET6 && ainfo_ipv6 == NULL) {
342 ainfo_ipv6 = ainfo_iter;
347 if (ainfo_ipv6 != NULL) {
348 ainfo_final = ainfo_ipv6;
350 ainfo_final = ainfo_ipv4;
353 if (ainfo_ipv4 != NULL) {
354 ainfo_final = ainfo_ipv4;
356 ainfo_final = ainfo_ipv6;
359 }
else if (ret == 0) {
365 if (ainfo_final == NULL) {
370 if (debug_ip_family == 0) {
375 debug_ip_family, addr);
381 totemip->
family = ainfo_final->ai_family;
382 if (ainfo_final->ai_family == AF_INET) {
383 sa = (
struct sockaddr_in *)ainfo_final->ai_addr;
384 memcpy(totemip->
addr, &sa->sin_addr,
sizeof(
struct in_addr));
387 sa6 = (
struct sockaddr_in6 *)ainfo_final->ai_addr;
388 memcpy(totemip->
addr, &sa6->sin6_addr,
sizeof(
struct in6_addr));
406 ip_addr->
family = saddr->ss_family;
409 if (saddr->ss_family == AF_INET) {
410 const struct sockaddr_in *sin = (
const struct sockaddr_in *)saddr;
412 memcpy(ip_addr->
addr, &sin->sin_addr,
sizeof(
struct in_addr));
416 if (saddr->ss_family == AF_INET6) {
417 const struct sockaddr_in6 *sin
418 = (
const struct sockaddr_in6 *)saddr;
420 memcpy(ip_addr->
addr, &sin->sin6_addr,
sizeof(
struct in6_addr));
429 struct ifaddrs *ifap, *ifa;
432 if (getifaddrs(&ifap) != 0)
437 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
438 if (ifa->ifa_addr == NULL || ifa->ifa_netmask == NULL)
441 if ((ifa->ifa_addr->sa_family != AF_INET && ifa->ifa_addr->sa_family != AF_INET6) ||
442 (ifa->ifa_netmask->sa_family != AF_INET && ifa->ifa_netmask->sa_family != AF_INET6 &&
443 ifa->ifa_netmask->sa_family != 0))
446 if (ifa->ifa_netmask->sa_family == 0) {
447 ifa->ifa_netmask->sa_family = ifa->ifa_addr->sa_family;
451 if (if_addr == NULL) {
452 goto error_free_ifaddrs;
455 qb_list_init(&if_addr->
list);
461 if_addr->
name = strdup(ifa->ifa_name);
462 if (if_addr->
name == NULL) {
463 goto error_free_addr;
468 goto error_free_addr_name;
473 goto error_free_addr_name;
476 qb_list_add_tail(&if_addr->
list, addrs);
483 error_free_addr_name:
498 struct qb_list_head *list, *tmp_iter;
500 qb_list_for_each_safe(list, tmp_iter, addrs) {
504 qb_list_del(&if_addr->
list);
516 struct qb_list_head addrs;
517 struct qb_list_head *list;
523 int exact_match_found = 0;
524 int net_match_found = 0;
533 qb_list_for_each(list, &addrs) {
541 switch (bindnet->
family) {
543 addr_len =
sizeof(
struct in_addr);
546 addr_len =
sizeof(
struct in6_addr);
557 exact_match_found = 1;
560 for (si = 0; si < addr_len; si++) {
565 if (exact_match_found || (!net_match_found &&
totemip_equal(&bn_netaddr, &if_netaddr))) {
574 if (exact_match_found) {
585 #define TOTEMIP_UDP_HEADER_SIZE 8 586 #define TOTEMIP_IPV4_HEADER_SIZE 20 587 #define TOTEMIP_IPV6_HEADER_SIZE 40 604 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
#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)