rpc_server.c 30.3 KB
Newer Older
1 2 3 4
/*
 * RPC server API
 *
 * Copyright 2001 Ove Kven, TransGaming Technologies
5
 * Copyright 2004 Filip Navara
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * TODO:
 *  - a whole lot
 */

Alexandre Julliard's avatar
Alexandre Julliard committed
25 26 27
#include "config.h"
#include "wine/port.h"

28
#include <stdarg.h>
29 30
#include <stdio.h>
#include <string.h>
31
#include <assert.h>
32 33 34 35 36

#include "windef.h"
#include "winbase.h"
#include "winerror.h"
#include "winreg.h"
37
#include "ntstatus.h"
38 39

#include "rpc.h"
40
#include "rpcndr.h"
41
#include "excpt.h"
42 43

#include "wine/debug.h"
44
#include "wine/exception.h"
45 46

#include "rpc_server.h"
47
#include "rpc_misc.h"
48
#include "rpc_message.h"
49 50
#include "rpc_defs.h"

51 52
#define MAX_THREADS 128

53 54
WINE_DEFAULT_DEBUG_CHANNEL(ole);

55 56 57 58
typedef struct _RpcPacket
{
  struct _RpcPacket* next;
  struct _RpcConnection* conn;
59 60
  RpcPktHdr* hdr;
  RPC_MESSAGE* msg;
61 62
} RpcPacket;

63 64 65 66 67 68 69 70 71 72
typedef struct _RpcObjTypeMap
{
  /* FIXME: a hash table would be better. */
  struct _RpcObjTypeMap *next;
  UUID Object;
  UUID Type;
} RpcObjTypeMap;

static RpcObjTypeMap *RpcObjTypeMaps;

73 74 75
static RpcServerProtseq* protseqs;
static RpcServerInterface* ifs;

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
static CRITICAL_SECTION server_cs;
static CRITICAL_SECTION_DEBUG server_cs_debug =
{
    0, 0, &server_cs,
    { &server_cs_debug.ProcessLocksList, &server_cs_debug.ProcessLocksList },
      0, 0, { 0, (DWORD)(__FILE__ ": server_cs") }
};
static CRITICAL_SECTION server_cs = { &server_cs_debug, -1, 0, 0, 0, 0 };

static CRITICAL_SECTION listen_cs;
static CRITICAL_SECTION_DEBUG listen_cs_debug =
{
    0, 0, &listen_cs,
    { &listen_cs_debug.ProcessLocksList, &listen_cs_debug.ProcessLocksList },
      0, 0, { 0, (DWORD)(__FILE__ ": listen_cs") }
};
static CRITICAL_SECTION listen_cs = { &listen_cs_debug, -1, 0, 0, 0, 0 };

94 95 96 97
static BOOL std_listen;
static LONG listen_count = -1;
static HANDLE mgr_event, server_thread;

98 99 100 101 102 103 104 105 106
static CRITICAL_SECTION spacket_cs;
static CRITICAL_SECTION_DEBUG spacket_cs_debug =
{
    0, 0, &spacket_cs,
    { &spacket_cs_debug.ProcessLocksList, &spacket_cs_debug.ProcessLocksList },
      0, 0, { 0, (DWORD)(__FILE__ ": spacket_cs") }
};
static CRITICAL_SECTION spacket_cs = { &spacket_cs_debug, -1, 0, 0, 0, 0 };

107 108 109 110
static RpcPacket* spacket_head;
static RpcPacket* spacket_tail;
static HANDLE server_sem;

111
static DWORD worker_count, worker_free, worker_tls;
112

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
static UUID uuid_nil;

inline static RpcObjTypeMap *LookupObjTypeMap(UUID *ObjUuid)
{
  RpcObjTypeMap *rslt = RpcObjTypeMaps;
  RPC_STATUS dummy;

  while (rslt) {
    if (! UuidCompare(ObjUuid, &rslt->Object, &dummy)) break;
    rslt = rslt->next;
  }

  return rslt;
}

inline static UUID *LookupObjType(UUID *ObjUuid)
{
  RpcObjTypeMap *map = LookupObjTypeMap(ObjUuid);
  if (map)
    return &map->Type;
  else
    return &uuid_nil;
}

137 138 139
static RpcServerInterface* RPCRT4_find_interface(UUID* object,
                                                 RPC_SYNTAX_IDENTIFIER* if_id,
                                                 BOOL check_object)
140 141 142 143 144
{
  UUID* MgrType = NULL;
  RpcServerInterface* cif = NULL;
  RPC_STATUS status;

145 146
  if (check_object)
    MgrType = LookupObjType(object);
147 148 149
  EnterCriticalSection(&server_cs);
  cif = ifs;
  while (cif) {
150 151 152
    if (!memcmp(if_id, &cif->If->InterfaceId, sizeof(RPC_SYNTAX_IDENTIFIER)) &&
        (check_object == FALSE || UuidEqual(MgrType, &cif->MgrTypeUuid, &status)) &&
        (std_listen || (cif->Flags & RPC_IF_AUTOLISTEN))) break;
153 154 155
    cif = cif->Next;
  }
  LeaveCriticalSection(&server_cs);
156
  TRACE("returning %p for %s\n", cif, debugstr_guid(object));
157 158 159
  return cif;
}

