totemip.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. /*
  2. * Copyright (c) 2005-2011 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. memcpy (&addr, ip_addr->addr, sizeof (uint32_t));
  119. if (ip_addr->family == AF_INET) {
  120. addr = ntohl(addr);
  121. if ((addr >> 28) != 0xE) {
  122. return -1;
  123. }
  124. }
  125. return 0;
  126. }
  127. /* For sorting etc. params are void * for qsort's benefit */
  128. int totemip_compare(const void *a, const void *b)
  129. {
  130. int i;
  131. const struct totem_ip_address *totemip_a = (const struct totem_ip_address *)a;
  132. const struct totem_ip_address *totemip_b = (const struct totem_ip_address *)b;
  133. struct in_addr ipv4_a1;
  134. struct in_addr ipv4_a2;
  135. struct in6_addr ipv6_a1;
  136. struct in6_addr ipv6_a2;
  137. unsigned short family;
  138. /*
  139. * Use memcpy to align since totem_ip_address is unaligned on various archs
  140. */
  141. memcpy (&family, &totemip_a->family, sizeof (unsigned short));
  142. if (family == AF_INET) {
  143. memcpy (&ipv4_a1, totemip_a->addr, sizeof (struct in_addr));
  144. memcpy (&ipv4_a2, totemip_b->addr, sizeof (struct in_addr));
  145. if (ipv4_a1.s_addr == ipv4_a2.s_addr) {
  146. return (0);
  147. }
  148. if (htonl(ipv4_a1.s_addr) < htonl(ipv4_a2.s_addr)) {
  149. return -1;
  150. } else {
  151. return +1;
  152. }
  153. } else
  154. if (family == AF_INET6) {
  155. /*
  156. * We can only compare 8 bits at time for portability reasons
  157. */
  158. memcpy (&ipv6_a1, totemip_a->addr, sizeof (struct in6_addr));
  159. memcpy (&ipv6_a2, totemip_b->addr, sizeof (struct in6_addr));
  160. for (i = 0; i < 16; i++) {
  161. int res = ipv6_a1.s6_addr[i] -
  162. ipv6_a2.s6_addr[i];
  163. if (res) {
  164. return res;
  165. }
  166. }
  167. return 0;
  168. } else {
  169. /*
  170. * Family not set, should be!
  171. */
  172. assert (0);
  173. }
  174. return 0;
  175. }
  176. /* Build a localhost totem_ip_address */
  177. int totemip_localhost(int family, struct totem_ip_address *localhost)
  178. {
  179. const char *addr_text;
  180. memset (localhost, 0, sizeof (struct totem_ip_address));
  181. if (family == AF_INET) {
  182. addr_text = LOCALHOST_IPV4;
  183. if (inet_pton(family, addr_text, (char *)&localhost->nodeid) <= 0) {
  184. return -1;
  185. }
  186. } else {
  187. addr_text = LOCALHOST_IPV6;
  188. }
  189. if (inet_pton(family, addr_text, (char *)localhost->addr) <= 0)
  190. return -1;
  191. localhost->family = family;
  192. return 0;
  193. }
  194. int totemip_localhost_check(const struct totem_ip_address *addr)
  195. {
  196. struct totem_ip_address localhost;
  197. if (totemip_localhost(addr->family, &localhost))
  198. return 0;
  199. return totemip_equal(addr, &localhost);
  200. }
  201. const char *totemip_print(const struct totem_ip_address *addr)
  202. {
  203. static char buf[INET6_ADDRSTRLEN];
  204. return (inet_ntop(addr->family, addr->addr, buf, sizeof(buf)));
  205. }
  206. /* Make a totem_ip_address into a usable sockaddr_storage */
  207. int totemip_totemip_to_sockaddr_convert(struct totem_ip_address *ip_addr,
  208. uint16_t port, struct sockaddr_storage *saddr, int *addrlen)
  209. {
  210. int ret = -1;
  211. if (ip_addr->family == AF_INET) {
  212. struct sockaddr_in *sin = (struct sockaddr_in *)saddr;
  213. memset(sin, 0, sizeof(struct sockaddr_in));
  214. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  215. sin->sin_len = sizeof(struct sockaddr_in);
  216. #endif
  217. sin->sin_family = ip_addr->family;
  218. sin->sin_port = ntohs(port);
  219. memcpy(&sin->sin_addr, ip_addr->addr, sizeof(struct in_addr));
  220. *addrlen = sizeof(struct sockaddr_in);
  221. ret = 0;
  222. }
  223. if (ip_addr->family == AF_INET6) {
  224. struct sockaddr_in6 *sin = (struct sockaddr_in6 *)saddr;
  225. memset(sin, 0, sizeof(struct sockaddr_in6));
  226. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  227. sin->sin6_len = sizeof(struct sockaddr_in6);
  228. #endif
  229. sin->sin6_family = ip_addr->family;
  230. sin->sin6_port = ntohs(port);
  231. sin->sin6_scope_id = 2;
  232. memcpy(&sin->sin6_addr, ip_addr->addr, sizeof(struct in6_addr));
  233. *addrlen = sizeof(struct sockaddr_in6);
  234. ret = 0;
  235. }
  236. return ret;
  237. }
  238. /* Converts an address string string into a totem_ip_address.
  239. family can be AF_INET, AF_INET6 or 0 ("for "don't care")
  240. */
  241. int totemip_parse(struct totem_ip_address *totemip, const char *addr, int family)
  242. {
  243. struct addrinfo *ainfo;
  244. struct addrinfo ahints;
  245. struct sockaddr_in *sa;
  246. struct sockaddr_in6 *sa6;
  247. int ret;
  248. memset(&ahints, 0, sizeof(ahints));
  249. ahints.ai_socktype = SOCK_DGRAM;
  250. ahints.ai_protocol = IPPROTO_UDP;
  251. ahints.ai_family = family;
  252. /* Lookup the nodename address */
  253. ret = getaddrinfo(addr, NULL, &ahints, &ainfo);
  254. if (ret)
  255. return -1;
  256. sa = (struct sockaddr_in *)ainfo->ai_addr;
  257. sa6 = (struct sockaddr_in6 *)ainfo->ai_addr;
  258. totemip->family = ainfo->ai_family;
  259. if (ainfo->ai_family == AF_INET)
  260. memcpy(totemip->addr, &sa->sin_addr, sizeof(struct in_addr));
  261. else
  262. memcpy(totemip->addr, &sa6->sin6_addr, sizeof(struct in6_addr));
  263. freeaddrinfo(ainfo);
  264. return 0;
  265. }
  266. /* Make a sockaddr_* into a totem_ip_address */
  267. int totemip_sockaddr_to_totemip_convert(const struct sockaddr_storage *saddr,
  268. struct totem_ip_address *ip_addr)
  269. {
  270. int ret = -1;
  271. ip_addr->family = saddr->ss_family;
  272. ip_addr->nodeid = 0;
  273. if (saddr->ss_family == AF_INET) {
  274. const struct sockaddr_in *sin = (const struct sockaddr_in *)saddr;
  275. memcpy(ip_addr->addr, &sin->sin_addr, sizeof(struct in_addr));
  276. ret = 0;
  277. }
  278. if (saddr->ss_family == AF_INET6) {
  279. const struct sockaddr_in6 *sin
  280. = (const struct sockaddr_in6 *)saddr;
  281. memcpy(ip_addr->addr, &sin->sin6_addr, sizeof(struct in6_addr));
  282. ret = 0;
  283. }
  284. return ret;
  285. }
  286. /*
  287. * On Solaris, man if_tcp describes this method
  288. */
  289. #if defined(COROSYNC_SOLARIS)
  290. int totemip_iface_check(struct totem_ip_address *bindnet,
  291. struct totem_ip_address *boundto,
  292. int *interface_up,
  293. int *interface_num,
  294. int mask_high_bit)
  295. {
  296. struct sockaddr_storage bindnet_ss;
  297. struct sockaddr_in *bindnet_sin = (struct sockaddr_in *)&bindnet_ss;
  298. struct sockaddr_in *sockaddr_in;
  299. int id_fd;
  300. struct lifconf lifconf;
  301. struct lifreq *lifreq;
  302. int numreqs = 0;
  303. int i;
  304. in_addr_t mask_addr;
  305. int res = -1;
  306. int addrlen;
  307. totemip_totemip_to_sockaddr_convert (bindnet,
  308. 0, &bindnet_ss, &addrlen);
  309. *interface_up = 0;
  310. id_fd = socket (AF_INET, SOCK_STREAM, 0);
  311. lifconf.lifc_family = AF_UNSPEC;
  312. lifconf.lifc_flags = 0;
  313. lifconf.lifc_buf = NULL;
  314. lifconf.lifc_len = 0;
  315. do {
  316. numreqs += 32;
  317. lifconf.lifc_len = sizeof (struct lifreq) * numreqs;
  318. lifconf.lifc_buf = (void *)realloc(lifconf.lifc_buf, lifconf.lifc_len);
  319. res = ioctl (id_fd, SIOCGLIFCONF, &lifconf);
  320. if (res < 0) {
  321. close (id_fd);
  322. return -1;
  323. }
  324. } while (lifconf.lifc_len == sizeof (struct lifconf) * numreqs);
  325. res = -1;
  326. lifreq = (struct lifreq *)lifconf.lifc_buf;
  327. /*
  328. * Find interface address to bind to
  329. */
  330. for (i = 0; i < lifconf.lifc_len / sizeof (struct lifreq); i++) {
  331. sockaddr_in = (struct sockaddr_in *)&lifreq[i].lifr_addr;
  332. mask_addr = inet_addr ("255.255.255.0");
  333. if ((sockaddr_in->sin_family == AF_INET) &&
  334. (sockaddr_in->sin_addr.s_addr & mask_addr) ==
  335. (bindnet_sin->sin_addr.s_addr & mask_addr)) {
  336. res = i;
  337. /*
  338. * Setup boundto output
  339. */
  340. totemip_sockaddr_to_totemip_convert((struct sockaddr_storage *)sockaddr_in, boundto);
  341. boundto->nodeid = sockaddr_in->sin_addr.s_addr;
  342. #if __BYTE_ORDER == __BIG_ENDIAN
  343. boundto->nodeid = swab32 (boundto->nodeid);
  344. #endif
  345. if (ioctl(id_fd, SIOCGLIFFLAGS, &lifreq[i]) < 0) {
  346. printf ("couldn't do ioctl\n");
  347. }
  348. *interface_up = lifreq[i].lifr_flags & IFF_UP;
  349. if (ioctl(id_fd, SIOCGLIFINDEX, &lifreq[i]) < 0) {
  350. printf ("couldn't do ioctl\n");
  351. }
  352. *interface_num = lifreq[i].lifr_index;
  353. break;
  354. }
  355. }
  356. free (lifconf.lifc_buf);
  357. close (id_fd);
  358. return (res);
  359. }
  360. #endif
  361. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  362. int totemip_iface_check(struct totem_ip_address *bindnet,
  363. struct totem_ip_address *boundto,
  364. int *interface_up,
  365. int *interface_num,
  366. int mask_high_bit)
  367. {
  368. #define NEXT_IFR(a) ((struct ifreq *)((u_char *)&(a)->ifr_addr +\
  369. ((a)->ifr_addr.sa_len ? (a)->ifr_addr.sa_len : sizeof((a)->ifr_addr))))
  370. struct sockaddr_in *intf_addr_mask;
  371. struct sockaddr_storage bindnet_ss;
  372. struct sockaddr_in *intf_addr_sin;
  373. struct sockaddr_in *bindnet_sin = (struct sockaddr_in *)&bindnet_ss;
  374. struct ifaddrs *ifap, *ifa;
  375. int res = -1;
  376. int addrlen;
  377. *interface_up = 0;
  378. *interface_num = 0;
  379. totemip_totemip_to_sockaddr_convert(bindnet,
  380. 0, &bindnet_ss, &addrlen);
  381. if (getifaddrs(&ifap) != 0)
  382. return -1;
  383. for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
  384. intf_addr_sin = (struct sockaddr_in *)ifa->ifa_addr;
  385. intf_addr_mask = (struct sockaddr_in *)ifa->ifa_netmask;
  386. if (intf_addr_sin->sin_family != AF_INET)
  387. continue;
  388. if ( bindnet_sin->sin_family == AF_INET &&
  389. (intf_addr_sin->sin_addr.s_addr & intf_addr_mask->sin_addr.s_addr) ==
  390. (bindnet_sin->sin_addr.s_addr & intf_addr_mask->sin_addr.s_addr)) {
  391. totemip_copy(boundto, bindnet);
  392. memcpy(boundto->addr, &intf_addr_sin->sin_addr, sizeof(intf_addr_sin->sin_addr));
  393. /* Get interface infos
  394. */
  395. *interface_up = ifa->ifa_flags & IFF_UP;
  396. *interface_num = if_nametoindex(ifa->ifa_name);
  397. /*
  398. * Handle case, when nodeid is set to 0 or not set.
  399. */
  400. if (bindnet->family == AF_INET && bindnet->nodeid == 0) {
  401. unsigned int nodeid = 0;
  402. memcpy (&nodeid, boundto->addr, sizeof (int));
  403. #if _BYTE_ORDER == _BIG_ENDIAN
  404. nodeid = swab32 (nodeid);
  405. #endif
  406. /*
  407. * Mask 32nd bit off to workaround bugs in other peoples code
  408. * (if configuration requests it).
  409. */
  410. if (mask_high_bit) {
  411. nodeid &= 0x7FFFFFFF;
  412. }
  413. boundto->nodeid = nodeid;
  414. }
  415. res = 0;
  416. break; /* for */
  417. }
  418. }
  419. freeifaddrs(ifap);
  420. return (res);
  421. }
  422. #elif defined(COROSYNC_LINUX)
  423. static void parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
  424. {
  425. while (RTA_OK(rta, len)) {
  426. if (rta->rta_type <= max)
  427. tb[rta->rta_type] = rta;
  428. rta = RTA_NEXT(rta,len);
  429. }
  430. }
  431. int totemip_iface_check(struct totem_ip_address *bindnet,
  432. struct totem_ip_address *boundto,
  433. int *interface_up,
  434. int *interface_num,
  435. int mask_high_bit)
  436. {
  437. int fd;
  438. int res = -1;
  439. struct {
  440. struct nlmsghdr nlh;
  441. struct rtgenmsg g;
  442. } req;
  443. struct sockaddr_nl nladdr;
  444. struct totem_ip_address ipaddr;
  445. static char rcvbuf[NETLINK_BUFSIZE];
  446. *interface_up = 0;
  447. *interface_num = 0;
  448. memset(&ipaddr, 0, sizeof(ipaddr));
  449. /* Make sure we preserve these */
  450. ipaddr.family = bindnet->family;
  451. ipaddr.nodeid = bindnet->nodeid;
  452. /* Ask netlink for a list of interface addresses */
  453. fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  454. if (fd <0)
  455. return -1;
  456. setsockopt(fd,SOL_SOCKET,SO_RCVBUF,&rcvbuf,sizeof(rcvbuf));
  457. memset(&nladdr, 0, sizeof(nladdr));
  458. nladdr.nl_family = AF_NETLINK;
  459. memset(&req, 0, sizeof(req));
  460. req.nlh.nlmsg_len = sizeof(req);
  461. req.nlh.nlmsg_type = RTM_GETADDR;
  462. req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
  463. req.nlh.nlmsg_pid = 0;
  464. req.nlh.nlmsg_seq = 1;
  465. req.g.rtgen_family = bindnet->family;
  466. if (sendto(fd, (void *)&req, sizeof(req), 0,
  467. (struct sockaddr*)&nladdr, sizeof(nladdr)) < 0) {
  468. close(fd);
  469. return -1;
  470. }
  471. /* Look through the return buffer for our address */
  472. while (1)
  473. {
  474. int status;
  475. struct nlmsghdr *h;
  476. struct iovec iov = { rcvbuf, sizeof(rcvbuf) };
  477. struct msghdr msg = {
  478. (void*)&nladdr, sizeof(nladdr),
  479. &iov, 1,
  480. NULL, 0,
  481. 0
  482. };
  483. status = recvmsg(fd, &msg, 0);
  484. if (!status) {
  485. close(fd);
  486. return -1;
  487. }
  488. h = (struct nlmsghdr *)rcvbuf;
  489. if (h->nlmsg_type == NLMSG_DONE)
  490. break;
  491. if (h->nlmsg_type == NLMSG_ERROR) {
  492. close(fd);
  493. return -1;
  494. }
  495. while (NLMSG_OK(h, status)) {
  496. if (h->nlmsg_type == RTM_NEWADDR) {
  497. struct ifaddrmsg *ifa = NLMSG_DATA(h);
  498. struct rtattr *tb[IFA_MAX+1];
  499. int len = h->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa));
  500. int found_if = 0;
  501. memset(tb, 0, sizeof(tb));
  502. parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len);
  503. memcpy(ipaddr.addr, RTA_DATA(tb[IFA_ADDRESS]), TOTEMIP_ADDRLEN);
  504. if (totemip_equal(&ipaddr, bindnet)) {
  505. found_if = 1;
  506. }
  507. /* If the address we have is an IPv4 network address, then
  508. substitute the actual IP address of this interface */
  509. if (!found_if && tb[IFA_LOCAL] && ifa->ifa_family == AF_INET) {
  510. uint32_t network;
  511. uint32_t addr;
  512. uint32_t netmask = htonl(~((1<<(32-ifa->ifa_prefixlen))-1));
  513. memcpy(&network, RTA_DATA(tb[IFA_LOCAL]), sizeof(uint32_t));
  514. memcpy(&addr, bindnet->addr, sizeof(uint32_t));
  515. if ((addr & netmask) == (network & netmask)) {
  516. memcpy(ipaddr.addr, RTA_DATA(tb[IFA_ADDRESS]), TOTEMIP_ADDRLEN);
  517. found_if = 1;
  518. }
  519. }
  520. if (found_if) {
  521. /* Found it - check I/F is UP */
  522. struct ifreq ifr;
  523. int ioctl_fd; /* Can't do ioctls on netlink FDs */
  524. ioctl_fd = socket(AF_INET, SOCK_STREAM, 0);
  525. if (ioctl_fd < 0) {
  526. close(fd);
  527. return -1;
  528. }
  529. memset(&ifr, 0, sizeof(ifr));
  530. ifr.ifr_ifindex = ifa->ifa_index;
  531. /* SIOCGIFFLAGS needs an interface name */
  532. status = ioctl(ioctl_fd, SIOCGIFNAME, &ifr);
  533. status = ioctl(ioctl_fd, SIOCGIFFLAGS, &ifr);
  534. close(ioctl_fd);
  535. if (status) {
  536. res = -1;
  537. goto finished;
  538. }
  539. if (ifr.ifr_flags & IFF_UP)
  540. *interface_up = 1;
  541. *interface_num = ifa->ifa_index;
  542. /*
  543. * Mask 32nd bit off to workaround bugs in other peoples code
  544. * (if configuration requests it).
  545. */
  546. if (ipaddr.family == AF_INET && ipaddr.nodeid == 0) {
  547. unsigned int nodeid = 0;
  548. memcpy (&nodeid, ipaddr.addr, sizeof (int));
  549. #if __BYTE_ORDER == __BIG_ENDIAN
  550. nodeid = swab32 (nodeid);
  551. #endif
  552. if (mask_high_bit) {
  553. nodeid &= 0x7FFFFFFF;
  554. }
  555. ipaddr.nodeid = nodeid;
  556. }
  557. totemip_copy (boundto, &ipaddr);
  558. res = 0;
  559. goto finished;
  560. }
  561. }
  562. h = NLMSG_NEXT(h, status);
  563. }
  564. }
  565. res = -1; /* address not found */
  566. finished:
  567. close(fd);
  568. return res;
  569. }
  570. #endif /* COROSYNC_LINUX */