Getting TTL of incoming UDP packet in Python

安稳与你 提交于 2019-12-24 17:15:20

问题


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


回答1:


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.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/9237006/getting-ttl-of-incoming-udp-packet-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!