160
static void RPCRT4_push_packet(RpcPacket* packet)
161
{
162 163
  packet->next = NULL;
  EnterCriticalSection(&spacket_cs);
164 165 166 167
  if (spacket_tail) {
    spacket_tail->next = packet;
    spacket_tail = packet;
  } else {
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    spacket_head = packet;
    spacket_tail = packet;
  }
  LeaveCriticalSection(&spacket_cs);
}

static RpcPacket* RPCRT4_pop_packet(void)
{
  RpcPacket* packet;
  EnterCriticalSection(&spacket_cs);
  packet = spacket_head;
  if (packet) {
    spacket_head = packet->next;
    if (!spacket_head) spacket_tail = NULL;
  }
  LeaveCriticalSection(&spacket_cs);
  if (packet) packet->next = NULL;
  return packet;
}

188 189 190 191 192
typedef struct {
  PRPC_MESSAGE msg;
  void* buf;
} packet_state;

193 194
static WINE_EXCEPTION_FILTER(rpc_filter)
{
195
  packet_state* state;
196
  PRPC_MESSAGE msg;
197 198 199
  state = TlsGetValue(worker_tls);
  msg = state->msg;
  if (msg->Buffer != state->buf) I_RpcFreeBuffer(msg);
200 201 202 203
  msg->RpcFlags |= WINE_RPCFLAG_EXCEPTION;
  msg->BufferLength = sizeof(DWORD);
  I_RpcGetBuffer(msg);
  *(DWORD*)msg->Buffer = GetExceptionCode();
204 205
  WARN("exception caught with code 0x%08lx = %ld\n", *(DWORD*)msg->Buffer, *(DWORD*)msg->Buffer);
  TRACE("returning failure packet\n");
206 207 208
  return EXCEPTION_EXECUTE_HANDLER;
}

209
static void RPCRT4_process_packet(RpcConnection* conn, RpcPktHdr* hdr, RPC_MESSAGE* msg)
210
{
211 212
  RpcServerInterface* sif;
  RPC_DISPATCH_FUNCTION func;
213
  packet_state state;
214 215 216 217
  UUID *object_uuid;
  RpcPktHdr *response;
  void *buf = msg->Buffer;
  RPC_STATUS status;
218

219
  state.msg = msg;
220 221
  state.buf = buf;
  TlsSetValue(worker_tls, &state);
222 223 224 225 226 227 228 229

  switch (hdr->common.ptype) {
    case PKT_BIND:
      TRACE("got bind packet\n");

      /* FIXME: do more checks! */
      if (hdr->bind.max_tsize < RPC_MIN_PACKET_SIZE ||
          !UuidIsNil(&conn->ActiveInterface.SyntaxGUID, &status)) {
230
        TRACE("packet size less than min size, or active interface syntax guid non-null\n");
231 232 233 234 235
        sif = NULL;
      } else {
        sif = RPCRT4_find_interface(NULL, &hdr->bind.abstract, FALSE);
      }
      if (sif == NULL) {
236
        TRACE("rejecting bind request on connection %p\n", conn);
237 238 239 240
        /* Report failure to client. */
        response = RPCRT4_BuildBindNackHeader(NDR_LOCAL_DATA_REPRESENTATION,
                                              RPC_VER_MAJOR, RPC_VER_MINOR);
      } else {
241
        TRACE("accepting bind request on connection %p\n", conn);
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260

        /* accept. */
        response = RPCRT4_BuildBindAckHeader(NDR_LOCAL_DATA_REPRESENTATION,
                                             RPC_MAX_PACKET_SIZE,
                                             RPC_MAX_PACKET_SIZE,
                                             conn->Endpoint,
                                             RESULT_ACCEPT, NO_REASON,
                                             &sif->If->TransferSyntax);

        /* save the interface for later use */
        conn->ActiveInterface = hdr->bind.abstract;
        conn->MaxTransmissionSize = hdr->bind.max_tsize;
      }

      if (RPCRT4_Send(conn, response, NULL, 0) != RPC_S_OK)
        goto fail;

      break;

261
    case PKT_REQUEST:
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
      TRACE("got request packet\n");

      /* fail if the connection isn't bound with an interface */
      if (UuidIsNil(&conn->ActiveInterface.SyntaxGUID, &status)) {
        response = RPCRT4_BuildFaultHeader(NDR_LOCAL_DATA_REPRESENTATION,
                                           status);

        RPCRT4_Send(conn, response, NULL, 0);
        break;
      }

      if (hdr->common.flags & RPC_FLG_OBJECT_UUID) {
        object_uuid = (UUID*)(&hdr->request + 1);
      } else {
        object_uuid = NULL;
      }

      sif = RPCRT4_find_interface(object_uuid, &conn->ActiveInterface, TRUE);
      msg->RpcInterfaceInformation = sif->If;
      /* copy the endpoint vector from sif to msg so that midl-generated code will use it */
      msg->ManagerEpv = sif->MgrEpv;
      if (object_uuid != NULL) {
        RPCRT4_SetBindingObject(msg->Handle, object_uuid);
      }

287
      /* find dispatch function */
288
      msg->ProcNum = hdr->request.opnum;
289 290 291 292 293
      if (sif->Flags & RPC_IF_OLE) {
        /* native ole32 always gives us a dispatch table with a single entry
         * (I assume that's a wrapper for IRpcStubBuffer::Invoke) */
        func = *sif->If->DispatchTable->DispatchTable;
      } else {
294
        if (msg->ProcNum >= sif->If->DispatchTable->DispatchTableCount) {
295 296 297
          ERR("invalid procnum\n");
          func = NULL;
        }
298
        func = sif->If->DispatchTable->DispatchTable[msg->ProcNum];
299 300 301 302
      }

      /* put in the drep. FIXME: is this more universally applicable?
         perhaps we should move this outward... */
303 304 305
      msg->DataRepresentation = 
        MAKELONG( MAKEWORD(hdr->common.drep[0], hdr->common.drep[1]),
                  MAKEWORD(hdr->common.drep[2], hdr->common.drep[3]));
306 307

      /* dispatch */
308
      __TRY {
309
        if (func) func(msg);
310 311 312
      } __EXCEPT(rpc_filter) {
        /* failure packet was created in rpc_filter */
      } __ENDTRY
313 314

      /* send response packet */
315 316 317 318
      I_RpcSend(msg);

      msg->RpcInterfaceInformation = NULL;

319
      break;
320

321
    default:
322
      FIXME("unhandled packet type\n");
323 324 325
      break;
  }

326
fail:
327
  /* clean up */
328
  if (msg->Buffer == buf) msg->Buffer = NULL;
329 330
  TRACE("freeing Buffer=%p\n", buf);
  HeapFree(GetProcessHeap(), 0, buf);
331 332 333 334 335
  RPCRT4_DestroyBinding(msg->Handle);
  msg->Handle = 0;
  I_RpcFreeBuffer(msg);
  msg->Buffer = NULL;
  RPCRT4_FreeHeader(hdr);
336
  TlsSetValue(worker_tls, NULL);
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
}

