Merge branch 'pu'
[jungerl.git] / lib / tuntap / src / eth2udp.erl
bloba1271eccc82aa64924982591a43130edaf93c0fd
1 %%%----------------------------------------------------------------------
2 %%% File : eth2udp.erl
3 %%% Author : Luke Gorrie <luke@bluetail.com>
4 %%% Purpose : Ethernet-over-UDP tunnels
5 %%% Created : 18 Feb 2003 by Luke Gorrie <luke@bluetail.com>
6 %%%----------------------------------------------------------------------
8 -module(eth2udp).
9 -author('luke@bluetail.com').
11 -import(lists, [foreach/2]).
13 -export([start_link/2, start_link/3]).
14 -export([init/3]).
15 -export([loop/3]).
17 %% start_link(Port, [EndPoint]) -> pid()
19 %% Port = integer() (UDP port for local endpoint)
20 %% EndPoint = {IP, Port} (UDP endpoint of remote tunnel)
21 start_link(Port, EPs) ->
22 start_link(Port, EPs, undefined).
23 start_link(Port, EPs, Dev) ->
24 spawn_link(?MODULE, init, [Port, EPs, Dev]).
26 init(Port, EPs, Dev) ->
27 {ok, Socket} = gen_udp:open(Port, [binary]),
28 {ok, Tunnel} = init_tunnel(Dev),
29 tuntap:set_active(Tunnel, true),
30 loop(Tunnel, Socket, EPs).
32 loop(Tunnel, Socket, EPs) ->
33 receive
34 {Tunnel, {data, Packet}} ->
35 io:format("Got ~p byte packet on tap~n", [size(Packet)]),
36 foreach(fun({IP, Port}) ->
37 gen_udp:send(Socket, IP, Port, Packet)
38 end,
39 EPs);
40 {udp, Socket, _, _, Packet} ->
41 io:format("Got ~p byte packet on UDP~n", [size(Packet)]),
42 tuntap:write(Tunnel, Packet)
43 end,
44 ?MODULE:loop(Tunnel, Socket, EPs).
46 init_tunnel(Dev) ->
47 Tun = tuntap:open_tuntap(tap, Dev),
48 Name = tuntap:device_name(Tun),
49 io:format("Alive and kicking on ~p~n", [Name]),
50 {ok, Tun}.