totemip.c 18 KB

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