libhydra: Move kernel interface to libcharon
[strongswan.git] / src / frontends / android / app / src / main / jni / libandroidbridge / backend / android_attr.c
1 /*
2 * Copyright (C) 2012-2013 Tobias Brunner
3 * Copyright (C) 2012 Giuliano Grassi
4 * Copyright (C) 2012 Ralf Sager
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include "android_attr.h"
19 #include "../charonservice.h"
20
21 #include <utils/debug.h>
22 #include <library.h>
23
24 typedef struct private_android_attr_t private_android_attr_t;
25
26 /**
27 * Private data of an android_attr_t object.
28 */
29 struct private_android_attr_t {
30
31 /**
32 * Public interface.
33 */
34 android_attr_t public;
35 };
36
37 METHOD(attribute_handler_t, handle, bool,
38 private_android_attr_t *this, ike_sa_t *ike_sa,
39 configuration_attribute_type_t type, chunk_t data)
40 {
41 vpnservice_builder_t *builder;
42 host_t *dns;
43
44 switch (type)
45 {
46 case INTERNAL_IP4_DNS:
47 dns = host_create_from_chunk(AF_INET, data, 0);
48 break;
49 case INTERNAL_IP6_DNS:
50 dns = host_create_from_chunk(AF_INET6, data, 0);
51 break;
52 default:
53 return FALSE;
54 }
55
56 if (!dns || dns->is_anyaddr(dns))
57 {
58 DESTROY_IF(dns);
59 return FALSE;
60 }
61
62 builder = charonservice->get_vpnservice_builder(charonservice);
63 builder->add_dns(builder, dns);
64 dns->destroy(dns);
65 return TRUE;
66 }
67
68 METHOD(attribute_handler_t, release, void,
69 private_android_attr_t *this, ike_sa_t *ike_sa,
70 configuration_attribute_type_t type, chunk_t data)
71 {
72 /* DNS servers cannot be removed from an existing TUN device */
73 }
74
75 METHOD(enumerator_t, enumerate_dns6, bool,
76 enumerator_t *this, configuration_attribute_type_t *type, chunk_t *data)
77 {
78 *type = INTERNAL_IP6_DNS;
79 *data = chunk_empty;
80 this->enumerate = (void*)return_false;
81 return TRUE;
82 }
83
84 METHOD(enumerator_t, enumerate_dns4, bool,
85 enumerator_t *this, configuration_attribute_type_t *type, chunk_t *data)
86 {
87 *type = INTERNAL_IP4_DNS;
88 *data = chunk_empty;
89 this->enumerate = (void*)_enumerate_dns6;
90 return TRUE;
91 }
92
93 METHOD(attribute_handler_t, create_attribute_enumerator, enumerator_t*,
94 private_android_attr_t *this, ike_sa_t *ike_sa, linked_list_t *vips)
95 {
96 enumerator_t *enumerator;
97
98 INIT(enumerator,
99 .enumerate = (void*)_enumerate_dns4,
100 .destroy = (void*)free,
101 );
102 return enumerator;
103 }
104
105 METHOD(android_attr_t, destroy, void,
106 private_android_attr_t *this)
107 {
108 free(this);
109 }
110
111 /**
112 * Described in header
113 */
114 android_attr_t *android_attr_create()
115 {
116 private_android_attr_t *this;
117
118 INIT(this,
119 .public = {
120 .handler = {
121 .handle = _handle,
122 .release = _release,
123 .create_attribute_enumerator = _create_attribute_enumerator,
124 },
125 .destroy = _destroy,
126 },
127 );
128
129 return &this->public;
130 }