An Android specific attribute handler installs DNS servers via Builder
[strongswan.git] / src / frontends / android / jni / libandroidbridge / backend / android_attr.c
1 /*
2 * Copyright (C) 2012 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 <hydra.h>
22 #include <debug.h>
23 #include <library.h>
24
25 typedef struct private_android_attr_t private_android_attr_t;
26
27 /**
28 * Private data of an android_attr_t object.
29 */
30 struct private_android_attr_t {
31
32 /**
33 * Public interface.
34 */
35 android_attr_t public;
36 };
37
38 METHOD(attribute_handler_t, handle, bool,
39 private_android_attr_t *this, identification_t *server,
40 configuration_attribute_type_t type, chunk_t data)
41 {
42 vpnservice_builder_t *builder;
43 host_t *dns;
44
45 switch (type)
46 {
47 case INTERNAL_IP4_DNS:
48 dns = host_create_from_chunk(AF_INET, data, 0);
49 break;
50 default:
51 return FALSE;
52 }
53
54 if (!dns || dns->is_anyaddr(dns))
55 {
56 DESTROY_IF(dns);
57 return FALSE;
58 }
59
60 builder = charonservice->get_vpnservice_builder(charonservice);
61 builder->add_dns(builder, dns);
62 dns->destroy(dns);
63 return TRUE;
64 }
65
66 METHOD(attribute_handler_t, release, void,
67 private_android_attr_t *this, identification_t *server,
68 configuration_attribute_type_t type, chunk_t data)
69 {
70 /* DNS servers cannot be removed from an existing TUN device */
71 }
72
73 METHOD(enumerator_t, enumerate_dns, bool,
74 enumerator_t *this, configuration_attribute_type_t *type, chunk_t *data)
75 {
76 *type = INTERNAL_IP4_DNS;
77 *data = chunk_empty;
78 this->enumerate = (void*)return_false;
79 return TRUE;
80 }
81
82 METHOD(attribute_handler_t, create_attribute_enumerator, enumerator_t*,
83 private_android_attr_t *this, identification_t *server, host_t *vip)
84 {
85 enumerator_t *enumerator;
86
87 INIT(enumerator,
88 .enumerate = (void*)_enumerate_dns,
89 .destroy = (void*)free,
90 );
91 return enumerator;
92 }
93
94 METHOD(android_attr_t, destroy, void,
95 private_android_attr_t *this)
96 {
97 free(this);
98 }
99
100 /**
101 * Described in header
102 */
103 android_attr_t *android_attr_create()
104 {
105 private_android_attr_t *this;
106
107 INIT(this,
108 .public = {
109 .handler = {
110 .handle = _handle,
111 .release = _release,
112 .create_attribute_enumerator = _create_attribute_enumerator,
113 },
114 .destroy = _destroy,
115 },
116 );
117
118 return &this->public;
119 }
120