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>
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((OpenResolverUPP)gDNRCodePtr, OPENRESOLVER,
270 /* problem with open resolver, flush it */
271 HUnlock(gDNRCodeHndl);
272 DisposeHandle(gDNRCodeHndl);
279 OSErr CloseResolver(void)
282 if (gDNRCodePtr == nil)
283 /* resolver not loaded error */
286 /* call close resolver */
287 // RRK modification 1/95 use CallCloseResolverProc define to call UPP
288 // (void) (*dnr)(CLOSERESOLVER);
290 CallCloseResolverProc((CloseResolverUPP)gDNRCodePtr, CLOSERESOLVER);
292 /* release the DNR resource package */
293 HUnlock(gDNRCodeHndl);
294 DisposeHandle(gDNRCodeHndl);
299 // RRK modification 1/95 declare parameter resultProc to be of type
300 // ResultProcUPP instead of a long
302 OSErr StrToAddr(char *hostName, struct hostInfo *rtnStruct,
303 ResultUPP resultproc, Ptr userDataPtr)
305 if (gDNRCodePtr == nil)
306 /* resolver not loaded error */
309 // RRK modification 1/95 use CallStrToAddrProc define to call UPP
310 // return((*dnr)(STRTOADDR, hostName, rtnStruct, resultproc, userDataPtr));
312 return (CallStrToAddrProc((StrToAddrUPP)gDNRCodePtr, STRTOADDR, hostName, rtnStruct, resultproc, userDataPtr));
315 OSErr AddrToStr(unsigned long addr, char *addrStr)
318 if (gDNRCodePtr == nil)
319 /* resolver not loaded error */
322 // RRK modification 1/95 use CallAddrToStrProc define to call UPP
323 // (*dnr)(ADDRTOSTR, addr, addrStr);
325 err = CallAddrToStrProc((AddrToStrUPP)gDNRCodePtr, ADDRTOSTR, addr, addrStr);
329 OSErr EnumCache(EnumResultUPP resultproc, Ptr userDataPtr)
332 if (gDNRCodePtr == nil)
333 /* resolver not loaded error */
336 // RRK modification 1/95 use CallEnumCacheProc define to call UPP
337 // return((*dnr)(ENUMCACHE, resultproc, userDataPtr));
339 return (CallEnumCacheProc((EnumCacheUPP)gDNRCodePtr, ENUMCACHE, resultproc, userDataPtr));
343 OSErr AddrToName(unsigned long addr, struct hostInfo *rtnStruct,
344 ResultUPP resultproc, Ptr userDataPtr)
346 if (gDNRCodePtr == nil)
347 /* resolver not loaded error */
350 // RRK modification 1/95 use CallAddrToNameProc define to call UPP
351 // return((*dnr)(ADDRTONAME, addr, rtnStruct, resultproc, userDataPtr));
353 return(CallAddrToNameProc((AddrToNameUPP)gDNRCodePtr, ADDRTONAME, addr, rtnStruct, resultproc, userDataPtr));
357 extern OSErr HInfo(char *hostName, struct returnRec *returnRecPtr,
358 ResultProc2UPP resultProc, Ptr userDataPtr)
360 if (gDNRCodePtr == nil)
361 /* resolver not loaded error */
364 // RRK modification 1/95 use CallHInfoProc define to call UPP
365 // return((*dnr)(HINFO, hostName, returnRecPtr, resultProc, userDataPtr));
367 return(CallHInfoProc((HInfoUPP)gDNRCodePtr, HXINFO, hostName, returnRecPtr, resultProc, userDataPtr));
371 extern OSErr MXInfo(char *hostName, struct returnRec *returnRecPtr,
372 ResultProc2UPP resultProc, Ptr userDataPtr)
374 if (gDNRCodePtr == nil)
375 /* resolver not loaded error */
378 // RRK modification 1/95 use CallHInfoProc define to call UPP
379 // return((*dnr)(MXINFO, hostName, returnRecPtr, resultProc, userDataPtr));
381 return(CallMXInfoProc((MXInfoUPP)gDNRCodePtr, MXINFO, hostName, returnRecPtr, resultProc, userDataPtr));
383 } /* removed ; (causes syntax err in Think C 5.0 */