2012-04-28 02:55:19 +00:00
|
|
|
/*
|
|
|
|
* Derived from https://github.com/ajrisi/lsif/blob/master/lsif.c
|
|
|
|
* No copyright information in the file, and published publicly,
|
|
|
|
* so presume no rights reserved.
|
|
|
|
*
|
|
|
|
* This method doesn't work properly on OSX, but is for Android where no other
|
|
|
|
* option seems to work. Should work on any linux system.
|
|
|
|
* ********************************
|
|
|
|
*
|
|
|
|
* Updated code to obtain IP and MAC address for all "up" network
|
|
|
|
* interfaces on a linux system. Now IPv6 friendly and updated to use
|
|
|
|
* inet_ntop instead of the deprecated inet_ntoa function. This version
|
|
|
|
* should not seg fault on newer linux systems
|
|
|
|
*
|
|
|
|
* Version 2.0
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Adam Pierce
|
|
|
|
* Adam Risi
|
|
|
|
* William Schaub
|
|
|
|
*
|
|
|
|
* Date: 11/11/2009
|
|
|
|
* http://www.adamrisi.com
|
|
|
|
* http://www.doctort.org/adam/
|
|
|
|
* http://teotwawki.steubentech.com/
|
|
|
|
*
|
|
|
|
*/
|
2012-05-10 22:47:37 +00:00
|
|
|
|
2012-06-08 07:01:59 +00:00
|
|
|
#include <assert.h>
|
2012-05-10 22:48:58 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#if __MACH__ || __NetBSD__ || __OpenBSD__ || __FreeBSD__
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#endif
|
|
|
|
/* Include sockio.h if needed */
|
|
|
|
#ifndef SIOCGIFCONF
|
|
|
|
#include <sys/sockio.h>
|
|
|
|
#endif
|
|
|
|
#include <netinet/if_ether.h>
|
|
|
|
#if __MACH__
|
|
|
|
#include <net/if_dl.h>
|
|
|
|
#endif
|
2013-07-19 01:04:24 +00:00
|
|
|
#ifdef HAVE_LINUX_IF_H
|
|
|
|
#include <linux/if.h>
|
|
|
|
#else
|
2013-08-29 06:20:13 +00:00
|
|
|
#if HAVE_NET_IF_H || __MACH__ || __NetBSD__ || __OpenBSD__ || __FreeBSD__
|
2012-07-02 03:49:54 +00:00
|
|
|
#include <net/if.h>
|
2013-07-19 01:04:24 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
2013-08-29 06:20:13 +00:00
|
|
|
#ifdef HAVE_IFADDRS_H
|
|
|
|
#include <ifaddrs.h>
|
|
|
|
#endif
|
2012-05-10 22:48:58 +00:00
|
|
|
|
2013-12-09 07:15:47 +00:00
|
|
|
#include "conf.h"
|
|
|
|
#include "overlay_interface.h"
|
|
|
|
|
2012-04-28 02:55:19 +00:00
|
|
|
/* On platforms that have variable length
|
|
|
|
ifreq use the old fixed length interface instead */
|
|
|
|
#ifdef OSIOCGIFCONF
|
|
|
|
#undef SIOCGIFCONF
|
|
|
|
#define SIOCGIFCONF OSIOCGIFCONF
|
|
|
|
#undef SIOCGIFADDR
|
|
|
|
#define SIOCGIFADDR OSIOCGIFADDR
|
|
|
|
#undef SIOCGIFBRDADDR
|
|
|
|
#define SIOCGIFBRDADDR OSIOCGIFBRDADDR
|
|
|
|
#endif
|
|
|
|
|
2012-05-28 05:24:33 +00:00
|
|
|
#ifdef linux
|
2012-05-03 13:16:00 +00:00
|
|
|
/* for when all other options fail, as can happen on Android,
|
|
|
|
if the permissions for the socket-based method are broken.
|
|
|
|
Down side is that it while it gets the interface name and
|
|
|
|
broadcast, it doesn't get the local address for that
|
|
|
|
interface.
|
|
|
|
*/
|
|
|
|
int scrapeProcNetRoute()
|
|
|
|
{
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.overlayinterfaces) DEBUG("called");
|
2012-06-08 07:01:59 +00:00
|
|
|
|
2012-05-03 13:16:00 +00:00
|
|
|
FILE *f=fopen("/proc/net/route","r");
|
2013-03-18 06:09:08 +00:00
|
|
|
if (!f)
|
|
|
|
return WHY_perror("fopen(\"/proc/net/route\")");
|
2012-05-03 13:16:00 +00:00
|
|
|
|
|
|
|
char line[1024],name[1024],dest[1024],mask[1024];
|
|
|
|
|
|
|
|
/* skip header line */
|
2013-03-18 06:09:08 +00:00
|
|
|
line[0] = '\0';
|
|
|
|
if (fgets(line,1024,f) == NULL)
|
|
|
|
return WHYF_perror("fgets(%p,1024,\"/proc/net/route\")", line);
|
2012-05-03 13:16:00 +00:00
|
|
|
|
2013-03-18 06:09:08 +00:00
|
|
|
line[0] = '\0';
|
|
|
|
if (fgets(line,1024,f) == NULL)
|
|
|
|
return WHYF_perror("fgets(%p,1024,\"/proc/net/route\")", line);
|
2013-12-09 07:15:47 +00:00
|
|
|
|
|
|
|
struct socket_address addr, broadcast;
|
|
|
|
bzero(&addr, sizeof(addr));
|
|
|
|
bzero(&broadcast, sizeof(broadcast));
|
|
|
|
addr.addrlen = sizeof(addr.inet);
|
|
|
|
addr.inet.sin_family = AF_INET;
|
|
|
|
broadcast.addrlen = sizeof(addr.inet);
|
|
|
|
broadcast.inet.sin_family = AF_INET;
|
|
|
|
|
2012-05-03 13:16:00 +00:00
|
|
|
while(line[0]) {
|
|
|
|
int r;
|
2013-03-18 06:09:08 +00:00
|
|
|
if ((r=sscanf(line,"%s %s %*s %*s %*s %*s %*s %s",name,dest,mask))==3) {
|
2013-12-09 07:15:47 +00:00
|
|
|
addr.inet.sin_addr.s_addr=strtol(dest,NULL,16);
|
2013-03-18 06:09:08 +00:00
|
|
|
struct in_addr netmask = {.s_addr=strtol(mask,NULL,16)};
|
2013-12-09 07:15:47 +00:00
|
|
|
broadcast.inet.sin_addr.s_addr=addr.inet.sin_addr.s_addr | ~netmask.s_addr;
|
|
|
|
overlay_interface_register(name,&addr,&broadcast);
|
2013-03-18 06:09:08 +00:00
|
|
|
}
|
|
|
|
line[0] = '\0';
|
|
|
|
if (fgets(line,1024,f) == NULL)
|
|
|
|
return WHYF_perror("fgets(%p,1024,\"/proc/net/route\")", line);
|
2012-05-03 13:16:00 +00:00
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-05-28 05:24:33 +00:00
|
|
|
#endif
|
2012-05-03 13:16:00 +00:00
|
|
|
|
2012-05-28 05:24:33 +00:00
|
|
|
#ifdef SIOCGIFCONF
|
2012-06-08 07:01:59 +00:00
|
|
|
|
|
|
|
/* Not present in Linux */
|
|
|
|
#ifndef _SIZEOF_ADDR_IFREQ
|
|
|
|
#define _SIZEOF_ADDR_IFREQ(x) sizeof(struct ifreq)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int
|
|
|
|
lsif(void) {
|
2012-06-20 05:59:46 +00:00
|
|
|
char buf[8192];
|
2012-06-08 07:01:59 +00:00
|
|
|
struct ifconf ifc;
|
2013-12-10 06:22:53 +00:00
|
|
|
int sck;
|
2012-06-08 07:01:59 +00:00
|
|
|
struct ifreq *ifr;
|
2013-12-09 07:15:47 +00:00
|
|
|
struct in_addr netmask;
|
|
|
|
struct socket_address addr, broadcast;
|
|
|
|
bzero(&addr, sizeof(addr));
|
|
|
|
bzero(&broadcast, sizeof(broadcast));
|
|
|
|
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.overlayinterfaces) DEBUG("called");
|
2012-04-28 02:55:19 +00:00
|
|
|
|
|
|
|
/* Get a socket handle. */
|
|
|
|
sck = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
if(sck < 0) {
|
2012-06-08 07:01:59 +00:00
|
|
|
WHY_perror("socket");
|
2012-04-28 02:55:19 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Query available interfaces. */
|
2013-12-10 06:22:53 +00:00
|
|
|
ifc.ifc_len = sizeof buf;
|
2012-04-28 02:55:19 +00:00
|
|
|
ifc.ifc_buf = buf;
|
|
|
|
if(ioctl(sck, SIOCGIFCONF, &ifc) < 0) {
|
2012-06-08 07:01:59 +00:00
|
|
|
WHY_perror("ioctl(SIOCGIFCONF)");
|
2012-07-25 07:23:44 +00:00
|
|
|
close(sck);
|
2012-04-28 02:55:19 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-12-09 07:15:47 +00:00
|
|
|
broadcast.addrlen = sizeof(addr.inet);
|
|
|
|
broadcast.inet.sin_family = AF_INET;
|
|
|
|
|
2012-04-28 02:55:19 +00:00
|
|
|
/* Iterate through the list of interfaces. */
|
2013-12-10 06:22:53 +00:00
|
|
|
unsigned nInterfaces = 0;
|
|
|
|
unsigned ofs = 0;
|
|
|
|
while (ofs < (unsigned)ifc.ifc_len && ofs < sizeof buf) {
|
2012-06-08 07:01:59 +00:00
|
|
|
ifr = (struct ifreq *)(ifc.ifc_ifcu.ifcu_buf + ofs);
|
|
|
|
ofs += _SIZEOF_ADDR_IFREQ(*ifr);
|
|
|
|
|
|
|
|
/* We're only interested in IPv4 addresses */
|
|
|
|
if (ifr->ifr_ifru.ifru_addr.sa_family != AF_INET) {
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.overlayinterfaces) DEBUGF("Skipping non-AF_INET address on %s", ifr->ifr_name);
|
2012-06-08 07:01:59 +00:00
|
|
|
continue;
|
|
|
|
}
|
2013-12-09 07:15:47 +00:00
|
|
|
|
|
|
|
addr.addrlen = sizeof(addr.inet);
|
|
|
|
bcopy(&ifr->ifr_ifru.ifru_addr, &addr.addr, addr.addrlen);
|
2012-07-25 05:23:53 +00:00
|
|
|
|
2012-06-08 07:01:59 +00:00
|
|
|
/* Get interface flags */
|
|
|
|
if (ioctl(sck, SIOCGIFFLAGS, ifr) == -1)
|
|
|
|
FATAL_perror("ioctl(SIOCGIFFLAGS)");
|
2012-07-25 05:23:53 +00:00
|
|
|
|
2012-06-08 07:01:59 +00:00
|
|
|
/* Not broadcast? Not interested.. */
|
|
|
|
if ((ifr->ifr_ifru.ifru_flags & IFF_BROADCAST) == 0) {
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.overlayinterfaces) DEBUGF("Skipping non-broadcast address on %s", ifr->ifr_name);
|
2012-06-08 07:01:59 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-07-25 05:23:53 +00:00
|
|
|
|
2012-08-08 05:27:27 +00:00
|
|
|
/* Get netmask */
|
|
|
|
if (ioctl(sck, SIOCGIFNETMASK, ifr, sizeof(*ifr)) != 0) {
|
|
|
|
WHY_perror("ioctl(SIOCGIFNETMASK)");
|
2012-07-25 07:23:44 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-08-08 05:27:27 +00:00
|
|
|
netmask = ((struct sockaddr_in *)&ifr->ifr_ifru.ifru_addr)->sin_addr;
|
|
|
|
|
2013-12-09 07:15:47 +00:00
|
|
|
broadcast.inet.sin_addr.s_addr=addr.inet.sin_addr.s_addr | ~netmask.s_addr;
|
|
|
|
|
|
|
|
overlay_interface_register(ifr->ifr_name, &addr, &broadcast);
|
2012-06-08 07:01:59 +00:00
|
|
|
nInterfaces++;
|
2012-04-28 02:55:19 +00:00
|
|
|
}
|
2012-06-08 07:01:59 +00:00
|
|
|
|
2013-12-10 06:22:53 +00:00
|
|
|
if (config.debug.overlayinterfaces) DEBUGF("Examined %u interface addresses", nInterfaces);
|
2012-06-08 07:01:59 +00:00
|
|
|
|
2012-04-28 04:54:57 +00:00
|
|
|
close(sck);
|
2012-04-28 02:55:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-06-08 07:01:59 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_IFADDRS_H
|
2012-06-08 07:30:50 +00:00
|
|
|
int
|
|
|
|
doifaddrs(void) {
|
|
|
|
struct ifaddrs *ifaddr, *ifa;
|
|
|
|
char *name;
|
2013-12-09 07:15:47 +00:00
|
|
|
struct socket_address addr, broadcast;
|
|
|
|
struct in_addr netmask;
|
|
|
|
bzero(&addr, sizeof(addr));
|
|
|
|
bzero(&broadcast, sizeof(broadcast));
|
2012-06-08 07:01:59 +00:00
|
|
|
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.overlayinterfaces) DEBUGF("called");
|
2012-06-08 07:01:59 +00:00
|
|
|
|
2012-06-08 07:30:50 +00:00
|
|
|
if (getifaddrs(&ifaddr) == -1)
|
|
|
|
return WHY_perror("getifaddr()");
|
2012-06-08 07:01:59 +00:00
|
|
|
|
2013-12-09 07:15:47 +00:00
|
|
|
broadcast.addrlen = sizeof(addr.inet);
|
|
|
|
broadcast.inet.sin_family = AF_INET;
|
|
|
|
|
2012-06-08 07:30:50 +00:00
|
|
|
for (ifa = ifaddr; ifa != NULL ; ifa = ifa->ifa_next) {
|
2012-11-01 03:30:18 +00:00
|
|
|
if (!ifa->ifa_addr || !ifa->ifa_netmask)
|
|
|
|
continue;
|
|
|
|
|
2012-06-08 07:30:50 +00:00
|
|
|
/* We're only interested in IPv4 addresses */
|
|
|
|
if (ifa->ifa_addr->sa_family != AF_INET) {
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.overlayinterfaces) DEBUGF("Skipping non-AF_INET address on %s", ifa->ifa_name);
|
2012-06-08 07:30:50 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Not broadcast? Not interested.. */
|
|
|
|
if ((ifa->ifa_flags & IFF_BROADCAST) == 0) {
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.overlayinterfaces) DEBUGF("Skipping non-broadcast address on %s", ifa->ifa_name);
|
2012-06-08 07:30:50 +00:00
|
|
|
continue;
|
2012-06-08 07:01:59 +00:00
|
|
|
}
|
2012-06-08 07:30:50 +00:00
|
|
|
|
|
|
|
name = ifa->ifa_name;
|
2013-12-09 07:15:47 +00:00
|
|
|
addr.addrlen = sizeof(addr.inet);
|
|
|
|
bcopy(ifa->ifa_addr, &addr.addr, addr.addrlen);
|
|
|
|
|
2012-08-08 05:27:27 +00:00
|
|
|
netmask = ((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr;
|
2013-12-09 07:15:47 +00:00
|
|
|
broadcast.inet.sin_addr.s_addr=addr.inet.sin_addr.s_addr | ~netmask.s_addr;
|
2012-06-08 07:30:50 +00:00
|
|
|
|
2013-12-09 07:15:47 +00:00
|
|
|
overlay_interface_register(name, &addr, &broadcast);
|
2012-06-08 07:01:59 +00:00
|
|
|
}
|
|
|
|
freeifaddrs(ifaddr);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-04-28 02:55:19 +00:00
|
|
|
#endif
|
2012-06-08 07:01:59 +00:00
|
|
|
|