home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Think Class Libraries / MacTCP class library / MacTCP class sources / CDNR.c next >
Encoding:
C/C++ Source or Header  |  1994-11-30  |  3.9 KB  |  180 lines  |  [TEXT/KAHL]

  1. /*
  2.  
  3.   CDNR.c
  4.   Superclass:    CMacTCPDriver
  5.  
  6.      Dynamic name resolution implementation.
  7.  
  8.   Copyright © NCSA, University of Illinois;    June 2, 1992
  9.   Igor Livshits
  10.   
  11.   This code may be used, modified, and distributed free of charge and obligation.
  12.   
  13. */
  14.  
  15. #ifdef THINK_C
  16.     #pragma    options(!require_protos)
  17. #endif
  18.  
  19. #include    "CDNR.h"
  20.  
  21.  
  22. /*=====================*/
  23. //    NOTE:    The following code assumes only ONE instance of this object at any given time!
  24. /*===---------------===*/
  25.  
  26. void    CDNR::IDNR(char*    fileName)
  27. Begin
  28.     Handle                    theCode;
  29.     CIsleResFile*        theFile;
  30.     SysEnvRec                info;                            //    System information
  31.     
  32.     resolver=        Null;
  33.     codeHandle= Null;
  34.     fileMacTCP= Null;
  35.     
  36.     TRY
  37.     {
  38.         if (gSystem.systemVersion >= kSystem603)
  39.         {
  40.             SysEnvirons(kBasicVersion, &info);//    Get system information
  41.             theFile= new CIsleResFile;        //    We are only interested in the resource fork
  42.             fileMacTCP= theFile;
  43.             fileMacTCP->IResFile();
  44.             fileMacTCP->FindCreatorType(info.sysVRefNum, kDefault, kMacTCP, kControlPanel, True);
  45.             fileMacTCP->Open(fsRdPerm);        //    Open the driver
  46.         }                                                                //    Otherwise, it may be a Mac 512KE
  47.         
  48.         theCode= GetIndResource(kNameResolver, kNameResolverID);
  49.         codeHandle= theCode;
  50.         FailNILRes(codeHandle);
  51.         
  52.         DetachResource(codeHandle);
  53.         FailResError();
  54.         ForgetObject(fileMacTCP);                //    We are done with this file
  55.             
  56.         HLock(codeHandle);                            //    Lock the DNR resource since it cannot be reloaded while opened
  57.         FailMemError();
  58.         resolver= (OSErrProcPtr) *codeHandle;
  59.         
  60.         FailOSErr( (*resolver)(kOpen, fileName) );
  61.     }
  62.     CATCH
  63.     {
  64.         ErrorAlert(gLastError, SpecifyMsg(kDNRError, kNoResolver));
  65.         
  66.         resolver= Null;
  67.         ForgetObject(fileMacTCP);
  68.         if (codeHandle)
  69.         {
  70.             HUnlock(codeHandle);
  71.             ForgetHandle(codeHandle);
  72.         }
  73.     }
  74.     ENDTRY;
  75. End
  76.  
  77. /*===---------------===*/
  78.  
  79. void    CDNR::Dispose(void)
  80. Begin
  81.     if (resolver)
  82.     {
  83.         FailOSErr( (*resolver)(kClose) );                //    Close the name resolver
  84.  
  85.         HUnlock(codeHandle);
  86.         FailMemError();
  87.         ForgetHandle(codeHandle);
  88.         resolver= Null;
  89.     }
  90.     
  91.     inherited::Dispose();
  92. End
  93.  
  94. /*===---------------===*/
  95.  
  96. OSErr    CDNR::StringToAddress(char* hostName, hostInfoPtr result, ProcPtr resultProcedure, char *done)
  97. Begin
  98.     OSErr    error;
  99.  
  100.     if (!resultProcedure)                        //    Assign the default result procedure
  101.         resultProcedure= (ProcPtr)ResultProc;
  102.  
  103.     if (resolver)
  104.     {
  105.         error= (*resolver)(kStringToAddress, hostName, result, resultProcedure, done);
  106.         if (error == kCacheFault)
  107.         {
  108.             while (!(*done))
  109.                 ;                                                    //    Wait for the domain name server to fetch the address
  110.             error= noErr;
  111.         }
  112.     }
  113.     
  114.     return error;
  115. End
  116.  
  117. /*===---------------===*/
  118.  
  119. void    CDNR::AddressToString(ip_addr address, char* addressStr)
  120. Begin
  121.     if (resolver)
  122.         FailOSErr( (*resolver)(kAddressToString, address, addressStr) );
  123. End
  124.  
  125. /*===---------------===*/
  126.  
  127. void    CDNR::EnumerateCache(ProcPtr resultProcedure, char* done)
  128. Begin
  129.     if (!resultProcedure)                        //    Assign the default result procedure
  130.         resultProcedure= (ProcPtr)EnumResultProc;
  131.  
  132.     if (resolver)
  133.         FailOSErr( (*resolver)(kEnumerateCache, EnumResultProc, done) );
  134. End
  135.  
  136. /*===---------------===*/
  137.  
  138. OSErr    CDNR::AddressToName(ip_addr address, hostInfoPtr result, ProcPtr resultProcedure, char *done)
  139. Begin
  140.     OSErr    error;
  141.  
  142.     if (!resultProcedure)                        //    Assign the default result procedure
  143.         resultProcedure= (ProcPtr)ResultProc;
  144.  
  145.     if (resolver)
  146.     {
  147.         error= (*resolver)(kAddressToName, address, result, resultProcedure, done);
  148.         if (error == kCacheFault)
  149.         {
  150.             while (!(*done))
  151.                 ;                                                    //    Wait for the domain name server to fetch the address
  152.             error= noErr;
  153.         }
  154.     }
  155.     
  156.     return error;
  157. End
  158.  
  159. /*=====================*/
  160. /*===---------------===*/
  161. /*=====================*/
  162.  
  163. pascal void ResultProc(struct hostInfo* hInfoPtr, char* done)
  164. Begin
  165. #pragma unused (hInfoPtr)
  166.  
  167.     *done= True;
  168. End
  169.  
  170. /*===---------------===*/
  171.  
  172. pascal void EnumResultProc(struct cacheEntryRecord* cacheEntryPtr, char* done)
  173. Begin
  174. #pragma unused (cacheEntryPtr)
  175.  
  176.     *done= True;
  177. End
  178.  
  179. /*===---------------===*/
  180. /*=====================*/