ReactOS 0.4.15-dev-7918-g2a2556c
pipetunnel.cpp
Go to the documentation of this file.
1//
2// pipetunnel.cpp
3//
4// Martin Fuchs, 30.11.2003
5//
6
7//
8// Invoke as: "pipetunnel [pipe_name]",
9// for example: "pipetunnel com_2"
10//
11// Then start up RectOS in VMWare, wait for the serial connect.
12// After that you can connect GDB using the command "target remote :9999".
13//
14
15
16#define WIN32_LEAN_AND_MEAN
17#include <windows.h>
18
19#include <winsock.h>
20
21#ifdef _MSC_VER
22#pragma comment(lib, "wsock32")
23#endif
24
25#include <stdio.h>
26#include <errno.h>
27
28
29 // This definition currently missing in MinGW.
30#ifndef FILE_FLAG_FIRST_PIPE_INSTANCE
31#define FILE_FLAG_FIRST_PIPE_INSTANCE 0x00080000
32#endif
33
34
35static void print_error(DWORD win32_error)
36{
37 fprintf(stderr, "WIN32 error %lu\n", win32_error);
38}
39
40
41#ifdef _DEBUG
42
43 // critical section wrapper
44struct CritSect : public CRITICAL_SECTION
45{
46 CritSect()
47 {
49 }
50
51 ~CritSect()
52 {
54 }
55};
56
57static void dbg_trace(char mode, const char* buffer, int l)
58{
59 static char s_mode = '\0';
60 static CritSect crit_sect;
61
62 EnterCriticalSection(&crit_sect);
63
64 if (l) {
65 for(const char*p=buffer; l--; ++p) {
66 if (mode != s_mode) {
67 putchar('\n');
69 putchar(' ');
70
71 s_mode = mode;
72 }
73
74 if (*p=='\n' || !*p /*|| *p=='#'*/) {
75 /*if (*p == '#')
76 putchar(*p);*/
77
78 s_mode = '\0';
79 } else
80 putchar(*p);
81 }
82 }
83
84 LeaveCriticalSection(&crit_sect);
85}
86
87#endif
88
89
91
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}
135
136
137
139{
141 : _sock(sock),
142 _hPipe(hPipe)
143 {
144 DWORD tid;
145
147
148 if (hThread) {
150
152 } else
153 delete this;
154 }
155
156protected:
159
161 {
162 WriterThread* pThis = (WriterThread*) param;
163
164 DWORD ret = pThis->Run();
165
166 delete pThis;
167
168 return ret;
169 }
170
172 {
173 char buffer[1024];
174
175 for(;;) {
176 int r = recv(_sock, buffer, sizeof(buffer), 0);
177
178 if (r == -1) {
179 perror("recv()");
180 fprintf(stderr, "debugger connection broken\n");
181 _sock = (SOCKET)-1;
182 return 1;
183 }
184
185 if (r) {
186 DWORD wrote;
187
188 if (!WriteFile(_hPipe, buffer, r, &wrote, NULL))
189 break;
190
191#ifdef _DEBUG
192 dbg_trace('<', buffer, r);
193#endif
194 }
195 }
196
197 return 0;
198 }
199};
200
201
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}
232
233
234int main(int argc, char** argv)
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}
RTL_CRITICAL_SECTION CritSect
static int argc
Definition: ServiceArgs.c:12
#define EINTR
Definition: acclib.h:80
#define read
Definition: acwin.h:96
r l[0]
Definition: byte_order.h:168
int puts(const char *string)
Definition: crtsupp.c:23
int putchar(int c)
Definition: crtsupp.c:12
#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 WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
BOOL WINAPI SetPriorityClass(IN HANDLE hProcess, IN DWORD dwPriorityClass)
Definition: proc.c:1692
BOOL WINAPI SetThreadPriority(IN HANDLE hThread, IN int nPriority)
Definition: thread.c:700
HANDLE WINAPI DECLSPEC_HOTPATCH CreateThread(IN LPSECURITY_ATTRIBUTES lpThreadAttributes, IN DWORD dwStackSize, IN LPTHREAD_START_ROUTINE lpStartAddress, IN LPVOID lpParameter, IN DWORD dwCreationFlags, OUT LPDWORD lpThreadId)
Definition: thread.c:137
INT WSAAPI recv(IN SOCKET s, OUT CHAR FAR *buf, IN INT len, IN INT flags)
Definition: recv.c:23
INT WSAAPI send(IN SOCKET s, IN CONST CHAR FAR *buf, IN INT len, IN INT flags)
Definition: send.c:23
INT WINAPI WSAStartup(IN WORD wVersionRequested, OUT LPWSADATA lpWSAData)
Definition: startup.c:113
int main()
Definition: test.c:6
#define SOCK_STREAM
Definition: tcpip.h:118
#define AF_INET
Definition: tcpip.h:117
#define INFINITE
Definition: serial.h:102
unsigned long DWORD
Definition: ntddk_ex.h:95
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
GLuint buffer
Definition: glext.h:5915
GLenum mode
Definition: glext.h:6217
GLfloat GLfloat p
Definition: glext.h:8902
GLfloat param
Definition: glext.h:5796
_CRTIMP void __cdecl perror(_In_opt_z_ const char *_ErrMsg)
#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 INADDR_ANY
Definition: inet.h:53
#define error(str)
Definition: mkdosfs.c:1605
#define htons(x)
Definition: module.h:215
#define htonl(x)
Definition: module.h:214
#define FILE_FLAG_OVERLAPPED
Definition: disk.h:46
#define sprintf(buf, format,...)
Definition: sprintf.c:55
static TfClientId tid
#define argv
Definition: mplay32.c:18
#define closesocket
Definition: ncftp.h:477
HANDLE hThread
Definition: wizard.c:28
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
static SOCKET s_srv_socket
Definition: pipetunnel.cpp:90
SOCKET open_tcp_connect()
Definition: pipetunnel.cpp:92
LONG read_pipe(HANDLE hPipe, SOCKET sock)
Definition: pipetunnel.cpp:202
#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
WriterThread(SOCKET sock, HANDLE hPipe)
Definition: pipetunnel.cpp:140
static DWORD WINAPI WriterThreadRoutine(LPVOID param)
Definition: pipetunnel.cpp:160
Definition: tcpcore.h:1455
struct in_addr sin_addr
Definition: winsock.h:512
short sin_family
Definition: winsock.h:510
u_short sin_port
Definition: winsock.h:511
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:790
#define MAKEWORD(a, b)
Definition: typedefs.h:248
int ret
#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
void WINAPI LeaveCriticalSection(LPCRITICAL_SECTION)
VOID WINAPI InitializeCriticalSection(LPCRITICAL_SECTION)
#define HIGH_PRIORITY_CLASS
Definition: winbase.h:183
#define PIPE_WAIT
Definition: winbase.h:171
void WINAPI EnterCriticalSection(LPCRITICAL_SECTION)
#define THREAD_PRIORITY_HIGHEST
Definition: winbase.h:277
void WINAPI DeleteCriticalSection(PCRITICAL_SECTION)
#define PIPE_TYPE_BYTE
Definition: winbase.h:167
#define WINAPI
Definition: msvc.h:6
#define ERROR_BROKEN_PIPE
Definition: winerror.h:183
#define ERROR_PIPE_LISTENING
Definition: winerror.h:353
#define PF_INET
Definition: winsock.h:373
UINT_PTR SOCKET
Definition: winsock.h:47