ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/vpn.C
(Generate patch)

Comparing gvpe/src/vpn.C (file contents):
Revision 1.1 by pcg, Wed Apr 2 03:25:17 2003 UTC vs.
Revision 1.2 by pcg, Wed Apr 2 05:15:00 2003 UTC

38 38
39#include "connection.h" 39#include "connection.h"
40#include "util.h" 40#include "util.h"
41#include "vpn.h" 41#include "vpn.h"
42 42
43#if ENABLE_TCP
44# include <map>
45# include <unistd.h>
46# include <fcntl.h>
47# include <sys/poll.h>
48#endif
49
43///////////////////////////////////////////////////////////////////////////// 50/////////////////////////////////////////////////////////////////////////////
44 51
45const char *vpn::script_if_up (int) 52const char *vpn::script_if_up ()
46{ 53{
47 // the tunnel device mtu should be the physical mtu - overhead 54 // the tunnel device mtu should be the physical mtu - overhead
48 // the tricky part is rounding to the cipher key blocksize 55 // the tricky part is rounding to the cipher key blocksize
49 int mtu = conf.mtu - ETH_OVERHEAD - VPE_OVERHEAD - MAX_OVERHEAD; 56 int mtu = conf.mtu - ETH_OVERHEAD - VPE_OVERHEAD - MAX_OVERHEAD;
50 mtu += ETH_OVERHEAD - 6 - 6; // now we have the data portion 57 mtu += ETH_OVERHEAD - 6 - 6; // now we have the data portion
71} 78}
72 79
73int 80int
74vpn::setup () 81vpn::setup ()
75{ 82{
76 sockinfo si;
77
78 si.set (THISNODE);
79
80 udpv4_fd = -1; 83 udpv4_fd = -1;
81 84
82 if (THISNODE->protocols & PROT_UDPv4) 85 if (THISNODE->protocols & PROT_UDPv4)
83 { 86 {
84 udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); 87 udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP);
85 88
86 if (udpv4_fd < 0) 89 if (udpv4_fd < 0)
87 return -1; 90 return -1;
88 91
92 sockinfo si (THISNODE, PROT_UDPv4);
93
89 if (bind (udpv4_fd, si.sav4 (), si.salenv4 ())) 94 if (bind (udpv4_fd, si.sav4 (), si.salenv4 ()))
90 { 95 {
91 slog (L_ERR, _("can't bind udpv4 to %s: %s"), (const char *)si, strerror (errno)); 96 slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno));
92 exit (1); 97 exit (1);
93 } 98 }
94 99
95#ifdef IP_MTU_DISCOVER 100#ifdef IP_MTU_DISCOVER
96 // this I really consider a linux bug. I am neither connected 101 // this I really consider a linux bug. I am neither connected
117 ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto); 122 ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto);
118 123
119 if (ipv4_fd < 0) 124 if (ipv4_fd < 0)
120 return -1; 125 return -1;
121 126
127 sockinfo si (THISNODE, PROT_IPv4);
128
122 if (bind (ipv4_fd, si.sav4 (), si.salenv4 ())) 129 if (bind (ipv4_fd, si.sav4 (), si.salenv4 ()))
123 { 130 {
124 slog (L_ERR, _("can't bind ipv4 socket to %s: %s"), (const char *)si, strerror (errno)); 131 slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno));
125 exit (1); 132 exit (1);
126 } 133 }
127 134
128#ifdef IP_MTU_DISCOVER 135#ifdef IP_MTU_DISCOVER
129 // this I really consider a linux bug. I am neither connected 136 // this I really consider a linux bug. I am neither connected
135 } 142 }
136#endif 143#endif
137 144
138 ipv4_ev_watcher.start (ipv4_fd, POLLIN); 145 ipv4_ev_watcher.start (ipv4_fd, POLLIN);
139 } 146 }
147
148#if ENABLE_TCP
149 if (THISNODE->protocols & PROT_TCPv4)
150 {
151 tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
152
153 if (tcpv4_fd < 0)
154 return -1;
155
156 sockinfo si (THISNODE, PROT_TCPv4);
157
158 if (bind (tcpv4_fd, si.sav4 (), si.salenv4 ()))
159 {
160 slog (L_ERR, _("can't bind tcpv4 on %s: %s"), (const char *)si, strerror (errno));
161 exit (1);
162 }
163
164 if (listen (tcpv4_fd, 5))
165 {
166 slog (L_ERR, _("can't listen tcpv4 on %s: %s"), (const char *)si, strerror (errno));
167 exit (1);
168 }
169
170 // standard daemon practise...
171 {
172 int oval = 1;
173 setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval);
174 }
175
176 tcpv4_accept_watcher.start (tcpv4_fd, POLLIN);
177 }
178#endif
140 179
141 tap = new tap_device (); 180 tap = new tap_device ();
142 if (!tap) //D this, of course, never catches 181 if (!tap) //D this, of course, never catches
143 { 182 {
144 slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); 183 slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname);
200 c->recv_vpn_packet (pkt, rsi); 239 c->recv_vpn_packet (pkt, rsi);
201 } 240 }
202} 241}
203 242
204void 243void
205vpn::udpv4_ev (short revents) 244vpn::udpv4_ev (int fd, short revents)
206{ 245{
207 if (revents & (POLLIN | POLLERR)) 246 if (revents & (POLLIN | POLLERR))
208 { 247 {
209 vpn_packet *pkt = new vpn_packet; 248 vpn_packet *pkt = new vpn_packet;
210 struct sockaddr_in sa; 249 struct sockaddr_in sa;
211 socklen_t sa_len = sizeof (sa); 250 socklen_t sa_len = sizeof (sa);
212 int len; 251 int len;
213 252
214 len = recvfrom (udpv4_fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); 253 len = recvfrom (fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
215 254
216 sockinfo si(sa); 255 sockinfo si(sa);
217 256
218 if (len > 0) 257 if (len > 0)
219 { 258 {
243 exit (1); 282 exit (1);
244 } 283 }
245} 284}
246 285
247void 286void
248vpn::ipv4_ev (short revents) 287vpn::ipv4_ev (int fd, short revents)
249{ 288{
250 if (revents & (POLLIN | POLLERR)) 289 if (revents & (POLLIN | POLLERR))
251 { 290 {
252 vpn_packet *pkt = new vpn_packet; 291 vpn_packet *pkt = new vpn_packet;
253 struct sockaddr_in sa; 292 struct sockaddr_in sa;
254 socklen_t sa_len = sizeof (sa); 293 socklen_t sa_len = sizeof (sa);
255 int len; 294 int len;
256 295
257 len = recvfrom (ipv4_fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); 296 len = recvfrom (fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
258 297
259 sockinfo si(sa, PROT_IPv4); 298 sockinfo si(sa, PROT_IPv4);
260 299
261 if (len > 0) 300 if (len > 0)
262 { 301 {
289 revents); 328 revents);
290 exit (1); 329 exit (1);
291 } 330 }
292} 331}
293 332
333#if ENABLE_TCP
334
335struct tcp_info {
336 int fd;
337 bool ok;
338 io_watcher r;
339
340 tcp_info (vpn *v)
341 : r(v, &vpn::tcpv4_ev)
342 {
343 fd = -1;
344 }
345
346 ~tcp_info () { close (fd); }
347};
348
349typedef map<sockinfo, tcp_info *> tcp_si_map;
350typedef map<int, tcp_info *> tcp_fd_map; // unneecssary if iom would be cooler
351static tcp_si_map tcp_si;
352static tcp_fd_map tcp_fd;
353
294void 354void
355vpn::send_tcpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
356{
357 tcp_si_map::iterator info = tcp_si.find (si);
358
359 if (info == tcp_si.end ())
360 {
361 // woaw, the first lost packet ;)
362 tcp_info *i = new tcp_info (this);
363
364 i->ok = false;
365 i->fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
366
367 if (i->fd >= 0)
368 {
369 fcntl (i->fd, F_SETFL, O_NONBLOCK);
370
371 if (connect (i->fd, si.sav4 (), si.salenv4 ()) >= 0
372 || errno == EINPROGRESS)
373 {
374 tcp_si.insert (tcp_si_map::value_type (si, i));
375 tcp_fd.insert (tcp_fd_map::value_type (i->fd, i));
376 return;
377 }
378
379 delete i;
380 }
381 }
382 else
383 {
384 tcp_info *i = info->second;
385
386 if (i->ok)
387 {
388 setsockopt (i->fd, SOL_IP, IP_TOS, &tos, sizeof tos);
389
390 if (write (i->fd, (void *)pkt, pkt->len + sizeof (u32)) != pkt->len + sizeof (u32))
391 {
392 // error, close socket, forget it and retry immediately
393 tcp_si.erase (info);
394 tcp_fd.erase (i->fd);
395 delete i;
396
397 // tail recursion is... better than goto(?)
398 send_tcpv4_packet (pkt, si, tos);
399 }
400 }
401 }
402
403#if 0
404 setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos);
405 sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ());
406#endif
407}
408
409void
410vpn::tcpv4_accept (int fd, short revents)
411{
412 if (revents & (POLLIN | POLLERR))
413 {
414 struct sockaddr_in sa;
415 socklen_t sa_len = sizeof (sa);
416 int len;
417
418 fd = accept (fd, (sockaddr *)&sa, &sa_len);
419
420 if (fd >= 0)
421 {
422 fcntl (fd, F_SETFL, O_NONBLOCK);
423
424 sockinfo si(sa, PROT_TCPv4);
425 tcp_info *i = new tcp_info (this);
426
427 i->fd = fd;
428 i->ok = true;
429 i->r.start (fd, POLLIN);
430
431 tcp_si.insert (tcp_si_map::value_type (si, i));
432 tcp_fd.insert (tcp_fd_map::value_type (fd, i));
433 }
434 }
435}
436
437void
438vpn::tcpv4_ev (int fd, short revents)
439{
440 if (revents & (POLLIN | POLLERR))
441 {
442 tcp_fd_map::iterator info = tcp_fd.find (fd);
443
444 if (info != tcp_fd.end ())
445 {
446 tcp_info *i = info->second;
447
448 if (!i->ok) // not yet established
449 {
450 i->ok = true;
451 fcntl (i->fd, F_SETFL, 0);
452 }
453
454 u32 len;
455
456 if (sizeof (len) == read (fd, &len, sizeof (len)))
457 {
458 slog (L_ERR, "%d bytes received\n", len);
459 }
460
461 //tcp_si.erase (i);
462 tcp_fd.erase (fd);
463 delete i;
464 }
465 }
466}
467
468#endif
469
470void
295vpn::tap_ev (short revents) 471vpn::tap_ev (int fd, short revents)
296{ 472{
297 if (revents & POLLIN) 473 if (revents & POLLIN)
298 { 474 {
299 /* process data */ 475 /* process data */
300 tap_packet *pkt; 476 tap_packet *pkt;
468 644
469 slog (L_NOTICE, _("END status dump")); 645 slog (L_NOTICE, _("END status dump"));
470} 646}
471 647
472vpn::vpn (void) 648vpn::vpn (void)
649: event(this, &vpn::event_cb)
473: udpv4_ev_watcher(this, &vpn::udpv4_ev) 650, udpv4_ev_watcher(this, &vpn::udpv4_ev)
474, ipv4_ev_watcher(this, &vpn::ipv4_ev) 651, ipv4_ev_watcher (this, &vpn::ipv4_ev)
475, tap_ev_watcher(this, &vpn::tap_ev) 652, tap_ev_watcher (this, &vpn::tap_ev)
476, event(this, &vpn::event_cb) 653#if ENABLE_TCP
654, tcpv4_accept_watcher(this, &vpn::tcpv4_accept)
655#endif
477{ 656{
478} 657}
479 658
480vpn::~vpn () 659vpn::~vpn ()
481{ 660{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines