HyperDbg Debugger
Loading...
Searching...
No Matches
namedpipe.h File Reference

Named pipe communication headers. More...

Go to the source code of this file.

Functions

HANDLE NamedPipeServerCreatePipe (LPCSTR PipeName, UINT32 OutputBufferSize, UINT32 InputBufferSize)
 Create a named pipe server.
BOOLEAN NamedPipeServerWaitForClientConntection (HANDLE PipeHandle)
 wait for client connection
UINT32 NamedPipeServerReadClientMessage (HANDLE PipeHandle, CHAR *BufferToSave, INT MaximumReadBufferLength)
 read client message from the named pipe
BOOLEAN NamedPipeServerSendMessageToClient (HANDLE PipeHandle, CHAR *BufferToSend, INT BufferSize)
VOID NamedPipeServerCloseHandle (HANDLE PipeHandle)
 Close handle of server's named pipe.
HANDLE NamedPipeClientCreatePipe (LPCSTR PipeName)
 Create a client named pipe.
HANDLE NamedPipeClientCreatePipeOverlappedIo (LPCSTR PipeName)
 Create a client named pipe through overlapped I/O.
BOOLEAN NamedPipeClientSendMessage (HANDLE PipeHandle, CHAR *BufferToSend, INT BufferSize)
 send client message over named pipe
UINT32 NamedPipeClientReadMessage (HANDLE PipeHandle, CHAR *BufferToRead, INT MaximumSizeOfBuffer)
VOID NamedPipeClientClosePipe (HANDLE PipeHandle)
 close named pipe handle of client

Detailed Description

Named pipe communication headers.

Author
Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
Version
0.1
Date
2020-04-11

Function Documentation

◆ NamedPipeClientClosePipe()

VOID NamedPipeClientClosePipe ( HANDLE PipeHandle)

close named pipe handle of client

Parameters
PipeHandle
Returns
VOID
465{
466 CloseHandle(PipeHandle);
467}

◆ NamedPipeClientCreatePipe()

HANDLE NamedPipeClientCreatePipe ( LPCSTR PipeName)

Create a client named pipe.

Pipe name format - \servername\pipe\pipename This pipe is for server on the same computer, however, pipes can be used to connect to a remote server

Parameters
PipeName
Returns
HANDLE
337{
338 HANDLE hPipe;
339
340 //
341 // Connect to the server pipe using CreateFile()
342 //
343 hPipe = CreateFileA(PipeName, // pipe name
344 GENERIC_READ | // read and write access
345 GENERIC_WRITE,
346 0, // no sharing
347 NULL, // default security attributes
348 OPEN_EXISTING, // opens existing pipe
349 0, // default attributes
350 NULL); // no template file
351
352 if (INVALID_HANDLE_VALUE == hPipe)
353 {
354 printf("err, occurred while connecting to the server (%x)\n",
355 GetLastError());
356 //
357 // One might want to check whether the server pipe is busy
358 // This sample will error out if the server pipe is busy
359 // Read on ERROR_PIPE_BUSY and WaitNamedPipe() for that
360 //
361
362 //
363 // Error
364 //
365 return NULL;
366 }
367 else
368 {
369 return hPipe;
370 }
371}
printf("ho")
NULL()
Definition test-case-generator.py:530

◆ NamedPipeClientCreatePipeOverlappedIo()

HANDLE NamedPipeClientCreatePipeOverlappedIo ( LPCSTR PipeName)

Create a client named pipe through overlapped I/O.

Pipe name format - \servername\pipe\pipename This pipe is for server on the same computer, however, pipes can be used to connect to a remote server

Parameters
PipeName
Returns
HANDLE
235{
236 HANDLE hPipe;
237
238 //
239 // Connect to the server pipe using CreateFile()
240 //
241 hPipe = CreateFileA(PipeName, // pipe name
242 GENERIC_READ | // read and write access
243 GENERIC_WRITE,
244 0, // no sharing
245 NULL, // default security attributes
246 OPEN_EXISTING, // opens existing pipe
247 FILE_FLAG_OVERLAPPED, // Overlapped I/O
248 NULL); // no template file
249
250 if (INVALID_HANDLE_VALUE == hPipe)
251 {
252 ShowMessages("err, occurred while connecting to the server (%x)\n",
253 GetLastError());
254 //
255 // One might want to check whether the server pipe is busy
256 // This sample will error out if the server pipe is busy
257 // Read on ERROR_PIPE_BUSY and WaitNamedPipe() for that
258 //
259
260 //
261 // Error
262 //
263 return NULL;
264 }
265 else
266 {
267 //
268 // Create event for overlapped I/O (Read)
269 //
271 CreateEvent(NULL, TRUE, FALSE, NULL);
272
273 //
274 // Create event for overlapped I/O (Write)
275 //
277 CreateEvent(NULL, TRUE, FALSE, NULL);
278
279 return hPipe;
280 }
281}
#define TRUE
Definition BasicTypes.h:114
#define FALSE
Definition BasicTypes.h:113
OVERLAPPED g_OverlappedIoStructureForWriteDebugger
OVERLAPPED g_OverlappedIoStructureForReadDebugger

◆ NamedPipeClientReadMessage()

UINT32 NamedPipeClientReadMessage ( HANDLE PipeHandle,
CHAR * BufferToRead,
INT MaximumSizeOfBuffer )
332{
333 DWORD cbBytes;
334
335 //
336 // Read server response
337 //
338 BOOL bResult = ReadFile(PipeHandle, // handle to pipe
339 BufferToRead, // buffer to receive data
340 MaximumSizeOfBuffer, // size of buffer
341 &cbBytes, // number of bytes read
342 NULL); // not overlapped I/O
343
344 if ((!bResult) || (0 == cbBytes))
345 {
346 ShowMessages("err, occurred while reading from the server (%x)\n",
347 GetLastError());
348 CloseHandle(PipeHandle);
349 return NULL; // Error
350 }
351
352 //
353 // Success
354 //
355 return cbBytes;
356}
int BOOL
Definition BasicTypes.h:25
unsigned long DWORD
Definition BasicTypes.h:38

