- jobs moved into queues
[strongswan.git] / Source / charon / socket.c
1 /**
2 * @file socket.c
3 *
4 * @brief management of sockets
5 *
6 * receiver reads from here, sender writes to here
7 *
8 */
9
10 /*
11 * Copyright (C) 2005 Jan Hutter, Martin Willi
12 * Hochschule fuer Technik Rapperswil
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
23 */
24
25 #include <pthread.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32
33 #include "socket.h"
34
35 #include <globals.h>
36 #include <utils/allocator.h>
37 #include <utils/logger_manager.h>
38
39 typedef struct private_socket_s private_socket_t;
40
41 struct private_socket_s{
42 /**
43 * public functions
44 */
45 socket_t public;
46
47 /**
48 * currently we only have one socket, maybe more in the future ?
49 */
50 int socket_fd;
51 /**
52 * logger for this socket
53 */
54 logger_t *logger;
55 };
56
57 /**
58 * implementation of socket_t.receive
59 */
60 status_t receiver(private_socket_t *this, packet_t **packet)
61 {
62 char buffer[MAX_PACKET];
63 int oldstate;
64 packet_t *pkt = packet_create();
65
66 /* add packet destroy handler for cancellation, enable cancellation */
67 pthread_cleanup_push((void(*)(void*))pkt->destroy, (void*)pkt);
68 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
69
70 pkt->source = host_create(AF_INET, "0.0.0.0", 0);
71 pkt->destination = host_create(AF_INET, "0.0.0.0", 0);
72
73
74 this->logger->log(this->logger, CONTROL|MORE, "going to read from socket");
75 /* do the read */
76 pkt->data.len = recvfrom(this->socket_fd, buffer, MAX_PACKET, 0,
77 pkt->source->get_sockaddr(pkt->source),
78 pkt->source->get_sockaddr_len(pkt->source));
79
80 /* reset cancellation, remove packet destroy handler (without executing) */
81 pthread_setcancelstate(oldstate, NULL);
82 pthread_cleanup_pop(0);
83
84
85 /* TODO: get senders destination address, using
86 * IP_PKTINFO and recvmsg */
87
88 if (pkt->data.len < 0)
89 {
90 pkt->destroy(pkt);
91 this->logger->log(this->logger, ERROR, "error reading from socket: %s", strerror(errno));
92 return FAILED;
93 }
94
95 this->logger->log(this->logger, CONTROL, "received packet from %s:%d",
96 pkt->source->get_address(pkt->source),
97 pkt->source->get_port(pkt->source));
98
99 /* fill in packet */
100 pkt->data.ptr = allocator_alloc(pkt->data.len);
101 memcpy(pkt->data.ptr, buffer, pkt->data.len);
102
103 /* return packet */
104 *packet = pkt;
105
106 return SUCCESS;
107 }
108
109 /**
110 * implementation of socket_t.send
111 */
112 status_t sender(private_socket_t *this, packet_t *packet)
113 {
114 ssize_t bytes_sent;
115
116
117 this->logger->log(this->logger, CONTROL, "sending packet to %s:%d",
118 packet->destination->get_address(packet->destination),
119 packet->destination->get_port(packet->destination));
120 /* send data */
121 bytes_sent = sendto(this->socket_fd, packet->data.ptr, packet->data.len,
122 0, packet->destination->get_sockaddr(packet->destination),
123 *(packet->destination->get_sockaddr_len(packet->destination)));
124
125 if (bytes_sent != packet->data.len)
126 {
127 this->logger->log(this->logger, ERROR, "error writing to socket: %s", strerror(errno));
128 return FAILED;
129 }
130 return SUCCESS;
131 }
132
133 /**
134 * implementation of socket_t.destroy
135 */
136 status_t destroy(private_socket_t *this)
137 {
138 close(this->socket_fd);
139 global_logger_manager->destroy_logger(global_logger_manager, this->logger);
140 allocator_free(this);
141
142 return SUCCESS;
143 }
144
145 socket_t *socket_create(u_int16_t port)
146 {
147 private_socket_t *this = allocator_alloc_thing(private_socket_t);
148 struct sockaddr_in addr;
149
150 /* public functions */
151 this->public.send = (status_t(*)(socket_t*, packet_t*))sender;
152 this->public.receive = (status_t(*)(socket_t*, packet_t**))receiver;
153 this->public.destroy = (status_t(*)(socket_t*))destroy;
154
155
156 this->logger = global_logger_manager->create_logger(global_logger_manager, SOCKET, NULL);
157 if (this->logger == NULL)
158 {
159 allocator_free(this);
160 return NULL;
161 }
162
163 /* create default ipv4 socket */
164 this->socket_fd = socket(PF_INET, SOCK_DGRAM, 0);
165 if (this->socket_fd < 0)
166 {
167 this->logger->log(this->logger, ERROR, "unable to open socket: %s", strerror(errno));
168 global_logger_manager->destroy_logger(global_logger_manager, this->logger);
169 allocator_free(this);
170 return NULL;
171 }
172
173 /* bind socket to all interfaces */
174 addr.sin_family = AF_INET;
175 addr.sin_addr.s_addr = INADDR_ANY;
176 addr.sin_port = htons(port);
177 if (bind(this->socket_fd,(struct sockaddr*)&addr, sizeof(addr)) < 0)
178 {
179 this->logger->log(this->logger, ERROR, "unable to bind socket to port %d: %s", port, strerror(errno));
180 global_logger_manager->destroy_logger(global_logger_manager, this->logger);
181 allocator_free(this);
182 return NULL;
183 }
184
185 return (socket_t*)this;
186 }