creating RAW sockets

Discussion in 'Python' started by moijes12, Mar 17, 2011.

  1. moijes12

    moijes12 Guest

    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
     
    moijes12, Mar 17, 2011
    #1
    1. Advertising

  2. moijes12

    Chris Rebert Guest

    On Wed, Mar 16, 2011 at 10:36 PM, moijes12 <> 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
     
    Chris Rebert, Mar 17, 2011
    #2
    1. Advertising

  3. moijes12

    Nobody Guest

    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.
     
    Nobody, Mar 17, 2011
    #3
  4. moijes12

    moijes12 Guest

    On Mar 17, 11:14 am, Nobody <> 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
     
    moijes12, Mar 17, 2011
    #4
  5. moijes12

    moijes12 Guest

    On Mar 17, 11:28 am, moijes12 <> wrote:
    > On Mar 17, 11:14 am, Nobody <> 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
     
    moijes12, Mar 17, 2011
    #5
  6. moijes12

    Nobody Guest

    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.
     
    Nobody, Mar 18, 2011
    #6
  7. moijes12

    moijes12 Guest

    On Mar 18, 6:20 am, Nobody <> 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
     
    moijes12, Mar 18, 2011
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guillaume Kaddouch

    RAW Sockets on Windows XP

    Guillaume Kaddouch, Dec 26, 2003, in forum: C++
    Replies:
    1
    Views:
    5,386
    Jack Klein
    Dec 27, 2003
  2. Lars Strand

    IPv6 header on raw sockets?

    Lars Strand, Feb 19, 2004, in forum: Python
    Replies:
    0
    Views:
    802
    Lars Strand
    Feb 19, 2004
  3. Matt

    Raw Sockets vs. What?

    Matt, Dec 11, 2004, in forum: Python
    Replies:
    4
    Views:
    381
  4. ionel

    raw sockets please

    ionel, Apr 5, 2005, in forum: Python
    Replies:
    1
    Views:
    317
    Peter Hansen
    Apr 5, 2005
  5. billiejoex

    Sniffer with RAW SOCKETS

    billiejoex, Sep 7, 2005, in forum: Python
    Replies:
    1
    Views:
    425
    Grant Edwards
    Sep 7, 2005
Loading...

Share This Page