645b3fa9b44533c522f44b726e4fa04b5cca36a7
[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 <hydra.h>
22 #include <utils/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, ike_sa_t *ike_sa,
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 case INTERNAL_IP6_DNS:
51 dns = host_create_from_chunk(AF_INET6, data, 0);
52 break;
53 default:
54 return FALSE;
55 }
56
57 if (!dns || dns->is_anyaddr(dns))
58 {
59 DESTROY_IF(dns);
60 return FALSE;
61 }
62
63 builder = charonservice->get_vpnservice_builder(charonservice);
64 builder->add_dns(builder, dns);
65 dns->destroy(dns);
66 return TRUE;
67 }
68
69 METHOD(attribute_handler_t, release, void,
70 private_android_attr_t *this, ike_sa_t *ike_sa,
71 configuration_attribute_type_t type, chunk_t data)
72 {
73 /* DNS servers cannot be removed from an existing TUN device */
74 }
75
76 METHOD(enumerator_t, enumerate_dns6, bool,
77 enumerator_t *this, configuration_attribute_type_t *type, chunk_t *data)
78 {
79 *type = INTERNAL_IP6_DNS;
80 *data = chunk_empty;
81 this->enumerate = (void*)return_false;
82 return TRUE;
83 }
84
85 METHOD(enumerator_t, enumerate_dns4, bool,
86 enumerator_t *this, configuration_attribute_type_t *type, chunk_t *data)
87 {
88 *type = INTERNAL_IP4_DNS;
89 *data = chunk_empty;
90 this->enumerate = (void*)_enumerate_dns6;
91 return TRUE;
92 }
93
94 METHOD(attribute_handler_t, create_attribute_enumerator, enumerator_t*,
95 private_android_attr_t *this, ike_sa_t *ike_sa, linked_list_t *vips)
96 {
97 enumerator_t *enumerator;
98
99 INIT(enumerator,
100 .enumerate = (void*)_enumerate_dns4,
101 .destroy = (void*)free,
102 );
103 return enumerator;
104 }
105
106 METHOD(android_attr_t, destroy, void,
107 private_android_attr_t *this)
108 {
109 free(this);
110 }
111
112 /**
113 * Described in header
114 */
115 android_attr_t *android_attr_create()
116 {
117 private_android_attr_t *this;
118
119 INIT(this,
120 .public = {
121 .handler = {
122 .handle = _handle,
123 .release = _release,
124 .create_attribute_enumerator = _create_attribute_enumerator,
125 },
126 .destroy = _destroy,
127 },
128 );
129
130 return &this->public;
131 }