diff --git a/Arrays/libarrays.a b/Arrays/libarrays.a index 079bb03..d63d945 100644 Binary files a/Arrays/libarrays.a and b/Arrays/libarrays.a differ diff --git a/Events/libevents.a b/Events/libevents.a index 9bf79f2..65625c1 100644 Binary files a/Events/libevents.a and b/Events/libevents.a differ diff --git a/NetTAP/libtap.a b/NetTAP/libtap.a index 5fdf7a5..1dc8ff4 100644 Binary files a/NetTAP/libtap.a and b/NetTAP/libtap.a differ diff --git a/Stack/neticmp.c b/Stack/neticmp.c index 69d5de4..01bef11 100644 --- a/Stack/neticmp.c +++ b/Stack/neticmp.c @@ -116,15 +116,14 @@ unsigned char icmpDecodePacket(EventsEvent *event,EventsSelector *selector){ unsigned char code= ICMPV4_CODE_NONE; // Compute the reply size of packet - int size_iph=sizeof(IPv4_fields) -1; // ip header size - int reply_size=size + size_iph; + int size_iph=sizeof(ICMPv4_fields) -1; // ip header size + int reply_size=size - size_iph; // Make packet fit the right size + memmove(data,data + size_iph,reply_size); data=(unsigned char *)realloc(data,reply_size); if(data==NULL){ perror("ipDecodePacket.realloc"); return 1; } - memmove(data + size_iph,data,size); - bzero(data,size_iph); // set the other fields to zero - + // Initialized and set the icmp packet to replay AssocArray *icmp_infos=NULL; arraysSetValue(&icmp_infos,"type",&type,sizeof(unsigned char),0); @@ -134,7 +133,7 @@ unsigned char icmpDecodePacket(EventsEvent *event,EventsSelector *selector){ arraysSetValue(&icmp_infos,"ldst",&rev_source,sizeof(IPv4Address),0); eventsTrigger(picmp->event_out,icmp_infos); } - free(data); return 0; + else free(data); break; case ICMPV4_TYPE_UNREACHABLE: if ( icmp->code == ICMPV4_UNREACHABLE_CODE_PORT ) { // port introuvable @@ -149,8 +148,9 @@ unsigned char icmpDecodePacket(EventsEvent *event,EventsSelector *selector){ // Get Source port from icmp_udp fields unsigned short int psource = ntohs(icmp_udp->source); - int psource_net= psource; - + + int psource_net= icmp_udp->source; + // Get he process linked to the psource getting previously StackProcess *process=stackFindProcess(IPV4_PROTOCOL_UDP,iph->source,psource); @@ -170,11 +170,11 @@ unsigned char icmpDecodePacket(EventsEvent *event,EventsSelector *selector){ arraysSetValue(&infos,"size",&size_data,sizeof(int),0); eventsTrigger(process->event,infos); } - free(data); return 0; + else free(data); break; default: break; } - + free(iph); return 0; } @@ -196,8 +196,8 @@ unsigned char icmpSendPacket(EventsEvent *event,EventsSelector *selector){ { arraysFreeArray(infos); return 1; } // Get the icmp layer -StackLayers *pip=stackFindProtoById(LEVEL_NETWORK,ETHERNET_PROTO_IP); -if(pip==NULL || pip->event_out<0){ arraysFreeArray(infos); return 0; } + StackLayers *pip=stackFindProtoById(LEVEL_NETWORK,ETHERNET_PROTO_IP); + if(pip==NULL || pip->event_out<0){ arraysFreeArray(infos); return 0; } // Get ICMP attributes: type, code, data, ldst unsigned char type = *(unsigned char *)arraysGetValue(infos,"type",NULL,0); //type @@ -206,11 +206,13 @@ if(pip==NULL || pip->event_out<0){ arraysFreeArray(infos); return 0; } unsigned char *data = (unsigned char *)arraysGetValue(infos,"data",NULL,0); //data IPv4Address target = *((IPv4Address *)arraysGetValue(infos,"ldst", NULL, 0)); //ldst - IPv4Address source = IPV4_ADDRESS_NULL; // set target adresse to NULL address - // free infos datas arraysFreeArray(infos); + IPv4Address source = IPV4_ADDRESS_NULL; // set target adresse to NULL address + EthernetInterface *device=stackFindDeviceByIPv4Network(target); + if(device!=NULL) source=device->IPv4[0].address; + // Fill ICMP Header // ----------------------------------------------------------------------- // Compute size_hicmp and size_icmp @@ -221,7 +223,7 @@ if(pip==NULL || pip->event_out<0){ arraysFreeArray(infos); return 0; } data=(unsigned char *) realloc(data, size_icmp); if (data == NULL) { perror("icmpSendPacket.realloc"); return 1; } - // put icmp_ header before icmp data + // put icmp_header before icmp data memmove(data+size_hicmp,data,data_size); bzero(data,size_hicmp); // fill icmp header with zero @@ -236,7 +238,7 @@ if(pip==NULL || pip->event_out<0){ arraysFreeArray(infos); return 0; } // icmp->checksum = 0; unsigned short int checksum=genericChecksum(data,size_icmp); icmp = (ICMPv4_fields *)data; - icmp->checksum = checksum; + icmp->checksum = htons(checksum); /* TODO: verbose for debugging icmp */ diff --git a/Stack/nettcp.c b/Stack/nettcp.c new file mode 100644 index 0000000..d3eec63 --- /dev/null +++ b/Stack/nettcp.c @@ -0,0 +1,52 @@ +/* + * Code for TCP protocol implementation + */ + +//// +// Include files +//// + +#include +#include +#include +#include + +#include +#include + +#include "netether.h" +#include "netip.h" +#include "neticmp.h" +#include "stack.h" + + + + + +//// +// Global variables +//// + + + + +//// +// Prototypes +//// + + + + +//// +// Functions +//// + + + +// +// Display IPv4 packet +// + + + + diff --git a/Stack/nettcp.h b/Stack/nettcp.h new file mode 100644 index 0000000..79567c6 --- /dev/null +++ b/Stack/nettcp.h @@ -0,0 +1,80 @@ +/* + * Definitions for IP protocol implementation + */ + +//// +// Constants +//// + +#define IPV4_ADDRESS_SIZE 4 +#define IPV4_STRING_MAX 16 + +#define IPV4_VERSION 0x04 +#define IPV4_DEFAULT_TTL 0x30 + +#define IPV4_PROTOCOL_RAW 0x00 +#define IPV4_PROTOCOL_ICMP 0x01 +#define IPV4_PROTOCOL_TCP 0x06 +#define IPV4_PROTOCOL_UDP 0x11 + +#define IPV4_RETRANS_MAX 5 +#define IPV4_RETRANS_WAIT_TIME 500000 + +//// +// Structures +//// + +#pragma pack(1) + +typedef struct{ + unsigned short int source; + unsigned short int target; + uint32_t seq_num; + uint32_t ack_num; + unsigned short int offset; + unsigned short int flags; // flag with the reserved part + unsigned char int window; + unsigned short int checksum; + unsigned short int urgent_pointer; + unsigned char diffserv; + unsigned short int length; + unsigned short int id; + unsigned short int mixed2; + unsigned char ttl; + + unsigned short int checksum; + unsigned char protocol; + unsigned short int checksum; + IPv4Address source; + IPv4Address target; + unsigned char options[1]; + } TCP_fields; + +#define IPv4_get_version(ip) (((ip)->mixed1&0xf0)>>4) +#define IPv4_get_hlength(ip) ((ip)->mixed1&0x0f) +#define IPv4_get_flags(ip) ((ntohs((ip)->mixed2)&0xe000)>>13) +#define IPv4_get_offset(ip) (ntohs((ip)->mixed2)&0x0fff) + +#define IPv4_set_version(ip,v) (ip)->mixed1=((v)<<4)|((ip)->mixed1&0x0f) +#define IPv4_set_hlength(ip,l) (ip)->mixed1=((ip)->mixed1&0xf0)|((l)&0x0f) +#define IPv4_set_flags(ip,f) (ip)->mixed2=htons( \ + ((f&07)<<13)|(ntohs((ip)->mixed2)&0x0fff)) +#define IPv4_set_offset(ip,o) (ip)->mixed2=htons( \ + (ntohs((ip)->mixed2)&0xe000)|o&0x0fff) + + +typedef struct{ + unsigned char code; + unsigned char length; + unsigned char data[1]; + } IPv4_option_fields; + +typedef struct{ + IPv4Address source; + IPv4Address target; + unsigned char zero; + unsigned char protocol; + unsigned short int length; + } IPv4_pseudo_header; + + diff --git a/Stack/stack b/Stack/stack index 28cad52..38da153 100755 Binary files a/Stack/stack and b/Stack/stack differ