Exemple #1
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 #2
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 #3
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 #4
0
static DWORD
IcmpThread(LPDWORD lpdwParam)
{
    IcmpThreadParam *threadParamPtr = (IcmpThreadParam *) lpdwParam;
    TnmIcmpRequest *icmpPtr = threadParamPtr->icmpPtr;
    TnmIcmpTarget *targetPtr = threadParamPtr->targetPtr;
    DWORD dwStatus;
    PIP_ECHO_REPLY pIpe;
    PIP_OPTION_INFORMATION optInfPtr = NULL;
    char *mem = NULL;
    int i;

    /*
     * We do not support ICMP mask or ICMP timestamp requests.
     */
    
    if (icmpPtr->type == TNM_ICMP_TYPE_MASK 
	|| icmpPtr->type == TNM_ICMP_TYPE_TIMESTAMP) {
	targetPtr->status = TNM_ICMP_STATUS_TIMEOUT;
	goto exit;
    }

    /*
     * Allocate the parameter block which is passed to the
     * ICMP library call.
     */

    mem = ckalloc(sizeof(IP_ECHO_REPLY) + icmpPtr->size);

    for (i = 0; i <= icmpPtr->retries; i++) {

	int timeout = (1000 * icmpPtr->timeout) * (i + 1)
	    / (icmpPtr->retries + 1);
	
#if 0
	{ char buf[80];
	  sprintf(buf, "try %d timeout %d\n", i, timeout);
	  TnmWriteMessage(buf);
	}
#endif
	  
	memset(mem, 0, sizeof(IP_ECHO_REPLY) + icmpPtr->size);

	pIpe = (PIP_ECHO_REPLY) mem;
	pIpe->Data = mem + sizeof(IP_ECHO_REPLY);
	pIpe->DataSize = icmpPtr->size;

	/*
	 * Set the TTL field if we are doing a traceroute step.
	 */
	
	if (icmpPtr->type == TNM_ICMP_TYPE_TRACE) {
	    optInfPtr = (PIP_OPTION_INFORMATION) ckalloc(sizeof(*optInfPtr));
	    memset((void *) optInfPtr, 0, sizeof(IP_OPTION_INFORMATION));
	    optInfPtr->Ttl = icmpPtr->ttl;
	}

	targetPtr->status = TNM_ICMP_STATUS_GENERROR;
	dwStatus = pIcmpSendEcho(hIP, targetPtr->dst.s_addr,
				 pIpe->Data, pIpe->DataSize, optInfPtr,
				 pIpe, sizeof(IP_ECHO_REPLY) + icmpPtr->size,
				 timeout);
	if (dwStatus) {
#if 0
	    { char buf[80];
	      sprintf(buf,
		      "Addr:%d.%d.%d.%d,\tRTT: %dms,\tTTL: %d,\tStatus: %d\n",
		      LOBYTE(LOWORD(pIpe->Address)),
		      HIBYTE(LOWORD(pIpe->Address)),
		      LOBYTE(HIWORD(pIpe->Address)),
		      HIBYTE(HIWORD(pIpe->Address)),
		      pIpe->RoundTripTime,
		      pIpe->Options.Ttl,
		      pIpe->Status);
	      TnmWriteMessage(buf);
	    }
#endif
	    if (pIpe->Status == IP_SUCCESS ||
		pIpe->Status == IP_TTL_EXPIRED_TRANSIT ||
		pIpe->Status == IP_DEST_PORT_UNREACHABLE) {
		targetPtr->status = TNM_ICMP_STATUS_NOERROR;
		targetPtr->res.s_addr = pIpe->Address;
		targetPtr->u.rtt = pIpe->RoundTripTime * 1000;
		if (icmpPtr->type == TNM_ICMP_TYPE_TRACE 
		    && pIpe->Status == IP_DEST_PORT_UNREACHABLE) {
		    targetPtr->flags |= TNM_ICMP_FLAG_LASTHOP;
		}
		break;
	    }
	}
    }

 exit:
    if (mem) ckfree((char *) mem);
    if (threadParamPtr) ckfree((char *) threadParamPtr);
    return 0;
}
Exemple #5
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);
}