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)));
271 static int totemip_getif_scopeid(
const unsigned char *addr16,
unsigned int *scopeid)
274 const struct sockaddr_in6 *sin6;
275 const socklen_t addr_len =
sizeof(
struct in6_addr);
277 struct ifaddrs *totemip_getif_scopeid_ifap;
279 if (getifaddrs(&totemip_getif_scopeid_ifap) != 0) {
283 for (ifa = totemip_getif_scopeid_ifap; ifa; ifa = ifa->ifa_next) {
284 if (ifa->ifa_addr == NULL || ifa->ifa_netmask == NULL)
287 if ((ifa->ifa_addr->sa_family != AF_INET6) ||
288 (ifa->ifa_netmask->sa_family != AF_INET6 &&
289 ifa->ifa_netmask->sa_family != 0))
292 sin6 = (
const struct sockaddr_in6 *)ifa->ifa_addr;
294 if (memcmp(&sin6->sin6_addr, addr16, addr_len) == 0) {
295 *scopeid = sin6->sin6_scope_id;
307 " Use 'ip a' to see details.",
311 freeifaddrs(totemip_getif_scopeid_ifap);
318 uint16_t port,
struct sockaddr_storage *saddr,
int *addrlen,
322 unsigned int scopeid;
324 if (ip_addr->
family == AF_INET) {
325 struct sockaddr_in *sin = (
struct sockaddr_in *)saddr;
327 memset(sin, 0,
sizeof(
struct sockaddr_in));
328 #ifdef HAVE_SOCK_SIN_LEN
329 sin->sin_len =
sizeof(
struct sockaddr_in);
331 sin->sin_family = ip_addr->
family;
332 sin->sin_port = ntohs(port);
333 memcpy(&sin->sin_addr, ip_addr->
addr,
sizeof(
struct in_addr));
334 *addrlen =
sizeof(
struct sockaddr_in);
338 if (ip_addr->
family == AF_INET6) {
339 struct sockaddr_in6 *sin = (
struct sockaddr_in6 *)saddr;
341 memset(sin, 0,
sizeof(
struct sockaddr_in6));
342 #ifdef HAVE_SOCK_SIN6_LEN
343 sin->sin6_len =
sizeof(
struct sockaddr_in6);
345 sin->sin6_family = ip_addr->
family;
346 sin->sin6_port = ntohs(port);
348 if (totemip_getif_scopeid(ip_addr->
addr, &scopeid) == 0) {
349 sin->sin6_scope_id = scopeid;
352 memcpy(&sin->sin6_addr, ip_addr->
addr,
sizeof(
struct in6_addr));
354 *addrlen =
sizeof(
struct sockaddr_in6);
363 uint16_t port,
struct sockaddr_storage *saddr,
int *addrlen)
375 struct addrinfo *ainfo;
376 struct addrinfo ahints;
377 struct sockaddr_in *sa;
378 struct sockaddr_in6 *sa6;
381 memset(&ahints, 0,
sizeof(ahints));
382 ahints.ai_socktype = SOCK_DGRAM;
383 ahints.ai_protocol = IPPROTO_UDP;
384 ahints.ai_family =
family;
387 ret = getaddrinfo(addr, NULL, &ahints, &ainfo);
391 sa = (
struct sockaddr_in *)ainfo->ai_addr;
392 sa6 = (
struct sockaddr_in6 *)ainfo->ai_addr;
393 totemip->
family = ainfo->ai_family;
395 if (ainfo->ai_family == AF_INET)
396 memcpy(totemip->
addr, &sa->sin_addr,
sizeof(
struct in_addr));
398 memcpy(totemip->
addr, &sa6->sin6_addr,
sizeof(
struct in6_addr));
410 ip_addr->
family = saddr->ss_family;
413 if (saddr->ss_family == AF_INET) {
414 const struct sockaddr_in *sin = (
const struct sockaddr_in *)saddr;
416 memcpy(ip_addr->
addr, &sin->sin_addr,
sizeof(
struct in_addr));
420 if (saddr->ss_family == AF_INET6) {
421 const struct sockaddr_in6 *sin
422 = (
const struct sockaddr_in6 *)saddr;
424 memcpy(ip_addr->
addr, &sin->sin6_addr,
sizeof(
struct in6_addr));
433 struct ifaddrs *ifap, *ifa;
436 if (getifaddrs(&ifap) != 0)
441 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
442 if (ifa->ifa_addr == NULL || ifa->ifa_netmask == NULL)
445 if ((ifa->ifa_addr->sa_family != AF_INET && ifa->ifa_addr->sa_family != AF_INET6) ||
446 (ifa->ifa_netmask->sa_family != AF_INET && ifa->ifa_netmask->sa_family != AF_INET6 &&
447 ifa->ifa_netmask->sa_family != 0))
450 if (ifa->ifa_netmask->sa_family == 0) {
451 ifa->ifa_netmask->sa_family = ifa->ifa_addr->sa_family;
455 if (if_addr == NULL) {
456 goto error_free_ifaddrs;
459 list_init(&if_addr->
list);
465 if_addr->
name = strdup(ifa->ifa_name);
466 if (if_addr->
name == NULL) {
467 goto error_free_addr;
472 goto error_free_addr_name;
477 goto error_free_addr_name;
480 list_add_tail(&if_addr->
list, addrs);
487 error_free_addr_name:
504 for (list = addrs->
next; list != addrs;) {
509 list_del(&if_addr->
list);
528 int exact_match_found = 0;
529 int net_match_found = 0;
538 for (list = addrs.
next; list != &addrs; list = list->
next) {
546 switch (bindnet->
family) {
548 addr_len =
sizeof(
struct in_addr);
551 addr_len =
sizeof(
struct in6_addr);
562 exact_match_found = 1;
565 for (si = 0; si < addr_len; si++) {
570 if (exact_match_found || (!net_match_found &&
totemip_equal(&bn_netaddr, &if_netaddr))) {
576 if (boundto->
family == AF_INET && boundto->
nodeid == 0) {
578 memcpy (&nodeid, boundto->
addr, sizeof (
int));
579 #if __BYTE_ORDER == __LITTLE_ENDIAN
583 nodeid &= 0x7FFFFFFF;
591 if (exact_match_found) {
602 #define TOTEMIP_UDP_HEADER_SIZE 8
603 #define TOTEMIP_IPV4_HEADER_SIZE 20
604 #define TOTEMIP_IPV6_HEADER_SIZE 40
621 return (header_size);
void totemip_freeifaddrs(struct list_head *addrs)
#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)
int totemip_totemip_to_sockaddr_convert_with_scopeid(const struct totem_ip_address *ip_addr, uint16_t port, struct sockaddr_storage *saddr, int *addrlen, int fill_scopeid)
unsigned char addr[TOTEMIP_ADDRLEN]
int totemip_localhost(int family, struct totem_ip_address *localhost)
int totemip_parse(struct totem_ip_address *totemip, const char *addr, int family)
#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)
#define LOGSYS_LEVEL_WARNING
int totemip_totemip_to_sockaddr_convert(struct totem_ip_address *ip_addr, uint16_t port, struct sockaddr_storage *saddr, int *addrlen)
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)
int totemip_getifaddrs(struct list_head *addrs)
struct totem_ip_address mask_addr
int totemip_compare(const void *a, const void *b)
#define swab32(x)
The swab32 macro.
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 list_entry(ptr, type, member)
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)