首页
社区
课程
招聘
[旧帖] [原创]自己写的一个PING程序,强烈抵制一些会员不负责的GOOGLE行为。 0.00雪花
发表于: 2009-7-16 18:56 3422

[旧帖] [原创]自己写的一个PING程序,强烈抵制一些会员不负责的GOOGLE行为。 0.00雪花

2009-7-16 18:56
3422
//ping.h======================

#pragma pack(1)

#define ICMP_ECHOREPLY        0
#define ICMP_ECHOREQ        8
#define REQ_DATASIZE 32

class CPing
{
public:
        HWND m_hWnd;
        void Ping(UINT nRetries,LPCSTR pstrHost,HWND hWnd);
        int  WaitForEchoReply(SOCKET s);

        int                SendEchoRequest(SOCKET, LPSOCKADDR_IN);
        DWORD        RecvEchoReply(SOCKET, LPSOCKADDR_IN, u_char *);
        u_short in_cksum(u_short *addr, int len);

protected:
        void WSAError(LPCSTR pstrFrom);

};

typedef struct tagIPHDR
{
        u_char  VIHL;
        u_char        TOS;
        short        TotLen;       
        short        ID;
        short        FlagOff;
        u_char        TTL;
        u_char        Protocol;
        u_short        Checksum;
        struct        in_addr iaSrc;
        struct        in_addr iaDst;
}IPHDR, *PIPHDR;

typedef struct tagICMPHDR
{
        u_char        Type;
        u_char        Code;
        u_short        Checksum;
        u_short        ID;
        u_short        Seq;
        char        Data;                       
}ICMPHDR, *PICMPHDR;

typedef struct tagECHOREQUEST
{
        ICMPHDR icmpHdr;
        DWORD        dwTime;
        char        cData[REQ_DATASIZE];
}ECHOREQUEST, *PECHOREQUEST;

typedef struct tagECHOREPLY
{
        IPHDR        ipHdr;
        ECHOREQUEST        echoRequest;
        char    cFiller[256];
}ECHOREPLY, *PECHOREPLY;

#pragma pack()

//ping.cpp=================================
#include "stdafx.h"
#include "ping.h"

void CPing::Ping(UINT nRetries,LPCSTR pstrHost,HWND hWnd)
{
        SOCKET          rawSocket;
        LPHOSTENT lpHost;
        UINT          nLoop;
        int       nRet;
        struct    sockaddr_in saDest;
        struct    sockaddr_in saSrc;
        DWORD          dwTimeSent;
        DWORD          dwElapsed;
        u_char    cTTL;

        m_hWnd = hWnd;

        CString str;

        ASSERT(IsWindow(hWnd));

        rawSocket = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
        if (rawSocket == SOCKET_ERROR)
        {
                WSAError("socket()");
                return;
        }
       
        lpHost = gethostbyname(pstrHost);
        if (lpHost == NULL)
        {
                str.Format("Host not found: %s", pstrHost);
                ::PostMessage(m_hWnd,WM_MSG_STATUS, 0, (LPARAM) AllocBuffer(str));
                ::PostMessage(m_hWnd,WM_PING_END, 0, 0);
                return;
        }
       
        saDest.sin_addr.s_addr = *((u_long FAR *) (lpHost->h_addr));
        saDest.sin_family = AF_INET;
        saDest.sin_port = 3077;

        str.Format("Pinging %s [%s] with %d bytes of data:",
                                pstrHost,
                                inet_ntoa(saDest.sin_addr),
                                REQ_DATASIZE);
        ::PostMessage(m_hWnd,WM_MSG_STATUS, 1, (LPARAM) AllocBuffer(str));

        for (nLoop = 0; nLoop < nRetries;)
        {
                if(nRetries<1000)
                        nLoop++;

                SendEchoRequest(rawSocket, &saDest);

                nRet = WaitForEchoReply(rawSocket);
                if (nRet == SOCKET_ERROR)
                {
                        WSAError("select()");
                        break;
                }
                if (!nRet)
                {
                        str.Format("Request Timed Out");
                        ::PostMessage(m_hWnd,WM_MSG_STATUS, 3, (LPARAM) AllocBuffer(str));
                }
                else

                {
                        dwTimeSent = RecvEchoReply(rawSocket, &saSrc, &cTTL);

                        dwElapsed = GetTickCount() - dwTimeSent;
                        str.Format("Reply[%d] from: %s: bytes=%d time=%ldms TTL=%d",
                                nLoop+1,
                                   inet_ntoa(saSrc.sin_addr),
                                   REQ_DATASIZE,
                                   dwElapsed,
                                   cTTL);

                        ::PostMessage(m_hWnd,WM_MSG_STATUS, 2, (LPARAM) AllocBuffer(str));

                        Sleep(1000);

                }

                        BOOL t1,t2=0;
                       
                        int t3=GetDlgItemInt(m_hWnd,1011,&t1,t2);
                        if(t3==1)
                                break;

        }
       
        ::PostMessage(m_hWnd,WM_PING_END, 0, 1);

        nRet = closesocket(rawSocket);
        if (nRet == SOCKET_ERROR)
                WSAError("closesocket()");

}

