-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathportforward.cpp
89 lines (72 loc) · 2.34 KB
/
portforward.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
// portforward.cpp : trivial port forwarding for windows
//
#include <winsock2.h>
#include <stdio.h>
DWORD WINAPI reader(LPVOID lpParameter)
{
SOCKET *socks = (SOCKET*)lpParameter;
char buf[65536];
int n;
while ((n = recv(socks[0], buf, sizeof(buf), 0)) > 0) {
send(socks[1], buf, n, 0);
}
closesocket(socks[0]);
closesocket(socks[1]);
return 0;
}
DWORD WINAPI writer(LPVOID lpParameter)
{
SOCKET *socks = (SOCKET*)lpParameter;
char buf[65536];
int n;
while ((n = recv(socks[1], buf, sizeof(buf), 0)) > 0) {
send(socks[0], buf, n, 0);
}
closesocket(socks[0]);
closesocket(socks[1]);
return 0;
}
int main(int argc, char* argv[])
{
if (argc < 4) {
printf("usage: portforward [port to listen on] [ip of host to connect to] [port to connect to].\n");
return 1;
}
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD( 2, 2 );
WSAStartup( wVersionRequested, &wsaData );
SOCKET s = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in sin;
sin.sin_addr.S_un.S_addr = INADDR_ANY;
sin.sin_family = AF_INET;
sin.sin_port = htons(atoi(argv[1]));
if (bind(s, (sockaddr*)&sin, sizeof(sin)) != 0) {
printf("cannot bind to port %i\n", atoi(argv[1]));
return 1;
}
listen(s, 5);
int ss = sizeof(sin);
SOCKET n;
while ((n = accept(s, (sockaddr*)&sin, &ss)) != -1) {
printf("received connection from %i.%i.%i.%i\n", sin.sin_addr.S_un.S_un_b.s_b1, sin.sin_addr.S_un.S_un_b.s_b2, sin.sin_addr.S_un.S_un_b.s_b3, sin.sin_addr.S_un.S_un_b.s_b4);
SOCKET d = socket(AF_INET, SOCK_STREAM, 0);
sin.sin_family = AF_INET;
sin.sin_addr.S_un.S_addr = inet_addr(argv[2]);
sin.sin_port = htons(atoi(argv[3]));
if (connect(d, (sockaddr*)&sin, sizeof(sin)) != 0) {
printf("received a connection but can't connect to %s:%i\n", argv[2], atoi(argv[3]));
closesocket(n);
} else {
printf("connection to %s:%i established\n", argv[2], atoi(argv[3]));
SOCKET *socks = new SOCKET[2];
socks[0] = n;
socks[1] = d;
DWORD id;
CreateThread(NULL, 0, reader, socks, 0, &id);
CreateThread(NULL, 0, writer, socks, 0, &id);
}
}
closesocket(s);
return 0;
}