talelh
Messages postés17Date d'inscriptionlundi 25 avril 2011StatutMembreDernière intervention 7 mai 2011
-
5 mai 2011 à 18:02
Bonjour,
Bonjour,
j'ai un problème dans un code ping implémenter avec le c++, que je n'arrive pas à résoudre. s'il vous plait aider moi. Merci d'avance.
voilà le code:
ip->ihl=5; //header length
ip->version=4; //version
ip->tos=0; //type of service
ip->tot_len=htons(sizeof(struct iphdr)+sizeof(struct icmphdr)); //toatal length
ip->id=htons(random()); //Assign id as a random number
ip->ttl=255; //time to live;
ip->protocol=IPPROTO_ICMP;//ICMP header follow next to the ip header
ip->check=0; //Assign checksum as zero for now
ip->saddr= INADDR_ANY;
ip->daddr=inet_addr(d_addr);
//Creating a raw socket
if((raw=socket(AF_INET,SOCK_RAW,IPPROTO_ICMP))==-1)
{
perror("Something wrong with socket call :");
}
}
/*
* in_cksum --
* Checksum routine for Internet Protocol
* family headers (C Version)
*/
unsigned short in_cksum(unsigned short *addr, int len)
{
register int sum = 0;
u_short answer = 0;
register u_short *w = addr;
register int nleft = len;
/*
* Our algorithm is simple, using a 32 bit accumulator (sum), we add
* sequential 16 bit words to it, and at the end, fold back all the
* carry bits from the top 16 bits into the lower 16 bits.
*/
while (nleft > 1)
{
sum += *w++;
nleft -= 2;
}
/* mop up an odd byte, if necessary */
if (nleft == 1)
{
*(u_char *) (&answer) = *(u_char *) w;
sum += answer;
}
/* add back carry outs from top 16 bits to low 16 bits */
sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
sum += (sum >> 16); /* add carry */
answer = ~sum; /* truncate to 16 bits */
return (answer);
}