home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / source / netnwscd.sit / tcpio.c < prev    next >
Text File  |  1990-10-15  |  4KB  |  197 lines

  1. /* tcp I/O */
  2.  
  3. #include    <MacTCPCommonTypes.h>
  4. #include    <TCPPB.h>
  5. #include     <GetMyIPAddr.h>
  6. #include    <AddressXlation.h>
  7.  
  8. /* prototypes */
  9. OSErr    tcp_open(char *host, int port);
  10. OSErr    tcp_create(short size);
  11. OSErr    tcp_write(char *buf, int cnt);
  12. OSErr    tcp_read(char *buf, int *cnt);
  13. OSErr    tcp_close(void);
  14. OSErr    tcp_release(void); 
  15. void    tcp_shutdown(void);
  16.  
  17. #define    TCP_BUFSIZ    (16*1024L)
  18. #define NULL        0L
  19.  
  20. short    driver = 0;
  21.  
  22. struct     hostInfo hp;
  23. Ptr IOBuffer;
  24. wdsEntry        wds[2];
  25. TCPiopb iopb;
  26.  
  27. /*
  28.  * needed for host resolution.
  29.  */
  30. pascal void dnrDone(struct hostInfo *hostInfoPtr, char *userDataPtr)
  31. {
  32.     *userDataPtr = 1;
  33. }
  34.  
  35. static
  36. delay(int n) {
  37.     long cur = TickCount();
  38.     
  39.     while (TickCount() - cur < n)
  40.         ;
  41. }
  42.  
  43.  
  44. /*
  45.  * Open - open a tcp connection
  46.  */
  47. OSErr
  48. tcp_open(char *host, int port)
  49. {
  50.     OSErr err;
  51.     Boolean dnrdoneflag;
  52.             
  53.     if (driver == 0) {        /* first open */
  54.         if ((err=OpenDriver("\p.ipp", &driver)) != noErr)
  55.             return (err);
  56.  
  57.         if ((err = OpenResolver(NULL)) != noErr)
  58.             return (err);
  59.     }
  60.     /* lookup hostname */
  61.     err = StrToAddr(host, &hp, &dnrDone, &dnrdoneflag);
  62.     if (err == cacheFault) {
  63.         while (!dnrdoneflag) {
  64.             /* SystemTask? WaitNextEvent ? */
  65.         }
  66.     }
  67.  
  68.     if ((err = err = tcp_create(TCP_BUFSIZ)) != noErr)
  69.         return (err);
  70.         
  71.     delay(20);    /* hack: wait for server */
  72.  
  73.     iopb.ioCompletion                = NULL;
  74.     iopb.csCode                     = TCPActiveOpen;
  75.     iopb.ioCRefNum                    = driver;
  76.     iopb.csParam.open.validityFlags = timeoutValue | timeoutAction;
  77.     iopb.csParam.open.ulpTimeoutValue = 60         /* seconds */;
  78.     iopb.csParam.open.ulpTimeoutAction = 1         /* 1:abort 0:report */;
  79.     iopb.csParam.open.commandTimeoutValue = 0;
  80.     iopb.csParam.open.remoteHost = hp.addr[0];
  81.     iopb.csParam.open.remotePort = port;
  82.     iopb.csParam.open.localHost = 0;
  83.     iopb.csParam.open.localPort = 0;
  84.     iopb.csParam.open.dontFrag = 0;
  85.     iopb.csParam.open.timeToLive = 0;
  86.     iopb.csParam.open.security = 0;
  87.     iopb.csParam.open.optionCnt = 0;
  88.     iopb.csParam.open.userDataPtr = "TCPActiveOpen";
  89.  
  90.     err = PBControl((ParmBlkPtr)&iopb, FALSE);
  91.     
  92.     atexit(tcp_shutdown);
  93.     
  94.     return (err);
  95. }
  96.  
  97. /*
  98.  * create a tcp stream
  99.  */
  100. OSErr
  101. tcp_create(short size)
  102. {
  103.     OSErr err;
  104.     
  105.     IOBuffer = NewPtr(size);        /* allocate a buffer for the connection */
  106.     if (IOBuffer == NULL) 
  107.         err = MemError();
  108.     
  109.     iopb.csCode                     = TCPCreate;
  110.     iopb.ioCompletion                = NULL;
  111.     iopb.ioCRefNum                    = driver;
  112.     iopb.csParam.create.notifyProc    = NULL;
  113.     iopb.csParam.create.rcvBuffLen    = size;
  114.     iopb.csParam.create.rcvBuff    = IOBuffer;
  115.     iopb.csParam.create.userDataPtr = "TCPCreate";
  116.         
  117.     return PBControl((ParmBlkPtr)&iopb, FALSE);
  118. }
  119.  
  120. OSErr
  121. tcp_write(char *buf, int cnt)
  122. {
  123.     int err;
  124.     
  125.     iopb.csCode                         = TCPSend;
  126.     iopb.ioCRefNum                    = driver;
  127.     iopb.csParam.send.ulpTimeoutValue    = 10;
  128.     iopb.csParam.send.ulpTimeoutAction = 1;
  129.  
  130.     /* build write-data structure */    
  131.     wds[0].length    = cnt;
  132.     wds[0].ptr        = buf;
  133.     wds[1].length    = 0;        
  134.     iopb.csParam.send.wdsPtr = (Ptr)&wds;
  135.     iopb.csParam.send.sendLength = 0;
  136.     iopb.csParam.send.userDataPtr = "TCPWrite";
  137.     
  138.     err = PBControl((ParmBlkPtr)&iopb, FALSE);
  139. }
  140.  
  141. OSErr
  142. tcp_read(char *buf, int *cnt)
  143. {
  144.     int err;
  145.     
  146.     iopb.csCode                     = TCPRcv;
  147.     iopb.ioCRefNum                    = driver;
  148.     iopb.csParam.receive.commandTimeoutValue = 10;             /*  */
  149.     iopb.csParam.receive.secondTimeStamp = 0;    /* must be zero */
  150.     iopb.csParam.receive.rcvBuff                = buf;
  151.     iopb.csParam.receive.rcvBuffLen            = *cnt;
  152.     iopb.csParam.receive.userDataPtr = "TCPRead";
  153.     
  154.     err = PBControl((ParmBlkPtr)&iopb, FALSE);
  155.     if (err == noErr)
  156.         *cnt = iopb.csParam.receive.rcvBuffLen;
  157.     return (err);
  158.     
  159. }
  160.  
  161. OSErr
  162. tcp_close()
  163. {
  164.     OSErr err;
  165.     
  166.     iopb.ioCompletion                    = NULL;
  167.     iopb.csCode                         = TCPClose;
  168.     iopb.ioCRefNum                    = driver;
  169.     iopb.csParam.close.validityFlags = timeoutValue | timeoutAction;
  170.     iopb.csParam.close.ulpTimeoutValue = 60     /* seconds */;
  171.     iopb.csParam.close.ulpTimeoutAction = 1     /* 1:abort 0:report */;
  172.     iopb.csParam.create.userDataPtr    = "TCPClose";
  173.     err = PBControl((ParmBlkPtr)&iopb, FALSE);
  174. }
  175.  
  176. /*
  177.  * close down a TCP stream (aborting a connection, if necessary)
  178.  */
  179. OSErr
  180. tcp_release() 
  181. {
  182.     OSErr err;
  183.     
  184.     iopb.ioCompletion                    = NULL;
  185.     iopb.csCode =             TCPRelease;
  186.     iopb.ioCRefNum                    = driver;
  187.     err = PBControl((ParmBlkPtr)&iopb, FALSE);
  188.     if (err == noErr)
  189.         DisposPtr(iopb.csParam.create.rcvBuff); /* there is no release pb */
  190.     return(err);
  191. }
  192.  
  193. void
  194. tcp_shutdown() {
  195.     tcp_release();
  196.     CloseResolver();
  197. }