📄 icmp.cpp
字号:
icmpCurSeq = copy.icmpCurSeq;
icmpCurId = copy.icmpCurId;
icmpRcvLen = copy.icmpRcvLen;
}
/////////////////////////////////////////////////////////////////////////////
// CIcmp member functions
//////////////////////////////////////////////////////////////////////////////////
// //
// OpenNewSocket //
// Opens a SOCKET connection to a RAW socket with ICMP protocol. //
// //
// Requires a handle to a message processor which will process the //
// socket events, the message number to process, and a mask giving //
// the events to process. //
// //
// Sets icmpSocketError and icmpSocketErrorMod to indicate the type //
// of error, and returns TRUE if successful, else FALSE. //
// //
// icmpSocketError and icmpSocketErrorMod are preserved until the next //
// operation on this Icmp object is performed. //
// //
//////////////////////////////////////////////////////////////////////////////////
//打开一个SOCKET连接使用ICMP协议,需要一个句柄去处理socket事件,
BOOL CIcmp::OpenNewSocket(HWND hWnd, unsigned int NotificationMessage, long NotifyEvents)
{
//调用另外一个多态函数
return OpenNewSocket (hWnd,
NotificationMessage,
NotifyEvents,
AF_INET,
SOCK_RAW,
IPPROTO_ICMP);
}
//////////////////////////////////////////////////////////////////////////////////
// //
// OpenNewSocket //
// Opens a SOCKET connection to a socket of specified family, type //
// and protocol //
// //
// Requires a handle to a message processor which will process the //
// socket events, the message number to process, and a mask giving //
// the events to process. //
// //
// Sets icmpSocketError and icmpSocketErrorMod to indicate the type //
// of error, and returns TRUE if successful, else FALSE. //
// //
// icmpSocketError and icmpSocketErrorMod are preserved until the next //
// operation on this Icmp object is performed. //
// //
//////////////////////////////////////////////////////////////////////////////////
//打开一个连接,确定具体的family,类型和协议
BOOL CIcmp::OpenNewSocket(HWND hWnd, unsigned int NotificationMessage, long NotifyEvents, int AFamily, int AType, int AProtocol)
{
if (icmpSocket != INVALID_SOCKET)
CloseIcmpSocket();
if (!Connect(&icmpPingTimer,
&icmpPingTimer,
AFamily,
AType,
AProtocol))
return FALSE;
//
// Set asynchronous notification and start message interrupt processor
//
if (SetAsynchNotification(hWnd,
NotificationMessage,
NotifyEvents) == SOCKET_ERROR)
{
return FALSE;
}
return TRUE;
}
//////////////////////////////////////////////////////////////////////////////////
// //
// Connect //
// Connects an Icmp object to a RAW socket with ICMP protocol. //
// //
// Requires 2 integer values specifying the receive and transmit //
// timeouts //
// //
// Sets icmpSocketError and icmpSocketErrorMod to indicate the type //
// of error, and returns TRUE if successful, else FALSE. //
// //
// icmpSocketError and icmpSocketErrorMod are preserved until the next //
// operation on this Icmp object is performed. //
// //
//////////////////////////////////////////////////////////////////////////////////
//连接一个Icmp对象需要两个整数变量来保存接收和发送超时时间
BOOL CIcmp::Connect(int ReceiveTimeout, int SendTimeout)
{
return Connect(&ReceiveTimeout,
&SendTimeout,
AF_INET,
SOCK_RAW,
IPPROTO_ICMP);
}
//////////////////////////////////////////////////////////////////////////////////
// //
// Connect //
// Connects an Icmp object to a socket of specified family, type //
// and protocol //
// //
// Requires 2 integer values specifying the receive and transmit //
// timeouts, the ICMP family, ICMP socket type, and ICMP protocol. // //
// //
// Sets icmpSocketError and icmpSocketErrorMod to indicate the type //
// of error, and returns TRUE if successful, else FALSE. //
// //
// icmpSocketError and icmpSocketErrorMod are preserved until the next //
// operation on this Icmp object is performed. //
// //
//////////////////////////////////////////////////////////////////////////////////
BOOL CIcmp::Connect(LPINT ReceiveTimeout, LPINT SendTimeout, int AFamily, int AType, int AProtocol)
{
int Result;
icmpSocket = NULL;
icmpSocket = socket (AFamily,
AType,
AProtocol);
if (icmpSocket == INVALID_SOCKET)
{
icmpSocketError = WSAGetLastError();
icmpSocketErrorMod = 1;
return FALSE;
}
//
// 设定接收超时时间
//
Result = setsockopt (icmpSocket,
SOL_SOCKET,
SO_RCVTIMEO,
(char *)ReceiveTimeout,
sizeof(int));
if (Result == SOCKET_ERROR)
{
icmpSocketError = WSAGetLastError();
icmpSocketErrorMod = 2;
closesocket (icmpSocket);
icmpSocket = INVALID_SOCKET;
return FALSE;
}
//
// 设定发送超时时间
//
Result = setsockopt (icmpSocket,
SOL_SOCKET,
SO_SNDTIMEO,
(char *)SendTimeout,
sizeof(int));
if (Result == SOCKET_ERROR)
{
icmpSocketError = WSAGetLastError();
icmpSocketErrorMod = 3;
closesocket (icmpSocket);
icmpSocket = INVALID_SOCKET;
return FALSE;
}
icmpCurSeq = 0;
icmpCurId = (USHORT)GetCurrentProcessId();
icmpHops = 0;
return TRUE;
}
//////////////////////////////////////////////////////////////////////////////////
// //
// CloseIcmpSocket //
// Closes a socket associated with an Icmp object. //
// //
// Sets icmpSocketError and icmpSocketErrorMod to indicate the type //
// of error, and returns the integer result of the operation. //
// //
// icmpSocketError and icmpSocketErrorMod are preserved until the next //
// operation on this Icmp object is performed. //
// //
//////////////////////////////////////////////////////////////////////////////////
int CIcmp::CloseIcmpSocket(void)
{
if (icmpSocket == INVALID_SOCKET)
return 0;
int Result = closesocket(icmpSocket);
if (Result == SOCKET_ERROR)
{
icmpSocketError = WSAGetLastError();
icmpSocketErrorMod = 1;
icmpSocket = INVALID_SOCKET;
}
return Result;
}
//////////////////////////////////////////////////////////////////////////////////
// //
// SetAsynchNotification //
// Sets the window (process) to notify when a network event on the //
// socket associated with the Icmp object occurs. //
// //
// Requires the message number to send to the window, and the events //
// to notify on. //
// //
// Sets icmpSocketError and icmpSocketErrorMod to indicate the type //
// of error, and returns the integer result of the operation. //
// //
// icmpSocketError and icmpSocketErrorMod are preserved until the next //
// operation on this Icmp object is performed. //
// //
//////////////////////////////////////////////////////////////////////////////////
//消息处理函数,当有网络事件产生的时候
int CIcmp::SetAsynchNotification(HWND hWnd, unsigned int Message, long Events)
{
//异步选择事件的I/O方法
int Result = WSAAsyncSelect (icmpSocket,
hWnd,
Message,
Events);
if (Result == SOCKET_ERROR)
{
icmpSocketError = WSAGetLastError();
icmpSocketErrorMod = 1;
icmpSocket = INVALID_SOCKET;
}
return Result;
}
//////////////////////////////////////////////////////////////////////////////////
// //
// SetTTL //
// Set the TTL option on the socket associated with an Icmp object. //
// A TTL value of non-zero specifies the number of hops to query //
// before a response is returned. By incrementing the TTL from 1 to //
// (number of hops to final destination), a TraceRoute function //
// can be performed. //
// //
// Sets icmpSocketError and icmpSocketErrorMod to indicate the type //
// of error, and returns the integer result of the operation. //
// //
// icmpSocketError and icmpSocketErrorMod are preserved until the next //
// operation on this Icmp object is performed. //
// //
//////////////////////////////////////////////////////////////////////////////////
//设定TTL
int CIcmp::SetTTL(int TTL)
{
int Result;
Result = setsockopt (icmpSocket, IPPROTO_IP, IP_TTL, (LPSTR)&TTL, sizeof(int));
if (Result == SOCKET_ERROR)
{
icmpSocketErrorMod = 1;
icmpSocketError = WSAGetLastError();
}
return Result;
}
//////////////////////////////////////////////////////////////////////////////////
// //
// Ping //
// Sends an ICMP ping message to the host indicated in the Icmp //
// variable icmpSockAddr. The Icmp variables icmpCurSeq and //
// icmpCurId are sent in the buffer referenced by pIcmpBuffer. The //
// referenced buffer must be large enough to hold DataLen bytes plus //
// the size of the ICMP message header. //
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -