home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / vptcp110.zip / SOCKETS.INT < prev    next >
Text File  |  1996-06-11  |  11KB  |  371 lines

  1. Unit Sockets;
  2.  
  3. (* Virtual Pascal TCPIP Sockets Library v1.10
  4.  * Copyright 1996 Antony T Curtis
  5.  *)
  6.  
  7. (*
  8.  * Constants and structures defined by the internet system,
  9.  * Per RFC 790, September 1981.
  10.  *)
  11.  
  12.  
  13. {$AlignRec-}{$PureInt+}
  14. interface
  15. Uses Os2Def,Use32;
  16.  
  17. type
  18.   TSocket        =Integer;
  19.   UShort        =Os2Def.UShort;
  20.  
  21. Const
  22.   Max_Alias        =4;
  23.   PZERO         =0;
  24.   BSD            =43;
  25.  
  26.   MAXHOSTNAMELEN    =120;
  27.   MAXPATHLEN        =80;
  28.   MAXSocketS        =2048;
  29.  
  30.   SIGALRM        = 0;
  31.  
  32.   SOMAXCONN        = 5;        (* Maximum queue length specifiable by listen. *)
  33.  
  34.  
  35. Const
  36. (*
  37.  * Protocols
  38.  *)
  39.   IPPROTO_IP            = 0;           (* dummy for IP *)
  40.   IPPROTO_ICMP            = 1;           (* control message protocol *)
  41.   IPPROTO_GGP            = 3;           (* gateway^2 (deprecated) *)
  42.   IPPROTO_TCP            = 6;           (* tcp *)
  43.   IPPROTO_EGP            = 8;           (* exterior gateway protocol *)
  44.   IPPROTO_PUP            = 12;           (* pup *)
  45.   IPPROTO_UDP            = 17;           (* user datagram protocol *)
  46.   IPPROTO_IDP            = 22;           (* xns idp *)
  47.  
  48.   IPPROTO_RAW            = 255;           (* raw IP packet *)
  49.   IPPROTO_MAX            = 256;
  50.  
  51.  
  52. (*
  53.  * Ports < IPPORT_RESERVED are reserved for
  54.  * privileged processes (e.g. root).
  55.  * Ports > IPPORT_USERRESERVED are reserved
  56.  * for servers, not necessarily privileged.
  57.  *)
  58.   IPPORT_RESERVED        = 1024;
  59.   IPPORT_USERRESERVED        = 5000;
  60.  
  61. (*
  62.  * Link numbers
  63.  *)
  64.   IMPLINK_IP            = 155;
  65.   IMPLINK_LOWEXPER        = 156;
  66.   IMPLINK_HIGHEXPER        = 158;
  67.  
  68. (*
  69.  * Definitions of bits in internet address integers.
  70.  * On subnets, the decomposition of addresses to host and net parts
  71.  * is done according to subnet mask, not the masks here.
  72.  *)
  73.   IN_CLASSA_NET         = $ff000000;
  74.   IN_CLASSA_NSHIFT        = 24;
  75.   IN_CLASSA_HOST        = $00ffffff;
  76.   IN_CLASSA_MAX         = 128;
  77.  
  78.   IN_CLASSB_NET         = $ffff0000;
  79.   IN_CLASSB_NSHIFT        = 16;
  80.   IN_CLASSB_HOST        = $0000ffff;
  81.   IN_CLASSB_MAX         = 65536;
  82.  
  83.   IN_CLASSC_NET         = $ffffff00;
  84.   IN_CLASSC_NSHIFT        = 8;
  85.   IN_CLASSC_HOST        = $000000ff;
  86.  
  87.   INADDR_ANY            = $00000000;
  88.   INADDR_BROADCAST        = $ffffffff;       (* must be masked *)
  89.   INADDR_NONE            = $ffffffff;        (* -1 return *)
  90.  
  91.   IN_LOOPBACKNET        = 127;            (* official! *)
  92.  
  93. (*
  94.  * Internet address (a structure for historical reasons)
  95.  *)
  96. Type
  97.   Tin_Addr            =record
  98.     s_addr            :ULong;
  99.   end;
  100.  
  101. (*
  102.  * Socket address, internet style.
  103.  *)
  104.   TSockAddr_in            =record
  105.     sin_family            :UShort;
  106.     sin_port            :UShort;
  107.     sin_addr            :Tin_Addr;
  108.     sin_zero            :array[0..7] of byte;
  109.   end;
  110.  
  111. (*
  112.  * Structure used for manipulating linger option.
  113.  *)
  114.   Tlinger        =record
  115.     l_onoff        :Integer;
  116.     l_linger        :Integer;
  117.   end;
  118.  
  119. (*
  120.  * Options for use with getsockopt at the IP level.
  121.  *)
  122. Const
  123.   IP_OPTIONS            = 1;           (* set/get IP per-packet options *)
  124.  
  125. (*
  126.  * Definitions related to Sockets: types, address families, options.
  127.  *)
  128.  
  129. (*
  130.  * Types
  131.  *)
  132. Const
  133.   SOCK_STREAM        = 1;           (* stream Socket *)
  134.   SOCK_DGRAM        = 2;           (* datagram Socket *)
  135.   SOCK_RAW        = 3;           (* raw-protocol interface *)
  136.   SOCK_RDM        = 4;           (* reliably-delivered message *)
  137.   SOCK_SEQPACKET    = 5;           (* sequenced packet stream *)
  138.  
  139. (*
  140.  * Option flags per-Socket.
  141.  *)
  142.   SO_DEBUG        = $0001;       (* turn on debugging info recording *)
  143.   SO_ACCEPTCONN     = $0002;       (* Socket has had listen() *)
  144.   SO_REUSEADDR        = $0004;       (* allow local address reuse *)
  145.   SO_KEEPALIVE        = $0008;       (* keep connections alive *)
  146.   SO_DONTROUTE        = $0010;       (* just use interface addresses *)
  147.   SO_BROADCAST        = $0020;       (* permit sending of broadcast msgs *)
  148.   SO_USELOOPBACK    = $0040;       (* bypass hardware when possible *)
  149.   SO_LINGER        = $0080;       (* linger on close if data present *)
  150.   SO_OOBINLINE        = $0100;       (* leave received OOB data in line *)
  151.  
  152. (*
  153.  * Additional options, not kept in so_options.
  154.  *)
  155.   SO_SNDBUF        = $1001;       (* send buffer size *)
  156.   SO_RCVBUF        = $1002;       (* receive buffer size *)
  157.   SO_SNDLOWAT        = $1003;       (* send low-water mark *)
  158.   SO_RCVLOWAT        = $1004;       (* receive low-water mark *)
  159.   SO_SNDTIMEO        = $1005;       (* send timeout *)
  160.   SO_RCVTIMEO        = $1006;       (* receive timeout *)
  161.   SO_ERROR        = $1007;       (* get error status and clear *)
  162.   SO_TYPE        = $1008;       (* get Socket type *)
  163.  
  164. (*
  165.  * Level number for (get/set)sockopt() to apply to Socket itself.
  166.  *)
  167.    SOL_Socket        = $ffff;       (* options for Socket level *)
  168.  
  169. (*
  170.  * Address families.
  171.  *)
  172.    AF_UNSPEC        = 0;           (* unspecified *)
  173.    AF_UNIX        = 1;           (* local to host (pipes, portals) *)
  174.    AF_INET        = 2;           (* internetwork: UDP, TCP, etc. *)
  175.    AF_IMPLINK        = 3;           (* arpanet imp addresses *)
  176.    AF_PUP        = 4;           (* pup protocols: e.g. BSP *)
  177.    AF_CHAOS        = 5;           (* mit CHAOS protocols *)
  178.    AF_NS        = 6;           (* XEROX NS protocols *)
  179.    AF_NBS        = 7;           (* nbs protocols *)
  180.    AF_ECMA        = 8;           (* european computer manufacturers *)
  181.    AF_DATAKIT        = 9;           (* datakit protocols *)
  182.    AF_CCITT        = 10;           (* CCITT protocols, X.25 etc *)
  183.    AF_SNA        = 11;           (* IBM SNA *)
  184.    AF_DECnet        = 12;           (* DECnet *)
  185.    AF_DLI        = 13;           (* Direct data link interface *)
  186.    AF_LAT        = 14;           (* LAT *)
  187.    AF_HYLINK        = 15;           (* NSC Hyperchannel *)
  188.    AF_APPLETALK     = 16;           (* Apple Talk *)
  189.  
  190.    AF_OS2        = AF_UNIX;
  191.  
  192.    AF_NB        = 17;           (* Netbios *)
  193.    AF_NETBIOS        = AF_NB;
  194.  
  195.    AF_MAX        = 18;
  196. (*
  197.  * Protocol families, same as address families for now.
  198.  *)
  199.    PF_UNSPEC        = AF_UNSPEC;
  200.    PF_UNIX        = AF_UNIX;
  201.    PF_INET        = AF_INET;
  202.    PF_IMPLINK        = AF_IMPLINK;
  203.    PF_PUP        = AF_PUP;
  204.    PF_CHAOS        = AF_CHAOS;
  205.    PF_NS        = AF_NS;
  206.    PF_NBS        = AF_NBS;
  207.    PF_ECMA        = AF_ECMA;
  208.    PF_DATAKIT        = AF_DATAKIT;
  209.    PF_CCITT        = AF_CCITT;
  210.    PF_SNA        = AF_SNA;
  211.    PF_DECnet        = AF_DECnet;
  212.    PF_DLI        = AF_DLI;
  213.    PF_LAT        = AF_LAT;
  214.    PF_HYLINK        = AF_HYLINK;
  215.    PF_APPLETALK     = AF_APPLETALK;
  216.    PF_NETBIOS        = AF_NB;
  217.    PF_NB        = AF_NB;
  218.    PF_OS2        = PF_UNIX;
  219.    PF_MAX        = AF_MAX;
  220.  
  221.  
  222.   FREAD         = 1;
  223.   FWRITE        = 2;
  224.  
  225.   MSG_OOB        = $1;           (* process out-of-band data *)
  226.   MSG_PEEK        = $2;           (* peek at incoming message *)
  227.   MSG_DONTROUTE     = $4;           (* send without using routing tables *)
  228.   MSG_FULLREAD        = $8;           (* send without using routing tables *)
  229.  
  230.   MSG_MAXIOVLEN     = 16;
  231.  
  232. (*
  233.  * Structure used by kernel to store most
  234.  * addresses.
  235.  *)
  236. Type
  237.   TSockAddr        =record
  238.     sa_family        :UShort;
  239.     sa_data        :Array[0..13] of Byte;
  240.   end;
  241. (*
  242.  * Structure used by kernel to pass protocol
  243.  * information in raw Sockets.
  244.  *)
  245.   TSockProto        =record
  246.     sp_family        :UShort;
  247.     sp_protocol     :UShort;
  248.   end;
  249.  
  250.   TioVec        =record
  251.     iov_base        :PChar;
  252.     iov_len        :Integer;
  253.   end;
  254.  
  255. (*
  256.  * Message header for recvmsg and sendmsg calls.
  257.  *)
  258.   TMsgHdr        =record
  259.     msg_name        :PChar;
  260.     msg_namelen     :Integer;
  261.     msg_iov        :^TioVec;
  262.     msg_iovlen        :Integer;
  263.     msg_accrights    :PChar;
  264.     msg_accrightslen    :Integer;
  265.   end;
  266.  
  267.   Tuio            =record
  268.     uio_iov        :^TioVec;
  269.     uio_iovcnt        :Integer;
  270.     uio_offset        :Longint;
  271.     uio_segflg        :Integer;
  272.     uio_resid        :Integer;
  273.   end;
  274.  
  275.   Tuio_rw        =(UIO_READ, UIO_WRITE);
  276.  
  277.   THostStr        =String[MAXHOSTNAMELEN];
  278.   THostAlias        =Array[0..Max_Alias-1] of THostStr;
  279.  
  280.   THostEnt        =record
  281.     h_name        :THostStr;        (* official name of host *)
  282.     h_aliases        :THostAlias;        (* alias list *)
  283.     h_addrtype        :Integer;        (* host address type *)
  284.     h_length        :Integer;        (* length of address *)
  285.     h_addr_list     :Array[0..3]of Tin_Addr;(* list of addresses from name server *)
  286.   end;
  287.  
  288.   TNetEnt        =record
  289.     n_name        :THostStr;        (* official name of net *)
  290.     n_aliases        :THostAlias;        (* alias list *)
  291.     n_addrtype        :Integer;        (* net address type *)
  292.     n_net        :ULong;         (* network # *)
  293.   end;
  294.  
  295.   TServEnt        =record
  296.     s_name        :THostStr;        (* official service name *)
  297.     s_aliases        :THostAlias;        (* alias list *)
  298.     s_port        :Integer;        (* port # *)
  299.     s_proto        :THostStr;        (* protocol to use *)
  300.   end;
  301.  
  302.   TProtoEnt        =record
  303.     p_name        :THostStr;        (* official protocol name *)
  304.     p_aliases        :THostAlias;        (* alias list *)
  305.     p_proto        :Integer;        (* protocol # *)
  306.   end;
  307.  
  308.   PFD_Set        =^TFD_Set;
  309.   TFD_Set        =Array[0..255] of Byte;
  310.  
  311. Procedure FD_Zero(var fd:TFD_Set);inline;
  312. begin FillChar(fd,sizeof(fd),0); end;
  313. Procedure FD_Set(Sock:TSocket;var fd:TFD_Set);inline;
  314. begin fd[Sock div 8]:=fd[Sock div 8] or (1 shl (Sock mod 8)); end;
  315. Function FD_IsSet(Sock:TSocket;var fd:TFD_Set):Boolean;inline;
  316. begin FD_IsSet:=(fd[Sock div 8] and (1 shl (Sock mod 8)))<>0; end;
  317.  
  318. threadvar
  319.   SockErrorProc     :PChar;
  320.   SockError        :Integer;
  321.  
  322. procedure Sock_Init;
  323. function  Sock_New(domain,typ,protocol:Integer):TSocket;
  324. function  Sock_Accept(Sock:TSocket;var address:TSockAddr):TSocket;
  325. procedure Sock_Connect(Sock:TSocket;var address:TSockAddr);
  326. procedure Sock_Close(Sock:TSocket);
  327. procedure Sock_Bind(Sock:TSocket;var address:TSockAddr);
  328. function  Sock_Listen(Sock:TSocket;backlog:Integer):Boolean;
  329. procedure Sock_Read(Sock:TSocket;var buf;size:Integer;var result:Integer);
  330. procedure Sock_Write(Sock:TSocket;var buf;size:Integer;var result:Integer);
  331. function  Sock_GetHostID:ULong;
  332. Function  Sock_Error:Boolean;
  333. function  Sock_ErrorID:Integer;
  334. procedure PSock_Error(st:PChar);
  335. procedure Sock_Select(num:Integer;rd,wr,ex:PFD_Set;timeout:Longint);
  336.  
  337. function  inet_addr(addr:String):ULong;
  338. function  inet_ntoa(var addr:Tin_Addr):String;
  339.  
  340. Function  GetHostByName(var res:THostEnt;host:String):Boolean;
  341. Function  GetHostByAddr(var res:THostEnt;var address:Tin_addr;protocol:Integer):Boolean;
  342. Function  GetServiceByName(var res:TServEnt;Service,Protocol:String):Boolean;
  343. Function  GetServiceByPort(var res:TServEnt;Port:UShort;Protocol:String):Boolean;
  344. Function  GetNetByName(var res:TNetEnt;Net:String):Boolean;
  345. Function  GetNetByAddr(var res:TNetEnt;address:ULong):Boolean;
  346. Function  GetProtocolByName(var res:TProtoEnt;Protocol:String):Boolean;
  347. Function  GetProtocolByNumber(var res:TProtoEnt;protocol:Integer):Boolean;
  348.  
  349. Function  lswap(l:ULong):ULong;
  350. Function  bswap(w:UShort):UShort;
  351. function  Sock_Okay(Sock:TSocket):Boolean;
  352. Function  htonl(var l:ULong):ULong; inline;     begin htonl:=lswap(l); end;
  353. Function  ntohl(var l:ULong):ULong; inline;     begin ntohl:=lswap(l); end;
  354. Function  htons(var w:UShort):UShort; inline;     begin htons:=bswap(w); end;
  355. Function  ntohs(var w:UShort):UShort; inline;     begin ntohs:=bswap(w); end;
  356.  
  357. procedure Sock_SetAddr(var address:TSockAddr;fam,port:UShort;ip:ULong); inline;
  358. begin
  359.   with TSockAddr_in(address) do begin
  360.     sin_family:=fam;
  361.     sin_port:=port;
  362.     sin_addr.s_addr:=ip;
  363.   end;
  364. end;
  365.  
  366. {Function sosetsockopt(Socket,level,optVar:Integer;var optVal;rc:Integer):Integer;}
  367.  
  368. implementation
  369. begin
  370. end.
  371.