static DWORD CALLBACK RPCRT4_worker_thread(LPVOID the_arg)
{
  DWORD obj;
  RpcPacket* pkt;

  for (;;) {
    /* idle timeout after 5s */
    obj = WaitForSingleObject(server_sem, 5000);
    if (obj == WAIT_TIMEOUT) {
      /* if another idle thread exist, self-destruct */
      if (worker_free > 1) break;
      continue;
    }
    pkt = RPCRT4_pop_packet();
    if (!pkt) continue;
    InterlockedDecrement(&worker_free);
    for (;;) {
356
      RPCRT4_process_packet(pkt->conn, pkt->hdr, pkt->msg);
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
      HeapFree(GetProcessHeap(), 0, pkt);
      /* try to grab another packet here without waiting
       * on the semaphore, in case it hits max */
      pkt = RPCRT4_pop_packet();
      if (!pkt) break;
      /* decrement semaphore */
      WaitForSingleObject(server_sem, 0);
    }
    InterlockedIncrement(&worker_free);
  }
  InterlockedDecrement(&worker_free);
  InterlockedDecrement(&worker_count);
  return 0;
}

static void RPCRT4_create_worker_if_needed(void)
{
  if (!worker_free && worker_count < MAX_THREADS) {
    HANDLE thread;
    InterlockedIncrement(&worker_count);
    InterlockedIncrement(&worker_free);
    thread = CreateThread(NULL, 0, RPCRT4_worker_thread, NULL, 0, NULL);
    if (thread) CloseHandle(thread);
    else {
      InterlockedDecrement(&worker_free);
      InterlockedDecrement(&worker_count);
    }
  }
}

static DWORD CALLBACK RPCRT4_io_thread(LPVOID the_arg)
{
  RpcConnection* conn = (RpcConnection*)the_arg;
390 391 392 393 394
  RpcPktHdr *hdr;
  RpcBinding *pbind;
  RPC_MESSAGE *msg;
  RPC_STATUS status;
  RpcPacket *packet;
395 396

  TRACE("(%p)\n", conn);
397 398

  for (;;) {
399
    msg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RPC_MESSAGE));
400

401 402 403 404 405 406 407 408
    /* create temporary binding for dispatch, it will be freed in
     * RPCRT4_process_packet */
    RPCRT4_MakeBinding(&pbind, conn);
    msg->Handle = (RPC_BINDING_HANDLE)pbind;

    status = RPCRT4_Receive(conn, &hdr, msg);
    if (status != RPC_S_OK) {
      WARN("receive failed with error %lx\n", status);
409 410 411
      break;
    }

