home *** CD-ROM | disk | FTP | other *** search
/ CD Loisirs 18 / cd.iso / PLANETE / WS_PING / WSPI_SRC.ZIP / WS_PING.C < prev    next >
C/C++ Source or Header  |  1994-01-23  |  19KB  |  600 lines

  1. /*************************************************************************
  2.  Windows Sockets PING Client Application
  3.  
  4.  Written by John A. Junod, 267 Hillwood St., Martinez, GA, 30907 93.10.01
  5.  <junodj@css583.gordon.army.mil> <zj8549@trotter.usma.edu>   C$:72321,366
  6.  
  7.  Released into the public domain with no restrictions other than to give
  8.  me some of the credit if you use this code in other applications.  Some
  9.  code concepts based on code published in UNIX Network Programming by
  10.  W. Richard Stevens or on BSD networking source code.
  11.  
  12.  Note that MOST Windows Sockets DLL's do not support RAW_SOCKETS.  This
  13.  does work with Trumpet's Windows Sockets DLL Alpha 18.
  14.  
  15.  Purpose of this program was to see how a PING program could be implemented
  16.  under Windows Sockets.  This program uses either blocking or async
  17.  socket calls to perform its magic.  
  18. *************************************************************************/
  19.  
  20. //----------------------
  21. #include "ws_glob.h"
  22. #include "WS_ping.H"
  23. #include "ip_icmp.h"
  24. //----------------------
  25.  
  26. BOOL bSpecified=FALSE;
  27.  
  28. int nPktLength=50;
  29. int nNumPkts=10;
  30. int nTimeOut=10;
  31. extern int nTransmitted,nReceived;
  32.  
  33. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  34.                    LPSTR lpszCmdLine, int nCmdShow)
  35. {
  36.   MSG        msg;           // MSG structure to store your messages
  37.   int        nRc;           // return value from Register Classes
  38.   long       nWndunits;     // window units for size and location
  39.   int        nX;            // the resulting starting point (x, y)
  40.   int        nY;
  41.   int        nWidth;        // the resulting width and height for this
  42.   int        nHeight;       // window
  43.   int        err;
  44.  
  45.   strcpy(szAppName, "WS_PING");
  46.   hInst = hInstance;
  47.   if(!hPrevInstance)
  48.   {
  49.     if ((nRc = nCwRegisterClasses()) == -1)
  50.     {
  51.       MessageBox(NULL, "Window creation failed", NULL, MB_ICONEXCLAMATION);
  52.       return nRc;    
  53.     }
  54.   }
  55.  
  56.   // Create a device independant size and location
  57.   nWndunits = GetDialogBaseUnits();
  58.   nWndx = LOWORD(nWndunits);
  59.   nWndy = HIWORD(nWndunits);
  60.   nX = ((18 * nWndx) / 4);
  61.   nY = ((18 * nWndy) / 8);
  62.   nWidth = ((247 * nWndx) / 4);
  63.   nHeight = ((218 * nWndy) / 8);
  64.  
  65.   // create application's Main window
  66.   hWndMain = CreateWindow(
  67.     szAppName, "WinSock_PING",
  68.     WS_CAPTION     | WS_SYSMENU    | WS_MINIMIZEBOX  |
  69.     WS_MAXIMIZEBOX | WS_THICKFRAME | WS_CLIPCHILDREN | WS_OVERLAPPED,
  70.     nX,nY, nWidth, nHeight,
  71.     NULL, NULL, hInst,  NULL);
  72.  
  73.   if(hWndMain == NULL)
  74.   {
  75.     MessageBox(NULL, "Error registering class", NULL, MB_ICONEXCLAMATION);
  76.     return 2;
  77.   }
  78.  
  79.   if(lstrlen((LPSTR)lpszCmdLine)>0) {
  80.     bSpecified=TRUE;
  81.     strcpy(szRemoteHost,lpszCmdLine);
  82.   }
  83.  
  84.   if (err = WSAStartup( 0x0101, &WSAData))  // register task with
  85.   {                                         // winsock tcp/ip API
  86.     ReportWSError(err);            
  87.   } else {
  88.     GetLocalInfo();
  89.     ShowWindow(hWndMain, nCmdShow);         // display main window
  90.     while(GetMessage(&msg, NULL, 0, 0))     // Until WM_QUIT message
  91.     {
  92.       TranslateMessage(&msg);
  93.       DispatchMessage(&msg);
  94.     }
  95.     WSACleanup();
  96.     ReleaseDisplayMem();
  97.   }
  98.   // Do clean up before exiting from the application
  99.   CwUnRegisterClasses();
  100.   return msg.wParam;
  101. }
  102.  
  103. #define WM_RESETCURSOR WM_USER+10
  104.  
  105. // Main window message processing
  106. LONG FAR PASCAL WndProc(HWND hWnd,WORD Message,WORD wParam,LONG lParam)
  107. {
  108.   int nIndex,nRC,nBytesRecv;
  109.   static int saFromAddrLen;
  110.   static struct sockaddr_in saDestAddr;
  111.   static struct sockaddr_in saFromAddr;
  112.   static struct hostent  HostEntry , *pHostEntry;
  113.   static struct protoent ProtoEntry, *pProtoEntry;
  114.   static HANDLE hATHhost;
  115.   static HANDLE hATHprot;
  116.  
  117.   switch (Message)
  118.   {
  119.     case WM_CREATE:
  120.       hStdCursor=LoadCursor(NULL,IDC_ARROW);
  121.       hWaitCursor=LoadCursor(NULL,IDC_WAIT);
  122.       if(!bSpecified)
  123.         GetPrivateProfileString("WS_PING", "HOSTNAME",
  124.           "129.29.64.246",szRemoteHost,79,szIniFile);
  125.       else
  126.         PostMessage(hWnd,WM_COMMAND,IDM_ASYNC_CONNECT,0L);
  127.       break;
  128.  
  129.     case WM_TIMER:
  130.       switch(wParam) {
  131.         case 10:
  132.           DoPrintf("[Timer-TIMEOUT] ");
  133.           KillTimer(hWnd,10);
  134.           if(WSAIsBlocking()) {
  135.             bAborted=TRUE;
  136.             WSACancelBlockingCall();
  137.           }
  138.           break;
  139.  
  140.         case 20:
  141.           DoPrintf("[Timer-ASYNC_SEND] ");
  142.           if(nTransmitted<nNumPkts) {
  143.             send_ping(ping_socket,&saDestAddr,szMsgBuf,
  144.                       nPktLength+SIZE_ICMP_HDR);
  145.             PostMessage(hWnd,WM_PING_RECEIVE,0,0l);
  146.           } else {
  147.             KillTimer(hWnd,20);
  148.             if(nReceived!=nTransmitted)
  149.               SendMessage(hWnd,WM_PING_RECEIVE,0,0l);
  150.             PostMessage(hWnd,WM_PING_FINISH,0,0l);
  151.           }
  152.           break;
  153.  
  154.         case 30:
  155.           DoPrintf("[Timer-BLOCK_SEND] ");
  156.           if(nTransmitted<nNumPkts)
  157.           {
  158.             if(send_ping(ping_socket,&saDestAddr,szMsgBuf,
  159.                       nPktLength+SIZE_ICMP_HDR))
  160.               recv_ping(ping_socket,szString,TRUE);
  161.           }
  162.           else {
  163.             KillTimer(hWnd,30);
  164.             if(nReceived!=nTransmitted)
  165.               recv_ping(ping_socket,szString,TRUE);
  166.             PostMessage(hWnd,WM_PING_FINISH,0,0l);
  167.           }
  168.           break;
  169.  
  170.         default:
  171.           KillTimer(hWnd,wParam);
  172.           break;
  173.       }
  174.       break;
  175.     case WM_COMMAND:
  176.       if(wParam==IDM_CLOSE || wParam==IDM_EXIT) {
  177.         if(ping_socket!=INVALID_SOCKET) {
  178.           ping_socket=DoClose(ping_socket);
  179.           SendMessage(hWnd,WM_RESETCURSOR,0,0l);
  180.         }
  181.         if(wParam==IDM_EXIT)
  182.           SendMessage(hWnd,WM_CLOSE,0,0L);
  183.         break;
  184.       } else if(bCmdInProgress) return(FALSE);
  185.       switch (wParam)
  186.       {
  187.         case IDM_ASYNC_CONNECT:
  188.           bCmdInProgress=1;
  189.           if(!bSpecified)
  190.             { FARPROC lpfnMsgProc;
  191.               lpfnMsgProc=MakeProcInstance((FARPROC)WS_HostMsgProc,hInst);
  192.               DialogBox(hInst,(LPSTR)"DLG_HOST",hWnd,lpfnMsgProc);
  193.               FreeProcInstance(lpfnMsgProc);
  194.             }
  195.           bSpecified=FALSE;
  196.           memset(&saDestAddr,0,sizeof(struct sockaddr_in));
  197.           memset(&HostEntry,0,sizeof(struct hostent));
  198.           memset(&ProtoEntry,0,sizeof(struct protoent));
  199.           saDestAddr.sin_family=AF_INET;
  200.           if((saDestAddr.sin_addr.s_addr=inet_addr(szRemoteHost))==INADDR_NONE)
  201.             hATHhost=WSAAsyncGetHostByName(hWnd,WM_PING_HOST,szRemoteHost,
  202.                         szMsgBuf,MAXGETHOSTSTRUCT);
  203.           else hATHhost=0;
  204.           hATHprot=WSAAsyncGetProtoByName(hWnd,WM_PING_PROTO,"icmp",
  205.                         &szMsgBuf[MAXGETHOSTSTRUCT],MAXGETHOSTSTRUCT);
  206.           break;
  207.  
  208.         case IDM_LOOKUP:
  209.           bCmdInProgress=1;
  210.           if(!bSpecified)
  211.             { FARPROC lpfnMsgProc;
  212.               lpfnMsgProc=MakeProcInstance((FARPROC)WS_HostMsgProc,hInst);
  213.               DialogBox(hInst,(LPSTR)"DLG_HOST",hWnd,lpfnMsgProc);
  214.               FreeProcInstance(lpfnMsgProc);
  215.             }
  216.           bSpecified=FALSE;
  217.           memset(&saDestAddr,0,sizeof(struct sockaddr));
  218.           saDestAddr.sin_family=AF_INET;
  219.           DoPrintf(" ");
  220.           
  221.           if((saDestAddr.sin_addr.s_addr=inet_addr(szRemoteHost))==INADDR_NONE) {
  222.             if(!(pHostEntry=gethostbyname(szRemoteHost))) {
  223.               DoPrintf("can't get \"%s\" host entry.",szRemoteHost);
  224.               SendMessage(hWnd,WM_RESETCURSOR,0,0l);
  225.               bCmdInProgress=0;
  226.               break;
  227.             }
  228.           } else {
  229.             if(!(pHostEntry=gethostbyaddr((LPSTR)&saDestAddr.sin_addr.s_addr,
  230.                 4,PF_INET))) {
  231.               DoPrintf("can't get \"%s\" ip entry.",szRemoteHost);
  232.               goto skipit;
  233.             }
  234.           }
  235.           {
  236.             struct in_addr *iptr;
  237.             LPSTR lptr;
  238.             int nCount=0;
  239.               memcpy(&saDestAddr.sin_addr,pHostEntry->h_addr,
  240.                      pHostEntry->h_length);
  241.