1 | /* |
1 | /* |
2 | connection.C -- manage a single connection |
2 | connection.C -- manage a single connection |
3 | Copyright (C) 2003-2005 Marc Lehmann <gvpe@schmorp.de> |
3 | Copyright (C) 2003-2008 Marc Lehmann <gvpe@schmorp.de> |
4 | |
4 | |
5 | This file is part of GVPE. |
5 | This file is part of GVPE. |
6 | |
6 | |
7 | GVPE is free software; you can redistribute it and/or modify |
7 | GVPE is free software; you can redistribute it and/or modify it |
8 | it under the terms of the GNU General Public License as published by |
8 | under the terms of the GNU General Public License as published by the |
9 | the Free Software Foundation; either version 2 of the License, or |
9 | Free Software Foundation; either version 3 of the License, or (at your |
10 | (at your option) any later version. |
10 | option) any later version. |
11 | |
11 | |
12 | This program is distributed in the hope that it will be useful, |
12 | This program is distributed in the hope that it will be useful, but |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
15 | GNU General Public License for more details. |
15 | Public License for more details. |
16 | |
16 | |
17 | You should have received a copy of the GNU General Public License |
17 | You should have received a copy of the GNU General Public License along |
18 | along with gvpe; if not, write to the Free Software |
18 | with this program; if not, see <http://www.gnu.org/licenses/>. |
19 | Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 | |
|
|
20 | Additional permission under GNU GPL version 3 section 7 |
|
|
21 | |
|
|
22 | If you modify this Program, or any covered work, by linking or |
|
|
23 | combining it with the OpenSSL project's OpenSSL library (or a modified |
|
|
24 | version of that library), containing parts covered by the terms of the |
|
|
25 | OpenSSL or SSLeay licenses, the licensors of this Program grant you |
|
|
26 | additional permission to convey the resulting work. Corresponding |
|
|
27 | Source for a non-source form of such a combination shall include the |
|
|
28 | source code for the parts of OpenSSL used as well as that of the |
|
|
29 | covered work. |
20 | */ |
30 | */ |
21 | |
31 | |
22 | #include "config.h" |
32 | #include "config.h" |
23 | |
33 | |
24 | #include <list> |
34 | #include <list> |
… | |
… | |
793 | { |
803 | { |
794 | reset_connection (); |
804 | reset_connection (); |
795 | return; |
805 | return; |
796 | } |
806 | } |
797 | |
807 | |
|
|
808 | last_establish_attempt = ev_now (); |
|
|
809 | |
798 | ev::tstamp retry_int = ev::tstamp (retry_cnt & 3 |
810 | ev::tstamp retry_int = ev::tstamp (retry_cnt & 3 |
799 | ? (retry_cnt & 3) + 1 |
811 | ? (retry_cnt & 3) + 1 |
800 | : 1 << (retry_cnt >> 2)); |
812 | : 1 << (retry_cnt >> 2)); |
801 | |
813 | |
802 | reset_si (); |
814 | reset_si (); |
… | |
… | |
825 | else |
837 | else |
826 | send_ping (dsi, 0); |
838 | send_ping (dsi, 0); |
827 | } |
839 | } |
828 | } |
840 | } |
829 | |
841 | |
830 | retry_int *= slow ? 8. : 0.7; |
842 | retry_int *= slow ? 8. : 0.9; |
831 | |
843 | |
832 | if (retry_int < conf->max_retry) |
844 | if (retry_int < conf->max_retry) |
833 | retry_cnt++; |
845 | retry_cnt++; |
834 | else |
846 | else |
835 | retry_int = conf->max_retry; |
847 | retry_int = conf->max_retry; |
… | |
… | |
905 | if (oseqno > MAX_SEQNO) |
917 | if (oseqno > MAX_SEQNO) |
906 | rekey (); |
918 | rekey (); |
907 | } |
919 | } |
908 | |
920 | |
909 | void |
921 | void |
|
|
922 | connection::post_inject_queue () |
|
|
923 | { |
|
|
924 | // force a connection every now and when when packets are sent (max 1/s) |
|
|
925 | if (ev_now () - last_establish_attempt >= 0.95) // arbitrary |
|
|
926 | establish_connection.stop (); |
|
|
927 | |
|
|
928 | establish_connection (); |
|
|
929 | } |
|
|
930 | |
|
|
931 | void |
910 | connection::inject_data_packet (tap_packet *pkt, bool broadcast/*TODO DDD*/) |
932 | connection::inject_data_packet (tap_packet *pkt) |
911 | { |
933 | { |
912 | if (ictx && octx) |
934 | if (ictx && octx) |
913 | send_data_packet (pkt); |
935 | send_data_packet (pkt); |
914 | else |
936 | else |
915 | { |
937 | { |
916 | if (!broadcast) |
|
|
917 | data_queue.put (new tap_packet (*pkt)); |
938 | data_queue.put (new tap_packet (*pkt)); |
918 | |
939 | post_inject_queue (); |
919 | establish_connection (); |
|
|
920 | } |
940 | } |
921 | } |
941 | } |
922 | |
942 | |
923 | void connection::inject_vpn_packet (vpn_packet *pkt, int tos) |
943 | void connection::inject_vpn_packet (vpn_packet *pkt, int tos) |
924 | { |
944 | { |
925 | if (ictx && octx) |
945 | if (ictx && octx) |
926 | send_vpn_packet (pkt, si, tos); |
946 | send_vpn_packet (pkt, si, tos); |
927 | else |
947 | else |
928 | { |
948 | { |
929 | vpn_queue.put ((vpn_packet *)new data_packet (*(data_packet *)pkt)); |
949 | vpn_queue.put ((vpn_packet *)new data_packet (*(data_packet *)pkt)); |
930 | |
950 | post_inject_queue (); |
931 | establish_connection (); |
|
|
932 | } |
951 | } |
933 | } |
952 | } |
934 | |
953 | |
935 | void |
954 | void |
936 | connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
955 | connection::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
… | |
… | |
1315 | { |
1334 | { |
1316 | rekey .set<connection, &connection::rekey_cb > (this); |
1335 | rekey .set<connection, &connection::rekey_cb > (this); |
1317 | keepalive .set<connection, &connection::keepalive_cb > (this); |
1336 | keepalive .set<connection, &connection::keepalive_cb > (this); |
1318 | establish_connection.set<connection, &connection::establish_connection_cb> (this); |
1337 | establish_connection.set<connection, &connection::establish_connection_cb> (this); |
1319 | |
1338 | |
|
|
1339 | last_establish_attempt = 0.; |
1320 | octx = ictx = 0; |
1340 | octx = ictx = 0; |
1321 | retry_cnt = 0; |
|
|
1322 | |
1341 | |
1323 | if (!conf->protocols) // make sure some protocol is enabled |
1342 | if (!conf->protocols) // make sure some protocol is enabled |
1324 | conf->protocols = PROT_UDPv4; |
1343 | conf->protocols = PROT_UDPv4; |
1325 | |
1344 | |
1326 | connectmode = conf->connectmode; |
1345 | connectmode = conf->connectmode; |
1327 | |
1346 | |
1328 | // queue a dummy packet to force an initial connection attempt |
1347 | // queue a dummy packet to force an initial connection attempt |
1329 | if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED) |
1348 | if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED) |
1330 | { |
1349 | vpn_queue.put (new net_packet); |
1331 | net_packet *p = new net_packet; |
|
|
1332 | p->len = 0; |
|
|
1333 | vpn_queue.put (p); |
|
|
1334 | } |
|
|
1335 | |
1350 | |
1336 | reset_connection (); |
1351 | reset_connection (); |
1337 | } |
1352 | } |
1338 | |
1353 | |
1339 | connection::~connection () |
1354 | connection::~connection () |