@@ -214,10 +214,15 @@ void fillTcpHeader(struct TCPHeader *tcpheader,unsigned char *destmac,unsigned s
214
214
tcpheader -> checksum = 0 ;
215
215
tcpheader -> urgent_pointer = 0 ;
216
216
217
- struct tcp_checksum_header * trx = (struct tcp_checksum_header * )malloc (sizeof (struct tcp_checksum_header ));
217
+ int payload = ( size - (sizeof (struct TCPHeader ) - sizeof (struct EthernetHeader )) );
218
+ unsigned char * start = (unsigned char * )malloc (sizeof (struct tcp_checksum_header ) + payload );
219
+ unsigned char * end = start ;
220
+ end += (sizeof (struct tcp_checksum_header ) + payload );
221
+
222
+ struct tcp_checksum_header * trx = (struct tcp_checksum_header * )start ;
218
223
trx -> dst = (to );
219
224
trx -> src = (from );
220
- trx -> len = switch_endian16 (20 );
225
+ trx -> len = switch_endian16 (20 + payload );
221
226
trx -> protocol = IPV4_TYPE_TCP ;
222
227
trx -> source_port = switch_endian16 (from_port );
223
228
trx -> destination_port = switch_endian16 (to_port );
@@ -228,7 +233,7 @@ void fillTcpHeader(struct TCPHeader *tcpheader,unsigned char *destmac,unsigned s
228
233
trx -> checksum = 0 ;
229
234
trx -> urgent_pointer = 0 ;
230
235
231
- tcpheader -> checksum = switch_endian16 (net_checksum (( const unsigned char * ) trx ,( const unsigned char * )( trx + sizeof ( struct tcp_checksum_header )) ));
236
+ tcpheader -> checksum = switch_endian16 (net_checksum (start , end ));
232
237
}
233
238
234
239
void fillDhcpDiscoverHeader (struct DHCPDISCOVERHeader * dhcpheader ){
@@ -442,28 +447,31 @@ unsigned char* getIPFromName(char* name){
442
447
return targetip ;
443
448
}
444
449
445
- void create_tcp_session (unsigned long from , unsigned long to , unsigned short from_port , unsigned short to_port ){
450
+ unsigned long ethjmplist [20000 ];
451
+
452
+ void setTcpHandler (unsigned short port ,unsigned long func ){
453
+ ethjmplist [port ] = func ;
454
+ }
455
+
456
+ void create_tcp_session (unsigned long from , unsigned long to , unsigned short from_port , unsigned short to_port , unsigned long func ){
446
457
unsigned long sizetype = sizeof (struct TCPHeader );
447
458
struct TCPHeader * tcp1 = (struct TCPHeader * ) malloc (sizetype );
448
459
unsigned char * destmac ;
449
460
unsigned char * t4 = (unsigned char * )& to ;
450
- if (t4 [0 ]== 192 ){
451
- destmac = getMACFromIp (t4 );
452
- }else {
453
- destmac = (unsigned char * )& router_ip ;
454
- }
461
+
462
+ destmac = getMACFromIp (t4 );
455
463
unsigned short size = sizeof (struct TCPHeader ) - sizeof (struct EthernetHeader );
456
464
fillTcpHeader (tcp1 ,destmac ,size ,from ,to ,from_port ,to_port ,1 ,0 ,5 ,TCP_SYN ,512 );
457
465
466
+ setTcpHandler (to_port ,func );
467
+
458
468
PackageRecievedDescriptor sec ;
459
469
sec .buffersize = sizetype ;
460
470
sec .high_buf = 0 ;
461
471
sec .low_buf = (unsigned long )tcp1 ;
462
472
sendEthernetPackage (sec );
463
473
}
464
474
465
- unsigned long ethjmplist [20000 ];
466
-
467
475
int ethernet_handle_package (PackageRecievedDescriptor desc ){
468
476
struct EthernetHeader * eh = (struct EthernetHeader * ) desc .low_buf ;
469
477
if (eh -> type == ETHERNET_TYPE_ARP ){
@@ -544,7 +552,7 @@ int ethernet_handle_package(PackageRecievedDescriptor desc){
544
552
unsigned long addr = desc .low_buf + sizeof (struct TCPHeader );
545
553
unsigned long count = desc .buffersize - sizeof (struct TCPHeader );
546
554
unsigned long func = ethjmplist [switch_endian16 (tcp -> destination_port )];
547
- // debugf("[ETH] TCP message reieved: size=%x string=%s \n",count,(unsigned char*)addr);
555
+ debugf ("[ETH] TCP message reieved: size=%x string=%s \n" ,count ,(unsigned char * )addr );
548
556
if (func ){
549
557
debugf ("[ETH] function handler is about to get called\n" );
550
558
int (* sendPackage )(unsigned long a ,unsigned long b ) = (void * )func ;
@@ -555,6 +563,22 @@ int ethernet_handle_package(PackageRecievedDescriptor desc){
555
563
}
556
564
}
557
565
return 1 ;
566
+ }else if (ip -> protocol == IPV4_TYPE_ICMP ){
567
+ struct ICMPHeader * icmp = (struct ICMPHeader * ) ip ;
568
+ if (icmp -> type == 8 ){
569
+ debugf ("[ETH] ICMP ping request found!\n" );
570
+
571
+ // struct ICMPHeader *newicmp = (struct ICMPHeader*) malloc(sizeof(struct ICMPHeader));
572
+
573
+ // fillIpv4Header((struct IPv4Header*)&newicmp->ipv4header,(unsigned char*)icmp->ipv4header.ethernetheader.from,size,IPV4_TYPE_ICMP,from,to);
574
+
575
+ // PackageRecievedDescriptor sec;
576
+ // sec.buffersize = sizeof(struct ICMPHeader);
577
+ // sec.high_buf = 0;
578
+ // sec.low_buf = (unsigned long)newicmp;
579
+ // sendEthernetPackage(sec);
580
+ return 1 ;
581
+ }
558
582
}
559
583
}
560
584
return 0 ;
@@ -565,6 +589,7 @@ unsigned long getOurIpAsLong(){
565
589
}
566
590
567
591
void exsend (unsigned long addr ,unsigned long count ){
592
+ printf ("Recieved message: " );
568
593
for (unsigned long i = 0 ; i < count ; i ++ ){
569
594
printf ("%c" ,((unsigned char * )addr )[i ]);
570
595
}
@@ -598,15 +623,6 @@ void initialise_ethernet(){
598
623
debugf ("[ETH] DNS IP is %d.%d.%d.%d \n" ,dns_ip [0 ],dns_ip [1 ],dns_ip [2 ],dns_ip [3 ]);
599
624
debugf ("[ETH] DHCP IP is %d.%d.%d.%d \n" ,dhcp_ip [0 ],dhcp_ip [1 ],dhcp_ip [2 ],dhcp_ip [3 ]);
600
625
601
- // 192.168.2.68
602
- unsigned char xxx [SIZE_OF_IP ];
603
- xxx [0 ] = 192 ;
604
- xxx [1 ] = 168 ;
605
- xxx [2 ] = 2 ;
606
- xxx [3 ] = 68 ;
607
- ethjmplist [19696 ] = (unsigned long )& exsend ;
608
- create_tcp_session (getOurIpAsLong (), ((unsigned long * )& xxx )[0 ], 19696 , 19696 );printf ("verzonden\n" );for (;;);
609
-
610
626
unsigned char * srve = getIPFromName ("tftp.local" );
611
627
if (srve [0 ]){
612
628
printf ("[ETH] TFTP IP is %d.%d.%d.%d \n" ,srve [0 ],srve [1 ],srve [2 ],srve [3 ]);
0 commit comments