-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
453 lines (378 loc) · 14.3 KB
/
main.cpp
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pcap.h>
#include <err.h>
#include <string.h>
#include <time.h>
#define SIZE_ETHERNET 14
int packet_counter = 0;
/* 6 bytes MAC address */
typedef struct mac_address{
u_char byte1;
u_char byte2;
u_char byte3;
u_char byte4;
u_char byte5;
u_char byte6;
}mac_address;
/* Ether header */
typedef struct eth_header{
mac_address saddr; // Source address
mac_address daddr; // Destination address
u_short typlen; // Type / Length
}eth_header;
/* 4 bytes IP address */
typedef struct ip_address{
u_char byte1;
u_char byte2;
u_char byte3;
u_char byte4;
}ip_address;
/* IPv4 header */
typedef struct ip_header{
u_char ver_ihl; // Version (4 bits) + Internet header length (4 bits)
u_char tos; // Type of service
u_short tlen; // Total length
u_short identification; // Identification
u_short flags_fo; // Flags (3 bits) + Fragment offset (13 bits)
u_char ttl; // Time to live
u_char proto; // Protocol
u_short crc; // Header checksum
ip_address saddr; // Source address
ip_address daddr; // Destination address
u_int op_pad; // Option + Padding
}ip_header;
/* UDP header*/
typedef struct udp_header{
u_short sport; // Source port
u_short dport; // Destination port
u_short len; // Datagram length
u_short crc; // Checksum
}udp_header;
/* TCP control bits */
typedef unsigned char control_bits;
enum control_bits_flags { CWR = 128, ECE = 64, URG = 32, ACK = 16, PSH = 8, RST = 4, SYN = 2, FIN = 1 };
control_bits control_bits_flags_array[] = { CWR, ECE, URG, ACK, PSH, RST, SYN, FIN };
char* control_bits_flags_names[] = { "CWR", "ECE", "URG", "ACK", "PSH", "RST", "SYN", "FIN" };
/* TCP header*/
typedef struct tcp_header {
u_short sport; // Source port
u_short dport; // Destination port
u_long seqnum; // Sequence Number
u_long acknum; // Acknowledgment Number
u_char data_offset; // Data Offset
control_bits ControlBits; // Control Bits
u_short window; // Window
u_short crc; // Checksum
u_short urg_pointer; // Urgent Pointer
}tcp_header;
typedef struct tcp_buffer {
u_char * data;
u_long data_len;
u_long seqnum;
tcp_buffer * next;
} tcp_buffer;
typedef enum tcp_states {
CLOSED = 0,
LISTEN = 1,
SYN_RECEIVED = 2,
SYN_SENT = 3,
ESTABLISHED = 4,
FIN_WAIT_1 = 5,
FIN_WAIT_2 = 6,
CLOSING = 7,
TIME_WAIT = 8,
CLOSE_WAIT = 9,
LAST_ACK = 10,
ERROR = -1
} tcp_states;
char* tcp_states_names[] = { "CLOSED", "LISTEN", "SYN_RECEIVED", "SYN_SENT", "ESTABLISHED", "FIN_WAIT_1", "FIN_WAIT_2", "CLOSING", "TIME_WAIT", "CLOSE_WAIT", "LAST_ACK" };
typedef struct computer_info {
char *name;
ip_address ip;
u_short port;
tcp_states tcp_state;
u_long init_seq;
u_long last_seq;
u_long last_ack;
char *prompt;
} computer_info;
/* global variables */
computer_info server;
computer_info client;
u_int global_mss;
FILE* OUT_DATA;
FILE* OUT_INFO;
/* prototype of the packet handler */
void textIP2structIP(char *text_ip, ip_address *struct_ip);
int are_ip_addresses_eql(const ip_address *ip_address_1, const ip_address *ip_address_2);
int global_communication_filter_ok(const struct ip_header *IPh, const struct tcp_header *TCPh);
void print_tcp_control_bits(const control_bits *pbits);
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);
void IPpacket_handler(const struct ip_header *IPh, const struct pcap_pkthdr *header, const u_char *pkt_data);
void TCPpacket_handler(const struct ip_header *IPh, const struct tcp_header *TCPh, const struct pcap_pkthdr *header, const u_char *pkt_data);
void FTPpacket_handler(const struct ip_header *IPh, const struct tcp_header *TCPh, const struct pcap_pkthdr *header, const u_char *pkt_data);
void process_packet_sent(computer_info* scomp, const struct ip_header *IPh, const struct tcp_header *TCPh);
void process_packet_received(computer_info* comp, const struct ip_header *IPh, const struct tcp_header *TCPh);
void print_mss_if_changed(const u_char* tcp_options, int len);
int main(int argc, char *argv[])
{
global_mss = 0;
pcap_t *in = NULL;
char errbuf[PCAP_ERRBUF_SIZE + 1];
if (6 != argc) {
fprintf(stderr, "usage: %s soubor.vstup server_ip server_port client_ip client_port \n",argv[0]);
exit(1);
}
in = pcap_open_offline(argv[1], errbuf);
if (NULL == in) {
fprintf(stderr, "stdin: %s", errbuf);
exit(1);
}
OUT_INFO = fopen("out.info", "w");
OUT_DATA = fopen("out.data", "w");
server.name = "server";
server.prompt = "<<< ";
textIP2structIP(argv[2], &(server.ip));
sscanf(argv[3], "%d", &server.port);
server.tcp_state = LISTEN;
fprintf(OUT_INFO, "%s: %d.%d.%d.%d:%d - initial state: %s\n", server.name, server.ip.byte1, server.ip.byte2, server.ip.byte3, server.ip.byte4, server.port, tcp_states_names[server.tcp_state]);
client.name = "client";
client.prompt = ">>> ";
textIP2structIP(argv[4], &(client.ip));
sscanf(argv[5], "%d", &client.port);
client.tcp_state = CLOSED;
fprintf(OUT_INFO, "%s: %d.%d.%d.%d:%d - initial state: %s\n\n", client.name, client.ip.byte1, client.ip.byte2, client.ip.byte3, client.ip.byte4, client.port, tcp_states_names[client.tcp_state]);
pcap_loop(in, 0, packet_handler, NULL);
fclose(OUT_DATA);
fclose(OUT_INFO);
printf("\n\nall ok\n");
exit(0);
}
void textIP2structIP(char *text_ip, ip_address *struct_ip) {
unsigned int ip_addr_segments[4]; // scanf requires int
sscanf(text_ip, "%d.%d.%d.%d", ip_addr_segments, ip_addr_segments + 1, ip_addr_segments + 2, ip_addr_segments + 3);
struct_ip->byte1 = ip_addr_segments[0];
struct_ip->byte2 = ip_addr_segments[1];
struct_ip->byte3 = ip_addr_segments[2];
struct_ip->byte4 = ip_addr_segments[3];
}
/**
* Returns 1 if (ip_address_1 == ip_address_2). Else returns 0.
*/
int are_ip_addresses_eql(const ip_address *ip_address_1, const ip_address *ip_address_2) {
if (ip_address_1->byte1 == ip_address_2->byte1 &&
ip_address_1->byte2 == ip_address_2->byte2 &&
ip_address_1->byte3 == ip_address_2->byte3 &&
ip_address_1->byte4 == ip_address_2->byte4
) {
return 1;
}
return 0;
}
/**
* Returns 1 if communication is betwen server and clietn. Else reutrns 0.
*/
int global_communication_filter_ok(const struct ip_header *IPh, const struct tcp_header *TCPh) {
u_short sport = ntohs( TCPh->sport );
u_short dport = ntohs( TCPh->dport );
/* src is server - dest is clietn */
if (are_ip_addresses_eql(&(IPh->saddr), &server.ip) && are_ip_addresses_eql(&(IPh->daddr), &client.ip) &&
sport == server.port && dport == client.port) {
return 1;
}
if (are_ip_addresses_eql(&(IPh->saddr), &client.ip) && are_ip_addresses_eql(&(IPh->daddr), &server.ip) &&
sport == client.port && dport == server.port) {
return 1;
}
return 0;
}
void separator(int *print_separator) {
if (*print_separator) {
fprintf(OUT_INFO, ", ");
} else {
*print_separator = 1; // print it next time
}
}
void print_tcp_control_bits(const control_bits *pbits) {
int i;
control_bits bits = *pbits;
int print_separator = 0;
for (i = 0; i < 8 ; i++) {
if (bits & control_bits_flags_array[i]) {
separator(&print_separator);
fprintf(OUT_INFO, control_bits_flags_names[i]);
}
}
//fprintf(OUT_INFO, " 0x%02hhx", *pbits);
}
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data) {
eth_header *ETHh;
ETHh = (eth_header *) (pkt_data);
if( ETHh->typlen == 8 ) IPpacket_handler( (ip_header *) (pkt_data + SIZE_ETHERNET), header, pkt_data);
}
void IPpacket_handler(const struct ip_header *IPh, const struct pcap_pkthdr *header, const u_char *pkt_data) {
u_int ip_len;
ip_len = (IPh->ver_ihl & 0xf) * 4;
if( ntohs(IPh->flags_fo) & 0x0 ) // ========== nefragmentace [010 0000000000000]
// ========== fragmentace [001 0000000000000]
// ========== posledni fragmentace [000 0000000000000]
TCPpacket_handler( IPh, (tcp_header *) ((u_char*)IPh + ip_len), header, pkt_data);
}
void TCPpacket_handler(const struct ip_header *IPh, const struct tcp_header *TCPh, const struct pcap_pkthdr *header, const u_char *pkt_data) {
u_int tcp_len;
u_short sport,dport;
tcp_len = TCPh->data_offset*4;
sport = ntohs( TCPh->sport );
dport = ntohs( TCPh->dport );
if (global_communication_filter_ok(IPh, TCPh)) {
FTPpacket_handler(IPh, TCPh, header, pkt_data);
}
}
void FTPpacket_handler(const struct ip_header *IPh, const struct tcp_header *TCPh, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
u_int tcp_len,ip_len;
u_short sport,dport;
ip_len = (IPh->ver_ihl & 0xf) * 4;
tcp_len = TCPh->data_offset * 4;
sport = ntohs( TCPh->sport );
dport = ntohs( TCPh->dport );
computer_info *scomp;
computer_info *dcomp;
if (sport == server.port) {
scomp = &server;
dcomp = &client;
} else {
scomp = &client;
dcomp = &server;
}
fprintf(OUT_INFO, "%d. packet %s{%s} > %s{%s} [", packet_counter++, scomp->name, tcp_states_names[scomp->tcp_state], dcomp->name, tcp_states_names[dcomp->tcp_state]);
print_tcp_control_bits(&(TCPh->ControlBits));
fprintf(OUT_INFO, "]\n");
process_packet_sent(scomp, IPh, TCPh);
process_packet_received(dcomp, IPh, TCPh);
fprintf(OUT_INFO, "\t\tWindow: %lu\n\t\trelative SEQ#: %lu ",
ntohs(TCPh->window),
ntohl(TCPh->seqnum) - scomp->init_seq
);
if (TCPh->ControlBits & PSH) {
if (scomp->last_seq>=(ntohl(TCPh->seqnum) - scomp->init_seq)) fprintf(OUT_INFO,"(repeating)");
scomp->last_seq=ntohl(TCPh->seqnum) - scomp->init_seq;
}
fprintf(OUT_INFO,"\n");
if (TCPh->ControlBits & ACK) {
fprintf(OUT_INFO, "\t\trelative ACK#: %lu\n", ntohl(TCPh->acknum) - dcomp->init_seq);
}
fprintf(OUT_INFO, "\t\tData offset: %u\n", (TCPh->data_offset >> 4) * 4);
print_mss_if_changed(((const u_char *) TCPh) + 20, (TCPh->data_offset >> 4) * 4 - 20);
//fprintf(OUT_INFO, "\t\tIP:: head len: %u, total len: %u\n", (IPh->ver_ihl & 0x0f) * 4, ntohs(IPh->tlen));
u_short tcp_data_len = ntohs(IPh->tlen) - (IPh->ver_ihl & 0x0f) * 4 - (TCPh->data_offset >> 4) * 4;
fprintf(OUT_INFO, "\t\tTPC data len: %d\n", tcp_data_len);
// VYRESIT sparvne poradi SEQ ;) !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//
// - doplnim data do bufferu (dle SEQ)
// - pak vypisuju z bufferu pokud SEQ=last_ack, last_ack=SEQ+data_len ... dokud neni konec, nebo se nerovna last_ack=SEQ
//
//
// memcpy();
if (tcp_data_len) {
const u_char *tcp_data = ((const u_char *) TCPh) + (TCPh->data_offset >> 4) * 4;
fprintf(OUT_DATA, scomp->prompt);
for (int i = 0; i < tcp_data_len; i++) {
fputc(*(tcp_data++), OUT_DATA);
}
}
}
void report_changed_state(computer_info *comp) {
fprintf(OUT_INFO, "\t\t%s -> {%s}\n", comp->name, tcp_states_names[comp->tcp_state]);
}
void process_packet_sent(computer_info* comp, const struct ip_header *IPh, const struct tcp_header *TCPh) {
switch (comp->tcp_state) {
case CLOSED:
if (TCPh->ControlBits == (SYN)) {
comp->tcp_state = SYN_SENT;
report_changed_state(comp);
client.init_seq = ntohl(TCPh->seqnum);
server.init_seq = 0;
fprintf(OUT_INFO, "\t\tclient's Initial sequnce num: %lu\n", client.init_seq);
}
break;
case SYN_RECEIVED:
if (TCPh->ControlBits == (SYN | ACK)) {
server.init_seq = ntohl(TCPh->seqnum);
fprintf(OUT_INFO, "\t\tserver's Initial sequnce num: %lu\n", server.init_seq);
}
break;
case ESTABLISHED:
if (TCPh->ControlBits & (FIN)) {
comp->tcp_state = FIN_WAIT_1;
report_changed_state(comp);
}
break;
case CLOSE_WAIT:
if (TCPh->ControlBits & (FIN)) {
comp->tcp_state = LAST_ACK;
report_changed_state(comp);
comp->tcp_state = CLOSED;
report_changed_state(comp);
}
break;
}
}
void process_packet_received(computer_info* comp, const struct ip_header *IPh, const struct tcp_header *TCPh) {
switch (comp->tcp_state) {
case LISTEN:
if (TCPh->ControlBits == (SYN)) {
comp->tcp_state = SYN_RECEIVED;
report_changed_state(comp);
}
break;
case SYN_RECEIVED:
if (TCPh->ControlBits == (ACK)) {
comp->tcp_state = ESTABLISHED;
report_changed_state(comp);
}
break;
case SYN_SENT:
if (TCPh->ControlBits == (SYN | ACK)) {
comp->tcp_state = ESTABLISHED;
report_changed_state(comp);
}
break;
case FIN_WAIT_1:
if (TCPh->ControlBits & ACK) {
comp->tcp_state = FIN_WAIT_2;
report_changed_state(comp);
}
break;
case FIN_WAIT_2:
if (TCPh->ControlBits & FIN) {
comp->tcp_state = TIME_WAIT;
report_changed_state(comp);
comp->tcp_state = CLOSED;
report_changed_state(comp);
}
break;
case ESTABLISHED:
if (TCPh->ControlBits & FIN) {
comp->tcp_state = CLOSE_WAIT;
report_changed_state(comp);
}
break;
}
}
void print_mss_if_changed(const u_char* tcp_options, int len) {
u_int mss;
for (int i = 0; i < len; i += 4) {
if (tcp_options[i] == 0x02 && tcp_options[i + 1] == 0x04) {
mss = tcp_options[i + 2];
mss <<= 8;
mss += tcp_options[i + 3];
if (global_mss != mss) {
global_mss = mss;
fprintf(OUT_INFO, "\t\tMSS: %u\n", mss);
}
}
}
}