- started implementation of netlink kernel interface
[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 int oldstate;
67 packet_t *pkt = packet_create();
68
69 /* add packet destroy handler for cancellation, enable cancellation */
70 pthread_cleanup_push((void(*)(void*))pkt->destroy, (void*)pkt);
71 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
72
73 pkt->source = host_create(AF_INET, "0.0.0.0", 0);
74 pkt->destination = host_create(AF_INET, "0.0.0.0", 0);
75
76
77 this->logger->log(this->logger, CONTROL|MORE, "going to read from socket");
78 /* do the read */
79 pkt->data.len = recvfrom(this->socket_fd, buffer, MAX_PACKET, 0,
80 pkt->source->get_sockaddr(pkt->source),
81 pkt->source->get_sockaddr_len(pkt->source));
82
83 /* reset cancellation, remove packet destroy handler (without executing) */
84 pthread_setcancelstate(oldstate, NULL);
85 pthread_cleanup_pop(0);
86
87
88 /* TODO: get senders destination address, using
89 * IP_PKTINFO and recvmsg */
90
91 if (pkt->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 pkt->source->get_address(pkt->source),
100 pkt->source->get_port(pkt->source));
101
102 /* fill in packet */
103 pkt->data.ptr = allocator_alloc(pkt->data.len);
104 memcpy(pkt->data.ptr, buffer, pkt->data.len);
105
106 /* return packet */
107 *packet = pkt;
108
109 return SUCCESS;
110 }
111
112 /**
113 * implementation of socket_t.send
114 */
115 status_t sender(private_socket_t *this, packet_t *packet)
116 {
117 ssize_t bytes_sent;
118
119
120 this->logger->log(this->logger, CONTROL, "sending packet to %s:%d",
121 packet->destination->get_address(packet->destination),
122 packet->destination->get_port(packet->destination));
123 /* send data */
124 bytes_sent = sendto(this->socket_fd, packet->data.ptr, packet->data.len,
125 0, packet->destination->get_sockaddr(packet->destination),
126 *(packet->destination->get_sockaddr_len(packet->destination)));
127
128 if (bytes_sent != packet->data.len)
129 {
130 this->logger->log(this->logger, ERROR, "error writing to socket: %s", strerror(errno));
131 return FAILED;
132 }
133 return SUCCESS;
134 }
135
136 /**
137 * implementation of socket_t.destroy
138 */
139 void destroy(private_socket_t *this)
140 {
141 close(this->socket_fd);
142 charon->logger_manager->destroy_logger(charon->logger_manager, this->logger);
143 allocator_free(this);
144 }
145
146 socket_t *socket_create(u_int16_t port)
147 {
148 private_socket_t *this = allocator_alloc_thing(private_socket_t);
149 struct sockaddr_in addr;
150
151 /* public functions */
152 this->public.send = (status_t(*)(socket_t*, packet_t*))sender;
153 this->public.receive = (status_t(*)(socket_t*, packet_t**))receiver;
154 this->public.destroy = (void(*)(socket_t*))destroy;
155
156 this->logger = charon->logger_manager->create_logger(charon->logger_manager, SOCKET, NULL);
157
158 /* create default ipv4 socket */
159 this->socket_fd = socket(PF_INET, SOCK_DGRAM, 0);
160 if (this->socket_fd < 0)
161 {
162 this->logger->log(this->logger, ERROR, "unable to open socket: %s", strerror(errno));
163 charon->logger_manager->destroy_logger(charon->logger_manager, this->logger);
164 allocator_free(this);
165 charon->kill(charon, "socket could not be opened");
166 }
167
168 /* bind socket to all interfaces */
169 addr.sin_family = AF_INET;
170 addr.sin_addr.s_addr = INADDR_ANY;
171 addr.sin_port = htons(port);
172 if (bind(this->socket_fd,(struct sockaddr*)&addr, sizeof(addr)) < 0)
173 {
174 this->logger->log(this->logger, ERROR, "unable to bind socket to port %d: %s", port, strerror(errno));
175 charon->logger_manager->destroy_logger(charon->logger_manager, this->logger);
176 allocator_free(this);
177 charon->kill(charon, "socket could not be opened");
178 }
179
180 return (socket_t*)this;
181 }