ReactOS  0.4.13-dev-79-gcd489d8
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 
35 static 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
44 struct CritSect : public CRITICAL_SECTION
45 {
46  CritSect()
47  {
49  }
50 
51  ~CritSect()
52  {
54  }
55 };
56 
57 static 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');
68  putchar(mode);
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 
90 static SOCKET s_srv_socket = (SOCKET)-1;
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 
156 protected:
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 
234 int 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
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 }
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
static int argc
Definition: ServiceArgs.c:12
#define CreateNamedPipe
Definition: winbase.h:3571
int main(int argc, char **argv)
Definition: pipetunnel.cpp:234
#define CloseHandle
Definition: compat.h:398
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:736
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 error(str)
Definition: mkdosfs.c:1605
#define htonl(x)
Definition: module.h:212
CRITICAL_SECTION CritSect
#define MAKEWORD(a, b)
Definition: typedefs.h:247
INT WSAAPI bind(IN SOCKET s, IN CONST struct sockaddr *name, IN INT namelen)
Definition: socklife.c:36
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
#define EINTR
Definition: acclib.h:80
static SOCKET s_srv_socket
Definition: pipetunnel.cpp:90
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
#define INADDR_ANY
Definition: StrAddr.c:4
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
GLuint buffer
Definition: glext.h:5915
int errno
void WINAPI EnterCriticalSection(LPCRITICAL_SECTION)
#define argv
Definition: mplay32.c:18
static DWORD WINAPI WriterThreadRoutine(LPVOID param)
Definition: pipetunnel.cpp:160
#define sprintf(buf, format,...)
Definition: sprintf.c:55
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
#define closesocket
Definition: main.c:39
HANDLE WINAPI GetCurrentThread(VOID)
Definition: proc.c:1178
_CRTIMP void __cdecl perror(_In_opt_z_ const char *_ErrMsg)
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
int putchar(int c)
Definition: crtsupp.c:12
#define HIGH_PRIORITY_CLASS
Definition: winbase.h:183
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:112
INT WINAPI WSAStartup(IN WORD wVersionRequested, OUT LPWSADATA lpWSAData)
Definition: startup.c:113
VOID WINAPI InitializeCriticalSection(OUT LPCRITICAL_SECTION lpCriticalSection)
Definition: synch.c:697
smooth NULL
Definition: ftsmooth.c:416
#define ERROR_PIPE_LISTENING
Definition: winerror.h:353
#define PF_INET
Definition: winsock.h:373
BOOL WINAPI SetThreadPriority(IN HANDLE hThread, IN int nPriority)
Definition: thread.c:662
r l[0]
Definition: byte_order.h:167
void WINAPI DeleteCriticalSection(PCRITICAL_SECTION)
#define PIPE_ACCESS_DUPLEX
Definition: winbase.h:164
INT WSAAPI send(IN SOCKET s, IN CONST CHAR FAR *buf, IN INT len, IN INT flags)
Definition: send.c:23
HANDLE WINAPI GetCurrentProcess(VOID)
Definition: proc.c:1168
GLfloat param
Definition: glext.h:5796
#define MAX_PATH
Definition: compat.h:26
#define WINAPI
Definition: msvc.h:8
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 ret
SOCKET WSAAPI accept(IN SOCKET s, OUT LPSOCKADDR addr, OUT INT FAR *addrlen)
Definition: socklife.c:23
GLenum mode
Definition: glext.h:6217
int puts(const char *string)
Definition: crtsupp.c:23
struct in_addr sin_addr
Definition: winsock.h:512
WriterThread(SOCKET sock, HANDLE hPipe)
Definition: pipetunnel.cpp:140
Definition: services.c:325
HANDLE hThread
Definition: wizard.c:27
INT WSAAPI listen(IN SOCKET s, IN INT backlog)
Definition: sockctrl.c:123
Definition: tcpcore.h:1455
static void print_error(DWORD win32_error)
Definition: pipetunnel.cpp:35
FILE * stderr
#define AF_INET
Definition: tcpip.h:117
SOCKET open_tcp_connect()
Definition: pipetunnel.cpp:92
UINT_PTR SOCKET
Definition: winsock.h:47
void WINAPI LeaveCriticalSection(LPCRITICAL_SECTION)
#define SOCK_STREAM
Definition: tcpip.h:118
GLfloat GLfloat p
Definition: glext.h:8902
LONG read_pipe(HANDLE hPipe, SOCKET sock)
Definition: pipetunnel.cpp:202
#define FILE_FLAG_OVERLAPPED
Definition: disk.h:46
#define htons(x)
Definition: module.h:213
#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
static TfClientId tid
INT WSAAPI recv(IN SOCKET s, OUT CHAR FAR *buf, IN INT len, IN INT flags)
Definition: recv.c:23
short sin_family
Definition: winsock.h:510
_CRTIMP int __cdecl read(_In_ int _FileHandle, _Out_writes_bytes_(_MaxCharCount) void *_DstBuf, _In_ unsigned int _MaxCharCount)