testprogs/win32/rpcecho-win32-pipe server 1 byte reads
[metze/samba/wip.git] / testprogs / win32 / rpcecho-win32-pipe / server.c
1 /*
2    RPC echo server.
3
4    Copyright (C) Tim Potter 2003
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #define _WIN32_WINNT 0x0500
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <ctype.h>
26 #include "rpcecho.h"
27 #include <NtDsApi.h>
28
29 #define RPC_MIN_CALLS 1
30 #define RPC_MAX_CALLS 20
31 #define RPC_ENDPOINT "\\pipe\\rpcecho"
32
33 void AddOne(int in_data, __RPC_FAR int *out_data)
34 {
35         printf("AddOne: got in_data = %d\n", in_data);
36         *out_data = in_data + 1;
37 }
38
39 void EchoData(int len, unsigned char __RPC_FAR in_data[],
40         unsigned char __RPC_FAR out_data[])
41 {
42         printf("EchoData: got len = %d\n", len);
43
44         memcpy(out_data, in_data, len);
45 }
46
47 void SinkData(int len, unsigned char __RPC_FAR in_data[  ])
48 {
49         printf("SinkData: got len = %d\n", len);
50 }
51
52 void SourceData(int len, unsigned char __RPC_FAR out_data[  ])
53 {
54         int i;
55
56         printf("SourceData: got len = %d\n", len);
57
58         for (i = 0; i < len; i++)
59                 out_data[i] = i & 0xff;
60 }
61
62 void TestCall(wchar_t **s1, wchar_t **s2)
63 {
64         if (*s1) {
65                 printf("s1='%S'\n", *s1);
66         } else {
67                 printf("s1=NULL\n");
68         }
69         *s2 = L"test string";
70 }
71
72 long TestCall2(short level, echo_Info **info)
73 {
74         static echo_Info i;
75
76         printf("TestCall2 level %d\n", level);
77
78         *info = &i;
79
80         switch (level) {
81         case 1:
82                 i.info1.v = 10;
83                 break;
84         case 2:
85                 i.info2.v = 20;
86                 break;
87         case 3:
88                 i.info3.v = 30;
89                 break;
90         case 4:
91                 i.info4.v = 40;
92                 break;
93         case 5:
94                 i.info5.v1 = 50;
95                 i.info5.v2 = 51;
96                 break;
97         case 6:
98                 i.info6.v1 = 60;
99                 i.info6.info1.v = 61;
100                 break;
101         case 7:
102                 i.info7.v1 = 70;
103                 i.info7.info4.v = 71;
104                 break;
105         default:
106                 return -1;
107         }
108         return 0;
109 }
110
111 #if 0
112 void TestSleep(PRPC_ASYNC_STATE pAsync, long seconds)
113 {
114         long ret;
115         printf("async Sleeping for %d seconds\n", seconds);
116         Sleep(1000 * seconds);
117         ret = seconds;
118         RpcAsyncCompleteCall(pAsync, &ret);
119 }
120 #else
121 long TestSleep(long seconds)
122 {
123         printf("non-async Sleeping for %d seconds\n", seconds);
124         Sleep(1000 * seconds);
125         return seconds;
126 }
127 #endif
128
129 void TestEnum(
130         Enum1 *foo1,
131         Enum2 *foo2,
132         Enum3 *foo3
133         )
134 {
135         printf("TestEnum ...\n");
136 }
137
138 void TestSurrounding(Surrounding *data)
139 {
140         printf("TestSurrounding ...\n");
141 }
142
143 short TestDoublePointer(short ***data)
144 {
145         printf("TestDoublePointer ...\n");
146         return 0;
147 }
148
149 void TestBytePipeIn(
150         PRPC_ASYNC_STATE TestBytePipeIn_AsyncHandle,
151         long num_chunks,
152         long chunk_size,
153         ASYNC_BYTE_PIPE *in_pipe)
154 {
155         long ret;
156         byte in_bytes[0x80000];
157         int in_bytes_count;
158         int i;
159
160         printf("TestBytePipeIn: Start\n");
161         fflush(stdout);
162
163         in_bytes_count = sizeof(in_bytes);
164         for (i=0; in_bytes_count > 0; i++) {
165                 in_bytes_count = sizeof(in_bytes);
166                 in_pipe->pull(in_pipe->state, in_bytes, sizeof(byte), &in_bytes_count);
167                 printf("pull[%u] bytes_count[%u]\n", i, in_bytes_count);
168                 fflush(stdout);
169         }
170
171         printf("TestBytePipeIn: End\n");
172         fflush(stdout);
173         ret = i;
174         RpcAsyncCompleteCall(TestBytePipeIn_AsyncHandle, &ret);
175 }
176
177 void TestBytePipeOut(
178         PRPC_ASYNC_STATE TestBytePipeOut_AsyncHandle,
179         long num_chunks,
180         long chunk_size,
181         ASYNC_BYTE_PIPE *out_pipe)
182 {
183         long ret;
184         byte *out_bytes = NULL;
185         unsigned long out_alloc_size = 0;
186         unsigned long out_bytes_count = 0;
187         int i = 0;
188
189         printf("TestBytePipeOut: Start\n");
190         fflush(stdout);
191
192         out_alloc_size = chunk_size * sizeof(byte);
193         out_pipe->alloc(out_pipe->state, out_alloc_size, &out_bytes, &out_bytes_count);
194         memset(out_bytes, 0xCC, out_bytes_count * sizeof(byte));
195
196         for (i = 0; i <= num_chunks; i++) {
197                 if (i == num_chunks) {
198                         out_bytes_count = 0;
199                 }
200                 printf("[%u] push outb_len[%u]\n", i, out_bytes_count);
201                 fflush(stdout);
202                 out_pipe->push(out_pipe->state, out_bytes, out_bytes_count);
203         }
204
205         printf("TestBytePipeOut: End\n");
206         fflush(stdout);
207         ret = i;
208         RpcAsyncCompleteCall(TestBytePipeOut_AsyncHandle, &ret);
209 }
210
211 void main(int argc, char **argv)
212 {
213         RPC_STATUS status;
214         DWORD dwStatus;
215         RPC_BINDING_VECTOR *pBindingVector;
216 #define SERVER_PRINC_LEN 1024
217         unsigned server_princ_len = SERVER_PRINC_LEN;
218         char server_princ[SERVER_PRINC_LEN];
219
220         if (argc != 1) {
221                 printf("Usage: rpcechosrv\n");
222                 exit(0);
223         }
224
225         status = RpcServerUseProtseqEp("ncacn_np", RPC_MAX_CALLS, "\\pipe\\rpcecho", NULL);
226         if (status) {
227                 printf("Failed to register ncacn_np endpoint\n");
228                 exit(status);
229         }
230
231         status = RpcServerUseProtseqEp("ncacn_ip_tcp", RPC_MAX_CALLS, "1234", NULL);
232         if (status) {
233                 printf("Failed to register ncacn_ip_tcp endpoint\n");
234                 exit(status);
235         }
236
237         status = RpcServerInqBindings(&pBindingVector);
238         if (status) {
239                 printf("Failed RpcServerInqBindings\n");
240                 exit(status);
241         }
242
243         status = RpcEpRegister(rpcecho_v1_0_s_ifspec, pBindingVector, NULL, "rpcecho server");
244         if (status) {
245                 printf("Failed RpcEpRegister\n");
246                 exit(status);
247         }
248
249         status = RpcServerRegisterIf(rpcecho_v1_0_s_ifspec, NULL, NULL);
250
251         if (status) {
252                 printf("Failed to register interface\n");
253                 exit(status);
254         }
255
256         dwStatus = DsMakeSpn("host",
257                              NULL,
258                              NULL,
259                              0,
260                              NULL,
261                              &server_princ_len,
262                              server_princ);
263         printf("server_princ: %s\n", server_princ);
264
265 #ifdef RPC_C_AUTHN_GSS_NEGOTIATE
266         status = RpcServerRegisterAuthInfo(server_princ, RPC_C_AUTHN_GSS_NEGOTIATE, NULL, NULL);
267         if (status) {
268                 printf("Failed to setup auth info: RPC_C_AUTHN_GSS_NEGOTIATE\n");
269         }
270 #else
271         printf("Sorry auth info RPC_C_AUTHN_GSS_NEGOTIATE not compiled in\n");
272 #endif
273
274         status = RpcServerRegisterAuthInfo(NULL, RPC_C_AUTHN_WINNT, NULL, NULL);
275         if (status) {
276                 printf("Failed to setup auth info: RPC_C_AUTHN_WINNT\n");
277         }
278
279 #ifdef RPC_C_AUTHN_GSS_KERBEROS
280         status = RpcServerRegisterAuthInfo(server_princ, RPC_C_AUTHN_GSS_KERBEROS, NULL, NULL);
281         if (status) {
282                 printf("Failed to setup auth info: RPC_C_AUTHN_GSS_KERBEROS\n");
283         }
284 #else
285         printf("Sorry auth info RPC_C_AUTHN_GSS_KERBEROS not compiled in\n");
286 #endif
287
288         status = RpcServerListen(RPC_MIN_CALLS, RPC_MAX_CALLS, FALSE);
289
290         if (status) {
291                 printf("RpcServerListen returned error %d\n", status);
292                 exit(status);
293         }
294 }