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
303{
304 CloseHandle(PipeHandle);
305}

◆ 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
180{
181 HANDLE hPipe;
182
183 //
184 // Connect to the server pipe using CreateFile()
185 //
186 hPipe = CreateFileA(PipeName, // pipe name
187 GENERIC_READ | // read and write access
188 GENERIC_WRITE,
189 0, // no sharing
190 NULL, // default security attributes
191 OPEN_EXISTING, // opens existing pipe
192 0, // default attributes
193 NULL); // no template file
194
195 if (INVALID_HANDLE_VALUE == hPipe)
196 {
197 printf("err, occurred while connecting to the server (%x)\n",
198 GetLastError());
199 //
200 // One might want to check whether the server pipe is busy
201 // This sample will error out if the server pipe is busy
202 // Read on ERROR_PIPE_BUSY and WaitNamedPipe() for that
203 //
204
205 //
206 // Error
207 //
208 return NULL;
209 }
210 else
211 {
212 return hPipe;
213 }
214}
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:55
#define FALSE
Definition BasicTypes.h:54
OVERLAPPED g_OverlappedIoStructureForWriteDebugger
Definition globals.h:299
OVERLAPPED g_OverlappedIoStructureForReadDebugger
This is an OVERLAPPED structure for managing simultaneous read and writes for debugger (in current de...
Definition globals.h:298
VOID ShowMessages(const char *Fmt,...)
Show messages.
Definition libhyperdbg.cpp:96

◆ NamedPipeClientReadMessage()

UINT32 NamedPipeClientReadMessage ( HANDLE PipeHandle,
char * BufferToRead,
int MaximumSizeOfBuffer )
269{
270 DWORD cbBytes;
271
272 //
273 // Read server response
274 //
275 BOOL bResult = ReadFile(PipeHandle, // handle to pipe
276 BufferToRead, // buffer to receive data
277 MaximumSizeOfBuffer, // size of buffer
278 &cbBytes, // number of bytes read
279 NULL); // not overlapped I/O
280
281 if ((!bResult) || (0 == cbBytes))
282 {
283 printf("err, occurred while reading from the server (%x)\n",
284 GetLastError());
285 CloseHandle(PipeHandle);
286 return NULL; // Error
287 }
288
289 //
290 // Success
291 //
292 return cbBytes;
293}
int BOOL
Definition BasicTypes.h:23
unsigned long DWORD
Definition BasicTypes.h:22

◆ NamedPipeClientSendMessage()

BOOLEAN NamedPipeClientSendMessage ( HANDLE PipeHandle,
char * BufferToSend,
int BufferSize )

send client message over named pipe

Parameters
PipeHandle
BufferToSend
BufferSize
Returns
BOOLEAN
226{
227 //
228 // We are done connecting to the server pipe,
229 // we can start communicating with
230 // the server using ReadFile()/WriteFile()
231 // on handle - hPipe
232 //
233
234 DWORD cbBytes;
235
236 //
237 // Send the message to server
238 //
239 BOOL bResult =
240 WriteFile(PipeHandle, // handle to pipe
241 BufferToSend, // buffer to write from
242 BufferSize, // number of bytes to write, include the NULL
243 &cbBytes, // number of bytes written
244 NULL); // not overlapped I/O
245
246 if ((!bResult) || (BufferSize != cbBytes))
247 {
248 printf("err, occurred while writing to the server (%x)\n",
249 GetLastError());
250 CloseHandle(PipeHandle);
251
252 //
253 // Error
254 //
255 CloseHandle(PipeHandle);
256 return FALSE;
257 }
258 else
259 {
260 return TRUE;
261 }
262}

◆ NamedPipeServerCloseHandle()

VOID NamedPipeServerCloseHandle ( HANDLE PipeHandle)

Close handle of server's named pipe.

Parameters
PipeHandle
Returns
VOID
157{
158 CloseHandle(PipeHandle);
159}

◆ NamedPipeServerCreatePipe()

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

Create a named pipe server.

Parameters
PipeName
OutputBufferSize
InputBufferSize
Returns
HANDLE
28{
29 HANDLE hPipe;
30
31 hPipe = CreateNamedPipeA(PipeName, // pipe name
32 PIPE_ACCESS_DUPLEX, // read/write access
33 PIPE_TYPE_MESSAGE | // message type pipe
34 PIPE_READMODE_MESSAGE | // message-read mode
35 PIPE_WAIT, // blocking mode
36 PIPE_UNLIMITED_INSTANCES, // max. instances
37 OutputBufferSize, // output buffer size
38 InputBufferSize, // input buffer size
39 NMPWAIT_USE_DEFAULT_WAIT, // client time-out
40 NULL); // default security attribute
41
42 if (INVALID_HANDLE_VALUE == hPipe)
43 {
44 printf("err, occurred while creating the pipe (%x)\n",
45 GetLastError());
46 return NULL;
47 }
48 return hPipe;
49}

◆ NamedPipeServerReadClientMessage()

UINT32 NamedPipeServerReadClientMessage ( HANDLE PipeHandle,
char * BufferToSave,
int MaximumReadBufferLength )

read client message from the named pipe

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

◆ NamedPipeServerSendMessageToClient()

BOOLEAN NamedPipeServerSendMessageToClient ( HANDLE PipeHandle,
char * BufferToSend,
int BufferSize )
126{
127 DWORD cbBytes;
128
129 //
130 // Reply to client
131 //
132 BOOLEAN bResult =
133 WriteFile(PipeHandle, // handle to pipe
134 BufferToSend, // buffer to write from
135 BufferSize, // number of bytes to write, include the NULL
136 &cbBytes, // number of bytes written
137 NULL); // not overlapped I/O
138
139 if ((!bResult) || (BufferSize != cbBytes))
140 {
141 printf("Error occurred while writing to the client (%x)\n",
142 GetLastError());
143 CloseHandle(PipeHandle);
144 return FALSE;
145 }
146 return TRUE;
147}
UCHAR BOOLEAN
Definition BasicTypes.h:39

◆ NamedPipeServerWaitForClientConntection()

BOOLEAN NamedPipeServerWaitForClientConntection ( HANDLE PipeHandle)

wait for client connection

Parameters
PipeHandle
Returns
BOOLEAN
59{
60 //
61 // Wait for the client to connect
62 //
63 BOOL bClientConnected = ConnectNamedPipe(PipeHandle, NULL);
64
65 if (FALSE == bClientConnected)
66 {
67 printf("err, occurred while connecting to the client (%x)\n",
68 GetLastError());
69 CloseHandle(PipeHandle);
70 return FALSE;
71 }
72
73 //
74 // Client connected
75 //
76 return TRUE;
77}