问题
I’ve written a C program which write a valid PCAP file into a malloc’ed chunk of memory:
u_char* myPCAP = writePCAP( ... );
The program works wonderfully, and if I write myPCAP to a file, I can read that file in Wireshark and everything. So I know that everything is working.
But now, I want to assign myPCAP a pcap_t*
file descriptor and pass that FD to another program. (nDPI, for those who are curious.) How could I do this? I was hoping this would work:
pcap_t* pcap = fdopen( ((int*)myPCAP), "r" );
But:
me@ubuntu:/home/me# make all
gcc -g -fPIC -DPIC -I../src/include -g -O2 -c MyCode.c -o MyCode.o
MyCode.c: In function ‘msgHandler’:
MyCode.c:750:34: warning: passing argument 1 of ‘fdopen’ makes integer from pointer without a cast [-Wint-conversion]
pcap_t* pcap = (pcap_t*)fdopen( ((int*)myPCAP), "r" );
^
In file included from MyCode.c:16:0:
/usr/include/stdio.h:265:14: note: expected ‘int’ but argument is of type ‘int *’
extern FILE *fdopen (int __fd, const char *__modes) __THROW __wur;
^~~~~~
g++ -g -fPIC -DPIC -I../src/include -g -O2 MyCode.o libndpiReader.a -o ndpiReader ../src/lib/libndpi.a -lpcap -lpthread -lm
me@ubuntu:/home/me#
Anyone see a solution? There’s got to be a way, right? Thank you.
来源:https://stackoverflow.com/questions/63784671/assign-a-pcap-t-file-descriptor-to-a-chunk-of-memory