Compiler Warning libpcap - pcap

Having followed the examples http://www.tcpdump.org/pcap.htm, and exploring the documentation, I can't see what I have done wrong with the following code
// main.c
void got_packet(u_char *args, struct pcap_pkthdr *header, const u_char *packet);
/// ...snip...
void got_packet(u_char *args, struct pcap_pkthdr *header, const u_char *packet) {
printf("Received Packet\n");
}
/// ...snip...
pcap_dispatch(handle, 100, got_packet, NULL);
to warrant the compiler warnings that I am seeing:
gcc -g -O3 -I../sensor/ -c -o sensordatad.o sensordatad.c
main.c: In function ‘start_capture’:
main.c:96:27: warning: initialization from incompatible pointer type [enabled by default]
main.c:146:3: warning: passing argument 3 of ‘pcap_dispatch’ from incompatible pointer
type [enabled by default]
It might be that I'm being too fussy, it seems like every other piece of C code I have ever seen has a page full of compiler warnings when compiling, but I want to at least finish this code without warnings!
I've also tried doing this, to somehow typecast the function pointer, something I hadn't seen in any of the examples; but I felt it worth a shot:
pcap_handler callback = &got_packet;
/// ...snip...
pcap_dispatch(handle, 100, &callback, NULL);
This resulted in the equally perplexing error:
main.c: In function ‘start_capture’:
main.c:96:27: warning: initialization from incompatible pointer type [enabled by default]
It's the first time I've run across function pointers with special typedef'ed types, the man page for pcap_{loop,dispatch} reads:
NAME
pcap_loop, pcap_dispatch - process packets from a live capture or savefile
SYNOPSIS
#include
typedef void (*pcap_handler)(u_char *user, const struct pcap_pkthdr *h,
const u_char *bytes);
int pcap_loop(pcap_t *p, int cnt,
pcap_handler callback, u_char *user);
int pcap_dispatch(pcap_t *p, int cnt,
pcap_handler callback, u_char *user);

Solved
The function signature should be:
void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet);
I had:
void got_packet(u_char *args, struct pcap_pkthdr *header, const u_char *packet);
I suppose in some ways the error makes sense?! But anyway Solved.

Related

How to declare double const* const* variable in cython?

I have a c++ function in "example.h":
bool myFunc(double const* const* p);
and I want to wrap it with cython code (in .pyx file).
Howerver, when I'm write the following code:
cdef extern from r"example.h":
bool myFunc(double const*const* p)
I'm receiving the following error:
Error compiling Cython file:
Expected ')', found '*'
and pycharm shows this error on double const* const* p:
Unresolved reference 'const'
How can I declare that kind of variables?
In C/C++, there is ongoing battle where to put the const-qualifier: either
void foo(const int *a);
or
void foo(int const *a);
both meaning the same thing.
There is no such battle in Cython, because it accept only the first version.
The above rule, applied to double** leads to:
cdef extern from r"example.h":
bool myFunc(const double * const* p)
Or as a work-around one could drop the const-qualifier altogether:
cdef extern from r"example.h":
bool myFunc(const double **p)
which I would not recommend, all above in large projects, where using const-qualifiers helps a lot when figuring out what happens.

Is it possible to invoke win32 call in windbg?

