packets/packet_tcpdump.c

Go to the documentation of this file.
00001 #include "packet_tcpdump.h"
00002 
00003 #include <stdio.h>
00004 #include <fcntl.h>
00005 #include <unistd.h>
00006 #include <netinet/in.h>
00007 #include <arpa/inet.h>
00008 #include <stdlib.h>
00009 
00010 /*struct out of libpcap*/
00011 struct dump_pcap_pkthdr {
00012         struct                  timeval ts;     /* time stamp */
00013         unsigned int    caplen; /* length of portion present */
00014         unsigned int    len;    /* length this packet (off wire) */
00015 };
00016 
00017 #define DUMP_PCAP_VERSION_MAJOR 2
00018 #define DUMP_PCAP_VERSION_MINOR 4
00019 
00020 /*struct out of libpcap*/
00021 struct dump_pcap_file_header {
00022         unsigned int    magic;
00023         unsigned short  version_major;
00024         unsigned short  version_minor;
00025         int                             thiszone;       /* gmt to local correction */
00026         unsigned int    sigfigs;        /* accuracy of timestamps */
00027         unsigned int    snaplen;        /* max length saved portion of each pkt */
00028         unsigned int    linktype;       /* data link type (LINKTYPE_*) */
00029 };
00030 
00031 
00032 //#define DEBUG
00033 
00034 extern GlobalVars       Globals;
00035 
00036 /*********************************************
00037 * Open an interface to read from a tcpdump file
00038 **********************************************/
00039 int OpenInterfaceTCPDump(int InterfaceID)
00040 {
00041         int                                             fd;
00042         InterfaceRec*                   Interface;
00043         struct dump_pcap_file_header    Header;
00044 
00045 #ifdef DEBUGPATH
00046         printf("In OpenInterfaceTCPDump\n");
00047 #endif
00048 
00049 
00050         Interface=&Globals.Interfaces[InterfaceID];
00051 
00052 #ifdef DEBUG
00053         printf("Opening tcpdump file %s\n", Interface->Name);
00054 #endif
00055 
00056         fd=open(Interface->Name, O_RDONLY);
00057         if (fd==-1){
00058                 printf("Failed to open \"%s\" for reading\n",Interface->Name);
00059                 return FALSE;
00060         }
00061 
00062         if (read(fd, &Header, sizeof(Header))!= sizeof(Header)){
00063                 printf("Failed to read TCPDump Header from \"%s\"\n",Interface->Name);
00064                 return FALSE;
00065         }
00066         
00067         if (Header.magic != htonl(0xd4c3b2a1)){
00068                 printf("Header magic number didn't match. Not a TCPDUMP file?\n");
00069                 return FALSE;
00070         }       
00071 
00072         Interface->FD=fd;
00073         Interface->MTU=Header.snaplen;
00074         Interface->IsPollable=TRUE;
00075         
00076         return TRUE;
00077 }
00078 
00079 /**********************************************
00080 * Read a packet off of a tcpdump file
00081 **********************************************/
00082 int ReadPacketTCPDump(int InterfaceID){
00083         int                     count;
00084         InterfaceRec*   Interface;
00085         int                             PacketSlot;
00086         PacketRec*              p;
00087         struct dump_pcap_pkthdr Header;
00088         
00089 #ifdef DEBUGPATH
00090         printf("In ReadPacketTCPDump\n");
00091 #endif
00092 
00093 #ifdef DEBUG
00094         printf("Reading packet from tcpdump file\n");
00095 #endif
00096 
00097         Interface=&Globals.Interfaces[InterfaceID];
00098         if ( (PacketSlot=GetEmptyPacket())==-1){
00099                 printf("Unable to allocate packet for reading\n");
00100                 return FALSE;           
00101         }       
00102         
00103         p=&Globals.Packets[PacketSlot];
00104         
00105         p->InterfaceNum=InterfaceID;
00106 
00107         /*read in the packet header*/
00108         count = read(Interface->FD, (char*)&Header, sizeof(Header));
00109         if (count!=sizeof(Header)){
00110 #ifdef DEBUG    
00111                 printf("Failed to read packet header\n");
00112 #endif          
00113                 ReturnEmptyPacket(PacketSlot);
00114                 /*exit when you get to the end of a tcpdump file*/
00115                 Globals.Done=TRUE;
00116         }
00117 
00118         /*read in the packet*/
00119         count = read(Interface->FD, (char*)p->RawPacket, Header.caplen);
00120         if (count==-1){
00121 #ifdef DEBUG    
00122                 printf("Failed to read packet.\n");
00123 #endif          
00124                 ReturnEmptyPacket(PacketSlot);
00125                 /*exit when you get to the end of a tcpdump file*/
00126                 exit(0);
00127         }
00128         p->PacketLen=count;
00129         p->tv=Header.ts;        
00130 
00131         if (!AddPacketToPending(PacketSlot)){
00132                 printf("Coulnd't add packet to pending queue\n");
00133                 ReturnEmptyPacket(PacketSlot);
00134                 return FALSE;
00135         }
00136 
00137         return TRUE;
00138 }
00139 
00140 /***************************************************
00141 * Send a packet off to the raw interface
00142 ****************************************************/
00143 int WritePacketTCPDump(int InterfaceID, unsigned char* Packet, int PacketLen){  
00144 #ifdef DEBUGPATH
00145         printf("In WritePacketTCPDumpRaw\n");
00146 #endif
00147         
00148         return FALSE;
00149 }
00150 
00151 /**********************************************
00152 * The thread func
00153 **********************************************/
00154 void* TCPDumpLoopFunc(void* v){
00155         int                             InterfaceID;
00156 
00157 #ifdef DEBUGPATH
00158         printf("In TCPDumpRawLoopFunc\n");
00159 #endif
00160 
00161         InterfaceID=(int)v;
00162         while (!Globals.Done){
00163                 ReadPacketTCPDump(InterfaceID);
00164         }
00165         
00166         return NULL;
00167 }
00168 
00169 /**********************************************
00170 * Start a thread to continuously read
00171 **********************************************/
00172 int LoopThreadTCPDump(int InterfaceID){
00173 #ifdef DEBUGPATH
00174         printf("In loopThreadTCPDumpRaw\n");
00175 #endif
00176 
00177 #ifndef HAS_THREADS
00178         return FALSE;
00179 #else
00180 
00181 #ifdef DEBUG
00182         printf("Starting Thread for interface %s\n",Globals.Interfaces[InterfaceID].Name);
00183 #endif
00184 
00185         Globals.Interfaces[InterfaceID].ThreadID=pthread_create(
00186                 &Globals.Interfaces[InterfaceID].Thread,
00187                 NULL,
00188                 TCPDumpLoopFunc,
00189                 (void*)InterfaceID
00190         );
00191         
00192         return (!Globals.Interfaces[InterfaceID].ThreadID);
00193 #endif
00194         
00195 }
00196 

Generated on Sat Jul 7 23:33:10 2007 for HLBR by  doxygen 1.5.2