Poperton
Poperton

Reputation: 2127

How does /dev/net/tun works? Do I get back what I write?

Suppose that I want to send and receive data through a tun device.

I found the answer https://stackoverflow.com/a/35735842/6655884 where it opens /dev/net/tun as if it were a file and reads from it. However, what if I want to send a packet to it? If I simply write to it, then I'm gonna get the packet back when I read from it, or I migth get lucky and read after the packet was sent.

How it works? Does /dev/net/tun consumes what I write and not give it back when I read? Does it have knowledge of what is sent and received? I imagine it works like a file so whatever I write I get back when I read.

Upvotes: 0

Views: 8185

Answers (1)

stsp
stsp

Reputation: 400

How it works? Does /dev/net/tun consumes what I write and not give it back when I read?

Indeed. You never get back what was written.

I imagine it works like a file so whatever I write I get back when I read.

No, it works like a device node. What is written, goes to a device (in this case - virtual device). What is read, is read from a (virtual) device. Think of /dev/tty: you don't read what was written there. Instead, you read what was typed on a keyboard, and what was written can be seen on a screen.

Upvotes: 1

Related Questions