How to open a tunnel device created using 'ip tuntap'

I am trying to open a TUN device in Linux (I hope, eventually, in a Java application). Since I do not want to use any native code (I want to avoid JNI if possible), I want to do as much as possible using the command line. Here is what I am trying to do:

  • Create a TUN interface using ip tuntap add dev tun0 mode tun
  • Install it and pass it the IP address (simple enough with the ip command)
  • Open the file /dev/tun0 to record network traffic.

At the last stage, I got a little confused - I understand that this will work on Unix, because network adapters are files, but I am on Linux, and I don't think I can access network adapters this way. I understand that this is just using native code (make a few calls to ioctl and get a file descriptor), but if there is no way to do this from the command line, this will not work.

Is there a way to open the tun tuned interface (configured using ip tuntap ) when calling open and start writing IP packets on the network side (without using ioctl )?

+4
source share
1 answer

If you configured it using ip tuntap , you can simply open it as a form to read / write a java file, and then write all ethernet packets to it. You can open it twice (FileInputStream / FileOutputStream) to read and write Ethernet packets.

+1
source

All Articles