1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
| void Ping(CString Adresse, TabString* TabResult, int nbPing, int &NbLigne)
{
char *IP;
SOCKET sock;
WSADATA wsaData;
struct sockaddr_in sin, from;
int envoi, recept;
char icmp_data[20000]; // 20 Ko de données
char recvbuf[MAX_PACKET];
int fromlen = sizeof(from);
struct hostent *phe, *sn;
struct in_addr in;
char* hostname;
fd_set fdsr;
struct timeval tv_timeout;
int lg = Adresse.GetLength();
char *Pinging = new char[lg];
//IP de CString à char*
for (int i=0; i<lg; i++)
{
Pinging[i] = Adresse.GetAt(i);
}
Pinging[lg] = '\0';
// Initialisation et création du socket
WSAStartup(MAKEWORD(2,0),&wsaData);
sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if(sock==INVALID_SOCKET)
{
TabResult[NbLigne].StringPing.Format(_T("Erreur de creation du socket"));
NbLigne++;
return;
}
memset(&sin,0,sizeof(&sin));
phe = NULL;
hostname = Pinging;
phe = gethostbyname(hostname);
if(!phe)
{
TabResult[NbLigne].StringPing.Format(_T("Hôte inconnu, impossible de résoudre l'adresse %s.",hostname));
NbLigne++;
return;
}
memcpy(&(sin.sin_addr),phe->h_addr,phe->h_length);
IP = inet_ntoa(sin.sin_addr);
if((strcmp(IP, Pinging) != 0) || (Resolv == 1))
{
in.s_addr = inet_addr(Pinging);
sn=gethostbyaddr((char *)&in, 4, AF_INET);
memset(&hostname,0,sizeof(hostname));
if(Resolv == 1) hostname = sn->h_name;
else hostname=phe->h_name;
strcat(hostname, " [");
strcat(hostname, IP);
strcat(hostname, "]");
}
if(phe) sin.sin_family = phe->h_addrtype;
else sin.sin_family = AF_INET;
int taille2;
taille2 = sizeof(IpHeader) + sizeof(IcmpHeader);
TCHAR szMess[128];
wsprintf(szMess, _T("Envoi d'une requête sur %s avec %i octets de données :"), hostname, taille2);
MessageBox(NULL, szMess, _T("Erreur"), MB_OK);
// Initialisation du délai d'attente
if(tpslimite == 0) tpslimite = DELAI;
while(envoye != nbPing)
{
memset(icmp_data,'1',500); //remplissage avec 500 fois le caractère '1'
int lg(sizeof(icmp_data));
id = (unsigned short)GetCurrentProcessId() + envoye;
// Construction du paquet ICMP
int size=2000; //quantité de données que je souahite envoyer
IcmpHeader *icmp_hdr;
icmp_hdr = (IcmpHeader*)icmp_data; //creation de l'header de la trame icmp a partir de icmp_data
icmp_hdr->i_type = 8; // Type du paquet ICMP : echo request
icmp_hdr->i_code = 0; // Il n'y a pas de code spécifique pour ce type de paquet
icmp_hdr->i_id = id;
icmp_hdr->i_seq = 1;
icmp_hdr->timestamp = GetTickCount(); // Initialisation du temps lors de l'envoi
// Somme de contrôle sur 16 bits
((IcmpHeader*)icmp_data)->i_cksum = CNetwork::checksum((unsigned short*)icmp_data, size);
// On envoie le paquet ICMP que l'on a construit
CString retour;
envoi = sendto(sock, icmp_data, size, 0, (struct sockaddr *)&sin, size); // envoi = 2000 OK
envoi = WSAGetLastError(); // valeur = 0
FD_ZERO(&fdsr);
FD_SET(sock, &fdsr);
tv_timeout.tv_sec = tpslimite/1000;
tv_timeout.tv_usec = 0;
findelai = 0;
do
{
if (select(sock+1, &fdsr, NULL, NULL, &tv_timeout) <= 0) // bloque jusqu'au timeout (10 secondes)
{
// Dans le cas où le temps est écoulé, on veut sortir de la boucle
// on compte le paquet comme envoyé mais perdu
TabResult[NbLigne].StringPing.Format(_T("Délai d'attente de la demande depassé"));
perte++;
envoye++;
NbLigne++;
findelai = 1;
}
... |
Partager