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.57 by pcg, Thu Jul 7 14:41:51 2005 UTC vs.
Revision 1.64 by pcg, Tue Dec 4 15:01:12 2007 UTC

82 require (EVP_DigestUpdate(&ctx, &id, sizeof id)); 82 require (EVP_DigestUpdate(&ctx, &id, sizeof id));
83 require (EVP_DigestFinal (&ctx, (unsigned char *)&h, 0)); 83 require (EVP_DigestFinal (&ctx, (unsigned char *)&h, 0));
84 EVP_MD_CTX_cleanup (&ctx); 84 EVP_MD_CTX_cleanup (&ctx);
85} 85}
86 86
87struct rsa_entry { 87struct rsa_entry
88{
88 tstamp expire; 89 tstamp expire;
89 rsaid id; 90 rsaid id;
90 rsachallenge chg; 91 rsachallenge chg;
91}; 92};
92 93
93struct rsa_cache : list<rsa_entry> 94struct rsa_cache : list<rsa_entry>
94{ 95{
95 void cleaner_cb (time_watcher &w); time_watcher cleaner; 96 void cleaner_cb (ev::timer &w, int revents); ev::timer cleaner;
96 97
97 bool find (const rsaid &id, rsachallenge &chg) 98 bool find (const rsaid &id, rsachallenge &chg)
98 { 99 {
99 for (iterator i = begin (); i != end (); ++i) 100 for (iterator i = begin (); i != end (); ++i)
100 { 101 {
101 if (!memcmp (&id, &i->id, sizeof id) && i->expire > NOW) 102 if (!memcmp (&id, &i->id, sizeof id) && i->expire > ev_now ())
102 { 103 {
103 memcpy (&chg, &i->chg, sizeof chg); 104 memcpy (&chg, &i->chg, sizeof chg);
104 105
105 erase (i); 106 erase (i);
106 return true; 107 return true;
107 } 108 }
108 } 109 }
109 110
110 if (cleaner.at < NOW) 111 if (!cleaner.is_active ())
111 cleaner.start (NOW + RSA_TTL); 112 cleaner.again ();
112 113
113 return false; 114 return false;
114 } 115 }
115 116
116 void gen (rsaid &id, rsachallenge &chg) 117 void gen (rsaid &id, rsachallenge &chg)
118 rsa_entry e; 119 rsa_entry e;
119 120
120 RAND_bytes ((unsigned char *)&id, sizeof id); 121 RAND_bytes ((unsigned char *)&id, sizeof id);
121 RAND_bytes ((unsigned char *)&chg, sizeof chg); 122 RAND_bytes ((unsigned char *)&chg, sizeof chg);
122 123
123 e.expire = NOW + RSA_TTL; 124 e.expire = ev_now () + RSA_TTL;
124 e.id = id; 125 e.id = id;
125 memcpy (&e.chg, &chg, sizeof chg); 126 memcpy (&e.chg, &chg, sizeof chg);
126 127
127 push_back (e); 128 push_back (e);
128 129
129 if (cleaner.at < NOW) 130 if (!cleaner.is_active ())
130 cleaner.start (NOW + RSA_TTL); 131 cleaner.again ();
131 } 132 }
132 133
133 rsa_cache () 134 rsa_cache ()
134 : cleaner (this, &rsa_cache::cleaner_cb) 135 : cleaner (this, &rsa_cache::cleaner_cb)
135 { } 136 {
137 cleaner.set (RSA_TTL, RSA_TTL);
138 }
136 139
137} rsa_cache; 140} rsa_cache;
138 141
139void rsa_cache::cleaner_cb (time_watcher &w) 142void rsa_cache::cleaner_cb (ev::timer &w, int revents)
140{ 143{
141 if (!empty ()) 144 if (empty ())
145 w.stop ();
146 else
142 { 147 {
143 w.start (NOW + RSA_TTL);
144
145 for (iterator i = begin (); i != end (); ) 148 for (iterator i = begin (); i != end (); )
146 if (i->expire <= NOW) 149 if (i->expire <= ev_now ())
147 i = erase (i); 150 i = erase (i);
148 else 151 else
149 ++i; 152 ++i;
150 } 153 }
151} 154}
189{ 192{
190 for (i = QUEUEDEPTH; --i > 0; ) 193 for (i = QUEUEDEPTH; --i > 0; )
191 delete queue[i]; 194 delete queue[i];
192} 195}
193 196
194struct net_rateinfo { 197struct net_rateinfo
198{
195 u32 host; 199 u32 host;
196 double pcnt, diff; 200 double pcnt, diff;
197 tstamp last; 201 tstamp last;
198}; 202};
199 203
218 iterator i; 222 iterator i;
219 223
220 for (i = begin (); i != end (); ) 224 for (i = begin (); i != end (); )
221 if (i->host == host) 225 if (i->host == host)
222 break; 226 break;
223 else if (i->last < NOW - NRL_EXPIRE) 227 else if (i->last < ev_now () - NRL_EXPIRE)
224 i = erase (i); 228 i = erase (i);
225 else 229 else
226 i++; 230 i++;
227 231
228 if (i == end ()) 232 if (i == end ())
230 net_rateinfo ri; 234 net_rateinfo ri;
231 235
232 ri.host = host; 236 ri.host = host;
233 ri.pcnt = 1.; 237 ri.pcnt = 1.;
234 ri.diff = NRL_MAXDIF; 238 ri.diff = NRL_MAXDIF;
235 ri.last = NOW; 239 ri.last = ev_now ();
236 240
237 push_front (ri); 241 push_front (ri);
238 242
239 return true; 243 return true;
240 } 244 }
242 { 246 {
243 net_rateinfo ri (*i); 247 net_rateinfo ri (*i);
244 erase (i); 248 erase (i);
245 249
246 ri.pcnt = ri.pcnt * NRL_ALPHA; 250 ri.pcnt = ri.pcnt * NRL_ALPHA;
247 ri.diff = ri.diff * NRL_ALPHA + (NOW - ri.last); 251 ri.diff = ri.diff * NRL_ALPHA + (ev_now () - ri.last);
248 252
249 ri.last = NOW; 253 ri.last = ev_now ();
250 254
251 double dif = ri.diff / ri.pcnt; 255 double dif = ri.diff / ri.pcnt;
252 256
253 bool send = dif > NRL_CUTOFF; 257 bool send = dif > NRL_CUTOFF;
254 258
463#if ENABLE_COMPRESSION 467#if ENABLE_COMPRESSION
464 f |= FEATURE_COMPRESSION; 468 f |= FEATURE_COMPRESSION;
465#endif 469#endif
466#if ENABLE_ROHC 470#if ENABLE_ROHC
467 f |= FEATURE_ROHC; 471 f |= FEATURE_ROHC;
472#endif
473#if ENABLE_BRIDGING
474 f |= FEATURE_BRIDGING;
468#endif 475#endif
469 return f; 476 return f;
470 } 477 }
471}; 478};
472 479
579///////////////////////////////////////////////////////////////////////////// 586/////////////////////////////////////////////////////////////////////////////
580 587
581void 588void
582connection::connection_established () 589connection::connection_established ()
583{ 590{
591 slog (L_TRACE, _("%s: possible connection establish (ictx %d, octx %d)"), conf->nodename, !!ictx, !!octx);
592
584 if (ictx && octx) 593 if (ictx && octx)
585 { 594 {
586 connectmode = conf->connectmode; 595 connectmode = conf->connectmode;
587 596
588 // make sure rekeying timeouts are slightly asymmetric 597 // make sure rekeying timeouts are slightly asymmetric
589 rekey.start (NOW + ::conf.rekey 598 ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0);
590 + (conf->id > THISNODE->id ? 10 : 0)); 599 rekey.start (rekey_interval, rekey_interval);
591 keepalive.start (NOW + ::conf.keepalive); 600 keepalive.start (::conf.keepalive);
592 601
593 // send queued packets 602 // send queued packets
594 if (ictx && octx) 603 if (ictx && octx)
595 { 604 {
596 while (tap_packet *p = (tap_packet *)data_queue.get ()) 605 while (tap_packet *p = (tap_packet *)data_queue.get ())
607 } 616 }
608 } 617 }
609 else 618 else
610 { 619 {
611 retry_cnt = 0; 620 retry_cnt = 0;
612 establish_connection.start (NOW + 5); 621 establish_connection.start (5);
613 keepalive.stop (); 622 keepalive.stop ();
614 rekey.stop (); 623 rekey.stop ();
615 } 624 }
616} 625}
617 626
622 631
623 // mask out protocols we cannot establish 632 // mask out protocols we cannot establish
624 if (!conf->udp_port) protocol &= ~PROT_UDPv4; 633 if (!conf->udp_port) protocol &= ~PROT_UDPv4;
625 if (!conf->tcp_port) protocol &= ~PROT_TCPv4; 634 if (!conf->tcp_port) protocol &= ~PROT_TCPv4;
626 if (!conf->dns_port) protocol &= ~PROT_DNSv4; 635 if (!conf->dns_port) protocol &= ~PROT_DNSv4;
636
637 if (protocol
638 && (!conf->can_direct (THISNODE)
639 || !THISNODE->can_direct (conf)))
640 {
641 slog (L_DEBUG, _("%s: direct connection denied"), conf->nodename);
642 protocol = 0;
643 }
627 644
628 si.set (conf, protocol); 645 si.set (conf, protocol);
629} 646}
630 647
631// ensure sockinfo is valid, forward if necessary 648// ensure sockinfo is valid, forward if necessary
717} 734}
718 735
719void 736void
720connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols) 737connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols)
721{ 738{
722 slog (L_TRACE, ">>%d PT_CONNECT_INFO(%d,%s)\n", 739 slog (L_TRACE, ">>%d PT_CONNECT_INFO(%d,%s)",
723 conf->id, rid, (const char *)rsi); 740 conf->id, rid, (const char *)rsi);
724 741
725 connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols); 742 connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols);
726 743
727 r->hmac_set (octx); 744 r->hmac_set (octx);
729 746
730 delete r; 747 delete r;
731} 748}
732 749
733void 750void
734connection::establish_connection_cb (time_watcher &w) 751connection::establish_connection_cb (ev::timer &w, int revents)
735{ 752{
736 if (!ictx 753 if (!ictx
737 && conf != THISNODE 754 && conf != THISNODE
738 && connectmode != conf_node::C_NEVER 755 && connectmode != conf_node::C_NEVER
739 && connectmode != conf_node::C_DISABLED 756 && connectmode != conf_node::C_DISABLED
740 && NOW > w.at) 757 && !w.is_active ())
741 { 758 {
742 w.at = TSTAMP_MAX; // first disable this watcher in case of recursion 759 ev::tstamp retry_int = ev::tstamp (retry_cnt & 3
743
744 double retry_int = double (retry_cnt & 3
745 ? (retry_cnt & 3) + 1 760 ? (retry_cnt & 3) + 1
746 : 1 << (retry_cnt >> 2)); 761 : 1 << (retry_cnt >> 2));
747 762
748 reset_si (); 763 reset_si ();
749 764
750 bool slow = si.prot & PROT_SLOW; 765 bool slow = si.prot & PROT_SLOW;
751 766
752 if (si.prot && !si.host) 767 if (si.prot && !si.host)
768 {
769 slog (L_TRACE, _("%s: connection request (indirect)"), conf->nodename);
770 /*TODO*/ /* start the timer so we don't recurse endlessly */
771 w.start (1);
753 vpn->send_connect_request (conf->id); 772 vpn->send_connect_request (conf->id);
773 }
754 else 774 else
755 { 775 {
776 slog (L_TRACE, _("%s: connection request (direct)"), conf->nodename, !!ictx, !!octx);
777
756 const sockinfo &dsi = forward_si (si); 778 const sockinfo &dsi = forward_si (si);
757 779
758 slow = slow || (dsi.prot & PROT_SLOW); 780 slow = slow || (dsi.prot & PROT_SLOW);
759 781
760 if (dsi.valid () && auth_rate_limiter.can (dsi)) 782 if (dsi.valid () && auth_rate_limiter.can (dsi))
771 if (retry_int < conf->max_retry) 793 if (retry_int < conf->max_retry)
772 retry_cnt++; 794 retry_cnt++;
773 else 795 else
774 retry_int = conf->max_retry; 796 retry_int = conf->max_retry;
775 797
776 w.start (NOW + retry_int); 798 w.start (retry_int);
777 } 799 }
778} 800}
779 801
780void 802void
781connection::reset_connection () 803connection::reset_connection ()
784 { 806 {
785 slog (L_INFO, _("%s(%s): connection lost"), 807 slog (L_INFO, _("%s(%s): connection lost"),
786 conf->nodename, (const char *)si); 808 conf->nodename, (const char *)si);
787 809
788 if (::conf.script_node_down) 810 if (::conf.script_node_down)
789 if (!run_script (run_script_cb (this, &connection::script_node_down), false)) 811 {
812 run_script_cb cb;
813 callback_set (cb, this, connection, script_node_down);
814 if (!run_script (cb, false))
790 slog (L_WARN, _("node-down command execution failed, continuing.")); 815 slog (L_WARN, _("node-down command execution failed, continuing."));
816 }
791 } 817 }
792 818
793 delete ictx; ictx = 0; 819 delete ictx; ictx = 0;
794 delete octx; octx = 0; 820 delete octx; octx = 0;
795#if ENABLE_DNS 821#if ENABLE_DNS
814 840
815 reset_connection (); 841 reset_connection ();
816} 842}
817 843
818void 844void
819connection::rekey_cb (time_watcher &w) 845connection::rekey_cb (ev::timer &w, int revents)
820{ 846{
821 reset_connection (); 847 reset_connection ();
822 establish_connection (); 848 establish_connection ();
823} 849}
824 850
868} 894}
869 895
870void 896void
871connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) 897connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi)
872{ 898{
873 last_activity = NOW; 899 last_activity = ev_now ();
874 900
875 slog (L_NOISE, "<<%d received packet type %d from %d to %d", 901 slog (L_NOISE, "<<%d received packet type %d from %d to %d",
876 conf->id, pkt->typ (), pkt->src (), pkt->dst ()); 902 conf->id, pkt->typ (), pkt->src (), pkt->dst ());
903
904 if (connectmode == conf_node::C_DISABLED)
905 return;
877 906
878 switch (pkt->typ ()) 907 switch (pkt->typ ())
879 { 908 {
880 case vpn_packet::PT_PING: 909 case vpn_packet::PT_PING:
881 // we send pings instead of auth packets after some retries, 910 // we send pings instead of auth packets after some retries,
1016 slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), 1045 slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"),
1017 conf->nodename, (const char *)rsi, 1046 conf->nodename, (const char *)rsi,
1018 p->prot_major, p->prot_minor); 1047 p->prot_major, p->prot_minor);
1019 1048
1020 if (::conf.script_node_up) 1049 if (::conf.script_node_up)
1021 if (!run_script (run_script_cb (this, &connection::script_node_up), false)) 1050 {
1051 run_script_cb cb;
1052 callback_set (cb, this, connection, script_node_up);
1053 if (!run_script (cb, false))
1022 slog (L_WARN, _("node-up command execution failed, continuing.")); 1054 slog (L_WARN, _("node-up command execution failed, continuing."));
1055 }
1023 1056
1024 break; 1057 break;
1025 } 1058 }
1026 else 1059 else
1027 slog (L_ERR, _("%s(%s): sent and received challenge do not match"), 1060 slog (L_ERR, _("%s(%s): sent and received challenge do not match"),
1087 if (p->id > 0 && p->id <= vpn->conns.size ()) 1120 if (p->id > 0 && p->id <= vpn->conns.size ())
1088 { 1121 {
1089 connection *c = vpn->conns[p->id - 1]; 1122 connection *c = vpn->conns[p->id - 1];
1090 conf->protocols = p->protocols; 1123 conf->protocols = p->protocols;
1091 1124
1092 slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]\n", 1125 slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]",
1093 conf->id, p->id, c->ictx && c->octx); 1126 conf->id, p->id, c->ictx && c->octx);
1094 1127
1095 if (c->ictx && c->octx) 1128 if (c->ictx && c->octx)
1096 { 1129 {
1097 // send connect_info packets to both sides, in case one is 1130 // send connect_info packets to both sides, in case one is
1143 send_reset (rsi); 1176 send_reset (rsi);
1144 break; 1177 break;
1145 } 1178 }
1146} 1179}
1147 1180
1148void connection::keepalive_cb (time_watcher &w) 1181void connection::keepalive_cb (ev::timer &w, int revents)
1149{ 1182{
1150 if (NOW >= last_activity + ::conf.keepalive + 30) 1183 if (ev_now () >= last_activity + ::conf.keepalive + 30)
1151 { 1184 {
1152 reset_connection (); 1185 reset_connection ();
1153 establish_connection (); 1186 establish_connection ();
1154 } 1187 }
1155 else if (NOW < last_activity + ::conf.keepalive) 1188 else if (ev_now () < last_activity + ::conf.keepalive)
1156 w.start (last_activity + ::conf.keepalive); 1189 w.start (last_activity + ::conf.keepalive - ev::now ());
1157 else if (conf->connectmode != conf_node::C_ONDEMAND 1190 else if (conf->connectmode != conf_node::C_ONDEMAND
1158 || THISNODE->connectmode != conf_node::C_ONDEMAND) 1191 || THISNODE->connectmode != conf_node::C_ONDEMAND)
1159 { 1192 {
1160 send_ping (si); 1193 send_ping (si);
1161 w.start (NOW + 5); 1194 w.start (5);
1162 } 1195 }
1163 else if (NOW < last_activity + ::conf.keepalive + 10) 1196 else if (ev_now () < last_activity + ::conf.keepalive + 10)
1164 // hold ondemand connections implicitly a few seconds longer 1197 // hold ondemand connections implicitly a few seconds longer
1165 // should delete octx, though, or something like that ;) 1198 // should delete octx, though, or something like that ;)
1166 w.start (last_activity + ::conf.keepalive + 10); 1199 w.start (last_activity + ::conf.keepalive + 10 - ev::now ());
1167 else 1200 else
1168 reset_connection (); 1201 reset_connection ();
1169} 1202}
1170 1203
1171void connection::send_connect_request (int id) 1204void connection::send_connect_request (int id)
1202 1235
1203const char *connection::script_node_up () 1236const char *connection::script_node_up ()
1204{ 1237{
1205 script_init_connect_env (); 1238 script_init_connect_env ();
1206 1239
1207 putenv ("STATE=up"); 1240 putenv ((char *)"STATE=up");
1208 1241
1209 char *filename; 1242 char *filename;
1210 asprintf (&filename, 1243 asprintf (&filename,
1211 "%s/%s", 1244 "%s/%s",
1212 confbase, 1245 confbase,
1217 1250
1218const char *connection::script_node_down () 1251const char *connection::script_node_down ()
1219{ 1252{
1220 script_init_connect_env (); 1253 script_init_connect_env ();
1221 1254
1222 putenv ("STATE=down"); 1255 putenv ((char *)"STATE=down");
1223 1256
1224 char *filename; 1257 char *filename;
1225 asprintf (&filename, 1258 asprintf (&filename,
1226 "%s/%s", 1259 "%s/%s",
1227 confbase, 1260 confbase,

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines