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 | |
4 | |
4 | 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 |
5 | 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 |
6 | the Free Software Foundation; either version 2 of the License, or |
7 | the Free Software Foundation; either version 2 of the License, or |
7 | (at your option) any later version. |
8 | (at your option) any later version. |
… | |
… | |
35 | #include "slog.h" |
36 | #include "slog.h" |
36 | #include "device.h" |
37 | #include "device.h" |
37 | #include "vpn.h" |
38 | #include "vpn.h" |
38 | #include "connection.h" |
39 | #include "connection.h" |
39 | |
40 | |
40 | #include <sys/socket.h> |
41 | #include "netcompat.h" |
41 | #ifdef HAVE_NETINET_IN_H |
|
|
42 | # include <netinet/in.h> |
|
|
43 | #endif |
|
|
44 | #include <arpa/inet.h> |
|
|
45 | #include <net/if.h> |
|
|
46 | #ifdef HAVE_NETINET_IN_SYSTM_H |
|
|
47 | # include <netinet/in_systm.h> |
|
|
48 | #endif |
|
|
49 | #ifdef HAVE_NETINET_IP_H |
|
|
50 | # include <netinet/ip.h> |
|
|
51 | #endif |
|
|
52 | |
|
|
53 | #ifndef IPTOS_TOS_MASK |
|
|
54 | # define IPTOS_TOS_MASK (IPTOS_LOWDELAY | IPTOS_THROUGHPUT | IPTOS_RELIABILITY | IPTOS_MINCOST) |
|
|
55 | #endif |
|
|
56 | |
42 | |
57 | #if !HAVE_RAND_PSEUDO_BYTES |
43 | #if !HAVE_RAND_PSEUDO_BYTES |
58 | # define RAND_pseudo_bytes RAND_bytes |
44 | # define RAND_pseudo_bytes RAND_bytes |
59 | #endif |
45 | #endif |
60 | |
46 | |
… | |
… | |
815 | reset_connection (); |
801 | reset_connection (); |
816 | establish_connection (); |
802 | establish_connection (); |
817 | } |
803 | } |
818 | |
804 | |
819 | void |
805 | void |
820 | connection::send_data_packet (tap_packet *pkt, bool broadcast) |
806 | connection::send_data_packet (tap_packet *pkt) |
821 | { |
807 | { |
822 | vpndata_packet *p = new vpndata_packet; |
808 | vpndata_packet *p = new vpndata_packet; |
823 | int tos = 0; |
809 | int tos = 0; |
824 | |
810 | |
825 | // I am not hilarious about peeking into packets, but so be it. |
811 | // I am not hilarious about peeking into packets, but so be it. |
826 | if (conf->inherit_tos |
812 | if (conf->inherit_tos && pkt->is_ipv4 ()) |
827 | && (*pkt)[12] == 0x08 && (*pkt)[13] == 0x00 // IP |
|
|
828 | && ((*pkt)[14] & 0xf0) == 0x40) // IPv4 |
|
|
829 | tos = (*pkt)[15] & IPTOS_TOS_MASK; |
813 | tos = (*pkt)[15] & IPTOS_TOS_MASK; |
830 | |
814 | |
831 | p->setup (this, broadcast ? 0 : conf->id, &((*pkt)[6 + 6]), pkt->len - 6 - 6, ++oseqno); // skip 2 macs |
815 | p->setup (this, conf->id, &((*pkt)[6 + 6]), pkt->len - 6 - 6, ++oseqno); // skip 2 macs |
832 | send_vpn_packet (p, si, tos); |
816 | send_vpn_packet (p, si, tos); |
833 | |
817 | |
834 | delete p; |
818 | delete p; |
835 | |
819 | |
836 | if (oseqno > MAX_SEQNO) |
820 | if (oseqno > MAX_SEQNO) |
837 | rekey (); |
821 | rekey (); |
838 | } |
822 | } |
839 | |
823 | |
840 | void |
824 | void |
841 | connection::inject_data_packet (tap_packet *pkt, bool broadcast) |
825 | connection::inject_data_packet (tap_packet *pkt, bool broadcast/*TODO DDD*/) |
842 | { |
826 | { |
843 | if (ictx && octx) |
827 | if (ictx && octx) |
844 | send_data_packet (pkt, broadcast); |
828 | send_data_packet (pkt); |
845 | else |
829 | else |
846 | { |
830 | { |
847 | if (!broadcast)//DDDD |
831 | if (!broadcast)//DDDD |
848 | data_queue.put (new tap_packet (*pkt)); |
832 | data_queue.put (new tap_packet (*pkt)); |
849 | |
833 | |
… | |
… | |
1052 | |
1036 | |
1053 | if (iseqno.recv_ok (seqno)) |
1037 | if (iseqno.recv_ok (seqno)) |
1054 | { |
1038 | { |
1055 | vpn->tap->send (d); |
1039 | vpn->tap->send (d); |
1056 | |
1040 | |
1057 | if (p->dst () == 0) // re-broadcast |
|
|
1058 | for (vpn::conns_vector::iterator i = vpn->conns.begin (); i != vpn->conns.end (); ++i) |
|
|
1059 | { |
|
|
1060 | connection *c = *i; |
|
|
1061 | |
|
|
1062 | if (c->conf != THISNODE && c->conf != conf) |
|
|
1063 | c->inject_data_packet (d); |
|
|
1064 | } |
|
|
1065 | |
|
|
1066 | if (si != rsi) |
1041 | if (si != rsi) |
1067 | { |
1042 | { |
1068 | // fast re-sync on connection changes, useful especially for tcp/ip |
1043 | // fast re-sync on connection changes, useful especially for tcp/ip |
1069 | si = rsi; |
1044 | si = rsi; |
1070 | |
1045 | |