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);
}
Exemple #2
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;
}
Exemple #3
0
int TPing::Ping(AnsiString ip,int &ok,int &time)
{
    ok=0;
    struct hostent* phe ;
    if ((phe = gethostbyname(ip.c_str())) == 0)
      return 0;

    HANDLE hIP = pIcmpCreateFile();
    if (hIP == INVALID_HANDLE_VALUE) return 0;
    memset(acPingBuffer, '\xAA', sizeof(acPingBuffer));
    PIP_ECHO_REPLY pIpe = (PIP_ECHO_REPLY)GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT,sizeof(IP_ECHO_REPLY) + sizeof(acPingBuffer));
    if (pIpe == 0) {
       return 0;
    }

    pIpe->Data = acPingBuffer;
    pIpe->DataSize = sizeof(acPingBuffer);

    // Send the ping packet
    DWORD dwStatus = pIcmpSendEcho(hIP, *((DWORD*)phe->h_addr_list[0]),
            acPingBuffer, sizeof(acPingBuffer), NULL, pIpe,
            sizeof(IP_ECHO_REPLY) + sizeof(acPingBuffer), 2400);
    if (dwStatus != 0) {
       if (pIpe->Status>0) ok=0;
       else {
         time=int(pIpe->RoundTripTime);
         ok=1;
       };
    }
    else {
        ok=0;
    }
 //----------------------
 GlobalFree(pIpe);
 return 1;
} ;
Exemple #4
0
Packet Ping::pingHost(const QString &hostname)
{
    Packet packetData;
    packetData.ErrorValue = 0;
    packetData.Time = QDateTime::currentDateTime();

    WSAData wsaData;
    if (WSAStartup(MAKEWORD(1, 1), &wsaData) == 0)
    {
        // Load the ICMP.DLL
        HINSTANCE hIcmp = LoadLibraryA("ICMP.DLL");
        if (hIcmp == 0)
        {
            packetData.Message = "Unable to locate ICMP.DLL!";
            packetData.ErrorValue = 2;
            return packetData;
        }

        // Look up an IP address for the given host name
        struct hostent* phe;
        if ((phe = gethostbyname(hostname.toLocal8Bit().data())) == 0)
        {
            packetData.Message = "Could not find IP address for " + hostname;
            packetData.ErrorValue = 3;
            return packetData;
        }

        // Get handles to the functions inside ICMP.DLL that we'll need
        typedef HANDLE (WINAPI* pfnHV)(VOID);
        typedef BOOL (WINAPI* pfnBH)(HANDLE);
        typedef DWORD (WINAPI* pfnDHDPWPipPDD)(HANDLE, DWORD, LPVOID, WORD,
                                               PIP_OPTION_INFORMATION, LPVOID, DWORD, DWORD); // evil, no?
        pfnHV pIcmpCreateFile;
        pfnBH pIcmpCloseHandle;
        pfnDHDPWPipPDD pIcmpSendEcho;
        pIcmpCreateFile = (pfnHV)GetProcAddress(hIcmp, "IcmpCreateFile");
        pIcmpCloseHandle = (pfnBH)GetProcAddress(hIcmp, "IcmpCloseHandle");
        pIcmpSendEcho = (pfnDHDPWPipPDD)GetProcAddress(hIcmp, "IcmpSendEcho");
        if ((pIcmpCreateFile == 0) || (pIcmpCloseHandle == 0) || (pIcmpSendEcho == 0))
        {
            packetData.Message = "Failed to get proc addr for function.";
            packetData.ErrorValue = 4;
            return packetData;
        }

        // Open the ping service
        HANDLE hIP = pIcmpCreateFile();
        if (hIP == INVALID_HANDLE_VALUE)
        {
            packetData.Message = "Unable to open ping service.";
            packetData.ErrorValue = 5;
            return packetData;
        }

        // Build ping packet
        char acPingBuffer[64];
        memset(acPingBuffer, '\xAA', sizeof(acPingBuffer));
        PIP_ECHO_REPLY pIpe = (PIP_ECHO_REPLY)GlobalAlloc(
                    GMEM_FIXED | GMEM_ZEROINIT,
                    sizeof(IP_ECHO_REPLY) + sizeof(acPingBuffer));

        if (pIpe == 0)
        {
            packetData.Message = "Failed to allocate global ping packet buffer.";
            packetData.ErrorValue = 6;
            return packetData;
        }
        pIpe->Data = acPingBuffer;
        pIpe->DataSize = sizeof(acPingBuffer);

        // Send the ping packet
        DWORD dwStatus = pIcmpSendEcho(hIP, *((DWORD*)phe->h_addr_list[0]),
                                       acPingBuffer, sizeof(acPingBuffer), NULL, pIpe,
                                       sizeof(IP_ECHO_REPLY) + sizeof(acPingBuffer), 5000);
        if (dwStatus != 0)
        {
            packetData.Host = QString::number(int(LOBYTE(LOWORD(pIpe->Address)))) + "." +
                    QString::number(int(HIBYTE(LOWORD(pIpe->Address)))) + "." +
                    QString::number(int(LOBYTE(HIWORD(pIpe->Address)))) + "." +
                    QString::number(int(HIBYTE(HIWORD(pIpe->Address))));
            packetData.PingTime = pIpe->RoundTripTime;

        }
        else
        {
            packetData.Message = "Error obtaining info from ping packet.";
            packetData.ErrorValue = 1;
        }

        // Shut down...
        GlobalFree(pIpe);
        FreeLibrary(hIcmp);
        WSACleanup();
    }
    else
    {
        packetData.ErrorValue = 7;
    }

    return packetData;
}
Exemple #5
0
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;
}
Exemple #6
0
//taken and adapted from http://tangentsoft.net/wskfaq/examples/dllping.html
void Find_Net(char *subnet, bool save)
{
	FILE *fp;
	HINSTANCE hIcmp = LoadLibrary((LPCWSTR)"ICMP.DLL");
	if (hIcmp == 0)
	{
		printf("Error: icmp.dll missing...\n");
		return;
	}
	typedef HANDLE (WINAPI* pfnHV)(VOID);
	typedef BOOL (WINAPI* pfnBH)(HANDLE);
	typedef DWORD (WINAPI* pfnDHDPWPipPDD)(HANDLE, DWORD, LPVOID, WORD,PIP_OPTION_INFORMATION, LPVOID, DWORD, DWORD);
	pfnHV pIcmpCreateFile;
	pfnBH pIcmpCloseHandle;
	pfnDHDPWPipPDD pIcmpSendEcho;
	pIcmpCreateFile = (pfnHV)GetProcAddress(hIcmp, "IcmpCreateFile");
	pIcmpCloseHandle = (pfnBH)GetProcAddress(hIcmp, "IcmpCloseHandle");
    pIcmpSendEcho = (pfnDHDPWPipPDD)GetProcAddress(hIcmp,"IcmpSendEcho");
	if ((pIcmpCreateFile == 0) || (pIcmpCloseHandle == 0) || (pIcmpSendEcho == 0)) {
			 printf("unable to create the functions...");
			 return;		
	}
	for(int i=1; i <= 255; i++)
	{
		char *ip;
		ip = (char *)sprintf("%s.%c", subnet, (char)i);
		if(save == true) *fp = MakeReport(ip);
		struct hostent* phe;
		if((phe = gethostbyname(ip)) == 0)
		{
			printf("error to convert ip address..");
			return;
		}
		
		HANDLE hIP = pIcmpCreateFile();
		if (hIP == INVALID_HANDLE_VALUE)
		{
			printf("Unable to open ping service");
			return;
		}
		if(save == true) FileHeader(fp, ip);
		char acPingBuffer[64];
		memset(acPingBuffer, '\xAA', sizeof(acPingBuffer));
		PIP_ECHO_REPLY pIpe = (PIP_ECHO_REPLY)GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT,sizeof(IP_ECHO_REPLY) + sizeof(acPingBuffer));
		if (pIpe == 0) {
			printf("Failed to allocate global ping packet buffer.");
			return;
		}
		pIpe->Data = acPingBuffer;
		pIpe->DataSize = sizeof(acPingBuffer); 
		DWORD dwStatus = pIcmpSendEcho(hIP, *((DWORD*)phe->h_addr_list[0]), acPingBuffer, sizeof(acPingBuffer), NULL, pIpe, sizeof(IP_ECHO_REPLY) + sizeof(acPingBuffer), 5000);
		if (dwStatus != 0) {
			//found
			printf("[FOUND] %s", ip);
			if(save == true) WriteOnReport(fp, ip);
		}
		else
			continue;
		GlobalFree(pIpe);
	}
	if(save == true) SaveReport(fp);
	FreeLibrary(hIcmp);
}