cooking.c 22 KB
Newer Older
Arne Øslebø's avatar
Arne Øslebø committed
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <pcap.h>
#include <sys/shm.h>
#include <netinet/in.h>

#include "mapid.h"
#include "mapidflib.h"
#include "mapidlib.h"
#include "mapidevices.h"
#include "fhelp.h"
#include "cooking.h"
#include "list.h"

#define COOKING "COOKING"
#define SERVER 0
#define CLIENT 1
#define FRAG_BUF_SIZE 16*1024

//#define COOK_DEBUG

static unsigned int threshold;
static unsigned int timeout;

//////////////////////////////////////////////

static int decode_pkt(packet_t* pkt, const unsigned char* dev_pkt, mapid_pkthdr_t* hdr, int copy)
{
	int ether_len=0, ip_len=0, tcp_len=0;

	//////////////Create packet///////////////////
	/////////
	if (copy)
	{
#ifdef COOK_DEBUG
		printf("\nCopy packet:\n");
#endif
		pkt->pkthdr = (mapid_pkthdr_t*)malloc(sizeof(mapid_pkthdr_t));
		memcpy(pkt->pkthdr, hdr, sizeof(mapid_pkthdr_t));
		pkt->pkt = (byte*)malloc(sizeof(byte)*hdr->wlen);
		memcpy(pkt->pkt, dev_pkt, hdr->wlen);
	}
	else
	{
		pkt->pkthdr = hdr;		//zero-copy
		pkt->pkt = (unsigned char*)dev_pkt;
	}
	pkt->len = hdr->wlen;

	pkt->eth = (ether_header*)pkt->pkt;
	ether_len = sizeof(ether_header);		//ethernet header has const size
	if (pkt->eth->ether_type != 8)	//ip protocol number
		return 0;

	pkt->ip = (ip_header*)(pkt->pkt + ether_len);
	ip_len = (pkt->ip->ver_ihl & 0xf) * 4;	//ip doesn't have const size due to opt & pad
	if (pkt->ip->ptcl != 6)		//tcp protocol number
		return 0;
	
	pkt->tcp = (tcp_header*)(pkt->pkt + ether_len + ip_len);
	tcp_len = pkt->tcp->off * 4;			//tcp doesn't have const size due to opt & pad

	if (!copy)
	{
		//////////////////////////////////////////////
		//////adjust values (network to host)
		pkt->ip->tlen = ntohs(pkt->ip->tlen);
		pkt->ip->id   = ntohs(pkt->ip->id);
		pkt->ip->off  = ntohs(pkt->ip->off);

		pkt->tcp->seq   = ntohl(pkt->tcp->seq);
		pkt->tcp->ack   = ntohl(pkt->tcp->ack);
		pkt->tcp->sport = ntohs(pkt->tcp->sport);
		pkt->tcp->dport = ntohs(pkt->tcp->dport);
		pkt->tcp->win   = ntohs(pkt->tcp->win);
		//////////////////////////////////////////////
	}

	pkt->payload = (byte*)(pkt->pkt + ether_len + ip_len + tcp_len);
	pkt->payload_len = pkt->ip->tlen - ((pkt->ip->ver_ihl & 0xf) * 4) - (pkt->tcp->off * 4);
	//or maybe --> pkt->payload_len = pkt->len - (ether_len + ip_len + tcp_len);

#ifdef COOK_DEBUG
{
	int i=0;			//auxiliary variable for flag's printf
	u_char mask=0x20;	//auxiliary variable for flag's printf

	fprintf(stderr, "source addr: %d.%d.%d.%d\n", pkt->ip->saddr.byte1, pkt->ip->saddr.byte2,
												pkt->ip->saddr.byte3, pkt->ip->saddr.byte4);
	fprintf(stderr, "destin addr: %d.%d.%d.%d\n", pkt->ip->daddr.byte1, pkt->ip->daddr.byte2,
												pkt->ip->daddr.byte3, pkt->ip->daddr.byte4);
	fprintf(stderr, "source port: %d\n", pkt->tcp->sport);
	fprintf(stderr, "destin port: %d\n", pkt->tcp->dport);
	fprintf(stderr, "packet sequence #%u\n", pkt->tcp->seq);
	fprintf(stderr, "packet ack_seq  #%u\n", pkt->tcp->ack);
	fprintf(stderr, "URG|ACK|PSH|RST|SYN|FIN\n");
	for (i=0; i<6; i++)
	{
		fprintf(stderr, " %d  ", (pkt->tcp->flags & mask) ? 1 : 0);
		mask >>= 1;
	}
	fprintf(stderr, "\n");
	fprintf(stderr, "DF: %d MF: %d OFFSET: %d\n", (pkt->ip->DF) ? 1 : 0, (pkt->ip->MF) ? 1 : 0, (pkt->ip->FRAG_OFF));
	fprintf(stderr, "total length --> %d\n", pkt->ip->tlen);
	fprintf(stderr, "data length --> %d\n", pkt->payload_len);
	fprintf(stderr, "data length (second method) --> %d\n", pkt->len - (ether_len + ip_len + tcp_len));
}
#endif

	return 1;
}

