ClientCollectors/ARODataCollector/external/libpcap-1.1.1/pcap-linux.c
static int
scan_sys_class_net(pcap_if_t **devlistp, char *errbuf)
{
	DIR *sys_class_net_d;
	int fd;
	struct dirent *ent;
	char *p;
	char name[512];	/* XXX - pick a size */
	char *q, *saveq;
	struct ifreq ifrflags;
	int ret = 1;

	sys_class_net_d = opendir("/sys/class/net");
	if (sys_class_net_d == NULL && errno == ENOENT)
		return (0);

	/*
	 * Create a socket from which to fetch interface information.
	 */
	fd = socket(AF_INET, SOCK_DGRAM, 0);
	if (fd < 0) {
		(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
		    "socket: %s", pcap_strerror(errno));
		return (-1);
	}

	for (;;) {
		errno = 0;
		ent = readdir(sys_class_net_d);
		if (ent == NULL) {
			/*
			 * Error or EOF; if errno != 0, it's an error.
			 */
			break;
		}

		/*
		 * Ignore directories (".", "..", and any subdirectories).
		 */
		if (ent->d_type == DT_DIR)
			continue;

		/*
		 * Get the interface name.
		 */
		p = &ent->d_name[0];
		q = &name[0];
		while (*p != '\0' && isascii(*p) && !isspace(*p)) {
			if (*p == ':') {
				/*
				 * This could be the separator between a
				 * name and an alias number, or it could be
				 * the separator between a name with no
				 * alias number and the next field.
				 *
				 * If there's a colon after digits, it
				 * separates the name and the alias number,
				 * otherwise it separates the name and the
				 * next field.
				 */
				saveq = q;
				while (isascii(*p) && isdigit(*p))
					*q++ = *p++;
				if (*p != ':') {
					/*
					 * That was the next field,
					 * not the alias number.
					 */
					q = saveq;
				}
				break;
			} else
				*q++ = *p++;
		}
		*q = '\0';

		/*
		 * Get the flags for this interface, and skip it if
		 * it's not up.
		 */
		strncpy(ifrflags.ifr_name, name, sizeof(ifrflags.ifr_name));
		if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifrflags) < 0) {
			if (errno == ENXIO)
				continue;
			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
			    "SIOCGIFFLAGS: %.*s: %s",
			    (int)sizeof(ifrflags.ifr_name),
			    ifrflags.ifr_name,
			    pcap_strerror(errno));
			ret = -1;
			break;
		}
		if (!(ifrflags.ifr_flags & IFF_UP))
			continue;

		/*
		 * Add an entry for this interface, with no addresses.
		 */
		if (pcap_add_if(devlistp, name, ifrflags.ifr_flags, NULL,
		    errbuf) == -1) {
			/*
			 * Failure.
			 */
			ret = -1;
			break;
		}
	}
	if (ret != -1) {
		/*
		 * Well, we didn't fail for any other reason; did we
		 * fail due to an error reading the directory?
		 */
		if (errno != 0) {
			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
			    "Error reading /sys/class/net: %s",
			    pcap_strerror(errno));
			ret = -1;
		}
	}

	(void)close(fd);
	(void)closedir(sys_class_net_d);
	return (ret);
}
/*
 * Get from "/sys/class/net" all interfaces listed there; if they're
 * already in the list of interfaces we have, that won't add another
 * instance, but if they're not, that'll add them.
 *
 * We don't bother getting any addresses for them; it appears you can't
 * use SIOCGIFADDR on Linux to get IPv6 addresses for interfaces, and,
 * although some other types of addresses can be fetched with SIOCGIFADDR,
 * we don't bother with them for now.
 *
 * We also don't fail if we couldn't open "/sys/class/net"; we just leave
 * the list of interfaces as is, and return 0, so that we can try
 * scanning /proc/net/dev.
 */
Get from "/sys/class/net" all interfaces listed there; if they're
already in the list of interfaces we have, that won't add another
instance, but if they're not, that'll add them.

We don't bother getting any addresses for them; it appears you can't
use SIOCGIFADDR on Linux to get IPv6 addresses for interfaces, and,
although some other types of addresses can be fetched with SIOCGIFADDR,
we don't bother with them for now.