◆ NamedPipeClientSendMessage()

BOOLEAN NamedPipeClientSendMessage ( HANDLE PipeHandle,
CHAR * BufferToSend,
INT BufferSize )

send client message over named pipe

Parameters
PipeHandle
BufferToSend
BufferSize
Returns
BOOLEAN
293{
294 //
295 // We are done connecting to the server pipe,
296 // we can start communicating with
297 // the server using ReadFile()/WriteFile()
298 // on handle - hPipe
299 //
300
301 DWORD cbBytes;
302
303 //
304 // Send the message to server
305 //
306 BOOL bResult =
307 WriteFile(PipeHandle, // handle to pipe
308 BufferToSend, // buffer to write from
309 BufferSize, // number of bytes to write, include the NULL
310 &cbBytes, // number of bytes written
311 NULL); // not overlapped I/O
312
313 if ((!bResult) || (BufferSize != cbBytes))
314 {
315 ShowMessages("err, occurred while writing to the server (%x)\n",
316 GetLastError());
317 CloseHandle(PipeHandle);
318
319 return FALSE;
320 }
321 else
322 {
323 return TRUE;
324 }
325}

◆ NamedPipeServerCloseHandle()

VOID NamedPipeServerCloseHandle ( HANDLE PipeHandle)

Close handle of server's named pipe.

Parameters
PipeHandle
Returns
VOID
314{
315 CloseHandle(PipeHandle);
316}

◆ NamedPipeServerCreatePipe()

HANDLE NamedPipeServerCreatePipe ( LPCSTR PipeName,
UINT32 OutputBufferSize,
UINT32 InputBufferSize )

Create a named pipe server.

Parameters
PipeName
OutputBufferSize
InputBufferSize
Returns
HANDLE
177{
178 HANDLE hPipe;
179
180 hPipe = CreateNamedPipeA(PipeName, // pipe name
181 PIPE_ACCESS_DUPLEX, // read/write access
182 PIPE_TYPE_MESSAGE | // message type pipe
183 PIPE_READMODE_MESSAGE | // message-read mode
184 PIPE_WAIT, // blocking mode
185 PIPE_UNLIMITED_INSTANCES, // max. instances
186 OutputBufferSize, // output buffer size
187 InputBufferSize, // input buffer size
188 NMPWAIT_USE_DEFAULT_WAIT, // client time-out
189 NULL); // default security attribute
190
191 if (INVALID_HANDLE_VALUE == hPipe)
192 {
193 printf("err, occurred while creating the pipe (%x)\n",
194 GetLastError());
195 return NULL;
196 }
197 return hPipe;
198}

◆ NamedPipeServerReadClientMessage()

UINT32 NamedPipeServerReadClientMessage ( HANDLE PipeHandle,
CHAR * BufferToSave,
INT MaximumReadBufferLength )

read client message from the named pipe

Parameters
PipeHandle
BufferToSave
MaximumReadBufferLength
Returns
UINT32
97{
98 DWORD cbBytes;
99
100 //
101 // We are connected to the client.
102 // To communicate with the client
103 // we will use ReadFile()/WriteFile()
104 // on the pipe handle - hPipe
105 //
106
107 //
108 // Read client message
109 //
110 BOOL bResult = ReadFile(PipeHandle, // handle to pipe
111 BufferToSave, // buffer to receive data
112 MaximumReadBufferLength, // size of buffer
113 &cbBytes, // number of bytes read
114 NULL); // not overlapped I/O
115
116 if ((!bResult) || (0 == cbBytes))
117 {
118 ShowMessages("err, occurred while reading from the client (%x)\n",
119 GetLastError());
120 CloseHandle(PipeHandle);
121 return 0;
122 }
123
124 //
125 // Number of bytes that the client sends to us
126 //
127 return cbBytes;
128}

◆ NamedPipeServerSendMessageToClient()

BOOLEAN NamedPipeServerSendMessageToClient ( HANDLE PipeHandle,
CHAR * BufferToSend,
INT BufferSize )
134{
135 DWORD cbBytes;
136
137 //
138 // Reply to client
139 //
140 BOOLEAN bResult =
141 WriteFile(PipeHandle, // handle to pipe
142 BufferToSend, // buffer to write from
143 BufferSize, // number of bytes to write, include the NULL
144 &cbBytes, // number of bytes written
145 NULL); // not overlapped I/O
146
147 if ((!bResult) || (BufferSize != cbBytes))
148 {
149 ShowMessages("err, occurred while writing to the client (%x)\n",
150 GetLastError());
151 CloseHandle(PipeHandle);
152 return FALSE;
153 }
154 return TRUE;
155}
UCHAR BOOLEAN
Definition BasicTypes.h:35

◆ NamedPipeServerWaitForClientConntection()

BOOLEAN NamedPipeServerWaitForClientConntection ( HANDLE PipeHandle)

wait for client connection

Parameters
PipeHandle
Returns
BOOLEAN
67{
68 //
69 // Wait for the client to connect
70 //
71 BOOL ClientConnected = ConnectNamedPipe(PipeHandle, NULL);
72
73 if (FALSE == ClientConnected)
74 {
75 ShowMessages("err, occurred while connecting to the client (%x)\n",
76 GetLastError());
77 CloseHandle(PipeHandle);
78 return FALSE;
79 }
80
81 //
82 // Client connected
83 //
84 return TRUE;
85}