-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
148 lines (117 loc) · 3.47 KB
/
main.cpp
File metadata and controls
148 lines (117 loc) · 3.47 KB
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
#include <pcap.h>
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <stdlib.h>
void usage() {
printf("syntax: pcap_test <interface>\n");
printf("sample: pcap_test wlan0\n");
}
void printPacket(const u_char* _packet,long int len);
int main(int argc, char* argv[]) {
//if (argc != 2) {
// usage();
// return -1;
//}
char dst_mac[6] = "";
int i;
char* dev = "ens33";
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t* handle = pcap_open_live(dev, BUFSIZ, 1, 1000, errbuf);
if (handle == NULL) {
fprintf(stderr, "couldn't open device %s: %s\n", dev, errbuf);
return -1;
}
while (true) {
struct pcap_pkthdr* header;
const u_char* packet;
int res = pcap_next_ex(handle, &header, &packet);
if (res == 0) continue;
if (res == -1 || res == -2) break;
printf("%u bytes captured\n", header->caplen);
printPacket(packet, header->caplen); //function for print packet (packet content and header length is argument)
}
pcap_close(handle);
return 0;
}
void printPacket(const u_char* _packet, long int len){
int i; //for loop
long long int src_port = 0; //for print src_port
long long int dst_port = 0; //for print dst_prot
long long int headerLength = 0; //for calculate tcp_headerLength
long long int totalHLEN = 0; //for print content 16 bytes
printf("dst_mac :\t");
for(i = 0; i < 6; i++){
if(i == 5){
if(_packet[i] < 16){
printf("0"); //if value is smaller than 10 print 0
}
printf("%X",_packet[i]);
break;
}
if(_packet[i] < 16){
printf("0"); //if value is smaller than 10 print 0
}
printf("%X:",_packet[i]);
}
printf("\n");
printf("src_mac :\t");
for(i = 6; i < 12; i++){
if(i == 11){
if(_packet[i] < 16){
printf("0"); //if value is smaller than 10 print 0
}
printf("%X",_packet[i]);
break;
}
if(_packet[i] < 16){
printf("0"); //if value is smaller than 10 print 0
}
printf("%X:",_packet[i]);
}
printf("\n");
totalHLEN += 14; //add 14bytes(ethernet Total Length)
if(_packet[12] == 0x08 && _packet[13] == 0x00){
//printf("%0X %0X %0X %0X\n",_packet[26],_packet[27],_packet[28],_packet[29]);
printf("src_ip :\t");
for(i = 26; i < 30; i++){
if(i == 29){
printf("%lld",_packet[i]);
break;
}
printf("%lld.", _packet[i]);
}
printf("\ndst_ip :\t");
for(i = 30; i < 34; i++){
if(i == 33){
printf("%lld",_packet[i]);
break;
}
printf("%lld.", _packet[i]);
}
printf("\n");
totalHLEN += 20; //add ip layer length
src_port += _packet[34]<<8; //calculate src_port value
src_port += _packet[35];
printf("src_port :\t%lld\n",src_port);
dst_port += _packet[36]<<8; //calculate dst_prot value
dst_port += _packet[37];
printf("dst_port :\t%lld\n",dst_port);
headerLength = (long long int)_packet[46];
printf("HLEN :\t\t%lld\n", headerLength);
totalHLEN += headerLength; //add tcp header length
headerLength += 34;
if( (len - totalHLEN) >= 16){ // if total header length is bigger than totalHLEN print data content
printf("data content 16 bytes : ");
for(i = headerLength ; i < (headerLength+16) ; i++){
printf("%X ",_packet[i]);
}
}
printf("\n");
}
//printf("------------packet contents------------\n");
//for(i = 0; i < len; i++){
// printf("%3X",_packet[i]);
//}
//printf("------------end --------------------\n");
}