Saturday, 15 May 2010

Why is my ICMP sequence number not incrementing? (C Socket Programming) -



Why is my ICMP sequence number not incrementing? (C Socket Programming) -

is possible can help me workout why icmp sequence number isn't incrementing on every request, when used ping programme increment sequence number on each ping. have thought why round trip times displaying negative numbers? worked fine when ping program.

please assume code 'works', have removed easier reading.

void respond (int signum) { struct sockaddr_storage peer_addr; socklen_t peer_addrlen; struct sockaddr_in addr; struct sockaddr_in dstaddr; struct iphdr * ip; struct icmphdr * icmp; struct timeval * sent; int skt; int sequence = 0; long int length; fd_set rdfds; int ready; int rtt; char buff [buf_size]; /* create , check socket number */ skt = socket (af_inet, sock_raw, ipproto_icmp); int ttl = 0; setsockopt(skt, ipproto_ip, ip_ttl, &ttl, sizeof(ttl)) < 0; /* check socket */ if (skt < 0) { perror ("socket()"); exit (1); } /* set ip addresses */ addr.sin_family = af_inet; addr.sin_port = 0; addr.sin_addr.s_addr = inaddr_any; /* check socket bind */ if (bind (skt, (struct sockaddr *)&addr, sizeof(struct sockaddr_in))) { perror ("can't bind socket"); exit (1); } /* start send loop*/ int i; (i = 0; < 7; i++){ ttl+=1; setsockopt(skt, ipproto_ip, ip_ttl, &ttl, sizeof(ttl)); /* ip buffer */ ip = (struct iphdr *)buff; peer_addrlen = (socklen_t) sizeof (struct sockaddr_storage); memset (&dstaddr, 0, sizeof(struct sockaddr_in)); dstaddr.sin_addr.s_addr = inet_addr(hostaddr); dstaddr.sin_family = af_inet; /* icmp buffer */ memset (buff, 0, sizeof(buff)); icmp = (struct icmphdr *) buff; icmp->type = echo_req; icmp->id = htons(getpid( ) & 0xffff); icmp->seqnum = htons(sequence++); /* check send time */ if (gettimeofday ((struct timeval *)icmp->data, null)) { perror ("can't found send time"); exit (1); } /*calculating packet size*/ length = sizeof(struct icmphdr) + sizeof(struct timeval); icmp->checksum = ~(sum (0, buff, length)); /* packet small, error send request */ if (sendto (skt, buff, length, 0, (struct sockaddr *) &dstaddr, sizeof(struct sockaddr_in)) <= 0) { perror ("sendto()"); exit (1); } /* define file descriptor */ timeout.tv_sec = 1; timeout.tv_usec = 1; fd_zero(&rdfds); fd_set (skt, &rdfds); /* select info file descriptor */ ready = select (skt + 1, &rdfds, null, null, &timeout); if (ready < 0) { perror ("select()"); exit (1); } /* recieve reply */ memset (buff, 0, sizeof(buff)); if (recvfrom (skt, buff, sizeof(buff), 0, (struct sockaddr *) &peer_addr, &peer_addrlen) <= 0) exit (1); /* check time stamp */ if (gettimeofday (&end, null)) { // timestamp reception perror ("can't found time of receipt"); exit (1); } /* check ip protocol */ if (ip->version != 4 || sum (0, buff, sizeof(struct iphdr)) != 0xffff || ip->protocol != icmp) exit(1); /* ip payload legth , icmp address*/ length = ntohs(ip->length) - ip->hdrlen * 4; // length of ip payload icmp = (struct icmphdr *)((uint32_t *)ip + ip->hdrlen); // find icmp hdr /* check icmp response type*/ if (icmp->type == 11){ printf(""); } /* if (icmp->type != echo_repl || sum (0, icmp, length) != 0xffff) { fprintf (stderr, "received %s\n", messages[icmp->type]); //exit (1); } */ /* find difference between sent , end times in 10s of ms */ sent = (struct timeval *)icmp->data; if ((rtt = (end.tv_usec - sent->tv_usec) / 10) < 0) rtt += 10000; // we've cycled new sec rtt += (end.tv_sec - sent->tv_sec) * 10000; // add together seconds /* print icmp reply*/ printf ("%ld bytes %s: icmp_req=%d ttl=%d time= %0.1f ms\n", length, iptos(ntohl(ip->srcip)), ntohs(icmp->seqnum), ip->ttl, ((float)rtt) / 10); } /*end send loop /* 3 sec probe */ alarm (5); }

you're never setting sequence 0 in code, declaring it's int.

c sockets ping icmp traceroute

No comments:

Post a Comment