fix network ets

This commit is contained in:
anlicheng 2026-05-01 15:31:03 +08:00
parent 128a937029
commit e931721728

View File

@ -228,31 +228,21 @@ handle_call({attach, ChannelPid, ClientId, Mac, Ip, Hostname}, _From,
%% ->ip的映射关系 %% ->ip的映射关系
sdlan_hostname_regedit:insert(Hostname, Domain, Ip), sdlan_hostname_regedit:insert(Hostname, Domain, Ip),
OldEndpoint = lookup_endpoint(EndpointTable, Mac),
%% mac对应的Endpoint存在ip变了arp %% mac对应的Endpoint存在ip变了arp
maybe maybe_nat_changed(ChannelPid, Mac, Ip, EndpointTable),
#endpoint{ip = OldIp} ?= OldEndpoint,
true ?= OldIp =/= Ip,
Event = sdlan_pb:encode_msg(#'SDLEvent'{
event = {nat_changed, #'SDLEvent.NatChanged' {
mac = Mac,
ip = Ip
}}
}),
logger:debug("Event: nat_changed, for attach"),
broadcast(fun(#endpoint{channel_pid = ChannelPid0}) ->
sdlan_quic_channel:send_event(ChannelPid0, Event)
end, [Mac], EndpointTable)
end,
%% attach需要清理之前的绑定信息使IP未变化channel
cleanup_endpoint(OldEndpoint, ChannelPid, rebind),
ChannelRef = monitor(process, ChannelPid), ChannelRef = monitor(process, ChannelPid),
SessionToken = gen_session_token(), SessionToken = gen_session_token(),
Endpoint = #endpoint{channel_pid = ChannelPid, channel_ref = ChannelRef, Endpoint = #endpoint{
client_id = ClientId, mac = Mac, ip = Ip, hostname = Hostname, session_token = SessionToken, last_seen = erlang:monotonic_time(second)}, channel_pid = ChannelPid,
channel_ref = ChannelRef,
client_id = ClientId,
mac = Mac,
ip = Ip,
hostname = Hostname,
session_token = SessionToken,
last_seen = erlang:monotonic_time(second)
},
insert_endpoint(EndpointTable, Mac, Endpoint), insert_endpoint(EndpointTable, Mac, Endpoint),
%% id %% id
@ -575,6 +565,28 @@ forward_broadcast_by_ets(NetworkId, Sock, SrcMac, DstMac, Packet) ->
[NetworkId, sdlan_util:format_mac(SrcMac), sdlan_util:format_mac(DstMac)]), [NetworkId, sdlan_util:format_mac(SrcMac), sdlan_util:format_mac(DstMac)]),
{ok, byte_size(Packet)}. {ok, byte_size(Packet)}.
maybe_nat_changed(ChannelPid, Mac, Ip, EndpointTable) ->
OldEndpoint = lookup_endpoint(EndpointTable, Mac),
%% mac对应的Endpoint存在ip变了arp
maybe
#endpoint{ip = OldIp} ?= OldEndpoint,
true ?= OldIp =/= Ip,
Event = sdlan_pb:encode_msg(#'SDLEvent'{
event = {nat_changed, #'SDLEvent.NatChanged' {
mac = Mac,
ip = Ip
}}
}),
logger:debug("Event: nat_changed, for attach"),
broadcast(fun(#endpoint{channel_pid = ChannelPid0}) ->
sdlan_quic_channel:send_event(ChannelPid0, Event)
end, [Mac], EndpointTable)
end,
%% attach需要清理之前的绑定信息使IP未变化channel
cleanup_endpoint(OldEndpoint, ChannelPid, rebind).
cleanup_endpoint(undefined, _KeepChannelPid, _Reason) -> cleanup_endpoint(undefined, _KeepChannelPid, _Reason) ->
ok; ok;
cleanup_endpoint(#endpoint{channel_ref = ChannelRef, channel_pid = ChannelPid}, KeepChannelPid, Reason) -> cleanup_endpoint(#endpoint{channel_ref = ChannelRef, channel_pid = ChannelPid}, KeepChannelPid, Reason) ->