fix network
This commit is contained in:
parent
5c3ed391ff
commit
bb5e0ddc17
@ -31,6 +31,6 @@ maybe_domain(QName) when is_binary(QName) ->
|
|||||||
false
|
false
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec insert(Domain :: binary()) -> no_return().
|
-spec insert(Domain :: binary()) -> true.
|
||||||
insert(Domain) when is_binary(Domain) ->
|
insert(Domain) when is_binary(Domain) ->
|
||||||
true = ets:insert(?TABLE, {Domain}).
|
true = ets:insert(?TABLE, {Domain}).
|
||||||
@ -27,7 +27,7 @@ lookup(FullHostname) when is_binary(FullHostname) ->
|
|||||||
error
|
error
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec insert(any(), Domain :: binary(), Ip :: integer()) -> no_return().
|
-spec insert(any(), Domain :: binary(), Ip :: integer()) -> true | ok.
|
||||||
insert(HostName, Domain, Ip) when is_binary(HostName), is_binary(Domain), is_integer(Ip), HostName /= <<>> ->
|
insert(HostName, Domain, Ip) when is_binary(HostName), is_binary(Domain), is_integer(Ip), HostName /= <<>> ->
|
||||||
FullHostname = <<HostName/binary, ".", Domain/binary>>,
|
FullHostname = <<HostName/binary, ".", Domain/binary>>,
|
||||||
LowerFullHostname = string:lowercase(FullHostname),
|
LowerFullHostname = string:lowercase(FullHostname),
|
||||||
|
|||||||
@ -10,6 +10,7 @@
|
|||||||
-author("anlicheng").
|
-author("anlicheng").
|
||||||
-include("sdlan.hrl").
|
-include("sdlan.hrl").
|
||||||
-include("sdlan_pb.hrl").
|
-include("sdlan_pb.hrl").
|
||||||
|
-include_lib("stdlib/include/ms_transform.hrl").
|
||||||
|
|
||||||
-behaviour(gen_server).
|
-behaviour(gen_server).
|
||||||
|
|
||||||
@ -94,7 +95,7 @@ get_network_id(Pid) when is_pid(Pid) ->
|
|||||||
attach(Pid, ChannelPid, ClientId, Mac, Ip, Hostname) when is_pid(Pid), is_pid(ChannelPid), is_binary(ClientId), is_binary(Mac), is_integer(Ip), is_binary(Hostname) ->
|
attach(Pid, ChannelPid, ClientId, Mac, Ip, Hostname) when is_pid(Pid), is_pid(ChannelPid), is_binary(ClientId), is_binary(Mac), is_integer(Ip), is_binary(Hostname) ->
|
||||||
gen_server:call(Pid, {attach, ChannelPid, ClientId, Mac, Ip, Hostname}).
|
gen_server:call(Pid, {attach, ChannelPid, ClientId, Mac, Ip, Hostname}).
|
||||||
|
|
||||||
-spec unregister(Pid :: pid(), ClientId :: binary(), Mac :: binary()) -> no_return().
|
-spec unregister(Pid :: pid(), ClientId :: binary(), Mac :: binary()) -> ok.
|
||||||
unregister(Pid, ClientId, Mac) when is_pid(Pid), is_binary(ClientId), is_binary(Mac) ->
|
unregister(Pid, ClientId, Mac) when is_pid(Pid), is_binary(ClientId), is_binary(Mac) ->
|
||||||
gen_server:cast(Pid, {unregister, ClientId, Mac}).
|
gen_server:cast(Pid, {unregister, ClientId, Mac}).
|
||||||
|
|
||||||
@ -144,7 +145,7 @@ forward_by_ets(NetworkId, Sock, SrcMac, DstMac, Packet) when is_integer(NetworkI
|
|||||||
end.
|
end.
|
||||||
|
|
||||||
%% 更新ip地址对应的nat关系
|
%% 更新ip地址对应的nat关系
|
||||||
-spec update_hole(Pid :: pid(), SessionToken :: binary(), ClientId :: binary(), Mac :: binary(), Peer :: tuple(), NatType :: integer(), V6Info :: undefined | #'SDLV6Info'{}) -> no_return().
|
-spec update_hole(Pid :: pid(), SessionToken :: binary(), ClientId :: binary(), Mac :: binary(), Peer :: tuple(), NatType :: integer(), V6Info :: undefined | #'SDLV6Info'{}) -> ok.
|
||||||
update_hole(Pid, SessionToken, ClientId, Mac, Peer, NatType, V6Info) when is_pid(Pid), is_binary(ClientId), is_binary(Mac), is_integer(NatType) ->
|
update_hole(Pid, SessionToken, ClientId, Mac, Peer, NatType, V6Info) when is_pid(Pid), is_binary(ClientId), is_binary(Mac), is_integer(NatType) ->
|
||||||
gen_server:cast(Pid, {update_hole, SessionToken, ClientId, Mac, Peer, NatType, V6Info}).
|
gen_server:cast(Pid, {update_hole, SessionToken, ClientId, Mac, Peer, NatType, V6Info}).
|
||||||
|
|
||||||
@ -236,7 +237,10 @@ handle_call({attach, ChannelPid, ClientId, Mac, Ip, Hostname}, _From,
|
|||||||
|
|
||||||
%% client设置为禁止状态,不允许重连
|
%% client设置为禁止状态,不允许重连
|
||||||
handle_call({disable_client, ClientId}, _From, State = #state{endpoint_table = EndpointTable}) ->
|
handle_call({disable_client, ClientId}, _From, State = #state{endpoint_table = EndpointTable}) ->
|
||||||
case match_endpoint(EndpointTable, {'$1', #endpoint{client_id = ClientId, _ = '_'}}) of
|
MatchSpec = ets:fun2ms(fun(Object = {_Mac, #endpoint{client_id = ClientId0}}) when ClientId0 =:= ClientId ->
|
||||||
|
Object
|
||||||
|
end),
|
||||||
|
case select_endpoint(EndpointTable, MatchSpec) of
|
||||||
{ok, Mac, Endpoint} ->
|
{ok, Mac, Endpoint} ->
|
||||||
cleanup_endpoint(Endpoint, undefined, disabled),
|
cleanup_endpoint(Endpoint, undefined, disabled),
|
||||||
delete_endpoint(EndpointTable, Mac),
|
delete_endpoint(EndpointTable, Mac),
|
||||||
@ -246,7 +250,10 @@ handle_call({disable_client, ClientId}, _From, State = #state{endpoint_table = E
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
handle_call({get_channel, ClientId}, _From, State = #state{endpoint_table = EndpointTable}) ->
|
handle_call({get_channel, ClientId}, _From, State = #state{endpoint_table = EndpointTable}) ->
|
||||||
case match_endpoint(EndpointTable, {'$1', #endpoint{client_id = ClientId, _ = '_'}}) of
|
MatchSpec = ets:fun2ms(fun(Object = {_Mac, #endpoint{client_id = ClientId0}}) when ClientId0 =:= ClientId ->
|
||||||
|
Object
|
||||||
|
end),
|
||||||
|
case select_endpoint(EndpointTable, MatchSpec) of
|
||||||
{ok, _, #endpoint{channel_pid = ChannelPid}} ->
|
{ok, _, #endpoint{channel_pid = ChannelPid}} ->
|
||||||
{reply, {ok, ChannelPid}, State};
|
{reply, {ok, ChannelPid}, State};
|
||||||
error ->
|
error ->
|
||||||
@ -284,7 +291,10 @@ handle_call({peer_info, SrcMac, DstMac}, _From, State = #state{endpoint_table =
|
|||||||
|
|
||||||
%% arp查询
|
%% arp查询
|
||||||
handle_call({arp_request, TargetIp}, _From, State = #state{endpoint_table = EndpointTable}) ->
|
handle_call({arp_request, TargetIp}, _From, State = #state{endpoint_table = EndpointTable}) ->
|
||||||
case match_endpoint(EndpointTable, {'$1', #endpoint{ip = TargetIp, _ = '_'}}) of
|
MatchSpec = ets:fun2ms(fun(Object = {_Mac, #endpoint{ip = Ip}}) when Ip =:= TargetIp ->
|
||||||
|
Object
|
||||||
|
end),
|
||||||
|
case select_endpoint(EndpointTable, MatchSpec) of
|
||||||
error ->
|
error ->
|
||||||
{reply, error, State};
|
{reply, error, State};
|
||||||
{ok, Mac, _} ->
|
{ok, Mac, _} ->
|
||||||
@ -293,7 +303,10 @@ handle_call({arp_request, TargetIp}, _From, State = #state{endpoint_table = Endp
|
|||||||
|
|
||||||
%% 发送命令
|
%% 发送命令
|
||||||
handle_call({command, ReceiverPid, ClientId, SubCommand}, _From, State = #state{endpoint_table = EndpointTable}) ->
|
handle_call({command, ReceiverPid, ClientId, SubCommand}, _From, State = #state{endpoint_table = EndpointTable}) ->
|
||||||
case match_endpoint(EndpointTable, {'$1', #endpoint{client_id = ClientId, _ = '_'}}) of
|
MatchSpec = ets:fun2ms(fun(Object = {_Mac, #endpoint{client_id = ClientId0}}) when ClientId0 =:= ClientId ->
|
||||||
|
Object
|
||||||
|
end),
|
||||||
|
case select_endpoint(EndpointTable, MatchSpec) of
|
||||||
{ok, _Mac, #endpoint{channel_pid = ChannelPid}} ->
|
{ok, _Mac, #endpoint{channel_pid = ChannelPid}} ->
|
||||||
Ref = make_ref(),
|
Ref = make_ref(),
|
||||||
sdlan_quic_channel:command(ChannelPid, Ref, ReceiverPid, SubCommand),
|
sdlan_quic_channel:command(ChannelPid, Ref, ReceiverPid, SubCommand),
|
||||||
@ -383,7 +396,7 @@ handle_info(Info, State) ->
|
|||||||
%% necessary cleaning up. When it returns, the gen_server terminates
|
%% necessary cleaning up. When it returns, the gen_server terminates
|
||||||
%% with Reason. The return value is ignored.
|
%% with Reason. The return value is ignored.
|
||||||
-spec(terminate(Reason :: (normal | shutdown | {shutdown, term()} | term()),
|
-spec(terminate(Reason :: (normal | shutdown | {shutdown, term()} | term()),
|
||||||
State :: #state{}) -> none()).
|
State :: #state{}) -> ok).
|
||||||
terminate(Reason, #state{network_id = NetworkId, endpoint_table = EndpointTable}) ->
|
terminate(Reason, #state{network_id = NetworkId, endpoint_table = EndpointTable}) ->
|
||||||
broadcast(fun(#endpoint{channel_pid = ChannelPid}) ->
|
broadcast(fun(#endpoint{channel_pid = ChannelPid}) ->
|
||||||
case is_pid(ChannelPid) andalso is_process_alive(ChannelPid) of
|
case is_pid(ChannelPid) andalso is_process_alive(ChannelPid) of
|
||||||
@ -581,7 +594,7 @@ should_stop_channel(ChannelPid, KeepChannelPid) when is_pid(ChannelPid), Channel
|
|||||||
should_stop_channel(_, _) ->
|
should_stop_channel(_, _) ->
|
||||||
false.
|
false.
|
||||||
|
|
||||||
-spec broadcast(Fun :: fun((#endpoint{}) -> no_return()), ExcludeMacs :: [binary()], Table :: ets:tid()) -> no_return().
|
-spec broadcast(Fun :: fun((#endpoint{}) -> term()), ExcludeMacs :: [binary()], Table :: ets:tid()) -> ok.
|
||||||
broadcast(Fun, ExcludeMacs, Table) when is_function(Fun, 1), is_list(ExcludeMacs) ->
|
broadcast(Fun, ExcludeMacs, Table) when is_function(Fun, 1), is_list(ExcludeMacs) ->
|
||||||
lists:foreach(fun({Mac, Endpoint}) ->
|
lists:foreach(fun({Mac, Endpoint}) ->
|
||||||
case lists:member(Mac, ExcludeMacs) of
|
case lists:member(Mac, ExcludeMacs) of
|
||||||
@ -655,9 +668,9 @@ channel_metrics(Table) ->
|
|||||||
end
|
end
|
||||||
end, [], list_endpoints(Table)).
|
end, [], list_endpoints(Table)).
|
||||||
|
|
||||||
-spec match_endpoint(Table :: ets:tid(), Pattern :: tuple()) -> error | {ok, Mac :: binary(), Endpoint :: #endpoint{}}.
|
-spec select_endpoint(Table :: ets:tid(), MatchSpec :: ets:match_spec()) -> error | {ok, Mac :: binary(), Endpoint :: #endpoint{}}.
|
||||||
match_endpoint(Table, Pattern) ->
|
select_endpoint(Table, MatchSpec) ->
|
||||||
case catch ets:match_object(Table, Pattern, 1) of
|
case catch ets:select(Table, MatchSpec, 1) of
|
||||||
{[], _Continuation} ->
|
{[], _Continuation} ->
|
||||||
error;
|
error;
|
||||||
{[{Mac, Endpoint = #endpoint{}}], _Continuation} ->
|
{[{Mac, Endpoint = #endpoint{}}], _Continuation} ->
|
||||||
|
|||||||
@ -29,7 +29,7 @@
|
|||||||
%%% API
|
%%% API
|
||||||
%%%===================================================================
|
%%%===================================================================
|
||||||
|
|
||||||
-spec attach(NetworkPid :: pid(), ThrottleKey :: atom()) -> no_return().
|
-spec attach(NetworkPid :: pid(), ThrottleKey :: atom()) -> ok.
|
||||||
attach(NetworkPid, ThrottleKey) when is_pid(NetworkPid), is_atom(ThrottleKey) ->
|
attach(NetworkPid, ThrottleKey) when is_pid(NetworkPid), is_atom(ThrottleKey) ->
|
||||||
gen_server:cast(?SERVER, {attach, NetworkPid, ThrottleKey}).
|
gen_server:cast(?SERVER, {attach, NetworkPid, ThrottleKey}).
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user