blob: 5474b73cedaf0b06ec814d6469e5efd146c52e55 [file] [log] [blame]
Jack Jansenedf58551994-12-14 13:36:34 +00001/* DNR.c - DNR library for MPW
2
3 (c) Copyright 1988 by Apple Computer. All rights reserved
4
5 Modifications by Jim Matthews, Dartmouth College, 5/91
6 Again modified for use with python by Jack Jansen, CWI, October 1994.
7
8*/
9
10#include <Traps.h>
11#include <OSUtils.h>
12#include <Errors.h>
13#include <Files.h>
14#include <Resources.h>
15#include <Memory.h>
16#include <Traps.h>
17#include <GestaltEqu.h>
18#include <Folders.h>
19#include <ToolUtils.h>
20#include <MacTCPCommonTypes.h>
21#include "AddressXlation.h"
22
23TrapType GetTrapType(unsigned long theTrap);
24Boolean TrapAvailable(unsigned long trap);
25void GetSystemFolder(short *vRefNumP, long *dirIDP);
26void GetCPanelFolder(short *vRefNumP, long *dirIDP);
27short SearchFolderForDNRP(long targetType, long targetCreator, short vRefNum, long dirID);
28short OpenOurRF(void);
29
30#define OPENRESOLVER 1L
31#define CLOSERESOLVER 2L
32#define STRTOADDR 3L
33#define ADDRTOSTR 4L
34#define ENUMCACHE 5L
35#define ADDRTONAME 6L
36#define HINFO 7L
37#define MXINFO 8L
38
39Handle codeHndl = nil;
40
41OSErrProcPtr dnr = nil;
42
43TrapType GetTrapType(theTrap)
44unsigned long theTrap;
45{
46 if (BitAnd(theTrap, 0x0800) > 0)
47 return(ToolTrap);
48 else
49 return(OSTrap);
50 }
51
52Boolean TrapAvailable(trap)
53unsigned long trap;
54{
55TrapType trapType = ToolTrap;
56unsigned long numToolBoxTraps;
57
58 if (NGetTrapAddress(_InitGraf, ToolTrap) == NGetTrapAddress(0xAA6E, ToolTrap))
59 numToolBoxTraps = 0x200;
60 else
61 numToolBoxTraps = 0x400;
62
63 trapType = GetTrapType(trap);
64 if (trapType == ToolTrap) {
65 trap = BitAnd(trap, 0x07FF);
66 if (trap >= numToolBoxTraps)
67 trap = _Unimplemented;
68 }
69 return(NGetTrapAddress(trap, trapType) != NGetTrapAddress(_Unimplemented, ToolTrap));
70
71}
72
73void GetSystemFolder(short *vRefNumP, long *dirIDP)
74{
75 SysEnvRec info;
76 long wdProcID;
77
78 SysEnvirons(1, &info);
79 if (GetWDInfo(info.sysVRefNum, vRefNumP, dirIDP, &wdProcID) != noErr) {
80 *vRefNumP = 0;
81 *dirIDP = 0;
82 }
83 }
84
85void GetCPanelFolder(short *vRefNumP, long *dirIDP)
86{
87 Boolean hasFolderMgr = false;
88 long feature;
89
90 if (Gestalt(gestaltFindFolderAttr, &feature) == noErr) hasFolderMgr = true;
91 if (!hasFolderMgr) {
92 GetSystemFolder(vRefNumP, dirIDP);
93 return;
94 }
95 else {
96 if (FindFolder(kOnSystemDisk, kControlPanelFolderType, kDontCreateFolder, vRefNumP, dirIDP) != noErr) {
97 *vRefNumP = 0;
98 *dirIDP = 0;
99 }
100 }
101 }
102
103/* SearchFolderForDNRP is called to search a folder for files that might
104 contain the 'dnrp' resource */
105short SearchFolderForDNRP(long targetType, long targetCreator, short vRefNum, long dirID)
106{
107 HParamBlockRec fi;
108 Str255 filename;
109 short refnum;
110
111 fi.fileParam.ioCompletion = nil;
112 fi.fileParam.ioNamePtr = filename;
113 fi.fileParam.ioVRefNum = vRefNum;
114 fi.fileParam.ioDirID = dirID;
115 fi.fileParam.ioFDirIndex = 1;
116
117 while (PBHGetFInfo(&fi, false) == noErr) {
118 /* scan system folder for driver resource files of specific type & creator */
119 if (fi.fileParam.ioFlFndrInfo.fdType == targetType &&
120 fi.fileParam.ioFlFndrInfo.fdCreator == targetCreator) {
121 /* found the MacTCP driver file? */
122 refnum = HOpenResFile(vRefNum, dirID, filename, fsRdPerm);
123 if (GetIndResource('dnrp', 1) == NULL)
124 CloseResFile(refnum);
125 else
126 return refnum;
127 }
128 /* check next file in system folder */
129 fi.fileParam.ioFDirIndex++;
130 fi.fileParam.ioDirID = dirID; /* PBHGetFInfo() clobbers ioDirID */
131 }
132 return(-1);
133 }
134
135/* OpenOurRF is called to open the MacTCP driver resources */
136
137short OpenOurRF()
138{
139 short refnum;
140 short vRefNum;
141 long dirID;
142
143 /* first search Control Panels for MacTCP 1.1 */
144 GetCPanelFolder(&vRefNum, &dirID);
145 refnum = SearchFolderForDNRP('cdev', 'ztcp', vRefNum, dirID);
146 if (refnum != -1) return(refnum);
147
148 /* next search System Folder for MacTCP 1.0.x */
149 GetSystemFolder(&vRefNum, &dirID);
150 refnum = SearchFolderForDNRP('cdev', 'mtcp', vRefNum, dirID);
151 if (refnum != -1) return(refnum);
152
153 /* finally, search Control Panels for MacTCP 1.0.x */
154 GetCPanelFolder(&vRefNum, &dirID);
155 refnum = SearchFolderForDNRP('cdev', 'mtcp', vRefNum, dirID);
156 if (refnum != -1) return(refnum);
157
158 return -1;
159 }
160
161
162OSErr OpenResolver(fileName)
163char *fileName;
164{
165 short refnum;
166 OSErr rc;
167
168 if (dnr != nil)
169 /* resolver already loaded in */
170 return(noErr);
171
172 /* open the MacTCP driver to get DNR resources. Search for it based on
173 creator & type rather than simply file name */
174 refnum = OpenOurRF();
175
176 /* ignore failures since the resource may have been installed in the
177 System file if running on a Mac 512Ke */
178
179 /* load in the DNR resource package */
180 codeHndl = GetIndResource('dnrp', 1);
181 if (codeHndl == nil) {
182 /* can't open DNR */
183 return(ResError());
184 }
185
186 DetachResource(codeHndl);
187 if (refnum != -1) {
188 CloseWD(refnum);
189 CloseResFile(refnum);
190 }
191
192 /* lock the DNR resource since it cannot be reloated while opened */
193 HLock(codeHndl);
194 dnr = (OSErrProcPtr) *codeHndl;
195
196 /* call open resolver */
197 rc = (*dnr)(OPENRESOLVER, fileName);
198 if (rc != noErr) {
199 /* problem with open resolver, flush it */
200 HUnlock(codeHndl);
201 DisposHandle(codeHndl);
202 dnr = nil;
203 }
204 return(rc);
205 }
206
207
208OSErr CloseResolver()
209{
210 if (dnr == nil)
211 /* resolver not loaded error */
212 return(notOpenErr);
213
214 /* call close resolver */
215 (void) (*dnr)(CLOSERESOLVER);
216
217 /* release the DNR resource package */
218 HUnlock(codeHndl);
219 DisposHandle(codeHndl);
220 dnr = nil;
221 return(noErr);
222 }
223
224OSErr StrToAddr(hostName, rtnStruct, resultproc, userDataPtr)
225char *hostName;
226struct hostInfo *rtnStruct;
227ResultProcPtr resultproc;
228char *userDataPtr;
229{
230 if (dnr == nil)
231 /* resolver not loaded error */
232 return(notOpenErr);
233
234 return((*dnr)(STRTOADDR, hostName, rtnStruct, resultproc, userDataPtr));
235 }
236
237OSErr AddrToStr(addr, addrStr)
238unsigned long addr;
239char *addrStr;
240{
241 if (dnr == nil)
242 /* resolver not loaded error */
243 return(notOpenErr);
244
245 (*dnr)(ADDRTOSTR, addr, addrStr);
246 return(noErr);
247 }
248
249OSErr EnumCache(resultproc, userDataPtr)
250EnumResultProcPtr resultproc;
251char *userDataPtr;
252{
253 if (dnr == nil)
254 /* resolver not loaded error */
255 return(notOpenErr);
256
257 return((*dnr)(ENUMCACHE, resultproc, userDataPtr));
258 }
259
260
261OSErr AddrToName(addr, rtnStruct, resultproc, userDataPtr)
262unsigned long addr;
263struct hostInfo *rtnStruct;
264ResultProcPtr resultproc;
265char *userDataPtr;
266{
267 if (dnr == nil)
268 /* resolver not loaded error */
269 return(notOpenErr);
270
271 return((*dnr)(ADDRTONAME, addr, rtnStruct, resultproc, userDataPtr));
272 }
273
274
275extern OSErr HInfo(hostName, returnRecPtr, resultProc, userDataPtr)
276char *hostName;
277struct returnRec *returnRecPtr;
278ResultProc2Ptr resultProc;
279char *userDataPtr;
280{
281 if (dnr == nil)
282 /* resolver not loaded error */
283 return(notOpenErr);
284
285 return((*dnr)(HINFO, hostName, returnRecPtr, resultProc, userDataPtr));
286
287 }
288
289extern OSErr MXInfo(hostName, returnRecPtr, resultProc, userDataPtr)
290char *hostName;
291struct returnRec *returnRecPtr;
292ResultProc2Ptr resultProc;
293char *userDataPtr;
294{
295 if (dnr == nil)
296 /* resolver not loaded error */
297 return(notOpenErr);
298
299 return((*dnr)(MXINFO, hostName, returnRecPtr, resultProc, userDataPtr));
300
301 }