-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinet_diag.c
137 lines (115 loc) · 3.55 KB
/
inet_diag.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
/*
* inet_diag_req version 1 test on kernel 2.6.32
* author: web <[email protected]>
*/
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <linux/inet_diag.h>
#include <linux/netlink.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc, char *argv[]) {
int fd;
fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_INET_DIAG);
// bind
struct sockaddr_nl bind_nladdr = {
.nl_family = AF_NETLINK,
.nl_pid = getpid(),
};
if (bind(fd, (struct sockaddr *) &bind_nladdr, sizeof(bind_nladdr)) < 0) {
perror("can't bind socket\n");
return -1;
}
// netlink request
struct sockaddr_nl nladdr = {
.nl_family = AF_NETLINK,
.nl_pid = 0, /* kernel */
};
struct {
struct nlmsghdr nlh;
struct inet_diag_req r;
} req = {
.nlh = {
// NLMSG_ALIGN(NLMSG_LENGTH(sizeof(req)))
.nlmsg_len = sizeof(req),
.nlmsg_type = TCPDIAG_GETSOCK,
.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP,
.nlmsg_seq = 1,
},
.r = {
.idiag_family = AF_INET,
.idiag_states = (1 << 10), // TCP_LISTEN, change it to monitor other states
.id = {
.idiag_cookie[0] = INET_DIAG_NOCOOKIE,
.idiag_cookie[1] = INET_DIAG_NOCOOKIE,
},
},
};
struct iovec iov_s[1] = {
[0] = {
.iov_base = &req,
.iov_len = sizeof(req),
},
};
struct msghdr msg_s = {
.msg_name = &nladdr,
.msg_namelen = sizeof(nladdr),
.msg_iov = iov_s,
.msg_iovlen = 1,
};
// send netlink request
if (sendmsg(fd, &msg_s, 0) < 0) {
perror("socket send error\n");
return -1;
}
// receive netlink request
int len;
char buff[16384];
char ip[48];
struct iovec iov_r[1] = {
[0] = {
.iov_base = buff,
.iov_len = sizeof(buff),
},
};
struct msghdr msg_r = {
.msg_name = &nladdr,
.msg_namelen = sizeof(nladdr),
.msg_iov = iov_r,
.msg_iovlen = 1,
};
while (1) {
if ((len = recvmsg(fd, &msg_r, 0)) < 0) {
close(fd);
fprintf(stderr, "recv error\n");
return -1;
}
struct nlmsghdr *h = (struct nlmsghdr *) buff;
while (NLMSG_OK(h, len)) {
if (h->nlmsg_type == NLMSG_DONE) {
close(fd);
return 0;
}
if (h->nlmsg_type == NLMSG_ERROR) {
if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
fprintf(stderr, "message truncated");
close(fd);
} else {
struct nlmsgerr *err = NLMSG_DATA(h);
errno = -err->error;
fprintf(stderr, "nlmsg error %s\n", strerror(errno));
close(fd);
return -1;
}
}
// parse msg data
struct inet_diag_msg *r = NLMSG_DATA(h);
printf("%s:%d\n",
inet_ntop(r->idiag_family, &r->id.idiag_src, ip, sizeof(ip)),
ntohs(r->id.idiag_sport));
h = NLMSG_NEXT(h, len);
}
}
}