//////////////////////////////////////////////

static int cmpSessions(session_t* left, session_t* right)
{
	u_int* left_server_ip = (u_int*)&left->server->addr;	//convert ip_addr structs into int for comparison
	u_int* left_client_ip = (u_int*)&left->client->addr;
	u_int* right_server_ip = (u_int*)&right->server->addr;
	u_int* right_client_ip = (u_int*)&right->client->addr;

	if ((*left_server_ip) == (*right_server_ip))
		if ((*left_client_ip) == (*right_client_ip))
			if (left->server->port == right->server->port)
				if (left->client->port == right->client->port)
					return 0;

	return -1;
}

//////////////////////////////////////////////

static int cmpFragments(fragment_t* left, fragment_t* right)
{
	return !(left->bufid == right->bufid);
}

//////////////////////////////////////////////

static int cmpPackets(packet_t* left, packet_t* right)
{
	return !(left->tcp->seq == right->tcp->seq);
}

//////////////////////////////////////////////

static void getDirection(stream_t** dst, stream_t** src, session_t* ssn, packet_t* pkt)
{
	u_int* src_addr = (u_int*)&(pkt->ip->saddr);	//convert ip_addr structs into int for comparison
	u_int* client_addr = (u_int*)&(ssn->client->addr);

	if ((*src_addr) == (*client_addr))
	{
		*dst = ssn->server;
		*src = ssn->client;
	}
	else
	{
		*dst = ssn->client;
		*src = ssn->server;
	}

	return;
}

//////////////////////////////////////////////

static session_t* getSession(list_t* sessions, packet_t* pkt)
{
	int index=-1;
	session_t pkt_ssn;
	stream_t server;
	stream_t client;

	server.addr = pkt->ip->saddr;
	client.addr = pkt->ip->daddr;
	server.port = pkt->tcp->sport;
	client.port = pkt->tcp->dport;
	pkt_ssn.server = &server;
	pkt_ssn.client = &client;

	index = list_find(sessions, &pkt_ssn);
	if (index < 0)
	{
		pkt_ssn.server = &client;
		pkt_ssn.client = &server;

		index = list_find(sessions, &pkt_ssn);
	}

	return (index < 0) ? NULL : list_get(sessions, index);
}

//////////////////////////////////////////////

static session_t* createSession(list_t* sessions, packet_t* pkt)
{
#define MIN_BUF_SIZE 32*1024
	session_t* ssn=NULL;
	int buf_size = (2*threshold > MIN_BUF_SIZE) ? 2*threshold : MIN_BUF_SIZE;

	if (pkt->tcp->flags != TCP_SYN)		//accept only sessions from beginning
		return NULL;

	ssn = (session_t*)malloc(sizeof(session_t));
	ssn->state = SSN_TWH;

	ssn->server = (stream_t*)malloc(sizeof(stream_t));
	ssn->server->buffer = (cooked_buffer_t*)malloc(sizeof(cooked_buffer_t));
	ssn->server->buffer->buf = (byte*)malloc(sizeof(byte)*buf_size);
	ssn->server->buffer->bufsize = buf_size;
	ssn->server->buffer->read_ptr = ssn->server->buffer->write_ptr = 0;
	ssn->server->buffer->first_seg_time = time(NULL);
	ssn->server->early_pkts = (list_t*)malloc(sizeof(list_t));
	list_init(ssn->server->early_pkts, (comparator)cmpPackets);

	ssn->client = (stream_t*)malloc(sizeof(stream_t));
	ssn->client->buffer = (cooked_buffer_t*)malloc(sizeof(cooked_buffer_t));
	ssn->client->buffer->buf = (byte*)malloc(sizeof(byte)*buf_size);
	ssn->client->buffer->bufsize = buf_size;
	ssn->client->buffer->read_ptr = ssn->client->buffer->write_ptr = 0;
	ssn->client->buffer->first_seg_time = time(NULL);
	ssn->client->early_pkts = (list_t*)malloc(sizeof(list_t));
	list_init(ssn->client->early_pkts, (comparator)cmpPackets);

	list_push_back(sessions, ssn);

	return ssn;
}

