Revert "testprogs/win32/rpcecho-win32-pipe/server.c ..."
[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         long ret;
158         byte *in_bytes;
159         int in_alloc_size;
160         int in_bytes_count;
161         int i;
162
163         printf("TestBytePipeIn: Start\n");
164         fflush(stdout);
165
166         in_alloc_size = 5840 * sizeof(byte);
167         in_bytes = malloc(in_alloc_size);
168
169         in_bytes_count = chunk_size;
170         for (i=0; in_bytes_count > 0; i++) {
171                 in_bytes_count = 0;
172                 in_pipe->pull(in_pipe->state, in_bytes, in_alloc_size, &in_bytes_count);
173                 printf("pull[%u] bytes_count[%u]\n", i, in_bytes_count);
174                 fflush(stdout);
175         }
176
177         printf("TestBytePipeIn: End\n");
178         fflush(stdout);
179         ret = i;
180         RpcAsyncCompleteCall(TestBytePipeIn_AsyncHandle, &ret);
181 }
182
183 void TestBytePipeOut(
184         PRPC_ASYNC_STATE TestBytePipeOut_AsyncHandle,
185         long num_chunks,
186         long chunk_size,
187         ASYNC_BYTE_PIPE *out_pipe)
188 {
189         long ret;
190         byte *out_bytes = NULL;
191         unsigned long out_alloc_size = 0;
192         unsigned long out_bytes_count = 0;
193         int i = 0;
194
195         printf("TestBytePipeOut: Start\n");
196         fflush(stdout);
197
198         out_alloc_size = chunk_size * sizeof(byte);
199         out_pipe->alloc(out_pipe->state, out_alloc_size, &out_bytes, &out_bytes_count);
200         memset(out_bytes, 0xCC, out_bytes_count * sizeof(byte));
201
202         for (i = 0; i <= num_chunks; i++) {
203                 if (i == num_chunks) {
204                         out_bytes_count = 0;
205                 }
206                 printf("[%u] push outb_len[%u]\n", i, out_bytes_count);
207                 fflush(stdout);
208                 out_pipe->push(out_pipe->state, out_bytes, out_bytes_count);
209         }
210
211         printf("TestBytePipeOut: End\n");
212         fflush(stdout);
213         ret = i;
214         RpcAsyncCompleteCall(TestBytePipeOut_AsyncHandle, &ret);
215 }
216
217 void main(int argc, char **argv)
218 {
219         RPC_STATUS status;
220         DWORD dwStatus;
221         RPC_BINDING_VECTOR *pBindingVector;
222 #define SERVER_PRINC_LEN 1024
223         unsigned server_princ_len = SERVER_PRINC_LEN;
224         char server_princ[SERVER_PRINC_LEN];
225
226         if (argc != 1) {
227                 printf("Usage: rpcechosrv\n");
228                 exit(0);
229         }
230
231         status = RpcServerUseProtseqEp("ncacn_np", RPC_MAX_CALLS, "\\pipe\\rpcecho", NULL);
232         if (status) {
233                 printf("Failed to register ncacn_np endpoint\n");
234                 exit(status);
235         }
236
237         status = RpcServerUseProtseqEp("ncacn_ip_tcp", RPC_MAX_CALLS, "1234", NULL);
238         if (status) {
239                 printf("Failed to register ncacn_ip_tcp endpoint\n");
240                 exit(status);
241         }
242
243         status = RpcServerInqBindings(&pBindingVector);
244         if (status) {
245                 printf("Failed RpcServerInqBindings\n");
246                 exit(status);
247         }
248
249         status = RpcEpRegister(rpcecho_v1_0_s_ifspec, pBindingVector, NULL, "rpcecho server");
250         if (status) {
251                 printf("Failed RpcEpRegister\n");
252                 exit(status);
253         }
254
255         status = RpcServerRegisterIf(rpcecho_v1_0_s_ifspec, NULL, NULL);
256
257         if (status) {
258                 printf("Failed to register interface\n");
259                 exit(status);
260         }
261
262         dwStatus = DsMakeSpn("host",
263                              NULL,
264                              NULL,
265                              0,
266                              NULL,
267                              &server_princ_len,
268                              server_princ);
269         printf("server_princ: %s\n", server_princ);
270
271 #ifdef RPC_C_AUTHN_GSS_NEGOTIATE
272         status = RpcServerRegisterAuthInfo(server_princ, RPC_C_AUTHN_GSS_NEGOTIATE, NULL, NULL);
273         if (status) {
274                 printf("Failed to setup auth info: RPC_C_AUTHN_GSS_NEGOTIATE\n");
275         }
276 #else
277         printf("Sorry auth info RPC_C_AUTHN_GSS_NEGOTIATE not compiled in\n");
278 #endif
279
280         status = RpcServerRegisterAuthInfo(NULL, RPC_C_AUTHN_WINNT, NULL, NULL);
281         if (status) {
282                 printf("Failed to setup auth info: RPC_C_AUTHN_WINNT\n");
283         }
284
285 #ifdef RPC_C_AUTHN_GSS_KERBEROS
286         status = RpcServerRegisterAuthInfo(server_princ, RPC_C_AUTHN_GSS_KERBEROS, NULL, NULL);
287         if (status) {
288                 printf("Failed to setup auth info: RPC_C_AUTHN_GSS_KERBEROS\n");
289         }
290 #else
291         printf("Sorry auth info RPC_C_AUTHN_GSS_KERBEROS not compiled in\n");
292 #endif
293
294         status = RpcServerListen(RPC_MIN_CALLS, RPC_MAX_CALLS, FALSE);
295
296         if (status) {
297                 printf("RpcServerListen returned error %d\n", status);
298                 exit(status);
299         }
300 }