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