11

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.

Dev Null
5,1071 gold badge34 silver badges49 bronze badges
asked Nov 1, 2013 at 17:37
6
  • FYI I get the same error when running it as root. Commented Nov 1, 2013 at 17:39
  • Try AF_UNIX, instead of AF_INET. Commented Nov 1, 2013 at 17:58
  • @Anthony, It seems work. I got new error, though. Let me do some test and see whether it works. Thanks for advice~~~ Commented Nov 1, 2013 at 18:03
  • Are you trying to receive all IP packets? All packets? Or all packets of a specific IP protocol? Commented Nov 1, 2013 at 18:08
  • @Robφ,All Packets. We have conversation in another post. And this is the way for me to bypass the TCP part. If I can receive all the packet then I can acheive the goal: one socket send, one socket receive. Commented Nov 1, 2013 at 18:10

3 Answers 3

10

Try socket.AF_PACKET instead of socket.AF_INET.

answered Nov 1, 2013 at 17:45
Sign up to request clarification or add additional context in comments.

11 Comments

I tried, and I got "[ERROR] No such device". It confused me, actually.
@JerryMeng - What OS are you running on? On Ubuntu 12.04, with root privileges, socket.socket(socket.AF_PACKET, socket.SOCK_RAW) worked for me.
@Robφ then how do you bind your socket to a host? like s.bind((HOST, 5454))? I got "[ERROR] No such device". And by the way, If I use AF_PACKET, why I don't need to specify the proto argument?
@JerryMeng 1) You don't bind raw sockets to hosts. You are bypassing IP, so you don't get any of its features. 2) I didn't specify a protocol because you said "All Packets".
@JerryMeng - You can bind raw sockets to a specific device & protocol. So, you might try s.bind(("eth0", 0x0800)).
|
0

This runs without error as root:

#!/usr/local/cpython-3.3/bin/python
import socket as socket_mod
#s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
socket = socket_mod.socket(socket_mod.AF_PACKET, socket_mod.SOCK_RAW, socket_mod.IPPROTO_IP)
#socket.bind(('localhost', 5454))
socket.bind(('lo', 5454))
answered Nov 1, 2013 at 19:21

2 Comments

In fact, I have figured it out. And the second line is not right. 5454 won't give your error, neither any packet. 0x0800 is the correct port to use. Thanks for answering~~
Why two answers? Wouldn't it be clearer to merge this into your first answer?
-2

Try socket.AF_UNIX, it can solve your problem, good luck.

answered Nov 1, 2013 at 18:04

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.