ReactOS 0.4.15-dev-8344-g0574987
lpcsrv.c
Go to the documentation of this file.
1/*
2 * DESCRIPTION: Simple LPC Server
3 * PROGRAMMER: David Welch
4 */
5#include <ddk/ntddk.h>
6#include <rosrtl/string.h>
7#include <windows.h>
8#include <stdarg.h>
9#include <string.h>
10#include <stdio.h>
11#include <stdlib.h>
12
13#include "lpctest.h"
14
15static const char * MyName = "LPC-SRV";
16
19
20void debug_printf(char* fmt, ...)
21{
23 char buffer[255];
24
28 va_end(args);
29}
30
31
32int main(int argc, char* argv[])
33{
34 UNICODE_STRING PortName = ROS_STRING_INITIALIZER(TEST_PORT_NAME_U);
37 HANDLE NamedPortHandle;
38 HANDLE PortHandle;
39 LPC_MAX_MESSAGE ConnectMsg;
40
41 printf("%s: Lpc test server\n", MyName);
42
44 &PortName,
45 0,
46 NULL,
47 NULL);
48
49 printf("%s: Creating port \"%s\"...\n", MyName, TEST_PORT_NAME);
50 Status = NtCreatePort(&NamedPortHandle,
52 0,
53 0,
54 0);
55 if (!NT_SUCCESS(Status))
56 {
57 printf("%s: NtCreatePort() failed with status = 0x%08lX.\n", MyName, Status);
58 return EXIT_FAILURE;
59 }
60 printf("%s: Port \"%s\" created (0x%x).\n\n", MyName, TEST_PORT_NAME, NamedPortHandle);
61
62 for (;;)
63 {
64 printf("%s: Listening for connections requests on port 0x%x...\n", MyName, NamedPortHandle);
65 Status = NtListenPort(NamedPortHandle,
66 &ConnectMsg.Header);
67 if (!NT_SUCCESS(Status))
68 {
69 printf("%s: NtListenPort() failed with status = 0x%08lX.\n", MyName, Status);
70 return EXIT_FAILURE;
71 }
72
73 printf("%s: Received connection request 0x%08x on port 0x%x.\n", MyName,
74 ConnectMsg.Header.MessageId, NamedPortHandle);
75 printf("%s: Request from: PID=%x, TID=%x.\n", MyName,
76 ConnectMsg.Header.ClientId.UniqueProcess, ConnectMsg.Header.ClientId.UniqueThread);
77
78 printf("%s: Accepting connection request 0x%08x...\n", MyName,
79 ConnectMsg.Header.MessageId);
80 Status = NtAcceptConnectPort(&PortHandle,
81 NamedPortHandle,
82 & ConnectMsg.Header,
83 TRUE,
84 0,
85 NULL);
86 if (!NT_SUCCESS(Status))
87 {
88 printf("%s: NtAcceptConnectPort() failed with status = 0x%08lX.\n", MyName, Status);
89 return EXIT_FAILURE;
90 }
91 printf("%s: Connection request 0x%08x accepted as port 0x%x.\n", MyName,
92 ConnectMsg.Header.MessageId, PortHandle);
93
94 printf("%s: Completing connection for port 0x%x (0x%08x).\n", MyName,
95 PortHandle, ConnectMsg.Header.MessageId);
96 Status = NtCompleteConnectPort(PortHandle);
97 if (!NT_SUCCESS(Status))
98 {
99 printf("%s: NtCompleteConnectPort() failed with status = 0x%08lX.\n", MyName, Status);
100 return EXIT_FAILURE;
101 }
102
103 printf("%s: Entering server loop for port 0x%x...\n", MyName, PortHandle);
104 for(;;)
105 {
106 LPC_MAX_MESSAGE Request;
107
108 Status = NtReplyWaitReceivePort(PortHandle,
109 0,
110 NULL,
111 &Request.Header);
112 if (!NT_SUCCESS(Status))
113 {
114 printf("%s: NtReplyWaitReceivePort() failed with status = 0x%08lX.\n", MyName, Status);
115 return EXIT_FAILURE;
116 }
117
118 if (LPC_DATAGRAM == PORT_MESSAGE_TYPE(Request))
119 {
120 printf("%s: Datagram message contents are <%s>.\n",
121 MyName,
122 Request.Data);
123 }
124 else
125 {
126 printf("%s: Message with type %d received on port 0x%x.\n", MyName,
127 PORT_MESSAGE_TYPE(Request), PortHandle);
128 NtClose(PortHandle);
129 printf("%s: Connected port 0x%x closed.\n\n", MyName, PortHandle);
130 break;
131 }
132 }
133 }
134 return EXIT_SUCCESS;
135}
136
137
138/* EOF */
static UNICODE_STRING PortName
static int argc
Definition: ServiceArgs.c:12
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
char * va_list
Definition: acmsvcex.h:78
#define va_end(ap)
Definition: acmsvcex.h:90
#define va_start(ap, A)
Definition: acmsvcex.h:91
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS NTAPI NtAcceptConnectPort(OUT PHANDLE PortHandle, IN PVOID PortContext OPTIONAL, IN PPORT_MESSAGE ReplyMessage, IN BOOLEAN AcceptConnection, IN OUT PPORT_VIEW ServerView OPTIONAL, OUT PREMOTE_PORT_VIEW ClientView OPTIONAL)
Definition: complete.c:40
NTSTATUS NTAPI NtCompleteConnectPort(IN HANDLE PortHandle)
Definition: complete.c:423
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:36
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleA(IN HANDLE hConsoleOutput, IN CONST VOID *lpBuffer, IN DWORD nNumberOfCharsToWrite, OUT LPDWORD lpNumberOfCharsWritten, LPVOID lpReserved)
Definition: readwrite.c:1468
int main()
Definition: test.c:6
#define printf
Definition: freeldr.h:97
Status
Definition: gdiplustypes.h:25
GLuint buffer
Definition: glext.h:5915
int __cdecl vsprintf(char *_Dest, const char *_Format, va_list _Args)
Definition: sprintf.c:733
#define EXIT_FAILURE
Definition: jerror.c:33
HANDLE InputHandle
Definition: lpcsrv.c:18
void debug_printf(char *fmt,...)
Definition: lpcsrv.c:20
static const char * MyName
Definition: lpcsrv.c:15
HANDLE OutputHandle
Definition: lpcsrv.c:17
#define TEST_PORT_NAME
Definition: lpctest.h:3
#define TEST_PORT_NAME_U
Definition: lpctest.h:4
#define LPC_DATAGRAM
Definition: port.c:95
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define argv
Definition: mplay32.c:18
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3402
NTSTATUS NTAPI NtCreatePort(OUT PHANDLE PortHandle, IN POBJECT_ATTRIBUTES ObjectAttributes, IN ULONG MaxConnectInfoLength, IN ULONG MaxDataLength, IN ULONG MaxPoolUsage)
Definition: create.c:222
NTSTATUS NTAPI NtListenPort(IN HANDLE PortHandle, OUT PPORT_MESSAGE ConnectMessage)
Definition: listen.c:22
NTSTATUS NTAPI NtReplyWaitReceivePort(IN HANDLE PortHandle, OUT PVOID *PortContext OPTIONAL, IN PPORT_MESSAGE ReplyMessage OPTIONAL, OUT PPORT_MESSAGE ReceiveMessage)
Definition: reply.c:743
#define EXIT_SUCCESS
Definition: rdjpgcom.c:55
#define args
Definition: format.c:66
Definition: match.c:390
Definition: dsound.c:943
_In_ WDFREQUEST Request
Definition: wdfdevice.h:547