diff --git a/src/sdlan_domain_regedit.erl b/src/sdlan_domain_regedit.erl index 7fe64df..dcefca0 100644 --- a/src/sdlan_domain_regedit.erl +++ b/src/sdlan_domain_regedit.erl @@ -31,6 +31,6 @@ maybe_domain(QName) when is_binary(QName) -> false end. --spec insert(Domain :: binary()) -> no_return(). +-spec insert(Domain :: binary()) -> true. insert(Domain) when is_binary(Domain) -> - true = ets:insert(?TABLE, {Domain}). \ No newline at end of file + true = ets:insert(?TABLE, {Domain}). diff --git a/src/sdlan_hostname_regedit.erl b/src/sdlan_hostname_regedit.erl index 0acb7a4..843045c 100644 --- a/src/sdlan_hostname_regedit.erl +++ b/src/sdlan_hostname_regedit.erl @@ -27,11 +27,11 @@ lookup(FullHostname) when is_binary(FullHostname) -> error 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 /= <<>> -> FullHostname = <>, LowerFullHostname = string:lowercase(FullHostname), <> = <>, true = ets:insert(?TABLE, {LowerFullHostname, {Ip0, Ip1, Ip2, Ip3}}); insert(_, _, _) -> - ok. \ No newline at end of file + ok. diff --git a/src/sdlan_network.erl b/src/sdlan_network.erl index 7f25434..bf49e1e 100644 --- a/src/sdlan_network.erl +++ b/src/sdlan_network.erl @@ -10,6 +10,7 @@ -author("anlicheng"). -include("sdlan.hrl"). -include("sdlan_pb.hrl"). +-include_lib("stdlib/include/ms_transform.hrl"). -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) -> 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) -> gen_server:cast(Pid, {unregister, ClientId, Mac}). @@ -144,7 +145,7 @@ forward_by_ets(NetworkId, Sock, SrcMac, DstMac, Packet) when is_integer(NetworkI end. %% 更新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) -> 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设置为禁止状态,不允许重连 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} -> cleanup_endpoint(Endpoint, undefined, disabled), delete_endpoint(EndpointTable, Mac), @@ -246,7 +250,10 @@ handle_call({disable_client, ClientId}, _From, State = #state{endpoint_table = E end; 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}} -> {reply, {ok, ChannelPid}, State}; error -> @@ -284,7 +291,10 @@ handle_call({peer_info, SrcMac, DstMac}, _From, State = #state{endpoint_table = %% arp查询 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 -> {reply, error, State}; {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}) -> - 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}} -> Ref = make_ref(), 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 %% with Reason. The return value is ignored. -spec(terminate(Reason :: (normal | shutdown | {shutdown, term()} | term()), - State :: #state{}) -> none()). + State :: #state{}) -> ok). terminate(Reason, #state{network_id = NetworkId, endpoint_table = EndpointTable}) -> broadcast(fun(#endpoint{channel_pid = ChannelPid}) -> 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(_, _) -> 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) -> lists:foreach(fun({Mac, Endpoint}) -> case lists:member(Mac, ExcludeMacs) of @@ -655,9 +668,9 @@ channel_metrics(Table) -> end end, [], list_endpoints(Table)). --spec match_endpoint(Table :: ets:tid(), Pattern :: tuple()) -> error | {ok, Mac :: binary(), Endpoint :: #endpoint{}}. -match_endpoint(Table, Pattern) -> - case catch ets:match_object(Table, Pattern, 1) of +-spec select_endpoint(Table :: ets:tid(), MatchSpec :: ets:match_spec()) -> error | {ok, Mac :: binary(), Endpoint :: #endpoint{}}. +select_endpoint(Table, MatchSpec) -> + case catch ets:select(Table, MatchSpec, 1) of {[], _Continuation} -> error; {[{Mac, Endpoint = #endpoint{}}], _Continuation} -> diff --git a/src/sdlan_network_coordinator.erl b/src/sdlan_network_coordinator.erl index 42aedcf..1017e14 100644 --- a/src/sdlan_network_coordinator.erl +++ b/src/sdlan_network_coordinator.erl @@ -29,7 +29,7 @@ %%% 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) -> gen_server:cast(?SERVER, {attach, NetworkPid, ThrottleKey}).