412
#if 0
413
    RPCRT4_process_packet(conn, hdr, msg);
414 415 416 417
#else
    packet = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcPacket));
    packet->conn = conn;
    packet->hdr = hdr;
418
    packet->msg = msg;
419 420 421 422
    RPCRT4_create_worker_if_needed();
    RPCRT4_push_packet(packet);
    ReleaseSemaphore(server_sem, 1, NULL);
#endif
423
    msg = NULL;
424
  }
425
  if (msg) HeapFree(GetProcessHeap(), 0, msg);
426
  RPCRT4_DestroyConnection(conn);
427 428 429
  return 0;
}

430
static void RPCRT4_new_client(RpcConnection* conn)
431
{
432 433
  HANDLE thread = CreateThread(NULL, 0, RPCRT4_io_thread, conn, 0, NULL);
  if (!thread) {
434 435
    DWORD err = GetLastError();
    ERR("failed to create thread, error=%08lx\n", err);
436
    RPCRT4_DestroyConnection(conn);
437
  }
438 439 440 441 442 443
  /* we could set conn->thread, but then we'd have to make the io_thread wait
   * for that, otherwise the thread might finish, destroy the connection, and
   * free the memory we'd write to before we did, causing crashes and stuff -
   * so let's implement that later, when we really need conn->thread */

  CloseHandle( thread );
444 445 446 447 448 449 450 451
}

static DWORD CALLBACK RPCRT4_server_thread(LPVOID the_arg)
{
  HANDLE m_event = mgr_event, b_handle;
  HANDLE *objs = NULL;
  DWORD count, res;
  RpcServerProtseq* cps;
452 453
  RpcConnection* conn;
  RpcConnection* cconn;
454

455 456
  TRACE("(the_arg == ^%p)\n", the_arg);

457 458
  for (;;) {
    EnterCriticalSection(&server_cs);
459
    /* open and count connections */
460 461 462
    count = 1;
    cps = protseqs;
    while (cps) {
463 464 465 466 467
      conn = cps->conn;
      while (conn) {
        RPCRT4_OpenConnection(conn);
        if (conn->ovl.hEvent) count++;
        conn = conn->Next;
468 469 470
      }
      cps = cps->Next;
    }
471
    /* make array of connections */
472 473 474 475 476
    if (objs)
	objs = HeapReAlloc(GetProcessHeap(), 0, objs, count*sizeof(HANDLE));
    else
	objs = HeapAlloc(GetProcessHeap(), 0, count*sizeof(HANDLE));

477 478 479 480
    objs[0] = m_event;
    count = 1;
    cps = protseqs;
    while (cps) {
481 482 483 484
      conn = cps->conn;
      while (conn) {
        if (conn->ovl.hEvent) objs[count++] = conn->ovl.hEvent;
        conn = conn->Next;
485 486 487 488 489 490 491 492
      }
      cps = cps->Next;
    }
    LeaveCriticalSection(&server_cs);

    /* start waiting */
    res = WaitForMultipleObjects(count, objs, FALSE, INFINITE);
    if (res == WAIT_OBJECT_0) {
493 494
      ResetEvent(m_event);
      if (!std_listen) break;
495 496 497 498 499 500
    }
    else if (res == WAIT_FAILED) {
      ERR("wait failed\n");
    }
    else {
      b_handle = objs[res - WAIT_OBJECT_0];
501
      /* find which connection got a RPC */
502
      EnterCriticalSection(&server_cs);
503
      conn = NULL;
504 505
      cps = protseqs;
      while (cps) {
506 507 508 509
        conn = cps->conn;
        while (conn) {
          if (conn->ovl.hEvent == b_handle) break;
          conn = conn->Next;
510
        }
511
        if (conn) break;
512 513
        cps = cps->Next;
      }
514 515
      cconn = NULL;
      if (conn) RPCRT4_SpawnConnection(&cconn, conn);
516
      LeaveCriticalSection(&server_cs);
517 518
      if (!conn) {
        ERR("failed to locate connection for handle %p\n", b_handle);
519
      }
520
      if (cconn) RPCRT4_new_client(cconn);
521 522 523 524
    }
  }
  HeapFree(GetProcessHeap(), 0, objs);
  EnterCriticalSection(&server_cs);
525
  /* close connections */
526 527
  cps = protseqs;
  while (cps) {
528 529 530 531
    conn = cps->conn;
    while (conn) {
      RPCRT4_CloseConnection(conn);
      conn = conn->Next;
532 533 534 535 536 537 538 539 540
    }
    cps = cps->Next;
  }
  LeaveCriticalSection(&server_cs);
  return 0;
}

static void RPCRT4_start_listen(void)
{
541
  TRACE("\n");
542 543 544 545

  EnterCriticalSection(&listen_cs);
  if (! ++listen_count) {
    if (!mgr_event) mgr_event = CreateEventA(NULL, TRUE, FALSE, NULL);
546
    if (!server_sem) server_sem = CreateSemaphoreA(NULL, 0, MAX_THREADS, NULL);
547
    if (!worker_tls) worker_tls = TlsAlloc();
548
    std_listen = TRUE;
549
    server_thread = CreateThread(NULL, 0, RPCRT4_server_thread, NULL, 0, NULL);
550 551 552 553
    LeaveCriticalSection(&listen_cs);
  } else {
    LeaveCriticalSection(&listen_cs);
    SetEvent(mgr_event);
554 555 556 557 558
  }
}

static void RPCRT4_stop_listen(void)
{
559 560 561 562 563 564 565 566 567 568
  EnterCriticalSection(&listen_cs);
  if (listen_count == -1)
    LeaveCriticalSection(&listen_cs);
  else if (--listen_count == -1) {
    std_listen = FALSE;
    LeaveCriticalSection(&listen_cs);
    SetEvent(mgr_event);
  } else
    LeaveCriticalSection(&listen_cs);
  assert(listen_count > -2);
569 570 571 572
}

static RPC_STATUS RPCRT4_use_protseq(RpcServerProtseq* ps)
{
573
  RPCRT4_CreateConnection(&ps->conn, TRUE, ps->Protseq, NULL, ps->Endpoint, NULL, NULL);
574 575 576 577 578 579

  EnterCriticalSection(&server_cs);
  ps->Next = protseqs;
  protseqs = ps;
  LeaveCriticalSection(&server_cs);

580
  if (std_listen) SetEvent(mgr_event);
581 582 583 584 585 586 587 588 589 590 591 592

  return RPC_S_OK;
}

/***********************************************************************
 *             RpcServerInqBindings (RPCRT4.@)
 */
RPC_STATUS WINAPI RpcServerInqBindings( RPC_BINDING_VECTOR** BindingVector )
{
  RPC_STATUS status;
  DWORD count;
  RpcServerProtseq* ps;
593
  RpcConnection* conn;
594

595 596 597
  if (BindingVector)
    TRACE("(*BindingVector == ^%p)\n", *BindingVector);
  else
598
    ERR("(BindingVector == NULL!!?)\n");
599

600
  EnterCriticalSection(&server_cs);
601
  /* count connections */
602 603 604
  count = 0;
  ps = protseqs;
  while (ps) {
605 606
    conn = ps->conn;
    while (conn) {
607
      count++;
608
      conn = conn->Next;
609 610 611 612 613 614 615 616 617 618 619 620
    }
    ps = ps->Next;
  }
  if (count) {
    /* export bindings */
    *BindingVector = HeapAlloc(GetProcessHeap(), 0,
                              sizeof(RPC_BINDING_VECTOR) +
                              sizeof(RPC_BINDING_HANDLE)*(count-1));
    (*BindingVector)->Count = count;
    count = 0;
    ps = protseqs;
    while (ps) {
621 622 623 624
      conn = ps->conn;
      while (conn) {
       RPCRT4_MakeBinding((RpcBinding**)&(*BindingVector)->BindingH[count],
                          conn);
625
       count++;
626
       conn = conn->Next;
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
      }
      ps = ps->Next;
    }
    status = RPC_S_OK;
  } else {
    *BindingVector = NULL;
    status = RPC_S_NO_BINDINGS;
  }
  LeaveCriticalSection(&server_cs);
  return status;
}

/***********************************************************************
 *             RpcServerUseProtseqEpA (RPCRT4.@)
 */
642
RPC_STATUS WINAPI RpcServerUseProtseqEpA( unsigned char *Protseq, UINT MaxCalls, unsigned char *Endpoint, LPVOID SecurityDescriptor )
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
{
  RPC_POLICY policy;
  
  TRACE( "(%s,%u,%s,%p)\n", Protseq, MaxCalls, Endpoint, SecurityDescriptor );
  
  /* This should provide the default behaviour */
  policy.Length        = sizeof( policy );
  policy.EndpointFlags = 0;
  policy.NICFlags      = 0;
  
  return RpcServerUseProtseqEpExA( Protseq, MaxCalls, Endpoint, SecurityDescriptor, &policy );
}

/***********************************************************************
 *             RpcServerUseProtseqEpW (RPCRT4.@)
 */
RPC_STATUS WINAPI RpcServerUseProtseqEpW( LPWSTR Protseq, UINT MaxCalls, LPWSTR Endpoint, LPVOID SecurityDescriptor )
{
  RPC_POLICY policy;
  
  TRACE( "(%s,%u,%s,%p)\n", debugstr_w( Protseq ), MaxCalls, debugstr_w( Endpoint ), SecurityDescriptor );
  
  /* This should provide the default behaviour */
  policy.Length        = sizeof( policy );
  policy.EndpointFlags = 0;
  policy.NICFlags      = 0;
  
  return RpcServerUseProtseqEpExW( Protseq, MaxCalls, Endpoint, SecurityDescriptor, &policy );
}

/***********************************************************************
 *             RpcServerUseProtseqEpExA (RPCRT4.@)
 */
676
RPC_STATUS WINAPI RpcServerUseProtseqEpExA( unsigned char *Protseq, UINT MaxCalls, unsigned char *Endpoint, LPVOID SecurityDescriptor,
677 678 679 680
                                            PRPC_POLICY lpPolicy )
{
  RpcServerProtseq* ps;

681
  TRACE("(%s,%u,%s,%p,{%u,%lu,%lu})\n", debugstr_a( Protseq ), MaxCalls,
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
       debugstr_a( Endpoint ), SecurityDescriptor,
       lpPolicy->Length, lpPolicy->EndpointFlags, lpPolicy->NICFlags );

  ps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcServerProtseq));
  ps->MaxCalls = MaxCalls;
  ps->Protseq = RPCRT4_strdupA(Protseq);
  ps->Endpoint = RPCRT4_strdupA(Endpoint);

  return RPCRT4_use_protseq(ps);
}

/***********************************************************************
 *             RpcServerUseProtseqEpExW (RPCRT4.@)
 */
RPC_STATUS WINAPI RpcServerUseProtseqEpExW( LPWSTR Protseq, UINT MaxCalls, LPWSTR Endpoint, LPVOID SecurityDescriptor,
                                            PRPC_POLICY lpPolicy )
{
  RpcServerProtseq* ps;

701
  TRACE("(%s,%u,%s,%p,{%u,%lu,%lu})\n", debugstr_w( Protseq ), MaxCalls,
702 703 704 705 706 707 708 709 710 711 712
       debugstr_w( Endpoint ), SecurityDescriptor,
       lpPolicy->Length, lpPolicy->EndpointFlags, lpPolicy->NICFlags );

  ps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcServerProtseq));
  ps->MaxCalls = MaxCalls;
  ps->Protseq = RPCRT4_strdupWtoA(Protseq);
  ps->Endpoint = RPCRT4_strdupWtoA(Endpoint);

  return RPCRT4_use_protseq(ps);
}

713 714 715
/***********************************************************************
 *             RpcServerUseProtseqA (RPCRT4.@)
 */
716
RPC_STATUS WINAPI RpcServerUseProtseqA(unsigned char *Protseq, unsigned int MaxCalls, void *SecurityDescriptor)
717
{
718
  TRACE("(Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)\n", debugstr_a(Protseq), MaxCalls, SecurityDescriptor);
719
  return RpcServerUseProtseqEpA(Protseq, MaxCalls, NULL, SecurityDescriptor);
720 721 722 723 724 725 726
}

/***********************************************************************
 *             RpcServerUseProtseqW (RPCRT4.@)
 */
RPC_STATUS WINAPI RpcServerUseProtseqW(LPWSTR Protseq, unsigned int MaxCalls, void *SecurityDescriptor)
{
727
  TRACE("Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)\n", debugstr_w(Protseq), MaxCalls, SecurityDescriptor);
728
  return RpcServerUseProtseqEpW(Protseq, MaxCalls, NULL, SecurityDescriptor);
729 730
}

731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
/***********************************************************************
 *             RpcServerRegisterIf (RPCRT4.@)
 */
RPC_STATUS WINAPI RpcServerRegisterIf( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv )
{
  TRACE("(%p,%s,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv);
  return RpcServerRegisterIf2( IfSpec, MgrTypeUuid, MgrEpv, 0, RPC_C_LISTEN_MAX_CALLS_DEFAULT, (UINT)-1, NULL );
}

/***********************************************************************
 *             RpcServerRegisterIfEx (RPCRT4.@)
 */
RPC_STATUS WINAPI RpcServerRegisterIfEx( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv,
                       UINT Flags, UINT MaxCalls, RPC_IF_CALLBACK_FN* IfCallbackFn )
{
  TRACE("(%p,%s,%p,%u,%u,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv, Flags, MaxCalls, IfCallbackFn);
  return RpcServerRegisterIf2( IfSpec, MgrTypeUuid, MgrEpv, Flags, MaxCalls, (UINT)-1, IfCallbackFn );
}

/***********************************************************************
 *             RpcServerRegisterIf2 (RPCRT4.@)
 */
RPC_STATUS WINAPI RpcServerRegisterIf2( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv,
                      UINT Flags, UINT MaxCalls, UINT MaxRpcSize, RPC_IF_CALLBACK_FN* IfCallbackFn )
{
  PRPC_SERVER_INTERFACE If = (PRPC_SERVER_INTERFACE)IfSpec;
  RpcServerInterface* sif;
  int i;

  TRACE("(%p,%s,%p,%u,%u,%u,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv, Flags, MaxCalls,
         MaxRpcSize, IfCallbackFn);
  TRACE(" interface id: %s %d.%d\n", debugstr_guid(&If->InterfaceId.SyntaxGUID),
                                     If->InterfaceId.SyntaxVersion.MajorVersion,
                                     If->InterfaceId.SyntaxVersion.MinorVersion);
Greg Turner's avatar
Greg Turner committed
765 766 767
  TRACE(" transfer syntax: %s %d.%d\n", debugstr_guid(&If->TransferSyntax.SyntaxGUID),
                                        If->TransferSyntax.SyntaxVersion.MajorVersion,
                                        If->TransferSyntax.SyntaxVersion.MinorVersion);
768 769 770 771 772 773 774 775 776 777 778 779 780 781
  TRACE(" dispatch table: %p\n", If->DispatchTable);
  if (If->DispatchTable) {
    TRACE("  dispatch table count: %d\n", If->DispatchTable->DispatchTableCount);
    for (i=0; i<If->DispatchTable->DispatchTableCount; i++) {
      TRACE("   entry %d: %p\n", i, If->DispatchTable->DispatchTable[i]);
    }
    TRACE("  reserved: %ld\n", If->DispatchTable->Reserved);
  }
  TRACE(" protseq endpoint count: %d\n", If->RpcProtseqEndpointCount);
  TRACE(" default manager epv: %p\n", If->DefaultManagerEpv);
  TRACE(" interpreter info: %p\n", If->InterpreterInfo);

  sif = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcServerInterface));
  sif->If           = If;
782
  if (MgrTypeUuid) {
783
    memcpy(&sif->MgrTypeUuid, MgrTypeUuid, sizeof(UUID));
784 785
    sif->MgrEpv       = MgrEpv;
  } else {
786
    memset(&sif->MgrTypeUuid, 0, sizeof(UUID));
787 788
    sif->MgrEpv       = If->DefaultManagerEpv;
  }
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
  sif->Flags        = Flags;
  sif->MaxCalls     = MaxCalls;
  sif->MaxRpcSize   = MaxRpcSize;
  sif->IfCallbackFn = IfCallbackFn;

  EnterCriticalSection(&server_cs);
  sif->Next = ifs;
  ifs = sif;
  LeaveCriticalSection(&server_cs);

  if (sif->Flags & RPC_IF_AUTOLISTEN) {
    /* well, start listening, I think... */
    RPCRT4_start_listen();
  }

  return RPC_S_OK;
}

