remove opt for additional

This commit is contained in:
anlicheng 2025-12-04 13:06:05 +08:00
parent 46ad37c81f
commit f0b84cdf65
2 changed files with 7 additions and 69 deletions

View File

@ -167,12 +167,9 @@ build_response(Query, #dns_cache{expire_at = ExpireAt, answers = Answers, author
Now = os:system_time(second),
RemainingTTL = ExpireAt - Now,
Adjust = fun(RR) -> RR#dns_rr{ttl = max(0, RemainingTTL)} end,
Answers2 = [Adjust(RR) || RR <- Answers],
Authority2 = [Adjust(RR) || RR <- Authority],
Additional0 = [Adjust(RR) || RR <- Additional],
Additional2 = add_opt_if_needed(Query, Additional0),
Answers2 = [adjust_ttl(RR, RemainingTTL) || RR <- Answers],
Authority2 = [adjust_ttl(RR, RemainingTTL) || RR <- Authority],
Additional2 = [adjust_ttl(RR, RemainingTTL) || RR <- Additional],
Query#dns_message{
qr = true,
@ -182,20 +179,7 @@ build_response(Query, #dns_cache{expire_at = ExpireAt, answers = Answers, author
additional = Additional2
}.
add_opt_if_needed(Query, Additional) ->
case dns_opt:find(Query#dns_message.additional) of
false ->
%% 使 EDNS OPT
Additional;
{ok, OptReq} ->
%% 使 EDNS OPT RR
UdpSize = dns_opt:udp_payload(OptReq),
DoBit = dns_opt:do_bit(OptReq),
OptResp = dns_opt:make(UdpSize, DoBit),
%% OPT
Additional2 = [RR || RR <- Additional, RR#dns_rr.type =/= opt],
%% OPT Additional
Additional2 ++ [OptResp]
end.
adjust_ttl(RR = #dns_rr{}, RemainingTTL) ->
RR#dns_rr{ttl = max(0, RemainingTTL)};
adjust_ttl(RR, _RemainingTTL) ->
RR.

View File

@ -1,46 +0,0 @@
%%--------------------------------------------------------------------
%% EDNS (OPT RR) Utility for dns_erlang
%%--------------------------------------------------------------------
-module(dns_opt).
-export([find/1, make/2, udp_payload/1, do_bit/1]).
-include_lib("dns_erlang/include/dns.hrl").
%%--------------------------------------------------------------------
%% OPT RRRR type = opt
%%--------------------------------------------------------------------
find(RRs) ->
case lists:dropwhile(fun(RR) -> RR#dns_rr.type =/= opt end, RRs) of
[] ->
false;
[RR|_] ->
{ok, RR}
end.
%%--------------------------------------------------------------------
%% DO bitTTL bit
%%--------------------------------------------------------------------
do_bit(RR) ->
(RR#dns_rr.ttl band 16#8000) =/= 0.
%%--------------------------------------------------------------------
%% UDP payload size class
%%--------------------------------------------------------------------
udp_payload(RR) ->
RR#dns_rr.class.
%%--------------------------------------------------------------------
%% OPT RR RFC6891
%% make(UdpPayloadSize, DoBit)
%%--------------------------------------------------------------------
make(UdpPayloadSize, DoBit) ->
TTL = if DoBit -> 16#8000; true -> 0 end,
#dns_rr{
name = <<>>, % Root label
type = opt,
class = UdpPayloadSize,
ttl = TTL,
% No EDNS options by default
data = []
}.