//////////////////////////////////////////////

static void destroySession(session_t* ssn)
{
	free(ssn->server->early_pkts);
	free(ssn->server->buffer->buf);
	free(ssn->server->buffer);
	free(ssn->server);

	free(ssn->client->early_pkts);
	free(ssn->client->buffer->buf);
	free(ssn->client->buffer);
	free(ssn->client);
	
	return;
}

//////////////////////////////////////////////

static int checkTWH(session_t* ssn, packet_t* pkt)
{
	if (pkt->tcp->flags == TCP_SYN)
	{
		ssn->start_time = ssn->last_pkt_time = time(NULL);

		ssn->client->addr = pkt->ip->saddr;
		ssn->client->port = pkt->tcp->sport;
		ssn->client->isn = ssn->client->last_seq = pkt->tcp->seq;
		ssn->client->state = SYN_SENT;

		ssn->server->addr = pkt->ip->daddr;
		ssn->server->port = pkt->tcp->dport;
		ssn->server->win_size = pkt->tcp->win;
		ssn->server->next_seq = ssn->client->last_seq +1;
		ssn->server->state = SYN_RCVD;
	}
	else if (pkt->tcp->flags == (TCP_SYN | TCP_ACK))
	{
		if (ssn->server->state != SYN_RCVD)		//problem server doesn't follow THW
		{
			fprintf(stderr, "error: server doesn't follow THW\n");
			//destroySession(ssn);
			return 0;
		}

		ssn->last_pkt_time = time(NULL);

		ssn->server->isn = ssn->server->last_seq = pkt->tcp->seq;
		ssn->server->state = LISTEN;

		ssn->client->win_size = pkt->tcp->win;
		ssn->client->next_seq = ssn->server->last_seq +1;
		ssn->client->state = SYN_RCVD;
	}
	else if (pkt->tcp->flags == TCP_ACK)
	{
		if (ssn->server->state != LISTEN)		//problem client didn't wait for SYN/ACK
		{
			fprintf(stderr, "error: client didn't wait for SYN/ACK\n");
			//destroySession(ssn);
			return 0;
		}

		ssn->last_pkt_time = time(NULL);
		ssn->state = SSN_ESTB;

		ssn->client->last_seq = pkt->tcp->seq;
		ssn->client->state = ESTB;

		ssn->server->win_size = pkt->tcp->win;
		ssn->server->next_seq = ssn->client->last_seq;
		ssn->server->state = ESTB;
	}

	return 1;
}

//////////////////////////////////////////////

static int checkClosure(session_t* ssn, packet_t* pkt)
{
	stream_t* dst;
	stream_t* src;

	getDirection(&dst, &src, ssn, pkt);

	if (ssn->state == SSN_ESTB && pkt->tcp->flags & TCP_FIN)
	{
		ssn->state = SSN_CLOSING;
		src->state = FIN_SENT;
		dst->state = FIN_RCVD;
	}
	else if (ssn->state >= SSN_CLOSING && (pkt->tcp->flags & TCP_ACK))
	{
		if (dst->state == FIN_SENT)
			dst->state = CLOSED;
		src->state = FIN_SENT;

		if (ssn->state == SSN_SCLOSED || ssn->state == SSN_CCLOSED)
			ssn->state = SSN_BCLOSED;
		else if (dst == ssn->server)
			ssn->state = SSN_SCLOSED;
		else
			ssn->state = SSN_CCLOSED;
	}

	return (ssn->state == SSN_BCLOSED) ? 1 : 0;
}

//////////////////////////////////////////////

