src/wscout_pcap_trace.hh

00001 /*
00002  * WScout - Lightweight PCAP visualizer.
00003  * Copyright (C) 2007  Universite Pierre et Marie Curie - Paris 6
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00018  * MA  02110-1301  USA
00019  *
00020  * Author: Thomas Claveirole <thomas.claveirole@lip6.fr>
00021  */
00022 
00023 #ifndef WSCOUT_PCAP_TRACE_HH_
00024 # define WSCOUT_PCAP_TRACE_HH_
00025 
00026 extern "C"
00027 {
00028 # include <sys/types.h>
00029 # include <sys/times.h>
00030 # include <inttypes.h>
00031 }
00032 
00033 # include <fstream>
00034 # include <cstdlib>
00035 # include <iterator>
00036 # include <vector>
00037 # include <stdexcept>
00038 
00039 # include <boost/optional.hpp>
00040 # include <boost/shared_ptr.hpp>
00041 
00042 namespace wscout
00043 {
00044 
00046   namespace pcap
00047   {
00048 
00050     struct Trace
00051     {
00052 
00054       struct PacketIterator
00055       {
00056 
00058         struct Packet
00059         {
00060           struct timeval                ts;     
00061           uint32_t                      caplen; 
00062           uint32_t                      len;    
00063           boost::shared_ptr<uint8_t>    data;   
00064 
00066           Packet(std::ifstream& fstream, bool swap, int32_t zone);
00067 
00069 
00070           bool operator == (const Packet& rhs) const;
00071           bool operator != (const Packet& lhs) const;
00073 
00074         }; // End of struct Packet.
00075 
00077 
00078         typedef ssize_t                 difference_type;
00079         typedef Packet                  value_type;
00080         typedef value_type*             pointer_type;
00081         typedef value_type&             reference_type;
00082         typedef std::input_iterator_tag iterator_category;
00084 
00091         PacketIterator(Trace& t, bool end_of_trace = true);
00092 
00094 
00095         bool            operator == (const PacketIterator& rhs) const;
00096         bool            operator != (const PacketIterator& rhs) const;
00098 
00100 
00101         const Packet&   operator * () const;
00102         const Packet*   operator -> () const;
00104 
00106 
00107         PacketIterator& operator ++ ();
00108         PacketIterator  operator ++ (int);
00110 
00111       private:
00112         typedef boost::optional<value_type>     optional_value;
00113 
00114         optional_value  v_;     
00115         Trace*          t_;     
00116       };
00117 
00119       struct invalid_operation: public std::runtime_error
00120       {
00122         invalid_operation(const std::string&);
00123       };
00124 
00126       struct interrupted: public std::runtime_error
00127       {
00129         interrupted(const std::string&);
00130       };
00131 
00133       typedef PacketIterator    iterator;
00134 
00136       enum link_type
00137         {
00138           LOOP          = 0,    
00139           EN10MB        = 1,    
00140           EN3MB         = 2,    
00141           AX25          = 3,    
00142           PRONET        = 4,    
00143           CHAOS         = 5,    
00144           IEEE802       = 6,    
00145           ARCNET        = 7,    
00146           SLIP          = 8,    
00147           PPP           = 9,    
00148           FDDI          = 10,   
00149           IEEE802_11    = 105,  
00150           PRISM_HEADER  = 119   
00151         };
00152 
00154       Trace(const std::string&);
00155 
00172       template <typename ProgressInfoSetupType,
00173                 typename ProgressInfoUpdateType>
00174       Trace(const std::string&          filename,
00175             ProgressInfoSetupType&      pi_setup,
00176             ProgressInfoUpdateType&     pi_update);
00177 
00179       PacketIterator            operator [] (size_t);
00180 
00182       PacketIterator            end_of_trace();
00183 
00185       size_t                    size() const;
00186 
00188       link_type                 linktype() const;
00189 
00191       std::streampos            file_size() const;
00192 
00194       const std::string&        file_name() const;
00195 
00196     protected:
00197 
00198       enum
00199         {
00210           mark_step     = 512,
00211 
00221           junk_len      = 4096
00222         };
00223 
00225       struct pcap_file_header
00226       {
00227         uint32_t        magic;          
00228         uint16_t        version_major;  
00229         uint16_t        version_minor;  
00230         int32_t         thiszone;       
00231         int32_t         sigfigs;        
00232         int32_t         snaplen;        
00233         int32_t         linktype;       
00234       };
00235 
00237       struct pcap_packet_header
00238       {
00239         struct timeval  ts;     
00240         uint32_t        caplen; 
00241         uint32_t        len;    
00242       };
00243 
00244     private:
00245       template <typename ProgressInfoSetupType,
00246                 typename ProgressInfoUpdateType>
00247       void
00248       setup(const std::string&                  filename,
00249             ProgressInfoSetupType&              pi_setup,
00250             ProgressInfoUpdateType&             pi_update);
00251 
00252       void              warn(const std::streampos&p, const std::string& msg);
00253       void              handle_truncation(const std::string& msg);
00254 
00255       template <typename ProgressInfoSetupType,
00256                 typename ProgressInfoUpdateType>
00257       void
00258       setup_marks(ProgressInfoSetupType&        pi_setup,
00259                   ProgressInfoUpdateType&       pi_update);
00260 
00261       std::ifstream                     file_;
00262       std::vector<std::streampos>       marks_;
00263 
00264       bool                              swap_;
00265       int32_t                           zone_;
00266       link_type                         type_;
00267       size_t                            pkt_count_;
00268       std::streampos                    file_size_;
00269       std::string                       file_name_;
00270     };
00271 
00272   } // End of namespace pcap.
00273 
00274 } // End of namespace wscout.
00275 
00276 # include "wscout_pcap_trace.hxx"
00277 
00278 #endif // ! WSCOUT_PCAP_TRACE_HH_

Generated on Wed Sep 12 16:02:49 2007 for WScout by  doxygen 1.5.3