int CPing::SendEchoRequest(SOCKET s,LPSOCKADDR_IN lpstToAddr)
{
        static ECHOREQUEST echoReq;
        static nId = 1;
        static nSeq = 1;
        int nRet;

        echoReq.icmpHdr.Type                = ICMP_ECHOREQ;
        echoReq.icmpHdr.Code                = 0;
        echoReq.icmpHdr.Checksum        = 0;
        echoReq.icmpHdr.ID                        = nId++;
        echoReq.icmpHdr.Seq                        = nSeq++;

        for (nRet = 0; nRet < REQ_DATASIZE; nRet++)
                echoReq.cData[nRet] = ' '+nRet;

        echoReq.dwTime                                = GetTickCount();

        echoReq.icmpHdr.Checksum = in_cksum((u_short *)&echoReq, sizeof(ECHOREQUEST));

        nRet = sendto(s,                                               
                                 (LPSTR)&echoReq,                       
                                 sizeof(ECHOREQUEST),
                                 0,                                                       
                                 (LPSOCKADDR)lpstToAddr,
                                 sizeof(SOCKADDR_IN));   

        if (nRet == SOCKET_ERROR)
                WSAError("sendto()");
        return (nRet);
}

DWORD CPing::RecvEchoReply(SOCKET s, LPSOCKADDR_IN lpsaFrom, u_char *pTTL)
{
        ECHOREPLY echoReply;
        int nRet;
        int nAddrLen = sizeof(struct sockaddr_in);

        nRet = recvfrom(s,                                       
                                        (LPSTR)&echoReply,       
                                        sizeof(ECHOREPLY),       
                                        0,                                       
                                        (LPSOCKADDR)lpsaFrom,
                                        &nAddrLen);                       

        if (nRet == SOCKET_ERROR)
                WSAError("recvfrom()");

        *pTTL = echoReply.ipHdr.TTL;

        return(echoReply.echoRequest.dwTime);                  
}

int CPing::WaitForEchoReply(SOCKET s)
{
        struct timeval Timeout;
        fd_set readfds;

        readfds.fd_count = 1;
        readfds.fd_array[0] = s;
        Timeout.tv_sec = 1;
    Timeout.tv_usec = 0;

        return(select(1, &readfds, NULL, NULL, &Timeout));
}

void CPing::WSAError(LPCSTR lpMsg)
{
        CString strMsg;
        strMsg.Format("%s - WSAError: %ld",lpMsg,WSAGetLastError());       
        ::PostMessage(m_hWnd,WM_MSG_STATUS, 0, (LPARAM) AllocBuffer(strMsg));
}

u_short CPing::in_cksum(u_short *addr, int len)
{
        register int nleft = len;
        register u_short *w = addr;
        register u_short answer;
        register int sum = 0;

        while( nleft > 1 )  {
                sum += *w++;
                nleft -= 2;
        }

        if( nleft == 1 ) {
                u_short        u = 0;

                *(u_char *)(&u) = *(u_char *)w ;
                sum += u;
        }

        sum = (sum >> 16) + (sum & 0xffff);       
        sum += (sum >> 16);                       
        answer = ~sum;                               
        return (answer);
}

