return false;
}
ret = bind(s, (struct sockaddr *)&ip, sizeof(ip));
- if (ret) {
- goto finished;
- }
-
-
- /* find out how much space we need to store the interface details */
- ifc.ifc_len = 0;
- ifc.ifc_req = NULL;
- ret = ioctl(s, SIOCGIFCONF, &ifc);
- if (ret) {
- DEBUG(0,(__location__ " ioctl to read interface list failed\n"));
- goto finished;
- }
-
- ifr = talloc_size(mem_ctx, ifc.ifc_len);
-
- /* get a list of all interface names and addresses */
- ifc.ifc_req = ifr;
- ret = ioctl(s, SIOCGIFCONF, &ifc);
- if (ret) {
- DEBUG(0,(__location__ " ioctl to read interface list failed\n"));
- goto finished;
- }
+
- /* loop over all interfaces and search for the one matching ip */
- num_ifs = ifc.ifc_len/sizeof(struct ifreq);
- for (i=0; i<num_ifs;i++) {
- struct sockaddr_in *sa;
-
- /* we only care bout ipv4 addresses */
- sa = (struct sockaddr_in *)&ifr[i].ifr_addr;
- if (sa->sin_family != AF_INET) {
- continue;
- }
-
- /* this is not the interface you are looking for */
- if (!ctdb_same_ip(sa, &ip)) {
- continue;
- }
-
- /* this is the ifr entry for this interface/address
- read the interface flags so we can tell if it is
- loopback or not
- */
- ret = ioctl(s, SIOCGIFFLAGS, &ifr[i]);
- if (ret) {
- DEBUG(0,(__location__ " failed to read interface flags for interface %s\n", ifr[i].ifr_name));
- goto finished;
- }
-
- /* was this ip tied to a loopback interface ? */
- if (ifr[i].ifr_flags & IFF_LOOPBACK) {
- if (is_loopback != NULL) {
- *is_loopback = true;
- }
- }
-
- if (ifname) {
- *ifname = talloc_asprintf(mem_ctx, "%s", ifr[i].ifr_name);
- }
-
- /* if we got this far, we have found our interface so we can
- exit the loop.
- */
- break;
- }
-
- finished:
- talloc_free(ifr);
close(s);
return ret == 0;
}