winapi_test: Regenerate tests.
[wine] / dlls / rpcrt4 / rpc_transport.c
1 /*
2  * RPC transport layer
3  *
4  * Copyright 2001 Ove Kåven, TransGaming Technologies
5  * Copyright 2003 Mike Hearn
6  * Copyright 2004 Filip Navara
7  * Copyright 2006 Mike McCormack
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  *
23  */
24
25 #include "config.h"
26
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <assert.h>
31 #include <errno.h>
32
33 #ifdef HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
36 #include <fcntl.h>
37 #include <stdlib.h>
38 #include <sys/types.h>
39 #ifdef HAVE_SYS_SOCKET_H
40 # include <sys/socket.h>
41 #endif
42 #ifdef HAVE_NETINET_IN_H
43 # include <netinet/in.h>
44 #endif
45 #ifdef HAVE_ARPA_INET_H
46 # include <arpa/inet.h>
47 #endif
48 #ifdef HAVE_NETDB_H
49 #include <netdb.h>
50 #endif
51
52 #include "windef.h"
53 #include "winbase.h"
54 #include "winnls.h"
55 #include "winerror.h"
56 #include "winreg.h"
57 #include "winternl.h"
58 #include "wine/unicode.h"
59
60 #include "rpc.h"
61 #include "rpcndr.h"
62
63 #include "wine/debug.h"
64
65 #include "rpc_binding.h"
66 #include "rpc_message.h"
67 #include "epm_towers.h"
68
69 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
70
71 static CRITICAL_SECTION connection_pool_cs;
72 static CRITICAL_SECTION_DEBUG connection_pool_cs_debug =
73 {
74     0, 0, &connection_pool_cs,
75     { &connection_pool_cs_debug.ProcessLocksList, &connection_pool_cs_debug.ProcessLocksList },
76       0, 0, { (DWORD_PTR)(__FILE__ ": connection_pool") }
77 };
78 static CRITICAL_SECTION connection_pool_cs = { &connection_pool_cs_debug, -1, 0, 0, 0, 0 };
79
80 static struct list connection_pool = LIST_INIT(connection_pool);
81
82 /**** ncacn_np support ****/
83
84 typedef struct _RpcConnection_np
85 {
86   RpcConnection common;
87   HANDLE pipe, thread;
88   OVERLAPPED ovl;
89 } RpcConnection_np;
90
91 static RpcConnection *rpcrt4_conn_np_alloc(void)
92 {
93   RpcConnection_np *npc = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_np));
94   if (npc)
95   {
96     npc->pipe = NULL;
97     npc->thread = NULL;
98     memset(&npc->ovl, 0, sizeof(npc->ovl));
99   }
100   return &npc->common;
101 }
102
103 static RPC_STATUS rpcrt4_connect_pipe(RpcConnection *Connection, LPCSTR pname)
104 {
105   RpcConnection_np *npc = (RpcConnection_np *) Connection;
106   TRACE("listening on %s\n", pname);
107
108   npc->pipe = CreateNamedPipeA(pname, PIPE_ACCESS_DUPLEX,
109                                PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE,
110                                PIPE_UNLIMITED_INSTANCES,
111                                RPC_MAX_PACKET_SIZE, RPC_MAX_PACKET_SIZE, 5000, NULL);
112   if (npc->pipe == INVALID_HANDLE_VALUE) {
113     WARN("CreateNamedPipe failed with error %ld\n", GetLastError());
114     return RPC_S_SERVER_UNAVAILABLE;
115   }
116
117   memset(&npc->ovl, 0, sizeof(npc->ovl));
118   npc->ovl.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
119   if (ConnectNamedPipe(npc->pipe, &npc->ovl))
120      return RPC_S_OK;
121
122   WARN("Couldn't ConnectNamedPipe (error was %ld)\n", GetLastError());
123   if (GetLastError() == ERROR_PIPE_CONNECTED) {
124     SetEvent(npc->ovl.hEvent);
125     return RPC_S_OK;
126   }
127   if (GetLastError() == ERROR_IO_PENDING) {
128     /* FIXME: looks like we need to GetOverlappedResult here? */
129     return RPC_S_OK;
130   }
131   return RPC_S_SERVER_UNAVAILABLE;
132 }
133
134 static RPC_STATUS rpcrt4_open_pipe(RpcConnection *Connection, LPCSTR pname, BOOL wait)
135 {
136   RpcConnection_np *npc = (RpcConnection_np *) Connection;
137   HANDLE pipe;
138   DWORD err, dwMode;
139
140   TRACE("connecting to %s\n", pname);
141
142   while (TRUE) {
143     pipe = CreateFileA(pname, GENERIC_READ|GENERIC_WRITE, 0, NULL,
144                        OPEN_EXISTING, 0, 0);
145     if (pipe != INVALID_HANDLE_VALUE) break;
146     err = GetLastError();
147     if (err == ERROR_PIPE_BUSY) {
148       TRACE("connection failed, error=%lx\n", err);
149       return RPC_S_SERVER_TOO_BUSY;
150     }
151     if (!wait)
152       return RPC_S_SERVER_UNAVAILABLE;
153     if (!WaitNamedPipeA(pname, NMPWAIT_WAIT_FOREVER)) {
154       err = GetLastError();
155       WARN("connection failed, error=%lx\n", err);
156       return RPC_S_SERVER_UNAVAILABLE;
157     }
158   }
159
160   /* success */
161   memset(&npc->ovl, 0, sizeof(npc->ovl));
162   /* pipe is connected; change to message-read mode. */
163   dwMode = PIPE_READMODE_MESSAGE;
164   SetNamedPipeHandleState(pipe, &dwMode, NULL, NULL);
165   npc->ovl.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
166   npc->pipe = pipe;
167
168   return RPC_S_OK;
169 }
170
171 static RPC_STATUS rpcrt4_ncalrpc_open(RpcConnection* Connection)
172 {
173   RpcConnection_np *npc = (RpcConnection_np *) Connection;
174   static LPCSTR prefix = "\\\\.\\pipe\\lrpc\\";
175   RPC_STATUS r;
176   LPSTR pname;
177
178   /* already connected? */
179   if (npc->pipe)
180     return RPC_S_OK;
181
182   /* protseq=ncalrpc: supposed to use NT LPC ports,
183    * but we'll implement it with named pipes for now */
184   pname = I_RpcAllocate(strlen(prefix) + strlen(Connection->Endpoint) + 1);
185   strcat(strcpy(pname, prefix), Connection->Endpoint);
186
187   if (Connection->server)
188     r = rpcrt4_connect_pipe(Connection, pname);
189   else
190     r = rpcrt4_open_pipe(Connection, pname, TRUE);
191   I_RpcFree(pname);
192
193   return r;
194 }
195
196 static RPC_STATUS rpcrt4_ncacn_np_open(RpcConnection* Connection)
197 {
198   RpcConnection_np *npc = (RpcConnection_np *) Connection;
199   static LPCSTR prefix = "\\\\.";
200   RPC_STATUS r;
201   LPSTR pname;
202
203   /* already connected? */
204   if (npc->pipe)
205     return RPC_S_OK;
206
207   /* protseq=ncacn_np: named pipes */
208   pname = I_RpcAllocate(strlen(prefix) + strlen(Connection->Endpoint) + 1);
209   strcat(strcpy(pname, prefix), Connection->Endpoint);
210   if (Connection->server)
211     r = rpcrt4_connect_pipe(Connection, pname);
212   else
213     r = rpcrt4_open_pipe(Connection, pname, FALSE);
214   I_RpcFree(pname);
215
216   return r;
217 }
218
219 static HANDLE rpcrt4_conn_np_get_connect_event(RpcConnection *Connection)
220 {
221   RpcConnection_np *npc = (RpcConnection_np *) Connection;
222   return npc->ovl.hEvent;
223 }
224
225 static RPC_STATUS rpcrt4_conn_np_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
226 {
227   RpcConnection_np *old_npc = (RpcConnection_np *) old_conn;
228   RpcConnection_np *new_npc = (RpcConnection_np *) new_conn;
229   /* because of the way named pipes work, we'll transfer the connected pipe
230    * to the child, then reopen the server binding to continue listening */
231
232   new_npc->pipe = old_npc->pipe;
233   new_npc->ovl = old_npc->ovl;
234   old_npc->pipe = 0;
235   memset(&old_npc->ovl, 0, sizeof(old_npc->ovl));
236   return RPCRT4_OpenConnection(old_conn);
237 }
238
239 static int rpcrt4_conn_np_read(RpcConnection *Connection,
240                         void *buffer, unsigned int count)
241 {
242   RpcConnection_np *npc = (RpcConnection_np *) Connection;
243   DWORD dwRead = 0;
244   if (!ReadFile(npc->pipe, buffer, count, &dwRead, NULL) &&
245       (GetLastError() != ERROR_MORE_DATA))
246     return -1;
247   return dwRead;
248 }
249
250 static int rpcrt4_conn_np_write(RpcConnection *Connection,
251                              const void *buffer, unsigned int count)
252 {
253   RpcConnection_np *npc = (RpcConnection_np *) Connection;
254   DWORD dwWritten = 0;
255   if (!WriteFile(npc->pipe, buffer, count, &dwWritten, NULL))
256     return -1;
257   return dwWritten;
258 }
259
260 static int rpcrt4_conn_np_close(RpcConnection *Connection)
261 {
262   RpcConnection_np *npc = (RpcConnection_np *) Connection;
263   if (npc->pipe) {
264     FlushFileBuffers(npc->pipe);
265     CloseHandle(npc->pipe);
266     npc->pipe = 0;
267   }
268   if (npc->ovl.hEvent) {
269     CloseHandle(npc->ovl.hEvent);
270     npc->ovl.hEvent = 0;
271   }
272   return 0;
273 }
274
275 static size_t rpcrt4_ncacn_np_get_top_of_tower(unsigned char *tower_data,
276                                                const char *networkaddr,
277                                                const char *endpoint)
278 {
279     twr_empty_floor_t *smb_floor;
280     twr_empty_floor_t *nb_floor;
281     size_t size;
282     size_t networkaddr_size;
283     size_t endpoint_size;
284
285     TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
286
287     networkaddr_size = strlen(networkaddr) + 1;
288     endpoint_size = strlen(endpoint) + 1;
289     size = sizeof(*smb_floor) + endpoint_size + sizeof(*nb_floor) + networkaddr_size;
290
291     if (!tower_data)
292         return size;
293
294     smb_floor = (twr_empty_floor_t *)tower_data;
295
296     tower_data += sizeof(*smb_floor);
297
298     smb_floor->count_lhs = sizeof(smb_floor->protid);
299     smb_floor->protid = EPM_PROTOCOL_SMB;
300     smb_floor->count_rhs = endpoint_size;
301
302     memcpy(tower_data, endpoint, endpoint_size);
303     tower_data += endpoint_size;
304
305     nb_floor = (twr_empty_floor_t *)tower_data;
306
307     tower_data += sizeof(*nb_floor);
308
309     nb_floor->count_lhs = sizeof(nb_floor->protid);
310     nb_floor->protid = EPM_PROTOCOL_NETBIOS;
311     nb_floor->count_rhs = networkaddr_size;
312
313     memcpy(tower_data, networkaddr, networkaddr_size);
314     tower_data += networkaddr_size;
315
316     return size;
317 }
318
319 static RPC_STATUS rpcrt4_ncacn_np_parse_top_of_tower(const unsigned char *tower_data,
320                                                      size_t tower_size,
321                                                      char **networkaddr,
322                                                      char **endpoint)
323 {
324     const twr_empty_floor_t *smb_floor = (const twr_empty_floor_t *)tower_data;
325     const twr_empty_floor_t *nb_floor;
326
327     TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
328
329     if (tower_size < sizeof(*smb_floor))
330         return EPT_S_NOT_REGISTERED;
331
332     tower_data += sizeof(*smb_floor);
333     tower_size -= sizeof(*smb_floor);
334
335     if ((smb_floor->count_lhs != sizeof(smb_floor->protid)) ||
336         (smb_floor->protid != EPM_PROTOCOL_SMB) ||
337         (smb_floor->count_rhs > tower_size))
338         return EPT_S_NOT_REGISTERED;
339
340     if (endpoint)
341     {
342         *endpoint = I_RpcAllocate(smb_floor->count_rhs);
343         if (!*endpoint)
344             return RPC_S_OUT_OF_RESOURCES;
345         memcpy(*endpoint, tower_data, smb_floor->count_rhs);
346     }
347     tower_data += smb_floor->count_rhs;
348     tower_size -= smb_floor->count_rhs;
349
350     if (tower_size < sizeof(*nb_floor))
351         return EPT_S_NOT_REGISTERED;
352
353     nb_floor = (const twr_empty_floor_t *)tower_data;
354
355     tower_data += sizeof(*nb_floor);
356     tower_size -= sizeof(*nb_floor);
357
358     if ((nb_floor->count_lhs != sizeof(nb_floor->protid)) ||
359         (nb_floor->protid != EPM_PROTOCOL_NETBIOS) ||
360         (nb_floor->count_rhs > tower_size))
361         return EPT_S_NOT_REGISTERED;
362
363     if (networkaddr)
364     {
365         *networkaddr = I_RpcAllocate(nb_floor->count_rhs);
366         if (!*networkaddr)
367         {
368             if (endpoint)
369             {
370                 I_RpcFree(*endpoint);
371                 *endpoint = NULL;
372             }
373             return RPC_S_OUT_OF_RESOURCES;
374         }
375         memcpy(*networkaddr, tower_data, nb_floor->count_rhs);
376     }
377
378     return RPC_S_OK;
379 }
380
381 static size_t rpcrt4_ncalrpc_get_top_of_tower(unsigned char *tower_data,
382                                               const char *networkaddr,
383                                               const char *endpoint)
384 {
385     twr_empty_floor_t *pipe_floor;
386     size_t size;
387     size_t endpoint_size;
388
389     TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
390
391     endpoint_size = strlen(networkaddr) + 1;
392     size = sizeof(*pipe_floor) + endpoint_size;
393
394     if (!tower_data)
395         return size;
396
397     pipe_floor = (twr_empty_floor_t *)tower_data;
398
399     tower_data += sizeof(*pipe_floor);
400
401     pipe_floor->count_lhs = sizeof(pipe_floor->protid);
402     pipe_floor->protid = EPM_PROTOCOL_SMB;
403     pipe_floor->count_rhs = endpoint_size;
404
405     memcpy(tower_data, endpoint, endpoint_size);
406     tower_data += endpoint_size;
407
408     return size;
409 }
410
411 static RPC_STATUS rpcrt4_ncalrpc_parse_top_of_tower(const unsigned char *tower_data,
412                                                     size_t tower_size,
413                                                     char **networkaddr,
414                                                     char **endpoint)
415 {
416     const twr_empty_floor_t *pipe_floor = (const twr_empty_floor_t *)tower_data;
417
418     TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
419
420     *networkaddr = NULL;
421     *endpoint = NULL;
422
423     if (tower_size < sizeof(*pipe_floor))
424         return EPT_S_NOT_REGISTERED;
425
426     tower_data += sizeof(*pipe_floor);
427     tower_size -= sizeof(*pipe_floor);
428
429     if ((pipe_floor->count_lhs != sizeof(pipe_floor->protid)) ||
430         (pipe_floor->protid != EPM_PROTOCOL_SMB) ||
431         (pipe_floor->count_rhs > tower_size))
432         return EPT_S_NOT_REGISTERED;
433
434     if (endpoint)
435     {
436         *endpoint = I_RpcAllocate(pipe_floor->count_rhs);
437         if (!*endpoint)
438             return RPC_S_OUT_OF_RESOURCES;
439         memcpy(*endpoint, tower_data, pipe_floor->count_rhs);
440     }
441
442     return RPC_S_OK;
443 }
444
445 /**** ncacn_ip_tcp support ****/
446
447 typedef struct _RpcConnection_tcp
448 {
449   RpcConnection common;
450   int sock;
451 } RpcConnection_tcp;
452
453 static RpcConnection *rpcrt4_conn_tcp_alloc(void)
454 {
455   RpcConnection_tcp *tcpc;
456   tcpc = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_tcp));
457   if (tcpc)
458     tcpc->sock = -1;
459   return &tcpc->common;
460 }
461
462 static RPC_STATUS rpcrt4_ncacn_ip_tcp_open(RpcConnection* Connection)
463 {
464   RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
465   int sock;
466   int ret;
467   struct addrinfo *ai;
468   struct addrinfo *ai_cur;
469   struct addrinfo hints;
470
471   TRACE("(%s, %s)\n", Connection->NetworkAddr, Connection->Endpoint);
472
473   if (Connection->server)
474   {
475     ERR("ncacn_ip_tcp servers not supported yet\n");
476     return RPC_S_SERVER_UNAVAILABLE;
477   }
478
479   if (tcpc->sock != -1)
480     return RPC_S_OK;
481
482   hints.ai_flags          = 0;
483   hints.ai_family         = PF_UNSPEC;
484   hints.ai_socktype       = SOCK_STREAM;
485   hints.ai_protocol       = IPPROTO_TCP;
486   hints.ai_addrlen        = 0;
487   hints.ai_addr           = NULL;
488   hints.ai_canonname      = NULL;
489   hints.ai_next           = NULL;
490
491   ret = getaddrinfo(Connection->NetworkAddr, Connection->Endpoint, &hints, &ai);
492   if (ret)
493   {
494     ERR("getaddrinfo failed: %s\n", gai_strerror(ret));
495     return RPC_S_SERVER_UNAVAILABLE;
496   }
497
498   for (ai_cur = ai; ai_cur; ai_cur = ai->ai_next)
499   {
500     if (TRACE_ON(rpc))
501     {
502       char host[256];
503       char service[256];
504       getnameinfo(ai_cur->ai_addr, ai_cur->ai_addrlen,
505         host, sizeof(host), service, sizeof(service),
506         NI_NUMERICHOST | NI_NUMERICSERV);
507       TRACE("trying %s:%s\n", host, service);
508     }
509
510     sock = socket(ai_cur->ai_family, ai_cur->ai_socktype, ai_cur->ai_protocol);
511     if (sock < 0)
512     {
513       WARN("socket() failed\n");
514       continue;
515     }
516
517     if (0>connect(sock, ai_cur->ai_addr, ai_cur->ai_addrlen))
518     {
519       WARN("connect() failed\n");
520       close(sock);
521       continue;
522     }
523
524     tcpc->sock = sock;
525
526     freeaddrinfo(ai);
527     TRACE("connected\n");
528     return RPC_S_OK;
529   }
530
531   freeaddrinfo(ai);
532   ERR("couldn't connect to %s:%s\n", Connection->NetworkAddr, Connection->Endpoint);
533   return RPC_S_SERVER_UNAVAILABLE;
534 }
535
536 static HANDLE rpcrt4_conn_tcp_get_wait_handle(RpcConnection *Connection)
537 {
538   assert(0);
539   return 0;
540 }
541
542 static RPC_STATUS rpcrt4_conn_tcp_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
543 {
544   assert(0);
545   return RPC_S_SERVER_UNAVAILABLE;
546 }
547
548 static int rpcrt4_conn_tcp_read(RpcConnection *Connection,
549                                 void *buffer, unsigned int count)
550 {
551   RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
552   int r = recv(tcpc->sock, buffer, count, MSG_WAITALL);
553   TRACE("%d %p %u -> %d\n", tcpc->sock, buffer, count, r);
554   return r;
555 }
556
557 static int rpcrt4_conn_tcp_write(RpcConnection *Connection,
558                                  const void *buffer, unsigned int count)
559 {
560   RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
561   int r = write(tcpc->sock, buffer, count);
562   TRACE("%d %p %u -> %d\n", tcpc->sock, buffer, count, r);
563   return r;
564 }
565
566 static int rpcrt4_conn_tcp_close(RpcConnection *Connection)
567 {
568   RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
569
570   TRACE("%d\n", tcpc->sock);
571   if (tcpc->sock != -1)
572     close(tcpc->sock);
573   tcpc->sock = -1;
574   return 0;
575 }
576
577 static size_t rpcrt4_ncacn_ip_tcp_get_top_of_tower(unsigned char *tower_data,
578                                                    const char *networkaddr,
579                                                    const char *endpoint)
580 {
581     twr_tcp_floor_t *tcp_floor;
582     twr_ipv4_floor_t *ipv4_floor;
583     struct addrinfo *ai;
584     struct addrinfo hints;
585     int ret;
586     size_t size = sizeof(*tcp_floor) + sizeof(*ipv4_floor);
587
588     TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
589
590     if (!tower_data)
591         return size;
592
593     tcp_floor = (twr_tcp_floor_t *)tower_data;
594     tower_data += sizeof(*tcp_floor);
595
596     ipv4_floor = (twr_ipv4_floor_t *)tower_data;
597
598     tcp_floor->count_lhs = sizeof(tcp_floor->protid);
599     tcp_floor->protid = EPM_PROTOCOL_TCP;
600     tcp_floor->count_rhs = sizeof(tcp_floor->port);
601
602     ipv4_floor->count_lhs = sizeof(ipv4_floor->protid);
603     ipv4_floor->protid = EPM_PROTOCOL_IP;
604     ipv4_floor->count_rhs = sizeof(ipv4_floor->ipv4addr);
605
606     hints.ai_flags          = AI_NUMERICHOST;
607     /* FIXME: only support IPv4 at the moment. how is IPv6 represented by the EPM? */
608     hints.ai_family         = PF_INET;
609     hints.ai_socktype       = SOCK_STREAM;
610     hints.ai_protocol       = IPPROTO_TCP;
611     hints.ai_addrlen        = 0;
612     hints.ai_addr           = NULL;
613     hints.ai_canonname      = NULL;
614     hints.ai_next           = NULL;
615
616     ret = getaddrinfo(networkaddr, endpoint, &hints, &ai);
617     if (ret)
618     {
619         ret = getaddrinfo("0.0.0.0", endpoint, &hints, &ai);
620         if (ret)
621         {
622             ERR("getaddrinfo failed: %s\n", gai_strerror(ret));
623             return 0;
624         }
625     }
626
627     if (ai->ai_family == PF_INET)
628     {
629         const struct sockaddr_in *sin = (const struct sockaddr_in *)ai->ai_addr;
630         tcp_floor->port = sin->sin_port;
631         ipv4_floor->ipv4addr = sin->sin_addr.s_addr;
632     }
633     else
634     {
635         ERR("unexpected protocol family %d\n", ai->ai_family);
636         return 0;
637     }
638
639     freeaddrinfo(ai);
640
641     return size;
642 }
643
644 static RPC_STATUS rpcrt4_ncacn_ip_tcp_parse_top_of_tower(const unsigned char *tower_data,
645                                                          size_t tower_size,
646                                                          char **networkaddr,
647                                                          char **endpoint)
648 {
649     const twr_tcp_floor_t *tcp_floor = (const twr_tcp_floor_t *)tower_data;
650     const twr_ipv4_floor_t *ipv4_floor;
651     struct in_addr in_addr;
652
653     TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
654
655     if (tower_size < sizeof(*tcp_floor))
656         return EPT_S_NOT_REGISTERED;
657
658     tower_data += sizeof(*tcp_floor);
659     tower_size -= sizeof(*tcp_floor);
660
661     if (tower_size < sizeof(*ipv4_floor))
662         return EPT_S_NOT_REGISTERED;
663
664     ipv4_floor = (const twr_ipv4_floor_t *)tower_data;
665
666     if ((tcp_floor->count_lhs != sizeof(tcp_floor->protid)) ||
667         (tcp_floor->protid != EPM_PROTOCOL_TCP) ||
668         (tcp_floor->count_rhs != sizeof(tcp_floor->port)) ||
669         (ipv4_floor->count_lhs != sizeof(ipv4_floor->protid)) ||
670         (ipv4_floor->protid != EPM_PROTOCOL_IP) ||
671         (ipv4_floor->count_rhs != sizeof(ipv4_floor->ipv4addr)))
672         return EPT_S_NOT_REGISTERED;
673
674     if (endpoint)
675     {
676         *endpoint = I_RpcAllocate(6 /* sizeof("65535") + 1 */);
677         if (!*endpoint)
678             return RPC_S_OUT_OF_RESOURCES;
679         sprintf(*endpoint, "%u", ntohs(tcp_floor->port));
680     }
681
682     if (networkaddr)
683     {
684         *networkaddr = I_RpcAllocate(INET_ADDRSTRLEN);
685         if (!*networkaddr)
686         {
687             if (endpoint)
688             {
689                 I_RpcFree(*endpoint);
690                 *endpoint = NULL;
691             }
692             return RPC_S_OUT_OF_RESOURCES;
693         }
694         in_addr.s_addr = ipv4_floor->ipv4addr;
695         if (!inet_ntop(AF_INET, &in_addr, *networkaddr, INET_ADDRSTRLEN))
696         {
697             ERR("inet_ntop: %s\n", strerror(errno));
698             I_RpcFree(*networkaddr);
699             *networkaddr = NULL;
700             if (endpoint)
701             {
702                 I_RpcFree(*endpoint);
703                 *endpoint = NULL;
704             }
705             return EPT_S_NOT_REGISTERED;
706         }
707     }
708
709     return RPC_S_OK;
710 }
711
712 static const struct protseq_ops protseq_list[] = {
713   { "ncacn_np",
714     { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SMB },
715     rpcrt4_conn_np_alloc,
716     rpcrt4_ncacn_np_open,
717     rpcrt4_conn_np_get_connect_event,
718     rpcrt4_conn_np_handoff,
719     rpcrt4_conn_np_read,
720     rpcrt4_conn_np_write,
721     rpcrt4_conn_np_close,
722     rpcrt4_ncacn_np_get_top_of_tower,
723     rpcrt4_ncacn_np_parse_top_of_tower,
724   },
725   { "ncalrpc",
726     { EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_PIPE },
727     rpcrt4_conn_np_alloc,
728     rpcrt4_ncalrpc_open,
729     rpcrt4_conn_np_get_connect_event,
730     rpcrt4_conn_np_handoff,
731     rpcrt4_conn_np_read,
732     rpcrt4_conn_np_write,
733     rpcrt4_conn_np_close,
734     rpcrt4_ncalrpc_get_top_of_tower,
735     rpcrt4_ncalrpc_parse_top_of_tower,
736   },
737   { "ncacn_ip_tcp",
738     { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_TCP },
739     rpcrt4_conn_tcp_alloc,
740     rpcrt4_ncacn_ip_tcp_open,
741     rpcrt4_conn_tcp_get_wait_handle,
742     rpcrt4_conn_tcp_handoff,
743     rpcrt4_conn_tcp_read,
744     rpcrt4_conn_tcp_write,
745     rpcrt4_conn_tcp_close,
746     rpcrt4_ncacn_ip_tcp_get_top_of_tower,
747     rpcrt4_ncacn_ip_tcp_parse_top_of_tower,
748   }
749 };
750
751 #define MAX_PROTSEQ (sizeof protseq_list / sizeof protseq_list[0])
752
753 static const struct protseq_ops *rpcrt4_get_protseq_ops(const char *protseq)
754 {
755   int i;
756   for(i=0; i<MAX_PROTSEQ; i++)
757     if (!strcmp(protseq_list[i].name, protseq))
758       return &protseq_list[i];
759   return NULL;
760 }
761
762 /**** interface to rest of code ****/
763
764 RPC_STATUS RPCRT4_OpenConnection(RpcConnection* Connection)
765 {
766   TRACE("(Connection == ^%p)\n", Connection);
767
768   return Connection->ops->open_connection(Connection);
769 }
770
771 RPC_STATUS RPCRT4_CloseConnection(RpcConnection* Connection)
772 {
773   TRACE("(Connection == ^%p)\n", Connection);
774   rpcrt4_conn_close(Connection);
775   return RPC_S_OK;
776 }
777
778 RPC_STATUS RPCRT4_CreateConnection(RpcConnection** Connection, BOOL server,
779     LPCSTR Protseq, LPCSTR NetworkAddr, LPCSTR Endpoint,
780     LPCSTR NetworkOptions, RpcAuthInfo* AuthInfo, RpcBinding* Binding)
781 {
782   const struct protseq_ops *ops;
783   RpcConnection* NewConnection;
784
785   ops = rpcrt4_get_protseq_ops(Protseq);
786   if (!ops)
787     return RPC_S_PROTSEQ_NOT_SUPPORTED;
788
789   NewConnection = ops->alloc();
790   NewConnection->Next = NULL;
791   NewConnection->server = server;
792   NewConnection->ops = ops;
793   NewConnection->NetworkAddr = RPCRT4_strdupA(NetworkAddr);
794   NewConnection->Endpoint = RPCRT4_strdupA(Endpoint);
795   NewConnection->Used = Binding;
796   NewConnection->MaxTransmissionSize = RPC_MAX_PACKET_SIZE;
797   memset(&NewConnection->ActiveInterface, 0, sizeof(NewConnection->ActiveInterface));
798   NewConnection->NextCallId = 1;
799
800   memset(&NewConnection->ctx, 0, sizeof(NewConnection->ctx));
801   if (AuthInfo) RpcAuthInfo_AddRef(AuthInfo);
802   NewConnection->AuthInfo = AuthInfo;
803   list_init(&NewConnection->conn_pool_entry);
804
805   TRACE("connection: %p\n", NewConnection);
806   *Connection = NewConnection;
807
808   return RPC_S_OK;
809 }
810
811 RpcConnection *RPCRT4_GetIdleConnection(const RPC_SYNTAX_IDENTIFIER *InterfaceId,
812     const RPC_SYNTAX_IDENTIFIER *TransferSyntax, LPCSTR Protseq, LPCSTR NetworkAddr,
813     LPCSTR Endpoint, RpcAuthInfo* AuthInfo)
814 {
815   RpcConnection *Connection;
816   /* try to find a compatible connection from the connection pool */
817   EnterCriticalSection(&connection_pool_cs);
818   LIST_FOR_EACH_ENTRY(Connection, &connection_pool, RpcConnection, conn_pool_entry)
819     if ((Connection->AuthInfo == AuthInfo) &&
820         !memcmp(&Connection->ActiveInterface, InterfaceId,
821            sizeof(RPC_SYNTAX_IDENTIFIER)) &&
822         !strcmp(rpcrt4_conn_get_name(Connection), Protseq) &&
823         !strcmp(Connection->NetworkAddr, NetworkAddr) &&
824         !strcmp(Connection->Endpoint, Endpoint))
825     {
826       list_remove(&Connection->conn_pool_entry);
827       LeaveCriticalSection(&connection_pool_cs);
828       TRACE("got connection from pool %p\n", Connection);
829       return Connection;
830     }
831
832   LeaveCriticalSection(&connection_pool_cs);
833   return NULL;
834 }
835
836 void RPCRT4_ReleaseIdleConnection(RpcConnection *Connection)
837 {
838   assert(!Connection->server);
839   EnterCriticalSection(&connection_pool_cs);
840   list_add_head(&connection_pool, &Connection->conn_pool_entry);
841   LeaveCriticalSection(&connection_pool_cs);
842 }
843
844
845 RPC_STATUS RPCRT4_SpawnConnection(RpcConnection** Connection, RpcConnection* OldConnection)
846 {
847   RPC_STATUS err;
848
849   err = RPCRT4_CreateConnection(Connection, OldConnection->server,
850                                 rpcrt4_conn_get_name(OldConnection),
851                                 OldConnection->NetworkAddr,
852                                 OldConnection->Endpoint, NULL,
853                                 OldConnection->AuthInfo, NULL);
854   if (err == RPC_S_OK)
855     rpcrt4_conn_handoff(OldConnection, *Connection);
856   return err;
857 }
858
859 RPC_STATUS RPCRT4_DestroyConnection(RpcConnection* Connection)
860 {
861   TRACE("connection: %p\n", Connection);
862
863   RPCRT4_CloseConnection(Connection);
864   RPCRT4_strfree(Connection->Endpoint);
865   RPCRT4_strfree(Connection->NetworkAddr);
866   if (Connection->AuthInfo) RpcAuthInfo_Release(Connection->AuthInfo);
867   HeapFree(GetProcessHeap(), 0, Connection);
868   return RPC_S_OK;
869 }
870
871 RPC_STATUS RpcTransport_GetTopOfTower(unsigned char *tower_data,
872                                       size_t *tower_size,
873                                       const char *protseq,
874                                       const char *networkaddr,
875                                       const char *endpoint)
876 {
877     twr_empty_floor_t *protocol_floor;
878     const struct protseq_ops *protseq_ops = rpcrt4_get_protseq_ops(protseq);
879
880     *tower_size = 0;
881
882     if (!protseq_ops)
883         return RPC_S_INVALID_RPC_PROTSEQ;
884
885     if (!tower_data)
886     {
887         *tower_size = sizeof(*protocol_floor);
888         *tower_size += protseq_ops->get_top_of_tower(NULL, networkaddr, endpoint);
889         return RPC_S_OK;
890     }
891
892     protocol_floor = (twr_empty_floor_t *)tower_data;
893     protocol_floor->count_lhs = sizeof(protocol_floor->protid);
894     protocol_floor->protid = protseq_ops->epm_protocols[0];
895     protocol_floor->count_rhs = 0;
896
897     tower_data += sizeof(*protocol_floor);
898
899     *tower_size = protseq_ops->get_top_of_tower(tower_data, networkaddr, endpoint);
900     if (!*tower_size)
901         return EPT_S_NOT_REGISTERED;
902
903     *tower_size += sizeof(*protocol_floor);
904
905     return RPC_S_OK;
906 }
907
908 RPC_STATUS RpcTransport_ParseTopOfTower(const unsigned char *tower_data,
909                                         size_t tower_size,
910                                         char **protseq,
911                                         char **networkaddr,
912                                         char **endpoint)
913 {
914     const twr_empty_floor_t *protocol_floor;
915     const twr_empty_floor_t *floor4;
916     const struct protseq_ops *protseq_ops = NULL;
917     RPC_STATUS status;
918     int i;
919
920     if (tower_size < sizeof(*protocol_floor))
921         return EPT_S_NOT_REGISTERED;
922
923     protocol_floor = (const twr_empty_floor_t *)tower_data;
924     tower_data += sizeof(*protocol_floor);
925     tower_size -= sizeof(*protocol_floor);
926     if ((protocol_floor->count_lhs != sizeof(protocol_floor->protid)) ||
927         (protocol_floor->count_rhs > tower_size))
928         return EPT_S_NOT_REGISTERED;
929     tower_data += protocol_floor->count_rhs;
930     tower_size -= protocol_floor->count_rhs;
931
932     floor4 = (const twr_empty_floor_t *)tower_data;
933     if ((tower_size < sizeof(*floor4)) ||
934         (floor4->count_lhs != sizeof(floor4->protid)))
935         return EPT_S_NOT_REGISTERED;
936
937     for(i = 0; i < MAX_PROTSEQ; i++)
938         if ((protocol_floor->protid == protseq_list[i].epm_protocols[0]) &&
939             (floor4->protid == protseq_list[i].epm_protocols[1]))
940         {
941             protseq_ops = &protseq_list[i];
942             break;
943         }
944
945     if (!protseq_ops)
946         return EPT_S_NOT_REGISTERED;
947
948     status = protseq_ops->parse_top_of_tower(tower_data, tower_size, networkaddr, endpoint);
949
950     if ((status == RPC_S_OK) && protseq)
951     {
952         *protseq = I_RpcAllocate(strlen(protseq_ops->name) + 1);
953         strcpy(*protseq, protseq_ops->name);
954     }
955
956     return status;
957 }
958
959 /***********************************************************************
960  *             RpcNetworkIsProtseqValidW (RPCRT4.@)
961  *
962  * Checks if the given protocol sequence is known by the RPC system.
963  * If it is, returns RPC_S_OK, otherwise RPC_S_PROTSEQ_NOT_SUPPORTED.
964  *
965  */
966 RPC_STATUS WINAPI RpcNetworkIsProtseqValidW(LPWSTR protseq)
967 {
968   char ps[0x10];
969
970   WideCharToMultiByte(CP_ACP, 0, protseq, -1,
971                       ps, sizeof ps, NULL, NULL);
972   if (rpcrt4_get_protseq_ops(ps))
973     return RPC_S_OK;
974
975   FIXME("Unknown protseq %s\n", debugstr_w(protseq));
976
977   return RPC_S_INVALID_RPC_PROTSEQ;
978 }
979
980 /***********************************************************************
981  *             RpcNetworkIsProtseqValidA (RPCRT4.@)
982  */
983 RPC_STATUS WINAPI RpcNetworkIsProtseqValidA(unsigned char *protseq)
984 {
985   UNICODE_STRING protseqW;
986
987   if (RtlCreateUnicodeStringFromAsciiz(&protseqW, (char*)protseq))
988   {
989     RPC_STATUS ret = RpcNetworkIsProtseqValidW(protseqW.Buffer);
990     RtlFreeUnicodeString(&protseqW);
991     return ret;
992   }
993   return RPC_S_OUT_OF_MEMORY;
994 }