New: XDA launches forum for app developers. Discuss coding, tools, marketing, and more.
XDA Developers Android and Mobile Development Forum
Forgot your password?
 
Post Reply+
Tip us?
 
daim0nas
Old
#1  
Junior Member - OP
Thanks Meter 1
Posts: 18
Join Date: Mar 2011
Question [Q] libpcap static linking errors

Hello,

I have downloaded the libpcap in external folder from android source code using the Git and
repo. I used the toolcains of ndk (android-ndk-r5b) to compile the library following these
steps:

./configure --prefix=path-to-android-src/mydroid/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin
make clean
make
make install

and then a file libpcap.a is produced.

I have make a simple sniffer using libpcap (sniffer.c). When I try to compile it, these errors occur:
agcc sniffer.c libpcap.a

sniffer.c:9:18: error: pcap.h: No such file or directory
sniffer.c:19: warning: 'struct pcap_pkthdr' declared inside parameter list
sniffer.c:19: warning: its scope is only this definition or declaration, which is probably not what you want
sniffer.c: In function 'processPacket':
sniffer.c:23: warning: incompatible implicit declaration of built-in function 'printf'
sniffer.c:24: error: dereferencing pointer to incomplete type
sniffer.c:26: error: dereferencing pointer to incomplete type
sniffer.c:33: error: dereferencing pointer to incomplete type
sniffer.c: In function 'main':
sniffer.c:45: error: 'pcap_t' undeclared (first use in this function)
sniffer.c:45: error: (Each undeclared identifier is reported only once
sniffer.c:45: error: for each function it appears in.)
sniffer.c:45: error: 'descr' undeclared (first use in this function)
sniffer.c:46: error: 'PCAP_ERRBUF_SIZE' undeclared (first use in this function)
sniffer.c:62: warning: incompatible implicit declaration of built-in function 'printf'
sniffer.c:66: warning: incompatible implicit declaration of built-in function 'fprintf'
sniffer.c:66: error: 'stderr' undeclared (first use in this function)
sniffer.c:72: warning: incompatible implicit declaration of built-in function 'fprintf'

when I try to produce the object code in advance:
agcc -c sniffer.o sniffer.c
the same errors occured.

Can someone explain me how to solve this problem?

here is the alias of agcc I use in bashrc file:
./configure --prefix=/home/petsas/projects/mobile-attacks/mydroid/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/

Also, I have attached a file with the outputs of the commands above: configure, make and make install.

Thank you in advance!
Attached Files
File Type: txt outputs.txt - [Click for QR Code] (7.1 KB, 20 views)
 
daim0nas
Old
#2  
Junior Member - OP
Thanks Meter 1
Posts: 18
Join Date: Mar 2011
I used the options -I and -L to specify the include and library files, and these errors are gone now..

Here is my new error log:

agcc --static -I /home/petsas/projects/mobile-attacks/mydroid/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/include -L /home/petsas/projects/mobile-attacks/mydroid/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/lib -o sniffer sniffer.c libpcap.a

/home/petsas/projects/mobile-attacks/android-ndk-r5b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/bin/ld: libpcap.a(pcap-linux.o): Relocations in generic ELF (EM: 3)
/home/petsas/projects/mobile-attacks/android-ndk-r5b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/bin/ld: libpcap.a(pcap-linux.o): Relocations in generic ELF (EM: 3)
/home/petsas/projects/mobile-attacks/android-ndk-r5b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/bin/ld: libpcap.a(pcap-linux.o): Relocations in generic ELF (EM: 3)
libpcap.a: could not read symbols: File in wrong format
collect2: ld returned 1 exit status

any ideas?
 
Zuljin
Old
#3  
Junior Member
Thanks Meter 7
Posts: 13
Join Date: May 2010
I'm not 100% sure, but if you want to build native library for android you shouldn't use ./configure script. You should create Android.mk file and build library using it. Android.mk file is a native build environment for Android.
Refer to android-ndk\docs\ANDROID-MK.html for more information.
The Following User Says Thank You to Zuljin For This Useful Post: [ Click to Expand ]
 
daim0nas
Old
#4  
Junior Member - OP
Thanks Meter 1
Posts: 18
Join Date: Mar 2011
Yeah!

Thank you Zuljin!!!

I put the libpcap direcotry into the android-ndk-r5b/samples, I modified a bit the Android.mdk
and the structure of the direcotry, so as to look like the other ones inside the samples dir and
I used the ndk-build command inside the android-ndk-r5b direcory:

ndk-build -C samples/android-ndk-r5b

All worked fine! I was able to produce a libpicap.so file, and put it in the phone using the
commands:
in android:
# su
# mount -o,rw remount /dev/block/mtdblock4 /system/lib

in my Desktop:
$ adb push libpcap.so /system/lib/

I made a simple sniffer, I compiled and linked with the libpcap.so and works perfectly in
my HTC Hero!
 
mail2vijayan
Old
#5  
Junior Member
Thanks Meter 0
Posts: 3
Join Date: Jun 2011
Location: Bangalore
Hi,

I have also trying to build this libpcap on Android.
I have downloaded the source from platform_external_libpcap and put all these files under jni directory for a android-ndk project (default) and tried building using ndk-build script.

But nothing happens but the control returns plainly without compiling the .so or .a file.


Can you tell me how you organized the folders in ndk project.?


regards,
Vijayan
 
daim0nas
Old
#6  
Junior Member - OP
Thanks Meter 1
Posts: 18
Join Date: Mar 2011
Hello Vijayan,

