So I am attempting to retro fit an amazing network packet sniffer already established but for linux. The basic jist is the code below works for socket.IPPROTO_UDP but not socket.IPPROTO_TCP.
import socket
#create an INET, STREAMing socket
HOST = socket.gethostbyname(socket.gethostname())
# create a raw socket and bind it to the public interface
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
s.bind((HOST, 0))
# Include IP headers
s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
# receive all packages
s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
# receive a packet
packet = s.recvfrom(65565)
print packet
The error I get when running the above is : "line 12, in s.bind((HOST, 0)) File "", line 1, in bind socket.error: [Errno 10022] An invalid argument was supplied. If I fuss around with the above and strip it to bare min:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
# receive a packet
packet = s.recvfrom(65565)
print packet
I get similar error but a tad diff line 7, in packet = s.recvfrom(65565) socket.error: [Errno 10022] An invalid argument was supplied
The sniffer I am trying to retro fit, using same formatting etc is here: http://www.binarytides.com/python-packet-sniffer-code-linux/ -- crd to dev -- if anyone has further advise i'd appreciate it.