1 %%%----------------------------------------------------------------------
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 %%%----------------------------------------------------------------------
9 -author('luke@bluetail.com').
11 -import(lists
, [foreach
/2]).
13 -export([start_link
/2, start_link
/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
) ->
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
)
40 {udp
, Socket
, _
, _
, Packet
} ->
41 io:format("Got ~p byte packet on UDP~n", [size(Packet
)]),
42 tuntap:write(Tunnel
, Packet
)
44 ?
MODULE:loop(Tunnel
, Socket
, EPs
).
47 Tun
= tuntap:open_tuntap(tap
, Dev
),
48 Name
= tuntap:device_name(Tun
),
49 io:format("Alive and kicking on ~p~n", [Name
]),