130 lines
4.5 KiB
Erlang
130 lines
4.5 KiB
Erlang
%%%-------------------------------------------------------------------
|
||
%%% @author anlicheng
|
||
%%% @copyright (C) 2024, <COMPANY>
|
||
%%% @doc
|
||
%%%
|
||
%%% @end
|
||
%%% Created : 09. 4月 2024 17:37
|
||
%%%-------------------------------------------------------------------
|
||
-module(dns_server).
|
||
-author("anlicheng").
|
||
-include("sdlan.hrl").
|
||
-include("sdlan_pb.hrl").
|
||
|
||
-behaviour(gen_server).
|
||
|
||
%% API
|
||
-export([start_link/2]).
|
||
-export([get_name/1]).
|
||
|
||
%% gen_server callbacks
|
||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
|
||
|
||
-define(SERVER, ?MODULE).
|
||
|
||
-record(state, {
|
||
socket
|
||
}).
|
||
|
||
%%%===================================================================
|
||
%%% API
|
||
%%%===================================================================
|
||
|
||
-spec get_name(Id :: integer()) -> atom().
|
||
get_name(Id) when is_integer(Id) ->
|
||
list_to_atom("dns_server:" ++ integer_to_list(Id)).
|
||
|
||
%% @doc Spawns the server and registers the local name (unique)
|
||
-spec(start_link(Name :: atom(), Port :: integer()) ->
|
||
{ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
|
||
start_link(Name, Port) when is_atom(Name), is_integer(Port) ->
|
||
gen_server:start_link({local, Name}, ?MODULE, [Port], []).
|
||
|
||
%%%===================================================================
|
||
%%% gen_server callbacks
|
||
%%%===================================================================
|
||
|
||
%% @private
|
||
%% @doc Initializes the server
|
||
-spec(init(Args :: term()) ->
|
||
{ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
|
||
{stop, Reason :: term()} | ignore).
|
||
init([Port]) ->
|
||
%% 需要提高进程的调度优先级
|
||
erlang:process_flag(priority, high),
|
||
Opts = [
|
||
binary,
|
||
{reuseaddr, true},
|
||
{reuseport, true},
|
||
{active, true},
|
||
{recbuf, 5 * 1024 * 1024},
|
||
{sndbuf, 5 * 1024 * 1024}
|
||
],
|
||
{ok, Socket} = gen_udp:open(Port, Opts),
|
||
inet_udp:controlling_process(Socket, self()),
|
||
|
||
logger:debug("[sdlan_stun] start at port: ~p", [Port]),
|
||
{ok, #state{socket = Socket}}.
|
||
|
||
%% @private
|
||
%% @doc Handling call messages
|
||
-spec(handle_call(Request :: term(), From :: {pid(), Tag :: term()},
|
||
State :: #state{}) ->
|
||
{reply, Reply :: term(), NewState :: #state{}} |
|
||
{reply, Reply :: term(), NewState :: #state{}, timeout() | hibernate} |
|
||
{noreply, NewState :: #state{}} |
|
||
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
||
{stop, Reason :: term(), Reply :: term(), NewState :: #state{}} |
|
||
{stop, Reason :: term(), NewState :: #state{}}).
|
||
handle_call(_Request, _From, State = #state{}) ->
|
||
{reply, ok, State}.
|
||
|
||
%% @private
|
||
%% @doc Handling cast messages
|
||
-spec(handle_cast(Request :: term(), State :: #state{}) ->
|
||
{noreply, NewState :: #state{}} |
|
||
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
||
{stop, Reason :: term(), NewState :: #state{}}).
|
||
%% 当前node下的转发,基于进程间的通讯
|
||
handle_cast(_Request, State) ->
|
||
{noreply, State}.
|
||
|
||
%% @private
|
||
%% @doc Handling all non call/cast messages
|
||
-spec(handle_info(Info :: timeout() | term(), State :: #state{}) ->
|
||
{noreply, NewState :: #state{}} |
|
||
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
||
{stop, Reason :: term(), NewState :: #state{}}).
|
||
handle_info({udp, Sock, Ip, Port, IpPacket}, State = #state{socket = Sock}) ->
|
||
case dns_resolver:resolve(IpPacket) of
|
||
{ok, RespIpPacket} ->
|
||
gen_udp:send(Sock, Ip, Port, RespIpPacket);
|
||
{error, Reason} ->
|
||
logger:debug("[dns_server] resolve dns query error: ~p", [Reason])
|
||
end,
|
||
{noreply, State};
|
||
handle_info(Info, State) ->
|
||
logger:error("[sdlan_stun] get a unknown message: ~p, channel will closed", [Info]),
|
||
{noreply, State}.
|
||
|
||
%% @private
|
||
%% @doc This function is called by a gen_server when it is about to
|
||
%% terminate. It should be the opposite of Module:init/1 and do any
|
||
%% 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{}) -> term()).
|
||
terminate(_Reason, _State = #state{}) ->
|
||
ok.
|
||
|
||
%% @private
|
||
%% @doc Convert process state when code is changed
|
||
-spec(code_change(OldVsn :: term() | {down, term()}, State :: #state{},
|
||
Extra :: term()) ->
|
||
{ok, NewState :: #state{}} | {error, Reason :: term()}).
|
||
code_change(_OldVsn, State = #state{}, _Extra) ->
|
||
{ok, State}.
|
||
|
||
%%%===================================================================
|
||
%%% Internal functions
|
||
%%%=================================================================== |