5 Contains: DNR library for MPW
7 Copyright: © 1989-1995 by Apple Computer, Inc., all rights reserved
9 Version: Technology: Networking
10 Package: Use with MacTCP 2.0.6 and the Universal
13 Change History (most recent first):
14 <3> 1/23/95 rrk implemented use of universal procptrs
15 Changed selector name HINFO to HXINFO
16 due to conflict of name in MacTCP header
17 Removed use of TrapAvailable and exchanged
18 for the TrapExists call.
19 Changed symbol codeHandle to gDNRCodeHndl
20 Changed symbol dnr to gDNRCodePtr
21 Further modifications by Steve Falkenburg, Apple MacDTS 8/91
22 Modifications by Jim Matthews, Dartmouth College, 5/91
40 #include <Resources.h>
51 #ifndef __GESTALTEQU__
52 #include <GestaltEqu.h>
60 #include <ToolUtils.h>
68 #ifndef __ADDRESSXLATION__
69 #include "AddressXlation.h"
72 // think C compatibility stuff
74 #ifndef _GestaltDispatch
75 #define _GestaltDispatch _Gestalt
79 /* RRK Modification 1/95 - commenting out the following defines as they are
80 defined in the DNRCalls.h header file
83 void GetSystemFolder(short *vRefNumP, long *dirIDP);
84 void GetCPanelFolder(short *vRefNumP, long *dirIDP);
85 short SearchFolderForDNRP(long targetType, long targetCreator, short vRefNum, long dirID);
86 short OpenOurRF(void);
87 short NumToolboxTraps(void);
88 TrapType GetTrapType(short theTrap);
89 Boolean TrapExists(short theTrap);
91 static Handle gDNRCodeHndl = nil;
92 static ProcPtr gDNRCodePtr = nil;
94 /* Check the bits of a trap number to determine its type. */
96 /* InitGraf is always implemented (trap $A86E). If the trap table is big
97 ** enough, trap $AA6E will always point to either Unimplemented or some other
98 ** trap, but will never be the same as InitGraf. Thus, you can check the size
99 ** of the trap table by asking if the address of trap $A86E is the same as
102 #pragma segment UtilMain
103 short NumToolboxTraps(void)
105 if (NGetTrapAddress(_InitGraf, ToolTrap) == NGetTrapAddress(0xAA6E, ToolTrap))
111 #pragma segment UtilMain
112 TrapType GetTrapType(short theTrap)
114 /* OS traps start with A0, Tool with A8 or AA. */
115 if ((theTrap & 0x0800) == 0) /* per D.A. */
121 Boolean TrapExists(short theTrap)
123 TrapType theTrapType;
125 theTrapType = GetTrapType(theTrap);
126 if ((theTrapType == ToolTrap) && ((theTrap &= 0x07FF) >= NumToolboxTraps()))
127 theTrap = _Unimplemented;
129 return(NGetTrapAddress(_Unimplemented, ToolTrap) != NGetTrapAddress(theTrap, theTrapType));
132 void GetSystemFolder(short *vRefNumP, long *dirIDP)
137 SysEnvirons(1, &info);
138 if (GetWDInfo(info.sysVRefNum, vRefNumP, dirIDP, &wdProcID) != noErr)
145 void GetCPanelFolder(short *vRefNumP, long *dirIDP)
147 Boolean hasFolderMgr = false;
150 if (TrapExists(_GestaltDispatch)) if (Gestalt(gestaltFindFolderAttr, &feature) == noErr) hasFolderMgr = true;
153 GetSystemFolder(vRefNumP, dirIDP);
158 if (FindFolder(kOnSystemDisk, kControlPanelFolderType, kDontCreateFolder, vRefNumP, dirIDP) != noErr)
166 /* SearchFolderForDNRP is called to search a folder for files that might
167 contain the 'dnrp' resource */
168 short SearchFolderForDNRP(long targetType, long targetCreator, short vRefNum, long dirID)
174 fi.fileParam.ioCompletion = nil;
175 fi.fileParam.ioNamePtr = filename;
176 fi.fileParam.ioVRefNum = vRefNum;
177 fi.fileParam.ioDirID = dirID;
178 fi.fileParam.ioFDirIndex = 1;
180 while (PBHGetFInfo(&fi, false) == noErr)
182 /* scan system folder for driver resource files of specific type & creator */
183 if (fi.fileParam.ioFlFndrInfo.fdType == targetType &&
184 fi.fileParam.ioFlFndrInfo.fdCreator == targetCreator)
186 /* found the MacTCP driver file? */
187 refnum = HOpenResFile(vRefNum, dirID, filename, fsRdPerm);
188 if (GetIndResource('dnrp', 1) == NULL)
189 CloseResFile(refnum);
193 /* check next file in system folder */
194 fi.fileParam.ioFDirIndex++;
195 fi.fileParam.ioDirID = dirID; /* PBHGetFInfo() clobbers ioDirID */
200 /* OpenOurRF is called to open the MacTCP driver resources */
202 short OpenOurRF(void)
208 /* first search Control Panels for MacTCP 1.1 */
209 GetCPanelFolder(&vRefNum, &dirID);
210 refnum = SearchFolderForDNRP('cdev', 'ztcp', vRefNum, dirID);
211 if (refnum != -1) return(refnum);
213 /* next search System Folder for MacTCP 1.0.x */
214 GetSystemFolder(&vRefNum, &dirID);
215 refnum = SearchFolderForDNRP('cdev', 'mtcp', vRefNum, dirID);
216 if (refnum != -1) return(refnum);
218 /* finally, search Control Panels for MacTCP 1.0.x */
219 GetCPanelFolder(&vRefNum, &dirID);
220 refnum = SearchFolderForDNRP('cdev', 'mtcp', vRefNum, dirID);
221 if (refnum != -1) return(refnum);
227 OSErr OpenResolver(char *fileName)
232 if (gDNRCodePtr != nil)
233 /* resolver already loaded in */
236 /* open the MacTCP driver to get DNR resources. Search for it based on
237 creator & type rather than simply file name */
238 refnum = OpenOurRF();
240 /* ignore failures since the resource may have been installed in the
241 System file if running on a Mac 512Ke */
243 /* load in the DNR resource package */
244 gDNRCodeHndl = GetIndResource('dnrp', 1);
245 if (gDNRCodeHndl == nil)
251 DetachResource(gDNRCodeHndl);
254 CloseResFile(refnum);
257 /* lock the DNR resource since it cannot be reloated while opened */
258 MoveHHi(gDNRCodeHndl);
261 gDNRCodePtr = (ProcPtr)*gDNRCodeHndl;
263 /* call open resolver */
264 // RRK modification 1/95 use CallOpenResolverProc define to call UPP
266 rc = CallOpenResolverProc(gDNRCodePtr, OPENRESOLVER, fileName);
269 /* problem with open resolver, flush it */
270 HUnlock(gDNRCodeHndl);
271 DisposeHandle(gDNRCodeHndl);
278 OSErr CloseResolver(void)
281 if (gDNRCodePtr == nil)
282 /* resolver not loaded error */
285 /* call close resolver */
286 // RRK modification 1/95 use CallCloseResolverProc define to call UPP
287 // (void) (*dnr)(CLOSERESOLVER);
289 CallCloseResolverProc(gDNRCodePtr, CLOSERESOLVER);
291 /* release the DNR resource package */
292 HUnlock(gDNRCodeHndl);
293 DisposeHandle(gDNRCodeHndl);
298 // RRK modification 1/95 declare parameter resultProc to be of type
299 // ResultProcUPP instead of a long
301 OSErr StrToAddr(char *hostName, struct hostInfo *rtnStruct,
302 ResultUPP resultproc, Ptr userDataPtr)
304 if (gDNRCodePtr == nil)
305 /* resolver not loaded error */
308 // RRK modification 1/95 use CallStrToAddrProc define to call UPP
309 // return((*dnr)(STRTOADDR, hostName, rtnStruct, resultproc, userDataPtr));
311 return (CallStrToAddrProc(gDNRCodePtr, STRTOADDR, hostName, rtnStruct, resultproc, userDataPtr));
314 OSErr AddrToStr(unsigned long addr, char *addrStr)
317 if (gDNRCodePtr == nil)
318 /* resolver not loaded error */
321 // RRK modification 1/95 use CallAddrToStrProc define to call UPP
322 // (*dnr)(ADDRTOSTR, addr, addrStr);
324 err = CallAddrToStrProc(gDNRCodePtr, ADDRTOSTR, addr, addrStr);
328 OSErr EnumCache(EnumResultUPP resultproc, Ptr userDataPtr)
331 if (gDNRCodePtr == nil)
332 /* resolver not loaded error */
335 // RRK modification 1/95 use CallEnumCacheProc define to call UPP
336 // return((*dnr)(ENUMCACHE, resultproc, userDataPtr));
338 return (CallEnumCacheProc(gDNRCodePtr, ENUMCACHE, resultproc, userDataPtr));
342 OSErr AddrToName(unsigned long addr, struct hostInfo *rtnStruct,
343 ResultUPP resultproc, Ptr userDataPtr)
345 if (gDNRCodePtr == nil)
346 /* resolver not loaded error */
349 // RRK modification 1/95 use CallAddrToNameProc define to call UPP
350 // return((*dnr)(ADDRTONAME, addr, rtnStruct, resultproc, userDataPtr));
352 return(CallAddrToNameProc(gDNRCodePtr, ADDRTONAME, addr, rtnStruct, resultproc, userDataPtr));
356 extern OSErr HInfo(char *hostName, struct returnRec *returnRecPtr,
357 ResultProc2UPP resultProc, Ptr userDataPtr)
359 if (gDNRCodePtr == nil)
360 /* resolver not loaded error */
363 // RRK modification 1/95 use CallHInfoProc define to call UPP
364 // return((*dnr)(HINFO, hostName, returnRecPtr, resultProc, userDataPtr));
366 return(CallHInfoProc(gDNRCodePtr, HXINFO, hostName, returnRecPtr, resultProc, userDataPtr));
370 extern OSErr MXInfo(char *hostName, struct returnRec *returnRecPtr,
371 ResultProc2UPP resultProc, Ptr userDataPtr)
373 if (gDNRCodePtr == nil)
374 /* resolver not loaded error */
377 // RRK modification 1/95 use CallHInfoProc define to call UPP
378 // return((*dnr)(MXINFO, hostName, returnRecPtr, resultProc, userDataPtr));
380 return(CallMXInfoProc(gDNRCodePtr, MXINFO, hostName, returnRecPtr, resultProc, userDataPtr));
382 } /* removed ; (causes syntax err in Think C 5.0 */