-
Notifications
You must be signed in to change notification settings - Fork 1
/
UDPThread.cpp
executable file
·218 lines (187 loc) · 6.73 KB
/
UDPThread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//
//---------------------------------------------------------------------------
#include "stdinc.h"
//---------------------------------------------------------------------------
#include "eventqueue.h"
#include "ServerManager.h"
#include "SettingManager.h"
#include "utility.h"
//---------------------------------------------------------------------------
#ifdef _WIN32
#pragma hdrstop
#endif
//---------------------------------------------------------------------------
#include "UDPThread.h"
//---------------------------------------------------------------------------
UDPThread * UDPThread::mPtrIPv4 = NULL;
UDPThread * UDPThread::mPtrIPv6 = NULL;
//---------------------------------------------------------------------------
UDPThread::UDPThread() {
#ifdef _WIN32
sock = INVALID_SOCKET;
#else
sock = -1;
#endif
rcvbuf[0] = '\0';
threadId = 0;
#ifdef _WIN32
threadHandle = INVALID_HANDLE_VALUE;
#endif
bTerminated = false;
}
bool UDPThread::Listen(const int &iAddressFamily) {
sock = socket(iAddressFamily, SOCK_DGRAM, IPPROTO_UDP);
#ifdef _WIN32
if(sock == INVALID_SOCKET) {
#else
if(sock == -1) {
#endif
AppendLog("[ERR] UDP Socket creation error.");
return false;
}
#ifndef _WIN32
int on = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
#endif
sockaddr_storage sas;
memset(&sas, 0, sizeof(sockaddr_storage));
socklen_t sas_len;
if(iAddressFamily == AF_INET6) {
((struct sockaddr_in6 *)&sas)->sin6_family = AF_INET6;
((struct sockaddr_in6 *)&sas)->sin6_port = htons((unsigned short)atoi(clsSettingManager::mPtr->sTexts[SETTXT_UDP_PORT]));
sas_len = sizeof(struct sockaddr_in6);
if(clsSettingManager::mPtr->bBools[SETBOOL_BIND_ONLY_SINGLE_IP] == true && clsServerManager::sHubIP6[0] != '\0') {
#ifdef _WIN32
win_inet_pton(clsServerManager::sHubIP6, &((struct sockaddr_in6 *)&sas)->sin6_addr);
#else
inet_pton(AF_INET6, clsServerManager::sHubIP6, &((struct sockaddr_in6 *)&sas)->sin6_addr);
#endif
} else {
((struct sockaddr_in6 *)&sas)->sin6_addr = in6addr_any;
if(iAddressFamily == AF_INET6 && clsServerManager::bIPv6DualStack == true) {
#ifdef _WIN32
DWORD dwIPv6 = 0;
setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&dwIPv6, sizeof(dwIPv6));
#else
int iIPv6 = 0;
setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &iIPv6, sizeof(iIPv6));
#endif
}
}
} else {
((struct sockaddr_in *)&sas)->sin_family = AF_INET;
((struct sockaddr_in *)&sas)->sin_port = htons((unsigned short)atoi(clsSettingManager::mPtr->sTexts[SETTXT_UDP_PORT]));
sas_len = sizeof(struct sockaddr_in);
if(clsSettingManager::mPtr->bBools[SETBOOL_BIND_ONLY_SINGLE_IP] == true && clsServerManager::sHubIP[0] != '\0') {
((struct sockaddr_in *)&sas)->sin_addr.s_addr = inet_addr(clsServerManager::sHubIP);
} else {
((struct sockaddr_in *)&sas)->sin_addr.s_addr = INADDR_ANY;
}
}
#ifdef _WIN32
if(bind(sock, (struct sockaddr *)&sas, sas_len) == SOCKET_ERROR) {
AppendLog("[ERR] UDP Socket bind error: "+string(WSAGetLastError()));
#else
if(bind(sock, (struct sockaddr *)&sas, sas_len) == -1) {
AppendLog("[ERR] UDP Socket bind error: "+string(ErrnoStr(errno))+" ("+string(errno)+")");
#endif
return false;
}
return true;
}
//---------------------------------------------------------------------------
UDPThread::~UDPThread() {
#ifdef _WIN32
if(sock != INVALID_SOCKET) {
closesocket(sock);
sock = INVALID_SOCKET;
}
if(threadHandle != INVALID_HANDLE_VALUE) {
CloseHandle(threadHandle);
#else
if(threadId != 0) {
Close();
WaitFor();
#endif
}
}
//---------------------------------------------------------------------------
#ifdef _WIN32
unsigned __stdcall ExecuteUDP(void* UDPThrd) {
#else
static void* ExecuteUDP(void* UDPThrd) {
#endif
((UDPThread *)UDPThrd)->Run();
return 0;
}
//---------------------------------------------------------------------------
void UDPThread::Resume() {
#ifdef _WIN32
threadHandle = (HANDLE)_beginthreadex(NULL, 0, ExecuteUDP, this, 0, &threadId);
if(threadHandle == 0) {
#else
int iRet = pthread_create(&threadId, NULL, ExecuteUDP, this);
if(iRet != 0) {
#endif
AppendDebugLog("%s - [ERR] Failed to create new UDPThread\n", 0);
}
}
//---------------------------------------------------------------------------
void UDPThread::Run() {
sockaddr_storage sas;
socklen_t sas_len = sizeof(sockaddr_storage);
while(bTerminated == false) {
int len = recvfrom(sock, rcvbuf, 4095, 0, (struct sockaddr *)&sas, &sas_len);
if(len < 5 || strncmp(rcvbuf, "$SR ", 4) != 0) {
continue;
}
rcvbuf[len] = '\0';
// added ip check, we don't want fake $SR causing kick of innocent user...
clsEventQueue::mPtr->AddThread(clsEventQueue::EVENT_UDP_SR, rcvbuf, &sas);
}
}
//---------------------------------------------------------------------------
void UDPThread::Close() {
bTerminated = true;
#ifdef _WIN32
closesocket(sock);
#else
shutdown(sock, SHUT_RDWR);
close(sock);
#endif
}
//---------------------------------------------------------------------------
void UDPThread::WaitFor() {
#ifdef _WIN32
WaitForSingleObject(threadHandle, INFINITE);
#else
if(threadId != 0) {
pthread_join(threadId, NULL);
threadId = 0;
}
#endif
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
UDPThread * UDPThread::Create(const int &iAddressFamily) {
UDPThread * pUDPThread = new UDPThread();
if(pUDPThread == NULL) {
AppendDebugLog("%s - [MEM] Cannot allocate pUDPThread in UDPThread::Create\n", 0);
return NULL;
}
if(pUDPThread->Listen(iAddressFamily) == true) {
pUDPThread->Resume();
return pUDPThread;
} else {
delete pUDPThread;
return NULL;
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void UDPThread::Destroy(UDPThread * pUDPThread) {
if(pUDPThread != NULL) {
pUDPThread->Close();
pUDPThread->WaitFor();
delete pUDPThread;
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------