dnr.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. File: DNR.c
  3. Contains: DNR library for MPW
  4. Copyright: © 1989-1995 by Apple Computer, Inc., all rights reserved
  5. Version: Technology: Networking
  6. Package: Use with MacTCP 2.0.6 and the Universal
  7. Interfaces 2.1b1
  8. Change History (most recent first):
  9. <3> 1/23/95 rrk implemented use of universal procptrs
  10. Changed selector name HINFO to HXINFO
  11. due to conflict of name in MacTCP header
  12. Removed use of TrapAvailable and exchanged
  13. for the TrapExists call.
  14. Changed symbol codeHandle to gDNRCodeHndl
  15. Changed symbol dnr to gDNRCodePtr
  16. Further modifications by Steve Falkenburg, Apple MacDTS 8/91
  17. Modifications by Jim Matthews, Dartmouth College, 5/91
  18. */
  19. #ifndef __OSUTILS__
  20. #include <OSUtils.h>
  21. #endif
  22. #ifndef __ERRORS__
  23. #include <Errors.h>
  24. #endif
  25. #ifndef __FILES__
  26. #include <Files.h>
  27. #endif
  28. #ifndef __RESOURCES__
  29. #include <Resources.h>
  30. #endif
  31. #ifndef __MEMORY__
  32. #include <Memory.h>
  33. #endif
  34. #ifndef __TRAPS__
  35. #include <Traps.h>
  36. #endif
  37. #ifndef __GESTALT__
  38. #include <Gestalt.h>
  39. #endif
  40. #ifndef __FOLDERS__
  41. #include <Folders.h>
  42. #endif
  43. #ifndef __TOOLUTILS__
  44. #include <ToolUtils.h>
  45. #endif
  46. #ifndef __MACTCP__
  47. #include "MacTCP.h"
  48. #endif
  49. #ifndef __ADDRESSXLATION__
  50. #include "AddressXlation.h"
  51. #endif
  52. // think C compatibility stuff
  53. #ifndef _GestaltDispatch
  54. #define _GestaltDispatch _Gestalt
  55. #endif
  56. /* RRK Modification 1/95 - commenting out the following defines as they are
  57. defined in the DNRCalls.h header file
  58. */
  59. void GetSystemFolder(short *vRefNumP, long *dirIDP);
  60. void GetCPanelFolder(short *vRefNumP, long *dirIDP);
  61. short SearchFolderForDNRP(long targetType, long targetCreator, short vRefNum, long dirID);
  62. short OpenOurRF(void);
  63. short NumToolboxTraps(void);
  64. TrapType GetTrapType(short theTrap);
  65. Boolean TrapExists(short theTrap);
  66. static Handle gDNRCodeHndl = nil;
  67. static ProcPtr gDNRCodePtr = nil;
  68. /* Check the bits of a trap number to determine its type. */
  69. /* InitGraf is always implemented (trap $A86E). If the trap table is big
  70. ** enough, trap $AA6E will always point to either Unimplemented or some other
  71. ** trap, but will never be the same as InitGraf. Thus, you can check the size
  72. ** of the trap table by asking if the address of trap $A86E is the same as
  73. ** $AA6E. */
  74. /* #pragma segment UtilMain */
  75. short NumToolboxTraps(void)
  76. {
  77. if (NGetTrapAddress(_InitGraf, ToolTrap) == NGetTrapAddress(0xAA6E, ToolTrap))
  78. return(0x200);
  79. else
  80. return(0x400);
  81. }
  82. /* #pragma segment UtilMain */
  83. TrapType GetTrapType(short theTrap)
  84. {
  85. /* OS traps start with A0, Tool with A8 or AA. */
  86. if ((theTrap & 0x0800) == 0) /* per D.A. */
  87. return(OSTrap);
  88. else
  89. return(ToolTrap);
  90. }
  91. Boolean TrapExists(short theTrap)
  92. {
  93. TrapType theTrapType;
  94. theTrapType = GetTrapType(theTrap);
  95. if ((theTrapType == ToolTrap) && ((theTrap &= 0x07FF) >= NumToolboxTraps()))
  96. theTrap = _Unimplemented;
  97. return(NGetTrapAddress(_Unimplemented, ToolTrap) != NGetTrapAddress(theTrap, theTrapType));
  98. }
  99. void GetSystemFolder(short *vRefNumP, long *dirIDP)
  100. {
  101. SysEnvRec info;
  102. long wdProcID;
  103. SysEnvirons(1, &info);
  104. if (GetWDInfo(info.sysVRefNum, vRefNumP, dirIDP, &wdProcID) != noErr)
  105. {
  106. *vRefNumP = 0;
  107. *dirIDP = 0;
  108. }
  109. }
  110. void GetCPanelFolder(short *vRefNumP, long *dirIDP)
  111. {
  112. Boolean hasFolderMgr = false;
  113. long feature;
  114. if (TrapExists(_GestaltDispatch)) if (Gestalt(gestaltFindFolderAttr, &feature) == noErr) hasFolderMgr = true;
  115. if (!hasFolderMgr)
  116. {
  117. GetSystemFolder(vRefNumP, dirIDP);
  118. return;
  119. }
  120. else
  121. {
  122. if (FindFolder(kOnSystemDisk, kControlPanelFolderType, kDontCreateFolder, vRefNumP, dirIDP) != noErr)
  123. {
  124. *vRefNumP = 0;
  125. *dirIDP = 0;
  126. }
  127. }
  128. }
  129. /* SearchFolderForDNRP is called to search a folder for files that might
  130. contain the 'dnrp' resource */
  131. short SearchFolderForDNRP(long targetType, long targetCreator, short vRefNum, long dirID)
  132. {
  133. HParamBlockRec fi;
  134. Str255 filename;
  135. short refnum;
  136. fi.fileParam.ioCompletion = nil;
  137. fi.fileParam.ioNamePtr = filename;
  138. fi.fileParam.ioVRefNum = vRefNum;
  139. fi.fileParam.ioDirID = dirID;
  140. fi.fileParam.ioFDirIndex = 1;
  141. while (PBHGetFInfo(&fi, false) == noErr)
  142. {
  143. /* scan system folder for driver resource files of specific type & creator */
  144. if (fi.fileParam.ioFlFndrInfo.fdType == targetType &&
  145. fi.fileParam.ioFlFndrInfo.fdCreator == targetCreator)
  146. {
  147. /* found the MacTCP driver file? */
  148. refnum = HOpenResFile(vRefNum, dirID, filename, fsRdPerm);
  149. if (GetIndResource('dnrp', 1) == NULL)
  150. CloseResFile(refnum);
  151. else
  152. return refnum;
  153. }
  154. /* check next file in system folder */
  155. fi.fileParam.ioFDirIndex++;
  156. fi.fileParam.ioDirID = dirID; /* PBHGetFInfo() clobbers ioDirID */
  157. }
  158. return(-1);
  159. }
  160. /* OpenOurRF is called to open the MacTCP driver resources */
  161. short OpenOurRF(void)
  162. {
  163. short refnum;
  164. short vRefNum;
  165. long dirID;
  166. /* first search Control Panels for MacTCP 1.1 */
  167. GetCPanelFolder(&vRefNum, &dirID);
  168. refnum = SearchFolderForDNRP('cdev', 'ztcp', vRefNum, dirID);
  169. if (refnum != -1) return(refnum);
  170. /* next search System Folder for MacTCP 1.0.x */
  171. GetSystemFolder(&vRefNum, &dirID);
  172. refnum = SearchFolderForDNRP('cdev', 'mtcp', vRefNum, dirID);
  173. if (refnum != -1) return(refnum);
  174. /* finally, search Control Panels for MacTCP 1.0.x */
  175. GetCPanelFolder(&vRefNum, &dirID);
  176. refnum = SearchFolderForDNRP('cdev', 'mtcp', vRefNum, dirID);
  177. if (refnum != -1) return(refnum);
  178. return -1;
  179. }
  180. OSErr OpenResolver(char *fileName)
  181. {
  182. short refnum;
  183. OSErr rc;
  184. if (gDNRCodePtr != nil)
  185. /* resolver already loaded in */
  186. return(noErr);
  187. /* open the MacTCP driver to get DNR resources. Search for it based on
  188. creator & type rather than simply file name */
  189. refnum = OpenOurRF();
  190. /* ignore failures since the resource may have been installed in the
  191. System file if running on a Mac 512Ke */
  192. /* load in the DNR resource package */
  193. gDNRCodeHndl = GetIndResource('dnrp', 1);
  194. if (gDNRCodeHndl == nil)
  195. {
  196. /* can't open DNR */
  197. return(ResError());
  198. }
  199. DetachResource(gDNRCodeHndl);
  200. if (refnum != -1)
  201. {
  202. CloseResFile(refnum);
  203. }
  204. /* lock the DNR resource since it cannot be reloated while opened */
  205. MoveHHi(gDNRCodeHndl);
  206. HLock(gDNRCodeHndl);
  207. gDNRCodePtr = (ProcPtr)*gDNRCodeHndl;
  208. /* call open resolver */
  209. // RRK modification 1/95 use CallOpenResolverProc define to call UPP
  210. rc = CallOpenResolverProc((OpenResolverUPP)gDNRCodePtr, OPENRESOLVER,
  211. fileName);
  212. if (rc != noErr)
  213. {
  214. /* problem with open resolver, flush it */
  215. HUnlock(gDNRCodeHndl);
  216. DisposeHandle(gDNRCodeHndl);
  217. gDNRCodePtr = nil;
  218. }
  219. return(rc);
  220. }
  221. OSErr CloseResolver(void)
  222. {
  223. if (gDNRCodePtr == nil)
  224. /* resolver not loaded error */
  225. return(notOpenErr);
  226. /* call close resolver */
  227. // RRK modification 1/95 use CallCloseResolverProc define to call UPP
  228. // (void) (*dnr)(CLOSERESOLVER);
  229. CallCloseResolverProc((CloseResolverUPP)gDNRCodePtr, CLOSERESOLVER);
  230. /* release the DNR resource package */
  231. HUnlock(gDNRCodeHndl);
  232. DisposeHandle(gDNRCodeHndl);
  233. gDNRCodePtr = nil;
  234. return(noErr);
  235. }
  236. // RRK modification 1/95 declare parameter resultProc to be of type
  237. // ResultProcUPP instead of a long
  238. OSErr StrToAddr(char *hostName, struct hostInfo *rtnStruct,
  239. ResultUPP resultproc, Ptr userDataPtr)
  240. {
  241. if (gDNRCodePtr == nil)
  242. /* resolver not loaded error */
  243. return(notOpenErr);
  244. // RRK modification 1/95 use CallStrToAddrProc define to call UPP
  245. // return((*dnr)(STRTOADDR, hostName, rtnStruct, resultproc, userDataPtr));
  246. return (CallStrToAddrProc((StrToAddrUPP)gDNRCodePtr, STRTOADDR, hostName, rtnStruct, resultproc, userDataPtr));
  247. }
  248. OSErr AddrToStr(unsigned long addr, char *addrStr)
  249. {
  250. OSErr err;
  251. if (gDNRCodePtr == nil)
  252. /* resolver not loaded error */
  253. return(notOpenErr);
  254. // RRK modification 1/95 use CallAddrToStrProc define to call UPP
  255. // (*dnr)(ADDRTOSTR, addr, addrStr);
  256. err = CallAddrToStrProc((AddrToStrUPP)gDNRCodePtr, ADDRTOSTR, addr, addrStr);
  257. return(noErr);
  258. }
  259. OSErr EnumCache(EnumResultUPP resultproc, Ptr userDataPtr)
  260. {
  261. if (gDNRCodePtr == nil)
  262. /* resolver not loaded error */
  263. return(notOpenErr);
  264. // RRK modification 1/95 use CallEnumCacheProc define to call UPP
  265. // return((*dnr)(ENUMCACHE, resultproc, userDataPtr));
  266. return (CallEnumCacheProc((EnumCacheUPP)gDNRCodePtr, ENUMCACHE, resultproc, userDataPtr));
  267. }
  268. OSErr AddrToName(unsigned long addr, struct hostInfo *rtnStruct,
  269. ResultUPP resultproc, Ptr userDataPtr)
  270. {
  271. if (gDNRCodePtr == nil)
  272. /* resolver not loaded error */
  273. return(notOpenErr);
  274. // RRK modification 1/95 use CallAddrToNameProc define to call UPP
  275. // return((*dnr)(ADDRTONAME, addr, rtnStruct, resultproc, userDataPtr));
  276. return(CallAddrToNameProc((AddrToNameUPP)gDNRCodePtr, ADDRTONAME, addr, rtnStruct, resultproc, userDataPtr));
  277. }
  278. extern OSErr HInfo(char *hostName, struct returnRec *returnRecPtr,
  279. ResultProc2UPP resultProc, Ptr userDataPtr)
  280. {
  281. if (gDNRCodePtr == nil)
  282. /* resolver not loaded error */
  283. return(notOpenErr);
  284. // RRK modification 1/95 use CallHInfoProc define to call UPP
  285. // return((*dnr)(HINFO, hostName, returnRecPtr, resultProc, userDataPtr));
  286. return(CallHInfoProc((HInfoUPP)gDNRCodePtr, HXINFO, hostName, returnRecPtr, resultProc, userDataPtr));
  287. }
  288. extern OSErr MXInfo(char *hostName, struct returnRec *returnRecPtr,
  289. ResultProc2UPP resultProc, Ptr userDataPtr)
  290. {
  291. if (gDNRCodePtr == nil)
  292. /* resolver not loaded error */
  293. return(notOpenErr);
  294. // RRK modification 1/95 use CallHInfoProc define to call UPP
  295. // return((*dnr)(MXINFO, hostName, returnRecPtr, resultProc, userDataPtr));
  296. return(CallMXInfoProc((MXInfoUPP)gDNRCodePtr, MXINFO, hostName, returnRecPtr, resultProc, userDataPtr));
  297. } /* removed ; (causes syntax err in Think C 5.0 */