I'm currently having a lot of trouble while writing a raw socket. While my implementation is pretty big by now, i think solving the problem for the following code, would also solve it for my whole implementation:
from scapy.all import *
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
src_port = 12345 # any random port > 1024 i guess ...
dst_port = 80 # should be tcp, as http runs over it in the end
dst_addr = socket.gethostbyname("www.google.de")
# ensuring that kernel will not create its own ip header
sock.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, True)
# create a packet to send using scapy
packet = IP(dst = dst_addr)/TCP(dport = dst_port)
sock.sendto(bytes(packet), (dst_addr, 80))
I get the following error :
OS error: [Errno 22] Invalid argument
which is raised by:
sock.sendto(bytes(packet), (dst_addr, 80))
Does someone have a clue, why this is not working?
-- EDIT --
I'm working on MacOSX. I already noticed this question: Raw sockets and sendto in python However, I'm hoping that someone has an answer anyways, as the old question lacks a good answer and is also around 4 years old.