Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python raw socket: Protocol not supported

I am trying to open a raw socket with Python under linux.

My simple code:

s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
s.bind((HOST, 5454))

And I got this error:

[ERROR] Protocol not supported

By the way, I am using python 2.7.3 under linux 12.04, and I used root to run the code.

Does anyone have a clue?

Update: The solution given by dstromberg is correct. If you want the whole packet, then use his solution. However, there is another combination:

s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)

that also works.

In this case, you will receive a whole TCP packet with IP and TCP headers on it. If your use dstromberg's solution, you will also see the ethernet header. So it depends on how 'raw' you want your packet to be.

like image 358
Jerry Meng Avatar asked Nov 01 '13 17:11

Jerry Meng


People also ask

What is raw socket Python?

Raw socket is a layer 2 python library for communication using the MAC addresses only. This allows you to create a custom made Ethernet/WiFi communication system which is not using IP nor TCP/UDP or to debug custom frames such as SERCOS III, Profibus, ARP, PTP, ...

Does Libpcap use raw sockets?

If you need access to the raw link layer, raw sockets on most OSes don't support that (Linux and Irix being obvious exceptions, as per the previous paragraph), but libpcap does. On Windows, it uses a lower-level network driver to capture data. RAW sockets are restricted to admin users on modern versions of Windows.

How use raw socket in Linux?

Raw sockets allow new IPv4 protocols to be implemented in user space. A raw socket receives or sends the raw datagram not including link level headers. The IPv4 layer generates an IP header when sending a packet unless the IP_HDRINCL socket option is enabled on the socket.

What is meant by raw socket?

A raw socket is a type of socket that allows access to the underlying transport provider. This topic focuses only on raw sockets and the IPv4 and IPv6 protocols. This is because most other protocols with the exception of ATM do not support raw sockets.


1 Answers

Try socket.AF_PACKET instead of socket.AF_INET.

like image 190
dstromberg Avatar answered Oct 03 '22 06:10

dstromberg