Sync to Wine-20050830:
[reactos.git] / reactos / lib / rpcrt4 / rpc_epmap.c
1 /*
2 * RPC endpoint mapper
3 *
4 * Copyright 2002 Greg Turner
5 * Copyright 2001 Ove Kåven, TransGaming Technologies
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * TODO:
22 * - actually do things right
23 */
24
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <string.h>
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winerror.h"
32 #include "winreg.h"
33
34 #include "rpc.h"
35
36 #include "wine/debug.h"
37
38 #include "rpc_binding.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(ole);
41
42 /* The "real" RPC portmapper endpoints that I know of are:
43 *
44 * ncadg_ip_udp: 135
45 * ncacn_ip_tcp: 135
46 * ncacn_np: \\pipe\epmapper (?)
47 * ncalrpc: epmapper
48 *
49 * If the user's machine ran a DCE RPC daemon, it would
50 * probably be possible to connect to it, but there are many
51 * reasons not to, like:
52 * - the user probably does *not* run one, and probably
53 * shouldn't be forced to run one just for local COM
54 * - very few Unix systems use DCE RPC... if they run a RPC
55 * daemon at all, it's usually Sun RPC
56 * - DCE RPC registrations are persistent and saved on disk,
57 * while MS-RPC registrations are documented as non-persistent
58 * and stored only in RAM, and auto-destroyed when the process
59 * dies (something DCE RPC can't do)
60 *
61 * Of course, if the user *did* want to run a DCE RPC daemon anyway,
62 * there would be interoperability advantages, like the possibility
63 * of running a fully functional DCOM server using Wine...
64 */
65
66 /***********************************************************************
67 * RpcEpRegisterA (RPCRT4.@)
68 */
69 RPC_STATUS WINAPI RpcEpRegisterA( RPC_IF_HANDLE IfSpec, RPC_BINDING_VECTOR *BindingVector,
70 UUID_VECTOR *UuidVector, unsigned char *Annotation )
71 {
72 RPCSS_NP_MESSAGE msg;
73 RPCSS_NP_REPLY reply;
74 char *vardata_payload, *vp;
75 PRPC_SERVER_INTERFACE If = (PRPC_SERVER_INTERFACE)IfSpec;
76 unsigned long c;
77 RPC_STATUS rslt = RPC_S_OK;
78
79 TRACE("(%p,%p,%p,%s)\n", IfSpec, BindingVector, UuidVector, debugstr_a((char*)Annotation));
80 TRACE(" ifid=%s\n", debugstr_guid(&If->InterfaceId.SyntaxGUID));
81 for (c=0; c<BindingVector->Count; c++) {
82 RpcBinding* bind = (RpcBinding*)(BindingVector->BindingH[c]);
83 TRACE(" protseq[%ld]=%s\n", c, debugstr_a(bind->Protseq));
84 TRACE(" endpoint[%ld]=%s\n", c, debugstr_a(bind->Endpoint));
85 }
86 if (UuidVector) {
87 for (c=0; c<UuidVector->Count; c++)
88 TRACE(" obj[%ld]=%s\n", c, debugstr_guid(UuidVector->Uuid[c]));
89 }
90
91 /* FIXME: Do something with annotation. */
92
93 /* construct the message to rpcss */
94 msg.message_type = RPCSS_NP_MESSAGE_TYPEID_REGISTEREPMSG;
95 msg.message.registerepmsg.iface = If->InterfaceId;
96 msg.message.registerepmsg.no_replace = 0;
97
98 msg.message.registerepmsg.object_count = (UuidVector) ? UuidVector->Count : 0;
99 msg.message.registerepmsg.binding_count = BindingVector->Count;
100
101 /* calculate vardata payload size */
102 msg.vardata_payload_size = msg.message.registerepmsg.object_count * sizeof(UUID);
103 for (c=0; c < msg.message.registerepmsg.binding_count; c++) {
104 RpcBinding *bind = (RpcBinding *)(BindingVector->BindingH[c]);
105 msg.vardata_payload_size += strlen(bind->Protseq) + 1;
106 msg.vardata_payload_size += strlen(bind->Endpoint) + 1;
107 }
108
109 /* allocate the payload buffer */
110 vp = vardata_payload = LocalAlloc(LPTR, msg.vardata_payload_size);
111 if (!vardata_payload)
112 return RPC_S_OUT_OF_MEMORY;
113
114 /* populate the payload data */
115 for (c=0; c < msg.message.registerepmsg.object_count; c++) {
116 CopyMemory(vp, UuidVector->Uuid[c], sizeof(UUID));
117 vp += sizeof(UUID);
118 }
119
120 for (c=0; c < msg.message.registerepmsg.binding_count; c++) {
121 RpcBinding *bind = (RpcBinding*)(BindingVector->BindingH[c]);
122 unsigned long pslen = strlen(bind->Protseq) + 1, eplen = strlen(bind->Endpoint) + 1;
123 CopyMemory(vp, bind->Protseq, pslen);
124 vp += pslen;
125 CopyMemory(vp, bind->Endpoint, eplen);
126 vp += eplen;
127 }
128
129 /* send our request */
130 if (!RPCRT4_RPCSSOnDemandCall(&msg, vardata_payload, &reply))
131 rslt = RPC_S_OUT_OF_MEMORY;
132
133 /* free the payload buffer */
134 LocalFree(vardata_payload);
135
136 return rslt;
137 }
138
139 /***********************************************************************
140 * RpcEpUnregister (RPCRT4.@)
141 */
142 RPC_STATUS WINAPI RpcEpUnregister( RPC_IF_HANDLE IfSpec, RPC_BINDING_VECTOR *BindingVector,
143 UUID_VECTOR *UuidVector )
144 {
145 RPCSS_NP_MESSAGE msg;
146 RPCSS_NP_REPLY reply;
147 char *vardata_payload, *vp;
148 PRPC_SERVER_INTERFACE If = (PRPC_SERVER_INTERFACE)IfSpec;
149 unsigned long c;
150 RPC_STATUS rslt = RPC_S_OK;
151
152 TRACE("(%p,%p,%p)\n", IfSpec, BindingVector, UuidVector);
153 TRACE(" ifid=%s\n", debugstr_guid(&If->InterfaceId.SyntaxGUID));
154 for (c=0; c<BindingVector->Count; c++) {
155 RpcBinding* bind = (RpcBinding*)(BindingVector->BindingH[c]);
156 TRACE(" protseq[%ld]=%s\n", c, debugstr_a(bind->Protseq));
157 TRACE(" endpoint[%ld]=%s\n", c, debugstr_a(bind->Endpoint));
158 }
159 if (UuidVector) {
160 for (c=0; c<UuidVector->Count; c++)
161 TRACE(" obj[%ld]=%s\n", c, debugstr_guid(UuidVector->Uuid[c]));
162 }
163
164 /* construct the message to rpcss */
165 msg.message_type = RPCSS_NP_MESSAGE_TYPEID_UNREGISTEREPMSG;
166 msg.message.unregisterepmsg.iface = If->InterfaceId;
167
168 msg.message.unregisterepmsg.object_count = (UuidVector) ? UuidVector->Count : 0;
169 msg.message.unregisterepmsg.binding_count = BindingVector->Count;
170
171 /* calculate vardata payload size */
172 msg.vardata_payload_size = msg.message.unregisterepmsg.object_count * sizeof(UUID);
173 for (c=0; c < msg.message.unregisterepmsg.binding_count; c++) {
174 RpcBinding *bind = (RpcBinding *)(BindingVector->BindingH[c]);
175 msg.vardata_payload_size += strlen(bind->Protseq) + 1;
176 msg.vardata_payload_size += strlen(bind->Endpoint) + 1;
177 }
178
179 /* allocate the payload buffer */
180 vp = vardata_payload = LocalAlloc(LPTR, msg.vardata_payload_size);
181 if (!vardata_payload)
182 return RPC_S_OUT_OF_MEMORY;
183
184 /* populate the payload data */
185 for (c=0; c < msg.message.unregisterepmsg.object_count; c++) {
186 CopyMemory(vp, UuidVector->Uuid[c], sizeof(UUID));
187 vp += sizeof(UUID);
188 }
189
190 for (c=0; c < msg.message.unregisterepmsg.binding_count; c++) {
191 RpcBinding *bind = (RpcBinding*)(BindingVector->BindingH[c]);
192 unsigned long pslen = strlen(bind->Protseq) + 1, eplen = strlen(bind->Endpoint) + 1;
193 CopyMemory(vp, bind->Protseq, pslen);
194 vp += pslen;
195 CopyMemory(vp, bind->Endpoint, eplen);
196 vp += eplen;
197 }
198
199 /* send our request */
200 if (!RPCRT4_RPCSSOnDemandCall(&msg, vardata_payload, &reply))
201 rslt = RPC_S_OUT_OF_MEMORY;
202
203 /* free the payload buffer */
204 LocalFree(vardata_payload);
205
206 return rslt;
207 }
208
209 /***********************************************************************
210 * RpcEpResolveBinding (RPCRT4.@)
211 */
212 RPC_STATUS WINAPI RpcEpResolveBinding( RPC_BINDING_HANDLE Binding, RPC_IF_HANDLE IfSpec )
213 {
214 RPCSS_NP_MESSAGE msg;
215 RPCSS_NP_REPLY reply;
216 PRPC_CLIENT_INTERFACE If = (PRPC_CLIENT_INTERFACE)IfSpec;
217 RpcBinding* bind = (RpcBinding*)Binding;
218
219 TRACE("(%p,%p)\n", Binding, IfSpec);
220 TRACE(" protseq=%s\n", debugstr_a(bind->Protseq));
221 TRACE(" obj=%s\n", debugstr_guid(&bind->ObjectUuid));
222 TRACE(" ifid=%s\n", debugstr_guid(&If->InterfaceId.SyntaxGUID));
223
224 /* FIXME: totally untested */
225
226 /* just return for fully bound handles */
227 if (bind->Endpoint && (bind->Endpoint[0] != '\0'))
228 return RPC_S_OK;
229
230 /* construct the message to rpcss */
231 msg.message_type = RPCSS_NP_MESSAGE_TYPEID_RESOLVEEPMSG;
232 msg.message.resolveepmsg.iface = If->InterfaceId;
233 msg.message.resolveepmsg.object = bind->ObjectUuid;
234
235 msg.vardata_payload_size = strlen(bind->Protseq) + 1;
236
237 /* send the message */
238 if (!RPCRT4_RPCSSOnDemandCall(&msg, bind->Protseq, &reply))
239 return RPC_S_OUT_OF_MEMORY;
240
241 /* empty-string result means not registered */
242 if (reply.as_string[0] == '\0')
243 return EPT_S_NOT_REGISTERED;
244
245 /* otherwise we fully bind the handle & return RPC_S_OK */
246 return RPCRT4_ResolveBinding(Binding, reply.as_string);
247 }