- uses new packet with host_t now
[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();
58
59 /* add packet destroy handler for cancellation, enable cancellation */
60 pthread_cleanup_push((void(*)(void*))pkt->destroy, (void*)pkt);
61 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
62
63 pkt->source = host_create(AF_INET, "0.0.0.0", 0);
64
65 /* do the read */
66 pkt->data.len = recvfrom(this->socket_fd, buffer, MAX_PACKET, 0,
67 pkt->source->get_sockaddr(pkt->source),
68 pkt->source->get_sockaddr_len(pkt->source));
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 = allocator_alloc(pkt->data.len);
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->get_sockaddr(packet->destination),
105 *(packet->destination->get_sockaddr_len(packet->destination)));
106
107 if (bytes_sent != packet->data.len)
108 {
109 /* TODO: log detailed error */
110 return FAILED;
111 }
112 return SUCCESS;
113 }
114
115 /**
116 * implementation of socket_t.destroy
117 */
118 status_t destroy(private_socket_t *this)
119 {
120 close(this->socket_fd);
121 allocator_free(this);
122
123 return SUCCESS;
124 }
125
126 socket_t *socket_create(u_int16_t port)
127 {
128 private_socket_t *this = allocator_alloc_thing(private_socket_t);
129 struct sockaddr_in addr;
130
131 /* public functions */
132 this->public.send = (status_t(*)(socket_t*, packet_t*))sender;
133 this->public.receive = (status_t(*)(socket_t*, packet_t**))receiver;
134 this->public.destroy = (status_t(*)(socket_t*))destroy;
135
136 /* create default ipv4 socket */
137 this->socket_fd = socket(PF_INET, SOCK_DGRAM, 0);
138 if (this->socket_fd < 0) {
139 allocator_free(this);
140 /* TODO: log detailed error */
141 return NULL;
142 }
143
144 /* bind socket to all interfaces */
145 addr.sin_family = AF_INET;
146 addr.sin_addr.s_addr = INADDR_ANY;
147 addr.sin_port = htons(port);
148 if (bind(this->socket_fd,(struct sockaddr*)&addr, sizeof(addr)) < 0) {
149 allocator_free(this);
150 /* TODO: log detailed error */
151 return NULL;
152 }
153
154 return (socket_t*)this;
155 }