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

Comparing gvpe/src/connection.C (file contents):
Revision 1.83 by pcg, Mon Sep 1 05:31:29 2008 UTC vs.
Revision 1.96 by root, Thu Mar 24 21:52:48 2011 UTC

1/* 1/*
2 connection.C -- manage a single connection 2 connection.C -- manage a single connection
3 Copyright (C) 2003-2008 Marc Lehmann <gvpe@schmorp.de> 3 Copyright (C) 2003-2008,2010,2011 Marc Lehmann <gvpe@schmorp.de>
4 4
5 This file is part of GVPE. 5 This file is part of GVPE.
6 6
7 GVPE is free software; you can redistribute it and/or modify it 7 GVPE is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the 8 under the terms of the GNU General Public License as published by the
50 50
51#if !HAVE_RAND_PSEUDO_BYTES 51#if !HAVE_RAND_PSEUDO_BYTES
52# define RAND_pseudo_bytes RAND_bytes 52# define RAND_pseudo_bytes RAND_bytes
53#endif 53#endif
54 54
55#define MAGIC "vped\xbd\xc6\xdb\x82" // 8 bytes of magic 55#define MAGIC_OLD "vped\xbd\xc6\xdb\x82" // 8 bytes of magic (still used in the protocol)
56#define MAGIC "gvpe\xbd\xc6\xdb\x82" // 8 bytes of magic (understood but not generated)
56 57
57#define ULTRA_FAST 1 58#define ULTRA_FAST 1
58#define HLOG 15 59#define HLOG 15
59#include "lzf/lzf.h" 60#include "lzf/lzf.h"
60#include "lzf/lzf_c.c" 61#include "lzf/lzf_c.c"
63////////////////////////////////////////////////////////////////////////////// 64//////////////////////////////////////////////////////////////////////////////
64 65
65static std::queue< std::pair<run_script_cb *, const char *> > rs_queue; 66static std::queue< std::pair<run_script_cb *, const char *> > rs_queue;
66static ev::child rs_child_ev; 67static ev::child rs_child_ev;
67 68
69namespace
70{
68void // c++ requires external linkage here, apparently :( 71 void // c++ requires external linkage here, apparently :(
69rs_child_cb (ev::child &w, int revents) 72 rs_child_cb (ev::child &w, int revents)
70{ 73 {
71 w.stop (); 74 w.stop ();
72 75
73 if (rs_queue.empty ()) 76 if (rs_queue.empty ())
74 return; 77 return;
75 78
76 pid_t pid = run_script (*rs_queue.front ().first, false); 79 pid_t pid = run_script (*rs_queue.front ().first, false);
77 if (pid) 80 if (pid)
78 { 81 {
79 w.set (pid); 82 w.set (pid);
80 w.start (); 83 w.start ();
81 } 84 }
82 else 85 else
83 slog (L_WARN, rs_queue.front ().second); 86 slog (L_WARN, rs_queue.front ().second);
84 87
85 delete rs_queue.front ().first; 88 delete rs_queue.front ().first;
86 rs_queue.pop (); 89 rs_queue.pop ();
87} 90 }
91};
88 92
89// despite the fancy name, this is quite a hack 93// despite the fancy name, this is quite a hack
90static void 94static void
91run_script_queued (run_script_cb *cb, const char *warnmsg) 95run_script_queued (run_script_cb *cb, const char *warnmsg)
92{ 96{
128rsa_hash (const rsaid &id, const rsachallenge &chg, rsaresponse &h) 132rsa_hash (const rsaid &id, const rsachallenge &chg, rsaresponse &h)
129{ 133{
130 EVP_MD_CTX ctx; 134 EVP_MD_CTX ctx;
131 135
132 EVP_MD_CTX_init (&ctx); 136 EVP_MD_CTX_init (&ctx);
133 require (EVP_DigestInit (&ctx, RSA_HASH)); 137 require (EVP_DigestInit (&ctx, RSA_HASH));
134 require (EVP_DigestUpdate(&ctx, &chg, sizeof chg)); 138 require (EVP_DigestUpdate (&ctx, &chg, sizeof chg));
135 require (EVP_DigestUpdate(&ctx, &id, sizeof id)); 139 require (EVP_DigestUpdate (&ctx, &id, sizeof id));
136 require (EVP_DigestFinal (&ctx, (unsigned char *)&h, 0)); 140 require (EVP_DigestFinal (&ctx, (unsigned char *)&h, 0));
137 EVP_MD_CTX_cleanup (&ctx); 141 EVP_MD_CTX_cleanup (&ctx);
138} 142}
139 143
140struct rsa_entry 144struct rsa_entry
141{ 145{
190 cleaner.set (RSA_TTL, RSA_TTL); 194 cleaner.set (RSA_TTL, RSA_TTL);
191 } 195 }
192 196
193} rsa_cache; 197} rsa_cache;
194 198
199void
195void rsa_cache::cleaner_cb (ev::timer &w, int revents) 200rsa_cache::cleaner_cb (ev::timer &w, int revents)
196{ 201{
197 if (empty ()) 202 if (empty ())
198 w.stop (); 203 w.stop ();
199 else 204 else
200 { 205 {
225 delete p; 230 delete p;
226 231
227 delete [] queue; 232 delete [] queue;
228} 233}
229 234
235void
230void pkt_queue::expire_cb (ev::timer &w, int revents) 236pkt_queue::expire_cb (ev::timer &w, int revents)
231{ 237{
232 ev_tstamp expire = ev_now () - max_ttl; 238 ev_tstamp expire = ev_now () - max_ttl;
233 239
234 for (;;) 240 for (;;)
235 { 241 {
246 252
247 delete get (); 253 delete get ();
248 } 254 }
249} 255}
250 256
257void
251void pkt_queue::put (net_packet *p) 258pkt_queue::put (net_packet *p)
252{ 259{
253 ev_tstamp now = ev_now (); 260 ev_tstamp now = ev_now ();
254 261
255 // start expiry timer 262 // start expiry timer
256 if (empty ()) 263 if (empty ())
265 queue[i].tstamp = now; 272 queue[i].tstamp = now;
266 273
267 i = ni; 274 i = ni;
268} 275}
269 276
270net_packet *pkt_queue::get () 277net_packet *
278pkt_queue::get ()
271{ 279{
272 if (empty ()) 280 if (empty ())
273 return 0; 281 return 0;
274 282
275 net_packet *p = queue[j].pkt; 283 net_packet *p = queue[j].pkt;
299 307
300 bool can (const sockinfo &si) { return can((u32)si.host); } 308 bool can (const sockinfo &si) { return can((u32)si.host); }
301 bool can (u32 host); 309 bool can (u32 host);
302}; 310};
303 311
304net_rate_limiter auth_rate_limiter, reset_rate_limiter; 312static net_rate_limiter auth_rate_limiter, reset_rate_limiter;
305 313
314bool
306bool net_rate_limiter::can (u32 host) 315net_rate_limiter::can (u32 host)
307{ 316{
308 iterator i; 317 iterator i;
309 318
310 for (i = begin (); i != end (); ) 319 for (i = begin (); i != end (); )
311 if (i->host == host) 320 if (i->host == host)
358 367
359///////////////////////////////////////////////////////////////////////////// 368/////////////////////////////////////////////////////////////////////////////
360 369
361unsigned char hmac_packet::hmac_digest[EVP_MAX_MD_SIZE]; 370unsigned char hmac_packet::hmac_digest[EVP_MAX_MD_SIZE];
362 371
372void
363void hmac_packet::hmac_gen (crypto_ctx *ctx) 373hmac_packet::hmac_gen (crypto_ctx *ctx)
364{ 374{
365 unsigned int xlen; 375 unsigned int xlen;
366 376
367 HMAC_CTX *hctx = &ctx->hctx; 377 HMAC_CTX *hctx = &ctx->hctx;
368 378
386 hmac_gen (ctx); 396 hmac_gen (ctx);
387 397
388 return !memcmp (hmac, hmac_digest, HMACLENGTH); 398 return !memcmp (hmac, hmac_digest, HMACLENGTH);
389} 399}
390 400
401void
391void vpn_packet::set_hdr (ptype type_, unsigned int dst) 402vpn_packet::set_hdr (ptype type_, unsigned int dst)
392{ 403{
393 type = type_; 404 type = type_;
394 405
395 int src = THISNODE->id; 406 int src = THISNODE->id;
396 407
401 412
402#define MAXVPNDATA (MAX_MTU - 6 - 6) 413#define MAXVPNDATA (MAX_MTU - 6 - 6)
403#define DATAHDR (sizeof (u32) + RAND_SIZE) 414#define DATAHDR (sizeof (u32) + RAND_SIZE)
404 415
405struct vpndata_packet : vpn_packet 416struct vpndata_packet : vpn_packet
417{
418 u8 data[MAXVPNDATA + DATAHDR]; // seqno
419
420 void setup (connection *conn, int dst, u8 *d, u32 len, u32 seqno);
421 tap_packet *unpack (connection *conn, u32 &seqno);
422
423private:
424 const u32 data_hdr_size () const
406 { 425 {
407 u8 data[MAXVPNDATA + DATAHDR]; // seqno
408
409 void setup (connection *conn, int dst, u8 *d, u32 len, u32 seqno);
410 tap_packet *unpack (connection *conn, u32 &seqno);
411private:
412
413 const u32 data_hdr_size () const
414 {
415 return sizeof (vpndata_packet) - sizeof (net_packet) - MAXVPNDATA - DATAHDR; 426 return sizeof (vpndata_packet) - sizeof (net_packet) - MAXVPNDATA - DATAHDR;
416 }
417 }; 427 }
428};
418 429
419void 430void
420vpndata_packet::setup (connection *conn, int dst, u8 *d, u32 l, u32 seqno) 431vpndata_packet::setup (connection *conn, int dst, u8 *d, u32 l, u32 seqno)
421{ 432{
422 EVP_CIPHER_CTX *cctx = &conn->octx->cctx; 433 EVP_CIPHER_CTX *cctx = &conn->octx->cctx;
424 ptype type = PT_DATA_UNCOMPRESSED; 435 ptype type = PT_DATA_UNCOMPRESSED;
425 436
426#if ENABLE_COMPRESSION 437#if ENABLE_COMPRESSION
427 u8 cdata[MAX_MTU]; 438 u8 cdata[MAX_MTU];
428 439
429 if (conn->features & ENABLE_COMPRESSION) 440 if (conn->features & FEATURE_COMPRESSION)
430 { 441 {
431 u32 cl = lzf_compress (d, l, cdata + 2, (l - 2) & ~7); 442 u32 cl = lzf_compress (d, l, cdata + 2, (l - 2) & ~7);
432 443
433 if (cl) 444 if (cl)
434 { 445 {
561#endif 572#endif
562 return f; 573 return f;
563 } 574 }
564}; 575};
565 576
577void
566void config_packet::setup (ptype type, int dst) 578config_packet::setup (ptype type, int dst)
567{ 579{
568 prot_major = PROTOCOL_MAJOR; 580 prot_major = PROTOCOL_MAJOR;
569 prot_minor = PROTOCOL_MINOR; 581 prot_minor = PROTOCOL_MINOR;
570 randsize = RAND_SIZE; 582 randsize = RAND_SIZE;
571 hmaclen = HMACLENGTH; 583 hmaclen = HMACLENGTH;
579 591
580 len = sizeof (*this) - sizeof (net_packet); 592 len = sizeof (*this) - sizeof (net_packet);
581 set_hdr (type, dst); 593 set_hdr (type, dst);
582} 594}
583 595
596bool
584bool config_packet::chk_config () const 597config_packet::chk_config () const
585{ 598{
586 if (prot_major != PROTOCOL_MAJOR) 599 if (prot_major != PROTOCOL_MAJOR)
587 slog (L_WARN, _("major version mismatch (remote %d <=> local %d)"), prot_major, PROTOCOL_MAJOR); 600 slog (L_WARN, _("major version mismatch (remote %d <=> local %d)"), prot_major, PROTOCOL_MAJOR);
588 else if (randsize != RAND_SIZE) 601 else if (randsize != RAND_SIZE)
589 slog (L_WARN, _("rand size mismatch (remote %d <=> local %d)"), randsize, RAND_SIZE); 602 slog (L_WARN, _("rand size mismatch (remote %d <=> local %d)"), randsize, RAND_SIZE);
613 rsaencrdata encr; 626 rsaencrdata encr;
614 627
615 auth_req_packet (int dst, bool initiate_, u8 protocols_) 628 auth_req_packet (int dst, bool initiate_, u8 protocols_)
616 { 629 {
617 config_packet::setup (PT_AUTH_REQ, dst); 630 config_packet::setup (PT_AUTH_REQ, dst);
618 strncpy (magic, MAGIC, 8); 631 strncpy (magic, MAGIC_OLD, 8);
619 initiate = !!initiate_; 632 initiate = !!initiate_;
620 protocols = protocols_; 633 protocols = protocols_;
621 634
622 len = sizeof (*this) - sizeof (net_packet); 635 len = sizeof (*this) - sizeof (net_packet);
623 } 636 }
818} 831}
819 832
820void 833void
821connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols) 834connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols)
822{ 835{
823 slog (L_TRACE, "%s << PT_CONNECT_INFO(%s,%s)", conf->nodename, 836 slog (L_TRACE, "%s << PT_CONNECT_INFO(%s,%s,p%02x)", conf->nodename,
824 vpn->conns[rid - 1]->conf->nodename, (const char *)rsi); 837 vpn->conns[rid - 1]->conf->nodename, (const char *)rsi,
838 conf->protocols);
825 839
826 connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols); 840 connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols);
827 841
828 r->hmac_set (octx); 842 r->hmac_set (octx);
829 send_vpn_packet (r, si); 843 send_vpn_packet (r, si);
832} 846}
833 847
834inline void 848inline void
835connection::establish_connection_cb (ev::timer &w, int revents) 849connection::establish_connection_cb (ev::timer &w, int revents)
836{ 850{
837 if (!ictx 851 if (!(ictx && octx)
838 && conf != THISNODE 852 && conf != THISNODE
839 && connectmode != conf_node::C_NEVER 853 && connectmode != conf_node::C_NEVER
840 && connectmode != conf_node::C_DISABLED 854 && connectmode != conf_node::C_DISABLED
841 && !w.is_active ()) 855 && !w.is_active ())
842 { 856 {
910 } 924 }
911 } 925 }
912 926
913 delete ictx; ictx = 0; 927 delete ictx; ictx = 0;
914 delete octx; octx = 0; 928 delete octx; octx = 0;
915#if ENABLE_DNS
916 dnsv4_reset_connection ();
917#endif
918 929
919 si.host = 0; 930 si.host = 0;
920 931
921 last_activity = 0.; 932 last_activity = 0.;
922 //last_si_change = 0.; 933 //last_si_change = 0.;
983 data_queue.put (new tap_packet (*pkt)); 994 data_queue.put (new tap_packet (*pkt));
984 post_inject_queue (); 995 post_inject_queue ();
985 } 996 }
986} 997}
987 998
999void
988void connection::inject_vpn_packet (vpn_packet *pkt, int tos) 1000connection::inject_vpn_packet (vpn_packet *pkt, int tos)
989{ 1001{
990 if (ictx && octx) 1002 if (ictx && octx)
991 send_vpn_packet (pkt, si, tos); 1003 send_vpn_packet (pkt, si, tos);
992 else 1004 else
993 { 1005 {
999void 1011void
1000connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) 1012connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi)
1001{ 1013{
1002 last_activity = ev_now (); 1014 last_activity = ev_now ();
1003 1015
1004 slog (L_NOISE, "%s >> received packet type %d from %d to %d.", 1016 slog (L_NOISE, "%s >> received packet type %d from %d to %d.",
1005 conf->nodename, pkt->typ (), pkt->src (), pkt->dst ()); 1017 conf->nodename, pkt->typ (), pkt->src (), pkt->dst ());
1006 1018
1007 if (connectmode == conf_node::C_DISABLED) 1019 if (connectmode == conf_node::C_DISABLED)
1008 return; 1020 return;
1009 1021
1019 { 1031 {
1020 if (auth_rate_limiter.can (rsi)) 1032 if (auth_rate_limiter.can (rsi))
1021 send_auth_request (rsi, true); 1033 send_auth_request (rsi, true);
1022 } 1034 }
1023 else 1035 else
1024 // we would love to change thre socket address here, but ping's aren't 1036 // we would love to change the socket address here, but ping's aren't
1025 // authenticated, so we best ignore it. 1037 // authenticated, so we best ignore it.
1026 send_ping (rsi, 1); // pong 1038 send_ping (rsi, 1); // pong
1027 1039
1028 break; 1040 break;
1029 1041
1030 case vpn_packet::PT_PONG: 1042 case vpn_packet::PT_PONG:
1031 slog (L_TRACE, "%s >> PT_PONG", conf->nodename); 1043 slog (L_TRACE, "%s >> PT_PONG", conf->nodename);
1044
1045 // a PONG might mean that the other side doesn't really know
1046 // about our desire for communication.
1047 establish_connection ();
1032 break; 1048 break;
1033 1049
1034 case vpn_packet::PT_RESET: 1050 case vpn_packet::PT_RESET:
1035 { 1051 {
1036 reset_connection (); 1052 reset_connection ();
1049 break; 1065 break;
1050 1066
1051 case vpn_packet::PT_AUTH_REQ: 1067 case vpn_packet::PT_AUTH_REQ:
1052 if (auth_rate_limiter.can (rsi)) 1068 if (auth_rate_limiter.can (rsi))
1053 { 1069 {
1054 auth_req_packet *p = (auth_req_packet *) pkt; 1070 auth_req_packet *p = (auth_req_packet *)pkt;
1055 1071
1056 slog (L_TRACE, "%s >> PT_AUTH_REQ(%s)", conf->nodename, p->initiate ? "initiate" : "reply"); 1072 slog (L_TRACE, "%s >> PT_AUTH_REQ(%s,p%02x,f%02x)",
1073 conf->nodename, p->initiate ? "initiate" : "reply",
1074 p->protocols, p->features);
1057 1075
1058 if (p->chk_config () && !strncmp (p->magic, MAGIC, 8)) 1076 if (p->chk_config ()
1077 && (!memcmp (p->magic, MAGIC_OLD, 8) || !memcmp (p->magic, MAGIC, 8)))
1059 { 1078 {
1060 if (p->prot_minor != PROTOCOL_MINOR) 1079 if (p->prot_minor != PROTOCOL_MINOR)
1061 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), 1080 slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."),
1062 conf->nodename, (const char *)rsi, 1081 conf->nodename, (const char *)rsi,
1063 PROTOCOL_MINOR, conf->nodename, p->prot_minor); 1082 PROTOCOL_MINOR, conf->nodename, p->prot_minor);
1210 // { 1229 // {
1211 slog (L_INFO, _("%s(%s): changing socket address to %s."), 1230 slog (L_INFO, _("%s(%s): changing socket address to %s."),
1212 conf->nodename, (const char *)si, (const char *)rsi); 1231 conf->nodename, (const char *)si, (const char *)rsi);
1213 1232
1214 si = rsi; 1233 si = rsi;
1234
1235 if (::conf.script_node_change)
1236 {
1237 run_script_cb *cb = new run_script_cb;
1238 cb->set<connection, &connection::script_node_change> (this);
1239 run_script_queued (cb, _("node-change command execution failed, continuing."));
1240 }
1241
1215 // } 1242 // }
1216 //else 1243 //else
1217 // slog (L_INFO, _("%s(%s): accepted packet from %s, not (yet) redirecting traffic."), 1244 // slog (L_INFO, _("%s(%s): accepted packet from %s, not (yet) redirecting traffic."),
1218 // conf->nodename, (const char *)si, (const char *)rsi); 1245 // conf->nodename, (const char *)si, (const char *)rsi);
1219 } 1246 }
1240 break; 1267 break;
1241 1268
1242 case vpn_packet::PT_CONNECT_REQ: 1269 case vpn_packet::PT_CONNECT_REQ:
1243 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) 1270 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx))
1244 { 1271 {
1245 connect_req_packet *p = (connect_req_packet *) pkt; 1272 connect_req_packet *p = (connect_req_packet *)pkt;
1246 1273
1247 if (p->id > 0 && p->id <= vpn->conns.size ()) 1274 if (p->id > 0 && p->id <= vpn->conns.size ())
1248 { 1275 {
1249 connection *c = vpn->conns[p->id - 1]; 1276 connection *c = vpn->conns[p->id - 1];
1250 conf->protocols = p->protocols; 1277 conf->protocols = p->protocols;
1251 1278
1252 slog (L_TRACE, "%s >> PT_CONNECT_REQ(%s) [%d]", 1279 slog (L_TRACE, "%s >> PT_CONNECT_REQ(%s,p%02x) [%d]",
1253 conf->nodename, vpn->conns[p->id - 1]->conf->nodename, c->ictx && c->octx); 1280 conf->nodename, vpn->conns[p->id - 1]->conf->nodename,
1281 p->protocols,
1282 c->ictx && c->octx);
1254 1283
1255 if (c->ictx && c->octx) 1284 if (c->ictx && c->octx)
1256 { 1285 {
1257 // send connect_info packets to both sides, in case one is 1286 // send connect_info packets to both sides, in case one is
1258 // behind a nat firewall (or both ;) 1287 // behind a nat firewall (or both ;)
1281 1310
1282 c->conf->protocols = p->protocols; 1311 c->conf->protocols = p->protocols;
1283 protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); 1312 protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf));
1284 p->si.upgrade_protocol (protocol, c->conf); 1313 p->si.upgrade_protocol (protocol, c->conf);
1285 1314
1286 slog (L_TRACE, "%s >> PT_CONNECT_INFO(%s,%s) [%d]", 1315 slog (L_TRACE, "%s >> PT_CONNECT_INFO(%s,%s,protocols=%02x,protocol=%02x,upgradable=%02x) [%d]",
1316 conf->nodename,
1287 conf->nodename, vpn->conns[p->id - 1]->conf->nodename, 1317 vpn->conns[p->id - 1]->conf->nodename,
1318 (const char *)p->si,
1319 p->protocols,
1320 protocol,
1321 p->si.supported_protocols (c->conf),
1288 (const char *)p->si, !c->ictx && !c->octx); 1322 !c->ictx && !c->octx);
1289 1323
1290 const sockinfo &dsi = forward_si (p->si); 1324 const sockinfo &dsi = forward_si (p->si);
1291 1325
1292 if (dsi.valid ()) 1326 if (dsi.valid ())
1293 c->send_auth_request (dsi, true); 1327 c->send_auth_request (dsi, true);
1328 else
1329 slog (L_INFO, "connect info for %s received (%s), but still unable to contact.",
1330 vpn->conns[p->id - 1]->conf->nodename,
1331 (const char *)p->si);
1294 } 1332 }
1295 else 1333 else
1296 slog (L_WARN, 1334 slog (L_WARN,
1297 _("received authenticated connection request from unknown node #%d, config file mismatch?"), 1335 _("received authenticated connection request from unknown node #%d, config file mismatch?"),
1298 p->id); 1336 p->id);
1328 w.start (last_activity + ::conf.keepalive + 10 - ev::now ()); 1366 w.start (last_activity + ::conf.keepalive + 10 - ev::now ());
1329 else 1367 else
1330 reset_connection (); 1368 reset_connection ();
1331} 1369}
1332 1370
1371void
1333void connection::send_connect_request (int id) 1372connection::send_connect_request (int id)
1334{ 1373{
1335 connect_req_packet *p = new connect_req_packet (conf->id, id, conf->protocols); 1374 connect_req_packet *p = new connect_req_packet (conf->id, id, THISNODE->protocols);
1336 1375
1337 slog (L_TRACE, "%s << PT_CONNECT_REQ(%s)", 1376 slog (L_TRACE, "%s << PT_CONNECT_REQ(%s,p%02x)",
1338 conf->nodename, vpn->conns[id - 1]->conf->nodename); 1377 conf->nodename, vpn->conns[id - 1]->conf->nodename,
1378 THISNODE->protocols);
1339 p->hmac_set (octx); 1379 p->hmac_set (octx);
1340 send_vpn_packet (p, si); 1380 send_vpn_packet (p, si);
1341 1381
1342 delete p; 1382 delete p;
1343} 1383}
1344 1384
1385void
1345void connection::script_init_env (const char *ext) 1386connection::script_init_env (const char *ext)
1346{ 1387{
1347 char *env; 1388 char *env;
1348 asprintf (&env, "IFUPDATA%s=%s", ext, conf->if_up_data); putenv (env); 1389 asprintf (&env, "IFUPDATA%s=%s", ext, conf->if_up_data); putenv (env);
1349 asprintf (&env, "NODENAME%s=%s", ext, conf->nodename); putenv (env); 1390 asprintf (&env, "NODENAME%s=%s", ext, conf->nodename); putenv (env);
1350 asprintf (&env, "MAC%s=%02x:%02x:%02x:%02x:%02x:%02x", ext, 1391 asprintf (&env, "MAC%s=%02x:%02x:%02x:%02x:%02x:%02x", ext,
1351 0xfe, 0xfd, 0x80, 0x00, conf->id >> 8, 1392 0xfe, 0xfd, 0x80, 0x00, conf->id >> 8,
1352 conf->id & 0xff); putenv (env); 1393 conf->id & 0xff); putenv (env);
1353} 1394}
1354 1395
1396void
1355void connection::script_init_connect_env () 1397connection::script_init_connect_env ()
1356{ 1398{
1357 vpn->script_init_env (); 1399 vpn->script_init_env ();
1358 1400
1359 char *env; 1401 char *env;
1360 asprintf (&env, "DESTID=%d", conf->id); putenv (env); 1402 asprintf (&env, "DESTID=%d", conf->id); putenv (env);
1403 asprintf (&env, "DESTSI=%s", (const char *)si); putenv (env);
1361 asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); 1404 asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env);
1362 asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); 1405 asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env);
1363 asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); 1406 asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env);
1364} 1407}
1365 1408
1366inline const char * 1409inline const char *
1367connection::script_node_up () 1410connection::script_node_up ()
1368{ 1411{
1373 char *filename; 1416 char *filename;
1374 asprintf (&filename, 1417 asprintf (&filename,
1375 "%s/%s", 1418 "%s/%s",
1376 confbase, 1419 confbase,
1377 ::conf.script_node_up ? ::conf.script_node_up : "node-up"); 1420 ::conf.script_node_up ? ::conf.script_node_up : "node-up");
1421
1422 return filename;
1423}
1424
1425inline const char *
1426connection::script_node_change ()
1427{
1428 script_init_connect_env ();
1429
1430 putenv ((char *)"STATE=change");
1431
1432 char *filename;
1433 asprintf (&filename,
1434 "%s/%s",
1435 confbase,
1436 ::conf.script_node_change ? ::conf.script_node_change : "node-change");
1378 1437
1379 return filename; 1438 return filename;
1380} 1439}
1381 1440
1382inline const char * 1441inline const char *
1408 establish_connection.set<connection, &connection::establish_connection_cb> (this); 1467 establish_connection.set<connection, &connection::establish_connection_cb> (this);
1409 1468
1410 last_establish_attempt = 0.; 1469 last_establish_attempt = 0.;
1411 octx = ictx = 0; 1470 octx = ictx = 0;
1412 1471
1413 if (!conf->protocols) // make sure some protocol is enabled
1414 conf->protocols = PROT_UDPv4;
1415
1416 connectmode = conf->connectmode; 1472 connectmode = conf->connectmode;
1417 1473
1418 // queue a dummy packet to force an initial connection attempt 1474 // queue a dummy packet to force an initial connection attempt
1419 if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED) 1475 if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED)
1420 vpn_queue.put (new net_packet); 1476 vpn_queue.put (new net_packet);
1425connection::~connection () 1481connection::~connection ()
1426{ 1482{
1427 shutdown (); 1483 shutdown ();
1428} 1484}
1429 1485
1486void
1430void connection_init () 1487connection_init ()
1431{ 1488{
1432 auth_rate_limiter.clear (); 1489 auth_rate_limiter.clear ();
1433 reset_rate_limiter.clear (); 1490 reset_rate_limiter.clear ();
1434} 1491}
1435 1492

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines