1 #include <stdio.h>
2 #include <string.h>
3 #include <unistd.h>
4 
5 #define  __FAVOR_BSD
6 #include <netinet/in_systm.h>
7 #include <netinet/in.h>
8 #include <netinet/ip.h>
9 #include <netinet/tcp.h>
10 #include <stdlib.h>
11 
12 
13 #include "file2pcap.h"
14 #include "helpers.h"
15 
16 
17 extern int packetLen4, packetLen6;
18 
19 /**************************************************************************/
20 
http2ConnectionUpgrade(struct handover * ho)21 int http2ConnectionUpgrade(struct handover *ho) {
22 	char *encoded=NULL;
23 	char requestEnd[] = 	"Host: wrl\r\n"
24                                 "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.17) Gecko/20081007 Firefox/2.0.0.17\r\n"
25 				"Accept: */*\r\n"
26 				"Connection: Upgrade, HTTP2-Settings\r\n"
27 				"Upgrade: h2c\r\n"
28 				"HTTP2-Settings: AAMAAABkAAQAAP__\r\n\r\n";
29 
30         char tmp[700];
31 
32 
33 	encoded=URLencoder(ho->srcFile);
34 
35 	if(encoded==NULL)
36 		exit(-1);
37 
38 
39         snprintf(tmp, sizeof(tmp)-1,"GET /file2pcap/%s HTTP/1.1\r\n%s", encoded, requestEnd);
40 
41 	tcpSendString(ho, tmp, TO_SERVER);
42 
43 
44 return(0);
45 }
46 
47 
48 
49 /***************************************************************************/
50 
http2SwitchingProtocols(struct handover * ho)51 int http2SwitchingProtocols(struct handover *ho) {
52 
53 	char switchingProtocols[] = 	"HTTP/1.1 101 Switching Protocols\r\n"
54 					"Connection: Upgrade\r\n"
55 					"Upgrade: h2c\r\n\r\n";
56 
57 	tcpSendString(ho, switchingProtocols, FROM_SERVER);
58 
59 return 0;
60 }
61 
62 
63 /***************************************************************************/
64 
http2ClientMagic(struct handover * ho)65 int http2ClientMagic(struct handover *ho) {
66 	char http2Magic[] = 	{	"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
67 				};
68 
69 
70 	struct pcap_packet_header ph;
71 	int len, packetLen;
72 
73 
74 
75 	len = strlen(http2Magic);
76 
77 
78 	ph.time = ho->time;
79 	ph.usec = ho->usec;
80 
81         if(ho->ipV == 4)
82                 packetLen = packetLen4;
83         else
84                 packetLen = packetLen6;
85 
86 
87 	ph.length1 = packetLen + len;
88 	ph.length2 = packetLen + len;
89 
90 
91 	write(fileno(ho->outFile), &ph, sizeof(struct pcap_packet_header));
92 
93 	if(ho->direction == FROM_SERVER)
94 		write(fileno(ho->outFile), ho->fromEther, sizeof(ho->fromEther)-1);
95 	else
96 		write(fileno(ho->outFile), ho->toEther, sizeof(ho->toEther)-1);
97 
98 	craftTcp(http2Magic, len, ho->direction == FROM_SERVER ? FROM_SERVER : TO_SERVER, TH_ACK|TH_PUSH, ho);
99 
100 
101 	//and now send the tcp ack
102 	ph.usec+=INTERVAL;
103 	ph.length1=packetLen;
104 	ph.length2=packetLen;
105 	write(fileno(ho->outFile), &ph, sizeof(struct pcap_packet_header));
106 
107 	if(ho->direction == FROM_SERVER)
108 		write(fileno(ho->outFile), ho->toEther, sizeof(ho->toEther)-1);
109 	else
110 		write(fileno(ho->outFile), ho->fromEther, sizeof(ho->fromEther)-1);
111 
112 
113 	craftTcp(NULL,0, ho->direction == FROM_SERVER ? TO_SERVER : FROM_SERVER, TH_ACK, ho);
114 
115 
116 return 0;
117 }
118 
119 
120 /***************************************************************************/
121 
http2ClientSettings(struct handover * ho)122 int http2ClientSettings(struct handover *ho) {
123 	char http2Settings[] = {        0x00, 0x00, 0x0c, 			//Length: 12
124 					0x04, 					//Type: Headers
125 					0x00, 					//Flags: 0x00
126 					0x00, 0x00, 0x00, 0x00, 		//Stream identifier: 0x00
127 					0x00, 0x03, 0x00, 0x00, 0x00, 0x64, 	//Max concurrent streams: 100
128 					0x00, 0x04, 0x00, 0x00, 0xff, 0xff 	//Initial window size: 0x00, 0x00, 0xff, 0xff
129 					};
130 
131 
132 
133 	struct pcap_packet_header ph;
134 	int len, packetLen;
135 
136 
137 
138 	len = sizeof(http2Settings);
139 
140 
141 	ph.time = ho->time;
142 	ph.usec = ho->usec;
143 
144         if(ho->ipV == 4)
145                 packetLen = packetLen4;
146         else
147                 packetLen = packetLen6;
148 
149 
150 	ph.length1 = packetLen + len;
151 	ph.length2 = packetLen + len;
152 
153 
154 	write(fileno(ho->outFile), &ph, sizeof(struct pcap_packet_header));
155 
156 	if(ho->direction == FROM_SERVER)
157 		write(fileno(ho->outFile), ho->fromEther, sizeof(ho->fromEther)-1);
158 	else
159 		write(fileno(ho->outFile), ho->toEther, sizeof(ho->toEther)-1);
160 
161 	craftTcp(http2Settings, len, ho->direction == FROM_SERVER ? FROM_SERVER : TO_SERVER, TH_ACK|TH_PUSH, ho);
162 
163 
164 	//and now send the tcp ack
165 	ph.usec+=INTERVAL;
166 	ph.length1=packetLen;
167 	ph.length2=packetLen;
168 	write(fileno(ho->outFile), &ph, sizeof(struct pcap_packet_header));
169 
170 	if(ho->direction == FROM_SERVER)
171 		write(fileno(ho->outFile), ho->toEther, sizeof(ho->toEther)-1);
172 	else
173 		write(fileno(ho->outFile), ho->fromEther, sizeof(ho->fromEther)-1);
174 
175 
176 	craftTcp(NULL,0, ho->direction == FROM_SERVER ? TO_SERVER : FROM_SERVER, TH_ACK, ho);
177 
178 
179 
180 return 0;
181 }
182 
183 
184 /***************************************************************************/
185 
http2ClientGetRequest(struct handover * ho)186 int http2ClientGetRequest(struct handover *ho) {
187 	char *encoded = NULL;
188 	char http2HeaderStart[] = {     0x01,   //stream id 1
189                                 	0x05,   //flags end headers && end stream
190                                 	0x00, 0x00, 0x00, 0x01, //reserved
191                                 	0x01, 0x06, 'w','r','l',':','8','0', //authority wrl:80
192 					0x82,   //method: GET, hufman
193                                 	0x04
194 				  };
195 	char http2HeaderCenter[] = {	 '/','f','i','l','e','2','p','c','a','p','/' //path
196                           	   };
197 	char http2HeaderEnd[] =  {      0x86,   //scheme:http
198                                 	0x53, 0x03, 0x2a, 0x2f, 0x2a,
199                                 	0x58, 0x08, 'n', 'o', '-', 'c', 'a', 'c', 'h', 'e'   //cache-control: no cache
200 				  };
201 	char request[500];
202 	struct pcap_packet_header ph;
203 	int len, packetLen;
204 
205 
206 
207 
208 	memset(request, 0, sizeof(request));
209         encoded=URLencoder(ho->srcFile);
210         if(encoded==NULL)
211                 exit(-1);
212 
213 
214 
215 	request[2]=(char)(sizeof(http2HeaderStart) - 6 + 1 + strlen(encoded) + strlen("/file2pcap/") + sizeof(http2HeaderEnd));
216         memcpy(request + 3, &http2HeaderStart, sizeof(http2HeaderStart));
217         request[3 + sizeof(http2HeaderStart) ]=(char)(strlen(encoded) + strlen("/file2pcap/"));
218         memcpy(request + 3 + sizeof(http2HeaderStart) + 1, &http2HeaderCenter, sizeof(http2HeaderCenter));
219         memcpy(request + 3 + sizeof(http2HeaderStart) + 1 + strlen("/file2pcap/"), encoded, strlen(encoded));
220 	memcpy(request + 3 + sizeof(http2HeaderStart) + 1 + strlen("/file2pcap/") + strlen(encoded), http2HeaderEnd, sizeof(http2HeaderEnd));
221 
222 
223 	len = 3 + sizeof(http2HeaderStart) + 1 + strlen("/file2pcap/") + strlen(encoded) + strlen(http2HeaderEnd);
224 
225 
226 
227 
228 	ph.time = ho->time;
229 	ph.usec = ho->usec;
230 
231         if(ho->ipV == 4)
232                 packetLen = packetLen4;
233         else
234                 packetLen = packetLen6;
235 
236 
237 	ph.length1 = packetLen + len;
238 	ph.length2 = packetLen + len;
239 
240 
241 	write(fileno(ho->outFile), &ph, sizeof(struct pcap_packet_header));
242 
243 	if(ho->direction == FROM_SERVER)
244 		write(fileno(ho->outFile), ho->fromEther, sizeof(ho->fromEther)-1);
245 	else
246 		write(fileno(ho->outFile), ho->toEther, sizeof(ho->toEther)-1);
247 
248 	craftTcp(request, len, ho->direction == FROM_SERVER ? FROM_SERVER : TO_SERVER, TH_ACK|TH_PUSH, ho);
249 
250 
251 	//and now send the tcp ack
252 	ph.usec+=INTERVAL;
253 	ph.length1=packetLen;
254 	ph.length2=packetLen;
255 	write(fileno(ho->outFile), &ph, sizeof(struct pcap_packet_header));
256 
257 	if(ho->direction == FROM_SERVER)
258 		write(fileno(ho->outFile), ho->toEther, sizeof(ho->toEther)-1);
259 	else
260 		write(fileno(ho->outFile), ho->fromEther, sizeof(ho->fromEther)-1);
261 
262 
263 	craftTcp(NULL,0, ho->direction == FROM_SERVER ? TO_SERVER : FROM_SERVER, TH_ACK, ho);
264 
265 
266 return 0;
267 }
268 
269 /*********************************************************************************************************************************************/
270 
http2MagicGetRequest(struct handover * ho)271 int http2MagicGetRequest(struct handover *ho) {
272 	char request[500];
273 	char *encoded=NULL;
274 	char http2Magic[] = {           "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"};
275 	char http2Settings[] = {        0x00, 0x00, 0x0c, 			//Length: 12
276 					0x04, 					//Type: Headers
277 					0x00, 					//Flags: 0x00
278 					0x00, 0x00, 0x00, 0x00, 		//Stream identifier: 0x00
279 					0x00, 0x03, 0x00, 0x00, 0x00, 0x64, 	//Max concurrent streams: 100
280 					0x00, 0x04, 0x00, 0x00, 0xff, 0xff 	//Initial window size: 0x00, 0x00, 0xff, 0xff
281 					};
282 
283 	char http2HeaderStart[] = {     0x01,   //stream id 1
284                                 	0x05,   //flags end headers && end stream
285                                 	0x00, 0x00, 0x00, 0x01, //reserved
286                                 	0x01, 0x06, 'w','r','l',':','8','0', //authority wrl:80
287 					0x82,   //method: GET, hufman
288                                 	0x04
289 				  };
290 	char http2HeaderCenter[] = {	 '/','f','i','l','e','2','p','c','a','p','/' //path
291                           	   };
292 	char http2HeaderEnd[] =  {      0x86,   //scheme:http
293                                 	0x53, 0x03, 0x2a, 0x2f, 0x2a,
294                                 	0x58, 0x08, 'n', 'o', '-', 'c', 'a', 'c', 'h', 'e'   //cache-control: no cache
295 				  };
296 	struct pcap_packet_header ph;
297 	int len, packetLen;
298 
299 
300 
301 
302 	memset(request,0,sizeof(request));
303 
304         encoded=URLencoder(ho->srcFile);
305         if(encoded==NULL)
306                 exit(-1);
307 
308 
309 	snprintf(request, sizeof(request)-1,"%s", http2Magic);
310 	memcpy(request+strlen(http2Magic), &http2Settings, sizeof(http2Settings));
311 	request[strlen(http2Magic) + sizeof(http2Settings) + 0]=0x00;
312 	request[strlen(http2Magic) + sizeof(http2Settings) + 1]=0x00;
313 	request[strlen(http2Magic) + sizeof(http2Settings) + 2]=(char)(sizeof(http2HeaderStart) - 6 + 1 + strlen(encoded) + strlen("/file2pcap/") + sizeof(http2HeaderEnd));
314 
315 
316         memcpy(request + strlen(http2Magic) + sizeof(http2Settings) + 3, &http2HeaderStart, sizeof(http2HeaderStart));
317 
318 
319         request[strlen(http2Magic) + sizeof(http2Settings) + 3 + sizeof(http2HeaderStart) ]=(char)(strlen(encoded) + strlen("/file2pcap/"));
320         memcpy(request + strlen(http2Magic) + sizeof(http2Settings) + 3 + sizeof(http2HeaderStart) + 1, &http2HeaderCenter, sizeof(http2HeaderCenter));
321         memcpy(request + strlen(http2Magic) + sizeof(http2Settings) + 3 + sizeof(http2HeaderStart) + 1 + strlen("/file2pcap/"), encoded, strlen(encoded));
322 	memcpy(request + strlen(http2Magic) + sizeof(http2Settings) + 3 + sizeof(http2HeaderStart) + 1 + strlen("/file2pcap/") + strlen(encoded), http2HeaderEnd, sizeof(http2HeaderEnd));
323 
324 
325 	len = strlen(http2Magic) + sizeof(http2Settings) + 3 + sizeof(http2HeaderStart) + 1 + strlen("/file2pcap/") + strlen(encoded) + strlen(http2HeaderEnd);
326 
327 
328 
329 
330 
331 	ph.time = ho->time;
332 	ph.usec = ho->usec;
333 
334         if(ho->ipV == 4)
335                 packetLen = packetLen4;
336         else
337                 packetLen = packetLen6;
338 
339 
340 	ph.length1 = packetLen + len;
341 	ph.length2 = packetLen + len;
342 
343 
344 	write(fileno(ho->outFile), &ph, sizeof(struct pcap_packet_header));
345 
346 	if(ho->direction == FROM_SERVER)
347 		write(fileno(ho->outFile), ho->fromEther, sizeof(ho->fromEther)-1);
348 	else
349 		write(fileno(ho->outFile), ho->toEther, sizeof(ho->toEther)-1);
350 
351 
352 	craftTcp(request, len, ho->direction == FROM_SERVER ? FROM_SERVER : TO_SERVER, TH_ACK|TH_PUSH, ho);
353 
354 
355 	//and now send the ack
356 	ph.usec+=INTERVAL;
357 	ph.length1=packetLen;
358 	ph.length2=packetLen;
359 	write(fileno(ho->outFile), &ph, sizeof(struct pcap_packet_header));
360 
361 	if(ho->direction == FROM_SERVER)
362 		write(fileno(ho->outFile), ho->toEther, sizeof(ho->toEther)-1);
363 	else
364 		write(fileno(ho->outFile), ho->fromEther, sizeof(ho->fromEther)-1);
365 
366 
367 	craftTcp(NULL,0, ho->direction == FROM_SERVER ? TO_SERVER : FROM_SERVER, TH_ACK, ho);    //direction - client to server
368 
369 
370 
371 
372 
373 return 0;
374 }
375 
376 
377 /************************************************************************************/
378 
379 //This sends the clients settings ACK to the server
380 
http2SettingsAck(struct handover * ho)381 int http2SettingsAck(struct handover *ho) {
382 	char settingsAck[] = 	{
383 					0x00, 0x00 ,0x00, 	//Length: 0
384 					0x04, 			//Type: Settings
385 					0x01,  			//Flags: Ack
386 					0x00, 0x00, 0x00, 0x00	//Stream identifier: 0
387 				};
388 	struct pcap_packet_header ph;
389 	int len, packetLen;
390 
391 
392 
393 	len = sizeof(settingsAck);
394 
395 
396 	ph.time = ho->time;
397 	ph.usec = ho->usec;
398 
399         if(ho->ipV == 4)
400                 packetLen = packetLen4;
401         else
402                 packetLen = packetLen6;
403 
404 
405 	ph.length1 = packetLen + len;
406 	ph.length2 = packetLen + len;
407 
408 
409 	write(fileno(ho->outFile), &ph, sizeof(struct pcap_packet_header));
410 
411 	if(ho->direction == FROM_SERVER)
412 		write(fileno(ho->outFile), ho->fromEther, sizeof(ho->fromEther)-1);
413 	else
414 		write(fileno(ho->outFile), ho->toEther, sizeof(ho->toEther)-1);
415 
416 	craftTcp(settingsAck, len, ho->direction == FROM_SERVER ? FROM_SERVER : TO_SERVER, TH_ACK|TH_PUSH, ho);
417 
418 
419 	//and now send the tcp ack
420 	ph.usec+=INTERVAL;
421 	ph.length1=packetLen;
422 	ph.length2=packetLen;
423 	write(fileno(ho->outFile), &ph, sizeof(struct pcap_packet_header));
424 
425 	if(ho->direction == FROM_SERVER)
426 		write(fileno(ho->outFile), ho->toEther, sizeof(ho->toEther)-1);
427 	else
428 		write(fileno(ho->outFile), ho->fromEther, sizeof(ho->fromEther)-1);
429 
430 
431 	craftTcp(NULL,0, ho->direction == FROM_SERVER ? TO_SERVER : FROM_SERVER, TH_ACK, ho);
432 
433 
434 return 0;
435 }
436 
437 
438 /************************************************************************************/
439 
440 //This sends the clients settings to the server, which the server acks, and also the client acks the server's settings, sent previously
http2Settings(struct handover * ho)441 int http2Settings(struct handover *ho) {
442 	char settings[] = {
443 					0x00, 0x00, 0x0c,					//Length: 12
444 					0x04,							//Type: Settings
445 					0x00,							//Flags: 0x00
446 					0x00, 0x00, 0x00, 0x00,					//Stream identifier: 0x00
447 					0x00, 0x03, 0x00, 0x00, 0x00, 0x64,			//Settings: Max concurrent stream: 100
448 					0x00, 0x04, 0x00, 0x00, 0xff, 0xff			//Settings: Initial window size: 0x40000000
449 				};
450 	struct pcap_packet_header ph;
451 	int len, packetLen;
452 
453 
454 	//Send settings from client to server
455 
456 	len = sizeof(settings);
457 
458 
459 	ph.time = ho->time;
460 	ph.usec = ho->usec;
461 
462         if(ho->ipV == 4)
463                 packetLen = packetLen4;
464         else
465                 packetLen = packetLen6;
466 
467 
468 	ph.length1 = packetLen + len;
469 	ph.length2 = packetLen + len;
470 
471 
472 	write(fileno(ho->outFile), &ph, sizeof(struct pcap_packet_header));
473 
474 	if(ho->direction == FROM_SERVER)
475 		write(fileno(ho->outFile), ho->fromEther, sizeof(ho->fromEther)-1);
476 	else
477 		write(fileno(ho->outFile), ho->toEther, sizeof(ho->toEther)-1);
478 
479 
480 	craftTcp(settings, len, ho->direction == FROM_SERVER ? FROM_SERVER : TO_SERVER, TH_ACK|TH_PUSH, ho);
481 
482 
483 	//and now send the ack
484 	ph.usec+=INTERVAL;
485 	ph.length1=packetLen;
486 	ph.length2=packetLen;
487 	write(fileno(ho->outFile), &ph, sizeof(struct pcap_packet_header));
488 
489 	if(ho->direction == FROM_SERVER)
490 		write(fileno(ho->outFile), ho->toEther, sizeof(ho->toEther)-1);
491 	else
492 		write(fileno(ho->outFile), ho->fromEther, sizeof(ho->fromEther)-1);
493 
494 
495 	craftTcp(NULL,0, ho->direction == FROM_SERVER ? TO_SERVER : FROM_SERVER, TH_ACK, ho);    //direction - client to server
496 
497 
498 return 0;
499 }
500 
501 
502 
503 /***************************************************************************************/
504 
http2Headers(struct handover * ho)505 int http2Headers(struct handover *ho) {
506 	char serverHeader[] = 	{	0x00, 0x00, 0x17, 	//3 bytes Length
507 					0x01,			//Type: Headers
508 					0x04,			//Flags
509 					0x00, 0x00, 0x00, 0x01,	//Reserved
510 					0x88,			//Status: 200
511 					0x10, 0x0e, 'c','o','n','t','e','n','t','-','l','e','n','g','t','h'};	//content-length
512 	int len, packetLen, fullLen;
513 	struct pcap_packet_header ph;
514 	char temp[20], request[sizeof(serverHeader) + sizeof(temp)];
515 	size_t filelenLen;
516 
517 
518 	memset(temp, 0, sizeof(temp));
519 	snprintf(temp, sizeof(temp)-1, "%d",(unsigned int)ho->inFileSize);
520 	filelenLen = (char)strlen(temp);
521 	char *bufferPointer;
522 
523 
524 	memcpy(&request, &serverHeader, sizeof(serverHeader));
525 	request[26]=filelenLen;
526 	bufferPointer = request+27;
527 	sprintf(bufferPointer, "%d",(unsigned int)ho->inFileSize);
528 
529 
530 
531 	len = sizeof(serverHeader) + 1 + filelenLen;
532 
533 	//fix the first 3 bytes - the length field
534 	fullLen = 18 + filelenLen;
535 	char testpointer = ((char)fullLen);
536 	memcpy(request+2, &testpointer, 1);
537 
538 
539 	ph.time = ho->time;
540 	ph.usec = ho->usec;
541 
542         if(ho->ipV == 4)
543                 packetLen = packetLen4;
544         else
545                 packetLen = packetLen6;
546 
547 
548 	ph.length1 = packetLen + len;
549 	ph.length2 = packetLen + len;
550 
551 
552 
553 
554 
555         write(fileno(ho->outFile), &ph, sizeof(struct pcap_packet_header));
556 
557         if(ho->direction == FROM_SERVER)
558                 write(fileno(ho->outFile), ho->fromEther, sizeof(ho->fromEther)-1);
559         else
560                 write(fileno(ho->outFile), ho->toEther, sizeof(ho->toEther)-1);
561 
562 	craftTcp(request, len, ho->direction == FROM_SERVER ? FROM_SERVER : TO_SERVER, TH_ACK|TH_PUSH, ho);
563 
564 
565 	//and now send the ack
566 	ph.usec+=INTERVAL;
567 	ph.length1=packetLen;
568 	ph.length2=packetLen;
569 	write(fileno(ho->outFile), &ph, sizeof(struct pcap_packet_header));
570 
571 	if(ho->direction == FROM_SERVER)
572 		write(fileno(ho->outFile), ho->toEther, sizeof(ho->toEther)-1);
573 	else
574 		write(fileno(ho->outFile), ho->fromEther, sizeof(ho->fromEther)-1);
575 
576 
577 	craftTcp(NULL,0, ho->direction == FROM_SERVER ? TO_SERVER : FROM_SERVER, TH_ACK, ho);    //direction - client to server
578 
579 
580 
581 
582 return 0;
583 }
584 
585 
586 
587 
588 /**************************************************************************************************************/
589 
590 
http2TransferFile(struct handover * ho)591 int http2TransferFile(struct handover *ho) {
592         int packetLen;
593         unsigned int count, len;
594         char buffer[1500], data[1520];
595 	struct pcap_packet_header ph;
596 
597 
598         if(ho->inFile != NULL)
599                 rewind(ho->inFile);
600 
601         //initialize ph with the current values
602 	ph.time = ho->time;
603 	ph.usec = ho->usec;
604 
605 
606 	memset(data, 0, sizeof(data));
607 	data[8]=0x01;	//stream id 1;
608 
609 
610 	if(ho->ipV == 4)
611 		packetLen = packetLen4;
612 	else
613 		packetLen = packetLen6;
614 
615 
616 
617         while(!(feof(ho->inFile)))
618         {
619                 count=read(fileno(ho->inFile), buffer, ho->blockSize);
620 
621 		len = count;
622 		data[0] = len / 65536;
623 		len     = len % 65536;
624 		data[1] = len / 256;
625 		len     = len % 256;
626 		data[2] = len;
627 
628 
629 		memcpy(data+9, &buffer, count);
630 
631                 if(count<=0)
632                 {
633 			ho->time = ph.time;
634 			ho->usec = ph.usec;
635                         return 0;	//FIXME ??
636                 }
637 
638 
639                 ph.usec += INTERVAL;
640                 if((ph.usec + INTERVAL) >= 1000000)
641                 {
642                         ph.time+=1;
643                         ph.usec=0;
644                 }
645 
646 
647                 ph.length1 = packetLen + count + 9;
648                 ph.length2 = packetLen + count + 9;
649 
650 
651                 write(fileno(ho->outFile), &ph, sizeof(struct pcap_packet_header));
652 
653 		if(ho->direction == FROM_SERVER)
654 	                write(fileno(ho->outFile), ho->fromEther, sizeof(ho->fromEther)-1);
655 		else
656 			write(fileno(ho->outFile), ho->toEther, sizeof(ho->toEther)-1);
657 
658 
659 
660                 craftTcp(data, count+9, ho->direction == FROM_SERVER ? FROM_SERVER : TO_SERVER, TH_ACK|TH_PUSH, ho);
661 
662                 //and now send the ack
663                 ph.usec+=INTERVAL;
664                 ph.length1=packetLen;
665                 ph.length2=packetLen;
666                 write(fileno(ho->outFile), &ph, sizeof(struct pcap_packet_header));
667 
668 		if(ho->direction == FROM_SERVER)
669 	                write(fileno(ho->outFile), ho->toEther, sizeof(ho->toEther)-1);
670 		else
671 	                write(fileno(ho->outFile), ho->fromEther, sizeof(ho->fromEther)-1);
672 
673 
674 
675                 craftTcp(NULL,0, ho->direction == FROM_SERVER ? TO_SERVER : FROM_SERVER, TH_ACK, ho);    //direction - client to server
676         }
677 
678 	ho->time = ph.time;
679 	ho->usec = ph.usec;
680 
681 
682 
683 return(0);
684 }
685 
686 
687 /***************************************************/
688 
689 
http2DataStreamClose(struct handover * ho)690 int http2DataStreamClose(struct handover *ho) {
691 	char dataClose[] = 	{	0x00, 0x00, 0x00, 		//3 bytes Length
692 					0x00,				//Type: Data
693 					0x01,				//Flags: End of data
694 					0x00, 0x00, 0x00, 0x01};	//Reserved; Stream id 1;
695 	int len, packetLen;
696 	struct pcap_packet_header ph;
697 
698 
699 	len = sizeof(dataClose);
700 
701 
702 	ph.time = ho->time;
703 	ph.usec = ho->usec;
704 
705         if(ho->ipV == 4)
706                 packetLen = packetLen4;
707         else
708                 packetLen = packetLen6;
709 
710 
711 	ph.length1 = packetLen + len;
712 	ph.length2 = packetLen + len;
713 
714 
715 
716 
717 
718         write(fileno(ho->outFile), &ph, sizeof(struct pcap_packet_header));
719 
720         if(ho->direction == FROM_SERVER)
721                 write(fileno(ho->outFile), ho->fromEther, sizeof(ho->fromEther)-1);
722         else
723                 write(fileno(ho->outFile), ho->toEther, sizeof(ho->toEther)-1);
724 
725 	craftTcp(dataClose, len, ho->direction == FROM_SERVER ? FROM_SERVER : TO_SERVER, TH_ACK|TH_PUSH, ho);
726 
727 
728 	//and now send the ack
729 	ph.usec+=INTERVAL;
730 	ph.length1=packetLen;
731 	ph.length2=packetLen;
732 	write(fileno(ho->outFile), &ph, sizeof(struct pcap_packet_header));
733 
734 	if(ho->direction == FROM_SERVER)
735 		write(fileno(ho->outFile), ho->toEther, sizeof(ho->toEther)-1);
736 	else
737 		write(fileno(ho->outFile), ho->fromEther, sizeof(ho->fromEther)-1);
738 
739 
740 	craftTcp(NULL,0, ho->direction == FROM_SERVER ? TO_SERVER : FROM_SERVER, TH_ACK, ho);    //direction - client to server
741 
742 
743 
744 
745 return 0;
746 }
747 
748 
749 /***************************************************/
750 
751 
http2GoAway(struct handover * ho)752 int http2GoAway(struct handover *ho) {
753 	char goAway[] = 	{	0x00, 0x00, 0x08, 		//3 bytes Length
754 					0x07,				//Type: GoAway
755 					0x00,				//Flags
756 					0x00, 0x00, 0x00, 0x00,		//Reserved; Stream id 0
757 					0x00, 0x00, 0x00, 0x00,		//Reserved
758 					0x00, 0x00, 0x00, 0x00};	//No Error
759 	int len, packetLen;
760 	struct pcap_packet_header ph;
761 
762 
763 	len = sizeof(goAway);
764 
765 
766 	ph.time = ho->time;
767 	ph.usec = ho->usec;
768 
769         if(ho->ipV == 4)
770                 packetLen = packetLen4;
771         else
772                 packetLen = packetLen6;
773 
774 
775 	ph.length1 = packetLen + len;
776 	ph.length2 = packetLen + len;
777 
778 
779 
780 
781 
782         write(fileno(ho->outFile), &ph, sizeof(struct pcap_packet_header));
783 
784         if(ho->direction == FROM_SERVER)
785                 write(fileno(ho->outFile), ho->fromEther, sizeof(ho->fromEther)-1);
786         else
787                 write(fileno(ho->outFile), ho->toEther, sizeof(ho->toEther)-1);
788 
789 	craftTcp(goAway, len, ho->direction == FROM_SERVER ? FROM_SERVER : TO_SERVER, TH_ACK|TH_PUSH, ho);
790 
791 
792 	//and now send the ack
793 	ph.usec+=INTERVAL;
794 	ph.length1=packetLen;
795 	ph.length2=packetLen;
796 	write(fileno(ho->outFile), &ph, sizeof(struct pcap_packet_header));
797 
798 	if(ho->direction == FROM_SERVER)
799 		write(fileno(ho->outFile), ho->toEther, sizeof(ho->toEther)-1);
800 	else
801 		write(fileno(ho->outFile), ho->fromEther, sizeof(ho->fromEther)-1);
802 
803 
804 	craftTcp(NULL,0, ho->direction == FROM_SERVER ? TO_SERVER : FROM_SERVER, TH_ACK, ho);    //direction - client to server
805 
806 
807 
808 
809 return 0;
810 }
811 
812 
813 
814