+ if (pd->debug_p)
+ fprintf (stderr, "%s: listing network interfaces\n", progname);
+
+ getifaddrs (&all);
+ for (ifa = all; ifa; ifa = ifa->ifa_next)
+ {
+ struct in_addr in2;
+ unsigned long mask;
+ if (ifa->ifa_addr->sa_family != AF_INET)
+ {
+ if (pd->debug_p)
+ fprintf (stderr, "%s: if: %4s: %s\n", progname,
+ ifa->ifa_name,
+ (
+# ifdef AF_UNIX
+ ifa->ifa_addr->sa_family == AF_UNIX ? "local" :
+# endif
+# ifdef AF_LINK
+ ifa->ifa_addr->sa_family == AF_LINK ? "link" :
+# endif
+# ifdef AF_INET6
+ ifa->ifa_addr->sa_family == AF_INET6 ? "ipv6" :
+# endif
+ "other"));
+ continue;
+ }
+ in2 = ((struct sockaddr_in *) ifa->ifa_addr)->sin_addr;
+ mask = ntohl (((struct sockaddr_in *) ifa->ifa_netmask)
+ ->sin_addr.s_addr);
+ if (pd->debug_p)
+ fprintf (stderr, "%s: if: %4s: inet = %s /%d 0x%08lx\n",
+ progname,
+ ifa->ifa_name,
+ inet_ntoa (in2),
+ mask_width (mask),
+ mask);
+ if (in2.s_addr == 0x0100007f || /* 127.0.0.1 in network order */
+ mask == 0)
+ continue;
+
+ /* At least on the AT&T 3G network, pinging either of the two
+ hosts on a /31 network doesn't work, so don't try.
+ */
+ if (mask_width (mask) == 31)
+ {
+ sprintf (buf,
+ "Can't ping subnet:\n"
+ "local network is\n"
+ "%.100s/%d,\n"
+ "a p2p bridge\n"
+ "on if %.100s.",
+ inet_ntoa (in2), mask_width (mask), ifa->ifa_name);
+ if (*error_ret) free (*error_ret);
+ *error_ret = strdup (buf);
+ continue;
+ }
+
+ in = in2;
+ subnet_width = mask_width (mask);
+ }
+
+ if (in.s_addr)
+ {
+ if (*error_ret) free (*error_ret);
+ *error_ret = 0;
+ n_base = in.s_addr; /* already in network order, I think? */
+ }
+ else if (!*error_ret)
+ *error_ret = strdup ("Unable to determine\nlocal IP address\n");
+
+ if (all)
+ freeifaddrs (all);
+
+ if (*error_ret)
+ return 0;
+
+# else /* !HAVE_GETIFADDRS */
+
+ /* If we can't walk the list of network interfaces to figure out
+ our local IP address, try to do it by finding the local host
+ name, then resolving that.
+ */
+ char hostname[BUFSIZ];
+ struct hostent *hent = 0;
+
+ if (gethostname(hostname, BUFSIZ))
+ {
+ *error_ret = strdup ("Unable to determine\n"
+ "local host name!");
+ return 0;
+ }
+
+ /* Get our IP address and convert it to a string */
+
+ hent = gethostbyname(hostname);
+ if (! hent)
+ {
+ strcat (hostname, ".local"); /* Necessary on iphone */
+ hent = gethostbyname(hostname);
+ }
+
+ if (! hent)
+ {
+ sprintf(buf,
+ "Unable to resolve\n"
+ "local host \"%.100s\"",
+ hostname);
+ *error_ret = strdup(buf);
+ return 0;
+ }
+
+ strcpy (address, inet_ntoa(*((struct in_addr *)hent->h_addr_list[0])));
+ n_base = pack_addr (hent->h_addr_list[0][0],
+ hent->h_addr_list[0][1],
+ hent->h_addr_list[0][2],
+ hent->h_addr_list[0][3]);
+
+ if (n_base == 0x0100007f) /* 127.0.0.1 in network order */
+ {
+ unsigned int a, b, c, d;
+ unpack_addr (n_base, &a, &b, &c, &d);
+ sprintf (buf,
+ "Unable to determine\n"
+ "local subnet address:\n"
+ "\"%.100s\"\n"
+ "resolves to\n"
+ "loopback address\n"
+ "%u.%u.%u.%u.",
+ hostname, a, b, c, d);
+ *error_ret = strdup(buf);
+ return 0;
+ }
+
+# endif /* !HAVE_GETIFADDRS */