ReactOS 0.4.16-dev-91-g764881a
pipetunnel.cpp File Reference
#include <windows.h>
#include <winsock.h>
#include <stdio.h>
#include <errno.h>
Include dependency graph for pipetunnel.cpp:

Go to the source code of this file.

Classes

struct  WriterThread
 

Macros

#define WIN32_LEAN_AND_MEAN
 
#define FILE_FLAG_FIRST_PIPE_INSTANCE   0x00080000
 

Functions

static void print_error (DWORD win32_error)
 
SOCKET open_tcp_connect ()
 
LONG read_pipe (HANDLE hPipe, SOCKET sock)
 
int main (int argc, char **argv)
 

Variables

static SOCKET s_srv_socket = (SOCKET)-1
 

Macro Definition Documentation

◆ FILE_FLAG_FIRST_PIPE_INSTANCE

#define FILE_FLAG_FIRST_PIPE_INSTANCE   0x00080000

Definition at line 31 of file pipetunnel.cpp.

◆ WIN32_LEAN_AND_MEAN

#define WIN32_LEAN_AND_MEAN

Definition at line 16 of file pipetunnel.cpp.

Function Documentation

◆ main()

int main ( int argc  ,
char **  argv 
)

Definition at line 234 of file pipetunnel.cpp.

