增加域名解析

This commit is contained in:
anlicheng 2025-12-13 21:26:48 +08:00
parent 1b069ba63c
commit 6d2f9d52c3
5 changed files with 40 additions and 25 deletions

View File

@ -126,7 +126,7 @@ resolver(Packet) when is_binary(Packet) ->
resolver0(Packet, dns:decode_message(Packet)).
resolver0(Packet, QueryMsg = #dns_message{qc = 1, questions = [Question = #dns_query{name = QName, type = QType, class = QClass}|_]}) ->
%%
case sdlan_dns_resolver:resolve(QName) of
case sdlan_hostname_regedit:lookup(QName) of
{ok, Ip} ->
Answer = #dns_rr {
name = QName,

View File

@ -16,6 +16,9 @@ start(_StartType, _StartArgs) ->
%%
erlang:system_flag(fullsweep_after, 16),
%%
sdlan_hostname_regedit:init(),
start_http_server(),
start_tcp_server(),
sdlan_sup:start_link().

View File

@ -1,24 +0,0 @@
%%%-------------------------------------------------------------------
%%% @author anlicheng
%%% @copyright (C) 2025, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 13. 12 2025 15:44
%%%-------------------------------------------------------------------
-module(sdlan_dns_resolver).
-author("anlicheng").
%% API
-export([resolve/1]).
-spec resolve(QName :: binary()) -> {ok, IpAddr :: inet:ip4_address()} | error.
resolve(QName) when is_binary(QName) ->
Suffix = <<".punchnet.ts.net">>,
case dns_utils:ends_with(QName, Suffix) of
true ->
Ip4 = rand:uniform(254),
{ok, {192, 168, 1, Ip4}};
false ->
error
end.

View File

@ -0,0 +1,32 @@
%%%-------------------------------------------------------------------
%%% @author anlicheng
%%% @copyright (C) 2025, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 13. 12 2025 21:09
%%%-------------------------------------------------------------------
-module(sdlan_hostname_regedit).
-author("anlicheng").
%% API
-export([init/0, lookup/1, insert/2]).
-define(TABLE, sdlan_hostname_regedit).
init() ->
ets:new(?TABLE, [named_table, set, public, {read_concurrency, true}, {write_concurrency, true}]).
-spec lookup(FullHostname :: binary()) -> error | {ok, Ip :: inet:ip4_address()}.
lookup(FullHostname) when is_binary(FullHostname) ->
case ets:lookup(?TABLE, FullHostname) of
[{_, Ip}] ->
{ok, Ip};
[] ->
error
end.
-spec insert(FullHostname :: binary(), Ip :: integer()) -> no_return().
insert(FullHostname, Ip) when is_integer(FullHostname) ->
<<Ip0, Ip1, Ip2, Ip3>> = <<Ip:32>>,
true = ets:insert(?TABLE, {FullHostname, {Ip0, Ip1, Ip2, Ip3}}).

View File

@ -231,6 +231,10 @@ handle_call({assign_ip_addr, ChannelPid, ClientId, Mac, NetAddr0, HostName}, _Fr
%% channel
maybe_close_channel(maps:get(Mac, UsedMap, undefined)),
%% ->ip的映射关系
FullHostname = <<HostName/binary, ".", Domain/binary>>,
sdlan_hostname_regedit:insert(FullHostname, Ip),
%% channel之间的关系
MRef = monitor(process, ChannelPid),
NUsedMap = maps:put(Mac, #host{client_id = ClientId, channel_pid = ChannelPid, monitor_ref = MRef}, UsedMap),