Multiping

primitives Programm um alle 254 IPs in einem Netzwerk anzupingen.

multiping.c
// multiping / richard@borwinius.de /2012
// pingt in der angegebenen IPrange 254 Hosts durch
// Bsp: multiping.exe 10.10.14 1
// kompilieren mit: lc multiping.c
 
#include <windows.h>
#include <winsock.h>
#include <stdio.h>
#include <string.h>
#include <tcconio.h>
 
typedef struct tagIPINFO
{
        u_char Ttl;                     // Time To Live
        u_char Tos;                     // Type Of Service
        u_char IPFlags;                 // IP flags
        u_char OptSize;                 // Size of options data
        u_char FAR *Options;            // Options data buffer
}IPINFO, *PIPINFO;
 
typedef struct tagICMPECHO
{
        u_long Source;                  // Source address
        u_long Status;                  // IP status
        u_long RTTime;                  // Round trip time in milliseconds
        u_short DataSize;               // Reply data size
        u_short Reserved;               // Unknown
        void FAR *pData;                // Reply data buffer
        IPINFO  ipInfo;                 // Reply options
}ICMPECHO, *PICMPECHO;
 
 
// ICMP.DLL Export Function Pointers
HANDLE (WINAPI *pIcmpCreateFile)(VOID);
BOOL (WINAPI *pIcmpCloseHandle)(HANDLE);
DWORD (WINAPI *pIcmpSendEcho)
        (HANDLE,DWORD,LPVOID,WORD,PIPINFO,LPVOID,DWORD,DWORD);
 
//--------------------------------------------------------------------------
//Fehlermeldung als Text ausgeben
void DisplayErrorText(DWORD dwLastError)
{
    HMODULE hModule = NULL; // default to system source
    LPSTR MessageBuffer;
    DWORD dwBufferLength;
 
    DWORD dwFormatFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER |
        FORMAT_MESSAGE_IGNORE_INSERTS |
        FORMAT_MESSAGE_FROM_SYSTEM ;
 
    //
    // Call FormatMessage() to allow for message
    //  text to be acquired from the system
    //  or from the supplied module handle.
    //
 
    if(dwBufferLength = FormatMessageA(
        dwFormatFlags,
        NULL,//hModule, // module to get message from (NULL == system)
        dwLastError,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // default language
        (LPSTR) &MessageBuffer, 0, NULL  ))
    {
        DWORD dwBytesWritten;
        printf("%05d\t0x%x\t%s\n",dwLastError,dwLastError,MessageBuffer);
        //
        // Output message string on stderr.
        //
        //WriteFile( GetStdHandle(STD_ERROR_HANDLE),MessageBuffer, dwBufferLength, &dwBytesWritten, NULL  );
        //
        // Free the buffer allocated by the system.
        //
        LocalFree(MessageBuffer);
    }
 
    //
    // If we loaded a message source, unload it.
    //
    if(hModule != NULL)
        FreeLibrary(hModule);
}
//----------------------------------------------------------------------------
// host oder ip anpingen
int icmpping(char * HOSTorIP,unsigned int pz)
{
 WSADATA wsaData;                        // WSADATA
        ICMPECHO icmpEcho;                      // ICMP Echo reply buffer
        HANDLE hndlIcmp;                        // LoadLibrary() handle to ICMP.DLL
        HANDLE hndlFile;                        // Handle for IcmpCreateFile()
        LPHOSTENT pHost;                                // Pointer to host entry structure
        struct in_addr iaDest;                  // Internet address structure
        DWORD *dwAddress;                       // IP Address
        IPINFO ipInfo;                          // IP Options structure
        int nRet;                               // General use return code
        DWORD dwRet;                            // DWORD return code
        int x;
 
       // Dynamically load the ICMP.DLL
        hndlIcmp = LoadLibrary("ICMP.DLL");
        if (hndlIcmp == NULL)
        {
          fprintf(stderr,"\nKonnte  ICMP.DLL nicht laden\n");
          return GetLastError();
        }
        // Retrieve ICMP function pointers
        pIcmpCreateFile = (HANDLE (WINAPI *)(void))
                GetProcAddress(hndlIcmp,"IcmpCreateFile");
        pIcmpCloseHandle = (BOOL (WINAPI *)(HANDLE))
                GetProcAddress(hndlIcmp,"IcmpCloseHandle");
        pIcmpSendEcho = (DWORD (WINAPI *)
                (HANDLE,DWORD,LPVOID,WORD,PIPINFO,LPVOID,DWORD,DWORD))
                GetProcAddress(hndlIcmp,"IcmpSendEcho");
        // Check all the function pointers
        if (pIcmpCreateFile == NULL             ||
                pIcmpCloseHandle == NULL        ||
                pIcmpSendEcho == NULL)
        {
                fprintf(stderr,"\nError getting ICMP proc address\n");
                FreeLibrary(hndlIcmp);
                return GetLastError();
        }
 
        // Init WinSock
        nRet = WSAStartup(0x0101, &wsaData );
    if (nRet)
    {
        fprintf(stderr,"\nWSAStartup() error: %d\n", nRet);
        WSACleanup();
        FreeLibrary(hndlIcmp);
       return nRet;
    }
    // Check WinSock version
    if (0x0101 != wsaData.wVersion)
    {   nRet = GetLastError();
        fprintf(stderr,"\nWinSock version 1.1 not supported\n");
        WSACleanup();
                FreeLibrary(hndlIcmp);
        return nRet;
    }
    // Lookup destination
    // Use inet_addr() to determine if we're dealing with a name
    // or an address
    iaDest.s_addr = inet_addr(HOSTorIP);
    if (iaDest.s_addr == INADDR_NONE)
        pHost = gethostbyname(HOSTorIP);
    else
        pHost = gethostbyaddr((const char *)&iaDest,  sizeof(struct in_addr), AF_INET);
        if (pHost == NULL)
        {
        nRet = GetLastError();
        textcolor(LIGHTRED);
		fprintf(stderr, "\n%s nicht gefunden \n", HOSTorIP);
		//cprintf("\n%s nicht gefunden\n",argv[1]);
	textcolor(LIGHTGRAY);
        WSACleanup();
        FreeLibrary(hndlIcmp);
        return nRet;
        }
        // Tell the user what we're doing
        printf("\nIch pinge %s [%s]\n", pHost->h_name, inet_ntoa((*(LPIN_ADDR)pHost->h_addr_list[0])));
        // Copy the IP address
        dwAddress = (DWORD *)(*pHost->h_addr_list);
 
        // Get an ICMP echo request handle
        hndlFile = pIcmpCreateFile();
        for (x = 0; x < pz; x++)	// Pinganzahl
        {
                // Set some reasonable default values
                ipInfo.Ttl = 255;
                ipInfo.Tos = 0;
                ipInfo.IPFlags = 0;
                ipInfo.OptSize = 0;
                ipInfo.Options = NULL;
                icmpEcho.ipInfo.Ttl = ipInfo.Ttl;
                // Reqest an ICMP echo
                dwRet = pIcmpSendEcho(
                        hndlFile,               // Handle from IcmpCreateFile()
                        *dwAddress,             // Destination IP address
                        NULL,                   // Pointer to buffer to send
                        0,                              // Size of buffer in bytes
                        &ipInfo,                // Request options
                        &icmpEcho,              // Reply buffer
                        sizeof(struct tagICMPECHO),
                        5000);                  // Time to wait in milliseconds
                // Print the results
                iaDest.s_addr = icmpEcho.Source;
                textcolor(LIGHTGREEN);
			cprintf("\nAntwort von %s  Antwortzeit = %ld ms  TTL = %d",
                        inet_ntoa(iaDest),
                        icmpEcho.RTTime,
                        icmpEcho.ipInfo.Ttl);
                textcolor(LIGHTGRAY);
		if (icmpEcho.Status)
                {       nRet = GetLastError();
                      	textcolor(LIGHTRED);
			cprintf("\n\nFehler : icmpEcho.Status=%ld\n",
                        icmpEcho.Status);
                        textcolor(LIGHTGRAY);
						//break;
			DisplayErrorText(icmpEcho.Status);
                }
        }
        printf("\n");
        // Close the echo request file handle
        pIcmpCloseHandle(hndlFile);
        FreeLibrary(hndlIcmp);
        WSACleanup();
 
 return GetLastError();
}
//-----------------------------------------------------------------------------
int main(int argc, char **argv)
{
         int pversuche = 0;
         int ret = 0;
         int i = 0;
         char hosts[254];
        // Check arguments
        if (argc != 3)
        {
            fprintf(stderr,"\nSyntax: %s iprange Pingversuche\n",argv[0]);
            fprintf(stderr,"\nBeispiel: %s 10.10.14 1\n",argv[0]);
 
            return GetLastError();
        }
        else
        {  
            pversuche = abs(atoi(argv[2]));
            if (pversuche !=0)
            {                       
             for (i = 1; i < 255; i++)	// hosts durchhecheln
             {
              wsprintf(hosts,"%s.%d\n",argv[1],i);
              ret = icmpping(hosts,pversuche);
               if (ret != 0)
                 {
                  DisplayErrorText(ret);
                 }
               }
             }          
 
            else
            {
            return GetLastError();
            }
        }
}
//-----------------------------------------------------------------------------