-
Notifications
You must be signed in to change notification settings - Fork 1
/
nlink_456.c
203 lines (178 loc) · 3.93 KB
/
nlink_456.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <linux/types.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <net/if.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
/* Utility function for parse rtattr. */
static void
netlink_parse_rtattr(struct rtattr **tb, int max, struct rtattr *rta, int len)
{
while (RTA_OK(rta, len))
{
if (rta->rta_type <= max)
{
tb[rta->rta_type] = rta;
}
rta = RTA_NEXT(rta, len);
}
}
/* Recieving netlink message. */
void netlink_recv(int nsock)
{
char buf[4096];
struct iovec iov = { buf, sizeof(buf) };
struct sockaddr_nl snl;
struct msghdr msg = { (void*)&snl, sizeof(snl), &iov, 1, NULL, 0, 0};
struct nlmsghdr *h;
struct rtattr *tb[IFLA_MAX > IFA_MAX ? IFLA_MAX : IFA_MAX + 1];
struct ifinfomsg *ifi;
int status = recvmsg(nsock, &msg, 0);
if (status < 0)
{
perror("recvmsg(nsock)");
return;
}
else if (status == 0)
{
perror("recvmsg(nsock): received zero sizenetlink message");
return;
}
if (msg.msg_namelen != sizeof(snl))
{
perror("recvmsg(nsock): received invalid netlink message");
return;
}
if (msg.msg_flags & MSG_TRUNC)
{
perror("recvmsg(nsock): received truncated netlink message");
return;
}
for (h = (struct nlmsghdr *) buf; NLMSG_OK(h, status); h = NLMSG_NEXT(h, status))
{
switch (h->nlmsg_type)
{
case NLMSG_DONE:
return;
case NLMSG_ERROR:
NLMSG_DATA(h);
if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
{
perror("received error message with invalid length");
return;
}
return;
case RTM_NEWLINK:
case RTM_DELLINK:
ifi = NLMSG_DATA(h);;
int len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg));
char ifname[IF_NAMESIZE+1];
if (len < 0)
{
continue;
}
memset(tb, 0, sizeof(tb));
netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
if (tb[IFLA_IFNAME] == NULL)
{
continue;
}
strncpy(ifname, (char *) RTA_DATA(tb[IFLA_IFNAME]), IF_NAMESIZE);
switch (h->nlmsg_type)
{
case RTM_NEWLINK:
if (ifi->ifi_flags & IFF_RUNNING)
{
printf("interface %s(%d): UP\n", ifname, ifi->ifi_index);
}
else
{
printf("interface %s(%d): DOWN\n", ifname, ifi->ifi_index);
}
break;
case RTM_DELLINK:
printf("interface %s(%d): REMOVED\n", ifname, ifi->ifi_index);
break;
}
break;
default:
continue;
}
}
}
/* Make the kernel send us an RTM_NEWLINK for all interfaces */
void netlink_getlink(int nsock)
{
struct nlmsghdr* n;
struct ifinfomsg *ifi;
u_int8_t req[sizeof(struct nlmsghdr) + sizeof(struct ifinfomsg) + sizeof(struct ifaddrmsg) + 4096];
memset(&req, 0, sizeof(req));
n = (struct nlmsghdr*) req;
n->nlmsg_len = NLMSG_LENGTH(sizeof(*ifi));
n->nlmsg_type = RTM_GETLINK;
n->nlmsg_seq = 1;
n->nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
n->nlmsg_pid = 0;
ifi = NLMSG_DATA(n);
ifi->ifi_family = AF_UNSPEC;
ifi->ifi_change = -1;
if (send(nsock, n, n->nlmsg_len, 0) < 0)
{
perror("send");
exit(1);
}
}
int main(int argc, char **argv)
{
struct sockaddr_nl nl_addr;
int nsock,ret;
fd_set r;
/* initialize netlink socket */
if ((nsock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0)
{
perror("socket");
return 2;
}
memset(&nl_addr, 0, sizeof(nl_addr));
nl_addr.nl_family = AF_NETLINK;
nl_addr.nl_groups = RTMGRP_LINK;
nl_addr.nl_pid = getpid();
if ((bind(nsock, (struct sockaddr *) &nl_addr, sizeof(nl_addr))) < 0)
{
perror("bind");
return 2;
}
netlink_getlink(nsock);
netlink_recv(nsock);
while (1)
{
FD_ZERO(&r);
FD_SET(nsock, &r);
ret = select(nsock + 1, &r, NULL, NULL, NULL);
switch (ret)
{
case -1:
if (errno != EINTR)
{
perror("select");
return 2;
}
continue;
case 0: /* timeout */
break;
default:
break;
}
if (FD_ISSET(nsock, &r))
{
netlink_recv(nsock);
}
}
return 0;
}