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.
- int calHop(int ttl) {
- int hop = -1;
- if (ttl < 32) {
- hop = 32 – ttl;
- } else if (ttl == 32) {
- hop = 0;
- } else if (ttl < 64) {
- hop = 64 – ttl;
- } else if (ttl == 64) {
- hop = 0;
- } else if (ttl < 128) {
- hop = 128 – ttl;
- } else if (ttl == 128) {
- hop = 0;
- } else if (ttl < 256) {
- hop = 255 – ttl;
- }
- return hop;
- }
Because the original TTL set in packet is different according systems, isn’t it?
You are right, Mr Ga.