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>
57 #define LOCALHOST_IPV4 "127.0.0.1"
58 #define LOCALHOST_IPV6 "::1"
60 #define NETLINK_BUFSIZE 16384
66 setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, (
void *)&on,
sizeof(on));
79 if (addr1->
family == AF_INET) {
80 addrlen =
sizeof(
struct in_addr);
82 if (addr1->
family == AF_INET6) {
83 addrlen =
sizeof(
struct in6_addr);
87 if (memcmp(addr1->
addr, addr2->
addr, addrlen) == 0)
95 const struct sockaddr *sa)
101 if (totem_ip->
family != sa->sa_family) {
105 switch (totem_ip->
family) {
107 res = (memcmp(totem_ip->
addr,
108 &((
const struct sockaddr_in *)sa)->sin_addr,
sizeof(
struct in_addr)) == 0);
111 res = (memcmp(totem_ip->
addr,
112 &((
const struct sockaddr_in6 *)sa)->sin6_addr,
sizeof(
struct in6_addr)) == 0);
145 memcpy (&addr, ip_addr->
addr, sizeof (uint32_t));
147 if (ip_addr->
family == AF_INET) {
149 if ((addr >> 28) != 0xE) {
162 struct in_addr ipv4_a1;
163 struct in_addr ipv4_a2;
164 struct in6_addr ipv6_a1;
165 struct in6_addr ipv6_a2;
171 memcpy (&family, &totemip_a->
family, sizeof (
unsigned short));
173 if (family == AF_INET) {
174 memcpy (&ipv4_a1, totemip_a->
addr, sizeof (
struct in_addr));
175 memcpy (&ipv4_a2, totemip_b->
addr, sizeof (
struct in_addr));
176 if (ipv4_a1.s_addr == ipv4_a2.s_addr) {
179 if (htonl(ipv4_a1.s_addr) < htonl(ipv4_a2.s_addr)) {
185 if (family == AF_INET6) {
189 memcpy (&ipv6_a1, totemip_a->
addr, sizeof (
struct in6_addr));
190 memcpy (&ipv6_a2, totemip_b->
addr, sizeof (
struct in6_addr));
191 for (i = 0; i < 16; i++) {
192 int res = ipv6_a1.s6_addr[i] -
211 const char *addr_text;
215 if (family == AF_INET) {
217 if (inet_pton(family, addr_text, (
char *)&localhost->
nodeid) <= 0) {
224 if (inet_pton(family, addr_text, (
char *)localhost->
addr) <= 0)
243 static char buf[INET6_ADDRSTRLEN];
247 switch (sa->sa_family) {
249 inet_ntop(sa->sa_family, &((
struct sockaddr_in *)(sa))->sin_addr, buf,
253 inet_ntop(sa->sa_family, &((
struct sockaddr_in6 *)(sa))->sin6_addr, buf,
265 static char buf[INET6_ADDRSTRLEN];
267 return (inet_ntop(addr->
family, addr->
addr, buf,
sizeof(buf)));
272 uint16_t port,
struct sockaddr_storage *saddr,
int *addrlen)
276 if (ip_addr->
family == AF_INET) {
277 struct sockaddr_in *sin = (
struct sockaddr_in *)saddr;
279 memset(sin, 0,
sizeof(
struct sockaddr_in));
280 #ifdef HAVE_SOCK_SIN_LEN
281 sin->sin_len =
sizeof(
struct sockaddr_in);
283 sin->sin_family = ip_addr->
family;
284 sin->sin_port = ntohs(port);
285 memcpy(&sin->sin_addr, ip_addr->
addr,
sizeof(
struct in_addr));
286 *addrlen =
sizeof(
struct sockaddr_in);
290 if (ip_addr->
family == AF_INET6) {
291 struct sockaddr_in6 *sin = (
struct sockaddr_in6 *)saddr;
293 memset(sin, 0,
sizeof(
struct sockaddr_in6));
294 #ifdef HAVE_SOCK_SIN6_LEN
295 sin->sin6_len =
sizeof(
struct sockaddr_in6);
297 sin->sin6_family = ip_addr->
family;
298 sin->sin6_port = ntohs(port);
299 sin->sin6_scope_id = 2;
300 memcpy(&sin->sin6_addr, ip_addr->
addr,
sizeof(
struct in6_addr));
302 *addrlen =
sizeof(
struct sockaddr_in6);
314 struct addrinfo *ainfo;
315 struct addrinfo ahints;
316 struct sockaddr_in *sa;
317 struct sockaddr_in6 *sa6;
320 memset(&ahints, 0,
sizeof(ahints));
321 ahints.ai_socktype = SOCK_DGRAM;
322 ahints.ai_protocol = IPPROTO_UDP;
323 ahints.ai_family =
family;
326 ret = getaddrinfo(addr, NULL, &ahints, &ainfo);
330 sa = (
struct sockaddr_in *)ainfo->ai_addr;
331 sa6 = (
struct sockaddr_in6 *)ainfo->ai_addr;
332 totemip->
family = ainfo->ai_family;
334 if (ainfo->ai_family == AF_INET)
335 memcpy(totemip->
addr, &sa->sin_addr,
sizeof(
struct in_addr));
337 memcpy(totemip->
addr, &sa6->sin6_addr,
sizeof(
struct in6_addr));
349 ip_addr->
family = saddr->ss_family;
352 if (saddr->ss_family == AF_INET) {
353 const struct sockaddr_in *sin = (
const struct sockaddr_in *)saddr;
355 memcpy(ip_addr->
addr, &sin->sin_addr,
sizeof(
struct in_addr));
359 if (saddr->ss_family == AF_INET6) {
360 const struct sockaddr_in6 *sin
361 = (
const struct sockaddr_in6 *)saddr;
363 memcpy(ip_addr->
addr, &sin->sin6_addr,
sizeof(
struct in6_addr));
372 struct ifaddrs *ifap, *ifa;
375 if (getifaddrs(&ifap) != 0)
380 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
381 if (ifa->ifa_addr == NULL || ifa->ifa_netmask == NULL)
384 if ((ifa->ifa_addr->sa_family != AF_INET && ifa->ifa_addr->sa_family != AF_INET6) ||
385 (ifa->ifa_netmask->sa_family != AF_INET && ifa->ifa_netmask->sa_family != AF_INET6 &&
386 ifa->ifa_netmask->sa_family != 0))
389 if (ifa->ifa_netmask->sa_family == 0) {
390 ifa->ifa_netmask->sa_family = ifa->ifa_addr->sa_family;
394 if (if_addr == NULL) {
395 goto error_free_ifaddrs;
398 list_init(&if_addr->
list);
404 if_addr->
name = strdup(ifa->ifa_name);
405 if (if_addr->
name == NULL) {
406 goto error_free_addr;
411 goto error_free_addr_name;
416 goto error_free_addr_name;
419 list_add_tail(&if_addr->
list, addrs);
426 error_free_addr_name:
443 for (list = addrs->
next; list != addrs;) {
448 list_del(&if_addr->
list);
467 int exact_match_found = 0;
468 int net_match_found = 0;
477 for (list = addrs.
next; list != &addrs; list = list->
next) {
485 switch (bindnet->
family) {
487 addr_len =
sizeof(
struct in_addr);
490 addr_len =
sizeof(
struct in6_addr);
501 exact_match_found = 1;
504 for (si = 0; si < addr_len; si++) {
509 if (exact_match_found || (!net_match_found &&
totemip_equal(&bn_netaddr, &if_netaddr))) {
515 if (boundto->
family == AF_INET && boundto->
nodeid == 0) {
517 memcpy (&nodeid, boundto->
addr, sizeof (
int));
518 #if __BYTE_ORDER == __LITTLE_ENDIAN
522 nodeid &= 0x7FFFFFFF;
530 if (exact_match_found) {
541 #define TOTEMIP_UDP_HEADER_SIZE 8
542 #define TOTEMIP_IPV4_HEADER_SIZE 20
543 #define TOTEMIP_IPV6_HEADER_SIZE 40
560 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)
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 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)
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)