Back to index

plt-scheme  4.2.1
macdnr.inc
Go to the documentation of this file.
00001 /*
00002     DNR.c - DNR library for MPW
00003 
00004     (c) Copyright 1988 by Apple Computer.  All rights reserved.
00005     PowerPC corrections by Eric Scouten, December 2, 1994.
00006 
00007 */
00008 
00009 #define MPW3.0
00010 
00011 #include <Carbon.h>
00012 #if 0
00013 #include <OSUtils.h>
00014 #include <Errors.h>
00015 #include <Files.h>
00016 #include <Resources.h>
00017 #include <Memory.h>
00018 #include <Traps.h>
00019 #include <Gestalt.h>
00020 #include <Folders.h>
00021 #include <MixedMode.h>
00022 #endif
00023 
00024 #define OPENRESOLVER    1L
00025 #define CLOSERESOLVER   2L
00026 #define STRTOADDR       3L
00027 #define ADDRTOSTR       4L
00028 #define ENUMCACHE       5L
00029 #define ADDRTONAME      6L
00030 #define HINFO           7L
00031 #define MXINFO          8L
00032 
00033 Handle              codeHndl = nil;
00034 UniversalProcPtr    dnr = nil;
00035 
00036 enum {
00037        NUM_ALT_ADDRS                      = 4
00038 };
00039 
00040 struct hostInfo {
00041        long                                             rtnCode;
00042        char                                             cname[255];
00043        SInt8                                            filler;                                          /* Filler for proper byte alignment        */
00044        unsigned long                             addr[NUM_ALT_ADDRS];
00045 };
00046 typedef struct hostInfo hostInfo;
00047 
00048 static pascal OSErr (*mzSysEnvirons)(int versionRequested, SysEnvRec *theworld);
00049 static pascal OSErr (*mzGetWDInfo)(short wdRefNum, short *vRefNum, long *dirID, long *procID);
00050 static pascal UniversalProcPtr (*mzNewRoutineDescriptor) (ProcPtr theProc, ProcInfoType theProcInfo, ISAType theISA);
00051 static long (*mzCallUniversalProc)(UniversalProcPtr theProcPtr, ProcInfoType theProcInfo, ...);
00052 
00053 #define NewRoutineDescriptor mzNewRoutineDescriptor 
00054 #define CallUniversalProc mzCallUniversalProc
00055 
00056 void GetSystemFolder(short *vRefNumP, long *dirIDP)
00057 {
00058     SysEnvRec info;
00059     long wdProcID;
00060 
00061     mzSysEnvirons(1, &info);
00062     if (mzGetWDInfo(info.sysVRefNum, vRefNumP, dirIDP, &wdProcID) != noErr) {
00063         *vRefNumP = 0;
00064         *dirIDP = 0;
00065         }
00066     }
00067 
00068 void GetCPanelFolder(short *vRefNumP, long *dirIDP)
00069 {
00070     Boolean hasFolderMgr = false;
00071     long feature;
00072 
00073     if (Gestalt(gestaltFindFolderAttr, &feature) == noErr)
00074         hasFolderMgr = true;
00075     if (!hasFolderMgr) {
00076         GetSystemFolder(vRefNumP, dirIDP);
00077         return;
00078         }
00079     else {
00080         if (FindFolder(kOnSystemDisk, kControlPanelFolderType, kDontCreateFolder, vRefNumP, dirIDP) != noErr) {
00081             *vRefNumP = 0;
00082             *dirIDP = 0;
00083             }
00084         }
00085     }
00086 
00087 /* SearchFolderForDNRP is called to search a folder for files that might
00088     contain the 'dnrp' resource */
00089 short SearchFolderForDNRP(long targetType, long targetCreator, short vRefNum, long dirID)
00090 {
00091     HParamBlockRec fi;
00092     Str255 filename;
00093     short refnum;
00094 
00095     fi.fileParam.ioCompletion = nil;
00096     fi.fileParam.ioNamePtr = filename;
00097     fi.fileParam.ioVRefNum = vRefNum;
00098     fi.fileParam.ioDirID = dirID;
00099     fi.fileParam.ioFDirIndex = 1;
00100 
00101     while (PBHGetFInfoSync(&fi) == noErr) {
00102         /* scan system folder for driver resource files of specific type & creator */
00103         if (fi.fileParam.ioFlFndrInfo.fdType == targetType &&
00104             fi.fileParam.ioFlFndrInfo.fdCreator == targetCreator) {
00105             /* found the MacTCP driver file? */
00106             refnum = HOpenResFile(vRefNum, dirID, filename, fsRdPerm);
00107             if (GetIndResource('dnrp', 1) == NULL)
00108                 CloseResFile(refnum);
00109             else
00110                 return refnum;
00111             }
00112         /* check next file in system folder */
00113         fi.fileParam.ioFDirIndex++;
00114         fi.fileParam.ioDirID = dirID;   /* PBHGetFInfo() clobbers ioDirID */
00115         }
00116     return(-1);
00117     }
00118 
00119 
00120 
00121 /* OpenOurRF is called to open the MacTCP driver resources */
00122 
00123 short OpenOurRF()
00124 {
00125     short refnum;
00126     short vRefNum;
00127     long dirID;
00128 
00129     /* first search Control Panels for MacTCP 1.1 */
00130     GetCPanelFolder(&vRefNum, &dirID);
00131     refnum = SearchFolderForDNRP('cdev', 'ztcp', vRefNum, dirID);
00132     if (refnum != -1) return(refnum);
00133 
00134     /* next search System Folder for MacTCP 1.0.x */
00135     GetSystemFolder(&vRefNum, &dirID);
00136     refnum = SearchFolderForDNRP('cdev', 'mtcp', vRefNum, dirID);
00137     if (refnum != -1) return(refnum);
00138 
00139     /* finally, search Control Panels for MacTCP 1.0.x */
00140     GetCPanelFolder(&vRefNum, &dirID);
00141     refnum = SearchFolderForDNRP('cdev', 'mtcp', vRefNum, dirID);
00142     if (refnum != -1) return(refnum);
00143 
00144     return -1;
00145     }
00146 
00147 
00148 
00149 
00150 typedef OSErr (*OpenResolverProcPtr)(long selector, char* fileName);
00151 
00152 enum {
00153     uppOpenResolverProcInfo = kCStackBased
00154          | RESULT_SIZE(SIZE_CODE(sizeof(long)))
00155          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(short)))
00156          | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(char *)))
00157 };
00158 
00159 #if USESROUTINEDESCRIPTORS
00160 typedef UniversalProcPtr OpenResolverUPP;
00161 
00162 #define NewOpenResolverProc(userRoutine)                        \
00163         (OpenResolverUPP) mzNewRoutineDescriptor(userRoutine, uppOpenResolverProcInfo, GetCurrentISA())
00164 #define CallOpenResolverProc(userRoutine, selector, filename)   \
00165         CallUniversalProc(userRoutine, uppOpenResolverProcInfo, selector, filename)
00166 #else
00167 typedef OpenResolverProcPtr OpenResolverUPP;
00168 
00169 #define NewOpenResolverProc(userRoutine)                    \
00170         (OpenResolverUPP)(userRoutine)
00171 #define CallOpenResolverProc(userRoutine, selector, filename)   \
00172         (*userRoutine)(selector, filename)
00173 #endif
00174 
00175 
00176 
00177 pascal OSErr OpenResolver(fileName)
00178 char *fileName;
00179 {
00180     short           refnum;
00181     OSErr           rc;
00182 
00183     if (dnr != nil)
00184         /* resolver already loaded in */
00185         return(noErr);
00186 
00187     /* open the MacTCP driver to get DNR resources. Search for it based on
00188        creator & type rather than simply file name */
00189     refnum = OpenOurRF();
00190 
00191     /* ignore failures since the resource may have been installed in the
00192        System file if running on a Mac 512Ke */
00193 
00194     /* load in the DNR resource package */
00195     codeHndl = GetIndResource('dnrp', 1);
00196     if (codeHndl == nil) {
00197         /* can't open DNR */
00198         return(ResError());
00199     }
00200 
00201     DetachResource(codeHndl);
00202     if (refnum != -1) {
00203         CloseResFile(refnum);
00204     }
00205 
00206     /* lock the DNR resource since it cannot be reloated while opened */
00207     HLock(codeHndl);
00208     dnr = (UniversalProcPtr) *codeHndl;
00209 
00210     /* call open resolver */
00211     rc = CallOpenResolverProc((OpenResolverUPP) dnr, OPENRESOLVER, fileName);
00212     if (rc != noErr) {
00213         /* problem with open resolver, flush it */
00214         HUnlock(codeHndl);
00215         DisposeHandle(codeHndl);
00216         dnr = nil;
00217     }
00218     return(rc);
00219 }
00220 
00221 
00222 
00223 typedef OSErr (*CloseResolverProcPtr)(long selector);
00224 
00225 enum {
00226     uppCloseResolverProcInfo = kCStackBased
00227          | RESULT_SIZE(SIZE_CODE(sizeof(long)))
00228          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(short)))
00229 };
00230 
00231 #if USESROUTINEDESCRIPTORS
00232 typedef UniversalProcPtr CloseResolverUPP;
00233 
00234 #define NewCloseResolverProc(userRoutine)                       \
00235         (CloseResolverUPP) mzNewRoutineDescriptor(userRoutine, uppCloseResolverProcInfo, GetCurrentISA())
00236 #define CallCloseResolverProc(userRoutine, selector)    \
00237         CallUniversalProc(userRoutine, uppCloseResolverProcInfo, selector)
00238 #else
00239 typedef CloseResolverProcPtr CloseResolverUPP;
00240 
00241 #define NewCloseResolverProc(userRoutine)                       \
00242         (CloseResolverUPP)(userRoutine)
00243 #define CallCloseResolverProc(userRoutine, selector)    \
00244         (*userRoutine)(selector)
00245 #endif
00246 
00247 
00248 
00249 pascal OSErr CloseResolver()
00250 {
00251     if (dnr == nil)
00252         /* resolver not loaded error */
00253         return(notOpenErr);
00254 
00255     /* call close resolver */
00256     CallCloseResolverProc((CloseResolverUPP) dnr, CLOSERESOLVER);
00257 
00258     /* release the DNR resource package */
00259     HUnlock(codeHndl);
00260     DisposeHandle(codeHndl);
00261     dnr = nil;
00262     return(noErr);
00263 }
00264 
00265 
00266 
00267 
00268 typedef OSErr (*StrToAddrProcPtr)(long selector, char* hostName, struct hostInfo* rtnStruct,
00269                                     long resultProc, char* userData);
00270 
00271 enum {
00272     uppStrToAddrProcInfo = kCStackBased
00273          | RESULT_SIZE(SIZE_CODE(sizeof(long)))
00274          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(short)))
00275          | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(char *)))
00276          | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(struct hostInfo *)))
00277          | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof(long)))
00278          | STACK_ROUTINE_PARAMETER(5, SIZE_CODE(sizeof(char *)))
00279 };
00280 
00281 #if USESROUTINEDESCRIPTORS
00282 typedef UniversalProcPtr StrToAddrUPP;
00283 
00284 #define NewStrToAddrProc(userRoutine)                       \
00285         (StrToAddrUPP) mzNewRoutineDescriptor(userRoutine, uppStrToAddrProcInfo, GetCurrentISA())
00286 #define CallStrToAddrProc(userRoutine, selector, hostName, rtnStruct, resultProc, userData) \
00287         CallUniversalProc(userRoutine, uppStrToAddrProcInfo, selector, hostName, rtnStruct, resultProc, userData)
00288 #else
00289 typedef StrToAddrProcPtr StrToAddrUPP;
00290 
00291 #define NewStrToAddrProc(userRoutine)                       \
00292         (StrToAddrUPP)(userRoutine)
00293 #define CallStrToAddrProc(userRoutine, selector, hostName, rtnStruct, resultProc, userData) \
00294         (*userRoutine)(selector, hostName, rtnStruct, resultProc, userData)
00295 #endif
00296 
00297 
00298 
00299 pascal OSErr StrToAddr(hostName, rtnStruct, resultproc, userDataPtr)
00300 char *hostName;
00301 struct hostInfo *rtnStruct;
00302 long resultproc;
00303 char *userDataPtr;
00304 {
00305     if (dnr == nil)
00306         /* resolver not loaded error */
00307         return(notOpenErr);
00308 
00309     return(CallStrToAddrProc((StrToAddrUPP) dnr, STRTOADDR, hostName, (void*) rtnStruct,
00310                             resultproc, userDataPtr));
00311 }
00312 
00313 
00314 typedef OSErr (*AddrToStrProcPtr)(long selector, long address, char* hostName);
00315 
00316 enum {
00317     uppAddrToStrProcInfo = kCStackBased
00318          | RESULT_SIZE(SIZE_CODE(sizeof(long)))
00319          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(short)))
00320          | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(unsigned long)))
00321          | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(char *)))
00322 };
00323 
00324 #if USESROUTINEDESCRIPTORS
00325 typedef UniversalProcPtr AddrToStrUPP;
00326 
00327 #define NewAddrToStrProc(userRoutine)                       \
00328         (AddrToStrUPP) mzNewRoutineDescriptor(userRoutine, uppAddrToStrProcInfo, GetCurrentISA())
00329 #define CallAddrToStrProc(userRoutine, selector, address, hostName) \
00330         CallUniversalProc(userRoutine, uppAddrToStrProcInfo, selector, address, hostName)
00331 #else
00332 typedef AddrToStrProcPtr AddrToStrUPP;
00333 
00334 #define NewAddrToStrProc(userRoutine)                       \
00335         (AddrToStrUPP)(userRoutine)
00336 #define CallAddrToStrProc(userRoutine, selector, address, hostName) \
00337         (*userRoutine)(selector, address, hostName)
00338 #endif
00339 
00340 
00341 pascal OSErr AddrToStr(addr, addrStr)
00342 unsigned long addr;
00343 char *addrStr;
00344 {
00345     if (dnr == nil)
00346         /* resolver not loaded error */
00347         return(notOpenErr);
00348 
00349     CallAddrToStrProc((AddrToStrUPP) dnr, ADDRTOSTR, addr, addrStr);
00350 
00351     return(noErr);
00352 }
00353 
00354 
00355 
00356 typedef OSErr (*EnumCacheProcPtr)(long selector, long result, char* userData);
00357 
00358 enum {
00359     uppEnumCacheProcInfo = kCStackBased
00360          | RESULT_SIZE(SIZE_CODE(sizeof(long)))
00361          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(short)))
00362          | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(long)))
00363          | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(char *)))
00364 };
00365 
00366 #if USESROUTINEDESCRIPTORS
00367 typedef UniversalProcPtr EnumCacheUPP;
00368 
00369 #define NewEnumCacheProc(userRoutine)                       \
00370         (EnumCacheUPP) mzNewRoutineDescriptor(userRoutine, uppEnumCacheProcInfo, GetCurrentISA())
00371 #define CallEnumCacheProc(userRoutine, selector, result, userData)  \
00372         CallUniversalProc(userRoutine, uppEnumCacheProcInfo, selector, result, userData)
00373 #else
00374 typedef EnumCacheProcPtr EnumCacheUPP;
00375 
00376 #define NewEnumCacheProc(userRoutine)                       \
00377         (EnumCacheUPP)(userRoutine)
00378 #define CallEnumCacheProc(userRoutine, selector, result, userData)  \
00379         (*userRoutine)(selector, result, userData)
00380 #endif
00381 
00382 
00383 
00384 pascal OSErr EnumCache(resultproc, userDataPtr)
00385 long resultproc;
00386 char *userDataPtr;
00387 {
00388     if (dnr == nil)
00389         /* resolver not loaded error */
00390         return(notOpenErr);
00391 
00392     return(CallEnumCacheProc((EnumCacheUPP) dnr, ENUMCACHE, resultproc, userDataPtr));
00393 }
00394 
00395 
00396 
00397 typedef OSErr (*AddrToNameProcPtr)(long selector, unsigned long addr, struct hostInfo* rtnStruct,
00398                                     long resultProc, char* userData);
00399 
00400 enum {
00401     uppAddrToNameProcInfo = kCStackBased
00402          | RESULT_SIZE(SIZE_CODE(sizeof(long)))
00403          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(short)))
00404          | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(unsigned long)))
00405          | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(struct hostInfo *)))
00406          | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof(long)))
00407          | STACK_ROUTINE_PARAMETER(5, SIZE_CODE(sizeof(char *)))
00408 
00409 };
00410 
00411 #if USESROUTINEDESCRIPTORS
00412 typedef UniversalProcPtr AddrToNameUPP;
00413 
00414 #define NewAddrToNameProc(userRoutine)                      \
00415         (AddrToNameUPP) mzNewRoutineDescriptor(userRoutine, uppAddrToNameProcInfo, GetCurrentISA())
00416 #define CallAddrToNameProc(userRoutine, selector, addr, rtnStruct, resultProc, userData)    \
00417         CallUniversalProc(userRoutine, uppAddrToNameProcInfo, selector, addr, rtnStruct, resultProc, userData)
00418 #else
00419 typedef AddrToNameProcPtr AddrToNameUPP;
00420 
00421 #define NewAddrToNameProc(userRoutine)                      \
00422         (AddrToNameUPP)(userRoutine)
00423 #define CallAddrToNameProc(userRoutine, selector, addr, rtnStruct, resultProc, userData)    \
00424         (*userRoutine)(selector, addr, rtnStruct, resultProc, userData)
00425 #endif
00426 
00427 
00428 
00429 pascal OSErr AddrToName(addr, rtnStruct, resultproc, userDataPtr)
00430 unsigned long addr;
00431 struct hostInfo *rtnStruct;
00432 long resultproc;
00433 char *userDataPtr;
00434 {
00435     if (dnr == nil)
00436         /* resolver not loaded error */
00437         return(notOpenErr);
00438 
00439     return(CallAddrToNameProc((AddrToNameUPP) dnr, ADDRTONAME, addr, rtnStruct, resultproc, userDataPtr));
00440 }
00441 
00442 typedef long ResultUPP;
00443 enum {
00444        uppResultProcInfo = kPascalStackBased
00445                | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(struct hostInfo*)))
00446                | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(Ptr))),
00447       };
00448 #define NewResultProc(proc) (ResultUPP)mzNewRoutineDescriptor((ProcPtr)proc, uppResultProcInfo, GetCurrentArchitecture())