I'm trying to invoke win32 api with windbg,
> .call kernel32!ExitProcess(0)
^ Symbol not a function in '.call kernel32!ExitProcess(0)'
windbg should support this, any idea?
One reason might be that the debugger cannot find the method:
0:004> x kernel32!Exit*
00007ffc`12e9b0c0 KERNEL32!ExitVDM (<no parameter info>)
00007ffc`12e7d620 KERNEL32!ExitProcessImplementation (<no parameter info>)
You certainly don't have private symbols for kernel32!ExitProcess(). If you don't have the private symbols, then you need to have a function in your own code (for which you have private symbols) that you can pass as the prototype:
.call /s SameSignatureAsExitProcess kernel32!ExitProcess(0)
So what I would suggest is: get the LLD extension, compile yourself some methods like
Header file:
#pragma once
#include <Windows.h>
class Call
{
public:
static void v();
static void vi(int);
static void vui(unsigned int);
static void vl(long);
static void vul(unsigned long);
};
Cpp file:
#include "Call.h"
void Call::v(){}
void Call::vi(int) {}
void Call::vui(unsigned int) {}
void Call::vl(long) {}
void Call::vul(unsigned long) {}
and then use it
0:000> .load ...\lld
0:000> !injectdll ...\Calls\x64\Debug\Calls.dll
ModLoad: 00007ffc`11270000 00007ffc`1129e000 C:\Windows\System32\IMM32.DLL
ModLoad: 00007ffc`070a0000 00007ffc`070c5000 ...\Calls\x64\Debug\Calls.dll
ModLoad: 00007ffb`f9000000 00007ffb`f91c2000 C:\Windows\System32\ucrtbased.dll
ModLoad: 00007ffc`07070000 00007ffc`07092000 C:\Windows\System32\VCRUNTIME140D.dll
ModLoad: 00007ffc`0d800000 00007ffc`0d89c000 C:\Windows\system32\uxtheme.dll
ntdll!NtTerminateThread+0x14:
00007ffc`138f01c4 c3 ret
0:004> ld Calls
*** WARNING: Unable to verify checksum for ...\Calls\x64\Debug\Calls.dll
Symbols loaded for Calls
0:004> x Calls!Call::*
00007ffc`070b1710 Calls!Call::vui (unsigned int)
00007ffc`070b1760 Calls!Call::vul (unsigned long)
00007ffc`070b1670 Calls!Call::vi (int)
00007ffc`070b1620 Calls!Call::v (void)
00007ffc`070b16c0 Calls!Call::vl (long)
0:004> .call /s Calls!Call::v kernelbase!TerminateProcess()
Thread is set up for call, 'g' will execute.
WARNING: This can have serious side-effects,
including deadlocks and corruption of the debuggee.
While this looks much better (no error message), it still does not work - the process does not terminate. I currently can't figure out why. Maybe it's still helpful for someone.
What works for me is
static void t(); // .h
void Call::t() // .cpp
{
ExitProcess(0);
}
and then
0:007> .call Calls!Call::t()
Thread is set up for call, 'g' will execute.
WARNING: This can have serious side-effects,
including deadlocks and corruption of the debuggee.
0:007> p
Calls!Call::t+0x2a:
00007ffc`0709166a 33c9 xor ecx,ecx
0:007> p
ntdll!NtTerminateProcess+0x14:
00007ffc`138efce4 c3 ret
but that's not very convenient, if you don't know in advance what method you want to call.

extern struct not recognised in CDT

I have following piece of code in one of my source file under a project in CDT(eclipse).
extern "C" {
void* obj1(int size); /* alloc uninit memory */
void* obj2(int size); /* alloc cleared memory */
void* obj3(void*, int size); /* extend memory, new mem is uninit */
void obj4(void* ptr);
}
I am getting this error message "expected identifier or '(' before string constants"
i think that compiler could not recognise it and i need to supply it with some flag for this purpose.please propose solution for it.It also gives the same message for another piece of code
extern "C" { int NlvStrmatch(const char*, const char*, int);
}

boost thread+signals: mem_fn error, invalid use of non-static member function

I am trying to get acquainted with boost thread and signals. I have therefore implemented this very simple code consisting of a class (Class1) implementing a thread. I'd like this class to provide services as result of signals reception. To this end I have just started to exploit the signal boost library but I am getting this error:
/home/andrea/libs/boost_1_50_0/boost/bind/mem_fn.hpp:359:22: error: invalid use of non-static member function
when I try to compile it in the Eclipse environment with gcc. Is there anything wrong with the singleton or is the binding to the instance method?
Here is Class1.cpp
#include "Class1.hpp"
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
#include "Package1.hpp"
Class1::Class1(){
boost::thread thread(boost::bind(&Class1::classifierBehavior,this));
};
void Class1::classifierBehavior(){
service.run();
Package1Signals::getInstance()->signal1.connect(boost::bind(&Class1::method1, boost::ref(*this)));
};
void Class1::method1(Signal1 signal1){}
And Package1.hpp
#ifndef PACKAGE1_HEADER
#define PACKAGE1_HEADER
#include <boost/signal.hpp>
struct Signal1{
int foo;
};
class Package1Signals{
private:
Package1Signals();
static Package1Signals * instance;
public:
boost::signal<void (Signal1)> signal1;
static Package1Signals * getInstance(){
if(!instance){
instance = new Package1Signals();
}
return instance;
};
};
#endif
Your binder should have 1 argument:
boost::bind(&Class1::methpod1, boost::ref(*this), _1)

