Create a branch for working on csrss and co.
[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 <stdlib.h>
33 #include <sys/types.h>
34
35 #if defined(__MINGW32__) || defined (_MSC_VER)
36 # include <ws2tcpip.h>
37 # ifndef EADDRINUSE
38 # define EADDRINUSE WSAEADDRINUSE
39 # endif
40 # ifndef EAGAIN
41 # define EAGAIN WSAEWOULDBLOCK
42 # endif
43 # undef errno
44 # define errno WSAGetLastError()
45 #else
46 # include <errno.h>
47 # ifdef HAVE_UNISTD_H
48 # include <unistd.h>
49 # endif
50 # include <fcntl.h>
51 # ifdef HAVE_SYS_SOCKET_H
52 # include <sys/socket.h>
53 # endif
54 # ifdef HAVE_NETINET_IN_H
55 # include <netinet/in.h>
56 # endif
57 # ifdef HAVE_NETINET_TCP_H
58 # include <netinet/tcp.h>
59 # endif
60 # ifdef HAVE_ARPA_INET_H
61 # include <arpa/inet.h>
62 # endif
63 # ifdef HAVE_NETDB_H
64 # include <netdb.h>
65 # endif
66 # ifdef HAVE_SYS_POLL_H
67 # include <sys/poll.h>
68 # endif
69 # ifdef HAVE_SYS_FILIO_H
70 # include <sys/filio.h>
71 # endif
72 # ifdef HAVE_SYS_IOCTL_H
73 # include <sys/ioctl.h>
74 # endif
75 # define closesocket close
76 # define ioctlsocket ioctl
77 #endif /* defined(__MINGW32__) || defined (_MSC_VER) */
78
79 #include "windef.h"
80 #include "winbase.h"
81 #include "winnls.h"
82 #include "winerror.h"
83 #include "wininet.h"
84 #include "winternl.h"
85 #include "wine/unicode.h"
86
87 #include "rpc.h"
88 #include "rpcndr.h"
89
90 #include "wine/debug.h"
91
92 #include "rpc_binding.h"
93 #include "rpc_assoc.h"
94 #include "rpc_message.h"
95 #include "rpc_server.h"
96 #include "epm_towers.h"
97
98 #ifndef SOL_TCP
99 # define SOL_TCP IPPROTO_TCP
100 #endif
101
102 #define DEFAULT_NCACN_HTTP_TIMEOUT (60 * 1000)
103
104 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
105
106 static RPC_STATUS RPCRT4_SpawnConnection(RpcConnection** Connection, RpcConnection* OldConnection);
107
108 /**** ncacn_np support ****/
109
110 typedef struct _RpcConnection_np
111 {
112 RpcConnection common;
113 HANDLE pipe;
114 OVERLAPPED ovl;
115 BOOL listening;
116 } RpcConnection_np;
117
118 static RpcConnection *rpcrt4_conn_np_alloc(void)
119 {
120 RpcConnection_np *npc = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_np));
121 if (npc)
122 {
123 npc->pipe = NULL;
124 memset(&npc->ovl, 0, sizeof(npc->ovl));
125 npc->listening = FALSE;
126 }
127 return &npc->common;
128 }
129
130 static RPC_STATUS rpcrt4_conn_listen_pipe(RpcConnection_np *npc)
131 {
132 if (npc->listening)
133 return RPC_S_OK;
134
135 npc->listening = TRUE;
136 for (;;)
137 {
138 if (ConnectNamedPipe(npc->pipe, &npc->ovl))
139 return RPC_S_OK;
140
141 switch(GetLastError())
142 {
143 case ERROR_PIPE_CONNECTED:
144 SetEvent(npc->ovl.hEvent);
145 return RPC_S_OK;
146 case ERROR_IO_PENDING:
147 /* will be completed in rpcrt4_protseq_np_wait_for_new_connection */
148 return RPC_S_OK;
149 case ERROR_NO_DATA_DETECTED:
150 /* client has disconnected, retry */
151 DisconnectNamedPipe( npc->pipe );
152 break;
153 default:
154 npc->listening = FALSE;
155 WARN("Couldn't ConnectNamedPipe (error was %d)\n", GetLastError());
156 return RPC_S_OUT_OF_RESOURCES;
157 }
158 }
159 }
160
161 static RPC_STATUS rpcrt4_conn_create_pipe(RpcConnection *Connection, LPCSTR pname)
162 {
163 RpcConnection_np *npc = (RpcConnection_np *) Connection;
164 TRACE("listening on %s\n", pname);
165
166 npc->pipe = CreateNamedPipeA(pname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
167 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE,
168 PIPE_UNLIMITED_INSTANCES,
169 RPC_MAX_PACKET_SIZE, RPC_MAX_PACKET_SIZE, 5000, NULL);
170 if (npc->pipe == INVALID_HANDLE_VALUE) {
171 WARN("CreateNamedPipe failed with error %d\n", GetLastError());
172 if (GetLastError() == ERROR_FILE_EXISTS)
173 return RPC_S_DUPLICATE_ENDPOINT;
174 else
175 return RPC_S_CANT_CREATE_ENDPOINT;
176 }
177
178 memset(&npc->ovl, 0, sizeof(npc->ovl));
179 npc->ovl.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
180
181 /* Note: we don't call ConnectNamedPipe here because it must be done in the
182 * server thread as the thread must be alertable */
183 return RPC_S_OK;
184 }
185
186 static RPC_STATUS rpcrt4_conn_open_pipe(RpcConnection *Connection, LPCSTR pname, BOOL wait)
187 {
188 RpcConnection_np *npc = (RpcConnection_np *) Connection;
189 HANDLE pipe;
190 DWORD err, dwMode;
191
192 TRACE("connecting to %s\n", pname);
193
194 while (TRUE) {
195 DWORD dwFlags = 0;
196 if (Connection->QOS)
197 {
198 dwFlags = SECURITY_SQOS_PRESENT;
199 switch (Connection->QOS->qos->ImpersonationType)
200 {
201 case RPC_C_IMP_LEVEL_DEFAULT:
202 /* FIXME: what to do here? */
203 break;
204 case RPC_C_IMP_LEVEL_ANONYMOUS:
205 dwFlags |= SECURITY_ANONYMOUS;
206 break;
207 case RPC_C_IMP_LEVEL_IDENTIFY:
208 dwFlags |= SECURITY_IDENTIFICATION;
209 break;
210 case RPC_C_IMP_LEVEL_IMPERSONATE:
211 dwFlags |= SECURITY_IMPERSONATION;
212 break;
213 case RPC_C_IMP_LEVEL_DELEGATE:
214 dwFlags |= SECURITY_DELEGATION;
215 break;
216 }
217 if (Connection->QOS->qos->IdentityTracking == RPC_C_QOS_IDENTITY_DYNAMIC)
218 dwFlags |= SECURITY_CONTEXT_TRACKING;
219 }
220 pipe = CreateFileA(pname, GENERIC_READ|GENERIC_WRITE, 0, NULL,
221 OPEN_EXISTING, dwFlags, 0);
222 if (pipe != INVALID_HANDLE_VALUE) break;
223 err = GetLastError();
224 if (err == ERROR_PIPE_BUSY) {
225 TRACE("connection failed, error=%x\n", err);
226 return RPC_S_SERVER_TOO_BUSY;
227 } else if (err == ERROR_BAD_NETPATH) {
228 TRACE("connection failed, error=%x\n", err);
229 return RPC_S_SERVER_UNAVAILABLE;
230 }
231 if (!wait || !WaitNamedPipeA(pname, NMPWAIT_WAIT_FOREVER)) {
232 err = GetLastError();
233 WARN("connection failed, error=%x\n", err);
234 return RPC_S_SERVER_UNAVAILABLE;
235 }
236 }
237
238 /* success */
239 memset(&npc->ovl, 0, sizeof(npc->ovl));
240 /* pipe is connected; change to message-read mode. */
241 dwMode = PIPE_READMODE_MESSAGE;
242 SetNamedPipeHandleState(pipe, &dwMode, NULL, NULL);
243 npc->ovl.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
244 npc->pipe = pipe;
245
246 return RPC_S_OK;
247 }
248
249 static RPC_STATUS rpcrt4_ncalrpc_open(RpcConnection* Connection)
250 {
251 RpcConnection_np *npc = (RpcConnection_np *) Connection;
252 static const char prefix[] = "\\\\.\\pipe\\lrpc\\";
253 RPC_STATUS r;
254 LPSTR pname;
255
256 /* already connected? */
257 if (npc->pipe)
258 return RPC_S_OK;
259
260 /* protseq=ncalrpc: supposed to use NT LPC ports,
261 * but we'll implement it with named pipes for now */
262 pname = I_RpcAllocate(strlen(prefix) + strlen(Connection->Endpoint) + 1);
263 strcat(strcpy(pname, prefix), Connection->Endpoint);
264 r = rpcrt4_conn_open_pipe(Connection, pname, TRUE);
265 I_RpcFree(pname);
266
267 return r;
268 }
269
270 static RPC_STATUS rpcrt4_protseq_ncalrpc_open_endpoint(RpcServerProtseq* protseq, const char *endpoint)
271 {
272 static const char prefix[] = "\\\\.\\pipe\\lrpc\\";
273 RPC_STATUS r;
274 LPSTR pname;
275 RpcConnection *Connection;
276 char generated_endpoint[22];
277
278 if (!endpoint)
279 {
280 static LONG lrpc_nameless_id;
281 DWORD process_id = GetCurrentProcessId();
282 ULONG id = InterlockedIncrement(&lrpc_nameless_id);
283 snprintf(generated_endpoint, sizeof(generated_endpoint),
284 "LRPC%08x.%08x", process_id, id);
285 endpoint = generated_endpoint;
286 }
287
288 r = RPCRT4_CreateConnection(&Connection, TRUE, protseq->Protseq, NULL,
289 endpoint, NULL, NULL, NULL);
290 if (r != RPC_S_OK)
291 return r;
292
293 /* protseq=ncalrpc: supposed to use NT LPC ports,
294 * but we'll implement it with named pipes for now */
295 pname = I_RpcAllocate(strlen(prefix) + strlen(Connection->Endpoint) + 1);
296 strcat(strcpy(pname, prefix), Connection->Endpoint);
297 r = rpcrt4_conn_create_pipe(Connection, pname);
298 I_RpcFree(pname);
299
300 EnterCriticalSection(&protseq->cs);
301 Connection->Next = protseq->conn;
302 protseq->conn = Connection;
303 LeaveCriticalSection(&protseq->cs);
304
305 return r;
306 }
307
308 static RPC_STATUS rpcrt4_ncacn_np_open(RpcConnection* Connection)
309 {
310 RpcConnection_np *npc = (RpcConnection_np *) Connection;
311 static const char prefix[] = "\\\\";
312 static const char local[] =".";
313 RPC_STATUS r;
314 LPSTR pname;
315 INT size;
316
317 /* already connected? */
318 if (npc->pipe)
319 return RPC_S_OK;
320
321 /* protseq=ncacn_np: named pipes */
322 size = strlen(prefix);
323 if (Connection->NetworkAddr == NULL || strlen(Connection->NetworkAddr) == 0)
324 size += strlen(local);
325 else
326 size += strlen(Connection->NetworkAddr);
327 size += strlen(Connection->Endpoint) + 1;
328
329 pname = I_RpcAllocate(size);
330 strcpy(pname, prefix);
331 if (Connection->NetworkAddr == NULL || strlen(Connection->NetworkAddr) == 0)
332 strcat(pname, local);
333 else
334 strcat(pname, Connection->NetworkAddr);
335 strcat(pname, Connection->Endpoint);
336 r = rpcrt4_conn_open_pipe(Connection, pname, TRUE);
337 I_RpcFree(pname);
338
339 return r;
340 }
341
342 static RPC_STATUS rpcrt4_protseq_ncacn_np_open_endpoint(RpcServerProtseq *protseq, const char *endpoint)
343 {
344 static const char prefix[] = "\\\\.";
345 RPC_STATUS r;
346 LPSTR pname;
347 RpcConnection *Connection;
348 char generated_endpoint[21];
349
350 if (!endpoint)
351 {
352 static LONG np_nameless_id;
353 DWORD process_id = GetCurrentProcessId();
354 ULONG id = InterlockedExchangeAdd(&np_nameless_id, 1 );
355 snprintf(generated_endpoint, sizeof(generated_endpoint),
356 "\\\\pipe\\\\%08x.%03x", process_id, id);
357 endpoint = generated_endpoint;
358 }
359
360 r = RPCRT4_CreateConnection(&Connection, TRUE, protseq->Protseq, NULL,
361 endpoint, NULL, NULL, NULL);
362 if (r != RPC_S_OK)
363 return r;
364
365 /* protseq=ncacn_np: named pipes */
366 pname = I_RpcAllocate(strlen(prefix) + strlen(Connection->Endpoint) + 1);
367 strcat(strcpy(pname, prefix), Connection->Endpoint);
368 r = rpcrt4_conn_create_pipe(Connection, pname);
369 I_RpcFree(pname);
370
371 EnterCriticalSection(&protseq->cs);
372 Connection->Next = protseq->conn;
373 protseq->conn = Connection;
374 LeaveCriticalSection(&protseq->cs);
375
376 return r;
377 }
378
379 static void rpcrt4_conn_np_handoff(RpcConnection_np *old_npc, RpcConnection_np *new_npc)
380 {
381 /* because of the way named pipes work, we'll transfer the connected pipe
382 * to the child, then reopen the server binding to continue listening */
383
384 new_npc->pipe = old_npc->pipe;
385 new_npc->ovl = old_npc->ovl;
386 old_npc->pipe = 0;
387 memset(&old_npc->ovl, 0, sizeof(old_npc->ovl));
388 old_npc->listening = FALSE;
389 }
390
391 static RPC_STATUS rpcrt4_ncacn_np_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
392 {
393 RPC_STATUS status;
394 LPSTR pname;
395 static const char prefix[] = "\\\\.";
396
397 rpcrt4_conn_np_handoff((RpcConnection_np *)old_conn, (RpcConnection_np *)new_conn);
398
399 pname = I_RpcAllocate(strlen(prefix) + strlen(old_conn->Endpoint) + 1);
400 strcat(strcpy(pname, prefix), old_conn->Endpoint);
401 status = rpcrt4_conn_create_pipe(old_conn, pname);
402 I_RpcFree(pname);
403
404 return status;
405 }
406
407 static RPC_STATUS rpcrt4_ncalrpc_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
408 {
409 RPC_STATUS status;
410 LPSTR pname;
411 static const char prefix[] = "\\\\.\\pipe\\lrpc\\";
412
413 TRACE("%s\n", old_conn->Endpoint);
414
415 rpcrt4_conn_np_handoff((RpcConnection_np *)old_conn, (RpcConnection_np *)new_conn);
416
417 pname = I_RpcAllocate(strlen(prefix) + strlen(old_conn->Endpoint) + 1);
418 strcat(strcpy(pname, prefix), old_conn->Endpoint);
419 status = rpcrt4_conn_create_pipe(old_conn, pname);
420 I_RpcFree(pname);
421
422 return status;
423 }
424
425 static int rpcrt4_conn_np_read(RpcConnection *Connection,
426 void *buffer, unsigned int count)
427 {
428 RpcConnection_np *npc = (RpcConnection_np *) Connection;
429 char *buf = buffer;
430 BOOL ret = TRUE;
431 unsigned int bytes_left = count;
432 OVERLAPPED ovl;
433
434 ZeroMemory(&ovl, sizeof(ovl));
435 ovl.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
436
437 while (bytes_left)
438 {
439 DWORD bytes_read;
440 ret = ReadFile(npc->pipe, buf, bytes_left, &bytes_read, &ovl);
441 if (!ret && GetLastError() == ERROR_IO_PENDING)
442 ret = GetOverlappedResult(npc->pipe, &ovl, &bytes_read, TRUE);
443 if (!ret && GetLastError() == ERROR_MORE_DATA)
444 ret = TRUE;
445 if (!ret || !bytes_read)
446 break;
447 bytes_left -= bytes_read;
448 buf += bytes_read;
449 }
450 CloseHandle(ovl.hEvent);
451 return ret ? count : -1;
452 }
453
454 static int rpcrt4_conn_np_write(RpcConnection *Connection,
455 const void *buffer, unsigned int count)
456 {
457 RpcConnection_np *npc = (RpcConnection_np *) Connection;
458 const char *buf = buffer;
459 BOOL ret = TRUE;
460 unsigned int bytes_left = count;
461 OVERLAPPED ovl;
462
463 ZeroMemory(&ovl, sizeof(ovl));
464 ovl.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
465
466 while (bytes_left)
467 {
468 DWORD bytes_written;
469 ret = WriteFile(npc->pipe, buf, bytes_left, &bytes_written, &ovl);
470 if (!ret && GetLastError() == ERROR_IO_PENDING)
471 ret = GetOverlappedResult(npc->pipe, &ovl, &bytes_written, TRUE);
472 if (!ret || !bytes_written)
473 break;
474 bytes_left -= bytes_written;
475 buf += bytes_written;
476 }
477 CloseHandle(ovl.hEvent);
478 return ret ? count : -1;
479 }
480
481 static int rpcrt4_conn_np_close(RpcConnection *Connection)
482 {
483 RpcConnection_np *npc = (RpcConnection_np *) Connection;
484 if (npc->pipe) {
485 FlushFileBuffers(npc->pipe);
486 CloseHandle(npc->pipe);
487 npc->pipe = 0;
488 }
489 if (npc->ovl.hEvent) {
490 CloseHandle(npc->ovl.hEvent);
491 npc->ovl.hEvent = 0;
492 }
493 return 0;
494 }
495
496 static void rpcrt4_conn_np_cancel_call(RpcConnection *Connection)
497 {
498 /* FIXME: implement when named pipe writes use overlapped I/O */
499 }
500
501 static int rpcrt4_conn_np_wait_for_incoming_data(RpcConnection *Connection)
502 {
503 /* FIXME: implement when named pipe writes use overlapped I/O */
504 return -1;
505 }
506
507 static size_t rpcrt4_ncacn_np_get_top_of_tower(unsigned char *tower_data,
508 const char *networkaddr,
509 const char *endpoint)
510 {
511 twr_empty_floor_t *smb_floor;
512 twr_empty_floor_t *nb_floor;
513 size_t size;
514 size_t networkaddr_size;
515 size_t endpoint_size;
516
517 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
518
519 networkaddr_size = networkaddr ? strlen(networkaddr) + 1 : 1;
520 endpoint_size = endpoint ? strlen(endpoint) + 1 : 1;
521 size = sizeof(*smb_floor) + endpoint_size + sizeof(*nb_floor) + networkaddr_size;
522
523 if (!tower_data)
524 return size;
525
526 smb_floor = (twr_empty_floor_t *)tower_data;
527
528 tower_data += sizeof(*smb_floor);
529
530 smb_floor->count_lhs = sizeof(smb_floor->protid);
531 smb_floor->protid = EPM_PROTOCOL_SMB;
532 smb_floor->count_rhs = endpoint_size;
533
534 if (endpoint)
535 memcpy(tower_data, endpoint, endpoint_size);
536 else
537 tower_data[0] = 0;
538 tower_data += endpoint_size;
539
540 nb_floor = (twr_empty_floor_t *)tower_data;
541
542 tower_data += sizeof(*nb_floor);
543
544 nb_floor->count_lhs = sizeof(nb_floor->protid);
545 nb_floor->protid = EPM_PROTOCOL_NETBIOS;
546 nb_floor->count_rhs = networkaddr_size;
547
548 if (networkaddr)
549 memcpy(tower_data, networkaddr, networkaddr_size);
550 else
551 tower_data[0] = 0;
552
553 return size;
554 }
555
556 static RPC_STATUS rpcrt4_ncacn_np_parse_top_of_tower(const unsigned char *tower_data,
557 size_t tower_size,
558 char **networkaddr,
559 char **endpoint)
560 {
561 const twr_empty_floor_t *smb_floor = (const twr_empty_floor_t *)tower_data;
562 const twr_empty_floor_t *nb_floor;
563
564 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
565
566 if (tower_size < sizeof(*smb_floor))
567 return EPT_S_NOT_REGISTERED;
568
569 tower_data += sizeof(*smb_floor);
570 tower_size -= sizeof(*smb_floor);
571
572 if ((smb_floor->count_lhs != sizeof(smb_floor->protid)) ||
573 (smb_floor->protid != EPM_PROTOCOL_SMB) ||
574 (smb_floor->count_rhs > tower_size) ||
575 (tower_data[smb_floor->count_rhs - 1] != '\0'))
576 return EPT_S_NOT_REGISTERED;
577
578 if (endpoint)
579 {
580 *endpoint = I_RpcAllocate(smb_floor->count_rhs);
581 if (!*endpoint)
582 return RPC_S_OUT_OF_RESOURCES;
583 memcpy(*endpoint, tower_data, smb_floor->count_rhs);
584 }
585 tower_data += smb_floor->count_rhs;
586 tower_size -= smb_floor->count_rhs;
587
588 if (tower_size < sizeof(*nb_floor))
589 return EPT_S_NOT_REGISTERED;
590
591 nb_floor = (const twr_empty_floor_t *)tower_data;
592
593 tower_data += sizeof(*nb_floor);
594 tower_size -= sizeof(*nb_floor);
595
596 if ((nb_floor->count_lhs != sizeof(nb_floor->protid)) ||
597 (nb_floor->protid != EPM_PROTOCOL_NETBIOS) ||
598 (nb_floor->count_rhs > tower_size) ||
599 (tower_data[nb_floor->count_rhs - 1] != '\0'))
600 return EPT_S_NOT_REGISTERED;
601
602 if (networkaddr)
603 {
604 *networkaddr = I_RpcAllocate(nb_floor->count_rhs);
605 if (!*networkaddr)
606 {
607 if (endpoint)
608 {
609 I_RpcFree(*endpoint);
610 *endpoint = NULL;
611 }
612 return RPC_S_OUT_OF_RESOURCES;
613 }
614 memcpy(*networkaddr, tower_data, nb_floor->count_rhs);
615 }
616
617 return RPC_S_OK;
618 }
619
620 static RPC_STATUS rpcrt4_conn_np_impersonate_client(RpcConnection *conn)
621 {
622 RpcConnection_np *npc = (RpcConnection_np *)conn;
623 BOOL ret;
624
625 TRACE("(%p)\n", conn);
626
627 if (conn->AuthInfo && SecIsValidHandle(&conn->ctx))
628 return RPCRT4_default_impersonate_client(conn);
629
630 ret = ImpersonateNamedPipeClient(npc->pipe);
631 if (!ret)
632 {
633 DWORD error = GetLastError();
634 WARN("ImpersonateNamedPipeClient failed with error %u\n", error);
635 switch (error)
636 {
637 case ERROR_CANNOT_IMPERSONATE:
638 return RPC_S_NO_CONTEXT_AVAILABLE;
639 }
640 }
641 return RPC_S_OK;
642 }
643
644 static RPC_STATUS rpcrt4_conn_np_revert_to_self(RpcConnection *conn)
645 {
646 BOOL ret;
647
648 TRACE("(%p)\n", conn);
649
650 if (conn->AuthInfo && SecIsValidHandle(&conn->ctx))
651 return RPCRT4_default_revert_to_self(conn);
652
653 ret = RevertToSelf();
654 if (!ret)
655 {
656 WARN("RevertToSelf failed with error %u\n", GetLastError());
657 return RPC_S_NO_CONTEXT_AVAILABLE;
658 }
659 return RPC_S_OK;
660 }
661
662 typedef struct _RpcServerProtseq_np
663 {
664 RpcServerProtseq common;
665 HANDLE mgr_event;
666 } RpcServerProtseq_np;
667
668 static RpcServerProtseq *rpcrt4_protseq_np_alloc(void)
669 {
670 RpcServerProtseq_np *ps = HeapAlloc(GetProcessHeap(), 0, sizeof(*ps));
671 if (ps)
672 ps->mgr_event = CreateEventW(NULL, FALSE, FALSE, NULL);
673 return &ps->common;
674 }
675
676 static void rpcrt4_protseq_np_signal_state_changed(RpcServerProtseq *protseq)
677 {
678 RpcServerProtseq_np *npps = CONTAINING_RECORD(protseq, RpcServerProtseq_np, common);
679 SetEvent(npps->mgr_event);
680 }
681
682 static void *rpcrt4_protseq_np_get_wait_array(RpcServerProtseq *protseq, void *prev_array, unsigned int *count)
683 {
684 HANDLE *objs = prev_array;
685 RpcConnection_np *conn;
686 RpcServerProtseq_np *npps = CONTAINING_RECORD(protseq, RpcServerProtseq_np, common);
687
688 EnterCriticalSection(&protseq->cs);
689
690 /* open and count connections */
691 *count = 1;
692 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
693 while (conn) {
694 rpcrt4_conn_listen_pipe(conn);
695 if (conn->ovl.hEvent)
696 (*count)++;
697 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
698 }
699
700 /* make array of connections */
701 if (objs)
702 objs = HeapReAlloc(GetProcessHeap(), 0, objs, *count*sizeof(HANDLE));
703 else
704 objs = HeapAlloc(GetProcessHeap(), 0, *count*sizeof(HANDLE));
705 if (!objs)
706 {
707 ERR("couldn't allocate objs\n");
708 LeaveCriticalSection(&protseq->cs);
709 return NULL;
710 }
711
712 objs[0] = npps->mgr_event;
713 *count = 1;
714 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
715 while (conn) {
716 if ((objs[*count] = conn->ovl.hEvent))
717 (*count)++;
718 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
719 }
720 LeaveCriticalSection(&protseq->cs);
721 return objs;
722 }
723
724 static void rpcrt4_protseq_np_free_wait_array(RpcServerProtseq *protseq, void *array)
725 {
726 HeapFree(GetProcessHeap(), 0, array);
727 }
728
729 static int rpcrt4_protseq_np_wait_for_new_connection(RpcServerProtseq *protseq, unsigned int count, void *wait_array)
730 {
731 HANDLE b_handle;
732 HANDLE *objs = wait_array;
733 DWORD res;
734 RpcConnection *cconn;
735 RpcConnection_np *conn;
736
737 if (!objs)
738 return -1;
739
740 do
741 {
742 /* an alertable wait isn't strictly necessary, but due to our
743 * overlapped I/O implementation in Wine we need to free some memory
744 * by the file user APC being called, even if no completion routine was
745 * specified at the time of starting the async operation */
746 res = WaitForMultipleObjectsEx(count, objs, FALSE, INFINITE, TRUE);
747 } while (res == WAIT_IO_COMPLETION);
748
749 if (res == WAIT_OBJECT_0)
750 return 0;
751 else if (res == WAIT_FAILED)
752 {
753 ERR("wait failed with error %d\n", GetLastError());
754 return -1;
755 }
756 else
757 {
758 b_handle = objs[res - WAIT_OBJECT_0];
759 /* find which connection got a RPC */
760 EnterCriticalSection(&protseq->cs);
761 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
762 while (conn) {
763 if (b_handle == conn->ovl.hEvent) break;
764 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
765 }
766 cconn = NULL;
767 if (conn)
768 RPCRT4_SpawnConnection(&cconn, &conn->common);
769 else
770 ERR("failed to locate connection for handle %p\n", b_handle);
771 LeaveCriticalSection(&protseq->cs);
772 if (cconn)
773 {
774 RPCRT4_new_client(cconn);
775 return 1;
776 }
777 else return -1;
778 }
779 }
780
781 static size_t rpcrt4_ncalrpc_get_top_of_tower(unsigned char *tower_data,
782 const char *networkaddr,
783 const char *endpoint)
784 {
785 twr_empty_floor_t *pipe_floor;
786 size_t size;
787 size_t endpoint_size;
788
789 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
790
791 endpoint_size = strlen(endpoint) + 1;
792 size = sizeof(*pipe_floor) + endpoint_size;
793
794 if (!tower_data)
795 return size;
796
797 pipe_floor = (twr_empty_floor_t *)tower_data;
798
799 tower_data += sizeof(*pipe_floor);
800
801 pipe_floor->count_lhs = sizeof(pipe_floor->protid);
802 pipe_floor->protid = EPM_PROTOCOL_PIPE;
803 pipe_floor->count_rhs = endpoint_size;
804
805 memcpy(tower_data, endpoint, endpoint_size);
806
807 return size;
808 }
809
810 static RPC_STATUS rpcrt4_ncalrpc_parse_top_of_tower(const unsigned char *tower_data,
811 size_t tower_size,
812 char **networkaddr,
813 char **endpoint)
814 {
815 const twr_empty_floor_t *pipe_floor = (const twr_empty_floor_t *)tower_data;
816
817 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
818
819 if (tower_size < sizeof(*pipe_floor))
820 return EPT_S_NOT_REGISTERED;
821
822 tower_data += sizeof(*pipe_floor);
823 tower_size -= sizeof(*pipe_floor);
824
825 if ((pipe_floor->count_lhs != sizeof(pipe_floor->protid)) ||
826 (pipe_floor->protid != EPM_PROTOCOL_PIPE) ||
827 (pipe_floor->count_rhs > tower_size) ||
828 (tower_data[pipe_floor->count_rhs - 1] != '\0'))
829 return EPT_S_NOT_REGISTERED;
830
831 if (networkaddr)
832 *networkaddr = NULL;
833
834 if (endpoint)
835 {
836 *endpoint = I_RpcAllocate(pipe_floor->count_rhs);
837 if (!*endpoint)
838 return RPC_S_OUT_OF_RESOURCES;
839 memcpy(*endpoint, tower_data, pipe_floor->count_rhs);
840 }
841
842 return RPC_S_OK;
843 }
844
845 static BOOL rpcrt4_ncalrpc_is_authorized(RpcConnection *conn)
846 {
847 return FALSE;
848 }
849
850 static RPC_STATUS rpcrt4_ncalrpc_authorize(RpcConnection *conn, BOOL first_time,
851 unsigned char *in_buffer,
852 unsigned int in_size,
853 unsigned char *out_buffer,
854 unsigned int *out_size)
855 {
856 /* since this protocol is local to the machine there is no need to
857 * authenticate the caller */
858 *out_size = 0;
859 return RPC_S_OK;
860 }
861
862 static RPC_STATUS rpcrt4_ncalrpc_secure_packet(RpcConnection *conn,
863 enum secure_packet_direction dir,
864 RpcPktHdr *hdr, unsigned int hdr_size,
865 unsigned char *stub_data, unsigned int stub_data_size,
866 RpcAuthVerifier *auth_hdr,
867 unsigned char *auth_value, unsigned int auth_value_size)
868 {
869 /* since this protocol is local to the machine there is no need to secure
870 * the packet */
871 return RPC_S_OK;
872 }
873
874 static RPC_STATUS rpcrt4_ncalrpc_inquire_auth_client(
875 RpcConnection *conn, RPC_AUTHZ_HANDLE *privs, RPC_WSTR *server_princ_name,
876 ULONG *authn_level, ULONG *authn_svc, ULONG *authz_svc, ULONG flags)
877 {
878 TRACE("(%p, %p, %p, %p, %p, %p, 0x%x)\n", conn, privs,
879 server_princ_name, authn_level, authn_svc, authz_svc, flags);
880
881 if (privs)
882 {
883 FIXME("privs not implemented\n");
884 *privs = NULL;
885 }
886 if (server_princ_name)
887 {
888 FIXME("server_princ_name not implemented\n");
889 *server_princ_name = NULL;
890 }
891 if (authn_level) *authn_level = RPC_C_AUTHN_LEVEL_PKT_PRIVACY;
892 if (authn_svc) *authn_svc = RPC_C_AUTHN_WINNT;
893 if (authz_svc)
894 {
895 FIXME("authorization service not implemented\n");
896 *authz_svc = RPC_C_AUTHZ_NONE;
897 }
898 if (flags)
899 FIXME("flags 0x%x not implemented\n", flags);
900
901 return RPC_S_OK;
902 }
903
904 /**** ncacn_ip_tcp support ****/
905
906 static size_t rpcrt4_ip_tcp_get_top_of_tower(unsigned char *tower_data,
907 const char *networkaddr,
908 unsigned char tcp_protid,
909 const char *endpoint)
910 {
911 twr_tcp_floor_t *tcp_floor;
912 twr_ipv4_floor_t *ipv4_floor;
913 struct addrinfo *ai;
914 struct addrinfo hints;
915 int ret;
916 size_t size = sizeof(*tcp_floor) + sizeof(*ipv4_floor);
917
918 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
919
920 if (!tower_data)
921 return size;
922
923 tcp_floor = (twr_tcp_floor_t *)tower_data;
924 tower_data += sizeof(*tcp_floor);
925
926 ipv4_floor = (twr_ipv4_floor_t *)tower_data;
927
928 tcp_floor->count_lhs = sizeof(tcp_floor->protid);
929 tcp_floor->protid = tcp_protid;
930 tcp_floor->count_rhs = sizeof(tcp_floor->port);
931
932 ipv4_floor->count_lhs = sizeof(ipv4_floor->protid);
933 ipv4_floor->protid = EPM_PROTOCOL_IP;
934 ipv4_floor->count_rhs = sizeof(ipv4_floor->ipv4addr);
935
936 hints.ai_flags = AI_NUMERICHOST;
937 /* FIXME: only support IPv4 at the moment. how is IPv6 represented by the EPM? */
938 hints.ai_family = PF_INET;
939 hints.ai_socktype = SOCK_STREAM;
940 hints.ai_protocol = IPPROTO_TCP;
941 hints.ai_addrlen = 0;
942 hints.ai_addr = NULL;
943 hints.ai_canonname = NULL;
944 hints.ai_next = NULL;
945
946 ret = getaddrinfo(networkaddr, endpoint, &hints, &ai);
947 if (ret)
948 {
949 ret = getaddrinfo("0.0.0.0", endpoint, &hints, &ai);
950 if (ret)
951 {
952 ERR("getaddrinfo failed: %s\n", gai_strerror(ret));
953 return 0;
954 }
955 }
956
957 if (ai->ai_family == PF_INET)
958 {
959 const struct sockaddr_in *sin = (const struct sockaddr_in *)ai->ai_addr;
960 tcp_floor->port = sin->sin_port;
961 ipv4_floor->ipv4addr = sin->sin_addr.s_addr;
962 }
963 else
964 {
965 ERR("unexpected protocol family %d\n", ai->ai_family);
966 return 0;
967 }
968
969 freeaddrinfo(ai);
970
971 return size;
972 }
973
974 static RPC_STATUS rpcrt4_ip_tcp_parse_top_of_tower(const unsigned char *tower_data,
975 size_t tower_size,
976 char **networkaddr,
977 unsigned char tcp_protid,
978 char **endpoint)
979 {
980 const twr_tcp_floor_t *tcp_floor = (const twr_tcp_floor_t *)tower_data;
981 const twr_ipv4_floor_t *ipv4_floor;
982 struct in_addr in_addr;
983
984 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
985
986 if (tower_size < sizeof(*tcp_floor))
987 return EPT_S_NOT_REGISTERED;
988
989 tower_data += sizeof(*tcp_floor);
990 tower_size -= sizeof(*tcp_floor);
991
992 if (tower_size < sizeof(*ipv4_floor))
993 return EPT_S_NOT_REGISTERED;
994
995 ipv4_floor = (const twr_ipv4_floor_t *)tower_data;
996
997 if ((tcp_floor->count_lhs != sizeof(tcp_floor->protid)) ||
998 (tcp_floor->protid != tcp_protid) ||
999 (tcp_floor->count_rhs != sizeof(tcp_floor->port)) ||
1000 (ipv4_floor->count_lhs != sizeof(ipv4_floor->protid)) ||
1001 (ipv4_floor->protid != EPM_PROTOCOL_IP) ||
1002 (ipv4_floor->count_rhs != sizeof(ipv4_floor->ipv4addr)))
1003 return EPT_S_NOT_REGISTERED;
1004
1005 if (endpoint)
1006 {
1007 *endpoint = I_RpcAllocate(6 /* sizeof("65535") + 1 */);
1008 if (!*endpoint)
1009 return RPC_S_OUT_OF_RESOURCES;
1010 sprintf(*endpoint, "%u", ntohs(tcp_floor->port));
1011 }
1012
1013 if (networkaddr)
1014 {
1015 *networkaddr = I_RpcAllocate(INET_ADDRSTRLEN);
1016 if (!*networkaddr)
1017 {
1018 if (endpoint)
1019 {
1020 I_RpcFree(*endpoint);
1021 *endpoint = NULL;
1022 }
1023 return RPC_S_OUT_OF_RESOURCES;
1024 }
1025 in_addr.s_addr = ipv4_floor->ipv4addr;
1026 if (!inet_ntop(AF_INET, &in_addr, *networkaddr, INET_ADDRSTRLEN))
1027 {
1028 ERR("inet_ntop: %s\n", strerror(errno));
1029 I_RpcFree(*networkaddr);
1030 *networkaddr = NULL;
1031 if (endpoint)
1032 {
1033 I_RpcFree(*endpoint);
1034 *endpoint = NULL;
1035 }
1036 return EPT_S_NOT_REGISTERED;
1037 }
1038 }
1039
1040 return RPC_S_OK;
1041 }
1042
1043 typedef struct _RpcConnection_tcp
1044 {
1045 RpcConnection common;
1046 int sock;
1047 #ifdef HAVE_SOCKETPAIR
1048 int cancel_fds[2];
1049 #else
1050 HANDLE sock_event;
1051 HANDLE cancel_event;
1052 #endif
1053 } RpcConnection_tcp;
1054
1055 #ifdef HAVE_SOCKETPAIR
1056
1057 static BOOL rpcrt4_sock_wait_init(RpcConnection_tcp *tcpc)
1058 {
1059 if (socketpair(PF_UNIX, SOCK_STREAM, 0, tcpc->cancel_fds) < 0)
1060 {
1061 ERR("socketpair() failed: %s\n", strerror(errno));
1062 return FALSE;
1063 }
1064 return TRUE;
1065 }
1066
1067 static BOOL rpcrt4_sock_wait_for_recv(RpcConnection_tcp *tcpc)
1068 {
1069 struct pollfd pfds[2];
1070 pfds[0].fd = tcpc->sock;
1071 pfds[0].events = POLLIN;
1072 pfds[1].fd = tcpc->cancel_fds[0];
1073 pfds[1].events = POLLIN;
1074 if (poll(pfds, 2, -1 /* infinite */) == -1 && errno != EINTR)
1075 {
1076 ERR("poll() failed: %s\n", strerror(errno));
1077 return FALSE;
1078 }
1079 if (pfds[1].revents & POLLIN) /* canceled */
1080 {
1081 char dummy;
1082 read(pfds[1].fd, &dummy, sizeof(dummy));
1083 return FALSE;
1084 }
1085 return TRUE;
1086 }
1087
1088 static BOOL rpcrt4_sock_wait_for_send(RpcConnection_tcp *tcpc)
1089 {
1090 struct pollfd pfd;
1091 pfd.fd = tcpc->sock;
1092 pfd.events = POLLOUT;
1093 if (poll(&pfd, 1, -1 /* infinite */) == -1 && errno != EINTR)
1094 {
1095 ERR("poll() failed: %s\n", strerror(errno));
1096 return FALSE;
1097 }
1098 return TRUE;
1099 }
1100
1101 static void rpcrt4_sock_wait_cancel(RpcConnection_tcp *tcpc)
1102 {
1103 char dummy = 1;
1104
1105 write(tcpc->cancel_fds[1], &dummy, 1);
1106 }
1107
1108 static void rpcrt4_sock_wait_destroy(RpcConnection_tcp *tcpc)
1109 {
1110 close(tcpc->cancel_fds[0]);
1111 close(tcpc->cancel_fds[1]);
1112 }
1113
1114 #else /* HAVE_SOCKETPAIR */
1115
1116 static BOOL rpcrt4_sock_wait_init(RpcConnection_tcp *tcpc)
1117 {
1118 static BOOL wsa_inited;
1119 if (!wsa_inited)
1120 {
1121 WSADATA wsadata;
1122 WSAStartup(MAKEWORD(2, 2), &wsadata);
1123 /* Note: WSAStartup can be called more than once so we don't bother with
1124 * making accesses to wsa_inited thread-safe */
1125 wsa_inited = TRUE;
1126 }
1127 tcpc->sock_event = CreateEventW(NULL, FALSE, FALSE, NULL);
1128 tcpc->cancel_event = CreateEventW(NULL, FALSE, FALSE, NULL);
1129 if (!tcpc->sock_event || !tcpc->cancel_event)
1130 {
1131 ERR("event creation failed\n");
1132 if (tcpc->sock_event) CloseHandle(tcpc->sock_event);
1133 return FALSE;
1134 }
1135 return TRUE;
1136 }
1137
1138 static BOOL rpcrt4_sock_wait_for_recv(RpcConnection_tcp *tcpc)
1139 {
1140 HANDLE wait_handles[2];
1141 DWORD res;
1142 if (WSAEventSelect(tcpc->sock, tcpc->sock_event, FD_READ | FD_CLOSE) == SOCKET_ERROR)
1143 {
1144 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1145 return FALSE;
1146 }
1147 wait_handles[0] = tcpc->sock_event;
1148 wait_handles[1] = tcpc->cancel_event;
1149 res = WaitForMultipleObjects(2, wait_handles, FALSE, INFINITE);
1150 switch (res)
1151 {
1152 case WAIT_OBJECT_0:
1153 return TRUE;
1154 case WAIT_OBJECT_0 + 1:
1155 return FALSE;
1156 default:
1157 ERR("WaitForMultipleObjects() failed with error %d\n", GetLastError());
1158 return FALSE;
1159 }
1160 }
1161
1162 static BOOL rpcrt4_sock_wait_for_send(RpcConnection_tcp *tcpc)
1163 {
1164 DWORD res;
1165 if (WSAEventSelect(tcpc->sock, tcpc->sock_event, FD_WRITE | FD_CLOSE) == SOCKET_ERROR)
1166 {
1167 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1168 return FALSE;
1169 }
1170 res = WaitForSingleObject(tcpc->sock_event, INFINITE);
1171 switch (res)
1172 {
1173 case WAIT_OBJECT_0:
1174 return TRUE;
1175 default:
1176 ERR("WaitForMultipleObjects() failed with error %d\n", GetLastError());
1177 return FALSE;
1178 }
1179 }
1180
1181 static void rpcrt4_sock_wait_cancel(RpcConnection_tcp *tcpc)
1182 {
1183 SetEvent(tcpc->cancel_event);
1184 }
1185
1186 static void rpcrt4_sock_wait_destroy(RpcConnection_tcp *tcpc)
1187 {
1188 CloseHandle(tcpc->sock_event);
1189 CloseHandle(tcpc->cancel_event);
1190 }
1191
1192 #endif
1193
1194 static RpcConnection *rpcrt4_conn_tcp_alloc(void)
1195 {
1196 RpcConnection_tcp *tcpc;
1197 tcpc = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_tcp));
1198 if (tcpc == NULL)
1199 return NULL;
1200 tcpc->sock = -1;
1201 if (!rpcrt4_sock_wait_init(tcpc))
1202 {
1203 HeapFree(GetProcessHeap(), 0, tcpc);
1204 return NULL;
1205 }
1206 return &tcpc->common;
1207 }
1208
1209 static RPC_STATUS rpcrt4_ncacn_ip_tcp_open(RpcConnection* Connection)
1210 {
1211 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1212 int sock;
1213 int ret;
1214 struct addrinfo *ai;
1215 struct addrinfo *ai_cur;
1216 struct addrinfo hints;
1217
1218 TRACE("(%s, %s)\n", Connection->NetworkAddr, Connection->Endpoint);
1219
1220 if (tcpc->sock != -1)
1221 return RPC_S_OK;
1222
1223 hints.ai_flags = 0;
1224 hints.ai_family = PF_UNSPEC;
1225 hints.ai_socktype = SOCK_STREAM;
1226 hints.ai_protocol = IPPROTO_TCP;
1227 hints.ai_addrlen = 0;
1228 hints.ai_addr = NULL;
1229 hints.ai_canonname = NULL;
1230 hints.ai_next = NULL;
1231
1232 ret = getaddrinfo(Connection->NetworkAddr, Connection->Endpoint, &hints, &ai);
1233 if (ret)
1234 {
1235 ERR("getaddrinfo for %s:%s failed: %s\n", Connection->NetworkAddr,
1236 Connection->Endpoint, gai_strerror(ret));
1237 return RPC_S_SERVER_UNAVAILABLE;
1238 }
1239
1240 for (ai_cur = ai; ai_cur; ai_cur = ai_cur->ai_next)
1241 {
1242 int val;
1243 u_long nonblocking;
1244
1245 if (ai_cur->ai_family != AF_INET && ai_cur->ai_family != AF_INET6)
1246 {
1247 TRACE("skipping non-IP/IPv6 address family\n");
1248 continue;
1249 }
1250
1251 if (TRACE_ON(rpc))
1252 {
1253 char host[256];
1254 char service[256];
1255 getnameinfo(ai_cur->ai_addr, ai_cur->ai_addrlen,
1256 host, sizeof(host), service, sizeof(service),
1257 NI_NUMERICHOST | NI_NUMERICSERV);
1258 TRACE("trying %s:%s\n", host, service);
1259 }
1260
1261 sock = socket(ai_cur->ai_family, ai_cur->ai_socktype, ai_cur->ai_protocol);
1262 if (sock == -1)
1263 {
1264 WARN("socket() failed: %s\n", strerror(errno));
1265 continue;
1266 }
1267
1268 if (0>connect(sock, ai_cur->ai_addr, ai_cur->ai_addrlen))
1269 {
1270 WARN("connect() failed: %s\n", strerror(errno));
1271 closesocket(sock);
1272 continue;
1273 }
1274
1275 /* RPC depends on having minimal latency so disable the Nagle algorithm */
1276 val = 1;
1277 setsockopt(sock, SOL_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
1278 nonblocking = 1;
1279 ioctlsocket(sock, FIONBIO, &nonblocking);
1280
1281 tcpc->sock = sock;
1282
1283 freeaddrinfo(ai);
1284 TRACE("connected\n");
1285 return RPC_S_OK;
1286 }
1287
1288 freeaddrinfo(ai);
1289 ERR("couldn't connect to %s:%s\n", Connection->NetworkAddr, Connection->Endpoint);
1290 return RPC_S_SERVER_UNAVAILABLE;
1291 }
1292
1293 static RPC_STATUS rpcrt4_protseq_ncacn_ip_tcp_open_endpoint(RpcServerProtseq *protseq, const char *endpoint)
1294 {
1295 RPC_STATUS status = RPC_S_CANT_CREATE_ENDPOINT;
1296 int sock;
1297 int ret;
1298 struct addrinfo *ai;
1299 struct addrinfo *ai_cur;
1300 struct addrinfo hints;
1301 RpcConnection *first_connection = NULL;
1302
1303 TRACE("(%p, %s)\n", protseq, endpoint);
1304
1305 hints.ai_flags = AI_PASSIVE /* for non-localhost addresses */;
1306 hints.ai_family = PF_UNSPEC;
1307 hints.ai_socktype = SOCK_STREAM;
1308 hints.ai_protocol = IPPROTO_TCP;
1309 hints.ai_addrlen = 0;
1310 hints.ai_addr = NULL;
1311 hints.ai_canonname = NULL;
1312 hints.ai_next = NULL;
1313
1314 ret = getaddrinfo(NULL, endpoint ? endpoint : "0", &hints, &ai);
1315 if (ret)
1316 {
1317 ERR("getaddrinfo for port %s failed: %s\n", endpoint,
1318 gai_strerror(ret));
1319 if ((ret == EAI_SERVICE) || (ret == EAI_NONAME))
1320 return RPC_S_INVALID_ENDPOINT_FORMAT;
1321 return RPC_S_CANT_CREATE_ENDPOINT;
1322 }
1323
1324 for (ai_cur = ai; ai_cur; ai_cur = ai_cur->ai_next)
1325 {
1326 RpcConnection_tcp *tcpc;
1327 RPC_STATUS create_status;
1328 struct sockaddr_storage sa;
1329 socklen_t sa_len;
1330 char service[NI_MAXSERV];
1331 u_long nonblocking;
1332
1333 if (ai_cur->ai_family != AF_INET && ai_cur->ai_family != AF_INET6)
1334 {
1335 TRACE("skipping non-IP/IPv6 address family\n");
1336 continue;
1337 }
1338
1339 if (TRACE_ON(rpc))
1340 {
1341 char host[256];
1342 getnameinfo(ai_cur->ai_addr, ai_cur->ai_addrlen,
1343 host, sizeof(host), service, sizeof(service),
1344 NI_NUMERICHOST | NI_NUMERICSERV);
1345 TRACE("trying %s:%s\n", host, service);
1346 }
1347
1348 sock = socket(ai_cur->ai_family, ai_cur->ai_socktype, ai_cur->ai_protocol);
1349 if (sock == -1)
1350 {
1351 WARN("socket() failed: %s\n", strerror(errno));
1352 status = RPC_S_CANT_CREATE_ENDPOINT;
1353 continue;
1354 }
1355
1356 ret = bind(sock, ai_cur->ai_addr, ai_cur->ai_addrlen);
1357 if (ret < 0)
1358 {
1359 WARN("bind failed: %s\n", strerror(errno));
1360 closesocket(sock);
1361 if (errno == EADDRINUSE)
1362 status = RPC_S_DUPLICATE_ENDPOINT;
1363 else
1364 status = RPC_S_CANT_CREATE_ENDPOINT;
1365 continue;
1366 }
1367
1368 sa_len = sizeof(sa);
1369 if (getsockname(sock, (struct sockaddr *)&sa, &sa_len))
1370 {
1371 WARN("getsockname() failed: %s\n", strerror(errno));
1372 status = RPC_S_CANT_CREATE_ENDPOINT;
1373 continue;
1374 }
1375
1376 ret = getnameinfo((struct sockaddr *)&sa, sa_len,
1377 NULL, 0, service, sizeof(service),
1378 NI_NUMERICSERV);
1379 if (ret)
1380 {
1381 WARN("getnameinfo failed: %s\n", gai_strerror(ret));
1382 status = RPC_S_CANT_CREATE_ENDPOINT;
1383 continue;
1384 }
1385
1386 create_status = RPCRT4_CreateConnection((RpcConnection **)&tcpc, TRUE,
1387 protseq->Protseq, NULL,
1388 service, NULL, NULL, NULL);
1389 if (create_status != RPC_S_OK)
1390 {
1391 closesocket(sock);
1392 status = create_status;
1393 continue;
1394 }
1395
1396 tcpc->sock = sock;
1397 ret = listen(sock, protseq->MaxCalls);
1398 if (ret < 0)
1399 {
1400 WARN("listen failed: %s\n", strerror(errno));
1401 RPCRT4_DestroyConnection(&tcpc->common);
1402 status = RPC_S_OUT_OF_RESOURCES;
1403 continue;
1404 }
1405 /* need a non-blocking socket, otherwise accept() has a potential
1406 * race-condition (poll() says it is readable, connection drops,
1407 * and accept() blocks until the next connection comes...)
1408 */
1409 nonblocking = 1;
1410 ret = ioctlsocket(sock, FIONBIO, &nonblocking);
1411 if (ret < 0)
1412 {
1413 WARN("couldn't make socket non-blocking, error %d\n", ret);
1414 RPCRT4_DestroyConnection(&tcpc->common);
1415 status = RPC_S_OUT_OF_RESOURCES;
1416 continue;
1417 }
1418
1419 tcpc->common.Next = first_connection;
1420 first_connection = &tcpc->common;
1421
1422 /* since IPv4 and IPv6 share the same port space, we only need one
1423 * successful bind to listen for both */
1424 break;
1425 }
1426
1427 freeaddrinfo(ai);
1428
1429 /* if at least one connection was created for an endpoint then
1430 * return success */
1431 if (first_connection)
1432 {
1433 RpcConnection *conn;
1434
1435 /* find last element in list */
1436 for (conn = first_connection; conn->Next; conn = conn->Next)
1437 ;
1438
1439 EnterCriticalSection(&protseq->cs);
1440 conn->Next = protseq->conn;
1441 protseq->conn = first_connection;
1442 LeaveCriticalSection(&protseq->cs);
1443
1444 TRACE("listening on %s\n", endpoint);
1445 return RPC_S_OK;
1446 }
1447
1448 ERR("couldn't listen on port %s\n", endpoint);
1449 return status;
1450 }
1451
1452 static RPC_STATUS rpcrt4_conn_tcp_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
1453 {
1454 int ret;
1455 struct sockaddr_in address;
1456 socklen_t addrsize;
1457 RpcConnection_tcp *server = (RpcConnection_tcp*) old_conn;
1458 RpcConnection_tcp *client = (RpcConnection_tcp*) new_conn;
1459 u_long nonblocking;
1460
1461 addrsize = sizeof(address);
1462 ret = accept(server->sock, (struct sockaddr*) &address, &addrsize);
1463 if (ret < 0)
1464 {
1465 ERR("Failed to accept a TCP connection: error %d\n", ret);
1466 return RPC_S_OUT_OF_RESOURCES;
1467 }
1468 nonblocking = 1;
1469 ioctlsocket(ret, FIONBIO, &nonblocking);
1470 client->sock = ret;
1471 TRACE("Accepted a new TCP connection\n");
1472 return RPC_S_OK;
1473 }
1474
1475 static int rpcrt4_conn_tcp_read(RpcConnection *Connection,
1476 void *buffer, unsigned int count)
1477 {
1478 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1479 int bytes_read = 0;
1480 while (bytes_read != count)
1481 {
1482 int r = recv(tcpc->sock, (char *)buffer + bytes_read, count - bytes_read, 0);
1483 if (!r)
1484 return -1;
1485 else if (r > 0)
1486 bytes_read += r;
1487 else if (errno != EAGAIN)
1488 {
1489 WARN("recv() failed: %s\n", strerror(errno));
1490 return -1;
1491 }
1492 else
1493 {
1494 if (!rpcrt4_sock_wait_for_recv(tcpc))
1495 return -1;
1496 }
1497 }
1498 TRACE("%d %p %u -> %d\n", tcpc->sock, buffer, count, bytes_read);
1499 return bytes_read;
1500 }
1501
1502 static int rpcrt4_conn_tcp_write(RpcConnection *Connection,
1503 const void *buffer, unsigned int count)
1504 {
1505 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1506 int bytes_written = 0;
1507 while (bytes_written != count)
1508 {
1509 int r = send(tcpc->sock, (const char *)buffer + bytes_written, count - bytes_written, 0);
1510 if (r >= 0)
1511 bytes_written += r;
1512 else if (errno != EAGAIN)
1513 return -1;
1514 else
1515 {
1516 if (!rpcrt4_sock_wait_for_send(tcpc))
1517 return -1;
1518 }
1519 }
1520 TRACE("%d %p %u -> %d\n", tcpc->sock, buffer, count, bytes_written);
1521 return bytes_written;
1522 }
1523
1524 static int rpcrt4_conn_tcp_close(RpcConnection *Connection)
1525 {
1526 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1527
1528 TRACE("%d\n", tcpc->sock);
1529
1530 if (tcpc->sock != -1)
1531 closesocket(tcpc->sock);
1532 tcpc->sock = -1;
1533 rpcrt4_sock_wait_destroy(tcpc);
1534 return 0;
1535 }
1536
1537 static void rpcrt4_conn_tcp_cancel_call(RpcConnection *Connection)
1538 {
1539 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1540 TRACE("%p\n", Connection);
1541 rpcrt4_sock_wait_cancel(tcpc);
1542 }
1543
1544 static int rpcrt4_conn_tcp_wait_for_incoming_data(RpcConnection *Connection)
1545 {
1546 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1547
1548 TRACE("%p\n", Connection);
1549
1550 if (!rpcrt4_sock_wait_for_recv(tcpc))
1551 return -1;
1552 return 0;
1553 }
1554
1555 static size_t rpcrt4_ncacn_ip_tcp_get_top_of_tower(unsigned char *tower_data,
1556 const char *networkaddr,
1557 const char *endpoint)
1558 {
1559 return rpcrt4_ip_tcp_get_top_of_tower(tower_data, networkaddr,
1560 EPM_PROTOCOL_TCP, endpoint);
1561 }
1562
1563 #ifdef HAVE_SOCKETPAIR
1564
1565 typedef struct _RpcServerProtseq_sock
1566 {
1567 RpcServerProtseq common;
1568 int mgr_event_rcv;
1569 int mgr_event_snd;
1570 } RpcServerProtseq_sock;
1571
1572 static RpcServerProtseq *rpcrt4_protseq_sock_alloc(void)
1573 {
1574 RpcServerProtseq_sock *ps = HeapAlloc(GetProcessHeap(), 0, sizeof(*ps));
1575 if (ps)
1576 {
1577 int fds[2];
1578 if (!socketpair(PF_UNIX, SOCK_DGRAM, 0, fds))
1579 {
1580 fcntl(fds[0], F_SETFL, O_NONBLOCK);
1581 fcntl(fds[1], F_SETFL, O_NONBLOCK);
1582 ps->mgr_event_rcv = fds[0];
1583 ps->mgr_event_snd = fds[1];
1584 }
1585 else
1586 {
1587 ERR("socketpair failed with error %s\n", strerror(errno));
1588 HeapFree(GetProcessHeap(), 0, ps);
1589 return NULL;
1590 }
1591 }
1592 return &ps->common;
1593 }
1594
1595 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq *protseq)
1596 {
1597 RpcServerProtseq_sock *sockps = CONTAINING_RECORD(protseq, RpcServerProtseq_sock, common);
1598 char dummy = 1;
1599 write(sockps->mgr_event_snd, &dummy, sizeof(dummy));
1600 }
1601
1602 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq *protseq, void *prev_array, unsigned int *count)
1603 {
1604 struct pollfd *poll_info = prev_array;
1605 RpcConnection_tcp *conn;
1606 RpcServerProtseq_sock *sockps = CONTAINING_RECORD(protseq, RpcServerProtseq_sock, common);
1607
1608 EnterCriticalSection(&protseq->cs);
1609
1610 /* open and count connections */
1611 *count = 1;
1612 conn = (RpcConnection_tcp *)protseq->conn;
1613 while (conn) {
1614 if (conn->sock != -1)
1615 (*count)++;
1616 conn = (RpcConnection_tcp *)conn->common.Next;
1617 }
1618
1619 /* make array of connections */
1620 if (poll_info)
1621 poll_info = HeapReAlloc(GetProcessHeap(), 0, poll_info, *count*sizeof(*poll_info));
1622 else
1623 poll_info = HeapAlloc(GetProcessHeap(), 0, *count*sizeof(*poll_info));
1624 if (!poll_info)
1625 {
1626 ERR("couldn't allocate poll_info\n");
1627 LeaveCriticalSection(&protseq->cs);
1628 return NULL;
1629 }
1630
1631 poll_info[0].fd = sockps->mgr_event_rcv;
1632 poll_info[0].events = POLLIN;
1633 *count = 1;
1634 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_tcp, common);
1635 while (conn) {
1636 if (conn->sock != -1)
1637 {
1638 poll_info[*count].fd = conn->sock;
1639 poll_info[*count].events = POLLIN;
1640 (*count)++;
1641 }
1642 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_tcp, common);
1643 }
1644 LeaveCriticalSection(&protseq->cs);
1645 return poll_info;
1646 }
1647
1648 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq *protseq, void *array)
1649 {
1650 HeapFree(GetProcessHeap(), 0, array);
1651 }
1652
1653 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq *protseq, unsigned int count, void *wait_array)
1654 {
1655 struct pollfd *poll_info = wait_array;
1656 int ret;
1657 unsigned int i;
1658 RpcConnection *cconn;
1659 RpcConnection_tcp *conn;
1660
1661 if (!poll_info)
1662 return -1;
1663
1664 ret = poll(poll_info, count, -1);
1665 if (ret < 0)
1666 {
1667 ERR("poll failed with error %d\n", ret);
1668 return -1;
1669 }
1670
1671 for (i = 0; i < count; i++)
1672 if (poll_info[i].revents & POLLIN)
1673 {
1674 /* RPC server event */
1675 if (i == 0)
1676 {
1677 char dummy;
1678 read(poll_info[0].fd, &dummy, sizeof(dummy));
1679 return 0;
1680 }
1681
1682 /* find which connection got a RPC */
1683 EnterCriticalSection(&protseq->cs);
1684 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_tcp, common);
1685 while (conn) {
1686 if (poll_info[i].fd == conn->sock) break;
1687 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_tcp, common);
1688 }
1689 cconn = NULL;
1690 if (conn)
1691 RPCRT4_SpawnConnection(&cconn, &conn->common);
1692 else
1693 ERR("failed to locate connection for fd %d\n", poll_info[i].fd);
1694 LeaveCriticalSection(&protseq->cs);
1695 if (cconn)
1696 RPCRT4_new_client(cconn);
1697 else
1698 return -1;
1699 }
1700
1701 return 1;
1702 }
1703
1704 #else /* HAVE_SOCKETPAIR */
1705
1706 typedef struct _RpcServerProtseq_sock
1707 {
1708 RpcServerProtseq common;
1709 HANDLE mgr_event;
1710 } RpcServerProtseq_sock;
1711
1712 static RpcServerProtseq *rpcrt4_protseq_sock_alloc(void)
1713 {
1714 RpcServerProtseq_sock *ps = HeapAlloc(GetProcessHeap(), 0, sizeof(*ps));
1715 if (ps)
1716 {
1717 static BOOL wsa_inited;
1718 if (!wsa_inited)
1719 {
1720 WSADATA wsadata;
1721 WSAStartup(MAKEWORD(2, 2), &wsadata);
1722 /* Note: WSAStartup can be called more than once so we don't bother with
1723 * making accesses to wsa_inited thread-safe */
1724 wsa_inited = TRUE;
1725 }
1726 ps->mgr_event = CreateEventW(NULL, FALSE, FALSE, NULL);
1727 }
1728 return &ps->common;
1729 }
1730
1731 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq *protseq)
1732 {
1733 RpcServerProtseq_sock *sockps = CONTAINING_RECORD(protseq, RpcServerProtseq_sock, common);
1734 SetEvent(sockps->mgr_event);
1735 }
1736
1737 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq *protseq, void *prev_array, unsigned int *count)
1738 {
1739 HANDLE *objs = prev_array;
1740 RpcConnection_tcp *conn;
1741 RpcServerProtseq_sock *sockps = CONTAINING_RECORD(protseq, RpcServerProtseq_sock, common);
1742
1743 EnterCriticalSection(&protseq->cs);
1744
1745 /* open and count connections */
1746 *count = 1;
1747 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_tcp, common);
1748 while (conn)
1749 {
1750 if (conn->sock != -1)
1751 (*count)++;
1752 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_tcp, common);
1753 }
1754
1755 /* make array of connections */
1756 if (objs)
1757 objs = HeapReAlloc(GetProcessHeap(), 0, objs, *count*sizeof(HANDLE));
1758 else
1759 objs = HeapAlloc(GetProcessHeap(), 0, *count*sizeof(HANDLE));
1760 if (!objs)
1761 {
1762 ERR("couldn't allocate objs\n");
1763 LeaveCriticalSection(&protseq->cs);
1764 return NULL;
1765 }
1766
1767 objs[0] = sockps->mgr_event;
1768 *count = 1;
1769 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_tcp, common);
1770 while (conn)
1771 {
1772 if (conn->sock != -1)
1773 {
1774 int res = WSAEventSelect(conn->sock, conn->sock_event, FD_ACCEPT);
1775 if (res == SOCKET_ERROR)
1776 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1777 else
1778 {
1779 objs[*count] = conn->sock_event;
1780 (*count)++;
1781 }
1782 }
1783 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_tcp, common);
1784 }
1785 LeaveCriticalSection(&protseq->cs);
1786 return objs;
1787 }
1788
1789 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq *protseq, void *array)
1790 {
1791 HeapFree(GetProcessHeap(), 0, array);
1792 }
1793
1794 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq *protseq, unsigned int count, void *wait_array)
1795 {
1796 HANDLE b_handle;
1797 HANDLE *objs = wait_array;
1798 DWORD res;
1799 RpcConnection *cconn;
1800 RpcConnection_tcp *conn;
1801
1802 if (!objs)
1803 return -1;
1804
1805 do
1806 {
1807 /* an alertable wait isn't strictly necessary, but due to our
1808 * overlapped I/O implementation in Wine we need to free some memory
1809 * by the file user APC being called, even if no completion routine was
1810 * specified at the time of starting the async operation */
1811 res = WaitForMultipleObjectsEx(count, objs, FALSE, INFINITE, TRUE);
1812 } while (res == WAIT_IO_COMPLETION);
1813
1814 if (res == WAIT_OBJECT_0)
1815 return 0;
1816 else if (res == WAIT_FAILED)
1817 {
1818 ERR("wait failed with error %d\n", GetLastError());
1819 return -1;
1820 }
1821 else
1822 {
1823 b_handle = objs[res - WAIT_OBJECT_0];
1824 /* find which connection got a RPC */
1825 EnterCriticalSection(&protseq->cs);
1826 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_tcp, common);
1827 while (conn)
1828 {
1829 if (b_handle == conn->sock_event) break;
1830 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_tcp, common);
1831 }
1832 cconn = NULL;
1833 if (conn)
1834 RPCRT4_SpawnConnection(&cconn, &conn->common);
1835 else
1836 ERR("failed to locate connection for handle %p\n", b_handle);
1837 LeaveCriticalSection(&protseq->cs);
1838 if (cconn)
1839 {
1840 RPCRT4_new_client(cconn);
1841 return 1;
1842 }
1843 else return -1;
1844 }
1845 }
1846
1847 #endif /* HAVE_SOCKETPAIR */
1848
1849 static RPC_STATUS rpcrt4_ncacn_ip_tcp_parse_top_of_tower(const unsigned char *tower_data,
1850 size_t tower_size,
1851 char **networkaddr,
1852 char **endpoint)
1853 {
1854 return rpcrt4_ip_tcp_parse_top_of_tower(tower_data, tower_size,
1855 networkaddr, EPM_PROTOCOL_TCP,
1856 endpoint);
1857 }
1858
1859 /**** ncacn_http support ****/
1860
1861 /* 60 seconds is the period native uses */
1862 #define HTTP_IDLE_TIME 60000
1863
1864 /* reference counted to avoid a race between a cancelled call's connection
1865 * being destroyed and the asynchronous InternetReadFileEx call being
1866 * completed */
1867 typedef struct _RpcHttpAsyncData
1868 {
1869 LONG refs;
1870 HANDLE completion_event;
1871 INTERNET_BUFFERSA inet_buffers;
1872 void *destination_buffer; /* the address that inet_buffers.lpvBuffer will be
1873 * copied into when the call completes */
1874 CRITICAL_SECTION cs;
1875 } RpcHttpAsyncData;
1876
1877 static ULONG RpcHttpAsyncData_AddRef(RpcHttpAsyncData *data)
1878 {
1879 return InterlockedIncrement(&data->refs);
1880 }
1881
1882 static ULONG RpcHttpAsyncData_Release(RpcHttpAsyncData *data)
1883 {
1884 ULONG refs = InterlockedDecrement(&data->refs);
1885 if (!refs)
1886 {
1887 TRACE("destroying async data %p\n", data);
1888 CloseHandle(data->completion_event);
1889 HeapFree(GetProcessHeap(), 0, data->inet_buffers.lpvBuffer);
1890 DeleteCriticalSection(&data->cs);
1891 HeapFree(GetProcessHeap(), 0, data);
1892 }
1893 return refs;
1894 }
1895
1896 typedef struct _RpcConnection_http
1897 {
1898 RpcConnection common;
1899 HINTERNET app_info;
1900 HINTERNET session;
1901 HINTERNET in_request;
1902 HINTERNET out_request;
1903 HANDLE timer_cancelled;
1904 HANDLE cancel_event;
1905 DWORD last_sent_time;
1906 ULONG bytes_received;
1907 ULONG flow_control_mark; /* send a control packet to the server when this many bytes received */
1908 ULONG flow_control_increment; /* number of bytes to increment flow_control_mark by */
1909 UUID connection_uuid;
1910 UUID in_pipe_uuid;
1911 UUID out_pipe_uuid;
1912 RpcHttpAsyncData *async_data;
1913 } RpcConnection_http;
1914
1915 static RpcConnection *rpcrt4_ncacn_http_alloc(void)
1916 {
1917 RpcConnection_http *httpc;
1918 httpc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*httpc));
1919 if (!httpc) return NULL;
1920 httpc->async_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcHttpAsyncData));
1921 if (!httpc->async_data)
1922 {
1923 HeapFree(GetProcessHeap(), 0, httpc);
1924 return NULL;
1925 }
1926 TRACE("async data = %p\n", httpc->async_data);
1927 httpc->cancel_event = CreateEventW(NULL, FALSE, FALSE, NULL);
1928 httpc->async_data->refs = 1;
1929 httpc->async_data->inet_buffers.dwStructSize = sizeof(INTERNET_BUFFERSA);
1930 httpc->async_data->inet_buffers.lpvBuffer = NULL;
1931 httpc->async_data->destination_buffer = NULL;
1932 InitializeCriticalSection(&httpc->async_data->cs);
1933 return &httpc->common;
1934 }
1935
1936 typedef struct _HttpTimerThreadData
1937 {
1938 PVOID timer_param;
1939 DWORD *last_sent_time;
1940 HANDLE timer_cancelled;
1941 } HttpTimerThreadData;
1942
1943 static VOID rpcrt4_http_keep_connection_active_timer_proc(PVOID param, BOOLEAN dummy)
1944 {
1945 HINTERNET in_request = param;
1946 RpcPktHdr *idle_pkt;
1947
1948 idle_pkt = RPCRT4_BuildHttpHeader(NDR_LOCAL_DATA_REPRESENTATION, 0x0001,
1949 0, 0);
1950 if (idle_pkt)
1951 {
1952 DWORD bytes_written;
1953 InternetWriteFile(in_request, idle_pkt, idle_pkt->common.frag_len, &bytes_written);
1954 RPCRT4_FreeHeader(idle_pkt);
1955 }
1956 }
1957
1958 static inline DWORD rpcrt4_http_timer_calc_timeout(DWORD *last_sent_time)
1959 {
1960 DWORD cur_time = GetTickCount();
1961 DWORD cached_last_sent_time = *last_sent_time;
1962 return HTTP_IDLE_TIME - (cur_time - cached_last_sent_time > HTTP_IDLE_TIME ? 0 : cur_time - cached_last_sent_time);
1963 }
1964
1965 static DWORD CALLBACK rpcrt4_http_timer_thread(PVOID param)
1966 {
1967 HttpTimerThreadData *data_in = param;
1968 HttpTimerThreadData data;
1969 DWORD timeout;
1970
1971 data = *data_in;
1972 HeapFree(GetProcessHeap(), 0, data_in);
1973
1974 for (timeout = HTTP_IDLE_TIME;
1975 WaitForSingleObject(data.timer_cancelled, timeout) == WAIT_TIMEOUT;
1976 timeout = rpcrt4_http_timer_calc_timeout(data.last_sent_time))
1977 {
1978 /* are we too soon after last send? */
1979 if (GetTickCount() - HTTP_IDLE_TIME < *data.last_sent_time)
1980 continue;
1981 rpcrt4_http_keep_connection_active_timer_proc(data.timer_param, TRUE);
1982 }
1983
1984 CloseHandle(data.timer_cancelled);
1985 return 0;
1986 }
1987
1988 static VOID WINAPI rpcrt4_http_internet_callback(
1989 HINTERNET hInternet,
1990 DWORD_PTR dwContext,
1991 DWORD dwInternetStatus,
1992 LPVOID lpvStatusInformation,
1993 DWORD dwStatusInformationLength)
1994 {
1995 RpcHttpAsyncData *async_data = (RpcHttpAsyncData *)dwContext;
1996
1997 switch (dwInternetStatus)
1998 {
1999 case INTERNET_STATUS_REQUEST_COMPLETE:
2000 TRACE("INTERNET_STATUS_REQUEST_COMPLETED\n");
2001 if (async_data)
2002 {
2003 if (async_data->inet_buffers.lpvBuffer)
2004 {
2005 EnterCriticalSection(&async_data->cs);
2006 if (async_data->destination_buffer)
2007 {
2008 memcpy(async_data->destination_buffer,
2009 async_data->inet_buffers.lpvBuffer,
2010 async_data->inet_buffers.dwBufferLength);
2011 async_data->destination_buffer = NULL;
2012 }
2013 LeaveCriticalSection(&async_data->cs);
2014 }
2015 HeapFree(GetProcessHeap(), 0, async_data->inet_buffers.lpvBuffer);
2016 async_data->inet_buffers.lpvBuffer = NULL;
2017 SetEvent(async_data->completion_event);
2018 RpcHttpAsyncData_Release(async_data);
2019 }
2020 break;
2021 }
2022 }
2023
2024 static RPC_STATUS rpcrt4_http_check_response(HINTERNET hor)
2025 {
2026 BOOL ret;
2027 DWORD status_code;
2028 DWORD size;
2029 DWORD index;
2030 WCHAR buf[32];
2031 WCHAR *status_text = buf;
2032 TRACE("\n");
2033
2034 index = 0;
2035 size = sizeof(status_code);
2036 ret = HttpQueryInfoW(hor, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &status_code, &size, &index);
2037 if (!ret)
2038 return GetLastError();
2039 if (status_code < 400)
2040 return RPC_S_OK;
2041 index = 0;
2042 size = sizeof(buf);
2043 ret = HttpQueryInfoW(hor, HTTP_QUERY_STATUS_TEXT, status_text, &size, &index);
2044 if (!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
2045 {
2046 status_text = HeapAlloc(GetProcessHeap(), 0, size);
2047 ret = HttpQueryInfoW(hor, HTTP_QUERY_STATUS_TEXT, status_text, &size, &index);
2048 }
2049
2050 ERR("server returned: %d %s\n", status_code, ret ? debugstr_w(status_text) : "<status text unavailable>");
2051 if(status_text != buf) HeapFree(GetProcessHeap(), 0, status_text);
2052
2053 if (status_code == HTTP_STATUS_DENIED)
2054 return ERROR_ACCESS_DENIED;
2055 return RPC_S_SERVER_UNAVAILABLE;
2056 }
2057
2058 static RPC_STATUS rpcrt4_http_internet_connect(RpcConnection_http *httpc)
2059 {
2060 static const WCHAR wszUserAgent[] = {'M','S','R','P','C',0};
2061 LPWSTR proxy = NULL;
2062 LPWSTR user = NULL;
2063 LPWSTR password = NULL;
2064 LPWSTR servername = NULL;
2065 const WCHAR *option;
2066 INTERNET_PORT port = INTERNET_INVALID_PORT_NUMBER; /* use default port */
2067
2068 if (httpc->common.QOS &&
2069 (httpc->common.QOS->qos->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP))
2070 {
2071 const RPC_HTTP_TRANSPORT_CREDENTIALS_W *http_cred = httpc->common.QOS->qos->u.HttpCredentials;
2072 if (http_cred->TransportCredentials)
2073 {
2074 WCHAR *p;
2075 const SEC_WINNT_AUTH_IDENTITY_W *cred = http_cred->TransportCredentials;
2076 ULONG len = cred->DomainLength + 1 + cred->UserLength;
2077 user = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
2078 if (!user)
2079 return RPC_S_OUT_OF_RESOURCES;
2080 p = user;
2081 if (cred->DomainLength)
2082 {
2083 memcpy(p, cred->Domain, cred->DomainLength * sizeof(WCHAR));
2084 p += cred->DomainLength;
2085 *p = '\\';
2086 p++;
2087 }
2088 memcpy(p, cred->User, cred->UserLength * sizeof(WCHAR));
2089 p[cred->UserLength] = 0;
2090
2091 password = RPCRT4_strndupW(cred->Password, cred->PasswordLength);
2092 }
2093 }
2094
2095 for (option = httpc->common.NetworkOptions; option;
2096 option = (strchrW(option, ',') ? strchrW(option, ',')+1 : NULL))
2097 {
2098 static const WCHAR wszRpcProxy[] = {'R','p','c','P','r','o','x','y','=',0};
2099 static const WCHAR wszHttpProxy[] = {'H','t','t','p','P','r','o','x','y','=',0};
2100
2101 if (!strncmpiW(option, wszRpcProxy, sizeof(wszRpcProxy)/sizeof(wszRpcProxy[0])-1))
2102 {
2103 const WCHAR *value_start = option + sizeof(wszRpcProxy)/sizeof(wszRpcProxy[0])-1;
2104 const WCHAR *value_end;
2105 const WCHAR *p;
2106
2107 value_end = strchrW(option, ',');
2108 if (!value_end)
2109 value_end = value_start + strlenW(value_start);
2110 for (p = value_start; p < value_end; p++)
2111 if (*p == ':')
2112 {
2113 port = atoiW(p+1);
2114 value_end = p;
2115 break;
2116 }
2117 TRACE("RpcProxy value is %s\n", debugstr_wn(value_start, value_end-value_start));
2118 servername = RPCRT4_strndupW(value_start, value_end-value_start);
2119 }
2120 else if (!strncmpiW(option, wszHttpProxy, sizeof(wszHttpProxy)/sizeof(wszHttpProxy[0])-1))
2121 {
2122 const WCHAR *value_start = option + sizeof(wszHttpProxy)/sizeof(wszHttpProxy[0])-1;
2123 const WCHAR *value_end;
2124
2125 value_end = strchrW(option, ',');
2126 if (!value_end)
2127 value_end = value_start + strlenW(value_start);
2128 TRACE("HttpProxy value is %s\n", debugstr_wn(value_start, value_end-value_start));
2129 proxy = RPCRT4_strndupW(value_start, value_end-value_start);
2130 }
2131 else
2132 FIXME("unhandled option %s\n", debugstr_w(option));
2133 }
2134
2135 httpc->app_info = InternetOpenW(wszUserAgent, proxy ? INTERNET_OPEN_TYPE_PROXY : INTERNET_OPEN_TYPE_PRECONFIG,
2136 NULL, NULL, INTERNET_FLAG_ASYNC);
2137 if (!httpc->app_info)
2138 {
2139 HeapFree(GetProcessHeap(), 0, password);
2140 HeapFree(GetProcessHeap(), 0, user);
2141 ERR("InternetOpenW failed with error %d\n", GetLastError());
2142 return RPC_S_SERVER_UNAVAILABLE;
2143 }
2144 InternetSetStatusCallbackW(httpc->app_info, rpcrt4_http_internet_callback);
2145
2146 /* if no RpcProxy option specified, set the HTTP server address to the
2147 * RPC server address */
2148 if (!servername)
2149 {
2150 servername = HeapAlloc(GetProcessHeap(), 0, (strlen(httpc->common.NetworkAddr) + 1)*sizeof(WCHAR));
2151 if (!servername)
2152 {
2153 HeapFree(GetProcessHeap(), 0, password);
2154 HeapFree(GetProcessHeap(), 0, user);
2155 return RPC_S_OUT_OF_RESOURCES;
2156 }
2157 MultiByteToWideChar(CP_ACP, 0, httpc->common.NetworkAddr, -1, servername, strlen(httpc->common.NetworkAddr) + 1);
2158 }
2159
2160 httpc->session = InternetConnectW(httpc->app_info, servername, port, user, password,
2161 INTERNET_SERVICE_HTTP, 0, 0);
2162
2163 HeapFree(GetProcessHeap(), 0, password);
2164 HeapFree(GetProcessHeap(), 0, user);
2165 HeapFree(GetProcessHeap(), 0, servername);
2166
2167 if (!httpc->session)
2168 {
2169 ERR("InternetConnectW failed with error %d\n", GetLastError());
2170 return RPC_S_SERVER_UNAVAILABLE;
2171 }
2172
2173 return RPC_S_OK;
2174 }
2175
2176 /* prepare the in pipe for use by RPC packets */
2177 static RPC_STATUS rpcrt4_http_prepare_in_pipe(HINTERNET in_request, RpcHttpAsyncData *async_data,
2178 const UUID *connection_uuid,
2179 const UUID *in_pipe_uuid,
2180 const UUID *association_uuid)
2181 {
2182 BYTE packet[44];
2183 BOOL ret;
2184 RPC_STATUS status;
2185 RpcPktHdr *hdr;
2186 INTERNET_BUFFERSW buffers_in;
2187 DWORD bytes_read, bytes_written;
2188
2189 /* prepare in pipe */
2190 ResetEvent(async_data->completion_event);
2191 RpcHttpAsyncData_AddRef(async_data);
2192 ret = HttpSendRequestW(in_request, NULL, 0, NULL, 0);
2193 if (!ret)
2194 {
2195 if (GetLastError() == ERROR_IO_PENDING)
2196 WaitForSingleObject(async_data->completion_event, INFINITE);
2197 else
2198 {
2199 RpcHttpAsyncData_Release(async_data);
2200 ERR("HttpSendRequestW failed with error %d\n", GetLastError());
2201 return RPC_S_SERVER_UNAVAILABLE;
2202 }
2203 }
2204 status = rpcrt4_http_check_response(in_request);
2205 if (status != RPC_S_OK) return status;
2206
2207 InternetReadFile(in_request, packet, 20, &bytes_read);
2208 /* FIXME: do something with retrieved data */
2209
2210 memset(&buffers_in, 0, sizeof(buffers_in));
2211 buffers_in.dwStructSize = sizeof(buffers_in);
2212 /* FIXME: get this from the registry */
2213 buffers_in.dwBufferTotal = 1024 * 1024 * 1024; /* 1Gb */
2214 ResetEvent(async_data->completion_event);
2215 RpcHttpAsyncData_AddRef(async_data);
2216 ret = HttpSendRequestExW(in_request, &buffers_in, NULL, 0, 0);
2217 if (!ret)
2218 {
2219 if (GetLastError() == ERROR_IO_PENDING)
2220 WaitForSingleObject(async_data->completion_event, INFINITE);
2221 else
2222 {
2223 RpcHttpAsyncData_Release(async_data);
2224 ERR("HttpSendRequestExW failed with error %d\n", GetLastError());
2225 return RPC_S_SERVER_UNAVAILABLE;
2226 }
2227 }
2228
2229 TRACE("sending HTTP connect header to server\n");
2230 hdr = RPCRT4_BuildHttpConnectHeader(0, FALSE, connection_uuid, in_pipe_uuid, association_uuid);
2231 if (!hdr) return RPC_S_OUT_OF_RESOURCES;
2232 ret = InternetWriteFile(in_request, hdr, hdr->common.frag_len, &bytes_written);
2233 RPCRT4_FreeHeader(hdr);
2234 if (!ret)
2235 {
2236 ERR("InternetWriteFile failed with error %d\n", GetLastError());
2237 return RPC_S_SERVER_UNAVAILABLE;
2238 }
2239
2240 return RPC_S_OK;
2241 }
2242
2243 static RPC_STATUS rpcrt4_http_read_http_packet(HINTERNET request, RpcPktHdr *hdr, BYTE **data)
2244 {
2245 BOOL ret;
2246 DWORD bytes_read;
2247 unsigned short data_len;
2248
2249 ret = InternetReadFile(request, hdr, sizeof(hdr->common), &bytes_read);
2250 if (!ret)
2251 return RPC_S_SERVER_UNAVAILABLE;
2252 if (hdr->common.ptype != PKT_HTTP || hdr->common.frag_len < sizeof(hdr->http))
2253 {
2254 ERR("wrong packet type received %d or wrong frag_len %d\n",
2255 hdr->common.ptype, hdr->common.frag_len);
2256 return RPC_S_PROTOCOL_ERROR;
2257 }
2258
2259 ret = InternetReadFile(request, &hdr->common + 1, sizeof(hdr->http) - sizeof(hdr->common), &bytes_read);
2260 if (!ret)
2261 return RPC_S_SERVER_UNAVAILABLE;
2262
2263 data_len = hdr->common.frag_len - sizeof(hdr->http);
2264 if (data_len)
2265 {
2266 *data = HeapAlloc(GetProcessHeap(), 0, data_len);
2267 if (!*data)
2268 return RPC_S_OUT_OF_RESOURCES;
2269 ret = InternetReadFile(request, *data, data_len, &bytes_read);
2270 if (!ret)
2271 {
2272 HeapFree(GetProcessHeap(), 0, *data);
2273 return RPC_S_SERVER_UNAVAILABLE;
2274 }
2275 }
2276 else
2277 *data = NULL;
2278
2279 if (!RPCRT4_IsValidHttpPacket(hdr, *data, data_len))
2280 {
2281 ERR("invalid http packet\n");
2282 return RPC_S_PROTOCOL_ERROR;
2283 }
2284
2285 return RPC_S_OK;
2286 }
2287
2288 /* prepare the out pipe for use by RPC packets */
2289 static RPC_STATUS rpcrt4_http_prepare_out_pipe(HINTERNET out_request,
2290 RpcHttpAsyncData *async_data,
2291 const UUID *connection_uuid,
2292 const UUID *out_pipe_uuid,
2293 ULONG *flow_control_increment)
2294 {
2295 BYTE packet[20];
2296 BOOL ret;
2297 RPC_STATUS status;
2298 RpcPktHdr *hdr;
2299 DWORD bytes_read;
2300 BYTE *data_from_server;
2301 RpcPktHdr pkt_from_server;
2302 ULONG field1, field3;
2303
2304 ResetEvent(async_data->completion_event);
2305 RpcHttpAsyncData_AddRef(async_data);
2306 ret = HttpSendRequestW(out_request, NULL, 0, NULL, 0);
2307 if (!ret)
2308 {
2309 if (GetLastError() == ERROR_IO_PENDING)
2310 WaitForSingleObject(async_data->completion_event, INFINITE);
2311 else
2312 {
2313 RpcHttpAsyncData_Release(async_data);
2314 ERR("HttpSendRequestW failed with error %d\n", GetLastError());
2315 return RPC_S_SERVER_UNAVAILABLE;
2316 }
2317 }
2318 status = rpcrt4_http_check_response(out_request);
2319 if (status != RPC_S_OK) return status;
2320
2321 InternetReadFile(out_request, packet, 20, &bytes_read);
2322 /* FIXME: do something with retrieved data */
2323
2324 hdr = RPCRT4_BuildHttpConnectHeader(0, TRUE, connection_uuid, out_pipe_uuid, NULL);
2325 if (!hdr) return RPC_S_OUT_OF_RESOURCES;
2326 ResetEvent(async_data->completion_event);
2327 RpcHttpAsyncData_AddRef(async_data);
2328 ret = HttpSendRequestW(out_request, NULL, 0, hdr, hdr->common.frag_len);
2329 if (!ret)
2330 {
2331 if (GetLastError() == ERROR_IO_PENDING)
2332 WaitForSingleObject(async_data->completion_event, INFINITE);
2333 else
2334 {
2335 RpcHttpAsyncData_Release(async_data);
2336 ERR("HttpSendRequestW failed with error %d\n", GetLastError());
2337 RPCRT4_FreeHeader(hdr);
2338 return RPC_S_SERVER_UNAVAILABLE;
2339 }
2340 }
2341 RPCRT4_FreeHeader(hdr);
2342 status = rpcrt4_http_check_response(out_request);
2343 if (status != RPC_S_OK) return status;
2344
2345 status = rpcrt4_http_read_http_packet(out_request, &pkt_from_server,
2346 &data_from_server);
2347 if (status != RPC_S_OK) return status;
2348 status = RPCRT4_ParseHttpPrepareHeader1(&pkt_from_server, data_from_server,
2349 &field1);
2350 HeapFree(GetProcessHeap(), 0, data_from_server);
2351 if (status != RPC_S_OK) return status;
2352 TRACE("received (%d) from first prepare header\n", field1);
2353
2354 status = rpcrt4_http_read_http_packet(out_request, &pkt_from_server,
2355 &data_from_server);
2356 if (status != RPC_S_OK) return status;
2357 status = RPCRT4_ParseHttpPrepareHeader2(&pkt_from_server, data_from_server,
2358 &field1, flow_control_increment,
2359 &field3);
2360 HeapFree(GetProcessHeap(), 0, data_from_server);
2361 if (status != RPC_S_OK) return status;
2362 TRACE("received (0x%08x 0x%08x %d) from second prepare header\n", field1, *flow_control_increment, field3);
2363
2364 return RPC_S_OK;
2365 }
2366
2367 static RPC_STATUS rpcrt4_ncacn_http_open(RpcConnection* Connection)
2368 {
2369 RpcConnection_http *httpc = (RpcConnection_http *)Connection;
2370 static const WCHAR wszVerbIn[] = {'R','P','C','_','I','N','_','D','A','T','A',0};
2371 static const WCHAR wszVerbOut[] = {'R','P','C','_','O','U','T','_','D','A','T','A',0};
2372 static const WCHAR wszRpcProxyPrefix[] = {'/','r','p','c','/','r','p','c','p','r','o','x','y','.','d','l','l','?',0};
2373 static const WCHAR wszColon[] = {':',0};
2374 static const WCHAR wszAcceptType[] = {'a','p','p','l','i','c','a','t','i','o','n','/','r','p','c',0};
2375 LPCWSTR wszAcceptTypes[] = { wszAcceptType, NULL };
2376 WCHAR *url;
2377 RPC_STATUS status;
2378 BOOL secure;
2379 HttpTimerThreadData *timer_data;
2380 HANDLE thread;
2381
2382 TRACE("(%s, %s)\n", Connection->NetworkAddr, Connection->Endpoint);
2383
2384 if (Connection->server)
2385 {
2386 ERR("ncacn_http servers not supported yet\n");
2387 return RPC_S_SERVER_UNAVAILABLE;
2388 }
2389
2390 if (httpc->in_request)
2391 return RPC_S_OK;
2392
2393 httpc->async_data->completion_event = CreateEventW(NULL, FALSE, FALSE, NULL);
2394
2395 status = UuidCreate(&httpc->connection_uuid);
2396 status = UuidCreate(&httpc->in_pipe_uuid);
2397 status = UuidCreate(&httpc->out_pipe_uuid);
2398
2399 status = rpcrt4_http_internet_connect(httpc);
2400 if (status != RPC_S_OK)
2401 return status;
2402
2403 url = HeapAlloc(GetProcessHeap(), 0, sizeof(wszRpcProxyPrefix) + (strlen(Connection->NetworkAddr) + 1 + strlen(Connection->Endpoint))*sizeof(WCHAR));
2404 if (!url)
2405 return RPC_S_OUT_OF_MEMORY;
2406 memcpy(url, wszRpcProxyPrefix, sizeof(wszRpcProxyPrefix));
2407 MultiByteToWideChar(CP_ACP, 0, Connection->NetworkAddr, -1, url+sizeof(wszRpcProxyPrefix)/sizeof(wszRpcProxyPrefix[0])-1, strlen(Connection->NetworkAddr)+1);
2408 strcatW(url, wszColon);
2409 MultiByteToWideChar(CP_ACP, 0, Connection->Endpoint, -1, url+strlenW(url), strlen(Connection->Endpoint)+1);
2410
2411 secure = httpc->common.QOS &&
2412 (httpc->common.QOS->qos->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP) &&
2413 (httpc->common.QOS->qos->u.HttpCredentials->Flags & RPC_C_HTTP_FLAG_USE_SSL);
2414
2415 httpc->in_request = HttpOpenRequestW(httpc->session, wszVerbIn, url, NULL, NULL,
2416 wszAcceptTypes,
2417 (secure ? INTERNET_FLAG_SECURE : 0)|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_PRAGMA_NOCACHE,
2418 (DWORD_PTR)httpc->async_data);
2419 if (!httpc->in_request)
2420 {
2421 ERR("HttpOpenRequestW failed with error %d\n", GetLastError());
2422 return RPC_S_SERVER_UNAVAILABLE;
2423 }
2424 httpc->out_request = HttpOpenRequestW(httpc->session, wszVerbOut, url, NULL, NULL,
2425 wszAcceptTypes,
2426 (secure ? INTERNET_FLAG_SECURE : 0)|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_PRAGMA_NOCACHE,
2427 (DWORD_PTR)httpc->async_data);
2428 if (!httpc->out_request)
2429 {
2430 ERR("HttpOpenRequestW failed with error %d\n", GetLastError());
2431 return RPC_S_SERVER_UNAVAILABLE;
2432 }
2433
2434 status = rpcrt4_http_prepare_in_pipe(httpc->in_request,
2435 httpc->async_data,
2436 &httpc->connection_uuid,
2437 &httpc->in_pipe_uuid,
2438 &Connection->assoc->http_uuid);
2439 if (status != RPC_S_OK)
2440 return status;
2441
2442 status = rpcrt4_http_prepare_out_pipe(httpc->out_request,
2443 httpc->async_data,
2444 &httpc->connection_uuid,
2445 &httpc->out_pipe_uuid,
2446 &httpc->flow_control_increment);
2447 if (status != RPC_S_OK)
2448 return status;
2449
2450 httpc->flow_control_mark = httpc->flow_control_increment / 2;
2451 httpc->last_sent_time = GetTickCount();
2452 httpc->timer_cancelled = CreateEventW(NULL, FALSE, FALSE, NULL);
2453
2454 timer_data = HeapAlloc(GetProcessHeap(), 0, sizeof(*timer_data));
2455 if (!timer_data)
2456 return ERROR_OUTOFMEMORY;
2457 timer_data->timer_param = httpc->in_request;
2458 timer_data->last_sent_time = &httpc->last_sent_time;
2459 timer_data->timer_cancelled = httpc->timer_cancelled;
2460 /* FIXME: should use CreateTimerQueueTimer when implemented */
2461 thread = CreateThread(NULL, 0, rpcrt4_http_timer_thread, timer_data, 0, NULL);
2462 if (!thread)
2463 {
2464 HeapFree(GetProcessHeap(), 0, timer_data);
2465 return GetLastError();
2466 }
2467 CloseHandle(thread);
2468
2469 return RPC_S_OK;
2470 }
2471
2472 static RPC_STATUS rpcrt4_ncacn_http_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
2473 {
2474 assert(0);
2475 return RPC_S_SERVER_UNAVAILABLE;
2476 }
2477
2478 static int rpcrt4_ncacn_http_read(RpcConnection *Connection,
2479 void *buffer, unsigned int count)
2480 {
2481 RpcConnection_http *httpc = (RpcConnection_http *) Connection;
2482 char *buf = buffer;
2483 BOOL ret = TRUE;
2484 unsigned int bytes_left = count;
2485
2486 ResetEvent(httpc->async_data->completion_event);
2487 while (bytes_left)
2488 {
2489 RpcHttpAsyncData_AddRef(httpc->async_data);
2490 httpc->async_data->inet_buffers.dwBufferLength = bytes_left;
2491 httpc->async_data->inet_buffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, bytes_left);
2492 httpc->async_data->destination_buffer = buf;
2493 ret = InternetReadFileExA(httpc->out_request, &httpc->async_data->inet_buffers, IRF_ASYNC, 0);
2494 if (ret)
2495 {
2496 /* INTERNET_STATUS_REQUEST_COMPLETED won't be sent, so release our
2497 * async ref now */
2498 RpcHttpAsyncData_Release(httpc->async_data);
2499 memcpy(buf, httpc->async_data->inet_buffers.lpvBuffer,
2500 httpc->async_data->inet_buffers.dwBufferLength);
2501 HeapFree(GetProcessHeap(), 0, httpc->async_data->inet_buffers.lpvBuffer);
2502 httpc->async_data->inet_buffers.lpvBuffer = NULL;
2503 httpc->async_data->destination_buffer = NULL;
2504 }
2505 else
2506 {
2507 if (GetLastError() == ERROR_IO_PENDING)
2508 {
2509 HANDLE handles[2] = { httpc->async_data->completion_event, httpc->cancel_event };
2510 DWORD result = WaitForMultipleObjects(2, handles, FALSE, DEFAULT_NCACN_HTTP_TIMEOUT);
2511 if (result == WAIT_OBJECT_0)
2512 ret = TRUE;
2513 else
2514 {
2515 TRACE("call cancelled\n");
2516 EnterCriticalSection(&httpc->async_data->cs);
2517 httpc->async_data->destination_buffer = NULL;
2518 LeaveCriticalSection(&httpc->async_data->cs);
2519 break;
2520 }
2521 }
2522 else
2523 {
2524 HeapFree(GetProcessHeap(), 0, httpc->async_data->inet_buffers.lpvBuffer);
2525 httpc->async_data->inet_buffers.lpvBuffer = NULL;
2526 httpc->async_data->destination_buffer = NULL;
2527 RpcHttpAsyncData_Release(httpc->async_data);
2528 break;
2529 }
2530 }
2531 if (!httpc->async_data->inet_buffers.dwBufferLength)
2532 break;
2533 bytes_left -= httpc->async_data->inet_buffers.dwBufferLength;
2534 buf += httpc->async_data->inet_buffers.dwBufferLength;
2535 }
2536 TRACE("%p %p %u -> %s\n", httpc->out_request, buffer, count, ret ? "TRUE" : "FALSE");
2537 return ret ? count : -1;
2538 }
2539
2540 static RPC_STATUS rpcrt4_ncacn_http_receive_fragment(RpcConnection *Connection, RpcPktHdr **Header, void **Payload)
2541 {
2542 RpcConnection_http *httpc = (RpcConnection_http *) Connection;
2543 RPC_STATUS status;
2544 DWORD hdr_length;
2545 LONG dwRead;
2546 RpcPktCommonHdr common_hdr;
2547
2548 *Header = NULL;
2549
2550 TRACE("(%p, %p, %p)\n", Connection, Header, Payload);
2551
2552 again:
2553 /* read packet common header */
2554 dwRead = rpcrt4_ncacn_http_read(Connection, &common_hdr, sizeof(common_hdr));
2555 if (dwRead != sizeof(common_hdr)) {
2556 WARN("Short read of header, %d bytes\n", dwRead);
2557 status = RPC_S_PROTOCOL_ERROR;
2558 goto fail;
2559 }
2560 if (!memcmp(&common_hdr, "HTTP/1.1", sizeof("HTTP/1.1")) ||
2561 !memcmp(&common_hdr, "HTTP/1.0", sizeof("HTTP/1.0")))
2562 {
2563 FIXME("server returned %s\n", debugstr_a((const char *)&common_hdr));
2564 status = RPC_S_PROTOCOL_ERROR;
2565 goto fail;
2566 }
2567
2568 status = RPCRT4_ValidateCommonHeader(&common_hdr);
2569 if (status != RPC_S_OK) goto fail;
2570
2571 hdr_length = RPCRT4_GetHeaderSize((RpcPktHdr*)&common_hdr);
2572 if (hdr_length == 0) {
2573 WARN("header length == 0\n");
2574 status = RPC_S_PROTOCOL_ERROR;
2575 goto fail;
2576 }
2577
2578 *Header = HeapAlloc(GetProcessHeap(), 0, hdr_length);
2579 if (!*Header)
2580 {
2581 status = RPC_S_OUT_OF_RESOURCES;
2582 goto fail;
2583 }
2584 memcpy(*Header, &common_hdr, sizeof(common_hdr));
2585
2586 /* read the rest of packet header */
2587 dwRead = rpcrt4_ncacn_http_read(Connection, &(*Header)->common + 1, hdr_length - sizeof(common_hdr));
2588 if (dwRead != hdr_length - sizeof(common_hdr)) {
2589 WARN("bad header length, %d bytes, hdr_length %d\n", dwRead, hdr_length);
2590 status = RPC_S_PROTOCOL_ERROR;
2591 goto fail;
2592 }
2593
2594 if (common_hdr.frag_len - hdr_length)
2595 {
2596 *Payload = HeapAlloc(GetProcessHeap(), 0, common_hdr.frag_len - hdr_length);
2597 if (!*Payload)
2598 {
2599 status = RPC_S_OUT_OF_RESOURCES;
2600 goto fail;
2601 }
2602
2603 dwRead = rpcrt4_ncacn_http_read(Connection, *Payload, common_hdr.frag_len - hdr_length);
2604 if (dwRead != common_hdr.frag_len - hdr_length)
2605 {
2606 WARN("bad data length, %d/%d\n", dwRead, common_hdr.frag_len - hdr_length);
2607 status = RPC_S_PROTOCOL_ERROR;
2608 goto fail;
2609 }
2610 }
2611 else
2612 *Payload = NULL;
2613
2614 if ((*Header)->common.ptype == PKT_HTTP)
2615 {
2616 if (!RPCRT4_IsValidHttpPacket(*Header, *Payload, common_hdr.frag_len - hdr_length))
2617 {
2618 ERR("invalid http packet of length %d bytes\n", (*Header)->common.frag_len);
2619 status = RPC_S_PROTOCOL_ERROR;
2620 goto fail;
2621 }
2622 if ((*Header)->http.flags == 0x0001)
2623 {
2624 TRACE("http idle packet, waiting for real packet\n");
2625 if ((*Header)->http.num_data_items != 0)
2626 {
2627 ERR("HTTP idle packet should have no data items instead of %d\n", (*Header)->http.num_data_items);
2628 status = RPC_S_PROTOCOL_ERROR;
2629 goto fail;
2630 }
2631 }
2632 else if ((*Header)->http.flags == 0x0002)
2633 {
2634 ULONG bytes_transmitted;
2635 ULONG flow_control_increment;
2636 UUID pipe_uuid;
2637 status = RPCRT4_ParseHttpFlowControlHeader(*Header, *Payload,
2638 Connection->server,
2639 &bytes_transmitted,
2640 &flow_control_increment,
2641 &pipe_uuid);
2642 if (status != RPC_S_OK)
2643 goto fail;
2644 TRACE("received http flow control header (0x%x, 0x%x, %s)\n",
2645 bytes_transmitted, flow_control_increment, debugstr_guid(&pipe_uuid));
2646 /* FIXME: do something with parsed data */
2647 }
2648 else
2649 {
2650 FIXME("unrecognised http packet with flags 0x%04x\n", (*Header)->http.flags);
2651 status = RPC_S_PROTOCOL_ERROR;
2652 goto fail;
2653 }
2654 RPCRT4_FreeHeader(*Header);
2655 *Header = NULL;
2656 HeapFree(GetProcessHeap(), 0, *Payload);
2657 *Payload = NULL;
2658 goto again;
2659 }
2660
2661 /* success */
2662 status = RPC_S_OK;
2663
2664 httpc->bytes_received += common_hdr.frag_len;
2665
2666 TRACE("httpc->bytes_received = 0x%x\n", httpc->bytes_received);
2667
2668 if (httpc->bytes_received > httpc->flow_control_mark)
2669 {
2670 RpcPktHdr *hdr = RPCRT4_BuildHttpFlowControlHeader(httpc->common.server,
2671 httpc->bytes_received,
2672 httpc->flow_control_increment,
2673 &httpc->out_pipe_uuid);
2674 if (hdr)
2675 {
2676 DWORD bytes_written;
2677 BOOL ret2;
2678 TRACE("sending flow control packet at 0x%x\n", httpc->bytes_received);
2679 ret2 = InternetWriteFile(httpc->in_request, hdr, hdr->common.frag_len, &bytes_written);
2680 RPCRT4_FreeHeader(hdr);
2681 if (ret2)
2682 httpc->flow_control_mark = httpc->bytes_received + httpc->flow_control_increment / 2;
2683 }
2684 }
2685
2686 fail:
2687 if (status != RPC_S_OK) {
2688 RPCRT4_FreeHeader(*Header);
2689 *Header = NULL;
2690 HeapFree(GetProcessHeap(), 0, *Payload);
2691 *Payload = NULL;
2692 }
2693 return status;
2694 }
2695
2696 static int rpcrt4_ncacn_http_write(RpcConnection *Connection,
2697 const void *buffer, unsigned int count)
2698 {
2699 RpcConnection_http *httpc = (RpcConnection_http *) Connection;
2700 DWORD bytes_written;
2701 BOOL ret;
2702
2703 httpc->last_sent_time = ~0U; /* disable idle packet sending */
2704 ret = InternetWriteFile(httpc->in_request, buffer, count, &bytes_written);
2705 httpc->last_sent_time = GetTickCount();
2706 TRACE("%p %p %u -> %s\n", httpc->in_request, buffer, count, ret ? "TRUE" : "FALSE");
2707 return ret ? bytes_written : -1;
2708 }
2709
2710 static int rpcrt4_ncacn_http_close(RpcConnection *Connection)
2711 {
2712 RpcConnection_http *httpc = (RpcConnection_http *) Connection;
2713
2714 TRACE("\n");
2715
2716 SetEvent(httpc->timer_cancelled);
2717 if (httpc->in_request)
2718 InternetCloseHandle(httpc->in_request);
2719 httpc->in_request = NULL;
2720 if (httpc->out_request)
2721 InternetCloseHandle(httpc->out_request);
2722 httpc->out_request = NULL;
2723 if (httpc->app_info)
2724 InternetCloseHandle(httpc->app_info);
2725 httpc->app_info = NULL;
2726 if (httpc->session)
2727 InternetCloseHandle(httpc->session);
2728 httpc->session = NULL;
2729 RpcHttpAsyncData_Release(httpc->async_data);
2730 if (httpc->cancel_event)
2731 CloseHandle(httpc->cancel_event);
2732
2733 return 0;
2734 }
2735
2736 static void rpcrt4_ncacn_http_cancel_call(RpcConnection *Connection)
2737 {
2738 RpcConnection_http *httpc = (RpcConnection_http *) Connection;
2739
2740 SetEvent(httpc->cancel_event);
2741 }
2742
2743 static int rpcrt4_ncacn_http_wait_for_incoming_data(RpcConnection *Connection)
2744 {
2745 BOOL ret;
2746 RpcConnection_http *httpc = (RpcConnection_http *) Connection;
2747
2748 RpcHttpAsyncData_AddRef(httpc->async_data);
2749 ret = InternetQueryDataAvailable(httpc->out_request,
2750 &httpc->async_data->inet_buffers.dwBufferLength, IRF_ASYNC, 0);
2751 if (ret)
2752 {
2753 /* INTERNET_STATUS_REQUEST_COMPLETED won't be sent, so release our
2754 * async ref now */
2755 RpcHttpAsyncData_Release(httpc->async_data);
2756 }
2757 else
2758 {
2759 if (GetLastError() == ERROR_IO_PENDING)
2760 {
2761 HANDLE handles[2] = { httpc->async_data->completion_event, httpc->cancel_event };
2762 DWORD result = WaitForMultipleObjects(2, handles, FALSE, DEFAULT_NCACN_HTTP_TIMEOUT);
2763 if (result != WAIT_OBJECT_0)
2764 {
2765 TRACE("call cancelled\n");
2766 return -1;
2767 }
2768 }
2769 else
2770 {
2771 RpcHttpAsyncData_Release(httpc->async_data);
2772 return -1;
2773 }
2774 }
2775
2776 /* success */
2777 return 0;
2778 }
2779
2780 static size_t rpcrt4_ncacn_http_get_top_of_tower(unsigned char *tower_data,
2781 const char *networkaddr,
2782 const char *endpoint)
2783 {
2784 return rpcrt4_ip_tcp_get_top_of_tower(tower_data, networkaddr,
2785 EPM_PROTOCOL_HTTP, endpoint);
2786 }
2787
2788 static RPC_STATUS rpcrt4_ncacn_http_parse_top_of_tower(const unsigned char *tower_data,
2789 size_t tower_size,
2790 char **networkaddr,
2791 char **endpoint)
2792 {
2793 return rpcrt4_ip_tcp_parse_top_of_tower(tower_data, tower_size,
2794 networkaddr, EPM_PROTOCOL_HTTP,
2795 endpoint);
2796 }
2797
2798 static const struct connection_ops conn_protseq_list[] = {
2799 { "ncacn_np",
2800 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SMB },
2801 rpcrt4_conn_np_alloc,
2802 rpcrt4_ncacn_np_open,
2803 rpcrt4_ncacn_np_handoff,
2804 rpcrt4_conn_np_read,
2805 rpcrt4_conn_np_write,
2806 rpcrt4_conn_np_close,
2807 rpcrt4_conn_np_cancel_call,
2808 rpcrt4_conn_np_wait_for_incoming_data,
2809 rpcrt4_ncacn_np_get_top_of_tower,
2810 rpcrt4_ncacn_np_parse_top_of_tower,
2811 NULL,
2812 RPCRT4_default_is_authorized,
2813 RPCRT4_default_authorize,
2814 RPCRT4_default_secure_packet,
2815 rpcrt4_conn_np_impersonate_client,
2816 rpcrt4_conn_np_revert_to_self,
2817 RPCRT4_default_inquire_auth_client,
2818 },
2819 { "ncalrpc",
2820 { EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_PIPE },
2821 rpcrt4_conn_np_alloc,
2822 rpcrt4_ncalrpc_open,
2823 rpcrt4_ncalrpc_handoff,
2824 rpcrt4_conn_np_read,
2825 rpcrt4_conn_np_write,
2826 rpcrt4_conn_np_close,
2827 rpcrt4_conn_np_cancel_call,
2828 rpcrt4_conn_np_wait_for_incoming_data,
2829 rpcrt4_ncalrpc_get_top_of_tower,
2830 rpcrt4_ncalrpc_parse_top_of_tower,
2831 NULL,
2832 rpcrt4_ncalrpc_is_authorized,
2833 rpcrt4_ncalrpc_authorize,
2834 rpcrt4_ncalrpc_secure_packet,
2835 rpcrt4_conn_np_impersonate_client,
2836 rpcrt4_conn_np_revert_to_self,
2837 rpcrt4_ncalrpc_inquire_auth_client,
2838 },
2839 { "ncacn_ip_tcp",
2840 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_TCP },
2841 rpcrt4_conn_tcp_alloc,
2842 rpcrt4_ncacn_ip_tcp_open,
2843 rpcrt4_conn_tcp_handoff,
2844 rpcrt4_conn_tcp_read,
2845 rpcrt4_conn_tcp_write,
2846 rpcrt4_conn_tcp_close,
2847 rpcrt4_conn_tcp_cancel_call,
2848 rpcrt4_conn_tcp_wait_for_incoming_data,
2849 rpcrt4_ncacn_ip_tcp_get_top_of_tower,
2850 rpcrt4_ncacn_ip_tcp_parse_top_of_tower,
2851 NULL,
2852 RPCRT4_default_is_authorized,
2853 RPCRT4_default_authorize,
2854 RPCRT4_default_secure_packet,
2855 RPCRT4_default_impersonate_client,
2856 RPCRT4_default_revert_to_self,
2857 RPCRT4_default_inquire_auth_client,
2858 },
2859 { "ncacn_http",
2860 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_HTTP },
2861 rpcrt4_ncacn_http_alloc,
2862 rpcrt4_ncacn_http_open,
2863 rpcrt4_ncacn_http_handoff,
2864 rpcrt4_ncacn_http_read,
2865 rpcrt4_ncacn_http_write,
2866 rpcrt4_ncacn_http_close,
2867 rpcrt4_ncacn_http_cancel_call,
2868 rpcrt4_ncacn_http_wait_for_incoming_data,
2869 rpcrt4_ncacn_http_get_top_of_tower,
2870 rpcrt4_ncacn_http_parse_top_of_tower,
2871 rpcrt4_ncacn_http_receive_fragment,
2872 RPCRT4_default_is_authorized,
2873 RPCRT4_default_authorize,
2874 RPCRT4_default_secure_packet,
2875 RPCRT4_default_impersonate_client,
2876 RPCRT4_default_revert_to_self,
2877 RPCRT4_default_inquire_auth_client,
2878 },
2879 };
2880
2881
2882 static const struct protseq_ops protseq_list[] =
2883 {
2884 {
2885 "ncacn_np",
2886 rpcrt4_protseq_np_alloc,
2887 rpcrt4_protseq_np_signal_state_changed,
2888 rpcrt4_protseq_np_get_wait_array,
2889 rpcrt4_protseq_np_free_wait_array,
2890 rpcrt4_protseq_np_wait_for_new_connection,
2891 rpcrt4_protseq_ncacn_np_open_endpoint,
2892 },
2893 {
2894 "ncalrpc",
2895 rpcrt4_protseq_np_alloc,
2896 rpcrt4_protseq_np_signal_state_changed,
2897 rpcrt4_protseq_np_get_wait_array,
2898 rpcrt4_protseq_np_free_wait_array,
2899 rpcrt4_protseq_np_wait_for_new_connection,
2900 rpcrt4_protseq_ncalrpc_open_endpoint,
2901 },
2902 {
2903 "ncacn_ip_tcp",
2904 rpcrt4_protseq_sock_alloc,
2905 rpcrt4_protseq_sock_signal_state_changed,
2906 rpcrt4_protseq_sock_get_wait_array,
2907 rpcrt4_protseq_sock_free_wait_array,
2908 rpcrt4_protseq_sock_wait_for_new_connection,
2909 rpcrt4_protseq_ncacn_ip_tcp_open_endpoint,
2910 },
2911 };
2912
2913 #define ARRAYSIZE(a) (sizeof((a)) / sizeof((a)[0]))
2914
2915 const struct protseq_ops *rpcrt4_get_protseq_ops(const char *protseq)
2916 {
2917 unsigned int i;
2918 for(i=0; i<ARRAYSIZE(protseq_list); i++)
2919 if (!strcmp(protseq_list[i].name, protseq))
2920 return &protseq_list[i];
2921 return NULL;
2922 }
2923
2924 static const struct connection_ops *rpcrt4_get_conn_protseq_ops(const char *protseq)
2925 {
2926 unsigned int i;
2927 for(i=0; i<ARRAYSIZE(conn_protseq_list); i++)
2928 if (!strcmp(conn_protseq_list[i].name, protseq))
2929 return &conn_protseq_list[i];
2930 return NULL;
2931 }
2932
2933 /**** interface to rest of code ****/
2934
2935 RPC_STATUS RPCRT4_OpenClientConnection(RpcConnection* Connection)
2936 {
2937 TRACE("(Connection == ^%p)\n", Connection);
2938
2939 assert(!Connection->server);
2940 return Connection->ops->open_connection_client(Connection);
2941 }
2942
2943 RPC_STATUS RPCRT4_CloseConnection(RpcConnection* Connection)
2944 {
2945 TRACE("(Connection == ^%p)\n", Connection);
2946 if (SecIsValidHandle(&Connection->ctx))
2947 {
2948 DeleteSecurityContext(&Connection->ctx);
2949 SecInvalidateHandle(&Connection->ctx);
2950 }
2951 rpcrt4_conn_close(Connection);
2952 return RPC_S_OK;
2953 }
2954
2955 RPC_STATUS RPCRT4_CreateConnection(RpcConnection** Connection, BOOL server,
2956 LPCSTR Protseq, LPCSTR NetworkAddr, LPCSTR Endpoint,
2957 LPCWSTR NetworkOptions, RpcAuthInfo* AuthInfo, RpcQualityOfService *QOS)
2958 {
2959 static LONG next_id;
2960 const struct connection_ops *ops;
2961 RpcConnection* NewConnection;
2962
2963 ops = rpcrt4_get_conn_protseq_ops(Protseq);
2964 if (!ops)
2965 {
2966 FIXME("not supported for protseq %s\n", Protseq);
2967 return RPC_S_PROTSEQ_NOT_SUPPORTED;
2968 }
2969
2970 NewConnection = ops->alloc();
2971 NewConnection->Next = NULL;
2972 NewConnection->server_binding = NULL;
2973 NewConnection->server = server;
2974 NewConnection->ops = ops;
2975 NewConnection->NetworkAddr = RPCRT4_strdupA(NetworkAddr);
2976 NewConnection->Endpoint = RPCRT4_strdupA(Endpoint);
2977 NewConnection->NetworkOptions = RPCRT4_strdupW(NetworkOptions);
2978 NewConnection->MaxTransmissionSize = RPC_MAX_PACKET_SIZE;
2979 memset(&NewConnection->ActiveInterface, 0, sizeof(NewConnection->ActiveInterface));
2980 NewConnection->NextCallId = 1;
2981
2982 SecInvalidateHandle(&NewConnection->ctx);
2983 memset(&NewConnection->exp, 0, sizeof(NewConnection->exp));
2984 NewConnection->attr = 0;
2985 if (AuthInfo) RpcAuthInfo_AddRef(AuthInfo);
2986 NewConnection->AuthInfo = AuthInfo;
2987 NewConnection->auth_context_id = InterlockedIncrement( &next_id );
2988 NewConnection->encryption_auth_len = 0;
2989 NewConnection->signature_auth_len = 0;
2990 if (QOS) RpcQualityOfService_AddRef(QOS);
2991 NewConnection->QOS = QOS;
2992
2993 list_init(&NewConnection->conn_pool_entry);
2994 NewConnection->async_state = NULL;
2995
2996 TRACE("connection: %p\n", NewConnection);
2997 *Connection = NewConnection;
2998
2999 return RPC_S_OK;
3000 }
3001
3002 static RPC_STATUS RPCRT4_SpawnConnection(RpcConnection** Connection, RpcConnection* OldConnection)
3003 {
3004 RPC_STATUS err;
3005
3006 err = RPCRT4_CreateConnection(Connection, OldConnection->server,
3007 rpcrt4_conn_get_name(OldConnection),
3008 OldConnection->NetworkAddr,
3009 OldConnection->Endpoint, NULL,
3010 OldConnection->AuthInfo, OldConnection->QOS);
3011 if (err == RPC_S_OK)
3012 rpcrt4_conn_handoff(OldConnection, *Connection);
3013 return err;
3014 }
3015
3016 RPC_STATUS RPCRT4_DestroyConnection(RpcConnection* Connection)
3017 {
3018 TRACE("connection: %p\n", Connection);
3019
3020 RPCRT4_CloseConnection(Connection);
3021 RPCRT4_strfree(Connection->Endpoint);
3022 RPCRT4_strfree(Connection->NetworkAddr);
3023 HeapFree(GetProcessHeap(), 0, Connection->NetworkOptions);
3024 if (Connection->AuthInfo) RpcAuthInfo_Release(Connection->AuthInfo);
3025 if (Connection->QOS) RpcQualityOfService_Release(Connection->QOS);
3026
3027 /* server-only */
3028 if (Connection->server_binding) RPCRT4_ReleaseBinding(Connection->server_binding);
3029
3030 HeapFree(GetProcessHeap(), 0, Connection);
3031 return RPC_S_OK;
3032 }
3033
3034 RPC_STATUS RpcTransport_GetTopOfTower(unsigned char *tower_data,
3035 size_t *tower_size,
3036 const char *protseq,
3037 const char *networkaddr,
3038 const char *endpoint)
3039 {
3040 twr_empty_floor_t *protocol_floor;
3041 const struct connection_ops *protseq_ops = rpcrt4_get_conn_protseq_ops(protseq);
3042
3043 *tower_size = 0;
3044
3045 if (!protseq_ops)
3046 return RPC_S_INVALID_RPC_PROTSEQ;
3047
3048 if (!tower_data)
3049 {
3050 *tower_size = sizeof(*protocol_floor);
3051 *tower_size += protseq_ops->get_top_of_tower(NULL, networkaddr, endpoint);
3052 return RPC_S_OK;
3053 }
3054
3055 protocol_floor = (twr_empty_floor_t *)tower_data;
3056 protocol_floor->count_lhs = sizeof(protocol_floor->protid);
3057 protocol_floor->protid = protseq_ops->epm_protocols[0];
3058 protocol_floor->count_rhs = 0;
3059
3060 tower_data += sizeof(*protocol_floor);
3061
3062 *tower_size = protseq_ops->get_top_of_tower(tower_data, networkaddr, endpoint);
3063 if (!*tower_size)
3064 return EPT_S_NOT_REGISTERED;
3065
3066 *tower_size += sizeof(*protocol_floor);
3067
3068 return RPC_S_OK;
3069 }
3070
3071 RPC_STATUS RpcTransport_ParseTopOfTower(const unsigned char *tower_data,
3072 size_t tower_size,
3073 char **protseq,
3074 char **networkaddr,
3075 char **endpoint)
3076 {
3077 const twr_empty_floor_t *protocol_floor;
3078 const twr_empty_floor_t *floor4;
3079 const struct connection_ops *protseq_ops = NULL;
3080 RPC_STATUS status;
3081 unsigned int i;
3082
3083 if (tower_size < sizeof(*protocol_floor))
3084 return EPT_S_NOT_REGISTERED;
3085
3086 protocol_floor = (const twr_empty_floor_t *)tower_data;
3087 tower_data += sizeof(*protocol_floor);
3088 tower_size -= sizeof(*protocol_floor);
3089 if ((protocol_floor->count_lhs != sizeof(protocol_floor->protid)) ||
3090 (protocol_floor->count_rhs > tower_size))
3091 return EPT_S_NOT_REGISTERED;
3092 tower_data += protocol_floor->count_rhs;
3093 tower_size -= protocol_floor->count_rhs;
3094
3095 floor4 = (const twr_empty_floor_t *)tower_data;
3096 if ((tower_size < sizeof(*floor4)) ||
3097 (floor4->count_lhs != sizeof(floor4->protid)))
3098 return EPT_S_NOT_REGISTERED;
3099
3100 for(i = 0; i < ARRAYSIZE(conn_protseq_list); i++)
3101 if ((protocol_floor->protid == conn_protseq_list[i].epm_protocols[0]) &&
3102 (floor4->protid == conn_protseq_list[i].epm_protocols[1]))
3103 {
3104 protseq_ops = &conn_protseq_list[i];
3105 break;
3106 }
3107
3108 if (!protseq_ops)
3109 return EPT_S_NOT_REGISTERED;
3110
3111 status = protseq_ops->parse_top_of_tower(tower_data, tower_size, networkaddr, endpoint);
3112
3113 if ((status == RPC_S_OK) && protseq)
3114 {
3115 *protseq = I_RpcAllocate(strlen(protseq_ops->name) + 1);
3116 strcpy(*protseq, protseq_ops->name);
3117 }
3118
3119 return status;
3120 }
3121
3122 /***********************************************************************
3123 * RpcNetworkIsProtseqValidW (RPCRT4.@)
3124 *
3125 * Checks if the given protocol sequence is known by the RPC system.
3126 * If it is, returns RPC_S_OK, otherwise RPC_S_PROTSEQ_NOT_SUPPORTED.
3127 *
3128 */
3129 RPC_STATUS WINAPI RpcNetworkIsProtseqValidW(RPC_WSTR protseq)
3130 {
3131 char ps[0x10];
3132
3133 WideCharToMultiByte(CP_ACP, 0, protseq, -1,
3134 ps, sizeof ps, NULL, NULL);
3135 if (rpcrt4_get_conn_protseq_ops(ps))
3136 return RPC_S_OK;
3137
3138 FIXME("Unknown protseq %s\n", debugstr_w(protseq));
3139
3140 return RPC_S_INVALID_RPC_PROTSEQ;
3141 }
3142
3143 /***********************************************************************
3144 * RpcNetworkIsProtseqValidA (RPCRT4.@)
3145 */
3146 RPC_STATUS WINAPI RpcNetworkIsProtseqValidA(RPC_CSTR protseq)
3147 {
3148 UNICODE_STRING protseqW;
3149
3150 if (RtlCreateUnicodeStringFromAsciiz(&protseqW, (char*)protseq))
3151 {
3152 RPC_STATUS ret = RpcNetworkIsProtseqValidW(protseqW.Buffer);
3153 RtlFreeUnicodeString(&protseqW);
3154 return ret;
3155 }
3156 return RPC_S_OUT_OF_MEMORY;
3157 }
3158
3159 /***********************************************************************
3160 * RpcProtseqVectorFreeA (RPCRT4.@)
3161 */
3162 RPC_STATUS WINAPI RpcProtseqVectorFreeA(RPC_PROTSEQ_VECTORA **protseqs)
3163 {
3164 TRACE("(%p)\n", protseqs);
3165
3166 if (*protseqs)
3167 {
3168 int i;
3169 for (i = 0; i < (*protseqs)->Count; i++)
3170 HeapFree(GetProcessHeap(), 0, (*protseqs)->Protseq[i]);
3171 HeapFree(GetProcessHeap(), 0, *protseqs);
3172 *protseqs = NULL;
3173 }
3174 return RPC_S_OK;
3175 }
3176
3177 /***********************************************************************
3178 * RpcProtseqVectorFreeW (RPCRT4.@)
3179 */
3180 RPC_STATUS WINAPI RpcProtseqVectorFreeW(RPC_PROTSEQ_VECTORW **protseqs)
3181 {
3182 TRACE("(%p)\n", protseqs);
3183
3184 if (*protseqs)
3185 {
3186 int i;
3187 for (i = 0; i < (*protseqs)->Count; i++)
3188 HeapFree(GetProcessHeap(), 0, (*protseqs)->Protseq[i]);
3189 HeapFree(GetProcessHeap(), 0, *protseqs);
3190 *protseqs = NULL;
3191 }
3192 return RPC_S_OK;
3193 }
3194
3195 /***********************************************************************
3196 * RpcNetworkInqProtseqsW (RPCRT4.@)
3197 */
3198 RPC_STATUS WINAPI RpcNetworkInqProtseqsW( RPC_PROTSEQ_VECTORW** protseqs )
3199 {
3200 RPC_PROTSEQ_VECTORW *pvector;
3201 int i = 0;
3202 RPC_STATUS status = RPC_S_OUT_OF_MEMORY;
3203
3204 TRACE("(%p)\n", protseqs);
3205
3206 *protseqs = HeapAlloc(GetProcessHeap(), 0, sizeof(RPC_PROTSEQ_VECTORW)+(sizeof(unsigned short*)*ARRAYSIZE(protseq_list)));
3207 if (!*protseqs)
3208 goto end;
3209 pvector = *protseqs;
3210 pvector->Count = 0;
3211 for (i = 0; i < ARRAYSIZE(protseq_list); i++)
3212 {
3213 pvector->Protseq[i] = HeapAlloc(GetProcessHeap(), 0, (strlen(protseq_list[i].name)+1)*sizeof(unsigned short));
3214 if (pvector->Protseq[i] == NULL)
3215 goto end;
3216 MultiByteToWideChar(CP_ACP, 0, (CHAR*)protseq_list[i].name, -1,
3217 (WCHAR*)pvector->Protseq[i], strlen(protseq_list[i].name) + 1);
3218 pvector->Count++;
3219 }
3220 status = RPC_S_OK;
3221
3222 end:
3223 if (status != RPC_S_OK)
3224 RpcProtseqVectorFreeW(protseqs);
3225 return status;
3226 }
3227
3228 /***********************************************************************
3229 * RpcNetworkInqProtseqsA (RPCRT4.@)
3230 */
3231 RPC_STATUS WINAPI RpcNetworkInqProtseqsA(RPC_PROTSEQ_VECTORA** protseqs)
3232 {
3233 RPC_PROTSEQ_VECTORA *pvector;
3234 int i = 0;
3235 RPC_STATUS status = RPC_S_OUT_OF_MEMORY;
3236
3237 TRACE("(%p)\n", protseqs);
3238
3239 *protseqs = HeapAlloc(GetProcessHeap(), 0, sizeof(RPC_PROTSEQ_VECTORW)+(sizeof(unsigned char*)*ARRAYSIZE(protseq_list)));
3240 if (!*protseqs)
3241 goto end;
3242 pvector = *protseqs;
3243 pvector->Count = 0;
3244 for (i = 0; i < ARRAYSIZE(protseq_list); i++)
3245 {
3246 pvector->Protseq[i] = HeapAlloc(GetProcessHeap(), 0, strlen(protseq_list[i].name)+1);
3247 if (pvector->Protseq[i] == NULL)
3248 goto end;
3249 strcpy((char*)pvector->Protseq[i], protseq_list[i].name);
3250 pvector->Count++;
3251 }
3252 status = RPC_S_OK;
3253
3254 end:
3255 if (status != RPC_S_OK)
3256 RpcProtseqVectorFreeA(protseqs);
3257 return status;
3258 }