-rwxr-xr-x | PumpKINDlg.cpp | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/PumpKINDlg.cpp b/PumpKINDlg.cpp index 3ff1500..0c5c19b 100755 --- a/PumpKINDlg.cpp +++ b/PumpKINDlg.cpp @@ -975,535 +975,537 @@ BOOL rv = TRUE; tmp.Format(IDS_LOG_XFERRRQFINISHED,(LPCTSTR)m_FileName);
m_Daddy->LogLine(tmp);
rv = FALSE;
DoSelect(TRUE);
}else if(state!=stateFinish){
UpdateList();
DoXfer();
}
break;
case tftp::opERROR:
{
ASSERT(m_Daddy);
CString tmp;
tmp.Format(IDS_LOG_GOTTFTPERROR,p->data.m_ERROR.Code(),(LPCTSTR)p->errMessage());
m_Daddy->LogLine(tmp);
}
Destroy(FALSE);
rv = FALSE;
break;
default:
{
ASSERT(m_Daddy);
CString tmp;
tmp.Format(IDS_LOG_XFEROPCODE,p->Opcode());
m_Daddy->LogLine(tmp);
// *** Self destruct maybe??
}
break;
}
return rv;
}
BOOL CWRQSocket::OnTFTP(tftp* p)
{
switch(p->Opcode()){
case tftp::opOACK:
ASSERT(state!=stateFinish);
{
if(m_bResume)
m_ACK=m_File.GetLength()/m_blkSize;
else
m_ACK=0;
tftp::tftpOptions o;
if(p->GetOptions(&o)){
CString v;
if(o.Lookup(tftpoBSize,v)){
m_blkSize=atoi(v);
if(!m_blkSize){ // *** More sanity checks
Deny(tftp::errOption,IDS_TFTP_ERROR_BSIZE);
return TRUE;
}
}
if(o.Lookup(tftpoTOut,v)){
m_timeOut=atoi(v);
if(!m_timeOut){ // *** More sanity checks
Deny(tftp::errOption,IDS_TFTP_ERROR_TOUT);
return TRUE;
}
}
if(o.Lookup(tftpoTSize,v)){
m_xferSize=atoi(v);
}
if(m_xferSize>=0 && m_xferSize>=(m_blkSize<<16)) {
Deny(tftp::errUndefined,IDS_TFTP_ERROR_TOOBIG);
return TRUE;
}
}
UpdateList();
DoXfer();
}
break;
case tftp::opDATA:
{
UINT block = p->data.m_DATA.Block();
TRY{
m_File.Seek((block-1)*m_blkSize,CFile::begin);
int bytes = p->length-sizeof(p->data.m_DATA.block)-(tftpHdrSize-tftpSlackSize);
if(bytes){
m_File.Write(p->data.m_DATA.data,bytes);
// *** Move to the other place where we can do it not that often
m_File.SetLength(m_File.GetPosition());
}
if(bytes<m_blkSize){
state=stateFinish;
ASSERT(m_Daddy);
CString tmp;
tmp.Format(IDS_LOG_XFERWRQFINISHED,(LPCTSTR)m_FileName);
m_Daddy->LogLine(tmp);
}
m_ACK=block;
m_LastSlack=m_blkSize-bytes;
UpdateList();
DoXfer();
}CATCH(CFileException,e){
Deny(e);
}END_CATCH
}
break;
case tftp::opERROR:
{
ASSERT(m_Daddy);
CString tmp;
tmp.Format(IDS_LOG_GOTTFTPERROR,p->data.m_ERROR.Code(),(LPCTSTR)p->errMessage());
m_Daddy->LogLine(tmp);
}
Destroy(FALSE);
return FALSE;
default:
{
ASSERT(m_Daddy);
CString tmp;
tmp.Format(IDS_LOG_XFEROPCODE,p->Opcode());
m_Daddy->LogLine(tmp);
// *** Self destruct maybe??
}
break;
}
return TRUE;
}
void tftp::SetOpcode(WORD op)
{
opcode = REVERSEBYTES(op);
}
void tftp::tftpDATA::SetBlock(WORD b)
{
block=REVERSEBYTES(b);
}
WORD tftp::tftpDATA::Block()
{
return REVERSEBYTES(block);
}
WORD tftp::tftpACK::Block()
{
return REVERSEBYTES(block);
}
void tftp::tftpACK::SetBlock(WORD b)
{
block = REVERSEBYTES(b);
}
WORD tftp::tftpERROR::Code()
{
return REVERSEBYTES(code);
}
void tftp::tftpERROR::SetCode(WORD c)
{
code = REVERSEBYTES(c);
}
CString tftp::errMessage()
{
CString rv;
if(memchr(data.m_ERROR.data,0,length-(tftpHdrSize-tftpSlackSize)-sizeof(data.m_ERROR.code)))
rv = (LPCTSTR)data.m_ERROR.data;
return rv;
}
void CXferSocket::Destroy(BOOL success)
{
if(m_wndResolver){
delete m_wndResolver;
m_wndResolver=NULL;
}
SetTry();
m_Daddy->m_bnw.StartSound(
success
? m_Daddy->m_bnwSuccess
: m_Daddy->m_bnwAbort
);
if(m_File.m_hFile!=CFile::hFileNull){
TRY{
m_File.Close();
}CATCH(CFileException,e){
TRACE0("Error closing file\n");
}END_CATCH
}
ASSERT(m_Daddy);
m_Daddy->KillTimer(m_hSocket);
m_Daddy->m_Xfers.RemoveKey(m_hSocket);
LV_FINDINFO lvf;
memset(&lvf,0,sizeof(lvf));
lvf.flags=LVFI_PARAM;
lvf.lParam=(LPARAM)this;
int i = m_Daddy->m_List.FindItem(&lvf);
if(i>=0)
m_Daddy->m_List.DeleteItem(i);
delete this;
}
void CPumpKINDlg::LogLineToScreen(LPCTSTR str)
{
ASSERT(m_LogLength);
while(m_Log.GetCount()>m_LogLength && m_Log.GetCount()!=LB_ERR){
CTime *t = (CTime*)m_Log.GetItemData(0);
if(((DWORD)t)!=LB_ERR){
ASSERT(t);
m_LogTimes.RemoveKey(t);
delete t;
}
m_Log.DeleteString(0);
}
int i = m_Log.AddString(str);
ASSERT(i!=LB_ERR);
CTime *t = new CTime(CTime::GetCurrentTime());
m_Log.SetItemData(i,(DWORD)(m_LogTimes[t]=t));
m_Log.SetCurSel(i);
}
void CPumpKINDlg::LogLine(UINT msgID)
{
CString tmp;
tmp.Format(msgID);
LogLine(tmp);
}
void CXferSocket::TurnSlashes(CString& fn,BOOL bBack)
{
int s = fn.Find(bBack?'/':'\\');
while(s>=0){
fn.SetAt(s,bBack?'\\':'/');
s = fn.Find(bBack?'/':'\\');
}
}
CString CXferSocket::ApplyRoot(LPCTSTR fileName)
{
ASSERT(m_Daddy);
CString fn = fileName;
CString rv = m_Daddy->m_TFTPRoot;
if(rv.IsEmpty())
rv = ".";
if(rv[rv.GetLength()-1]!='\\')
rv+="\\";
while((!fn.IsEmpty()) && fn[0]=='\\')
fn=fn.Mid(1);
rv+=fn;
return rv;
}
void CPumpKINDlg::OnOptions()
{
CPropertySheet cps(IDS_TITLE_OPTIONS,this);
CPropsServer server;
CPropsNetwork network;
CPropsSounds sounds;
CPropsACL acl;
server.m_RRQMode=m_RRQMode;
server.m_TFTPRoot=m_TFTPRoot;
server.m_TFTPSubdirs=m_bTFTPSubdirs;
server.m_WRQMode=m_WRQMode;
server.m_PromptTimeOut=m_PromptTimeOut;
server.m_LogFile=m_LogFile;
network.m_ListenPort=m_ListenPort;
+ network.m_ListenAddress=m_ListenAddress;
network.m_SpeakPort=m_SpeakPort;
network.m_TimeOut=m_TFTPTimeOut.GetTotalSeconds();
network.m_BlockSize=m_BlockSize;
sounds.m_Request = m_bnwRequest;
sounds.m_Success = m_bnwSuccess;
sounds.m_Abort = m_bnwAbort;
acl.m_rulist = m_aclRules;
cps.AddPage(&server);
cps.AddPage(&network);
cps.AddPage(&sounds);
cps.AddPage(&acl);
if(cps.DoModal()==IDOK){
m_RRQMode=server.m_RRQMode;
m_TFTPRoot=server.m_TFTPRoot;
m_bTFTPSubdirs=server.m_TFTPSubdirs;
m_WRQMode=server.m_WRQMode;
m_PromptTimeOut=server.m_PromptTimeOut;
m_LogFile=server.m_LogFile;
m_ListenPort=network.m_ListenPort;
+ m_ListenAddress=network.m_ListenAddress;
m_SpeakPort=network.m_SpeakPort;
m_TFTPTimeOut=CTimeSpan(network.m_TimeOut);
m_BlockSize=network.m_BlockSize;
m_bnwRequest = sounds.m_Request;
m_bnwSuccess = sounds.m_Success;
m_bnwAbort = sounds.m_Abort;
m_aclRules = acl.m_rulist;
m_lastlogerr.Empty();
}
}
BOOL CRRQSocket::ConfirmRequest()
{
CConfirmRRQDlg cd(NULL);
cd.m_Daddy=this;
cd.m_File=m_FileName;
cd.m_Host=inet_ntoa(m_Peer.sin_addr);
if(cd.DoModal()==IDOK)
return TRUE;
return FALSE;
}
CWRQSocket::CWRQSocket(CPumpKINDlg* daddy,LPCTSTR fileName,LPCTSTR type,SOCKADDR_IN *sin)
: CXferSocket(daddy,fileName,type,sin)
{
state=stateNone;
m_ACK=0;
m_LastSlack=0;
m_bResume=FALSE;
}
BOOL CWRQSocket::Create(LPCTSTR localFile,LPCTSTR hostName)
{
if(!CAsyncSocket::Create(0,SOCK_DGRAM))
return FALSE;
ASSERT(m_Daddy);
ASSERT(m_Peer.sin_addr.s_addr!=INADDR_NONE || hostName);
m_Daddy->m_Xfers[m_hSocket]=this;
UpdateList();
CString lf;
if(!localFile) {
lf = m_FileName;
TurnSlashes(lf,TRUE);
}
CString fn = localFile?ApplyRootGently(localFile):ApplyRoot(lf);
if(!localFile){ // This is an incoming request..
if(CheckBadRelativeness(m_FileName)){
Deny(tftp::errAccessViolation,IDS_TFTP_ERROR_ACCESS);
return TRUE;
}
BOOL exists;
if(!_access((LPCTSTR)fn,0))
m_Rename=exists=TRUE;
else
m_Rename=exists=FALSE;
int atar=m_Daddy->m_aclRules.FindTarget(acl_rule::opWRQ,m_Peer.sin_addr.s_addr);
if(atar<0)
atar=m_Daddy->m_WRQMode;
switch(atar){
case CPumpKINDlg::wrqTakeAll:
if(exists){
if(!RenameFile(fn)){
Deny(tftp::errDiskFull,IDS_TFTP_ERROR_FAILEDTORENAME);
return TRUE;
}
}
break;
case CPumpKINDlg::wrqConfirmIfExists:
if(!exists)
break;
case CPumpKINDlg::wrqAlwaysConfirm:
if(exists)
m_bResume=TRUE;
if(ConfirmRequest()){
if(m_Rename){
RenameFile(fn);
if(SaveAs(fn))
break;
}else
break;
}
default:
TRACE1("Unexpected access target: %d\n",atar);
case CPumpKINDlg::wrqDenyAll:
Deny(tftp::errAccessViolation,IDS_TFTP_ERROR_ACCESS);
return TRUE;
}
}
CFileException e;
if(!m_File.Open(
fn,
m_bResume
?(CFile::modeWrite|CFile::shareDenyWrite)
:(CFile::modeCreate|CFile::modeWrite|CFile::shareDenyWrite),
&e
)){
if(localFile){ // Outgoing request
CString tmp;
tmp.Format(IDS_LOG_FAILEDTOOPEN,fn);
m_Daddy->LogLine(tmp);
return FALSE;
}else{
Deny(&e);
return TRUE;
}
}
if(hostName){
m_HostName=hostName;
CString tmp;
tmp.Format(IDS_LOG_REQUESTING,m_FileName,m_HostName);
m_Daddy->LogLine(tmp);
CString inAddr = hostName;
int at = inAddr.Find('@');
if(at>=0)
inAddr=inAddr.Mid(at+1);
if((m_Peer.sin_addr.s_addr=inet_addr((LPCTSTR)inAddr))==INADDR_NONE){
ASSERT(!m_wndResolver);
m_wndResolver = new CResolver(this);
ASSERT(m_wndResolver);
return m_wndResolver->Resolve();
}else{
OnHostKnown();
return TRUE;
}
}
CString v;
tftp::tftpOptions oack;
if(m_Options.Lookup(tftpoTSize,v)){
m_xferSize=atol(v);
if(!m_xferSize){
Deny(tftp::errOption,IDS_TFTP_ERROR_TSIZE);
return TRUE;
}
}
if(m_Options.Lookup(tftpoBSize,v)){
m_blkSize=atoi(v);
if(!m_blkSize){ // *** Do more about sanity check
Deny(tftp::errOption,IDS_TFTP_ERROR_BSIZE);
return TRUE;
}
v.Format("%u",m_blkSize);
oack[tftpoBSize]=v;
}
if(m_Options.Lookup(tftpoTOut,v)){
m_timeOut=atoi(v);
if(!m_timeOut){ // *** Do more about sanity check
Deny(tftp::errOption,IDS_TFTP_ERROR_TOUT);
return TRUE;
}
v.Format("%u",m_timeOut);
oack[tftpoTOut]=v;
}
if(m_Options.Lookup(tftpoXResume,v) && m_bResume){
m_ACK=m_File.GetLength()/m_blkSize;
v.Format("%u",m_ACK);
oack[tftpoXResume]=v;
}else
m_ACK=0;
// XXX: see if we can negotiate the right block size somehow
if(m_xferSize>=0 && m_xferSize>=(m_blkSize<<16)) {
Deny(tftp::errUndefined,IDS_TFTP_ERROR_TOOBIG);
return TRUE;
}
state=stateXfer;
if(oack.GetCount()){
tftp *p = tftp::Allocate(tftp::tftpOACK::tftpSize(&oack));
ASSERT(p);
p->SetOpcode(tftp::opOACK);
p->data.m_OACK.Set(&oack);
PostTFTP(p,TRUE);
}else
DoXfer();
return TRUE;
}
BOOL CWRQSocket::ConfirmRequest()
{
CConfirmWRQDlg cd(NULL);
cd.m_Daddy=this;
cd.m_File=m_FileName;
cd.m_Host=inet_ntoa(m_Peer.sin_addr);
switch(cd.DoModal()){
case IDOK:
m_Rename=FALSE;
m_bResume=FALSE;
return TRUE;
case IDC_RENAME:
m_bResume=FALSE;
m_Rename=TRUE;
return TRUE;
case IDC_RESUME:
m_Rename=FALSE;
m_bResume=TRUE;
return TRUE;
case IDCANCEL:
return FALSE;
}
return FALSE;
}
BOOL CWRQSocket::RenameFile(CString& fn)
{
CString renamed = fn;
if(fn.IsEmpty())
return FALSE;
if(fn[fn.GetLength()-1]==')'){
int op = fn.ReverseFind('(');
if(op>0 && fn[op-1]==' '){
if(fn.Mid(op+1,fn.GetLength()-op-2).SpanExcluding("0123456789").IsEmpty())
renamed = renamed.Left(op-1);
}
}
CString testFN;
for(UINT tmp=0;tmp<32768;tmp++){
testFN.Format("%s (%u)",(LPCTSTR)renamed,tmp);
if(!_access((LPCTSTR)testFN,0))
continue;
fn=testFN;
return TRUE;
}
return FALSE;
}
BOOL CWRQSocket::SaveAs(CString& fn)
{
CFileDialog cfd(FALSE,NULL,fn,OFN_EXPLORER|OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_PATHMUSTEXIST,NULL,m_Daddy);
CString title;
title.LoadString(IDS_RENAME_TITLE);
cfd.m_ofn.lpstrTitle=(LPCTSTR)title;
if(cfd.DoModal()!=IDOK)
return FALSE;
fn = cfd.GetPathName();
return TRUE;
}
void CWRQSocket::DoXfer()
{
tftp *p = tftp::Allocate(tftp::tftpACK::tftpSize());
ASSERT(p);
p->SetOpcode(tftp::opACK);
p->data.m_ACK.SetBlock(m_ACK);
TRACE1("WRQ-ACK-%u\n",m_ACK);
PostTFTP(p,TRUE); // *** ??? Hope this is right
}
UINT tftp::tftpACK::tftpSize()
{
return tftpHdrSize-tftpSlackSize+sizeof(tftp::tftpACK);
}
ULONG CWRQSocket::GetACK()
@@ -1622,555 +1624,555 @@ void CPumpKINDlg::OnItemchangedConnections(NMHDR* pNMHDR, LRESULT* pResult) void CPumpKINDlg::OnAbort()
{
if(!m_List.GetSelectedCount())
return;
int items = m_List.GetItemCount();
for(int tmp=0;tmp<items;tmp++){
if(!(m_List.GetItemState(tmp,LVIS_SELECTED)&LVIS_SELECTED))
continue;
CXferSocket *xfer = (CXferSocket*) m_List.GetItemData(tmp);
ASSERT(xfer);
xfer->Abort();
}
}
void CXferSocket::OnFailedToResolve()
{
TRACE0("Resolve failed\n");
delete m_wndResolver;
m_wndResolver=NULL;
ASSERT(m_Daddy);
CString tmp;
tmp.Format(IDS_LOG_RESOLVEFAILED,m_HostName);
m_Daddy->LogLine(tmp);
Abort();
}
void CXferSocket::OnResolved()
{
delete m_wndResolver;
m_wndResolver=NULL;
TRACE0("Resolved\n");
m_Peer.sin_addr.s_addr = *(DWORD*)(((hostent*)m_ResolveBuff)->h_addr);
}
void CRRQSocket::OnResolved()
{
CXferSocket::OnResolved();
OnHostKnown();
}
void CRRQSocket::OnHostKnown()
{
ASSERT(m_Daddy);
m_Peer.sin_port=htons(m_Daddy->m_SpeakPort);
tftp::tftpOptions o;
CString v;
ASSERT(m_xferSize>=0);
v.Format("%lu",m_xferSize);
o[tftpoTSize] = v;
ASSERT(m__blkSize);
v.Format("%u",m__blkSize);
o[tftpoBSize] = v;
ASSERT(m__timeOut);
v.Format("%u",m__timeOut);
o[tftpoTOut] = v;
o[tftpoXResume] = "0";
tftp *p = tftp::Allocate(tftp::tftpWRQ::tftpSize(m_FileName,m_Type,&o));
ASSERT(p);
p->SetOpcode(tftp::opWRQ);
p->data.m_WRQ.Set(m_FileName,m_Type,&o);
PostTFTP(p,TRUE);
state=stateInit;
UpdateList();
}
UINT tftp::tftpRQ::tftpSize(LPCTSTR file,LPCTSTR type,tftp::tftpOptions* ops)
{
UINT rv = tftpHdrSize-tftpSlackSize+strlen(file)+1+strlen(type)+1;
if(ops){
tftpOptions& o = *ops;
POSITION p = o.GetStartPosition();
while(p){
CString n,v;
o.GetNextAssoc(p,n,v);
rv+=n.GetLength()+1+v.GetLength()+1;
}
}
return rv;
}
UINT tftp::tftpRRQ::tftpSize(LPCTSTR file,LPCTSTR type,tftp::tftpOptions* ops)
{
return tftp::tftpRQ::tftpSize(file,type,ops);
}
UINT tftp::tftpWRQ::tftpSize(LPCTSTR file,LPCTSTR type,tftp::tftpOptions* ops)
{
return tftp::tftpRQ::tftpSize(file,type,ops);
}
UINT tftp::tftpOACK::tftpSize(tftp::tftpOptions* ops)
{
UINT rv = tftpHdrSize-tftpSlackSize;
if(ops){
tftpOptions& o = *ops;
POSITION p = o.GetStartPosition();
while(p){
CString n,v;
o.GetNextAssoc(p,n,v);
rv+=n.GetLength()+1+v.GetLength()+1;
}
}
return rv;
}
void tftp::tftpRQ::Set(LPCTSTR file,LPCTSTR type,tftp::tftpOptions* ops)
{
// MAY BE DANGEROUS!
UINT ptr = 0;
strcpy((LPTSTR)&data[ptr],file); ptr+=strlen(file)+1;
strcpy((LPTSTR)&data[ptr],type); ptr+=strlen(type)+1;
if(ops){
tftpOptions& o = *ops;
POSITION p = o.GetStartPosition();
while(p){
CString n,v;
o.GetNextAssoc(p,n,v);
strcpy((LPTSTR)&data[ptr],(LPCTSTR)n); ptr+=n.GetLength()+1;
strcpy((LPTSTR)&data[ptr],(LPCTSTR)v); ptr+=v.GetLength()+1;
}
}
}
void tftp::tftpRRQ::Set(LPCTSTR file,LPCTSTR type,tftp::tftpOptions* ops)
{
// MAY BE DANGEROUS!
UINT ptr = 0;
strcpy((LPTSTR)&data[ptr],file); ptr+=strlen(file)+1;
strcpy((LPTSTR)&data[ptr],type); ptr+=strlen(type)+1;
if(ops){
tftpOptions& o = *ops;
POSITION p = o.GetStartPosition();
while(p){
CString n,v;
o.GetNextAssoc(p,n,v);
strcpy((LPTSTR)&data[ptr],(LPCTSTR)n); ptr+=n.GetLength()+1;
strcpy((LPTSTR)&data[ptr],(LPCTSTR)v); ptr+=v.GetLength()+1;
}
}
}
void tftp::tftpWRQ::Set(LPCTSTR file,LPCTSTR type,tftp::tftpOptions* ops)
{
// MAY BE DANGEROUS!
UINT ptr = 0;
strcpy((LPTSTR)&data[ptr],file); ptr+=strlen(file)+1;
strcpy((LPTSTR)&data[ptr],type); ptr+=strlen(type)+1;
if(ops){
tftpOptions& o = *ops;
POSITION p = o.GetStartPosition();
while(p){
CString n,v;
o.GetNextAssoc(p,n,v);
strcpy((LPTSTR)&data[ptr],(LPCTSTR)n); ptr+=n.GetLength()+1;
strcpy((LPTSTR)&data[ptr],(LPCTSTR)v); ptr+=v.GetLength()+1;
}
}
}
void tftp::tftpOACK::Set(tftpOptions* ops)
{
ASSERT(ops);
UINT ptr = 0;
tftpOptions& o = *ops;
POSITION p = o.GetStartPosition();
while(p){
CString n,v;
o.GetNextAssoc(p,n,v);
strcpy((LPTSTR)&data[ptr],(LPCTSTR)n); ptr+=n.GetLength()+1;
strcpy((LPTSTR)&data[ptr],(LPCTSTR)v); ptr+=v.GetLength()+1;
}
}
void CWRQSocket::OnResolved()
{
CXferSocket::OnResolved();
OnHostKnown();
}
void CWRQSocket::OnHostKnown()
{
ASSERT(m_Daddy);
m_Peer.sin_port=htons(m_Daddy->m_SpeakPort);
tftp::tftpOptions o;
CString v;
o[tftpoTSize]="0";
if(m__blkSize){
v.Format("%u",m__blkSize);
o[tftpoBSize]=v;
}
if(m__timeOut){
v.Format("%u",m__timeOut);
o[tftpoTOut]=v;
}
tftp *p = tftp::Allocate(tftp::tftpRRQ::tftpSize(m_FileName,m_Type,&o));
ASSERT(p);
p->SetOpcode(tftp::opRRQ);
p->data.m_RRQ.Set(m_FileName,m_Type,&o);
PostTFTP(p,TRUE);
state=stateInit;
UpdateList();
}
void CPumpKINDlg::OnClose()
{
OnTrayShowpumpkinwindow();
}
void CPumpKINDlg::OnTrayShowpumpkinwindow()
{
if(IsWindowVisible()){
m_bShown=FALSE;
ShowWindow(SW_HIDE);
}else{
m_bShown=TRUE;
ShowWindow(SW_SHOW);
SetForegroundWindow();
SetFocus();
}
}
void CPumpKINDlg::OnTrayExit()
{
OnExit();
}
void CPumpKINDlg::OnTrayAboutpumpkin()
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
void CPumpKINDlg::OnTrayFetchfile()
{
OnGet();
}
void CPumpKINDlg::OnTrayHelp()
{
OnHelp();
}
void CPumpKINDlg::OnTrayOptions()
{
OnOptions();
}
void CPumpKINDlg::OnTraySendfile()
{
OnPut();
}
void CPumpKINDlg::LoadSettings()
{
CWinApp *app = AfxGetApp();
ASSERT(app);
m_bListen=app->GetProfileInt("TFTPSettings","Listen",m_bListen);
m_bnwRequest=app->GetProfileString("BellsNWhistles","Request",m_bnwRequest);
m_bnwSuccess=app->GetProfileString("BellsNWhistles","Success",m_bnwSuccess);
m_bnwAbort=app->GetProfileString("BellsNWhistles","Abort",m_bnwAbort);
m_bTFTPSubdirs=app->GetProfileInt("TFTPSettings","Subdirs",m_bTFTPSubdirs);
m_ListenPort=app->GetProfileInt("TFTPSettings","ListenPort",m_ListenPort);
+ m_ListenAddress=app->GetProfileString("TFTPSettings","ListenAddress",m_ListenAddress);
m_LogLength=app->GetProfileInt("UISettings","LogLength",m_LogLength);
m_PromptTimeOut=app->GetProfileInt("UISettings","PromptTimeout",m_PromptTimeOut);
m_RRQMode=app->GetProfileInt("TFTPSettings","RRQMode",m_RRQMode);
m_SpeakPort=app->GetProfileInt("TFTPSettings","SpeakPort",m_SpeakPort);
m_TFTPRoot=app->GetProfileString("TFTPSettings","TFTPRoot",m_TFTPRoot);
m_LogFile=app->GetProfileString("General","LogFile",m_LogFile);
m_TFTPTimeOut=CTimeSpan(app->GetProfileInt("TFTPSettings","TFTPTimeout",m_TFTPTimeOut.GetTotalSeconds()));
m_BlockSize=app->GetProfileInt("TFTPSettings","TFTPBlockSize",m_BlockSize);
m_RetryTimeOut=CTimeSpan(app->GetProfileInt("TFTPSettings","RetryTimeout",m_RetryTimeOut.GetTotalSeconds()));
m_WRQMode=app->GetProfileInt("TFTPSettings","WRQMode",m_WRQMode);
m_bShown=app->GetProfileInt("UISettings","Visble",m_bShown);
if(m_TFTPRoot.IsEmpty()){
DWORD dL = ::GetCurrentDirectory(0,NULL);
VERIFY(::GetCurrentDirectory(dL,m_TFTPRoot.GetBuffer(dL)));
m_TFTPRoot.ReleaseBuffer();
}
::SetCurrentDirectory(m_TFTPRoot);
m_aclRules.LoadProfile(app);
}
void CPumpKINDlg::SaveSettings()
{
CWinApp *app = AfxGetApp();
ASSERT(app);
app->WriteProfileInt("TFTPSettings","Listen",m_bListen);
app->WriteProfileString("BellsNWhistles","Request",m_bnwRequest);
app->WriteProfileString("BellsNWhistles","Success",m_bnwSuccess);
app->WriteProfileString("BellsNWhistles","Abort",m_bnwAbort);
app->WriteProfileInt("TFTPSettings","Subdirs",m_bTFTPSubdirs);
app->WriteProfileInt("TFTPSettings","ListenPort",m_ListenPort);
+ app->WriteProfileString("TFTPSettings","ListenAddress",m_ListenAddress);
app->WriteProfileInt("UISettings","LogLength",m_LogLength);
app->WriteProfileInt("UISettings","PromptTimeout",m_PromptTimeOut);
app->WriteProfileInt("TFTPSettings","RRQMode",m_RRQMode);
app->WriteProfileInt("TFTPSettings","SpeakPort",m_SpeakPort);
app->WriteProfileString("TFTPSettings","TFTPRoot",m_TFTPRoot);
app->WriteProfileString("General","LogFile",m_LogFile);
app->WriteProfileInt("TFTPSettings","TFTPTimeout",m_TFTPTimeOut.GetTotalSeconds());
app->WriteProfileInt("TFTPSettings","TFTPBlockSize",m_BlockSize);
app->WriteProfileInt("TFTPSettings","RetryTimeout",m_RetryTimeOut.GetTotalSeconds());
app->WriteProfileInt("TFTPSettings","WRQMode",m_WRQMode);
app->WriteProfileInt("UISettings","Visble",m_bShown);
m_aclRules.SaveProfile(app);
}
void CPumpKINDlg::OnWindowPosChanging(WINDOWPOS FAR* lpwndpos)
{
CDialog::OnWindowPosChanging(lpwndpos);
if(!m_bExiting){
if(m_bShown){
lpwndpos->flags&=~SWP_HIDEWINDOW;
lpwndpos->flags|=SWP_SHOWWINDOW;
}else{
lpwndpos->flags&=~SWP_SHOWWINDOW;
lpwndpos->flags|=SWP_HIDEWINDOW;
if(::GetForegroundWindow()==m_hWnd && (m_Trayer && !m_Trayer->m_inMenu))
GetDesktopWindow()->SetForegroundWindow();
}
}
}
CString CXferSocket::ApplyRootGently(LPCTSTR fn)
{
CString f = fn;
CString rv = f;
if((!f.IsEmpty()) && f[0]!='\\' && f.Find(":")<0 && f.Find("\\")<0)
rv = ApplyRoot(f);
return rv;
}
BOOL CXferSocket::CheckBadRelativeness(LPCTSTR file)
{
CString tmp = file;
if(tmp.IsEmpty())
return FALSE;
if(tmp.Find("..")>=0)
return TRUE;
if(tmp.Find(":")>=0)
return TRUE;
if((!m_Daddy->m_bTFTPSubdirs) && m_FileName.Find('\\')>=0)
return TRUE;
return FALSE;
}
void CAboutDlg::OnKlevernet()
{
CString url;
url.LoadString(IDS_KLEVERNET_URL);
ShellExecute(::GetDesktopWindow(),"open",url,NULL,NULL,SW_SHOWMAXIMIZED);
}
BOOL CPumpKINDlg::PreTranslateMessage(MSG* pMsg)
{
// CG: The following block was added by the ToolTips component.
{
// Let the ToolTip process this message.
m_tooltip.RelayEvent(pMsg);
return CDialog::PreTranslateMessage(pMsg);
}
}
void CPumpKINDlg::OnSelchangeLog()
{
int sel = m_Log.GetCurSel();
if(sel==LB_ERR){
TRACE0("Error retrieving selection\n");
}else{
CTime *t = (CTime*)m_Log.GetItemData(sel);
ASSERT(t);
m_tooltip.UpdateTipText(t->Format(IDS_LOGTIMEFORMAT),&m_Log);
}
}
void CPumpKINDlg::OnTrayOpenfilesfolder()
{
ShellExecute(::GetDesktopWindow(),NULL,m_TFTPRoot,NULL,NULL,SW_SHOWDEFAULT);
}
void CPumpKINDlg::OnDropFiles(HDROP hDropInfo)
{
UINT files = ::DragQueryFile(hDropInfo,0xFFFFFFFF,NULL,0);
ASSERT(files);
for(UINT file=0;file<files;file++){
CString theFile;
UINT fileNameLength = ::DragQueryFile(hDropInfo,file,NULL,0);
ASSERT(fileNameLength);
VERIFY(::DragQueryFile(hDropInfo,file,theFile.GetBuffer(fileNameLength+5),fileNameLength+4)<=fileNameLength);
theFile.ReleaseBuffer();
// Send it over!
CRequestDlg crd(NULL);
crd.m_Drop=TRUE;
crd.m_Put=TRUE;
crd.m_BSize=m_BlockSize;
crd.m_LocalFile=theFile;
if(crd.DoModal()==IDOK){
CRRQSocket *socket = new CRRQSocket(this,crd.m_RemoteFile,crd.m_Type,NULL);
if(crd.m_BSize)
socket->m__blkSize=crd.m_BSize;
if(!socket->Create(crd.m_LocalFile,crd.m_Host))
socket->Destroy();
}
}
::DragFinish(hDropInfo);
}
void CPumpKINDlg::OnCancel()
{
OnClose();
}
CPumpKINDlg::~CPumpKINDlg()
{
delete m_Trayer;
delete m_Retrier;
}
void CXferSocket::OnRetry()
{
if(!m_Retry){
TRACE("Retrying unretriable..\n");
return;
}
TRACE0("Retrying..\n");
m_bRetry=TRUE;
PostTFTP(tftp::Copy(m_Retry));
m_bRetry=FALSE;
}
tftp* tftp::Copy(tftp *src)
{
ASSERT(src);
ASSERT(src->length);
tftp* rv = Allocate(src->length);
ASSERT(rv);
memmove(rv,src,tftpSlackSize+src->length);
return rv;
}
void CXferSocket::SetTry(tftp *p)
{
if(m_Retry)
delete m_Retry;
m_Retry=p?tftp::Copy(p):NULL;
}
void CPumpKINDlg::OnHelp()
{
AfxGetApp()->WinHelp(0,HELP_FINDER);
}
BOOL CListenSocket::SetListen(BOOL b) {
ASSERT(m_Daddy);
- if(b==m_bListen)
- return TRUE;
- if(b) {
- if(!Create(m_Daddy->m_ListenPort,SOCK_DGRAM))
- return FALSE;
- return m_bListen=TRUE;
- }else{
+ if(b==m_bListen) return TRUE;
+ if(!b) {
Close(); m_bListen=FALSE;
return TRUE;
}
+ return m_bListen=Create(m_Daddy->m_ListenPort,SOCK_DGRAM,
+ FD_READ|FD_WRITE|FD_OOB|FD_ACCEPT|FD_CONNECT|FD_CLOSE,
+ m_Daddy->m_ListenAddress.IsEmpty()?NULL:(LPCTSTR)m_Daddy->m_ListenAddress);
}
void CPumpKINDlg::OnListening()
{
if(!m_Listener.SetListen(m_ListenCtl.GetCheck()==1)) {
TRACE0("Failed to create socket\n");
AfxMessageBox(IDS_BOX_CANTBIND,MB_OK|MB_ICONEXCLAMATION);
}
m_ListenCtl.SetCheck(m_Listener.m_bListen?1:0);
m_bListen=m_Listener.m_bListen;
}
void CPumpKINDlg::OnTrayListen()
{
if(!m_Listener.SetListen(!m_Listener.m_bListen)) {
TRACE0("Failed to create socket\n");
AfxMessageBox(IDS_BOX_CANTBIND,MB_OK|MB_ICONEXCLAMATION);
}
m_ListenCtl.SetCheck(m_Listener.m_bListen?1:0);
m_bListen=m_Listener.m_bListen;
}
void CPumpKINDlg::LogLine(LPCTSTR str)
{
LogLineToScreen(str);
if(!m_LogFile.IsEmpty()) {
if(!Klever::LogRecord((LPCTSTR)m_LogFile,str)) {
if(m_lastlogerr!=m_LogFile) {
CString tmp;
tmp.Format(IDS_LOG_LOGERROR,m_LogFile);
LogLineToScreen(tmp);
m_lastlogerr=m_LogFile;
}
}
}
}
void CPumpKINDlg::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI)
{
CDialog::OnGetMinMaxInfo(lpMMI);
if(m_MinSize.cx>0 && m_MinSize.cy>0){
lpMMI->ptMinTrackSize.x = m_MinSize.cx;
lpMMI->ptMinTrackSize.y = m_MinSize.cy;
}
}
void CPumpKINDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
if(nType==SIZE_RESTORED)
RecalcLayout(cx,cy);
}
void CPumpKINDlg::RecalcLayout(int,int)
{
CRect wrc;
GetClientRect(&wrc);
AdjustButton(m_GetCtl,wrc);
AdjustButton(m_PutCtl,wrc);
AdjustButton(m_AbortCtl,wrc);
AdjustButton(m_HelpCtl,wrc);
AdjustButton(m_ExitCtl,wrc);
AdjustButton(m_OptionsCtl,wrc);
CRect brc;
m_List.GetWindowRect(&brc); ScreenToClient(&brc);
m_List.SetWindowPos(
0,
brc.left, brc.top,
wrc.right-m_rightGapList-brc.left, brc.bottom-brc.top,
SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOZORDER|SWP_NOCOPYBITS );
m_Log.GetWindowRect(&brc); ScreenToClient(&brc);
m_Log.SetWindowPos(
0,
brc.left, brc.top,
wrc.right-m_rightGapButtons-brc.left, wrc.bottom-m_bottomGapLog-brc.top,
SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOZORDER|SWP_NOCOPYBITS );
m_ListenCtl.GetWindowRect(&brc); ScreenToClient(&brc);
m_ListenCtl.SetWindowPos(
0,
wrc.right-brc.Width()-m_rightGapButtons, wrc.bottom-brc.Height()-m_bottomGapListen,
0,0,
SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER|SWP_NOCOPYBITS );
int i = m_Log.GetCount();
if(i!=LB_ERR)
m_Log.SetCurSel(i-1);
}
void CPumpKINDlg::AdjustButton(CWnd& w,CRect& wrc)
{
CRect brc;
w.GetWindowRect(&brc); ScreenToClient(&brc);
w.SetWindowPos(
0,
wrc.right-brc.Width()-m_rightGapButtons, brc.top,
0, 0,
SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER|SWP_NOCOPYBITS );
}
|