-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcallback.c
81 lines (65 loc) · 1.86 KB
/
callback.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
#include "main.h"
/* callback function */
void packet_info (u_char *user, const struct pcap_pkthdr *h, const u_char *p)
{
int i, ipos=0; // counters
unsigned short type; // type field in ethernet frame
double crnt_t; // current time
// set initial time
if ( cpkNum == 0 )
init_t = (double)h->ts.tv_sec + 0.000001*h->ts.tv_usec;
if(user)
pcap_dump(user, h, p);
/* set color */
if((type=p[12]<<8 | p[13]) == 0x0800)
{
if(p[23]==IP_PROTO_TCP) setcolor(BLUE);
else if(p[23]==IP_PROTO_UDP) setcolor(PURPLE);
else setcolor(YELLO);
}else
setcolor(RED);
//printf("(%4d) clen=%3d, len=%4d \r",cpkNum++,h->caplen,h->len);
/* IP detection routine */
checkIpAddress((unsigned char *)p, "168.126.63.1");
/* Every 20 packets, close & reopen a log file for consistency */
if((cpkNum%20) == 0 && (cpkNum != 0))
LogIpDetection(NULL, NULL, 1);
// current time compared to the initial time
crnt_t = (double)h->ts.tv_sec + 0.000001*h->ts.tv_usec;
printf("%9.4f: [",crnt_t-init_t);
// source MAC address
for ( i=0; i<6; i++) printf("%02x%s",p[i+6],i==5 ? "->" : ":") ;
// destination MAC address
for ( i=0; i<6; i++) printf("%02x%s",p[i],i==5? "]": ":");
// IP datagram
if ( (type=p[12]<<8 | p[13]) == 0x0800 )
{
printf("(");
// source IP address
for ( i=0; i<4; i++)
printf("%02d%s",p[i+26], i==3?"->":".");
// destination IP address
for ( i=0; i<4; i++)
printf("%02d%s",p[i+30], i==3?")":".");
// upper layer protocol
printf("%s", p[23]==IP_PROTO_TCP? "TCP":
p[23]==IP_PROTO_UDP? "UDP":"OTH");
}
else
printf("(Non-IP)");
printf("\n");
setcolor(RESET_BG);
cpkNum++;
if ( cpkNum == maxPkt )
{
setcolor(RESET_BG);
// close all devices and files
pcap_close(adhandle);
if(user)
{
pcap_dump_close((pcap_dumper_t *)user);
makeStat();
}
exit(0);
}
}