The real, definitive, Visual C++ support branch. Accept no substitutes
[reactos.git] / dll / win32 / 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 * Copyright 2006 Damjan Jovanovic
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 *
24 */
25
26 #include "config.h"
27
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <assert.h>
32 #include <errno.h>
33
34 #ifdef HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
37 #include <fcntl.h>
38 #include <stdlib.h>
39 #include <sys/types.h>
40 #ifdef HAVE_SYS_SOCKET_H
41 # include <sys/socket.h>
42 #endif
43 #ifdef HAVE_NETINET_IN_H
44 # include <netinet/in.h>
45 #endif
46 #ifdef HAVE_NETINET_TCP_H
47 # include <netinet/tcp.h>
48 #endif
49 #ifdef HAVE_ARPA_INET_H
50 # include <arpa/inet.h>
51 #endif
52 #ifdef HAVE_NETDB_H
53 #include <netdb.h>
54 #endif
55 #ifdef HAVE_SYS_POLL_H
56 #include <sys/poll.h>
57 #endif
58
59 #include <winsock2.h>
60 #include <ws2tcpip.h>
61
62 #include "windef.h"
63 #include "winbase.h"
64 #include "winnls.h"
65 #include "winerror.h"
66 #include "winternl.h"
67 #include "wine/unicode.h"
68
69 #include "rpc.h"
70 #include "rpcndr.h"
71
72 #include "wine/debug.h"
73
74 #include "rpc_binding.h"
75 #include "rpc_message.h"
76 #include "rpc_server.h"
77 #include "epm_towers.h"
78
79 #include "unix_func.h"
80
81 #ifndef SOL_TCP
82 # define SOL_TCP IPPROTO_TCP
83 #endif
84
85 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
86
87 /**** ncacn_np support ****/
88
89 typedef struct _RpcConnection_np
90 {
91 RpcConnection common;
92 HANDLE pipe;
93 OVERLAPPED ovl[2];
94 BOOL listening;
95 } RpcConnection_np;
96
97 static RpcConnection *rpcrt4_conn_np_alloc(void)
98 {
99 RpcConnection_np *npc = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_np));
100 if (npc)
101 {
102 npc->pipe = NULL;
103 memset(&npc->ovl, 0, sizeof(npc->ovl));
104 npc->listening = FALSE;
105 }
106 return &npc->common;
107 }
108
109 static RPC_STATUS rpcrt4_conn_listen_pipe(RpcConnection_np *npc)
110 {
111 if (npc->listening)
112 return RPC_S_OK;
113
114 npc->listening = TRUE;
115 if (ConnectNamedPipe(npc->pipe, &npc->ovl[0]))
116 return RPC_S_OK;
117
118 if (GetLastError() == ERROR_PIPE_CONNECTED) {
119 SetEvent(npc->ovl[0].hEvent);
120 return RPC_S_OK;
121 }
122 if (GetLastError() == ERROR_IO_PENDING) {
123 /* will be completed in rpcrt4_protseq_np_wait_for_new_connection */
124 return RPC_S_OK;
125 }
126 npc->listening = FALSE;
127 WARN("Couldn't ConnectNamedPipe (error was %d)\n", GetLastError());
128 return RPC_S_OUT_OF_RESOURCES;
129 }
130
131 static RPC_STATUS rpcrt4_conn_create_pipe(RpcConnection *Connection, LPCSTR pname)
132 {
133 RpcConnection_np *npc = (RpcConnection_np *) Connection;
134 TRACE("listening on %s\n", pname);
135
136 npc->pipe = CreateNamedPipeA(pname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
137 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE,
138 PIPE_UNLIMITED_INSTANCES,
139 RPC_MAX_PACKET_SIZE, RPC_MAX_PACKET_SIZE, 5000, NULL);
140 if (npc->pipe == INVALID_HANDLE_VALUE) {
141 WARN("CreateNamedPipe failed with error %d\n", GetLastError());
142 if (GetLastError() == ERROR_FILE_EXISTS)
143 return RPC_S_DUPLICATE_ENDPOINT;
144 else
145 return RPC_S_CANT_CREATE_ENDPOINT;
146 }
147
148 memset(&npc->ovl, 0, sizeof(npc->ovl));
149 npc->ovl[0].hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
150 npc->ovl[1].hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
151
152 /* Note: we don't call ConnectNamedPipe here because it must be done in the
153 * server thread as the thread must be alertable */
154 return RPC_S_OK;
155 }
156
157 static RPC_STATUS rpcrt4_conn_open_pipe(RpcConnection *Connection, LPCSTR pname, BOOL wait)
158 {
159 RpcConnection_np *npc = (RpcConnection_np *) Connection;
160 HANDLE pipe;
161 DWORD err, dwMode;
162
163 TRACE("connecting to %s\n", pname);
164
165 while (TRUE) {
166 DWORD dwFlags = 0;
167 if (Connection->QOS)
168 {
169 dwFlags = SECURITY_SQOS_PRESENT;
170 switch (Connection->QOS->qos->ImpersonationType)
171 {
172 case RPC_C_IMP_LEVEL_DEFAULT:
173 /* FIXME: what to do here? */
174 break;
175 case RPC_C_IMP_LEVEL_ANONYMOUS:
176 dwFlags |= SECURITY_ANONYMOUS;
177 break;
178 case RPC_C_IMP_LEVEL_IDENTIFY:
179 dwFlags |= SECURITY_IDENTIFICATION;
180 break;
181 case RPC_C_IMP_LEVEL_IMPERSONATE:
182 dwFlags |= SECURITY_IMPERSONATION;
183 break;
184 case RPC_C_IMP_LEVEL_DELEGATE:
185 dwFlags |= SECURITY_DELEGATION;
186 break;
187 }
188 if (Connection->QOS->qos->IdentityTracking == RPC_C_QOS_IDENTIFY_DYNAMIC)
189 dwFlags |= SECURITY_CONTEXT_TRACKING;
190 }
191 pipe = CreateFileA(pname, GENERIC_READ|GENERIC_WRITE, 0, NULL,
192 OPEN_EXISTING, dwFlags, 0);
193 if (pipe != INVALID_HANDLE_VALUE) break;
194 err = GetLastError();
195 if (err == ERROR_PIPE_BUSY) {
196 TRACE("connection failed, error=%x\n", err);
197 return RPC_S_SERVER_TOO_BUSY;
198 }
199 if (!wait)
200 return RPC_S_SERVER_UNAVAILABLE;
201 if (!WaitNamedPipeA(pname, NMPWAIT_WAIT_FOREVER)) {
202 err = GetLastError();
203 WARN("connection failed, error=%x\n", err);
204 return RPC_S_SERVER_UNAVAILABLE;
205 }
206 }
207
208 /* success */
209 memset(&npc->ovl, 0, sizeof(npc->ovl));
210 /* pipe is connected; change to message-read mode. */
211 dwMode = PIPE_READMODE_MESSAGE;
212 SetNamedPipeHandleState(pipe, &dwMode, NULL, NULL);
213 npc->ovl[0].hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
214 npc->ovl[1].hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
215
216 npc->pipe = pipe;
217
218 return RPC_S_OK;
219 }
220
221 static RPC_STATUS rpcrt4_ncalrpc_open(RpcConnection* Connection)
222 {
223 RpcConnection_np *npc = (RpcConnection_np *) Connection;
224 static const char prefix[] = "\\\\.\\pipe\\lrpc\\";
225 RPC_STATUS r;
226 LPSTR pname;
227
228 /* already connected? */
229 if (npc->pipe)
230 return RPC_S_OK;
231
232 /* protseq=ncalrpc: supposed to use NT LPC ports,
233 * but we'll implement it with named pipes for now */
234 pname = I_RpcAllocate(strlen(prefix) + strlen(Connection->Endpoint) + 1);
235 strcat(strcpy(pname, prefix), Connection->Endpoint);
236 r = rpcrt4_conn_open_pipe(Connection, pname, TRUE);
237 I_RpcFree(pname);
238
239 return r;
240 }
241
242 static RPC_STATUS rpcrt4_protseq_ncalrpc_open_endpoint(RpcServerProtseq* protseq, LPSTR endpoint)
243 {
244 static const char prefix[] = "\\\\.\\pipe\\lrpc\\";
245 RPC_STATUS r;
246 LPSTR pname;
247 RpcConnection *Connection;
248
249 r = RPCRT4_CreateConnection(&Connection, TRUE, protseq->Protseq, NULL,
250 endpoint, NULL, NULL, NULL);
251 if (r != RPC_S_OK)
252 return r;
253
254 /* protseq=ncalrpc: supposed to use NT LPC ports,
255 * but we'll implement it with named pipes for now */
256 pname = I_RpcAllocate(strlen(prefix) + strlen(Connection->Endpoint) + 1);
257 strcat(strcpy(pname, prefix), Connection->Endpoint);
258 r = rpcrt4_conn_create_pipe(Connection, pname);
259 I_RpcFree(pname);
260
261 EnterCriticalSection(&protseq->cs);
262 Connection->Next = protseq->conn;
263 protseq->conn = Connection;
264 LeaveCriticalSection(&protseq->cs);
265
266 return r;
267 }
268
269 static RPC_STATUS rpcrt4_ncacn_np_open(RpcConnection* Connection)
270 {
271 RpcConnection_np *npc = (RpcConnection_np *) Connection;
272 static const char prefix[] = "\\\\.";
273 RPC_STATUS r;
274 LPSTR pname;
275
276 /* already connected? */
277 if (npc->pipe)
278 return RPC_S_OK;
279
280 /* protseq=ncacn_np: named pipes */
281 pname = I_RpcAllocate(strlen(prefix) + strlen(Connection->Endpoint) + 1);
282 strcat(strcpy(pname, prefix), Connection->Endpoint);
283 r = rpcrt4_conn_open_pipe(Connection, pname, FALSE);
284 I_RpcFree(pname);
285
286 return r;
287 }
288
289 static RPC_STATUS rpcrt4_protseq_ncacn_np_open_endpoint(RpcServerProtseq *protseq, LPSTR endpoint)
290 {
291 static const char prefix[] = "\\\\.";
292 RPC_STATUS r;
293 LPSTR pname;
294 RpcConnection *Connection;
295
296 r = RPCRT4_CreateConnection(&Connection, TRUE, protseq->Protseq, NULL,
297 endpoint, NULL, NULL, NULL);
298 if (r != RPC_S_OK)
299 return r;
300
301 /* protseq=ncacn_np: named pipes */
302 pname = I_RpcAllocate(strlen(prefix) + strlen(Connection->Endpoint) + 1);
303 strcat(strcpy(pname, prefix), Connection->Endpoint);
304 r = rpcrt4_conn_create_pipe(Connection, pname);
305 I_RpcFree(pname);
306
307 EnterCriticalSection(&protseq->cs);
308 Connection->Next = protseq->conn;
309 protseq->conn = Connection;
310 LeaveCriticalSection(&protseq->cs);
311
312 return r;
313 }
314
315 static void rpcrt4_conn_np_handoff(RpcConnection_np *old_npc, RpcConnection_np *new_npc)
316 {
317 /* because of the way named pipes work, we'll transfer the connected pipe
318 * to the child, then reopen the server binding to continue listening */
319
320 new_npc->pipe = old_npc->pipe;
321 new_npc->ovl[0] = old_npc->ovl[0];
322 new_npc->ovl[1] = old_npc->ovl[1];
323 old_npc->pipe = 0;
324 memset(&old_npc->ovl, 0, sizeof(old_npc->ovl));
325 old_npc->listening = FALSE;
326 }
327
328 static RPC_STATUS rpcrt4_ncacn_np_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
329 {
330 RPC_STATUS status;
331 LPSTR pname;
332 static const char prefix[] = "\\\\.";
333
334 rpcrt4_conn_np_handoff((RpcConnection_np *)old_conn, (RpcConnection_np *)new_conn);
335
336 pname = I_RpcAllocate(strlen(prefix) + strlen(old_conn->Endpoint) + 1);
337 strcat(strcpy(pname, prefix), old_conn->Endpoint);
338 status = rpcrt4_conn_create_pipe(old_conn, pname);
339 I_RpcFree(pname);
340
341 return status;
342 }
343
344 static RPC_STATUS rpcrt4_ncalrpc_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
345 {
346 RPC_STATUS status;
347 LPSTR pname;
348 static const char prefix[] = "\\\\.\\pipe\\lrpc\\";
349
350 TRACE("%s\n", old_conn->Endpoint);
351
352 rpcrt4_conn_np_handoff((RpcConnection_np *)old_conn, (RpcConnection_np *)new_conn);
353
354 pname = I_RpcAllocate(strlen(prefix) + strlen(old_conn->Endpoint) + 1);
355 strcat(strcpy(pname, prefix), old_conn->Endpoint);
356 status = rpcrt4_conn_create_pipe(old_conn, pname);
357 I_RpcFree(pname);
358
359 return status;
360 }
361
362 static int rpcrt4_conn_np_read(RpcConnection *Connection,
363 void *buffer, unsigned int count)
364 {
365 RpcConnection_np *npc = (RpcConnection_np *) Connection;
366 char *buf = buffer;
367 BOOL ret = TRUE;
368 unsigned int bytes_left = count;
369
370 while (bytes_left)
371 {
372 DWORD bytes_read;
373 ret = ReadFile(npc->pipe, buf, bytes_left, &bytes_read, &npc->ovl[0]);
374 if ((!ret || !bytes_read) && (GetLastError() != ERROR_IO_PENDING))
375 break;
376
377 ret = GetOverlappedResult(npc->pipe, &npc->ovl[0], &bytes_read, TRUE);
378 if (!ret && GetLastError() != ERROR_MORE_DATA)
379 break;
380
381 bytes_left -= bytes_read;
382 buf += bytes_read;
383 }
384 return ret ? count : -1;
385 }
386
387 static int rpcrt4_conn_np_write(RpcConnection *Connection,
388 const void *buffer, unsigned int count)
389 {
390 RpcConnection_np *npc = (RpcConnection_np *) Connection;
391 const char *buf = buffer;
392 BOOL ret = TRUE;
393 unsigned int bytes_left = count;
394
395 while (bytes_left)
396 {
397 DWORD bytes_written;
398 ret = WriteFile(npc->pipe, buf, count, &bytes_written, &npc->ovl[1]);
399 if ((!ret || !bytes_written) && (GetLastError() != ERROR_IO_PENDING))
400 break;
401
402 ret = GetOverlappedResult(npc->pipe, &npc->ovl[1], &bytes_written, TRUE);
403 if (!ret && GetLastError() != ERROR_MORE_DATA)
404 break;
405
406 bytes_left -= bytes_written;
407 buf += bytes_written;
408 }
409 return ret ? count : -1;
410 }
411
412 static int rpcrt4_conn_np_close(RpcConnection *Connection)
413 {
414 RpcConnection_np *npc = (RpcConnection_np *) Connection;
415 if (npc->pipe) {
416 FlushFileBuffers(npc->pipe);
417 CloseHandle(npc->pipe);
418 npc->pipe = 0;
419 }
420 if (npc->ovl[0].hEvent) {
421 CloseHandle(npc->ovl[0].hEvent);
422 npc->ovl[0].hEvent = 0;
423 }
424 if (npc->ovl[1].hEvent) {
425 CloseHandle(npc->ovl[1].hEvent);
426 npc->ovl[1].hEvent = 0;
427 }
428
429 return 0;
430 }
431
432 static void rpcrt4_conn_np_cancel_call(RpcConnection *Connection)
433 {
434 /* FIXME: implement when named pipe writes use overlapped I/O */
435 }
436
437 static int rpcrt4_conn_np_wait_for_incoming_data(RpcConnection *Connection)
438 {
439 /* FIXME: implement when named pipe writes use overlapped I/O */
440 return -1;
441 }
442
443 static size_t rpcrt4_ncacn_np_get_top_of_tower(unsigned char *tower_data,
444 const char *networkaddr,
445 const char *endpoint)
446 {
447 twr_empty_floor_t *smb_floor;
448 twr_empty_floor_t *nb_floor;
449 size_t size;
450 size_t networkaddr_size;
451 size_t endpoint_size;
452
453 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
454
455 networkaddr_size = networkaddr ? strlen(networkaddr) + 1 : 1;
456 endpoint_size = endpoint ? strlen(endpoint) + 1 : 1;
457 size = sizeof(*smb_floor) + endpoint_size + sizeof(*nb_floor) + networkaddr_size;
458
459 if (!tower_data)
460 return size;
461
462 smb_floor = (twr_empty_floor_t *)tower_data;
463
464 tower_data += sizeof(*smb_floor);
465
466 smb_floor->count_lhs = sizeof(smb_floor->protid);
467 smb_floor->protid = EPM_PROTOCOL_SMB;
468 smb_floor->count_rhs = endpoint_size;
469
470 if (endpoint)
471 memcpy(tower_data, endpoint, endpoint_size);
472 else
473 tower_data[0] = 0;
474 tower_data += endpoint_size;
475
476 nb_floor = (twr_empty_floor_t *)tower_data;
477
478 tower_data += sizeof(*nb_floor);
479
480 nb_floor->count_lhs = sizeof(nb_floor->protid);
481 nb_floor->protid = EPM_PROTOCOL_NETBIOS;
482 nb_floor->count_rhs = networkaddr_size;
483
484 if (networkaddr)
485 memcpy(tower_data, networkaddr, networkaddr_size);
486 else
487 tower_data[0] = 0;
488 tower_data += networkaddr_size;
489
490 return size;
491 }
492
493 static RPC_STATUS rpcrt4_ncacn_np_parse_top_of_tower(const unsigned char *tower_data,
494 size_t tower_size,
495 char **networkaddr,
496 char **endpoint)
497 {
498 const twr_empty_floor_t *smb_floor = (const twr_empty_floor_t *)tower_data;
499 const twr_empty_floor_t *nb_floor;
500
501 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
502
503 if (tower_size < sizeof(*smb_floor))
504 return EPT_S_NOT_REGISTERED;
505
506 tower_data += sizeof(*smb_floor);
507 tower_size -= sizeof(*smb_floor);
508
509 if ((smb_floor->count_lhs != sizeof(smb_floor->protid)) ||
510 (smb_floor->protid != EPM_PROTOCOL_SMB) ||
511 (smb_floor->count_rhs > tower_size))
512 return EPT_S_NOT_REGISTERED;
513
514 if (endpoint)
515 {
516 *endpoint = I_RpcAllocate(smb_floor->count_rhs);
517 if (!*endpoint)
518 return RPC_S_OUT_OF_RESOURCES;
519 memcpy(*endpoint, tower_data, smb_floor->count_rhs);
520 }
521 tower_data += smb_floor->count_rhs;
522 tower_size -= smb_floor->count_rhs;
523
524 if (tower_size < sizeof(*nb_floor))
525 return EPT_S_NOT_REGISTERED;
526
527 nb_floor = (const twr_empty_floor_t *)tower_data;
528
529 tower_data += sizeof(*nb_floor);
530 tower_size -= sizeof(*nb_floor);
531
532 if ((nb_floor->count_lhs != sizeof(nb_floor->protid)) ||
533 (nb_floor->protid != EPM_PROTOCOL_NETBIOS) ||
534 (nb_floor->count_rhs > tower_size))
535 return EPT_S_NOT_REGISTERED;
536
537 if (networkaddr)
538 {
539 *networkaddr = I_RpcAllocate(nb_floor->count_rhs);
540 if (!*networkaddr)
541 {
542 if (endpoint)
543 {
544 I_RpcFree(*endpoint);
545 *endpoint = NULL;
546 }
547 return RPC_S_OUT_OF_RESOURCES;
548 }
549 memcpy(*networkaddr, tower_data, nb_floor->count_rhs);
550 }
551
552 return RPC_S_OK;
553 }
554
555 typedef struct _RpcServerProtseq_np
556 {
557 RpcServerProtseq common;
558 HANDLE mgr_event;
559 } RpcServerProtseq_np;
560
561 static RpcServerProtseq *rpcrt4_protseq_np_alloc(void)
562 {
563 RpcServerProtseq_np *ps = HeapAlloc(GetProcessHeap(), 0, sizeof(*ps));
564 if (ps)
565 ps->mgr_event = CreateEventW(NULL, FALSE, FALSE, NULL);
566 return &ps->common;
567 }
568
569 static void rpcrt4_protseq_np_signal_state_changed(RpcServerProtseq *protseq)
570 {
571 RpcServerProtseq_np *npps = CONTAINING_RECORD(protseq, RpcServerProtseq_np, common);
572 SetEvent(npps->mgr_event);
573 }
574
575 static void *rpcrt4_protseq_np_get_wait_array(RpcServerProtseq *protseq, void *prev_array, unsigned int *count)
576 {
577 HANDLE *objs = prev_array;
578 RpcConnection_np *conn;
579 RpcServerProtseq_np *npps = CONTAINING_RECORD(protseq, RpcServerProtseq_np, common);
580
581 EnterCriticalSection(&protseq->cs);
582
583 /* open and count connections */
584 *count = 1;
585 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
586 while (conn) {
587 rpcrt4_conn_listen_pipe(conn);
588 if (conn->ovl[0].hEvent)
589 (*count)++;
590 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
591 }
592
593 /* make array of connections */
594 if (objs)
595 objs = HeapReAlloc(GetProcessHeap(), 0, objs, *count*sizeof(HANDLE));
596 else
597 objs = HeapAlloc(GetProcessHeap(), 0, *count*sizeof(HANDLE));
598 if (!objs)
599 {
600 ERR("couldn't allocate objs\n");
601 LeaveCriticalSection(&protseq->cs);
602 return NULL;
603 }
604
605 objs[0] = npps->mgr_event;
606 *count = 1;
607 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
608 while (conn) {
609 if ((objs[*count] = conn->ovl[0].hEvent))
610 (*count)++;
611 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
612 }
613 LeaveCriticalSection(&protseq->cs);
614 return objs;
615 }
616
617 static void rpcrt4_protseq_np_free_wait_array(RpcServerProtseq *protseq, void *array)
618 {
619 HeapFree(GetProcessHeap(), 0, array);
620 }
621
622 static int rpcrt4_protseq_np_wait_for_new_connection(RpcServerProtseq *protseq, unsigned int count, void *wait_array)
623 {
624 HANDLE b_handle;
625 HANDLE *objs = wait_array;
626 DWORD res;
627 RpcConnection *cconn;
628 RpcConnection_np *conn;
629
630 if (!objs)
631 return -1;
632
633 do
634 {
635 /* an alertable wait isn't strictly necessary, but due to our
636 * overlapped I/O implementation in Wine we need to free some memory
637 * by the file user APC being called, even if no completion routine was
638 * specified at the time of starting the async operation */
639 res = WaitForMultipleObjectsEx(count, objs, FALSE, INFINITE, TRUE);
640 } while (res == WAIT_IO_COMPLETION);
641
642 if (res == WAIT_OBJECT_0)
643 return 0;
644 else if (res == WAIT_FAILED)
645 {
646 ERR("wait failed with error %d\n", GetLastError());
647 return -1;
648 }
649 else
650 {
651 b_handle = objs[res - WAIT_OBJECT_0];
652 /* find which connection got a RPC */
653 EnterCriticalSection(&protseq->cs);
654 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
655 while (conn) {
656 if (b_handle == conn->ovl[0].hEvent) break;
657 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
658 }
659 cconn = NULL;
660 if (conn)
661 RPCRT4_SpawnConnection(&cconn, &conn->common);
662 else
663 ERR("failed to locate connection for handle %p\n", b_handle);
664 LeaveCriticalSection(&protseq->cs);
665 if (cconn)
666 {
667 RPCRT4_new_client(cconn);
668 return 1;
669 }
670 else return -1;
671 }
672 }
673
674 static size_t rpcrt4_ncalrpc_get_top_of_tower(unsigned char *tower_data,
675 const char *networkaddr,
676 const char *endpoint)
677 {
678 twr_empty_floor_t *pipe_floor;
679 size_t size;
680 size_t endpoint_size;
681
682 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
683
684 endpoint_size = strlen(networkaddr) + 1;
685 size = sizeof(*pipe_floor) + endpoint_size;
686
687 if (!tower_data)
688 return size;
689
690 pipe_floor = (twr_empty_floor_t *)tower_data;
691
692 tower_data += sizeof(*pipe_floor);
693
694 pipe_floor->count_lhs = sizeof(pipe_floor->protid);
695 pipe_floor->protid = EPM_PROTOCOL_SMB;
696 pipe_floor->count_rhs = endpoint_size;
697
698 memcpy(tower_data, endpoint, endpoint_size);
699 tower_data += endpoint_size;
700
701 return size;
702 }
703
704 static RPC_STATUS rpcrt4_ncalrpc_parse_top_of_tower(const unsigned char *tower_data,
705 size_t tower_size,
706 char **networkaddr,
707 char **endpoint)
708 {
709 const twr_empty_floor_t *pipe_floor = (const twr_empty_floor_t *)tower_data;
710
711 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
712
713 *networkaddr = NULL;
714 *endpoint = NULL;
715
716 if (tower_size < sizeof(*pipe_floor))
717 return EPT_S_NOT_REGISTERED;
718
719 tower_data += sizeof(*pipe_floor);
720 tower_size -= sizeof(*pipe_floor);
721
722 if ((pipe_floor->count_lhs != sizeof(pipe_floor->protid)) ||
723 (pipe_floor->protid != EPM_PROTOCOL_SMB) ||
724 (pipe_floor->count_rhs > tower_size))
725 return EPT_S_NOT_REGISTERED;
726
727 if (endpoint)
728 {
729 *endpoint = I_RpcAllocate(pipe_floor->count_rhs);
730 if (!*endpoint)
731 return RPC_S_OUT_OF_RESOURCES;
732 memcpy(*endpoint, tower_data, pipe_floor->count_rhs);
733 }
734
735 return RPC_S_OK;
736 }
737
738 /**** ncacn_ip_tcp support ****/
739
740 typedef struct _RpcConnection_tcp
741 {
742 RpcConnection common;
743 int sock;
744 int cancel_fds[2];
745 } RpcConnection_tcp;
746
747 static RpcConnection *rpcrt4_conn_tcp_alloc(void)
748 {
749 RpcConnection_tcp *tcpc;
750 tcpc = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_tcp));
751 if (tcpc == NULL)
752 return NULL;
753 tcpc->sock = -1;
754 #ifndef __REACTOS__
755 if (socketpair(PF_UNIX, SOCK_STREAM, 0, tcpc->cancel_fds) < 0)
756 {
757 ERR("socketpair() failed: %s\n", strerror(errno));
758 HeapFree(GetProcessHeap(), 0, tcpc);
759 return NULL;
760 }
761 #endif
762 return &tcpc->common;
763 }
764
765 static RPC_STATUS rpcrt4_ncacn_ip_tcp_open(RpcConnection* Connection)
766 {
767 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
768 int sock;
769 int ret;
770 struct addrinfo *ai;
771 struct addrinfo *ai_cur;
772 struct addrinfo hints;
773
774 TRACE("(%s, %s)\n", Connection->NetworkAddr, Connection->Endpoint);
775
776 if (tcpc->sock != -1)
777 return RPC_S_OK;
778
779 hints.ai_flags = 0;
780 hints.ai_family = PF_UNSPEC;
781 hints.ai_socktype = SOCK_STREAM;
782 hints.ai_protocol = IPPROTO_TCP;
783 hints.ai_addrlen = 0;
784 hints.ai_addr = NULL;
785 hints.ai_canonname = NULL;
786 hints.ai_next = NULL;
787
788 ret = getaddrinfo(Connection->NetworkAddr, Connection->Endpoint, &hints, &ai);
789 if (ret)
790 {
791 ERR("getaddrinfo for %s:%s failed: %s\n", Connection->NetworkAddr,
792 Connection->Endpoint, gai_strerror(ret));
793 return RPC_S_SERVER_UNAVAILABLE;
794 }
795
796 for (ai_cur = ai; ai_cur; ai_cur = ai_cur->ai_next)
797 {
798 int val;
799
800 if (TRACE_ON(rpc))
801 {
802 char host[256];
803 char service[256];
804 getnameinfo(ai_cur->ai_addr, ai_cur->ai_addrlen,
805 host, sizeof(host), service, sizeof(service),
806 NI_NUMERICHOST | NI_NUMERICSERV);
807 TRACE("trying %s:%s\n", host, service);
808 }
809
810 sock = socket(ai_cur->ai_family, ai_cur->ai_socktype, ai_cur->ai_protocol);
811 if (sock == -1)
812 {
813 WARN("socket() failed: %s\n", strerror(errno));
814 continue;
815 }
816
817 if (0>connect(sock, ai_cur->ai_addr, ai_cur->ai_addrlen))
818 {
819 WARN("connect() failed: %s\n", strerror(errno));
820 close(sock);
821 continue;
822 }
823
824 /* RPC depends on having minimal latency so disable the Nagle algorithm */
825 val = 1;
826 setsockopt(sock, SOL_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
827
828 tcpc->sock = sock;
829
830 freeaddrinfo(ai);
831 TRACE("connected\n");
832 return RPC_S_OK;
833 }
834
835 freeaddrinfo(ai);
836 ERR("couldn't connect to %s:%s\n", Connection->NetworkAddr, Connection->Endpoint);
837 return RPC_S_SERVER_UNAVAILABLE;
838 }
839
840 static RPC_STATUS rpcrt4_protseq_ncacn_ip_tcp_open_endpoint(RpcServerProtseq *protseq, LPSTR endpoint)
841 {
842 RPC_STATUS status = RPC_S_CANT_CREATE_ENDPOINT;
843 int sock;
844 int ret;
845 struct addrinfo *ai;
846 struct addrinfo *ai_cur;
847 struct addrinfo hints;
848 u_long blocking;
849 RpcConnection *first_connection = NULL;
850
851 TRACE("(%p, %s)\n", protseq, endpoint);
852
853 hints.ai_flags = AI_PASSIVE /* for non-localhost addresses */;
854 hints.ai_family = PF_UNSPEC;
855 hints.ai_socktype = SOCK_STREAM;
856 hints.ai_protocol = IPPROTO_TCP;
857 hints.ai_addrlen = 0;
858 hints.ai_addr = NULL;
859 hints.ai_canonname = NULL;
860 hints.ai_next = NULL;
861
862 ret = getaddrinfo(NULL, endpoint, &hints, &ai);
863 if (ret)
864 {
865 ERR("getaddrinfo for port %s failed: %s\n", endpoint,
866 gai_strerror(ret));
867 if ((ret == EAI_SERVICE) || (ret == EAI_NONAME))
868 return RPC_S_INVALID_ENDPOINT_FORMAT;
869 return RPC_S_CANT_CREATE_ENDPOINT;
870 }
871
872 for (ai_cur = ai; ai_cur; ai_cur = ai_cur->ai_next)
873 {
874 RpcConnection_tcp *tcpc;
875 RPC_STATUS create_status;
876
877 if (TRACE_ON(rpc))
878 {
879 char host[256];
880 char service[256];
881 getnameinfo(ai_cur->ai_addr, ai_cur->ai_addrlen,
882 host, sizeof(host), service, sizeof(service),
883 NI_NUMERICHOST | NI_NUMERICSERV);
884 TRACE("trying %s:%s\n", host, service);
885 }
886
887 sock = socket(ai_cur->ai_family, ai_cur->ai_socktype, ai_cur->ai_protocol);
888 if (sock == -1)
889 {
890 WARN("socket() failed: %s\n", strerror(errno));
891 status = RPC_S_CANT_CREATE_ENDPOINT;
892 continue;
893 }
894
895 ret = bind(sock, ai_cur->ai_addr, ai_cur->ai_addrlen);
896 if (ret < 0)
897 {
898 WARN("bind failed: %s\n", strerror(errno));
899 close(sock);
900 if (errno == WSAEADDRINUSE)
901 status = RPC_S_DUPLICATE_ENDPOINT;
902 else
903 status = RPC_S_CANT_CREATE_ENDPOINT;
904 continue;
905 }
906 create_status = RPCRT4_CreateConnection((RpcConnection **)&tcpc, TRUE,
907 protseq->Protseq, NULL,
908 endpoint, NULL, NULL, NULL);
909 if (create_status != RPC_S_OK)
910 {
911 close(sock);
912 status = create_status;
913 continue;
914 }
915
916 tcpc->sock = sock;
917 ret = listen(sock, protseq->MaxCalls);
918 if (ret < 0)
919 {
920 WARN("listen failed: %s\n", strerror(errno));
921 RPCRT4_DestroyConnection(&tcpc->common);
922 status = RPC_S_OUT_OF_RESOURCES;
923 continue;
924 }
925 /* need a non-blocking socket, otherwise accept() has a potential
926 * race-condition (poll() says it is readable, connection drops,
927 * and accept() blocks until the next connection comes...)
928 */
929 blocking = 1;
930 ret = ioctlsocket(sock, FIONBIO, &blocking);
931 if (ret < 0)
932 {
933 WARN("couldn't make socket non-blocking, error %d\n", ret);
934 RPCRT4_DestroyConnection(&tcpc->common);
935 status = RPC_S_OUT_OF_RESOURCES;
936 continue;
937 }
938
939 tcpc->common.Next = first_connection;
940 first_connection = &tcpc->common;
941 }
942
943 freeaddrinfo(ai);
944
945 /* if at least one connection was created for an endpoint then
946 * return success */
947 if (first_connection)
948 {
949 RpcConnection *conn;
950
951 /* find last element in list */
952 for (conn = first_connection; conn->Next; conn = conn->Next)
953 ;
954
955 EnterCriticalSection(&protseq->cs);
956 conn->Next = protseq->conn;
957 protseq->conn = first_connection;
958 LeaveCriticalSection(&protseq->cs);
959
960 TRACE("listening on %s\n", endpoint);
961 return RPC_S_OK;
962 }
963
964 ERR("couldn't listen on port %s\n", endpoint);
965 return status;
966 }
967
968 static RPC_STATUS rpcrt4_conn_tcp_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
969 {
970 int ret;
971 struct sockaddr_in address;
972 socklen_t addrsize;
973 u_long blocking;
974 RpcConnection_tcp *server = (RpcConnection_tcp*) old_conn;
975 RpcConnection_tcp *client = (RpcConnection_tcp*) new_conn;
976
977 addrsize = sizeof(address);
978 ret = accept(server->sock, (struct sockaddr*) &address, &addrsize);
979 if (ret < 0)
980 {
981 ERR("Failed to accept a TCP connection: error %d\n", ret);
982 return RPC_S_OUT_OF_RESOURCES;
983 }
984 /* reset to blocking behaviour */
985 blocking = 0;
986 ret = ioctlsocket(ret, FIONBIO, &blocking);
987 client->sock = ret;
988 TRACE("Accepted a new TCP connection\n");
989 return RPC_S_OK;
990 }
991
992 static int rpcrt4_conn_tcp_read(RpcConnection *Connection,
993 void *buffer, unsigned int count)
994 {
995 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
996 int bytes_read = 0;
997 do
998 {
999 int r = recv(tcpc->sock, (char *)buffer + bytes_read, count - bytes_read, 0);
1000 if (!r)
1001 return -1;
1002 else if (r > 0)
1003 bytes_read += r;
1004 else if (errno != EAGAIN)
1005 {
1006 WARN("recv() failed: %s\n", strerror(errno));
1007 return -1;
1008 }
1009 else
1010 {
1011 struct pollfd pfds[2];
1012 pfds[0].fd = tcpc->sock;
1013 pfds[0].events = POLLIN;
1014 pfds[1].fd = tcpc->cancel_fds[0];
1015 pfds[1].events = POLLIN;
1016 if (poll(pfds, 2, -1 /* infinite */) == -1 && errno != EINTR)
1017 {
1018 ERR("poll() failed: %s\n", strerror(errno));
1019 return -1;
1020 }
1021 if (pfds[1].revents & POLLIN) /* canceled */
1022 {
1023 char dummy;
1024 read(pfds[1].fd, &dummy, sizeof(dummy));
1025 return -1;
1026 }
1027 }
1028 } while (bytes_read != count);
1029 TRACE("%d %p %u -> %d\n", tcpc->sock, buffer, count, bytes_read);
1030 return bytes_read;
1031 }
1032
1033 static int rpcrt4_conn_tcp_write(RpcConnection *Connection,
1034 const void *buffer, unsigned int count)
1035 {
1036 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1037 int bytes_written = 0;
1038 do
1039 {
1040 int r = send(tcpc->sock, (const char *)buffer + bytes_written, count - bytes_written, 0);
1041 if (r >= 0)
1042 bytes_written += r;
1043 else if (errno != EAGAIN)
1044 return -1;
1045 else
1046 {
1047 struct pollfd pfd;
1048 pfd.fd = tcpc->sock;
1049 pfd.events = POLLOUT;
1050 if (poll(&pfd, 1, -1 /* infinite */) == -1 && errno != EINTR)
1051 {
1052 ERR("poll() failed: %s\n", strerror(errno));
1053 return -1;
1054 }
1055 }
1056 } while (bytes_written != count);
1057 TRACE("%d %p %u -> %d\n", tcpc->sock, buffer, count, bytes_written);
1058 return bytes_written;
1059 }
1060
1061 static int rpcrt4_conn_tcp_close(RpcConnection *Connection)
1062 {
1063 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1064
1065 TRACE("%d\n", tcpc->sock);
1066
1067 if (tcpc->sock != -1)
1068 close(tcpc->sock);
1069 tcpc->sock = -1;
1070 close(tcpc->cancel_fds[0]);
1071 close(tcpc->cancel_fds[1]);
1072 return 0;
1073 }
1074
1075 static void rpcrt4_conn_tcp_cancel_call(RpcConnection *Connection)
1076 {
1077 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1078 char dummy = 1;
1079
1080 TRACE("%p\n", Connection);
1081
1082 write(tcpc->cancel_fds[1], &dummy, 1);
1083 }
1084
1085 static int rpcrt4_conn_tcp_wait_for_incoming_data(RpcConnection *Connection)
1086 {
1087 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1088 struct pollfd pfds[2];
1089
1090 TRACE("%p\n", Connection);
1091
1092 pfds[0].fd = tcpc->sock;
1093 pfds[0].events = POLLIN;
1094 pfds[1].fd = tcpc->cancel_fds[0];
1095 pfds[1].events = POLLIN;
1096 if (poll(pfds, 2, -1 /* infinite */) == -1 && errno != EINTR)
1097 {
1098 ERR("poll() failed: %s\n", strerror(errno));
1099 return -1;
1100 }
1101 if (pfds[1].revents & POLLIN) /* canceled */
1102 {
1103 char dummy;
1104 read(pfds[1].fd, &dummy, sizeof(dummy));
1105 return -1;
1106 }
1107
1108 return 0;
1109 }
1110
1111 static size_t rpcrt4_ncacn_ip_tcp_get_top_of_tower(unsigned char *tower_data,
1112 const char *networkaddr,
1113 const char *endpoint)
1114 {
1115 twr_tcp_floor_t *tcp_floor;
1116 twr_ipv4_floor_t *ipv4_floor;
1117 struct addrinfo *ai;
1118 struct addrinfo hints;
1119 int ret;
1120 size_t size = sizeof(*tcp_floor) + sizeof(*ipv4_floor);
1121
1122 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
1123
1124 if (!tower_data)
1125 return size;
1126
1127 tcp_floor = (twr_tcp_floor_t *)tower_data;
1128 tower_data += sizeof(*tcp_floor);
1129
1130 ipv4_floor = (twr_ipv4_floor_t *)tower_data;
1131
1132 tcp_floor->count_lhs = sizeof(tcp_floor->protid);
1133 tcp_floor->protid = EPM_PROTOCOL_TCP;
1134 tcp_floor->count_rhs = sizeof(tcp_floor->port);
1135
1136 ipv4_floor->count_lhs = sizeof(ipv4_floor->protid);
1137 ipv4_floor->protid = EPM_PROTOCOL_IP;
1138 ipv4_floor->count_rhs = sizeof(ipv4_floor->ipv4addr);
1139
1140 hints.ai_flags = AI_NUMERICHOST;
1141 /* FIXME: only support IPv4 at the moment. how is IPv6 represented by the EPM? */
1142 hints.ai_family = PF_INET;
1143 hints.ai_socktype = SOCK_STREAM;
1144 hints.ai_protocol = IPPROTO_TCP;
1145 hints.ai_addrlen = 0;
1146 hints.ai_addr = NULL;
1147 hints.ai_canonname = NULL;
1148 hints.ai_next = NULL;
1149
1150 ret = getaddrinfo(networkaddr, endpoint, &hints, &ai);
1151 if (ret)
1152 {
1153 ret = getaddrinfo("0.0.0.0", endpoint, &hints, &ai);
1154 if (ret)
1155 {
1156 ERR("getaddrinfo failed: %s\n", gai_strerror(ret));
1157 return 0;
1158 }
1159 }
1160
1161 if (ai->ai_family == PF_INET)
1162 {
1163 const struct sockaddr_in *sin = (const struct sockaddr_in *)ai->ai_addr;
1164 tcp_floor->port = sin->sin_port;
1165 ipv4_floor->ipv4addr = sin->sin_addr.s_addr;
1166 }
1167 else
1168 {
1169 ERR("unexpected protocol family %d\n", ai->ai_family);
1170 return 0;
1171 }
1172
1173 freeaddrinfo(ai);
1174
1175 return size;
1176 }
1177
1178 static RPC_STATUS rpcrt4_ncacn_ip_tcp_parse_top_of_tower(const unsigned char *tower_data,
1179 size_t tower_size,
1180 char **networkaddr,
1181 char **endpoint)
1182 {
1183 const twr_tcp_floor_t *tcp_floor = (const twr_tcp_floor_t *)tower_data;
1184 const twr_ipv4_floor_t *ipv4_floor;
1185 struct in_addr in_addr;
1186
1187 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
1188
1189 if (tower_size < sizeof(*tcp_floor))
1190 return EPT_S_NOT_REGISTERED;
1191
1192 tower_data += sizeof(*tcp_floor);
1193 tower_size -= sizeof(*tcp_floor);
1194
1195 if (tower_size < sizeof(*ipv4_floor))
1196 return EPT_S_NOT_REGISTERED;
1197
1198 ipv4_floor = (const twr_ipv4_floor_t *)tower_data;
1199
1200 if ((tcp_floor->count_lhs != sizeof(tcp_floor->protid)) ||
1201 (tcp_floor->protid != EPM_PROTOCOL_TCP) ||
1202 (tcp_floor->count_rhs != sizeof(tcp_floor->port)) ||
1203 (ipv4_floor->count_lhs != sizeof(ipv4_floor->protid)) ||
1204 (ipv4_floor->protid != EPM_PROTOCOL_IP) ||
1205 (ipv4_floor->count_rhs != sizeof(ipv4_floor->ipv4addr)))
1206 return EPT_S_NOT_REGISTERED;
1207
1208 if (endpoint)
1209 {
1210 *endpoint = I_RpcAllocate(6 /* sizeof("65535") + 1 */);
1211 if (!*endpoint)
1212 return RPC_S_OUT_OF_RESOURCES;
1213 sprintf(*endpoint, "%u", ntohs(tcp_floor->port));
1214 }
1215
1216 if (networkaddr)
1217 {
1218 *networkaddr = I_RpcAllocate(INET_ADDRSTRLEN);
1219 if (!*networkaddr)
1220 {
1221 if (endpoint)
1222 {
1223 I_RpcFree(*endpoint);
1224 *endpoint = NULL;
1225 }
1226 return RPC_S_OUT_OF_RESOURCES;
1227 }
1228 in_addr.s_addr = ipv4_floor->ipv4addr;
1229 if (!inet_ntop(AF_INET, &in_addr, *networkaddr, INET_ADDRSTRLEN))
1230 {
1231 ERR("inet_ntop: %s\n", strerror(errno));
1232 I_RpcFree(*networkaddr);
1233 *networkaddr = NULL;
1234 if (endpoint)
1235 {
1236 I_RpcFree(*endpoint);
1237 *endpoint = NULL;
1238 }
1239 return EPT_S_NOT_REGISTERED;
1240 }
1241 }
1242
1243 return RPC_S_OK;
1244 }
1245
1246 typedef struct _RpcServerProtseq_sock
1247 {
1248 RpcServerProtseq common;
1249 int mgr_event_rcv;
1250 int mgr_event_snd;
1251 } RpcServerProtseq_sock;
1252
1253 static RpcServerProtseq *rpcrt4_protseq_sock_alloc(void)
1254 {
1255 RpcServerProtseq_sock *ps = HeapAlloc(GetProcessHeap(), 0, sizeof(*ps));
1256 if (ps)
1257 {
1258 int fds[2];
1259 u_long blocking;
1260 if (!socketpair(PF_UNIX, SOCK_DGRAM, 0, fds))
1261 {
1262 blocking = 1;
1263 ioctlsocket(fds[0], FIONBIO, &blocking);
1264 ioctlsocket(fds[1], FIONBIO, &blocking);
1265 ps->mgr_event_rcv = fds[0];
1266 ps->mgr_event_snd = fds[1];
1267 }
1268 else
1269 {
1270 ERR("socketpair failed with error %s\n", strerror(errno));
1271 HeapFree(GetProcessHeap(), 0, ps);
1272 return NULL;
1273 }
1274 }
1275 return &ps->common;
1276 }
1277
1278 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq *protseq)
1279 {
1280 RpcServerProtseq_sock *sockps = CONTAINING_RECORD(protseq, RpcServerProtseq_sock, common);
1281 char dummy = 1;
1282 write(sockps->mgr_event_snd, &dummy, sizeof(dummy));
1283 }
1284
1285 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq *protseq, void *prev_array, unsigned int *count)
1286 {
1287 struct pollfd *poll_info = prev_array;
1288 RpcConnection_tcp *conn;
1289 RpcServerProtseq_sock *sockps = CONTAINING_RECORD(protseq, RpcServerProtseq_sock, common);
1290
1291 EnterCriticalSection(&protseq->cs);
1292
1293 /* open and count connections */
1294 *count = 1;
1295 conn = (RpcConnection_tcp *)protseq->conn;
1296 while (conn) {
1297 if (conn->sock != -1)
1298 (*count)++;
1299 conn = (RpcConnection_tcp *)conn->common.Next;
1300 }
1301
1302 /* make array of connections */
1303 if (poll_info)
1304 poll_info = HeapReAlloc(GetProcessHeap(), 0, poll_info, *count*sizeof(*poll_info));
1305 else
1306 poll_info = HeapAlloc(GetProcessHeap(), 0, *count*sizeof(*poll_info));
1307 if (!poll_info)
1308 {
1309 ERR("couldn't allocate poll_info\n");
1310 LeaveCriticalSection(&protseq->cs);
1311 return NULL;
1312 }
1313
1314 poll_info[0].fd = sockps->mgr_event_rcv;
1315 poll_info[0].events = POLLIN;
1316 *count = 1;
1317 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_tcp, common);
1318 while (conn) {
1319 if (conn->sock != -1)
1320 {
1321 poll_info[*count].fd = conn->sock;
1322 poll_info[*count].events = POLLIN;
1323 (*count)++;
1324 }
1325 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_tcp, common);
1326 }
1327 LeaveCriticalSection(&protseq->cs);
1328 return poll_info;
1329 }
1330
1331 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq *protseq, void *array)
1332 {
1333 HeapFree(GetProcessHeap(), 0, array);
1334 }
1335
1336 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq *protseq, unsigned int count, void *wait_array)
1337 {
1338 struct pollfd *poll_info = wait_array;
1339 int ret, i;
1340 RpcConnection *cconn;
1341 RpcConnection_tcp *conn;
1342
1343 if (!poll_info)
1344 return -1;
1345
1346 ret = poll(poll_info, count, -1);
1347 if (ret < 0)
1348 {
1349 ERR("poll failed with error %d\n", ret);
1350 return -1;
1351 }
1352
1353 for (i = 0; i < count; i++)
1354 if (poll_info[i].revents & POLLIN)
1355 {
1356 /* RPC server event */
1357 if (i == 0)
1358 {
1359 char dummy;
1360 read(poll_info[0].fd, &dummy, sizeof(dummy));
1361 return 0;
1362 }
1363
1364 /* find which connection got a RPC */
1365 EnterCriticalSection(&protseq->cs);
1366 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_tcp, common);
1367 while (conn) {
1368 if (poll_info[i].fd == conn->sock) break;
1369 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_tcp, common);
1370 }
1371 cconn = NULL;
1372 if (conn)
1373 RPCRT4_SpawnConnection(&cconn, &conn->common);
1374 else
1375 ERR("failed to locate connection for fd %d\n", poll_info[i].fd);
1376 LeaveCriticalSection(&protseq->cs);
1377 if (cconn)
1378 RPCRT4_new_client(cconn);
1379 else
1380 return -1;
1381 }
1382
1383 return 1;
1384 }
1385
1386 static const struct connection_ops conn_protseq_list[] = {
1387 { "ncacn_np",
1388 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SMB },
1389 rpcrt4_conn_np_alloc,
1390 rpcrt4_ncacn_np_open,
1391 rpcrt4_ncacn_np_handoff,
1392 rpcrt4_conn_np_read,
1393 rpcrt4_conn_np_write,
1394 rpcrt4_conn_np_close,
1395 rpcrt4_conn_np_cancel_call,
1396 rpcrt4_conn_np_wait_for_incoming_data,
1397 rpcrt4_ncacn_np_get_top_of_tower,
1398 rpcrt4_ncacn_np_parse_top_of_tower,
1399 },
1400 { "ncalrpc",
1401 { EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_PIPE },
1402 rpcrt4_conn_np_alloc,
1403 rpcrt4_ncalrpc_open,
1404 rpcrt4_ncalrpc_handoff,
1405 rpcrt4_conn_np_read,
1406 rpcrt4_conn_np_write,
1407 rpcrt4_conn_np_close,
1408 rpcrt4_conn_np_cancel_call,
1409 rpcrt4_conn_np_wait_for_incoming_data,
1410 rpcrt4_ncalrpc_get_top_of_tower,
1411 rpcrt4_ncalrpc_parse_top_of_tower,
1412 },
1413 { "ncacn_ip_tcp",
1414 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_TCP },
1415 rpcrt4_conn_tcp_alloc,
1416 rpcrt4_ncacn_ip_tcp_open,
1417 rpcrt4_conn_tcp_handoff,
1418 rpcrt4_conn_tcp_read,
1419 rpcrt4_conn_tcp_write,
1420 rpcrt4_conn_tcp_close,
1421 rpcrt4_conn_tcp_cancel_call,
1422 rpcrt4_conn_tcp_wait_for_incoming_data,
1423 rpcrt4_ncacn_ip_tcp_get_top_of_tower,
1424 rpcrt4_ncacn_ip_tcp_parse_top_of_tower,
1425 }
1426 };
1427
1428
1429 static const struct protseq_ops protseq_list[] =
1430 {
1431 {
1432 "ncacn_np",
1433 rpcrt4_protseq_np_alloc,
1434 rpcrt4_protseq_np_signal_state_changed,
1435 rpcrt4_protseq_np_get_wait_array,
1436 rpcrt4_protseq_np_free_wait_array,
1437 rpcrt4_protseq_np_wait_for_new_connection,
1438 rpcrt4_protseq_ncacn_np_open_endpoint,
1439 },
1440 {
1441 "ncalrpc",
1442 rpcrt4_protseq_np_alloc,
1443 rpcrt4_protseq_np_signal_state_changed,
1444 rpcrt4_protseq_np_get_wait_array,
1445 rpcrt4_protseq_np_free_wait_array,
1446 rpcrt4_protseq_np_wait_for_new_connection,
1447 rpcrt4_protseq_ncalrpc_open_endpoint,
1448 },
1449 {
1450 "ncacn_ip_tcp",
1451 rpcrt4_protseq_sock_alloc,
1452 rpcrt4_protseq_sock_signal_state_changed,
1453 rpcrt4_protseq_sock_get_wait_array,
1454 rpcrt4_protseq_sock_free_wait_array,
1455 rpcrt4_protseq_sock_wait_for_new_connection,
1456 rpcrt4_protseq_ncacn_ip_tcp_open_endpoint,
1457 },
1458 };
1459
1460 #define ARRAYSIZE(a) (sizeof((a)) / sizeof((a)[0]))
1461
1462 const struct protseq_ops *rpcrt4_get_protseq_ops(const char *protseq)
1463 {
1464 int i;
1465 for(i=0; i<ARRAYSIZE(protseq_list); i++)
1466 if (!strcmp(protseq_list[i].name, protseq))
1467 return &protseq_list[i];
1468 return NULL;
1469 }
1470
1471 static const struct connection_ops *rpcrt4_get_conn_protseq_ops(const char *protseq)
1472 {
1473 int i;
1474 for(i=0; i<ARRAYSIZE(conn_protseq_list); i++)
1475 if (!strcmp(conn_protseq_list[i].name, protseq))
1476 return &conn_protseq_list[i];
1477 return NULL;
1478 }
1479
1480 /**** interface to rest of code ****/
1481
1482 RPC_STATUS RPCRT4_OpenClientConnection(RpcConnection* Connection)
1483 {
1484 TRACE("(Connection == ^%p)\n", Connection);
1485
1486 assert(!Connection->server);
1487 return Connection->ops->open_connection_client(Connection);
1488 }
1489
1490 RPC_STATUS RPCRT4_CloseConnection(RpcConnection* Connection)
1491 {
1492 TRACE("(Connection == ^%p)\n", Connection);
1493 if (SecIsValidHandle(&Connection->ctx))
1494 {
1495 DeleteSecurityContext(&Connection->ctx);
1496 SecInvalidateHandle(&Connection->ctx);
1497 }
1498 rpcrt4_conn_close(Connection);
1499 return RPC_S_OK;
1500 }
1501
1502 RPC_STATUS RPCRT4_CreateConnection(RpcConnection** Connection, BOOL server,
1503 LPCSTR Protseq, LPCSTR NetworkAddr, LPCSTR Endpoint,
1504 LPCWSTR NetworkOptions, RpcAuthInfo* AuthInfo, RpcQualityOfService *QOS)
1505 {
1506 const struct connection_ops *ops;
1507 RpcConnection* NewConnection;
1508
1509 ops = rpcrt4_get_conn_protseq_ops(Protseq);
1510 if (!ops)
1511 {
1512 FIXME("not supported for protseq %s\n", Protseq);
1513 return RPC_S_PROTSEQ_NOT_SUPPORTED;
1514 }
1515
1516 NewConnection = ops->alloc();
1517 NewConnection->Next = NULL;
1518 NewConnection->server_binding = NULL;
1519 NewConnection->server = server;
1520 NewConnection->ops = ops;
1521 NewConnection->NetworkAddr = RPCRT4_strdupA(NetworkAddr);
1522 NewConnection->Endpoint = RPCRT4_strdupA(Endpoint);
1523 NewConnection->NetworkOptions = RPCRT4_strdupW(NetworkOptions);
1524 NewConnection->MaxTransmissionSize = RPC_MAX_PACKET_SIZE;
1525 memset(&NewConnection->ActiveInterface, 0, sizeof(NewConnection->ActiveInterface));
1526 NewConnection->NextCallId = 1;
1527
1528 SecInvalidateHandle(&NewConnection->ctx);
1529 memset(&NewConnection->exp, 0, sizeof(NewConnection->exp));
1530 NewConnection->attr = 0;
1531 if (AuthInfo) RpcAuthInfo_AddRef(AuthInfo);
1532 NewConnection->AuthInfo = AuthInfo;
1533 NewConnection->encryption_auth_len = 0;
1534 NewConnection->signature_auth_len = 0;
1535 if (QOS) RpcQualityOfService_AddRef(QOS);
1536 NewConnection->QOS = QOS;
1537
1538 list_init(&NewConnection->conn_pool_entry);
1539 NewConnection->async_state = NULL;
1540
1541 TRACE("connection: %p\n", NewConnection);
1542 *Connection = NewConnection;
1543
1544 return RPC_S_OK;
1545 }
1546
1547
1548 RPC_STATUS RPCRT4_SpawnConnection(RpcConnection** Connection, RpcConnection* OldConnection)
1549 {
1550 RPC_STATUS err;
1551
1552 err = RPCRT4_CreateConnection(Connection, OldConnection->server,
1553 rpcrt4_conn_get_name(OldConnection),
1554 OldConnection->NetworkAddr,
1555 OldConnection->Endpoint, NULL,
1556 OldConnection->AuthInfo, OldConnection->QOS);
1557 if (err == RPC_S_OK)
1558 rpcrt4_conn_handoff(OldConnection, *Connection);
1559 return err;
1560 }
1561
1562 RPC_STATUS RPCRT4_DestroyConnection(RpcConnection* Connection)
1563 {
1564 TRACE("connection: %p\n", Connection);
1565
1566 RPCRT4_CloseConnection(Connection);
1567 RPCRT4_strfree(Connection->Endpoint);
1568 RPCRT4_strfree(Connection->NetworkAddr);
1569 HeapFree(GetProcessHeap(), 0, Connection->NetworkOptions);
1570 if (Connection->AuthInfo) RpcAuthInfo_Release(Connection->AuthInfo);
1571 if (Connection->QOS) RpcQualityOfService_Release(Connection->QOS);
1572
1573 /* server-only */
1574 if (Connection->server_binding) RPCRT4_ReleaseBinding(Connection->server_binding);
1575
1576 HeapFree(GetProcessHeap(), 0, Connection);
1577 return RPC_S_OK;
1578 }
1579
1580 RPC_STATUS RpcTransport_GetTopOfTower(unsigned char *tower_data,
1581 size_t *tower_size,
1582 const char *protseq,
1583 const char *networkaddr,
1584 const char *endpoint)
1585 {
1586 twr_empty_floor_t *protocol_floor;
1587 const struct connection_ops *protseq_ops = rpcrt4_get_conn_protseq_ops(protseq);
1588
1589 *tower_size = 0;
1590
1591 if (!protseq_ops)
1592 return RPC_S_INVALID_RPC_PROTSEQ;
1593
1594 if (!tower_data)
1595 {
1596 *tower_size = sizeof(*protocol_floor);
1597 *tower_size += protseq_ops->get_top_of_tower(NULL, networkaddr, endpoint);
1598 return RPC_S_OK;
1599 }
1600
1601 protocol_floor = (twr_empty_floor_t *)tower_data;
1602 protocol_floor->count_lhs = sizeof(protocol_floor->protid);
1603 protocol_floor->protid = protseq_ops->epm_protocols[0];
1604 protocol_floor->count_rhs = 0;
1605
1606 tower_data += sizeof(*protocol_floor);
1607
1608 *tower_size = protseq_ops->get_top_of_tower(tower_data, networkaddr, endpoint);
1609 if (!*tower_size)
1610 return EPT_S_NOT_REGISTERED;
1611
1612 *tower_size += sizeof(*protocol_floor);
1613
1614 return RPC_S_OK;
1615 }
1616
1617 RPC_STATUS RpcTransport_ParseTopOfTower(const unsigned char *tower_data,
1618 size_t tower_size,
1619 char **protseq,
1620 char **networkaddr,
1621 char **endpoint)
1622 {
1623 const twr_empty_floor_t *protocol_floor;
1624 const twr_empty_floor_t *floor4;
1625 const struct connection_ops *protseq_ops = NULL;
1626 RPC_STATUS status;
1627 int i;
1628
1629 if (tower_size < sizeof(*protocol_floor))
1630 return EPT_S_NOT_REGISTERED;
1631
1632 protocol_floor = (const twr_empty_floor_t *)tower_data;
1633 tower_data += sizeof(*protocol_floor);
1634 tower_size -= sizeof(*protocol_floor);
1635 if ((protocol_floor->count_lhs != sizeof(protocol_floor->protid)) ||
1636 (protocol_floor->count_rhs > tower_size))
1637 return EPT_S_NOT_REGISTERED;
1638 tower_data += protocol_floor->count_rhs;
1639 tower_size -= protocol_floor->count_rhs;
1640
1641 floor4 = (const twr_empty_floor_t *)tower_data;
1642 if ((tower_size < sizeof(*floor4)) ||
1643 (floor4->count_lhs != sizeof(floor4->protid)))
1644 return EPT_S_NOT_REGISTERED;
1645
1646 for(i = 0; i < ARRAYSIZE(conn_protseq_list); i++)
1647 if ((protocol_floor->protid == conn_protseq_list[i].epm_protocols[0]) &&
1648 (floor4->protid == conn_protseq_list[i].epm_protocols[1]))
1649 {
1650 protseq_ops = &conn_protseq_list[i];
1651 break;
1652 }
1653
1654 if (!protseq_ops)
1655 return EPT_S_NOT_REGISTERED;
1656
1657 status = protseq_ops->parse_top_of_tower(tower_data, tower_size, networkaddr, endpoint);
1658
1659 if ((status == RPC_S_OK) && protseq)
1660 {
1661 *protseq = I_RpcAllocate(strlen(protseq_ops->name) + 1);
1662 strcpy(*protseq, protseq_ops->name);
1663 }
1664
1665 return status;
1666 }
1667
1668 /***********************************************************************
1669 * RpcNetworkIsProtseqValidW (RPCRT4.@)
1670 *
1671 * Checks if the given protocol sequence is known by the RPC system.
1672 * If it is, returns RPC_S_OK, otherwise RPC_S_PROTSEQ_NOT_SUPPORTED.
1673 *
1674 */
1675 RPC_STATUS WINAPI RpcNetworkIsProtseqValidW(RPC_WSTR protseq)
1676 {
1677 char ps[0x10];
1678
1679 WideCharToMultiByte(CP_ACP, 0, protseq, -1,
1680 ps, sizeof ps, NULL, NULL);
1681 if (rpcrt4_get_conn_protseq_ops(ps))
1682 return RPC_S_OK;
1683
1684 FIXME("Unknown protseq %s\n", debugstr_w(protseq));
1685
1686 return RPC_S_INVALID_RPC_PROTSEQ;
1687 }
1688
1689 /***********************************************************************
1690 * RpcNetworkIsProtseqValidA (RPCRT4.@)
1691 */
1692 RPC_STATUS WINAPI RpcNetworkIsProtseqValidA(RPC_CSTR protseq)
1693 {
1694 UNICODE_STRING protseqW;
1695
1696 if (RtlCreateUnicodeStringFromAsciiz(&protseqW, (char*)protseq))
1697 {
1698 RPC_STATUS ret = RpcNetworkIsProtseqValidW(protseqW.Buffer);
1699 RtlFreeUnicodeString(&protseqW);
1700 return ret;
1701 }
1702 return RPC_S_OUT_OF_MEMORY;
1703 }