00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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