testprogs/win32/rpcecho-win32-pipe ...
[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 main(int argc, char **argv)
130 {
131         RPC_STATUS status;
132         DWORD dwStatus;
133         RPC_BINDING_VECTOR *pBindingVector;
134 #define SERVER_PRINC_LEN 1024
135         unsigned server_princ_len = SERVER_PRINC_LEN;
136         char server_princ[SERVER_PRINC_LEN];
137
138         if (argc != 1) {
139                 printf("Usage: rpcechosrv\n");
140                 exit(0);
141         }
142
143         status = RpcServerUseProtseqEp("ncacn_np", RPC_MAX_CALLS, "\\pipe\\rpcecho", NULL);
144         if (status) {
145                 printf("Failed to register ncacn_np endpoint\n");
146                 exit(status);
147         }
148
149         status = RpcServerUseProtseqEp("ncacn_ip_tcp", RPC_MAX_CALLS, "1234", NULL);
150         if (status) {
151                 printf("Failed to register ncacn_ip_tcp endpoint\n");
152                 exit(status);
153         }
154
155         status = RpcServerInqBindings(&pBindingVector);
156         if (status) {
157                 printf("Failed RpcServerInqBindings\n");
158                 exit(status);
159         }
160
161         status = RpcEpRegister(rpcecho_v4_0_s_ifspec, pBindingVector, NULL, "rpcecho server");
162         if (status) {
163                 printf("Failed RpcEpRegister\n");
164                 exit(status);
165         }
166
167         status = RpcServerRegisterIf(rpcecho_v4_0_s_ifspec, NULL, NULL);
168
169         if (status) {
170                 printf("Failed to register interface\n");
171                 exit(status);
172         }
173
174         dwStatus = DsMakeSpn("host",
175                              NULL,
176                              NULL,
177                              0,
178                              NULL,
179                              &server_princ_len,
180                              server_princ);
181         printf("server_princ: %s\n", server_princ);
182
183 #ifdef RPC_C_AUTHN_GSS_NEGOTIATE
184         status = RpcServerRegisterAuthInfo(server_princ, RPC_C_AUTHN_GSS_NEGOTIATE, NULL, NULL);
185         if (status) {
186                 printf("Failed to setup auth info: RPC_C_AUTHN_GSS_NEGOTIATE\n");
187         }
188 #else
189         printf("Sorry auth info RPC_C_AUTHN_GSS_NEGOTIATE not compiled in\n");
190 #endif
191
192         status = RpcServerRegisterAuthInfo(NULL, RPC_C_AUTHN_WINNT, NULL, NULL);
193         if (status) {
194                 printf("Failed to setup auth info: RPC_C_AUTHN_WINNT\n");
195         }
196
197 #ifdef RPC_C_AUTHN_GSS_KERBEROS
198         status = RpcServerRegisterAuthInfo(server_princ, RPC_C_AUTHN_GSS_KERBEROS, NULL, NULL);
199         if (status) {
200                 printf("Failed to setup auth info: RPC_C_AUTHN_GSS_KERBEROS\n");
201         }
202 #else
203         printf("Sorry auth info RPC_C_AUTHN_GSS_KERBEROS not compiled in\n");
204 #endif
205
206         status = RpcServerListen(RPC_MIN_CALLS, RPC_MAX_CALLS, FALSE);
207
208         if (status) {
209                 printf("RpcServerListen returned error %d\n", status);
210                 exit(status);
211         }
212 }