Is it possible to have an exported function in a DLL return a static member of an exported class?

I'm working on a plug-in protocol of sorts. The idea is that there's a base clase PCOperatorBase and that plugins would subclass this base class to provide specific functionality through a "process" virtual method that they override. The subclasses also should have a static struct member that hold typical plugin info: plug name, type and subtype. This info is used by another class (a PCOperatorManager) to know what types of plugins is dealing with. And I thought about having it be a static member so that there's no need to instantiate a plug to merely find out about the type of operator it is.
I have the following classes:
PCOperatorBase.h the superclass from which to derive all other plugs:
#ifdef PCOPERATORBASE_EXPORTS
#define PCOPERATORBASE_API __declspec(dllexport)
#else
#define PCOPERATORBASE_API __declspec(dllimport)
#endif
class PCOPERATORBASE_API PCOperatorBase
{
public:
typedef struct OperatorInfo
{
wchar_t* name;
wchar_t* type;
wchar_t* subtype;
} OperatorInfo;
PCOperatorBase(void);
virtual ~PCOperatorBase(void);
virtual int process(int* inBuffer, int* outBuffer, int bufferSize);
};
And then, for instance, a subclass:
BlackNWhite.h: a RGB->black / white operator -- yes, these are going to be graphics plugs, and disregard the types for the in / out buffers.. they are merely placeholders at this point.
#ifdef BLACKNWHITE_EXPORTS
#define BLACKNWHITE_API __declspec(dllexport)
#else
#define BLACKNWHITE_API __declspec(dllimport)
#endif
// This class is exported from the BlackNWhite.dll
class BLACKNWHITE_API CBlackNWhite : PCOperatorBase
{
public:
static PCOperatorBase::OperatorInfo* operatorInfo;
CBlackNWhite(void);
virtual ~CBlackNWhite(void);
//virtual int process(int* inBuffer, int* outBuffer, int bufferSize);
};
BLACKNWHITE_API CBlackNWhite* getOperatorInstance();
BLACKNWHITE_API const PCOperatorBase::OperatorInfo* getOperatorInfo();
And here the implementation file:
BlackNWhite.cpp:
#include "stdafx.h"
#include "BlackNWhite.h"
BLACKNWHITE_API CBlackNWhite* getOperatorInstance()
{
return new CBlackNWhite();
}
BLACKNWHITE_API const PCOperatorBase::OperatorInfo* getOperatorInfo()
{
return CBlackNWhite::operatorInfo;
}
CBlackNWhite::CBlackNWhite()
{
}
CBlackNWhite::~CBlackNWhite()
{
}
Now, I've tried a few approaches but I can't get the DLL to compile, because of the static member.The linker throws:
\BlackNWhite.lib and object c:\Projects\BlackNWhite\Debug\BlackNWhite.exp
1>BlackNWhite.obj : error LNK2001: unresolved external symbol "public: static struct PCOperatorBase::OperatorInfo * CBlackNWhite::operatorInfo" (?operatorInfo#CBlackNWhite##2PAUOperatorInfo#PCOperatorBase##A)
1>c:\Projects\BlackNWhite\Debug\BlackNWhite.dll : fatal error LNK1120: 1 unresolved externals
I thought that since the struct is defined inside the base class and the base class is exporting, the struct would export too. But I guess I'm wrong?
So how should I be doing it?
And regardless, is there a better approach to having the plugs' factory export their name,type and subtype without the need for instantiation than a static class member? For example, a resource file? or even another extern "C" function could return this info.. But I just felt that since it's C++ it makes the most sense to encapsulate this data (which is about the class as a factory itself) within the class, whether through a member or a method.
It doesn't have anything to do with DLLs, you declared the static member but you forgot to define it. Add this line to BlackNWhite.cpp:
PCOperatorBase::OperatorInfo* CBlackNWhite::operatorInfo = NULL;