-/*
- * Return the interface list
- */
-static int
-ifaddrlist(struct IFADDRLIST **ipaddrp)
-{
- enum { IFREQ_BUFSIZE = (32 * 1024) / sizeof(struct ifreq) };
-
- int fd, nipaddr;
-#ifdef HAVE_SOCKADDR_SA_LEN
- int n;
-#endif
- struct ifreq *ifrp, *ifend, *ifnext;
- struct sockaddr_in *addr_sin;
- struct IFADDRLIST *al;
- struct ifconf ifc;
- struct ifreq ifr;
- /* Was on stack, but 32k is a bit too much: */
- struct ifreq *ibuf = xmalloc(IFREQ_BUFSIZE * sizeof(ibuf[0]));
- struct IFADDRLIST *st_ifaddrlist;
-
- fd = xsocket(AF_INET, SOCK_DGRAM, 0);
-
- ifc.ifc_len = IFREQ_BUFSIZE * sizeof(ibuf[0]);
- ifc.ifc_buf = (caddr_t)ibuf;
-
- if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0
- || ifc.ifc_len < sizeof(struct ifreq)
- ) {
- if (errno == EINVAL)
- bb_error_msg_and_die(
- "SIOCGIFCONF: ifreq struct too small (%d bytes)",
- IFREQ_BUFSIZE * sizeof(ibuf[0]));
- bb_perror_msg_and_die("SIOCGIFCONF");
- }
- ifrp = ibuf;
- ifend = (struct ifreq *)((char *)ibuf + ifc.ifc_len);
-
- nipaddr = 1 + (ifc.ifc_len / sizeof(struct ifreq));
- st_ifaddrlist = xzalloc(nipaddr * sizeof(struct IFADDRLIST));
- al = st_ifaddrlist;
- nipaddr = 0;
-
- for (; ifrp < ifend; ifrp = ifnext) {
-#ifdef HAVE_SOCKADDR_SA_LEN
- n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name);
- if (n < sizeof(*ifrp))
- ifnext = ifrp + 1;
- else
- ifnext = (struct ifreq *)((char *)ifrp + n);
- if (ifrp->ifr_addr.sa_family != AF_INET)
- continue;
-#else
- ifnext = ifrp + 1;
-#endif
- /*
- * Need a template to preserve address info that is
- * used below to locate the next entry. (Otherwise,
- * SIOCGIFFLAGS stomps over it because the requests
- * are returned in a union.)
- */
- strncpy(ifr.ifr_name, ifrp->ifr_name, sizeof(ifr.ifr_name));
- if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) < 0) {
- if (errno == ENXIO)
- continue;
- bb_perror_msg_and_die("SIOCGIFFLAGS: %.*s",
- (int)sizeof(ifr.ifr_name), ifr.ifr_name);
- }
-
- /* Must be up */
- if ((ifr.ifr_flags & IFF_UP) == 0)
- continue;
-
- safe_strncpy(al->device, ifr.ifr_name, sizeof(ifr.ifr_name) + 1);
-#ifdef sun
- /* Ignore sun virtual interfaces */
- if (strchr(al->device, ':') != NULL)
- continue;
-#endif
- if (ioctl(fd, SIOCGIFADDR, (char *)&ifr) < 0)
- bb_perror_msg_and_die("SIOCGIFADDR: %s", al->device);
-
- addr_sin = (struct sockaddr_in *)&ifr.ifr_addr;
- al->addr = addr_sin->sin_addr.s_addr;
- ++al;
- ++nipaddr;
- }
- if (nipaddr == 0)
- bb_error_msg_and_die("can't find any network interfaces");
-
- free(ibuf);
- close(fd);
- *ipaddrp = st_ifaddrlist;
- return nipaddr;
-}
-
-
-static void
-setsin(struct sockaddr_in *addr_sin, uint32_t addr)
-{
- memset(addr_sin, 0, sizeof(*addr_sin));
-#ifdef HAVE_SOCKADDR_SA_LEN
- addr_sin->sin_len = sizeof(*addr_sin);
-#endif
- addr_sin->sin_family = AF_INET;
- addr_sin->sin_addr.s_addr = addr;
-}
-
-
-/*
- * Return the source address for the given destination address
- */
-static void
-findsaddr(const struct sockaddr_in *to, struct sockaddr_in *from)
-{
- int i, n;
- FILE *f;
- uint32_t mask;
- uint32_t dest, tmask;
- struct IFADDRLIST *al;
- char buf[256], tdevice[256], device[256];
-
- f = xfopen("/proc/net/route", "r");
-
- /* Find the appropriate interface */
- n = 0;
- mask = 0;
- device[0] = '\0';
- while (fgets(buf, sizeof(buf), f) != NULL) {
- ++n;
- if (n == 1 && strncmp(buf, "Iface", 5) == 0)
- continue;
- i = sscanf(buf, "%255s %x %*s %*s %*s %*s %*s %x",
- tdevice, &dest, &tmask);
- if (i != 3)
- bb_error_msg_and_die("junk in buffer");
- if ((to->sin_addr.s_addr & tmask) == dest
- && (tmask > mask || mask == 0)
- ) {
- mask = tmask;
- strcpy(device, tdevice);
- }
- }
- fclose(f);
-
- if (device[0] == '\0')
- bb_error_msg_and_die("can't find interface");
-
- /* Get the interface address list */
- n = ifaddrlist(&al);
-
- /* Find our appropriate source address */
- for (i = n; i > 0; --i, ++al)
- if (strcmp(device, al->device) == 0)
- break;
- if (i <= 0)
- bb_error_msg_and_die("can't find interface %s", device);
-
- setsin(from, al->addr);
-}
-
-/*
-"Usage: %s [-dFIlnrvx] [-g gateway] [-i iface] [-f first_ttl]\n"
-"\t[-m max_ttl] [ -p port] [-q nqueries] [-s src_addr] [-t tos]\n"
-"\t[-w waittime] [-z pausemsecs] host [packetlen]"
-
-*/