-
Notifications
You must be signed in to change notification settings - Fork 2
/
packet_capture.c
executable file
·411 lines (340 loc) · 10.5 KB
/
packet_capture.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// ©.
// https://github.com/sizet/packet_capture
#include <time.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/utsname.h>
#include <arpa/inet.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#ifndef PACKET_AUXDATA
#error "must be support PACKET_AUXDATA (since linux 2.6.21)"
#endif
#define DMSG(msg_fmt, msg_args...) \
printf("%s(%04u): " msg_fmt "\n", __FILE__, __LINE__, ##msg_args)
struct pcap_hdr
{
__u32 magic_number;
__u16 version_major;
__u16 version_minor;
__s32 thiszone;
__u32 sigfigs;
__u32 snaplen;
__u32 network;
};
struct pcaprec_hdr
{
__u32 ts_sec;
__u32 ts_usec;
__u32 incl_len;
__u32 orig_len;
};
#define VLAN_HLEN 4
struct vlan_ethhdr
{
__u8 h_dest[ETH_ALEN];
__u8 h_source[ETH_ALEN];
__be16 h_vlan_proto;
__be16 h_vlan_TCI;
__be16 h_vlan_encapsulated_proto;
} __attribute__((packed));
int shutdown_process = 0;
unsigned long long int recv_pkts = 0, recv_bytes = 0, send_pkts = 0, send_bytes = 0;
void signal_handle(
int signal_value)
{
switch(signal_value)
{
case SIGINT:
case SIGQUIT:
case SIGTERM:
shutdown_process = 1;
break;
case SIGUSR1:
DMSG("recv = %llu packets, %llu bytes", recv_pkts, recv_bytes);
DMSG("send = %llu packets, %llu bytes", send_pkts, send_bytes);
break;
}
return;
}
int add_pid_file(
char *file_path)
{
FILE *file_fp;
if(file_path != NULL)
{
file_fp = fopen(file_path, "w");
if(file_fp == NULL)
{
DMSG("call fopen(%s) fail [%s]", file_path, strerror(errno));
return -1;
}
fprintf(file_fp, "%d", getpid());
fclose(file_fp);
}
return 0;
}
int del_pid_file(
char *file_path)
{
if(file_path != NULL)
if(unlink(file_path) == -1)
{
DMSG("call unlink(%s) fail [%s]", file_path, strerror(errno));
return -1;
}
return 0;
}
int pcap_init(
char *file_path,
FILE **filefp_buf)
{
FILE *file_fp;
struct pcap_hdr pcap_fhdr;
file_fp = fopen(file_path, "wb");
if(file_fp == NULL)
{
DMSG("call fopen(%s) fail [%s]", file_path, strerror(errno));
return -1;
}
memset(&pcap_fhdr, 0, sizeof(pcap_fhdr));
pcap_fhdr.magic_number = htonl(0xA1B2C3D4);
pcap_fhdr.version_major = htons(2);
pcap_fhdr.version_minor = htons(4);
pcap_fhdr.thiszone = htonl(0);
pcap_fhdr.sigfigs = htonl(0);
pcap_fhdr.snaplen = htonl(65535);
pcap_fhdr.network = htonl(1);
fwrite(&pcap_fhdr, 1, sizeof(pcap_fhdr), file_fp);
*filefp_buf = file_fp;
return 0;
}
int socket_init(
char *if_name,
int *sockfd_buf)
{
int sock_fd, opt_flag;
struct sockaddr_ll sock_addrll;
struct ifreq if_req;
sock_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if(sock_fd == -1)
{
DMSG("call socket() fail [%s]", strerror(errno));
goto FREE_01;
}
// 需要封包的 timestamp 資料.
opt_flag = 1;
if(setsockopt(sock_fd, SOL_SOCKET, SO_TIMESTAMP, &opt_flag, sizeof(opt_flag)) == -1)
{
DMSG("call setsockopt(SOL_SOCKET, SO_TIMESTAMP) fail [%s]", strerror(errno));
goto FREE_02;
}
// 需要封包的 VLAN 資料.
opt_flag = 1;
if(setsockopt(sock_fd, SOL_PACKET, PACKET_AUXDATA, &opt_flag, sizeof(opt_flag)) == -1)
{
DMSG("call setsockopt(SOL_PACKET, PACKET_AUXDATA) fail [%s]", strerror(errno));
goto FREE_02;
}
memset(&if_req, 0, sizeof(if_req));
snprintf(if_req.ifr_name, sizeof(if_req.ifr_name), "%s", if_name);
if(ioctl(sock_fd, SIOCGIFINDEX, &if_req) == -1)
{
DMSG("call ioctl(SIOCGIFINDEX, %s) fail [%s]", if_req.ifr_name, strerror(errno));
goto FREE_02;
}
memset(&sock_addrll, 0, sizeof(sock_addrll));
sock_addrll.sll_family = AF_PACKET;
sock_addrll.sll_ifindex = if_req.ifr_ifindex;
sock_addrll.sll_protocol = htons(ETH_P_ALL);
if(bind(sock_fd, (struct sockaddr *) &sock_addrll, sizeof(sock_addrll)) == -1)
{
DMSG("call bind() fail [%s]", strerror(errno));
goto FREE_02;
}
*sockfd_buf = sock_fd;
return 0;
FREE_02:
close(sock_fd);
FREE_01:
return -1;
}
int main(
int argc,
char **argv)
{
char opt_key, *if_name = NULL, *file_path = NULL, *pid_path = NULL;
FILE *file_fp;
int sock_fd;
fd_set sock_set;
struct sockaddr_ll sock_addrll;
struct iovec io_vec;
struct msghdr msg_hdr;
struct cmsghdr *cmsg_hdr;
struct timeval *tv_data;
struct tpacket_auxdata *aux_data;
struct vlan_ethhdr *eth_vlan_hdr;
struct pcaprec_hdr pcap_phdr;
__u8 msg_buf[CMSG_SPACE(sizeof(struct timeval) + sizeof(struct tpacket_auxdata))];
__u8 pkt_buf[65536], *pkt_data, *pkt_loc;
size_t pkt_size;
ssize_t rlen;
while((opt_key = getopt(argc , argv, "i:c:p:")) != -1)
switch(opt_key)
{
case 'i':
if_name = optarg;
break;
case 'c':
file_path = optarg;
break;
case 'p':
pid_path = optarg;
break;
}
if(if_name == NULL)
goto FREE_HELP;
if(file_path == NULL)
goto FREE_HELP;
if(add_pid_file(pid_path) < 0)
{
DMSG("call add_pid_file() fail");
goto FREE_01;
}
signal(SIGINT, signal_handle);
signal(SIGQUIT, signal_handle);
signal(SIGTERM, signal_handle);
signal(SIGUSR1, signal_handle);
signal(SIGPIPE, SIG_IGN);
if(pcap_init(file_path, &file_fp) < 0)
{
DMSG("call pcap_init() fail");
goto FREE_02;
}
if(socket_init(if_name, &sock_fd) < 0)
{
DMSG("call socket_init() fail");
goto FREE_03;
}
// 在封包緩衝開頭預留空間給 VLAN 標頭.
pkt_data = pkt_buf + VLAN_HLEN;
pkt_size = sizeof(pkt_buf) - VLAN_HLEN;
// 使用 recvmsg() 接收封包和輔助資料 (ancillary data).
memset(&io_vec, 0, sizeof(io_vec));
io_vec.iov_base = pkt_data;
io_vec.iov_len = pkt_size;
memset(&msg_hdr, 0, sizeof(msg_hdr));
msg_hdr.msg_name = &sock_addrll;
msg_hdr.msg_namelen = sizeof(sock_addrll);
msg_hdr.msg_iov = &io_vec;
msg_hdr.msg_iovlen = 1;
msg_hdr.msg_control = msg_buf;
msg_hdr.msg_controllen = sizeof(msg_buf);
while(shutdown_process == 0)
{
FD_ZERO(&sock_set);
FD_SET(sock_fd, &sock_set);
if(select(sock_fd + 1, &sock_set, NULL, NULL, NULL) == -1)
{
if(errno == EINTR)
continue;
DMSG("call select() fail [%s]", strerror(errno));
goto FREE_04;
}
if(FD_ISSET(sock_fd, &sock_set) == 0)
continue;
rlen = recvmsg(sock_fd, &msg_hdr, 0);
if(rlen == -1)
{
DMSG("call recvmsg() fail [%s]", strerror(errno));
if(errno == ENETDOWN)
continue;
goto FREE_04;
}
// 取出輔助資料.
tv_data = NULL;
aux_data = NULL;
for(cmsg_hdr = CMSG_FIRSTHDR(&msg_hdr); cmsg_hdr != NULL;
cmsg_hdr = CMSG_NXTHDR(&msg_hdr, cmsg_hdr))
{
// timestamp 資料.
if(cmsg_hdr->cmsg_level == SOL_SOCKET)
if(cmsg_hdr->cmsg_type == SCM_TIMESTAMP)
if(cmsg_hdr->cmsg_len >= CMSG_LEN(sizeof(struct timeval)))
{
tv_data = (struct timeval *) CMSG_DATA(cmsg_hdr);
continue;
}
// VLAN 資料.
if(cmsg_hdr->cmsg_level == SOL_PACKET)
if(cmsg_hdr->cmsg_type == PACKET_AUXDATA)
if(cmsg_hdr->cmsg_len >= CMSG_LEN(sizeof(struct timeval)))
{
aux_data = (struct tpacket_auxdata *) CMSG_DATA(cmsg_hdr);
continue;
}
}
pkt_loc = pkt_data;
// 將 VLAN 資料塞入封包的 VLAN 標頭部分.
if(aux_data != NULL)
if(aux_data->tp_vlan_tci != 0)
{
memmove(pkt_loc - VLAN_HLEN, pkt_loc, ETH_ALEN * 2);
pkt_loc -= VLAN_HLEN;
rlen += VLAN_HLEN;
eth_vlan_hdr = (struct vlan_ethhdr *) pkt_loc;
eth_vlan_hdr->h_vlan_proto = htons(ETH_P_8021Q);
eth_vlan_hdr->h_vlan_TCI = htons(aux_data->tp_vlan_tci);
}
if(sock_addrll.sll_pkttype == PACKET_OUTGOING)
{
send_pkts++;
send_bytes += rlen;
}
else
{
recv_pkts++;
recv_bytes += rlen;
}
pcap_phdr.ts_sec = htonl(tv_data->tv_sec);
pcap_phdr.ts_usec = htonl(tv_data->tv_usec);
pcap_phdr.incl_len = pcap_phdr.orig_len = htonl(rlen);
fwrite(&pcap_phdr, sizeof(pcap_phdr), 1, file_fp);
fwrite(pkt_loc, sizeof(__u8), rlen, file_fp);
}
DMSG("recv = %llu packets, %llu bytes", recv_pkts, recv_bytes);
DMSG("send = %llu packets, %llu bytes", send_pkts, send_bytes);
FREE_04:
close(sock_fd);
FREE_03:
fclose(file_fp);
FREE_02:
del_pid_file(pid_path);
FREE_01:
return 0;
FREE_HELP:
printf("\npcaket_capture argument-list\n");
printf(" argument :\n");
printf(" <-i ethernet-interface-name>\n");
printf(" capture the packet of this network interface.\n");
printf(" ex : -i eth0\n");
printf(" <-c file-path>\n");
printf(" save captured packet to this file (pcap format).\n");
printf(" ex : -c packet.pcap\n");
printf(" [-p file-path]\n");
printf(" save process id to this file.\n");
printf(" ex : -p /var/run/pcaket_capture.pid\n");
printf(" signal control :\n");
printf(" SIGINT, SIGQUIT, SIGTERM\n");
printf(" stop process.\n");
printf(" SIGUSR1\n");
printf(" show captured packet counter.\n\n");
return 0;
}