Skip to content
This repository has been archived by the owner on Mar 4, 2021. It is now read-only.

Commit

Permalink
Add Portolan sync JNI API
Browse files Browse the repository at this point in the history
In doing so, also refactor OONI sync API such that it is easier to
add support for Portolan and such that the structure of the repository
makes more sense than before.
  • Loading branch information
bassosimone committed Dec 9, 2015
1 parent dd841f0 commit d776b76
Show file tree
Hide file tree
Showing 12 changed files with 316 additions and 67 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
measurement_kit-jni-libs-v*.tar.bz2
measurement_kit-jni-libs-v*.tar.bz2.asc
measurement_kit-jni-v*.tar.bz2
measurement_kit-jni-v*.tar.bz2.asc
18 changes: 15 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
PHONIES += check dist download-and-verify help jni-libs jni-libs-no-unpack
PHONIES += unpack unpack-clean
PHONIES += check dist download-and-verify help javah jni-libs
PHONIES += jni-libs-no-unpack unpack unpack-clean
.PHONY: $(PHONIES)

GPG2 = gpg2
JAVAH = javah
NDK_BUILD = ndk-build
WGET = wget

Expand All @@ -12,6 +13,7 @@ TAG = 5-ge0eb44b
INPUT = measurement_kit-jni-$(VERSION)-$(TAG).tar.bz2
OVERSION = $$(git describe --tags)
OUTPUT = measurement_kit-jni-libs-$(OVERSION).tar.bz2
PACKAGE = io.github.measurement_kit.jni

ABIS = arm64-v8a armeabi armeabi-v7a mips mips64 x86 x86_64

Expand All @@ -27,7 +29,12 @@ dist: jni-libs
@echo "Creating $(OUTPUT)..."
@tar -cjf $(OUTPUT) java jniLibs

jni-libs: unpack jni-libs-no-unpack
jni-libs: unpack javah jni-libs-no-unpack

javah:
@echo "Creating header files in jni using $(JAVAH)..."
@cd jni && $(JAVAH) -cp ../java $(PACKAGE).sync.OoniSyncApi
@cd jni && $(JAVAH) -cp ../java $(PACKAGE).sync.PortolanSyncApi

jni-libs-no-unpack:
$(NDK_BUILD) NDK_LIBS_OUT=./jniLibs
Expand All @@ -51,6 +58,11 @@ check:
exit 1; \
fi
@echo "Using $(GPG2): $$(which $(GPG2))"
@if [ -z "$$(which $(JAVAH))" ]; then \
echo "FATAL: install $(JAVAH) or make sure it's in PATH" 1>&2; \
exit 1; \
fi
@echo "Using $(JAVAH): $$(which $(JAVAH))"
@if [ -z "$$(which $(NDK_BUILD))" ]; then \
echo "FATAL: install $(NDK_BUILD) or make sure it's in PATH" 1>&2; \
exit 1; \
Expand Down
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@ This will download the latest Measurement Kit binaries from GitHub, verify
them with GnuPG, cross-compile the JNI code and statically link it with the
downloaded binaries, package a tarball suitable for distribution.

The version number of the tarball will be the version downloaded from
GitHub, plus the abbreviated reference of the current HEAD.

A Unix environment is assumed. You need to have the following executables
in your PATH:

- git
- wget
- ndk-build
- gpg2
- javah
- ndk-build
- wget

