15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


CIcmp - это ICMP класс для MFC


Автор JayWheeler.


CIcmp - это ICMP класс для MFC

 


Описание ICMP

    В ICMP определены следующие типы сообщений:

      Номер Сообщения Тип Сообщения Описание
      0 Echo Reply Ответ на Эхо-запрос
      3 Destination Unreachable Неизвесная Сеть или Host
      4 Source Quench Слишком длинное сообщение или вернулось слишком быстро
      5 Redirect The host can be reached through a different gateway
      8 Echo Request Запрос шлюзу/узлу вернуть посланный пакет
      11 Time Exceeded TTL exceeded
      12 Parameter Problem Неверные параметры в заголовке сообщения
      13 Timestamp Requests gateway/host time in mSec
      14 Timestamp Reply Gateway/host time in mSec
      15 Information Request Request host network number
      16 Information Reply Host network number

CIcmp требования

 

    The CIcmp класс наследуется от MFC классаCSocket. Этот класс был разработан, используя Microsoft VisualC++ версии 4.2 и MFC, и был протестирован как на WindowsNT 4.0 Workstation так и на Server используя WindowsNT Winsock 2.

    Эта версия класса CIcmp включает в себя только Эхо-запросы описаные в RFC792. Для более детального изучения, почитайте RFC792 - Internet Control Message Protocol, DARPA Internet Program, Protocol Specification. CIcmp может быть легко расширен для полной поддержки спецификации RFC792.

 


Методы класса

 


 

    BOOL OpenNewSocket

      (HWND hWnd,
      unsigned int NotificationMessage,
      long NotifyEvents)

    BOOL OpenNewSocket

      (HWND hWnd,
      unsigned int NotificationMessage,
      long NotifyEvents,
      int AFamily,
      int AType,
      int AProtocol)

      Открывает SOCKET типа AType семейства AFamily и протоколом AProtocol. Если AFamily, AType и AProtocol не поддерживаются, то SOCKET становится RAW сокетом с ICMP протоколом.

      hWnd это хэндл (GetSafeHwnd) окна, которое будет обрабатывать сообщения поступающие с ассинхронного сокета, NotificationMessage сообщения, которые должен обрабатывать сокет, и NotifyEvents это сообщение, которое будет поступать в окно при возникновении на сокете события.

      icmpSocketError и icmpSocketErrorMod будут содержать TRUE при удачном завершении операции, иначе FALSE.


    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)

      Computes the ICMP checksum of the data in the buffer referenced by lpBuf for Len bytes.

      Returns an unsigned integer value of the computed checksum.


    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
  • Указатель на структуру IcmpHeader;
  • Usually mapped to the current ICMP buffer.
LPIpHeader pIpHeader
  • Указатель на структуру IpHeader;
  • Usually mapped to the current receive buffer.
SOCKET icmpSocket
  • An asynchronous socket. See CSocket class for implementation;
  • Contains the current socket for this CIcmp object;
  • Default is INVALID_SOCKET for unopened/unassigned socket.
SOCKADDR_IN icmpSockAddr
  • Socket address structure for outgoing (send) socket communication;
  • Refer to SOCKADDR_IN structure in VisualC++ help or MFC documentation.
SOCKADDR_IN rcvSockAddr
  • Socket address structure for incoming (recvfrom) socket communication;
  • Refer to SOCKADDR_IN structure in VisualC++ help or MFC documentation.
DWORD icmpRoundTripTime
  • Time between sending a message and receiving the echo;
  • in mSec.
unsigned long icmpHostAddress
  • Host address in network byte order;
  • Maintained external to the CIcmp class;
  • see inet_aton for format.
DWORD icmpPingSentAt
  • System time when the last ICMP message was sent;
  • in mSec.
DWORD icmpPingReceivedAt
  • System time when the last ICMP message was received;
  • in mSec.
int icmpRcvLen
  • integer length of the last message received;
  • set by Receive.
int icmpHops
  • Number of hops (routers contacted) in a trace route operation;
  • Set to zero by Connect method;
  • Maintained external to class (by application program).
int icmpMaxHops
  • A limit set for trace route operations which fail to complete gracefully;
  • Defaults to 30 in the CIcmp constructor;
  • Maintained external to class (by application program).
int icmpCurSeq
  • Current ICMP message sequence number;
  • Set by application program before calling Ping;
  • Set by Receive after processing an incoming (recvfrom) message.
int icmpCurId
  • ICMP id field;
  • Set to current process id by Connect;
  • Assigned to outgoing ICMP message by Ping;
  • Set to incoming ICMP id field by Receive
int icmpPingTimer
  • send (Ping) timeout in mSec;
  • recvfrom (Receive) timeout in mSec.
int icmpSocketError
  • The WSAGetLastError for the last error which occurred;
  • Preserved until the next socket error occurs.
int icmpSocketErrorMod
  • The entry level in the processing routine at which icmpSocketError occurred;
  • Preserved until the next socket error occurs.

 


Инсталляция

 

    To use this class,

    1. Скопируйте файлы Icmp.cpp, Icmp.h и Icmp.rc в директорию Вашего проекта;
    2. Включите Icmp.cpp и Icmp.rc в Ваш проект. Может выскочить сообщение об ошибке, это потому что .rc может использоваться...пропустите, далее мы это исправим;
    3. Откройте файл Icmp.rc, выберите таблицу строк, и выделите все сообщения;
    4. Скопируйте строковые ресурсы в клипбоард (кнопка copy);
    5. Open the projects' string table and paste the strings into the next available slot (paste button);
    6. Close both string tables and remove (delete) the file Icmp.rc from your project;
    7. Add Icmp.h to any modules in which you make reference to CIcmp or one of its' methods or variables. This will normally take the form of

        #include "Icmp.h"

      in the header files in which the CIcmp object is declared.

 


EWDPing - демонстрирует использование класса CIcmp

    EWDPing is a sample program which illustrates the use of the CIcmp class.

    This program implements the ICMP Echo Request message (commonly known as PING) to check the on-line status of an Internet host, and uses TTL to trace the route to an Internet host.

    The .zip file contains the source for EWDPing as well as the source for the other custom classes used in the project:

      CIcmp
      An object class derived from the MFC CSocket object class to provide access to RAW sockets and ICMP (Internet Control Message Protocol) functions.
      CRegKey
      An object class to provide to WindowsNT/95 system registry objects.
      CSysTCPIP
      An object class to provide access to some of the TCP/IP related information stored in the system registry on Windows NT using Microsoft's Winsock 2 interface.

    The program also makes use of a system timer to prevent "hanging" on a non-responsive gateway.

    Documentation for the methods and variables are provided within the .zip file.

Скачать Демо программку - 151KB
Скачать исходник класса - 26KB