summaryrefslogtreecommitdiffabout
path: root/PumpKINDlg.cpp
Side-by-side diff
Diffstat (limited to 'PumpKINDlg.cpp') (more/less context) (ignore whitespace changes)
-rwxr-xr-xPumpKINDlg.cpp16
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
@@ -1135,215 +1135,217 @@ 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)
@@ -1782,394 +1784,394 @@ POSITION p = o.GetStartPosition();
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 );