… | |
… | |
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 | |
… | |
… | |
582 | ///////////////////////////////////////////////////////////////////////////// |
619 | ///////////////////////////////////////////////////////////////////////////// |
583 | |
620 | |
584 | void |
621 | void |
585 | connection::connection_established () |
622 | connection::connection_established () |
586 | { |
623 | { |
|
|
624 | slog (L_TRACE, _("%s: possible connection establish (ictx %d, octx %d)"), conf->nodename, !!ictx, !!octx); |
|
|
625 | |
587 | if (ictx && octx) |
626 | if (ictx && octx) |
588 | { |
627 | { |
589 | connectmode = conf->connectmode; |
|
|
590 | |
|
|
591 | // make sure rekeying timeouts are slightly asymmetric |
628 | // make sure rekeying timeouts are slightly asymmetric |
592 | rekey.start (NOW + ::conf.rekey |
629 | ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0); |
593 | + (conf->id > THISNODE->id ? 10 : 0)); |
630 | rekey.start (rekey_interval, rekey_interval); |
594 | keepalive.start (NOW + ::conf.keepalive); |
631 | keepalive.start (::conf.keepalive); |
595 | |
632 | |
596 | // send queued packets |
633 | // send queued packets |
597 | if (ictx && octx) |
634 | if (ictx && octx) |
598 | { |
635 | { |
599 | while (tap_packet *p = (tap_packet *)data_queue.get ()) |
636 | while (tap_packet *p = (tap_packet *)data_queue.get ()) |
600 | { |
637 | { |
601 | send_data_packet (p); |
638 | if (p->len) send_data_packet (p); |
602 | delete p; |
639 | delete p; |
603 | } |
640 | } |
604 | |
641 | |
605 | while (vpn_packet *p = (vpn_packet *)vpn_queue.get ()) |
642 | while (vpn_packet *p = (vpn_packet *)vpn_queue.get ()) |
606 | { |
643 | { |
607 | send_vpn_packet (p, si, IPTOS_RELIABILITY); |
644 | if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY); |
608 | delete p; |
645 | delete p; |
609 | } |
646 | } |
610 | } |
647 | } |
611 | } |
648 | } |
612 | else |
649 | else |
613 | { |
650 | { |
614 | retry_cnt = 0; |
651 | retry_cnt = 0; |
615 | establish_connection.start (NOW + 5); |
652 | establish_connection.start (5); |
616 | keepalive.stop (); |
653 | keepalive.stop (); |
617 | rekey.stop (); |
654 | rekey.stop (); |
618 | } |
655 | } |
619 | } |
656 | } |
620 | |
657 | |
… | |
… | |
739 | send_vpn_packet (r, si); |
776 | send_vpn_packet (r, si); |
740 | |
777 | |
741 | delete r; |
778 | delete r; |
742 | } |
779 | } |
743 | |
780 | |
744 | void |
781 | inline void |
745 | connection::establish_connection_cb (time_watcher &w) |
782 | connection::establish_connection_cb (ev::timer &w, int revents) |
746 | { |
783 | { |
747 | if (!ictx |
784 | if (!ictx |
748 | && conf != THISNODE |
785 | && conf != THISNODE |
749 | && connectmode != conf_node::C_NEVER |
786 | && connectmode != conf_node::C_NEVER |
750 | && connectmode != conf_node::C_DISABLED |
787 | && connectmode != conf_node::C_DISABLED |
751 | && NOW > w.at) |
788 | && !w.is_active ()) |
752 | { |
789 | { |
753 | 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 | } |
754 | |
797 | |
755 | double retry_int = double (retry_cnt & 3 |
798 | last_establish_attempt = ev_now (); |
|
|
799 | |
|
|
800 | ev::tstamp retry_int = ev::tstamp (retry_cnt & 3 |
756 | ? (retry_cnt & 3) + 1 |
801 | ? (retry_cnt & 3) + 1 |
757 | : 1 << (retry_cnt >> 2)); |
802 | : 1 << (retry_cnt >> 2)); |
758 | |
803 | |
759 | reset_si (); |
804 | reset_si (); |
760 | |
805 | |
761 | bool slow = si.prot & PROT_SLOW; |
806 | bool slow = si.prot & PROT_SLOW; |
762 | |
807 | |
763 | if (si.prot && !si.host) |
808 | if (si.prot && !si.host) |
|
|
809 | { |
|
|
810 | slog (L_TRACE, _("%s: connection request (indirect)"), conf->nodename); |
|
|
811 | /*TODO*/ /* start the timer so we don't recurse endlessly */ |
|
|
812 | w.start (1); |
764 | vpn->send_connect_request (conf->id); |
813 | vpn->send_connect_request (conf->id); |
|
|
814 | } |
765 | else |
815 | else |
766 | { |
816 | { |
|
|
817 | slog (L_TRACE, _("%s: connection request (direct)"), conf->nodename, !!ictx, !!octx); |
|
|
818 | |
767 | const sockinfo &dsi = forward_si (si); |
819 | const sockinfo &dsi = forward_si (si); |
768 | |
820 | |
769 | slow = slow || (dsi.prot & PROT_SLOW); |
821 | slow = slow || (dsi.prot & PROT_SLOW); |
770 | |
822 | |
771 | if (dsi.valid () && auth_rate_limiter.can (dsi)) |
823 | if (dsi.valid () && auth_rate_limiter.can (dsi)) |
… | |
… | |
775 | else |
827 | else |
776 | send_ping (dsi, 0); |
828 | send_ping (dsi, 0); |
777 | } |
829 | } |
778 | } |
830 | } |
779 | |
831 | |
780 | retry_int *= slow ? 8. : 0.7; |
832 | retry_int *= slow ? 8. : 0.9; |
781 | |
833 | |
782 | if (retry_int < conf->max_retry) |
834 | if (retry_int < conf->max_retry) |
783 | retry_cnt++; |
835 | retry_cnt++; |
784 | else |
836 | else |
785 | retry_int = conf->max_retry; |
837 | retry_int = conf->max_retry; |
786 | |
838 | |
787 | w.start (NOW + retry_int); |
839 | w.start (retry_int); |
788 | } |
840 | } |
789 | } |
841 | } |
790 | |
842 | |
791 | void |
843 | void |
792 | connection::reset_connection () |
844 | connection::reset_connection () |
… | |
… | |
795 | { |
847 | { |
796 | slog (L_INFO, _("%s(%s): connection lost"), |
848 | slog (L_INFO, _("%s(%s): connection lost"), |
797 | conf->nodename, (const char *)si); |
849 | conf->nodename, (const char *)si); |
798 | |
850 | |
799 | if (::conf.script_node_down) |
851 | if (::conf.script_node_down) |
800 | 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)) |
801 | slog (L_WARN, _("node-down command execution failed, continuing.")); |
856 | slog (L_WARN, _("node-down command execution failed, continuing.")); |
|
|
857 | } |
802 | } |
858 | } |
803 | |
859 | |
804 | delete ictx; ictx = 0; |
860 | delete ictx; ictx = 0; |
805 | delete octx; octx = 0; |
861 | delete octx; octx = 0; |
806 | #if ENABLE_DNS |
862 | #if ENABLE_DNS |
… | |
… | |
824 | send_reset (si); |
880 | send_reset (si); |
825 | |
881 | |
826 | reset_connection (); |
882 | reset_connection (); |
827 | } |
883 | } |
828 | |
884 | |
829 | void |
885 | inline void |
830 | connection::rekey_cb (time_watcher &w) |
886 | connection::rekey_cb (ev::timer &w, int revents) |
831 | { |
887 | { |
832 | reset_connection (); |
888 | reset_connection (); |
833 | establish_connection (); |
889 | establish_connection (); |
834 | } |
890 | } |
835 | |
891 | |
… | |
… | |
851 | if (oseqno > MAX_SEQNO) |
907 | if (oseqno > MAX_SEQNO) |
852 | rekey (); |
908 | rekey (); |
853 | } |
909 | } |
854 | |
910 | |
855 | void |
911 | void |
|
|
912 | connection::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 | |
|
|
921 | void |
856 | connection::inject_data_packet (tap_packet *pkt, bool broadcast/*TODO DDD*/) |
922 | connection::inject_data_packet (tap_packet *pkt) |
857 | { |
923 | { |
858 | if (ictx && octx) |
924 | if (ictx && octx) |
859 | send_data_packet (pkt); |
925 | send_data_packet (pkt); |
860 | else |
926 | else |
861 | { |
927 | { |
862 | if (!broadcast) |
|
|
863 | data_queue.put (new tap_packet (*pkt)); |
928 | data_queue.put (new tap_packet (*pkt)); |
864 | |
929 | post_inject_queue (); |
865 | establish_connection (); |
|
|
866 | } |
930 | } |
867 | } |
931 | } |
868 | |
932 | |
869 | void connection::inject_vpn_packet (vpn_packet *pkt, int tos) |
933 | void connection::inject_vpn_packet (vpn_packet *pkt, int tos) |
870 | { |
934 | { |
871 | if (ictx && octx) |
935 | if (ictx && octx) |
872 | send_vpn_packet (pkt, si, tos); |
936 | send_vpn_packet (pkt, si, tos); |
873 | else |
937 | else |
874 | { |
938 | { |
875 | vpn_queue.put ((vpn_packet *)new data_packet (*(data_packet *)pkt)); |
939 | vpn_queue.put ((vpn_packet *)new data_packet (*(data_packet *)pkt)); |
876 | |
940 | post_inject_queue (); |
877 | establish_connection (); |
|
|
878 | } |
941 | } |
879 | } |
942 | } |
880 | |
943 | |
881 | void |
944 | void |
882 | connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
945 | connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
883 | { |
946 | { |
884 | last_activity = NOW; |
947 | last_activity = ev_now (); |
885 | |
948 | |
886 | slog (L_NOISE, "<<%d received packet type %d from %d to %d", |
949 | slog (L_NOISE, "<<%d received packet type %d from %d to %d", |
887 | conf->id, pkt->typ (), pkt->src (), pkt->dst ()); |
950 | conf->id, pkt->typ (), pkt->src (), pkt->dst ()); |
|
|
951 | |
|
|
952 | if (connectmode == conf_node::C_DISABLED) |
|
|
953 | return; |
888 | |
954 | |
889 | switch (pkt->typ ()) |
955 | switch (pkt->typ ()) |
890 | { |
956 | { |
891 | case vpn_packet::PT_PING: |
957 | case vpn_packet::PT_PING: |
892 | // we send pings instead of auth packets after some retries, |
958 | // we send pings instead of auth packets after some retries, |
… | |
… | |
1027 | slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), |
1093 | slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), |
1028 | conf->nodename, (const char *)rsi, |
1094 | conf->nodename, (const char *)rsi, |
1029 | p->prot_major, p->prot_minor); |
1095 | p->prot_major, p->prot_minor); |
1030 | |
1096 | |
1031 | if (::conf.script_node_up) |
1097 | if (::conf.script_node_up) |
1032 | 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)) |
1033 | slog (L_WARN, _("node-up command execution failed, continuing.")); |
1102 | slog (L_WARN, _("node-up command execution failed, continuing.")); |
|
|
1103 | } |
1034 | |
1104 | |
1035 | break; |
1105 | break; |
1036 | } |
1106 | } |
1037 | else |
1107 | else |
1038 | 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"), |
… | |
… | |
1154 | send_reset (rsi); |
1224 | send_reset (rsi); |
1155 | break; |
1225 | break; |
1156 | } |
1226 | } |
1157 | } |
1227 | } |
1158 | |
1228 | |
1159 | void connection::keepalive_cb (time_watcher &w) |
1229 | inline void |
|
|
1230 | connection::keepalive_cb (ev::timer &w, int revents) |
1160 | { |
1231 | { |
1161 | if (NOW >= last_activity + ::conf.keepalive + 30) |
1232 | if (ev_now () >= last_activity + ::conf.keepalive + 30) |
1162 | { |
1233 | { |
1163 | reset_connection (); |
1234 | reset_connection (); |
1164 | establish_connection (); |
1235 | establish_connection (); |
1165 | } |
1236 | } |
1166 | else if (NOW < last_activity + ::conf.keepalive) |
1237 | else if (ev_now () < last_activity + ::conf.keepalive) |
1167 | w.start (last_activity + ::conf.keepalive); |
1238 | w.start (last_activity + ::conf.keepalive - ev::now ()); |
1168 | else if (conf->connectmode != conf_node::C_ONDEMAND |
1239 | else if (conf->connectmode != conf_node::C_ONDEMAND |
1169 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
1240 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
1170 | { |
1241 | { |
1171 | send_ping (si); |
1242 | send_ping (si); |
1172 | w.start (NOW + 5); |
1243 | w.start (5); |
1173 | } |
1244 | } |
1174 | else if (NOW < last_activity + ::conf.keepalive + 10) |
1245 | else if (ev_now () < last_activity + ::conf.keepalive + 10) |
1175 | // hold ondemand connections implicitly a few seconds longer |
1246 | // hold ondemand connections implicitly a few seconds longer |
1176 | // should delete octx, though, or something like that ;) |
1247 | // should delete octx, though, or something like that ;) |
1177 | w.start (last_activity + ::conf.keepalive + 10); |
1248 | w.start (last_activity + ::conf.keepalive + 10 - ev::now ()); |
1178 | else |
1249 | else |
1179 | reset_connection (); |
1250 | reset_connection (); |
1180 | } |
1251 | } |
1181 | |
1252 | |
1182 | void connection::send_connect_request (int id) |
1253 | void connection::send_connect_request (int id) |
… | |
… | |
1209 | asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); |
1280 | asprintf (&env, "DESTNODE=%s", conf->nodename); putenv (env); |
1210 | asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); |
1281 | asprintf (&env, "DESTIP=%s", si.ntoa ()); putenv (env); |
1211 | asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); |
1282 | asprintf (&env, "DESTPORT=%d", ntohs (si.port)); putenv (env); |
1212 | } |
1283 | } |
1213 | |
1284 | |
|
|
1285 | inline const char * |
1214 | const char *connection::script_node_up () |
1286 | connection::script_node_up () |
1215 | { |
1287 | { |
1216 | script_init_connect_env (); |
1288 | script_init_connect_env (); |
1217 | |
1289 | |
1218 | putenv ("STATE=up"); |
1290 | putenv ((char *)"STATE=up"); |
1219 | |
1291 | |
1220 | char *filename; |
1292 | char *filename; |
1221 | asprintf (&filename, |
1293 | asprintf (&filename, |
1222 | "%s/%s", |
1294 | "%s/%s", |
1223 | confbase, |
1295 | confbase, |
1224 | ::conf.script_node_up ? ::conf.script_node_up : "node-up"); |
1296 | ::conf.script_node_up ? ::conf.script_node_up : "node-up"); |
1225 | |
1297 | |
1226 | return filename; |
1298 | return filename; |
1227 | } |
1299 | } |
1228 | |
1300 | |
|
|
1301 | inline const char * |
1229 | const char *connection::script_node_down () |
1302 | connection::script_node_down () |
1230 | { |
1303 | { |
1231 | script_init_connect_env (); |
1304 | script_init_connect_env (); |
1232 | |
1305 | |
1233 | putenv ("STATE=down"); |
1306 | putenv ((char *)"STATE=down"); |
1234 | |
1307 | |
1235 | char *filename; |
1308 | char *filename; |
1236 | asprintf (&filename, |
1309 | asprintf (&filename, |
1237 | "%s/%s", |
1310 | "%s/%s", |
1238 | confbase, |
1311 | confbase, |
… | |
… | |
1240 | |
1313 | |
1241 | return filename; |
1314 | return filename; |
1242 | } |
1315 | } |
1243 | |
1316 | |
1244 | connection::connection (struct vpn *vpn, conf_node *conf) |
1317 | connection::connection (struct vpn *vpn, conf_node *conf) |
1245 | : vpn(vpn), conf(conf) |
1318 | : vpn(vpn), conf(conf), |
1246 | , rekey (this, &connection::rekey_cb) |
|
|
1247 | , keepalive (this, &connection::keepalive_cb) |
|
|
1248 | , establish_connection (this, &connection::establish_connection_cb) |
|
|
1249 | #if ENABLE_DNS |
1319 | #if ENABLE_DNS |
1250 | , dns (0) |
1320 | dns (0), |
1251 | #endif |
1321 | #endif |
|
|
1322 | data_queue(conf->max_ttl, conf->max_queue), |
|
|
1323 | vpn_queue(conf->max_ttl, conf->max_queue) |
1252 | { |
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.; |
1253 | octx = ictx = 0; |
1330 | octx = ictx = 0; |
1254 | retry_cnt = 0; |
|
|
1255 | |
1331 | |
1256 | if (!conf->protocols) // make sure some protocol is enabled |
1332 | if (!conf->protocols) // make sure some protocol is enabled |
1257 | conf->protocols = PROT_UDPv4; |
1333 | conf->protocols = PROT_UDPv4; |
1258 | |
1334 | |
1259 | 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 | |
1260 | reset_connection (); |
1341 | reset_connection (); |
1261 | } |
1342 | } |
1262 | |
1343 | |
1263 | connection::~connection () |
1344 | connection::~connection () |
1264 | { |
1345 | { |