1 // SoftEther VPN Source Code - Stable Edition Repository
2 // Cedar Communication Module
3 //
4 // SoftEther VPN Server, Client and Bridge are free software under the Apache License, Version 2.0.
5 //
6 // Copyright (c) Daiyuu Nobori.
7 // Copyright (c) SoftEther VPN Project, University of Tsukuba, Japan.
8 // Copyright (c) SoftEther Corporation.
9 // Copyright (c) all contributors on SoftEther VPN project in GitHub.
10 //
11 // All Rights Reserved.
12 //
13 // http://www.softether.org/
14 //
15 // This stable branch is officially managed by Daiyuu Nobori, the owner of SoftEther VPN Project.
16 // Pull requests should be sent to the Developer Edition Master Repository on https://github.com/SoftEtherVPN/SoftEtherVPN
17 //
18 // License: The Apache License, Version 2.0
19 // https://www.apache.org/licenses/LICENSE-2.0
20 //
21 // DISCLAIMER
22 // ==========
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 // SOFTWARE.
31 //
32 // THIS SOFTWARE IS DEVELOPED IN JAPAN, AND DISTRIBUTED FROM JAPAN, UNDER
33 // JAPANESE LAWS. YOU MUST AGREE IN ADVANCE TO USE, COPY, MODIFY, MERGE, PUBLISH,
34 // DISTRIBUTE, SUBLICENSE, AND/OR SELL COPIES OF THIS SOFTWARE, THAT ANY
35 // JURIDICAL DISPUTES WHICH ARE CONCERNED TO THIS SOFTWARE OR ITS CONTENTS,
36 // AGAINST US (SOFTETHER PROJECT, SOFTETHER CORPORATION, DAIYUU NOBORI OR OTHER
37 // SUPPLIERS), OR ANY JURIDICAL DISPUTES AGAINST US WHICH ARE CAUSED BY ANY KIND
38 // OF USING, COPYING, MODIFYING, MERGING, PUBLISHING, DISTRIBUTING, SUBLICENSING,
39 // AND/OR SELLING COPIES OF THIS SOFTWARE SHALL BE REGARDED AS BE CONSTRUED AND
40 // CONTROLLED BY JAPANESE LAWS, AND YOU MUST FURTHER CONSENT TO EXCLUSIVE
41 // JURISDICTION AND VENUE IN THE COURTS SITTING IN TOKYO, JAPAN. YOU MUST WAIVE
42 // ALL DEFENSES OF LACK OF PERSONAL JURISDICTION AND FORUM NON CONVENIENS.
43 // PROCESS MAY BE SERVED ON EITHER PARTY IN THE MANNER AUTHORIZED BY APPLICABLE
44 // LAW OR COURT RULE.
45 //
46 // USE ONLY IN JAPAN. DO NOT USE THIS SOFTWARE IN ANOTHER COUNTRY UNLESS YOU HAVE
47 // A CONFIRMATION THAT THIS SOFTWARE DOES NOT VIOLATE ANY CRIMINAL LAWS OR CIVIL
48 // RIGHTS IN THAT PARTICULAR COUNTRY. USING THIS SOFTWARE IN OTHER COUNTRIES IS
49 // COMPLETELY AT YOUR OWN RISK. THE SOFTETHER VPN PROJECT HAS DEVELOPED AND
50 // DISTRIBUTED THIS SOFTWARE TO COMPLY ONLY WITH THE JAPANESE LAWS AND EXISTING
51 // CIVIL RIGHTS INCLUDING PATENTS WHICH ARE SUBJECTS APPLY IN JAPAN. OTHER
52 // COUNTRIES' LAWS OR CIVIL RIGHTS ARE NONE OF OUR CONCERNS NOR RESPONSIBILITIES.
53 // WE HAVE NEVER INVESTIGATED ANY CRIMINAL REGULATIONS, CIVIL LAWS OR
54 // INTELLECTUAL PROPERTY RIGHTS INCLUDING PATENTS IN ANY OF OTHER 200+ COUNTRIES
55 // AND TERRITORIES. BY NATURE, THERE ARE 200+ REGIONS IN THE WORLD, WITH
56 // DIFFERENT LAWS. IT IS IMPOSSIBLE TO VERIFY EVERY COUNTRIES' LAWS, REGULATIONS
57 // AND CIVIL RIGHTS TO MAKE THE SOFTWARE COMPLY WITH ALL COUNTRIES' LAWS BY THE
58 // PROJECT. EVEN IF YOU WILL BE SUED BY A PRIVATE ENTITY OR BE DAMAGED BY A
59 // PUBLIC SERVANT IN YOUR COUNTRY, THE DEVELOPERS OF THIS SOFTWARE WILL NEVER BE
60 // LIABLE TO RECOVER OR COMPENSATE SUCH DAMAGES, CRIMINAL OR CIVIL
61 // RESPONSIBILITIES. NOTE THAT THIS LINE IS NOT LICENSE RESTRICTION BUT JUST A
62 // STATEMENT FOR WARNING AND DISCLAIMER.
63 //
64 // READ AND UNDERSTAND THE 'WARNING.TXT' FILE BEFORE USING THIS SOFTWARE.
65 // SOME SOFTWARE PROGRAMS FROM THIRD PARTIES ARE INCLUDED ON THIS SOFTWARE WITH
66 // LICENSE CONDITIONS WHICH ARE DESCRIBED ON THE 'THIRD_PARTY.TXT' FILE.
67 //
68 //
69 // SOURCE CODE CONTRIBUTION
70 // ------------------------
71 //
72 // Your contribution to SoftEther VPN Project is much appreciated.
73 // Please send patches to us through GitHub.
74 // Read the SoftEther VPN Patch Acceptance Policy in advance:
75 // http://www.softether.org/5-download/src/9.patch
76 //
77 //
78 // DEAR SECURITY EXPERTS
79 // ---------------------
80 //
81 // If you find a bug or a security vulnerability please kindly inform us
82 // about the problem immediately so that we can fix the security problem
83 // to protect a lot of users around the world as soon as possible.
84 //
85 // Our e-mail address for security reports is:
86 // softether-vpn-security [at] softether.org
87 //
88 // Please note that the above e-mail address is not a technical support
89 // inquiry address. If you need technical assistance, please visit
90 // http://www.softether.org/ and ask your question on the users forum.
91 //
92 // Thank you for your cooperation.
93 //
94 //
95 // NO MEMORY OR RESOURCE LEAKS
96 // ---------------------------
97 //
98 // The memory-leaks and resource-leaks verification under the stress
99 // test has been passed before release this source code.
100 
101 
102 // Listener.h
103 // Header of Listener.c
104 
105 #ifndef	LISTENER_H
106 #define	LISTENER_H
107 
108 
109 // Function to call when receiving a new connection
110 typedef void (NEW_CONNECTION_PROC)(CONNECTION *c);
111 
112 
113 
114 // Listener structure
115 struct LISTENER
116 {
117 	LOCK *lock;						// Lock
118 	REF *ref;						// Reference counter
119 	CEDAR *Cedar;					// Cedar
120 	UINT Protocol;					// Protocol
121 	UINT Port;						// Port number
122 	THREAD *Thread;					// Operating thread
123 	SOCK *Sock;						// Socket
124 	EVENT *Event;					// Event
125 	volatile bool Halt;				// Halting flag
126 	UINT Status;					// State
127 
128 
129 	THREAD_PROC *ThreadProc;		// Thread procedure
130 	void *ThreadParam;				// Thread parameters
131 	bool LocalOnly;					// Can be connected only from localhost
132 	bool ShadowIPv6;				// Flag indicating that the shadow IPv6 listener
133 	LISTENER *ShadowListener;		// Reference to managing shadow IPv6 listener
134 	bool DisableDos;				// Disable the DoS attack detection
135 	volatile UINT *NatTGlobalUdpPort;	// NAT-T global UDP port number
136 	UCHAR RandPortId;				// NAT-T UDP random port ID
137 	bool EnableConditionalAccept;	// The flag of whether to enable the Conditional Accept
138 };
139 
140 // Parameters of TCPAcceptedThread
141 struct TCP_ACCEPTED_PARAM
142 {
143 	LISTENER *r;
144 	SOCK *s;
145 };
146 
147 // UDP entry
148 struct UDP_ENTRY
149 {
150 	UINT SessionKey32;				// 32bit session key
151 	SESSION *Session;				// Reference to the session
152 };
153 
154 // Dynamic listener
155 struct DYNAMIC_LISTENER
156 {
157 	UINT Protocol;					// Protocol
158 	UINT Port;						// Port
159 	LOCK *Lock;						// Lock
160 	CEDAR *Cedar;					// Cedar
161 	bool *EnablePtr;				// A pointer to the flag of the valid / invalid state
162 	LISTENER *Listener;				// Listener
163 };
164 
165 
166 // Function prototype
167 LISTENER *NewListener(CEDAR *cedar, UINT proto, UINT port);
168 LISTENER *NewListenerEx(CEDAR *cedar, UINT proto, UINT port, THREAD_PROC *proc, void *thread_param);
169 LISTENER *NewListenerEx2(CEDAR *cedar, UINT proto, UINT port, THREAD_PROC *proc, void *thread_param, bool local_only);
170 LISTENER *NewListenerEx3(CEDAR *cedar, UINT proto, UINT port, THREAD_PROC *proc, void *thread_param, bool local_only, bool shadow_ipv6);
171 LISTENER *NewListenerEx4(CEDAR *cedar, UINT proto, UINT port, THREAD_PROC *proc, void *thread_param, bool local_only, bool shadow_ipv6,
172 						 volatile UINT *natt_global_udp_port, UCHAR rand_port_id);
173 LISTENER *NewListenerEx5(CEDAR *cedar, UINT proto, UINT port, THREAD_PROC *proc, void *thread_param, bool local_only, bool shadow_ipv6,
174 						 volatile UINT *natt_global_udp_port, UCHAR rand_port_id, bool enable_ca);
175 void ReleaseListener(LISTENER *r);
176 void CleanupListener(LISTENER *r);
177 void ListenerThread(THREAD *thread, void *param);
178 void ListenerTCPMainLoop(LISTENER *r);
179 void StopListener(LISTENER *r);
180 int CompareListener(void *p1, void *p2);
181 void TCPAccepted(LISTENER *r, SOCK *s);
182 void EnableDosProtect();
183 void DisableDosProtect();
184 void TCPAcceptedThread(THREAD *t, void *param);
185 void ListenerUDPMainLoop(LISTENER *r);
186 void UDPReceivedPacket(CEDAR *cedar, SOCK *s, IP *ip, UINT port, void *data, UINT size);
187 int CompareUDPEntry(void *p1, void *p2);
188 void CleanupUDPEntry(CEDAR *cedar);
189 void AddUDPEntry(CEDAR *cedar, SESSION *session);
190 void DelUDPEntry(CEDAR *cedar, SESSION *session);
191 SESSION *GetSessionFromUDPEntry(CEDAR *cedar, UINT key32);
192 UINT GetMaxConnectionsPerIp();
193 void SetMaxConnectionsPerIp(UINT num);
194 UINT GetMaxUnestablishedConnections();
195 void SetMaxUnestablishedConnections(UINT num);
196 DYNAMIC_LISTENER *NewDynamicListener(CEDAR *c, bool *enable_ptr, UINT protocol, UINT port);
197 void ApplyDynamicListener(DYNAMIC_LISTENER *d);
198 void FreeDynamicListener(DYNAMIC_LISTENER *d);
199 bool ListenerRUDPRpcRecvProc(RUDP_STACK *r, UDPPACKET *p);
200 void ListenerSetProcRecvRpcEnable(bool b);
201 
202 
203 #endif	// LISTENER_H
204 
205 
206