packetcapture.h

00001 /***************************************************************************
00002  *   Copyright (C) 2001 by Rick L. Vinyard, Jr.                            *
00003  *   rvinyard@cs.nmsu.edu                                                  *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation version 2.1.                *
00008  *                                                                         *
00009  *   This program is distributed in the hope that it will be useful,       *
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00012  *   GNU General Public License for more details.                          *
00013  *                                                                         *
00014  *   You should have received a copy of the GNU Lesser General Public      *
00015  *   License along with this library; if not, write to the                 *
00016  *   Free Software Foundation, Inc.,                                       *
00017  *   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA              *
00018  ***************************************************************************/
00019 #ifndef CONEXUSPACKETCAPTURE_H
00020 #define CONEXUSPACKETCAPTURE_H
00021 
00022 #include <conexus/conexus-config.h>
00023 
00024 #ifdef CONEXUS_HAVE_PCAP_H
00025 
00026 #include <vector>
00027 #include <map>
00028 #include <pcap.h>
00029 
00030 #include <conexus/endpoint.h>
00031 #include <conexus/ipv4_address.h>
00032 
00037 namespace Conexus
00038   {
00039 
00043   class PacketCapture : public Endpoint
00044     {
00045     public:
00046       typedef boost::shared_ptr<PacketCapture> pointer;
00047       
00048       PacketCapture(std::string device=std::string(), int snaplen=65535, bool promiscuous=false, int timeout_ms=0);
00049 
00050       static PacketCapture::pointer create(std::string device=std::string(), int snaplen=65535, bool promiscuous=false, int timeout_ms=0);
00051       
00052       virtual ~PacketCapture();
00053 
00054       virtual void open() throw (open_error);
00055       virtual void close(bool force=false) throw (close_error);
00056 
00057       virtual Data read(size_t s) throw (read_error);
00058 
00059       virtual ssize_t write(CData data);
00060       virtual ssize_t write(const void* data, size_t size, IOMETHOD block=BLOCK) throw (write_error);
00061       virtual ssize_t write(Data data);
00062 
00063       typedef enum Address {ADDRESS, NETMASK, BROADCAST, DESTINATION} Address;
00064 
00065       struct Device
00066         {
00067           Device(): loopback(false)
00068           { }
00069           std::string name;
00070           std::string description;
00071           std::map<unsigned, IPv4::Address> addresses;
00072           bool loopback;
00073         };
00074 
00075       typedef std::vector<Device> Devices;
00076 
00077       static Devices get_all_devices();
00078 
00079       std::string device() const;
00080       void set_device(const std::string& value=std::string());
00081 
00082       int snapshot_length() const;
00083       void set_snapshot_length(const int& value=65535);
00084 
00085       bool promiscuous() const;
00086       void set_promiscuous(bool value=false);
00087 
00088       int timeout_ms() const;
00089       void set_timeout_ms(const int& value=0);
00090 
00091       virtual void stop();
00092       virtual void set_responsiveness(long r);
00093       virtual long responsiveness();
00094 
00095       virtual const std::string& object_type() { static std::string s("Conexus::PacketCapture"); return s; }
00096 
00097     protected:
00098       char m_errbuf[PCAP_ERRBUF_SIZE];
00099       pcap_t* m_pcapd;
00100       std::string m_device;
00101       int m_snapshot_length;
00102       bool m_promiscuous;
00103       int m_timeout_ms;
00104       struct pcap_pkthdr* m_capture_header;
00105       uint8_t* m_capture_data;
00106       long m_responsiveness;
00107 
00108       pthread_mutex_t m_mutex_working;
00109 
00110 
00111       static char m_static_errbuf[PCAP_ERRBUF_SIZE];
00112 
00113       static void pcap_callback(u_char* object, const struct pcap_pkthdr* pkthdr, const u_char* packet);
00114 
00115       virtual void read_thread_main();
00116 
00117     };
00118 
00119 }
00120 
00121 #endif // CONEXUS_HAVE_PCAP_H
00122 
00123 #endif

Generated on Sun Aug 6 12:16:57 2006 by  doxygen 1.4.6