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 
28 #define RPC_MIN_CALLS 1
29 #define RPC_MAX_CALLS 20
30 #define RPC_ENDPOINT "\\pipe\\rpcecho"
31 
AddOne(int in_data,__RPC_FAR int * out_data)32 void AddOne(int in_data, __RPC_FAR int *out_data)
33 {
34 	printf("AddOne: got in_data = %d\n", in_data);
35 	*out_data = in_data + 1;
36 }
37 
EchoData(int len,unsigned char __RPC_FAR in_data[],unsigned char __RPC_FAR out_data[])38 void EchoData(int len, unsigned char __RPC_FAR in_data[],
39 	unsigned char __RPC_FAR out_data[])
40 {
41 	printf("EchoData: got len = %d\n", len);
42 
43 	memcpy(out_data, in_data, len);
44 }
45 
SinkData(int len,unsigned char __RPC_FAR in_data[])46 void SinkData(int len, unsigned char __RPC_FAR in_data[  ])
47 {
48 	printf("SinkData: got len = %d\n", len);
49 }
50 
SourceData(int len,unsigned char __RPC_FAR out_data[])51 void SourceData(int len, unsigned char __RPC_FAR out_data[  ])
52 {
53 	int i;
54 
55 	printf("SourceData: got len = %d\n", len);
56 
57 	for (i = 0; i < len; i++)
58 		out_data[i] = i & 0xff;
59 }
60 
TestCall(wchar_t ** s1,wchar_t ** s2)61 void TestCall(wchar_t **s1, wchar_t **s2)
62 {
63 	if (*s1) {
64 		printf("s1='%S'\n", *s1);
65 	} else {
66 		printf("s1=NULL\n");
67 	}
68 	*s2 = L"test string";
69 }
70 
TestCall2(short level,echo_Info ** info)71 long TestCall2(short level, echo_Info **info)
72 {
73 	static echo_Info i;
74 
75 	printf("TestCall2 level %d\n", level);
76 
77 	*info = &i;
78 
79 	switch (level) {
80 	case 1:
81 		i.info1.v = 10;
82 		break;
83 	case 2:
84 		i.info2.v = 20;
85 		break;
86 	case 3:
87 		i.info3.v = 30;
88 		break;
89 	case 4:
90 		i.info4.v = 40;
91 		break;
92 	case 5:
93 		i.info5.v1 = 50;
94 		i.info5.v2 = 51;
95 		break;
96 	case 6:
97 		i.info6.v1 = 60;
98 		i.info6.info1.v = 61;
99 		break;
100 	case 7:
101 		i.info7.v1 = 70;
102 		i.info7.info4.v = 71;
103 		break;
104 	default:
105 		return -1;
106 	}
107 	return 0;
108 }
109 
110 #if 0
111 void TestSleep(PRPC_ASYNC_STATE pAsync, long seconds)
112 {
113 	long ret;
114 	printf("async Sleeping for %d seconds\n", seconds);
115 	Sleep(1000 * seconds);
116 	ret = seconds;
117 	RpcAsyncCompleteCall(pAsync, &ret);
118 }
119 #else
TestSleep(long seconds)120 long TestSleep(long seconds)
121 {
122 	printf("non-async Sleeping for %d seconds\n", seconds);
123 	Sleep(1000 * seconds);
124 	return seconds;
125 }
126 #endif
127 
128 
echo_TestEnum(echo_Enum1 * foo1,echo_Enum2 * foo2,echo_Enum3 * foo3)129 void echo_TestEnum(echo_Enum1 *foo1,
130 		   echo_Enum2 *foo2,
131 		   echo_Enum3 *foo3)
132 {
133 	foo2->e1 = ECHO_ENUM2;
134 }
135 
echo_TestSurrounding(echo_Surrounding * data)136 void echo_TestSurrounding(echo_Surrounding *data)
137 {
138 	printf("Incoming array of size %d\n", data->x);
139 	data->x *= 2;
140 }
141 
echo_TestDoublePointer(short *** data)142 short echo_TestDoublePointer(short ***data)
143 {
144 	if (!*data) {
145 		printf("WARNING: *data == NULL\n");
146 		return 0;
147 	}
148 	if (!**data) {
149 		printf("WARNING: **data == NULL\n");
150 		return 0;
151 	}
152 	printf("Incoming double pointer: %d\n", ***data);
153 	return ***data;
154 }
155 
main(int argc,char ** argv)156 void main(int argc, char **argv)
157 {
158 	RPC_STATUS status;
159 	RPC_BINDING_VECTOR *pBindingVector;
160 
161 	if (argc != 1) {
162 		printf("Usage: rpcechosrv\n");
163 		exit(0);
164 	}
165 
166 	status = RpcServerUseProtseqEp("ncacn_np", RPC_MAX_CALLS, "\\pipe\\rpcecho", NULL);
167 	if (status) {
168 		printf("Failed to register ncacn_np endpoint\n");
169 		exit(status);
170 	}
171 
172 	status = RpcServerUseProtseqEp("ncacn_ip_tcp", RPC_MAX_CALLS, "1234", NULL);
173 	if (status) {
174 		printf("Failed to register ncacn_ip_tcp endpoint\n");
175 		exit(status);
176 	}
177 
178 	status = RpcServerInqBindings(&pBindingVector);
179 	if (status) {
180 		printf("Failed RpcServerInqBindings\n");
181 		exit(status);
182 	}
183 
184 	status = RpcEpRegister(rpcecho_v1_0_s_ifspec, pBindingVector, NULL, "rpcecho server");
185 	if (status) {
186 		printf("Failed RpcEpRegister\n");
187 		exit(status);
188 	}
189 
190 	status = RpcServerRegisterIf(rpcecho_v1_0_s_ifspec, NULL, NULL);
191 
192 	if (status) {
193 		printf("Failed to register interface\n");
194 		exit(status);
195 	}
196 
197 	status = RpcServerRegisterAuthInfo(NULL, RPC_C_AUTHN_WINNT, NULL, NULL);
198 	if (status) {
199 		printf("Failed to setup auth info\n");
200 	}
201 
202 	status = RpcServerListen(RPC_MIN_CALLS, RPC_MAX_CALLS, FALSE);
203 
204 	if (status) {
205 		printf("RpcServerListen returned error %d\n", status);
206 		exit(status);
207 	}
208 }
209 
210