home *** CD-ROM | disk | FTP | other *** search
/ The Best Internet Programs / BESTINTERNET.bin / internet / winftp / ws_icmp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-15  |  23.2 KB  |  677 lines

  1. #include "ws_glob.h"
  2. #include "winftp.h"
  3. #include "winicmp.h"
  4. #include <stdio.h>
  5. #include <stdlib.h> 
  6. #include <direct.h>
  7. #include <dos.h>
  8.  
  9. #include <stdarg.h>
  10.  
  11. static char szPingHost[80];
  12. static HOSTENT HostEntry;
  13. static LPHOSTENT phe;
  14. static PROTOENT ProtoEntry;
  15. static LPPROTOENT ppe; 
  16. static HANDLE hTaskAsyncHost, hTaskAsyncProto;
  17. static HANDLE hStdCursor, hWaitCursor;
  18. static int nIndex, nRC, nBytesRecv;
  19. static int nTransmitted, nReceived;
  20. static int saFromAddrLen;
  21. static int nPktLength=50;
  22. static int nNumPkts=10;
  23. static u_long lTotalTime,lMinTime,lMaxTime;
  24. static bPingInProgress=FALSE;
  25.  
  26. SOCKET sockPing=INVALID_SOCKET;
  27.  
  28. int nMaxWait=5000;  // milliseconds
  29. int nExtent=0;
  30. int nExtra =0;
  31.  
  32. //************************************************************************
  33. //  Add a line to the Ping List Box
  34. //************************************************************************
  35. void AddPingLine (HWND hDlg, LPSTR lpStr)
  36. {
  37.   HDC hDC;
  38.   HWND hWnd;
  39.   SIZE size;
  40.   
  41.   if (lpStr==NULL) return;
  42.   if (!bVerbose && lpStr[0]=='[') return;
  43.   SendDlgItemMessage (hDlg, IDD_LBPING, LB_ADDSTRING, (WPARAM) 0, (LPARAM)(LPCSTR) lpStr);
  44.   if (nExtra==0) nExtra = GetSystemMetrics (SM_CXVSCROLL);
  45.   hDC = GetDC (hWnd=GetDlgItem (hDlg, IDD_LBPING));
  46.   SelectObject (hDC, GetStockObject (ANSI_FIXED_FONT));
  47.   GetTextExtentPoint (hDC, lpStr, lstrlen (lpStr), &size);
  48.   ReleaseDC (hWnd, hDC);
  49.   if ((size.cx+nExtra)>nExtent) SendDlgItemMessage (hDlg, IDD_LBPING, LB_SETHORIZONTALEXTENT, (WPARAM) (nExtent=size.cx+2*nExtra), 0L);
  50. }
  51.  
  52. //*************************************************************************
  53. //*************************************************************************
  54. void DoDlgPrintf (HWND hDlg, char *szFormat,...)
  55. {
  56.    va_list vaArgs;
  57.    static char szBuf[256];
  58.  
  59.    va_start (vaArgs, szFormat);
  60.    if (vsprintf (szBuf, szFormat, vaArgs)!=EOF) AddPingLine (hDlg, szBuf);
  61.    va_end (vaArgs);
  62. }
  63.  
  64. //*************************************************************************
  65. //*************************************************************************
  66. void PrintHeader (HWND hDlg, LPSOCKADDR_IN saDestAddr, int nDataSize)
  67. {
  68.   DoDlgPrintf (hDlg, "PING %s (%s): %u data bytes",
  69.       szPingHost, inet_ntoa (saDestAddr->sin_addr), nDataSize+SIZE_ICMP_HDR);
  70.   lMaxTime=lTotalTime=0l; 
  71.   lMinTime=99999999l;
  72. }
  73.  
  74. //*************************************************************************
  75. //*************************************************************************
  76. void PrintStats (HWND hDlg) 
  77. {
  78.   AddPingLine (hDlg, " ");
  79.   DoDlgPrintf (hDlg, "PING Statistics for %s", szPingHost);
  80.   if (nTransmitted && nReceived)
  81.     DoDlgPrintf (hDlg, "%d packets transmitted, %d packets received, %d%% packet loss",
  82.         nTransmitted, nReceived, (int)(((nTransmitted-nReceived)*100)/nTransmitted));
  83.   else if (nTransmitted)
  84.     DoDlgPrintf (hDlg, "%d packets transmitted, %d packets received, 100%% packet loss",
  85.       nTransmitted, nReceived);
  86.   if (nReceived)
  87.     DoDlgPrintf (hDlg, "round-trip (ms) min/avg/max = %ld/%ld/%ld",
  88.          lMinTime, lTotalTime/nReceived, lMaxTime);
  89.   AddPingLine (hDlg, " ");
  90.   AddPingLine (hDlg, " ");
  91. }
  92.  
  93. //************************************************************************
  94. // compute packet checksum
  95. //************************************************************************
  96. int in_cksum (u_short FAR *pPacket, int nBytes)
  97. {
  98.   long sum;
  99.   u_short oddbyte;
  100.   u_short answer;
  101.  
  102.   sum=0l;
  103.   while(nBytes>1) { sum += *pPacket++; nBytes -= 2; }
  104.   if(nBytes==1) 
  105.   {
  106.     oddbyte=0;
  107.     *((u_char FAR *) &oddbyte) = *(u_char FAR *)pPacket;
  108.     sum += oddbyte;
  109.   }
  110.   sum = (sum >> 16) + (sum & 0xffff);
  111.   sum += (sum >> 16);
  112.   answer= (u_short) ~sum;
  113.   return (answer);
  114. }
  115.  
  116. //************************************************************************
  117. //************************************************************************
  118. LPSTR ReturnICMPType (int nType)
  119. {
  120.   static char *ICMPTypeTable[]={
  121.     "Echo Reply", "ICMP 1", "ICMP 2", "DestUnrchbl",
  122.     "SrcQnch", "Redirect", "6", "7","Echo","9","10",
  123.     "Time Exceed", "ParamPrblm", "Timestamp", "Timestamp reply",
  124.     "InfoRqst", "InfoRply"
  125.   };
  126.  
  127.   if(nType<0 || nType>16) return "Out-of-range";
  128.   return ICMPTypeTable[nType];
  129. }
  130.  
  131. //************************************************************************
  132. //************************************************************************
  133. BOOL PrintPkt (HWND hDlg, LPSTR pPacket, int nLength, LPSOCKADDR_IN saFromAddr, BOOL bVerbose)
  134. {
  135.   int iphdrlen;
  136.   static struct ip *ip;
  137.   static struct icmp *icmp;
  138.   static struct in_addr *iptr;
  139.   u_long ulrc;
  140.   static u_long lEndTime,*lpStartTime,lTripTime;
  141.  
  142.   lEndTime = GetTickCount();
  143.   saFromAddr->sin_addr.s_addr = ntohl (saFromAddr->sin_addr.s_addr);
  144.   ip = (struct ip *) pPacket;
  145.   iphdrlen = ip->ip_hl << 2;
  146.   if (nLength < iphdrlen + ICMP_MINLEN) 
  147.   {
  148.     if(bVerbose) 
  149.     {
  150.       ulrc=ntohl (saFromAddr->sin_addr.s_addr);
  151.       iptr = (struct in_addr *) &ulrc;
  152.       DoDlgPrintf (hDlg, "[received] too short (%d bytes) from %s\n", nLength, inet_ntoa(*iptr));
  153.     }
  154.     return FALSE;
  155.   }
  156.   nLength -= iphdrlen;
  157.   icmp=(struct icmp *)(pPacket + iphdrlen);
  158.   if (icmp->icmp_type != ICMP_ECHOREPLY) 
  159.   {
  160.     if (bVerbose) 
  161.     {
  162.       ulrc=ntohl(saFromAddr->sin_addr.s_addr);
  163.       iptr = (struct in_addr *) &ulrc;
  164.       DoDlgPrintf (hDlg, "[received] %d bytes from %s: icmp_type=%d (%s) icmp_code=%d",
  165.         nLength, inet_ntoa(*iptr), icmp->icmp_type, ReturnICMPType(icmp->icmp_type), icmp->icmp_code);
  166.     }
  167.     return FALSE;
  168.   }
  169.  
  170.   ulrc = ntohl (saFromAddr->sin_addr.s_addr);
  171.   iptr = (struct in_addr *) &ulrc;
  172.  
  173.   if (icmp->icmp_id!=(WORD) hDlg) 
  174.   {
  175.     DoDlgPrintf (hDlg, "RCV %d bytes from %s. not for us", nLength, inet_ntoa (*iptr));
  176.     return FALSE;
  177.   }
  178.   lpStartTime = (u_long *) (pPacket + SIZE_ICMP_HDR);
  179.   lTripTime = lEndTime - *lpStartTime;
  180.   lTotalTime += lTripTime;
  181.   if(lTripTime < lMinTime) lMinTime=lTripTime;
  182.   if(lTripTime > lMaxTime) lMaxTime=lTripTime;
  183.  
  184.   DoDlgPrintf (hDlg, "%-12s:(%3d) %6ldms %4d bytes",
  185.        inet_ntoa(*iptr), icmp->icmp_seq, lTripTime, nLength);
  186.  
  187.   nReceived++;
  188.  
  189.   return TRUE;
  190. }
  191.  
  192. //************************************************************************
  193. //************************************************************************
  194. int send_ping (HWND hDlg, SOCKET sockfd, LPSOCKADDR_IN saDestAddr, LPSTR pPacket, int nLength)
  195. {
  196.   int nRC;
  197.   struct icmp *icmp;
  198.   u_long *timeloc;
  199.  
  200.   nLength+=SIZE_ICMP_HDR;
  201.   icmp=(struct icmp *)pPacket;
  202.   // set the send time
  203.   timeloc= (u_long *) (pPacket+SIZE_ICMP_HDR);
  204.   *timeloc=GetTickCount();
  205.   // complete the ICMP packet header
  206.   icmp->icmp_type=ICMP_ECHO;
  207.   icmp->icmp_code=0;
  208.   icmp->icmp_cksum=0;
  209.   icmp->icmp_id = (int) hDlg;
  210.   icmp->icmp_seq=nTransmitted;
  211.   // compute the checksum
  212.   icmp->icmp_cksum = in_cksum ((u_short FAR *) icmp, nLength);
  213.   // send the packet
  214.   if ((nRC = sendto (sockfd, pPacket, nLength,0, (struct sockaddr *) saDestAddr,
  215.                  sizeof(struct sockaddr)))==SOCKET_ERROR) 
  216.   {
  217.     DoDlgPrintf(hDlg, "[sendto] %s", ReturnWSError (WSAGetLastError(), NULL));
  218.     if (WSAGetLastError()!=WSAEWOULDBLOCK && WSAGetLastError()!=WSAEINPROGRESS)
  219.       nTransmitted++;
  220.   } 
  221.   else 
  222.   {
  223.     nTransmitted++;
  224.     if (nRC!=nLength)
  225.       DoDlgPrintf (hDlg, "[sendto] wrote %d bytes, return=%d",nLength,nRC);
  226.   }
  227.  
  228.   return (nRC==nLength);
  229. }
  230.  
  231. //************************************************************************
  232. //************************************************************************
  233. recv_ping (HWND hDlg, SOCKET sockfd, LPSTR szRecvPkt, BOOL bPrintFlag)
  234. {
  235.   int  nBytesRecv;
  236.   int  saFromAddrLen;
  237.   struct sockaddr_in  saFromAddr;
  238.   int  nRC;
  239.  
  240. // NOTE: this is for the blocking version..... this means we won't exit
  241. // until we get the packet(s) we are looking for.  This means we have to
  242. // use a timer function to make us exit.
  243.  
  244.   nRC = FALSE;
  245.   saFromAddrLen = sizeof (saFromAddr);
  246.   SetTimer (hDlg, 10, nMaxWait, NULL);
  247.   nBytesRecv = recvfrom (sockfd, szRecvPkt, MAXPACKET,0, (struct sockaddr *)&saFromAddr, &saFromAddrLen);
  248.   KillTimer (hDlg, 10);
  249.   if (nBytesRecv!=SOCKET_ERROR || WSAGetLastError()!=WSAEINTR) 
  250.   {
  251.     if (nBytesRecv==SOCKET_ERROR) 
  252.     {
  253.       DoDlgPrintf (hDlg, "[recvfrom] %s",ReturnWSError(WSAGetLastError(),NULL));
  254.       return FALSE;
  255.     } 
  256.     else
  257.     {
  258.       nRC = PrintPkt (hDlg, szRecvPkt, nBytesRecv, &saFromAddr, bPrintFlag);
  259.     }
  260.   }
  261.   return nRC;
  262. }
  263.  
  264. //************************************************************************
  265. //  Run Ping Async mode
  266. //************************************************************************
  267. BOOL DoAsyncPing (HWND hDlg)
  268. {
  269.   bPingInProgress = TRUE;
  270.   memset (&saDestAddr, 0, sizeof(struct sockaddr_in));
  271.   memset (&HostEntry,  0, sizeof(struct hostent));
  272.   memset (&ProtoEntry, 0, sizeof(struct protoent));
  273.   saDestAddr.sin_family=AF_INET;
  274.  
  275.   if ((saDestAddr.sin_addr.s_addr=inet_addr (szPingHost))==INADDR_NONE)
  276.        hTaskAsyncHost = WSAAsyncGetHostByName (hDlg, WM_PING_HOST, szPingHost, szMsgBuf, MAXGETHOSTSTRUCT);
  277.   else hTaskAsyncHost = 0;
  278.   hTaskAsyncProto = WSAAsyncGetProtoByName (hDlg, WM_PING_PROTO, "icmp", szMsgBuf+MAXGETHOSTSTRUCT, MAXGETHOSTSTRUCT);
  279.   return TRUE;
  280. }
  281.  
  282. //************************************************************************
  283. //  Run Ping Blocking mode
  284. //************************************************************************
  285. BOOL DoBlockingPing (HWND hDlg)
  286. {
  287.   int nProto;
  288.   
  289.   bPingInProgress = TRUE;
  290.   memset (&saDestAddr, 0, sizeof (struct sockaddr));
  291.   saDestAddr.sin_family = AF_INET;
  292.   if ((saDestAddr.sin_addr.s_addr=inet_addr (szPingHost))==INADDR_NONE)
  293.   {
  294.     if ((phe = gethostbyname (szPingHost))!=NULL)
  295.     {
  296.       memcpy (&saDestAddr.sin_addr, phe->h_addr, phe->h_length);
  297.     }
  298.     else
  299.     {
  300.       DoDlgPrintf (hDlg, "can't get \"%s\" host entry.", szPingHost);
  301.       SendMessage (hDlg, WM_RESETCURSOR, 0, 0l);
  302.       return TRUE;
  303.     }
  304.   }
  305.   if ((ppe=getprotobyname("icmp"))==NULL) 
  306.   {
  307.     DoDlgPrintf (hDlg, "[Proto] Unknown Protocol: icmp Using Default");
  308.     nProto = 1;
  309.   }
  310.   else
  311.   {
  312.     nProto = ppe->p_proto;
  313.   }
  314.   
  315.   if ((sockPing=socket (saDestAddr.sin_family, SOCK_RAW, nProto))==INVALID_SOCKET)
  316.   {
  317.     DoDlgPrintf (hDlg, "Can't Create Raw Socket %s", ReturnWSError (WSAGetLastError(), NULL));
  318.     SendMessage (hDlg, WM_RESETCURSOR, 0, 0l);
  319.     return TRUE;
  320.   }
  321.   SetWindowText (GetDlgItem (hDlg, IDD_PING), "Reset");
  322.   nTransmitted=0; nReceived=0;
  323.   PrintHeader (hDlg, &saDestAddr, nPktLength);
  324.   SetTimer (hDlg, 30, 1000, NULL);
  325.   return TRUE;
  326. }
  327.  
  328. //************************************************************************
  329. //  Process the IDOK message - Resolve the Name
  330. //************************************************************************
  331. BOOL OnDlgCmdIdOk (HWND hDlg, WORD wCtlID, WORD wNotifyCode)
  332. {
  333.   int nCount = 0;
  334.   char szBuf[30];
  335.   
  336.   GetDlgItemText (hDlg, IDD_HOSTNAME, szPingHost, 79);
  337.   if ((phe = gethostbyname (szPingHost)) == NULL) 
  338.   {
  339.     MessageBox (hDlg, "GetHostByName() failed", "Error", MB_OK);
  340.     return TRUE;
  341.   }
  342.   nCount = 0;
  343.   SendDlgItemMessage (hDlg, IDD_LBALIAS, LB_RESETCONTENT, (WPARAM) 0, (LPARAM) 0);
  344.   SendDlgItemMessage (hDlg, IDD_LBADDR,  LB_RESETCONTENT, (WPARAM) 0, (LPARAM) 0);
  345.   SendDlgItemMessage (hDlg, IDD_LBPING,  LB_RESETCONTENT, (WPARAM) 0, (LPARAM) 0);
  346.  
  347.   SendDlgItemMessage (hDlg, IDD_LBALIAS, LB_ADDSTRING, 0, (LPARAM) (phe->h_name));
  348.   while (phe->h_aliases[nCount] != NULL) 
  349.   {
  350.     SendDlgItemMessage (hDlg, IDD_LBALIAS, LB_ADDSTRING, 0, (LPARAM) (phe->h_aliases[nCount]));
  351.     nCount++;
  352.   }
  353.   nCount = 0;
  354.   while (phe->h_addr_list[nCount] != NULL) 
  355.   {
  356.     sprintf (szBuf, "%u.%u.%u.%u", (unsigned char) phe->h_addr_list[nCount][0],
  357.        (unsigned char) phe->h_addr_list[nCount][1],
  358.        (unsigned char) phe->h_addr_list[nCount][2],
  359.        (unsigned char) phe->h_addr_list[nCount][3]);
  360.     nCount++;
  361.     if (SendDlgItemMessage (hDlg, IDD_LBADDR, LB_ADDSTRING, 0, (LPARAM)(LPCSTR) szBuf) == LB_ERR)
  362.        MessageBox (hDlg, szBuf, "Couldn't add address..", MB_OK);
  363.   }
  364.   return TRUE;
  365. }
  366.  
  367. //***********************************************************************
  368. //  Process the IDD_PING command
  369. //***********************************************************************
  370. BOOL OnDlgCmdPingHost (HWND hDlg, WORD wCtlID, WORD wNotifyCode)
  371. {
  372.   if (bPingInProgress) 
  373.   {
  374.     if (sockPing!=INVALID_SOCKET) 
  375.     {
  376.       KillTimer (hDlg, 10);
  377.       KillTimer (hDlg, 20);
  378.       KillTimer (hDlg, 30);
  379.       if (WSAIsBlocking()) WSACancelBlockingCall();
  380.       sockPing = DoClose (sockPing);
  381.       SendMessage(hDlg, WM_RESETCURSOR, 0, 0l);
  382.       SetWindowText (GetDlgItem (hDlg, IDD_PING), "Ping Host");
  383.     }
  384.     bPingInProgress = FALSE; 
  385.     return TRUE;
  386.   }
  387.  
  388.   if (GetDlgItemText (hDlg, IDD_HOSTNAME, szPingHost, 79)==0) return 0;
  389.   nNumPkts   = GetDlgItemInt (hDlg, IDD_PINGCOUNT, NULL, FALSE);
  390.   nNumPkts = __max (nNumPkts, 1);
  391.   nPktLength = GetDlgItemInt (hDlg, IDD_PKTSIZE, NULL, FALSE);
  392.   nPktLength = __min (__max (nPktLength, sizeof (struct icmp)), MAXPACKET);
  393.   switch ((int) IsDlgButtonChecked (hDlg, IDC_BLOCKINGPING))
  394.   {
  395.     case  0  : DoAsyncPing (hDlg); break;
  396.     default  : DoBlockingPing (hDlg); break;
  397.   }
  398.   return TRUE;
  399. }
  400.  
  401. //***********************************************************************
  402. //  Process the Cancel Button
  403. //***********************************************************************
  404. BOOL OnDlgCmdCancel (HWND hDlg, WORD wCtlID, WORD wNotifyCode)
  405. {
  406.   if (sockPing!=INVALID_SOCKET) 
  407.   {
  408.     sockPing = DoClose (sockPing);
  409.     SendMessage(hDlg, WM_RESETCURSOR, 0, 0l);
  410.   }
  411.   EndDialog (hDlg, FALSE); 
  412.   return TRUE;
  413. }
  414.  
  415. //***********************************************************************
  416. //  Process the WM_COMMAND message
  417. //***********************************************************************
  418. BOOL OnDlgCommand (HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
  419. {
  420.   int count = 0;
  421.   WORD wNotifyCode;
  422.   WORD wCtlID;
  423.  
  424. #ifdef WIN32
  425.   wNotifyCode = HIWORD (wParam);
  426.   wCtlID      = LOWORD (wParam);
  427. #else
  428.   wNotifyCode = HIWORD (lParam);
  429.   wCtlID      = wParam;
  430. #endif
  431.   
  432.   switch (wCtlID)
  433.   {
  434.     case IDOK     : OnDlgCmdIdOk (hDlg, wCtlID, wNotifyCode); return TRUE;
  435.     case IDD_PING : OnDlgCmdPingHost (hDlg, wCtlID, wNotifyCode); return TRUE;
  436.     case IDCANCEL : OnDlgCmdCancel (hDlg, wCtlID, wNotifyCode); return TRUE;
  437.     default       : return FALSE;
  438.   }
  439.   return TRUE;
  440. }
  441.  
  442. //***********************************************************************
  443. //  Process the WM_TIMER message
  444. //***********************************************************************
  445. BOOL OnDlgTimer (HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
  446. {
  447.   switch (wParam) 
  448.   {
  449.     case 10: AddPingLine (hDlg, "[Timer-TIMEOUT] ");
  450.              KillTimer (hDlg, 10);
  451.              if (WSAIsBlocking()) WSACancelBlockingCall();
  452.              bPingInProgress = FALSE;
  453.              break;
  454.  
  455.     case 20: if (nTransmitted<nNumPkts) 
  456.              {
  457.                send_ping (hDlg, sockPing, &saDestAddr, szMsgBuf, nPktLength+SIZE_ICMP_HDR);
  458.                PostMessage(hDlg, WM_PING_RECEIVE, 0, 0l);
  459.              } 
  460.              else 
  461.              {
  462.                KillTimer (hDlg, 20);
  463.                if (nReceived!=nTransmitted) SendMessage (hDlg, WM_PING_RECEIVE, 0, 0l);
  464.                PostMessage (hDlg, WM_PING_FINISH, 0, 0l);
  465.              }
  466.              break;
  467.     case 30: if (nTransmitted<nNumPkts)
  468.              {
  469.                if (send_ping (hDlg, sockPing, &saDestAddr, szMsgBuf, nPktLength+SIZE_ICMP_HDR))
  470.                   recv_ping (hDlg, sockPing, szString, TRUE);
  471.              }
  472.              else 
  473.              {
  474.                KillTimer (hDlg, 30);
  475.                if (nReceived!=nTransmitted) recv_ping (hDlg, sockPing, szString, TRUE);
  476.                PostMessage (hDlg, WM_PING_FINISH, 0, 0l);
  477.              }
  478.              break;
  479.     default: KillTimer (hDlg, wParam);
  480.              break;
  481.   }
  482.   return TRUE;
  483. }
  484.  
  485. //***********************************************************************
  486. //***********************************************************************
  487. BOOL OnDlgPingHost (HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
  488. {
  489.   if (WSAGETASYNCERROR (lParam)!=0) 
  490.   {
  491.     ReportWSError (NULL, WSAGETASYNCERROR (lParam));
  492.     SendMessage (hDlg, WM_RESETCURSOR, 0, 0l);
  493.   } 
  494.   else 
  495.   {
  496.     memcpy (&HostEntry, szMsgBuf, sizeof (struct hostent));
  497.     memcpy (&saDestAddr.sin_addr, HostEntry.h_addr, HostEntry.h_length);
  498.     hTaskAsyncHost=0;
  499.     if (hTaskAsyncProto==0) PostMessage (hDlg, WM_PING_CAS, 0, 0l);
  500.   }
  501.   return TRUE;
  502. }
  503.  
  504. //***********************************************************************
  505. //***********************************************************************
  506. BOOL OnDlgPingProto (HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
  507. {
  508.   if (WSAGETASYNCERROR (lParam)!=0) 
  509.   {
  510.     ReportWSError (NULL, WSAGETASYNCERROR (lParam));
  511.     SendMessage (hDlg, WM_RESETCURSOR, 0, 0l);
  512.   } 
  513.   else 
  514.   {
  515.     memcpy (&ProtoEntry, szMsgBuf+MAXGETHOSTSTRUCT, sizeof (struct protoent));
  516.     hTaskAsyncProto=0;
  517.     if (hTaskAsyncHost==0) PostMessage (hDlg, WM_PING_CAS, 0, 0l);
  518.   }
  519.   return 0;
  520. }
  521.  
  522. //***********************************************************************
  523. //***********************************************************************
  524. BOOL OnDlgPingAsync (HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
  525. {
  526.   PrintHeader (hDlg, &saDestAddr, nPktLength+SIZE_ICMP_HDR);
  527.   sockPing = socket (saDestAddr.sin_family, SOCK_RAW, ProtoEntry.p_proto);
  528.   if (sockPing==INVALID_SOCKET) 
  529.   {
  530.     DoDlgPrintf (hDlg, "Can't Create Raw Socket: %s", ReturnWSError (WSAGetLastError(), NULL));
  531.     SendMessage (hDlg, WM_RESETCURSOR, 0, 0l);
  532.   } 
  533.   else 
  534.   {
  535.     if (WSAAsyncSelect (sockPing, hDlg, WM_PING_RECEIVE, FD_READ)==SOCKET_ERROR)
  536.     {
  537.       DoDlgPrintf (hDlg, "AsyncSelect: %s", ReturnWSError (WSAGetLastError(), NULL));
  538.       sockPing = DoClose (sockPing);
  539.       SendMessage (hDlg, WM_RESETCURSOR, 0, 0l);
  540.     } 
  541.     else 
  542.     {
  543.       SetWindowText (GetDlgItem (hDlg, IDD_PING), "Reset");
  544.       nTransmitted = nReceived = 0;
  545.       SetTimer (hDlg, 20, 1000, NULL);
  546.     }
  547.   }
  548.   return TRUE;
  549. }
  550.  
  551. //***********************************************************************
  552. //***********************************************************************
  553. BOOL OnDlgPingRecv (HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
  554. {
  555.   saFromAddrLen = sizeof(struct sockaddr);
  556.   nBytesRecv = recvfrom (sockPing, szString, MAXPACKET, 0, (struct sockaddr *)&saFromAddr, &saFromAddrLen);
  557.   if (nBytesRecv==SOCKET_ERROR) 
  558.   {
  559.     DoDlgPrintf (hDlg, "[Recv] %s", ReturnWSError (WSAGetLastError(), NULL));
  560.   } 
  561.   else
  562.   {
  563.     PrintPkt (hDlg, szString, nBytesRecv, &saFromAddr, TRUE);
  564.   }
  565.   return TRUE;
  566. }
  567.  
  568. //***********************************************************************
  569. //***********************************************************************
  570. BOOL OnDlgPingFinish (HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
  571. {
  572.   sockPing = DoClose (sockPing);
  573.   KillTimer (hDlg, 10);
  574.   PrintStats (hDlg);
  575.   bPingInProgress = FALSE;
  576.   SetWindowText (GetDlgItem (hDlg, IDD_PING), "Ping Host");
  577.   return TRUE;
  578. }
  579.  
  580. //***********************************************************************
  581. //***********************************************************************
  582. BOOL OnDlgResetCursor (hDlg, Msg, wParam, lParam)
  583. {
  584.   bCmdInProgress=FALSE;
  585.   SetCursor(hStdCursor);
  586.   return TRUE;
  587. }
  588.  
  589. //***********************************************************************
  590. //***********************************************************************
  591. BOOL OnDlgSetCursor (hDlg, Msg, wParam, lParam)
  592. {
  593.   if (bCmdInProgress) 
  594.   {
  595.     SetCursor (hWaitCursor);
  596.     return TRUE;
  597.   }
  598.   return FALSE;
  599. }
  600.  
  601. //***********************************************************************
  602. //***********************************************************************
  603. BOOL OnDlgInitDlg (HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
  604. {
  605.   SetDlgItemText (hDlg, IDD_HOSTNAME, szRemoteHost);
  606.   SetDlgItemInt (hDlg, IDD_PINGCOUNT, nNumPkts, FALSE);
  607.   SetDlgItemInt (hDlg, IDD_PKTSIZE, nPktLength, FALSE);
  608.   CheckRadioButton (hDlg, IDC_ASYNCPING, IDC_BLOCKINGPING, IDC_ASYNCPING);
  609.   SendDlgItemMessage (hDlg, IDD_LBPING, WM_SETFONT, (WPARAM) GetStockObject (ANSI_FIXED_FONT), (LPARAM) 0);
  610.   sockPing = INVALID_SOCKET;
  611.   return TRUE;
  612. }
  613.  
  614. //***********************************************************************
  615. //***********************************************************************
  616. BOOL OnDlgResize (HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
  617. {
  618.   int cx=LOWORD (lParam);
  619.   int cy=HIWORD (lParam);
  620.   DWORD dwBase;
  621.   int cxb, cyb;
  622.   int cTmpX, cTmpY;
  623.   
  624.   dwBase = GetDialogBaseUnits();
  625.   cxb = LOWORD (dwBase) / 4;
  626.   cyb = HIWORD (dwBase) / 8;
  627.   if (cxb==0 || cyb==0) return 0;
  628.   cx /= cxb;
  629.   cy /= cyb;
  630.   if (cx<145) return 0;
  631.   if (cy<190) return 0;
  632.   cTmpX = (cx-15)/3;
  633.   cTmpY = cy - 120 - 20;
  634.  
  635.   MoveWindow (GetDlgItem (hDlg, IDC_BOX_BLOCK), 92*cxb,      0, (cx-92-3)*cxb, 36*cyb, TRUE);
  636.   MoveWindow (GetDlgItem (hDlg, IDC_BOX_HOST) ,  2*cxb, 34*cyb, (cx- 2-3)*cxb, 25*cyb, TRUE);
  637.   MoveWindow (GetDlgItem (hDlg, IDD_HOSTNAME) , 28*cxb, 41*cyb, (cx-28-7)*cxb, 15*cyb, TRUE);
  638.   MoveWindow (GetDlgItem (hDlg, IDC_BOX_ALIAS),  2*cxb, 59*cyb, (cx- 2-3)*cxb, 59*cyb, TRUE);
  639.   MoveWindow (GetDlgItem (hDlg, IDD_LBALIAS)  ,  6*cxb, 70*cyb, (cx- 6-7)*cxb, 21*cxb, TRUE);
  640.   MoveWindow (GetDlgItem (hDlg, IDD_LBADDR)   ,  6*cxb, 93*cyb, (cx- 6-7)*cxb, 21*cyb, TRUE);
  641.   MoveWindow (GetDlgItem (hDlg, IDD_LBPING)   ,  2*cxb,120*cyb, (cx- 2-3)*cxb, cTmpY*cyb, TRUE);
  642.   
  643.   cTmpY += 122;
  644.   
  645.   MoveWindow (GetDlgItem (hDlg, IDOK)    ,           2*cxb, cTmpY*cyb, (cTmpX-2)*cxb, 15*cyb, TRUE);
  646.   MoveWindow (GetDlgItem (hDlg, IDD_PING),   (cTmpX+2)*cxb, cTmpY*cyb, (cTmpX-2)*cxb, 15*cyb, TRUE);
  647.   MoveWindow (GetDlgItem (hDlg, IDCANCEL), (2*cTmpX+4)*cxb, cTmpY*cyb, (cTmpX-2)*cxb, 15*cyb, TRUE);
  648.  
  649.   SendDlgItemMessage (hDlg, IDD_LBPING, LB_SETHORIZONTALEXTENT, (WPARAM) nExtent, 0L);
  650.   return TRUE;
  651. }
  652.  
  653. //***********************************************************************
  654. // Misc Dialog Window Procedures                                        *
  655. //***********************************************************************
  656. BOOL CALLBACK WS_FindHostProc (HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
  657.   switch (Msg)
  658.   {
  659.       case WM_INITDIALOG  : return OnDlgInitDlg (hDlg, Msg, wParam, lParam);
  660.       case WM_SIZE        : return OnDlgResize (hDlg, Msg, wParam, lParam);
  661.     case WM_TIMER       : return OnDlgTimer (hDlg, Msg, wParam, lParam);
  662.       case WM_COMMAND     : return OnDlgCommand (hDlg, Msg, wParam, lParam);
  663.     case WM_PING_HOST   : return OnDlgPingHost (hDlg, Msg, wParam, lParam);
  664.     case WM_PING_PROTO  : return OnDlgPingProto (hDlg, Msg, wParam, lParam);
  665.     case WM_PING_CAS    : return OnDlgPingAsync (hDlg, Msg, wParam, lParam);
  666.     case WM_PING_RECEIVE: return OnDlgPingRecv (hDlg, Msg, wParam, lParam);
  667.     case WM_PING_FINISH : return OnDlgPingFinish (hDlg, Msg, wParam, lParam);
  668.     case WM_RESETCURSOR : return OnDlgResetCursor (hDlg, Msg, wParam, lParam);
  669.     case WM_SETCURSOR   : return OnDlgSetCursor (hDlg, Msg, wParam, lParam);
  670.     default             : return FALSE;
  671.   }
  672.   return FALSE;
  673. }
  674.  
  675.  
  676.