-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_interface.c
executable file
·149 lines (139 loc) · 5.2 KB
/
get_interface.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <arpa/inet.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <netinet/ether.h>
#include "get_interface.h"
int interface_num = 0; // 接口数量
INTERFACE net_interface[MAXINTERFACES]; // 接口数据
/******************************************************************
函 数: int get_interface_num()
功 能: 获取接口数量
参 数: 无
*******************************************************************/
int get_interface_num()
{
return interface_num;
}
/******************************************************************
函 数: int getinterface()
功 能: 获取接口信息
参 数: 无
*******************************************************************/
void getinterface()
{
struct ifreq buf[MAXINTERFACES]; /* ifreq结构数组 */
struct ifconf ifc; /* ifconf结构 */
int sock_raw_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
/* 初始化ifconf结构 */
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = (caddr_t)buf;
/* 获得接口列表 */
if (ioctl(sock_raw_fd, SIOCGIFCONF, (char *)&ifc) == -1)
{
perror("SIOCGIFCONF ioctl");
return;
}
interface_num = ifc.ifc_len / sizeof(struct ifreq); /* 接口数量 */
printf("interface_num=%d\n\n", interface_num);
char buff[20] = "";
int ip;
int if_len = interface_num;
while (if_len-- > 0)
{ /* 遍历每个接口 */
printf("%s\n", buf[if_len].ifr_name); /* 接口名称 */
sprintf(net_interface[if_len].name, "%s", buf[if_len].ifr_name); /* 接口名称 */
printf("-%d-%s--\n", if_len, net_interface[if_len].name);
/* 获得接口标志 */
if (!(ioctl(sock_raw_fd, SIOCGIFFLAGS, (char *)&buf[if_len])))
{
/* 接口状态 */
if (buf[if_len].ifr_flags & IFF_UP)
{
printf("UP\n");
net_interface[if_len].flag = 1;
}
else
{
printf("DOWN\n");
net_interface[if_len].flag = 0;
}
}
else
{
char str[256];
sprintf(str, "SIOCGIFFLAGS ioctl %s", buf[if_len].ifr_name);
perror(str);
}
/* IP地址 */
if (!(ioctl(sock_raw_fd, SIOCGIFADDR, (char *)&buf[if_len])))
{
printf("IP:%s\n", (char *)inet_ntoa(((struct sockaddr_in *)(&buf[if_len].ifr_addr))->sin_addr));
bzero(buff, sizeof(buff));
sprintf(buff, "%s", (char *)inet_ntoa(((struct sockaddr_in *)(&buf[if_len].ifr_addr))->sin_addr));
inet_pton(AF_INET, buff, &ip);
memcpy(net_interface[if_len].ip, &ip, 4);
}
else
{
char str[256];
sprintf(str, "SIOCGIFADDR ioctl %s", buf[if_len].ifr_name);
perror(str);
}
/* 子网掩码 */
if (!(ioctl(sock_raw_fd, SIOCGIFNETMASK, (char *)&buf[if_len])))
{
printf("netmask:%s\n", (char *)inet_ntoa(((struct sockaddr_in *)(&buf[if_len].ifr_addr))->sin_addr));
bzero(buff, sizeof(buff));
sprintf(buff, "%s", (char *)inet_ntoa(((struct sockaddr_in *)(&buf[if_len].ifr_addr))->sin_addr));
inet_pton(AF_INET, buff, &ip);
memcpy(net_interface[if_len].netmask, &ip, 4);
}
else
{
char str[256];
sprintf(str, "SIOCGIFADDR ioctl %s", buf[if_len].ifr_name);
perror(str);
}
/* 广播地址 */
if (!(ioctl(sock_raw_fd, SIOCGIFBRDADDR, (char *)&buf[if_len])))
{
printf("br_ip:%s\n", (char *)inet_ntoa(((struct sockaddr_in *)(&buf[if_len].ifr_addr))->sin_addr));
bzero(buff, sizeof(buff));
sprintf(buff, "%s", (char *)inet_ntoa(((struct sockaddr_in *)(&buf[if_len].ifr_addr))->sin_addr));
inet_pton(AF_INET, buff, &ip);
memcpy(net_interface[if_len].br_ip, &ip, 4);
}
else
{
char str[256];
sprintf(str, "SIOCGIFADDR ioctl %s", buf[if_len].ifr_name);
perror(str);
}
/*MAC地址 */
if (!(ioctl(sock_raw_fd, SIOCGIFHWADDR, (char *)&buf[if_len])))
{
printf("MAC:%02x:%02x:%02x:%02x:%02x:%02x\n\n",
(unsigned char)buf[if_len].ifr_hwaddr.sa_data[0],
(unsigned char)buf[if_len].ifr_hwaddr.sa_data[1],
(unsigned char)buf[if_len].ifr_hwaddr.sa_data[2],
(unsigned char)buf[if_len].ifr_hwaddr.sa_data[3],
(unsigned char)buf[if_len].ifr_hwaddr.sa_data[4],
(unsigned char)buf[if_len].ifr_hwaddr.sa_data[5]);
memcpy(net_interface[if_len].mac, (unsigned char *)buf[if_len].ifr_hwaddr.sa_data, 6);
}
else
{
char str[256];
sprintf(str, "SIOCGIFHWADDR ioctl %s", buf[if_len].ifr_name);
perror(str);
}
} // –while end
close(sock_raw_fd); // 关闭socket
}