89a6c11a66f2e6f598997bfbddbc27285d46d361
[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 <unistd.h>
30 #include <stdlib.h>
31
32 #include "socket.h"
33
34 #include "utils/allocator.h"
35
36 typedef struct private_socket_s private_socket_t;
37
38 struct private_socket_s{
39 /**
40 * public functions
41 */
42 socket_t public;
43
44 /**
45 * currently we only have one socket, maybe more in the future ?
46 */
47 int socket_fd;
48 };
49
50 /**
51 * implementation of socket_t.receive
52 */
53 status_t receiver(private_socket_t *this, packet_t **packet)
54 {
55 char buffer[MAX_PACKET];
56 int oldstate;
57 packet_t *pkt = packet_create(AF_INET);
58
59
60 /* add packet destroy handler for cancellation, enable cancellation */
61 pthread_cleanup_push((void(*)(void*))pkt->destroy, (void*)pkt);
62 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
63
64 /* do the read */
65 pkt->data.len = recvfrom(this->socket_fd, buffer, MAX_PACKET, 0,
66 &(pkt->source), &(pkt->sockaddr_len));
67
68 /* reset cancellation, remove packet destroy handler (without executing) */
69 pthread_setcancelstate(oldstate, NULL);
70 pthread_cleanup_pop(0);
71
72
73 /* TODO: get senders destination address, using
74 * IP_PKTINFO and recvmsg */
75
76 if (pkt->data.len < 0)
77 {
78 pkt->destroy(pkt);
79 /* TODO: log detailed error */
80 return FAILED;
81 }
82
83 /* fill in packet */
84 pkt->data.ptr = allocator_alloc(pkt->data.len);
85 memcpy(pkt->data.ptr, buffer, pkt->data.len);
86
87 /* return packet */
88 *packet = pkt;
89
90 return SUCCESS;
91 }
92
93 /**
94 * implementation of socket_t.send
95 */
96 status_t sender(private_socket_t *this, packet_t *packet)
97 {
98 ssize_t bytes_sent;
99
100 /* send data */
101 bytes_sent = sendto(this->socket_fd, packet->data.ptr, packet->data.len,
102 0, &(packet->destination), packet->sockaddr_len);
103
104 if (bytes_sent != packet->data.len)
105 {
106 /* TODO: log detailed error */
107 return FAILED;
108 }
109 return SUCCESS;
110 }
111
112 /**
113 * implementation of socket_t.destroy
114 */
115 status_t destroy(private_socket_t *this)
116 {
117 close(this->socket_fd);
118 allocator_free(this);
119
120 return SUCCESS;
121 }
122
123 socket_t *socket_create(u_int16_t port)
124 {
125 private_socket_t *this = allocator_alloc_thing(private_socket_t);
126 struct sockaddr_in addr;
127
128 /* public functions */
129 this->public.send = (status_t(*)(socket_t*, packet_t*))sender;
130 this->public.receive = (status_t(*)(socket_t*, packet_t**))receiver;
131 this->public.destroy = (status_t(*)(socket_t*))destroy;
132
133 /* create default ipv4 socket */
134 this->socket_fd = socket(PF_INET, SOCK_DGRAM, 0);
135 if (this->socket_fd < 0) {
136 allocator_free(this);
137 /* TODO: log detailed error */
138 return NULL;
139 }
140
141 /* bind socket to all interfaces */
142 addr.sin_family = AF_INET;
143 addr.sin_addr.s_addr = INADDR_ANY;
144 addr.sin_port = htons(port);
145 if (bind(this->socket_fd,(struct sockaddr*)&addr, sizeof(addr)) < 0) {
146 allocator_free(this);
147 /* TODO: log detailed error */
148 return NULL;
149 }
150
151 return (socket_t*)this;
152 }