home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / GUSI 1.4.1 / GUSI / include / GUSIINET_P.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-01  |  5.3 KB  |  183 lines  |  [TEXT/MPS ]

  1. /*********************************************************************
  2. Project    :    GUSI                -    Grand Unified Socket Interface
  3. File        :    GUSIINET_P.h    -    Common definitions for TCP/IP Sockets
  4. Author    :    Matthias Neeracher
  5.  
  6.     This file was derived from the socket library by 
  7.     
  8.         Charlie Reiman    <creiman@ncsa.uiuc.edu> and
  9.         Tom Milligan    <milligan@madhaus.utcs.utoronto.ca>
  10.           
  11. Language    :    MPW C/C++
  12.  
  13. $Log: GUSIINET_P.h,v $
  14. Revision 1.2  1994/05/01  23:50:43  neeri
  15. INETScoketDomain::Open/CloseSocket()
  16.  
  17. Revision 1.1  1994/02/25  02:57:11  neeri
  18. Initial revision
  19.  
  20. Revision 0.7  1993/09/01  00:00:00  neeri
  21. Turn terminus into a short
  22.  
  23. Revision 0.6  1993/06/17  00:00:00  neeri
  24. UDPSocket::getsockname()
  25.  
  26. Revision 0.5  1993/01/31  00:00:00  neeri
  27. Support for inetd
  28.  
  29. Revision 0.4  1993/01/21  00:00:00  neeri
  30. Remove torecv
  31.  
  32. Revision 0.3  1993/01/08  00:00:00  neeri
  33. tcp_notify was setting the wrong stream to unconnected
  34.  
  35. Revision 0.2  1992/08/23  00:00:00  neeri
  36. Available()
  37.  
  38. Revision 0.1  1992/08/20  00:00:00  neeri
  39. First approach
  40.  
  41. *********************************************************************/
  42.  
  43. #include "GUSI_P.h"
  44.  
  45. #include <Devices.h>
  46. #include <MacTCPCommonTypes.h>
  47. #include <TCPPB.h>
  48. #include <UDPPB.h>
  49. #include <MiscIPPB.h>
  50. #include <AddressXlation.h>
  51. #include <GetMyIPAddr.h>
  52.  
  53. #define STREAM_BUFFER_SIZE    8192
  54. #define UDP_MAX_MSG            65507    /* MacTCP max legal udp message */
  55. #define TCP_MAX_MSG            65535    /* MacTCP max legal tcp message */
  56.  
  57. #define TCP_MAX_WDS        4        /* arbitrary number of wds to alloc in sock_tcp_send */
  58.  
  59. class INETSocket : public Socket    {                // That's what this file's all about
  60.     friend class INETSocketDomain;    
  61.  
  62. protected:
  63.     StreamPtr                stream;        /* stream pointer */
  64.     byte                        status;        /* Is file descriptor in use */
  65.     Boolean                    nonblocking;/* socket set for non-blocking I/O. */
  66.     char *                    recvBuf;        /* receive buffer */
  67.     int                        recvd;        /* amount received */
  68.     struct sockaddr_in    sa;            /* My address. */
  69.     struct sockaddr_in    peer;            /* Her address. */
  70.     byte                        sstate;        /* socket's connection state. */
  71.     int                        asyncerr;    /* Last async error to arrive.  zero if none. */
  72.     
  73.                     INETSocket();
  74.                     INETSocket(StreamPtr stream);
  75.                     
  76.     virtual         ~INETSocket();
  77.     
  78.     virtual u_long    Available();
  79. public:
  80.     virtual int        bind(void * name, int namelen);
  81.     virtual int     getsockname(void * name, int * namelen);
  82.     virtual int     getpeername(void * name, int * namelen);
  83.     virtual int        fcntl(unsigned int cmd, int arg);
  84.     virtual int        ioctl(unsigned int request, void *argp);
  85.     virtual int     shutdown(int how);
  86. };    
  87.  
  88. class AnnotatedPB;
  89.  
  90. class TCPSocket : public INETSocket    {    
  91.     friend class INETSocketDomain;
  92.     friend pascal void tcp_notify(StreamPtr, u_short, Ptr, u_short, struct ICMPReport *);
  93.     friend void tcp_connect_done(AnnotatedPB *);
  94.     friend void tcp_listen_done(AnnotatedPB *);
  95.     friend void tcp_recv_done(AnnotatedPB *);
  96.     friend void tcp_send_done(AnnotatedPB *);
  97.     friend pascal OSErr TCPPossession(AppleEvent*, AppleEvent*, long);
  98.  
  99. // Since an userDataPtr may not be changed during the lifetime of a stream, we need
  100. // this indirection. The class invariant *(this->self) == this holds.
  101.  
  102.     TCPSocket **    self;
  103.  
  104.                         TCPSocket();
  105.                         TCPSocket(StreamPtr stream);
  106.                         TCPSocket(TCPSocket * sock);
  107.                     
  108.     virtual             ~TCPSocket();
  109.     TCPiopb *        GetPB();
  110.     virtual u_long    Available();
  111. public:
  112.     virtual int     connect(void * address, int addrlen);
  113.     virtual int     listen(int qlen);
  114.     virtual Socket * accept(void * address, int * addrlen);
  115.     virtual int     recvfrom(void * buffer, int buflen, int flags, void * from, int * fromlen);
  116.     virtual int     sendto(void * buffer, int buflen, int flags, void * to, int tolen);
  117.     virtual int     select(Boolean * canRead, Boolean * canWrite, Boolean * exception);
  118. };    
  119.  
  120. class UDPSocket : public INETSocket    {    
  121.     friend class INETSocketDomain;    
  122.     friend void udp_read_ahead_done(AnnotatedPB *);
  123.     friend pascal OSErr UDPPossession(AppleEvent*, AppleEvent*, long);
  124.  
  125.                         UDPSocket();
  126.                         UDPSocket(StreamPtr stream);
  127.     virtual             ~UDPSocket();
  128.     UDPiopb *        GetPB();
  129.     virtual u_long    Available();
  130.     int                NewStream();
  131.     int                FlushReadAhead();
  132.     int                ReadAhead();
  133. public:
  134.     virtual int     getsockname(void * name, int * namelen);
  135.     virtual int     connect(void * address, int addrlen);
  136.     virtual int     recvfrom(void * buffer, int buflen, int flags, void * from, int * fromlen);
  137.     virtual int     sendto(void * buffer, int buflen, int flags, void * to, int tolen);
  138.     virtual int     select(Boolean * canRead, Boolean * canWrite, Boolean * exception);
  139. };    
  140.  
  141. class AnnotatedPB {
  142.     union {
  143.         TCPiopb        tcp;
  144.         UDPiopb        udp;
  145.     } pb;
  146.     INETSocket    *    sock;
  147. public:
  148.     void                SetOwner(INETSocket * s)        {    sock    =    s;                            }
  149.     INETSocket *    Owner()                                {    return sock;                        }
  150.     TCPiopb *        TCP()                                    {    return &pb.tcp;                    }
  151.     UDPiopb *        UDP()                                    {    return &pb.udp;                    }
  152.     Boolean            Busy()                                {    return pb.tcp.ioResult == 1;    }
  153. };
  154.  
  155. struct miniwds {
  156.     u_short    length;
  157.     char *     ptr;
  158.     short        terminus;    /* must be zero'd for use */
  159. };
  160.  
  161. class INETSocketDomain : public SocketDomain {
  162.     OSErr                driverState;
  163.     OSErr                resolverState;
  164.     short                drvrRefNum;
  165.     short                inetCount;                            /* # of existing sockets */
  166.     short             pbLast;                                /* last pb used */
  167.     AnnotatedPB *    pbList;                                /* The pb array */
  168. public:
  169.     INETSocketDomain();
  170.     
  171.     short                Driver();
  172.     OSErr                Resolver();
  173.     AnnotatedPB *    GetPB();
  174.     void                 OpenSocket();
  175.     void                CloseSocket();
  176.     
  177.     virtual Socket *     socket(int type, short protocol);
  178. };
  179.  
  180. extern INETSocketDomain    INETSockets;
  181.  
  182. int TCP_error(int MacTCPerr);
  183.