fix network ets

This commit is contained in:
anlicheng 2026-05-01 15:20:51 +08:00
parent 69be3c0154
commit 128a937029

View File

@ -132,8 +132,7 @@ wait_command_ack(Ref, Timeout) when is_reference(Ref), is_integer(Timeout) ->
-spec forward_by_ets(NetworkId :: integer(), Sock :: any(), SrcMac :: binary(), DstMac :: binary(), Packet :: binary()) -> -spec forward_by_ets(NetworkId :: integer(), Sock :: any(), SrcMac :: binary(), DstMac :: binary(), Packet :: binary()) ->
{ok, ForwardBytes :: integer()} | {error, Reason :: any()}. {ok, ForwardBytes :: integer()} | {error, Reason :: any()}.
forward_by_ets(NetworkId, Sock, SrcMac, DstMac, Packet) forward_by_ets(NetworkId, Sock, SrcMac, DstMac, Packet) when is_integer(NetworkId), is_binary(SrcMac), is_binary(DstMac), is_binary(Packet) ->
when is_integer(NetworkId), is_binary(SrcMac), is_binary(DstMac), is_binary(Packet) ->
case lookup_endpoint(NetworkId, SrcMac) of case lookup_endpoint(NetworkId, SrcMac) of
#endpoint{} -> #endpoint{} ->
case sdlan_util:is_broadcast_mac(DstMac) orelse sdlan_util:is_multicast_mac(DstMac) of case sdlan_util:is_broadcast_mac(DstMac) orelse sdlan_util:is_multicast_mac(DstMac) of
@ -263,7 +262,7 @@ 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 search_endpoint(fun(_, #endpoint{client_id = ClientId0}) -> ClientId =:= ClientId0 end, EndpointTable) of case match_endpoint(EndpointTable, {'$1', #endpoint{client_id = ClientId, _ = '_'}}) 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),
@ -273,7 +272,7 @@ 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 search_endpoint(fun(_, #endpoint{client_id = ClientId0}) -> ClientId =:= ClientId0 end, EndpointTable) of case match_endpoint(EndpointTable, {'$1', #endpoint{client_id = ClientId, _ = '_'}}) of
{ok, _, #endpoint{channel_pid = ChannelPid}} -> {ok, _, #endpoint{channel_pid = ChannelPid}} ->
{reply, {ok, ChannelPid}, State}; {reply, {ok, ChannelPid}, State};
error -> error ->
@ -311,7 +310,7 @@ 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 search_endpoint(fun(_, #endpoint{ip = Ip0}) -> Ip0 =:= TargetIp end, EndpointTable) of case match_endpoint(EndpointTable, {'$1', #endpoint{ip = TargetIp, _ = '_'}}) of
error -> error ->
{reply, error, State}; {reply, error, State};
{ok, Mac, _} -> {ok, Mac, _} ->
@ -320,7 +319,7 @@ 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 search_endpoint(fun(_, #endpoint{client_id = ClientId0}) -> ClientId =:= ClientId0 end, EndpointTable) of case match_endpoint(EndpointTable, {'$1', #endpoint{client_id = ClientId, _ = '_'}}) 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),
@ -507,24 +506,24 @@ delete_endpoint(Table, Mac) when is_binary(Mac) ->
lookup_endpoint(NetworkId, Mac) when is_integer(NetworkId), is_binary(Mac) -> lookup_endpoint(NetworkId, Mac) when is_integer(NetworkId), is_binary(Mac) ->
lookup_endpoint(endpoint_table_name(NetworkId), Mac); lookup_endpoint(endpoint_table_name(NetworkId), Mac);
lookup_endpoint(Table, Mac) when is_binary(Mac) -> lookup_endpoint(Table, Mac) when is_binary(Mac) ->
case catch ets:lookup(Table, Mac) of try ets:lookup(Table, Mac) of
[{Mac, Endpoint = #endpoint{}}] -> [{Mac, Endpoint = #endpoint{}}] ->
Endpoint; Endpoint;
[] -> [] ->
undefined;
{'EXIT', _} ->
undefined undefined
catch error:_ ->
undefined
end. end.
-spec list_endpoints(NetworkIdOrTable :: integer() | ets:tid()) -> [{binary(), #endpoint{}}]. -spec list_endpoints(NetworkIdOrTable :: integer() | ets:tid()) -> [{binary(), #endpoint{}}].
list_endpoints(NetworkId) when is_integer(NetworkId) -> list_endpoints(NetworkId) when is_integer(NetworkId) ->
list_endpoints(endpoint_table_name(NetworkId)); list_endpoints(endpoint_table_name(NetworkId));
list_endpoints(Table) -> list_endpoints(Table) ->
case catch ets:tab2list(Table) of try ets:tab2list(Table) of
Endpoints when is_list(Endpoints) -> Endpoints when is_list(Endpoints) ->
Endpoints; Endpoints
{'EXIT', _} -> catch error:_ ->
[] []
end. end.
-spec remove_channel_endpoints(ChannelPid :: pid(), Table :: ets:tid()) -> ok. -spec remove_channel_endpoints(ChannelPid :: pid(), Table :: ets:tid()) -> ok.
@ -668,18 +667,18 @@ channel_metrics(Table) ->
end end
end, [], list_endpoints(Table)). end, [], list_endpoints(Table)).
-spec search_endpoint(F :: fun((term(), term()) -> boolean()), Table :: ets:tid()) -> error | {ok, Key :: any(), Val :: any()}. -spec match_endpoint(Table :: ets:tid(), Pattern :: tuple()) -> error | {ok, Mac :: binary(), Endpoint :: #endpoint{}}.
search_endpoint(F, Table) when is_function(F, 2) -> match_endpoint(Table, Pattern) ->
search_endpoint0(F, list_endpoints(Table)). case catch ets:match_object(Table, Pattern, 1) of
search_endpoint0(F, [{Key, Value}|Rest]) when is_function(F, 2) -> {[], _Continuation} ->
case F(Key, Value) of error;
true -> {[{Mac, Endpoint = #endpoint{}}], _Continuation} ->
{ok, Key, Value}; {ok, Mac, Endpoint};
false -> '$end_of_table' ->
search_endpoint0(F, Rest) error;
end; {'EXIT', _} ->
search_endpoint0(_F, []) -> error
error. end.
-spec same_hole(Hole :: #hole{}, Hole :: #hole{}) -> boolean(). -spec same_hole(Hole :: #hole{}, Hole :: #hole{}) -> boolean().
same_hole(#hole{peer = OldPeer, nat_type = OldNatType}, #hole{peer = Peer, nat_type = NatType}) when OldPeer =:= Peer, OldNatType =:= NatType -> same_hole(#hole{peer = OldPeer, nat_type = OldNatType}, #hole{peer = Peer, nat_type = NatType}) when OldPeer =:= Peer, OldNatType =:= NatType ->