added missing enumerator implementation
[strongswan.git] / src / manager / gateway.c
1 /**
2 * @file gateway.c
3 *
4 * @brief Implementation of gateway_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2007 Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include "gateway.h"
24
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <sys/socket.h>
30 #include <sys/un.h>
31
32 #include <lib/xml.h>
33
34 typedef struct private_gateway_t private_gateway_t;
35
36 /**
37 * private data of gateway
38 */
39 struct private_gateway_t {
40
41 /**
42 * public functions
43 */
44 gateway_t public;
45
46 /**
47 * name of the gateway
48 */
49 char *name;
50
51 /**
52 * host to connect using tcp
53 */
54 host_t *host;
55
56 /**
57 * socket file descriptor, > 0 if connected
58 */
59 int fd;
60 };
61
62 struct sockaddr_un unix_addr = { AF_UNIX, IPSEC_PIDDIR "/charon.xml"};
63
64 /**
65 * establish connection to gateway
66 */
67 static bool connect_(private_gateway_t *this)
68 {
69 int family, len;
70 struct sockaddr *addr;
71
72 if (this->fd >= 0)
73 {
74 close(this->fd);
75 }
76 if (this->host)
77 {
78 family = AF_INET;
79 addr = this->host->get_sockaddr(this->host);
80 len = *this->host->get_sockaddr_len(this->host);
81 }
82 else
83 {
84 family = AF_UNIX;
85 addr = (struct sockaddr*)&unix_addr;
86 len = sizeof(unix_addr);
87 }
88
89 this->fd = socket(family, SOCK_STREAM, 0);
90 if (this->fd < 0)
91 {
92 return FALSE;
93 }
94 if (connect(this->fd, addr, len) != 0)
95 {
96 close(this->fd);
97 this->fd = -1;
98 return FALSE;
99 }
100 return TRUE;
101 }
102
103 /**
104 * Implementation of gateway_t.request.
105 */
106 static char* request(private_gateway_t *this, char *xml)
107 {
108 if (this->fd < 0)
109 {
110 if (!connect_(this))
111 {
112 return NULL;
113 }
114 }
115 while (TRUE)
116 {
117 char buf[8096];
118 ssize_t len;
119
120 len = strlen(xml);
121 if (send(this->fd, xml, len, 0) != len)
122 {
123 return NULL;
124 }
125 len = recv(this->fd, buf, sizeof(buf) - 1, 0);
126 if (len < 0)
127 {
128 return NULL;
129 }
130 if (len == 0)
131 {
132 if (!connect_(this))
133 {
134 return NULL;
135 }
136 continue;
137 }
138 buf[len] = 0;
139 return strdup(buf);
140 }
141 }
142
143 /**
144 * Implementation of gateway_t.query_ikesalist.
145 */
146 static enumerator_t* query_ikesalist(private_gateway_t *this)
147 {
148 char *str, *name, *value;
149 xml_t *xml;
150 enumerator_t *e1, *e2, *e3, *e4 = NULL;
151
152 str = request(this, "<message type=\"request\" id=\"1\">"
153 "<query>"
154 "<ikesalist/>"
155 "</query>"
156 "</message>");
157 if (str == NULL)
158 {
159 return NULL;
160 }
161 xml = xml_create(str);
162 if (xml == NULL)
163 {
164 return NULL;
165 }
166
167 e1 = xml->children(xml);
168 free(str);
169 while (e1->enumerate(e1, &xml, &name, &value))
170 {
171 if (streq(name, "message"))
172 {
173 e2 = xml->children(xml);
174 while (e2->enumerate(e2, &xml, &name, &value))
175 {
176 if (streq(name, "query"))
177 {
178 e3 = xml->children(xml);
179 while (e3->enumerate(e3, &xml, &name, &value))
180 {
181 if (streq(name, "ikesalist"))
182 {
183 e4 = xml->children(xml);
184 e1->destroy(e1);
185 e2->destroy(e2);
186 e3->destroy(e3);
187 return e4;
188 }
189 }
190 e3->destroy(e3);
191 }
192 }
193 e2->destroy(e2);
194 }
195 }
196 e1->destroy(e1);
197 return NULL;
198 }
199
200 /**
201 * Implementation of gateway_t.destroy
202 */
203 static void destroy(private_gateway_t *this)
204 {
205 if (this->fd >= 0)
206 {
207 close(this->fd);
208 }
209 if (this->host) this->host->destroy(this->host);
210 free(this->name);
211 free(this);
212 }
213
214 /**
215 * generic constructor
216 */
217 static private_gateway_t *gateway_create(char *name)
218 {
219 private_gateway_t *this = malloc_thing(private_gateway_t);
220
221 this->public.request = (char*(*)(gateway_t*, char *xml))request;
222 this->public.query_ikesalist = (enumerator_t*(*)(gateway_t*))query_ikesalist;
223 this->public.destroy = (void(*)(gateway_t*))destroy;
224
225 this->name = strdup(name);
226 this->host = NULL;
227 this->fd = -1;
228
229 return this;
230 }
231
232 /**
233 * see header
234 */
235 gateway_t *gateway_create_tcp(char *name, host_t *host)
236 {
237 private_gateway_t *this = gateway_create(name);
238
239 this->host = host;
240
241 return &this->public;
242 }
243
244 /**
245 * see header
246 */
247 gateway_t *gateway_create_unix(char *name)
248 {
249 private_gateway_t *this = gateway_create(name);
250
251 return &this->public;
252 }
253