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