home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Telnet 2.7b5 / source / main / Connections.c next >
Encoding:
C/C++ Source or Header  |  1995-04-24  |  25.4 KB  |  867 lines  |  [TEXT/CWIE]

  1. /****************************************************************
  2. *    NCSA Telnet for the Macintosh                                *
  3. *                                                                *
  4. *    National Center for Supercomputing Applications                *
  5. *    Software Development Group                                    *
  6. *    152 Computing Applications Building                            *
  7. *    605 E. Springfield Ave.                                        *
  8. *    Champaign, IL  61820                                        *
  9. *                                                                *
  10. *    Copyright (c) 1986-1993,                                    *
  11. *    Board of Trustees of the University of Illinois                *
  12. *****************************************************************/
  13.  
  14.  
  15.  
  16. #include "telneterrors.h"
  17. #include "DlogUtils.proto.h"
  18.  
  19. #include "wind.h"
  20. #include "event.proto.h"
  21.  
  22. #include "rsinterf.proto.h"
  23. #include "vsdata.h"
  24. #include "vskeys.h"
  25. #include "vsinterf.proto.h"
  26. #include "vgtek.proto.h"
  27. #include "tekrgmac.proto.h"
  28. #include "vr.h"
  29. #include "vrrgmac.proto.h" 
  30. #include "network.proto.h"
  31. #include "mydnr.proto.h"
  32. #include "InternalEvents.h"
  33. #include "menuseg.proto.h"
  34. #include "maclook.proto.h"
  35. #include "parse.proto.h"
  36. #include "parse.h"
  37. #include "configure.proto.h"
  38. #include "netevent.proto.h"
  39. #include "linemode.proto.h"
  40. #include "mainseg.proto.h"
  41. #include "prefs.proto.h"
  42. #include "popup.h"
  43. #include "popup.proto.h"
  44.  
  45. #include "Connections.proto.h"
  46. #include "tnae.h"
  47. #include "authencrypt.h"
  48. #include "authencrypt.proto.h"
  49. #include "wdefpatch.proto.h"
  50. #include "LinkedList.proto.h"
  51.  
  52. /*    These are all of the variables we need... */
  53.  
  54. extern    Cursor    *theCursors[NUMCURS];        /* all the cursors in a nice bundle */
  55. extern    WindRec    *screens;
  56. extern    short    scrn;
  57. extern    short    nNational;                // Number of user-installed translation tables
  58. extern    MenuHandle    myMenus[];
  59. extern    Boolean    authOK;
  60. extern    Boolean    encryptOK;
  61. extern    unsigned char *gReadspace;
  62. extern    short    gBlocksize;
  63.  
  64. static    short    WindByPort(short port);
  65. static    void setSessStates(DialogPtr dptr);
  66.  
  67. static    pascal short POCdlogfilter( DialogPtr dptr, EventRecord *evt, short *item);
  68. PROTO_UPP(POCdlogfilter, ModalFilter);
  69.  
  70. void OpenPortSpecial(MenuHandle menuh, short item)
  71. {
  72.     ConnInitParams        **theParams;
  73.     Boolean                success;
  74.     Str255                scratchPstring;
  75.     
  76.     GetItem(menuh, item, scratchPstring);
  77.     
  78.     theParams = NameToConnInitParams(scratchPstring,TRUE);
  79.     if (theParams == NULL) {
  80.         OutOfMemory(1020);
  81.         return;
  82.         }
  83.         
  84.     success = CreateConnectionFromParams(theParams);
  85. }
  86.  
  87. SIMPLE_UPP(POCdlogfilter, ModalFilter);
  88. pascal short POCdlogfilter( DialogPtr dptr, EventRecord *evt, short *item)
  89. {
  90.     short key;
  91.  
  92.     if (evt->what == keyDown) {
  93.         key = evt->message & charCodeMask;
  94.         if ( ((key == 'A') || (key == 'a')) && (evt->modifiers & cmdKey) ) {
  95.             *item = NCauthenticate;
  96.             return(-1);
  97.             }
  98.         if ( ((key == 'E') || (key == 'e')) && (evt->modifiers & cmdKey) ) {
  99.             *item = NCencrypt;
  100.             return(-1);
  101.             }
  102.         }
  103.     if (evt->what == mouseDown)
  104.         return(PopupMousedown(dptr, evt, item));
  105.     
  106.     return(DLOGwOK_Cancel(dptr, evt, item));
  107. }
  108.  
  109. void    PresentOpenConnectionDialog(void)
  110. {
  111.     ConnInitParams    **InitParams;
  112.     DialogPtr        dptr;
  113.     short            ditem, scratchshort;
  114.     Boolean            success;
  115.     long            scratchlong;
  116.     Str255            scratchPstring, terminalPopupString;
  117.     Handle            ItemHandle;
  118.     SessionPrefs    **defaultSessHdl,**tempSessHdl;
  119.     short             numberOfTerms;
  120.     MenuHandle        SessPopupHdl, TermPopupHdl;
  121.     Rect            scratchRect;
  122.     Point            SessPopupLoc;
  123.     short            TerminalIndex, itemNumber = 1;
  124.     popup TPopup[] = {{NCtermpopup, (MenuHandle) 0, 1},
  125.                         {0, (MenuHandle) 0, 0}};
  126.     Size             junk;
  127.     LinkedListNode    *theHead;
  128.     
  129.     SetCursor(theCursors[normcurs]);
  130.     
  131.     dptr = GetNewMyDialog(NewCnxnDLOG, NULL, kInFront, (void *)ThirdCenterDialog);
  132.     if (dptr == NULL) {
  133.         OutOfMemory(1000);
  134.         return;
  135.         }
  136.         
  137.     ditem = 3;
  138.     
  139.     GetIndString(scratchPstring,MISC_STRINGS,SESSION_STRING);
  140.     SessPopupHdl = NewMenu(668, scratchPstring);
  141.     if (SessPopupHdl == NULL) {
  142.         DisposeDialog(dptr);
  143.         OutOfMemory(1000);
  144.         return;
  145.         }
  146.     UseResFile(TelInfo->SettingsFile);
  147.     numberOfTerms = Count1Resources(SESSIONPREFS_RESTYPE);
  148.     theHead  = createSortedList(SESSIONPREFS_RESTYPE,numberOfTerms,"\p<Default>");
  149.     EnableItem(SessPopupHdl, 0);        // Make sure the entire menu is enabled
  150.     addListToMenu(SessPopupHdl, theHead);
  151.     deleteList(&theHead);
  152.  
  153.     GetDItem(dptr, NCsesspopup, &scratchshort, &ItemHandle, &scratchRect);
  154.     SessPopupLoc.h = scratchRect.left;
  155.     SessPopupLoc.v = scratchRect.top;
  156.     
  157.     TermPopupHdl = NewMenu(666, "\p");
  158.     if (TermPopupHdl == NULL) {
  159.         DisposeHandle((Handle)SessPopupHdl);
  160.         DisposeDialog(dptr);
  161.         OutOfMemory(1000);
  162.         return;
  163.         }
  164.     SetPort(dptr);
  165.     LocalToGlobal(&SessPopupLoc);
  166.  
  167.     numberOfTerms = Count1Resources(TERMINALPREFS_RESTYPE);
  168.     theHead  = createSortedList(TERMINALPREFS_RESTYPE,numberOfTerms,"\p<Default>");
  169.     addListToMenu(TermPopupHdl, theHead);
  170.     deleteList(&theHead);
  171.     TPopup[0].h = TermPopupHdl;
  172.     PopupInit(dptr, TPopup);
  173.     
  174.     // Get default auth/encrypt settings from default session
  175.     defaultSessHdl = GetDefaultSession();
  176.     HLock((Handle)defaultSessHdl);
  177.  
  178.     BlockMove("\p<Default>", scratchPstring, 15);
  179.     GetHostNameFromSession(scratchPstring);
  180.     SetTEText(dptr, NChostname, scratchPstring);
  181.     SelIText(dptr, NChostname, 0, 32767);
  182.  
  183.     SetCntrl(dptr, NCauthenticate, (**defaultSessHdl).authenticate && authOK);
  184.     SetCntrl(dptr, NCencrypt, (**defaultSessHdl).encrypt && encryptOK);
  185.     if (!authOK)
  186.     {
  187.         Hilite( dptr, NCauthenticate, 255);
  188.         Hilite( dptr, NCencrypt, 255);
  189.     }
  190.         
  191.     TerminalIndex = findPopupMenuItem(TermPopupHdl,(**defaultSessHdl).TerminalEmulation);
  192.     TPopup[0].choice = TerminalIndex;
  193.     PopupInit(dptr, TPopup);
  194.  
  195.     DisposeHandle((Handle)defaultSessHdl);
  196.     setSessStates(dptr);
  197.  
  198.     while (ditem > NCcancel) {
  199.         ModalDialog(POCdlogfilterUPP, &ditem);
  200.         switch(ditem) 
  201.         {
  202.             case    NCauthenticate:
  203.             case    NCencrypt:
  204.                 GetDItem(dptr, ditem, &scratchshort, &ItemHandle, &scratchRect);
  205.                 if ((**(ControlHandle)ItemHandle).contrlHilite == 0) {    // if control not disabled
  206.                     FlipCheckBox(dptr, ditem);
  207.                     setSessStates(dptr);
  208.                 }
  209.                 break;
  210.             case    NCsesspopup:
  211.                 InsertMenu(SessPopupHdl, hierMenu);
  212.                 CalcMenuSize(SessPopupHdl);
  213.                 scratchlong = PopUpMenuSelect(SessPopupHdl, SessPopupLoc.v,
  214.                                                 SessPopupLoc.h, 0);
  215.                 DeleteMenu(668);
  216.                 if (scratchlong) 
  217.                 {
  218.                     scratchshort = scratchlong & 0xFFFF; //    Apple sez ignore the high word
  219.                     GetItem(SessPopupHdl, scratchshort, scratchPstring);
  220.                     tempSessHdl = (SessionPrefs **)Get1NamedResource(SESSIONPREFS_RESTYPE, scratchPstring);
  221.                     if (tempSessHdl) 
  222.                     {
  223.                         TerminalIndex = findPopupMenuItem(TermPopupHdl,
  224.                                 (**tempSessHdl).TerminalEmulation);
  225.                         TPopup[0].choice = TerminalIndex;
  226.                         DrawPopUp(dptr, NCtermpopup); //update popup
  227.                         SetTEText(dptr, NChostname, (**tempSessHdl).hostname);//update the hostname
  228.                         SelIText(dptr, NChostname, 0, 32767);
  229.                         SetCntrl(dptr, NCauthenticate, (**tempSessHdl).authenticate && authOK);//update the auth status
  230.                         SetCntrl(dptr, NCencrypt, (**tempSessHdl).encrypt && encryptOK);
  231.                         setSessStates(dptr);//encrypt cant be on w/o authenticate
  232.                         ReleaseResource((Handle)tempSessHdl);
  233.                     }
  234.                 }
  235.                 break;
  236.             default:
  237.                 break;
  238.         } // switch
  239.     } // while
  240.     
  241.     
  242.     if (ditem == NCcancel) {
  243.         PopupCleanup();
  244.         DisposeDialog(dptr);
  245.         return;
  246.         }
  247.     
  248.     GetTEText(dptr, NChostname, scratchPstring);
  249.     if (!Length(scratchPstring)) {
  250.         PopupCleanup();
  251.         DisposeDialog(dptr);
  252.         return;
  253.         }
  254.     
  255.     GetItem(TPopup[0].h, TPopup[0].choice, terminalPopupString);
  256.     PopupCleanup();
  257.     
  258.     MaxMem(&junk);
  259.     InitParams = NameToConnInitParams(scratchPstring, FALSE);
  260.     if (InitParams == NULL)
  261.     {
  262.         DisposeDialog(dptr);
  263.         OutOfMemory(1000);
  264.         return;
  265.         }
  266.     if ((**InitParams).terminal == NULL)  //if this is not null, then the string was an alias,
  267.     {                                        // so dont use the popup terminal
  268.         (**InitParams).terminal = (TerminalPrefs **)
  269.                 Get1NamedResource(TERMINALPREFS_RESTYPE,terminalPopupString);
  270.         DetachResource((Handle)(**InitParams).terminal);
  271.         if (InitParams == NULL) {
  272.             OutOfMemory(1000);
  273.             DisposeDialog(dptr);
  274.             return;
  275.             }
  276.          if (GetCntlVal(dptr, NCauthenticate))
  277.               (**(**InitParams).session).authenticate = 1;
  278.          else
  279.              (**(**InitParams).session).authenticate = 0;
  280.          if (GetCntlVal(dptr, NCencrypt))
  281.               (**(**InitParams).session).encrypt = 1;
  282.          else
  283.              (**(**InitParams).session).encrypt = 0;
  284.     }
  285.     HLock((Handle)InitParams);
  286.     HLock((Handle)(**InitParams).session);
  287.     GetTEText(dptr, NCwindowname, scratchPstring);
  288.  
  289.     // Copy over the user specified window name.  If blank, CreateConnectionFromParams 
  290.     // will copy the hostname to the windowname and append a number.
  291.     if (Length(scratchPstring)) 
  292.         BlockMove(scratchPstring, (**InitParams).WindowName,
  293.                     (Length(scratchPstring) > 63) ? 64 : (Length(scratchPstring) + 1));
  294.  
  295.     HUnlock((Handle)(**InitParams).session);
  296.     HUnlock((Handle)InitParams);
  297.         
  298.     DisposeDialog(dptr);
  299.     
  300.     success = CreateConnectionFromParams(InitParams);
  301. }
  302.  
  303. // Set states of session checkboxes
  304. static    void setSessStates (DialogPtr dptr)
  305. {        
  306.     if (GetCntlVal(dptr, NCauthenticate)) {
  307.         Hilite(dptr, NCencrypt, (encryptOK)? 0 : 255);
  308.     } else {
  309.         Hilite(dptr, NCencrypt, 255);
  310.         SetCntrl(dptr, NCencrypt, false);
  311.     }
  312. }
  313.  
  314. void    OpenConnectionFromURL(char *host, char *portstring, char *user, char *password)
  315. {
  316.     ConnInitParams    **Params;
  317.     Str255            windowName, tempString;
  318.     short            len;
  319.     long            port;
  320.     
  321.     Params = ReturnDefaultConnInitParams();
  322.     
  323.     windowName[0] = 0;
  324.     
  325.     // Set up window name if user (and password) given
  326.     if (user != nil) {
  327.         GetIndString(windowName, MISC_STRINGS, MISC_USERPRMPT);
  328.         len = strlen(user);
  329.         BlockMoveData(user,& windowName[Length(windowName)+1], len);
  330.         windowName[0] += len;
  331.         if (password != nil) {
  332.         GetIndString(tempString, MISC_STRINGS, MISC_PSWDPRMPT);
  333.             BlockMoveData(&tempString[1], &windowName[Length(windowName)+1], tempString[0]);
  334.             windowName[0] += tempString[0];
  335.             len = strlen(password);
  336.             BlockMoveData(password, &windowName[Length(windowName)+1], len);
  337.             windowName[0] += len;
  338.             }
  339.  
  340.         if (windowName[0] != 0) {
  341.             BlockMoveData(windowName, (**Params).WindowName, Length(windowName)+1);
  342.             }
  343.         }        
  344.     
  345.     CtoPstr(host);
  346.     BlockMoveData(host, (**(**Params).session).hostname, host[0]+1);
  347.     
  348.     if (portstring != nil) {
  349.         CtoPstr(portstring);
  350.         StringToNum((StringPtr)portstring, &port);
  351.         (**(**Params).session).port = port;
  352.         }
  353.         
  354.     (void)CreateConnectionFromParams(Params);
  355. }    
  356.  
  357. Boolean CreateConnectionFromParams( ConnInitParams **Params)
  358. {
  359.     short            scratchshort, fontnumber;
  360.     static short    numWind = 1, stagNum = 1;
  361.     SessionPrefs    *SessPtr;
  362.     TerminalPrefs    *TermPtr;
  363.     short            cur;
  364.     Str32            numPstring;
  365.     Str255            scratchPstring;
  366.     Boolean            scratchBoolean;
  367.     WindRec            *theScreen;
  368.     SetCursor(theCursors[watchcurs]);                    /* We may be here a bit */
  369.  
  370.     // Check if we have the max number of sessions open
  371.     if (TelInfo->numwindows == MaxSess) return(FALSE);
  372.     
  373.     cur = TelInfo->numwindows;            /* Adjust # of windows and get this window's number */
  374.     TelInfo->numwindows++;
  375.     theScreen = &screens[cur];
  376.     
  377.     theScreen->active = CNXN_NOTINUSE;    // Make sure it is marked as dead (in case we
  378.                                         // abort screen creation after initiating DNR.
  379.                                         // That way CompleteConnectionOpening will know
  380.                                         // we didn't make it.
  381.     HLockHi((Handle)Params);
  382.     HLockHi((Handle)(**Params).terminal);
  383.     HLockHi((Handle)(**Params).session);
  384.     SessPtr = *((**Params).session);
  385.     TermPtr = *((**Params).terminal);
  386.     
  387.     if (Length((**Params).WindowName) == 0) {
  388.         BlockMove((**(**Params).session).hostname, (**Params).WindowName, 
  389.                     Length((**(**Params).session).hostname)+1);
  390.         NumToString(numWind++, numPstring);
  391.         pstrcat((**Params).WindowName, "\p ");
  392.         pstrcat((**Params).WindowName, numPstring);    // tack the number onto the end.
  393.         }
  394.         
  395.     // Get the IP for the host while we set up the connection
  396.     if (DoTheDNR(SessPtr->hostname, cur) != noErr) {
  397.         OutOfMemory(1010);
  398.         DisposeHandle((Handle)(**Params).terminal);
  399.         DisposeHandle((Handle)(**Params).session);
  400.         DisposeHandle((Handle)Params);
  401.         TelInfo->numwindows--;
  402.         updateCursor(1);
  403.         return(FALSE);
  404.         }
  405.         
  406.     DoTheMenuChecks();
  407.     
  408.       theScreen->authenticate = SessPtr->authenticate && authOK;
  409.       theScreen->encrypt = SessPtr->encrypt && encryptOK;
  410.  
  411.     theScreen->aedata = NULL;
  412.      
  413.      for (scratchshort = 0; scratchshort < sizeof(theScreen->myopts); scratchshort++) {
  414.         theScreen->myopts[scratchshort] = 0;
  415.         theScreen->hisopts[scratchshort] = 0;        
  416.     }    
  417.     theScreen->cannon[0] = '\0';
  418.  
  419.     theScreen->vtemulation = TermPtr->vtemulation;
  420.     theScreen->forcesave = SessPtr->forcesave;
  421.     theScreen->eightbit = TermPtr->eightbit;    // Is this necessary?
  422.     theScreen->portNum = SessPtr->port;
  423.     theScreen->emacsmeta = TermPtr->emacsmetakey;
  424.     theScreen->Xterm = TermPtr->Xtermsequences;
  425.     theScreen->keypadmap = TermPtr->remapKeypad;
  426.     theScreen->port = -1;                // netxopen will take care of this
  427.  
  428.     theScreen->lineAllow = SessPtr->linemode;
  429.     if (SessPtr->linemode) //we allow linemode
  430.         initLinemode(&screens[cur]);
  431.         
  432.         
  433.     GetFNum(TermPtr->DisplayFont, &fontnumber);
  434.     
  435.     theScreen->vs = RSnewwindow(&((**Params).WindowLocation),TermPtr->numbkscroll, TermPtr->vtwidth,
  436.                                     TermPtr->vtheight, (**Params).WindowName, TermPtr->vtwrap,
  437.                                     fontnumber, TermPtr->fontsize, 0,
  438.                                     1, SessPtr->forcesave,cur);
  439.                                     
  440.     if (theScreen->vs <0 ) {     /* we have a problem opening up the virtual screen */
  441.         OutOfMemory(1011);
  442.         DisposeHandle((Handle)(**Params).terminal);
  443.         DisposeHandle((Handle)(**Params).session);
  444.         DisposeHandle((Handle)Params);
  445.         TelInfo->numwindows--;
  446.         DoTheMenuChecks();
  447.         updateCursor(1);
  448.         return(FALSE);
  449.         }
  450.  
  451.     theScreen->wind = RSgetwindow( theScreen->vs);
  452.     ((WindowPeek)theScreen->wind)->windowKind = WIN_CNXN;
  453.     
  454.     
  455.     
  456.     /*
  457.      * Attach our extra part to display encryption status
  458.      */
  459.     PatchWindowWDEF(theScreen->wind, &screens[cur]);
  460.  
  461.     theScreen->arrowmap = TermPtr->emacsarrows;          /* MAT -- save our arrow setting */
  462.     theScreen->maxscroll= TermPtr->numbkscroll;
  463.     theScreen->bsdel = SessPtr->bksp;
  464.     theScreen->crmap = SessPtr->crmap;
  465.     theScreen->tekclear = SessPtr->tekclear;
  466.     theScreen->ESscroll= TermPtr->clearsave;
  467.     theScreen->ANSIgraphics  = TermPtr->ANSIgraphics; //ANSI graphics, 2.7
  468.     theScreen->tektype = SessPtr->tektype;
  469.     theScreen->wrap = TermPtr->vtwrap;
  470.     theScreen->pgupdwn = TermPtr->MATmappings;        /* JMB: map pgup/pgdwn/home/end? */
  471.     theScreen->TELstop = SessPtr->skey;
  472.     theScreen->TELgo = SessPtr->qkey;
  473.     theScreen->TELip = SessPtr->ckey;
  474.     BlockMove((Ptr)SessPtr->hostname, theScreen->machine, Length(SessPtr->hostname)+1);
  475.     BlockMove(TermPtr->AnswerBackMessage, theScreen->answerback, 32);
  476.     theScreen->termstate = VTEKTYPE;
  477.     theScreen->naws = 0;                                /* NCSA - set default NAWS to zero */
  478.     theScreen->telstate=0;
  479.     theScreen->timing=0;
  480.     theScreen->curgraph=-1;                /* No graphics screen */
  481.     theScreen->clientflags = 0;            /* BYU */
  482.     theScreen->kblen = 0;                /* nothing in the buffer */
  483.     theScreen->enabled = 1;            /* Gotta be enabled to start with */
  484.     theScreen->Ittype = 0;
  485.     theScreen->Isga = 0;                /* I suppress go ahead = no */
  486.     theScreen->Usga = 0;                /* U suppress go ahead = no */
  487.     theScreen->remote_flow = 0;        /* they handle toggling remote_flow */
  488.     theScreen->allow_flow = 1;        /* initially, we allow flow control */
  489.     theScreen->restart_any_flow = 0;    /* initially, only an XON turns flow control back on  */
  490.     theScreen->termstate=VTEKTYPE;    /* BYU */
  491.     theScreen->echo = 1;
  492.     theScreen->halfdup = SessPtr->halfdup;    /* BYU */
  493.     
  494.     theScreen->national = 0;            // Default to no translation.
  495.     // Now see if the desired translation is available, if not use default translation.
  496.     for(scratchshort = 1; scratchshort <= nNational+1; scratchshort++) {
  497.         GetItem(myMenus[National], scratchshort, scratchPstring);
  498.         if (EqualString(SessPtr->TranslationTable, scratchPstring, TRUE, FALSE))
  499.             theScreen->national = scratchshort-1;
  500.         }
  501.                 
  502.     
  503.     // Set up paste related variables
  504.     theScreen->incount = 0;
  505.     theScreen->outcount = 0;
  506.     theScreen->outptr = NULL;
  507.     theScreen->outhand = NULL;
  508.     theScreen->outlen = 0;
  509.     theScreen->pastemethod = SessPtr->pastemethod;
  510.     theScreen->pastesize = SessPtr->pasteblocksize;
  511.     
  512.     scratchBoolean = RSsetcolor( theScreen->vs, 0, TermPtr->nfcolor);
  513.     scratchBoolean = RSsetcolor( theScreen->vs, 1, TermPtr->nbcolor);
  514.     scratchBoolean = RSsetcolor( theScreen->vs, 2, TermPtr->bfcolor);
  515.     scratchBoolean = RSsetcolor( theScreen->vs, 3, TermPtr->bbcolor);
  516.  
  517.     addinmenu(cur, (**Params).WindowName, diamondMark);
  518.     theScreen->active = CNXN_DNRWAIT;            // Signal we are waiting for DNR.
  519.  
  520.     theScreen->myInitParams = (Handle)Params;
  521.     HUnlock((Handle)(**Params).terminal);
  522.     HUnlock((Handle)(**Params).session);
  523.     // Params handle must stay locked because interrupt level DNR completion routine needs to deref it
  524.  
  525.     VSscrolcontrol( theScreen->vs, -1, theScreen->ESscroll);
  526.  
  527.     updateCursor(1);                            /* Done stalling the user */
  528.     return(TRUE);
  529. }
  530.  
  531. void    CompleteConnectionOpening(short dat, ip_addr the_IP, OSErr DNRerror, char *cname)
  532. {
  533.     ConnInitParams    **Params;
  534.     
  535.     if (screens[dat].active != CNXN_DNRWAIT) return;            // Something is wrong.
  536.     
  537.     Params = (ConnInitParams **)screens[dat].myInitParams;
  538.     
  539.     if (DNRerror == noErr) {
  540.         HLockHi((Handle)(**Params).session);
  541.  
  542.         if ((**(**Params).session).NetBlockSize < 512)    
  543.             (**(**Params).session).NetBlockSize = 512; //less than this can really get messy
  544.             
  545.         if (setReadBlockSize((**(**Params).session).NetBlockSize,dat) != 0) //couldnt get read buffer
  546.             return;
  547.         screens[dat].port  = netxopen(the_IP,(**(**Params).session).port,/* BYU 2.4.15 - open to host name */
  548.                     gApplicationPrefs->OpenTimeout);/* CCP 2.7 allow user set-able timeouts on open */
  549.         
  550.         // We need the cannonical hostname for Kerberos. Make best guess if
  551.         // DNR did not return a cname.
  552.         if (cname)
  553.             strncpy(screens[dat].cannon, cname, sizeof(screens[dat].cannon));
  554.         else
  555.             strncpy(screens[dat].cannon, (char *)(**(**Params).session).hostname, sizeof(screens[dat].cannon));
  556.         screens[dat].cannon[sizeof(screens[dat].cannon)-1] = '\0';
  557.         
  558.         DisposeHandle((Handle)(**Params).session);
  559.         DisposeHandle((Handle)(**Params).terminal);
  560.         DisposeHandle((Handle)Params);
  561.         
  562.         if (screens[dat].port <0) {                    /* Handle netxopen fail */
  563.             destroyport(dat);
  564.             }
  565.         screens[dat].active = CNXN_OPENING;
  566.         SetMenuMarkToOpeningForAGivenScreen(dat);    /* Change status mark */
  567.         }
  568.     else
  569.         {    // We should report the real DNR error here!
  570.         Str255        errorString, numberString, numberString2, scratchPstring;
  571.         DialogPtr    theDialog;
  572.         short        message, ditem = 3;
  573.         
  574.         HLockHi((Handle)(**Params).session);
  575.         BlockMove((**(**Params).session).hostname, scratchPstring, Length((**(**Params).session).hostname)+1);
  576.  
  577.         if (DNRerror >= -23048 && DNRerror <= -23041) message = DNRerror + 23050;
  578.         else message = 1;
  579.         
  580.         GetIndString(errorString,DNR_MESSAGES_ID, message);
  581.         NumToString((long)0, numberString);
  582.         NumToString((long)DNRerror, numberString2);
  583.         ParamText(scratchPstring, errorString, numberString, numberString2);
  584.         
  585.         theDialog = GetNewMyDialog(DNRErrorDLOG, NULL, kInFront, (void *)ThirdCenterDialog);
  586.         ShowWindow(theDialog);
  587.     
  588.         while (ditem > 1)    ModalDialog(DLOGwOKUPP, &ditem);
  589.         DisposeDialog(theDialog);
  590.  
  591.         DisposeHandle((Handle)(**Params).session);
  592.         DisposeHandle((Handle)(**Params).terminal);
  593.         DisposeHandle((Handle)Params);
  594.         destroyport(dat);
  595.         }
  596. }
  597.  
  598. void    ConnectionOpenEvent(short port)
  599. {
  600.     short    i;
  601.     
  602.     i=WindByPort(port);
  603.     if (i<0) { 
  604.         return;
  605.         }
  606.              
  607.     screens[ i].active= CNXN_ACTIVE;
  608.     RSshow( screens[i].vs);            /* BYU */
  609.     SelectWindow(screens[i].wind);    /* BYU */
  610.  
  611.     telnet_send_initial_options(&screens[i]);
  612.  
  613.     changeport(scrn,i);        /* BYU */
  614.     SetMenuMarkToLiveForAGivenScreen(scrn);            /* BYU */
  615.     DoTheMenuChecks();        /* BYU */
  616. }
  617.  
  618. void    ConnectionDataEvent(short port)
  619. {
  620.     short    i, cnt;
  621.     
  622.     i=WindByPort(port);                                    /* BYU */
  623.     if (i<0) {return; }                    /* BYU */
  624.     if (TelInfo->ScrlLock || !screens[i].enabled)    /* BYU LSC */
  625.         netputuev( CONCLASS, CONDATA, port,0);
  626.     else {
  627.         cnt = netread(port,gReadspace,gBlocksize);    /* BYU LSC */
  628.         parse( &screens[i], gReadspace, cnt);    /* BYU LSC */
  629.         screens[i].incount += cnt;                /* BYU LSC */
  630.         }
  631. }
  632.  
  633. void    ConnectionFailedEvent(short port)
  634. {
  635.     short    i;
  636.     Str255    scratchPstring;
  637.     
  638.     netclose( port);
  639.     i= WindByPort(port);
  640.     if (i<0) { return; }
  641.     BlockMove((Ptr)screens[i].machine, (Ptr)scratchPstring, Length(screens[i].machine)+1);
  642.     PtoCstr(scratchPstring);
  643.     DoError(807 | NET_ERRORCLASS, LEVEL2, (char *)scratchPstring);
  644.     
  645.     if (screens[i].active != CNXN_ACTIVE) destroyport(i);    // JMB - 2.6
  646.     else removeport(&screens[i]);        // JMB - 2.6
  647. }
  648.  
  649. void    ConnectionClosedEvent(short port)
  650. {
  651.     short i;
  652.  
  653.     i= WindByPort(port);
  654.     if (i<0) { 
  655.         netclose( port);            /* We close again.... */
  656.         return;
  657.         }
  658.  
  659.     FlushNetwork(i);                /* BYU */
  660.     netclose( screens[i].port);        /* BYU */
  661.     removeport(&screens[i]);                    /* BYU */
  662. }
  663.  
  664. static    short    WindByPort(short port)
  665. {
  666.     short i=0;
  667.  
  668.     while (i<TelInfo->numwindows &&
  669.             (screens[i].port != port || 
  670.                 ((screens[i].active != CNXN_ACTIVE) && (screens[i].active != CNXN_OPENING)))
  671.             ) i++;
  672.  
  673.     if (i>=TelInfo->numwindows) {                    /* BYU */
  674.         putln("Can't find a window for the port # in WindByPort");    /* BYU */
  675.         if (i==0) i=999;        /* BYU */
  676.         return(-i);                /* BYU */
  677.         }                        /* BYU */
  678.  
  679.     return(i);
  680. }
  681.  
  682. void destroyport(short wind)
  683. {
  684.     Handle    h;
  685.     short    i,
  686.             callNoWindow=0;
  687.     Size     junk;
  688.     WindRecPtr    tw;
  689.     
  690.     tw = &screens[wind];
  691.  
  692.     
  693.     SetCursor(theCursors[watchcurs]);        /* We may be here a while */
  694.  
  695.     if (tw->active == CNXN_ISCORPSE) {
  696.         if (tw->curgraph>-1)
  697.             detachGraphics( tw->curgraph);    /* Detach the Tek screen */
  698.         if (tw->outlen>0) {
  699.             tw->outlen=0;                        /* Kill the remaining send*/
  700.             HUnlock( tw->outhand);            /*  buffer */
  701.             HPurge ( tw->outhand);
  702.             }
  703.         }
  704.  
  705.     if (FrontWindow() == tw->wind)
  706.         callNoWindow=1;
  707.  
  708.     if (tw->aedata != NULL)
  709.         DisposePtr((Ptr)tw->aedata);
  710.  
  711.     /*
  712.      * Get handle to the WDEF patch block, kill the window, and then
  713.      * release the handle.
  714.      */
  715.     h = GetPatchStuffHandle(tw->wind, tw);
  716.     RSkillwindow( tw->vs);
  717.     SetDefaultKCHR();
  718.     if (h)
  719.         DisposeHandle(h);
  720.     tw->active = CNXN_NOTINUSE;
  721.     for (i=wind;i<TelInfo->numwindows-1;i++) {
  722.         screens[i]=screens[i+1];        /* Bump all of the pointers */
  723.         RePatchWindowWDEF(screens[i].wind, &screens[i]);    /* hack hack hack */
  724.         }
  725.     if (scrn>wind) scrn--;                /* Adjust for deleting a lower #ered screen */
  726.  
  727.     TelInfo->numwindows--;                        /* There are now fewer windows */
  728.     extractmenu( wind);                    /* remove from the menu bar */
  729.  
  730.     DoTheMenuChecks();
  731.     MaxMem(&junk);
  732. /* BYU 2.4.11 - the call to "NoWindow()" changes "myfrontwindow", 
  733.                 which is used by "updateCursor()", so we reversed 
  734.                 the order of the following two lines. */
  735.     if (callNoWindow) NoWindow();        /* BYU 2.4.11 - Update cursor stuff if front window */
  736.     updateCursor(1);                    /* BYU 2.4.11 - Done stalling the user */
  737.  
  738.     
  739. } /* destroyport */
  740.  
  741. void removeport(WindRecPtr tw)
  742. {
  743.     Str255        scratchPstring;
  744.     
  745.     SetCursor(theCursors[watchcurs]);                /* We may be here a while */
  746.  
  747.     if (tw->curgraph>-1)
  748.         detachGraphics( tw->curgraph);        /* Detach the Tek screen */
  749.         
  750.     if (tw->outlen>0) {
  751.                 tw->outlen=0;                /* Kill the remaining send*/
  752.                 HUnlock( tw->outhand);        /*  buffer */
  753.                 HPurge ( tw->outhand);
  754.                 }
  755.  
  756.     if (VSiscapturing(tw->vs))                /* NCSA: close up the capture */
  757.         CloseCaptureFile(tw->vs);            /* NCSA */
  758.  
  759.     if (VSisprinting(tw->vs))
  760.         ClosePrintingFile(tw->vs);
  761.  
  762.     if ((gApplicationPrefs->destroyKTickets)&&(numberLiveConnections() == 1))//if this is last window
  763.         DestroyTickets();
  764.         
  765.     if (!gApplicationPrefs->WindowsDontGoAway)
  766.         destroyport(findbyVS(tw->vs));
  767.     else {
  768.         Str255    temp;
  769.         
  770.         GetWTitle(tw->wind, scratchPstring);
  771.         sprintf((char *)temp, "(%#s)", scratchPstring);
  772.         CtoPstr((char *)temp);
  773.         SetWTitle(tw->wind, temp);
  774.  
  775.         tw->port = 32700;
  776.         tw->active = CNXN_ISCORPSE;
  777.         }
  778.     updateCursor(1);                            /* Done stalling the user */
  779. } /* removeport */
  780.  
  781.  
  782. //    We recognize the following input string: "xxxx yyyy"
  783. //    If "xxxx" matches a session name, that session record is used.  Otherwise, the default
  784. //    session record is used with "xxxx" as the DNS hostname.   "yyyy", if extant, is
  785. //    converted to a number.  If it is a valid number, it is used as the port to connect to.
  786. //    WARNING: Do not pass this routing a blank string.  (We check this in PresentOpenConnectionDialog.)
  787. //
  788. //    CCP 2.7:  If second argument is true, get terminal out of session pref; otherwise set it to NULL
  789. ConnInitParams    **NameToConnInitParams(StringPtr InputString, Boolean useDefaultTerminal)
  790. {
  791.     ConnInitParams    **theHdl;
  792.     SessionPrefs    **sessHdl;
  793.     TerminalPrefs    **termHdl;
  794.     short            portRequested;
  795.     Boolean            foundPort, wasAlias = FALSE;
  796.     long             junk;
  797.     theHdl = (ConnInitParams **)myNewHandleCritical(sizeof(ConnInitParams));
  798.     if (theHdl == NULL)
  799.         return NULL;
  800.     foundPort = ProcessHostnameString(InputString, &portRequested);        
  801.         
  802.     UseResFile(TelInfo->SettingsFile);
  803.     sessHdl = (SessionPrefs **)Get1NamedResource(SESSIONPREFS_RESTYPE, InputString);
  804.     if (sessHdl == NULL) 
  805.     {                // Connect to host xxxx w/default session.
  806.         sessHdl = GetDefaultSession();
  807.         DetachResource((Handle) sessHdl);
  808.         HLock((Handle)sessHdl);
  809.         BlockMove(InputString, (**sessHdl).hostname, 64);
  810.     }
  811.     else 
  812.     {    
  813.         DetachResource((Handle) sessHdl);
  814.         HLock((Handle)sessHdl);
  815.         wasAlias = TRUE;
  816.     }
  817.  
  818.     (**theHdl).session = sessHdl;
  819.     
  820.     UseResFile(TelInfo->SettingsFile);
  821.     if ((useDefaultTerminal)||(wasAlias))
  822.     {    
  823.         termHdl = (TerminalPrefs **)Get1NamedResource
  824.                 (TERMINALPREFS_RESTYPE, (**sessHdl).TerminalEmulation);
  825.         if (termHdl == NULL) termHdl = GetDefaultTerminal();
  826.         DetachResource((Handle) termHdl);
  827.         (**theHdl).terminal = termHdl;
  828.     }
  829.     else
  830.         (**theHdl).terminal = NULL;
  831.  
  832.     UnloadSeg(&PREFSUnload);
  833.     MaxMem(&junk);      //swap out space so we can make the new window CCP
  834.     HUnlock((Handle)sessHdl);
  835.     
  836.     ((**theHdl).WindowName)[0] = 0;
  837.     (**sessHdl).ip_address = 0;
  838.     
  839.     if (foundPort) (**sessHdl).port = portRequested;
  840.     
  841.     return(theHdl);
  842. }
  843.  
  844. ConnInitParams    **ReturnDefaultConnInitParams(void)
  845. {
  846.     ConnInitParams    **theHdl;
  847.  
  848.     theHdl = (ConnInitParams **)myNewHandle(sizeof(ConnInitParams));
  849.  
  850.     (**theHdl).session = GetDefaultSession();
  851.     (**(**theHdl).session).ip_address = 0;
  852.     (**theHdl).terminal = GetDefaultTerminal();
  853.     
  854.     return(theHdl);
  855. }
  856.  
  857. short numberLiveConnections(void)
  858. {
  859.     short i;
  860.     short liveConnections = 0;
  861.     for(i = 0; i < MaxSess; i++)
  862.         if ((screens[i].active == CNXN_ACTIVE)||(screens[i].active == CNXN_OPENING))
  863.             liveConnections++;
  864.     return liveConnections;
  865. }
  866.  
  867.