d26f66a9b1bfab1065452b4085cd2f7511e109f5
[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 *
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 <pthread.h>
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30
31 #include "socket.h"
32
33 #include <daemon.h>
34 #include <utils/allocator.h>
35 #include <utils/logger_manager.h>
36
37
38 typedef struct private_socket_t private_socket_t;
39
40 /**
41 * Private data of an socket_t object
42 */
43 struct private_socket_t{
44 /**
45 * public functions
46 */
47 socket_t public;
48
49 /**
50 * currently we only have one socket, maybe more in the future ?
51 */
52 int socket_fd;
53
54 /**
55 * logger for this socket
56 */
57 logger_t *logger;
58 };
59
60 /**
61 * implementation of socket_t.receive
62 */
63 status_t receiver(private_socket_t *this, packet_t **packet)
64 {
65 char buffer[MAX_PACKET];
66 chunk_t data;
67 int oldstate;
68 host_t *source, *dest;
69 packet_t *pkt = packet_create();
70
71 /* add packet destroy handler for cancellation, enable cancellation */
72 pthread_cleanup_push((void(*)(void*))pkt->destroy, (void*)pkt);
73 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
74
75 source = host_create(AF_INET, "0.0.0.0", 0);
76 dest = host_create(AF_INET, "0.0.0.0", 0);
77 pkt->set_source(pkt, source);
78 pkt->set_destination(pkt, dest);
79
80 this->logger->log(this->logger, CONTROL|LEVEL1, "going to read from socket");
81 /* do the read */
82 data.len = recvfrom(this->socket_fd, buffer, MAX_PACKET, 0,
83 source->get_sockaddr(source),
84 source->get_sockaddr_len(source));
85
86 /* reset cancellation, remove packet destroy handler (without executing) */
87 pthread_setcancelstate(oldstate, NULL);
88 pthread_cleanup_pop(0);
89
90
91 if (data.len < 0)
92 {
93 pkt->destroy(pkt);
94 this->logger->log(this->logger, ERROR, "error reading from socket: %s", strerror(errno));
95 return FAILED;
96 }
97
98 this->logger->log(this->logger, CONTROL, "received packet from %s:%d",
99 source->get_address(source),
100 source->get_port(source));
101
102 /* fill in packet */
103 data.ptr = allocator_alloc(data.len);
104 memcpy(data.ptr, buffer, data.len);
105
106 pkt->set_data(pkt, data);
107
108 /* return packet */
109 *packet = pkt;
110
111 return SUCCESS;
112 }
113
114 /**
115 * implementation of socket_t.send
116 */
117 status_t sender(private_socket_t *this, packet_t *packet)
118 {
119 ssize_t bytes_sent;
120 chunk_t data;
121 host_t *source, *dest;
122
123 source = packet->get_source(packet);
124 dest = packet->get_destination(packet);
125 data = packet->get_data(packet);
126
127
128 this->logger->log(this->logger, CONTROL, "sending packet to %s:%d",
129 dest->get_address(dest),
130 dest->get_port(dest));
131 /* send data */
132 bytes_sent = sendto(this->socket_fd, data.ptr, data.len, 0,
133 dest->get_sockaddr(dest), *(dest->get_sockaddr_len(dest)));
134
135 if (bytes_sent != data.len)
136 {
137 this->logger->log(this->logger, ERROR, "error writing to socket: %s", strerror(errno));
138 return FAILED;
139 }
140 return SUCCESS;
141 }
142
143 /**
144 * implementation of socket_t.destroy
145 */
146 void destroy(private_socket_t *this)
147 {
148 close(this->socket_fd);
149 charon->logger_manager->destroy_logger(charon->logger_manager, this->logger);
150 allocator_free(this);
151 }
152
153 socket_t *socket_create(u_int16_t port)
154 {
155 private_socket_t *this = allocator_alloc_thing(private_socket_t);
156 struct sockaddr_in addr;
157
158 /* public functions */
159 this->public.send = (status_t(*)(socket_t*, packet_t*))sender;
160 this->public.receive = (status_t(*)(socket_t*, packet_t**))receiver;
161 this->public.destroy = (void(*)(socket_t*))destroy;
162
163 this->logger = charon->logger_manager->create_logger(charon->logger_manager, SOCKET, NULL);
164
165 /* create default ipv4 socket */
166 this->socket_fd = socket(PF_INET, SOCK_DGRAM, 0);
167 if (this->socket_fd < 0)
168 {
169 this->logger->log(this->logger, ERROR, "unable to open socket: %s", strerror(errno));
170 charon->logger_manager->destroy_logger(charon->logger_manager, this->logger);
171 allocator_free(this);
172 charon->kill(charon, "socket could not be opened");
173 }
174
175 /* bind socket to all interfaces */
176 addr.sin_family = AF_INET;
177 addr.sin_addr.s_addr = INADDR_ANY;
178 addr.sin_port = htons(port);
179 if (bind(this->socket_fd,(struct sockaddr*)&addr, sizeof(addr)) < 0)
180 {
181 this->logger->log(this->logger, ERROR, "unable to bind socket to port %d: %s", port, strerror(errno));
182 charon->logger_manager->destroy_logger(charon->logger_manager, this->logger);
183 allocator_free(this);
184 charon->kill(charon, "socket could not be opened");
185 }
186
187 return (socket_t*)this;
188 }