totemip.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /*
  2. * Copyright (c) 2005-2007, 2009 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Patrick Caulfield (pcaulfie@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. /* IPv4/6 abstraction */
  35. #include <config.h>
  36. #include <sys/ioctl.h>
  37. #include <sys/types.h>
  38. #include <sys/socket.h>
  39. #include <arpa/inet.h>
  40. #include <netinet/in.h>
  41. #include <arpa/inet.h>
  42. #include <netdb.h>
  43. #if defined(COROSYNC_SOLARIS)
  44. #include <net/if.h>
  45. #include <sys/sockio.h>
  46. #endif
  47. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  48. #include <sys/sockio.h>
  49. #include <net/if.h>
  50. #include <net/if_var.h>
  51. #include <netinet/in_var.h>
  52. #include <netinet/in.h>
  53. #include <ifaddrs.h>
  54. #endif
  55. #include <string.h>
  56. #include <stdio.h>
  57. #include <errno.h>
  58. #include <assert.h>
  59. #include <stdlib.h>
  60. #include <unistd.h>
  61. #if defined(COROSYNC_LINUX)
  62. #include <net/if.h>
  63. #include <asm/types.h>
  64. #include <linux/rtnetlink.h>
  65. #endif
  66. #include <corosync/totem/totemip.h>
  67. #include <corosync/swab.h>
  68. #define LOCALHOST_IPV4 "127.0.0.1"
  69. #define LOCALHOST_IPV6 "::1"
  70. #define NETLINK_BUFSIZE 16384
  71. #ifdef SO_NOSIGPIPE
  72. void totemip_nosigpipe(int s)
  73. {
  74. int on = 1;
  75. setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, (void *)&on, sizeof(on));
  76. }
  77. #endif
  78. /* Compare two addresses */
  79. int totemip_equal(const struct totem_ip_address *addr1,
  80. const struct totem_ip_address *addr2)
  81. {
  82. int addrlen = 0;
  83. if (addr1->family != addr2->family)
  84. return 0;
  85. if (addr1->family == AF_INET) {
  86. addrlen = sizeof(struct in_addr);
  87. }
  88. if (addr1->family == AF_INET6) {
  89. addrlen = sizeof(struct in6_addr);
  90. }
  91. assert(addrlen);
  92. if (memcmp(addr1->addr, addr2->addr, addrlen) == 0)
  93. return 1;
  94. else
  95. return 0;
  96. }
  97. /* Copy a totem_ip_address */
  98. void totemip_copy(struct totem_ip_address *addr1,
  99. const struct totem_ip_address *addr2)
  100. {
  101. memcpy(addr1, addr2, sizeof(struct totem_ip_address));
  102. }
  103. void totemip_copy_endian_convert(struct totem_ip_address *addr1,
  104. const struct totem_ip_address *addr2)
  105. {
  106. addr1->nodeid = swab32(addr2->nodeid);
  107. addr1->family = swab16(addr2->family);
  108. memcpy(addr1->addr, addr2->addr, TOTEMIP_ADDRLEN);
  109. }
  110. /*
  111. * Multicast address range is 224.0.0.0 to 239.255.255.255 this
  112. * translates to the first 4 bits == 1110 (0xE).
  113. * http://en.wikipedia.org/wiki/Multicast_address
  114. */
  115. int32_t totemip_is_mcast(struct totem_ip_address *ip_addr)
  116. {
  117. uint32_t addr = 0;
  118. if (ip_addr->family == AF_INET) {
  119. addr = ntohl(*(int32_t*)ip_addr->addr);
  120. if ((addr >> 28) != 0xE) {
  121. return -1;
  122. }
  123. }
  124. return 0;
  125. }
  126. /* For sorting etc. params are void * for qsort's benefit */
  127. int totemip_compare(const void *a, const void *b)
  128. {
  129. int i;
  130. const struct totem_ip_address *totemip_a = (const struct totem_ip_address *)a;
  131. const struct totem_ip_address *totemip_b = (const struct totem_ip_address *)b;
  132. struct in_addr ipv4_a1;
  133. struct in_addr ipv4_a2;
  134. struct in6_addr ipv6_a1;
  135. struct in6_addr ipv6_a2;
  136. unsigned short family;
  137. /*
  138. * Use memcpy to align since totem_ip_address is unaligned on various archs
  139. */
  140. memcpy (&family, &totemip_a->family, sizeof (unsigned short));
  141. if (family == AF_INET) {
  142. memcpy (&ipv4_a1, totemip_a->addr, sizeof (struct in_addr));
  143. memcpy (&ipv4_a2, totemip_b->addr, sizeof (struct in_addr));
  144. if (ipv4_a1.s_addr == ipv4_a2.s_addr) {
  145. return (0);
  146. }
  147. if (htonl(ipv4_a1.s_addr) < htonl(ipv4_a2.s_addr)) {
  148. return -1;
  149. } else {
  150. return +1;
  151. }
  152. } else
  153. if (family == AF_INET6) {
  154. /*
  155. * We can only compare 8 bits at time for portability reasons
  156. */
  157. memcpy (&ipv6_a1, totemip_a->addr, sizeof (struct in6_addr));
  158. memcpy (&ipv6_a2, totemip_b->addr, sizeof (struct in6_addr));
  159. for (i = 0; i < 16; i++) {
  160. int res = ipv6_a1.s6_addr[i] -
  161. ipv6_a2.s6_addr[i];
  162. if (res) {
  163. return res;
  164. }
  165. }
  166. return 0;
  167. } else {
  168. /*
  169. * Family not set, should be!
  170. */
  171. assert (0);
  172. }
  173. return 0;
  174. }
  175. /* Build a localhost totem_ip_address */
  176. int totemip_localhost(int family, struct totem_ip_address *localhost)
  177. {
  178. const char *addr_text;
  179. memset (localhost, 0, sizeof (struct totem_ip_address));
  180. if (family == AF_INET) {
  181. addr_text = LOCALHOST_IPV4;
  182. if (inet_pton(family, addr_text, (char *)&localhost->nodeid) <= 0) {
  183. return -1;
  184. }
  185. } else {
  186. addr_text = LOCALHOST_IPV6;
  187. }
  188. if (inet_pton(family, addr_text, (char *)localhost->addr) <= 0)
  189. return -1;
  190. localhost->family = family;
  191. return 0;
  192. }
  193. int totemip_localhost_check(const struct totem_ip_address *addr)
  194. {
  195. struct totem_ip_address localhost;
  196. if (totemip_localhost(addr->family, &localhost))
  197. return 0;
  198. return totemip_equal(addr, &localhost);
  199. }
  200. const char *totemip_print(const struct totem_ip_address *addr)
  201. {
  202. static char buf[INET6_ADDRSTRLEN];
  203. return (inet_ntop(addr->family, addr->addr, buf, sizeof(buf)));
  204. }
  205. /* Make a totem_ip_address into a usable sockaddr_storage */
  206. int totemip_totemip_to_sockaddr_convert(struct totem_ip_address *ip_addr,
  207. uint16_t port, struct sockaddr_storage *saddr, int *addrlen)
  208. {
  209. int ret = -1;
  210. if (ip_addr->family == AF_INET) {
  211. struct sockaddr_in *sin = (struct sockaddr_in *)saddr;
  212. memset(sin, 0, sizeof(struct sockaddr_in));
  213. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  214. sin->sin_len = sizeof(struct sockaddr_in);
  215. #endif
  216. sin->sin_family = ip_addr->family;
  217. sin->sin_port = ntohs(port);
  218. memcpy(&sin->sin_addr, ip_addr->addr, sizeof(struct in_addr));
  219. *addrlen = sizeof(struct sockaddr_in);
  220. ret = 0;
  221. }
  222. if (ip_addr->family == AF_INET6) {
  223. struct sockaddr_in6 *sin = (struct sockaddr_in6 *)saddr;
  224. memset(sin, 0, sizeof(struct sockaddr_in6));
  225. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  226. sin->sin6_len = sizeof(struct sockaddr_in6);
  227. #endif
  228. sin->sin6_family = ip_addr->family;
  229. sin->sin6_port = ntohs(port);
  230. sin->sin6_scope_id = 2;
  231. memcpy(&sin->sin6_addr, ip_addr->addr, sizeof(struct in6_addr));
  232. *addrlen = sizeof(struct sockaddr_in6);
  233. ret = 0;
  234. }
  235. return ret;
  236. }
  237. /* Converts an address string string into a totem_ip_address.
  238. family can be AF_INET, AF_INET6 or 0 ("for "don't care")
  239. */
  240. int totemip_parse(struct totem_ip_address *totemip, const char *addr, int family)
  241. {
  242. struct addrinfo *ainfo;
  243. struct addrinfo ahints;
  244. struct sockaddr_in *sa;
  245. struct sockaddr_in6 *sa6;
  246. int ret;
  247. memset(&ahints, 0, sizeof(ahints));
  248. ahints.ai_socktype = SOCK_DGRAM;
  249. ahints.ai_protocol = IPPROTO_UDP;
  250. ahints.ai_family = family;
  251. /* Lookup the nodename address */
  252. ret = getaddrinfo(addr, NULL, &ahints, &ainfo);
  253. if (ret)
  254. return -1;
  255. sa = (struct sockaddr_in *)ainfo->ai_addr;
  256. sa6 = (struct sockaddr_in6 *)ainfo->ai_addr;
  257. totemip->family = ainfo->ai_family;
  258. if (ainfo->ai_family == AF_INET)
  259. memcpy(totemip->addr, &sa->sin_addr, sizeof(struct in_addr));
  260. else
  261. memcpy(totemip->addr, &sa6->sin6_addr, sizeof(struct in6_addr));
  262. return 0;
  263. }
  264. /* Make a sockaddr_* into a totem_ip_address */
  265. int totemip_sockaddr_to_totemip_convert(const struct sockaddr_storage *saddr,
  266. struct totem_ip_address *ip_addr)
  267. {
  268. int ret = -1;
  269. ip_addr->family = saddr->ss_family;
  270. ip_addr->nodeid = 0;
  271. if (saddr->ss_family == AF_INET) {
  272. const struct sockaddr_in *sin = (const struct sockaddr_in *)saddr;
  273. memcpy(ip_addr->addr, &sin->sin_addr, sizeof(struct in_addr));
  274. ret = 0;
  275. }
  276. if (saddr->ss_family == AF_INET6) {
  277. const struct sockaddr_in6 *sin
  278. = (const struct sockaddr_in6 *)saddr;
  279. memcpy(ip_addr->addr, &sin->sin6_addr, sizeof(struct in6_addr));
  280. ret = 0;
  281. }
  282. return ret;
  283. }
  284. /*
  285. * On Solaris, man if_tcp describes this method
  286. */
  287. #if defined(COROSYNC_SOLARIS)
  288. int totemip_iface_check(struct totem_ip_address *bindnet,
  289. struct totem_ip_address *boundto,
  290. int *interface_up,
  291. int *interface_num,
  292. int mask_high_bit)
  293. {
  294. struct sockaddr_storage bindnet_ss;
  295. struct sockaddr_in *bindnet_sin = (struct sockaddr_in *)&bindnet_ss;
  296. struct sockaddr_in *sockaddr_in;
  297. int id_fd;
  298. struct lifconf lifconf;
  299. struct lifreq *lifreq;
  300. int numreqs = 0;
  301. int i;
  302. in_addr_t mask_addr;
  303. int res = -1;
  304. int addrlen;
  305. totemip_totemip_to_sockaddr_convert (bindnet,
  306. 0, &bindnet_ss, &addrlen);
  307. *interface_up = 0;
  308. id_fd = socket (AF_INET, SOCK_STREAM, 0);
  309. lifconf.lifc_family = AF_UNSPEC;
  310. lifconf.lifc_flags = 0;
  311. lifconf.lifc_buf = NULL;
  312. lifconf.lifc_len = 0;
  313. do {
  314. numreqs += 32;
  315. lifconf.lifc_len = sizeof (struct lifreq) * numreqs;
  316. lifconf.lifc_buf = (void *)realloc(lifconf.lifc_buf, lifconf.lifc_len);
  317. res = ioctl (id_fd, SIOCGLIFCONF, &lifconf);
  318. if (res < 0) {
  319. close (id_fd);
  320. return -1;
  321. }
  322. } while (lifconf.lifc_len == sizeof (struct lifconf) * numreqs);
  323. res = -1;
  324. lifreq = (struct lifreq *)lifconf.lifc_buf;
  325. /*
  326. * Find interface address to bind to
  327. */
  328. for (i = 0; i < lifconf.lifc_len / sizeof (struct lifreq); i++) {
  329. sockaddr_in = (struct sockaddr_in *)&lifreq[i].lifr_addr;
  330. mask_addr = inet_addr ("255.255.255.0");
  331. if ((sockaddr_in->sin_family == AF_INET) &&
  332. (sockaddr_in->sin_addr.s_addr & mask_addr) ==
  333. (bindnet_sin->sin_addr.s_addr & mask_addr)) {
  334. res = i;
  335. /*
  336. * Setup boundto output
  337. */
  338. totemip_sockaddr_to_totemip_convert((struct sockaddr_storage *)sockaddr_in, boundto);
  339. boundto->nodeid = sockaddr_in->sin_addr.s_addr;
  340. #if __BYTE_ORDER == __BIG_ENDIAN
  341. boundto->nodeid = swab32 (boundto->nodeid);
  342. #endif
  343. if (ioctl(id_fd, SIOCGLIFFLAGS, &lifreq[i]) < 0) {
  344. printf ("couldn't do ioctl\n");
  345. }
  346. *interface_up = lifreq[i].lifr_flags & IFF_UP;
  347. if (ioctl(id_fd, SIOCGLIFINDEX, &lifreq[i]) < 0) {
  348. printf ("couldn't do ioctl\n");
  349. }
  350. *interface_num = lifreq[i].lifr_index;
  351. break;
  352. }
  353. }
  354. free (lifconf.lifc_buf);
  355. close (id_fd);
  356. return (res);
  357. }
  358. #endif
  359. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  360. int totemip_iface_check(struct totem_ip_address *bindnet,
  361. struct totem_ip_address *boundto,
  362. int *interface_up,
  363. int *interface_num,
  364. int mask_high_bit)
  365. {
  366. #define NEXT_IFR(a) ((struct ifreq *)((u_char *)&(a)->ifr_addr +\
  367. ((a)->ifr_addr.sa_len ? (a)->ifr_addr.sa_len : sizeof((a)->ifr_addr))))
  368. struct sockaddr_in *intf_addr_mask;
  369. struct sockaddr_storage bindnet_ss;
  370. struct sockaddr_in *intf_addr_sin;
  371. struct sockaddr_in *bindnet_sin = (struct sockaddr_in *)&bindnet_ss;
  372. struct ifaddrs *ifap, *ifa;
  373. int res = -1;
  374. int addrlen;
  375. *interface_up = 0;
  376. *interface_num = 0;
  377. totemip_totemip_to_sockaddr_convert(bindnet,
  378. 0, &bindnet_ss, &addrlen);
  379. if (getifaddrs(&ifap) != 0)
  380. return -1;
  381. for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
  382. intf_addr_sin = (struct sockaddr_in *)ifa->ifa_addr;
  383. intf_addr_mask = (struct sockaddr_in *)ifa->ifa_netmask;
  384. if (intf_addr_sin->sin_family != AF_INET)
  385. continue;
  386. if ( bindnet_sin->sin_family == AF_INET &&
  387. (intf_addr_sin->sin_addr.s_addr & intf_addr_mask->sin_addr.s_addr) ==
  388. (bindnet_sin->sin_addr.s_addr & intf_addr_mask->sin_addr.s_addr)) {
  389. totemip_copy(boundto, bindnet);
  390. memcpy(boundto->addr, &intf_addr_sin->sin_addr, sizeof(intf_addr_sin->sin_addr));
  391. /* Get interface infos
  392. */
  393. *interface_up = ifa->ifa_flags & IFF_UP;
  394. *interface_num = if_nametoindex(ifa->ifa_name);
  395. /*
  396. * Handle case, when nodeid is set to 0 or not set.
  397. */
  398. if (bindnet->family == AF_INET && bindnet->nodeid == 0) {
  399. unsigned int nodeid = 0;
  400. memcpy (&nodeid, boundto->addr, sizeof (int));
  401. #if _BYTE_ORDER == _BIG_ENDIAN
  402. nodeid = swab32 (nodeid);
  403. #endif
  404. /*
  405. * Mask 32nd bit off to workaround bugs in other peoples code
  406. * (if configuration requests it).
  407. */
  408. if (mask_high_bit) {
  409. nodeid &= 0x7FFFFFFF;
  410. }
  411. boundto->nodeid = nodeid;
  412. }
  413. res = 0;
  414. break; /* for */
  415. }
  416. }
  417. freeifaddrs(ifap);
  418. return (res);
  419. }
  420. #elif defined(COROSYNC_LINUX)
  421. static void parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
  422. {
  423. while (RTA_OK(rta, len)) {
  424. if (rta->rta_type <= max)
  425. tb[rta->rta_type] = rta;
  426. rta = RTA_NEXT(rta,len);
  427. }
  428. }
  429. int totemip_iface_check(struct totem_ip_address *bindnet,
  430. struct totem_ip_address *boundto,
  431. int *interface_up,
  432. int *interface_num,
  433. int mask_high_bit)
  434. {
  435. int fd;
  436. int res = -1;
  437. struct {
  438. struct nlmsghdr nlh;
  439. struct rtgenmsg g;
  440. } req;
  441. struct sockaddr_nl nladdr;
  442. struct totem_ip_address ipaddr;
  443. static char rcvbuf[NETLINK_BUFSIZE];
  444. *interface_up = 0;
  445. *interface_num = 0;
  446. memset(&ipaddr, 0, sizeof(ipaddr));
  447. /* Make sure we preserve these */
  448. ipaddr.family = bindnet->family;
  449. ipaddr.nodeid = bindnet->nodeid;
  450. /* Ask netlink for a list of interface addresses */
  451. fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  452. if (fd <0)
  453. return -1;
  454. setsockopt(fd,SOL_SOCKET,SO_RCVBUF,&rcvbuf,sizeof(rcvbuf));
  455. memset(&nladdr, 0, sizeof(nladdr));
  456. nladdr.nl_family = AF_NETLINK;
  457. memset(&req, 0, sizeof(req));
  458. req.nlh.nlmsg_len = sizeof(req);
  459. req.nlh.nlmsg_type = RTM_GETADDR;
  460. req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
  461. req.nlh.nlmsg_pid = 0;
  462. req.nlh.nlmsg_seq = 1;
  463. req.g.rtgen_family = bindnet->family;
  464. if (sendto(fd, (void *)&req, sizeof(req), 0,
  465. (struct sockaddr*)&nladdr, sizeof(nladdr)) < 0) {
  466. close(fd);
  467. return -1;
  468. }
  469. /* Look through the return buffer for our address */
  470. while (1)
  471. {
  472. int status;
  473. struct nlmsghdr *h;
  474. struct iovec iov = { rcvbuf, sizeof(rcvbuf) };
  475. struct msghdr msg = {
  476. (void*)&nladdr, sizeof(nladdr),
  477. &iov, 1,
  478. NULL, 0,
  479. 0
  480. };
  481. status = recvmsg(fd, &msg, 0);
  482. if (!status) {
  483. close(fd);
  484. return -1;
  485. }
  486. h = (struct nlmsghdr *)rcvbuf;
  487. if (h->nlmsg_type == NLMSG_DONE)
  488. break;
  489. if (h->nlmsg_type == NLMSG_ERROR) {
  490. close(fd);
  491. return -1;
  492. }
  493. while (NLMSG_OK(h, status)) {
  494. if (h->nlmsg_type == RTM_NEWADDR) {
  495. struct ifaddrmsg *ifa = NLMSG_DATA(h);
  496. struct rtattr *tb[IFA_MAX+1];
  497. int len = h->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa));
  498. int found_if = 0;
  499. memset(tb, 0, sizeof(tb));
  500. parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len);
  501. memcpy(ipaddr.addr, RTA_DATA(tb[IFA_ADDRESS]), TOTEMIP_ADDRLEN);
  502. if (totemip_equal(&ipaddr, bindnet)) {
  503. found_if = 1;
  504. }
  505. /* If the address we have is an IPv4 network address, then
  506. substitute the actual IP address of this interface */
  507. if (!found_if && tb[IFA_LOCAL] && ifa->ifa_family == AF_INET) {
  508. uint32_t network;
  509. uint32_t addr;
  510. uint32_t netmask = htonl(~((1<<(32-ifa->ifa_prefixlen))-1));
  511. memcpy(&network, RTA_DATA(tb[IFA_LOCAL]), sizeof(uint32_t));
  512. memcpy(&addr, bindnet->addr, sizeof(uint32_t));
  513. if ((addr & netmask) == (network & netmask)) {
  514. memcpy(ipaddr.addr, RTA_DATA(tb[IFA_ADDRESS]), TOTEMIP_ADDRLEN);
  515. found_if = 1;
  516. }
  517. }
  518. if (found_if) {
  519. /* Found it - check I/F is UP */
  520. struct ifreq ifr;
  521. int ioctl_fd; /* Can't do ioctls on netlink FDs */
  522. ioctl_fd = socket(AF_INET, SOCK_STREAM, 0);
  523. if (ioctl_fd < 0) {
  524. close(fd);
  525. return -1;
  526. }
  527. memset(&ifr, 0, sizeof(ifr));
  528. ifr.ifr_ifindex = ifa->ifa_index;
  529. /* SIOCGIFFLAGS needs an interface name */
  530. status = ioctl(ioctl_fd, SIOCGIFNAME, &ifr);
  531. status = ioctl(ioctl_fd, SIOCGIFFLAGS, &ifr);
  532. close(ioctl_fd);
  533. if (status) {
  534. res = -1;
  535. goto finished;
  536. }
  537. if (ifr.ifr_flags & IFF_UP)
  538. *interface_up = 1;
  539. *interface_num = ifa->ifa_index;
  540. /*
  541. * Mask 32nd bit off to workaround bugs in other peoples code
  542. * (if configuration requests it).
  543. */
  544. if (ipaddr.family == AF_INET && ipaddr.nodeid == 0) {
  545. unsigned int nodeid = 0;
  546. memcpy (&nodeid, ipaddr.addr, sizeof (int));
  547. #if __BYTE_ORDER == __BIG_ENDIAN
  548. nodeid = swab32 (nodeid);
  549. #endif
  550. if (mask_high_bit) {
  551. nodeid &= 0x7FFFFFFF;
  552. }
  553. ipaddr.nodeid = nodeid;
  554. }
  555. totemip_copy (boundto, &ipaddr);
  556. res = 0;
  557. goto finished;
  558. }
  559. }
  560. h = NLMSG_NEXT(h, status);
  561. }
  562. }
  563. res = -1; /* address not found */
  564. finished:
  565. close(fd);
  566. return res;
  567. }
  568. #endif /* COROSYNC_LINUX */