Merge branch 'master' of git+ssh://repo.or.cz/srv/git/wine/winequartzdrv
[wine/winequartzdrv.git] / dlls / rpcrt4 / rpc_epmap.c
blob46244ec36e0d865e1f1b579d3c193375cca22b02
1 /*
2 * RPC endpoint mapper
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
21 * TODO:
22 * - actually do things right
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <string.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winerror.h"
33 #include "rpc.h"
35 #include "wine/debug.h"
37 #include "rpc_binding.h"
38 #include "epm_towers.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(ole);
42 /* The "real" RPC portmapper endpoints that I know of are:
44 * ncadg_ip_udp: 135
45 * ncacn_ip_tcp: 135
46 * ncacn_np: \\pipe\epmapper (?)
47 * ncalrpc: epmapper
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)
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...
66 /***********************************************************************
67 * RpcEpRegisterA (RPCRT4.@)
69 RPC_STATUS WINAPI RpcEpRegisterA( RPC_IF_HANDLE IfSpec, RPC_BINDING_VECTOR *BindingVector,
70 UUID_VECTOR *UuidVector, RPC_CSTR Annotation )
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;
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));
86 if (UuidVector) {
87 for (c=0; c<UuidVector->Count; c++)
88 TRACE(" obj[%ld]=%s\n", c, debugstr_guid(UuidVector->Uuid[c]));
91 /* FIXME: Do something with annotation. */
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;
98 msg.message.registerepmsg.object_count = (UuidVector) ? UuidVector->Count : 0;
99 msg.message.registerepmsg.binding_count = BindingVector->Count;
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;
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;
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);
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;
129 /* send our request */
130 if (!RPCRT4_RPCSSOnDemandCall(&msg, vardata_payload, &reply))
131 rslt = RPC_S_OUT_OF_MEMORY;
133 /* free the payload buffer */
134 LocalFree(vardata_payload);
136 return rslt;
139 /***********************************************************************
140 * RpcEpUnregister (RPCRT4.@)
142 RPC_STATUS WINAPI RpcEpUnregister( RPC_IF_HANDLE IfSpec, RPC_BINDING_VECTOR *BindingVector,
143 UUID_VECTOR *UuidVector )
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;
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));
159 if (UuidVector) {
160 for (c=0; c<UuidVector->Count; c++)
161 TRACE(" obj[%ld]=%s\n", c, debugstr_guid(UuidVector->Uuid[c]));
164 /* construct the message to rpcss */
165 msg.message_type = RPCSS_NP_MESSAGE_TYPEID_UNREGISTEREPMSG;
166 msg.message.unregisterepmsg.iface = If->InterfaceId;
168 msg.message.unregisterepmsg.object_count = (UuidVector) ? UuidVector->Count : 0;
169 msg.message.unregisterepmsg.binding_count = BindingVector->Count;
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;
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;
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);
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;
199 /* send our request */
200 if (!RPCRT4_RPCSSOnDemandCall(&msg, vardata_payload, &reply))
201 rslt = RPC_S_OUT_OF_MEMORY;
203 /* free the payload buffer */
204 LocalFree(vardata_payload);
206 return rslt;
209 /***********************************************************************
210 * RpcEpResolveBinding (RPCRT4.@)
212 RPC_STATUS WINAPI RpcEpResolveBinding( RPC_BINDING_HANDLE Binding, RPC_IF_HANDLE IfSpec )
214 RPCSS_NP_MESSAGE msg;
215 RPCSS_NP_REPLY reply;
216 PRPC_CLIENT_INTERFACE If = (PRPC_CLIENT_INTERFACE)IfSpec;
217 RpcBinding* bind = (RpcBinding*)Binding;
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));
224 /* FIXME: totally untested */
226 /* just return for fully bound handles */
227 if (bind->Endpoint && (bind->Endpoint[0] != '\0'))
228 return RPC_S_OK;
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;
235 msg.vardata_payload_size = strlen(bind->Protseq) + 1;
237 /* send the message */
238 if (!RPCRT4_RPCSSOnDemandCall(&msg, bind->Protseq, &reply))
239 return RPC_S_OUT_OF_MEMORY;
241 /* empty-string result means not registered */
242 if (reply.as_string[0] == '\0')
243 return EPT_S_NOT_REGISTERED;
245 /* otherwise we fully bind the handle & return RPC_S_OK */
246 return RPCRT4_ResolveBinding(Binding, reply.as_string);
249 typedef unsigned int unsigned32;
250 typedef struct twr_t
252 unsigned32 tower_length;
253 /* [size_is] */ BYTE tower_octet_string[ 1 ];
254 } twr_t;
256 /***********************************************************************
257 * TowerExplode (RPCRT4.@)
259 RPC_STATUS WINAPI TowerExplode(
260 const twr_t *tower, PRPC_SYNTAX_IDENTIFIER object, PRPC_SYNTAX_IDENTIFIER syntax,
261 char **protseq, char **endpoint, char **address)
263 size_t tower_size;
264 RPC_STATUS status;
265 const unsigned char *p;
266 u_int16 floor_count;
267 const twr_uuid_floor_t *object_floor;
268 const twr_uuid_floor_t *syntax_floor;
270 TRACE("(%p, %p, %p, %p, %p, %p)\n", tower, object, syntax, protseq,
271 endpoint, address);
273 if (protseq)
274 *protseq = NULL;
275 if (endpoint)
276 *endpoint = NULL;
277 if (address)
278 *address = NULL;
280 tower_size = tower->tower_length;
282 if (tower_size < sizeof(u_int16))
283 return EPT_S_NOT_REGISTERED;
285 p = &tower->tower_octet_string[0];
287 floor_count = *(const u_int16 *)p;
288 p += sizeof(u_int16);
289 tower_size -= sizeof(u_int16);
290 TRACE("floor_count: %d\n", floor_count);
291 /* FIXME: should we do something with the floor count? at the moment we don't */
293 if (tower_size < sizeof(*object_floor) + sizeof(*syntax_floor))
294 return EPT_S_NOT_REGISTERED;
296 object_floor = (const twr_uuid_floor_t *)p;
297 p += sizeof(*object_floor);
298 tower_size -= sizeof(*object_floor);
299 syntax_floor = (const twr_uuid_floor_t *)p;
300 p += sizeof(*syntax_floor);
301 tower_size -= sizeof(*syntax_floor);
303 if ((object_floor->count_lhs != sizeof(object_floor->protid) +
304 sizeof(object_floor->uuid) + sizeof(object_floor->major_version)) ||
305 (object_floor->protid != EPM_PROTOCOL_UUID) ||
306 (object_floor->count_rhs != sizeof(object_floor->minor_version)))
307 return EPT_S_NOT_REGISTERED;
309 if ((syntax_floor->count_lhs != sizeof(syntax_floor->protid) +
310 sizeof(syntax_floor->uuid) + sizeof(syntax_floor->major_version)) ||
311 (syntax_floor->protid != EPM_PROTOCOL_UUID) ||
312 (syntax_floor->count_rhs != sizeof(syntax_floor->minor_version)))
313 return EPT_S_NOT_REGISTERED;
315 status = RpcTransport_ParseTopOfTower(p, tower_size, protseq, address, endpoint);
316 if ((status == RPC_S_OK) && syntax && object)
318 syntax->SyntaxGUID = syntax_floor->uuid;
319 syntax->SyntaxVersion.MajorVersion = syntax_floor->major_version;
320 syntax->SyntaxVersion.MinorVersion = syntax_floor->minor_version;
321 object->SyntaxGUID = object_floor->uuid;
322 object->SyntaxVersion.MajorVersion = object_floor->major_version;
323 object->SyntaxVersion.MinorVersion = object_floor->minor_version;
325 return status;
328 /***********************************************************************
329 * TowerConstruct (RPCRT4.@)
331 RPC_STATUS WINAPI TowerConstruct(
332 const RPC_SYNTAX_IDENTIFIER *object, const RPC_SYNTAX_IDENTIFIER *syntax,
333 const char *protseq, const char *endpoint, const char *address,
334 twr_t **tower)
336 size_t tower_size;
337 RPC_STATUS status;
338 unsigned char *p;
339 twr_uuid_floor_t *object_floor;
340 twr_uuid_floor_t *syntax_floor;
342 TRACE("(%p, %p, %s, %s, %s, %p)\n", object, syntax, debugstr_a(protseq),
343 debugstr_a(endpoint), debugstr_a(address), tower);
345 *tower = NULL;
347 status = RpcTransport_GetTopOfTower(NULL, &tower_size, protseq, address, endpoint);
349 if (status != RPC_S_OK)
350 return status;
352 tower_size += sizeof(u_int16) + sizeof(*object_floor) + sizeof(*syntax_floor);
353 *tower = I_RpcAllocate(FIELD_OFFSET(twr_t, tower_octet_string[tower_size]));
354 if (!*tower)
355 return RPC_S_OUT_OF_RESOURCES;
357 (*tower)->tower_length = tower_size;
358 p = &(*tower)->tower_octet_string[0];
359 *(u_int16 *)p = 5; /* number of floors */
360 p += sizeof(u_int16);
361 object_floor = (twr_uuid_floor_t *)p;
362 p += sizeof(*object_floor);
363 syntax_floor = (twr_uuid_floor_t *)p;
364 p += sizeof(*syntax_floor);
366 object_floor->count_lhs = sizeof(object_floor->protid) + sizeof(object_floor->uuid) +
367 sizeof(object_floor->major_version);
368 object_floor->protid = EPM_PROTOCOL_UUID;
369 object_floor->count_rhs = sizeof(object_floor->minor_version);
370 object_floor->uuid = object->SyntaxGUID;
371 object_floor->major_version = object->SyntaxVersion.MajorVersion;
372 object_floor->minor_version = object->SyntaxVersion.MinorVersion;
374 syntax_floor->count_lhs = sizeof(syntax_floor->protid) + sizeof(syntax_floor->uuid) +
375 sizeof(syntax_floor->major_version);
376 syntax_floor->protid = EPM_PROTOCOL_UUID;
377 syntax_floor->count_rhs = sizeof(syntax_floor->minor_version);
378 syntax_floor->uuid = syntax->SyntaxGUID;
379 syntax_floor->major_version = syntax->SyntaxVersion.MajorVersion;
380 syntax_floor->minor_version = syntax->SyntaxVersion.MinorVersion;
382 status = RpcTransport_GetTopOfTower(p, &tower_size, protseq, address, endpoint);
383 if (status != RPC_S_OK)
385 I_RpcFree(*tower);
386 *tower = NULL;
387 return status;
389 return RPC_S_OK;