From d41992099dd3a734aca9e35c8195deda61fbeead Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Thu, 9 Apr 2026 16:22:35 +0800 Subject: [PATCH] fix --- include/sdlan.hrl | 9 -- src/quic/sdlan_quic_channel.erl | 14 +-- src/sdlan_network.erl | 8 +- src/sdlan_pb.erl | 160 ++++++++++++++++---------------- proto/tun.proto => tun.proto | 0 5 files changed, 91 insertions(+), 100 deletions(-) rename proto/tun.proto => tun.proto (100%) diff --git a/include/sdlan.hrl b/include/sdlan.hrl index c25f5e3..c5d6790 100644 --- a/include/sdlan.hrl +++ b/include/sdlan.hrl @@ -71,15 +71,6 @@ %% 数据转发 -define(PACKET_STUN_DATA, 16#FF). -%% 二级命名 - -%% 定义事件信息 --define(EVENT_KNOWN_IP, 16#01). --define(EVENT_DROP_IP, 16#02). --define(EVENT_NAT_CHANGED, 16#03). --define(EVENT_SEND_REGISTER, 16#04). --define(EVENT_NETWORK_SHUTDOWN, 16#FF). - %% stun请求的attr -define(STUN_ATTR_CHANGE_NONE, 0). -define(STUN_ATTR_CHANGE_PORT, 1). diff --git a/src/quic/sdlan_quic_channel.erl b/src/quic/sdlan_quic_channel.erl index 44ae91c..b474724 100644 --- a/src/quic/sdlan_quic_channel.erl +++ b/src/quic/sdlan_quic_channel.erl @@ -25,7 +25,7 @@ %% API -export([start_link/2]). --export([send_event/3, command/4, stop/2]). +-export([send_event/2, command/4, stop/2]). -export([test_rules/2]). %% gen_statem callbacks @@ -71,9 +71,9 @@ test_rules(SrcIdentityId, DstIdentityId) when is_integer(SrcIdentityId), is_inte logger:debug("[sdlan_channel] test_rules policy_request src_identity_id: ~p, dst_identity_id: ~p, rules: ~p", [SrcIdentityId, DstIdentityId, Rules]), iolist_to_binary(lists:map(fun({Proto, Port}) -> <> end, Rules)). --spec send_event(Pid :: pid(), EventType :: integer(), Event :: binary()) -> no_return(). -send_event(Pid, EventType, Event) when is_pid(Pid), is_integer(EventType), is_binary(Event) -> - gen_statem:cast(Pid, {send_event, EventType, Event}). +-spec send_event(Pid :: pid(), Event :: binary()) -> no_return(). +send_event(Pid, ProtobufEvent) when is_pid(Pid), is_binary(ProtobufEvent) -> + gen_statem:cast(Pid, {send_event, ProtobufEvent}). -spec command(Pid :: pid(), Ref :: reference(), ReceiverPid :: pid(), {Tag :: atom(), SubCommand :: any()}) -> no_return(). command(Pid, Ref, ReceiverPid, SubCommand) when is_pid(Pid), is_pid(ReceiverPid) -> @@ -340,9 +340,9 @@ handle_event(info, {timeout, _, ping_ticker}, _, State = #state{client_id = Clie end; %% 发送指令信息 -handle_event(cast, {send_event, EventType, Event}, registered, #state{stream = Stream, client_id = ClientId}) -> - logger:debug("[sdlan_channel] client_id: ~p, will send eventType: ~p, event: ~p", [ClientId, EventType, Event]), - quic_send(Stream, <>), +handle_event(cast, {send_event, Event}, registered, #state{stream = Stream, client_id = ClientId}) -> + logger:debug("[sdlan_channel] client_id: ~p, will send eventType: ~p, event: ~p", [ClientId, Event]), + quic_send(Stream, <>), keep_state_and_data; %% 发送命令信息 diff --git a/src/sdlan_network.erl b/src/sdlan_network.erl index 9a9922b..05c7069 100644 --- a/src/sdlan_network.erl +++ b/src/sdlan_network.erl @@ -231,7 +231,7 @@ handle_call({attach, ChannelPid, ClientId, Mac, Ip, Hostname}, _From, logger:debug("Event: nat_changed, for attach"), broadcast(fun(#endpoint{channel_pid = ChannelPid0}) -> - sdlan_quic_channel:send_event(ChannelPid0, ?EVENT_NAT_CHANGED, Event) + sdlan_quic_channel:send_event(ChannelPid0, Event) end, [Mac], Endpoints), %% 清理就的绑定关系 @@ -293,7 +293,7 @@ handle_call({peer_info, SrcMac, DstMac}, _From, State = #state{endpoints = Endpo }), logger:debug("Event: send_register, for peer_info"), - sdlan_quic_channel:send_event(DstChannelPid, ?EVENT_SEND_REGISTER, RegisterEvent) + sdlan_quic_channel:send_event(DstChannelPid, RegisterEvent) end, {reply, {ok, {DstNatPeer, DstNatType}, DstV6Info}, State}; _ -> @@ -416,7 +416,7 @@ handle_cast({update_hole, SessionToken, ClientId, Mac, Peer, NatType, V6Info}, S logger:debug("[sdlan_network] Event: nat_changed, update_hole, client_id: ~p(~p), hole changed", [ClientId, Ip]), broadcast(fun(#endpoint{channel_pid = ChannelPid}) -> - sdlan_quic_channel:send_event(ChannelPid, ?EVENT_NAT_CHANGED, NatChangedEvent) + sdlan_quic_channel:send_event(ChannelPid, NatChangedEvent) end, [Mac], Endpoints) end, NEndpoint = Endpoint0#endpoint{hole = NHole, v6_info = V6Info, last_seen = erlang:monotonic_time(second)}, @@ -463,7 +463,7 @@ terminate(Reason, #state{network_id = NetworkId, endpoints = Endpoints}) -> logger:debug("[sdlan_network] Event: shutdown"), - sdlan_quic_channel:send_event(ChannelPid, ?EVENT_NETWORK_SHUTDOWN, NetworkShutdownEvent), + sdlan_quic_channel:send_event(ChannelPid, NetworkShutdownEvent), sdlan_quic_channel:stop(ChannelPid, normal); false -> ok diff --git a/src/sdlan_pb.erl b/src/sdlan_pb.erl index 2dbf384..724a451 100644 --- a/src/sdlan_pb.erl +++ b/src/sdlan_pb.erl @@ -46,8 +46,8 @@ -export([gpb_version_as_string/0, gpb_version_as_list/0]). -export([gpb_version_source/0]). --include_lib("gpb/include/gpb.hrl"). -include("sdlan_pb.hrl"). +-include("gpb.hrl"). %% enumerated types @@ -4403,61 +4403,61 @@ fqbins_to_service_and_rpc_name(S, R) -> error({gpb_error, {badservice_or_rpc, {S service_and_rpc_name_to_fqbins(S, R) -> error({gpb_error, {badservice_or_rpc, {S, R}}}). -fqbin_to_msg_name(<<"message.SDLV4Info">>) -> 'SDLV4Info'; -fqbin_to_msg_name(<<"message.SDLV6Info">>) -> 'SDLV6Info'; -fqbin_to_msg_name(<<"message.SDLWelcome">>) -> 'SDLWelcome'; -fqbin_to_msg_name(<<"message.SDLRegisterSuper">>) -> 'SDLRegisterSuper'; -fqbin_to_msg_name(<<"message.SDLRegisterSuperAck">>) -> 'SDLRegisterSuperAck'; -fqbin_to_msg_name(<<"message.SDLRegisterSuperNak">>) -> 'SDLRegisterSuperNak'; -fqbin_to_msg_name(<<"message.SDLQueryInfo">>) -> 'SDLQueryInfo'; -fqbin_to_msg_name(<<"message.SDLPeerInfo">>) -> 'SDLPeerInfo'; -fqbin_to_msg_name(<<"message.SDLArpRequest">>) -> 'SDLArpRequest'; -fqbin_to_msg_name(<<"message.SDLArpResponse">>) -> 'SDLArpResponse'; -fqbin_to_msg_name(<<"message.SDLPolicyRequest">>) -> 'SDLPolicyRequest'; -fqbin_to_msg_name(<<"message.SDLPolicyResponse">>) -> 'SDLPolicyResponse'; -fqbin_to_msg_name(<<"message.SDLEvent.NatChanged">>) -> 'SDLEvent.NatChanged'; -fqbin_to_msg_name(<<"message.SDLEvent.SendRegister">>) -> 'SDLEvent.SendRegister'; -fqbin_to_msg_name(<<"message.SDLEvent.NetworkShutdown">>) -> 'SDLEvent.NetworkShutdown'; -fqbin_to_msg_name(<<"message.SDLEvent">>) -> 'SDLEvent'; -fqbin_to_msg_name(<<"message.SDLCommand.ExitNodeControl">>) -> 'SDLCommand.ExitNodeControl'; -fqbin_to_msg_name(<<"message.SDLCommand">>) -> 'SDLCommand'; -fqbin_to_msg_name(<<"message.SDLCommandAck">>) -> 'SDLCommandAck'; -fqbin_to_msg_name(<<"message.SDLStunRequest">>) -> 'SDLStunRequest'; -fqbin_to_msg_name(<<"message.SDLStunReply">>) -> 'SDLStunReply'; -fqbin_to_msg_name(<<"message.SDLData">>) -> 'SDLData'; -fqbin_to_msg_name(<<"message.SDLStunProbe">>) -> 'SDLStunProbe'; -fqbin_to_msg_name(<<"message.SDLStunProbeReply">>) -> 'SDLStunProbeReply'; -fqbin_to_msg_name(<<"message.SDLRegister">>) -> 'SDLRegister'; -fqbin_to_msg_name(<<"message.SDLRegisterAck">>) -> 'SDLRegisterAck'; +fqbin_to_msg_name(<<"SDLV4Info">>) -> 'SDLV4Info'; +fqbin_to_msg_name(<<"SDLV6Info">>) -> 'SDLV6Info'; +fqbin_to_msg_name(<<"SDLWelcome">>) -> 'SDLWelcome'; +fqbin_to_msg_name(<<"SDLRegisterSuper">>) -> 'SDLRegisterSuper'; +fqbin_to_msg_name(<<"SDLRegisterSuperAck">>) -> 'SDLRegisterSuperAck'; +fqbin_to_msg_name(<<"SDLRegisterSuperNak">>) -> 'SDLRegisterSuperNak'; +fqbin_to_msg_name(<<"SDLQueryInfo">>) -> 'SDLQueryInfo'; +fqbin_to_msg_name(<<"SDLPeerInfo">>) -> 'SDLPeerInfo'; +fqbin_to_msg_name(<<"SDLArpRequest">>) -> 'SDLArpRequest'; +fqbin_to_msg_name(<<"SDLArpResponse">>) -> 'SDLArpResponse'; +fqbin_to_msg_name(<<"SDLPolicyRequest">>) -> 'SDLPolicyRequest'; +fqbin_to_msg_name(<<"SDLPolicyResponse">>) -> 'SDLPolicyResponse'; +fqbin_to_msg_name(<<"SDLEvent.NatChanged">>) -> 'SDLEvent.NatChanged'; +fqbin_to_msg_name(<<"SDLEvent.SendRegister">>) -> 'SDLEvent.SendRegister'; +fqbin_to_msg_name(<<"SDLEvent.NetworkShutdown">>) -> 'SDLEvent.NetworkShutdown'; +fqbin_to_msg_name(<<"SDLEvent">>) -> 'SDLEvent'; +fqbin_to_msg_name(<<"SDLCommand.ExitNodeControl">>) -> 'SDLCommand.ExitNodeControl'; +fqbin_to_msg_name(<<"SDLCommand">>) -> 'SDLCommand'; +fqbin_to_msg_name(<<"SDLCommandAck">>) -> 'SDLCommandAck'; +fqbin_to_msg_name(<<"SDLStunRequest">>) -> 'SDLStunRequest'; +fqbin_to_msg_name(<<"SDLStunReply">>) -> 'SDLStunReply'; +fqbin_to_msg_name(<<"SDLData">>) -> 'SDLData'; +fqbin_to_msg_name(<<"SDLStunProbe">>) -> 'SDLStunProbe'; +fqbin_to_msg_name(<<"SDLStunProbeReply">>) -> 'SDLStunProbeReply'; +fqbin_to_msg_name(<<"SDLRegister">>) -> 'SDLRegister'; +fqbin_to_msg_name(<<"SDLRegisterAck">>) -> 'SDLRegisterAck'; fqbin_to_msg_name(E) -> error({gpb_error, {badmsg, E}}). -msg_name_to_fqbin('SDLV4Info') -> <<"message.SDLV4Info">>; -msg_name_to_fqbin('SDLV6Info') -> <<"message.SDLV6Info">>; -msg_name_to_fqbin('SDLWelcome') -> <<"message.SDLWelcome">>; -msg_name_to_fqbin('SDLRegisterSuper') -> <<"message.SDLRegisterSuper">>; -msg_name_to_fqbin('SDLRegisterSuperAck') -> <<"message.SDLRegisterSuperAck">>; -msg_name_to_fqbin('SDLRegisterSuperNak') -> <<"message.SDLRegisterSuperNak">>; -msg_name_to_fqbin('SDLQueryInfo') -> <<"message.SDLQueryInfo">>; -msg_name_to_fqbin('SDLPeerInfo') -> <<"message.SDLPeerInfo">>; -msg_name_to_fqbin('SDLArpRequest') -> <<"message.SDLArpRequest">>; -msg_name_to_fqbin('SDLArpResponse') -> <<"message.SDLArpResponse">>; -msg_name_to_fqbin('SDLPolicyRequest') -> <<"message.SDLPolicyRequest">>; -msg_name_to_fqbin('SDLPolicyResponse') -> <<"message.SDLPolicyResponse">>; -msg_name_to_fqbin('SDLEvent.NatChanged') -> <<"message.SDLEvent.NatChanged">>; -msg_name_to_fqbin('SDLEvent.SendRegister') -> <<"message.SDLEvent.SendRegister">>; -msg_name_to_fqbin('SDLEvent.NetworkShutdown') -> <<"message.SDLEvent.NetworkShutdown">>; -msg_name_to_fqbin('SDLEvent') -> <<"message.SDLEvent">>; -msg_name_to_fqbin('SDLCommand.ExitNodeControl') -> <<"message.SDLCommand.ExitNodeControl">>; -msg_name_to_fqbin('SDLCommand') -> <<"message.SDLCommand">>; -msg_name_to_fqbin('SDLCommandAck') -> <<"message.SDLCommandAck">>; -msg_name_to_fqbin('SDLStunRequest') -> <<"message.SDLStunRequest">>; -msg_name_to_fqbin('SDLStunReply') -> <<"message.SDLStunReply">>; -msg_name_to_fqbin('SDLData') -> <<"message.SDLData">>; -msg_name_to_fqbin('SDLStunProbe') -> <<"message.SDLStunProbe">>; -msg_name_to_fqbin('SDLStunProbeReply') -> <<"message.SDLStunProbeReply">>; -msg_name_to_fqbin('SDLRegister') -> <<"message.SDLRegister">>; -msg_name_to_fqbin('SDLRegisterAck') -> <<"message.SDLRegisterAck">>; +msg_name_to_fqbin('SDLV4Info') -> <<"SDLV4Info">>; +msg_name_to_fqbin('SDLV6Info') -> <<"SDLV6Info">>; +msg_name_to_fqbin('SDLWelcome') -> <<"SDLWelcome">>; +msg_name_to_fqbin('SDLRegisterSuper') -> <<"SDLRegisterSuper">>; +msg_name_to_fqbin('SDLRegisterSuperAck') -> <<"SDLRegisterSuperAck">>; +msg_name_to_fqbin('SDLRegisterSuperNak') -> <<"SDLRegisterSuperNak">>; +msg_name_to_fqbin('SDLQueryInfo') -> <<"SDLQueryInfo">>; +msg_name_to_fqbin('SDLPeerInfo') -> <<"SDLPeerInfo">>; +msg_name_to_fqbin('SDLArpRequest') -> <<"SDLArpRequest">>; +msg_name_to_fqbin('SDLArpResponse') -> <<"SDLArpResponse">>; +msg_name_to_fqbin('SDLPolicyRequest') -> <<"SDLPolicyRequest">>; +msg_name_to_fqbin('SDLPolicyResponse') -> <<"SDLPolicyResponse">>; +msg_name_to_fqbin('SDLEvent.NatChanged') -> <<"SDLEvent.NatChanged">>; +msg_name_to_fqbin('SDLEvent.SendRegister') -> <<"SDLEvent.SendRegister">>; +msg_name_to_fqbin('SDLEvent.NetworkShutdown') -> <<"SDLEvent.NetworkShutdown">>; +msg_name_to_fqbin('SDLEvent') -> <<"SDLEvent">>; +msg_name_to_fqbin('SDLCommand.ExitNodeControl') -> <<"SDLCommand.ExitNodeControl">>; +msg_name_to_fqbin('SDLCommand') -> <<"SDLCommand">>; +msg_name_to_fqbin('SDLCommandAck') -> <<"SDLCommandAck">>; +msg_name_to_fqbin('SDLStunRequest') -> <<"SDLStunRequest">>; +msg_name_to_fqbin('SDLStunReply') -> <<"SDLStunReply">>; +msg_name_to_fqbin('SDLData') -> <<"SDLData">>; +msg_name_to_fqbin('SDLStunProbe') -> <<"SDLStunProbe">>; +msg_name_to_fqbin('SDLStunProbeReply') -> <<"SDLStunProbeReply">>; +msg_name_to_fqbin('SDLRegister') -> <<"SDLRegister">>; +msg_name_to_fqbin('SDLRegisterAck') -> <<"SDLRegisterAck">>; msg_name_to_fqbin(E) -> error({gpb_error, {badmsg, E}}). @@ -4469,7 +4469,7 @@ fqbin_to_enum_name(E) -> error({gpb_error, {badenum, E}}). enum_name_to_fqbin(E) -> error({gpb_error, {badenum, E}}). -get_package_name() -> message. +get_package_name() -> undefined. %% Whether or not the message names @@ -4542,32 +4542,32 @@ get_enum_containment("sdlan") -> []; get_enum_containment(P) -> error({gpb_error, {badproto, P}}). -get_proto_by_msg_name_as_fqbin(<<"message.SDLData">>) -> "sdlan"; -get_proto_by_msg_name_as_fqbin(<<"message.SDLRegisterSuper">>) -> "sdlan"; -get_proto_by_msg_name_as_fqbin(<<"message.SDLRegister">>) -> "sdlan"; -get_proto_by_msg_name_as_fqbin(<<"message.SDLEvent.SendRegister">>) -> "sdlan"; -get_proto_by_msg_name_as_fqbin(<<"message.SDLStunRequest">>) -> "sdlan"; -get_proto_by_msg_name_as_fqbin(<<"message.SDLPolicyRequest">>) -> "sdlan"; -get_proto_by_msg_name_as_fqbin(<<"message.SDLEvent.NatChanged">>) -> "sdlan"; -get_proto_by_msg_name_as_fqbin(<<"message.SDLEvent">>) -> "sdlan"; -get_proto_by_msg_name_as_fqbin(<<"message.SDLCommand">>) -> "sdlan"; -get_proto_by_msg_name_as_fqbin(<<"message.SDLArpRequest">>) -> "sdlan"; -get_proto_by_msg_name_as_fqbin(<<"message.SDLWelcome">>) -> "sdlan"; -get_proto_by_msg_name_as_fqbin(<<"message.SDLStunProbe">>) -> "sdlan"; -get_proto_by_msg_name_as_fqbin(<<"message.SDLPolicyResponse">>) -> "sdlan"; -get_proto_by_msg_name_as_fqbin(<<"message.SDLArpResponse">>) -> "sdlan"; -get_proto_by_msg_name_as_fqbin(<<"message.SDLStunReply">>) -> "sdlan"; -get_proto_by_msg_name_as_fqbin(<<"message.SDLStunProbeReply">>) -> "sdlan"; -get_proto_by_msg_name_as_fqbin(<<"message.SDLRegisterSuperNak">>) -> "sdlan"; -get_proto_by_msg_name_as_fqbin(<<"message.SDLRegisterSuperAck">>) -> "sdlan"; -get_proto_by_msg_name_as_fqbin(<<"message.SDLRegisterAck">>) -> "sdlan"; -get_proto_by_msg_name_as_fqbin(<<"message.SDLCommandAck">>) -> "sdlan"; -get_proto_by_msg_name_as_fqbin(<<"message.SDLCommand.ExitNodeControl">>) -> "sdlan"; -get_proto_by_msg_name_as_fqbin(<<"message.SDLEvent.NetworkShutdown">>) -> "sdlan"; -get_proto_by_msg_name_as_fqbin(<<"message.SDLV6Info">>) -> "sdlan"; -get_proto_by_msg_name_as_fqbin(<<"message.SDLV4Info">>) -> "sdlan"; -get_proto_by_msg_name_as_fqbin(<<"message.SDLQueryInfo">>) -> "sdlan"; -get_proto_by_msg_name_as_fqbin(<<"message.SDLPeerInfo">>) -> "sdlan"; +get_proto_by_msg_name_as_fqbin(<<"SDLData">>) -> "sdlan"; +get_proto_by_msg_name_as_fqbin(<<"SDLRegisterSuper">>) -> "sdlan"; +get_proto_by_msg_name_as_fqbin(<<"SDLRegister">>) -> "sdlan"; +get_proto_by_msg_name_as_fqbin(<<"SDLEvent.SendRegister">>) -> "sdlan"; +get_proto_by_msg_name_as_fqbin(<<"SDLStunRequest">>) -> "sdlan"; +get_proto_by_msg_name_as_fqbin(<<"SDLPolicyRequest">>) -> "sdlan"; +get_proto_by_msg_name_as_fqbin(<<"SDLEvent.NatChanged">>) -> "sdlan"; +get_proto_by_msg_name_as_fqbin(<<"SDLEvent">>) -> "sdlan"; +get_proto_by_msg_name_as_fqbin(<<"SDLCommand">>) -> "sdlan"; +get_proto_by_msg_name_as_fqbin(<<"SDLArpRequest">>) -> "sdlan"; +get_proto_by_msg_name_as_fqbin(<<"SDLWelcome">>) -> "sdlan"; +get_proto_by_msg_name_as_fqbin(<<"SDLStunProbe">>) -> "sdlan"; +get_proto_by_msg_name_as_fqbin(<<"SDLPolicyResponse">>) -> "sdlan"; +get_proto_by_msg_name_as_fqbin(<<"SDLArpResponse">>) -> "sdlan"; +get_proto_by_msg_name_as_fqbin(<<"SDLStunReply">>) -> "sdlan"; +get_proto_by_msg_name_as_fqbin(<<"SDLStunProbeReply">>) -> "sdlan"; +get_proto_by_msg_name_as_fqbin(<<"SDLRegisterSuperNak">>) -> "sdlan"; +get_proto_by_msg_name_as_fqbin(<<"SDLRegisterSuperAck">>) -> "sdlan"; +get_proto_by_msg_name_as_fqbin(<<"SDLRegisterAck">>) -> "sdlan"; +get_proto_by_msg_name_as_fqbin(<<"SDLCommandAck">>) -> "sdlan"; +get_proto_by_msg_name_as_fqbin(<<"SDLCommand.ExitNodeControl">>) -> "sdlan"; +get_proto_by_msg_name_as_fqbin(<<"SDLEvent.NetworkShutdown">>) -> "sdlan"; +get_proto_by_msg_name_as_fqbin(<<"SDLV6Info">>) -> "sdlan"; +get_proto_by_msg_name_as_fqbin(<<"SDLV4Info">>) -> "sdlan"; +get_proto_by_msg_name_as_fqbin(<<"SDLQueryInfo">>) -> "sdlan"; +get_proto_by_msg_name_as_fqbin(<<"SDLPeerInfo">>) -> "sdlan"; get_proto_by_msg_name_as_fqbin(E) -> error({gpb_error, {badmsg, E}}). diff --git a/proto/tun.proto b/tun.proto similarity index 100% rename from proto/tun.proto rename to tun.proto