- updated comments
[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
38 typedef struct {
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 } private_socket_t;
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
57 packet_t *pkt = packet_create(AF_INET);
58
59 /* do the read */
60 pkt->data.len = recvfrom(this->socket_fd, buffer, MAX_PACKET, 0,
61 &(pkt->source), &(pkt->sockaddr_len));
62 /* TODO: get senders destination address, using
63 * IP_PKTINFO and recvmsg */
64
65 if (pkt->data.len < 0)
66 {
67 pkt->destroy(pkt);
68 /* TODO: log detailed error */
69 return FAILED;
70 }
71
72 /* fill in packet */
73 pkt->data.ptr = alloc_bytes(pkt->data.len, "data in packet_t");
74 memcpy(pkt->data.ptr, buffer, pkt->data.len);
75
76 /* return packet */
77 *packet = pkt;
78
79 return SUCCESS;
80 }
81
82 /**
83 * implementation of socket_t.send
84 */
85 status_t sender(private_socket_t *this, packet_t *packet)
86 {
87 ssize_t bytes_sent;
88
89 /* send data */
90 bytes_sent = sendto(this->socket_fd, packet->data.ptr, packet->data.len,
91 0, &(packet->destination), packet->sockaddr_len);
92
93 if (bytes_sent != packet->data.len)
94 {
95 /* TODO: log detailed error */
96 return FAILED;
97 }
98 return SUCCESS;
99 }
100
101 /**
102 * implementation of socket_t.destroy
103 */
104 status_t destroy(private_socket_t *this)
105 {
106 close(this->socket_fd);
107 pfree(this);
108
109 return SUCCESS;
110 }
111
112 socket_t *socket_create(u_int16_t port)
113 {
114 private_socket_t *this = alloc_thing(socket_t, "private_socket_t");
115 struct sockaddr_in addr;
116
117 /* public functions */
118 this->public.send = (status_t(*)(socket_t*, packet_t*))sender;
119 this->public.receive = (status_t(*)(socket_t*, packet_t**))receiver;
120 this->public.destroy = (status_t(*)(socket_t*))destroy;
121
122 /* create default ipv4 socket */
123 this->socket_fd = socket(PF_INET, SOCK_DGRAM, 0);
124 if (this->socket_fd < 0) {
125 pfree(this);
126 /* TODO: log detailed error */
127 return NULL;
128 }
129
130 /* bind socket to all interfaces */
131 addr.sin_family = AF_INET;
132 addr.sin_addr.s_addr = INADDR_ANY;
133 addr.sin_port = htons(port);
134 if (bind(this->socket_fd,(struct sockaddr*)&addr, sizeof(addr)) < 0) {
135 pfree(this);
136 /* TODO: log detailed error */
137 return NULL;
138 }
139
140 return (socket_t*)this;
141 }