AmendHub

Download

jcs

/

subtext

/

dnr.c

 

(View History)

jcs   tcp: Move TCPResolveName to dnr, put SOCKS code behind an ifdef Latest amendment: 141 on 2022-06-13

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