Initiate an SA via native JNI method

This commit is contained in:
Tobias Brunner 2012-08-08 13:23:41 +02:00
parent dffee9e2b0
commit c6c39c783b
2 changed files with 74 additions and 0 deletions

View File

@ -22,6 +22,7 @@
#include "charonservice.h"
#include "android_jni.h"
#include "backend/android_creds.h"
#include "backend/android_service.h"
#include "kernel/android_ipsec.h"
#include "kernel/android_net.h"
@ -50,6 +51,11 @@ struct private_charonservice_t {
*/
android_creds_t *creds;
/**
* android_service instance
*/
android_service_t *service;
/**
* CharonVpnService reference
*/
@ -195,6 +201,27 @@ failed:
return NULL;
}
/**
* Initiate a new connection
*
* @param local local ip address (gets owned)
* @param gateway gateway address (gets owned)
* @param username username (gets owned)
* @param password password (gets owned)
*/
static void initiate(char *local, char *gateway, char *username, char *password)
{
private_charonservice_t *this = (private_charonservice_t*)charonservice;
this->creds->clear(this->creds);
this->creds->add_username_password(this->creds, username, password);
memwipe(password, strlen(password));
free(password);
DESTROY_IF(this->service);
this->service = android_service_create(local, gateway, username);
}
/**
* Initialize/deinitialize Android backend
*/
@ -209,6 +236,11 @@ static bool charonservice_register(void *plugin, plugin_feature_t *feature,
else
{
lib->credmgr->remove_set(lib->credmgr, &this->creds->set);
if (this->service)
{
this->service->destroy(this->service);
this->service = NULL;
}
}
return TRUE;
}
@ -341,3 +373,34 @@ JNI_METHOD(CharonVpnService, deinitializeCharon, void)
library_deinit();
}
/**
* Convert a Java string to a C string. Memory is allocated.
*/
static inline char *convert_jstring(JNIEnv *env, jstring jstr)
{
char *str;
jsize len;
len = (*env)->GetStringUTFLength(env, jstr);
str = malloc(len + 1);
(*env)->GetStringUTFRegion(env, jstr, 0, len, str);
str[len] = '\0';
return str;
}
/**
* Initiate SA
*/
JNI_METHOD(CharonVpnService, initiate, void,
jstring jlocal_address, jstring jgateway, jstring jusername,
jstring jpassword)
{
char *local_address, *gateway, *username, *password;
local_address = convert_jstring(env, jlocal_address);
gateway = convert_jstring(env, jgateway);
username = convert_jstring(env, jusername);
password = convert_jstring(env, jpassword);
initiate(local_address, gateway, username, password);
}

View File

@ -194,6 +194,11 @@ public class CharonVpnService extends VpnService implements Runnable
initializeCharon();
Log.i(TAG, "charon started");
String local_address = getLocalIPv4Address();
initiate(local_address != null ? local_address : "0.0.0.0",
mCurrentProfile.getGateway(), mCurrentProfile.getUsername(),
mCurrentProfile.getPassword());
}
}
catch (InterruptedException ex)
@ -403,6 +408,12 @@ public class CharonVpnService extends VpnService implements Runnable
*/
public native void deinitializeCharon();
/**
* Initiate VPN, provided by libandroidbridge.so
*/
public native void initiate(String local_address, String gateway,
String username, String password);
/**
* Helper function that retrieves a local IPv4 address.
*