home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CD Loisirs 18
/
cd.iso
/
PLANETE
/
WS_PING
/
WSPI_SRC.ZIP
/
WS_PING.C
< prev
next >
Wrap
C/C++ Source or Header
|
1994-01-23
|
19KB
|
600 lines
/*************************************************************************
Windows Sockets PING Client Application
Written by John A. Junod, 267 Hillwood St., Martinez, GA, 30907 93.10.01
<junodj@css583.gordon.army.mil> <zj8549@trotter.usma.edu> C$:72321,366
Released into the public domain with no restrictions other than to give
me some of the credit if you use this code in other applications. Some
code concepts based on code published in UNIX Network Programming by
W. Richard Stevens or on BSD networking source code.
Note that MOST Windows Sockets DLL's do not support RAW_SOCKETS. This
does work with Trumpet's Windows Sockets DLL Alpha 18.
Purpose of this program was to see how a PING program could be implemented
under Windows Sockets. This program uses either blocking or async
socket calls to perform its magic.
*************************************************************************/
//----------------------
#include "ws_glob.h"
#include "WS_ping.H"
#include "ip_icmp.h"
//----------------------
BOOL bSpecified=FALSE;
int nPktLength=50;
int nNumPkts=10;
int nTimeOut=10;
extern int nTransmitted,nReceived;
int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)
{
MSG msg; // MSG structure to store your messages
int nRc; // return value from Register Classes
long nWndunits; // window units for size and location
int nX; // the resulting starting point (x, y)
int nY;
int nWidth; // the resulting width and height for this
int nHeight; // window
int err;
strcpy(szAppName, "WS_PING");
hInst = hInstance;
if(!hPrevInstance)
{
if ((nRc = nCwRegisterClasses()) == -1)
{
MessageBox(NULL, "Window creation failed", NULL, MB_ICONEXCLAMATION);
return nRc;
}
}
// Create a device independant size and location
nWndunits = GetDialogBaseUnits();
nWndx = LOWORD(nWndunits);
nWndy = HIWORD(nWndunits);
nX = ((18 * nWndx) / 4);
nY = ((18 * nWndy) / 8);
nWidth = ((247 * nWndx) / 4);
nHeight = ((218 * nWndy) / 8);
// create application's Main window
hWndMain = CreateWindow(
szAppName, "WinSock_PING",
WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
WS_MAXIMIZEBOX | WS_THICKFRAME | WS_CLIPCHILDREN | WS_OVERLAPPED,
nX,nY, nWidth, nHeight,
NULL, NULL, hInst, NULL);
if(hWndMain == NULL)
{
MessageBox(NULL, "Error registering class", NULL, MB_ICONEXCLAMATION);
return 2;
}
if(lstrlen((LPSTR)lpszCmdLine)>0) {
bSpecified=TRUE;
strcpy(szRemoteHost,lpszCmdLine);
}
if (err = WSAStartup( 0x0101, &WSAData)) // register task with
{ // winsock tcp/ip API
ReportWSError(err);
} else {
GetLocalInfo();
ShowWindow(hWndMain, nCmdShow); // display main window
while(GetMessage(&msg, NULL, 0, 0)) // Until WM_QUIT message
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
WSACleanup();
ReleaseDisplayMem();
}
// Do clean up before exiting from the application
CwUnRegisterClasses();
return msg.wParam;
}
#define WM_RESETCURSOR WM_USER+10
// Main window message processing
LONG FAR PASCAL WndProc(HWND hWnd,WORD Message,WORD wParam,LONG lParam)
{
int nIndex,nRC,nBytesRecv;
static int saFromAddrLen;
static struct sockaddr_in saDestAddr;
static struct sockaddr_in saFromAddr;
static struct hostent HostEntry , *pHostEntry;
static struct protoent ProtoEntry, *pProtoEntry;
static HANDLE hATHhost;
static HANDLE hATHprot;
switch (Message)
{
case WM_CREATE:
hStdCursor=LoadCursor(NULL,IDC_ARROW);
hWaitCursor=LoadCursor(NULL,IDC_WAIT);
if(!bSpecified)
GetPrivateProfileString("WS_PING", "HOSTNAME",
"129.29.64.246",szRemoteHost,79,szIniFile);
else
PostMessage(hWnd,WM_COMMAND,IDM_ASYNC_CONNECT,0L);
break;
case WM_TIMER:
switch(wParam) {
case 10:
DoPrintf("[Timer-TIMEOUT] ");
KillTimer(hWnd,10);
if(WSAIsBlocking()) {
bAborted=TRUE;
WSACancelBlockingCall();
}
break;
case 20:
DoPrintf("[Timer-ASYNC_SEND] ");
if(nTransmitted<nNumPkts) {
send_ping(ping_socket,&saDestAddr,szMsgBuf,
nPktLength+SIZE_ICMP_HDR);
PostMessage(hWnd,WM_PING_RECEIVE,0,0l);
} else {
KillTimer(hWnd,20);
if(nReceived!=nTransmitted)
SendMessage(hWnd,WM_PING_RECEIVE,0,0l);
PostMessage(hWnd,WM_PING_FINISH,0,0l);
}
break;
case 30:
DoPrintf("[Timer-BLOCK_SEND] ");
if(nTransmitted<nNumPkts)
{
if(send_ping(ping_socket,&saDestAddr,szMsgBuf,
nPktLength+SIZE_ICMP_HDR))
recv_ping(ping_socket,szString,TRUE);
}
else {
KillTimer(hWnd,30);
if(nReceived!=nTransmitted)
recv_ping(ping_socket,szString,TRUE);
PostMessage(hWnd,WM_PING_FINISH,0,0l);
}
break;
default:
KillTimer(hWnd,wParam);
break;
}
break;
case WM_COMMAND:
if(wParam==IDM_CLOSE || wParam==IDM_EXIT) {
if(ping_socket!=INVALID_SOCKET) {
ping_socket=DoClose(ping_socket);
SendMessage(hWnd,WM_RESETCURSOR,0,0l);
}
if(wParam==IDM_EXIT)
SendMessage(hWnd,WM_CLOSE,0,0L);
break;
} else if(bCmdInProgress) return(FALSE);
switch (wParam)
{
case IDM_ASYNC_CONNECT:
bCmdInProgress=1;
if(!bSpecified)
{ FARPROC lpfnMsgProc;
lpfnMsgProc=MakeProcInstance((FARPROC)WS_HostMsgProc,hInst);
DialogBox(hInst,(LPSTR)"DLG_HOST",hWnd,lpfnMsgProc);
FreeProcInstance(lpfnMsgProc);
}
bSpecified=FALSE;
memset(&saDestAddr,0,sizeof(struct sockaddr_in));
memset(&HostEntry,0,sizeof(struct hostent));
memset(&ProtoEntry,0,sizeof(struct protoent));
saDestAddr.sin_family=AF_INET;
if((saDestAddr.sin_addr.s_addr=inet_addr(szRemoteHost))==INADDR_NONE)
hATHhost=WSAAsyncGetHostByName(hWnd,WM_PING_HOST,szRemoteHost,
szMsgBuf,MAXGETHOSTSTRUCT);
else hATHhost=0;
hATHprot=WSAAsyncGetProtoByName(hWnd,WM_PING_PROTO,"icmp",
&szMsgBuf[MAXGETHOSTSTRUCT],MAXGETHOSTSTRUCT);
break;
case IDM_LOOKUP:
bCmdInProgress=1;
if(!bSpecified)
{ FARPROC lpfnMsgProc;
lpfnMsgProc=MakeProcInstance((FARPROC)WS_HostMsgProc,hInst);
DialogBox(hInst,(LPSTR)"DLG_HOST",hWnd,lpfnMsgProc);
FreeProcInstance(lpfnMsgProc);
}
bSpecified=FALSE;
memset(&saDestAddr,0,sizeof(struct sockaddr));
saDestAddr.sin_family=AF_INET;
DoPrintf(" ");
if((saDestAddr.sin_addr.s_addr=inet_addr(szRemoteHost))==INADDR_NONE) {
if(!(pHostEntry=gethostbyname(szRemoteHost))) {
DoPrintf("can't get \"%s\" host entry.",szRemoteHost);
SendMessage(hWnd,WM_RESETCURSOR,0,0l);
bCmdInProgress=0;
break;
}
} else {
if(!(pHostEntry=gethostbyaddr((LPSTR)&saDestAddr.sin_addr.s_addr,
4,PF_INET))) {
DoPrintf("can't get \"%s\" ip entry.",szRemoteHost);
goto skipit;
}
}
{
struct in_addr *iptr;
LPSTR lptr;
int nCount=0;
memcpy(&saDestAddr.sin_addr,pHostEntry->h_addr,
pHostEntry->h_length);