I'm trying to write a socket server in Python that can receive ICMP packets.
Here's my code:
s = socket.socket(socket.AF_INET,socket.SOCK_RAW, socket.IPPROTO_ICMP)
host = socket.gethostbyname(socket.gethostname())
s.bind((host,0))
s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
However, I dont know how to receive the packets - I tried using s.listen but it failed. What am I missing or am I completly in the wrong direction?
Thanks!
Building on the accepted answer, this code unpacks the received ICMP header and displays its data (ICMP type, code, etc)
I've done this before in twisted and I set the socket up like this: