Removed strayed code fragment
[strongswan.git] / src / charon / plugins / android / android_handler.c
1 /*
2 * Copyright (C) 2010 Martin Willi
3 * Copyright (C) 2010 Tobias Brunner
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_handler.h"
18
19 #include <utils/linked_list.h>
20
21 #include <cutils/properties.h>
22
23 typedef struct private_android_handler_t private_android_handler_t;
24
25 /**
26 * Private data of an android_handler_t object.
27 */
28 struct private_android_handler_t {
29
30 /**
31 * Public android_handler_t interface.
32 */
33 android_handler_t public;
34
35 /**
36 * List of registered DNS servers
37 */
38 linked_list_t *dns;
39 };
40
41 /**
42 * Struct to store a pair of old and installed DNS servers
43 */
44 typedef struct {
45 /** installed dns server */
46 host_t *dns;
47 /** old dns server */
48 host_t *old;
49 } dns_pair_t;
50
51 /**
52 * Destroy a pair of old and installed DNS servers
53 */
54 void destroy_dns_pair(dns_pair_t *this)
55 {
56 DESTROY_IF(this->dns);
57 DESTROY_IF(this->old);
58 free(this);
59 }
60
61 /**
62 * Filter pairs of DNS servers
63 */
64 bool filter_dns_pair(void *data, dns_pair_t **in, host_t **out)
65 {
66 *out = (*in)->dns;
67 return TRUE;
68 }
69
70 /**
71 * Read DNS server property with a given index
72 */
73 host_t *get_dns_server(int index)
74 {
75 host_t *dns = NULL;
76 char key[10], value[PROPERTY_VALUE_MAX];
77
78 if (snprintf(key, sizeof(key), "net.dns%d", index) >= sizeof(key))
79 {
80 return NULL;
81 }
82
83 if (property_get(key, value, NULL) > 0)
84 {
85 dns = host_create_from_string(value, 0);
86 }
87 return dns;
88 }
89
90 /**
91 * Set DNS server property with a given index
92 */
93 bool set_dns_server(int index, host_t *dns)
94 {
95 char key[10], value[PROPERTY_VALUE_MAX];
96
97 if (snprintf(key, sizeof(key), "net.dns%d", index) >= sizeof(key))
98 {
99 return FALSE;
100 }
101
102 if (dns)
103 {
104 if (snprintf(value, sizeof(value), "%H", dns) >= sizeof(value))
105 {
106 return FALSE;
107 }
108 }
109 else
110 {
111 value[0] = '\0';
112 }
113
114 if (property_set(key, value) != 0)
115 {
116 return FALSE;
117 }
118 return TRUE;
119 }
120
121 METHOD(attribute_handler_t, handle, bool,
122 private_android_handler_t *this, identification_t *id,
123 configuration_attribute_type_t type, chunk_t data)
124 {
125 switch (type)
126 {
127 case INTERNAL_IP4_DNS:
128 {
129 host_t *dns;
130 dns_pair_t *pair;
131 int index;
132
133 dns = host_create_from_chunk(AF_INET, data, 0);
134 if (dns)
135 {
136 pair = malloc_thing(dns_pair_t);
137 pair->dns = dns;
138 index = this->dns->get_count(this->dns) + 1;
139 pair->old = get_dns_server(index);
140 set_dns_server(index, dns);
141 this->dns->insert_last(this->dns, pair);
142 return TRUE;
143 }
144 return FALSE;
145 }
146 default:
147 return FALSE;
148 }
149 }
150
151 METHOD(attribute_handler_t, release, void,
152 private_android_handler_t *this, identification_t *server,
153 configuration_attribute_type_t type, chunk_t data)
154 {
155 if (type == INTERNAL_IP4_DNS)
156 {
157 enumerator_t *enumerator;
158 dns_pair_t *pair;
159 int index;
160
161 enumerator = this->dns->create_enumerator(this->dns);
162 for (index = 1; enumerator->enumerate(enumerator, &pair); index++)
163 {
164 if (chunk_equals(pair->dns->get_address(pair->dns), data))
165 {
166 this->dns->remove_at(this->dns, enumerator);
167 set_dns_server(index, pair->old);
168 destroy_dns_pair(pair);
169 }
170 }
171 enumerator->destroy(enumerator);
172 }
173 }
174
175 METHOD(enumerator_t, enumerate_dns, bool,
176 enumerator_t *this, configuration_attribute_type_t *type, chunk_t *data)
177 {
178 *type = INTERNAL_IP4_DNS;
179 *data = chunk_empty;
180 /* stop enumeration */
181 this->enumerate = (void*)return_false;
182 return TRUE;
183 }
184
185 METHOD(attribute_handler_t, create_attribute_enumerator, enumerator_t *,
186 android_handler_t *this, identification_t *id, host_t *vip)
187 {
188 enumerator_t *enumerator;
189
190 INIT(enumerator,
191 .enumerate = (void*)_enumerate_dns,
192 .destroy = (void*)free,
193 );
194 return enumerator;
195 }
196
197 METHOD(android_handler_t, destroy, void,
198 private_android_handler_t *this)
199 {
200 this->dns->destroy_function(this->dns, (void*)destroy_dns_pair);
201 free(this);
202 }
203
204 /**
205 * See header
206 */
207 android_handler_t *android_handler_create()
208 {
209 private_android_handler_t *this;
210
211 INIT(this,
212 .public = {
213 .handler = {
214 .handle = _handle,
215 .release = _release,
216 .create_attribute_enumerator = _create_attribute_enumerator,
217 },
218 .destroy = _destroy,
219 },
220 .dns = linked_list_create(),
221 );
222
223 return &this->public;
224 }
225