static session_t* connect2Session(list_t* sessions, packet_t* pkt)
{
	session_t* ssn = getSession(sessions, pkt);

	if (ssn == NULL)	//first packet of a session
		ssn = createSession(sessions, pkt);

	if (ssn == NULL)	//it will still be NULL if packet cought in the middle of a session	(I drop it?)
	{
#ifdef COOK_DEBUG
		fprintf(stderr, "packet cought in the middle of a session (drop it?)\n");
#endif
		return NULL;
	}

	if (ssn->state == SSN_TWH && !checkTWH(ssn, pkt))
		return NULL;

	if (ssn->state >= SSN_CLOSING || (pkt->tcp->flags & TCP_FIN))
		checkClosure(ssn, pkt);

	return ssn;
}

//////////////////////////////////////////////

static int isIPFragmented(packet_t* pkt)
{
	pkt->ip->off = ntohs(pkt->ip->off);

	if ((pkt->ip->DF))
		return 0;
	else if ((pkt->ip->MF))
		return 1;
	else if ((pkt->ip->FRAG_OFF))
		return 1;

	return 0;
}

//////////////////////////////////////////////

static void destroyFragment(fragment_t* frag)
{
	free(frag->buf);
	free(frag->hdr_buf);
	free(frag->rcvbt);
	free(frag);

	return;
}

//////////////////////////////////////////////

static int defrag(packet_t* defrag_pkt, packet_t* pkt)
{
	static list_t* fragment_list = NULL;
	fragment_t* frag;
	int* src = (int*)&(pkt->ip->saddr);
	int* dst = (int*)&(pkt->ip->daddr);
	int bufid = *src | *dst | pkt->ip->ptcl | pkt->ip->id;
	fragment_t temp;
	int index;

	if (!fragment_list)
	{
		fragment_list = (list_t*)malloc(sizeof(list_t));
		list_init(fragment_list, (comparator)cmpFragments);
	}

	if ((pkt->ip->MF) && ((pkt->ip->FRAG_OFF) == 0))
		//first fragmented packet, create a fragment_t and insert it to list
	{
		frag = (fragment_t*)malloc(sizeof(fragment_t));
		frag->buf = (byte*)malloc(sizeof(byte) * FRAG_BUF_SIZE);
		frag->rcvbt = (byte*)calloc(FRAG_BUF_SIZE/8, sizeof(byte));
		frag->hdr_buf = (ip_header*)malloc(sizeof(byte) * ((pkt->ip->ver_ihl & 0xf) * 4));
		memcpy(frag->hdr_buf, pkt->ip, ((pkt->ip->ver_ihl & 0xf) * 4));	//store the original header
		frag->bufid = bufid;
		frag->timer = time(NULL);
		frag->tdl = 0;

		list_push_back(fragment_list, frag);
	}

	temp.bufid = bufid;
	index = list_find(fragment_list, &temp);
	if (index < 0)
		return 0;

	frag = (fragment_t*)list_get(fragment_list, index);
	memcpy(&frag->buf[(pkt->ip->FRAG_OFF)*8], ((byte*)pkt->ip + (pkt->ip->ver_ihl & 0xf)*4), pkt->ip->tlen - (pkt->ip->ver_ihl & 0xf)*4);
	memset(&frag->rcvbt[pkt->ip->FRAG_OFF], 0xFF, (pkt->ip->tlen - (pkt->ip->ver_ihl & 0xf)*4 + 7)/8);
	frag->timer = (frag->timer > pkt->ip->ttl) ? time(NULL) - frag->timer : pkt->ip->ttl;

	if (!(pkt->ip->MF) && (pkt->ip->FRAG_OFF))
		//last fragment of packet, time to reassemble
	{
		int ether_len, ip_len, tcp_len, i;

		frag->tdl = (pkt->ip->FRAG_OFF)*8 + pkt->payload_len + ((frag->hdr_buf->ver_ihl & 0xf) * 4);

		for (i=0; i < (frag->tdl - ((frag->hdr_buf->ver_ihl & 0xf) * 4)); i++)
		{
			if (frag->rcvbt[i] != 0xFF)
				printf("WARNING: a fragment of the packet has been lost!\n");
		}

		list_remove(fragment_list, index);
		defrag_pkt->pkt = (unsigned char*)malloc(sizeof(unsigned char)*frag->tdl);
		memcpy(defrag_pkt->pkt, frag->hdr_buf, ((frag->hdr_buf->ver_ihl & 0xf) * 4));
		memcpy(defrag_pkt->pkt, frag->buf, frag->tdl - ((frag->hdr_buf->ver_ihl & 0xf) * 4));

		defrag_pkt->pkthdr = (mapid_pkthdr_t*)malloc(sizeof(mapid_pkthdr_t));
		defrag_pkt->pkthdr->ts = pkt->pkthdr->ts;
		defrag_pkt->pkthdr->caplen = defrag_pkt->pkthdr->wlen = frag->tdl;

		defrag_pkt->eth = (ether_header*)defrag_pkt->pkt;
		ether_len = sizeof(ether_header);			//ethernet header has const size

		defrag_pkt->ip = (ip_header*)(defrag_pkt->pkt + ether_len);
		defrag_pkt->ip->tlen = frag->tdl + ((frag->hdr_buf->ver_ihl & 0xf) * 4);
		ip_len = (defrag_pkt->ip->ver_ihl & 0xf) * 4;	//ip doesn't have const size due to opt & pad

		defrag_pkt->tcp = (tcp_header*)(defrag_pkt->pkt + ether_len + ip_len);
		tcp_len = defrag_pkt->tcp->off * 4;			//tcp doesn't have const size due to opt & pad

		defrag_pkt->payload = defrag_pkt->pkt + ether_len + ip_len + tcp_len;
		defrag_pkt->len = frag->tdl;
		defrag_pkt->payload_len = frag->tdl - (ether_len + ip_len + tcp_len);

		destroyFragment(frag);
		return 1;
	}

	return 0;
}

