Removed backend for old Android frontend patch
[strongswan.git] / src / libcharon / plugins / android_dns / android_dns_handler.c
1 /*
2 * Copyright (C) 2010-2013 Tobias Brunner
3 * Copyright (C) 2010 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "android_dns_handler.h"
18
19 #include <networking/host.h>
20 #include <collections/linked_list.h>
21
22 #include <cutils/properties.h>
23
24 typedef struct private_android_dns_handler_t private_android_dns_handler_t;
25
26 /**
27 * Private data of an android_dns_handler_t object.
28 */
29 struct private_android_dns_handler_t {
30
31 /**
32 * Public interface
33 */
34 android_dns_handler_t public;
35
36 /**
37 * List of registered DNS servers
38 */
39 linked_list_t *dns;
40 };
41
42 /**
43 * Prefix to be used when installing DNS servers
44 */
45 #define DNS_PREFIX_DEFAULT "net"
46
47 /**
48 * Struct to store a pair of old and installed DNS servers
49 */
50 typedef struct {
51 /** installed dns server */
52 host_t *dns;
53 /** old dns server */
54 host_t *old;
55 } dns_pair_t;
56
57 /**
58 * Destroy a pair of old and installed DNS servers
59 */
60 static void destroy_dns_pair(dns_pair_t *this)
61 {
62 DESTROY_IF(this->dns);
63 DESTROY_IF(this->old);
64 free(this);
65 }
66
67 /**
68 * Filter pairs of DNS servers
69 */
70 static bool filter_dns_pair(void *data, dns_pair_t **in, host_t **out)
71 {
72 *out = (*in)->dns;
73 return TRUE;
74 }
75
76 /**
77 * Read DNS server property with a given index
78 */
79 static host_t *get_dns_server(private_android_dns_handler_t *this, int index)
80 {
81 host_t *dns = NULL;
82 char key[10], value[PROPERTY_VALUE_MAX],
83 *prefix = DNS_PREFIX_DEFAULT;
84
85 if (snprintf(key, sizeof(key), "%s.dns%d", prefix, index) >= sizeof(key))
86 {
87 return NULL;
88 }
89
90 if (property_get(key, value, NULL) > 0)
91 {
92 dns = host_create_from_string(value, 0);
93 }
94 return dns;
95 }
96
97 /**
98 * Set DNS server property with a given index
99 */
100 static bool set_dns_server(private_android_dns_handler_t *this, int index,
101 host_t *dns)
102 {
103 char key[10], value[PROPERTY_VALUE_MAX],
104 *prefix = DNS_PREFIX_DEFAULT;
105
106 if (snprintf(key, sizeof(key), "%s.dns%d", prefix, index) >= sizeof(key))
107 {
108 return FALSE;
109 }
110
111 if (dns)
112 {
113 if (snprintf(value, sizeof(value), "%H", dns) >= sizeof(value))
114 {
115 return FALSE;
116 }
117 }
118 else
119 {
120 value[0] = '\0';
121 }
122
123 if (property_set(key, value) != 0)
124 {
125 return FALSE;
126 }
127 return TRUE;
128 }
129
130 METHOD(attribute_handler_t, handle, bool,
131 private_android_dns_handler_t *this, identification_t *id,
132 configuration_attribute_type_t type, chunk_t data)
133 {
134 switch (type)
135 {
136 case INTERNAL_IP4_DNS:
137 {
138 host_t *dns;
139 dns_pair_t *pair;
140 int index;
141
142 dns = host_create_from_chunk(AF_INET, data, 0);
143 if (dns)
144 {
145 pair = malloc_thing(dns_pair_t);
146 pair->dns = dns;
147 index = this->dns->get_count(this->dns) + 1;
148 pair->old = get_dns_server(this, index);
149 set_dns_server(this, index, dns);
150 this->dns->insert_last(this->dns, pair);
151 return TRUE;
152 }
153 return FALSE;
154 }
155 default:
156 return FALSE;
157 }
158 }
159
160 METHOD(attribute_handler_t, release, void,
161 private_android_dns_handler_t *this, identification_t *server,
162 configuration_attribute_type_t type, chunk_t data)
163 {
164 if (type == INTERNAL_IP4_DNS)
165 {
166 enumerator_t *enumerator;
167 dns_pair_t *pair;
168 int index;
169
170 enumerator = this->dns->create_enumerator(this->dns);
171 for (index = 1; enumerator->enumerate(enumerator, &pair); index++)
172 {
173 if (chunk_equals(pair->dns->get_address(pair->dns), data))
174 {
175 this->dns->remove_at(this->dns, enumerator);
176 set_dns_server(this, index, pair->old);
177 destroy_dns_pair(pair);
178 }
179 }
180 enumerator->destroy(enumerator);
181 }
182 }
183
184 METHOD(enumerator_t, enumerate_dns, bool,
185 enumerator_t *this, configuration_attribute_type_t *type, chunk_t *data)
186 {
187 *type = INTERNAL_IP4_DNS;
188 *data = chunk_empty;
189 /* stop enumeration */
190 this->enumerate = (void*)return_false;
191 return TRUE;
192 }
193
194 METHOD(attribute_handler_t, create_attribute_enumerator, enumerator_t *,
195 private_android_dns_handler_t *this, identification_t *id,
196 linked_list_t *vips)
197 {
198 enumerator_t *enumerator;
199
200 INIT(enumerator,
201 .enumerate = (void*)_enumerate_dns,
202 .destroy = (void*)free,
203 );
204 return enumerator;
205 }
206
207 METHOD(android_dns_handler_t, destroy, void,
208 private_android_dns_handler_t *this)
209 {
210 this->dns->destroy_function(this->dns, (void*)destroy_dns_pair);
211 free(this);
212 }
213
214 /**
215 * See header
216 */
217 android_dns_handler_t *android_dns_handler_create()
218 {
219 private_android_dns_handler_t *this;
220
221 INIT(this,
222 .public = {
223 .handler = {
224 .handle = _handle,
225 .release = _release,
226 .create_attribute_enumerator = _create_attribute_enumerator,
227 },
228 .destroy = _destroy,
229 },
230 .dns = linked_list_create(),
231 );
232
233 return &this->public;
234 }
235