Skip to content

Commit

Permalink
Add FileSniffer constructor with FILE pointer as pcap source (#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
peckto authored Apr 30, 2023
1 parent df509e7 commit ba0c820
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
20 changes: 18 additions & 2 deletions include/tins/sniffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,17 @@ class TINS_API Sniffer : public BaseSniffer {
*/
class TINS_API FileSniffer : public BaseSniffer {
public:
/**
* \brief Constructs an instance of FileSniffer.
* \param fp The pcap file which will be parsed.
* \param configuration A SnifferConfiguration to be used on the file.
*/
FileSniffer(FILE *fp, const SnifferConfiguration& configuration);

/**
* \brief Constructs an instance of FileSniffer.
* \param file_name The pcap file which will be parsed.
* \param filter A capture filter to be used on the file.(optional);
* \param configuration A SnifferConfiguration to be used on the file.
*/
FileSniffer(const std::string& file_name, const SnifferConfiguration& configuration);

Expand All @@ -418,9 +425,18 @@ class TINS_API FileSniffer : public BaseSniffer {
*
* \brief Constructs an instance of FileSniffer.
* \param file_name The pcap file which will be parsed.
* \param filter A capture filter to be used on the file.(optional);
* \param filter A capture filter to be used on the file. (optional)
*/
FileSniffer(const std::string& file_name, const std::string& filter = "");

/**
* \deprecated Use the constructor that takes a SnifferConfiguration instead.
*
* \brief Constructs an instance of FileSniffer.
* \param fp The pcap file which will be parsed.
* \param filter A capture filter to be used on the file. (optional)
*/
FileSniffer(FILE *fp, const std::string& filter = "");
};

template <typename T>
Expand Down
30 changes: 30 additions & 0 deletions src/sniffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,20 @@ void Sniffer::set_rfmon(bool rfmon_enabled) {

// **************************** FileSniffer ****************************

FileSniffer::FileSniffer(FILE *fp,
const SnifferConfiguration& configuration) {
char error[PCAP_ERRBUF_SIZE];
pcap_t* phandle = pcap_fopen_offline(fp, error);
if (!phandle) {
throw pcap_error(error);
}
set_pcap_handle(phandle);

// Configure the sniffer
configuration.configure_sniffer_pre_activation(*this);

}

FileSniffer::FileSniffer(const string& file_name,
const SnifferConfiguration& configuration) {
char error[PCAP_ERRBUF_SIZE];
Expand Down Expand Up @@ -420,6 +434,22 @@ FileSniffer::FileSniffer(const string& file_name, const string& filter) {
config.configure_sniffer_pre_activation(*this);
}

FileSniffer::FileSniffer(FILE *fp, const string& filter) {
SnifferConfiguration config;
config.set_filter(filter);

char error[PCAP_ERRBUF_SIZE];
pcap_t* phandle = pcap_fopen_offline(fp, error);
if (!phandle) {
throw pcap_error(error);
}
set_pcap_handle(phandle);

// Configure the sniffer
config.configure_sniffer_pre_activation(*this);
}


// ************************ SnifferConfiguration ************************

const unsigned SnifferConfiguration::DEFAULT_SNAP_LEN = 65535;
Expand Down

0 comments on commit ba0c820

Please # to comment.