# Python raw sockets on freebsd



## Anonymous (Feb 20, 2010)

socket.error (49, "can't assign requested address")


```
import socket

HOST = "127.0.0.1"

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

s.bind((HOST, 8080))

s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)

s.listen(1)

print s.recvfrom(65565)
```


----------



## Anonymous (Feb 20, 2010)

Does anyone know where I can find a list of the supported protocols and how I would pass them to "socket(int domain, int type, int protocol)" for the SOCK_RAW type. I've tried google and looked in http://www.freebsd.org/doc/en/books/developers-handbook/sockets-essential-functions.html and http://www.freebsd.org/cgi/man.cgi?query=socket&sektion=2 but can't find anything.


----------



## expl (Feb 21, 2010)

On BSD systems you can't read UDP/TCP packets with raw sockets like you can on linux. So I am not sure just how useful raw socket interface is for you as its mostly limited to intercepting ICMP packets. If thats what you want you should pass IPPROTO_ICMP as protocol type.


----------



## Anonymous (Feb 21, 2010)

What I'm trying to do is listen on port 53/UDP for DNS requests and then process the headers of the request. When I use a SOCK_DGRAM socket though the output of listnening on the socket just produces something like "xgooglecom" for a DNS query of google.com, no headers. How do programs like tcpdump and wirshark get the full packet headers?


----------



## expl (Feb 22, 2010)

mharvey87 said:
			
		

> ... How do programs like tcpdump and wirshark get the full packet headers?



tcpdump uses libpcap to handle raw streams.


----------



## Anonymous (Feb 23, 2010)

Thanks expl, that's what I was looking for.


----------



## expl (Feb 23, 2010)

libcap is also portable to many systems so your code will work everywhere.


----------



## ProFTP (Feb 25, 2010)

SOCK_RAW

http://bitbucket.org/hizel/py-ipfw/changeset/434dd0bc3cb7/


----------

