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       Trace(const Trace&);
00180 
00182       Trace&                    operator = (const Trace&);
00183 
00185       PacketIterator            begin();
00186 
00188       PacketIterator            operator [] (size_t);
00189 
00191       PacketIterator            end();
00192 
00194       size_t                    size() const;
00195 
00197       link_type                 linktype() const;
00198 
00200       std::streampos            file_size() const;
00201 
00203       const std::string&        file_name() const;
00204 
00205     protected:
00206 
00207       enum
00208         {
00219           mark_step     = 512,
00220 
00230           junk_len      = 4096
00231         };
00232 
00234       struct pcap_file_header
00235       {
00236         uint32_t        magic;          
00237         uint16_t        version_major;  
00238         uint16_t        version_minor;  
00239         int32_t         thiszone;       
00240         int32_t         sigfigs;        
00241         int32_t         snaplen;        
00242         int32_t         linktype;       
00243       };
00244 
00246       struct pcap_packet_header
00247       {
00248         struct timeval  ts;     
00249         uint32_t        caplen; 
00250         uint32_t        len;    
00251       };
00252 
00253     private:
00254       template <typename ProgressInfoSetupType,
00255                 typename ProgressInfoUpdateType>
00256       void
00257       setup(const std::string&                  filename,
00258             ProgressInfoSetupType&              pi_setup,
00259             ProgressInfoUpdateType&             pi_update);
00260 
00261       void              warn(const std::streampos&p, const std::string& msg);
00262       void              handle_truncation(const std::string& msg);
00263 
00264       template <typename ProgressInfoSetupType,
00265                 typename ProgressInfoUpdateType>
00266       void
00267       setup_marks(ProgressInfoSetupType&        pi_setup,
00268                   ProgressInfoUpdateType&       pi_update);
00269 
00270       std::ifstream                     file_;
00271       std::vector<std::streampos>       marks_;
00272 
00273       bool                              swap_;
00274       int32_t                           zone_;
00275       link_type                         type_;
00276       size_t                            pkt_count_;
00277       std::streampos                    file_size_;
00278       std::string                       file_name_;
00279     };
00280 
00281   } // End of namespace pcap.
00282 
00283 } // End of namespace wscout.
00284 
00285 # include "wscout_pcap_trace.hxx"
00286 
00287 #endif // ! WSCOUT_PCAP_TRACE_HH_

Generated on Mon Sep 24 18:13:52 2007 for WScout by  doxygen 1.5.3