-- File: RPCInternal.mesa - last edit:
-- AOF                  8-Feb-88 16:33:22
-- HGM                 21-Jan-84 20:39:32
-- Andrew Birrell       2-Mar-82 14:11:30
-- Copyright (C) 1982, 1984, 1988 by Xerox Corporation. All rights reserved. 


DIRECTORY
  PupDefs USING [PupBuffer],
  DESFace USING [IV, Key],
  MesaRPC USING [
    Conversation, InterfaceName, Principal, SecurityLevel, VersionRange],
  MesaRPCLupine USING [DataLength, Dispatcher, RPCPkt],
  RPCPkt USING [
    CallCount, ConnectionID, ConversationID, DispatcherDetails, DispatcherID,
    ExportHandle, Header, PktConversationID, PktID, Machine];

RPCInternal: DEFINITIONS =

  BEGIN

  -- ******** Modules for start-up sequence.  Main control is RPCPktIO ******** --

  RPCBinding: PROGRAM;
  RPCSecurity: PROGRAM;
  RPCPktStreams: PROGRAM;



  -- ******** Binding:  exported by RPCBinding ******** --

  ExportInstance: TYPE = RECORD [
    -- Exporter's record of exported interface --
    id: RPCPkt.DispatcherID,
    dispatcher: MesaRPCLupine.Dispatcher,
    mds: CARDINAL,
    name: MesaRPC.InterfaceName,
    stubProtocol: MesaRPC.VersionRange];

  ExportTable: TYPE = RECORD [
    used: CARDINAL,
    entries: SEQUENCE length: RPCPkt.ExportHandle OF ExportInstance];

  exportTable: LONG POINTER TO ExportTable;

  ImportInstance: TYPE = RECORD [
    -- Importer's record of successful binding --
    host: RPCPkt.Machine, dispatcher: RPCPkt.DispatcherDetails];



  -- ******** Authentication/Security: exported by RPCSecurity ******** --

  ConversationObject: TYPE = RECORD [
    next: LONG POINTER TO ConversationObject,
    id: RPCPkt.ConversationID,
    level: MesaRPC.SecurityLevel,
    key: DESFace.Key,  -- conversation key --
    iv: DESFace.IV,
    originator: MesaRPC.Principal,
    responder: MesaRPC.Principal,
    authenticator: LONG DESCRIPTOR FOR ARRAY OF WORD];

  firstConversation: RPCPkt.PktConversationID;  -- for unencrypted conversations --

  EncryptPkt: PROC [pkt: MesaRPCLupine.RPCPkt, l: MesaRPCLupine.DataLength]
    RETURNS [CARDINAL];
  -- Encrypts packet using pkt.convHandle; "l" is data length of pkt;
  -- Returns Pup length of packet. --

  DecryptPkt: PROC [
    header: LONG POINTER TO RPCPkt.Header, convHandle: MesaRPC.Conversation]
    RETURNS [ok: BOOLEAN, l: MesaRPCLupine.DataLength];
  -- Decrypts packet using pkt.convHandle; ok iff checksum matches;
  -- Returns data length of packet --

  GetConnectionState: PROC [decrypted: BOOLEAN, callPkt: MesaRPCLupine.RPCPkt]
    RETURNS [
      ok: BOOLEAN, id: RPCPkt.ConnectionID, call: RPCPkt.CallCount,
      conv: MesaRPC.Conversation, l: MesaRPCLupine.DataLength];
  -- On entry, packet is decrypted iff "decrypted".
  -- On exit, packet is decrypted if "ok".
  -- If "ok" and not "decrypted", returns pkt data length in "l".
  -- Sends RFA and accepts response; results valid iff "ok" --

  ReplyToRFA: PROC [
    b: PupDefs.PupBuffer,  -- incoming packet --
    callHeader: LONG POINTER TO RPCPkt.Header,  -- encrypted --
    callPktID: RPCPkt.PktID -- clear -- , convHandle: MesaRPC.Conversation]
    RETURNS [BOOLEAN];
  -- Generates RFA response packet if request matches thisPktID --



  -- ******** Packet stream functions: exported by RPCPktStream ******** --

  DoSignal: PROC [
    b: PupDefs.PupBuffer, pktLength: MesaRPCLupine.DataLength,
    signalHandler: MesaRPCLupine.Dispatcher, convHandle: MesaRPC.Conversation]
    RETURNS [
      resumePkt: MesaRPCLupine.RPCPkt, resumeLength: MesaRPCLupine.DataLength,
      myLocalFrame: POINTER];
  -- Called from inside PktExchange to handle signal-back packets --

  ServerMain: PROCEDURE;
  -- FORK'ed when needed by RPCPktIO, to maintain an adequate stock of them --

  END.