Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > creating RAW sockets

Reply
Thread Tools

creating RAW sockets

 
 
moijes12
Guest
Posts: n/a
 
      03-17-2011
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
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      03-17-2011
On Wed, Mar 16, 2011 at 10:36 PM, moijes12 <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
 
 
 
Nobody
Guest
Posts: n/a
 
      03-17-2011
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.

 
Reply With Quote
 
moijes12
Guest
Posts: n/a
 
      03-17-2011
On Mar 17, 11:14*am, Nobody <(E-Mail Removed)> 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
 
Reply With Quote
 
moijes12
Guest
Posts: n/a
 
      03-17-2011
On Mar 17, 11:28*am, moijes12 <(E-Mail Removed)> wrote:
> On Mar 17, 11:14*am, Nobody <(E-Mail Removed)> 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
 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      03-18-2011
On Wed, 16 Mar 2011 23:50:03 -0700, moijes12 wrote:

> 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.

 
Reply With Quote
 
moijes12
Guest
Posts: n/a
 
      03-18-2011
On Mar 18, 6:20*am, Nobody <(E-Mail Removed)> wrote:
> On Wed, 16 Mar 2011 23:50:03 -0700, moijes12 wrote:
> > 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.


thanks guys
Moijes
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
raw sockets please ionel Python 1 04-05-2005 01:13 PM
raw sockets ? i need a python sniffer ionel Python 1 01-10-2005 08:36 AM
Raw Sockets vs. What? Matt Python 4 01-08-2005 07:19 AM
IPv6 header on raw sockets? Lars Strand Python 0 02-19-2004 01:59 PM
RAW Sockets on Windows XP Guillaume Kaddouch C++ 1 12-27-2003 06:04 AM



Advertisments