diff --git a/include/tins/sniffer.h b/include/tins/sniffer.h index cd0383b..942cd71 100644 --- a/include/tins/sniffer.h +++ b/include/tins/sniffer.h @@ -299,13 +299,18 @@ public: PROMISC }; + /** + * \brief Constructs an instance of Sniffer + * + * \param device The device from which to capture packets + */ + Sniffer(const std::string& device); + /** * \brief Constructs an instance of Sniffer using the provided configuration. * - * This constructor was added as a way to improve the parameter bloat - * introduced by the other ones available. You should create an instance - * of SnifferConfiguration, set the desired parameters, and then use it - * when constructing a Sniffer object. + * Use the SnifferConfiguration object to specify how you want to configure + * the properties of this sniffer * * \sa SnifferConfiguration * @@ -344,12 +349,13 @@ public: * \param filter A capture filter to be used on the sniffing session.(optional); * \param rfmon Indicates if the interface should be put in monitor mode.(optional); */ - TINS_DEPRECATED(Sniffer(const std::string& device, promisc_type promisc = NON_PROMISC, + TINS_DEPRECATED(Sniffer(const std::string& device, promisc_type promisc, const std::string& filter = "", bool rfmon = false)); private: friend class SnifferConfiguration; + void init(const std::string& device, const SnifferConfiguration& configuration); void set_snap_len(unsigned snap_len); void set_buffer_size(unsigned buffer_size); void set_promisc_mode(bool promisc_enabled); diff --git a/src/sniffer.cpp b/src/sniffer.cpp index e393a1d..105b2e1 100644 --- a/src/sniffer.cpp +++ b/src/sniffer.cpp @@ -232,30 +232,12 @@ bool BaseSniffer::set_direction(pcap_direction_t d) { // ****************************** Sniffer ****************************** +Sniffer::Sniffer(const string& device) { + init(device, SnifferConfiguration()); +} + Sniffer::Sniffer(const string& device, const SnifferConfiguration& configuration) { - char error[PCAP_ERRBUF_SIZE]; - pcap_t* phandle = pcap_create(TINS_PREFIX_INTERFACE(device).c_str(), error); - if (!phandle) { - throw pcap_error(error); - } - set_pcap_handle(phandle); - - // Set the netmask if we are able to find it. - bpf_u_int32 ip, if_mask; - if (pcap_lookupnet(TINS_PREFIX_INTERFACE(device).c_str(), &ip, &if_mask, error) == 0) { - set_if_mask(if_mask); - } - - // Configure the sniffer's attributes prior to activation. - configuration.configure_sniffer_pre_activation(*this); - - // Finally, activate the pcap. In case of error, throw - if (pcap_activate(get_pcap_handle()) < 0) { - throw pcap_error(pcap_geterr(get_pcap_handle())); - } - - // Configure the sniffer's attributes after activation. - configuration.configure_sniffer_post_activation(*this); + init(device, configuration); } Sniffer::Sniffer(const string& device, @@ -269,29 +251,7 @@ Sniffer::Sniffer(const string& device, configuration.set_filter(filter); configuration.set_rfmon(rfmon); - char error[PCAP_ERRBUF_SIZE]; - pcap_t* phandle = pcap_create(TINS_PREFIX_INTERFACE(device).c_str(), error); - if (!phandle) { - throw pcap_error(error); - } - set_pcap_handle(phandle); - - // Set the netmask if we are able to find it. - bpf_u_int32 ip, if_mask; - if (pcap_lookupnet(TINS_PREFIX_INTERFACE(device).c_str(), &ip, &if_mask, error) == 0) { - set_if_mask(if_mask); - } - - // Configure the sniffer's attributes prior to activation. - configuration.configure_sniffer_pre_activation(*this); - - // Finally, activate the pcap. In case of error, throw - if (pcap_activate(get_pcap_handle()) < 0) { - throw pcap_error(pcap_geterr(get_pcap_handle())); - } - - // Configure the sniffer's attributes after activation. - configuration.configure_sniffer_post_activation(*this); + init(device, configuration); } Sniffer::Sniffer(const string& device, @@ -303,6 +263,10 @@ Sniffer::Sniffer(const string& device, configuration.set_filter(filter); configuration.set_rfmon(rfmon); + init(device, configuration); +} + +void Sniffer::init(const string& device, const SnifferConfiguration& configuration) { char error[PCAP_ERRBUF_SIZE]; pcap_t* phandle = pcap_create(TINS_PREFIX_INTERFACE(device).c_str(), error); if (!phandle) {