bool ICMP::ping(char *host, ICMP_ECHO_REPLY &reply) { if(!functions_loaded) return false; unsigned long address; HOSTENT *rec; IP_OPTION_INFORMATION ipoi; address = inet_addr(host); if (address == INADDR_NONE) { rec = gethostbyname(host); if(rec) address = *(unsigned long *)(*rec->h_addr_list); else return false; } ipoi.Ttl = 255; ipoi.Tos = 0; ipoi.Flags = 0; ipoi.OptionsSize = 0; ipoi.OptionsData = 0; reply.Status = 0; hIP = pIcmpCreateFile(); if (hIP == INVALID_HANDLE_VALUE) return false; //pIcmpSendEcho2(hIP, 0, 0, 0, address, data, sizeof(data), &ipoi, buff, sizeof(ICMP_ECHO_REPLY) + sizeof(data), timeout); if(pIcmpSendEcho2(hIP, 0, 0, 0, address, data, sizeof(data), 0, buff, BUFFER_SIZE, timeout) == 0) { DWORD code = GetLastError(); if(code != 11010) { char winmsg[512], msg[1024]; FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, 0, code, 0, winmsg, 512, 0); mir_snprintf(msg, 1024, "Ping error (%d): %s", code, winmsg); PUShowMessage(msg, SM_NOTIFY); return false; } } memcpy(&reply, buff, sizeof(ICMP_ECHO_REPLY)); pIcmpCloseHandle(hIP); return (reply.Status == 0); }
int CPing::Ping(string ip) { iaDest.s_addr = inet_addr(ip); dwAddress = (DWORD )(iaDest.s_addr); hndlFile = pIcmpCreateFile(); ipInfo.Ttl = 255; ipInfo.Tos = 0; ipInfo.IPFlags = 0; ipInfo.OptSize = 0; ipInfo.Options = NULL; dwRet = pIcmpSendEcho(hndlFile,dwAddress,NULL,0,&ipInfo,&icmpEcho,sizeof(struct tagICMPECHO),1000); pIcmpCloseHandle(hndlFile); //FreeLibrary(hndlIcmp); //WSACleanup(); //getch(); return icmpEcho.RTTime; }
int TnmIcmp(Tcl_Interp *interp, TnmIcmpRequest *icmpPtr) { static HANDLE hIcmp = 0; /* The handle for ICMP.DLL. */ int j, i, code; DWORD nCount, dwStatus; HANDLE *lpHandles; if (! hIcmp) { hIcmp = LoadLibrary("ICMP.DLL"); if (hIcmp) { Tcl_CreateExitHandler(IcmpExit, hIcmp); (FARPROC) pIcmpCreateFile = (FARPROC) GetProcAddress(hIcmp, "IcmpCreateFile"); (FARPROC) pIcmpCloseHandle = (FARPROC) GetProcAddress(hIcmp, "IcmpCloseHandle"); (FARPROC) pIcmpSendEcho = (FARPROC) GetProcAddress(hIcmp, "IcmpSendEcho"); if (! pIcmpCreateFile || ! pIcmpCloseHandle || ! pIcmpSendEcho) { FreeLibrary(hIcmp); hIcmp = 0; } } } if (! hIcmp) { Tcl_SetResult(interp, "failed to load ICMP.DLL", TCL_STATIC); return TCL_ERROR; } hIP = pIcmpCreateFile(); if (hIP == INVALID_HANDLE_VALUE) { Tcl_SetResult(interp, "failed to access ICMP.DLL", TCL_STATIC); return TCL_ERROR; } /* * Create a thread for every single target. */ lpHandles = (HANDLE *) ckalloc(icmpPtr->numTargets * sizeof(HANDLE)); code = TCL_OK; j = 0; while (j < icmpPtr->numTargets) { for (i = j, nCount = 0; i < icmpPtr->numTargets && nCount < 200 && (! icmpPtr->window || (int) nCount < icmpPtr->window); i++, nCount++) { DWORD dwThreadId; TnmIcmpTarget *targetPtr = &(icmpPtr->targets[i]); IcmpThreadParam *threadParamPtr = (IcmpThreadParam *) ckalloc(sizeof(IcmpThreadParam)); threadParamPtr->icmpPtr = icmpPtr; threadParamPtr->targetPtr = targetPtr; lpHandles[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) IcmpThread, (LPDWORD) threadParamPtr, 0, &dwThreadId); if (! lpHandles[i]) { Tcl_ResetResult(interp); Tcl_SetResult(interp, "failed to create ICMP thread", TCL_STATIC); code = TCL_ERROR; break; } } /* * Wait here until all threads have finished, release the handles, * free the handle array and finally move on. */ dwStatus = WaitForMultipleObjects(nCount, lpHandles+j, TRUE, INFINITE); if (dwStatus == WAIT_FAILED) { Tcl_ResetResult(interp); Tcl_SetResult(interp, "failed to wait for ICMP thread", TCL_STATIC); code = TCL_ERROR; } for (i = j; nCount-- > 0; i++, j++) { if (lpHandles[i]) { CloseHandle(lpHandles[i]); } } } ckfree((char *) lpHandles); pIcmpCloseHandle(hIP); return code; }