@@ -41,6 +41,11 @@ namespace ipxp {
4141// Read only 1 packet into packet block
4242constexpr size_t PCAP_PACKET_BLOCK_SIZE = 1 ;
4343
44+ struct UserData {
45+ parser_opt_t * opt;
46+ ParserStats& stats;
47+ };
48+
4449__attribute__ ((constructor)) static void register_this_plugin ()
4550{
4651 static PluginRecord rec = PluginRecord (" pcap" , [](){return new PcapReader ();});
@@ -49,12 +54,14 @@ __attribute__((constructor)) static void register_this_plugin()
4954
5055/* *
5156 * \brief Parsing callback function for pcap_dispatch() call. Parse packets up to transport layer.
52- * \param [in,out] arg Serves for passing pointer to Packet structure into callback function.
57+ * \param [in,out] arg Serves for passing pointer to Packet user data structure into callback function.
5358 * \param [in] h Contains timestamp and packet size.
5459 * \param [in] data Pointer to the captured packet data.
5560 */
5661void packet_handler (u_char *arg, const struct pcap_pkthdr *h, const u_char *data)
5762{
63+ UserData *user_data = reinterpret_cast <UserData *>(arg);
64+
5865#ifdef __CYGWIN__
5966 // WinPcap, uses Microsoft's definition of struct timeval, which has `long` data type
6067 // used for both tv_sec and tv_usec and has 32 bit even on 64 bit platform.
@@ -64,9 +71,9 @@ void packet_handler(u_char *arg, const struct pcap_pkthdr *h, const u_char *data
6471 new_h.ts .tv_usec = *(reinterpret_cast <const uint32_t *>(h) + 1 );
6572 new_h.caplen = *(reinterpret_cast <const uint32_t *>(h) + 2 );
6673 new_h.len = *(reinterpret_cast <const uint32_t *>(h) + 3 );
67- parse_packet (( parser_opt_t *) arg , new_h.ts , data, new_h.len , new_h.caplen );
74+ parse_packet (user_data-> opt , user_data-> stats , new_h.ts , data, new_h.len , new_h.caplen );
6875#else
69- parse_packet (( parser_opt_t *) arg , h->ts , data, h->len , h->caplen );
76+ parse_packet (user_data-> opt , user_data-> stats , h->ts , data, h->len , h->caplen );
7077#endif
7178}
7279
@@ -249,12 +256,14 @@ InputPlugin::Result PcapReader::get(PacketBlock &packets)
249256 parser_opt_t opt = {&packets, false , false , m_datalink};
250257 int ret;
251258
259+ UserData user_data = {&opt, m_parser_stats};
260+
252261 if (m_handle == nullptr ) {
253262 throw PluginError (" no interface capture or file opened" );
254263 }
255264
256265 packets.cnt = 0 ;
257- ret = pcap_dispatch (m_handle, PCAP_PACKET_BLOCK_SIZE, packet_handler, (u_char *) (&opt ));
266+ ret = pcap_dispatch (m_handle, PCAP_PACKET_BLOCK_SIZE, packet_handler, (u_char *) (&user_data ));
258267 if (m_live) {
259268 if (ret == 0 ) {
260269 return Result::TIMEOUT;
0 commit comments