Reading packets from a tun device.

Discussion in 'Python' started by Andre, Jul 12, 2003.

  1. Andre

    Andre Guest

    I'm trying to read packets from a tun device in Python, the code I used
    for this is the following:

    f = file( '/dev/tun0', 'r+' )
    pkt = f.read( 1500 )

    The problem is that the f.read() call doesn't return. When I used a small
    value in the f.read() call instead of the 1500, like 1 or 5, it did
    return the first bytes of the packet.
    I then went to the manuals and found that file() uses stdio's fopen, so I
    changed my code to make file() not do any buffering, like this:

    f = file( '/dev/tun0', 'r+', 0 )
    pkt = f.read( 1500 )

    But the behavior of the program is the same as the one above.

    What I'm looking for is a way to open a file in Python without using
    stdio, or a way to read from a tun device using it.
    I also tried google'ing for another Python program that uses a tun device,
    but couldn't find any yet, so if you know of a Python program that uses a
    tun device, that'd be enough for me to figure out what I'm doing wrong.

    Thank you for reading all this!
     
    Andre, Jul 12, 2003
    #1
    1. Advertising

  2. Andre

    Noah Guest

    Andre <> wrote in message news:<>...
    > I'm trying to read packets from a tun device in Python, the code I used
    > for this is the following:
    > f = file( '/dev/tun0', 'r+' )
    > pkt = f.read( 1500 )
    >
    > The problem is that the f.read() call doesn't return. When I used a small
    > value in the f.read() call instead of the 1500, like 1 or 5, it did
    > return the first bytes of the packet.
    > I then went to the manuals and found that file() uses stdio's fopen, so I
    > changed my code to make file() not do any buffering, like this:
    >
    > f = file( '/dev/tun0', 'r+', 0 )
    > pkt = f.read( 1500 )
    > But the behavior of the program is the same as the one above.
    > What I'm looking for is a way to open a file in Python without using
    > stdio, or a way to read from a tun device using it.


    I'm not sure if this problem is due to buffering on your client side, but
    you can open plain file descriptors in Python. The buffering could also
    be on the server side of the tun device. What is connected to the other
    side of the tun? Is that code that you wrote too? If so then send a copy
    of that and I can understand better what is going on.

    The 'os' package supports raw file descriptor file IO which is below stdio.

    import os
    fd_tun = os.open ('/dev/tun0', os.O_RDWR)
    data = os.read (fd_tun, 1500)
    os.close (fd_tun)

    Let me know how this goes.

    Yours,
    Noah
     
    Noah, Jul 13, 2003
    #2
    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. JROBINSON
    Replies:
    1
    Views:
    384
  2. Terrie
    Replies:
    5
    Views:
    7,807
    JScoobyCed
    Nov 22, 2004
  3. Praveen

    CONfiguring TOMCAT to tun PHP

    Praveen, Apr 13, 2005, in forum: Java
    Replies:
    0
    Views:
    4,194
    Praveen
    Apr 13, 2005
  4. antoine
    Replies:
    3
    Views:
    4,910
    antoine
    Dec 7, 2005
  5. Andrew
    Replies:
    0
    Views:
    419
    Andrew
    May 23, 2008
Loading...

Share This Page