ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/tincd/uml_socket/device.c
Revision: 1.1
Committed: Thu Mar 17 23:59:38 2005 UTC (19 years, 3 months ago) by pcg
Content type: text/plain
Branch: MAIN
CVS Tags: rel-1_9, rel-1_8, rel-2_01, rel-3_0, rel-2_2, rel-2_0, rel-2_21, rel-2_22, rel-2_25, HEAD
Log Message:
*** empty log message ***

File Contents

# Content
1 /*
2 device.c -- UML network socket
3 Copyright (C) 2002-2004 Ivo Timmermans <ivo@tinc-vpn.org>,
4 2002-2004 Guus Sliepen <guus@tinc-vpn.org>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 $Id: device.c 1374 2004-03-21 14:21:22Z guus $
21 */
22
23 #include <time.h>
24 #include <sys/time.h>
25 #include <sys/un.h>
26
27 int device_fd = -1;
28 static int listen_fd = -1;
29 static int request_fd = -1;
30 static int data_fd = -1;
31 static int write_fd = -1;
32 static int state = 0;
33 char *device;
34 char *iface = NULL;
35 char *device_info;
36
37 bool running;
38
39 static int device_total_in = 0;
40 static int device_total_out = 0;
41
42 enum request_type { REQ_NEW_CONTROL };
43
44 static struct request {
45 uint32_t magic;
46 uint32_t version;
47 enum request_type type;
48 struct sockaddr_un sock;
49 } request;
50
51 static struct sockaddr_un data_sun;
52
53 bool setup_device(void)
54 {
55 struct sockaddr_un listen_sun;
56 static const int one = 1;
57 struct {
58 char zero;
59 int pid;
60 int usecs;
61 } name;
62 struct timeval tv;
63
64 cp();
65
66 if(!get_config_string(lookup_config(config_tree, "Device"), &device))
67 return false;
68
69 get_config_string(lookup_config(config_tree, "Interface"), &iface);
70
71 device_info = _("UML network socket");
72
73 if((write_fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) {
74 logger(LOG_ERR, _("Could not open write %s: %s"), device_info, strerror(errno));
75 running = false;
76 return false;
77 }
78
79 setsockopt(write_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
80
81 if(fcntl(write_fd, F_SETFL, O_NONBLOCK) < 0) {
82 logger(LOG_ERR, _("System call `%s' failed: %s"), "fcntl", strerror(errno));
83 running = false;
84 return false;
85 }
86
87 if((data_fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) {
88 logger(LOG_ERR, _("Could not open data %s: %s"), device_info, strerror(errno));
89 running = false;
90 return false;
91 }
92
93 setsockopt(data_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
94
95 if(fcntl(data_fd, F_SETFL, O_NONBLOCK) < 0) {
96 logger(LOG_ERR, _("System call `%s' failed: %s"), "fcntl", strerror(errno));
97 running = false;
98 return false;
99 }
100
101 name.zero = 0;
102 name.pid = getpid();
103 gettimeofday(&tv, NULL);
104 name.usecs = tv.tv_usec;
105 data_sun.sun_family = AF_UNIX;
106 memcpy(&data_sun.sun_path, &name, sizeof name);
107
108 if(bind(data_fd, (struct sockaddr *)&data_sun, sizeof data_sun) < 0) {
109 logger(LOG_ERR, _("Could not bind data %s: %s"), device_info, strerror(errno));
110 running = false;
111 return false;
112 }
113
114 if((listen_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
115 logger(LOG_ERR, _("Could not open %s: %s"), device_info,
116 strerror(errno));
117 return false;
118 }
119
120 setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
121
122 if(fcntl(listen_fd, F_SETFL, O_NONBLOCK) < 0) {
123 logger(LOG_ERR, _("System call `%s' failed: %s"), "fcntl", strerror(errno));
124 return false;
125 }
126
127 listen_sun.sun_family = AF_UNIX;
128 strncpy(listen_sun.sun_path, device, sizeof listen_sun.sun_path);
129 if(bind(listen_fd, (struct sockaddr *)&listen_sun, sizeof listen_sun) < 0) {
130 logger(LOG_ERR, _("Could not bind %s to %s: %s"), device_info, device, strerror(errno));
131 return false;
132 }
133
134 if(listen(listen_fd, 1) < 0) {
135 logger(LOG_ERR, _("Could not listen on %s %s: %s"), device_info, device, strerror(errno));
136 return false;
137 }
138
139 device_fd = listen_fd;
140 state = 0;
141
142 logger(LOG_INFO, _("%s is a %s"), device, device_info);
143
144 if(routing_mode == RMODE_ROUTER)
145 overwrite_mac = true;
146
147 return true;
148 }
149
150 void close_device(void)
151 {
152 cp();
153
154 if(listen_fd >= 0)
155 close(listen_fd);
156
157 if(request_fd >= 0)
158 close(request_fd);
159
160 if(data_fd >= 0)
161 close(data_fd);
162
163 if(write_fd >= 0)
164 close(write_fd);
165
166 unlink(device);
167 }
168
169 bool read_packet(vpn_packet_t *packet)
170 {
171 int lenin;
172
173 cp();
174
175 switch(state) {
176 case 0: {
177 struct sockaddr sa;
178 socklen_t salen = sizeof sa;
179
180 request_fd = accept(listen_fd, &sa, &salen);
181 if(request_fd < 0) {
182 logger(LOG_ERR, _("Could not accept connection to %s %s: %s"), device_info, device, strerror(errno));
183 return false;
184 }
185
186 if(fcntl(listen_fd, F_SETFL, O_NONBLOCK) < 0) {
187 logger(LOG_ERR, _("System call `%s' failed: %s"), "fcntl", strerror(errno));
188 running = false;
189 return false;
190 }
191
192 close(listen_fd);
193 listen_fd = -1;
194 device_fd = request_fd;
195 state = 1;
196
197 return false;
198 }
199
200 case 1: {
201 if((lenin = read(request_fd, &request, sizeof request)) != sizeof request) {
202 logger(LOG_ERR, _("Error while reading request from %s %s: %s"), device_info,
203 device, strerror(errno));
204 running = false;
205 return false;
206 }
207
208 if(request.magic != 0xfeedface || request.version != 3 || request.type != REQ_NEW_CONTROL) {
209 logger(LOG_ERR, _("Unknown magic %x, version %d, request type %d from %s %s"),
210 request.magic, request.version, request.type, device_info, device);
211 running = false;
212 return false;
213 }
214
215 if(connect(write_fd, (sockaddr *)&request.sock, sizeof request.sock) < 0) {
216 logger(LOG_ERR, _("Could not bind write %s: %s"), device_info, strerror(errno));
217 running = false;
218 return false;
219 }
220
221 write(request_fd, &data_sun, sizeof data_sun);
222 device_fd = data_fd;
223
224 logger(LOG_INFO, _("Connection with UML established"));
225
226 state = 2;
227 return false;
228 }
229
230 case 2: {
231 if((lenin = read(data_fd, packet->data, MTU)) <= 0) {
232 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
233 device, strerror(errno));
234 running = false;
235 return false;
236 }
237
238 packet->len = lenin;
239
240 device_total_in += packet->len;
241
242 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"), packet->len,
243 device_info);
244
245 return true;
246 }
247 }
248 }
249
250 bool write_packet(vpn_packet_t *packet)
251 {
252 cp();
253
254 if(state != 2) {
255 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Dropping packet of %d bytes to %s: not connected to UML yet"),
256 packet->len, device_info);
257 return false;
258 }
259
260 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
261 packet->len, device_info);
262
263 if(write(write_fd, packet->data, packet->len) < 0) {
264 if(errno != EINTR && errno != EAGAIN) {
265 logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device, strerror(errno));
266 running = false;
267 }
268
269 return false;
270 }
271
272 device_total_out += packet->len;
273
274 return true;
275 }
276
277 void dump_device_stats(void)
278 {
279 cp();
280
281 logger(LOG_DEBUG, _("Statistics for %s %s:"), device_info, device);
282 logger(LOG_DEBUG, _(" total bytes in: %10d"), device_total_in);
283 logger(LOG_DEBUG, _(" total bytes out: %10d"), device_total_out);
284 }