home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 April: Mac OS SDK / Dev.CD Apr 97 SDK1.toast / Development Kits (Disc 1) / Open Transport / Sample Code / DTS Sample Code / Option Management Samples / EnableIPReuseAddrSample.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-19  |  2.2 KB  |  73 lines  |  [TEXT/CWIE]

  1. /*
  2.     File: EnableIPReuseAddrSample.c
  3.     By:        Rich Kubota
  4.             Developer Technical Support
  5.     
  6.     Purpose: Demonstrate the use of the OTOptionManagement call to tell a TCP
  7.             endpoint that the address may be reused immediately after closing
  8.             the endpoint.  This allows the endpoint to be reopened to the
  9.             same address.  Under OT, TCP has a 2-minute timeout on a binding 
  10.             after a connection has closed before the same port can be bound 
  11.             to again. This prevents data from stale connections from 
  12.             corrupting a new one. 
  13. */
  14. #include <OpenTransport.h>            // open transport files            
  15. #include <OpenTptInternet.h>
  16.  
  17. OSStatus DoNegotiateIPReuseAddrOption(EndpointRef ep, Boolean enableReuseIPMode);
  18.  
  19. /* Input: ep - endpointref on which to negotiate the option
  20.             enableReuseIPMode - desired option setting - true/false
  21.    Return: kOTNoError indicates that the option was successfully negotiated
  22.                OSStatus is an error if < 0, otherwise, the status field is
  23.                returned and is > 0.
  24. */
  25.  
  26.  
  27. OSStatus DoNegotiateIPReuseAddrOption(EndpointRef ep, Boolean enableReuseIPMode)
  28.  
  29. {
  30.     UInt8        buf[kOTFourByteOptionSize];    // define buffer for fourByte Option size
  31.     TOption*    opt;                        // option ptr to make items easier to access
  32.     TOptMgmt    req;
  33.     TOptMgmt    ret;
  34.     OSStatus    err;
  35.     Boolean        isAsync = false;
  36.     
  37.     opt = (TOption*)buf;                    // set option ptr to buffer
  38.     req.opt.buf    = buf;
  39.     req.opt.len    = sizeof(buf);
  40.     req.flags    = T_NEGOTIATE;                // negotiate for option
  41.  
  42.     ret.opt.buf = buf;
  43.     ret.opt.maxlen = kOTFourByteOptionSize;
  44.  
  45.     opt->level    = INET_IP;                    // dealing with an IP Level function
  46.     opt->name    = IP_REUSEADDR;
  47.     opt->len    = kOTFourByteOptionSize;
  48.     opt->status = 0;
  49.     *(UInt32*)opt->value = enableReuseIPMode;        // set the desired option level, true or false
  50.  
  51.     if (OTIsSynchronous(ep) == false)            // check whether ep sync or not
  52.     {
  53.         isAsync = true;                        // set flag if async
  54.         OTSetSynchronous(ep);                    // set endpoint to sync    
  55.     }
  56.                 
  57.     err = OTOptionManagement(ep, &req, &ret);
  58.     
  59.     if (isAsync == true)                    // restore ep state if necessary
  60.         OTSetAsynchronous(ep);
  61.  
  62.         // if no error then return the option status value
  63.     if (err == kOTNoError)
  64.     {
  65.         if (opt->status != T_SUCCESS)
  66.             err = opt->status;
  67.         else
  68.             err = kOTNoError;
  69.     }
  70.                 
  71.     return err;
  72. }
  73.