summaryrefslogtreecommitdiffabout
path: root/DipstickDlg.cpp
Side-by-side diff
Diffstat (limited to 'DipstickDlg.cpp') (more/less context) (show whitespace changes)
-rw-r--r--DipstickDlg.cpp48
1 files changed, 45 insertions, 3 deletions
diff --git a/DipstickDlg.cpp b/DipstickDlg.cpp
index ace2aff..f27b219 100644
--- a/DipstickDlg.cpp
+++ b/DipstickDlg.cpp
@@ -1,265 +1,268 @@
// DipstickDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Dipstick.h"
#include "DipstickDlg.h"
#include "DragPad.h"
#include "OptionsDlg.h"
#include "HostNameDlg.h"
#include "HostProperties.h"
#include "AutoBestDlg.h"
#include "ddeml.h"
#include "URLSource.h"
#include "SelectURLsDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
virtual BOOL OnInitDialog();
//{{AFX_MSG(CAboutDlg)
afx_msg void OnKlevernet();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
ON_BN_CLICKED(IDC_KLEVERNET, OnKlevernet)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDipstickDlg dialog
CDipstickDlg::CDipstickDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDipstickDlg::IDD, pParent),
m_DragPad(NULL), m_PingsPerHost(10), m_PingSize(32), m_TimeOut(5000), m_Interval(500),
m_SortCriteria(sortAvg), m_bShown(FALSE), m_MaxThreads(10), m_bExiting(FALSE),
m_bMSWorkAround(TRUE), m_MinSize(-1,-1), m_bAutoBest(TRUE), m_bSmartAdjust(TRUE),
m_bAdjustInnermost(TRUE), m_ActiveThreads(0), m_bIgnorelative(TRUE)
{
CWinApp *app = AfxGetApp();
ASSERT(app);
WORD hi,lo;
hi=app->GetProfileInt("Options","IntervalHi",HIWORD(m_Interval));
lo=app->GetProfileInt("Options","IntervalLo",LOWORD(m_Interval));
m_Interval=MAKELONG(lo,hi);
m_MaxThreads=app->GetProfileInt("Options","MaxThreads",m_MaxThreads);
m_PingSize=app->GetProfileInt("Options","PingSize",m_PingSize);
m_PingsPerHost=app->GetProfileInt("Options","PingsPerHost",m_PingsPerHost);
m_SortCriteria=app->GetProfileInt("Options","SortBy",m_SortCriteria);
hi=app->GetProfileInt("Options","TimeOutHi",HIWORD(m_TimeOut));
lo=app->GetProfileInt("Options","TimeOutLo",LOWORD(m_TimeOut));
m_TimeOut=MAKELONG(lo,hi);
m_bMSWorkAround = app->GetProfileInt("Options","InnermostURL",m_bMSWorkAround);
m_bAutoBest = app->GetProfileInt("Options","AutoBest",m_bAutoBest);
m_bSmartAdjust = app->GetProfileInt("Options","SmartAdjust",m_bSmartAdjust);
m_bAdjustInnermost = app->GetProfileInt("Options","AdjustInnermost",m_bAdjustInnermost);
m_bIgnorelative = app->GetProfileInt("Options","IgnoreRelative",m_bIgnorelative);
m_impexPath = app->GetProfileString("Options","ImportExportPath",m_impexPath);
//{{AFX_DATA_INIT(CDipstickDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_bmpBack.LoadBitmap(IDB_DIABACK);
m_bmpBack.GetBitmap(&m_bitmapBack);
CString tmp;
VERIFY(tmp.LoadString(IDS_REGEX_HTMLURL));
VERIFY(m_reHTMLURL.Compile(tmp,CRegEx::regExtended|CRegEx::regIgnoreCase));
+ VERIFY(tmp.LoadString(IDS_REGEX_URL));
+ VERIFY(m_reURL.Compile(tmp,CRegEx::regExtended|CRegEx::regIgnoreCase));
}
CDipstickDlg::~CDipstickDlg()
{
}
void CDipstickDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDipstickDlg)
DDX_Control(pDX, IDC_ADVANCED, m_AdvancedCtl);
DDX_Control(pDX, ID_HELP, m_HelpCtl);
DDX_Control(pDX, IDC_EXIT, m_ExitCtl);
DDX_Control(pDX, IDC_OPTIONS, m_OptionsCtl);
DDX_Control(pDX, IDC_MANUALPING, m_ManualPingCtl);
DDX_Control(pDX, IDC_REMOVE, m_RemoveCtl);
DDX_Control(pDX, IDC_REPINGALL, m_RepingAllCtl);
DDX_Control(pDX, IDC_REPING, m_RepingCtl);
DDX_Control(pDX, IDC_CLEAR, m_ClearListCtl);
DDX_Control(pDX, IDC_HOSTLIST, m_HostList);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDipstickDlg, CDialog)
//{{AFX_MSG_MAP(CDipstickDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_OPTIONS, OnOptions)
+ ON_MESSAGE(WM_DNP_URLSPING, OnURLSPing)
ON_MESSAGE(WM_DNP_URLPING, OnUrlPing)
ON_MESSAGE(WM_DNP_ACTIVITYCOUNT, OnActivityCount)
ON_MESSAGE(WM_DNP_UPDATEHOSTDATA, OnUpdateHostData)
ON_MESSAGE(WM_DNP_HTML, OnHTMLTransfer)
ON_NOTIFY(LVN_COLUMNCLICK, IDC_HOSTLIST, OnColumnclickHostlist)
ON_WM_WINDOWPOSCHANGING()
ON_COMMAND(ID_DRAGPAD_OPTIONS, OnDragpadOptions)
ON_COMMAND(ID_DRAGPAD_SHOWMAINWINDOW, OnDragpadShowmainwindow)
ON_COMMAND(ID_DRAGPAD_HELP, OnDragpadHelp)
ON_COMMAND(ID_DRAGPAD_ABOUT, OnDragpadAbout)
ON_COMMAND(ID_DRAGPAD_EXIT, OnDragpadExit)
ON_BN_CLICKED(IDC_MANUALPING, OnManualping)
ON_COMMAND(ID_DRAGPAD_MANUALPING, OnDragpadManualping)
ON_COMMAND(ID_DRAGPAD_REPINGALL, OnDragpadRepingAll)
ON_COMMAND(ID_DRAGPAD_REMOVEALL, OnDragpadRemoveAll)
ON_BN_CLICKED(IDC_REPING, OnReping)
ON_BN_CLICKED(IDC_REPINGALL, OnRepingall)
ON_BN_CLICKED(IDC_CLEAR, OnClear)
ON_NOTIFY(LVN_INSERTITEM, IDC_HOSTLIST, OnInsertitemHostlist)
ON_NOTIFY(LVN_DELETEITEM, IDC_HOSTLIST, OnDeleteitemHostlist)
ON_NOTIFY(LVN_DELETEALLITEMS, IDC_HOSTLIST, OnDeleteallitemsHostlist)
ON_BN_CLICKED(IDC_EXIT, OnExit)
ON_NOTIFY(LVN_ITEMCHANGED, IDC_HOSTLIST, OnItemchangedHostlist)
ON_BN_CLICKED(IDC_REMOVE, OnRemove)
ON_NOTIFY(NM_RCLICK, IDC_HOSTLIST, OnRclickHostlist)
ON_COMMAND(ID_HOSTLIST_REPING, OnHostListReping)
ON_COMMAND(ID_HOSTLIST_REMOVE, OnHostListRemove)
ON_COMMAND(ID_HOSTLIST_REPINGALL, OnHostListRepingAll)
ON_COMMAND(ID_HOSTLIST_REMOVEALL, OnHostListRemoveAll)
ON_COMMAND(ID_HOSTLIST_OPENURL, OnHostListOpenURL)
ON_COMMAND(ID_HOSTLIST_PROPERTIES, OnHostListProperties)
ON_NOTIFY(NM_DBLCLK, IDC_HOSTLIST, OnDblclkHostlist)
ON_WM_CLOSE()
ON_WM_GETMINMAXINFO()
ON_WM_SIZE()
ON_BN_CLICKED(ID_HELP, OnHelp)
ON_NOTIFY(LVN_BEGINDRAG, IDC_HOSTLIST, OnBegindragHostlist)
ON_COMMAND(ID_HOSTLIST_PASTEURL, OnPasteurl)
ON_COMMAND(ID_HOSTLIST_COPYURL, OnHostlistCopyurl)
ON_WM_HELPINFO()
ON_WM_DROPFILES()
ON_BN_CLICKED(IDC_ADVANCED, OnAdvanced)
ON_COMMAND(ID_ADVANCED_EXPORTLIST, OnAdvancedExportlist)
ON_COMMAND(ID_ADVANCED_IMPORTLIST, OnAdvancedImportlist)
ON_COMMAND(ID_DRAGPAD_PASTEURL, OnPasteurl)
ON_COMMAND(ID_DRAGPAD_ADVANCED_EXPORTLIST, OnDragpadAdvancedExportlist)
ON_COMMAND(ID_DRAGPAD_ADVANCED_IMPORTLIST, OnDragpadAdvancedImportlist)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDipstickDlg message handlers
BOOL CDipstickDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
CRect wrci, wrco;
GetWindowRect(&wrco);
GetClientRect(&wrci);
CRect brc1, brc2, lrc;
m_HostList.GetWindowRect(&lrc);
ScreenToClient(&lrc);
m_ManualPingCtl.GetWindowRect(&brc1);
ScreenToClient(brc1);
m_RepingCtl.GetWindowRect(&brc2);
ScreenToClient(&brc2);
m_OverX = lrc.left;
m_OverY = lrc.top;
m_GapY = brc2.top - brc1.bottom;
m_MinSize.cx = m_OverX+m_OverX+m_OverX+brc1.Width()+m_OverX;
m_MinSize.cy = m_OverY+(brc1.Height()+m_GapY)*6+(brc1.Height()+m_GapY)*3+m_OverY;
m_MinSize.cx+=wrco.Width()-wrci.Width();
m_MinSize.cy+=wrco.Height()-wrci.Height();
CWinApp *app = AfxGetApp();
ASSERT(app);
CRect wrc;
wrc.top=app->GetProfileInt("Windows","DlgTop",-1);
wrc.bottom=app->GetProfileInt("Windows","DlgBottom",-1);
wrc.left=app->GetProfileInt("Windows","DlgLeft",-1);
wrc.right=app->GetProfileInt("Windows","DlgRight",-1);
if((wrc.top!=-1|| wrc.bottom!=-1) && wrc.left!=-1&& wrc.right!=-1)
MoveWindow(&wrc);
RecalcLayout(-1,-1);
m_DragPad = new CDragPad();
m_DragPad->m_Daddy=this;
m_DragPad->Create(CDragPad::IDD);
m_Images.Create(16,16,TRUE,4,1);
m_iPending=m_Images.Add(app->LoadIcon(IDI_PENDING));
m_iPinging=m_Images.Add(app->LoadIcon(IDI_PINGING));
m_iCompleted=m_Images.Add(app->LoadIcon(IDI_COMPLETED));
m_iUnreachable=m_Images.Add(app->LoadIcon(IDI_UNREACHABLE));
ASSERT(!(m_iPending<0 || m_iPinging<0 || m_iCompleted<0 || m_iUnreachable<0));
m_HostList.SetImageList(&m_Images,LVSIL_NORMAL);
m_HostList.SetImageList(&m_Images,LVSIL_SMALL);
m_HostList.SetImageList(&m_Images,LVSIL_STATE);
m_HostList.SetTextColor(RGB(255,255,0)); // Yellow
m_HostList.SetTextBkColor(RGB(12,167,0)); // Green
m_HostList.SetBkColor(RGB(12,167,0)); // Green
CRect listrc;
m_HostList.GetClientRect(&listrc);
m_HostList.InsertColumn(0,"Host Name",LVCFMT_LEFT,app->GetProfileInt("Columns","HostName",listrc.Width()-(listrc.Width()*2/16)*4-(listrc.Width()*3/16)),subitemHost);
m_HostList.InsertColumn(1,"Host IP",LVCFMT_LEFT,app->GetProfileInt("Columns","HostIP",listrc.Width()*3/16),subitemIP);
m_HostList.InsertColumn(2,"Min RTT",LVCFMT_RIGHT,app->GetProfileInt("Columns","MinRTT",listrc.Width()*2/16),subitemMin);
m_HostList.InsertColumn(3,"Avg RTT",LVCFMT_RIGHT,app->GetProfileInt("Columns","AvgRTT",listrc.Width()*2/16),subitemAvg);
@@ -1060,328 +1063,367 @@ int cyb = brc.Height()+m_GapY;
m_OptionsCtl.SetWindowPos(NULL,xb,wrc.Height()-m_OverY-cyb-brc.Height(),0,0,SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER|SWP_NOCOPYBITS);
m_HelpCtl.SetWindowPos(NULL,xb,wrc.Height()-m_OverY-cyb*2-brc.Height(),0,0,SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER|SWP_NOCOPYBITS);
}
void CDipstickDlg::OnHelp()
{
WinHelp(0,HELP_FINDER);
}
void CDipstickDlg::UpdateDragpadTitle()
{
int ic = m_HostList.GetItemCount();
CString title;
if(!ic){
VERIFY(title.LoadString(IDS_DRAGPAD_TITLE));
}else{
int ip = 0;
POSITION p = m_Hosts.GetHeadPosition();
while(p){
CPingyHost *host = m_Hosts.GetNext(p);
ASSERT(host);
if(!(host->m_Pinging || host->m_bToPing))
ip++;
}
title.Format(IDS_DRAGPAD_URLS,ip,ic);
}
if(m_DragPad)
m_DragPad->SetWindowText(title);
}
void CDipstickDlg::OnBegindragHostlist(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
if(pNMListView->iItem>=0){
CPingyHost *host = (CPingyHost*) m_HostList.GetItemData(pNMListView->iItem);
ASSERT(host);
if(!host->m_URL.IsEmpty()){
CURLSource us(host->m_URL);
us.DoDragDrop(DROPEFFECT_COPY|DROPEFFECT_LINK);
}
}
*pResult = 0;
}
void CDipstickDlg::OnPasteurl()
{
COleDataObject odo;
if(!odo.AttachClipboard())
return;
CollectURLs(&odo,FALSE);
}
BOOL CDipstickDlg::IsPasteableClipboard()
{
COleDataObject odo;
if(!odo.AttachClipboard())
return FALSE;
if(CollectURLs(&odo,TRUE))
return TRUE;
return FALSE;
}
BOOL CDipstickDlg::CollectURLs(COleDataObject* pDO,BOOL bTestOnly)
{
UINT cfURL = RegisterClipboardFormat("UniformResourceLocator");
UINT cfHTML = RegisterClipboardFormat("HTML Format");
UINT cfNIF = RegisterClipboardFormat("Netscape Image Format");
if(pDO->IsDataAvailable(cfURL)){
if(!bTestOnly){
HGLOBAL hg = pDO->GetGlobalData(cfURL);
ASSERT(hg);
LPVOID lpv = GlobalLock(hg);
ASSERT(lpv);
PostMessage(WM_DNP_URLPING,0,(LPARAM)new CString((LPCTSTR)lpv));
GlobalUnlock(hg);
GlobalFree(hg);
}
return TRUE;
}
if(pDO->IsDataAvailable(cfHTML)){
HGLOBAL hg = pDO->GetGlobalData(cfHTML);
ASSERT(hg);
LPCTSTR lps = (LPCTSTR)GlobalLock(hg);
ASSERT(lps);
BOOL rv = FALSE;
do{
static LPCTSTR v10 = "Version:1.0\r\n";
if(memcmp(v10,lps,strlen(v10)))
break;
static LPCTSTR strStartSelection = "StartSelection:";
static LPCTSTR strEndSelection = "EndSelection:";
static LPCTSTR strSourceURL = "SourceURL:";
LPTSTR subs = strstr(lps,strStartSelection);
if(!subs)
break;
LONG startSelection = atol(&subs[strlen(strStartSelection)]);
if(!startSelection)
break;
if(!(subs=strstr(lps,strEndSelection)))
break;
LONG endSelection = atol(&subs[strlen(strEndSelection)]);
if(!endSelection)
break;
if(!(subs=strstr(lps,strSourceURL)))
break;
subs = &subs[strlen(strSourceURL)];
LPTSTR eol = strchr(subs,'\r');
if(!eol)
break;
(*eol)=0;
CString sourceURL = subs;
if(!sourceURL.GetLength())
break;
TRACE0("SourceURL = "+sourceURL+"\n");
_xferHTML* xf = new _xferHTML;
VERIFY(xf->src.Crack(sourceURL));
LPTSTR htmlBuff = xf->html.GetBuffer(endSelection-startSelection+2);
ASSERT(htmlBuff);
memmove(htmlBuff,&lps[startSelection],endSelection-startSelection+1);
htmlBuff[endSelection-startSelection+1]=0;
xf->html.ReleaseBuffer();
if(bTestOnly){
rv = m_reHTMLURL.Match(xf->html);
delete xf;
}else{
PostMessage(WM_DNP_HTML,0,(LPARAM)xf);
+ rv = TRUE;
}
}while(FALSE);
GlobalUnlock(hg);
GlobalFree(hg);
return rv;
}
if(pDO->IsDataAvailable(cfNIF)){
if(!bTestOnly){
HGLOBAL hg = pDO->GetGlobalData(cfNIF);
ASSERT(hg);
LPVOID lpv = GlobalLock(hg);
ASSERT(lpv);
struct _nif {
BYTE data[0x20];
DWORD m_offDescr;
DWORD m_offURL;
DWORD m_offToImage;
} *nif = (_nif*)lpv;
PostMessage(WM_DNP_URLPING,0,(LPARAM)new CString((LPCTSTR)&nif->data[nif->m_offURL]));
GlobalUnlock(hg);
GlobalFree(hg);
}
return TRUE;
}
if(pDO->IsDataAvailable(CF_TEXT)){
- if(!bTestOnly){
+ BOOL rv = FALSE;
HGLOBAL hg = pDO->GetGlobalData(CF_TEXT);
ASSERT(hg);
LPVOID lpv = GlobalLock(hg);
ASSERT(lpv);
+ if(bTestOnly){
+ rv = m_reURL.Match((LPCSTR)lpv);
+ }else{
+ CString sel = (LPCSTR)lpv;
+ if(m_reURL.Match(sel)) {
+ rv = TRUE;
+ CString tmp = m_reURL.GetMatch(CRegEx::matchPostMatch);
+ if(m_reURL.Match(tmp)) {
+ PostMessage(WM_DNP_URLSPING,0,(LPARAM)new CString((LPCSTR)lpv));
+ }else{
PostMessage(WM_DNP_URLPING,0,(LPARAM)new CString((LPCTSTR)lpv));
+ }
+ }
+ }
GlobalUnlock(hg);
GlobalFree(hg);
- }
- return TRUE;
+ return rv;
}
return FALSE;
}
LRESULT CDipstickDlg::OnHTMLTransfer(WPARAM wP, LPARAM lP)
{
_xferHTML* xf = (_xferHTML*)lP;
CSelectURLsDlg su;
su.m_bAdjust = m_bSmartAdjust;
su.m_bAdjustInnermost = m_bAdjustInnermost;
BeginWaitCursor();
while(m_reHTMLURL.Match(xf->html)){
CString url = m_reHTMLURL.GetMatch(1);
xf->html = m_reHTMLURL.GetMatch(CRegEx::matchPostMatch);
if(CCrackURL::GetHostName(url)==url){
if(m_bIgnorelative)
continue;
CCrackURL cracked;
VERIFY(cracked.Crack(url));
cracked.Adjust(xf->src);
url = cracked.Build();
}
if(!su.m_URLs.Find(url))
VERIFY(su.m_URLs.AddTail(url));
}
EndWaitCursor();
delete xf;
if(su.DoModal()==IDOK){
POSITION p = su.m_URLs.GetHeadPosition();
while(p){
CString url = su.m_URLs.GetNext(p);
PostMessage(WM_DNP_URLPING,0,(LPARAM)new CString(url));
}
}
return 0;
}
+LRESULT CDipstickDlg::OnURLSPing(WPARAM wP,LPARAM lP)
+{
+CString *sel = (CString*)lP;
+TRACE1("URLS: %s\n",(LPCTSTR)*sel);
+CSelectURLsDlg su;
+ su.m_bAdjust = m_bSmartAdjust;
+ su.m_bAdjustInnermost = m_bAdjustInnermost;
+ BeginWaitCursor();
+ while(m_reURL.Match(*sel)){
+ CString url = m_reURL.GetMatch(CRegEx::matchMatch);
+ (*sel) = m_reURL.GetMatch(CRegEx::matchPostMatch);
+ if(!su.m_URLs.Find(url))
+ VERIFY(su.m_URLs.AddTail(url));
+ }
+ EndWaitCursor();
+ delete sel;
+ if(su.DoModal()==IDOK){
+ POSITION p = su.m_URLs.GetHeadPosition();
+ while(p){
+ CString url = su.m_URLs.GetNext(p);
+ PostMessage(WM_DNP_URLPING,0,(LPARAM)new CString(url));
+ }
+ }
+ return 0;
+}
+
UINT CPingyHost::PingProc(LPVOID pParam)
{
CPingyHost* pThis = (CPingyHost*)pParam;
ASSERT(pThis->m_Daddy);
return pThis->DoPing();
}
UINT CPingyHost::DoPing()
{
CSingleLock slock(&m_mutex);
if(!slock.Lock(2000)){
m_Status.LoadString(IDS_PS_FAILEDTOLOCK);
return 1;
}
m_Pinging = TRUE;
m_bToPing = FALSE;
VERIFY(m_Daddy->m_eveStartup.SetEvent());
m_Daddy->PostMessage(WM_DNP_UPDATEHOSTDATA,0,(LPARAM)this);
in_addr ip;
UINT packets, lost;
packets=lost=0;
CICMP* pICMP = NULL;
UINT dataSize = m_Daddy->m_PingSize;
int ploss = 100;
do{
if(m_HostName.IsEmpty()){
ip.s_addr = m_IP;
hostent *he = gethostbyaddr((char*)&ip,4,PF_INET);
if(!he)
m_HostName.Empty();
else
m_HostName = he->h_name;
}else{
hostent *he = gethostbyname(m_HostName);
if(!he){
m_Status.LoadString(IDS_PS_FAILEDTORESOLVE);
m_IP = 0;
break;
}
memmove(&ip.s_addr,he->h_addr,sizeof(ip.s_addr));
m_IP = ip.s_addr;
}
pICMP = CICMP::CreateICMP();
if(!(pICMP && pICMP->Initialize())){
m_Status.LoadString(IDS_PS_UNABLETOICMP);
break;
}
m_Daddy->PostMessage(WM_DNP_UPDATEHOSTDATA,0,(LPARAM)this);
ULONG sum, num, pmin, pmax;
sum = num = 0;
pmin = 0xFFFFFFFF;
pmax = 0;
UINT seq;
for(seq = 0;seq<m_Daddy->m_PingsPerHost;seq++){
INT status;
LONG rtt = pICMP->Ping(ip,dataSize,m_Daddy->m_TimeOut,&status);
UINT stString = 0;
switch(status){
case CICMP::ipSuccess:
sum+=rtt; num++;
if(rtt<pmin)
pmin=rtt;
if(rtt>pmax)
pmax=rtt;
break;
case CICMP::ipBuffTooSmall: stString = IDS_PS_INTERNALERROR;
break;
case CICMP::ipDestNetUnreachable: stString = IDS_PS_NETUNREACHABLE;
break;
case CICMP::ipDestHostUnreachable: stString = IDS_PS_HOSTUNREACHABLE;
break;
case CICMP::ipDestProtUnreachable: stString = IDS_PS_PROTUNREACHABLE;
break;
case CICMP::ipDestPortUnreachable: stString = IDS_PS_PORTUNREACHABLE;
break;
case CICMP::ipNoResources: stString = IDS_PS_NORESOURCES;
break;
case CICMP::ipBadOption: stString = IDS_PS_INTERNALERROR;
break;
case CICMP::ipHWError: stString = IDS_PS_HWERROR;
break;
case CICMP::ipPacketTooBig: stString = IDS_PS_BIGPACKET;
break;
case CICMP::ipTimeOut: stString = IDS_PS_TIMEOUT;
break;
case CICMP::ipBadRequest: stString = IDS_PS_INTERNALERROR;
break;
case CICMP::ipBadRoute: stString = IDS_PS_BADROUTE;
break;
case CICMP::ipTTLExpiredInTransit: stString = IDS_PS_TTLEXPTRANSIT;
break;
case CICMP::ipTTLExpiredInReasm: stString = IDS_PS_TTLEXPREASM;
break;
case CICMP::ipParamProblem: stString = IDS_PS_IPARAMP;
break;
case CICMP::ipSourceQuench: stString = IDS_PS_SOURCEQUENCH;
break;
case CICMP::ipOptionTooBig: stString = IDS_PS_BIGOPTION;
break;
case CICMP::ipBadDest: stString = IDS_PS_BADDEST;
break;
default:
stString = IDS_PS_UNKNOWNERROR;
break;
}
if(stString)
m_Status.LoadString(stString);
packets++;
if(rtt<0)
lost++;
Sleep(m_Daddy->m_Interval);
}
if(packets==lost || !packets){
ploss=100;
}else{
ploss = lost*100/packets;
m_MinRTT = pmin;
m_MaxRTT = pmax;
if(sum){
ASSERT(num);
m_AvgRTT = sum/num;
}else{
m_AvgRTT = 0;
}
}
}while(FALSE);
m_Packetloss=ploss;
m_Pinged=TRUE;