1 | /* |
1 | /* |
2 | connection.C -- manage a single connection |
2 | connection.C -- manage a single connection |
3 | Copyright (C) 2003 Marc Lehmann <pcg@goof.com> |
3 | Copyright (C) 2003-2004 Marc Lehmann <pcg@goof.com> |
4 | |
4 | |
5 | This program is free software; you can redistribute it and/or modify |
5 | This program is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published by |
6 | it under the terms of the GNU General Public License as published by |
7 | the Free Software Foundation; either version 2 of the License, or |
7 | the Free Software Foundation; either version 2 of the License, or |
8 | (at your option) any later version. |
8 | (at your option) any later version. |
… | |
… | |
21 | |
21 | |
22 | extern "C" { |
22 | extern "C" { |
23 | # include "lzf/lzf.h" |
23 | # include "lzf/lzf.h" |
24 | } |
24 | } |
25 | |
25 | |
|
|
26 | #include <cassert> |
|
|
27 | |
26 | #include <list> |
28 | #include <list> |
27 | |
29 | |
28 | #include <openssl/rand.h> |
30 | #include <openssl/rand.h> |
29 | #include <openssl/evp.h> |
31 | #include <openssl/evp.h> |
30 | #include <openssl/rsa.h> |
32 | #include <openssl/rsa.h> |
… | |
… | |
134 | |
136 | |
135 | } rsa_cache; |
137 | } rsa_cache; |
136 | |
138 | |
137 | void rsa_cache::cleaner_cb (time_watcher &w) |
139 | void rsa_cache::cleaner_cb (time_watcher &w) |
138 | { |
140 | { |
139 | if (empty ()) |
141 | if (!empty ()) |
140 | w.at = TSTAMP_CANCEL; |
|
|
141 | else |
|
|
142 | { |
142 | { |
143 | w.at = NOW + RSA_TTL; |
143 | w.start (NOW + RSA_TTL); |
144 | |
144 | |
145 | for (iterator i = begin (); i != end (); ) |
145 | for (iterator i = begin (); i != end (); ) |
146 | if (i->expire <= NOW) |
146 | if (i->expire <= NOW) |
147 | i = erase (i); |
147 | i = erase (i); |
148 | else |
148 | else |
… | |
… | |
598 | } |
598 | } |
599 | else |
599 | else |
600 | { |
600 | { |
601 | retry_cnt = 0; |
601 | retry_cnt = 0; |
602 | establish_connection.start (NOW + 5); |
602 | establish_connection.start (NOW + 5); |
603 | keepalive.reset (); |
603 | keepalive.stop (); |
604 | rekey.reset (); |
604 | rekey.stop (); |
605 | } |
605 | } |
606 | } |
606 | } |
607 | |
607 | |
608 | void |
608 | void |
609 | connection::reset_si () |
609 | connection::reset_si () |
… | |
… | |
720 | } |
720 | } |
721 | |
721 | |
722 | void |
722 | void |
723 | connection::establish_connection_cb (time_watcher &w) |
723 | connection::establish_connection_cb (time_watcher &w) |
724 | { |
724 | { |
725 | if (ictx || conf == THISNODE |
725 | if (!ictx |
|
|
726 | && conf != THISNODE |
726 | || connectmode == conf_node::C_NEVER |
727 | && connectmode != conf_node::C_NEVER |
727 | || connectmode == conf_node::C_DISABLED) |
728 | && connectmode != conf_node::C_DISABLED |
728 | w.at = TSTAMP_CANCEL; |
729 | && w.at <= NOW) |
729 | else if (w.at <= NOW) |
|
|
730 | { |
730 | { |
731 | double retry_int = double (retry_cnt & 3 ? (retry_cnt & 3) : 1 << (retry_cnt >> 2)) * 0.6; |
731 | double retry_int = double (retry_cnt & 3 ? (retry_cnt & 3) : 1 << (retry_cnt >> 2)) * 0.6; |
732 | |
732 | |
733 | if (retry_int < 3600 * 8) |
733 | if (retry_int < 3600 * 8) |
734 | retry_cnt++; |
734 | retry_cnt++; |
735 | |
735 | |
736 | w.at = NOW + retry_int; |
736 | w.start (NOW + retry_int); |
737 | |
737 | |
738 | reset_si (); |
738 | reset_si (); |
739 | |
739 | |
740 | if (si.prot && !si.host) |
740 | if (si.prot && !si.host) |
741 | vpn->send_connect_request (conf->id); |
741 | vpn->send_connect_request (conf->id); |
… | |
… | |
772 | si.host= 0; |
772 | si.host= 0; |
773 | |
773 | |
774 | last_activity = 0; |
774 | last_activity = 0; |
775 | retry_cnt = 0; |
775 | retry_cnt = 0; |
776 | |
776 | |
777 | rekey.reset (); |
777 | rekey.stop (); |
778 | keepalive.reset (); |
778 | keepalive.stop (); |
779 | establish_connection.reset (); |
779 | establish_connection.stop (); |
780 | } |
780 | } |
781 | |
781 | |
782 | void |
782 | void |
783 | connection::shutdown () |
783 | connection::shutdown () |
784 | { |
784 | { |
… | |
… | |
789 | } |
789 | } |
790 | |
790 | |
791 | void |
791 | void |
792 | connection::rekey_cb (time_watcher &w) |
792 | connection::rekey_cb (time_watcher &w) |
793 | { |
793 | { |
794 | w.at = TSTAMP_CANCEL; |
|
|
795 | |
|
|
796 | reset_connection (); |
794 | reset_connection (); |
797 | establish_connection (); |
795 | establish_connection (); |
798 | } |
796 | } |
799 | |
797 | |
800 | void |
798 | void |
… | |
… | |
1114 | { |
1112 | { |
1115 | reset_connection (); |
1113 | reset_connection (); |
1116 | establish_connection (); |
1114 | establish_connection (); |
1117 | } |
1115 | } |
1118 | else if (NOW < last_activity + ::conf.keepalive) |
1116 | else if (NOW < last_activity + ::conf.keepalive) |
1119 | w.at = last_activity + ::conf.keepalive; |
1117 | w.start (last_activity + ::conf.keepalive); |
1120 | else if (conf->connectmode != conf_node::C_ONDEMAND |
1118 | else if (conf->connectmode != conf_node::C_ONDEMAND |
1121 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
1119 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
1122 | { |
1120 | { |
1123 | send_ping (si); |
1121 | send_ping (si); |
1124 | w.at = NOW + 5; |
1122 | w.start (NOW + 5); |
1125 | } |
1123 | } |
1126 | else if (NOW < last_activity + ::conf.keepalive + 10) |
1124 | else if (NOW < last_activity + ::conf.keepalive + 10) |
1127 | // hold ondemand connections implicitly a few seconds longer |
1125 | // hold ondemand connections implicitly a few seconds longer |
1128 | // should delete octx, though, or something like that ;) |
1126 | // should delete octx, though, or something like that ;) |
1129 | w.at = last_activity + ::conf.keepalive + 10; |
1127 | w.start (last_activity + ::conf.keepalive + 10); |
1130 | else |
1128 | else |
1131 | reset_connection (); |
1129 | reset_connection (); |
1132 | } |
1130 | } |
1133 | |
1131 | |
1134 | void connection::send_connect_request (int id) |
1132 | void connection::send_connect_request (int id) |