//////////////////////////////////////////////

static void destroyPacket(packet_t* pkt)
{
	free(pkt->pkt);
	free(pkt->pkthdr);

	return;
}

//////////////////////////////////////////////

static void toBuffer(stream_t* dst, stream_t* src, packet_t* pkt)
{
	unsigned int i;
	cooked_buffer_t* cbuf = dst->buffer;
	char* buf = cbuf->buf;
	unsigned int first = cbuf->read_ptr;
	unsigned int last  = cbuf->write_ptr;
	unsigned int free_buf = (first <= last) ? cbuf->bufsize - (last - first) : first - last;

	//keep tracking of session state
	src->last_seq = pkt->tcp->seq;		//place all these in toBuffer
	dst->last_ack = pkt->tcp->ack;
	dst->win_size = pkt->tcp->win;
	dst->next_seq = src->last_seq + pkt->payload_len;
	//dst->next_seq = src->last_seq + (pkt->ip->tlen - ((pkt->ip->ver_ihl & 0xf) * 4) - (pkt->tcp->off * 4));
	//printf("pkt->tcp->seq: %u -- dst->next_seq: %u -- dst->last_seq :%u\n", pkt->tcp->seq, dst->next_seq, dst->last_seq);

	if (free_buf < pkt->payload_len)	//packet dropped
		return;

#ifdef COOK_DEBUG
	for (i=0; i < pkt->payload_len; i++)
		putchar(pkt->payload[i]);
	putchar('\n');
#endif

	if (free_buf == cbuf->bufsize)		//if it's first segment in an empty buffer store its timestamp
		cbuf->first_seg_time = time(NULL);

	for (i=0; i < pkt->payload_len; i++)
		buf[(last++) % (cbuf->bufsize)] = pkt->payload[i];
	cbuf->write_ptr = last;
	free_buf -= pkt->payload_len;

	return;
}

//////////////////////////////////////////////

static void addPkt2Session(session_t* ssn, packet_t* pkt)
{
	stream_t* src=NULL;
	stream_t* dst=NULL;

	getDirection(&dst, &src, ssn, pkt);

	ssn->last_pkt_time = time(NULL);
	if (pkt->tcp->seq == dst->next_seq)
		//add packet into buffer
	{
		toBuffer(dst, src, pkt);		//write pkt to dst->buffer

		while (dst->early_pkts->size > 0)	//aka. there are some packets which arrived earlier and should be checked
		{
			int index;
			tcp_header tcp;
			packet_t tmp_pkt;

			tcp.seq = dst->next_seq;
			tmp_pkt.tcp = &tcp;

			index = list_find(dst->early_pkts, &tmp_pkt);
			if (index >= 0)
			{
				packet_t* halted_pkt = list_remove(dst->early_pkts, index);
				toBuffer(dst, src, halted_pkt);
				destroyPacket(halted_pkt);
			}
		}
	}
	else if (pkt->tcp->seq > dst->next_seq)
		//a packet arrived earlier than another, store it temporarly
	{
		packet_t* early_pkt = (packet_t*)malloc(sizeof(early_pkt));
		decode_pkt(early_pkt, pkt->pkt, pkt->pkthdr, 1);

		list_push_back(dst->early_pkts, early_pkt);
	}

	return;
}

