00001 #include "decode_tcp.h" 00002 #include "decode_ip.h" 00003 #include "../packets/packet.h" 00004 #include "../engine/hlbrlib.h" 00005 #include "../engine/session.h" 00006 #include <stdio.h> 00007 #include <stdlib.h> 00008 #include <netinet/in.h> 00009 00010 //#define DEBUG 00011 00012 extern GlobalVars Globals; 00013 00014 int IPDecoderID; 00015 00019 void* DecodeTCP(int PacketSlot) 00020 { 00021 TCPData* data; 00022 IPData* ip_data; 00023 unsigned char ip_proto; 00024 PacketRec* p; 00025 00026 DEBUGPATH; 00027 00028 p = &Globals.Packets[PacketSlot]; 00029 00030 if (!GetDataByID(PacketSlot, IPDecoderID, (void**)&ip_data)) { 00031 PRINTERROR("Failed to get IP header data\n"); 00032 return NULL; 00033 } 00034 00035 ip_proto = ip_data->Header->protocol; 00036 00037 if (ip_proto != IP_PROTO_TCP) { 00038 DBG( PRINTERROR1("IP doesn't think this is a tcp packet %02x\n",ip_proto) ); 00039 return NULL; 00040 } 00041 00042 data = malloc(sizeof(TCPData)); 00043 data->Header = (TCPHdr*)(p->RawPacket+p->BeginData); 00044 p->BeginData += (data->Header->doff*4); 00045 data->Data = (unsigned char*)(p->RawPacket+p->BeginData); 00046 data->DataLen = p->PacketLen-((int)(data->Data)-(int)(p->RawPacket)); 00047 00048 DBG( PRINTERROR3("In PacketSlot %i TCP %u->%u\n",PacketSlot, ntohs(data->Header->source), ntohs(data->Header->dest)) ); 00049 00050 // Assigns a session structure (PortPair) for this packet 00051 // this will enable the stream tests, in further decoders. 00052 AssignSessionTCP(PacketSlot, (void*)data); 00053 00054 return data; 00055 } 00056 00060 int InitDecoderTCP() 00061 { 00062 int DecoderID; 00063 00064 DEBUGPATH; 00065 00066 if ((DecoderID = CreateDecoder("TCP")) == DECODER_NONE) { 00067 DBG( PRINTERROR("Couldn't Allocate TCP Decoder\n") ); 00068 return FALSE; 00069 } 00070 00071 Globals.Decoders[DecoderID].DecodeFunc = DecodeTCP; 00072 if (!DecoderAddDecoder(GetDecoderByName("IP"), DecoderID)) { 00073 PRINTERROR("Failed to Bind TCP Decoder to IPDefrag Decoder\n"); 00074 return FALSE; 00075 } 00076 00077 IPDecoderID = GetDecoderByName("IP"); 00078 00079 return TRUE; 00080 }