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
Wrap
Text File
|
1994-04-02
|
28KB
|
1,038 lines
/********************************************************************
* *
* Source File: GCP_CLNT.cpp *
* Date: Fri Feb 28 14:56:07 1992 *
* *
********************************************************************/
#define STRICT
#pragma warning (disable:4706 4355)
#include <afx.h>
#include <afxwin.h>
#include <afxext.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "..\include\gcp.h"
#include "..\include\defs.h"
#include "resource.h"
#include "gcp_clnt.hpp"
#define new DEBUG_NEW
#define M_SENDCALLBACK WM_USER+100
#define M_POSTCALLBACK WM_USER+101
/****************************************************
* CGcpWnd implementations:
****************************************************/
CGcpApp GcpApp; // application instance
CGcpWnd *pGcpWnd;
CGcpApp::CGcpApp() : CWinApp("GCP")
{
}
BOOL CGcpApp::InitInstance()
{
m_pMainWnd=pGcpWnd=new CGcpWnd();
return TRUE;
}
CGcpApp::~CGcpApp()
{
}
// Define TConnectDlg, a CDialog constructor
BEGIN_MESSAGE_MAP(TConnectTelnetDlg,CDialog)
ON_BN_CLICKED(ID_LISTEN, OnListen)
END_MESSAGE_MAP()
TConnectTelnetDlg::TConnectTelnetDlg(PCGcpWnd AParent, LPSTR AName)
{
// create a modeless dialog box
Create(AName,AParent);
// fill address combo with host names
CComboBox *pAddress=(CComboBox *)GetDlgItem(ID_ADDRESS);
GCP_QUERY_RESULTS Results;
if (GCP_OK==GCPquery(NULL, GCP_GET_FIRST_HOST, &Results))
{
pAddress->AddString ((char *)Results.Host.Name);
while (GCP_OK==GCPquery(NULL, GCP_GET_NEXT_HOST, &Results))
pAddress->AddString ((char *)Results.Host.Name);
}
SetDlgItemText(ID_REMOTEPORT,"23");
}
void TConnectTelnetDlg::OnCancel(void)
{
// Cancel button selected
ShowWindow(SW_HIDE);
}
void TConnectTelnetDlg::OnOK(void)
{
// OK button selected
char RemoteAddress[40], PortBuf[10];
// fill local char arrays
GetDlgItemText(ID_ADDRESS,RemoteAddress, sizeof(RemoteAddress)-1);
GetDlgItemText(ID_REMOTEPORT,PortBuf, sizeof(PortBuf)-1);
if (!*RemoteAddress)
MessageBox("No Address Selected","Selection Error",MB_OK);
else if (!*PortBuf)
MessageBox("No Port Selected","Selection Error",MB_OK);
else
GetParent()->ShowData(
GCPopen(TELNET_SESSION, GetParent()->m_hWnd,M_SENDCALLBACK,0l,RemoteAddress,atoi(PortBuf)),
"opening TELNET_SESSION");
ShowWindow(SW_HIDE);
}
void TConnectTelnetDlg::OnListen(void)
{
// listener selected
char PortBuf[10];
GetDlgItemText(ID_REMOTEPORT,PortBuf, sizeof(PortBuf)-1);
// user wants to enable TCP listener
GetParent()->ShowData(
GCPopen(TELNET_DAEMON,GetParent()->m_hWnd,M_SENDCALLBACK,0l,NULL,atoi(PortBuf)),
"opening TELNET_DAEMON");
ShowWindow(SW_HIDE);
}
// Define CTelnetCommandsDlg, a CDialog constructor
static char *DO="DO";
static char *DONT="DONT";
static char *WILL="WILL";
static char *WONT="WONT";
static char *GO_AHEAD="GO AHEAD";
static char *SUBOPTION="SUBOPTION";
int CTelnetCommandsDlg::CommandsReceived=0; // static init
CTelnetCommandsDlg::CTelnetCommandsDlg(PCGcpWnd AParent):
CDialog("TELNET",AParent)
{
// create a modal dialog box
memset(&Telnet,0,sizeof(Telnet));
DoModal();
}
BOOL CTelnetCommandsDlg::OnInitDialog(void)
{
CDialog::OnInitDialog();
// fill COMMAND combo with host names
CComboBox *pCommand=(CComboBox *)GetDlgItem(ID_SCOMMAND);
pCommand->AddString(DO);
pCommand->AddString(DONT);
pCommand->AddString(WILL);
pCommand->AddString(WONT);
pCommand->AddString(GO_AHEAD);
pCommand->AddString(SUBOPTION);
// update fields for Option, Cmd, and SubOption
char sbuf[100], rbuf[100];
sprintf(sbuf,"%d TELNET Commands Received",++CommandsReceived);
SetWindowText(sbuf);
SetDlgItemText(ID_ROPTION,_itoa(Telnet.Option,rbuf,10));
SetDlgItemText(ID_SOPTION,_itoa(Telnet.Option,sbuf,10));
switch (Telnet.Cmd)
{
case GO_AHEAD_CMD:
strcpy(rbuf,GO_AHEAD);
strcpy(sbuf,"");
break;
case DO_CMD:
strcpy(rbuf,DO);
strcpy(sbuf,WILL);
break;
case DONT_CMD:
strcpy(rbuf,DONT);
strcpy(sbuf,WONT);
break;
case WILL_CMD:
strcpy(rbuf,WILL);
strcpy(sbuf,DO);
break;
case WONT_CMD:
strcpy(rbuf,WONT);
strcpy(sbuf,DONT);
break;
case SB_CMD:
strcpy(rbuf,SUBOPTION);
strcpy(sbuf,SUBOPTION);
break;
default:
strcpy(rbuf,"");
strcpy(sbuf,"");
break;
}
SetDlgItemText(ID_RCOMMAND,rbuf);
SetDlgItemText(ID_SCOMMAND,sbuf);
unsigned char SubOption[256];
if (Telnet.SubOptionCnt)
{
Encode(Telnet.SubOption,
SubOption,Telnet.SubOptionCnt,256);
SetDlgItemText(ID_RSUBOPTION,(LPSTR)SubOption);
}
else
SetDlgItemText(ID_RSUBOPTION,"");
return TRUE;
}
CTelnetCommandsDlg::CTelnetCommandsDlg(PCGcpWnd AParent, GCP_TELNET_PARAMS &TelnetIn):
CDialog("TELNET",AParent)
{
// create a modal dialog box
Telnet=TelnetIn;
DoModal();
}
int CTelnetCommandsDlg::Decode (const unsigned char far *Src,
unsigned char far *Dest, int SrcLen, int DestLen)
{
// convert control symbols (^) characters to ASCII
char Carrot=94;
unsigned char far *BeginDest=Dest;
const unsigned char far *BeginSrc=Src;
while ((Src-BeginSrc) < SrcLen)
if (*Src == Carrot)
{
// found a Carrot, so interpret the 3 numbers after it
char buf[5];
_fmemcpy(buf,Src+1,3);
buf[3]=NULL;
*Dest=(char)atoi(buf);
Src+=4;
Dest++;
}
else
{
*Dest=*Src;
Src++;
Dest++;
}
return (Dest-BeginDest);
}
int CTelnetCommandsDlg::Encode (const unsigned char far *Src,
unsigned char far *Dest, int SrcLen, int DestLen)
{
// convert unprintable characters to (^) characters
char Carrot=94;
unsigned char far *BeginDest=Dest;
const unsigned char far *BeginSrc=Src;
while ((Src-BeginSrc) < SrcLen && (Dest-BeginDest) < DestLen)
if (!isprint(*Src))
{
char buf[5];
// not a printable char
sprintf(buf,"^%03d",*Src);
_fmemcpy(Dest,buf,4);
Dest+=4;
Src++;
}
else
{
*Dest=*Src;
Src++;
Dest++;
}
// NULL terminate
*Dest=NULL;
return (Dest-BeginDest);
}
void CTelnetCommandsDlg::OnOK(void)
{
// SEND button selected
char Option[20],
Cmd[20];
unsigned char SubOption[300];
// fill local char arrays
GetDlgItemText(ID_SOPTION,Option, sizeof(Option)-1);
GetDlgItemText(ID_SCOMMAND,Cmd, sizeof(Cmd)-1);
int size=GetDlgItemText(ID_SSUBOPTION,(LPSTR)SubOption, sizeof(SubOption)-1);
if (size)
Telnet.SubOptionCnt=Decode(SubOption,Telnet.SubOption,size,256);
else
Telnet.SubOptionCnt=0;
Telnet.Option=LOBYTE(atoi(Option));
//Telnet.SubOption=NewSubOption;
if (!strcmp(Cmd,GO_AHEAD))
Telnet.Cmd=GO_AHEAD_CMD;
else if (!strcmp(Cmd,DO))
Telnet.Cmd=DO_CMD;
else if (!strcmp(Cmd,DONT))
Telnet.Cmd=DONT_CMD;
else if (!strcmp(Cmd,WILL))
Telnet.Cmd=WILL_CMD;
else if (!strcmp(Cmd,SUBOPTION))
Telnet.Cmd=SB_CMD;
else
Telnet.Cmd=WONT_CMD;
GetParent()->ShowData(
GCPdispatch(GetParent()->Status.hSession, GCP_TELNET,(GCP_COMMAND_PARAMS far *)&Telnet),
"calling GCP_TELNET");
CDialog::OnOK();
}
// Define TConnectDlg, a CDialog constructor
BEGIN_MESSAGE_MAP(TConnectTcpDlg,CDialog)
ON_BN_CLICKED(ID_LISTEN, OnListen)
END_MESSAGE_MAP()
TConnectTcpDlg::TConnectTcpDlg(PCGcpWnd AParent, LPSTR AName)
{
// create a modeless dialog box
Create(AName,AParent);
// fill address combo with host names
CComboBox *pAddress=(CComboBox *)GetDlgItem(ID_ADDRESS);
GCP_QUERY_RESULTS Results;
if (GCP_OK==GCPquery(NULL, GCP_GET_FIRST_HOST, &Results))
{
pAddress->AddString ((char *)Results.Host.Name);
while (GCP_OK==GCPquery(NULL, GCP_GET_NEXT_HOST, &Results))
pAddress->AddString ((char *)Results.Host.Name);
}
SetDlgItemText(ID_REMOTEPORT,"1");
}
void TConnectTcpDlg::OnListen(void)
{
char PortBuf[10];
GetDlgItemText(ID_REMOTEPORT,PortBuf, sizeof(PortBuf)-1);
// user wants to enable TCP listener
GetParent()->ShowData(
GCPopen(TCP_DAEMON,GetParent()->m_hWnd,M_SENDCALLBACK,0l,NULL,atoi(PortBuf)),
"opening TCP_DAEMON");
ShowWindow(SW_HIDE);
}
void TConnectTcpDlg::OnCancel(void)
{
// Cancel button selected
ShowWindow(SW_HIDE);
}
void TConnectTcpDlg::OnOK(void)
{
char RemoteAddress[40], PortBuf[10];
// fill local char arrays
GetDlgItemText(ID_ADDRESS,RemoteAddress, sizeof(RemoteAddress)-1);
GetDlgItemText(ID_REMOTEPORT,PortBuf, sizeof(PortBuf)-1);
if (!*PortBuf)
MessageBox("No Port Selected","Selection Error",MB_OK);
else
GetParent()->ShowData(
GCPopen(TCP_SESSION,GetParent()->m_hWnd,M_SENDCALLBACK,0l,RemoteAddress,atoi(PortBuf)),
"opening TCP_SESSION");
ShowWindow(SW_HIDE);
}
// Define TConnectTftpDlg, a CDialog constructor
BEGIN_MESSAGE_MAP(TConnectTftpDlg,CDialog)
ON_BN_CLICKED(ID_LISTEN, OnListen)
END_MESSAGE_MAP()
TConnectTftpDlg::TConnectTftpDlg(PCGcpWnd AParent, LPSTR AName)
{
// create a modeless dialog box
Create(AName,AParent);
}
void TConnectTftpDlg::OnListen(void)
{
// daemon button selected
// user wants to enable TFTP Server
GetParent()->ShowData(
GCPopen(TFTP_DAEMON,GetParent()->m_hWnd,M_SENDCALLBACK,0l,NULL,NULL),
"opening TFTP_DAEMON");
ShowWindow(SW_HIDE);
}
void TConnectTftpDlg::OnCancel(void)
{
// Cancel button selected
ShowWindow(SW_HIDE);
}
void TConnectTftpDlg::OnOK(void)
{
GetParent()->ShowData(
GCPopen(TFTP_SESSION,GetParent()->m_hWnd,M_SENDCALLBACK,0l,NULL,NULL),
"opening TFTP_SESSION");
// done
ShowWindow(SW_HIDE);
}
// Define TConnectDlg, a CDialog constructor
TConnectUdpDlg::TConnectUdpDlg(PCGcpWnd AParent, LPSTR AName)
{
// create a modeless dialog box
Create(AName,AParent);
SetDlgItemText(ID_LOCALPORT,"1");
}
void TConnectUdpDlg::OnCancel(void)
{
// Cancel button selected
ShowWindow(SW_HIDE);
}
void TConnectUdpDlg::OnOK()
{
// OK button selected
char Port[10];
// fill local char arrays
GetDlgItemText(ID_LOCALPORT,Port, sizeof(Port)-1);
GetParent()->ShowData(
GCPopen(UDP_SESSION,GetParent()->m_hWnd,M_SENDCALLBACK,0l,NULL,atoi(Port)),
"opening UDP_SESSION");
ShowWindow(SW_HIDE);
}
// Define TFileDlg, a CDialog constructor
BEGIN_MESSAGE_MAP(TFileDlg,CDialog)
ON_WM_LBUTTONDOWN()
ON_BN_CLICKED(ID_GET_FILE, OnGetFile)
ON_BN_CLICKED(ID_PUT_FILE, OnPutFile)
END_MESSAGE_MAP()
TFileDlg::TFileDlg(PCGcpWnd AParent, LPSTR AName)
{
// create a modeless dialog box
Create(AName,AParent);
// fill address combo with host names
CComboBox *pAddress=(CComboBox *)GetDlgItem(ID_ADDRESS);
GCP_QUERY_RESULTS Results;
if (GCP_OK==GCPquery(NULL, GCP_GET_FIRST_HOST, &Results))
{
pAddress->AddString ((char *)Results.Host.Name);
while (GCP_OK==GCPquery(NULL, GCP_GET_NEXT_HOST, &Results))
pAddress->AddString ((char *)Results.Host.Name);
}
}
void TFileDlg::OnCancel(void)
{
ShowWindow(SW_HIDE);
}
void TFileDlg::OnGetFile(void)
{
GCP_COMMAND_PARAMS Params;
// Get Button clicked
GetDlgItemText(ID_LOCAL_FILE,Params.Tftp.LocalFileSpec,sizeof(Params.Tftp.LocalFileSpec)-1);
GetDlgItemText(ID_REMOTE_FILE,Params.Tftp.RemoteFileSpec,sizeof(Params.Tftp.RemoteFileSpec)-1);
// fill local char arrays
GetDlgItemText(ID_ADDRESS,Params.Tftp.RemoteAddress, sizeof(Params.Tftp.RemoteAddress)-1);
GetParent()->ShowData(
GCPdispatch(GetParent()->Status.hSession, GCP_GET_TFTP_FILE,&Params),
"calling GCP_GET_TFTP_FILE");
}
void TFileDlg::OnPutFile(void)
{
GCP_COMMAND_PARAMS Params;
// Send Button clicked
GetDlgItemText(ID_LOCAL_FILE,Params.Tftp.LocalFileSpec,sizeof(Params.Tftp.LocalFileSpec)-1);
GetDlgItemText(ID_REMOTE_FILE,Params.Tftp.RemoteFileSpec,sizeof(Params.Tftp.RemoteFileSpec)-1);
// fill local char arrays
GetDlgItemText(ID_ADDRESS,Params.Tftp.RemoteAddress, sizeof(Params.Tftp.RemoteAddress)-1);
// check for Mode
CButton *pAscii=(CButton *)GetDlgItem(ID_ASCII);
if (pAscii->GetCheck())
Params.Tftp.Mode=NETASCII;
else
Params.Tftp.Mode=OCTET; // binary
GetParent()->ShowData(
GCPdispatch(GetParent()->Status.hSession, GCP_PUT_TFTP_FILE,&Params),
"calling GCP_PUT_FILE");
}
// Define TBufferDlg, a CDialog constructor
BEGIN_MESSAGE_MAP(TBufferDlg,CDialog)
ON_WM_LBUTTONDOWN()
ON_BN_CLICKED (ID_BROADCAST, BroadcastCheck)
ON_BN_CLICKED (ID_ADDRESS, AddressSelect)
END_MESSAGE_MAP()
TBufferDlg::TBufferDlg(PCGcpWnd AParent, LPSTR AName)
{
// create a modeless dialog box
Create(AName,AParent);
// fill address combo with host names
CComboBox *pAddress=(CComboBox *)GetDlgItem(ID_ADDRESS);
GCP_QUERY_RESULTS Results;
if (GCP_OK==GCPquery(NULL, GCP_GET_FIRST_HOST, &Results))
{
pAddress->AddString ((char *)Results.Host.Name);
while (GCP_OK==GCPquery(NULL, GCP_GET_NEXT_HOST, &Results))
pAddress->AddString ((char *)Results.Host.Name);
}
SetDlgItemText(ID_SENDBUFFER,"GCP++ Makes TCP/IP Programming Fun!");
}
void TBufferDlg::BroadcastCheck()
{
// broadcast check selected
CComboBox *pAddress=(CComboBox *)GetDlgItem(ID_ADDRESS);
pAddress->Clear();
}
void TBufferDlg::AddressSelect(void)
{
// address selected
// checked, so NULL out address
CButton *pBroadcast=(CButton*)GetDlgItem(ID_BROADCAST);
pBroadcast->SetCheck(0);
}
void TBufferDlg::OnOK(void)
{
GCP_COMMAND_PARAMS Params;
char Buffer[10000];
char AsciiStr[10], RemoteAddress[50], RemotePortBuf[10];
char * pCarrot, *pFirstNonDigit;
char Carrot=94, AsciiChar;
char EOL[]={13,0};
int Repetitions, i;
// Send button selected
// fill local char arrays
GetDlgItemText(ID_ADDRESS,Params.Buffer.RemoteAddress, sizeof(Params.Buffer.RemoteAddress)-1);
GetDlgItemText(ID_REMOTEPORT,RemotePortBuf, sizeof(RemotePortBuf)-1);
Params.Buffer.Cnt=GetDlgItemText(ID_SENDBUFFER,Buffer, sizeof (Buffer)-1);
GetDlgItemText(ID_SIZE,AsciiStr, sizeof(AsciiStr)-1);
int OverrideSize=atoi(AsciiStr);
if (OverrideSize)
Params.Buffer.Cnt=OverrideSize;
GetDlgItemText(ID_REPEAT,AsciiStr,sizeof(AsciiStr)-1);
Repetitions=max(1,atoi(AsciiStr));
Buffer[Params.Buffer.Cnt]=NULL;
CButton *pBroadcast=(CButton*)GetDlgItem(ID_BROADCAST);
if (*RemoteAddress && 1==pBroadcast->GetCheck())
MessageBox("No Address allowed for Broadcast","Selection Error",MB_OK);
else
{
// fill buffer with cool stuff
//char Banner[]=" *** GENISYS Comm Pack++ makes TCP/IP apps a snap *** ";
//strcat(Buffer,Banner);
// increase buffer size for stress testing
//Params.Buffer.Size=sizeof(Buffer);
//if (Parent->Status.AgentType==UDP_AGENT)
//Params.Buffer.Size=1000; // so we don't force a WSAEMSGSIZE error
//Buffer[4999]=NULL;
Params.Buffer.Ptr=(BYTE far *)Buffer;
Buffer[Params.Buffer.Cnt]=NULL;
// put in port for use by UDP agent type
Params.Buffer.RemotePort=atoi(RemotePortBuf);
if (GetParent()->Status.SessionType==TELNET_SESSION)
{
strcat(Buffer,EOL);
Params.Buffer.Cnt+=sizeof(EOL);
}
//if (GetParent()->Status.AgentType==UDP_AGENT)
// {
// put in address for use by UDP agent type
// Params.Buffer.pAddress=RemoteAddress;
// put in port for use by UDP agent type
// Params.Buffer.Port=atoi(RemotePortBuf);
// }
// convert control symbols (^) characters to ASCII
pCarrot=Buffer;
while (pCarrot=strchr(pCarrot,Carrot))
{
// found a Carrot, so interpret the numbers after it
pFirstNonDigit=pCarrot;
// scan until *pCarrot is not a digit
while (isdigit(*++pFirstNonDigit));
// convert string to char
AsciiChar=(char)atoi(pCarrot+1);
// move data down
*pCarrot=AsciiChar;
memmove (pCarrot+1,pFirstNonDigit,&Buffer[4999]-pFirstNonDigit);
}
for (i=0; i<Repetitions; i++)
if (GetParent()->Status.SessionType==UDP_SESSION)
GetParent()->ShowData(
GCPdispatch (GetParent()->Status.hSession,GCP_SEND_DATAGRAM,&Params),
"calling GCP_SEND_DATAGRAM");
else
GetParent()->ShowData(
GCPdispatch (GetParent()->Status.hSession,GCP_SEND,&Params),
"calling GCP_SEND");
// reset control to left
CEdit *pBufferOut=(CEdit*)GetDlgItem(ID_SENDBUFFER);
pBufferOut->SetSel(0,0);
}
}
void TBufferDlg::OnCancel()
{
// Cancel button selected
ShowWindow(SW_HIDE);
}
// Define THostNameTableDlg, a CDialog constructor
THostTableDlg::THostTableDlg(PCGcpWnd AParent, LPSTR AName):
CDialog(AName, AParent)
{
}
BOOL THostTableDlg::OnInitDialog()
{
char Buffer[120];
CDialog::OnInitDialog();
// fill address combo with host names
CListBox *pAddress=(CListBox *)GetDlgItem(ID_ADDRESS);
GCP_QUERY_RESULTS Results;
if (GCP_OK==GCPquery(NULL, GCP_GET_FIRST_HOST, &Results))
{
sprintf (Buffer,"%-16s \t%s",Results.Host.Name,Results.Host.Addr);
pAddress->AddString (Buffer);
while (GCP_OK==GCPquery(NULL, GCP_GET_NEXT_HOST, &Results))
{
sprintf (Buffer,"%-16s \t%s",Results.Host.Name,Results.Host.Addr);
pAddress->AddString (Buffer);
}
}
return TRUE;
}
// Define TAboutDlg, a CDialog constructor
BEGIN_MESSAGE_MAP(CAboutDlg,CDialog)
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()
CAboutDlg::CAboutDlg(PCGcpWnd AParent, LPSTR AName):
CDialog(AName, AParent)
{
}
void CAboutDlg::OnLButtonDown (UINT uint, CPoint Point)
{
// user clicked on window
OnOK();
}
/****************************************************
* CGcpWnd implementations:
****************************************************/
// define message map for CGcpWnd
BEGIN_MESSAGE_MAP(CGcpWnd,CFrameWnd)
ON_WM_SIZE()
ON_WM_CLOSE()
ON_MESSAGE(M_SENDCALLBACK, SendCallback)
ON_MESSAGE(M_POSTCALLBACK, PostCallback)
ON_COMMAND(IDM_SHOW, OnShow)
ON_COMMAND(IDM_ABOUT, OnAbout)
ON_COMMAND(IDM_HIDE, OnHide)
ON_COMMAND(IDM_TELNET_CMD, OnTelnetCmd)
ON_COMMAND(IDM_TELNET, OnTelnet)
ON_COMMAND(IDM_TCP, OnTcp)
ON_COMMAND(IDM_TFTP, OnTftp)
ON_COMMAND(IDM_UDP, OnUdp)
ON_COMMAND(IDM_BUFFER, OnBuffer)
ON_COMMAND(IDM_FILE, OnFile)
ON_COMMAND(IDM_CLOSE, OnAgentClose)
ON_COMMAND(IDM_ABORT, OnAgentAbort)
ON_COMMAND(IDM_HOSTTABLE, OnHostTable)
ON_COMMAND(IDM_LOCALHOST, OnLocalHost)
ON_COMMAND(IDM_INDEX, OnIndex)
END_MESSAGE_MAP()
// Define CGcpWnd, a TWindow constructor
CGcpWnd::CGcpWnd()
{
Status.hSession=NULL;
Closing=FALSE;
Create (NULL, "GCP Client Ready",
WS_OVERLAPPEDWINDOW, rectDefault,
NULL,"GCP_CLIENT");
// create the edit window too
RECT Rect={0,0,0,0};
EditWnd.Create (WS_CHILD|WS_VISIBLE|ES_MULTILINE|
ES_NOHIDESEL|WS_VSCROLL|WS_HSCROLL|ES_AUTOVSCROLL,
Rect, this, 1);
EditWnd.SetReadOnly();
pConnectTcpDlg=new TConnectTcpDlg(this, "CONNECT_TCP");
pConnectUdpDlg=new TConnectUdpDlg(this, "CONNECT_UDP");
pConnectTftpDlg=new TConnectTftpDlg(this, "CONNECT_TFTP");
pConnectTelnetDlg=new TConnectTelnetDlg(this, "CONNECT_TELNET");
pBufferDlg=new TBufferDlg(this, "BUFFER");
pFileDlg=new TFileDlg(this, "FILE");
// set manual close while testing
OnMenuSelect(IDM_MANUALCLOSE,NULL,NULL);
ShowWindow (SW_SHOWNORMAL);
}
CGcpWnd::~CGcpWnd()
{
delete pConnectTcpDlg;
delete pConnectUdpDlg;
delete pConnectTftpDlg;
delete pConnectTelnetDlg;
delete pBufferDlg;
delete pFileDlg;
}
void CGcpWnd::OnSize(UINT ntype, int cx, int cy)
{
if (ntype==SIZE_RESTORED || ntype==SIZE_MAXIMIZED)
EditWnd.MoveWindow(0,0,cx,cy,TRUE);
}
void CGcpWnd::OnShow(void)
{
GCP_ERROR err=GCPdispatch(NULL, GCP_SHOW, NULL);
ShowData(err, "Show GCP Server");
}
void CGcpWnd::OnHide (void)
{
GCP_ERROR err=GCPdispatch(NULL, GCP_HIDE, NULL);
ShowData(err, "Hide GCP Server");
}
void CGcpWnd::OnTelnet (void)
{
pConnectTelnetDlg->BringWindowToTop();
pConnectTelnetDlg->ShowWindow(SW_SHOWNORMAL);
}
void CGcpWnd::OnTcp (void)
{
pConnectTcpDlg->BringWindowToTop();
pConnectTcpDlg->ShowWindow(SW_SHOWNORMAL);
}
void CGcpWnd::OnTftp (void)
{
pConnectTftpDlg->BringWindowToTop();
pConnectTftpDlg->ShowWindow(SW_SHOWNORMAL);
}
void CGcpWnd::OnUdp (void)
{
pConnectUdpDlg->BringWindowToTop();
pConnectUdpDlg->ShowWindow(SW_SHOWNORMAL);
}
void CGcpWnd::OnBuffer (void)
{
pBufferDlg->BringWindowToTop();
pBufferDlg->ShowWindow(SW_SHOWNORMAL);
}
void CGcpWnd::OnFile (void)
{
pFileDlg->BringWindowToTop();
pFileDlg->ShowWindow(SW_SHOWNORMAL);
}
void CGcpWnd::OnAgentClose (void)
{
ShowData(
GCPclose (Status.hSession,FALSE),
"closing session");
}
void CGcpWnd::OnAgentAbort (void)
{
ShowData(
GCPclose (Status.hSession,TRUE),
"closing session");
}
void CGcpWnd::OnHostTable (void)
{
THostTableDlg *ptr=new THostTableDlg(this, "HostNameTable");
ptr->DoModal();
}
void CGcpWnd::OnLocalHost (void)
{
GCP_QUERY_RESULTS Results;
GCPquery(NULL,GCP_GET_LOCAL_HOST,&Results);
MessageBox(Results.Host.Name,Results.Host.Addr,MB_OK);
}
void CGcpWnd::OnIndex (void)
{
WinExec("winhelp.exe gcp_clnt.hlp",SW_SHOWNORMAL);
}
void CGcpWnd::OnTelnetCmd (void)
{
new CTelnetCommandsDlg(this);
}
void CGcpWnd::OnAbout (void)
{
CAboutDlg *ptr=new CAboutDlg(this, "ABOUT");
ptr->DoModal();
}
void CGcpWnd::OnClose (void)
{
OnAbout();
if (!Status.hSession || Closing)
DestroyWindow();
else
{
Closing=TRUE;
if (GCPclose (Status.hSession,TRUE))
DestroyWindow();
}
}
LONG CGcpWnd::SendCallback(WPARAM wParam, LPARAM lParam)
{
// SendMessage arrives from GCP server...copy and use PostMessage
// ensure GCPfree is used after processing PostMessage
PostMessage(M_POSTCALLBACK,wParam,GCPalloc(wParam, lParam));
return 1l;
}
LONG CGcpWnd::PostCallback(WPARAM wParam, LPARAM lParam)
{
GCP_COMMAND Msg=(GCP_COMMAND)wParam;
Status=*(LPGCP_STATUS)lParam;
GCP_QUERY_RESULTS Results;
if (Msg!=GCP_CLOSED)
GCPquery (Status.hSession, GCP_GET_STATUS, &Results);
ShowData(Msg, Status.SessionType);
// do special processing according to message
switch(Msg)
{
case GCP_CLOSED:
Status.hSession=NULL;
GcpApp.m_pMainWnd->SetWindowText ("Session Closed!");
GcpApp.m_pMainWnd->DrawMenuBar ();
if (Closing)
CFrameWnd::OnClose();
break;
case GCP_OPENED:
if (Status.hSession)
{
// disable Connect Menu
//EnableMenuItem(GetMenu(m_hWnd),0,MF_BYPOSITION | MF_GRAYED);
// enable Command Menu
//EnableMenuItem(GetMenu(m_hWnd),1,MF_BYPOSITION | MF_ENABLED);
SetWindowText ("Session Open!");
DrawMenuBar ();
}
break;
default:
;
}
//free memory
BOOL success=GCPfree(lParam);
return 1l;
}
void CGcpWnd::TranslateMessage(char *theOutput, GCP_COMMAND MsgType)
{
switch (MsgType)
{
case GCP_OPENED:
strcpy (theOutput, "GCP_OPENED");
break;
case GCP_SEND:
strcpy (theOutput, "GCP_SEND");
break;
case GCP_RECV:
strcpy (theOutput, "GCP_RECV");
break;
case GCP_SEND_DATAGRAM:
strcpy (theOutput, "GCP_SEND_DATAGRAM");
break;
case GCP_RECV_DATAGRAM:
strcpy (theOutput, "GCP_RECV_DATAGRAM");
break;
case GCP_PUT_TFTP_FILE:
strcpy (theOutput, "GCP_PUT_TFTP_FILE");
break;
case GCP_GET_TFTP_FILE:
strcpy (theOutput, "GCP_GET_TFTP_FILE");
break;
case GCP_CLOSED:
strcpy (theOutput, "GCP_CLOSED");
break;
case GCP_TELNET:
strcpy (theOutput, "GCP_TELNET");
break;
default:
strcpy (theOutput, "------ Undefined GCP Message ------");
}
}
void CGcpWnd::TranslateType(char *theOutput, GCP_SESSION_TYPE SessionType)
{
switch (SessionType)
{
case TCP_SESSION:
strcpy (theOutput, "TCP_SESSION");
break;
case UDP_SESSION:
strcpy (theOutput, "UDP_SESSION");
break;
case TELNET_SESSION:
strcpy (theOutput, "TELNET_SESSION");
break;
case TFTP_SESSION:
strcpy (theOutput, "TFTP_SESSION");
break;
case TCP_DAEMON:
strcpy (theOutput, "TCP_DAEMON");
break;
case TELNET_DAEMON:
strcpy (theOutput, "TELNET_DAEMON");
break;
case TFTP_DAEMON:
strcpy (theOutput, "TFTP_DAEMON");
break;
default:
strcpy (theOutput, "------ Undefined GCP Session Type ------");
}
}
GCP_ERROR CGcpWnd::ShowData(GCP_ERROR ReturnValue, LPSTR DisplayString)
{
char outputString[200];
char errbuf[100];
if (ReturnValue)
{
FormatError(ReturnValue,errbuf);
sprintf(outputString, " Calling %s caused an ERROR: ",DisplayString);
strcat(outputString,errbuf);
}
else
sprintf(outputString, " %s", DisplayString);
Append (outputString);
return ReturnValue;
}
void CGcpWnd::FormatError (GCP_ERROR Error, char * errbuf)
{
_fstrcpy (errbuf,GCPperror(Error));
}
void CGcpWnd::ShowData(GCP_COMMAND Message, GCP_SESSION_TYPE Type)
{
char outputString[120];
char szMessage[40];
char szType[40];
TranslateMessage(szMessage, Message);
TranslateType(szType, Type);
if (Status.hSession)
sprintf (outputString,
"\r\n%s message from %s #%u",
szMessage, szType, Status.hSession);
else
sprintf (outputString,"\r\nFailure attempting GCPopen");
Append (outputString);
// Now output the data
switch (Message)
{
case GCP_SEND:
sprintf(outputString," %d bytes sent",
Status.Params.Buffer.Cnt);
Append (outputString);
break;
case GCP_SEND_DATAGRAM:
sprintf(outputString," %d bytes sent to %s, port %d",
Status.Params.Buffer.Cnt, Status.Params.Buffer.RemoteAddress,
Status.Params.Buffer.RemotePort);
Append (outputString);
break;
case GCP_RECV:
sprintf(outputString," %d bytes received: ",
Status.Params.Buffer.Cnt);
_fstrncat(outputString,(LPSTR)Status.Params.Buffer.Ptr,min(80,Status.Params.Buffer.Cnt));
Append (outputString);
break;
case GCP_RECV_DATAGRAM:
sprintf(outputString," %d bytes received from %s, port %d: ",
Status.Params.Buffer.Cnt, Status.Params.Buffer.RemoteAddress,
Status.Params.Buffer.RemotePort);
_fstrncat(outputString,(LPSTR)Status.Params.Buffer.Ptr,min(80,Status.Params.Buffer.Cnt));
Append (outputString);
break;
case GCP_PUT_TFTP_FILE:
sprintf(outputString," FileSpec: ");
_fstrncat(outputString,Status.Params.Tftp.LocalFileSpec,80);
Append (outputString);
break;
case GCP_GET_TFTP_FILE:
sprintf(outputString," FileSpec: ");
_fstrncat(outputString,Status.Params.Tftp.LocalFileSpec,80);
Append (outputString);
break;
case GCP_TELNET:
// create modal dialog box for display of information
new CTelnetCommandsDlg(this,Status.Params.Telnet);
break;
default:
;
}
// Bytes transeferred
if (Status.Stats.InCnt)
{
sprintf(outputString, " %ld cummulative bytes received averaging %ld bytes/sec",
Status.Stats.InCnt,Status.Stats.InRate);
Append (outputString);
}
if (Status.Stats.OutCnt)
{
sprintf(outputString, " %ld cummulative bytes sent averaging %ld bytes/sec",
Status.Stats.OutCnt,
Status.Stats.OutRate);
Append (outputString);
}
// Error Code
if (Status.Error)
{
char errbuf[100];
FormatError(Status.Error,errbuf);
sprintf(outputString, " %s for Session %d",errbuf,Status.hSession);
Append (outputString);
}
}
void CGcpWnd::Append (LPSTR String)
{
int Lines=EditWnd.GetLineCount();
if (Lines>400)
EditWnd.SetWindowText ("");
EditWnd.SetSel (32000, 32000);
EditWnd.ReplaceSel (String);
EditWnd.ReplaceSel ((LPSTR)"\r\n");
}