Git, wget, and gpg2 could be installed using your distributions package
Most of these could be installed using your distributions package
manager. As regards ndk-build, [there are instructions to install it inside
Measurement Kit repository](https://github.com/measurement-kit/measurement-kit/tree/master/mobile/android#installing-the-ndk).
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
package io.github.measurement_kit.jni.sync;

/**
* Allows to run OONI tests.
* JNI API to run synchronous OONI tests.
*/
class OONI {
public class OoniSyncApi {

/**
* Runs OONI dns-injection test.
Expand Down Expand Up @@ -45,5 +45,4 @@ public static native String tcpConnect(String port,
String inputPath,
boolean verbose,
String logPath);

}
84 changes: 84 additions & 0 deletions java/io/github/measurement_kit/jni/sync/PortolanSyncApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Part of measurement-kit <https://measurement-kit.github.io/>.
// Measurement-kit is free software. See AUTHORS and LICENSE for more
// information on the copying conditions.

package io.github.measurement_kit.jni.sync;

/**
* JNI API to run synchronous Portolan tests.
*/
public class PortolanSyncApi {

/**
* Open traceroute prober (i.e. opaque object to send probes).
* @param useIpv4 whether to use IPv4.
* @param sourcePort Specify source port.
* @return opaque prober on success, zero on failure.
*/
public static native long openProber(boolean useIpv4, int sourcePort);

/**
* Send single traceroute probe.
*
* @param prober Opaque traceroute prober instance.
* @param destIp Destination IP address.
* @param ttl Time to live.
* @param timeout Time to wait for response.
*
* @param outStrings Vector of strings filled by this function that will
* contain the statusCode as first element and the interfaceIp
* as second element. As of MeasurementKit 0.1.0, the status code
* should be one of the following:
*
* - OTHER
* - NO_ROUTE_TO_HOST
* - ADDRESS_UNREACH
* - PROTO_NOT_IMPL
* - PORT_IS_CLOSED
* - TTL_EXCEEDED
* - ADMIN_FILTER
* - GOT_REPLY_PACKET
*
* @param outInts Vector of integers filled by this function that will
* contain the ttl, the number of received bytes, and whether
* the response is IPv4 (the latter with the semantic that zero
* is false and nonzero is true).
*
* @param outDouble On success this vector will be filled with the RTT
* of the response in position 0.
*/
public static native void sendProbe(long prober,
String destIp,
int destPort,
int ttl,
double timeout,
String[
// statusCode
// interfaceIp
] outStrings,
int[
// ttl
// recvBytes
// isIpv4
] outInts,
double[
// rtt
] outDoubles);

/**
* Close opaque traceroute prober instance.
* @param prober Opaque traceroute prober instance.
*/
public static native void closeProber(long prober);

/**
* Checks whether a TCP port is open.
* @param useIpv4 Whether to use IPv4 or not.
* @param addr Address to use.
* @param port Port to use.
* @param timeo Connect() timeout.
* @return True if port is open, false otherwise.
*/
public static native boolean checkPort(boolean useIpv4, String addr,
String port, double timeo);
}
8 changes: 8 additions & 0 deletions jni/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
arm64-v8a/
armeabi/
armeabi-v7a/
io_github_measurement_kit_jni_*.h
mips/
mips64/
x86/
x86_64/
2 changes: 1 addition & 1 deletion jni/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ LOCAL_LDLIBS := -llog -latomic
LOCAL_STATIC_LIBRARIES := measurement_kit event event_pthreads jansson \
maxminddb yaml-cpp
LOCAL_MODULE := measurement_kit_jni
LOCAL_SRC_FILES := measurement_kit_jni.cpp
LOCAL_SRC_FILES := common.cpp ooni_sync_api.cpp portolan_sync_api.cpp
APP_PLATFORM := android-21
LOCAL_CPPFLAGS += -I jni/$(TARGET_ARCH_ABI)/include -std=c++11
include $(BUILD_SHARED_LIBRARY)
21 changes: 21 additions & 0 deletions jni/common.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Part of measurement-kit <https://measurement-kit.github.io/>.
// Measurement-kit is free software. See AUTHORS and LICENSE for more
// information on the copying conditions.

#include <jni.h>
#include <string>
#include "common.hpp"

namespace mk {
namespace jni {

std::string cxxstring(JNIEnv *env, jstring source) {
const char *ptr = env->GetStringUTFChars(source, nullptr);
if (ptr == nullptr) throw std::bad_alloc();
std::string copy = ptr;
env->ReleaseStringUTFChars(source, ptr);
return copy;
}

} // namespace jni
} // namespace mk
19 changes: 19 additions & 0 deletions jni/common.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Part of measurement-kit <https://measurement-kit.github.io/>.
// Measurement-kit is free software. See AUTHORS and LICENSE for more
// information on the copying conditions.

#ifndef JNI_COMMON_HPP
#define JNI_COMMON_HPP

#include <jni.h>
#include <string>

namespace mk {
namespace jni {

std::string cxxstring(JNIEnv *env, jstring source);

} // namespace jni
} // namespace mk

#endif
37 changes: 0 additions & 37 deletions jni/io_github_measurement_kit_jni_sync_OONI.h

This file was deleted.

27 changes: 10 additions & 17 deletions jni/measurement_kit_jni.cpp → jni/ooni_sync_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,17 @@
#include <jni.h>
#include <measurement_kit/ooni.hpp>
#include <string>
#include "io_github_measurement_kit_jni_sync_OONI.h"

static std::string jstring_to_cxxstring(JNIEnv *env, jstring source) {
const char *ptr = env->GetStringUTFChars(source, nullptr);
if (ptr == nullptr) throw std::bad_alloc();
std::string copy = ptr;
env->ReleaseStringUTFChars(source, ptr);
return copy;
}
#include "common.hpp"
#include "io_github_measurement_kit_jni_sync_OoniSyncApi.h"

JNIEXPORT jstring JNICALL
Java_io_github_measurement_1kit_jni_sync_OONI_dnsInjection(
Java_io_github_measurement_1kit_jni_sync_OoniSyncApi_dnsInjection(
JNIEnv *env, jclass /*clazz*/, jstring backend, jstring inputPath,
jboolean /*verbose*/, jstring logPath) {
try {
mk::ooni::DnsInjectionTest()
.set_backend(jstring_to_cxxstring(env, backend))
.set_input_file_path(jstring_to_cxxstring(env, inputPath))
.set_backend(mk::jni::cxxstring(env, backend))
.set_input_file_path(mk::jni::cxxstring(env, inputPath))
.set_verbose()
.run();
} catch (...) {
Expand All @@ -32,12 +25,12 @@ Java_io_github_measurement_1kit_jni_sync_OONI_dnsInjection(
}

JNIEXPORT jstring JNICALL
Java_io_github_measurement_1kit_jni_sync_OONI_httpInvalidRequestLine(
Java_io_github_measurement_1kit_jni_sync_OoniSyncApi_httpInvalidRequestLine(
JNIEnv *env, jclass /*clazz*/, jstring backend, jboolean /*verbose*/,
jstring logPath) {
try {
mk::ooni::HttpInvalidRequestLineTest()
.set_backend(jstring_to_cxxstring(env, backend))
.set_backend(mk::jni::cxxstring(env, backend))
.set_verbose()
.run();
} catch (...) {
Expand All @@ -47,13 +40,13 @@ Java_io_github_measurement_1kit_jni_sync_OONI_httpInvalidRequestLine(
}

JNIEXPORT jstring JNICALL
Java_io_github_measurement_1kit_jni_sync_OONI_tcpConnect(
Java_io_github_measurement_1kit_jni_sync_OoniSyncApi_tcpConnect(
JNIEnv *env, jclass /*clazz*/, jstring port, jstring inputPath,
jboolean verbose, jstring logPath) {
try {
mk::ooni::TcpConnectTest()
.set_port(jstring_to_cxxstring(env, port))
.set_input_file_path(jstring_to_cxxstring(env, inputPath))
.set_port(mk::jni::cxxstring(env, port))
.set_input_file_path(mk::jni::cxxstring(env, inputPath))
.set_verbose()
.run();
} catch (...) {
Expand Down
Loading

0 comments on commit d776b76

Please sign in to comment.