forked from cgutman/udp2tcp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathu2t-common.cpp
244 lines (201 loc) · 5.87 KB
/
u2t-common.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <WinSock2.h>
#include <Ws2ipdef.h>
#define MAX_PACKET_SIZE 4096
#pragma comment(lib, "ws2_32.lib")
static void enableTcpNoDelay(SOCKET sock)
{
BOOL enable = TRUE;
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (const char*)&enable, sizeof(enable));
}
SOCKET createUdpSocket(int localPort)
{
SOCKET sock;
int error;
SOCKADDR_IN addr;
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock == INVALID_SOCKET) {
error = WSAGetLastError();
printf("socket() failed: %d\n", error);
return INVALID_SOCKET;
}
RtlZeroMemory(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(localPort);
if (bind(sock, (PSOCKADDR)&addr, sizeof(addr)) == SOCKET_ERROR) {
error = WSAGetLastError();
printf("bind() failed: %d\n", error);
closesocket(sock);
return INVALID_SOCKET;
}
return sock;
}
SOCKET createListeningTcpSocket(int localPort)
{
SOCKET sock;
int error;
SOCKADDR_IN addr;
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET) {
error = WSAGetLastError();
printf("socket() failed: %d\n", error);
return INVALID_SOCKET;
}
RtlZeroMemory(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(localPort);
if (bind(sock, (PSOCKADDR)&addr, sizeof(addr)) == SOCKET_ERROR) {
error = WSAGetLastError();
printf("bind() failed: %d\n", error);
closesocket(sock);
return INVALID_SOCKET;
}
if (listen(sock, 1) == SOCKET_ERROR) {
error = WSAGetLastError();
printf("listen() failed: %d\n", error);
closesocket(sock);
return INVALID_SOCKET;
}
return sock;
}
SOCKET createConnectedTcpSocket(char* remoteIp, int remotePort)
{
SOCKET sock;
int error;
SOCKADDR_IN addr;
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET) {
error = WSAGetLastError();
printf("socket() failed: %d\n", error);
return INVALID_SOCKET;
}
RtlZeroMemory(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(remotePort);
addr.sin_addr.S_un.S_addr = inet_addr(remoteIp);
if (connect(sock, (PSOCKADDR)&addr, sizeof(addr)) == SOCKET_ERROR) {
error = WSAGetLastError();
printf("connect() failed: %d\n", error);
closesocket(sock);
return INVALID_SOCKET;
}
enableTcpNoDelay(sock);
return sock;
}
SOCKET createConnectedUdpSocket(int localPort, char* remoteIp, int remotePort)
{
SOCKET sock;
SOCKADDR_IN addr;
int error;
sock = createUdpSocket(localPort);
if (sock == INVALID_SOCKET) {
return INVALID_SOCKET;
}
RtlZeroMemory(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(remotePort);
addr.sin_addr.S_un.S_addr = inet_addr(remoteIp);
if (connect(sock, (PSOCKADDR)&addr, sizeof(addr)) == SOCKET_ERROR) {
error = WSAGetLastError();
printf("connect() failed: %d\n", error);
closesocket(sock);
return INVALID_SOCKET;
}
return sock;
}
bool readTcpXmitUdpPacket(SOCKET tcpSock, SOCKET udpSock)
{
unsigned short packetLen;
int error;
char buffer[MAX_PACKET_SIZE];
// Read the packet length header
error = recv(tcpSock, (char*)&packetLen, sizeof(packetLen), MSG_WAITALL);
if (error == SOCKET_ERROR) {
error = WSAGetLastError();
printf("recv() failed: %d\n", error);
return false;
}
else if (error == 0) {
printf("Remote server disconnected\n");
return false;
}
if (packetLen > sizeof(buffer)) {
printf("Packet too large to relay: %d\n", packetLen);
return false;
}
// Now the payload
error = recv(tcpSock, buffer, packetLen, MSG_WAITALL);
if (error == SOCKET_ERROR) {
error = WSAGetLastError();
printf("recv() failed: %d\n", error);
return false;
}
else if (error == 0) {
printf("Remote server disconnected\n");
return false;
}
// And transmit it
send(udpSock, buffer, packetLen, 0);
return true;
}
bool readUdpXmitTcpPacket(SOCKET tcpSock, SOCKET udpSock)
{
unsigned short packetLen;
int error;
char buffer[MAX_PACKET_SIZE+sizeof(packetLen)];
// Read the UDP packet
error = recv(udpSock, &buffer[sizeof(packetLen)], MAX_PACKET_SIZE, 0);
if (error == SOCKET_ERROR) {
error = WSAGetLastError();
printf("recv() failed: %d\n", error);
return false;
}
// Construct the packet header
packetLen = (unsigned short)error;
RtlCopyMemory(buffer, &packetLen, sizeof(packetLen));
// Send the data
error = send(tcpSock, buffer, sizeof(packetLen)+packetLen, 0);
if (error == SOCKET_ERROR) {
error = WSAGetLastError();
printf("send() failed: %d\n", error);
return false;
}
return true;
}
SOCKET acceptSocket(SOCKET tcpServerSock)
{
int error;
SOCKET tcpSock = accept(tcpServerSock, NULL, NULL);
if (tcpSock == INVALID_SOCKET) {
error = WSAGetLastError();
printf("accept() failed: %d\n", error);
return error;
}
enableTcpNoDelay(tcpSock);
return tcpSock;
}
SOCKET pollForRead(SOCKET tcpSock, SOCKET udpSock)
{
fd_set readfds;
int error;
FD_ZERO(&readfds);
FD_SET(tcpSock, &readfds);
FD_SET(udpSock, &readfds);
error = select(0, &readfds, NULL, NULL, NULL);
if (error == SOCKET_ERROR) {
error = WSAGetLastError();
printf("select() failed: %d\n", error);
return INVALID_SOCKET;
}
if (FD_ISSET(tcpSock, &readfds)) {
return tcpSock;
}
if (FD_ISSET(udpSock, &readfds)) {
return udpSock;
}
return INVALID_SOCKET;
}