creating RAW sockets

moijes12 moijes12 at gmail.com
Thu Mar 17 02:50:03 EDT 2011


On Mar 17, 11:28 am, moijes12 <moije... at gmail.com> wrote:
> On Mar 17, 11:14 am, Nobody <nob... at nowhere.com> wrote:
>
> > On Wed, 16 Mar 2011 22:36:07 -0700, moijes12 wrote:
> > > 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.
>
> @ 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



More information about the Python-list mailing list