summaryrefslogtreecommitdiff
path: root/src/libcharon/plugins/android/android_handler.c
blob: a53962f160853f561c241430b5922e55259bc446 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 * Copyright (C) 2010-2011 Tobias Brunner
 * Copyright (C) 2010 Martin Willi
 * 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_handler.h"

#include <utils/linked_list.h>

#include <cutils/properties.h>

typedef struct private_android_handler_t private_android_handler_t;

/**
 * Private data of an android_handler_t object.
 */
struct private_android_handler_t {

	/**
	 * Public android_handler_t interface.
	 */
	android_handler_t public;

	/**
	 * List of registered DNS servers
	 */
	linked_list_t *dns;

	/**
	 * Whether the VPN frontend is used
	 */
	bool frontend;
};

/**
 * Prefixes to be used when installing DNS servers
 */
#define DNS_PREFIX_DEFAULT  "net"
#define DNS_PREFIX_FRONTEND "vpn"

/**
 * Struct to store a pair of old and installed DNS servers
 */
typedef struct {
	/** installed dns server */
	host_t *dns;
	/** old dns server */
	host_t *old;
} dns_pair_t;

/**
 * Destroy a pair of old and installed DNS servers
 */
void destroy_dns_pair(dns_pair_t *this)
{
	DESTROY_IF(this->dns);
	DESTROY_IF(this->old);
	free(this);
}

/**
 * Filter pairs of DNS servers
 */
bool filter_dns_pair(void *data, dns_pair_t **in, host_t **out)
{
	*out = (*in)->dns;
	return TRUE;
}

/**
 * Read DNS server property with a given index
 */
host_t *get_dns_server(private_android_handler_t *this, int index)
{
	host_t *dns = NULL;
	char key[10], value[PROPERTY_VALUE_MAX],
		 *prefix = this->frontend ? DNS_PREFIX_FRONTEND : DNS_PREFIX_DEFAULT;

	if (snprintf(key, sizeof(key), "%s.dns%d", prefix, index) >= sizeof(key))
	{
		return NULL;
	}

	if (property_get(key, value, NULL) > 0)
	{
		dns = host_create_from_string(value, 0);
	}
	return dns;
}

/**
 * Set DNS server property with a given index
 */
bool set_dns_server(private_android_handler_t *this, int index, host_t *dns)
{
	char key[10], value[PROPERTY_VALUE_MAX],
		 *prefix = this->frontend ? DNS_PREFIX_FRONTEND : DNS_PREFIX_DEFAULT;

	if (snprintf(key, sizeof(key), "%s.dns%d", prefix, index) >= sizeof(key))
	{
		return FALSE;
	}

	if (dns)
	{
		if (snprintf(value, sizeof(value), "%H", dns) >= sizeof(value))
		{
			return FALSE;
		}
	}
	else
	{
		value[0] = '\0';
	}

	if (property_set(key, value) != 0)
	{
		return FALSE;
	}
	return TRUE;
}

METHOD(attribute_handler_t, handle, bool,
	private_android_handler_t *this, identification_t *id,
	configuration_attribute_type_t type, chunk_t data)
{
	switch (type)
	{
		case INTERNAL_IP4_DNS:
		{
			host_t *dns;
			dns_pair_t *pair;
			int index;

			dns = host_create_from_chunk(AF_INET, data, 0);
			if (dns)
			{
				pair = malloc_thing(dns_pair_t);
				pair->dns = dns;
				index = this->dns->get_count(this->dns) + 1;
				pair->old = get_dns_server(this, index);
				set_dns_server(this, index, dns);
				this->dns->insert_last(this->dns, pair);
				return TRUE;
			}
			return FALSE;
		}
		default:
			return FALSE;
	}
}

METHOD(attribute_handler_t, release, void,
	private_android_handler_t *this, identification_t *server,
	configuration_attribute_type_t type, chunk_t data)
{
	if (type == INTERNAL_IP4_DNS)
	{
		enumerator_t *enumerator;
		dns_pair_t *pair;
		int index;

		enumerator = this->dns->create_enumerator(this->dns);
		for (index = 1; enumerator->enumerate(enumerator, &pair); index++)
		{
			if (chunk_equals(pair->dns->get_address(pair->dns), data))
			{
				this->dns->remove_at(this->dns, enumerator);
				set_dns_server(this, index, pair->old);
				destroy_dns_pair(pair);
			}
		}
		enumerator->destroy(enumerator);
	}
}

METHOD(enumerator_t, enumerate_dns, bool,
	enumerator_t *this, configuration_attribute_type_t *type, chunk_t *data)
{
	*type = INTERNAL_IP4_DNS;
	*data = chunk_empty;
	/* stop enumeration */
	this->enumerate = (void*)return_false;
	return TRUE;
}

METHOD(attribute_handler_t, create_attribute_enumerator, enumerator_t *,
	android_handler_t *this, identification_t *id, host_t *vip)
{
	enumerator_t *enumerator;

	INIT(enumerator,
		.enumerate = (void*)_enumerate_dns,
		.destroy = (void*)free,
	);
	return enumerator;
}

METHOD(android_handler_t, destroy, void,
	private_android_handler_t *this)
{
	this->dns->destroy_function(this->dns, (void*)destroy_dns_pair);
	free(this);
}

/**
 * See header
 */
android_handler_t *android_handler_create(bool frontend)
{
	private_android_handler_t *this;

	INIT(this,
		.public = {
			.handler = {
				.handle = _handle,
				.release = _release,
				.create_attribute_enumerator = _create_attribute_enumerator,
			},
			.destroy = _destroy,
		},
		.dns = linked_list_create(),
		.frontend = frontend,
	);

	return &this->public;
}