CIcmp Class Requirements
The CIcmp class, derived from the MFC CSocket class,
was developed to fill the requirement for class-based access to RAW
sockets to perform ICMP echo
requests. This class was designed using Microsoft's VisualC++ version 4.2 and
MFC, and was tested on both a WindowsNT 4.0 Workstation and Server using
WindowsNT Winsock 2. Because of its reliance upon WindowsNT Winsock 2 structures,
it will not work on Windows95. A version which will work with Windows95 will be
available soon.
This version of the CIcmp class implements only the Echo Request
message definition of RFC792. Refer to RFC792 - Internet Control Message
Protocol, DARPA Internet Program, Protocol Specification for details.
The CIcmp class can be easily expanded to support the complete RFC792
specification.
Class Methods
BOOL OpenNewSocket
(HWND hWnd,
unsigned int NotificationMessage,
long NotifyEvents,
int AFamily,
int AType,
int AProtocol)
Opens a SOCKET of type AType belonging to family
AFamily and with protocol AProtocol.
If AFamily, AType and AProtocol are not provided, opens
a SOCKET connection to a RAW socket with ICMP protocol.
hWnd is a handle (GetSafeHwnd) to the window containing the
message processor which will process the asynchronous
socket events, NotificationMessage is the user-defined message number
to associate with socket events, and NotifyEvents is the 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 CIcmp object is performed.
BOOL Connect
(LPINT ReceiveTimeout,
LPINT SendTimeout)
BOOL Connect
(LPINT ReceiveTimeout,
LPINT SendTimeout,
int AFamily,
int AType,
int AProtocol)
Connects a CIcmp object to a socket
of specified family (AFamily), type (AType) and
protocol (AProtocol). If AFamily, AType
and AProtocol are not provided, connects a CIcmp
object to a RAW socket with ICMP protocol.
ReceiveTimeout and SendTimeout specifies 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 CIcmp object is performed.
int CloseIcmpSocket(void)
Closes a socket associated with a CIcmp 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 CIcmp object is performed.
int SetAsynchNotification
(HWND hWnd,
unsigned int Message,
long Events)
Sets the window (process) to notify when a network event on the
socket associated with the CIcmp object occurs.
Message is the message number to send to the window,
and Events is the event mask 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 CIcmp object is performed.
int SetTTL (int TTL)
Set the TTL (Time-To-Live) option on the socket associated with a
CIcmp object. The TTL value specifies the number of seconds
an Internet datagram is to exist.
Since each gateway encountered along the trip to the specified host
decrements the TTL value by 1, TTL specifies the number of hops (gateways)
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. In order to reach a designated host, TTL must be at
least the number of gateways encountered on the way to the host, plus 1
(for the host itself).
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 CIcmp object is performed.
int Ping (LPSTR pIcmpBuffer, int IcmpBufferSize)
Sends an ICMP Echo Request (ping) message to the host indicated
in the CIcmp
variable icmpSockAddr. The CIcmp 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.
The CIcmp variable icmpPingSentAt is set to the time value at the
time the ping message is sent.
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 CIcmp object is performed.
unsigned short IcmpChecksum (unsigned short FAR *lpBuf, int Len)
unsigned long GetIPAddress (LPSTR iHostName)
Returns the value of the IP address of the host pointed by
iHostName as an unsigned long in network byte order, as described
in inet_aton.
Sets iHostName to the value of the host name returned in
icmpHostEnt.
Requires the message number to send to the window, and the events
to notify on.
Sets CIcmp variable icmpSockAddr fields appropriately,
leaving the IP address in icmpSockAddr.sin_addr, and the
type of address in
icmpSockAddr.sin_family. Also sets icmpHostEnt fields appropriately
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 CIcmp object is performed.
int Receive (LPSTR pIcmpBuffer, int IcmpBufferSize)
Reads the socket associated with the CIcmp object and returns the
number of bytes read, or SOCKET_ERROR, if an error occurs.
pIcmpBuffer is the address of the buffer to store the incoming message
and IcmpBufferSize is the integer buffer size. The buffer must be large
enough to contain the incoming message. In the case of an ICMP Echo
Reply, the size would be the size of the outgoing message plus
the size of an ICMP header plus the size of an IP header.
Sets icmpRcvLen to the length of the message received,
icmpPingReceivedAt to the time the receive command is issued,
icmpRoundTripTime to the difference between icmpPingSentAt
and icmpPingReceivedAt, icmpCurSeq to the incoming
sequence number and icmpCurId to the incoming id field.
Sets rcvSockAddr fields appropriately.
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 CIcmp object is performed.
void DisplayError (CString ErrorType, CString FunctionName)
Displays the provided ErrorType message prior to the decoded error
message in icmpSocketError and icmpSocketErrorMod.
Sets the message box title to FunctionName.
The message is displayed in a modal box, requiring an OK response
before the thread containing this method can progress.
Class Variables
The CIcmp class contains the following public variables:
- LPIcmpHeader pIcmpHeader