Get from " / sys / class / net " all interfaces listed there ; if they ' re already in the list of interfaces we have that won ' t add another instance but if they ' re not that ' ll add them . We don ' t bother getting any addresses for them ; it appears you can ' t use SIOCGIFADDR on Linux to get IPv6 addresses for interfaces and although some other types of addresses can be fetched with SIOCGIFADDR we don ' t bother with them for now .
static int
scan_sys_class_net(pcap_if_t **devlistp, char *errbuf)
{
	DIR *sys_class_net_d;
	int fd;
	struct dirent *ent;
	char *p;
	char name[512];	
	char *q, *saveq;
	struct ifreq ifrflags;
	int ret = 1;
	sys_class_net_d = opendir("/sys/class/net");
	if (sys_class_net_d == NULL && errno == ENOENT)
		return (0);
	fd = socket(AF_INET, SOCK_DGRAM, 0);
	if (fd < 0) {
		(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
		    "socket: %s", pcap_strerror(errno));
		return (-1);
	}
	for (;;) {
		errno = 0;
		ent = readdir(sys_class_net_d);
		if (ent == NULL) {
			break;
		}
		if (ent->d_type == DT_DIR)
			continue;
		p = &ent->d_name[0];
		q = &name[0];
		while (*p != '\0' && isascii(*p) && !isspace(*p)) {
			if (*p == ':') {
				saveq = q;
				while (isascii(*p) && isdigit(*p))
					*q++ = *p++;
				if (*p != ':') {
					q = saveq;
				}
				break;
			} else
				*q++ = *p++;
		}
		*q = '\0';
		strncpy(ifrflags.ifr_name, name, sizeof(ifrflags.ifr_name));
		if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifrflags) < 0) {
			if (errno == ENXIO)
				continue;
			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
			    "SIOCGIFFLAGS: %.*s: %s",
			    (int)sizeof(ifrflags.ifr_name),
			    ifrflags.ifr_name,
			    pcap_strerror(errno));
			ret = -1;
			break;
		}
		if (!(ifrflags.ifr_flags & IFF_UP))
			continue;
		if (pcap_add_if(devlistp, name, ifrflags.ifr_flags, NULL,
		    errbuf) == -1) {
			ret = -1;
			break;
		}
	}
	if (ret != -1) {
		if (errno != 0) {
			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
			    "Error reading /sys/class/net: %s",
			    pcap_strerror(errno));
			ret = -1;
		}
	}
	(void)close(fd);
	(void)closedir(sys_class_net_d);
	return (ret);
}
static int scan_sys_class_net ( pcap_if_t * * devlistp , char * errbuf ) { DIR * sys_class_net_d ; int fd ; struct dirent * ent ; char * p ; char name [ 512 ] ; char * q , * saveq ; struct ifreq ifrflags ; int ret = 1 ; sys_class_net_d = opendir ( " " ) ; if ( sys_class_net_d == NULL && errno == ENOENT ) return ( 0 ) ; fd = socket ( AF_INET , SOCK_DGRAM , 0 ) ; if ( fd < 0 ) { ( void ) snprintf ( errbuf , PCAP_ERRBUF_SIZE , " " , pcap_strerror ( errno ) ) ; return ( -1 ) ; } for ( ; ; ) { errno = 0 ; ent = readdir ( sys_class_net_d ) ; if ( ent == NULL ) { break ; } if ( ent -> d_type == DT_DIR ) continue ; p = & ent -> d_name [ 0 ] ; q = & name [ 0 ] ; while ( * p != ' \0 ' && isascii ( * p ) && ! isspace ( * p ) ) { if ( * p == ' ' ) { saveq = q ; while ( isascii ( * p ) && isdigit ( * p ) ) * q ++ = * p ++ ; if ( * p != ' ' ) { q = saveq ; } break ; } else * q ++ = * p ++ ; } * q = ' \0 ' ; strncpy ( ifrflags . ifr_name , name , sizeof ( ifrflags . ifr_name ) ) ; if ( ioctl ( fd , SIOCGIFFLAGS , ( char * ) & ifrflags ) < 0 ) { if ( errno == ENXIO ) continue ; ( void ) snprintf ( errbuf , PCAP_ERRBUF_SIZE , " " , ( int ) sizeof ( ifrflags . ifr_name ) , ifrflags . ifr_name , pcap_strerror ( errno ) ) ; ret = -1 ; break ; } if ( ! ( ifrflags . ifr_flags & IFF_UP ) ) continue ; if ( pcap_add_if ( devlistp , name , ifrflags . ifr_flags , NULL , errbuf ) == -1 ) { ret = -1 ; break ; } } if ( ret != -1 ) { if ( errno != 0 ) { ( void ) snprintf ( errbuf , PCAP_ERRBUF_SIZE , " " , pcap_strerror ( errno ) ) ; ret = -1 ; } } ( void ) close ( fd ) ; ( void ) closedir ( sys_class_net_d ) ; return ( ret ) ; }
Get from "/sys/class/net" all interfaces listed there; if they're
 already in the list of interfaces we have, that won't add another
 instance, but if they're not, that'll add them.
Get from " / sys / class / net " all interfaces listed there ; if they ' re already in the list of interfaces we have that won ' t add another instance but if they ' re not that ' ll add them .
/* XXX - pick a size */ /*
	 * Create a socket from which to fetch interface information.
	 */ /*
			 * Error or EOF; if errno != 0, it's an error.
			 */ /*
		 * Ignore directories (".", "..", and any subdirectories).
		 */ /*
		 * Get the interface name.
		 */ /*
				 * This could be the separator between a
				 * name and an alias number, or it could be
				 * the separator between a name with no
				 * alias number and the next field.
				 *
				 * If there's a colon after digits, it
				 * separates the name and the alias number,
				 * otherwise it separates the name and the
				 * next field.
				 */ /*
					 * That was the next field,
					 * not the alias number.
					 */ /*
		 * Get the flags for this interface, and skip it if
		 * it's not up.
		 */ /*
		 * Add an entry for this interface, with no addresses.
		 */ /*
			 * Failure.
			 */ /*
		 * Well, we didn't fail for any other reason; did we
		 * fail due to an error reading the directory?
		 */