Changeset 110

Show
Ignore:
Timestamp:
06.08.2005 00:14:53 (3 years ago)
Author:
tibob
Message:

- Changed from pcap_next to pcap_next_ex so we are able to know if ter is an error or a time-out. Closes #1
- Added a debug message before we do anything.

Location:
trunk/source
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/source/ipfm.c

    r101 r110  
    5555#include "pcap.h" 
    5656#include "utils.h" 
     57#include "debug.h" 
    5758 
    5859struct AllLogsType * pAllLogs = NULL; 
     
    6364  struct ip *p_packet; 
    6465  struct AllLogsType * pTempLog; 
     66   
     67  DEBUG_MSG("Starting IPFM\n"); 
    6568   
    6669  ParseCmdLine(argc, argv); 
  • trunk/source/pcap.c

    r101 r110  
    247247u_char *getnextippkt() 
    248248{ 
    249   struct pcap_pkthdr useless; 
    250   u_char * packet; 
     249  /* these are allocated by pcap (I think they are saved in pcap_global_descriptor allocated by me...) */ 
     250  struct pcap_pkthdr *header; 
     251  u_char *packet; 
     252   
     253  int pcap_result; 
     254  struct AllLogsType *pTempLog; 
    251255 
    252256  if (NULL == pcap_global_descriptor) { 
     
    255259 
    256260  for (;;) { 
    257     packet = (u_char *) pcap_next(pcap_global_descriptor, &useless); 
    258     DEBUG_MSG("received a packet\n") 
    259     if (NULL == packet) { 
    260       /* if pcap_next returns, there is an error on the link, so we close it and try to reopen it */ 
    261       DEBUG_MSG("pcap_next returned a null pointer [errormessage : %s]\n", pcap_geterr(pcap_global_descriptor)); 
    262       if (Options.ReadFromFile != NULL) { 
     261    pcap_result = pcap_next_ex(pcap_global_descriptor, &header, (const u_char **) &packet); 
     262    DEBUG_MSG("pcap_next_ex returned %i\n", pcap_result); 
     263    switch (pcap_result) { 
     264      case 1: 
     265        /* A packet was received, nothing to do! */ 
     266        DEBUG_MSG("packet time/length/size %i %i %i\n", header->ts, header->caplen, header->len); 
     267        break; 
     268      case 0: 
     269        /* pcap_next_ex reached a time out, check if there is someting to dump and if not, continue */ 
     270        /* FIXME: ungly code duplication here! */ 
     271        /* handle HUP signal */ 
     272        if (SigHup) { 
     273          struct AllLogsType *pTempLog; 
     274          for (pTempLog = pAllLogs; pTempLog; pTempLog = pTempLog->Next) { 
     275            data_dump(pTempLog); 
     276            data_clear(pTempLog); 
     277          } 
     278 
     279          Clean(); 
     280          ReInit(); 
     281          SigHup = 0; 
     282        } 
     283         
     284        /* handle USR1 signal */ 
     285        if (SigDump) { 
     286          for (pTempLog = pAllLogs; pTempLog; pTempLog = pTempLog->Next) { 
     287            data_dump(pTempLog); 
     288          } 
     289          SigDump = 0; 
     290        } 
     291         
     292        continue; 
     293        break; 
     294       
     295      case -2: 
    263296        /* we reached the end of the file ; dump and exit. */ 
    264         struct AllLogsType *pTempLog; 
    265  
    266297        for (pTempLog = pAllLogs; 
    267298             NULL != pTempLog; 
     
    271302        } 
    272303        Exit(0); 
    273       } 
    274       closepcap(); 
    275       sleep(2); 
    276       while (!openpcap(device, promisc)) { 
    277         sleep(2); 
    278       } 
    279       continue; 
     304        break; 
     305       
     306      default: 
     307        /* If pcap_next returns with -1, there is an error on the link, so we close it and try to reopen it 
     308           Other return codes should not exist, but as I don't know what could hapen, we'll handel them like a -1 return code. 
     309        */ 
     310        DEBUG_MSG("pcap_next_ex error [errormessage : %s]\n", pcap_geterr(pcap_global_descriptor)); 
     311       
     312        closepcap(); 
     313        sleep(2); 
     314        while (!openpcap(device, promisc)) { 
     315          sleep(2); 
     316        } 
     317        continue; 
     318        break; 
     319 
     320       
    280321    } 
    281322    packet = packet + offset; 
     
    291332 
    292333    /* handle HUP signal */ 
    293  
    294334    if (SigHup) { 
    295335      struct AllLogsType *pTempLog;