4 * Copyright 2002 Greg Turner
5 * Copyright 2001 Ove Kåven, TransGaming Technologies
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * - actually do things right
36 #include "wine/debug.h"
38 #include "rpc_binding.h"
39 #include "epm_towers.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(ole);
43 /* The "real" RPC portmapper endpoints that I know of are:
47 * ncacn_np: \\pipe\epmapper (?)
50 * If the user's machine ran a DCE RPC daemon, it would
51 * probably be possible to connect to it, but there are many
52 * reasons not to, like:
53 * - the user probably does *not* run one, and probably
54 * shouldn't be forced to run one just for local COM
55 * - very few Unix systems use DCE RPC... if they run a RPC
56 * daemon at all, it's usually Sun RPC
57 * - DCE RPC registrations are persistent and saved on disk,
58 * while MS-RPC registrations are documented as non-persistent
59 * and stored only in RAM, and auto-destroyed when the process
60 * dies (something DCE RPC can't do)
62 * Of course, if the user *did* want to run a DCE RPC daemon anyway,
63 * there would be interoperability advantages, like the possibility
64 * of running a fully functional DCOM server using Wine...
67 /***********************************************************************
68 * RpcEpRegisterA (RPCRT4.@)
70 RPC_STATUS WINAPI RpcEpRegisterA( RPC_IF_HANDLE IfSpec, RPC_BINDING_VECTOR *BindingVector,
71 UUID_VECTOR *UuidVector, RPC_CSTR Annotation )
75 char *vardata_payload, *vp;
76 PRPC_SERVER_INTERFACE If = (PRPC_SERVER_INTERFACE)IfSpec;
78 RPC_STATUS rslt = RPC_S_OK;
80 TRACE("(%p,%p,%p,%s)\n", IfSpec, BindingVector, UuidVector, debugstr_a((char*)Annotation));
81 TRACE(" ifid=%s\n", debugstr_guid(&If->InterfaceId.SyntaxGUID));
82 for (c=0; c<BindingVector->Count; c++) {
83 RpcBinding* bind = (RpcBinding*)(BindingVector->BindingH[c]);
84 TRACE(" protseq[%ld]=%s\n", c, debugstr_a(bind->Protseq));
85 TRACE(" endpoint[%ld]=%s\n", c, debugstr_a(bind->Endpoint));
88 for (c=0; c<UuidVector->Count; c++)
89 TRACE(" obj[%ld]=%s\n", c, debugstr_guid(UuidVector->Uuid[c]));
92 /* FIXME: Do something with annotation. */
94 /* construct the message to rpcss */
95 msg.message_type = RPCSS_NP_MESSAGE_TYPEID_REGISTEREPMSG;
96 msg.message.registerepmsg.iface = If->InterfaceId;
97 msg.message.registerepmsg.no_replace = 0;
99 msg.message.registerepmsg.object_count = (UuidVector) ? UuidVector->Count : 0;
100 msg.message.registerepmsg.binding_count = BindingVector->Count;
102 /* calculate vardata payload size */
103 msg.vardata_payload_size = msg.message.registerepmsg.object_count * sizeof(UUID);
104 for (c=0; c < msg.message.registerepmsg.binding_count; c++) {
105 RpcBinding *bind = (RpcBinding *)(BindingVector->BindingH[c]);
106 msg.vardata_payload_size += strlen(bind->Protseq) + 1;
107 msg.vardata_payload_size += strlen(bind->Endpoint) + 1;
110 /* allocate the payload buffer */
111 vp = vardata_payload = LocalAlloc(LPTR, msg.vardata_payload_size);
112 if (!vardata_payload)
113 return RPC_S_OUT_OF_MEMORY;
115 /* populate the payload data */
116 for (c=0; c < msg.message.registerepmsg.object_count; c++) {
117 CopyMemory(vp, UuidVector->Uuid[c], sizeof(UUID));
121 for (c=0; c < msg.message.registerepmsg.binding_count; c++) {
122 RpcBinding *bind = (RpcBinding*)(BindingVector->BindingH[c]);
123 unsigned long pslen = strlen(bind->Protseq) + 1, eplen = strlen(bind->Endpoint) + 1;
124 CopyMemory(vp, bind->Protseq, pslen);
126 CopyMemory(vp, bind->Endpoint, eplen);
130 /* send our request */
131 if (!RPCRT4_RPCSSOnDemandCall(&msg, vardata_payload, &reply))
132 rslt = RPC_S_OUT_OF_MEMORY;
134 /* free the payload buffer */
135 LocalFree(vardata_payload);
140 /***********************************************************************
141 * RpcEpUnregister (RPCRT4.@)
143 RPC_STATUS WINAPI RpcEpUnregister( RPC_IF_HANDLE IfSpec, RPC_BINDING_VECTOR *BindingVector,
144 UUID_VECTOR *UuidVector )
146 RPCSS_NP_MESSAGE msg;
147 RPCSS_NP_REPLY reply;
148 char *vardata_payload, *vp;
149 PRPC_SERVER_INTERFACE If = (PRPC_SERVER_INTERFACE)IfSpec;
151 RPC_STATUS rslt = RPC_S_OK;
153 TRACE("(%p,%p,%p)\n", IfSpec, BindingVector, UuidVector);
154 TRACE(" ifid=%s\n", debugstr_guid(&If->InterfaceId.SyntaxGUID));
155 for (c=0; c<BindingVector->Count; c++) {
156 RpcBinding* bind = (RpcBinding*)(BindingVector->BindingH[c]);
157 TRACE(" protseq[%ld]=%s\n", c, debugstr_a(bind->Protseq));
158 TRACE(" endpoint[%ld]=%s\n", c, debugstr_a(bind->Endpoint));
161 for (c=0; c<UuidVector->Count; c++)
162 TRACE(" obj[%ld]=%s\n", c, debugstr_guid(UuidVector->Uuid[c]));
165 /* construct the message to rpcss */
166 msg.message_type = RPCSS_NP_MESSAGE_TYPEID_UNREGISTEREPMSG;
167 msg.message.unregisterepmsg.iface = If->InterfaceId;
169 msg.message.unregisterepmsg.object_count = (UuidVector) ? UuidVector->Count : 0;
170 msg.message.unregisterepmsg.binding_count = BindingVector->Count;
172 /* calculate vardata payload size */
173 msg.vardata_payload_size = msg.message.unregisterepmsg.object_count * sizeof(UUID);
174 for (c=0; c < msg.message.unregisterepmsg.binding_count; c++) {
175 RpcBinding *bind = (RpcBinding *)(BindingVector->BindingH[c]);
176 msg.vardata_payload_size += strlen(bind->Protseq) + 1;
177 msg.vardata_payload_size += strlen(bind->Endpoint) + 1;
180 /* allocate the payload buffer */
181 vp = vardata_payload = LocalAlloc(LPTR, msg.vardata_payload_size);
182 if (!vardata_payload)
183 return RPC_S_OUT_OF_MEMORY;
185 /* populate the payload data */
186 for (c=0; c < msg.message.unregisterepmsg.object_count; c++) {
187 CopyMemory(vp, UuidVector->Uuid[c], sizeof(UUID));
191 for (c=0; c < msg.message.unregisterepmsg.binding_count; c++) {
192 RpcBinding *bind = (RpcBinding*)(BindingVector->BindingH[c]);
193 unsigned long pslen = strlen(bind->Protseq) + 1, eplen = strlen(bind->Endpoint) + 1;
194 CopyMemory(vp, bind->Protseq, pslen);
196 CopyMemory(vp, bind->Endpoint, eplen);
200 /* send our request */
201 if (!RPCRT4_RPCSSOnDemandCall(&msg, vardata_payload, &reply))
202 rslt = RPC_S_OUT_OF_MEMORY;
204 /* free the payload buffer */
205 LocalFree(vardata_payload);
210 /***********************************************************************
211 * RpcEpResolveBinding (RPCRT4.@)
213 RPC_STATUS WINAPI RpcEpResolveBinding( RPC_BINDING_HANDLE Binding, RPC_IF_HANDLE IfSpec )
215 RPCSS_NP_MESSAGE msg;
216 RPCSS_NP_REPLY reply;
217 PRPC_CLIENT_INTERFACE If = (PRPC_CLIENT_INTERFACE)IfSpec;
218 RpcBinding* bind = (RpcBinding*)Binding;
220 TRACE("(%p,%p)\n", Binding, IfSpec);
221 TRACE(" protseq=%s\n", debugstr_a(bind->Protseq));
222 TRACE(" obj=%s\n", debugstr_guid(&bind->ObjectUuid));
223 TRACE(" ifid=%s\n", debugstr_guid(&If->InterfaceId.SyntaxGUID));
225 /* FIXME: totally untested */
227 /* just return for fully bound handles */
228 if (bind->Endpoint && (bind->Endpoint[0] != '\0'))
231 /* construct the message to rpcss */
232 msg.message_type = RPCSS_NP_MESSAGE_TYPEID_RESOLVEEPMSG;
233 msg.message.resolveepmsg.iface = If->InterfaceId;
234 msg.message.resolveepmsg.object = bind->ObjectUuid;
236 msg.vardata_payload_size = strlen(bind->Protseq) + 1;
238 /* send the message */
239 if (!RPCRT4_RPCSSOnDemandCall(&msg, bind->Protseq, &reply))
240 return RPC_S_OUT_OF_MEMORY;
242 /* empty-string result means not registered */
243 if (reply.as_string[0] == '\0')
244 return EPT_S_NOT_REGISTERED;
246 /* otherwise we fully bind the handle & return RPC_S_OK */
247 return RPCRT4_ResolveBinding(Binding, reply.as_string);
250 typedef unsigned int unsigned32;
253 unsigned32 tower_length;
254 /* [size_is] */ BYTE tower_octet_string[ 1 ];
257 /***********************************************************************
258 * TowerExplode (RPCRT4.@)
260 RPC_STATUS WINAPI TowerExplode(
261 const twr_t *tower, PRPC_SYNTAX_IDENTIFIER object, PRPC_SYNTAX_IDENTIFIER syntax,
262 char **protseq, char **endpoint, char **address)
266 const unsigned char *p;
268 const twr_uuid_floor_t *object_floor;
269 const twr_uuid_floor_t *syntax_floor;
278 tower_size = tower->tower_length;
280 if (tower_size < sizeof(u_int16))
281 return EPT_S_NOT_REGISTERED;
283 p = &tower->tower_octet_string[0];
285 floor_count = *(const u_int16 *)p;
286 p += sizeof(u_int16);
287 tower_size -= sizeof(u_int16);
288 TRACE("floor_count: %d\n", floor_count);
289 /* FIXME: should we do something with the floor count? at the moment we don't */
291 if (tower_size < sizeof(*object_floor) + sizeof(*syntax_floor))
292 return EPT_S_NOT_REGISTERED;
294 object_floor = (const twr_uuid_floor_t *)p;
295 p += sizeof(*object_floor);
296 tower_size -= sizeof(*object_floor);
297 syntax_floor = (const twr_uuid_floor_t *)p;
298 p += sizeof(*syntax_floor);
299 tower_size -= sizeof(*syntax_floor);
301 if ((object_floor->count_lhs != sizeof(object_floor->protid) +
302 sizeof(object_floor->uuid) + sizeof(object_floor->major_version)) ||
303 (object_floor->protid != EPM_PROTOCOL_UUID) ||
304 (object_floor->count_rhs != sizeof(object_floor->minor_version)))
305 return EPT_S_NOT_REGISTERED;
307 if ((syntax_floor->count_lhs != sizeof(syntax_floor->protid) +
308 sizeof(syntax_floor->uuid) + sizeof(syntax_floor->major_version)) ||
309 (syntax_floor->protid != EPM_PROTOCOL_UUID) ||
310 (syntax_floor->count_rhs != sizeof(syntax_floor->minor_version)))
311 return EPT_S_NOT_REGISTERED;
313 status = RpcTransport_ParseTopOfTower(p, tower_size, protseq, address, endpoint);
314 if ((status == RPC_S_OK) && syntax && object)
316 syntax->SyntaxGUID = syntax_floor->uuid;
317 syntax->SyntaxVersion.MajorVersion = syntax_floor->major_version;
318 syntax->SyntaxVersion.MinorVersion = syntax_floor->minor_version;
319 object->SyntaxGUID = object_floor->uuid;
320 object->SyntaxVersion.MajorVersion = object_floor->major_version;
321 object->SyntaxVersion.MinorVersion = object_floor->minor_version;
326 /***********************************************************************
327 * TowerConstruct (RPCRT4.@)
329 RPC_STATUS WINAPI TowerConstruct(
330 const RPC_SYNTAX_IDENTIFIER *object, const RPC_SYNTAX_IDENTIFIER *syntax,
331 const char *protseq, const char *endpoint, const char *address,
337 twr_uuid_floor_t *object_floor;
338 twr_uuid_floor_t *syntax_floor;
342 status = RpcTransport_GetTopOfTower(NULL, &tower_size, protseq, address, endpoint);
344 if (status != RPC_S_OK)
347 tower_size += sizeof(u_int16) + sizeof(*object_floor) + sizeof(*syntax_floor);
348 *tower = I_RpcAllocate(FIELD_OFFSET(twr_t, tower_octet_string[tower_size]));
350 return RPC_S_OUT_OF_RESOURCES;
352 (*tower)->tower_length = tower_size;
353 p = &(*tower)->tower_octet_string[0];
354 *(u_int16 *)p = 5; /* number of floors */
355 p += sizeof(u_int16);
356 object_floor = (twr_uuid_floor_t *)p;
357 p += sizeof(*object_floor);
358 syntax_floor = (twr_uuid_floor_t *)p;
359 p += sizeof(*syntax_floor);
361 object_floor->count_lhs = sizeof(object_floor->protid) + sizeof(object_floor->uuid) +
362 sizeof(object_floor->major_version);
363 object_floor->protid = EPM_PROTOCOL_UUID;
364 object_floor->count_rhs = sizeof(object_floor->minor_version);
365 object_floor->uuid = object->SyntaxGUID;
366 object_floor->major_version = object->SyntaxVersion.MajorVersion;
367 object_floor->minor_version = object->SyntaxVersion.MinorVersion;
369 syntax_floor->count_lhs = sizeof(syntax_floor->protid) + sizeof(syntax_floor->uuid) +
370 sizeof(syntax_floor->major_version);
371 syntax_floor->protid = EPM_PROTOCOL_UUID;
372 syntax_floor->count_rhs = sizeof(syntax_floor->minor_version);
373 syntax_floor->uuid = syntax->SyntaxGUID;
374 syntax_floor->major_version = syntax->SyntaxVersion.MajorVersion;
375 syntax_floor->minor_version = syntax->SyntaxVersion.MinorVersion;
377 status = RpcTransport_GetTopOfTower(p, &tower_size, protseq, address, endpoint);
378 if (status != RPC_S_OK)