Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9260161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:49:28+00:00 2026-06-18T12:49:28+00:00

i’m trying to implement a Traceroute program but i’ve ran into two problems, one

  • 0

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.

I’ve only included the code i think is necessary, thanks.

Thanks in advance 🙂

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, &ttl, sizeof(ttl)) < 0;


    /* START SEND LOOP*/
    int i;
    for (i = 0; i < 4; i++){
        ttl+=1;
        setsockopt(skt, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl));


   /* 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);
   }

    /* 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;

   ip->ttl=(ttl++);

   /* 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 establish send time");
      exit (1);
   }

   /*Calculating packet size*/
   length = sizeof(struct icmphdr) + sizeof(struct timeval);
   icmp->checksum = ~(sum (0, buff, length));



   /* Packet too 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  = 2;
   timeout.tv_usec = 0;
   FD_ZERO(&rdfds);
   FD_SET (skt, &rdfds);

   /* Select Data from 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 establish time of receipt");
      exit (1);
   }


   /* Check IP Protocol */
   if (ip->version != 4 ||
       sum (0, buff, sizeof(struct iphdr)) != 0xffff ||
       ip->protocol != ICMP)
      exit(1);


   /* Get IP Payload legth and 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("Type 11: ICMP....");
       }

  /* if (icmp->type != ECHO_REPL || sum (0, icmp, length) != 0xffff) {
      fprintf (stderr, "Received %s\n", messages[icmp->type]);
      //exit (1);
   } */

   /*   Find the difference between sent and end times in 10s of ms */
   sent = (struct timeval *)icmp->data;
   if ((rtt = (end.tv_usec - sent->tv_usec) / 100) < 0)
      rtt += 10000;  // We've cycled to a new second
   rtt += (end.tv_sec - sent->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->srcip)),
      ntohs(icmp->seqNum),
      /*Set initial TTL */
      ip->ttl,
      ((float)rtt) / 10);


    } /*END SEND LOOP

   /* Invalid Signal returned */
   if (signum == SIGINT) {
      printf ("\nGoodbye!\n");
      exit(0);
   }

   /* 3 Second Probe */
   alarm (3);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:49:29+00:00Added an answer on June 18, 2026 at 12:49 pm
    /* START SEND LOOP*/
    int i;
    for (i = 0; i < 4; i++){
        ttl+=1;
        setsockopt(skt, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl));
    

    Here you increase ttl by one each time the loop iterates.

     ip->ttl=(ttl++);
    

    And here you increase it by one again. So if it was 1, it will be 2 after this line. However, once the loop iterates, you increase it again, so it will now be 3. That’s why your ttl goes up by two on each iteration.

    The line ip->ttl=(ttl++) means: Take the value of ttl and copy it to ip->ttl, then after doing so, increase ttl by one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.