807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
/***********************************************************************
 *             RpcServerUnregisterIf (RPCRT4.@)
 */
RPC_STATUS WINAPI RpcServerUnregisterIf( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, UINT WaitForCallsToComplete )
{
  FIXME("(IfSpec == (RPC_IF_HANDLE)^%p, MgrTypeUuid == %s, WaitForCallsToComplete == %u): stub\n",
    IfSpec, debugstr_guid(MgrTypeUuid), WaitForCallsToComplete);

  return RPC_S_OK;
}

/***********************************************************************
 *             RpcServerUnregisterIfEx (RPCRT4.@)
 */
RPC_STATUS WINAPI RpcServerUnregisterIfEx( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, int RundownContextHandles )
{
  FIXME("(IfSpec == (RPC_IF_HANDLE)^%p, MgrTypeUuid == %s, RundownContextHandles == %d): stub\n",
    IfSpec, debugstr_guid(MgrTypeUuid), RundownContextHandles);

  return RPC_S_OK;
}

829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
/***********************************************************************
 *             RpcObjectSetType (RPCRT4.@)
 *
 * PARAMS
 *   ObjUuid  [I] "Object" UUID
 *   TypeUuid [I] "Type" UUID
 *
 * RETURNS
 *   RPC_S_OK                 The call succeeded
 *   RPC_S_INVALID_OBJECT     The provided object (nil) is not valid
 *   RPC_S_ALREADY_REGISTERED The provided object is already registered
 *
 * Maps "Object" UUIDs to "Type" UUID's.  Passing the nil UUID as the type
 * resets the mapping for the specified object UUID to nil (the default).
 * The nil object is always associated with the nil type and cannot be
 * reassigned.  Servers can support multiple implementations on the same
 * interface by registering different end-point vectors for the different
 * types.  There's no need to call this if a server only supports the nil
 * type, as is typical.
 */
RPC_STATUS WINAPI RpcObjectSetType( UUID* ObjUuid, UUID* TypeUuid )
{
  RpcObjTypeMap *map = RpcObjTypeMaps, *prev = NULL;
  RPC_STATUS dummy;

  TRACE("(ObjUUID == %s, TypeUuid == %s).\n", debugstr_guid(ObjUuid), debugstr_guid(TypeUuid));
  if ((! ObjUuid) || UuidIsNil(ObjUuid, &dummy)) {
    /* nil uuid cannot be remapped */
    return RPC_S_INVALID_OBJECT;
  }

  /* find the mapping for this object if there is one ... */
  while (map) {
    if (! UuidCompare(ObjUuid, &map->Object, &dummy)) break;
    prev = map;
    map = map->next;
  }
  if ((! TypeUuid) || UuidIsNil(TypeUuid, &dummy)) {
    /* ... and drop it from the list */
    if (map) {
      if (prev) 
        prev->next = map->next;
      else
        RpcObjTypeMaps = map->next;
      HeapFree(GetProcessHeap(), 0, map);
    }
  } else {
    /* ... , fail if we found it ... */
    if (map)
      return RPC_S_ALREADY_REGISTERED;
    /* ... otherwise create a new one and add it in. */
    map = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcObjTypeMap));
    memcpy(&map->Object, ObjUuid, sizeof(UUID));
    memcpy(&map->Type, TypeUuid, sizeof(UUID));
    map->next = NULL;
    if (prev)
      prev->next = map; /* prev is the last map in the linklist */
    else
      RpcObjTypeMaps = map;
  }

  return RPC_S_OK;
}

