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 ahints;
292 struct sockaddr_in *sa;
293 struct sockaddr_in6 *sa6;
296 int ai_family1, ai_family2;
298 memset(&ahints, 0,
sizeof(ahints));
299 ahints.ai_socktype = SOCK_DGRAM;
300 ahints.ai_protocol = IPPROTO_UDP;
302 ai_family1 = ai_family2 = -1;
304 switch (ip_version) {
306 ai_family1 = AF_INET;
309 ai_family1 = AF_INET6;
312 ai_family1 = AF_INET;
313 ai_family2 = AF_INET6;
316 ai_family1 = AF_INET6;
317 ai_family2 = AF_INET;
321 ahints.ai_family = ai_family1;
322 ret = getaddrinfo(addr, NULL, &ahints, &ainfo);
323 if (ret && ai_family2 != -1) {
324 ahints.ai_family = ai_family2;
325 ret = getaddrinfo(addr, NULL, &ahints, &ainfo);
329 if (ahints.ai_family == AF_INET6) {
335 debug_ip_family, addr);
340 sa = (
struct sockaddr_in *)ainfo->ai_addr;
341 sa6 = (
struct sockaddr_in6 *)ainfo->ai_addr;
342 totemip->
family = ainfo->ai_family;
344 if (ainfo->ai_family == AF_INET)
345 memcpy(totemip->
addr, &sa->sin_addr,
sizeof(
struct in_addr));
347 memcpy(totemip->
addr, &sa6->sin6_addr,
sizeof(
struct in6_addr));
362 ip_addr->
family = saddr->ss_family;
365 if (saddr->ss_family == AF_INET) {
366 const struct sockaddr_in *sin = (
const struct sockaddr_in *)saddr;
368 memcpy(ip_addr->
addr, &sin->sin_addr,
sizeof(
struct in_addr));
372 if (saddr->ss_family == AF_INET6) {
373 const struct sockaddr_in6 *sin
374 = (
const struct sockaddr_in6 *)saddr;
376 memcpy(ip_addr->
addr, &sin->sin6_addr,
sizeof(
struct in6_addr));
385 struct ifaddrs *ifap, *ifa;
388 if (getifaddrs(&ifap) != 0)
393 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
394 if (ifa->ifa_addr == NULL || ifa->ifa_netmask == NULL)
397 if ((ifa->ifa_addr->sa_family != AF_INET && ifa->ifa_addr->sa_family != AF_INET6) ||
398 (ifa->ifa_netmask->sa_family != AF_INET && ifa->ifa_netmask->sa_family != AF_INET6 &&
399 ifa->ifa_netmask->sa_family != 0))
402 if (ifa->ifa_netmask->sa_family == 0) {
403 ifa->ifa_netmask->sa_family = ifa->ifa_addr->sa_family;
407 if (if_addr == NULL) {
408 goto error_free_ifaddrs;
411 qb_list_init(&if_addr->
list);
417 if_addr->
name = strdup(ifa->ifa_name);
418 if (if_addr->
name == NULL) {
419 goto error_free_addr;
424 goto error_free_addr_name;
429 goto error_free_addr_name;
432 qb_list_add_tail(&if_addr->
list, addrs);
439 error_free_addr_name:
454 struct qb_list_head *list, *tmp_iter;
456 qb_list_for_each_safe(list, tmp_iter, addrs) {
460 qb_list_del(&if_addr->
list);
472 struct qb_list_head addrs;
473 struct qb_list_head *list;
479 int exact_match_found = 0;
480 int net_match_found = 0;
489 qb_list_for_each(list, &addrs) {
497 switch (bindnet->
family) {
499 addr_len =
sizeof(
struct in_addr);
502 addr_len =
sizeof(
struct in6_addr);
513 exact_match_found = 1;
516 for (si = 0; si < addr_len; si++) {
521 if (exact_match_found || (!net_match_found &&
totemip_equal(&bn_netaddr, &if_netaddr))) {
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);
#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)