ea8ed694c4ef3733e25e5676e458bf65072283a1
[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 #include <stdio.h>
22 #include <stdlib.h>
23 #include <ctype.h>
24 #include "rpcecho.h"
25 #include <NtDsApi.h>
26
27 #ifndef _M_AMD64
28 #error "please run 'vcvarsall.bat amd64' -midltests_tcp needs 64-bit support!"
29 #endif
30
31 #define RPC_MIN_CALLS 1
32 #define RPC_MAX_CALLS 20
33 #define RPC_ENDPOINT "\\pipe\\rpcecho"
34
35 void AddOne(int in_data, __RPC_FAR int *out_data)
36 {
37         printf("AddOne: got in_data = %d\n", in_data);
38         *out_data = in_data + 1;
39 }
40
41 void EchoData(int len, unsigned char __RPC_FAR in_data[],
42         unsigned char __RPC_FAR out_data[])
43 {
44         printf("EchoData: got len = %d\n", len);
45
46         memcpy(out_data, in_data, len);
47 }
48
49 void SinkData(int len, unsigned char __RPC_FAR in_data[  ])
50 {
51         printf("SinkData: got len = %d\n", len);
52 }
53
54 void SourceData(int len, unsigned char __RPC_FAR out_data[  ])
55 {
56         int i;
57
58         printf("SourceData: got len = %d\n", len);
59
60         for (i = 0; i < len; i++)
61                 out_data[i] = i & 0xff;
62 }
63
64 void TestCall(wchar_t **s1, wchar_t **s2)
65 {
66         if (*s1) {
67                 printf("s1='%S'\n", *s1);
68         } else {
69                 printf("s1=NULL\n");
70         }
71         *s2 = L"test string";
72 }
73
74 long TestCall2(short level, echo_Info **info)
75 {
76         static echo_Info i;
77
78         printf("TestCall2 level %d\n", level);
79
80         *info = &i;
81
82         switch (level) {
83         case 1:
84                 i.info1.v = 10;
85                 break;
86         case 2:
87                 i.info2.v = 20;
88                 break;
89         case 3:
90                 i.info3.v = 30;
91                 break;
92         case 4:
93                 i.info4.v = 40;
94                 break;
95         case 5:
96                 i.info5.v1 = 50;
97                 i.info5.v2 = 51;
98                 break;
99         case 6:
100                 i.info6.v1 = 60;
101                 i.info6.info1.v = 61;
102                 break;
103         case 7:
104                 i.info7.v1 = 70;
105                 i.info7.info4.v = 71;
106                 break;
107         default:
108                 return -1;
109         }
110         return 0;
111 }
112
113 #if 0
114 void TestSleep(PRPC_ASYNC_STATE pAsync, long seconds)
115 {
116         long ret;
117         printf("async Sleeping for %d seconds\n", seconds);
118         Sleep(1000 * seconds);
119         ret = seconds;
120         RpcAsyncCompleteCall(pAsync, &ret);
121 }
122 #else
123 long TestSleep(long seconds)
124 {
125         printf("non-async Sleeping for %d seconds\n", seconds);
126         Sleep(1000 * seconds);
127         return seconds;
128 }
129 #endif
130
131 void TestEnum(
132         Enum1 *foo1,
133         Enum2 *foo2,
134         Enum3 *foo3
135         )
136 {
137         printf("TestEnum ...\n");
138 }
139
140 void TestSurrounding(Surrounding *data)
141 {
142         printf("TestSurrounding ...\n");
143 }
144
145 short TestDoublePointer(short ***data)
146 {
147         printf("TestDoublePointer ...\n");
148         return 0;
149 }
150
151 void TestBytePipeIn(
152         PRPC_ASYNC_STATE TestBytePipeIn_AsyncHandle,
153         long num_chunks,
154         long chunk_size,
155         ASYNC_BYTE_PIPE *in_pipe)
156 {
157         RPC_STATUS status;
158         long ret = -1;
159         byte *in_bytes;
160         int in_alloc_size;
161         int in_bytes_count;
162         int i;
163
164         printf("TestBytePipeIn: Start\n");
165         fflush(stdout);
166
167         in_alloc_size = chunk_size * sizeof(byte);
168         in_bytes = malloc(in_alloc_size);
169         if (in_bytes == NULL) {
170                 printf("nomem: %d\n", in_alloc_size);
171                 fflush(stdout);
172                 ret = -2;
173                 goto done;
174         }
175
176         in_bytes_count = chunk_size;
177         for (i=0; in_bytes_count > 0; i++) {
178                 in_bytes_count = 0;
179                 status = in_pipe->pull(in_pipe->state, in_bytes, in_alloc_size, &in_bytes_count);
180                 if (status != 0) {
181                         printf("status: %d\n", status);
182                         fflush(stdout);
183                         ret = -3;
184                         goto done;
185                 }
186                 printf("pull[%u] bytes_count[%u]\n", i, in_bytes_count);
187                 fflush(stdout);
188         }
189
190         ret = i;
191 done:
192         printf("TestBytePipeIn: End ret[%d]\n", ret);
193         fflush(stdout);
194         if (in_bytes) {
195                 free(in_bytes);
196         }
197         RpcAsyncCompleteCall(TestBytePipeIn_AsyncHandle, &ret);
198 }
199
200 void TestBytePipeOut(
201         PRPC_ASYNC_STATE TestBytePipeOut_AsyncHandle,
202         long num_chunks,
203         long chunk_size,
204         ASYNC_BYTE_PIPE *out_pipe)
205 {
206         RPC_STATUS status;
207         long ret = -1;
208         byte *out_bytes = NULL;
209         unsigned long out_alloc_size = 0;
210         unsigned long out_bytes_count = 0;
211         int i = 0;
212
213         printf("TestBytePipeOut: Start\n");
214         fflush(stdout);
215
216         out_alloc_size = chunk_size * sizeof(byte);
217         status = out_pipe->alloc(out_pipe->state, out_alloc_size, &out_bytes, &out_bytes_count);
218         if (status != 0) {
219                 printf("status: %d\n", status);
220                 fflush(stdout);
221                 ret = -2;
222                 goto done;
223         }
224         memset(out_bytes, 0xCC, out_bytes_count * sizeof(byte));
225
226         for (i = 0; i <= num_chunks; i++) {
227                 if (i == num_chunks) {
228                         out_bytes_count = 0;
229                 }
230                 printf("[%u] push outb_len[%u]\n", i, out_bytes_count);
231                 fflush(stdout);
232                 status = out_pipe->push(out_pipe->state, out_bytes, out_bytes_count);
233                 if (status != 0) {
234                         printf("status: %d\n", status);
235                         fflush(stdout);
236                         ret = -3;
237                         goto done;
238                 }
239         }
240
241         ret = i;
242 done:
243         printf("TestBytePipeOut: End ret[%d]\n", ret);
244         fflush(stdout);
245         if (out_bytes) {
246                 free(out_bytes);
247         }
248         RpcAsyncCompleteCall(TestBytePipeOut_AsyncHandle, &ret);
249 }
250
251 void main(int argc, char **argv)
252 {
253         RPC_STATUS status;
254         DWORD dwStatus;
255         RPC_BINDING_VECTOR *pBindingVector;
256 #define SERVER_PRINC_LEN 1024
257         unsigned server_princ_len = SERVER_PRINC_LEN;
258         char server_princ[SERVER_PRINC_LEN];
259
260         if (argc != 1) {
261                 printf("Usage: rpcechosrv\n");
262                 exit(0);
263         }
264
265         status = RpcServerUseProtseqEp("ncacn_np", RPC_MAX_CALLS, "\\pipe\\rpcecho", NULL);
266         if (status) {
267                 printf("Failed to register ncacn_np endpoint\n");
268                 exit(status);
269         }
270
271         status = RpcServerUseProtseqEp("ncacn_ip_tcp", RPC_MAX_CALLS, "1234", NULL);
272         if (status) {
273                 printf("Failed to register ncacn_ip_tcp endpoint\n");
274                 exit(status);
275         }
276
277         status = RpcServerInqBindings(&pBindingVector);
278         if (status) {
279                 printf("Failed RpcServerInqBindings\n");
280                 exit(status);
281         }
282
283         status = RpcEpRegister(rpcecho_v1_0_s_ifspec, pBindingVector, NULL, "rpcecho server");
284         if (status) {
285                 printf("Failed RpcEpRegister\n");
286                 exit(status);
287         }
288
289         status = RpcServerRegisterIf(rpcecho_v1_0_s_ifspec, NULL, NULL);
290
291         if (status) {
292                 printf("Failed to register interface\n");
293                 exit(status);
294         }
295
296         dwStatus = DsMakeSpn("host",
297                              NULL,
298                              NULL,
299                              0,
300                              NULL,
301                              &server_princ_len,
302                              server_princ);
303         printf("server_princ: %s\n", server_princ);
304
305 #ifdef RPC_C_AUTHN_GSS_NEGOTIATE
306         status = RpcServerRegisterAuthInfo(server_princ, RPC_C_AUTHN_GSS_NEGOTIATE, NULL, NULL);
307         if (status) {
308                 printf("Failed to setup auth info: RPC_C_AUTHN_GSS_NEGOTIATE\n");
309         }
310 #else
311         printf("Sorry auth info RPC_C_AUTHN_GSS_NEGOTIATE not compiled in\n");
312 #endif
313
314         status = RpcServerRegisterAuthInfo(NULL, RPC_C_AUTHN_WINNT, NULL, NULL);
315         if (status) {
316                 printf("Failed to setup auth info: RPC_C_AUTHN_WINNT\n");
317         }
318
319 #ifdef RPC_C_AUTHN_GSS_KERBEROS
320         status = RpcServerRegisterAuthInfo(server_princ, RPC_C_AUTHN_GSS_KERBEROS, NULL, NULL);
321         if (status) {
322                 printf("Failed to setup auth info: RPC_C_AUTHN_GSS_KERBEROS\n");
323         }
324 #else
325         printf("Sorry auth info RPC_C_AUTHN_GSS_KERBEROS not compiled in\n");
326 #endif
327
328         status = RpcServerListen(RPC_MIN_CALLS, RPC_MAX_CALLS, FALSE);
329
330         if (status) {
331                 printf("RpcServerListen returned error %d\n", status);
332                 exit(status);
333         }
334 }