4 * @brief Implementation of gateway_t.
9 * Copyright (C) 2007 Martin Willi
10 * Hochschule fuer Technik Rapperswil
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>.
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
25 #include <sys/types.h>
26 #include <sys/socket.h>
29 #include <sys/socket.h>
34 typedef struct private_gateway_t private_gateway_t
;
37 * private data of gateway
39 struct private_gateway_t
{
52 * host to connect using tcp
57 * socket file descriptor, > 0 if connected
62 struct sockaddr_un unix_addr
= { AF_UNIX
, IPSEC_PIDDIR
"/charon.xml"};
65 * establish connection to gateway
67 static bool connect_(private_gateway_t
*this)
70 struct sockaddr
*addr
;
79 addr
= this->host
->get_sockaddr(this->host
);
80 len
= *this->host
->get_sockaddr_len(this->host
);
85 addr
= (struct sockaddr
*)&unix_addr
;
86 len
= sizeof(unix_addr
);
89 this->fd
= socket(family
, SOCK_STREAM
, 0);
94 if (connect(this->fd
, addr
, len
) != 0)
104 * Implementation of gateway_t.request.
106 static char* request(private_gateway_t
*this, char *xml
)
121 if (send(this->fd
, xml
, len
, 0) != len
)
125 len
= recv(this->fd
, buf
, sizeof(buf
) - 1, 0);
144 * Implementation of gateway_t.query_ikesalist.
146 static enumerator_t
* query_ikesalist(private_gateway_t
*this)
148 char *str
, *name
, *value
;
150 enumerator_t
*e1
, *e2
, *e3
, *e4
= NULL
;
152 str
= request(this, "<message type=\"request\" id=\"1\">"
161 xml
= xml_create(str
);
167 e1
= xml
->children(xml
);
169 while (e1
->enumerate(e1
, &xml
, &name
, &value
))
171 if (streq(name
, "message"))
173 e2
= xml
->children(xml
);
174 while (e2
->enumerate(e2
, &xml
, &name
, &value
))
176 if (streq(name
, "query"))
178 e3
= xml
->children(xml
);
179 while (e3
->enumerate(e3
, &xml
, &name
, &value
))
181 if (streq(name
, "ikesalist"))
183 e4
= xml
->children(xml
);
201 * Implementation of gateway_t.destroy
203 static void destroy(private_gateway_t
*this)
209 if (this->host
) this->host
->destroy(this->host
);
215 * generic constructor
217 static private_gateway_t
*gateway_create(char *name
)
219 private_gateway_t
*this = malloc_thing(private_gateway_t
);
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
;
225 this->name
= strdup(name
);
235 gateway_t
*gateway_create_tcp(char *name
, host_t
*host
)
237 private_gateway_t
*this = gateway_create(name
);
241 return &this->public;
247 gateway_t
*gateway_create_unix(char *name
)
249 private_gateway_t
*this = gateway_create(name
);
251 return &this->public;