title: Why is my TTL value increasing by 2 each time? (C Socket programming)
body: <p>i'm trying to implement a Traceroute program but i've ran into two problems, one is that the TTL and RTT print out wrong; though they printed out right when implemented as a ping program. Finally my main issue is that when i'm incrementing the TTL it's going up by 2 instead of 1.</p>
  
  <p>I've only included the code i think is necessary, thanks.</p>
  
  <p>Thanks in advance :)</p>
  
  <pre><code>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;
     long int length;
     fd_set rdfds;
     int ready;
     int rtt;
     char buff [BUF_SIZE];
  
     /* Create and check Socket Number */
     skt = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
  
  
      int ttl = 0;
      setsockopt(skt, IPPROTO_IP, IP_TTL, &amp;ttl, sizeof(ttl)) &lt; 0;
  
  
      /* START SEND LOOP*/
      int i;
      for (i = 0; i &lt; 4; i++){
          ttl+=1;
          setsockopt(skt, IPPROTO_IP, IP_TTL, &amp;ttl, sizeof(ttl));
  
  
     /* Check Socket */
     if (skt &lt; 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 *)&amp;addr, sizeof(struct sockaddr_in))) {
        perror ("Can't bind socket");
        exit (1);
     }
  
      /* IP Buffer */
     ip = (struct iphdr *)buff;
     peer_addrlen = (socklen_t) sizeof (struct sockaddr_storage);
     memset (&amp;dstaddr, 0, sizeof(struct sockaddr_in));
     dstaddr.sin_addr.s_addr = inet_addr(HOSTADDR);
     dstaddr.sin_family = AF_INET;
  
     ip-&gt;ttl=(ttl++);
  
     /* ICMP Buffer */
     memset (buff, 0, sizeof(buff));
     icmp = (struct icmphdr *) buff;
     icmp-&gt;type   = ECHO_REQ;
     icmp-&gt;id     = htons(getpid( ) &amp; 0xffff);
     icmp-&gt;seqNum = htons(sequence++);
  
  
     /* Check Send Time */
     if (gettimeofday ((struct timeval *)icmp-&gt;data, NULL)) {
        perror ("Can't establish send time");
        exit (1);
     }
  
     /*Calculating packet size*/
     length = sizeof(struct icmphdr) + sizeof(struct timeval);
     icmp-&gt;checksum = ~(sum (0, buff, length));
  
  
  
     /* Packet too small, ERROR
     SEND Request             */
     if (sendto (skt, buff, length, 0,
           (struct sockaddr *) &amp;dstaddr, sizeof(struct sockaddr_in)) &lt;= 0) {
        perror ("sendto()");
        exit (1);
  }
  
     /* Define File Descriptor */
     timeout.tv_sec  = 2;
     timeout.tv_usec = 0;
     FD_ZERO(&amp;rdfds);
     FD_SET (skt, &amp;rdfds);
  
     /* Select Data from File Descriptor */
     ready = select (skt + 1, &amp;rdfds, NULL, NULL, &amp;timeout);
     if (ready &lt; 0) {
        perror ("Select()");
        exit (1);
     }
  
   /* Recieve Reply */
   memset (buff, 0, sizeof(buff));
     if (recvfrom (skt, buff, sizeof(buff), 0,
          (struct sockaddr *) &amp;peer_addr, &amp;peer_addrlen) &lt;= 0) exit (1);
  
  
  
     /* Check Time Stamp */
     if (gettimeofday (&amp;end, NULL)) {   // Timestamp reception
        perror ("Can't establish time of receipt");
        exit (1);
     }
  
  
     /* Check IP Protocol */
     if (ip-&gt;version != 4 ||
         sum (0, buff, sizeof(struct iphdr)) != 0xffff ||
         ip-&gt;protocol != ICMP)
        exit(1);
  
  
     /* Get IP Payload legth and ICMP Address*/
     length = ntohs(ip-&gt;length) - ip-&gt;hdrlen * 4;       // Length of IP payload
     icmp = (struct icmphdr *)((uint32_t *)ip + ip-&gt;hdrlen);   // Find ICMP hdr
  
  
     /* Check ICMP response type*/
     if (icmp-&gt;type == 11){
         printf("Type 11: ICMP....");
         }
  
    /* if (icmp-&gt;type != ECHO_REPL || sum (0, icmp, length) != 0xffff) {
        fprintf (stderr, "Received %s\n", messages[icmp-&gt;type]);
        //exit (1);
     } */
  
     /*   Find the difference between sent and end times in 10s of ms */
     sent = (struct timeval *)icmp-&gt;data;
     if ((rtt = (end.tv_usec - sent-&gt;tv_usec) / 100) &lt; 0)
        rtt += 10000;  // We've cycled to a new second
     rtt += (end.tv_sec - sent-&gt;tv_sec) * 10000;  // Add any seconds
  
     /* PRINT ICMP REPLY*/
     printf ("%ld bytes from %s: icmp_req=%d ttl=%d time=%0.1f ms\n",
        length,
        iptos(ntohl(ip-&gt;srcip)),
        ntohs(icmp-&gt;seqNum),
        /*Set initial TTL */
        ip-&gt;ttl,
        ((float)rtt) / 10);
  
  
      } /*END SEND LOOP
  
     /* Invalid Signal returned */
     if (signum == SIGINT) {
        printf ("\nGoodbye!\n");
        exit(0);
     }
  
     /* 3 Second Probe */
     alarm (3);
  }
  </code></pre>