Skip to content

Commit

Permalink
- added : starting TCP protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
first-developer committed Nov 28, 2011
1 parent e59c97c commit fb5db3a
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 16 deletions.
Binary file modified Arrays/libarrays.a
Binary file not shown.
Binary file modified Events/libevents.a
Binary file not shown.
Binary file modified NetTAP/libtap.a
Binary file not shown.
34 changes: 18 additions & 16 deletions Stack/neticmp.c
Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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);

Expand All @@ -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;
}

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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 */

Expand Down
52 changes: 52 additions & 0 deletions Stack/nettcp.c
@@ -0,0 +1,52 @@
/*
* Code for TCP protocol implementation
*/

////
// Include files
////

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>

#include <libarrays.h>
#include <libevents.h>

#include "netether.h"
#include "netip.h"
#include "neticmp.h"
#include "stack.h"





////
// Global variables
////




////
// Prototypes
////




////
// Functions
////



//
// Display IPv4 packet
//




80 changes: 80 additions & 0 deletions 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;


Binary file modified Stack/stack
Binary file not shown.

0 comments on commit fb5db3a

Please sign in to comment.