//////////////////////////////////////////////

static stream_t* isCookedPktReady(session_t* ssn, packet_t* pkt)
{
	cooked_buffer_t* buf=NULL;
	stream_t* dst;
	stream_t* src;
	unsigned int first, last, free_buf;

	getDirection(&dst, &src, ssn, pkt);
	buf = dst->buffer;

	first = buf->read_ptr;
	last  = buf->write_ptr;
	free_buf = (first <= last) ? buf->bufsize - (last - first) : first - last;

#ifdef COOK_DEBUG
printf("%d -- %d\n", (int)(time(NULL) - buf->first_seg_time), timeout);
#endif
	if ((buf->bufsize - free_buf) >= threshold)
		return dst;
	else if ((unsigned)(time(NULL) - buf->first_seg_time) >= timeout && ssn->server->buffer->read_ptr != ssn->server->buffer->write_ptr)
		return dst;
	else if (ssn->state == SSN_CLOSING && buf->read_ptr != buf->write_ptr)
		return dst;
	else if (ssn->state == SSN_SCLOSED && ssn->server->buffer->read_ptr != ssn->server->buffer->write_ptr)
		return ssn->server;
	else if (ssn->state == SSN_CCLOSED && ssn->client->buffer->read_ptr != ssn->client->buffer->write_ptr)
		return ssn->client;
	else if (ssn->state == SSN_BCLOSED && buf->read_ptr != buf->write_ptr)
		return dst;


	return NULL;
}

//////////////////////////////////////////////

static void getPkt(unsigned char* cooked_pkt, mapid_pkthdr_t* cooked_hdr, session_t* ssn, stream_t* dst)
{
	//unsigned char* pkt = NULL;
	unsigned int first, last, hdr_size, size, i;
	cooked_buffer_t* buf=NULL;
	ip_header* iph=NULL;
	tcp_header* tcph=NULL;

	buf  = dst->buffer;
	first = dst->buffer->read_ptr;
	last  = dst->buffer->write_ptr;
	size  = (first < last) ? last - first : dst->buffer->bufsize - (first - last);
	hdr_size = 14 + sizeof(ip_header) + sizeof(tcp_header);					//14 is ethernet header
	//pkt = (unsigned char*)malloc(sizeof(unsigned char)*(hdr_size + size));

	//////////////////////////////////////////////
	//////Construct pseudo tcp/ip header
	iph = (ip_header*)(cooked_pkt + sizeof(ether_header));
	tcph = (tcp_header*)(cooked_pkt + sizeof(ether_header) + sizeof(ip_header));

	iph->ver_ihl = 69;			//IPv4 & IP hdr len: 20bytes
	iph->ptcl = iph->ttl = 6;	//define protocol to TCP and give it 6sec to live
	iph->tos = iph->id = iph->off = iph->sum = 0;
	iph->tlen = ntohs(sizeof(ip_header) + sizeof(tcp_header) + size);
	iph->saddr = (ssn->server == dst) ? ssn->client->addr : ssn->server->addr;
	iph->daddr = (ssn->server == dst) ? ssn->server->addr : ssn->client->addr;

	tcph->off = 5;
	tcph->seq = tcph->ack = tcph->urp = tcph->crc = tcph->flags = tcph->win = 0;
	tcph->sport = (ssn->server == dst) ? ntohs(ssn->client->port) : ntohs(ssn->server->port);
	tcph->dport = (ssn->server == dst) ? ntohs(ssn->server->port) : ntohs(ssn->client->port);

	//////////////////////////////////////////////

	for (i=hdr_size; i < (hdr_size+size); i++)
		cooked_pkt[i] = buf->buf[(first++) % (buf->bufsize)];
	buf->read_ptr = first;

	//free(cooked_pkt);	//free old cooked_pkt
	//memcpy (cooked_pkt, pkt, hdr_size + size);	//assign just constructed packet as cooked_pkt
	cooked_hdr->caplen = cooked_hdr->wlen = hdr_size + size;

	return;
}

//////////////////////////////////////////////