You have to make a directory i.e. libpcap and then inside this directory,
you've got to create another one named as jni and put there all the source
files, as you have done. Then, inside this directory (jni) create a file named as
Android.mk so that it contains the follow lines:

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_SRC_FILES:=\
/bpf_dump.c\
/bpf/net/bpf_filter.c\
/bpf_image.c\
/etherent.c\
/fad-gifc.c\
/gencode.c\
/grammar.c\
/inet.c\
/nametoaddr.c\
/optimize.c\
/pcap.c\
/pcap-linux.c\
/savefile.c\
/scanner.c\
/version.c


LOCAL_CFLAGS:=-O2 -g
LOCAL_CFLAGS+=-DHAVE_CONFIG_H -D_U_="__attribute__((unused))" -Dlinux -D__GLIBC__ -D_GNU_SOURCE

LOCAL_MODULE:= pcap
LOCAL_MODULE_FILENAME:= libpcap


LOCAL_LDLIBS := \
-ldl

include $(BUILD_SHARED_LIBRARY)

Then, you only have to type this command:
ndk-build -C <path-to-jni-direcotry>

That's all! Now, inside the libpcap directory, and in the path:
libpcap/libs/armeabi/ should have been created the file you want:
libpcap.so

Let me know if you have any problems,
Thanasis
 
mail2vijayan
Old
#7  
Junior Member
Thanks Meter 0
Posts: 3
Join Date: Jun 2011
Location: Bangalore
Hi,

Thanks. Now I'm able to compile the libpcap.so.

using this .so I have compiled the jpcap-0.7 (C source fiiles) to generate libjpcap.so.

Then I have created my Sniffer Application on Android. I have added the jpcap.jar, jpcap.so and libpcap.so in the lib folder of my Application.

But when I call this line

NetworkInterface[] devices = JpcapCaptor.getDeviceList();

am getting the following runtime error:

Quote:
FATAL EXCEPTION: main
java.lang.UnsatisfiedLinkError: getDeviceList
at jpcap.JpcapCaptor.getDeviceList(Native Method)
at com.example.snifferapp.SnifferApp.onCreate(Sniffer App.java:26)
at android.app.Instrumentation.callActivityOnCreate(I nstrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:1611)
at android.app.ActivityThread.handleLaunchActivity(Ac tivityThread.java:1663)
at android.app.ActivityThread.access$1500(ActivityThr ead.java:117)
at android.app.ActivityThread$H.handleMessage(Activit yThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99 )
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.jav a:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:597)
at dalvik.system.NativeStart.main(Native Method)

Can you tellme What is going wrong here ?


Thanks.
Vijay
 
daim0nas
Old
#8  
Junior Member - OP
Thanks Meter 1
Posts: 18
Join Date: Mar 2011
Yes, I had the same problem with you when I tried to make a sniffing application
using another one known java wrapper of libpcap: the jNetPcap, and particularly
I couldn't get the devices through this call: Pcap.findAllDevs(alldevs, errbuf);

That's because, even if you have root privileges on the console of the phone, as
you have a rooted phone, you cannot raun an application as root. That's not possible.
You can do that, only if your application is signed with the key of the platform, that is
of the Android OS that runs inside your phone.. So, the only way is to built by yourself
the Android OS and put it into the mobile. And then you have to sign your application
with the key that you have used to sign the OS.

For more information you can look here:
http://stackoverflow.com/questions/5...alldevs-method
The Following User Says Thank You to daim0nas For This Useful Post: [ Click to Expand ]
 
mail2vijayan
Old
#9  
Junior Member
Thanks Meter 0
Posts: 3
Join Date: Jun 2011
Location: Bangalore
Hi,

Thanks for your reply.
But its very confusing. :)
Can you tellme, Whether Its possible to run a client application into Android which can monitor the network traffic in the device by anymeans?

If then what are all the possible ways. (like jpcap, etc....)

My idea is to develop a simple client application. I cannot think of building the whole Android OS and loading it into each device.

Hope to get a reply.


Thanks,
Vijay
 
daim0nas
Old
#10  
Junior Member - OP
Thanks Meter 1
Posts: 18
Join Date: Mar 2011
I haven't done it before, because I only experience with native
programs that do network monitoring..

I think there are two simple solutions here. The first one is what exactly
Shark does: A tcpdump is running in the background that produces
traffic traces (pcap files) and you can develop an application that in periodic
time intervals reads these files and produce some outputs to the user.
The other one solution I can imagine is to develop again a native sniffer in
C with libpcap and then for each packet you receive or better for a batch of
packets you receive, you can send it through sockets to you application for
further processing..

Hope that helps..

FYI: There is a new application in Android called faceniff that does the work of
firesheep. That is you can hijack web sessions from facebook users. How is it
possible this application to monitor the web traffic of the phone? I think that
uses a native sniffer for this.. I am not sure, but it is not open source..

Tags
libpcap, library, linking, ndk, static

XDA PORTAL POSTS

MicrowaveTimePicker Brings 4.2 TimePicker to 2.1+ Devices

It’s frustrating to see slick new features that you can’t use when … more

What’s Possible with CASUAL & How to Make Your Own CASUAL – XDA Developer TV

XDA Elite Recognized Developer AdamOutler is known … more

Pearl Chen to Talk NFC Development at XDA:DevCon 2013

From HTML to LEDs or Android to Arduino, Hardware Hacking is a pastime of many people … more

Avoid Framework Bootloops on Xperias Running Jelly Bean

If you’re a Sony device owner running a stock Android Jelly Bean firmware and … more