home *** CD-ROM | disk | FTP | other *** search
- /*
- MacTCP.c
-
- Code to implement the MacTCP driver level information.
-
- 01/31/94 dn - Created.
- 03/01/94 dn - Modified to call OpenDriver.
- 11/28/94 mc - Converted to CodeWarrior
- 12/04/95 dn - Prep'd for Apprentice 4
- */
-
- #include <MyTCPIncludes.h>
-
- #include <SegLoad.h>
- #include <OSUtils.h>
- #include <Traps.h>
-
- // local static global variables
- static short _gMacTCP=-1;
- static ExitToShellUPP _gETSOriginal;
-
- // Local prototypes
- void MacTCPInstallETSPatch(void);
- pascal void ETSZapMacTCP(void);
-
- /*
- OpenMacTCP
-
- Opens the MacTCP driver using OpenDriver. The PBOpen way depended greatly upon the permissions passed
- to the driver. If they were not set correctly, the one of the apps could open the driver first, but any other
- app could not get to it because of invalid permissions.
- */
- OSErr OpenMacTCP(short* drv,Boolean patch){
- OSErr err=noErr;
-
- *drv=0;
-
- if (_gMacTCP!=-1){
- // the driver is already open...
- *drv=_gMacTCP;
- return noErr;
- }
-
- // else the driver hasn't been opened yet...
- err=OpenDriver("\p.IPP",&_gMacTCP);
-
- if (err!=noErr)
- return err;
-
- *drv=_gMacTCP;
-
- if (patch) // only install the patch once....
- MacTCPInstallETSPatch();
-
- return err;
- }
-
- /*
- KillMacTCP
-
- Kills any open streams that the application has open, but only if the library was used to open
- the MacTCP driver.
-
- This code comes from DTS's ZapTCP application. It is slightly modified to:
- a. Work automatically if the library is used to open MacTCP.
- b. Provide an external function call for external use.
- c. Removes notification of closed/terminated streams.
- d. Optimized.
- e. Reformated and reorganized.
- */
- OSErr KillMacTCP(void){
- THz theZone;
- OSErr err;
- TCPiopbPtr tcp;
- StreamPtr *curStream,stream;
- long theStream;
- unsigned long index,max;
-
- if (_gMacTCP==-1) // exit if the library didn't open MacTCP
- return noErr;
-
- theZone = ApplicZone();
-
- // call TCPGlobalInfo, which returns a list of the open connections and streams
- tcp=NewTCPiopbPtr();
-
- tcp->ioCRefNum=_gMacTCP;
- err=TCP_GlobalInfo(tcp,false);
-
- if (err!=noErr)
- return err;
-
- // check each stream to see if its buffers are in our application heap. if so,
- // release the connection via TCPAbort and TCPRelease.
-
- max = (*tcp).csParam.globalInfo.tcpParamPtr->tcpMaxConn;
-
- if (max>0){
- curStream = (*tcp).csParam.globalInfo.tcpCDBTable[0];
-
- for (index=0; index<max; index++,curStream++) {
-
- stream=*curStream;
- theStream = (long)*curStream;
-
- if ((theStream)&&((theStream%2)==0)&&(PtrZone((Ptr)theStream)==theZone)){
- // only release streams in our heap
-
- // abort connection
- tcp->tcpStream = stream;
- err = TCP_Abort(tcp,false);
-
- // release stream
- tcp->tcpStream = stream;
- err = TCP_Release(tcp,false);
- }
- }
- }
-
- DisposeTCPiopbPtr(tcp);
-
- return err;
- }
-
- /*
- MacTCPInstallETSPatch
-
- Installs a patch on ExitToShell to ensure that our routine is called.
- */
- void MacTCPInstallETSPatch(void){
- ExitToShellUPP ets;
-
- ets=NewExitToShellProc(ETSZapMacTCP);
-
- _gETSOriginal=(ExitToShellUPP)NGetTrapAddress(_ExitToShell,ToolTrap);
- NSetTrapAddress((UniversalProcPtr)ets,_ExitToShell,ToolTrap);
- }
-
- /*
- ETSZapMacTCP
-
- Routine called in place of the original ExitToShell procedure. It calls KillMacTCP to dispose
- of any open streams, then it calls the original ExitToShell procedure to allow the process to continue.
- */
- pascal void ETSZapMacTCP(void){
- long savedA5=SetCurrentA5();
- OSErr err;
-
- err=KillMacTCP();
-
- SetA5(savedA5);
-
- CallExitToShellProc(_gETSOriginal);
- }
-
-
-
-
-
-