static int cook_instance(mapidflib_function_instance_t* instance,
663
664
			 MAPI_UNUSED flist_t *flist,
			 MAPI_UNUSED mapidflib_flow_mod_t *flow_mod, MAPI_UNUSED function_manipulation_t* manip)
Arne Øslebø's avatar
Arne Øslebø committed
665
666
667
668
669
670
671
672
{
	instance->internal_data = malloc(sizeof(struct cooking_data*));
	return 0;
}

//////////////////////////////////////////////

static int cook_init(mapidflib_function_instance_t *instance,
673
		     MAPI_UNUSED flist_t *flist)
Arne Øslebø's avatar
Arne Øslebø committed
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
{
	mapiFunctArg* fargs = instance->args;
	struct cooking_data* data = (struct cooking_data*)instance->internal_data;
	list_t* list = (list_t*)malloc(sizeof(list_t));
	list_init(list, (comparator)cmpSessions);

	data->threshold = getargint(&fargs);
	data->timeout   = getargint(&fargs);

	printf("threshold: %d\ttimeout: %d\n",
		(data->threshold < 0) ? 32*1024 : data->threshold,
		(data->timeout < 0) ? 30 : (data->timeout));
	data->sessions = (void*)list;

	return 0;
}

//////////////////////////////////////////////

static int cook_process(mapidflib_function_instance_t *instance,
694
			MAPI_UNUSED const unsigned char* dev_pkt,
Arne Øslebø's avatar
Arne Øslebø committed
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
			const unsigned char* link_pkt,
			mapid_pkthdr_t* hdr)
{
#ifdef COOK_DEBUG
	static int counter=0, cooked_cnt=0;
#endif
	struct cooking_data* data = (struct cooking_data*)instance->internal_data;
	list_t* sessions = (list_t*)((struct cooking_data*)data)->sessions;
	packet_t pkt;
	session_t* ssn;
	stream_t* dst;

	threshold = (data->threshold == -1) ? 32*1024 : data->threshold;
	timeout = (data->timeout == -1) ? 30 : data->timeout;

#ifdef COOK_DEBUG
	fprintf(stderr, "\nPacket captured #%d\n", ++counter);
#endif
	if (!decode_pkt(&pkt, link_pkt, hdr, 0))
		return 0;

	if ((ssn = connect2Session(sessions, &pkt)) == NULL)
		return 0;

	if (pkt.payload_len)
	{
		if (isIPFragmented(&pkt))
		{
			packet_t defrag_pkt;

			if (defrag(&defrag_pkt, &pkt))
				addPkt2Session(ssn, &defrag_pkt);
		}

		addPkt2Session(ssn, &pkt);
	}

	if ((dst = isCookedPktReady(ssn, &pkt)))
	{
		getPkt((unsigned char*)link_pkt, hdr, ssn, dst);

#ifdef COOK_DEBUG
		printf("%d bytes cooked packet #%d send away!\n", hdr->wlen, ++cooked_cnt);
#endif
		if (ssn->state == SSN_BCLOSED)	//destroy session if it has been closed
		{
			destroySession(ssn);
			free(ssn);
		}
#ifdef COOK_DEBUG
		decode_pkt(&pkt, link_pkt, hdr, 0);
#endif
		return 1;
	}

	return 0;
}

//////////////////////////////////////////////

static int cook_cleanup(mapidflib_function_instance_t *instance)
{
	struct cooking_data* data = (struct cooking_data*)instance->internal_data;
	list_destroy(data->sessions, LIST_FREE_DATA);
	free((void*)data->sessions);
	data->sessions = NULL;
		
	return 0;
}

//////////////////////////////////////////////

static mapidflib_function_def_t finfo =
{
	"", //libname
	COOKING, //name
	"Cooking TCP/IP packets\nParameters:\n\tthreshold : int\n\ttimeout : int\n", //Description
	"ii", //argdescr
	MAPI_DEVICE_ALL, //Devoid
	MAPIRES_NONE,
	0, //shm size
	1, //modifies_pkts
	cook_instance, //instance
	cook_init, //init
	cook_process, //process
	NULL, //get_result,
	NULL, //change_args
	NULL, //reset
	cook_cleanup, //cleanup
	NULL, //client_init
	NULL, //client_read_result
	NULL  //client_cleanup
};

mapidflib_function_def_t* cooking_get_funct_info();

mapidflib_function_def_t* cooking_get_funct_info()
{
	return &finfo;
};