Home > Coding > How to calculate Hop Count with the TTL of received packets

How to calculate Hop Count with the TTL of received packets

July 15th, 2009

Let’s assume you scanned a host, and receives a response packet. You can find the TTL (Time to Live) within this packet. Assume it’s 51, what’s the distance (number of hops) between your local machine and the machine you just scanned?

I haven’t found official documents about this issue, but I found a segment of code that performs the calculation. It’s not 100% correct, but works most of the times.

  1. int calHop(int ttl) {
  2.         int hop = -1;
  3.  
  4.         if (ttl < 32) {
  5.                 hop = 32 – ttl;
  6.         } else if (ttl == 32) {
  7.                 hop = 0;
  8.         } else if (ttl < 64) {
  9.                 hop = 64 – ttl;
  10.         } else if (ttl == 64) {
  11.                 hop = 0;
  12.         } else if (ttl < 128) {
  13.                 hop = 128 – ttl;
  14.         } else if (ttl == 128) {
  15.                 hop = 0;
  16.         } else if (ttl < 256) {
  17.                 hop = 255 – ttl;
  18.         }
  19.  
  20.         return hop;
  21. }

Coding

  1. July 16th, 2009 at 14:00 | #1

    Because the original TTL set in packet is different according systems, isn’t it?

  2. July 16th, 2009 at 14:15 | #2

    You are right, Mr Ga.

  1. No trackbacks yet.