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
15 static const char * MyName = "LPC-SRV";
16
17 HANDLE OutputHandle;
18 HANDLE InputHandle;
19
debug_printf(char * fmt,...)20 void debug_printf(char* fmt, ...)
21 {
22 va_list args;
23 char buffer[255];
24
25 va_start(args,fmt);
26 vsprintf(buffer,fmt,args);
27 WriteConsoleA(OutputHandle, buffer, strlen(buffer), NULL, NULL);
28 va_end(args);
29 }
30
31
main(int argc,char * argv[])32 int main(int argc, char* argv[])
33 {
34 UNICODE_STRING PortName = ROS_STRING_INITIALIZER(TEST_PORT_NAME_U);
35 OBJECT_ATTRIBUTES ObjectAttributes;
36 NTSTATUS Status;
37 HANDLE NamedPortHandle;
38 HANDLE PortHandle;
39 LPC_MAX_MESSAGE ConnectMsg;
40
41 printf("%s: Lpc test server\n", MyName);
42
43 InitializeObjectAttributes(&ObjectAttributes,
44 &PortName,
45 0,
46 NULL,
47 NULL);
48
49 printf("%s: Creating port \"%s\"...\n", MyName, TEST_PORT_NAME);
50 Status = NtCreatePort(&NamedPortHandle,
51 &ObjectAttributes,
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 */
139