home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Entertainment / MacMud / Sockets / dnr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-17  |  4.6 KB  |  210 lines  |  [TEXT/MPS ]

  1. /*     DNR.c - DNR library for MPW
  2.  
  3.     (c) Copyright 1988 by Apple Computer.  All rights reserved
  4.     
  5. */
  6.  
  7. #include <Resources.h>
  8. #include <Files.h>
  9. #include <OSUtils.h>
  10. #include <Memory.h>
  11. #include <Errors.h>
  12. #include <Traps.h>
  13. #include <GestaltEqu.h>
  14. #include <Folders.h>
  15. #include <ToolUtils.h>
  16.  
  17. #define OPENRESOLVER    1
  18. #define CLOSERESOLVER    2
  19. #define STRTOADDR        3
  20. #define    ADDRTOSTR        4
  21. #define    ENUMCACHE        5
  22. #define ADDRTONAME        6
  23.  
  24. Handle codeHndl = nil;
  25.  
  26. typedef OSErr (*OSErrProcPtr)();
  27. OSErrProcPtr dnr = nil;
  28.  
  29. TrapType GetTrapType(theTrap)
  30. unsigned long theTrap;
  31. {
  32.     if (BitAnd(theTrap, 0x0800) > 0)
  33.         return(ToolTrap);
  34.     else
  35.         return(OSTrap);
  36. }
  37.  
  38. Boolean TrapAvailable1(trap)
  39. unsigned long trap;
  40. {
  41.     TrapType trapType = ToolTrap;
  42.     unsigned long numToolBoxTraps;
  43.  
  44.     if (NGetTrapAddress(_InitGraf, ToolTrap) == NGetTrapAddress(0xAA6E, ToolTrap))
  45.         numToolBoxTraps = 0x200;
  46.     else
  47.         numToolBoxTraps = 0x400;
  48.  
  49.     trapType = GetTrapType(trap);
  50.     if (trapType = ToolTrap) {
  51.         trap = BitAnd(trap, 0x07FF);
  52.         if (trap > numToolBoxTraps)
  53.             trap = _Unimplemented;
  54.     }
  55.     return(NGetTrapAddress(trap, trapType) != NGetTrapAddress(_Unimplemented, ToolTrap));
  56. }
  57.  
  58. short GetCPanelFolder()
  59. {
  60.     short vRefNum = 0;
  61.     long dirID = 0;
  62.     SysEnvRec info;
  63.     Boolean hasFolderMgr, hasGestalt = false;
  64.     long feature;
  65.     short wdRef;
  66.  
  67.     dirID = 0;
  68.     if (TrapAvailable1(_Gestalt)) if (Gestalt(gestaltFindFolderAttr, &feature) == noErr) hasFolderMgr = true;
  69.     if (!hasFolderMgr) {
  70.         SysEnvirons(1, &info);
  71.         return(info.sysVRefNum);
  72.     }
  73.     else {
  74.         if (FindFolder(kOnSystemDisk, kControlPanelFolderType, kDontCreateFolder, &vRefNum, &dirID) != noErr) return(0);
  75.         if (OpenWD(vRefNum, dirID, 'dnrp', &wdRef) == noErr)
  76.             return(wdRef);
  77.         else
  78.             return(0);
  79.     }
  80. }
  81.  
  82. /* OpenOurRF is called to open the MacTCP driver resources */
  83.  
  84. short OpenOurRF()
  85. {
  86.     ParamBlockRec fi;
  87.     Str255 filename;
  88.     short vRefNum;
  89.  
  90.     vRefNum = GetCPanelFolder();
  91.     fi.fileParam.ioCompletion = nil;
  92.     fi.fileParam.ioNamePtr = &filename;
  93.     fi.fileParam.ioVRefNum = vRefNum;
  94.     fi.fileParam.ioFDirIndex = 1;
  95.  
  96.     while (PBGetFInfo(&fi, false) == noErr) {
  97.         /* scan system folder for driver resource files of specific type & creator */
  98.         if (fi.fileParam.ioFlFndrInfo.fdType == 'cdev' &&
  99.             fi.fileParam.ioFlFndrInfo.fdCreator == 'ztcp') {
  100.             /* found the MacTCP driver file */
  101.             return(OpenRFPerm(&filename, vRefNum, fsRdPerm));
  102.         }
  103.         /* check next file in system folder */
  104.         fi.fileParam.ioFDirIndex++;
  105.     }
  106.     return(-1);
  107. }    
  108.  
  109. OSErr OpenResolver(fileName)
  110. char *fileName;
  111. {
  112.     short refnum;
  113.     OSErr rc;
  114.     long feature = 0;
  115.     Boolean hasTimeMgr = false;
  116.  
  117.     if (dnr != nil)
  118.         /* resolver already loaded in */
  119.         return(noErr);
  120.  
  121.     /* open the MacTCP driver to get DNR resources. Search for it based on
  122.        creator & type rather than simply file name */    
  123.     refnum = OpenOurRF();
  124.  
  125.     /* ignore failures since the resource may have been installed in the 
  126.        System file if running on a Mac 512Ke */
  127.  
  128.     /* load in the DNR resource package */
  129.     codeHndl = GetIndResource('dnrp', 1);
  130.  
  131.     if (codeHndl == nil) {
  132.         /* can't open DNR */
  133.         return(ResError());
  134.     }
  135.  
  136.     DetachResource(codeHndl);
  137.     if (refnum != -1) {
  138.         CloseWD(refnum);
  139.         CloseResFile(refnum);
  140.     }
  141.  
  142.     /* lock the DNR resource since it cannot be reloated while opened */
  143.     HLock(codeHndl);
  144.     dnr = (OSErrProcPtr) *codeHndl;
  145.  
  146.     /* call open resolver */
  147.     rc = (*dnr)(OPENRESOLVER, fileName);
  148.     if (rc != noErr) {
  149.         /* problem with open resolver, flush it */
  150.         HUnlock(codeHndl);
  151.         DisposHandle(codeHndl);
  152.         dnr = nil;
  153.     }
  154.  
  155.     /* check is the extended time manager is available */
  156.     if (TrapAvailable1(_Gestalt)) if (Gestalt(gestaltTimeMgrVersion, &feature) == noErr) hasTimeMgr = true;
  157.     if (hasTimeMgr) {
  158.         if ((feature == gestaltExtendedTimeMgr) || (feature == gestaltRevisedTimeMgr))
  159.             rc == noErr;
  160.         else rc == 1;
  161.     }
  162.  
  163.     return(rc);
  164. }
  165.  
  166. OSErr CloseResolver()
  167. {
  168.     if (dnr == nil)
  169.         /* resolver not loaded error */
  170.         return(notOpenErr);
  171.  
  172.     /* call close resolver */
  173.     (void) (*dnr)(CLOSERESOLVER);
  174.  
  175.     /* release the DNR resource package */
  176.     HUnlock(codeHndl);
  177.     DisposHandle(codeHndl);
  178.     dnr = nil;
  179.     return(noErr);
  180. }
  181.  
  182. OSErr StrToAddr(hostName, rtnStruct, resultproc, userDataPtr)
  183. char *hostName;
  184. struct hostInfo *rtnStruct;
  185. long resultproc;
  186. char *userDataPtr;
  187. {
  188.     return((*dnr)(STRTOADDR, hostName, rtnStruct, resultproc, userDataPtr));
  189. }
  190.  
  191. void AddrToStr(addr, addrStr)
  192. unsigned long addr;
  193. char *addrStr;                                    
  194. {
  195.     (*dnr)(ADDRTOSTR, addr, addrStr);
  196. }
  197.  
  198. OSErr AddrToName(addr, rtnStruct, resultproc, userDataPtr)
  199. unsigned long addr;
  200. struct hostInfo *rtnStruct;
  201. long resultproc;
  202. char *userDataPtr;                                    
  203. {
  204.     if (dnr == nil)
  205.         /* resolver not loaded error */
  206.         return(notOpenErr);
  207.  
  208.     return((*dnr)(ADDRTONAME, addr, rtnStruct, resultproc, userDataPtr));
  209. }
  210.