- //facename.c 枚举出iface使用的是/proc/net/dev文件
- #include <sys/types.h>
-
#include <sys/ioctl.h>
-
#include <sys/socket.h>
-
#include <netinet/in.h>
-
#include <net/if.h>
-
#include <string.h>
-
#include <stdio.h>
-
#include <stdlib.h>
-
-
#define LINE_MAX 512
-
#define PATH_PROC_NET_DEV "/proc/net/dev"
-
-
int main(int argc, char *argv[])
-
{
-
FILE * fp;
-
char line[LINE_MAX];
-
char * tmp,i=0;
-
char interfacename[32][32];
-
if(!(fp = fopen(PATH_PROC_NET_DEV, "r")))
-
{
-
perror("fopen");
-
exit(1);
-
}
-
-
fgets(line, sizeof(line), fp);
-
fgets(line, sizeof(line), fp);
-
while(fgets(line, sizeof(line), fp))
-
{
-
if((tmp = strchr(line,':')))
-
{
-
*tmp = '\0';
-
tmp = line;
-
while(*tmp ==' ')
-
{
-
tmp++;
-
}
-
strcpy(interfacename[i],tmp);
-
printf("%s\n", tmp);
-
i++;
-
}
-
}
-
fclose(fp);
-
for(i=i-1;i>=0;i--)
-
{
-
printf("%s\n", interfacename[i]);
-
}
-
exit(0);
-
}
/****************************************************************************************/
//phy_ioctrl.c 使用ioctrl测试网络状态,
- #include <sys/types.h>
-
#include <string.h>
-
#include <stdlib.h>
-
#include <sys/ioctl.h>
-
#include <stdio.h>
-
#include <errno.h>
-
#include <net/if.h>
-
-
struct ethtool_value
-
{
-
__uint32_t cmd;
-
__uint32_t data;
-
};
-
-
int main(char argc,char**argv)
-
{
-
struct ethtool_value edata;
-
int fd=-1,err=0;
-
struct ifreq ifr;
-
-
memset(&ifr,0,sizeof(ifr));
-
strcpy(ifr.ifr_name,"eth0");
-
fd = socket(AF_INET,SOCK_DGRAM,0);
-
if(fd<0)
-
{
-
perror("cannot get control socket");
-
return 70;
-
}
-
-
edata.cmd = 0x0000000a;
-
ifr.ifr_data = (caddr_t)&edata;
-
err = ioctl(fd,0x8946,&ifr);
-
if(err == 0)
-
{
-
printf("Link detected:%s\n",edata.data? "yes":"no");
-
}
-
else if(errno != EOPNOTSUPP)
-
{
-
perror("cannot get link status");
-
}
-
return 0;
-
}
/*********************************************************************/
iface_name.c
- #include <sys/types.h>
-
#include <sys/ioctl.h>
-
#include <sys/socket.h>
-
#include <netinet/in.h>
-
#include <net/if.h>
-
#include <string.h>
-
#include <stdio.h>
-
#include <stdlib.h>
-
-
#define IF_MAX 30
-
-
int main(int argc, char *argv[])
-
{
-
int numreqs = 10;
-
int fd;
-
int i;
-
struct ifreq * ifr = NULL;
-
struct ifconf ifc;
-
-
if((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
-
{
-
perror("socket");
-
exit(1);
-
}
-
-
ifc.ifc_len = sizeof(struct ifreq) * numreqs;
-
ifc.ifc_buf = malloc(ifc.ifc_len);
-
memset(ifc.ifc_buf, 0, ifc.ifc_len);
-
-
if(ioctl(fd, SIOCGIFCONF, &ifc) != 0)
-
{
-
perror("SIOCGIFCONF");
-
free(ifc.ifc_buf);
-
close(fd);
-
exit(1);
-
}
-
-
close(fd);
-
-
if(ifc.ifc_len == sizeof(struct ifreq) * numreqs)
-
printf("The system has at least %d interfaces, "
-
"please increase the buffer.\n", numreqs);
-
-
ifr = ifc.ifc_req;
-
-
printf("Interfaces in the system (by SIOCGIFCONF):\n");
-
printf("--------------------------------------------------\n");
-
for(i=0; i<ifc.ifc_len; i+=sizeof(struct ifreq))
-
{
-
printf("%s\n", ifr->ifr_name);
-
ifr++;
-
}
-
-
free(ifc.ifc_buf);
-
-
exit(0);
-
}
/**************************************************************************/