[课程]Android-CTF解题方法汇总!

收藏
免费 0
支持
分享
最新回复 (8)
雪    币: 21
活跃值: (11)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
贴的部分源码,个人没有做注释的习惯````````````

各位见谅````
2009-7-16 18:58
0
雪    币: 21
活跃值: (11)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
不拿邀请,誓不罢休```
2009-7-16 19:00
0
雪    币: 183
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
txx
4
说实话,估计没希望拿。
2009-7-16 19:41
0
雪    币: 2362
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
http://www.phpchinaz.cn/2006c/2/2629/2629981.html
不小心发现了个没错误有注释的版本S君啊S君
2009-7-16 19:57
0
雪    币: 2362
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
6
//-------------   
  //ping.h   
  //-------------   
  #include   "winsock.h"   
  #include   <stdio.h>   
   
  #define   WM_MSG_STATUS (WM_USER   +   200)   
  //   string   message   allocator   for   posting   messages   between   windows...   
  static   char*   AllocBuffer(int   len,   char   *strMsg)   
  {   
  int   nLen   =   len;   
  char   *pBuffer   =   new   char[nLen+1];     
  strcpy(pBuffer,(const   char*)strMsg);   
  //if(pBuffer   ==   NULL)   
  // return   NULL;   
  return   pBuffer;   
  }   
   
  #pragma   pack(1)   
   
  #define   ICMP_ECHOREPLY 0   
  #define   ICMP_ECHOREQ 8   
  #define   REQ_DATASIZE 32   
   
  class   CPing   
  {   
  public:   
  HWND   m_hWnd;   
  void   Ping(UINT   nRetries,LPCSTR   pstrHost,HWND   hWnd);   
  int     WaitForEchoReply(SOCKET   s);   
  //ICMP回应的请求和回答函数   
  int SendEchoRequest(SOCKET,   LPSOCKADDR_IN);   
  DWORD RecvEchoReply(SOCKET,   LPSOCKADDR_IN,   u_char   *);   
  u_short   in_cksum(u_short   *addr,   int   len);   
  protected:   
  void   WSAError(LPCSTR   pstrFrom);   
  };   
   
  //   IP   Header   --   RFC   791   
  typedef   struct   tagIPHDR   
  {   
  u_char     VIHL; //   Version   and   IHL   
  u_char TOS; //   Type   Of   Service   
  short TotLen; //   Total   Length   
  short ID; //   Identification   
  short FlagOff; //   Flags   and   Fragment   Offset   
  u_char TTL; //   Time   To   Live   
  u_char Protocol; //   Protocol   
  u_short Checksum; //   Checksum   
  struct in_addr   iaSrc; //   Internet   Address   -   Source   
  struct in_addr   iaDst; //   Internet   Address   -   Destination   
  }IPHDR,   *PIPHDR;   
   
  //   ICMP   Header   -   RFC   792   
  typedef   struct   tagICMPHDR   
  {   
  u_char Type; //   Type   
  u_char Code; //   Code   
  u_short Checksum; //   Checksum   
  u_short ID; //   Identification   
  u_short Seq; //   Sequence   
  char Data; //   Data   
  }ICMPHDR,   *PICMPHDR;   
   
  //   ICMP   Echo   Request   
  typedef   struct   tagECHOREQUEST   
  {   
  ICMPHDR   icmpHdr;   
  DWORD dwTime;   
  char cData[REQ_DATASIZE];   
  }ECHOREQUEST,   *PECHOREQUEST;   
   
  //   ICMP   Echo   Reply   
  typedef   struct   tagECHOREPLY   
  {   
  IPHDR ipHdr;   
  ECHOREQUEST echoRequest;   
  char         cFiller[256];   
  }ECHOREPLY,   *PECHOREPLY;   
   
  #pragma   pack()   
  
hyf0537//---------   
  //ping.cpp   
  //---------   
  //文件名:   Ping.cpp     CPing类的实现文件   
  #include   "stdafx.h"   
  #include   "ping.h"   
  void   CPing::Ping(UINT   nRetries,LPCSTR   pstrHost,HWND   hWnd)   
  {   
  SOCKET     rawSocket;   
  LPHOSTENT   lpHost;   
  UINT     nLoop;   
  int               nRet;   
  struct         sockaddr_in   saDest;   
  struct         sockaddr_in   saSrc;   
  DWORD     dwTimeSent;   
  DWORD     dwElapsed;   
  u_char         cTTL;   
  m_hWnd   =   hWnd;   
  char   str[1024];   
  //ASSERT(IsWindow(hWnd));   
  //创建一个Raw套接字   
  rawSocket   =   socket(AF_INET,   SOCK_RAW,   IPPROTO_ICMP);   
  if   (rawSocket   ==   SOCKET_ERROR)   
  {   
  WSAError("socket()");   
  return;   
  }   
  //获得主机信息   
  lpHost   =   gethostbyname(pstrHost);   
  if   (lpHost   ==   NULL)   
  {   
  sprintf(str,"Host   not   found:   %s",   pstrHost);   
  ::PostMessage(m_hWnd,WM_MSG_STATUS,   0,   (LPARAM)   AllocBuffer(1024,str));   
  return;   
  }   
  //构造目标套接字地址信息   
  saDest.sin_addr.s_addr   =   *((u_long   FAR   *)   (lpHost->h_addr));   
  saDest.sin_family   =   AF_INET;   
  saDest.sin_port   =   0;   
  //告诉用户我们现在的工作   
  sprintf(str,"Pinging   %s   [%s]   with   %d   bytes   of   data:",   
  pstrHost,   
  inet_ntoa(saDest.sin_addr),   
  REQ_DATASIZE);   
  ::PostMessage(m_hWnd,WM_MSG_STATUS,   1,   (LPARAM)AllocBuffer(1024,str));   
  //多次ping   
  for   (nLoop   =   0;   nLoop   <   nRetries;   nLoop++)   
  {   
  //发送ICMP回应请求   
  SendEchoRequest(rawSocket,   &saDest);   
  nRet   =   WaitForEchoReply(rawSocket);   
  if   (nRet   ==   SOCKET_ERROR)   
  {   
  WSAError("select()");   
  break;   
  }   
  if   (!nRet)   
  {   
  sprintf(str,"Request   Timed   Out");   
  ::PostMessage(m_hWnd,WM_MSG_STATUS,   3,   (LPARAM)   AllocBuffer(1024,str));   
  }   
  else   
  {   
  //获得回应   
  dwTimeSent   =   RecvEchoReply(rawSocket,   &saSrc,   &cTTL);   
  //计算时间   
  dwElapsed   =   GetTickCount()   -   dwTimeSent;   
  sprintf(str,"Reply[%d]   from:   %s:   bytes=%d   time=%ldms   TTL=%d",   
  nLoop+1,   
        inet_ntoa(saSrc.sin_addr),   
        REQ_DATASIZE,   
        dwElapsed,   
        cTTL);   
  ::PostMessage(m_hWnd,WM_MSG_STATUS,   2,   (LPARAM)   AllocBuffer(1024,str));   
  Sleep(20);   
  }   
  }   
   
  nRet   =   closesocket(rawSocket);   
  if   (nRet   ==   SOCKET_ERROR)   
  WSAError("closesocket()");   
   
  }   
  //发送ICMPECHO数据包请求   
  int   CPing::SendEchoRequest(SOCKET   s,LPSOCKADDR_IN   lpstToAddr)     
  {   
  static   ECHOREQUEST   echoReq;   
  static   nId   =   1;   
  static   nSeq   =   1;   
  int   nRet;   
  //构造回应请求   
  echoReq.icmpHdr.Type =   ICMP_ECHOREQ;   
  echoReq.icmpHdr.Code =   0;   
  echoReq.icmpHdr.Checksum =   0;   
  echoReq.icmpHdr.ID =   nId++;   
  echoReq.icmpHdr.Seq =   nSeq++;   
  for   (nRet   =   0;   nRet   <   REQ_DATASIZE;   nRet++)   
  echoReq.cData[nRet]   =   '   '+nRet;   
  //保存发送时间   
  echoReq.dwTime =   GetTickCount();   
  echoReq.icmpHdr.Checksum   =   1024;   //in_cksum((u_short   *)&echoReq,   sizeof(ECHOREQUEST));   
  //发送请求   
  nRet   =   sendto(s,   
    (LPSTR)&echoReq,   
    sizeof(ECHOREQUEST),   
    0,   
    (LPSOCKADDR)lpstToAddr,     
    sizeof(SOCKADDR_IN));         
   
  if   (nRet   ==   SOCKET_ERROR)     
  WSAError("sendto()");   
  return   (nRet);   
  }   
  //接收ICMPECHO数据包回应   
  DWORD   CPing::RecvEchoReply(SOCKET   s,   LPSOCKADDR_IN   lpsaFrom,   u_char   *pTTL)     
  {   
  ECHOREPLY   echoReply;   
  int   nRet;   
  int   nAddrLen   =   sizeof(struct   sockaddr_in);   
   
  //接收请求回应   
  nRet   =   recvfrom(s,   
  (LPSTR)&echoReply,   
  sizeof(ECHOREPLY),   
  0,   
  (LPSOCKADDR)lpsaFrom,   
  &nAddrLen);   
  //检查返回值   
  if   (nRet   ==   SOCKET_ERROR)     
  WSAError("recvfrom()");   
  //返回发送的时间   
  *pTTL   =   echoReply.ipHdr.TTL;   
  return(echoReply.echoRequest.dwTime);         
  }   
  //等待回应   
  int   CPing::WaitForEchoReply(SOCKET   s)   
  {   
  struct   timeval   Timeout;   
  fd_set   readfds;   
   
  readfds.fd_count   =   1;   
  readfds.fd_array[0]   =   s;   
  Timeout.tv_sec   =   0;   
          Timeout.tv_usec   =   500;   
  return(select(1,   &readfds,   NULL,   NULL,   &Timeout));   
  }   
  //错误处理   
  void   CPing::WSAError(LPCSTR   lpMsg)   
  {   
  char   strMsg[1024]   =   {0};   
  sprintf(strMsg,"%s   -   WSAError:   %ld",lpMsg,WSAGetLastError());   
  //发送报错信息   
  ::PostMessage(m_hWnd,WM_MSG_STATUS,   0,   (LPARAM)   AllocBuffer(1024,   strMsg));   
  }   
  //转换地址   
  u_short   CPing::in_cksum(u_short   *addr,   int   len)   
  {   
  register   int   nleft   =   len;   
  register   u_short   *w   =   addr;   
  register   u_short   answer;   
  register   int   sum   =   0;   
   
  while(   nleft   >   1   )     {   
  sum   +=   *w++;   
  nleft   -=   2;   
  }   
   
  if(   nleft   ==   1   )   {   
  u_short u   =   0;   
   
  *(u_char   *)(&u)   =   *(u_char   *)w   ;   
  sum   +=   u;   
  }   
   
  sum   =   (sum   >>   16)   +   (sum   &   0xffff);   
  sum   +=   (sum   >>   16);   
  answer   =   ~sum;   
  return   (answer);   
  }
2009-7-16 20:05
0
雪    币: 108
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
7
啊,楼主被比下去了。。。。
2009-7-17 12:04
0
雪    币: 2067
活跃值: (82)
能力值: ( LV9,RANK:180 )
在线值:
发帖
回帖
粉丝
8
zapline曾经是高级会员.
不是没原因的.
2009-7-17 12:09
0
雪    币: 68
活跃值: (47)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
9
支持zapline打假!
2009-7-17 13:25
0
游客
登录 | 注册 方可回帖
返回
//