LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-28-2016, 06:48 AM   #1
kikilinux
Member
 
Registered: Sep 2012
Posts: 125

Rep: Reputation: Disabled
Question problem with callback function in pcap_loop function


I define a class named sessionSeparator
then I define a callback function name packet_handler like bellow :
Code:
	void SessionSeparator::packet_handler(u_char *dumpfile, const struct pcap_pkthdr *header, const u_char *pkt_data)
	{
		// TODO
	}
I call pcap_loop in another public function but eclipse notify a bellow error
Code:
cannot convert ‘Tins::SessionSeparator::packet_handler’ from type ‘void (Tins::SessionSeparator::)(u_char*, const pcap_pkthdr*, const u_char*) {aka void (Tins::SessionSeparator::)(unsigned char*, const pcap_pkthdr*, const unsigned char*)}’ to type ‘pcap_handler {aka void (*)(unsigned char*, const pcap_pkthdr*, const unsigned char*)}’
the complete cpp file is as bellow
Code:
]#include "SessionSeparator.h"

namespace Tins {


	void SessionSeparator::set_in_out_files (char * infile, char * outfile) {
		infile_ = infile;
		outfile_ = outfile;
	}

	void SessionSeparator::run(){

		pcap_t *fp;
		pcap_dumper_t *dumpfile;


		fp = pcap_open_offline(infile_, NULL);
		if ( fp == NULL)
		{
			fprintf(stderr,"\nUnable to find input file.\n");
			return;
		}

		dumpfile = pcap_dump_open(fp, this->outfile_);
		if(dumpfile == NULL)
		{
			fprintf(stderr,"\nError opening output file\n");
			return;
		}

		pcap_loop(fp, 0, packet_handler, (u_char *)dumpfile);

	}

	void SessionSeparator::packet_handler(u_char *dumpfile, const struct pcap_pkthdr *header, const u_char *pkt_data)
	{
		// TODO
	}


} /* namespace Tins */
and header file :
Code:
#include <stdlib.h>
#include <stdio.h>
#include <linux/ip.h>
#include <stdbool.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <net/ethernet.h>
#include <math.h>
#include <unistd.h>

#include <pcap.h>


#ifndef SESSIONSEPARATOR_H_
#define SESSIONSEPARATOR_H_

namespace Tins {


class SessionSeparator {
public:
	void set_in_out_files (char * infile, char * outfile);
	void run() ;
	void packet_handler(u_char *dumpfile, const struct pcap_pkthdr *header, const u_char *pkt_data);

private:
	char * infile_;
	char * outfile_;
};

} /* namespace Tins */

#endif /* SESSIONSEPARATOR_H_ *
what is the correct solution to implement this code ?
 
Old 03-28-2016, 07:14 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
forget C++ for a moment and create a normal callback function:

Code:
void my_handler (unsigned char *uspar, const pcap_pkthdr *pkthdr, const unsigned char*bytes)
{
    pcap_dumper_t *dumpfile= (pcap_dumper_t *)uspar;
    ...
}
PS Callback examples: http://web.axelero.hu/lzsiga/cback.cc
 
  


Reply

Tags
pcap file



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Class callback function teapottwo Programming 2 01-12-2016 07:52 PM
[SOLVED] C - Callback function pointers arashi256 Programming 4 02-21-2014 04:07 AM
How do I write the following callback function? coffeenet Programming 2 01-03-2012 12:15 AM
what is callback function context? manohar Programming 1 03-18-2011 01:05 PM
how does function callback works ?? alred Programming 5 05-12-2006 01:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration