home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pmos2002.zip / DEF / sockets.def < prev    next >
Text File  |  1997-11-27  |  24KB  |  466 lines

  1. <* M2EXTENSIONS + *>
  2.  
  3. DEFINITION MODULE ["SysCall"] Sockets;
  4.  
  5.         (********************************************************)
  6.         (*                                                      *)
  7.         (*           The network sockets interface              *)
  8.         (*                                                      *)
  9.         (*  Programmer:         P. Moylan                       *)
  10.         (*  Started:            18 August 1997                  *)
  11.         (*  Last edited:        27 November 1997                *)
  12.         (*  Status:             Partly implemented              *)
  13.         (*                                                      *)
  14.         (*      Still some missing functions, but they seem     *)
  15.         (*      to be functions that I'm never going to use.    *)
  16.         (*                                                      *)
  17.         (********************************************************)
  18.  
  19. (********************************************************************************)
  20. (*                                                                              *)
  21. (*  TYPICAL USE                                                                 *)
  22. (*    Note: I'm assuming Internet operations here (address family AF_INET).     *)
  23. (*    Nothing has been tested with other protocols.                             *)
  24. (*                                                                              *)
  25. (*  The first thing you have to do is create a socket with the "socket" call.   *)
  26. (*  After that, the operations depend on whether you want to act as a server    *)
  27. (*  or as a client.                                                             *)
  28. (*                                                                              *)
  29. (*  OPERATING AS A CLIENT                                                       *)
  30. (*     1. (Optional) Call "bind" to bind the socket with a local address.       *)
  31. (*     2. Call "connect", specifying which server you want to connect to.       *)
  32. (*     3. Use procedures "send" and "receive" to transfer data.                 *)
  33. (*     4. Use "soclose" to clean up at the end.                                 *)
  34. (*                                                                              *)
  35. (*  OPERATING AS A SERVER                                                       *)
  36. (*     1. (Compulsory) Call "bind" to bind the socket with a local address.     *)
  37. (*        You can usually afford to specify INADDR_ANY as the machine           *)
  38. (*        address, but you'd normally bind to a specific port number.           *)
  39. (*     2. Call "listen" to indicate your willingness to accept connections.     *)
  40. (*     3. Call "accept", getting a new socket (say ns) from the client.         *)
  41. (*     4. Use procedures "send" and "receive" to transfer data, using socket    *)
  42. (*        ns.  (Meanwhile, your original socket remains available to accept     *)
  43. (*        more connections, so you can continue with more "accept" operations   *)
  44. (*        in parallel with these data operations.  If so, you should of course  *)
  45. (*        be prepared to run multiple threads.)                                 *)
  46. (*     5. Use "soclose(ns)" to terminate the session with that particular       *)
  47. (*        client.                                                               *)
  48. (*     6. Use "soclose" on your original socket to clean up at the end.         *)
  49. (*                                                                              *)
  50. (********************************************************************************)
  51.  
  52. (********************************************************************************)
  53. (* This module is derived in part from the sockets.h file that comes            *)
  54. (* with OS/2 Warp 4, and that file carries the following copyright              *)
  55. (* notice:                                                                      *)
  56. (*                                                                              *)
  57. (* Copyright (c) 1982, 1985, 1986 Regents of the University of California.      *)
  58. (* All rights reserved.                                                         *)
  59. (*                                                                              *)
  60. (* Redistribution and use in source and binary forms are permitted              *)
  61. (* provided that this notice is preserved and that due credit is given          *)
  62. (* to the University of California at Berkeley. The name of the University      *)
  63. (* may not be used to endorse or promote products derived from this             *)
  64. (* software without specific prior written permission. This software            *)
  65. (* is provided ``as is'' without express or implied warranty.                   *)
  66. (*                                                                              *)
  67. (*      @(#)socket.h    7.2 (Berkeley) 12/30/87                                 *)
  68. (*                                                                              *)
  69. (********************************************************************************)
  70.  
  71. FROM SYSTEM IMPORT LOC, CARD8, ADDRESS;
  72.  
  73. FROM Internet IMPORT InternetSocketAddress;
  74.  
  75. TYPE Socket = CARDINAL;
  76. CONST NotASocket = MAX(CARDINAL);
  77.  
  78. TYPE
  79.     <* ENUMSIZE="2" *>
  80.     AddressFamily = (AF_UNSPEC, AF_UNIX, AF_INET, AF_IMPLINK, AF_PUP, AF_CHAOS, AF_NS,
  81.                      AF_NBS, AF_ECMA, AF_DATAKIT, AF_CCITT, AF_SNA, AF_DECnet, AF_DLI,
  82.                      AF_LAT, AF_HYLINK, AF_APPLETALK, AF_NETBIOS, AF_MAX);
  83.  
  84.         (*    AF_UNSPEC       0               unspecified                      *)
  85.         (*    AF_UNIX         1               local to host (pipes, portals)   *)
  86.         (*    AF_INET         2               internetwork: UDP, TCP, etc.     *)
  87.         (*    AF_IMPLINK      3               arpanet imp addresses            *)
  88.         (*    AF_PUP          4               pup protocols: e.g. BSP          *)
  89.         (*    AF_CHAOS        5               mit CHAOS protocols              *)
  90.         (*    AF_NS           6               XEROX NS protocols               *)
  91.         (*    AF_NBS          7               nbs protocols                    *)
  92.         (*    AF_ECMA         8               european computer manufacturers  *)
  93.         (*    AF_DATAKIT      9               datakit protocols                *)
  94.         (*    AF_CCITT        10              CCITT protocols, X.25 etc        *)
  95.         (*    AF_SNA          11              IBM SNA                          *)
  96.         (*    AF_DECnet       12              DECnet                           *)
  97.         (*    AF_DLI          13              Direct data link interface       *)
  98.         (*    AF_LAT          14              LAT                              *)
  99.         (*    AF_HYLINK       15              NSC Hyperchannel                 *)
  100.         (*    AF_APPLETALK    16              Apple Talk                       *)
  101.         (*    AF_NETBIOS      17              Netbios                          *)
  102.         (*    AF_MAX          18                                               *)
  103.  
  104.     <* ENUMSIZE = "1" *>
  105.     SocketType = (SOCK_INVALID, SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, SOCK_RDM, SOCK_SEQPACKET);
  106.  
  107.         (*    SOCK_INVALID    0               not used                   *)
  108.         (*    SOCK_STREAM     1               stream socket              *)
  109.         (*    SOCK_DGRAM      2               datagram socket            *)
  110.         (*    SOCK_RAW        3               raw-protocol interface     *)
  111.         (*    SOCK_RDM        4               reliably-delivered message *)
  112.         (*    SOCK_SEQPACKET  5               sequenced packet stream    *)
  113.  
  114.     SockAddr = RECORD
  115.                    CASE family: AddressFamily OF
  116.  
  117.                        AF_INET:  (* Internet address format *)
  118.  
  119.                                  in_addr: InternetSocketAddress;
  120.                        |
  121.                        ELSE      (* I'm not bothering in this version to *)
  122.                                  (* fully defined the other formats.     *)
  123.  
  124.                                  sa_data: ARRAY [0..13] OF CARD8;
  125.  
  126.                    END (*CASE*);
  127.                END (*RECORD*);
  128.  
  129. (********************************************************************************)
  130.  
  131. PROCEDURE ["SysCall"] sock_init (): INTEGER;
  132.  
  133.     (* Initialises the socket system.  Supposedly optional, since this call     *)
  134.     (* is implicitly made the first time you try to do something with sockets;  *)
  135.     (* but I think there are some situations where it's needed anyway.          *)
  136.  
  137.     (* Returns 0 for success, +1 (?) if network not available.  *)
  138.  
  139. PROCEDURE ["SysCall"] so_cancel (S: Socket): INTEGER;
  140.  
  141.     (* Forces a pending operation on socket S to return with a failure          *)
  142.     (* indication.  This can be used to interrupt another thread that is        *)
  143.     (* blocked while waiting for a socket response.                             *)
  144.     (* Note: won't cancel an accept() call, and I'm not sure what else it       *)
  145.     (* won't work for; but it does seem to work with select().                  *)
  146.  
  147. PROCEDURE ["SysCall"] soabort (S: Socket): INTEGER;
  148.  
  149.     (* Undocumented, but I'm hoping to work out what it does by trial and       *)
  150.     (* error.  It looks very much as if it's a variant of so_cancel.  It also   *)
  151.     (* appears that this function stopped working at approximately version      *)
  152.     (* 4.02o of the TCP/IP stack.                                               *)
  153.  
  154. PROCEDURE ["SysCall"] socket (domain: AddressFamily;  type: SocketType;
  155.                                                protocol: AddressFamily): Socket;
  156.  
  157.     (* Creates a socket descriptor.  This is normally the first thing an        *)
  158.     (* application must do, before using any of the other procedures in this    *)
  159.     (* module.  Choosing AF_UNSPEC as the protocol family gives you the default *)
  160.     (* protocol for the specified socket type.                                  *)
  161.  
  162. PROCEDURE ["SysCall"] bind (S: Socket;  VAR (*IN*) address: SockAddr;
  163.                                                 size: CARDINAL): BOOLEAN;
  164.  
  165.     (* Binds a socket to a port on the local machine.  This is a prerequisite   *)
  166.     (* for procedures listen or recvfrom, but is optional before connect.       *)
  167.     (* Socket S should have been created by a previous call to socket.          *)
  168.     (* The second parameter specifies the address, and the third parameter      *)
  169.     (* should be set to SIZE(address).  The details of the address record       *)
  170.     (* should be completely filled in before the call, with unused fields       *)
  171.     (* set to 0.  The return value is FALSE for a successful call, or TRUE for  *)
  172.     (* an error.  (Yes, I know that's back to front, but that's the way C       *)
  173.     (* programmers think.)  You can find the error code by a call to sock_errno *)
  174.     (* or psock_errno.                                                          *)
  175.  
  176. PROCEDURE ["SysCall"] sock_errno (): CARDINAL;
  177.  
  178.     (* Returns an error code for the last error (presumably the last one in     *)
  179.     (* this thread - there doesn't seem to be any way to specify which socket   *)
  180.     (* you're asking about).                                                    *)
  181.  
  182. PROCEDURE ["SysCall"] psock_errno (message: ARRAY OF CHAR);
  183.  
  184.     (* Like sock_errno, except that it doesn't return any value, instead it     *)
  185.     (* writes an error message to the standard error channel (usually the       *)
  186.     (* screen).  The input message is written as a prefix; if you don't need    *)
  187.     (* any prefix, just set message to the empty string.                        *)
  188.  
  189. PROCEDURE ["SysCall"] soclose (S: Socket): BOOLEAN;
  190.  
  191.     (* Closes the socket. *)
  192.  
  193. PROCEDURE ["SysCall"] listen (S: Socket;  queuesize: CARDINAL): BOOLEAN;
  194.  
  195.     (* Turns the socket into a passive socket that can accept connections.      *)
  196.     (* queuesize (legal range [0..5]) is the number of pending requests that    *)
  197.     (* can be queued.                                                           *)
  198.  
  199. PROCEDURE ["SysCall"] accept (S: Socket;  VAR (*OUT*) from: SockAddr;
  200.                                   VAR (*INOUT*) size: CARDINAL): Socket;
  201.  
  202.     (* Accepts a connection to socket S from a remote socket.  Parameter size   *)
  203.     (* should be set equal to SIZE(from) before the call.  The returned value   *)
  204.     (* identifies a newly created socket that can be used for further           *)
  205.     (* communication with the other node.  Meanwhile, socket S remains          *)
  206.     (* available for further "accept" calls if desired.                         *)
  207.  
  208. PROCEDURE ["SysCall"] connect (S: Socket;  VAR (*IN*) target: SockAddr;
  209.                                                  size: CARDINAL): BOOLEAN;
  210.  
  211.     (* Requests a connection from socket S to a remote socket.  Parameter size  *)
  212.     (* should be set equal to SIZE(target).                                     *)
  213.  
  214. PROCEDURE ["SysCall"] select (VAR (*INOUT*) group: ARRAY OF Socket;
  215.                           NumberOfReads, NumberOfWrites, NumberOfExcepts: CARDINAL;
  216.                           Timeout: CARDINAL): INTEGER;
  217.  
  218.     (* Waits until one or more of the sockets in "group" is ready.  The next    *)
  219.     (* three parameters specify how many sockets are in the array.  The sockets *)
  220.     (* to be checked for input come first, then the ones to be checked for      *)
  221.     (* output, then the ones to be checked for exceptional conditions.  The     *)
  222.     (* Timeout value is in milliseconds, except that a value of MAX(CARDINAL)   *)
  223.     (* means "wait forever".  If one or more of the sockets are ready when this *)
  224.     (* function returns, the return value is the number of ready sockets, and   *)
  225.     (* the entries in group corresponding to not-ready sockets are reset to     *)
  226.     (* the value NotASocket.  A return value of 0 indicates timeout.  A return  *)
  227.     (* value of -1 means error.                                                 *)
  228.  
  229. PROCEDURE ["SysCall"] getsockname (S: Socket;  VAR (*OUT*) myaddr: SockAddr;
  230.                                          VAR (*INOUT*) size: CARDINAL): BOOLEAN;
  231.  
  232.     (* The most typical use of connect is where we don't first do a bind, but   *)
  233.     (* instead allow connect to choose a port for us and do an implicit bind.   *)
  234.     (* The getsockname procedure lets us find out which port was chosen for     *)
  235.     (* us, by returning the corresponding SockAddr structure.                   *)
  236.  
  237. PROCEDURE ["SysCall"] getpeername (S: Socket;  VAR (*OUT*) peer: SockAddr;
  238.                                          VAR (*INOUT*) size: CARDINAL): BOOLEAN;
  239.  
  240.     (* Like getsockname, but returns the details for the machine at the         *)
  241.     (* other end.                                                               *)
  242.  
  243. PROCEDURE ["SysCall"] send (S: Socket;  VAR (*IN*) message: ARRAY OF LOC;
  244.                                  length: CARDINAL;  flags: CARDINAL): CARDINAL;
  245.  
  246.     (* Sends a message of "length" bytes on socket S, which must be a connected *)
  247.     (* socket.  Normally flags should be 0.  The returned value is the number   *)
  248.     (* of bytes sent, or MAX(CARDINAL) if there was an error.                   *)
  249.  
  250. PROCEDURE ["SysCall"] recv (S: Socket;  VAR (*OUT*) message: ARRAY OF LOC;
  251.                                  buffersize: CARDINAL;  flags: CARDINAL): CARDINAL;
  252.  
  253.     (* Receives a byte string on socket S, which must be a connected            *)
  254.     (* socket.  Normally flags should be 0.  The returned value is the number   *)
  255.     (* of bytes received, or MAX(CARDINAL) if there was an error.               *)
  256.  
  257. PROCEDURE ["SysCall"] gethostid (): CARDINAL;
  258.  
  259.     (* Returns the ID of the local host, using network byte order.  *)
  260.  
  261. PROCEDURE ["SysCall"] getsockopt (S: Socket;  level: CARDINAL;  Optionname: CARDINAL;
  262.                                    VAR (*OUT*) OptionValue: ARRAY OF LOC;
  263.                                    VAR (*INOUT*) OptionLength: CARDINAL): BOOLEAN;
  264.  
  265.     (* Gets socket options.  For details, see setsockopt comments below. *)
  266.  
  267. PROCEDURE ["SysCall"] setsockopt (S: Socket;  level: CARDINAL;  Optionname: CARDINAL;
  268.                    VAR OptionValue: ARRAY OF LOC;  OptionLength: CARDINAL): BOOLEAN;
  269.  
  270.     (* THE 1005H AND 1006H OPTIONS ARE APPARENTLY NOT WORKING.                  *)
  271.  
  272.     (* Sets socket options.  The possible parameter values don't follow a very  *)
  273.     (* logical pattern, so you have to read the associated documentation        *)
  274.     (* carefully.  The supported values for level are:                          *)
  275.     (*           0FFFFH         Options for socket level                        *)
  276.     (*           0              IP                                              *)
  277.     (*           1              NetBIOS                                         *)
  278.     (*           6              TCP                                             *)
  279.     (* (Confused?  Join the club.)  The Optionname parameter has a number of    *)
  280.     (* possible values, but the only ones that interest me (so far) are:        *)
  281.     (*          1005H           Send timeout                                    *)
  282.     (*          1006H           Receive timeout                                 *)
  283.     (* (These are both socket level options.)  On the other hand, my tests      *)
  284.     (* seem to suggest that these are the only two options that don't work.     *)
  285.     (* For these two options, OptionValue is a record of two CARDINALs, giving  *)
  286.     (* seconds and microseconds.  OptionLength is the size in bytes of this     *)
  287.     (* record.                                                                  *)
  288.     (* Result: TRUE for error, FALSE for OK.                                    *)
  289.  
  290. END Sockets.
  291.  
  292. (************************************************************************)
  293. (*   THE FOLLOWING HAS NOT YET BEEN TRANSLATED                          *)
  294. (************************************************************************)
  295.  
  296. #ifndef __SOCKET_32H
  297. #define __SOCKET_32H
  298.  
  299. #include <types.h>
  300.  
  301. /*
  302.  * Definitions related to sockets: types, address families, options.
  303.  */
  304.  
  305. /*
  306.  * Option flags per-socket.
  307.  */
  308. #define SO_DEBUG        0x0001          /* turn on debugging info recording */
  309. #define SO_ACCEPTCONN   0x0002          /* socket has had listen() */
  310. #define SO_REUSEADDR    0x0004          /* allow local address reuse */
  311. #define SO_KEEPALIVE    0x0008          /* keep connections alive */
  312. #define SO_DONTROUTE    0x0010          /* just use interface addresses */
  313. #define SO_BROADCAST    0x0020          /* permit sending of broadcast msgs */
  314. #define SO_USELOOPBACK  0x0040          /* bypass hardware when possible */
  315. #define SO_LINGER       0x0080          /* linger on close if data present */
  316. #define SO_OOBINLINE    0x0100          /* leave received OOB data in line */
  317. #define SO_L_BROADCAST  0x0200          /* limited broadcast sent on all IFs*/
  318. #define SO_RCV_SHUTDOWN 0x0400          /* set if shut down called for rcv */
  319. #define SO_SND_SHUTDOWN 0x0800          /* set if shutdown called for send */
  320.  
  321. /*
  322.  * Additional options, not kept in so_options.
  323.  */
  324. #define SO_SNDBUF       0x1001          /* send buffer size */
  325. #define SO_RCVBUF       0x1002          /* receive buffer size */
  326. #define SO_SNDLOWAT     0x1003          /* send low-water mark */
  327. #define SO_RCVLOWAT     0x1004          /* receive low-water mark */
  328. #define SO_ERROR        0x1007          /* get error status and clear */
  329. #define SO_TYPE         0x1008          /* get socket type */
  330. #define SO_OPTIONS      0x1010          /* get socket options */
  331.  
  332. /*
  333.  * Structure used for manipulating linger option.
  334.  */
  335. struct  linger {
  336.         int     l_onoff;                /* option on/off */
  337.         int     l_linger;               /* linger time */
  338. };
  339.  
  340. /*
  341.  * Level number for (get/set)sockopt() to apply to socket itself.
  342.  */
  343. #define SOL_SOCKET      0xffff          /* options for socket level */
  344.  
  345. /*
  346.  * Structure used by kernel to pass protocol
  347.  * information in raw sockets.
  348.  */
  349. struct sockproto {
  350.         unsigned short sp_family;              /* address family */
  351.         unsigned short sp_protocol;            /* protocol */
  352. };
  353.  
  354. /*
  355.  * Protocol families, same as address families for now.
  356.  */
  357. #define PF_UNSPEC       AF_UNSPEC
  358. #define PF_UNIX         AF_UNIX
  359. #define PF_INET         AF_INET
  360. #define PF_IMPLINK      AF_IMPLINK
  361. #define PF_PUP          AF_PUP
  362. #define PF_CHAOS        AF_CHAOS
  363. #define PF_NS           AF_NS
  364. #define PF_NBS          AF_NBS
  365. #define PF_ECMA         AF_ECMA
  366. #define PF_DATAKIT      AF_DATAKIT
  367. #define PF_CCITT        AF_CCITT
  368. #define PF_SNA          AF_SNA
  369. #define PF_DECnet       AF_DECnet
  370. #define PF_DLI          AF_DLI
  371. #define PF_LAT          AF_LAT
  372. #define PF_HYLINK       AF_HYLINK
  373. #define PF_APPLETALK    AF_APPLETALK
  374. #define PF_NETBIOS      AF_NB
  375. #define PF_NB           AF_NB
  376. #define PF_OS2          PF_UNIX
  377. #define PF_MAX          AF_MAX
  378.  
  379. /*
  380.  * Maximum queue length specifiable by listen.
  381.  */
  382. #define SOMAXCONN       5
  383.  
  384.  
  385. /*
  386.  * Message header for recvmsg and sendmsg calls.
  387.  */
  388. struct msghdr {
  389.         char * msg_name;               /* optional address */
  390.         int msg_namelen;            /* size of address */
  391.         struct  iovec *  msg_iov;         /* scatter/gather array */
  392.         int   msg_iovlen;             /* # elements in msg_iov */
  393.         char *  msg_accrights;          /* access rights sent/received */
  394.         int   msg_accrightslen;
  395. };
  396.  
  397. struct iovec {
  398.         char *  iov_base;
  399.         int     iov_len;
  400. };
  401.  
  402. struct uio {
  403.         struct  iovec  *uio_iov;
  404.         int     uio_iovcnt;
  405.         off_t   uio_offset;
  406.         int     uio_segflg;
  407.         unsigned int     uio_resid;
  408. };
  409. enum    uio_rw { UIO_READ, UIO_WRITE };
  410. #define FREAD  1
  411. #define FWRITE 2
  412.  
  413. #define MSG_OOB         0x1             /* process out-of-band data */
  414. #define MSG_PEEK        0x2             /* peek at incoming message */
  415. #define MSG_DONTROUTE   0x4             /* send without using routing tables */
  416. #define MSG_FULLREAD    0x8             /* send without using routing tables */
  417.  
  418. #define MSG_MAXIOVLEN   16
  419.  
  420. int _System ioctl(int, int, char *, int);
  421. int _System recvmsg( int, struct msghdr * , int);
  422. int _System recvfrom(int, char *, int, int, struct sockaddr *, int * );
  423. #ifndef BSD_SELECT
  424. #endif
  425. int _System sendmsg( int, struct msghdr * , int);
  426. int _System sendto( int, char *, int, int, struct sockaddr *, int);
  427. int _System readv(int, struct iovec * , int);
  428. int _System writev(int, struct iovec* , int);
  429. int _System shutdown(int, int);
  430. int _System getinetversion(char *);
  431.  
  432. #define MT_FREE         0       /* should be on free list */
  433. #define MT_DATA         1       /* dynamic (data) allocation */
  434. #define MT_HEADER       2       /* packet header */
  435. #define MT_SOCKET       3       /* socket structure */
  436. #define MT_PCB          4       /* protocol control block */
  437. #define MT_RTABLE       5       /* routing tables */
  438. #define MT_HTABLE       6       /* IMP host tables */
  439. #define MT_ATABLE       7       /* address resolution tables */
  440. #define MT_SONAME       8       /* socket name */
  441. #define MT_ZOMBIE       9       /* zombie proc status */
  442. #define MT_SOOPTS       10      /* socket options */
  443. #define MT_FTABLE       11      /* fragment reassembly header */
  444. #define MT_RIGHTS       12      /* access rights */
  445. #define MT_IFADDR       13      /* interface address */
  446.  
  447. #pragma pack(1)
  448. /* used to get mbuf statistics */
  449. struct mbstat {
  450.         unsigned short   m_mbufs;        /* mbufs obtained from page pool */
  451.         unsigned short   m_clusters;     /* clusters obtained from page pool */
  452.         unsigned short   m_clfree;       /* free clusters */
  453.         unsigned short   m_drops;        /* times failed to find space */
  454.         unsigned long    m_wait;         /* times waited for space */
  455.         unsigned short   m_mtypes[256];  /* type specific mbuf allocations */
  456. };
  457.  
  458. struct sostats {
  459.   short count;
  460.   short socketdata[9*MAXSOCKETS];
  461. };
  462.  
  463. #pragma pack()
  464.  
  465. #endif /* __SOCKET_32H */
  466.