Skip to content

Commit

Permalink
tuncer: persist interface on shutdown
Browse files Browse the repository at this point in the history
If the caller has set the tuntap interface to persistent, do not destroy
or unconfigure the interface on shutdown as suggested by @alexshavelev
in #5
  • Loading branch information
msantos committed Aug 21, 2017
1 parent 4141890 commit 8e8eb63
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/tuncer.erl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%% Copyright (c) 2011-2016, Michael Santos <[email protected]>
%% Copyright (c) 2011-2017, Michael Santos <[email protected]>
%% All rights reserved.
%%
%% Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -64,7 +64,8 @@
pid, % PID of controlling process
fd, % TUN/TAP file descriptor
dev, % device name
flag % TUNSETIFF ifr flags
flag, % TUNSETIFF ifr flags
persist
}).

-define(IFNAMSIZ, 16).
Expand Down Expand Up @@ -254,7 +255,7 @@ handle_call({recv, _Len}, _From, State) ->

handle_call({persist, Status}, _From, #state{fd = FD} = State) ->
Reply = tunctl:persist(FD, Status),
{reply, Reply, State};
{reply, Reply, State#state{persist = Status}};

handle_call({owner, Owner}, _From, #state{fd = FD} = State) ->
Reply = tunctl:owner(FD, Owner),
Expand Down Expand Up @@ -290,10 +291,15 @@ handle_info({'EXIT',_,normal}, State) ->
handle_info({'EXIT', _, _}, #state{port = false} = State) ->
{noreply, State};

handle_info({'EXIT', Port, Error}, #state{port = Port, pid = Pid, fd = FD, dev = Dev} = State) ->
handle_info({'EXIT', Port, Error}, #state{port = Port, pid = Pid, fd = FD, dev = Dev, persist = Persist} = State) ->
Pid ! {tuntap_error, self(), Error},
_ = tunctl:down(Dev),
tunctl:persist(FD, false),
case Persist of
true ->
ok;
false ->
_ = tunctl:down(Dev),
tunctl:persist(FD, false)
end,
procket:close(FD),
{stop, normal, State};

Expand Down

0 comments on commit 8e8eb63

Please sign in to comment.