2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 #include <sys/types.h>
19 #include <sys/socket.h>
22 #include <sys/socket.h>
27 typedef struct private_gateway_t private_gateway_t
;
30 * private data of gateway
32 struct private_gateway_t
{
45 * host to connect using tcp
50 * socket file descriptor, > 0 if connected
55 * unique id assigned to each xml message
60 struct sockaddr_un unix_addr
= { AF_UNIX
, IPSEC_PIDDIR
"/charon.xml"};
63 * establish connection to gateway
65 static bool connect_(private_gateway_t
*this)
68 struct sockaddr
*addr
;
77 addr
= this->host
->get_sockaddr(this->host
);
78 len
= *this->host
->get_sockaddr_len(this->host
);
83 addr
= (struct sockaddr
*)&unix_addr
;
84 len
= sizeof(unix_addr
);
87 this->fd
= socket(family
, SOCK_STREAM
, 0);
92 if (connect(this->fd
, addr
, len
) != 0)
101 METHOD(gateway_t
, request
, char*,
102 private_gateway_t
*this, char *xml
, ...)
118 len
= vsnprintf(buf
, sizeof(buf
), xml
, args
);
120 if (len
< 0 || len
>= sizeof(buf
))
124 if (send(this->fd
, buf
, len
, 0) != len
)
132 len
= recv(this->fd
, buf
, sizeof(buf
) - 1, 0);
146 METHOD(gateway_t
, query_ikesalist
, enumerator_t
*,
147 private_gateway_t
*this)
149 char *str
, *name
, *value
;
151 enumerator_t
*e1
, *e2
, *e3
, *e4
= NULL
;
153 str
= request(this, "<message type=\"request\" id=\"%d\">"
157 "</message>", this->xmlid
++);
162 xml
= xml_create(str
);
168 e1
= xml
->children(xml
);
170 while (e1
->enumerate(e1
, &xml
, &name
, &value
))
172 if (streq(name
, "message"))
174 e2
= xml
->children(xml
);
175 while (e2
->enumerate(e2
, &xml
, &name
, &value
))
177 if (streq(name
, "query"))
179 e3
= xml
->children(xml
);
180 while (e3
->enumerate(e3
, &xml
, &name
, &value
))
182 if (streq(name
, "ikesalist"))
184 e4
= xml
->children(xml
);
201 METHOD(gateway_t
, query_configlist
, enumerator_t
*,
202 private_gateway_t
*this)
204 char *str
, *name
, *value
;
206 enumerator_t
*e1
, *e2
, *e3
, *e4
= NULL
;
208 str
= request(this, "<message type=\"request\" id=\"%d\">"
212 "</message>", this->xmlid
++);
217 xml
= xml_create(str
);
223 e1
= xml
->children(xml
);
225 while (e1
->enumerate(e1
, &xml
, &name
, &value
))
227 if (streq(name
, "message"))
229 e2
= xml
->children(xml
);
230 while (e2
->enumerate(e2
, &xml
, &name
, &value
))
232 if (streq(name
, "query"))
234 e3
= xml
->children(xml
);
235 while (e3
->enumerate(e3
, &xml
, &name
, &value
))
237 if (streq(name
, "configlist"))
239 e4
= xml
->children(xml
);
257 * create enumerator over control elements children of a control response
259 static enumerator_t
* read_result(private_gateway_t
*this, char *res
)
263 enumerator_t
*e1
, *e2
, *e3
;
269 xml
= xml_create(res
);
274 e1
= xml
->children(xml
);
276 while (e1
->enumerate(e1
, &xml
, &name
, &value
))
278 if (streq(name
, "message"))
280 e2
= xml
->children(xml
);
281 while (e2
->enumerate(e2
, &xml
, &name
, &value
))
283 if (streq(name
, "control"))
285 e3
= xml
->children(xml
);
298 METHOD(gateway_t
, initiate
, enumerator_t
*,
299 private_gateway_t
*this, bool ike
, char *name
)
311 str
= request(this, "<message type=\"request\" id=\"%d\">"
313 "<%ssainitiate>%s</%ssainitiate>"
315 "</message>", this->xmlid
++, kind
, name
, kind
);
316 return read_result(this, str
);
319 METHOD(gateway_t
, terminate
, enumerator_t
*,
320 private_gateway_t
*this, bool ike
, uint32_t id
)
332 str
= request(this, "<message type=\"request\" id=\"%d\">"
334 "<%ssaterminate>%d</%ssaterminate>"
336 "</message>", this->xmlid
++, kind
, id
, kind
);
337 return read_result(this, str
);
340 METHOD(gateway_t
, destroy
, void,
341 private_gateway_t
*this)
347 if (this->host
) this->host
->destroy(this->host
);
353 * generic constructor
355 static private_gateway_t
*gateway_create(char *name
)
357 private_gateway_t
*this;
362 .query_ikesalist
= _query_ikesalist
,
363 .query_configlist
= _query_configlist
,
364 .initiate
= _initiate
,
365 .terminate
= _terminate
,
368 .name
= strdup(name
),
379 gateway_t
*gateway_create_tcp(char *name
, host_t
*host
)
381 private_gateway_t
*this = gateway_create(name
);
385 return &this->public;
391 gateway_t
*gateway_create_unix(char *name
)
393 private_gateway_t
*this = gateway_create(name
);
395 return &this->public;