4 * Copyright 2001 Ove Kåven, TransGaming Technologies
5 * Copyright 2003 Mike Hearn
6 * Copyright 2004 Filip Navara
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
37 #include "wine/unicode.h"
42 #include "wine/debug.h"
44 #include "rpc_binding.h"
45 #include "rpc_message.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(ole);
49 static RpcConnection* conn_cache;
51 static CRITICAL_SECTION conn_cache_cs;
52 static CRITICAL_SECTION_DEBUG critsect_debug =
55 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
56 0, 0, { 0, (DWORD)(__FILE__ ": conn_cache_cs") }
58 static CRITICAL_SECTION conn_cache_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
60 LPSTR RPCRT4_strndupA(LPCSTR src, INT slen)
64 if (!src) return NULL;
65 if (slen == -1) slen = strlen(src);
67 s = HeapAlloc(GetProcessHeap(), 0, len+1);
73 LPSTR RPCRT4_strdupWtoA(LPWSTR src)
77 if (!src) return NULL;
78 len = WideCharToMultiByte(CP_ACP, 0, src, -1, NULL, 0, NULL, NULL);
79 s = HeapAlloc(GetProcessHeap(), 0, len);
80 WideCharToMultiByte(CP_ACP, 0, src, -1, s, len, NULL, NULL);
84 LPWSTR RPCRT4_strdupAtoW(LPSTR src)
88 if (!src) return NULL;
89 len = MultiByteToWideChar(CP_ACP, 0, src, -1, NULL, 0);
90 s = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
91 MultiByteToWideChar(CP_ACP, 0, src, -1, s, len);
95 LPWSTR RPCRT4_strndupW(LPWSTR src, INT slen)
99 if (!src) return NULL;
100 if (slen == -1) slen = strlenW(src);
102 s = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
103 memcpy(s, src, len*sizeof(WCHAR));
108 void RPCRT4_strfree(LPSTR src)
110 if (src) HeapFree(GetProcessHeap(), 0, src);
113 RPC_STATUS RPCRT4_CreateConnection(RpcConnection** Connection, BOOL server, LPSTR Protseq, LPSTR NetworkAddr, LPSTR Endpoint, LPSTR NetworkOptions, RpcBinding* Binding)
115 RpcConnection* NewConnection;
117 NewConnection = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcConnection));
118 NewConnection->server = server;
119 NewConnection->Protseq = RPCRT4_strdupA(Protseq);
120 NewConnection->NetworkAddr = RPCRT4_strdupA(NetworkAddr);
121 NewConnection->Endpoint = RPCRT4_strdupA(Endpoint);
122 NewConnection->Used = Binding;
123 NewConnection->MaxTransmissionSize = RPC_MAX_PACKET_SIZE;
125 EnterCriticalSection(&conn_cache_cs);
126 NewConnection->Next = conn_cache;
127 conn_cache = NewConnection;
128 LeaveCriticalSection(&conn_cache_cs);
130 TRACE("connection: %p\n", NewConnection);
131 *Connection = NewConnection;
136 RPC_STATUS RPCRT4_DestroyConnection(RpcConnection* Connection)
138 RpcConnection* PrevConnection;
140 TRACE("connection: %p\n", Connection);
141 if (Connection->Used) ERR("connection is still in use\n");
143 EnterCriticalSection(&conn_cache_cs);
144 PrevConnection = conn_cache;
145 if (PrevConnection == Connection) {
146 conn_cache = Connection->Next;
148 while (PrevConnection && PrevConnection->Next != Connection)
149 PrevConnection = PrevConnection->Next;
150 if (PrevConnection) PrevConnection->Next = Connection->Next;
152 LeaveCriticalSection(&conn_cache_cs);
154 RPCRT4_CloseConnection(Connection);
155 RPCRT4_strfree(Connection->Endpoint);
156 RPCRT4_strfree(Connection->NetworkAddr);
157 RPCRT4_strfree(Connection->Protseq);
158 HeapFree(GetProcessHeap(), 0, Connection);
162 RPC_STATUS RPCRT4_GetConnection(RpcConnection** Connection, BOOL server, LPSTR Protseq, LPSTR NetworkAddr, LPSTR Endpoint, LPSTR NetworkOptions, RpcBinding* Binding)
164 RpcConnection* NewConnection;
167 EnterCriticalSection(&conn_cache_cs);
168 for (NewConnection = conn_cache; NewConnection; NewConnection = NewConnection->Next) {
169 if (NewConnection->Used) continue;
170 if (NewConnection->server != server) continue;
171 if (Protseq && strcmp(NewConnection->Protseq, Protseq)) continue;
172 if (NetworkAddr && strcmp(NewConnection->NetworkAddr, NetworkAddr)) continue;
173 if (Endpoint && strcmp(NewConnection->Endpoint, Endpoint)) continue;
174 /* this connection fits the bill */
175 NewConnection->Used = Binding;
178 LeaveCriticalSection(&conn_cache_cs);
180 TRACE("cached connection: %p\n", NewConnection);
181 *Connection = NewConnection;
185 return RPCRT4_CreateConnection(Connection, server, Protseq, NetworkAddr, Endpoint, NetworkOptions, Binding);
188 RPC_STATUS RPCRT4_ReleaseConnection(RpcConnection* Connection)
190 TRACE("connection: %p\n", Connection);
191 Connection->Used = NULL;
192 if (!Connection->server) {
193 /* cache the open connection for reuse later */
194 /* FIXME: we should probably clean the cache someday */
197 return RPCRT4_DestroyConnection(Connection);
200 RPC_STATUS RPCRT4_OpenConnection(RpcConnection* Connection)
202 TRACE("(Connection == ^%p)\n", Connection);
203 if (!Connection->conn) {
204 if (Connection->server) { /* server */
205 /* protseq=ncalrpc: supposed to use NT LPC ports,
206 * but we'll implement it with named pipes for now */
207 if (strcmp(Connection->Protseq, "ncalrpc") == 0) {
208 static LPCSTR prefix = "\\\\.\\pipe\\lrpc\\";
210 pname = HeapAlloc(GetProcessHeap(), 0, strlen(prefix) + strlen(Connection->Endpoint) + 1);
211 strcat(strcpy(pname, prefix), Connection->Endpoint);
212 TRACE("listening on %s\n", pname);
213 Connection->conn = CreateNamedPipeA(pname, PROFILE_SERVER | PIPE_ACCESS_DUPLEX,
214 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE, PIPE_UNLIMITED_INSTANCES,
215 RPC_MAX_PACKET_SIZE, RPC_MAX_PACKET_SIZE, 5000, NULL);
216 HeapFree(GetProcessHeap(), 0, pname);
217 memset(&Connection->ovl, 0, sizeof(Connection->ovl));
218 Connection->ovl.hEvent = CreateEventA(NULL, TRUE, FALSE, NULL);
219 if (!ConnectNamedPipe(Connection->conn, &Connection->ovl)) {
220 WARN("Couldn't ConnectNamedPipe (error was %ld)\n", GetLastError());
221 if (GetLastError() == ERROR_PIPE_CONNECTED) {
222 SetEvent(Connection->ovl.hEvent);
224 } else if (GetLastError() == ERROR_IO_PENDING) {
227 return RPC_S_SERVER_UNAVAILABLE;
230 /* protseq=ncacn_np: named pipes */
231 else if (strcmp(Connection->Protseq, "ncacn_np") == 0) {
232 static LPCSTR prefix = "\\\\.";
234 pname = HeapAlloc(GetProcessHeap(), 0, strlen(prefix) + strlen(Connection->Endpoint) + 1);
235 strcat(strcpy(pname, prefix), Connection->Endpoint);
236 TRACE("listening on %s\n", pname);
237 Connection->conn = CreateNamedPipeA(pname, PROFILE_SERVER | PIPE_ACCESS_DUPLEX,
238 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES,
239 RPC_MAX_PACKET_SIZE, RPC_MAX_PACKET_SIZE, 5000, NULL);
240 HeapFree(GetProcessHeap(), 0, pname);
241 memset(&Connection->ovl, 0, sizeof(Connection->ovl));
242 Connection->ovl.hEvent = CreateEventA(NULL, TRUE, FALSE, NULL);
243 if (!ConnectNamedPipe(Connection->conn, &Connection->ovl)) {
244 WARN("Couldn't ConnectNamedPipe (error was %ld)\n", GetLastError());
245 if (GetLastError() == ERROR_PIPE_CONNECTED) {
246 SetEvent(Connection->ovl.hEvent);
249 return RPC_S_SERVER_UNAVAILABLE;
253 ERR("protseq %s not supported\n", Connection->Protseq);
254 return RPC_S_PROTSEQ_NOT_SUPPORTED;
258 /* protseq=ncalrpc: supposed to use NT LPC ports,
259 * but we'll implement it with named pipes for now */
260 if (strcmp(Connection->Protseq, "ncalrpc") == 0) {
261 static LPCSTR prefix = "\\\\.\\pipe\\lrpc\\";
267 pname = HeapAlloc(GetProcessHeap(), 0, strlen(prefix) + strlen(Connection->Endpoint) + 1);
268 strcat(strcpy(pname, prefix), Connection->Endpoint);
269 TRACE("connecting to %s\n", pname);
271 if (WaitNamedPipeA(pname, NMPWAIT_WAIT_FOREVER)) {
272 conn = CreateFileA(pname, GENERIC_READ|GENERIC_WRITE, 0, NULL,
273 OPEN_EXISTING, 0, 0);
274 if (conn != INVALID_HANDLE_VALUE) break;
275 err = GetLastError();
276 if (err == ERROR_PIPE_BUSY) continue;
277 TRACE("connection failed, error=%lx\n", err);
278 HeapFree(GetProcessHeap(), 0, pname);
279 return RPC_S_SERVER_TOO_BUSY;
281 err = GetLastError();
282 TRACE("connection failed, error=%lx\n", err);
283 HeapFree(GetProcessHeap(), 0, pname);
284 return RPC_S_SERVER_UNAVAILABLE;
289 HeapFree(GetProcessHeap(), 0, pname);
290 memset(&Connection->ovl, 0, sizeof(Connection->ovl));
291 /* pipe is connected; change to message-read mode. */
292 dwMode = PIPE_READMODE_MESSAGE;
293 SetNamedPipeHandleState(conn, &dwMode, NULL, NULL);
294 Connection->ovl.hEvent = CreateEventA(NULL, TRUE, FALSE, NULL);
295 Connection->conn = conn;
297 /* protseq=ncacn_np: named pipes */
298 else if (strcmp(Connection->Protseq, "ncacn_np") == 0) {
299 static LPCSTR prefix = "\\\\.";
305 pname = HeapAlloc(GetProcessHeap(), 0, strlen(prefix) + strlen(Connection->Endpoint) + 1);
306 strcat(strcpy(pname, prefix), Connection->Endpoint);
307 TRACE("connecting to %s\n", pname);
308 conn = CreateFileA(pname, GENERIC_READ|GENERIC_WRITE, 0, NULL,
309 OPEN_EXISTING, 0, 0);
310 if (conn == INVALID_HANDLE_VALUE) {
311 err = GetLastError();
312 /* we don't need to handle ERROR_PIPE_BUSY here,
313 * the doc says that it is returned to the app */
314 TRACE("connection failed, error=%lx\n", err);
315 HeapFree(GetProcessHeap(), 0, pname);
316 if (err == ERROR_PIPE_BUSY)
317 return RPC_S_SERVER_TOO_BUSY;
319 return RPC_S_SERVER_UNAVAILABLE;
323 HeapFree(GetProcessHeap(), 0, pname);
324 memset(&Connection->ovl, 0, sizeof(Connection->ovl));
325 /* pipe is connected; change to message-read mode. */
326 dwMode = PIPE_READMODE_MESSAGE;
327 SetNamedPipeHandleState(conn, &dwMode, NULL, NULL);
328 Connection->ovl.hEvent = CreateEventA(NULL, TRUE, FALSE, NULL);
329 Connection->conn = conn;
331 ERR("protseq %s not supported\n", Connection->Protseq);
332 return RPC_S_PROTSEQ_NOT_SUPPORTED;
339 RPC_STATUS RPCRT4_CloseConnection(RpcConnection* Connection)
341 TRACE("(Connection == ^%p)\n", Connection);
342 if (Connection->conn) {
343 CancelIo(Connection->conn);
344 CloseHandle(Connection->conn);
345 Connection->conn = 0;
347 if (Connection->ovl.hEvent) {
348 CloseHandle(Connection->ovl.hEvent);
349 Connection->ovl.hEvent = 0;
354 RPC_STATUS RPCRT4_SpawnConnection(RpcConnection** Connection, RpcConnection* OldConnection)
356 RpcConnection* NewConnection;
357 RPC_STATUS err = RPCRT4_CreateConnection(&NewConnection, OldConnection->server, OldConnection->Protseq,
358 OldConnection->NetworkAddr, OldConnection->Endpoint, NULL, NULL);
359 if (err == RPC_S_OK) {
360 /* because of the way named pipes work, we'll transfer the connected pipe
361 * to the child, then reopen the server binding to continue listening */
362 NewConnection->conn = OldConnection->conn;
363 NewConnection->ovl = OldConnection->ovl;
364 OldConnection->conn = 0;
365 memset(&OldConnection->ovl, 0, sizeof(OldConnection->ovl));
366 *Connection = NewConnection;
367 RPCRT4_OpenConnection(OldConnection);
372 RPC_STATUS RPCRT4_AllocBinding(RpcBinding** Binding, BOOL server)
374 RpcBinding* NewBinding;
376 NewBinding = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcBinding));
377 NewBinding->refs = 1;
378 NewBinding->server = server;
380 *Binding = NewBinding;
385 RPC_STATUS RPCRT4_CreateBindingA(RpcBinding** Binding, BOOL server, LPSTR Protseq)
387 RpcBinding* NewBinding;
389 RPCRT4_AllocBinding(&NewBinding, server);
390 NewBinding->Protseq = RPCRT4_strdupA(Protseq);
392 TRACE("binding: %p\n", NewBinding);
393 *Binding = NewBinding;
398 RPC_STATUS RPCRT4_CreateBindingW(RpcBinding** Binding, BOOL server, LPWSTR Protseq)
400 RpcBinding* NewBinding;
402 RPCRT4_AllocBinding(&NewBinding, server);
403 NewBinding->Protseq = RPCRT4_strdupWtoA(Protseq);
405 TRACE("binding: %p\n", NewBinding);
406 *Binding = NewBinding;
411 RPC_STATUS RPCRT4_CompleteBindingA(RpcBinding* Binding, LPSTR NetworkAddr, LPSTR Endpoint, LPSTR NetworkOptions)
413 TRACE("(RpcBinding == ^%p, NetworkAddr == \"%s\", EndPoint == \"%s\", NetworkOptions == \"%s\")\n", Binding,
414 debugstr_a(NetworkAddr), debugstr_a(Endpoint), debugstr_a(NetworkOptions));
416 RPCRT4_strfree(Binding->NetworkAddr);
417 Binding->NetworkAddr = RPCRT4_strdupA(NetworkAddr);
418 RPCRT4_strfree(Binding->Endpoint);
420 Binding->Endpoint = RPCRT4_strdupA(Endpoint);
422 Binding->Endpoint = RPCRT4_strdupA("");
424 if (!Binding->Endpoint) ERR("out of memory?\n");
429 RPC_STATUS RPCRT4_CompleteBindingW(RpcBinding* Binding, LPWSTR NetworkAddr, LPWSTR Endpoint, LPWSTR NetworkOptions)
431 TRACE("(RpcBinding == ^%p, NetworkAddr == \"%s\", EndPoint == \"%s\", NetworkOptions == \"%s\")\n", Binding,
432 debugstr_w(NetworkAddr), debugstr_w(Endpoint), debugstr_w(NetworkOptions));
434 RPCRT4_strfree(Binding->NetworkAddr);
435 Binding->NetworkAddr = RPCRT4_strdupWtoA(NetworkAddr);
436 RPCRT4_strfree(Binding->Endpoint);
438 Binding->Endpoint = RPCRT4_strdupWtoA(Endpoint);
440 Binding->Endpoint = RPCRT4_strdupA("");
442 if (!Binding->Endpoint) ERR("out of memory?\n");
447 RPC_STATUS RPCRT4_ResolveBinding(RpcBinding* Binding, LPSTR Endpoint)
449 TRACE("(RpcBinding == ^%p, EndPoint == \"%s\"\n", Binding, Endpoint);
451 RPCRT4_strfree(Binding->Endpoint);
452 Binding->Endpoint = RPCRT4_strdupA(Endpoint);
457 RPC_STATUS RPCRT4_SetBindingObject(RpcBinding* Binding, UUID* ObjectUuid)
459 TRACE("(*RpcBinding == ^%p, UUID == %s)\n", Binding, debugstr_guid(ObjectUuid));
460 if (ObjectUuid) memcpy(&Binding->ObjectUuid, ObjectUuid, sizeof(UUID));
461 else UuidCreateNil(&Binding->ObjectUuid);
465 RPC_STATUS RPCRT4_MakeBinding(RpcBinding** Binding, RpcConnection* Connection)
467 RpcBinding* NewBinding;
468 TRACE("(*RpcBinding == ^%p, Connection == ^%p)\n", *Binding, Connection);
470 RPCRT4_AllocBinding(&NewBinding, Connection->server);
471 NewBinding->Protseq = RPCRT4_strdupA(Connection->Protseq);
472 NewBinding->NetworkAddr = RPCRT4_strdupA(Connection->NetworkAddr);
473 NewBinding->Endpoint = RPCRT4_strdupA(Connection->Endpoint);
474 NewBinding->FromConn = Connection;
476 TRACE("binding: %p\n", NewBinding);
477 *Binding = NewBinding;
482 RPC_STATUS RPCRT4_ExportBinding(RpcBinding** Binding, RpcBinding* OldBinding)
484 InterlockedIncrement(&OldBinding->refs);
485 *Binding = OldBinding;
489 RPC_STATUS RPCRT4_DestroyBinding(RpcBinding* Binding)
491 if (InterlockedDecrement(&Binding->refs))
494 TRACE("binding: %p\n", Binding);
495 /* FIXME: release connections */
496 RPCRT4_strfree(Binding->Endpoint);
497 RPCRT4_strfree(Binding->NetworkAddr);
498 RPCRT4_strfree(Binding->Protseq);
499 HeapFree(GetProcessHeap(), 0, Binding);
503 RPC_STATUS RPCRT4_OpenBinding(RpcBinding* Binding, RpcConnection** Connection,
504 PRPC_SYNTAX_IDENTIFIER TransferSyntax,
505 PRPC_SYNTAX_IDENTIFIER InterfaceId)
507 RpcConnection* NewConnection;
510 TRACE("(Binding == ^%p)\n", Binding);
512 /* if we try to bind a new interface and the connection is already opened,
513 * close the current connection and create a new with the new binding. */
514 if (!Binding->server && Binding->FromConn &&
515 memcmp(&Binding->FromConn->ActiveInterface, InterfaceId,
516 sizeof(RPC_SYNTAX_IDENTIFIER))) {
517 RPCRT4_ReleaseConnection(Binding->FromConn);
518 Binding->FromConn = NULL;
520 /* we already have an connection with acceptable binding, so use it */
521 if (Binding->FromConn) {
522 *Connection = Binding->FromConn;
527 /* create a new connection */
528 RPCRT4_GetConnection(&NewConnection, Binding->server, Binding->Protseq, Binding->NetworkAddr, Binding->Endpoint, NULL, Binding);
529 *Connection = NewConnection;
530 status = RPCRT4_OpenConnection(NewConnection);
531 if (status != RPC_S_OK) {
535 /* we need to send a binding packet if we are client. */
536 if (!(*Connection)->server) {
540 RpcPktHdr *response_hdr;
542 TRACE("sending bind request to server\n");
544 hdr = RPCRT4_BuildBindHeader(NDR_LOCAL_DATA_REPRESENTATION,
545 RPC_MAX_PACKET_SIZE, RPC_MAX_PACKET_SIZE,
546 InterfaceId, TransferSyntax);
548 status = RPCRT4_Send(*Connection, hdr, NULL, 0);
549 if (status != RPC_S_OK) {
550 RPCRT4_ReleaseConnection(*Connection);
554 response = HeapAlloc(GetProcessHeap(), 0, RPC_MAX_PACKET_SIZE);
555 if (response == NULL) {
556 WARN("Can't allocate memory for binding response\n");
557 RPCRT4_ReleaseConnection(*Connection);
558 return E_OUTOFMEMORY;
562 if (!ReadFile(NewConnection->conn, response, RPC_MAX_PACKET_SIZE, &count, NULL)) {
563 WARN("ReadFile failed with error %ld\n", GetLastError());
564 RPCRT4_ReleaseConnection(*Connection);
565 return RPC_S_PROTOCOL_ERROR;
568 if (count < sizeof(response_hdr->common)) {
569 WARN("received invalid header\n");
570 RPCRT4_ReleaseConnection(*Connection);
571 return RPC_S_PROTOCOL_ERROR;
574 response_hdr = (RpcPktHdr*)response;
576 if (response_hdr->common.rpc_ver != RPC_VER_MAJOR ||
577 response_hdr->common.rpc_ver_minor != RPC_VER_MINOR ||
578 response_hdr->common.ptype != PKT_BIND_ACK) {
579 WARN("invalid protocol version or rejection packet\n");
580 RPCRT4_ReleaseConnection(Binding->FromConn);
581 return RPC_S_PROTOCOL_ERROR;
584 if (response_hdr->bind_ack.max_tsize < RPC_MIN_PACKET_SIZE) {
585 WARN("server doesn't allow large enough packets\n");
586 RPCRT4_ReleaseConnection(Binding->FromConn);
587 return RPC_S_PROTOCOL_ERROR;
590 /* FIXME: do more checks? */
592 (*Connection)->MaxTransmissionSize = response_hdr->bind_ack.max_tsize;
593 (*Connection)->ActiveInterface = *InterfaceId;
599 RPC_STATUS RPCRT4_CloseBinding(RpcBinding* Binding, RpcConnection* Connection)
601 TRACE("(Binding == ^%p)\n", Binding);
602 if (!Connection) return RPC_S_OK;
603 if (Binding->FromConn == Connection) return RPC_S_OK;
604 return RPCRT4_ReleaseConnection(Connection);
607 /* utility functions for string composing and parsing */
608 static unsigned RPCRT4_strcopyA(LPSTR data, LPCSTR src)
610 unsigned len = strlen(src);
611 memcpy(data, src, len*sizeof(CHAR));
615 static unsigned RPCRT4_strcopyW(LPWSTR data, LPCWSTR src)
617 unsigned len = strlenW(src);
618 memcpy(data, src, len*sizeof(WCHAR));
622 static LPSTR RPCRT4_strconcatA(LPSTR dst, LPCSTR src)
624 DWORD len = strlen(dst), slen = strlen(src);
625 LPSTR ndst = HeapReAlloc(GetProcessHeap(), 0, dst, (len+slen+2)*sizeof(CHAR));
628 HeapFree(GetProcessHeap(), 0, dst);
632 memcpy(ndst+len+1, src, slen+1);
636 static LPWSTR RPCRT4_strconcatW(LPWSTR dst, LPCWSTR src)
638 DWORD len = strlenW(dst), slen = strlenW(src);
639 LPWSTR ndst = HeapReAlloc(GetProcessHeap(), 0, dst, (len+slen+2)*sizeof(WCHAR));
642 HeapFree(GetProcessHeap(), 0, dst);
646 memcpy(ndst+len+1, src, (slen+1)*sizeof(WCHAR));
651 /***********************************************************************
652 * RpcStringBindingComposeA (RPCRT4.@)
654 RPC_STATUS WINAPI RpcStringBindingComposeA(unsigned char *ObjUuid, unsigned char *Protseq,
655 unsigned char *NetworkAddr, unsigned char *Endpoint,
656 unsigned char *Options, unsigned char** StringBinding )
661 TRACE( "(%s,%s,%s,%s,%s,%p)\n",
662 debugstr_a( ObjUuid ), debugstr_a( Protseq ),
663 debugstr_a( NetworkAddr ), debugstr_a( Endpoint ),
664 debugstr_a( Options ), StringBinding );
666 if (ObjUuid && *ObjUuid) len += strlen(ObjUuid) + 1;
667 if (Protseq && *Protseq) len += strlen(Protseq) + 1;
668 if (NetworkAddr && *NetworkAddr) len += strlen(NetworkAddr);
669 if (Endpoint && *Endpoint) len += strlen(Endpoint) + 2;
670 if (Options && *Options) len += strlen(Options) + 2;
672 data = HeapAlloc(GetProcessHeap(), 0, len);
673 *StringBinding = data;
675 if (ObjUuid && *ObjUuid) {
676 data += RPCRT4_strcopyA(data, ObjUuid);
679 if (Protseq && *Protseq) {
680 data += RPCRT4_strcopyA(data, Protseq);
683 if (NetworkAddr && *NetworkAddr)
684 data += RPCRT4_strcopyA(data, NetworkAddr);
686 if ((Endpoint && *Endpoint) ||
687 (Options && *Options)) {
689 if (Endpoint && *Endpoint) {
690 data += RPCRT4_strcopyA(data, Endpoint);
691 if (Options && *Options) *data++ = ',';
693 if (Options && *Options) {
694 data += RPCRT4_strcopyA(data, Options);
703 /***********************************************************************
704 * RpcStringBindingComposeW (RPCRT4.@)
706 RPC_STATUS WINAPI RpcStringBindingComposeW( LPWSTR ObjUuid, LPWSTR Protseq,
707 LPWSTR NetworkAddr, LPWSTR Endpoint,
708 LPWSTR Options, LPWSTR* StringBinding )
713 TRACE("(%s,%s,%s,%s,%s,%p)\n",
714 debugstr_w( ObjUuid ), debugstr_w( Protseq ),
715 debugstr_w( NetworkAddr ), debugstr_w( Endpoint ),
716 debugstr_w( Options ), StringBinding);
718 if (ObjUuid && *ObjUuid) len += strlenW(ObjUuid) + 1;
719 if (Protseq && *Protseq) len += strlenW(Protseq) + 1;
720 if (NetworkAddr && *NetworkAddr) len += strlenW(NetworkAddr);
721 if (Endpoint && *Endpoint) len += strlenW(Endpoint) + 2;
722 if (Options && *Options) len += strlenW(Options) + 2;
724 data = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
725 *StringBinding = data;
727 if (ObjUuid && *ObjUuid) {
728 data += RPCRT4_strcopyW(data, ObjUuid);
731 if (Protseq && *Protseq) {
732 data += RPCRT4_strcopyW(data, Protseq);
735 if (NetworkAddr && *NetworkAddr) {
736 data += RPCRT4_strcopyW(data, NetworkAddr);
738 if ((Endpoint && *Endpoint) ||
739 (Options && *Options)) {
741 if (Endpoint && *Endpoint) {
742 data += RPCRT4_strcopyW(data, Endpoint);
743 if (Options && *Options) *data++ = ',';
745 if (Options && *Options) {
746 data += RPCRT4_strcopyW(data, Options);
756 /***********************************************************************
757 * RpcStringBindingParseA (RPCRT4.@)
759 RPC_STATUS WINAPI RpcStringBindingParseA( unsigned char *StringBinding, unsigned char **ObjUuid,
760 unsigned char **Protseq, unsigned char **NetworkAddr,
761 unsigned char **Endpoint, unsigned char **Options)
764 static const char ep_opt[] = "endpoint=";
766 TRACE("(%s,%p,%p,%p,%p,%p)\n", debugstr_a(StringBinding),
767 ObjUuid, Protseq, NetworkAddr, Endpoint, Options);
769 if (ObjUuid) *ObjUuid = NULL;
770 if (Protseq) *Protseq = NULL;
771 if (NetworkAddr) *NetworkAddr = NULL;
772 if (Endpoint) *Endpoint = NULL;
773 if (Options) *Options = NULL;
775 data = StringBinding;
777 next = strchr(data, '@');
779 if (ObjUuid) *ObjUuid = RPCRT4_strndupA(data, next - data);
783 next = strchr(data, ':');
785 if (Protseq) *Protseq = RPCRT4_strndupA(data, next - data);
789 next = strchr(data, '[');
793 if (NetworkAddr) *NetworkAddr = RPCRT4_strndupA(data, next - data);
795 close = strchr(data, ']');
796 if (!close) goto fail;
798 /* tokenize options */
799 while (data < close) {
800 next = strchr(data, ',');
801 if (!next || next > close) next = close;
802 /* FIXME: this is kind of inefficient */
803 opt = RPCRT4_strndupA(data, next - data);
807 next = strchr(opt, '=');
809 /* not an option, must be an endpoint */
810 if (*Endpoint) goto fail;
813 if (strncmp(opt, ep_opt, strlen(ep_opt)) == 0) {
814 /* endpoint option */
815 if (*Endpoint) goto fail;
816 *Endpoint = RPCRT4_strdupA(next+1);
817 HeapFree(GetProcessHeap(), 0, opt);
821 /* FIXME: this is kind of inefficient */
822 *Options = RPCRT4_strconcatA(*Options, opt);
823 HeapFree(GetProcessHeap(), 0, opt);
831 if (*data) goto fail;
833 else if (NetworkAddr)
834 *NetworkAddr = RPCRT4_strdupA(data);
839 if (ObjUuid) RpcStringFreeA((unsigned char**)ObjUuid);
840 if (Protseq) RpcStringFreeA((unsigned char**)Protseq);
841 if (NetworkAddr) RpcStringFreeA((unsigned char**)NetworkAddr);
842 if (Endpoint) RpcStringFreeA((unsigned char**)Endpoint);
843 if (Options) RpcStringFreeA((unsigned char**)Options);
844 return RPC_S_INVALID_STRING_BINDING;
847 /***********************************************************************
848 * RpcStringBindingParseW (RPCRT4.@)
850 RPC_STATUS WINAPI RpcStringBindingParseW( LPWSTR StringBinding, LPWSTR *ObjUuid,
851 LPWSTR *Protseq, LPWSTR *NetworkAddr,
852 LPWSTR *Endpoint, LPWSTR *Options)
855 static const WCHAR ep_opt[] = {'e','n','d','p','o','i','n','t','=',0};
857 TRACE("(%s,%p,%p,%p,%p,%p)\n", debugstr_w(StringBinding),
858 ObjUuid, Protseq, NetworkAddr, Endpoint, Options);
860 if (ObjUuid) *ObjUuid = NULL;
861 if (Protseq) *Protseq = NULL;
862 if (NetworkAddr) *NetworkAddr = NULL;
863 if (Endpoint) *Endpoint = NULL;
864 if (Options) *Options = NULL;
866 data = StringBinding;
868 next = strchrW(data, '@');
870 if (ObjUuid) *ObjUuid = RPCRT4_strndupW(data, next - data);
874 next = strchrW(data, ':');
876 if (Protseq) *Protseq = RPCRT4_strndupW(data, next - data);
880 next = strchrW(data, '[');
884 if (NetworkAddr) *NetworkAddr = RPCRT4_strndupW(data, next - data);
886 close = strchrW(data, ']');
887 if (!close) goto fail;
889 /* tokenize options */
890 while (data < close) {
891 next = strchrW(data, ',');
892 if (!next || next > close) next = close;
893 /* FIXME: this is kind of inefficient */
894 opt = RPCRT4_strndupW(data, next - data);
898 next = strchrW(opt, '=');
900 /* not an option, must be an endpoint */
901 if (*Endpoint) goto fail;
904 if (strncmpW(opt, ep_opt, strlenW(ep_opt)) == 0) {
905 /* endpoint option */
906 if (*Endpoint) goto fail;
907 *Endpoint = RPCRT4_strdupW(next+1);
908 HeapFree(GetProcessHeap(), 0, opt);
912 /* FIXME: this is kind of inefficient */
913 *Options = RPCRT4_strconcatW(*Options, opt);
914 HeapFree(GetProcessHeap(), 0, opt);
922 if (*data) goto fail;
923 } else if (NetworkAddr)
924 *NetworkAddr = RPCRT4_strdupW(data);
929 if (ObjUuid) RpcStringFreeW(ObjUuid);
930 if (Protseq) RpcStringFreeW(Protseq);
931 if (NetworkAddr) RpcStringFreeW(NetworkAddr);
932 if (Endpoint) RpcStringFreeW(Endpoint);
933 if (Options) RpcStringFreeW(Options);
934 return RPC_S_INVALID_STRING_BINDING;
937 /***********************************************************************
938 * RpcBindingFree (RPCRT4.@)
940 RPC_STATUS WINAPI RpcBindingFree( RPC_BINDING_HANDLE* Binding )
943 TRACE("(%p) = %p\n", Binding, *Binding);
944 status = RPCRT4_DestroyBinding(*Binding);
945 if (status == RPC_S_OK) *Binding = 0;
949 /***********************************************************************
950 * RpcBindingVectorFree (RPCRT4.@)
952 RPC_STATUS WINAPI RpcBindingVectorFree( RPC_BINDING_VECTOR** BindingVector )
957 TRACE("(%p)\n", BindingVector);
958 for (c=0; c<(*BindingVector)->Count; c++) {
959 status = RpcBindingFree(&(*BindingVector)->BindingH[c]);
961 HeapFree(GetProcessHeap(), 0, *BindingVector);
962 *BindingVector = NULL;
966 /***********************************************************************
967 * RpcBindingInqObject (RPCRT4.@)
969 RPC_STATUS WINAPI RpcBindingInqObject( RPC_BINDING_HANDLE Binding, UUID* ObjectUuid )
971 RpcBinding* bind = (RpcBinding*)Binding;
973 TRACE("(%p,%p) = %s\n", Binding, ObjectUuid, debugstr_guid(&bind->ObjectUuid));
974 memcpy(ObjectUuid, &bind->ObjectUuid, sizeof(UUID));
978 /***********************************************************************
979 * RpcBindingSetObject (RPCRT4.@)
981 RPC_STATUS WINAPI RpcBindingSetObject( RPC_BINDING_HANDLE Binding, UUID* ObjectUuid )
983 RpcBinding* bind = (RpcBinding*)Binding;
985 TRACE("(%p,%s)\n", Binding, debugstr_guid(ObjectUuid));
986 if (bind->server) return RPC_S_WRONG_KIND_OF_BINDING;
987 return RPCRT4_SetBindingObject(Binding, ObjectUuid);
990 /***********************************************************************
991 * RpcBindingFromStringBindingA (RPCRT4.@)
993 RPC_STATUS WINAPI RpcBindingFromStringBindingA( unsigned char *StringBinding, RPC_BINDING_HANDLE* Binding )
996 RpcBinding* bind = NULL;
997 unsigned char *ObjectUuid, *Protseq, *NetworkAddr, *Endpoint, *Options;
1000 TRACE("(%s,%p)\n", debugstr_a(StringBinding), Binding);
1002 ret = RpcStringBindingParseA(StringBinding, &ObjectUuid, &Protseq,
1003 &NetworkAddr, &Endpoint, &Options);
1004 if (ret != RPC_S_OK) return ret;
1006 ret = UuidFromStringA(ObjectUuid, &Uuid);
1008 if (ret == RPC_S_OK)
1009 ret = RPCRT4_CreateBindingA(&bind, FALSE, Protseq);
1010 if (ret == RPC_S_OK)
1011 ret = RPCRT4_SetBindingObject(bind, &Uuid);
1012 if (ret == RPC_S_OK)
1013 ret = RPCRT4_CompleteBindingA(bind, NetworkAddr, Endpoint, Options);
1015 RpcStringFreeA((unsigned char**)&Options);
1016 RpcStringFreeA((unsigned char**)&Endpoint);
1017 RpcStringFreeA((unsigned char**)&NetworkAddr);
1018 RpcStringFreeA((unsigned char**)&Protseq);
1019 RpcStringFreeA((unsigned char**)&ObjectUuid);
1021 if (ret == RPC_S_OK)
1022 *Binding = (RPC_BINDING_HANDLE)bind;
1024 RPCRT4_DestroyBinding(bind);
1029 /***********************************************************************
1030 * RpcBindingFromStringBindingW (RPCRT4.@)
1032 RPC_STATUS WINAPI RpcBindingFromStringBindingW( LPWSTR StringBinding, RPC_BINDING_HANDLE* Binding )
1035 RpcBinding* bind = NULL;
1036 LPWSTR ObjectUuid, Protseq, NetworkAddr, Endpoint, Options;
1039 TRACE("(%s,%p)\n", debugstr_w(StringBinding), Binding);
1041 ret = RpcStringBindingParseW(StringBinding, &ObjectUuid, &Protseq,
1042 &NetworkAddr, &Endpoint, &Options);
1043 if (ret != RPC_S_OK) return ret;
1045 ret = UuidFromStringW(ObjectUuid, &Uuid);
1047 if (ret == RPC_S_OK)
1048 ret = RPCRT4_CreateBindingW(&bind, FALSE, Protseq);
1049 if (ret == RPC_S_OK)
1050 ret = RPCRT4_SetBindingObject(bind, &Uuid);
1051 if (ret == RPC_S_OK)
1052 ret = RPCRT4_CompleteBindingW(bind, NetworkAddr, Endpoint, Options);
1054 RpcStringFreeW(&Options);
1055 RpcStringFreeW(&Endpoint);
1056 RpcStringFreeW(&NetworkAddr);
1057 RpcStringFreeW(&Protseq);
1058 RpcStringFreeW(&ObjectUuid);
1060 if (ret == RPC_S_OK)
1061 *Binding = (RPC_BINDING_HANDLE)bind;
1063 RPCRT4_DestroyBinding(bind);
1068 /***********************************************************************
1069 * RpcBindingToStringBindingA (RPCRT4.@)
1071 RPC_STATUS WINAPI RpcBindingToStringBindingA( RPC_BINDING_HANDLE Binding, unsigned char** StringBinding )
1074 RpcBinding* bind = (RpcBinding*)Binding;
1077 TRACE("(%p,%p)\n", Binding, StringBinding);
1079 ret = UuidToStringA(&bind->ObjectUuid, (unsigned char**)&ObjectUuid);
1080 if (ret != RPC_S_OK) return ret;
1082 ret = RpcStringBindingComposeA(ObjectUuid, bind->Protseq, bind->NetworkAddr,
1083 bind->Endpoint, NULL, StringBinding);
1085 RpcStringFreeA((unsigned char**)&ObjectUuid);
1090 /***********************************************************************
1091 * RpcBindingToStringBindingW (RPCRT4.@)
1093 RPC_STATUS WINAPI RpcBindingToStringBindingW( RPC_BINDING_HANDLE Binding, LPWSTR* StringBinding )
1096 unsigned char *str = NULL;
1097 TRACE("(%p,%p)\n", Binding, StringBinding);
1098 ret = RpcBindingToStringBindingA(Binding, &str);
1099 *StringBinding = RPCRT4_strdupAtoW(str);
1100 RpcStringFreeA((unsigned char**)&str);
1104 /***********************************************************************
1105 * I_RpcBindingSetAsync (RPCRT4.@)
1107 * Exists in win9x and winNT, but with different number of arguments
1108 * (9x version has 3 arguments, NT has 2).
1110 RPC_STATUS WINAPI I_RpcBindingSetAsync( RPC_BINDING_HANDLE Binding, RPC_BLOCKING_FN BlockingFn)
1112 RpcBinding* bind = (RpcBinding*)Binding;
1114 TRACE( "(%p,%p): stub\n", Binding, BlockingFn );
1116 bind->BlockingFn = BlockingFn;
1121 /***********************************************************************
1122 * RpcNetworkIsProtseqValidA (RPCRT4.@)
1124 RPC_STATUS RPC_ENTRY RpcNetworkIsProtseqValidA(unsigned char *protseq) {
1125 UNICODE_STRING protseqW;
1127 if (!protseq) return RPC_S_INVALID_RPC_PROTSEQ; /* ? */
1129 if (RtlCreateUnicodeStringFromAsciiz(&protseqW, protseq)) {
1130 RPC_STATUS ret = RpcNetworkIsProtseqValidW(protseqW.Buffer);
1131 RtlFreeUnicodeString(&protseqW);
1133 } else return RPC_S_OUT_OF_MEMORY;
1136 /***********************************************************************
1137 * RpcNetworkIsProtseqValidW (RPCRT4.@)
1139 * Checks if the given protocol sequence is known by the RPC system.
1140 * If it is, returns RPC_S_OK, otherwise RPC_S_PROTSEQ_NOT_SUPPORTED.
1142 * We currently support:
1143 * ncalrpc local-only rpc over LPC (LPC is not really used)
1144 * ncacn_np rpc over named pipes
1146 RPC_STATUS RPC_ENTRY RpcNetworkIsProtseqValidW(LPWSTR protseq) {
1147 static const WCHAR protseqsW[][15] = {
1148 {'n','c','a','l','r','p','c',0},
1149 {'n','c','a','c','n','_','n','p',0}
1151 static const int count = sizeof(protseqsW) / sizeof(protseqsW[0]);
1154 if (!protseq) return RPC_S_INVALID_RPC_PROTSEQ; /* ? */
1156 for (i = 0; i < count; i++) {
1157 if (!strcmpW(protseq, protseqsW[i])) return RPC_S_OK;
1160 FIXME("Unknown protseq %s - we probably need to implement it one day\n", debugstr_w(protseq));
1161 return RPC_S_PROTSEQ_NOT_SUPPORTED;