235{
236 char path[MAX_PATH];
237 const char* pipe_name;
238
239 if (argc > 1)
240 pipe_name = *++argv;
241 else
242 pipe_name = "com_2";
243
244 sprintf(path, "\\\\.\\pipe\\%s", pipe_name);
245
246
247 // initialize winsock
248 WSADATA wsa_data;
249
250 if (WSAStartup(MAKEWORD(2,2), &wsa_data)) {
251 fprintf(stderr, "WSAStartup() failed\n");
252 return 0;
253 }
254
255
256 // increment priority to be faster than the cpu eating VMWare process
259
260
262
263 for(;;) {
264 DWORD read;
265
266 if (hPipe == INVALID_HANDLE_VALUE) {
268
269 if (hPipe == INVALID_HANDLE_VALUE) {
271 return 1;
272 }
273 }
274
275 // wait for the client side of the pipe
276 while(!ReadFile(hPipe, NULL, 0, &read, NULL) &&
278 Sleep(1000);
279
280 puts("\nnamed pipe connected, now waiting for TCP connection...");
281
283 if (sock == (SOCKET)-1)
284 break;
285
286 puts("TCP connection established.");
287
288 // launch writer thread
289 new WriterThread(sock, hPipe);
290
291 // launch reader loop
292 LONG error = read_pipe(hPipe, sock);
293
294
295 // close TCP connectiom
297 sock = (SOCKET)-1;
298
299 // close named pipe
300 CloseHandle(hPipe);
301 hPipe = INVALID_HANDLE_VALUE;
302
303
305 puts("\nconnection closed."); // normal connection termination
306 else {
308 break;
309 }
310 }
311
312 if (hPipe != INVALID_HANDLE_VALUE)
313 if (!CloseHandle(hPipe))
315
316 return 0;
317}
static int argc
Definition: ServiceArgs.c:12
#define read
Definition: acwin.h:96
int puts(const char *string)
Definition: crtsupp.c:23
#define NULL
Definition: types.h:112
#define CloseHandle
Definition: compat.h:739
#define ReadFile(a, b, c, d, e)
Definition: compat.h:742
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define GetCurrentProcess()
Definition: compat.h:759
#define MAX_PATH
Definition: compat.h:34
BOOL WINAPI SetPriorityClass(IN HANDLE hProcess, IN DWORD dwPriorityClass)
Definition: proc.c:1698
BOOL WINAPI SetThreadPriority(IN HANDLE hThread, IN int nPriority)
Definition: thread.c:700
INT WINAPI WSAStartup(IN WORD wVersionRequested, OUT LPWSADATA lpWSAData)
Definition: startup.c:113
unsigned long DWORD
Definition: ntddk_ex.h:95
#define stderr
Definition: stdio.h:100
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
#define error(str)
Definition: mkdosfs.c:1605
#define FILE_FLAG_OVERLAPPED
Definition: disk.h:46
#define sprintf(buf, format,...)
Definition: sprintf.c:55
#define argv
Definition: mplay32.c:18
#define closesocket
Definition: ncftp.h:477
long LONG
Definition: pedump.c:60
#define FILE_FLAG_FIRST_PIPE_INSTANCE
Definition: pipetunnel.cpp:31
static void print_error(DWORD win32_error)
Definition: pipetunnel.cpp:35
SOCKET open_tcp_connect()
Definition: pipetunnel.cpp:92
LONG read_pipe(HANDLE hPipe, SOCKET sock)
Definition: pipetunnel.cpp:202
Definition: tcpcore.h:1455
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:790
#define MAKEWORD(a, b)
Definition: typedefs.h:248
#define CreateNamedPipe
Definition: winbase.h:3757
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
HANDLE WINAPI GetCurrentThread(void)
Definition: proc.c:1148
#define PIPE_ACCESS_DUPLEX
Definition: winbase.h:164
#define HIGH_PRIORITY_CLASS
Definition: winbase.h:183
#define PIPE_WAIT
Definition: winbase.h:171
#define THREAD_PRIORITY_HIGHEST
Definition: winbase.h:277
#define PIPE_TYPE_BYTE
Definition: winbase.h:167
#define ERROR_BROKEN_PIPE
Definition: winerror.h:183
#define ERROR_PIPE_LISTENING
Definition: winerror.h:353
UINT_PTR SOCKET
Definition: winsock.h:47

◆ open_tcp_connect()

SOCKET open_tcp_connect ( )

Definition at line 92 of file pipetunnel.cpp.

93{
94 if (s_srv_socket == (SOCKET)-1) {
95 SOCKADDR_IN srv_addr = {0};
96
97 srv_addr.sin_family = AF_INET;
98 srv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
99 srv_addr.sin_port = htons(9999);
100
102 if (s_srv_socket == (SOCKET)-1) {
103 perror("socket()");
104 return 0;
105 }
106
107 if (bind(s_srv_socket, (struct sockaddr*) &srv_addr, sizeof(srv_addr)) == -1) {
108 perror("bind()");
109 return 0;
110 }
111
112 if (listen(s_srv_socket, 4) == -1) {
113 perror("listen()");
114 return 0;
115 }
116 }
117
118 SOCKADDR_IN rem_addr;
119 int rem_len = sizeof(rem_addr);
120
121 for(;;) {
122 SOCKET sock = accept(s_srv_socket, (struct sockaddr*)&rem_addr, &rem_len);
123
124 if (sock < 0) {
125 if (errno == EINTR)
126 continue;
127
128 perror("accept()");
129 return 0;
130 }
131
132 return sock;
133 }
134}
#define EINTR
Definition: acclib.h:80
#define SOCK_STREAM
Definition: tcpip.h:118
#define AF_INET
Definition: tcpip.h:117
_CRTIMP void __cdecl perror(_In_opt_z_ const char *_ErrMsg)
#define INADDR_ANY
Definition: inet.h:53
#define htons(x)
Definition: module.h:215
#define htonl(x)
Definition: module.h:214
static SOCKET s_srv_socket
Definition: pipetunnel.cpp:90
#define errno
Definition: errno.h:18
INT WSAAPI listen(IN SOCKET s, IN INT backlog)
Definition: sockctrl.c:123
INT WSAAPI bind(IN SOCKET s, IN CONST struct sockaddr *name, IN INT namelen)
Definition: socklife.c:36
SOCKET WSAAPI accept(IN SOCKET s, OUT LPSOCKADDR addr, OUT INT FAR *addrlen)
Definition: socklife.c:23
SOCKET WSAAPI socket(IN INT af, IN INT type, IN INT protocol)
Definition: socklife.c:143
struct in_addr sin_addr
Definition: winsock.h:512
short sin_family
Definition: winsock.h:510
u_short sin_port
Definition: winsock.h:511
#define PF_INET
Definition: winsock.h:373

Referenced by main().

◆ print_error()

static void print_error ( DWORD  win32_error)
static

Definition at line 35 of file pipetunnel.cpp.

36{
37 fprintf(stderr, "WIN32 error %lu\n", win32_error);
38}

Referenced by main().

◆ read_pipe()

LONG read_pipe ( HANDLE  hPipe,
SOCKET  sock 
)

Definition at line 202 of file pipetunnel.cpp.

203{
204 for(;;) {
205 DWORD read;
206 char buffer[1024];
207
208 // wait for input data
210
211 if (!ReadFile(hPipe, buffer, sizeof(buffer), &read, NULL)) {
213
215 Sleep(1000);
216 else
217 return error;
218 }
219
220 if (read) {
221#ifdef _DEBUG
222 dbg_trace('>', buffer, read);
223#endif
224
225 if (!send(sock, buffer, read, 0)) {
226 perror("send()");
227 return GetLastError();
228 }
229 }
230 }
231}
INT WSAAPI send(IN SOCKET s, IN CONST CHAR FAR *buf, IN INT len, IN INT flags)
Definition: send.c:23
#define INFINITE
Definition: serial.h:102
GLuint buffer
Definition: glext.h:5915
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82

Referenced by main().

Variable Documentation

◆ s_srv_socket

SOCKET s_srv_socket = (SOCKET)-1
static

Definition at line 90 of file pipetunnel.cpp.

Referenced by open_tcp_connect().