home *** CD-ROM | disk | FTP | other *** search
/ CICA 1994 September / CICA_Shareware_for_Windows_Walnut_Creek_September_1994.iso / win3 / winsock / gcp_24.exe / GCP_CLNT.CP_ / GCP_CLNT.CP
Text File  |  1994-04-02  |  28KB  |  1,038 lines

  1.       /********************************************************************
  2.         *                                                                  *
  3.         *   Source File: GCP_CLNT.cpp                                      *
  4.         *   Date:        Fri Feb 28 14:56:07 1992                          *
  5.       *                                                                  *
  6.         ********************************************************************/
  7.  
  8. #define STRICT
  9. #pragma warning (disable:4706 4355)  
  10. #include <afx.h>
  11. #include <afxwin.h>
  12. #include <afxext.h>
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <ctype.h>
  17.  
  18. #include "..\include\gcp.h"
  19. #include "..\include\defs.h"
  20. #include "resource.h"
  21. #include "gcp_clnt.hpp"
  22. #define new DEBUG_NEW
  23. #define M_SENDCALLBACK WM_USER+100
  24. #define M_POSTCALLBACK WM_USER+101
  25.  
  26. /****************************************************
  27.  * CGcpWnd implementations:
  28.  ****************************************************/
  29.  
  30. CGcpApp GcpApp;  // application instance
  31. CGcpWnd *pGcpWnd;
  32.  
  33. CGcpApp::CGcpApp() : CWinApp("GCP")
  34.     {
  35.     }
  36.  
  37. BOOL CGcpApp::InitInstance()
  38.     {
  39.     m_pMainWnd=pGcpWnd=new CGcpWnd();
  40.     return TRUE;
  41.     }
  42.         
  43. CGcpApp::~CGcpApp()
  44.     {
  45.     }
  46.     
  47.  
  48. // Define TConnectDlg, a CDialog constructor
  49.  
  50. BEGIN_MESSAGE_MAP(TConnectTelnetDlg,CDialog)
  51.     ON_BN_CLICKED(ID_LISTEN, OnListen)
  52. END_MESSAGE_MAP()
  53.  
  54. TConnectTelnetDlg::TConnectTelnetDlg(PCGcpWnd AParent, LPSTR AName)
  55.     {
  56.     // create a modeless dialog box
  57.     Create(AName,AParent);
  58.     
  59.     // fill address combo with host names
  60.     CComboBox *pAddress=(CComboBox *)GetDlgItem(ID_ADDRESS);    
  61.     GCP_QUERY_RESULTS Results;
  62.     if (GCP_OK==GCPquery(NULL, GCP_GET_FIRST_HOST, &Results))
  63.        {
  64.         pAddress->AddString ((char *)Results.Host.Name);
  65.         while (GCP_OK==GCPquery(NULL, GCP_GET_NEXT_HOST, &Results))
  66.             pAddress->AddString ((char *)Results.Host.Name);
  67.         }
  68.     SetDlgItemText(ID_REMOTEPORT,"23");
  69.     }
  70.  
  71. void TConnectTelnetDlg::OnCancel(void)
  72.     {
  73.     // Cancel button selected
  74.     ShowWindow(SW_HIDE);
  75.     }
  76.  
  77. void TConnectTelnetDlg::OnOK(void)
  78.     {
  79.     // OK button selected
  80.     char RemoteAddress[40], PortBuf[10];
  81.     // fill local char arrays
  82.     GetDlgItemText(ID_ADDRESS,RemoteAddress, sizeof(RemoteAddress)-1);
  83.     GetDlgItemText(ID_REMOTEPORT,PortBuf, sizeof(PortBuf)-1);
  84.     if (!*RemoteAddress)
  85.         MessageBox("No Address Selected","Selection Error",MB_OK);
  86.     else if (!*PortBuf)
  87.         MessageBox("No Port Selected","Selection Error",MB_OK);
  88.    else
  89.         GetParent()->ShowData(
  90.             GCPopen(TELNET_SESSION, GetParent()->m_hWnd,M_SENDCALLBACK,0l,RemoteAddress,atoi(PortBuf)),
  91.             "opening TELNET_SESSION");
  92.     ShowWindow(SW_HIDE);
  93.     }
  94.  
  95. void TConnectTelnetDlg::OnListen(void)
  96.     {
  97.     // listener selected
  98.     char PortBuf[10];
  99.     GetDlgItemText(ID_REMOTEPORT,PortBuf, sizeof(PortBuf)-1);
  100.     // user wants to enable TCP listener
  101.     GetParent()->ShowData(
  102.         GCPopen(TELNET_DAEMON,GetParent()->m_hWnd,M_SENDCALLBACK,0l,NULL,atoi(PortBuf)),
  103.         "opening TELNET_DAEMON");
  104.     ShowWindow(SW_HIDE);
  105.    }
  106.    
  107. // Define CTelnetCommandsDlg, a CDialog constructor
  108. static char *DO="DO";
  109. static char *DONT="DONT";
  110. static char *WILL="WILL";
  111. static char *WONT="WONT";
  112. static char *GO_AHEAD="GO AHEAD";
  113. static char *SUBOPTION="SUBOPTION";
  114. int CTelnetCommandsDlg::CommandsReceived=0; // static init
  115.  
  116. CTelnetCommandsDlg::CTelnetCommandsDlg(PCGcpWnd AParent):
  117.     CDialog("TELNET",AParent)
  118.     {
  119.     // create a modal dialog box
  120.     memset(&Telnet,0,sizeof(Telnet));
  121.     DoModal();
  122.     }
  123.     
  124. BOOL CTelnetCommandsDlg::OnInitDialog(void)
  125.     {
  126.     CDialog::OnInitDialog();
  127.     // fill COMMAND combo with host names
  128.     CComboBox *pCommand=(CComboBox *)GetDlgItem(ID_SCOMMAND);
  129.     pCommand->AddString(DO);
  130.     pCommand->AddString(DONT);
  131.     pCommand->AddString(WILL);
  132.     pCommand->AddString(WONT);
  133.     pCommand->AddString(GO_AHEAD);
  134.     pCommand->AddString(SUBOPTION);
  135.     
  136.     // update fields for Option, Cmd, and SubOption
  137.     char sbuf[100], rbuf[100];
  138.     sprintf(sbuf,"%d TELNET Commands Received",++CommandsReceived);
  139.     SetWindowText(sbuf);
  140.     SetDlgItemText(ID_ROPTION,_itoa(Telnet.Option,rbuf,10));
  141.     SetDlgItemText(ID_SOPTION,_itoa(Telnet.Option,sbuf,10));
  142.     switch (Telnet.Cmd)
  143.         {
  144.         case GO_AHEAD_CMD:
  145.             strcpy(rbuf,GO_AHEAD);
  146.             strcpy(sbuf,"");
  147.             break;
  148.         case DO_CMD:
  149.             strcpy(rbuf,DO);
  150.             strcpy(sbuf,WILL);
  151.             break;
  152.         case DONT_CMD:
  153.             strcpy(rbuf,DONT);
  154.             strcpy(sbuf,WONT);
  155.             break;
  156.         case WILL_CMD:
  157.             strcpy(rbuf,WILL);
  158.             strcpy(sbuf,DO);
  159.             break;
  160.         case WONT_CMD:
  161.             strcpy(rbuf,WONT);
  162.             strcpy(sbuf,DONT);
  163.             break;
  164.         case SB_CMD:
  165.             strcpy(rbuf,SUBOPTION);
  166.             strcpy(sbuf,SUBOPTION);
  167.             break;
  168.         default: 
  169.             strcpy(rbuf,"");
  170.             strcpy(sbuf,"");
  171.             break;
  172.         }
  173.     SetDlgItemText(ID_RCOMMAND,rbuf);
  174.     SetDlgItemText(ID_SCOMMAND,sbuf);
  175.     unsigned char SubOption[256];
  176.     if (Telnet.SubOptionCnt)
  177.         {
  178.         Encode(Telnet.SubOption,
  179.             SubOption,Telnet.SubOptionCnt,256);
  180.         SetDlgItemText(ID_RSUBOPTION,(LPSTR)SubOption);
  181.         }
  182.     else
  183.         SetDlgItemText(ID_RSUBOPTION,"");
  184.     return TRUE;
  185.     }
  186.  
  187. CTelnetCommandsDlg::CTelnetCommandsDlg(PCGcpWnd AParent, GCP_TELNET_PARAMS &TelnetIn):
  188.     CDialog("TELNET",AParent)
  189.     {
  190.     // create a modal dialog box
  191.     Telnet=TelnetIn;    
  192.     DoModal();
  193.     }
  194.  
  195. int CTelnetCommandsDlg::Decode (const unsigned char far *Src, 
  196.     unsigned char far *Dest, int SrcLen, int DestLen)
  197.     { 
  198.     // convert control symbols (^) characters to ASCII
  199.     char Carrot=94;
  200.     unsigned char far *BeginDest=Dest;
  201.     const unsigned char far *BeginSrc=Src;
  202.     while ((Src-BeginSrc) < SrcLen)
  203.         if (*Src == Carrot)
  204.             {
  205.             // found a Carrot, so interpret the 3 numbers after it
  206.             char buf[5];
  207.             _fmemcpy(buf,Src+1,3);
  208.             buf[3]=NULL;
  209.             *Dest=(char)atoi(buf);
  210.             Src+=4;
  211.             Dest++;
  212.             }
  213.         else
  214.             {
  215.             *Dest=*Src;
  216.             Src++;
  217.             Dest++;
  218.             }
  219.     return (Dest-BeginDest);
  220.     }
  221.             
  222.  
  223. int CTelnetCommandsDlg::Encode (const unsigned char far *Src, 
  224.     unsigned char far *Dest, int SrcLen, int DestLen)
  225.     { 
  226.     // convert unprintable characters to (^) characters
  227.     char Carrot=94;
  228.     unsigned char far *BeginDest=Dest;
  229.     const unsigned char far *BeginSrc=Src;
  230.     while ((Src-BeginSrc) < SrcLen && (Dest-BeginDest) < DestLen)
  231.         if (!isprint(*Src))
  232.             {
  233.             char buf[5];
  234.             // not a printable char
  235.             sprintf(buf,"^%03d",*Src);
  236.             _fmemcpy(Dest,buf,4);
  237.             Dest+=4;
  238.             Src++;
  239.             }
  240.         else
  241.             {
  242.             *Dest=*Src;
  243.             Src++;
  244.             Dest++;
  245.             }
  246.     // NULL terminate
  247.     *Dest=NULL;
  248.     return (Dest-BeginDest);
  249.     }
  250.  
  251. void CTelnetCommandsDlg::OnOK(void)
  252.     {
  253.     // SEND button selected
  254.     char Option[20], 
  255.         Cmd[20];
  256.     unsigned char SubOption[300];
  257.     // fill local char arrays
  258.     GetDlgItemText(ID_SOPTION,Option, sizeof(Option)-1);
  259.     GetDlgItemText(ID_SCOMMAND,Cmd, sizeof(Cmd)-1);
  260.     int size=GetDlgItemText(ID_SSUBOPTION,(LPSTR)SubOption, sizeof(SubOption)-1);
  261.     if (size)
  262.         Telnet.SubOptionCnt=Decode(SubOption,Telnet.SubOption,size,256);
  263.     else
  264.         Telnet.SubOptionCnt=0;
  265.      Telnet.Option=LOBYTE(atoi(Option));
  266.      //Telnet.SubOption=NewSubOption;
  267.    if (!strcmp(Cmd,GO_AHEAD))
  268.        Telnet.Cmd=GO_AHEAD_CMD;
  269.    else if (!strcmp(Cmd,DO))
  270.        Telnet.Cmd=DO_CMD;
  271.    else if (!strcmp(Cmd,DONT))
  272.        Telnet.Cmd=DONT_CMD;
  273.    else if (!strcmp(Cmd,WILL))
  274.        Telnet.Cmd=WILL_CMD;
  275.     else if (!strcmp(Cmd,SUBOPTION))
  276.        Telnet.Cmd=SB_CMD;
  277.    else
  278.        Telnet.Cmd=WONT_CMD;
  279.     GetParent()->ShowData(
  280.         GCPdispatch(GetParent()->Status.hSession, GCP_TELNET,(GCP_COMMAND_PARAMS far *)&Telnet),
  281.         "calling GCP_TELNET");
  282.     CDialog::OnOK();
  283.     }
  284.  
  285. // Define TConnectDlg, a CDialog constructor
  286.  
  287. BEGIN_MESSAGE_MAP(TConnectTcpDlg,CDialog)
  288.     ON_BN_CLICKED(ID_LISTEN, OnListen)
  289. END_MESSAGE_MAP()
  290.  
  291. TConnectTcpDlg::TConnectTcpDlg(PCGcpWnd AParent, LPSTR AName)
  292.     {
  293.     // create a modeless dialog box
  294.     Create(AName,AParent);
  295.     
  296.     // fill address combo with host names
  297.     CComboBox *pAddress=(CComboBox *)GetDlgItem(ID_ADDRESS);    
  298.     GCP_QUERY_RESULTS Results;
  299.     if (GCP_OK==GCPquery(NULL, GCP_GET_FIRST_HOST, &Results))
  300.        {
  301.         pAddress->AddString ((char *)Results.Host.Name);
  302.         while (GCP_OK==GCPquery(NULL, GCP_GET_NEXT_HOST, &Results))
  303.             pAddress->AddString ((char *)Results.Host.Name);
  304.         }
  305.     SetDlgItemText(ID_REMOTEPORT,"1");
  306.     }
  307.  
  308. void TConnectTcpDlg::OnListen(void)
  309.     {
  310.     char PortBuf[10];
  311.     GetDlgItemText(ID_REMOTEPORT,PortBuf, sizeof(PortBuf)-1);
  312.     // user wants to enable TCP listener
  313.     GetParent()->ShowData(
  314.         GCPopen(TCP_DAEMON,GetParent()->m_hWnd,M_SENDCALLBACK,0l,NULL,atoi(PortBuf)),
  315.         "opening TCP_DAEMON");
  316.     ShowWindow(SW_HIDE);
  317.      }
  318.  
  319. void TConnectTcpDlg::OnCancel(void)
  320.     {
  321.     // Cancel button selected
  322.     ShowWindow(SW_HIDE);
  323.     }
  324.  
  325. void TConnectTcpDlg::OnOK(void)
  326.     {
  327.     char RemoteAddress[40], PortBuf[10];
  328.     // fill local char arrays
  329.     GetDlgItemText(ID_ADDRESS,RemoteAddress, sizeof(RemoteAddress)-1);
  330.     GetDlgItemText(ID_REMOTEPORT,PortBuf, sizeof(PortBuf)-1);
  331.     if (!*PortBuf)
  332.         MessageBox("No Port Selected","Selection Error",MB_OK);
  333.     else
  334.         GetParent()->ShowData(
  335.             GCPopen(TCP_SESSION,GetParent()->m_hWnd,M_SENDCALLBACK,0l,RemoteAddress,atoi(PortBuf)),
  336.             "opening TCP_SESSION");
  337.     ShowWindow(SW_HIDE);
  338.     }
  339.  
  340. // Define TConnectTftpDlg, a CDialog constructor
  341.  
  342. BEGIN_MESSAGE_MAP(TConnectTftpDlg,CDialog)
  343.     ON_BN_CLICKED(ID_LISTEN, OnListen)
  344. END_MESSAGE_MAP()
  345.  
  346. TConnectTftpDlg::TConnectTftpDlg(PCGcpWnd AParent, LPSTR AName)
  347.     {
  348.     // create a modeless dialog box
  349.     Create(AName,AParent);
  350.     }
  351.  
  352. void TConnectTftpDlg::OnListen(void)
  353.     {
  354.     // daemon button selected
  355.     // user wants to enable TFTP Server
  356.     GetParent()->ShowData(
  357.         GCPopen(TFTP_DAEMON,GetParent()->m_hWnd,M_SENDCALLBACK,0l,NULL,NULL),
  358.         "opening TFTP_DAEMON");
  359.    ShowWindow(SW_HIDE);
  360.     }
  361.  
  362. void TConnectTftpDlg::OnCancel(void)
  363.     {
  364.     // Cancel button selected
  365.     ShowWindow(SW_HIDE);
  366.     }
  367.  
  368. void TConnectTftpDlg::OnOK(void)
  369.     {
  370.     GetParent()->ShowData(
  371.         GCPopen(TFTP_SESSION,GetParent()->m_hWnd,M_SENDCALLBACK,0l,NULL,NULL),
  372.         "opening TFTP_SESSION");
  373.     // done
  374.      ShowWindow(SW_HIDE);
  375.     }
  376.  
  377. // Define TConnectDlg, a CDialog constructor
  378.  
  379. TConnectUdpDlg::TConnectUdpDlg(PCGcpWnd AParent, LPSTR AName)
  380.     {
  381.     // create a modeless dialog box
  382.     Create(AName,AParent);
  383.     SetDlgItemText(ID_LOCALPORT,"1");
  384.     }
  385.  
  386. void TConnectUdpDlg::OnCancel(void)
  387.     {
  388.     // Cancel button selected
  389.     ShowWindow(SW_HIDE);
  390.     }
  391.  
  392. void TConnectUdpDlg::OnOK()
  393.     {
  394.     // OK button selected
  395.     char Port[10];
  396.     // fill local char arrays
  397.     GetDlgItemText(ID_LOCALPORT,Port, sizeof(Port)-1);
  398.     GetParent()->ShowData(
  399.         GCPopen(UDP_SESSION,GetParent()->m_hWnd,M_SENDCALLBACK,0l,NULL,atoi(Port)),
  400.         "opening UDP_SESSION");
  401.     ShowWindow(SW_HIDE);
  402.     }
  403.  
  404. // Define TFileDlg, a CDialog constructor
  405.  
  406. BEGIN_MESSAGE_MAP(TFileDlg,CDialog)
  407.     ON_WM_LBUTTONDOWN()
  408.     ON_BN_CLICKED(ID_GET_FILE, OnGetFile)
  409.     ON_BN_CLICKED(ID_PUT_FILE, OnPutFile)
  410. END_MESSAGE_MAP()
  411.  
  412. TFileDlg::TFileDlg(PCGcpWnd AParent, LPSTR AName)
  413.     {
  414.     // create a modeless dialog box
  415.     Create(AName,AParent);
  416.     
  417.     // fill address combo with host names
  418.     CComboBox *pAddress=(CComboBox *)GetDlgItem(ID_ADDRESS);    
  419.     GCP_QUERY_RESULTS Results;
  420.     if (GCP_OK==GCPquery(NULL, GCP_GET_FIRST_HOST, &Results))
  421.        {
  422.         pAddress->AddString ((char *)Results.Host.Name);
  423.         while (GCP_OK==GCPquery(NULL, GCP_GET_NEXT_HOST, &Results))
  424.             pAddress->AddString ((char *)Results.Host.Name);
  425.         }
  426.     }
  427.  
  428. void TFileDlg::OnCancel(void)
  429.     {
  430.     ShowWindow(SW_HIDE);
  431.     }
  432.  
  433. void TFileDlg::OnGetFile(void)
  434.     {
  435.     GCP_COMMAND_PARAMS Params;
  436.     // Get Button clicked
  437.     GetDlgItemText(ID_LOCAL_FILE,Params.Tftp.LocalFileSpec,sizeof(Params.Tftp.LocalFileSpec)-1);
  438.     GetDlgItemText(ID_REMOTE_FILE,Params.Tftp.RemoteFileSpec,sizeof(Params.Tftp.RemoteFileSpec)-1);
  439.     // fill local char arrays
  440.     GetDlgItemText(ID_ADDRESS,Params.Tftp.RemoteAddress, sizeof(Params.Tftp.RemoteAddress)-1);
  441.     GetParent()->ShowData(
  442.         GCPdispatch(GetParent()->Status.hSession, GCP_GET_TFTP_FILE,&Params),
  443.         "calling GCP_GET_TFTP_FILE");
  444.     }
  445.  
  446. void TFileDlg::OnPutFile(void)
  447.     {
  448.     GCP_COMMAND_PARAMS Params;
  449.     // Send Button clicked
  450.     GetDlgItemText(ID_LOCAL_FILE,Params.Tftp.LocalFileSpec,sizeof(Params.Tftp.LocalFileSpec)-1);
  451.     GetDlgItemText(ID_REMOTE_FILE,Params.Tftp.RemoteFileSpec,sizeof(Params.Tftp.RemoteFileSpec)-1);
  452.     // fill local char arrays
  453.     GetDlgItemText(ID_ADDRESS,Params.Tftp.RemoteAddress, sizeof(Params.Tftp.RemoteAddress)-1);
  454.     // check for Mode
  455.     CButton *pAscii=(CButton *)GetDlgItem(ID_ASCII);    
  456.     if (pAscii->GetCheck())
  457.         Params.Tftp.Mode=NETASCII;
  458.     else
  459.         Params.Tftp.Mode=OCTET; // binary
  460.     GetParent()->ShowData(
  461.         GCPdispatch(GetParent()->Status.hSession, GCP_PUT_TFTP_FILE,&Params),
  462.         "calling GCP_PUT_FILE");
  463.     }
  464.  
  465. // Define TBufferDlg, a CDialog constructor
  466.  
  467. BEGIN_MESSAGE_MAP(TBufferDlg,CDialog)
  468.     ON_WM_LBUTTONDOWN()
  469.     ON_BN_CLICKED (ID_BROADCAST, BroadcastCheck)
  470.     ON_BN_CLICKED (ID_ADDRESS, AddressSelect)
  471. END_MESSAGE_MAP()
  472.  
  473. TBufferDlg::TBufferDlg(PCGcpWnd AParent, LPSTR AName)
  474.     {
  475.     // create a modeless dialog box
  476.     Create(AName,AParent);
  477.     
  478.     // fill address combo with host names
  479.     CComboBox *pAddress=(CComboBox *)GetDlgItem(ID_ADDRESS);    
  480.     GCP_QUERY_RESULTS Results;
  481.     if (GCP_OK==GCPquery(NULL, GCP_GET_FIRST_HOST, &Results))
  482.        {
  483.         pAddress->AddString ((char *)Results.Host.Name);
  484.         while (GCP_OK==GCPquery(NULL, GCP_GET_NEXT_HOST, &Results))
  485.             pAddress->AddString ((char *)Results.Host.Name);
  486.         }
  487.     SetDlgItemText(ID_SENDBUFFER,"GCP++ Makes TCP/IP Programming Fun!");
  488.     }
  489.  
  490. void TBufferDlg::BroadcastCheck()
  491.     {
  492.     // broadcast check selected
  493.     CComboBox *pAddress=(CComboBox *)GetDlgItem(ID_ADDRESS);    
  494.     pAddress->Clear();
  495.    }
  496.  
  497. void TBufferDlg::AddressSelect(void)
  498.     {
  499.     // address selected
  500.     // checked, so NULL out address
  501.     CButton *pBroadcast=(CButton*)GetDlgItem(ID_BROADCAST);
  502.     pBroadcast->SetCheck(0);
  503.    }
  504.  
  505. void TBufferDlg::OnOK(void)
  506.     {
  507.     GCP_COMMAND_PARAMS Params;
  508.     char Buffer[10000];
  509.     char AsciiStr[10], RemoteAddress[50], RemotePortBuf[10];
  510.     char * pCarrot, *pFirstNonDigit;
  511.     char Carrot=94, AsciiChar;
  512.     char EOL[]={13,0};
  513.    int Repetitions, i;
  514.  
  515.     // Send button selected
  516.     // fill local char arrays
  517.     GetDlgItemText(ID_ADDRESS,Params.Buffer.RemoteAddress, sizeof(Params.Buffer.RemoteAddress)-1);
  518.     GetDlgItemText(ID_REMOTEPORT,RemotePortBuf, sizeof(RemotePortBuf)-1);
  519.     Params.Buffer.Cnt=GetDlgItemText(ID_SENDBUFFER,Buffer, sizeof (Buffer)-1);
  520.     GetDlgItemText(ID_SIZE,AsciiStr, sizeof(AsciiStr)-1);
  521.     int OverrideSize=atoi(AsciiStr);
  522.     if (OverrideSize)
  523.         Params.Buffer.Cnt=OverrideSize;
  524.     GetDlgItemText(ID_REPEAT,AsciiStr,sizeof(AsciiStr)-1);
  525.    Repetitions=max(1,atoi(AsciiStr));
  526.     Buffer[Params.Buffer.Cnt]=NULL;
  527.     CButton *pBroadcast=(CButton*)GetDlgItem(ID_BROADCAST);
  528.     if (*RemoteAddress && 1==pBroadcast->GetCheck())
  529.         MessageBox("No Address allowed for Broadcast","Selection Error",MB_OK);
  530.     else
  531.         {
  532.         // fill buffer with cool stuff
  533.         //char Banner[]=" *** GENISYS Comm Pack++ makes TCP/IP apps a snap *** ";
  534.         //strcat(Buffer,Banner);
  535.        // increase buffer size for stress testing
  536.         //Params.Buffer.Size=sizeof(Buffer);
  537.         //if (Parent->Status.AgentType==UDP_AGENT)
  538.             //Params.Buffer.Size=1000;  // so we don't force a WSAEMSGSIZE error
  539.         //Buffer[4999]=NULL;
  540.         Params.Buffer.Ptr=(BYTE far *)Buffer;
  541.         Buffer[Params.Buffer.Cnt]=NULL;
  542.         // put in port for use by UDP agent type
  543.         Params.Buffer.RemotePort=atoi(RemotePortBuf);
  544.         if (GetParent()->Status.SessionType==TELNET_SESSION)
  545.             {
  546.             strcat(Buffer,EOL);
  547.             Params.Buffer.Cnt+=sizeof(EOL);
  548.          }
  549.         //if (GetParent()->Status.AgentType==UDP_AGENT)
  550.         //    {
  551.             // put in address for use by UDP agent type
  552.         //    Params.Buffer.pAddress=RemoteAddress;
  553.             // put in port for use by UDP agent type
  554.         //    Params.Buffer.Port=atoi(RemotePortBuf);
  555.        //  }
  556.         // convert control symbols (^) characters to ASCII
  557.         pCarrot=Buffer;
  558.         while (pCarrot=strchr(pCarrot,Carrot))
  559.             {
  560.             // found a Carrot, so interpret the numbers after it
  561.             pFirstNonDigit=pCarrot;
  562.             // scan until *pCarrot is not a digit
  563.             while (isdigit(*++pFirstNonDigit));
  564.             // convert string to char
  565.             AsciiChar=(char)atoi(pCarrot+1);
  566.             // move data down
  567.             *pCarrot=AsciiChar;
  568.             memmove (pCarrot+1,pFirstNonDigit,&Buffer[4999]-pFirstNonDigit);
  569.             }
  570.         for (i=0; i<Repetitions; i++)
  571.             if (GetParent()->Status.SessionType==UDP_SESSION)
  572.                 GetParent()->ShowData(
  573.                     GCPdispatch (GetParent()->Status.hSession,GCP_SEND_DATAGRAM,&Params),
  574.                     "calling GCP_SEND_DATAGRAM");
  575.             else
  576.                 GetParent()->ShowData(
  577.                     GCPdispatch (GetParent()->Status.hSession,GCP_SEND,&Params),
  578.                     "calling GCP_SEND");
  579.         // reset control to left
  580.         CEdit *pBufferOut=(CEdit*)GetDlgItem(ID_SENDBUFFER);
  581.         pBufferOut->SetSel(0,0);
  582.         }
  583.     }
  584.  
  585. void TBufferDlg::OnCancel()
  586.     {
  587.     // Cancel button selected
  588.    ShowWindow(SW_HIDE);
  589.     }
  590.  
  591. // Define THostNameTableDlg, a CDialog constructor
  592.  
  593. THostTableDlg::THostTableDlg(PCGcpWnd AParent, LPSTR AName):
  594.                         CDialog(AName, AParent)
  595.     {
  596.     }
  597.  
  598. BOOL THostTableDlg::OnInitDialog()
  599.     {
  600.     char    Buffer[120];
  601.  
  602.     CDialog::OnInitDialog();
  603.     // fill address combo with host names
  604.     CListBox *pAddress=(CListBox *)GetDlgItem(ID_ADDRESS);    
  605.     GCP_QUERY_RESULTS Results;
  606.     if (GCP_OK==GCPquery(NULL, GCP_GET_FIRST_HOST, &Results))
  607.         {
  608.         sprintf (Buffer,"%-16s \t%s",Results.Host.Name,Results.Host.Addr);
  609.         pAddress->AddString (Buffer);
  610.         while (GCP_OK==GCPquery(NULL, GCP_GET_NEXT_HOST, &Results))
  611.             {
  612.             sprintf (Buffer,"%-16s \t%s",Results.Host.Name,Results.Host.Addr);
  613.             pAddress->AddString (Buffer);
  614.             }
  615.         }
  616.    return TRUE;
  617.     }
  618.  
  619. // Define TAboutDlg, a CDialog constructor
  620.  
  621. BEGIN_MESSAGE_MAP(CAboutDlg,CDialog)
  622.     ON_WM_LBUTTONDOWN()
  623. END_MESSAGE_MAP()
  624.  
  625. CAboutDlg::CAboutDlg(PCGcpWnd AParent, LPSTR AName):
  626.                         CDialog(AName, AParent)
  627.     {
  628.     }
  629.  
  630. void CAboutDlg::OnLButtonDown (UINT uint, CPoint Point)
  631.     {
  632.     // user clicked on window
  633.     OnOK();
  634.    }
  635.  
  636. /****************************************************
  637.  * CGcpWnd implementations:
  638.  ****************************************************/ 
  639.  
  640.  
  641.  // define message map for CGcpWnd
  642. BEGIN_MESSAGE_MAP(CGcpWnd,CFrameWnd)
  643.     ON_WM_SIZE()
  644.     ON_WM_CLOSE()
  645.     ON_MESSAGE(M_SENDCALLBACK, SendCallback)
  646.     ON_MESSAGE(M_POSTCALLBACK, PostCallback)
  647.     ON_COMMAND(IDM_SHOW, OnShow)
  648.     ON_COMMAND(IDM_ABOUT, OnAbout)
  649.     ON_COMMAND(IDM_HIDE, OnHide)
  650.     ON_COMMAND(IDM_TELNET_CMD, OnTelnetCmd)
  651.     ON_COMMAND(IDM_TELNET, OnTelnet)
  652.     ON_COMMAND(IDM_TCP, OnTcp)
  653.     ON_COMMAND(IDM_TFTP, OnTftp)
  654.     ON_COMMAND(IDM_UDP, OnUdp)
  655.     ON_COMMAND(IDM_BUFFER, OnBuffer)
  656.     ON_COMMAND(IDM_FILE, OnFile)
  657.     ON_COMMAND(IDM_CLOSE, OnAgentClose)
  658.     ON_COMMAND(IDM_ABORT, OnAgentAbort)
  659.     ON_COMMAND(IDM_HOSTTABLE, OnHostTable)
  660.     ON_COMMAND(IDM_LOCALHOST, OnLocalHost)
  661.     ON_COMMAND(IDM_INDEX, OnIndex)
  662. END_MESSAGE_MAP()
  663.  
  664. // Define CGcpWnd, a TWindow constructor
  665. CGcpWnd::CGcpWnd()
  666.     {
  667.     Status.hSession=NULL;
  668.     Closing=FALSE;
  669.     Create (NULL, "GCP Client Ready", 
  670.         WS_OVERLAPPEDWINDOW, rectDefault,
  671.         NULL,"GCP_CLIENT"); 
  672.     // create the edit window too
  673.     RECT Rect={0,0,0,0};
  674.     EditWnd.Create (WS_CHILD|WS_VISIBLE|ES_MULTILINE|
  675.         ES_NOHIDESEL|WS_VSCROLL|WS_HSCROLL|ES_AUTOVSCROLL,
  676.         Rect, this, 1);
  677.     EditWnd.SetReadOnly();
  678.     pConnectTcpDlg=new TConnectTcpDlg(this, "CONNECT_TCP");
  679.     pConnectUdpDlg=new TConnectUdpDlg(this, "CONNECT_UDP");
  680.     pConnectTftpDlg=new TConnectTftpDlg(this, "CONNECT_TFTP");
  681.     pConnectTelnetDlg=new TConnectTelnetDlg(this, "CONNECT_TELNET");
  682.     pBufferDlg=new TBufferDlg(this, "BUFFER");
  683.     pFileDlg=new TFileDlg(this, "FILE");
  684.     // set manual close while testing
  685.    OnMenuSelect(IDM_MANUALCLOSE,NULL,NULL);
  686.     ShowWindow (SW_SHOWNORMAL);
  687.     }
  688.  
  689. CGcpWnd::~CGcpWnd()
  690.     {
  691.     delete pConnectTcpDlg;
  692.     delete pConnectUdpDlg;
  693.     delete pConnectTftpDlg;
  694.     delete pConnectTelnetDlg;
  695.     delete pBufferDlg;
  696.     delete pFileDlg;
  697.     }
  698.  
  699. void CGcpWnd::OnSize(UINT ntype, int cx, int cy)
  700.     {
  701.     if (ntype==SIZE_RESTORED || ntype==SIZE_MAXIMIZED)
  702.         EditWnd.MoveWindow(0,0,cx,cy,TRUE);
  703.     }
  704.     
  705. void CGcpWnd::OnShow(void)
  706.     {
  707.     GCP_ERROR err=GCPdispatch(NULL, GCP_SHOW, NULL);
  708.     ShowData(err, "Show GCP Server");
  709.     }
  710.     
  711. void CGcpWnd::OnHide (void)
  712.     {
  713.     GCP_ERROR err=GCPdispatch(NULL, GCP_HIDE, NULL);
  714.     ShowData(err, "Hide GCP Server");
  715.     }
  716.     
  717. void CGcpWnd::OnTelnet (void)
  718.     {
  719.     pConnectTelnetDlg->BringWindowToTop();
  720.     pConnectTelnetDlg->ShowWindow(SW_SHOWNORMAL);
  721.     }
  722.     
  723. void CGcpWnd::OnTcp (void)
  724.     {
  725.     pConnectTcpDlg->BringWindowToTop();
  726.     pConnectTcpDlg->ShowWindow(SW_SHOWNORMAL);
  727.     }
  728.     
  729. void CGcpWnd::OnTftp (void)
  730.     {
  731.     pConnectTftpDlg->BringWindowToTop();
  732.     pConnectTftpDlg->ShowWindow(SW_SHOWNORMAL);
  733.     }
  734.     
  735. void CGcpWnd::OnUdp (void)
  736.     {
  737.     pConnectUdpDlg->BringWindowToTop();
  738.     pConnectUdpDlg->ShowWindow(SW_SHOWNORMAL);
  739.     }
  740.     
  741. void CGcpWnd::OnBuffer (void)
  742.     {
  743.     pBufferDlg->BringWindowToTop();
  744.     pBufferDlg->ShowWindow(SW_SHOWNORMAL);
  745.     }
  746.     
  747. void CGcpWnd::OnFile (void)
  748.     {
  749.     pFileDlg->BringWindowToTop();
  750.     pFileDlg->ShowWindow(SW_SHOWNORMAL);
  751.     }
  752.  
  753. void CGcpWnd::OnAgentClose (void)
  754.     {
  755.     ShowData(
  756.         GCPclose (Status.hSession,FALSE),
  757.         "closing session");
  758.     }
  759.  
  760. void CGcpWnd::OnAgentAbort (void)
  761.     {
  762.     ShowData(
  763.         GCPclose (Status.hSession,TRUE),
  764.         "closing session");
  765.     }
  766.     
  767. void CGcpWnd::OnHostTable (void)
  768.     {
  769.     THostTableDlg *ptr=new THostTableDlg(this, "HostNameTable");
  770.     ptr->DoModal();
  771.     }
  772.     
  773. void CGcpWnd::OnLocalHost (void)
  774.     {
  775.     GCP_QUERY_RESULTS Results;
  776.     GCPquery(NULL,GCP_GET_LOCAL_HOST,&Results);
  777.     MessageBox(Results.Host.Name,Results.Host.Addr,MB_OK);
  778.     }
  779.     
  780. void CGcpWnd::OnIndex (void)
  781.     {
  782.     WinExec("winhelp.exe gcp_clnt.hlp",SW_SHOWNORMAL);
  783.     }
  784.  
  785. void CGcpWnd::OnTelnetCmd (void)
  786.     {
  787.     new CTelnetCommandsDlg(this);
  788.     }
  789.  
  790. void CGcpWnd::OnAbout (void)
  791.     {
  792.     CAboutDlg *ptr=new CAboutDlg(this, "ABOUT");
  793.     ptr->DoModal();
  794.     }
  795.  
  796. void CGcpWnd::OnClose (void)
  797.     {
  798.     OnAbout();
  799.     if (!Status.hSession || Closing)
  800.         DestroyWindow();
  801.     else
  802.         {
  803.         Closing=TRUE;
  804.         if (GCPclose (Status.hSession,TRUE))
  805.             DestroyWindow();
  806.         }
  807.    }
  808.     
  809. LONG CGcpWnd::SendCallback(WPARAM wParam, LPARAM lParam)
  810.     {
  811.     // SendMessage arrives from GCP server...copy and use PostMessage
  812.     // ensure GCPfree is used after processing PostMessage
  813.     PostMessage(M_POSTCALLBACK,wParam,GCPalloc(wParam, lParam));
  814.     return 1l; 
  815.     }
  816.     
  817. LONG CGcpWnd::PostCallback(WPARAM wParam, LPARAM lParam)
  818.     {
  819.     GCP_COMMAND Msg=(GCP_COMMAND)wParam;
  820.     Status=*(LPGCP_STATUS)lParam;
  821.     
  822.     GCP_QUERY_RESULTS Results;
  823.     if (Msg!=GCP_CLOSED)
  824.         GCPquery (Status.hSession, GCP_GET_STATUS, &Results);
  825.     ShowData(Msg, Status.SessionType);
  826.    // do special processing according to message
  827.     switch(Msg)
  828.         {
  829.         case GCP_CLOSED:
  830.             Status.hSession=NULL;
  831.             GcpApp.m_pMainWnd->SetWindowText ("Session Closed!");
  832.             GcpApp.m_pMainWnd->DrawMenuBar ();
  833.             if (Closing)
  834.                 CFrameWnd::OnClose();
  835.             break;
  836.         case GCP_OPENED:
  837.             if (Status.hSession)
  838.              {
  839.                 // disable Connect Menu
  840.                 //EnableMenuItem(GetMenu(m_hWnd),0,MF_BYPOSITION | MF_GRAYED);
  841.                 // enable Command Menu
  842.                 //EnableMenuItem(GetMenu(m_hWnd),1,MF_BYPOSITION | MF_ENABLED);
  843.                 SetWindowText ("Session Open!");
  844.                 DrawMenuBar ();
  845.                 }
  846.          break;
  847.         default:
  848.             ;
  849.         } 
  850.     //free memory
  851.     BOOL success=GCPfree(lParam);
  852.     return 1l;
  853.     }
  854.  
  855. void CGcpWnd::TranslateMessage(char *theOutput, GCP_COMMAND MsgType)
  856. {
  857.     switch (MsgType)
  858.     {
  859.         case GCP_OPENED:
  860.             strcpy (theOutput, "GCP_OPENED");
  861.             break;
  862.         case GCP_SEND:
  863.             strcpy (theOutput, "GCP_SEND");
  864.             break;
  865.         case GCP_RECV:
  866.             strcpy (theOutput, "GCP_RECV");
  867.             break;
  868.         case GCP_SEND_DATAGRAM:
  869.             strcpy (theOutput, "GCP_SEND_DATAGRAM");
  870.             break;
  871.         case GCP_RECV_DATAGRAM:
  872.             strcpy (theOutput, "GCP_RECV_DATAGRAM");
  873.             break;
  874.         case GCP_PUT_TFTP_FILE:
  875.             strcpy (theOutput, "GCP_PUT_TFTP_FILE");
  876.             break;
  877.         case GCP_GET_TFTP_FILE:
  878.             strcpy (theOutput, "GCP_GET_TFTP_FILE");
  879.             break;
  880.         case GCP_CLOSED:
  881.             strcpy (theOutput, "GCP_CLOSED");
  882.             break;
  883.         case GCP_TELNET:
  884.             strcpy (theOutput, "GCP_TELNET");
  885.          break;
  886.         default:
  887.             strcpy (theOutput, "------ Undefined GCP Message ------");
  888.     }
  889. }
  890.  
  891. void CGcpWnd::TranslateType(char *theOutput, GCP_SESSION_TYPE SessionType)
  892.     {
  893.     switch (SessionType)
  894.         {
  895.         case TCP_SESSION:
  896.             strcpy (theOutput, "TCP_SESSION");
  897.             break;
  898.         case UDP_SESSION:
  899.             strcpy (theOutput, "UDP_SESSION");
  900.             break;
  901.         case TELNET_SESSION:
  902.             strcpy (theOutput, "TELNET_SESSION");
  903.             break;
  904.         case TFTP_SESSION:
  905.             strcpy (theOutput, "TFTP_SESSION");
  906.             break;
  907.         case TCP_DAEMON:
  908.             strcpy (theOutput, "TCP_DAEMON");
  909.             break;
  910.         case TELNET_DAEMON:
  911.             strcpy (theOutput, "TELNET_DAEMON");
  912.             break;
  913.         case TFTP_DAEMON:
  914.             strcpy (theOutput, "TFTP_DAEMON");
  915.             break;
  916.         default:
  917.             strcpy (theOutput, "------ Undefined GCP Session Type ------");
  918.         }
  919.     }
  920.  
  921. GCP_ERROR CGcpWnd::ShowData(GCP_ERROR ReturnValue, LPSTR DisplayString)
  922.     {
  923.     char outputString[200];
  924.     char errbuf[100];
  925.  
  926.     if (ReturnValue)
  927.         {
  928.         FormatError(ReturnValue,errbuf);
  929.         sprintf(outputString, "    Calling %s caused an ERROR: ",DisplayString);
  930.         strcat(outputString,errbuf);
  931.         }
  932.     else
  933.         sprintf(outputString, "    %s", DisplayString);
  934.     Append (outputString);
  935.     return ReturnValue;
  936.     }
  937.  
  938. void CGcpWnd::FormatError (GCP_ERROR Error, char * errbuf)
  939.     {
  940.     _fstrcpy (errbuf,GCPperror(Error));
  941.     }
  942.  
  943. void CGcpWnd::ShowData(GCP_COMMAND Message, GCP_SESSION_TYPE Type)
  944. {
  945.     char outputString[120];
  946.     char szMessage[40];
  947.     char szType[40];
  948.  
  949.     TranslateMessage(szMessage, Message);
  950.     TranslateType(szType, Type);
  951.     if (Status.hSession)
  952.         sprintf (outputString,
  953.             "\r\n%s message from %s #%u",
  954.              szMessage, szType, Status.hSession);
  955.     else
  956.         sprintf (outputString,"\r\nFailure attempting GCPopen");
  957.     Append (outputString);
  958.  
  959.     // Now output the data
  960.     switch (Message)
  961.        {
  962.         case GCP_SEND:
  963.             sprintf(outputString,"    %d bytes sent",
  964.                 Status.Params.Buffer.Cnt);
  965.             Append (outputString);
  966.             break;
  967.         case GCP_SEND_DATAGRAM:
  968.             sprintf(outputString,"    %d bytes sent to %s, port %d",
  969.                 Status.Params.Buffer.Cnt, Status.Params.Buffer.RemoteAddress, 
  970.                 Status.Params.Buffer.RemotePort);
  971.             Append (outputString);
  972.             break;
  973.         case GCP_RECV:
  974.             sprintf(outputString,"    %d bytes received: ",
  975.                 Status.Params.Buffer.Cnt);
  976.             _fstrncat(outputString,(LPSTR)Status.Params.Buffer.Ptr,min(80,Status.Params.Buffer.Cnt));
  977.             Append (outputString);
  978.             break;
  979.         case GCP_RECV_DATAGRAM:
  980.             sprintf(outputString,"    %d bytes received from %s, port %d: ",
  981.                 Status.Params.Buffer.Cnt, Status.Params.Buffer.RemoteAddress, 
  982.                 Status.Params.Buffer.RemotePort);
  983.             _fstrncat(outputString,(LPSTR)Status.Params.Buffer.Ptr,min(80,Status.Params.Buffer.Cnt));
  984.             Append (outputString);
  985.             break;
  986.         case GCP_PUT_TFTP_FILE:
  987.             sprintf(outputString,"    FileSpec: ");
  988.             _fstrncat(outputString,Status.Params.Tftp.LocalFileSpec,80);
  989.             Append (outputString);
  990.             break;
  991.         case GCP_GET_TFTP_FILE:
  992.             sprintf(outputString,"    FileSpec: ");
  993.             _fstrncat(outputString,Status.Params.Tftp.LocalFileSpec,80);
  994.             Append (outputString);
  995.             break;
  996.         case GCP_TELNET:
  997.             // create modal dialog box for display of information
  998.             new CTelnetCommandsDlg(this,Status.Params.Telnet);
  999.             break;
  1000.         default:
  1001.             ;
  1002.         }
  1003.  
  1004.     // Bytes transeferred
  1005.     if (Status.Stats.InCnt)
  1006.        {
  1007.         sprintf(outputString, "    %ld cummulative bytes received averaging %ld bytes/sec",
  1008.             Status.Stats.InCnt,Status.Stats.InRate);
  1009.         Append (outputString);
  1010.       }
  1011.     if (Status.Stats.OutCnt)
  1012.        {
  1013.         sprintf(outputString, "    %ld cummulative bytes sent averaging %ld bytes/sec",
  1014.             Status.Stats.OutCnt,
  1015.             Status.Stats.OutRate);
  1016.         Append (outputString);
  1017.       }
  1018.  
  1019.     // Error Code
  1020.     if (Status.Error)
  1021.         {
  1022.         char errbuf[100];
  1023.         FormatError(Status.Error,errbuf);
  1024.         sprintf(outputString, "    %s for Session %d",errbuf,Status.hSession);
  1025.         Append (outputString);
  1026.         }
  1027.     }
  1028.  
  1029. void CGcpWnd::Append (LPSTR String)
  1030.     {
  1031.     int Lines=EditWnd.GetLineCount();
  1032.     if (Lines>400)
  1033.        EditWnd.SetWindowText ("");
  1034.     EditWnd.SetSel (32000, 32000);
  1035.     EditWnd.ReplaceSel (String);
  1036.     EditWnd.ReplaceSel ((LPSTR)"\r\n");
  1037.     }
  1038.