如何编程检测到自己被PING?象天网那样??

解决方案 »

  1.   

    需要写IP网络层的抓包程序,推荐你看一个开发原码的工程Winpcap:
    http://winpcap.polito.it/
    如果你想研究在Unix-like平台下如何做这事,看libcap:
    http://www.tcpdump.org/它们都有原码。
      

  2.   

    Ping程序是利用ICMP数据包来发送/接收的,并且有特定的包头信息,你捕获网卡上的ICMP数据包,然后分析他的包结构,可以得出这是否一个ping的数据包
      

  3.   

    因为ICMP在IP层,所以利用RAW_socket编程就能查出来的
    不难的。
      

  4.   

    抓icmp包。。http://expert.csdn.net/Expert/topic/2333/2333459.xml?temp=.3471796
      

  5.   

    我已经解决了,是用 RAW_socket,有空我把源码贴上来。
    现在我想实现像防火墙那样关闭一个端口的功能,哪位大侠给点提示!
      

  6.   

    这是我做东西中监测ping的线程,供大家参考,另外,现在想实现像防火墙那样关闭一个端口的功能,哪位大侠给点提示!最好是在应用层,我没有时间学写驱动typedef struct _ICMP
    {
    BYTE i_type;
    BYTE i_code;
    USHORT i_cksum;
    USHORT i_id;
    USHORT i_seq;
    } ICMP;
    typedef ICMP * LPICMP;
    typedef ICMP UNALIGNED * ULPICMP;typedef struct _IP{
    union

    BYTE Version; // °æ±¾
    BYTE HdrLen; // IHL
    };
    BYTE ServiceType; // ·þÎñÀàÐÍ
    WORD TotalLen; // ×ܳ¤
    WORD ID; // ±êʶ
    union

    WORD Flags; // ±êÖ¾
    WORD FragOff; // ·Ö¶ÎÆ«ÒÆ
    };
    BYTE TimeToLive; // ÉúÃüÆÚ
    BYTE Protocol; // Ð­Òé
    WORD HdrChksum; // Í·Ð£ÑéºÍ
    DWORD SrcAddr; // Ô´µØÖ·
    DWORD DstAddr; // Ä¿µÄµØÖ·
    BYTE Options; // Ñ¡Ïî
    } IP; 
    typedef IP * LPIP;
    typedef IP UNALIGNED * ULPIP;
    //////////////////////////////
    UINT PingThread(LPVOID lp)
    {
    CRightView * pDlg=(CRightView*)lp;
    BOOL mobileflag=pDlg->user_config.PortConfig.sendflag; WSADATA WSAData; 
    SOCKET sock; 
    int flag=0;
    char LocalName[256]={0}; // ¼ì²é Winsock °æ±¾ºÅ£¬WSADataΪWSADATA½á¹¹¶ÔÏó
    WSAStartup(MAKEWORD(2, 2), &WSAData);
    // ´´½¨Ô­Ê¼Ì×½Ó×Ö
    sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
    // ÉèÖÃIPÍ·²Ù×÷Ñ¡ÏÆäÖÐflag ÉèÖÃΪture£¬Ç××Ô¶ÔIPÍ·½øÐд¦Àí
    setsockopt(sock, IPPROTO_IP, IP_HDRINCL, (char*)&flag, sizeof(flag));
    // »ñÈ¡±¾»úÃû
    gethostname((char*)LocalName, sizeof(LocalName)-1);
    // »ñÈ¡±¾µØ IP µØÖ·
    struct hostent *pHost = gethostbyname((char*)LocalName);
    // Ìî³äSOCKADDR_IN½á¹¹
    SOCKADDR_IN addr_in;  addr_in.sin_addr = *(in_addr *)pHost->h_addr_list[0]; //IP
    addr_in.sin_family = AF_INET;
    addr_in.sin_port = htons(57274);
    // °ÑԭʼÌ×½Ó×Ösock °ó¶¨µ½±¾µØÍø¿¨µØÖ·ÉÏ
    bind(sock, (PSOCKADDR)&addr_in, sizeof(addr_in));
    // dwValueΪÊäÈëÊä³ö²ÎÊý£¬Îª1ʱִÐУ¬0ʱȡÏû
    DWORD dwValue = 1; 
    // ÉèÖàSOCK_RAW ÎªSIO_RCVALL£¬ÒÔ±ã½ÓÊÕËùÓеÄIP°ü¡£ÆäÖÐSIO_RCVALL
    // µÄ¶¨ÒåΪ£º #define SIO_RCVALL _WSAIOW(IOC_VENDOR,1)
    ioctlsocket(sock, SIO_RCVALL, &dwValue);  char RecvBuf[4096]={0};
    int size =4096; CString icmp_type,ip_from,ip_to,pack_size; int times=pDlg->user_config.PortConfig.PingTimes;
    CTime t;

    int first=-1,last=0;//µ±Ç°·ÖÖÓ
    while (!pDlg->stop)
    {
    // ½ÓÊÕԭʼÊý¾Ý°üÐÅÏ¢
    int ret = recv(sock, RecvBuf, size, 0);
    if (ret > 0)
    {
    // ¶ÔÊý¾Ý°ü½øÐзÖÎö£¬²¢Êä³ö·ÖÎö½á¹û
    _IP ip = *(IP*)RecvBuf; if(ip.Protocol==1)
    {
    _ICMP icmp = *(ICMP*)(RecvBuf+20);

    if(icmp.i_type==8) ///ping ÇëÇó
    {
    icmp_type="ÇëÇó";
    }
    if(icmp.i_type==0) ///ping ´ðÓ¦
    {
    icmp_type="´ðÓ¦";
    }
    if(icmp.i_type==0||icmp.i_type==8)
    {
    if(first==-1)
    {
    t=CTime::GetCurrentTime();
    first=t.GetMinute();
    }
    ip_from.Format("%s",inet_ntoa(*(in_addr*)&ip.SrcAddr));
    ip_to.Format("%s",inet_ntoa(*(in_addr*)&ip.DstAddr));
    pack_size.Format("%d",ntohs(ip.TotalLen));

    pDlg->m_list_ping.InsertItem(0,icmp_type);
    pDlg->m_list_ping.SetItemText(0,1,ip_from);
    pDlg->m_list_ping.SetItemText(0,2,ip_to);
    pDlg->m_list_ping.SetItemText(0,3,pack_size);
    times--;
    if(!times)
    {
    /*Send to Server*/
    t=CTime::GetCurrentTime();
    last=t.GetMinute();
    if(last==first)
    {
    EnterCriticalSection(&pDlg->mutex);
    pDlg->send_msg.m_error_msg="±»PING´ÎÊý´óÓÚ·§Öµ";
    if(mobileflag)
    {
    pDlg->send_msg.m_error_level="·¢ËÍÖÁСÁéͨ";
    }
    else pDlg->send_msg.m_error_level="·¢ËÍÖÁ·þÎñÆ÷"; AfxBeginThread(&SendThread,pDlg);
    LeaveCriticalSection(&pDlg->mutex);
    times=pDlg->user_config.PortConfig.PingTimes;
    first=-1;
    }
    else
    {
    first=-1;
    times=pDlg->user_config.PortConfig.PingTimes;
    }
    }

    }

    } }


    AfxEndThread(0);
    return 0;
    }