android use pcap library - java

I have a general question according an android app, I need to use some pcap functionality in my android app. Because java does not give the possibility in raw packet injections and low layer programming (as far as I know, pls correct me if I'm wrong) so I was looking for an alternative. So far I found the following:
ANDROID NDK
JNETPCAP
Any suggestions which one I should use or does anyone have other suggestions?

The JNI Solution
You need to wrap the calls and the logic you need out of libpcap in C or C++ and expose the underlying functions through JNI (Java Native Interface) so your application can call native code in Java.
The documentation on JNI is pretty complete on internet, a lot of tutorials exists on this subject such as this one.
If you want to easily wrap native code in JNI you can use Swig which allow you to automatically generate JNI code based on your C/C++ native headers.
The obtained JNI code should be compiled using the Android NDK as a dynamic library (.so). This library is to be placed in your application package under libs/. You can then invoke System.loadLibrary(path_to_you_dynamic_library) to load all the symbols contained in the library and use them in Java.
Using a third-party library
If you're afraid of getting headaches while figuring out how to use JNI, you can look at this library which does the hard work for you, and provides an API to manipulate raw sockets in Java.
http://www.savarese.com/software/rocksaw/

You need to wrap the calls and the logic you need out of libpcap in C or C++ and expose the underlying functions through JNI (Java Native Interface) so your application can call native code in Java.
Or you need to get a library that's already done that, such as, err, umm, jNetPcap.
One problem you may have with any attempt to do packet capture on Android - or any other OS using the Linux kernel - is that, by default, the underlying kernel mechanism used by libpcap (PF_PACKET sockets) requires root privileges. If there's a way to run your code as root, or to give it CAP_NET_RAW and possibly CAP_NET_ADMIN privileges, it might be possible to make it work.

Related

Combine C-Code(32-bit) with Java

I have method/function to get the drive info using Visual C++ of Microsoft Visual Studio. The method only return the type of drive. But it is become difficult when I'm trying to get more information about the drive. This difficulty gets resolved by Java's existing package. Furthermore, few codes are also there which is easy to handle with C platform. So, I'm thinking, if there is any efficient process which can include a native C code with in Java so that the functionality of both the language can be used in meaningful way.
Can anyone help me in this regard?
Java Native Access (JNA) is a way you can access C libraries from Java code. Its advantage over Java Native Interface (JNI) is that the library glue code is written in Java, not C, making it more maintainable.
(I've used JNA a lot at work. If I get permission from work, I may be able to post the JNA stuff I've written, as examples for you to work with.)

What is the easiest way to call Objective-C code from Java?

I need to access a list of Wifi devices on Mac OSX from Java code and after researching it, I've found that I need to resort to "native" code - namely the CoreWLAN framework and the CWInterface.h class (https://developer.apple.com/library/mac/#documentation/CoreWLAN/Reference/CWInterface_reference/translated_content/CWInterface.html)
I initially thought I would be able to call this code using JNA but realized that I needed something since CoreWLAN is Objective-C (not C/C++).
I've tried this Objective-C Java bridge (https://github.com/shannah/Java-Objective-C-Bridge) but I can't work out how to make it find the CoreWLAN framework.
So I've also tried using JNAerator (https://code.google.com/p/jnaerator/wiki/ObjectiveC) so I can I use BridJ (https://code.google.com/p/bridj/), but I can't make it generate the right Java code.
Just using this style java -Xmx1000m -jar jnaerator.jar -framework CoreWLAN -jar CoreWlan.jar runs quickly but results in a jar that only contains mappings for CoreWlan.h
If I run jnaerator against CWInterface.h then jnaerator fails.
(I'm using the latest snapshot version of jnaerator)
What is the best way to call methods on CWInterface.h from Java?
What I would do is create a C++ class that communicates with the Java code through JNI. You can use both C++ and Obj-C in your xCode project. I haven't tried myself to use a Obj-C++ class with JNI, but I would just create a C++ class responsible for all communication between Java and Obj-C, this class can just be included in the Obj-C class where you need it (change the extension of this Obj-C file to .mm since it'll include C++ code).
I found this a helpful article on JNI: http://www.ibm.com/developerworks/java/tutorials/j-jni/
Personally I would do this via two totally separate programs that communicate via a shared memory mapped file. One program written in straight Objective-C, and the other in straight Java.
Memory mapping would mean that both programs would have access to the same region of memory, without having to resort to JNI at all.
Notes on memory mapping from Java:
http://javarevisited.blogspot.co.uk/2012/01/memorymapped-file-and-io-in-java.html
Notes on memory mapping from Objective C:
https://gist.github.com/jverkoey/2985830
That said, if you do want to do go the JNI route, then Apple has the following advice:
It is recommended that you use the Java JNI template in Xcode as a starting point for your JNI development.
To interoperate with the Objective-C runtime from JNI, link against JavaNativeFoundation.framework, which
is a sub-framework of JavaVM.framework. It contains Objective-C classes and macros to automatically set up
and tear down autorelease pools, catch and re-throw Java and Cocoa exceptions, hold JNI global references
in Foundation container classes, and convert object graphs of strings, numbers, lists, maps, and sets.
The above quotes were taken from here:
https://developer.apple.com/library/mac/documentation/java/conceptual/java14development/Java14Development.pdf
You can use the java objective-c bridge for this. You just need to load the framework you want to use with jna's Native.loadLibrary() method.
This example uses the WebKit framework. https://github.com/shannah/Java-Objective-C-Bridge/blob/master/java/test/ca/weblite/objc/TestWebView.java

How to use a C API in Java? JNI?

I'm building a J2EE project, in which I would like to use an API which is only available in C.
I was thinking of using JNI to do so, but after a quick look at this tutorial, it looks that I in order to use JNI, I need to have the source code (.c files) to compile some kind of "JNI library".
In my case, the API only comports the .h with the signature of all the methods, and the already compiled .dll (or .so).
How could I do this?
Thank you!
JNA is a JNI-based library that allows calling normal C functions without needing a JNI-specific wrapper for each one.
Check out JNA. It allows you to use the .DLL directly. All you need to do is write a Java interface with the same functions you need from the .DLL.
Create a small C wrapper for the native library, compile this to a .dll/.so.
For each needed function in the existing C api, create one JNI-compliant C function which simply calls the real API.
JNI offers a pretty low level API for interfacing your Java code with native code. If you are OK with shelling out money, Jinvoke looks like a pretty good alternative which doesn't require you to write any C/C++ code. The plus here is that you get full paid support. If you don't require it, you can go with JNA. Anything but JNI IMO...

Calling Java library from Objective C on Mac

I want to create native Mac OS X application using Cocoa + Objective C but I need to connect to proprietary data source, and for this, owner of the data source only provides Java library. So I need to somehow import this Java library into my project and call functions on its Java classes.(Or create java wrapper around this library and then call my wrapper from objective-C).
Now, how can I do this? Quick google search leads me to JNI but I haven't found any good and actual(current) article/tutorial. I would really need some HOW TO article, how to load this java library, start VM if needed, and how to create java objects and call functions on them. Really something simple and I can move from there. Thanks.
Just to clarify, I repeat: I WANT to call Java functions from Objective-C, I do NOT want to call native functions from Java.
You're probably looking for the Invocation API, a little-known corner of Java Native Interface (JNI) which allows you to load the Java runtime in-process.
That said, you might have an easier time of it with a Java service application that communicates with your Objective-C application over network sockets.
You're looking for the Java-Objective C bridge, try looking at this article or on Apple's developer site. Be aware it is deprecated, that is it isn't being kept up to date with changes to Cocoa. But if you're just using it for an API passing standard Java datatypes you should be OK.

Is there a Java library to access the native Windows API?

Is there a Java library to access the native Windows API? Either with COM or JNI.
You could try these two, I have seen success with both.
http://jawinproject.sourceforge.net
The Java/Win32 integration project
(Jawin) is a free, open source
architecture for interoperation
between Java and components exposed
through Microsoft's Component Object
Model (COM) or through Win32 Dynamic
Link Libraries (DLLs).
https://github.com/twall/jna/
JNA provides Java programs easy access
to native shared libraries (DLLs on
Windows) without writing anything but
Java codeā€”no JNI or native code is
required. This functionality is
comparable to Windows' Platform/Invoke
and Python's ctypes. Access is dynamic
at runtime without code generation.
JNA allows you to call directly into
native functions using natural Java
method invocation. The Java call looks
just like it does in native code. Most
calls require no special handling or
configuration; no boilerplate or
generated code is required.
Also read up here:
http://en.wikipedia.org/wiki/Java_Native_Interface
The Java Native Interface (JNI) is a
programming framework that allows Java
code running in a Java Virtual Machine
(JVM) to call and to be called1 by
native applications (programs specific
to a hardware and operating system
platform) and libraries written in
other languages, such as C, C++ and
assembly.
http://en.wikipedia.org/wiki/Java_Native_Access
Java Native Access provides Java
programs easy access to native shared
libraries without using the Java
Native Interface. JNA's design aims to
provide native access in a natural way
with a minimum of effort. No
boilerplate or generated glue code is
required.
JNA is pretty nice. I'm just a beginner and I found it very easy. Works not only for the Win32 API but for almost any other DLL.
Jacob is quite good on the COM side (but it's real COM - like you write in C++ - if you are familiar with true COM programming, then Jacob is a snap to use)
JNIWrapper or ConfyJ from from TeamDev.
One more option is WinRun4J. It has a native binding layer that aims to be compatible with pinvoke.net (the native binding format used in dot net). See examples for more information. Its a little early days so YMMV.
(full disclosure: i work on the project).
Yet another option is JFFI - this is used in jruby and jython to interact with native libraries.
Check out Waffle

Categories