creating RAW sockets

M

moijes12

Hi

I am unable to create RAW sockets using python.

Traceback (most recent call last):
File "getsockopt_handler.py", line 6, in ?
send = socket(AF_INET,SOCK_RAW,IPPROTO_IP)
socket.error: (94, 'Socket type not supported')

I've tried changing the type from IPPROTO_IP to IPPROTO_RAW but the I
get the below error

Traceback (most recent call last):
File "getsockopt_handler.py", line 7, in ?
send.bind((gethostbyname(gethostname()),50000))
socket.error: (99, 'Cannot assign requested address')

I am running this on python 2.2 .Do I need to change any system
settings or do I need to use a newer python version.Also,please
suggest further reading.

thanks
moijes12
 
C

Chris Rebert

Hi

I am unable to create RAW sockets using python.

Traceback (most recent call last):
 File "getsockopt_handler.py", line 6, in ?
   send = socket(AF_INET,SOCK_RAW,IPPROTO_IP)
socket.error: (94, 'Socket type not supported')

I've tried changing the type from IPPROTO_IP to IPPROTO_RAW but the I
get the below error

Traceback (most recent call last):
 File "getsockopt_handler.py", line 7, in ?
   send.bind((gethostbyname(gethostname()),50000))
socket.error: (99, 'Cannot assign requested address')

I am running this on python 2.2 .Do I need to change any system
settings or do I need to use a newer python version.

What operating system are you using?

Cheers,
Chris
 
N

Nobody

Traceback (most recent call last):
File "getsockopt_handler.py", line 7, in ?
send.bind((gethostbyname(gethostname()),50000))
socket.error: (99, 'Cannot assign requested address')

Specifying a port number isn't meaningful for a raw socket. At the kernel
level, the sin_port field in a sockaddr_in structure is used for the IP
protocol (e.g. 6 for TCP), if it's used at all.
 
M

moijes12

Specifying a port number isn't meaningful for a raw socket. At the kernel
level, the sin_port field in a sockaddr_in structure is used for the IP
protocol (e.g. 6 for TCP), if it's used at all.

@ Chris :
I am using windows xp sp2.

@ Nobody :
My main aim here is decode the IP header.

I tried the below code and it seems to work.I have shifted to python
2.5 (though I think it was more of a code problem).My main problem is
to decode the IP header

s = socket(AF_INET,SOCK_RAW,IPPROTO_IP)
r = socket(AF_INET,SOCK_RAW,IPPROTO_IP)
r.bind(('',0))
s.connect(('localhost',0))

for i in range(10) :
s.send(str(i))
data = r.recvfrom(256)
hdr = r.getsockopt(SOL_IP, IP_OPTIONS, 20)
print binascii.hexlify(hdr)

r.close()
s.close()

Here it prints nothing.I'll try some more stuff and will post my
findings in about 30 minutes.

thanks
moijes
 
M

moijes12

@ Chris :
I am using windows xp sp2.

@ Nobody :
My main aim here is decode the IP header.

I tried the below code and it seems to work.I have shifted to python
2.5 (though I think it was more of a code problem).My main problem is
to decode the IP header

s = socket(AF_INET,SOCK_RAW,IPPROTO_IP)
r = socket(AF_INET,SOCK_RAW,IPPROTO_IP)
r.bind(('',0))
s.connect(('localhost',0))

for i in range(10) :
    s.send(str(i))
    data = r.recvfrom(256)
    hdr = r.getsockopt(SOL_IP, IP_OPTIONS, 20)
    print binascii.hexlify(hdr)

r.close()
s.close()

Here it prints nothing.I'll try some more stuff and will post my
findings in about 30 minutes.

thanks
moijes

Hi

I tried the below code on python 3.0.1.This was an available example
in the manual and it was successfull in printing all packets

import socket

# the public network interface
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_IP)
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 package
while 1 :
print(s.recvfrom(65565))

Now,please can someone guide(as in what should I read and NOT as in
give me the code) me in decoding the IP header of packets using python
3.0.1.

thanks
moijes
 
N

Nobody

Now,please can someone guide(as in what should I read and NOT as in
give me the code) me in decoding the IP header of packets using python
3.0.1.

The "struct" module is the usual approach for decoding binary data
structures. Fields which aren't whole bytes/words will need to
be extracted manually using >> and &.

In case you don't already have it, the layout of an IPv4 header can be
found at:

http://en.wikipedia.org/wiki/IPv4#Header

Remember that TCP/IP uses big-endian format.
 
M

moijes12

The "struct" module is the usual approach for decoding binary data
structures. Fields which aren't whole bytes/words will need to
be extracted manually using >> and &.

In case you don't already have it, the layout of an IPv4 header can be
found at:

       http://en.wikipedia.org/wiki/IPv4#Header

Remember that TCP/IP uses big-endian format.

thanks guys
Moijes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top