… | |
… | |
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 | |
87 | struct rsa_entry { |
87 | struct 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 | |
93 | struct rsa_cache : list<rsa_entry> |
94 | struct rsa_cache : list<rsa_entry> |
94 | { |
95 | { |
95 | void cleaner_cb (time_watcher &w); time_watcher cleaner; |
96 | inline 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 | { } |
135 | { |
|
|
136 | cleaner.set<rsa_cache, &rsa_cache::cleaner_cb> (this); |
|
|
137 | cleaner.set (RSA_TTL, RSA_TTL); |
|
|
138 | } |
136 | |
139 | |
137 | } rsa_cache; |
140 | } rsa_cache; |
138 | |
141 | |
139 | void rsa_cache::cleaner_cb (time_watcher &w) |
142 | void 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 | } |
152 | |
155 | |
153 | ////////////////////////////////////////////////////////////////////////////// |
156 | ////////////////////////////////////////////////////////////////////////////// |
154 | |
157 | |
155 | void pkt_queue::put (net_packet *p) |
158 | pkt_queue::pkt_queue (double max_ttl, int max_queue) |
|
|
159 | : max_ttl (max_ttl), max_queue (max_queue) |
156 | { |
160 | { |
157 | if (queue[i]) |
161 | queue = new pkt [max_queue]; |
158 | { |
|
|
159 | delete queue[i]; |
|
|
160 | j = (j + 1) % QUEUEDEPTH; |
|
|
161 | } |
|
|
162 | |
162 | |
163 | queue[i] = p; |
|
|
164 | |
|
|
165 | i = (i + 1) % QUEUEDEPTH; |
|
|
166 | } |
|
|
167 | |
|
|
168 | net_packet *pkt_queue::get () |
|
|
169 | { |
|
|
170 | net_packet *p = queue[j]; |
|
|
171 | |
|
|
172 | if (p) |
|
|
173 | { |
|
|
174 | queue[j] = 0; |
|
|
175 | j = (j + 1) % QUEUEDEPTH; |
|
|
176 | } |
|
|
177 | |
|
|
178 | return p; |
|
|
179 | } |
|
|
180 | |
|
|
181 | pkt_queue::pkt_queue () |
|
|
182 | { |
|
|
183 | memset (queue, 0, sizeof (queue)); |
|
|
184 | i = 0; |
163 | i = 0; |
185 | j = 0; |
164 | j = 0; |
|
|
165 | |
|
|
166 | expire.set<pkt_queue, &pkt_queue::expire_cb> (this); |
186 | } |
167 | } |
187 | |
168 | |
188 | pkt_queue::~pkt_queue () |
169 | pkt_queue::~pkt_queue () |
189 | { |
170 | { |
190 | for (i = QUEUEDEPTH; --i > 0; ) |
171 | while (net_packet *p = get ()) |
|
|
172 | delete p; |
|
|
173 | |
191 | delete queue[i]; |
174 | delete [] queue; |
192 | } |
175 | } |
193 | |
176 | |
|
|
177 | void 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 | |
|
|
198 | void 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 | |
|
|
217 | net_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; |
|
|
228 | } |
|
|
229 | |
194 | struct net_rateinfo { |
230 | struct net_rateinfo |
|
|
231 | { |
195 | u32 host; |
232 | u32 host; |
196 | double pcnt, diff; |
233 | double pcnt, diff; |
197 | tstamp last; |
234 | tstamp last; |
198 | }; |
235 | }; |
199 | |
236 | |
… | |
… | |
218 | iterator i; |
255 | iterator i; |
219 | |
256 | |
220 | for (i = begin (); i != end (); ) |
257 | for (i = begin (); i != end (); ) |
221 | if (i->host == host) |
258 | if (i->host == host) |
222 | break; |
259 | break; |
223 | else if (i->last < NOW - NRL_EXPIRE) |
260 | else if (i->last < ev_now () - NRL_EXPIRE) |
224 | i = erase (i); |
261 | i = erase (i); |
225 | else |
262 | else |
226 | i++; |
263 | i++; |
227 | |
264 | |
228 | if (i == end ()) |
265 | if (i == end ()) |
… | |
… | |
230 | net_rateinfo ri; |
267 | net_rateinfo ri; |
231 | |
268 | |
232 | ri.host = host; |
269 | ri.host = host; |
233 | ri.pcnt = 1.; |
270 | ri.pcnt = 1.; |
234 | ri.diff = NRL_MAXDIF; |
271 | ri.diff = NRL_MAXDIF; |
235 | ri.last = NOW; |
272 | ri.last = ev_now (); |
236 | |
273 | |
237 | push_front (ri); |
274 | push_front (ri); |
238 | |
275 | |
239 | return true; |
276 | return true; |
240 | } |
277 | } |
… | |
… | |
242 | { |
279 | { |
243 | net_rateinfo ri (*i); |
280 | net_rateinfo ri (*i); |
244 | erase (i); |
281 | erase (i); |
245 | |
282 | |
246 | ri.pcnt = ri.pcnt * NRL_ALPHA; |
283 | ri.pcnt = ri.pcnt * NRL_ALPHA; |
247 | ri.diff = ri.diff * NRL_ALPHA + (NOW - ri.last); |
284 | ri.diff = ri.diff * NRL_ALPHA + (ev_now () - ri.last); |
248 | |
285 | |
249 | ri.last = NOW; |
286 | ri.last = ev_now (); |
250 | |
287 | |
251 | double dif = ri.diff / ri.pcnt; |
288 | double dif = ri.diff / ri.pcnt; |
252 | |
289 | |
253 | bool send = dif > NRL_CUTOFF; |
290 | bool send = dif > NRL_CUTOFF; |
254 | |
291 | |
… | |
… | |
463 | #if ENABLE_COMPRESSION |
500 | #if ENABLE_COMPRESSION |
464 | f |= FEATURE_COMPRESSION; |
501 | f |= FEATURE_COMPRESSION; |
465 | #endif |
502 | #endif |
466 | #if ENABLE_ROHC |
503 | #if ENABLE_ROHC |
467 | f |= FEATURE_ROHC; |
504 | f |= FEATURE_ROHC; |
|
|
505 | #endif |
|
|
506 | #if ENABLE_BRIDGING |
|
|
507 | f |= FEATURE_BRIDGING; |
468 | #endif |
508 | #endif |
469 | return f; |
509 | return f; |
470 | } |
510 | } |
471 | }; |
511 | }; |
472 | |
512 | |
… | |
… | |
579 | ///////////////////////////////////////////////////////////////////////////// |
619 | ///////////////////////////////////////////////////////////////////////////// |
580 | |
620 | |
581 | void |
621 | void |
582 | connection::connection_established () |
622 | connection::connection_established () |
583 | { |
623 | { |
|
|
624 | slog (L_TRACE, _("%s: possible connection establish (ictx %d, octx %d)"), conf->nodename, !!ictx, !!octx); |
|
|
625 | |
584 | if (ictx && octx) |
626 | if (ictx && octx) |
585 | { |
627 | { |
586 | connectmode = conf->connectmode; |
|
|
587 | |
|
|
588 | // make sure rekeying timeouts are slightly asymmetric |
628 | // make sure rekeying timeouts are slightly asymmetric |
589 | rekey.start (NOW + ::conf.rekey |
629 | ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0); |
590 | + (conf->id > THISNODE->id ? 10 : 0)); |
630 | rekey.start (rekey_interval, rekey_interval); |
591 | keepalive.start (NOW + ::conf.keepalive); |
631 | keepalive.start (::conf.keepalive); |
592 | |
632 | |
593 | // send queued packets |
633 | // send queued packets |
594 | if (ictx && octx) |
634 | if (ictx && octx) |
595 | { |
635 | { |
596 | while (tap_packet *p = (tap_packet *)data_queue.get ()) |
636 | while (tap_packet *p = (tap_packet *)data_queue.get ()) |
597 | { |
637 | { |
598 | send_data_packet (p); |
638 | if (p->len) send_data_packet (p); |
599 | delete p; |
639 | delete p; |
600 | } |
640 | } |
601 | |
641 | |
602 | while (vpn_packet *p = (vpn_packet *)vpn_queue.get ()) |
642 | while (vpn_packet *p = (vpn_packet *)vpn_queue.get ()) |
603 | { |
643 | { |
604 | send_vpn_packet (p, si, IPTOS_RELIABILITY); |
644 | if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY); |
605 | delete p; |
645 | delete p; |
606 | } |
646 | } |
607 | } |
647 | } |
608 | } |
648 | } |
609 | else |
649 | else |
610 | { |
650 | { |
611 | retry_cnt = 0; |
651 | retry_cnt = 0; |
612 | establish_connection.start (NOW + 5); |
652 | establish_connection.start (5); |
613 | keepalive.stop (); |
653 | keepalive.stop (); |
614 | rekey.stop (); |
654 | rekey.stop (); |
615 | } |
655 | } |
616 | } |
656 | } |
617 | |
657 | |
… | |
… | |
622 | |
662 | |
623 | // mask out protocols we cannot establish |
663 | // mask out protocols we cannot establish |
624 | if (!conf->udp_port) protocol &= ~PROT_UDPv4; |
664 | if (!conf->udp_port) protocol &= ~PROT_UDPv4; |
625 | if (!conf->tcp_port) protocol &= ~PROT_TCPv4; |
665 | if (!conf->tcp_port) protocol &= ~PROT_TCPv4; |
626 | if (!conf->dns_port) protocol &= ~PROT_DNSv4; |
666 | if (!conf->dns_port) protocol &= ~PROT_DNSv4; |
|
|
667 | |
|
|
668 | if (protocol |
|
|
669 | && (!conf->can_direct (THISNODE) |
|
|
670 | || !THISNODE->can_direct (conf))) |
|
|
671 | { |
|
|
672 | slog (L_DEBUG, _("%s: direct connection denied"), conf->nodename); |
|
|
673 | protocol = 0; |
|
|
674 | } |
627 | |
675 | |
628 | si.set (conf, protocol); |
676 | si.set (conf, protocol); |
629 | } |
677 | } |
630 | |
678 | |
631 | // ensure sockinfo is valid, forward if necessary |
679 | // ensure sockinfo is valid, forward if necessary |
… | |
… | |
717 | } |
765 | } |
718 | |
766 | |
719 | void |
767 | void |
720 | connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols) |
768 | connection::send_connect_info (int rid, const sockinfo &rsi, u8 rprotocols) |
721 | { |
769 | { |
722 | slog (L_TRACE, ">>%d PT_CONNECT_INFO(%d,%s)\n", |
770 | slog (L_TRACE, ">>%d PT_CONNECT_INFO(%d,%s)", |
723 | conf->id, rid, (const char *)rsi); |
771 | conf->id, rid, (const char *)rsi); |
724 | |
772 | |
725 | connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols); |
773 | connect_info_packet *r = new connect_info_packet (conf->id, rid, rsi, rprotocols); |
726 | |
774 | |
727 | r->hmac_set (octx); |
775 | r->hmac_set (octx); |
728 | send_vpn_packet (r, si); |
776 | send_vpn_packet (r, si); |
729 | |
777 | |
730 | delete r; |
778 | delete r; |
731 | } |
779 | } |
732 | |
780 | |
733 | void |
781 | inline void |
734 | connection::establish_connection_cb (time_watcher &w) |
782 | connection::establish_connection_cb (ev::timer &w, int revents) |
735 | { |
783 | { |
736 | if (!ictx |
784 | if (!ictx |
737 | && conf != THISNODE |
785 | && conf != THISNODE |
738 | && connectmode != conf_node::C_NEVER |
786 | && connectmode != conf_node::C_NEVER |
739 | && connectmode != conf_node::C_DISABLED |
787 | && connectmode != conf_node::C_DISABLED |
740 | && NOW > w.at) |
788 | && !w.is_active ()) |
741 | { |
789 | { |
742 | w.at = TSTAMP_MAX; // first disable this watcher in case of recursion |
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 | } |
743 | |
797 | |
744 | double retry_int = double (retry_cnt & 3 |
798 | ev::tstamp retry_int = ev::tstamp (retry_cnt & 3 |
745 | ? (retry_cnt & 3) + 1 |
799 | ? (retry_cnt & 3) + 1 |
746 | : 1 << (retry_cnt >> 2)); |
800 | : 1 << (retry_cnt >> 2)); |
747 | |
801 | |
748 | reset_si (); |
802 | reset_si (); |
749 | |
803 | |
750 | bool slow = si.prot & PROT_SLOW; |
804 | bool slow = si.prot & PROT_SLOW; |
751 | |
805 | |
752 | if (si.prot && !si.host) |
806 | if (si.prot && !si.host) |
|
|
807 | { |
|
|
808 | slog (L_TRACE, _("%s: connection request (indirect)"), conf->nodename); |
|
|
809 | /*TODO*/ /* start the timer so we don't recurse endlessly */ |
|
|
810 | w.start (1); |
753 | vpn->send_connect_request (conf->id); |
811 | vpn->send_connect_request (conf->id); |
|
|
812 | } |
754 | else |
813 | else |
755 | { |
814 | { |
|
|
815 | slog (L_TRACE, _("%s: connection request (direct)"), conf->nodename, !!ictx, !!octx); |
|
|
816 | |
756 | const sockinfo &dsi = forward_si (si); |
817 | const sockinfo &dsi = forward_si (si); |
757 | |
818 | |
758 | slow = slow || (dsi.prot & PROT_SLOW); |
819 | slow = slow || (dsi.prot & PROT_SLOW); |
759 | |
820 | |
760 | if (dsi.valid () && auth_rate_limiter.can (dsi)) |
821 | if (dsi.valid () && auth_rate_limiter.can (dsi)) |
… | |
… | |
771 | if (retry_int < conf->max_retry) |
832 | if (retry_int < conf->max_retry) |
772 | retry_cnt++; |
833 | retry_cnt++; |
773 | else |
834 | else |
774 | retry_int = conf->max_retry; |
835 | retry_int = conf->max_retry; |
775 | |
836 | |
776 | w.start (NOW + retry_int); |
837 | w.start (retry_int); |
777 | } |
838 | } |
778 | } |
839 | } |
779 | |
840 | |
780 | void |
841 | void |
781 | connection::reset_connection () |
842 | connection::reset_connection () |
… | |
… | |
784 | { |
845 | { |
785 | slog (L_INFO, _("%s(%s): connection lost"), |
846 | slog (L_INFO, _("%s(%s): connection lost"), |
786 | conf->nodename, (const char *)si); |
847 | conf->nodename, (const char *)si); |
787 | |
848 | |
788 | if (::conf.script_node_down) |
849 | if (::conf.script_node_down) |
789 | if (!run_script (run_script_cb (this, &connection::script_node_down), false)) |
850 | { |
|
|
851 | run_script_cb cb; |
|
|
852 | cb.set<connection, &connection::script_node_down> (this); |
|
|
853 | if (!run_script (cb, false)) |
790 | slog (L_WARN, _("node-down command execution failed, continuing.")); |
854 | slog (L_WARN, _("node-down command execution failed, continuing.")); |
|
|
855 | } |
791 | } |
856 | } |
792 | |
857 | |
793 | delete ictx; ictx = 0; |
858 | delete ictx; ictx = 0; |
794 | delete octx; octx = 0; |
859 | delete octx; octx = 0; |
795 | #if ENABLE_DNS |
860 | #if ENABLE_DNS |
… | |
… | |
813 | send_reset (si); |
878 | send_reset (si); |
814 | |
879 | |
815 | reset_connection (); |
880 | reset_connection (); |
816 | } |
881 | } |
817 | |
882 | |
818 | void |
883 | inline void |
819 | connection::rekey_cb (time_watcher &w) |
884 | connection::rekey_cb (ev::timer &w, int revents) |
820 | { |
885 | { |
821 | reset_connection (); |
886 | reset_connection (); |
822 | establish_connection (); |
887 | establish_connection (); |
823 | } |
888 | } |
824 | |
889 | |
… | |
… | |
868 | } |
933 | } |
869 | |
934 | |
870 | void |
935 | void |
871 | connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
936 | connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
872 | { |
937 | { |
873 | last_activity = NOW; |
938 | last_activity = ev_now (); |
874 | |
939 | |
875 | slog (L_NOISE, "<<%d received packet type %d from %d to %d", |
940 | slog (L_NOISE, "<<%d received packet type %d from %d to %d", |
876 | conf->id, pkt->typ (), pkt->src (), pkt->dst ()); |
941 | conf->id, pkt->typ (), pkt->src (), pkt->dst ()); |
|
|
942 | |
|
|
943 | if (connectmode == conf_node::C_DISABLED) |
|
|
944 | return; |
877 | |
945 | |
878 | switch (pkt->typ ()) |
946 | switch (pkt->typ ()) |
879 | { |
947 | { |
880 | case vpn_packet::PT_PING: |
948 | case vpn_packet::PT_PING: |
881 | // we send pings instead of auth packets after some retries, |
949 | // we send pings instead of auth packets after some retries, |
… | |
… | |
1016 | slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), |
1084 | slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), |
1017 | conf->nodename, (const char *)rsi, |
1085 | conf->nodename, (const char *)rsi, |
1018 | p->prot_major, p->prot_minor); |
1086 | p->prot_major, p->prot_minor); |
1019 | |
1087 | |
1020 | if (::conf.script_node_up) |
1088 | if (::conf.script_node_up) |
1021 | if (!run_script (run_script_cb (this, &connection::script_node_up), false)) |
1089 | { |
|
|
1090 | run_script_cb cb; |
|
|
1091 | cb.set<connection, &connection::script_node_up> (this); |
|
|
1092 | if (!run_script (cb, false)) |
1022 | slog (L_WARN, _("node-up command execution failed, continuing.")); |
1093 | slog (L_WARN, _("node-up command execution failed, continuing.")); |
|
|
1094 | } |
1023 | |
1095 | |
1024 | break; |
1096 | break; |
1025 | } |
1097 | } |
1026 | else |
1098 | else |
1027 | slog (L_ERR, _("%s(%s): sent and received challenge do not match"), |
1099 | slog (L_ERR, _("%s(%s): sent and received challenge do not match"), |
… | |
… | |
1087 | if (p->id > 0 && p->id <= vpn->conns.size ()) |
1159 | if (p->id > 0 && p->id <= vpn->conns.size ()) |
1088 | { |
1160 | { |
1089 | connection *c = vpn->conns[p->id - 1]; |
1161 | connection *c = vpn->conns[p->id - 1]; |
1090 | conf->protocols = p->protocols; |
1162 | conf->protocols = p->protocols; |
1091 | |
1163 | |
1092 | slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]\n", |
1164 | slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]", |
1093 | conf->id, p->id, c->ictx && c->octx); |
1165 | conf->id, p->id, c->ictx && c->octx); |
1094 | |
1166 | |
1095 | if (c->ictx && c->octx) |
1167 | if (c->ictx && c->octx) |
1096 | { |
1168 | { |
1097 | // send connect_info packets to both sides, in case one is |
1169 | // send connect_info packets to both sides, in case one is |
… | |
… | |
1143 | send_reset (rsi); |
1215 | send_reset (rsi); |
1144 | break; |
1216 | break; |
1145 | } |
1217 | } |
1146 | } |
1218 | } |
1147 | |
1219 | |
1148 | void connection::keepalive_cb (time_watcher &w) |
1220 | inline void |
|
|
1221 | connection::keepalive_cb (ev::timer &w, int revents) |
1149 | { |
1222 | { |
1150 | if (NOW >= last_activity + ::conf.keepalive + 30) |
1223 | if (ev_now () >= last_activity + ::conf.keepalive + 30) |
1151 | { |
1224 | { |
1152 | reset_connection (); |
1225 | reset_connection (); |
1153 | establish_connection (); |
1226 | establish_connection (); |
1154 | } |
1227 | } |
1155 | else if (NOW < last_activity + ::conf.keepalive) |
1228 | else if (ev_now () < last_activity + ::conf.keepalive) |
1156 | w.start (last_activity + ::conf.keepalive); |
1229 | w.start (last_activity + ::conf.keepalive - ev::now ()); |
1157 | else if (conf->connectmode != conf_node::C_ONDEMAND |
1230 | else if (conf->connectmode != conf_node::C_ONDEMAND |
1158 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
1231 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
1159 | { |
1232 | { |
1160 | send_ping (si); |
1233 | send_ping (si); |
1161 | w.start (NOW + 5); |
1234 | w.start (5); |
1162 | } |
1235 | } |
1163 | else if (NOW < last_activity + ::conf.keepalive + 10) |
1236 | else if (ev_now () < last_activity + ::conf.keepalive + 10) |
1164 | // hold ondemand connections implicitly a few seconds longer |
1237 | // hold ondemand connections implicitly a few seconds longer |
1165 | // should delete octx, though, or something like that ;) |
1238 | // should delete octx, though, or something like that ;) |
1166 | w.start (last_activity + ::conf.keepalive + 10); |
1239 | w.start (last_activity + ::conf.keepalive + 10 - ev::now ()); |
1167 | else |
1240 | else |
1168 | reset_connection (); |
1241 | reset_connection (); |
1169 | } |
1242 | } |
1170 | |
1243 | |
1171 | void connection::send_connect_request (int id) |
1244 | void connection::send_connect_request (int id) |
… | |
… | |
1198 | asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); |
1271 | asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); |
1199 | asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); |
1272 | asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); |
1200 | asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); |
1273 | asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); |
1201 | } |
1274 | } |
1202 | |
1275 | |
|
|
1276 | inline const char * |
1203 | const char *connection::script_node_up () |
1277 | connection::script_node_up () |
1204 | { |
1278 | { |
1205 | script_init_connect_env (); |
1279 | script_init_connect_env (); |
1206 | |
1280 | |
1207 | putenv ("STATE=up"); |
1281 | putenv ((char *)"STATE=up"); |
1208 | |
1282 | |
1209 | char *filename; |
1283 | char *filename; |
1210 | asprintf (&filename, |
1284 | asprintf (&filename, |
1211 | "%s/%s", |
1285 | "%s/%s", |
1212 | confbase, |
1286 | confbase, |
1213 | ::conf.script_node_up ? ::conf.script_node_up : "node-up"); |
1287 | ::conf.script_node_up ? ::conf.script_node_up : "node-up"); |
1214 | |
1288 | |
1215 | return filename; |
1289 | return filename; |
1216 | } |
1290 | } |
1217 | |
1291 | |
|
|
1292 | inline const char * |
1218 | const char *connection::script_node_down () |
1293 | connection::script_node_down () |
1219 | { |
1294 | { |
1220 | script_init_connect_env (); |
1295 | script_init_connect_env (); |
1221 | |
1296 | |
1222 | putenv ("STATE=down"); |
1297 | putenv ((char *)"STATE=down"); |
1223 | |
1298 | |
1224 | char *filename; |
1299 | char *filename; |
1225 | asprintf (&filename, |
1300 | asprintf (&filename, |
1226 | "%s/%s", |
1301 | "%s/%s", |
1227 | confbase, |
1302 | confbase, |
… | |
… | |
1229 | |
1304 | |
1230 | return filename; |
1305 | return filename; |
1231 | } |
1306 | } |
1232 | |
1307 | |
1233 | connection::connection (struct vpn *vpn, conf_node *conf) |
1308 | connection::connection (struct vpn *vpn, conf_node *conf) |
1234 | : vpn(vpn), conf(conf) |
1309 | : vpn(vpn), conf(conf), |
1235 | , rekey (this, &connection::rekey_cb) |
|
|
1236 | , keepalive (this, &connection::keepalive_cb) |
|
|
1237 | , establish_connection (this, &connection::establish_connection_cb) |
|
|
1238 | #if ENABLE_DNS |
1310 | #if ENABLE_DNS |
1239 | , dns (0) |
1311 | dns (0), |
1240 | #endif |
1312 | #endif |
|
|
1313 | data_queue(conf->max_ttl, conf->max_queue), |
|
|
1314 | vpn_queue(conf->max_ttl, conf->max_queue) |
1241 | { |
1315 | { |
|
|
1316 | rekey .set<connection, &connection::rekey_cb > (this); |
|
|
1317 | keepalive .set<connection, &connection::keepalive_cb > (this); |
|
|
1318 | establish_connection.set<connection, &connection::establish_connection_cb> (this); |
|
|
1319 | |
1242 | octx = ictx = 0; |
1320 | octx = ictx = 0; |
1243 | retry_cnt = 0; |
1321 | retry_cnt = 0; |
1244 | |
1322 | |
1245 | if (!conf->protocols) // make sure some protocol is enabled |
1323 | if (!conf->protocols) // make sure some protocol is enabled |
1246 | conf->protocols = PROT_UDPv4; |
1324 | conf->protocols = PROT_UDPv4; |
1247 | |
1325 | |
1248 | connectmode = conf_node::C_ALWAYS; // initial setting |
1326 | connectmode = conf->connectmode; |
|
|
1327 | |
|
|
1328 | // queue a dummy packet to force an initial connection attempt |
|
|
1329 | if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED) |
|
|
1330 | { |
|
|
1331 | net_packet *p = new net_packet; |
|
|
1332 | p->len = 0; |
|
|
1333 | vpn_queue.put (p); |
|
|
1334 | } |
|
|
1335 | |
1249 | reset_connection (); |
1336 | reset_connection (); |
1250 | } |
1337 | } |
1251 | |
1338 | |
1252 | connection::~connection () |
1339 | connection::~connection () |
1253 | { |
1340 | { |