(*
    Title: 	Unix Socket Interface
    LastEdit:	"Mon Dec 14 12:07:40 1987"
    Author: 	Derek McAuley,Oms1,4663,329629
		Cambridge University Computer Laboratory
*)

DEFINITION MODULE UnixSocket;


IMPORT
  SYSTEM, SystemTypes;


EXPORT QUALIFIED
  SocketLevelOpt,
  
  T, PairT, Type, Option, Options, 
  Family, Protocol, Addr, Flag, Flags, HowShut,
  
  Socket, Accept, Bind, GetSockName, Listen, RecV, RecVFrom, 
  Send, SendTo, Shutdown, SocketPair, GetSockOpt, SetSockOpt;


CONST
  SocketLevelOpt = 0ffffH;
  
  
TYPE
  T = INTEGER;
  PairT = ARRAY [0..1] OF T;
  Type = (sockUnused, sockStream, sockDatagram, 
          sockRaw, sockRDM, sockSeqPacket);
  Option = (soDebug, soAcceptConn, soReuseAddr, soKeepAlive,
            soDontRoute, soBroadcast, soUseLoopback, soLinger);
  Options = SET OF Option;
  Family = (afUnspecified, afUnix, afInet, afIMPLink, afPUP, afChaos,
            afNS, afNBS, afECMA, afDatakit, afCCITT, afSNA);
  Protocol = Family;
  Addr = 
    RECORD
    CASE family:Family OF
      afUnix: name:SystemTypes.String;
    | afInet: port, ipAddr:CARDINAL
    | afUnspecified,
      afIMPLink,
      afPUP,
      afChaos,
      afNS,
      afNBS,
      afECMA,
      afDatakit,
      afCCITT,
      afSNA:
    END
    END;
  Flag = (msgNull, msgOob, msgPeek);
  Flags = SET OF Flag;
  HowShut = (NoReceives, NoSends, NoSendsOrReceives);
  

PROCEDURE Socket(
      af:  Family;
      type:Type; 
      prot:Protocol
 ):T;

PROCEDURE Accept(
      sock:   T;
  VAR addr:   Addr (* Out *)
 ):T;

PROCEDURE Bind(
      sock:   T;
  VAR name:   Addr (* In *)
 ):INTEGER;

PROCEDURE GetSockName(
      sock:   T; 
  VAR name:   Addr   (* Out *)
 ):INTEGER;

PROCEDURE Listen(
      sock:T;
      backlog:INTEGER
 ):INTEGER;

PROCEDURE RecV(
      sock: T; 
      buf:  SYSTEM.ADDRESS;
      len:  INTEGER;
      flags:Flags
 ):INTEGER;

PROCEDURE RecVFrom(
      sock:   T; 
      buf:    SYSTEM.ADDRESS;
      len:    INTEGER;
      flags:  Flags;
  VAR from:   Addr  (* InOut *)
 ):INTEGER;
  
PROCEDURE Send(
      sock: T;
      msg:  SYSTEM.ADDRESS;
      len:  INTEGER;
      flags:Flags
 ):INTEGER;

PROCEDURE SendTo(
      sock: T;
      msg:  SYSTEM.ADDRESS;
      len:  INTEGER;
      flags:Flags;
  VAR to:   Addr   (* In *)
 ):INTEGER;

PROCEDURE Shutdown(
      sock:T; 
      how: HowShut
 ):INTEGER;

PROCEDURE SocketPair(
      af:  Family;
      type:Type;
      prot:Protocol;
  VAR sv:  PairT     (* Out *)
 ):INTEGER;

PROCEDURE GetSockOpt(
      sock:T; 
      level, optname:INTEGER; 
      optval:SYSTEM.ADDRESS;
  VAR optlen:INTEGER (* InOut *)
 ):INTEGER;

PROCEDURE SetSockOpt(
      sock:T; 
      level, optname:INTEGER; 
      optval:SYSTEM.ADDRESS;
  VAR optlen:INTEGER (* In *)
 ):INTEGER;

END UnixSocket.
