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.33 by pcg, Sun Mar 21 13:16:36 2004 UTC vs.
Revision 1.41 by pcg, Wed Mar 2 05:49:31 2005 UTC

202// only do action once every x seconds per host whole allowing bursts. 202// only do action once every x seconds per host whole allowing bursts.
203// this implementation ("splay list" ;) is inefficient, 203// this implementation ("splay list" ;) is inefficient,
204// but low on resources. 204// but low on resources.
205struct net_rate_limiter : list<net_rateinfo> 205struct net_rate_limiter : list<net_rateinfo>
206{ 206{
207 static const double ALPHA = 1. - 1. / 600.; // allow bursts 207# define NRL_ALPHA (1. - 1. / 600.) // allow bursts
208 static const double CUTOFF = 10.; // one event every CUTOFF seconds 208# define NRL_CUTOFF 10. // one event every CUTOFF seconds
209 static const double EXPIRE = CUTOFF * 30.; // expire entries after this time 209# define NRL_EXPIRE (NRL_CUTOFF * 30.) // expire entries after this time
210 static const double MAXDIF = CUTOFF * (1. / (1. - ALPHA)); // maximum diff /count value 210# define NRL_MAXDIF (NRL_CUTOFF * (1. / (1. - NRL_ALPHA))) // maximum diff /count value
211 211
212 bool can (const sockinfo &si) { return can((u32)si.host); } 212 bool can (const sockinfo &si) { return can((u32)si.host); }
213 bool can (u32 host); 213 bool can (u32 host);
214}; 214};
215 215
220 iterator i; 220 iterator i;
221 221
222 for (i = begin (); i != end (); ) 222 for (i = begin (); i != end (); )
223 if (i->host == host) 223 if (i->host == host)
224 break; 224 break;
225 else if (i->last < NOW - EXPIRE) 225 else if (i->last < NOW - NRL_EXPIRE)
226 i = erase (i); 226 i = erase (i);
227 else 227 else
228 i++; 228 i++;
229 229
230 if (i == end ()) 230 if (i == end ())
231 { 231 {
232 net_rateinfo ri; 232 net_rateinfo ri;
233 233
234 ri.host = host; 234 ri.host = host;
235 ri.pcnt = 1.; 235 ri.pcnt = 1.;
236 ri.diff = MAXDIF; 236 ri.diff = NRL_MAXDIF;
237 ri.last = NOW; 237 ri.last = NOW;
238 238
239 push_front (ri); 239 push_front (ri);
240 240
241 return true; 241 return true;
243 else 243 else
244 { 244 {
245 net_rateinfo ri (*i); 245 net_rateinfo ri (*i);
246 erase (i); 246 erase (i);
247 247
248 ri.pcnt = ri.pcnt * ALPHA; 248 ri.pcnt = ri.pcnt * NRL_ALPHA;
249 ri.diff = ri.diff * ALPHA + (NOW - ri.last); 249 ri.diff = ri.diff * NRL_ALPHA + (NOW - ri.last);
250 250
251 ri.last = NOW; 251 ri.last = NOW;
252 252
253 double dif = ri.diff / ri.pcnt; 253 double dif = ri.diff / ri.pcnt;
254 254
255 bool send = dif > CUTOFF; 255 bool send = dif > NRL_CUTOFF;
256 256
257 if (dif > MAXDIF) 257 if (dif > NRL_MAXDIF)
258 { 258 {
259 ri.pcnt = 1.; 259 ri.pcnt = 1.;
260 ri.diff = MAXDIF; 260 ri.diff = NRL_MAXDIF;
261 } 261 }
262 else if (send) 262 else if (send)
263 ri.pcnt++; 263 ri.pcnt++;
264 264
265 push_front (ri); 265 push_front (ri);
589{ 589{
590 if (ictx && octx) 590 if (ictx && octx)
591 { 591 {
592 connectmode = conf->connectmode; 592 connectmode = conf->connectmode;
593 593
594 // make sure rekeying timeouts are slightly asymmetric
594 rekey.start (NOW + ::conf.rekey); 595 rekey.start (NOW + ::conf.rekey
596 + (conf->id > THISNODE->id ? 10 : 0));
595 keepalive.start (NOW + ::conf.keepalive); 597 keepalive.start (NOW + ::conf.keepalive);
596 598
597 // send queued packets 599 // send queued packets
598 if (ictx && octx) 600 if (ictx && octx)
599 { 601 {
625 protocol = best_protocol (THISNODE->protocols & conf->protocols); 627 protocol = best_protocol (THISNODE->protocols & conf->protocols);
626 628
627 // mask out protocols we cannot establish 629 // mask out protocols we cannot establish
628 if (!conf->udp_port) protocol &= ~PROT_UDPv4; 630 if (!conf->udp_port) protocol &= ~PROT_UDPv4;
629 if (!conf->tcp_port) protocol &= ~PROT_TCPv4; 631 if (!conf->tcp_port) protocol &= ~PROT_TCPv4;
632 if (!conf->dns_port) protocol &= ~PROT_DNSv4;
630 633
631 si.set (conf, protocol); 634 si.set (conf, protocol);
632} 635}
633 636
634// ensure sockinfo is valid, forward if necessary 637// ensure sockinfo is valid, forward if necessary
654} 657}
655 658
656void 659void
657connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) 660connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos)
658{ 661{
659 if (!vpn->send_vpn_packet (pkt, si, tos)) 662 bool ok;
663
664 switch (si.prot)
665 {
666 case PROT_IPv4:
667 ok = vpn->send_ipv4_packet (pkt, si, tos); break;
668 case PROT_UDPv4:
669 ok = vpn->send_udpv4_packet (pkt, si, tos); break;
670#if ENABLE_TCP
671 case PROT_TCPv4:
672 ok = vpn->send_tcpv4_packet (pkt, si, tos); break;
673#endif
674#if ENABLE_ICMP
675 case PROT_ICMPv4:
676 ok = vpn->send_icmpv4_packet (pkt, si, tos); break;
677#endif
678#if ENABLE_DNS
679 case PROT_DNSv4:
680 ok = send_dnsv4_packet (pkt, si, tos); break;
681#endif
682
683 default:
684 slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si);
685 ok = false;
686 }
687
688 if (!ok)
660 reset_connection (); 689 reset_connection ();
661} 690}
662 691
663void 692void
664connection::send_ping (const sockinfo &si, u8 pong) 693connection::send_ping (const sockinfo &si, u8 pong)
742 && connectmode != conf_node::C_DISABLED 771 && connectmode != conf_node::C_DISABLED
743 && NOW > w.at) 772 && NOW > w.at)
744 { 773 {
745 double retry_int = double (retry_cnt & 3 ? (retry_cnt & 3) : 1 << (retry_cnt >> 2)) * 0.6; 774 double retry_int = double (retry_cnt & 3 ? (retry_cnt & 3) : 1 << (retry_cnt >> 2)) * 0.6;
746 775
747 if (retry_int < 3600 * 8) 776 if (retry_int < conf->max_retry)
748 retry_cnt++; 777 retry_cnt++;
778 else
779 retry_int = conf->max_retry;
749 780
750 w.start (NOW + retry_int); 781 w.start (NOW + retry_int);
751 782
752 reset_si (); 783 reset_si ();
753 784
781 } 812 }
782 813
783 delete ictx; ictx = 0; 814 delete ictx; ictx = 0;
784 delete octx; octx = 0; 815 delete octx; octx = 0;
785 816
786 si.host= 0; 817 si.host = 0;
787 818
788 last_activity = 0; 819 last_activity = 0;
789 retry_cnt = 0; 820 retry_cnt = 0;
790 821
791 rekey.stop (); 822 rekey.stop ();
1094 break; 1125 break;
1095 1126
1096 case vpn_packet::PT_CONNECT_INFO: 1127 case vpn_packet::PT_CONNECT_INFO:
1097 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx)) 1128 if (ictx && octx && rsi == si && pkt->hmac_chk (ictx))
1098 { 1129 {
1099 connect_info_packet *p = (connect_info_packet *) pkt; 1130 connect_info_packet *p = (connect_info_packet *)pkt;
1100 1131
1101 assert (p->id > 0 && p->id <= vpn->conns.size ()); // hmac-auth does not mean we accept anything 1132 if (p->id > 0 && p->id <= vpn->conns.size ()) // hmac-auth does not mean we accept anything
1102 1133 {
1103 connection *c = vpn->conns[p->id - 1]; 1134 connection *c = vpn->conns[p->id - 1];
1104 1135
1105 c->conf->protocols = p->protocols; 1136 c->conf->protocols = p->protocols;
1106 protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); 1137 protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf));
1107 p->si.upgrade_protocol (protocol, c->conf); 1138 p->si.upgrade_protocol (protocol, c->conf);
1108 1139
1109 slog (L_TRACE, "<<%d PT_CONNECT_INFO(%d,%s) (%d)", 1140 slog (L_TRACE, "<<%d PT_CONNECT_INFO(%d,%s) (%d)",
1110 conf->id, p->id, (const char *)p->si, !c->ictx && !c->octx); 1141 conf->id, p->id, (const char *)p->si, !c->ictx && !c->octx);
1111 1142
1112 const sockinfo &dsi = forward_si (p->si); 1143 const sockinfo &dsi = forward_si (p->si);
1113 1144
1114 if (dsi.valid ()) 1145 if (dsi.valid ())
1115 c->send_auth_request (dsi, true); 1146 c->send_auth_request (dsi, true);
1147 }
1116 } 1148 }
1117 1149
1118 break; 1150 break;
1119 1151
1120 default: 1152 default:
1184 putenv ("STATE=down"); 1216 putenv ("STATE=down");
1185 1217
1186 return ::conf.script_node_up ? ::conf.script_node_down : "node-down"; 1218 return ::conf.script_node_up ? ::conf.script_node_down : "node-down";
1187} 1219}
1188 1220
1189connection::connection(struct vpn *vpn_) 1221connection::connection (struct vpn *vpn, conf_node *conf)
1190: vpn(vpn_) 1222: vpn(vpn), conf(conf)
1191, rekey (this, &connection::rekey_cb) 1223, rekey (this, &connection::rekey_cb)
1192, keepalive (this, &connection::keepalive_cb) 1224, keepalive (this, &connection::keepalive_cb)
1193, establish_connection (this, &connection::establish_connection_cb) 1225, establish_connection (this, &connection::establish_connection_cb)
1226#if ENABLE_DNS
1227, dnsv4_tw (this, &connection::dnsv4_cb)
1228, dns_rcvdq (0), dns_snddq (0)
1229, dns_rcvseq (0), dns_sndseq (0)
1230#endif
1194{ 1231{
1195 octx = ictx = 0; 1232 octx = ictx = 0;
1196 retry_cnt = 0; 1233 retry_cnt = 0;
1197 1234
1235 if (!conf->protocols) // make sure some protocol is enabled
1236 conf->protocols = PROT_UDPv4;
1237
1198 connectmode = conf_node::C_ALWAYS; // initial setting 1238 connectmode = conf_node::C_ALWAYS; // initial setting
1199 reset_connection (); 1239 reset_connection ();
1200} 1240}
1201 1241
1202connection::~connection () 1242connection::~connection ()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines