begin process at 2012 05 27 17:48:05
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Réseaux & Internet

 > PING SANS RAWSOCKET ET SANS MS-DOS (FONCTIONNE SOUS NT4 EN MODE UTILISATEUR)

PING SANS RAWSOCKET ET SANS MS-DOS (FONCTIONNE SOUS NT4 EN MODE UTILISATEUR)


 Information sur la source

Note :
10 / 10 - par 1 personne
10,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :Réseaux & Internet Niveau :Initié Date de création :13/11/2003 Vu :5 484

Auteur : cbestern

Ecrire un message privé
Site perso
Commentaire sur cette source (7)
Ajouter un commentaire et/ou une note

 Description

Voici le problème que j'avais, je doit réaliser un petit pingueur pour une société, la compilation marche parfaitement chez moi, et au moment de l'installé dans la société, impossible de l'executer, rien ne marche....

apres plusieurs recherche je me rend compte que cela viens du RAW-SOCKET qui ne peut pas être executer sous windows en mode simple utilisateur.

Apres quelques recherche je suis tombé sur cette excelente source.
qui n'utilise ni RAW-socket, ni commande MS-DOS

Source

  • #include <iostream.h>
  • #include <winsock.h>
  • #include <windowsx.h>
  • file://#include "icmpdefs.h"
  • typedef struct {
  • unsigned char Ttl; // Time To Live
  • unsigned char Tos; // Type Of Service
  • unsigned char Flags; // IP header flags
  • unsigned char OptionsSize; // Size in bytes of options data
  • unsigned char *OptionsData; // Pointer to options data
  • } IP_OPTION_INFORMATION, * PIP_OPTION_INFORMATION;
  • typedef struct {
  • DWORD Address; // Replying address
  • unsigned long Status; // Reply status
  • unsigned long RoundTripTime; // RTT in milliseconds
  • unsigned short DataSize; // Echo data size
  • unsigned short Reserved; // Reserved for system use
  • void *Data; // Pointer to the echo data
  • IP_OPTION_INFORMATION Options; // Reply options
  • } IP_ECHO_REPLY, * PIP_ECHO_REPLY;
  • int ping(char* NomHote)
  • {
  • WSAData wsaData;
  • if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
  • return 255;
  • }
  • // Load the ICMP.DLL
  • HINSTANCE hIcmp = LoadLibrary("ICMP.DLL");
  • if (hIcmp == 0) {
  • cerr << "Unable to locate ICMP.DLL!" << endl;
  • return 2;
  • }
  • // Look up an IP address for the given host name
  • struct hostent* phe;
  • if ((phe = gethostbyname(NomHote)) == 0) {
  • cerr << "Could not find IP address for " << NomHote << endl;
  • return 3;
  • }
  • // 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)) {
  • cerr << "Failed to get proc addr for function." << endl;
  • return 4;
  • }
  • // Open the ping service
  • HANDLE hIP = pIcmpCreateFile();
  • if (hIP == INVALID_HANDLE_VALUE) {
  • cerr << "Unable to open ping service." << endl;
  • return 5;
  • }
  • // 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) {
  • cerr << "Failed to allocate global ping packet buffer." << endl;
  • return 6;
  • }
  • 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) {
  • cout << "Addr: " <<
  • int(LOBYTE(LOWORD(pIpe->Address))) << "." <<
  • int(HIBYTE(LOWORD(pIpe->Address))) << "." <<
  • int(LOBYTE(HIWORD(pIpe->Address))) << "." <<
  • int(HIBYTE(HIWORD(pIpe->Address))) << ", " <<
  • "RTT: " << int(pIpe->RoundTripTime) << "ms, " <<
  • "TTL: " << int(pIpe->Options.Ttl) << endl;
  • }
  • else {
  • cerr << "Error obtaining info from ping packet." << endl;
  • }
  • // Shut down...
  • GlobalFree(pIpe);
  • FreeLibrary(hIcmp);
  • WSACleanup();
  • return 0;
  • }
  • int main(int argc, char* argv[])
  • {
  • // Check for correct command-line args
  • if (argc < 2) {
  • cerr << "usage: ping <host>" << endl;
  • return 1;
  • }
  • int retval = ping(argv[1]);
  • cout << "Code Retour:" << retval;
  • return retval;
  • }
  • file://icmpdefs.h
  • // Structures required to use functions in ICMP.DLL
#include <iostream.h>
#include <winsock.h>
#include <windowsx.h>
file://#include "icmpdefs.h"
 
typedef struct {
    unsigned char Ttl;                         // Time To Live
    unsigned char Tos;                         // Type Of Service
    unsigned char Flags;                       // IP header flags
    unsigned char OptionsSize;                 // Size in bytes of options data
    unsigned char *OptionsData;                // Pointer to options data
} IP_OPTION_INFORMATION, * PIP_OPTION_INFORMATION;
 
typedef struct {
    DWORD Address;                             // Replying address
    unsigned long  Status;                     // Reply status
    unsigned long  RoundTripTime;              // RTT in milliseconds
    unsigned short DataSize;                   // Echo data size
    unsigned short Reserved;                   // Reserved for system use
    void *Data;                                // Pointer to the echo data
    IP_OPTION_INFORMATION Options;             // Reply options
} IP_ECHO_REPLY, * PIP_ECHO_REPLY;
 

int ping(char* NomHote)
{
    WSAData wsaData;
    if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
        return 255;
    }
 
    // Load the ICMP.DLL
    HINSTANCE hIcmp = LoadLibrary("ICMP.DLL");
    if (hIcmp == 0) {
        cerr << "Unable to locate ICMP.DLL!" << endl;
        return 2;
    }
 
    // Look up an IP address for the given host name
    struct hostent* phe;
    if ((phe = gethostbyname(NomHote)) == 0) {
        cerr << "Could not find IP address for " << NomHote << endl;
        return 3;
    }
 
    // 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)) {
        cerr << "Failed to get proc addr for function." << endl;
        return 4;
    }
 
    // Open the ping service
    HANDLE hIP = pIcmpCreateFile();
    if (hIP == INVALID_HANDLE_VALUE) {
        cerr << "Unable to open ping service." << endl;
        return 5;
    }
   
    // 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) {
        cerr << "Failed to allocate global ping packet buffer." << endl;
        return 6;
    }
    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) {
        cout << "Addr: " <<
                int(LOBYTE(LOWORD(pIpe->Address))) << "." <<
                int(HIBYTE(LOWORD(pIpe->Address))) << "." <<
                int(LOBYTE(HIWORD(pIpe->Address))) << "." <<
                int(HIBYTE(HIWORD(pIpe->Address))) << ", " <<
                "RTT: " << int(pIpe->RoundTripTime) << "ms, " <<
                "TTL: " << int(pIpe->Options.Ttl) << endl;
    }
    else {
        cerr << "Error obtaining info from ping packet." << endl;
    }
 
    // Shut down...
    GlobalFree(pIpe);
    FreeLibrary(hIcmp);
    WSACleanup();
    return 0;
}
 
int main(int argc, char* argv[])
{
    // Check for correct command-line args
    if (argc < 2) {
        cerr << "usage: ping <host>" << endl;
        return 1;
    }
    int retval = ping(argv[1]);
    cout << "Code Retour:" << retval;
    return retval;
}
 

file://icmpdefs.h
// Structures required to use functions in ICMP.DLL



 Sources du même auteur

IMPRESSION D'UN EDITBOX EN PLUSIEURS PAGES
Source avec Zip TENTATIVE DE DAMES CHINOISES SOUS DOS EN C++ AVEC SOURIS
Source avec Zip PETIT UTILITAIRE DE DESSINS SOUS DOS EN C++ AVEC SOURIS

 Sources de la même categorie

Source avec Zip Source avec une capture MINI SERVEUR HTTP [WINDOWS] par ganjarasta
Source avec Zip Source avec une capture CLIENT DE TEST MODBUS TCP par brunovan
Source avec Zip Source avec une capture SCANIP [ARP / ICMP] par ganjarasta
Source avec Zip Source avec une capture TRACEROUTE [WINPCAP] par ganjarasta
Source avec Zip SERVEUR MULTITHREAD [LINUX/WIN] par nipepsinicolas

Commentaires et avis

Commentaire de aardman le 13/11/2003 17:14:49

Salut,
J'avais aussi posté une source du meme style sur ce site, en win32.

Commentaire de cbestern le 16/11/2003 00:53:54

oui j'ai bien lut ton code: UN PETIT "PINGER" (WIN32) mais je ne suis jamais arrivé à le faire fonctionner correctement, celui la marche tres simplement.

Commentaire de aardman le 16/11/2003 01:00:48

Salut,
Bizzare, car c'est les meme api que j'utilise.

Commentaire de cbestern le 17/11/2003 03:40:00

je ne sais pas, j'utilise pourant dev-c++, je n'ai rien modifier dans les bibluiothèques, bref, pour tous ceux qui veullent voir un autre: chercher: UN PETIT "PINGER" (WIN32) , c le même style

Commentaire de aardman le 17/11/2003 10:30:44

Salut,
Si tu n'a rien modifié c'est normal, c'est un projet VC++ 6.0,
Dev c++ comprend pas les pragma comments et autres.

Commentaire de 5h33ck0n le 20/04/2004 18:45:04

ca a lair cool comme truc, moi j essai de faire ca mais avec un shellexecut, le pb c'est que je suis en MFC et j'ai un problème de Updatedata, le resultat voulu ne s'affiche seulement après avoir cliquer deux fois sur le bouton !! et la je tombe sur cette source qui a l'air pas mal, mais je comprend pas un truc, le "icmpdefs.h" ! je ne lai pa du tout dans le pc ! (j'ai bien VC++ 6)

merci d avance !!

Commentaire de rootroot1985 le 28/11/2008 12:19:23 10/10

bon merci pour le code mais j'arrive pas a le faire tourner sur ma machine

le fichier icmpdefs.h n'existe pas.

tu peut me donner le code complet qui comporte ce fichier et merci une autre foi

 Ajouter un commentaire




Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Mai 2012
LMMJVSD
 123456
78910111213
14151617181920
21222324252627
28293031   

Consulter la suite du CalendriCode

A découvrir



 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,562 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales