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)
118 memcpy (&addr, ip_addr->
addr, sizeof (uint32_t));
120 if (ip_addr->
family == AF_INET) {
122 if ((addr >> 28) != 0xE) {
135 struct in_addr ipv4_a1;
136 struct in_addr ipv4_a2;
137 struct in6_addr ipv6_a1;
138 struct in6_addr ipv6_a2;
144 memcpy (&family, &totemip_a->
family, sizeof (
unsigned short));
146 if (family == AF_INET) {
147 memcpy (&ipv4_a1, totemip_a->
addr, sizeof (
struct in_addr));
148 memcpy (&ipv4_a2, totemip_b->
addr, sizeof (
struct in_addr));
149 if (ipv4_a1.s_addr == ipv4_a2.s_addr) {
152 if (htonl(ipv4_a1.s_addr) < htonl(ipv4_a2.s_addr)) {
158 if (family == AF_INET6) {
162 memcpy (&ipv6_a1, totemip_a->
addr, sizeof (
struct in6_addr));
163 memcpy (&ipv6_a2, totemip_b->
addr, sizeof (
struct in6_addr));
164 for (i = 0; i < 16; i++) {
165 int res = ipv6_a1.s6_addr[i] -
184 const char *addr_text;
188 if (family == AF_INET) {
190 if (inet_pton(family, addr_text, (
char *)&localhost->
nodeid) <= 0) {
197 if (inet_pton(family, addr_text, (
char *)localhost->
addr) <= 0)
216 static char buf[INET6_ADDRSTRLEN];
218 return (inet_ntop(addr->
family, addr->
addr, buf,
sizeof(buf)));
223 uint16_t port,
struct sockaddr_storage *saddr,
int *addrlen)
227 if (ip_addr->
family == AF_INET) {
228 struct sockaddr_in *sin = (
struct sockaddr_in *)saddr;
230 memset(sin, 0,
sizeof(
struct sockaddr_in));
231 #ifdef HAVE_SOCK_SIN_LEN 232 sin->sin_len =
sizeof(
struct sockaddr_in);
234 sin->sin_family = ip_addr->
family;
235 sin->sin_port = ntohs(port);
236 memcpy(&sin->sin_addr, ip_addr->
addr,
sizeof(
struct in_addr));
237 *addrlen =
sizeof(
struct sockaddr_in);
241 if (ip_addr->
family == AF_INET6) {
242 struct sockaddr_in6 *sin = (
struct sockaddr_in6 *)saddr;
244 memset(sin, 0,
sizeof(
struct sockaddr_in6));
245 #ifdef HAVE_SOCK_SIN6_LEN 246 sin->sin6_len =
sizeof(
struct sockaddr_in6);
248 sin->sin6_family = ip_addr->
family;
249 sin->sin6_port = ntohs(port);
250 sin->sin6_scope_id = 2;
251 memcpy(&sin->sin6_addr, ip_addr->
addr,
sizeof(
struct in6_addr));
253 *addrlen =
sizeof(
struct sockaddr_in6);
265 struct addrinfo *ainfo;
266 struct addrinfo ahints;
267 struct sockaddr_in *sa;
268 struct sockaddr_in6 *sa6;
271 memset(&ahints, 0,
sizeof(ahints));
272 ahints.ai_socktype = SOCK_DGRAM;
273 ahints.ai_protocol = IPPROTO_UDP;
274 ahints.ai_family =
family;
277 ret = getaddrinfo(addr, NULL, &ahints, &ainfo);
281 sa = (
struct sockaddr_in *)ainfo->ai_addr;
282 sa6 = (
struct sockaddr_in6 *)ainfo->ai_addr;
283 totemip->
family = ainfo->ai_family;
285 if (ainfo->ai_family == AF_INET)
286 memcpy(totemip->
addr, &sa->sin_addr,
sizeof(
struct in_addr));
288 memcpy(totemip->
addr, &sa6->sin6_addr,
sizeof(
struct in6_addr));
300 ip_addr->
family = saddr->ss_family;
303 if (saddr->ss_family == AF_INET) {
304 const struct sockaddr_in *sin = (
const struct sockaddr_in *)saddr;
306 memcpy(ip_addr->
addr, &sin->sin_addr,
sizeof(
struct in_addr));
310 if (saddr->ss_family == AF_INET6) {
311 const struct sockaddr_in6 *sin
312 = (
const struct sockaddr_in6 *)saddr;
314 memcpy(ip_addr->
addr, &sin->sin6_addr,
sizeof(
struct in6_addr));
323 struct ifaddrs *ifap, *ifa;
326 if (getifaddrs(&ifap) != 0)
331 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
332 if (ifa->ifa_addr == NULL || ifa->ifa_netmask == NULL)
335 if ((ifa->ifa_addr->sa_family != AF_INET && ifa->ifa_addr->sa_family != AF_INET6) ||
336 (ifa->ifa_netmask->sa_family != AF_INET && ifa->ifa_netmask->sa_family != AF_INET6 &&
337 ifa->ifa_netmask->sa_family != 0))
340 if (ifa->ifa_netmask->sa_family == 0) {
341 ifa->ifa_netmask->sa_family = ifa->ifa_addr->sa_family;
345 if (if_addr == NULL) {
346 goto error_free_ifaddrs;
349 list_init(&if_addr->
list);
355 if_addr->
name = strdup(ifa->ifa_name);
356 if (if_addr->
name == NULL) {
357 goto error_free_addr;
362 goto error_free_addr_name;
367 goto error_free_addr_name;
370 list_add_tail(&if_addr->
list, addrs);
377 error_free_addr_name:
394 for (list = addrs->
next; list != addrs;) {
399 list_del(&if_addr->
list);
418 int exact_match_found = 0;
419 int net_match_found = 0;
428 for (list = addrs.
next; list != &addrs; list = list->
next) {
436 switch (bindnet->
family) {
438 addr_len =
sizeof(
struct in_addr);
441 addr_len =
sizeof(
struct in6_addr);
452 exact_match_found = 1;
455 for (si = 0; si < addr_len; si++) {
460 if (exact_match_found || (!net_match_found &&
totemip_equal(&bn_netaddr, &if_netaddr))) {
466 if (boundto->
family == AF_INET && boundto->
nodeid == 0) {
468 memcpy (&nodeid, boundto->
addr, sizeof (
int));
469 #if __BYTE_ORDER == __LITTLE_ENDIAN 473 nodeid &= 0x7FFFFFFF;
481 if (exact_match_found) {
492 #define TOTEMIP_UDP_HEADER_SIZE 8 493 #define TOTEMIP_IPV4_HEADER_SIZE 20 494 #define TOTEMIP_IPV6_HEADER_SIZE 40 511 return (header_size);
void totemip_freeifaddrs(struct list_head *addrs)
#define TOTEMIP_IPV4_HEADER_SIZE
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)
int totemip_getifaddrs(struct list_head *addrs)
struct totem_ip_address mask_addr
int totemip_compare(const void *a, const void *b)
int totemip_equal(const struct totem_ip_address *addr1, const struct totem_ip_address *addr2)
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)
#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)