home *** CD-ROM | disk | FTP | other *** search
- /*
- DNR.c
-
- C routines to implement the domain name resolving functions.
-
- Some portions © Apple Computer.
-
- 01/30/94 dn - Created.
- 12/04/95 dn - Prep'd for Apprentice 4
- */
-
- #include <Gestalt.h>
- #include <Folders.h>
- #include <Traps.h>
-
- #include <MyTCPIncludes.h>
-
- Handle gMacDNRcode=(Handle)0L; // the DNR code resource’s handle
- UniversalProcPtr gMacDNRentry=(UniversalProcPtr)0L; // the DNR code entry point
-
- /* Local Constants */
- #define _kDefaultHostFilename "\pHosts"
-
- /* Local Prototypes */
- OSErr BuildHostFile(StringPtr filename,short vRefNum,long dirId);
- OSErr SearchFolderForDNRP(OSType targetType,OSType targetCreator,short vRefNum, long dirID);
- OSErr OpenTheDNR(void);
-
- /*
- OpenResolver
-
- Makes the resolver available for the other DNR routines. This routine must be called before any of
- the other DNR routines.
- */
- OSErr OpenResolver(StringPtr fileName){
- short vRefNum;
- short refnum;
- long dirID;
- short fRef;
- OSErr rc;
-
- // is the resolver already there?
- if (gMacDNRentry)
- return noErr;
-
- // open the MacTCP driver to get DNR resources
- refnum = (short)OpenTheDNR();
- // Ignore any errors because the resource may be installed in the system file (Mac 512k's).
-
- // load the DNR resource package
- gMacDNRcode = GetIndResource('dnrp', 1);
- if (gMacDNRcode==(Handle)0){
- rc=ResError();
- return rc;
- }
-
- DetachResource(gMacDNRcode);
-
- if (refnum != -1){ // i.e. if it's not the system folder...
- CloseWD(refnum);
- CloseResFile(refnum);
- }
-
- // lock the DNR resource since it cannot be relocated while opened
- MoveHHi((Handle)gMacDNRcode);
- HLock((Handle)gMacDNRcode);
-
- gMacDNRentry = (UniversalProcPtr)*gMacDNRcode;
-
- if (fileName!=(StringPtr)0){
- if (fileName[0]==0){
- // then we were passed an empty string
- // pretend it is the null string (because it is in reality)
-
- fileName=(StringPtr)0;
- }
- }
-
- // if the filename is null...
- if (fileName==(StringPtr)0){
- /*
- Then the resolver is going to try to use the hosts file in the system
- folder. We will check to see if it is there; if not, we are going to create
- it...
- */
-
- GetSystemFolder(&vRefNum, &dirID);
-
- // try to open the file...
- rc = HOpen(vRefNum,dirID,_kDefaultHostFilename,fsRdPerm,&fRef);
-
- switch (rc) {
- case noErr: // the file exists...
- FSClose(fRef);
- break;
- case fnfErr: // the file doesn't exist, create one...
- BuildHostFile(_kDefaultHostFilename,vRefNum,dirID);
- break;
- }
- }
-
- // convert the name to a C string temporarily...
- if (fileName!=(StringPtr)NULL)
- MyP2CStr(fileName);
-
- // ask the DNR resource to open the resolver
- rc = CallOpenResolverProc((OpenResolverUPP)gMacDNRentry,OPENRESOLVER,(char*)fileName);
-
- // quick convert the name back...
- if (fileName!=(StringPtr)NULL)
- MyC2PStr((char*)fileName);
-
- if (rc != noErr) {
- HUnlock((Handle)gMacDNRcode); // problem with open resolver, flush DNR resource
- DisposeHandle((Handle)gMacDNRcode);
- gMacDNRcode = NULL;
- gMacDNRentry = NULL;
- return dnrNoResolver; // signal failure of DNR
- }
- return noErr;
- }
-
- /*
- CloseResolver
-
- Closes the resolver. Do not make any more resolver calls unless the resolver is opened again.
- */
- OSErr CloseResolver(){
- if (gMacDNRentry ==(UniversalProcPtr)NULL)
- return dnrNoResolver;
-
- // call CloseResolver function in DNR
- CallCloseResolverProc((CloseResolverUPP)gMacDNRentry,CLOSERESOLVER);
-
- // release the DNR code resource
- HUnlock((Handle)gMacDNRcode);
- DisposeHandle((Handle)gMacDNRcode);
- gMacDNRcode = (Handle)NULL;
- gMacDNRentry = (UniversalProcPtr)NULL;
-
- return noErr;
- }
-
- /*
- StrToAddr
-
- Converts a host name string to an address. (ASYNCHRONOUS CALL)
- */
- OSErr StrToAddr(char* hostName,HostInfoPtr host,HostInfoUPP hiproc,char* userDataPtr){
- if (gMacDNRentry == (UniversalProcPtr)0)
- return dnrNoResolver;
-
- return CallStrToAddrProc((StrToAddrUPP)gMacDNRentry,STRTOADDR,hostName,host,hiproc,userDataPtr);
- }
-
- /*
- AddrToStr
-
- Convert an address to it's string equivalent. (SYNCHRONOUS CALL)
- */
- OSErr AddrToStr(ip_addr addr, char* str){
- if (gMacDNRentry == (UniversalProcPtr)0)
- return dnrNoResolver;
-
- CallAddrToStrProc((AddrToStrUPP)gMacDNRentry,ADDRTOSTR,addr,str);
-
- return noErr;
- }
-
- /*
- EnumCache
-
- Enumerates through the cache of addresses internal to the resolver. (ASYNCHRONOUS CALL)
- */
- OSErr EnumCache(EnumUPP eproc,char* userDataPtr){
- if (gMacDNRentry==(UniversalProcPtr)0)
- return dnrNoResolver;
-
- return CallEnumCacheProc((EnumCacheUPP)gMacDNRentry,ENUMCACHE,eproc,userDataPtr);
- }
-
- /*
- AddrToName
-
- Convert an address to it's name equivalent. (ASYNCHRONOUS CALL)
- */
- OSErr AddrToName(ip_addr addr,HostInfoPtr host,HostInfoUPP hiproc,char* userDataPtr){
- if (gMacDNRentry==(UniversalProcPtr)0)
- return dnrNoResolver;
-
- return CallAddrToNameProc((AddrToNameUPP)gMacDNRentry,ADDRTONAME,addr,host,hiproc,userDataPtr);
- }
-
- /*
- HInfo
-
- Get Host Information. (cpu type and os type) (ASYNCHRONOUS CALL)
- */
- OSErr HInfo(char* hostName,ReturnPtr ret,ReturnRecUPP rproc,char* userDataPtr){
- if (gMacDNRentry==(UniversalProcPtr)0)
- return dnrNoResolver;
-
- return CallHInfoProc((HInfoUPP)gMacDNRentry,HINFO,hostName,ret,rproc,userDataPtr);
- }
-
- /*
- MXInfo
-
- Get Host Mailer Info. (postmaster's mail address) (ASYNCHRONOUS CALL)
- */
- OSErr MXInfo(char* hostName,ReturnPtr ret,ReturnRecUPP rproc,char* userDataPtr){
- if (gMacDNRentry==(UniversalProcPtr)0)
- return dnrNoResolver;
-
- return CallMXInfoProc((MXInfoUPP)gMacDNRentry,MXINFO,hostName,ret,rproc,userDataPtr);
- }
-
- /*
- OpenTheDNR
-
- Opens the resource file containing the DNR.
- */
- OSErr OpenTheDNR(){
- short refnum;
- short vRefNum;
- long dirID;
-
- // first search Control Panels for MacTCP 1.1 or greater
- GetCPanelFolder(&vRefNum, &dirID);
- refnum = SearchFolderForDNRP('cdev', 'ztcp', vRefNum, dirID);
- if (refnum != fnfErr)
- return refnum;
-
- // next search System Folder for MacTCP 1.0.x
- GetSystemFolder(&vRefNum, &dirID);
- refnum = SearchFolderForDNRP('cdev', 'mtcp', vRefNum, dirID);
- if (refnum != fnfErr)
- return refnum;
-
- // finally, search Control Panels for MacTCP 1.0.x
- GetCPanelFolder(&vRefNum, &dirID);
- refnum = SearchFolderForDNRP('cdev', 'mtcp', vRefNum, dirID);
- if (refnum != fnfErr)
- return refnum;
-
- return fnfErr;
- }
-
- /*
- SearchFolderForDNRP
-
- Search a designated folder for a file with the given type/creator and containing the 'dnrp' resource.
- */
- OSErr SearchFolderForDNRP(OSType targetType,OSType targetCreator,short vRefNum, long dirID){
- HParamBlockRec fi;
- Str255 filename;
- short refnum;
-
- // initialize our search mechanism
- fi.fileParam.ioCompletion = nil;
- fi.fileParam.ioNamePtr = filename;
- fi.fileParam.ioVRefNum = vRefNum;
- fi.fileParam.ioDirID = dirID;
- fi.fileParam.ioFDirIndex = 1;
-
- // keep looking till we run out of files
- while (PBHGetFInfo(&fi, false) == noErr) {
-
- // scan the folder for files that match our type & creator
- if (fi.fileParam.ioFlFndrInfo.fdType == targetType &&
- fi.fileParam.ioFlFndrInfo.fdCreator == targetCreator) {
-
- // type/creator match, look for the resource
- refnum = HOpenResFile(vRefNum, dirID, filename, fsRdPerm);
- if (GetIndResource('dnrp', 1) == NULL)
- CloseResFile(refnum);
- else
- return refnum;
- }
-
- // no match or no resource, try next file in folder
- fi.fileParam.ioFDirIndex++;
- fi.fileParam.ioDirID = dirID; // PBHGetFInfo() clobbers ioDirID
- }
- return fnfErr; // nothing found
- }
-
- /*
- BuildHostFile
-
- This function will create a host file and copy the information stored from the resources into
- the new host file.
-
- */
- OSErr BuildHostFile(StringPtr filename,short vRefNum,long dirId){
- OSErr err;
- Handle tH;
- short fref;
- long len;
-
- // first try to create the file...
- err=HCreate(vRefNum, dirId,filename,kHostFileCreatorType,'TEXT');
-
- if (err!=noErr)
- return err;
-
- // the file has been created...
-
- // now try to get the text resource...
- tH=GetResource('TEXT',30004);
-
- if (tH==(Handle)0L)
- return ResError();
-
- HLock(tH);
-
- len=SizeResource(tH);
-
- // Got the text, open the file
- err=HOpenDF(vRefNum,dirId,filename,fsWrPerm,&fref);
-
- if (err!=noErr)
- goto ExitFromBuildHostFile;
-
- // got the file, write the text...
- err=FSWrite(fref,&len,*tH);
-
- // all done...
- FSClose(fref);
-
- ExitFromBuildHostFile:
- HUnlock(tH);
-
- ReleaseResource(tH);
-
- return err;
- }
-
- /*
- GetSystemFolder
-
- Return the ID of the current system folder.
- */
- OSErr GetSystemFolder (short *vRefNumP, long *dirIDP){
- SysEnvRec info;
- long wdProcID;
-
- SysEnvirons(1, &info);
- if (GetWDInfo(info.sysVRefNum, vRefNumP, dirIDP, &wdProcID) != noErr) {
- *vRefNumP = 0;
- *dirIDP = 0;
- }
- }
-
- /*
- GetCPanelFolder
-
- Return the ID of the current “Control Panels” folder.
- */
- OSErr GetCPanelFolder (short *vRefNumP, long *dirIDP){
- Boolean hasFolderMgr = false;
- long feature;
-
- // see if we have the Folder Manager available
- if (TrapAvailable((short) _Gestalt))
- if (Gestalt(gestaltFindFolderAttr, &feature) == noErr)
- hasFolderMgr = TRUE;
-
- if (!hasFolderMgr) {
- // return the system folder because we are obviously running on system 6 or less when there
- // was no control panels folder.
-
- return GetSystemFolder(vRefNumP, dirIDP);
- } else {
- OSErr err;
-
- err=FindFolder(kOnSystemDisk, kControlPanelFolderType,kDontCreateFolder, vRefNumP, dirIDP);
-
- if (err!= noErr) {
- *vRefNumP = 0;
- *dirIDP = 0;
-
- return err;
- }
- }
- return noErr;
- }
-
- /*
- NumToolboxTraps
-
- Returns the number of toolbox traps.
- */
- short NumToolboxTraps(){
- if (NGetTrapAddress(_InitGraf, ToolTrap) == NGetTrapAddress(0xAA6E, ToolTrap))
- return(0x0200);
- else
- return(0x0400);
- }
-
- /*
- GetTrapType
-
- Returns the type of the specified trap.
- */
- TrapType GetTrapType(short theTrap){
- if ((theTrap & TrapMask) > 0)
- return(ToolTrap);
- else
- return(OSTrap);
- }
-
- /*
- TrapAvailable
-
- Is the specified trap available?
- */
- Boolean TrapAvailable(short theTrap){
- TrapType tType;
-
- tType = GetTrapType(theTrap);
- if (tType == ToolTrap)
- theTrap = theTrap & 0x07FF;
-
- if (theTrap >= NumToolboxTraps())
- theTrap = _Unimplemented;
-
- return (NGetTrapAddress(theTrap, tType) !=
- NGetTrapAddress(_Unimplemented, ToolTrap));
- }
-
-
-