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