00001 #include "decode_ip.h" 00002 #include "decode_ethernet.h" 00003 #include "../packets/packet.h" 00004 #include <stdio.h> 00005 #include <stdlib.h> 00006 #include <netinet/in.h> 00007 #include <arpa/inet.h> 00008 00009 //#define DEBUG 00010 00011 extern GlobalVars Globals; 00012 00013 int EthernetDecoderID; 00014 00015 /*************************************** 00016 * Apply the ip decoding 00017 ****************************************/ 00018 void* DecodeIP(int PacketSlot){ 00019 IPData* data; 00020 EthernetData* edata; 00021 unsigned short etype; 00022 PacketRec* p; 00023 00024 DEBUGPATH; 00025 00026 #ifdef DEBUG 00027 printf("Decoding IP Header\n"); 00028 #endif 00029 00030 p=&Globals.Packets[PacketSlot]; 00031 00032 if (!GetDataByID(PacketSlot, EthernetDecoderID, (void**)&edata)){ 00033 printf("2Failed to get Ethernet header data\n"); 00034 return NULL; 00035 } 00036 00037 etype=ntohs(edata->Header->Type); 00038 00039 if (etype!=ETHERNET_TYPE_IP){ 00040 #ifdef DEBUG 00041 printf("Ethernet doesn't think this is an IP packet %04x\n",etype); 00042 #endif 00043 return NULL; 00044 } 00045 00046 data=malloc(sizeof(IPData)); 00047 data->Header=(IPHdr*)(p->RawPacket+p->BeginData); 00048 p->BeginData+=(data->Header->ihl*4); 00049 00050 #ifdef DEBUG 00051 printf("IP %s->",inet_ntoa(*(struct in_addr*)&data->Header->saddr)); 00052 printf("%s\n",inet_ntoa(*(struct in_addr*)&data->Header->daddr)); 00053 #endif 00054 00055 return data; 00056 } 00057 00058 /************************************* 00059 * Set up the decoder 00060 *************************************/ 00061 int InitDecoderIP(){ 00062 int DecoderID; 00063 00064 DEBUGPATH; 00065 00066 if ((DecoderID=CreateDecoder("IP"))==DECODER_NONE){ 00067 #ifdef DEBUG 00068 printf("Couldn't Allocate IP Decoder\n"); 00069 #endif 00070 return FALSE; 00071 } 00072 00073 Globals.Decoders[DecoderID].DecodeFunc=DecodeIP; 00074 if (!DecoderAddDecoder(GetDecoderByName("Ethernet"), DecoderID)){ 00075 printf("Failed to Bind IP Decoder to Ethernet Decoder\n"); 00076 return FALSE; 00077 } 00078 00079 EthernetDecoderID=GetDecoderByName("Ethernet"); 00080 00081 return TRUE; 00082 }