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.63 by pcg, Sun Dec 2 00:45:41 2007 UTC vs.
Revision 1.68 by pcg, Thu Aug 7 17:30:27 2008 UTC

91 rsachallenge chg; 91 rsachallenge chg;
92}; 92};
93 93
94struct rsa_cache : list<rsa_entry> 94struct rsa_cache : list<rsa_entry>
95{ 95{
96 void cleaner_cb (ev::timer &w, int revents); ev::timer cleaner; 96 inline void cleaner_cb (ev::timer &w, int revents); ev::timer cleaner;
97 97
98 bool find (const rsaid &id, rsachallenge &chg) 98 bool find (const rsaid &id, rsachallenge &chg)
99 { 99 {
100 for (iterator i = begin (); i != end (); ++i) 100 for (iterator i = begin (); i != end (); ++i)
101 { 101 {
130 if (!cleaner.is_active ()) 130 if (!cleaner.is_active ())
131 cleaner.again (); 131 cleaner.again ();
132 } 132 }
133 133
134 rsa_cache () 134 rsa_cache ()
135 : cleaner (this, &rsa_cache::cleaner_cb)
136 { 135 {
136 cleaner.set<rsa_cache, &rsa_cache::cleaner_cb> (this);
137 cleaner.set (RSA_TTL, RSA_TTL); 137 cleaner.set (RSA_TTL, RSA_TTL);
138 } 138 }
139 139
140} rsa_cache; 140} rsa_cache;
141 141
153 } 153 }
154} 154}
155 155
156////////////////////////////////////////////////////////////////////////////// 156//////////////////////////////////////////////////////////////////////////////
157 157
158void pkt_queue::put (net_packet *p) 158pkt_queue::pkt_queue (double max_ttl, int max_queue)
159: max_ttl (max_ttl), max_queue (max_queue)
159{ 160{
160 if (queue[i]) 161 queue = new pkt [max_queue];
161 {
162 delete queue[i];
163 j = (j + 1) % QUEUEDEPTH;
164 }
165 162
166 queue[i] = p;
167
168 i = (i + 1) % QUEUEDEPTH;
169}
170
171net_packet *pkt_queue::get ()
172{
173 net_packet *p = queue[j];
174
175 if (p)
176 {
177 queue[j] = 0;
178 j = (j + 1) % QUEUEDEPTH;
179 }
180
181 return p;
182}
183
184pkt_queue::pkt_queue ()
185{
186 memset (queue, 0, sizeof (queue));
187 i = 0; 163 i = 0;
188 j = 0; 164 j = 0;
165
166 expire.set<pkt_queue, &pkt_queue::expire_cb> (this);
189} 167}
190 168
191pkt_queue::~pkt_queue () 169pkt_queue::~pkt_queue ()
192{ 170{
193 for (i = QUEUEDEPTH; --i > 0; ) 171 while (net_packet *p = get ())
172 delete p;
173
194 delete queue[i]; 174 delete [] queue;
175}
176
177void pkt_queue::expire_cb (ev::timer &w, int revents)
178{
179 ev_tstamp expire = ev_now () - max_ttl;
180
181 for (;;)
182 {
183 if (empty ())
184 break;
185
186 double diff = queue[j].tstamp - expire;
187
188 if (diff >= 0.)
189 {
190 w.start (diff > 0.5 ? diff : 0.5);
191 break;
192 }
193
194 delete get ();
195 }
196}
197
198void pkt_queue::put (net_packet *p)
199{
200 ev_tstamp now = ev_now ();
201
202 // start expiry timer
203 if (empty ())
204 expire.start (max_ttl);
205
206 int ni = i + 1 == max_queue ? 0 : i + 1;
207
208 if (ni == j)
209 delete get ();
210
211 queue[i].pkt = p;
212 queue[i].tstamp = now;
213
214 i = ni;
215}
216
217net_packet *pkt_queue::get ()
218{
219 if (empty ())
220 return 0;
221
222 net_packet *p = queue[j].pkt;
223 queue[j].pkt = 0;
224
225 j = j + 1 == max_queue ? 0 : j + 1;
226
227 return p;
195} 228}
196 229
197struct net_rateinfo 230struct net_rateinfo
198{ 231{
199 u32 host; 232 u32 host;
590{ 623{
591 slog (L_TRACE, _("%s: possible connection establish (ictx %d, octx %d)"), conf->nodename, !!ictx, !!octx); 624 slog (L_TRACE, _("%s: possible connection establish (ictx %d, octx %d)"), conf->nodename, !!ictx, !!octx);
592 625
593 if (ictx && octx) 626 if (ictx && octx)
594 { 627 {
595 connectmode = conf->connectmode;
596
597 // make sure rekeying timeouts are slightly asymmetric 628 // make sure rekeying timeouts are slightly asymmetric
598 ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0); 629 ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0);
599 rekey.start (rekey_interval, rekey_interval); 630 rekey.start (rekey_interval, rekey_interval);
600 keepalive.start (::conf.keepalive); 631 keepalive.start (::conf.keepalive);
601 632
602 // send queued packets 633 // send queued packets
603 if (ictx && octx) 634 if (ictx && octx)
604 { 635 {
605 while (tap_packet *p = (tap_packet *)data_queue.get ()) 636 while (tap_packet *p = (tap_packet *)data_queue.get ())
606 { 637 {
607 send_data_packet (p); 638 if (p->len) send_data_packet (p);
608 delete p; 639 delete p;
609 } 640 }
610 641
611 while (vpn_packet *p = (vpn_packet *)vpn_queue.get ()) 642 while (vpn_packet *p = (vpn_packet *)vpn_queue.get ())
612 { 643 {
613 send_vpn_packet (p, si, IPTOS_RELIABILITY); 644 if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY);
614 delete p; 645 delete p;
615 } 646 }
616 } 647 }
617 } 648 }
618 else 649 else
745 send_vpn_packet (r, si); 776 send_vpn_packet (r, si);
746 777
747 delete r; 778 delete r;
748} 779}
749 780
750void 781inline void
751connection::establish_connection_cb (ev::timer &w, int revents) 782connection::establish_connection_cb (ev::timer &w, int revents)
752{ 783{
753 if (!ictx 784 if (!ictx
754 && conf != THISNODE 785 && conf != THISNODE
755 && connectmode != conf_node::C_NEVER 786 && connectmode != conf_node::C_NEVER
756 && connectmode != conf_node::C_DISABLED 787 && connectmode != conf_node::C_DISABLED
757 && !w.is_active ()) 788 && !w.is_active ())
758 { 789 {
790 // a bit hacky, if ondemand, and packets are no longer queued, then reset the connection
791 // and stop trying. should probably be handled by a per-connection expire handler.
792 if (connectmode == conf_node::C_ONDEMAND && vpn_queue.empty () && data_queue.empty ())
793 {
794 reset_connection ();
795 return;
796 }
797
798 last_establish_attempt = ev_now ();
799
759 ev::tstamp retry_int = ev::tstamp (retry_cnt & 3 800 ev::tstamp retry_int = ev::tstamp (retry_cnt & 3
760 ? (retry_cnt & 3) + 1 801 ? (retry_cnt & 3) + 1
761 : 1 << (retry_cnt >> 2)); 802 : 1 << (retry_cnt >> 2));
762 803
763 reset_si (); 804 reset_si ();
786 else 827 else
787 send_ping (dsi, 0); 828 send_ping (dsi, 0);
788 } 829 }
789 } 830 }
790 831
791 retry_int *= slow ? 8. : 0.7; 832 retry_int *= slow ? 8. : 0.9;
792 833
793 if (retry_int < conf->max_retry) 834 if (retry_int < conf->max_retry)
794 retry_cnt++; 835 retry_cnt++;
795 else 836 else
796 retry_int = conf->max_retry; 837 retry_int = conf->max_retry;
806 { 847 {
807 slog (L_INFO, _("%s(%s): connection lost"), 848 slog (L_INFO, _("%s(%s): connection lost"),
808 conf->nodename, (const char *)si); 849 conf->nodename, (const char *)si);
809 850
810 if (::conf.script_node_down) 851 if (::conf.script_node_down)
811 if (!run_script (run_script_cb (this, &connection::script_node_down), false)) 852 {
853 run_script_cb cb;
854 cb.set<connection, &connection::script_node_down> (this);
855 if (!run_script (cb, false))
812 slog (L_WARN, _("node-down command execution failed, continuing.")); 856 slog (L_WARN, _("node-down command execution failed, continuing."));
857 }
813 } 858 }
814 859
815 delete ictx; ictx = 0; 860 delete ictx; ictx = 0;
816 delete octx; octx = 0; 861 delete octx; octx = 0;
817#if ENABLE_DNS 862#if ENABLE_DNS
835 send_reset (si); 880 send_reset (si);
836 881
837 reset_connection (); 882 reset_connection ();
838} 883}
839 884
840void 885inline void
841connection::rekey_cb (ev::timer &w, int revents) 886connection::rekey_cb (ev::timer &w, int revents)
842{ 887{
843 reset_connection (); 888 reset_connection ();
844 establish_connection (); 889 establish_connection ();
845} 890}
862 if (oseqno > MAX_SEQNO) 907 if (oseqno > MAX_SEQNO)
863 rekey (); 908 rekey ();
864} 909}
865 910
866void 911void
912connection::post_inject_queue ()
913{
914 // force a connection every now and when when packets are sent (max 1/s)
915 if (ev_now () - last_establish_attempt >= 0.95) // arbitrary
916 establish_connection.stop ();
917
918 establish_connection ();
919}
920
921void
867connection::inject_data_packet (tap_packet *pkt, bool broadcast/*TODO DDD*/) 922connection::inject_data_packet (tap_packet *pkt)
868{ 923{
869 if (ictx && octx) 924 if (ictx && octx)
870 send_data_packet (pkt); 925 send_data_packet (pkt);
871 else 926 else
872 { 927 {
873 if (!broadcast)
874 data_queue.put (new tap_packet (*pkt)); 928 data_queue.put (new tap_packet (*pkt));
875 929 post_inject_queue ();
876 establish_connection ();
877 } 930 }
878} 931}
879 932
880void connection::inject_vpn_packet (vpn_packet *pkt, int tos) 933void connection::inject_vpn_packet (vpn_packet *pkt, int tos)
881{ 934{
882 if (ictx && octx) 935 if (ictx && octx)
883 send_vpn_packet (pkt, si, tos); 936 send_vpn_packet (pkt, si, tos);
884 else 937 else
885 { 938 {
886 vpn_queue.put ((vpn_packet *)new data_packet (*(data_packet *)pkt)); 939 vpn_queue.put ((vpn_packet *)new data_packet (*(data_packet *)pkt));
887 940 post_inject_queue ();
888 establish_connection ();
889 } 941 }
890} 942}
891 943
892void 944void
893connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) 945connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi)
1041 slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), 1093 slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"),
1042 conf->nodename, (const char *)rsi, 1094 conf->nodename, (const char *)rsi,
1043 p->prot_major, p->prot_minor); 1095 p->prot_major, p->prot_minor);
1044 1096
1045 if (::conf.script_node_up) 1097 if (::conf.script_node_up)
1046 if (!run_script (run_script_cb (this, &connection::script_node_up), false)) 1098 {
1099 run_script_cb cb;
1100 cb.set<connection, &connection::script_node_up> (this);
1101 if (!run_script (cb, false))
1047 slog (L_WARN, _("node-up command execution failed, continuing.")); 1102 slog (L_WARN, _("node-up command execution failed, continuing."));
1103 }
1048 1104
1049 break; 1105 break;
1050 } 1106 }
1051 else 1107 else
1052 slog (L_ERR, _("%s(%s): sent and received challenge do not match"), 1108 slog (L_ERR, _("%s(%s): sent and received challenge do not match"),
1168 send_reset (rsi); 1224 send_reset (rsi);
1169 break; 1225 break;
1170 } 1226 }
1171} 1227}
1172 1228
1229inline void
1173void connection::keepalive_cb (ev::timer &w, int revents) 1230connection::keepalive_cb (ev::timer &w, int revents)
1174{ 1231{
1175 if (ev_now () >= last_activity + ::conf.keepalive + 30) 1232 if (ev_now () >= last_activity + ::conf.keepalive + 30)
1176 { 1233 {
1177 reset_connection (); 1234 reset_connection ();
1178 establish_connection (); 1235 establish_connection ();
1223 asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); 1280 asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env);
1224 asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); 1281 asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env);
1225 asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); 1282 asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env);
1226} 1283}
1227 1284
1285inline const char *
1228const char *connection::script_node_up () 1286connection::script_node_up ()
1229{ 1287{
1230 script_init_connect_env (); 1288 script_init_connect_env ();
1231 1289
1232 putenv ((char *)"STATE=up"); 1290 putenv ((char *)"STATE=up");
1233 1291
1238 ::conf.script_node_up ? ::conf.script_node_up : "node-up"); 1296 ::conf.script_node_up ? ::conf.script_node_up : "node-up");
1239 1297
1240 return filename; 1298 return filename;
1241} 1299}
1242 1300
1301inline const char *
1243const char *connection::script_node_down () 1302connection::script_node_down ()
1244{ 1303{
1245 script_init_connect_env (); 1304 script_init_connect_env ();
1246 1305
1247 putenv ((char *)"STATE=down"); 1306 putenv ((char *)"STATE=down");
1248 1307
1254 1313
1255 return filename; 1314 return filename;
1256} 1315}
1257 1316
1258connection::connection (struct vpn *vpn, conf_node *conf) 1317connection::connection (struct vpn *vpn, conf_node *conf)
1259: vpn(vpn), conf(conf) 1318: vpn(vpn), conf(conf),
1260, rekey (this, &connection::rekey_cb)
1261, keepalive (this, &connection::keepalive_cb)
1262, establish_connection (this, &connection::establish_connection_cb)
1263#if ENABLE_DNS 1319#if ENABLE_DNS
1264, dns (0) 1320 dns (0),
1265#endif 1321#endif
1322 data_queue(conf->max_ttl, conf->max_queue),
1323 vpn_queue(conf->max_ttl, conf->max_queue)
1266{ 1324{
1325 rekey .set<connection, &connection::rekey_cb > (this);
1326 keepalive .set<connection, &connection::keepalive_cb > (this);
1327 establish_connection.set<connection, &connection::establish_connection_cb> (this);
1328
1329 last_establish_attempt = 0.;
1267 octx = ictx = 0; 1330 octx = ictx = 0;
1268 retry_cnt = 0;
1269 1331
1270 if (!conf->protocols) // make sure some protocol is enabled 1332 if (!conf->protocols) // make sure some protocol is enabled
1271 conf->protocols = PROT_UDPv4; 1333 conf->protocols = PROT_UDPv4;
1272 1334
1273 connectmode = conf_node::C_ALWAYS; // initial setting 1335 connectmode = conf->connectmode;
1336
1337 // queue a dummy packet to force an initial connection attempt
1338 if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED)
1339 vpn_queue.put (new net_packet);
1340
1274 reset_connection (); 1341 reset_connection ();
1275} 1342}
1276 1343
1277connection::~connection () 1344connection::~connection ()
1278{ 1345{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines