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 NamedPipeServerWaitForClientConnection (HANDLE PipeHandle)
 wait for client connection
UINT32 NamedPipeServerReadClientMessage (HANDLE PipeHandle, CHAR *BufferToSave, INT32 MaximumReadBufferLength)
 read client message from the named pipe
BOOLEAN NamedPipeServerSendMessageToClient (HANDLE PipeHandle, CHAR *BufferToSend, INT32 BufferSize)
 Send a message to the client over named pipe.
VOID NamedPipeServerCloseHandle (HANDLE PipeHandle)
 Close handle of server's named pipe.
HANDLE NamedPipeClientCreatePipe (LPCSTR PipeName)
 Create a client named pipe.
BOOLEAN NamedPipeClientSendMessage (HANDLE PipeHandle, CHAR *BufferToSend, INT32 BufferSize)
 Send client message over named pipe.
UINT32 NamedPipeClientReadMessage (HANDLE PipeHandle, CHAR *BufferToRead, INT32 MaximumSizeOfBuffer)
 Read a message from the server over named pipe.
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

◆ NamedPipeClientReadMessage()

UINT32 NamedPipeClientReadMessage ( HANDLE PipeHandle,
CHAR * BufferToRead,
INT32 MaximumSizeOfBuffer )

Read a message from the server over named pipe.

Parameters
PipeHandleHandle of the named pipe
BufferToReadBuffer to store the received message
MaximumSizeOfBufferMaximum size of the receive buffer
Returns
UINT32 number of bytes read, or 0 on failure
431{
432 DWORD BytesTransferred;
433
434 //
435 // Read server response
436 //
437 BOOLEAN Result = ReadFile(PipeHandle, // handle to pipe
438 BufferToRead, // buffer to receive data
439 MaximumSizeOfBuffer, // size of buffer
440 &BytesTransferred, // number of bytes read
441 NULL); // not overlapped I/O
442
443 if ((!Result) || (0 == BytesTransferred))
444 {
445 printf("err, occurred while reading from the server (%x)\n",
446 GetLastError());
447 CloseHandle(PipeHandle);
448 return 0; // Error
449 }
450
451 //
452 // Success
453 //
454 return BytesTransferred;
455}
UCHAR BOOLEAN
Definition BasicTypes.h:35
unsigned long DWORD
Definition BasicTypes.h:38

◆ NamedPipeClientSendMessage()

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

Send client message over named pipe.

Parameters
PipeHandleHandle of the named pipe
BufferToSendBuffer containing the message to send
BufferSizeSize of the buffer to send
Returns
BOOLEAN TRUE if successful, FALSE otherwise
383{
384 //
385 // We are done connecting to the server pipe,
386 // we can start communicating with
387 // the server using ReadFile()/WriteFile()
388 // on handle - hPipe
389 //
390
391 DWORD BytesTransferred;
392
393 //
394 // Send the message to server
395 //
396 BOOLEAN Result =
397 WriteFile(PipeHandle, // handle to pipe
398 BufferToSend, // buffer to write from
399 BufferSize, // number of bytes to write, include the NULL
400 &BytesTransferred, // number of bytes written
401 NULL); // not overlapped I/O
402
403 if ((!Result) || (BufferSize != (INT32)BytesTransferred))
404 {
405 printf("err, occurred while writing to the server (%x)\n",
406 GetLastError());
407 CloseHandle(PipeHandle);
408
409 //
410 // Error
411 //
412 CloseHandle(PipeHandle);
413 return FALSE;
414 }
415 else
416 {
417 return TRUE;
418 }
419}
signed int INT32
Definition BasicTypes.h:50
#define TRUE
Definition BasicTypes.h:114
#define FALSE
Definition BasicTypes.h:113

◆ 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,
INT32 MaximumReadBufferLength )

read client message from the named pipe

Parameters
PipeHandle
BufferToSave
MaximumReadBufferLength
Returns
UINT32
238{
239 DWORD BytesTransferred;
240
241 //
242 // We are connected to the client.
243 // To communicate with the client
244 // we will use ReadFile()/WriteFile()
245 // on the pipe handle - hPipe
246 //
247
248 //
249 // Read client message
250 //
251 BOOLEAN Result = ReadFile(PipeHandle, // handle to pipe
252 BufferToSave, // buffer to receive data
253 MaximumReadBufferLength, // size of buffer
254 &BytesTransferred, // number of bytes read
255 NULL); // not overlapped I/O
256
257 if ((!Result) || (0 == BytesTransferred))
258 {
259 printf("err, occurred while reading from the client (%x)\n",
260 GetLastError());
261 CloseHandle(PipeHandle);
262 return 0;
263 }
264
265 //
266 // Number of bytes that the client sends to us
267 //
268 return BytesTransferred;
269}

◆ NamedPipeServerSendMessageToClient()

BOOLEAN NamedPipeServerSendMessageToClient ( HANDLE PipeHandle,
CHAR * BufferToSend,
INT32 BufferSize )

Send a message to the client over named pipe.

Parameters
PipeHandleHandle of the named pipe
BufferToSendBuffer containing the message to send
BufferSizeSize of the buffer to send
Returns
BOOLEAN TRUE if successful, FALSE otherwise
283{
284 DWORD BytesTransferred;
285
286 //
287 // Reply to client
288 //
289 BOOLEAN Result =
290 WriteFile(PipeHandle, // handle to pipe
291 BufferToSend, // buffer to write from
292 BufferSize, // number of bytes to write, include the NULL
293 &BytesTransferred, // number of bytes written
294 NULL); // not overlapped I/O
295
296 if ((!Result) || (BufferSize != (INT32)BytesTransferred))
297 {
298 printf("err, occurred while writing to the client (%x)\n",
299 GetLastError());
300 CloseHandle(PipeHandle);
301 return FALSE;
302 }
303 return TRUE;
304}

◆ NamedPipeServerWaitForClientConnection()

BOOLEAN NamedPipeServerWaitForClientConnection ( HANDLE PipeHandle)

wait for client connection

Parameters
PipeHandle
Returns
BOOLEAN
208{
209 //
210 // Wait for the client to connect
211 //
212 BOOLEAN ClientConnected = ConnectNamedPipe(PipeHandle, NULL);
213
214 if (FALSE == ClientConnected)
215 {
216 printf("err, occurred while connecting to the client (%x)\n",
217 GetLastError());
218 CloseHandle(PipeHandle);
219 return FALSE;
220 }
221
222 //
223 // Client connected
224 //
225 return TRUE;
226}