BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file server.h |
||
3 | * @author Ambroz Bizjak <ambrop7@gmail.com> |
||
4 | * |
||
5 | * @section LICENSE |
||
6 | * |
||
7 | * Redistribution and use in source and binary forms, with or without |
||
8 | * modification, are permitted provided that the following conditions are met: |
||
9 | * 1. Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * 2. Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * 3. Neither the name of the author nor the |
||
15 | * names of its contributors may be used to endorse or promote products |
||
16 | * derived from this software without specific prior written permission. |
||
17 | * |
||
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
21 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
||
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
||
25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||
27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
28 | */ |
||
29 | |||
30 | #include <stdint.h> |
||
31 | |||
32 | #include <protocol/scproto.h> |
||
33 | #include <structure/LinkedList1.h> |
||
34 | #include <structure/BAVL.h> |
||
35 | #include <flow/PacketProtoDecoder.h> |
||
36 | #include <flow/PacketStreamSender.h> |
||
37 | #include <flow/PacketPassPriorityQueue.h> |
||
38 | #include <flow/PacketPassFairQueue.h> |
||
39 | #include <flow/PacketProtoFlow.h> |
||
40 | #include <system/BReactor.h> |
||
41 | #include <system/BConnection.h> |
||
42 | #include <nspr_support/BSSLConnection.h> |
||
43 | |||
44 | // name of the program |
||
45 | #define PROGRAM_NAME "server" |
||
46 | |||
47 | // maxiumum number of connected clients. Must be <=2^16. |
||
48 | #define DEFAULT_MAX_CLIENTS 30 |
||
49 | // client output control flow buffer size in packets |
||
50 | // it must hold: initdata, newclient's, endclient's (if other peers die when informing them) |
||
51 | // make it big enough to hold the initial packet burst (initdata, newclient's), |
||
52 | #define CLIENT_CONTROL_BUFFER_MIN_PACKETS (1 + 2*(MAX_CLIENTS - 1)) |
||
53 | // size of client-to-client buffers in packets |
||
54 | #define CLIENT_PEER_FLOW_BUFFER_MIN_PACKETS 10 |
||
55 | // after how long of not hearing anything from the client we disconnect it |
||
56 | #define CLIENT_NO_DATA_TIME_LIMIT 30000 |
||
57 | // SO_SNDBFUF socket option for clients |
||
58 | #define CLIENT_DEFAULT_SOCKET_SNDBUF 16384 |
||
59 | // reset time when a buffer runs out or when we get the resetpeer message |
||
60 | #define CLIENT_RESET_TIME 30000 |
||
61 | |||
62 | // maxiumum listen addresses |
||
63 | #define MAX_LISTEN_ADDRS 16 |
||
64 | |||
65 | //#define SIMULATE_OUT_OF_CONTROL_BUFFER 20 |
||
66 | //#define SIMULATE_OUT_OF_FLOW_BUFFER 100 |
||
67 | |||
68 | |||
69 | // performing SSL handshake |
||
70 | #define INITSTATUS_HANDSHAKE 1 |
||
71 | // waiting for clienthello |
||
72 | #define INITSTATUS_WAITHELLO 2 |
||
73 | // initialisation was complete |
||
74 | #define INITSTATUS_COMPLETE 3 |
||
75 | |||
76 | #define INITSTATUS_HASLINK(status) ((status) == INITSTATUS_WAITHELLO || (status) == INITSTATUS_COMPLETE) |
||
77 | |||
78 | struct client_data; |
||
79 | struct peer_know; |
||
80 | |||
81 | struct peer_flow { |
||
82 | // source client |
||
83 | struct client_data *src_client; |
||
84 | // destination client |
||
85 | struct client_data *dest_client; |
||
86 | peerid_t dest_client_id; |
||
87 | // node in source client hash table (by destination), only when src_client != NULL |
||
88 | BAVLNode src_tree_node; |
||
89 | // node in source client list, only when src_client != NULL |
||
90 | LinkedList1Node src_list_node; |
||
91 | // node in destination client list |
||
92 | LinkedList1Node dest_list_node; |
||
93 | // output chain |
||
94 | int have_io; |
||
95 | PacketPassFairQueueFlow qflow; |
||
96 | PacketProtoFlow oflow; |
||
97 | BufferWriter *input; |
||
98 | int packet_len; |
||
99 | uint8_t *packet; |
||
100 | // reset timer |
||
101 | BTimer reset_timer; |
||
102 | // opposite flow |
||
103 | struct peer_flow *opposite; |
||
104 | // pair data |
||
105 | struct peer_know *know; |
||
106 | int accepted; |
||
107 | int resetting; |
||
108 | }; |
||
109 | |||
110 | struct peer_know { |
||
111 | struct client_data *from; |
||
112 | struct client_data *to; |
||
113 | int relay_server; |
||
114 | int relay_client; |
||
115 | LinkedList1Node from_node; |
||
116 | LinkedList1Node to_node; |
||
117 | BPending inform_job; |
||
118 | BPending uninform_job; |
||
119 | }; |
||
120 | |||
121 | struct client_data { |
||
122 | // socket |
||
123 | BConnection con; |
||
124 | BAddr addr; |
||
125 | |||
126 | // SSL connection, if using SSL |
||
127 | PRFileDesc bottom_prfd; |
||
128 | PRFileDesc *ssl_prfd; |
||
129 | BSSLConnection sslcon; |
||
130 | |||
131 | // initialization state |
||
132 | int initstatus; |
||
133 | |||
134 | // client data if using SSL |
||
135 | uint8_t cert[SCID_NEWCLIENT_MAX_CERT_LEN]; |
||
136 | int cert_len; |
||
137 | uint8_t cert_old[SCID_NEWCLIENT_MAX_CERT_LEN]; |
||
138 | int cert_old_len; |
||
139 | char *common_name; |
||
140 | |||
141 | // client version |
||
142 | int version; |
||
143 | |||
144 | // no data timer |
||
145 | BTimer disconnect_timer; |
||
146 | |||
147 | // client ID |
||
148 | peerid_t id; |
||
149 | |||
150 | // node in clients linked list |
||
151 | LinkedList1Node list_node; |
||
152 | // node in clients tree (by ID) |
||
153 | BAVLNode tree_node; |
||
154 | |||
155 | // knowledge lists |
||
156 | LinkedList1 know_out_list; |
||
157 | LinkedList1 know_in_list; |
||
158 | |||
159 | // flows from us |
||
160 | LinkedList1 peer_out_flows_list; |
||
161 | BAVL peer_out_flows_tree; |
||
162 | |||
163 | // whether it's being removed |
||
164 | int dying; |
||
165 | BPending dying_job; |
||
166 | |||
167 | // input |
||
168 | PacketProtoDecoder input_decoder; |
||
169 | PacketPassInterface input_interface; |
||
170 | |||
171 | // output common |
||
172 | PacketStreamSender output_sender; |
||
173 | PacketPassPriorityQueue output_priorityqueue; |
||
174 | |||
175 | // output control flow |
||
176 | PacketPassPriorityQueueFlow output_control_qflow; |
||
177 | PacketProtoFlow output_control_oflow; |
||
178 | BufferWriter *output_control_input; |
||
179 | int output_control_packet_len; |
||
180 | uint8_t *output_control_packet; |
||
181 | |||
182 | // output peers flow |
||
183 | PacketPassPriorityQueueFlow output_peers_qflow; |
||
184 | PacketPassFairQueue output_peers_fairqueue; |
||
185 | LinkedList1 output_peers_flows; |
||
186 | }; |