473,326 Members | 2,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Reading packets from a tun device.


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!
Jul 18 '05 #1
1 3440
Andre <an***@netvision.com.br> wrote in message news:<ma**********************************@python. org>...
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
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Eric Lilja | last post by:
Hello, I have a text file that contains a number of entries describing a recipe. Each entry consists of a number of strings. Here's an example file with only one entry (recipe): Name=Maple Quill...
1
by: Ldaled | last post by:
Okay, I had a previous post called reading an XML document. Since this post I have revised my code and got it to work. Now, Like Derek had mentioned in answer to my previous post, I am getting an...
0
by: rs | last post by:
Hi guys, I am trying to read from a USB keyboard using vb.net and HID classes. the USB keyboard is not my primary keyboard. I have a ps2 keyboard connected and is detected in device manager as...
2
by: rs | last post by:
Hi guys, I am trying to read from a USB keyboard using vb.net and HID classes. the USB keyboard is not my primary keyboard. I have a ps2 keyboard connected and is detected in device manager as...
9
by: Macca | last post by:
Hi, I have a synchronous socket server which my app uses to read data from clients. To test this I have a simulated client that sends 100 byte packets. I have set up the socket server so...
3
by: Pashlipops | last post by:
Hi there I am looking for some guidance. I am new to C++ and whilst I am reading up about the language, my actual goal is to create a program that reads a UDP packet sent by a data logger and...
7
by: sreehari | last post by:
Hi All, I have this problem, I have developed a Master - Slave kinda appliacation , were the slave is generally a device.when i broadcast a message from my application, the Device seems to...
1
Plater
by: Plater | last post by:
Useing a packet sniffer I have been watching some tcpip traffic between my computer and my embedded device I am developing on (the embeded device runs wince 5.0) and I have been noticing that a LOT...
0
by: makTSPL | last post by:
Dear All, I am developing an application in C#. This application communicates with Garmin Forerunner device. I have developed a demo for it, which shows Device software version. but I am not able to...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.