Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using tun/tap device the erlang way #9

Open
benoitc opened this issue Feb 8, 2020 · 2 comments
Open

using tun/tap device the erlang way #9

benoitc opened this issue Feb 8, 2020 · 2 comments

Comments

@benoitc
Copy link

benoitc commented Feb 8, 2020

I am interresting to setup such tunnel, but how would you open such device from erlang and read it? For example if you want to accept connections for an http server over it. Any hint is welcome :)

@colrack
Copy link

colrack commented Feb 8, 2020

Hi Benoit
You can open tun/tap devices in active or passive mode with tuncer:create.
There is a C port that opens the tun/tap sockets since the vm is not capable of opening them.
The port opens the socket and then passes it to the vm (this is done in procket).
When you open such a socket you are dealing with raw IP packets or Ethernet frames; in the former case you opened a tun device, in the latter a tap device.
Implementing an http server on top of a tun device is not trivial since you need userspace TCP stack;
implementing it on top of layer 2 is even harder!
The main use case for tun/tap devices is to leverage the kernel net stack from userspace.
Usually you set up a "tunnel" on top of transport protocols; eg, you open a UDP or TCP socket and you send IP or Ethernet packets over it (the packets you read from the tun socket); on the other side, you receive packets via UDP/TCP and you write them to the tun/tap device and let the kernel deal with them.
Hope this helps

@msantos
Copy link
Owner

msantos commented Feb 9, 2020

Hey @benoitc ! @colrack's answer is excellent and pretty much covers
everything.

Basically the erlang process creates and attaches to a virtual interface
and receives either ethernet (tap interface) or ip (tun interface)
frames. Imagine we have 2 computers on different networks, assigned
10.10.10.10 and 10.10.20.10:

host1: 192.168.100.1/24 tun0 -- erlang -- 10.10.10.10/eth0 ---- host2: 10.10.20.10/eth0 -- erlang tun0 192.168.100.2/24                                           

Any packets on host1 destined for 192.168.100.2 would be routed via
tun0. The erlang process would receive the frame and could modify, drop
or forward it. How the erlang process encodes and forwards the frame is
arbitrary: for example, it could base64 encode the frame and send it as an
HTTP body to 10.10.20.10 or send it over the erlang distribution protocol.

The erlang process can also handle the frames directly. In that case,
as @colrack mentioned, the process could implement a UDP/IP or TCP/IP stack.

If anything isn't clear, feel free to ask questions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants