-
Notifications
You must be signed in to change notification settings - Fork 0
/
ifconf.c
58 lines (48 loc) · 1.14 KB
/
ifconf.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
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <linux/if.h>
#include <errno.h>
int init_socket(void)
{
int fd = -1;
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0)
{
printf("create socket error\n");
return -1;
}
return fd;
}
int main(int argc, char **argv)
{
struct ifconf ifconf;
char buf[1024] ={0};
int len = 256;
int sockfd = -1;
int ret = -1;
int i = 0;
struct ifreq *ifreq = NULL;
ifconf.ifc_buf = buf;
ifconf.ifc_len = len;
sockfd = init_socket();
ret = ioctl(sockfd, SIOCGIFCONF, &ifconf);
if (ret == -1)
{
printf("ioctl error, errno =%d\n", errno);
return -1;
}
ifreq = (struct ifreq *)ifconf.ifc_buf;
for (i=(ifconf.ifc_len/sizeof (struct ifreq)); i>0; i--)
{
printf("name = [%s]\n" , ifreq->ifr_name);
printf("local addr = [%s]\n" ,inet_ntoa(((struct sockaddr_in*)&(ifreq->ifr_addr))->sin_addr));
printf("local addr = [%s]\n" ,inet_ntoa(((struct sockaddr_in*)&(ifreq->ifr_broadaddr))->sin_addr));
ifreq++;
}
return 0;
}