An Android specific attribute handler installs DNS servers via Builder

This commit is contained in:
Tobias Brunner
2012-08-13 11:09:33 +02:00
parent ae4f1ea180
commit 3a05756b42
4 changed files with 185 additions and 0 deletions
@@ -4,6 +4,7 @@ include $(CLEAR_VARS)
# copy-n-paste from Makefile.am
LOCAL_SRC_FILES := \
android_jni.c android_jni.h \
backend/android_attr.c backend/android_attr.h \
backend/android_creds.c backend/android_creds.h \
backend/android_service.c backend/android_service.h \
charonservice.c charonservice.h \
@@ -0,0 +1,120 @@
/*
* Copyright (C) 2012 Tobias Brunner
* Copyright (C) 2012 Giuliano Grassi
* Copyright (C) 2012 Ralf Sager
* Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include "android_attr.h"
#include "../charonservice.h"
#include <hydra.h>
#include <debug.h>
#include <library.h>
typedef struct private_android_attr_t private_android_attr_t;
/**
* Private data of an android_attr_t object.
*/
struct private_android_attr_t {
/**
* Public interface.
*/
android_attr_t public;
};
METHOD(attribute_handler_t, handle, bool,
private_android_attr_t *this, identification_t *server,
configuration_attribute_type_t type, chunk_t data)
{
vpnservice_builder_t *builder;
host_t *dns;
switch (type)
{
case INTERNAL_IP4_DNS:
dns = host_create_from_chunk(AF_INET, data, 0);
break;
default:
return FALSE;
}
if (!dns || dns->is_anyaddr(dns))
{
DESTROY_IF(dns);
return FALSE;
}
builder = charonservice->get_vpnservice_builder(charonservice);
builder->add_dns(builder, dns);
dns->destroy(dns);
return TRUE;
}
METHOD(attribute_handler_t, release, void,
private_android_attr_t *this, identification_t *server,
configuration_attribute_type_t type, chunk_t data)
{
/* DNS servers cannot be removed from an existing TUN device */
}
METHOD(enumerator_t, enumerate_dns, bool,
enumerator_t *this, configuration_attribute_type_t *type, chunk_t *data)
{
*type = INTERNAL_IP4_DNS;
*data = chunk_empty;
this->enumerate = (void*)return_false;
return TRUE;
}
METHOD(attribute_handler_t, create_attribute_enumerator, enumerator_t*,
private_android_attr_t *this, identification_t *server, host_t *vip)
{
enumerator_t *enumerator;
INIT(enumerator,
.enumerate = (void*)_enumerate_dns,
.destroy = (void*)free,
);
return enumerator;
}
METHOD(android_attr_t, destroy, void,
private_android_attr_t *this)
{
free(this);
}
/**
* Described in header
*/
android_attr_t *android_attr_create()
{
private_android_attr_t *this;
INIT(this,
.public = {
.handler = {
.handle = _handle,
.release = _release,
.create_attribute_enumerator = _create_attribute_enumerator,
},
.destroy = _destroy,
},
);
return &this->public;
}
@@ -0,0 +1,52 @@
/*
* Copyright (C) 2012 Giuliano Grassi
* Copyright (C) 2012 Ralf Sager
* Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
/**
* @defgroup android_attr android_attr
* @{ @ingroup android_backend
*/
#ifndef ANDROID_ATTR_H_
#define ANDROID_ATTR_H_
#include <library.h>
#include <attributes/attribute_handler.h>
typedef struct android_attr_t android_attr_t;
/**
* Handler for DNS configuration
*/
struct android_attr_t {
/**
* implements the attribute_handler_t interface
*/
attribute_handler_t handler;
/**
* Destroy a android_attr_t
*/
void (*destroy)(android_attr_t *this);
};
/**
* Create a android_attr_t instance.
*/
android_attr_t *android_attr_create(void);
#endif /** ANDROID_ATTR_H_ @}*/
@@ -21,6 +21,7 @@
#include "charonservice.h"
#include "android_jni.h"
#include "backend/android_attr.h"
#include "backend/android_creds.h"
#include "backend/android_service.h"
#include "kernel/android_ipsec.h"
@@ -46,6 +47,11 @@ struct private_charonservice_t {
*/
charonservice_t public;
/**
* android_attr instance
*/
android_attr_t *attr;
/**
* android_creds instance
*/
@@ -243,10 +249,14 @@ static bool charonservice_register(void *plugin, plugin_feature_t *feature,
if (reg)
{
lib->credmgr->add_set(lib->credmgr, &this->creds->set);
hydra->attributes->add_handler(hydra->attributes,
&this->attr->handler);
}
else
{
lib->credmgr->remove_set(lib->credmgr, &this->creds->set);
hydra->attributes->remove_handler(hydra->attributes,
&this->attr->handler);
if (this->service)
{
this->service->destroy(this->service);
@@ -279,6 +289,7 @@ static void charonservice_init(JNIEnv *env, jobject service, jobject builder)
.get_trusted_certificates = _get_trusted_certificates,
.get_vpnservice_builder = _get_vpnservice_builder,
},
.attr = android_attr_create(),
.creds = android_creds_create(),
.builder = vpnservice_builder_create(builder),
.vpn_service = (*env)->NewGlobalRef(env, service),
@@ -301,6 +312,7 @@ static void charonservice_deinit(JNIEnv *env)
this->builder->destroy(this->builder);
this->creds->destroy(this->creds);
this->attr->destroy(this->attr);
(*env)->DeleteGlobalRef(env, this->vpn_service);
free(this);
charonservice = NULL;