- fixed socket code, so we know on which address we receive traffic
[strongswan.git] / Source / charon / network / socket.c
1 /**
2 * @file socket.c
3 *
4 * @brief Implementation of socket_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 * Copyright (C) 1998-2002 D. Hugh Redelmeier.
12 * Copyright (C) 1997 Angelos D. Keromytis.
13 *
14 * Some parts of interface lookup code from pluto.
15 *
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the
18 * Free Software Foundation; either version 2 of the License, or (at your
19 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
20 *
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * for more details.
25 */
26
27 #include <pthread.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <fcntl.h>
35 #include <net/if.h>
36 #include <sys/ioctl.h>
37
38 #include "socket.h"
39
40 #include <daemon.h>
41 #include <utils/allocator.h>
42 #include <utils/logger_manager.h>
43
44
45 typedef struct interface_t interface_t;
46
47 /**
48 * An interface on which we listen.
49 */
50 struct interface_t {
51
52 /**
53 * Name of the interface
54 */
55 char name[IFNAMSIZ+1];
56
57 /**
58 * Associated socket
59 */
60 int socket_fd;
61 };
62
63 typedef struct private_socket_t private_socket_t;
64
65 /**
66 * Private data of an socket_t object
67 */
68 struct private_socket_t{
69 /**
70 * public functions
71 */
72 socket_t public;
73
74 /**
75 * Master socket
76 */
77 int master_fd;
78
79 /**
80 * List of all socket to listen
81 */
82 linked_list_t* interfaces;
83
84 /**
85 * logger for this socket
86 */
87 logger_t *logger;
88 };
89
90 /**
91 * implementation of socket_t.receive
92 */
93 status_t receiver(private_socket_t *this, packet_t **packet)
94 {
95 char buffer[MAX_PACKET];
96 chunk_t data;
97 packet_t *pkt = packet_create();
98 host_t *source, *dest;
99 int bytes_read = 0;
100
101 source = host_create(AF_INET, "0.0.0.0", 0);
102 dest = host_create(AF_INET, "0.0.0.0", 0);
103 pkt->set_source(pkt, source);
104 pkt->set_destination(pkt, dest);
105
106 while (bytes_read >= 0)
107 {
108 int max_fd = 1;
109 fd_set readfds;
110 iterator_t *iterator;
111 int oldstate;
112 interface_t *interface;
113
114 /* build fd_set */
115 FD_ZERO(&readfds);
116 iterator = this->interfaces->create_iterator(this->interfaces, TRUE);
117 while (iterator->has_next(iterator))
118 {
119 iterator->current(iterator, (void**)&interface);
120 FD_SET(interface->socket_fd, &readfds);
121 if (interface->socket_fd > max_fd)
122 {
123 max_fd = interface->socket_fd + 1;
124 }
125 }
126 iterator->destroy(iterator);
127
128 /* add packet destroy handler for cancellation, enable cancellation */
129 pthread_cleanup_push((void(*)(void*))pkt->destroy, (void*)pkt);
130 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
131
132 this->logger->log(this->logger, CONTROL|LEVEL1, "waiting on sockets");
133 bytes_read = select(max_fd, &readfds, NULL, NULL, NULL);
134
135 /* reset cancellation, remove packet destroy handler (without executing) */
136 pthread_setcancelstate(oldstate, NULL);
137 pthread_cleanup_pop(0);
138
139 if (bytes_read < 0)
140 {
141 this->logger->log(this->logger, ERROR, "error reading from socket: %s", strerror(errno));
142 continue;
143 }
144
145 /* read on the first nonblocking socket */
146 bytes_read = 0;
147 iterator = this->interfaces->create_iterator(this->interfaces, TRUE);
148 while (iterator->has_next(iterator))
149 {
150 iterator->current(iterator, (void**)&interface);
151 if (FD_ISSET(interface->socket_fd, &readfds))
152 {
153 /* do the read */
154 bytes_read = recvfrom(interface->socket_fd, buffer, MAX_PACKET, 0,
155 source->get_sockaddr(source),
156 source->get_sockaddr_len(source));
157 getsockname(interface->socket_fd, dest->get_sockaddr(dest), dest->get_sockaddr_len(dest));
158 break;
159 }
160 }
161 iterator->destroy(iterator);
162 if (bytes_read > 0)
163 {
164 break;
165 }
166 }
167
168 this->logger->log(this->logger, CONTROL, "received packet from %s:%d",
169 source->get_address(source),
170 source->get_port(source));
171
172 /* fill in packet */
173 data.len = bytes_read;
174 data.ptr = allocator_alloc(data.len);
175 memcpy(data.ptr, buffer, data.len);
176 pkt->set_data(pkt, data);
177
178 /* return packet */
179 *packet = pkt;
180
181 return SUCCESS;
182 }
183
184 /**
185 * implementation of socket_t.send
186 */
187 status_t sender(private_socket_t *this, packet_t *packet)
188 {
189 ssize_t bytes_sent;
190 chunk_t data;
191 host_t *source, *dest;
192
193 source = packet->get_source(packet);
194 dest = packet->get_destination(packet);
195 data = packet->get_data(packet);
196
197 this->logger->log(this->logger, CONTROL, "sending packet to %s:%d",
198 dest->get_address(dest),
199 dest->get_port(dest));
200
201 /* send data */
202 /* TODO: should we send via the interface we received the packet? */
203 bytes_sent = sendto(this->master_fd, data.ptr, data.len, 0,
204 dest->get_sockaddr(dest), *(dest->get_sockaddr_len(dest)));
205
206 if (bytes_sent != data.len)
207 {
208 this->logger->log(this->logger, ERROR, "error writing to socket: %s", strerror(errno));
209 return FAILED;
210 }
211 return SUCCESS;
212 }
213
214 /**
215 * Find all suitable interfaces, bind them and add them to the list
216 */
217 static status_t build_interface_list(private_socket_t *this, u_int16_t port)
218 {
219 int on = TRUE;
220 int i;
221 struct sockaddr_in addr;
222 struct ifconf ifconf;
223 struct ifreq buf[300];
224
225 /* master socket for querying socket for a specific interfaces */
226 this->master_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
227 if (this->master_fd == -1)
228 {
229 this->logger->log(this->logger, ERROR, "could not open IPv4 master socket!");
230 return FAILED;
231 }
232
233 /* allow binding of multiplo sockets */
234 if (setsockopt(this->master_fd, SOL_SOCKET, SO_REUSEADDR, (void*)&on, sizeof(on)) < 0)
235 {
236 this->logger->log(this->logger, ERROR, "unable to set SO_REUSEADDR on master socket!");
237 return FAILED;
238 }
239
240 /* bind the master socket */
241 addr.sin_family = AF_INET;
242 addr.sin_addr.s_addr = INADDR_ANY;
243 addr.sin_port = htons(port);
244 if (bind(this->master_fd,(struct sockaddr*)&addr, sizeof(addr)) < 0)
245 {
246 this->logger->log(this->logger, ERROR, "unable to bind master socket!");
247 return FAILED;
248 }
249
250 /* get all interfaces */
251 ifconf.ifc_len = sizeof(buf);
252 ifconf.ifc_buf = (void*) buf;
253 memset(buf, 0, sizeof(buf));
254 if (ioctl(this->master_fd, SIOCGIFCONF, &ifconf) == -1)
255 {
256 this->logger->log(this->logger, ERROR, "unable to get interfaces!");
257 return FAILED;
258 }
259
260 /* add every interesting interfaces to our interface list */
261 for (i = 0; (i+1) * sizeof(*buf) <= (size_t)ifconf.ifc_len; i++)
262 {
263 struct sockaddr_in *current = (struct sockaddr_in*) &buf[i].ifr_addr;
264 struct ifreq auxinfo;
265 int skt;
266 interface_t *interface;
267
268 if (current->sin_family != AF_INET)
269 {
270 /* ignore all but AF_INET interfaces */
271 continue;
272 }
273
274 /* get auxilary info about socket */
275 memset(&auxinfo, 0, sizeof(auxinfo));
276 memcpy(auxinfo.ifr_name, buf[i].ifr_name, IFNAMSIZ);
277 if (ioctl(this->master_fd, SIOCGIFFLAGS, &auxinfo) == -1)
278 {
279 this->logger->log(this->logger, ERROR, "unable to SIOCGIFFLAGS master socket!");
280 continue;
281 }
282 if (!(auxinfo.ifr_flags & IFF_UP))
283 {
284 /* ignore an interface that isn't up */
285 continue;
286 }
287 if (current->sin_addr.s_addr == 0)
288 {
289 /* ignore unconfigured interfaces */
290 continue;
291 }
292
293 /* set up interface socket */
294 skt = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
295 if (socket < 0)
296 {
297 this->logger->log(this->logger, ERROR, "unable to open interface socket!");
298 continue;
299 }
300 if (setsockopt(skt, SOL_SOCKET, SO_REUSEADDR, (void*)&on, sizeof(on)) < 0)
301 {
302 this->logger->log(this->logger, ERROR, "unable to set SO_REUSEADDR on interface socket!");
303 close(skt);
304 continue;
305 }
306 current->sin_port = htons(port);
307 current->sin_family = AF_INET;
308 if (bind(skt, (struct sockaddr*)current, sizeof(struct sockaddr_in)) < 0)
309 {
310 this->logger->log(this->logger, ERROR, "unable to bind interface socket!");
311 close(skt);
312 continue;
313 }
314
315 /* add socket with interface name to list */
316 interface = allocator_alloc_thing(interface_t);
317 memcpy(interface->name, buf[i].ifr_name, IFNAMSIZ);
318 interface->name[IFNAMSIZ] = '\0';
319 interface->socket_fd = skt;
320 this->interfaces->insert_last(this->interfaces, (void*)interface);
321 }
322
323 if (this->interfaces->get_count(this->interfaces) == 0)
324 {
325 this->logger->log(this->logger, ERROR, "unable to find any usable interface!");
326 return FAILED;
327 }
328 return SUCCESS;
329 }
330
331 /**
332 * implementation of socket_t.destroy
333 */
334 void destroy(private_socket_t *this)
335 {
336 interface_t *interface;
337 while (this->interfaces->remove_last(this->interfaces, (void**)&interface) == SUCCESS)
338 {
339 close(interface->socket_fd);
340 allocator_free(interface);
341 }
342 this->interfaces->destroy(this->interfaces);
343 charon->logger_manager->destroy_logger(charon->logger_manager, this->logger);
344 close(this->master_fd);
345 allocator_free(this);
346 }
347
348 /*
349 * See header for description
350 */
351 socket_t *socket_create(u_int16_t port)
352 {
353 private_socket_t *this = allocator_alloc_thing(private_socket_t);
354
355 /* public functions */
356 this->public.send = (status_t(*)(socket_t*, packet_t*))sender;
357 this->public.receive = (status_t(*)(socket_t*, packet_t**))receiver;
358 this->public.destroy = (void(*)(socket_t*)) destroy;
359
360 this->logger = charon->logger_manager->create_logger(charon->logger_manager, SOCKET, NULL);
361 this->interfaces = linked_list_create();
362
363 if (build_interface_list(this, port) != SUCCESS)
364 {
365 charon->kill(charon, "could not bind any interface!");
366 }
367
368 return (socket_t*)this;
369 }