00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "stdafx.h"
00037 #include "PingSocket.h"
00038
00039 #include "ErrorHandlerMacros.h"
00040
00041 #ifdef _MEMORY_DEBUG
00042 #define new DEBUG_NEW
00043 #define malloc DEBUG_MALLOC
00044 static char THIS_FILE[] = __FILE__;
00045 #endif
00046
00047 KOMODIA_NAMESPACE_START
00048
00049 #define CPingSocket_Class "CPingSocket"
00050
00051 CPingSocket::CPingSocket() : CICMPSocketAsync(FALSE),
00052 m_bPingDone(TRUE),
00053 m_bSameID(FALSE),
00054 m_usLastPingID(0),
00055 m_usLastPingSequence(0)
00056 {
00057 try
00058 {
00059
00060 SetName(CPingSocket_Class);
00061
00062
00063 while (!m_usLastPingID)
00064 m_usLastPingID=GetTickCount();
00065 }
00066 ERROR_HANDLER("CPingSocket")
00067 }
00068
00069 CPingSocket::~CPingSocket()
00070 {
00071 }
00072
00073 BOOL CPingSocket::OnSocketReceive(int iErrorCode)
00074 {
00075 try
00076 {
00077
00078 if (CICMPSocketAsync::OnSocketReceive(iErrorCode))
00079 {
00080
00081 if ((IsIDOK(GetLastICMPHeader()->sICMP.sUS.us1,
00082 GetLastICMPHeader()->sICMP.sUS.us2) &&
00083 GetLastICMPHeader()->ucICMPType==ICMP_Echo_Reply) ||
00084 GetLastICMPHeader()->ucICMPType==ICMP_Time)
00085 {
00086
00087 if (m_dwTimeout)
00088
00089 CAsyncSocket::KillTimer();
00090
00091
00092
00093 m_dwTimeElapsed=GetTickCount()-m_dwLastTick;
00094
00095
00096 m_ucTTL=GetLastIPHeader()->ucTTL;
00097
00098
00099 m_iDataSize=GetLastDataSize();
00100
00101
00102 m_aReplyingAddress=GetLastIPHeader()->ulSourceAddress;
00103
00104
00105 m_bPingDone=TRUE;
00106
00107
00108 m_bTimeout=FALSE;
00109
00110
00111 PingDone(FALSE);
00112
00113
00114 unsigned short usSequence;
00115 if (GetLastICMPHeader()->ucICMPType==ICMP_Time)
00116 usSequence=GetLastICMPICMPHeader()->sICMP.sUS.us1;
00117 else
00118 usSequence=GetLastICMPHeader()->sICMP.sUS.us1;
00119
00120
00121 PingDoneData(m_aReplyingAddress,
00122 m_ucTTL,
00123 m_dwTimeElapsed,
00124 m_iDataSize,
00125 usSequence,
00126 GetLastICMPHeader()->ucICMPType==ICMP_Time);
00127 }
00128 }
00129
00130
00131 return TRUE;
00132 }
00133 ERROR_HANDLER_RETURN("OnSocketReceive",TRUE)
00134 }
00135
00136 BOOL CPingSocket::IsPingDone()const
00137 {
00138 return m_bPingDone;
00139 }
00140
00141 void CPingSocket::SetUseSameID(BOOL bSameID)
00142 {
00143 m_bSameID=bSameID;
00144 }
00145
00146 BOOL CPingSocket::Ping(const std::string& rDestinationAddress,
00147 DWORD dwTimeout)
00148 {
00149 try
00150 {
00151 return Ping(StringToLong(rDestinationAddress),
00152 dwTimeout);
00153 }
00154 ERROR_HANDLER_RETURN("Ping",FALSE)
00155 }
00156
00157 BOOL CPingSocket::Ping(IP aDestinationAddress,
00158 DWORD dwTimeout)
00159 {
00160 try
00161 {
00162
00163 m_ucTTL=0;
00164 m_aReplyingAddress=0;
00165 m_bTimeout=FALSE;
00166 m_dwTimeElapsed=0;
00167 m_iDataSize=0;
00168 m_dwTimeout=dwTimeout;
00169
00170
00171 if (dwTimeout)
00172
00173 SetTimeout(dwTimeout);
00174
00175
00176 m_dwLastTick=GetTickCount();
00177
00178
00179 m_bPingDone=FALSE;
00180
00181 if (!m_bSameID)
00182
00183 m_usLastPingID=GetTickCount()*GetCurrentProcessId();
00184
00185
00186 return SendEcho(aDestinationAddress,
00187 FALSE,
00188 m_usLastPingID,
00189 m_usLastPingSequence,
00190 0)!=GetErrorCode();
00191 }
00192 ERROR_HANDLER_RETURN("Ping",FALSE)
00193 }
00194
00195 BOOL CPingSocket::OnSocketTimeout()
00196 {
00197 try
00198 {
00199
00200 KillTimer();
00201
00202
00203 m_bPingDone=TRUE;
00204
00205
00206 m_bTimeout=TRUE;
00207
00208
00209 PingDone(TRUE);
00210
00211
00212 return FALSE;
00213 }
00214 ERROR_HANDLER_RETURN("OnSocketTimeout",FALSE)
00215 }
00216
00217 void CPingSocket::PingDone(BOOL bTimeout)
00218 {
00219 }
00220
00221 IP CPingSocket::GetReplyingAddress()const
00222 {
00223 return m_aReplyingAddress;
00224 }
00225
00226 BOOL CPingSocket::IsTimeout()const
00227 {
00228 return m_bTimeout;
00229 }
00230
00231 DWORD CPingSocket::GetTimeElapsed()const
00232 {
00233 return m_dwTimeElapsed;
00234 }
00235
00236 unsigned char CPingSocket::GetTTL()const
00237 {
00238 return m_ucTTL;
00239 }
00240
00241 int CPingSocket::GetDataSize()const
00242 {
00243 return m_iDataSize;
00244 }
00245
00246 void CPingSocket::PingDoneData(IP aReplyingAddress,
00247 unsigned char ucTTL,
00248 DWORD dwTimeElapsed,
00249 int iDataSize,
00250 unsigned short usID,
00251 BOOL bTTLExpired)
00252 {
00253 }
00254
00255 void CPingSocket::SetID(unsigned short usID)
00256 {
00257 m_usLastPingID=usID;
00258 }
00259
00260 void CPingSocket::SetSequence(unsigned short usSequence)
00261 {
00262 m_usLastPingSequence=usSequence;
00263 }
00264
00265 BOOL CPingSocket::IsIDOK(unsigned short usID,
00266 unsigned short usSequence)const
00267 {
00268 return m_usLastPingID==usID &&
00269 m_usLastPingSequence==usSequence;
00270 }
00271
00272 KOMODIA_NAMESPACE_END