893 894 895
/***********************************************************************
 *             RpcServerRegisterAuthInfoA (RPCRT4.@)
 */
896
RPC_STATUS WINAPI RpcServerRegisterAuthInfoA( unsigned char *ServerPrincName, ULONG AuthnSvc, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn,
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
                            LPVOID Arg )
{
  FIXME( "(%s,%lu,%p,%p): stub\n", ServerPrincName, AuthnSvc, GetKeyFn, Arg );
  
  return RPC_S_UNKNOWN_AUTHN_SERVICE; /* We don't know any authentication services */
}

/***********************************************************************
 *             RpcServerRegisterAuthInfoW (RPCRT4.@)
 */
RPC_STATUS WINAPI RpcServerRegisterAuthInfoW( LPWSTR ServerPrincName, ULONG AuthnSvc, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn,
                            LPVOID Arg )
{
  FIXME( "(%s,%lu,%p,%p): stub\n", debugstr_w( ServerPrincName ), AuthnSvc, GetKeyFn, Arg );
  
  return RPC_S_UNKNOWN_AUTHN_SERVICE; /* We don't know any authentication services */
}

/***********************************************************************
 *             RpcServerListen (RPCRT4.@)
 */
RPC_STATUS WINAPI RpcServerListen( UINT MinimumCallThreads, UINT MaxCalls, UINT DontWait )
{
  TRACE("(%u,%u,%u)\n", MinimumCallThreads, MaxCalls, DontWait);

  if (!protseqs)
    return RPC_S_NO_PROTSEQS_REGISTERED;

925 926 927 928 929 930 931
  EnterCriticalSection(&listen_cs);

  if (std_listen) {
    LeaveCriticalSection(&listen_cs);
    return RPC_S_ALREADY_LISTENING;
  }

932 933
  RPCRT4_start_listen();

934 935
  LeaveCriticalSection(&listen_cs);

936 937
  if (DontWait) return RPC_S_OK;

938 939 940 941 942 943 944 945
  return RpcMgmtWaitServerListen();
}

/***********************************************************************
 *             RpcMgmtServerWaitListen (RPCRT4.@)
 */
RPC_STATUS WINAPI RpcMgmtWaitServerListen( void )
{
946
  RPC_STATUS rslt = RPC_S_OK;
947

948
  TRACE("\n");
949 950 951

  EnterCriticalSection(&listen_cs);

952
  if (!std_listen)
953 954
    if ( (rslt = RpcServerListen(1, 0, TRUE)) != RPC_S_OK ) {
      LeaveCriticalSection(&listen_cs);
955
      return rslt;
956
    }
957
  
958 959
  LeaveCriticalSection(&listen_cs);

960
  while (std_listen) {
961 962 963 964 965
    WaitForSingleObject(mgr_event, INFINITE);
    if (!std_listen) {
      Sleep(100); /* don't spin violently */
      TRACE("spinning.\n");
    }
966 967
  }

968
  return rslt;
969 970
}

971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
/***********************************************************************
 *             RpcMgmtStopServerListening (RPCRT4.@)
 */
RPC_STATUS WINAPI RpcMgmtStopServerListening ( RPC_BINDING_HANDLE Binding )
{
  TRACE("(Binding == (RPC_BINDING_HANDLE)^%p)\n", Binding);

  if (Binding) {
    FIXME("client-side invocation not implemented.\n");
    return RPC_S_WRONG_KIND_OF_BINDING;
  }
  
  /* hmm... */
  EnterCriticalSection(&listen_cs);
  while (std_listen)
    RPCRT4_stop_listen();
  LeaveCriticalSection(&listen_cs);

  return RPC_S_OK;
}

992 993 994
/***********************************************************************
 *             I_RpcServerStartListening (RPCRT4.@)
 */
995
RPC_STATUS WINAPI I_RpcServerStartListening( HWND hWnd )
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
{
  FIXME( "(%p): stub\n", hWnd );

  return RPC_S_OK;
}

/***********************************************************************
 *             I_RpcServerStopListening (RPCRT4.@)
 */
RPC_STATUS WINAPI I_RpcServerStopListening( void )
{
  FIXME( "(): stub\n" );

  return RPC_S_OK;
}

/***********************************************************************
 *             I_RpcWindowProc (RPCRT4.@)
 */
1015
UINT WINAPI I_RpcWindowProc( void *hWnd, UINT Message, UINT wParam, ULONG lParam )
1016 1017 1018 1019 1020
{
  FIXME( "(%p,%08x,%08x,%08lx): stub\n", hWnd, Message, wParam, lParam );

  return 0;
}