ReactOS  0.4.13-dev-73-gcfe54aa
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
296  closesocket(sock);
297  sock = (SOCKET)-1;
298 
299  // close named pipe
300  CloseHandle(hPipe);
301  hPipe = INVALID_HANDLE_VALUE;
302 
303 
304  if (error == ERROR_BROKEN_PIPE)
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 CreateNamedPipe
Definition: winbase.h:3571
#define CloseHandle
Definition: compat.h:398
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:736
#define error(str)
Definition: mkdosfs.c:1605
#define MAKEWORD(a, b)
Definition: typedefs.h:247
BOOL WINAPI SetPriorityClass(IN HANDLE hProcess, IN DWORD dwPriorityClass)
Definition: proc.c:1722
#define PIPE_WAIT
Definition: winbase.h:171
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
#define argv
Definition: mplay32.c:18
#define sprintf(buf, format,...)
Definition: sprintf.c:55
#define closesocket
Definition: main.c:39
HANDLE WINAPI GetCurrentThread(VOID)
Definition: proc.c:1178
long LONG
Definition: pedump.c:60
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
#define THREAD_PRIORITY_HIGHEST
Definition: winbase.h:274
#define PIPE_TYPE_BYTE
Definition: winbase.h:167
#define HIGH_PRIORITY_CLASS
Definition: winbase.h:183
INT WINAPI WSAStartup(IN WORD wVersionRequested, OUT LPWSADATA lpWSAData)
Definition: startup.c:113
smooth NULL
Definition: ftsmooth.c:416
#define ERROR_PIPE_LISTENING
Definition: winerror.h:353
BOOL WINAPI SetThreadPriority(IN HANDLE hThread, IN int nPriority)
Definition: thread.c:662
#define PIPE_ACCESS_DUPLEX
Definition: winbase.h:164
HANDLE WINAPI GetCurrentProcess(VOID)
Definition: proc.c:1168
#define MAX_PATH
Definition: compat.h:26
unsigned long DWORD
Definition: ntddk_ex.h:95
#define ERROR_BROKEN_PIPE
Definition: winerror.h:183
#define FILE_FLAG_FIRST_PIPE_INSTANCE
Definition: pipetunnel.cpp:31
int puts(const char *string)
Definition: crtsupp.c:23
Definition: services.c:325
Definition: tcpcore.h:1455
static void print_error(DWORD win32_error)
Definition: pipetunnel.cpp:35
FILE * stderr
SOCKET open_tcp_connect()
Definition: pipetunnel.cpp:92
UINT_PTR SOCKET
Definition: winsock.h:47
LONG read_pipe(HANDLE hPipe, SOCKET sock)
Definition: pipetunnel.cpp:202
#define FILE_FLAG_OVERLAPPED
Definition: disk.h:46
BOOL WINAPI ReadFile(IN HANDLE hFile, IN LPVOID lpBuffer, IN DWORD nNumberOfBytesToRead, OUT LPDWORD lpNumberOfBytesRead OPTIONAL, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:123
_CRTIMP int __cdecl read(_In_ int _FileHandle, _Out_writes_bytes_(_MaxCharCount) void *_DstBuf, _In_ unsigned int _MaxCharCount)

◆ 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 }
static SOCKET sock
Definition: syslog.c:45
SOCKET WSAAPI socket(IN INT af, IN INT type, IN INT protocol)
Definition: socklife.c:143
u_short sin_port
Definition: winsock.h:511
#define htonl(x)
Definition: module.h:212
INT WSAAPI bind(IN SOCKET s, IN CONST struct sockaddr *name, IN INT namelen)
Definition: socklife.c:36
#define EINTR
Definition: acclib.h:80
static SOCKET s_srv_socket
Definition: pipetunnel.cpp:90
#define INADDR_ANY
Definition: StrAddr.c:4
int errno
_CRTIMP void __cdecl perror(_In_opt_z_ const char *_ErrMsg)
#define PF_INET
Definition: winsock.h:373
SOCKET WSAAPI accept(IN SOCKET s, OUT LPSOCKADDR addr, OUT INT FAR *addrlen)
Definition: socklife.c:23
struct in_addr sin_addr
Definition: winsock.h:512
INT WSAAPI listen(IN SOCKET s, IN INT backlog)
Definition: sockctrl.c:123
Definition: tcpcore.h:1455
#define AF_INET
Definition: tcpip.h:117
UINT_PTR SOCKET
Definition: winsock.h:47
#define SOCK_STREAM
Definition: tcpip.h:118
#define htons(x)
Definition: module.h:213
short sin_family
Definition: winsock.h:510

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 }
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
FILE * stderr

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 }
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:736
#define error(str)
Definition: mkdosfs.c:1605
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
GLuint buffer
Definition: glext.h:5915
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
_CRTIMP void __cdecl perror(_In_opt_z_ const char *_ErrMsg)
smooth NULL
Definition: ftsmooth.c:416
#define ERROR_PIPE_LISTENING
Definition: winerror.h:353
INT WSAAPI send(IN SOCKET s, IN CONST CHAR FAR *buf, IN INT len, IN INT flags)
Definition: send.c:23
unsigned long DWORD
Definition: ntddk_ex.h:95
Definition: tcpcore.h:1455
#define INFINITE
Definition: serial.h:102
BOOL WINAPI ReadFile(IN HANDLE hFile, IN LPVOID lpBuffer, IN DWORD nNumberOfBytesToRead, OUT LPDWORD lpNumberOfBytesRead OPTIONAL, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:123
_CRTIMP int __cdecl read(_In_ int _FileHandle, _Out_writes_bytes_(_MaxCharCount) void *_DstBuf, _In_ unsigned int _MaxCharCount)

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().