Skip to content

Instantly share code, notes, and snippets.

@msantos
Created January 7, 2015 15:18
Show Gist options
  • Save msantos/f2823fcba40975003dc3 to your computer and use it in GitHub Desktop.
Save msantos/f2823fcba40975003dc3 to your computer and use it in GitHub Desktop.
Tunnel over erlang distribution, randomly drops/delays frames
-module(wastrel).
-export([start/5]).
start(Node, SrcIP, DstIP, Drop, Delay) ->
random:seed(now()),
Pid = peer(Node, addr(SrcIP), addr(DstIP), Drop, Delay),
{ok, Dev} = tuncer:create("wastrel", [tap, no_pi, {active, true}]),
ok = tuncer:up(Dev, SrcIP),
proxy(Dev, Pid, Drop, Delay).
peer(N, SrcIP, DstIP, Drop, Delay) when is_atom(N) ->
pong = net_adm:ping(N),
Self = self(),
spawn_link(N, wastrel, start, [Self, DstIP, SrcIP, Drop, Delay]);
peer(N, _, _, _, _) when is_pid(N) ->
N.
proxy(Dev, Pid, Drop, Delay) ->
receive
{tuntap, _Pid, Data} ->
case random:uniform() < Drop of
true ->
error_logger:info_report([{drop, Data}]),
ok;
false ->
Pid ! {wastrel, Data}
end,
proxy(Dev, Pid, Drop, Delay);
{wastrel, Data} ->
case random:uniform() < Delay of
true ->
Sleep = random:uniform(1000),
error_logger:info_report([{delay, Sleep}]),
timer:sleep(Sleep);
false ->
ok
end,
ok = tuncer:send(Dev, Data),
proxy(Dev, Pid, Drop, Delay);
Error ->
Error
end.
addr(Addr) when is_tuple(Addr) ->
Addr;
addr(Str) when is_list(Str) ->
{ok, Addr} = inet_parse:address(Str),
Addr.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment