Matteo Monti
Matteo Monti

Reputation: 8940

Getting TTL of incoming UDP packet in Python

I'm using the socket module in Python to do some basic UDP client-server communication. What I would need to do is quite simple: client sends server a packet, server answers with client's public ip address, port and a number representing the TTL the UDP packet had when it got to the server. This is my main problem: is there any way to recieve a packet with recvfrom() or so, and read the TTL value it had when it reached my server?

Thank you very much!

Matteo Monti

Upvotes: 1

Views: 3287

Answers (3)

Christian Q
Christian Q

Reputation: 21

Python 3.51 has the support for flags such as IP_RECVTTL or IP_RECVTOS. I gave it a try and it worked for me in a 3.x linux kernel.

Upvotes: 2

John Zwinck
John Zwinck

Reputation: 249153

I think you want to setsockopt(IP_RECVTTL) and then use recvmsg(). But Python doesn't seem to have recvmsg in its standard libraries (see http://bugs.python.org/issue6560). So probably you will need to write a small C or C++ shared library which is importable by Python and which does what you want. Or maybe try using the patches from page I linked.

Upvotes: 2

Jeremy Roman
Jeremy Roman

Reputation: 16355

This isn't generally exposed to userspace, as far as I know. I think you'll have to use something like libpcap to accomplish this, as described in this Stack Overflow answer:

https://stackoverflow.com/a/2362554/864393

Upvotes: 0

Related Questions