Skip to content

Commit eda58f7

Browse files
committed
bugfix: when the AD and CD bits are set in the DNS responses as per RFC 2065, they would erroneously be treated as part of the error code (RCODE). thanks Celebi Lui for the report in #14 and the original patch in #15.
1 parent 5a527cf commit eda58f7

File tree

3 files changed

+102
-3
lines changed

3 files changed

+102
-3
lines changed

lib/resty/dns/resolver.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ local function parse_response(buf, id)
335335
return nil, "truncated"
336336
end
337337

338-
local code = band(flags, 0x7f)
338+
local code = band(flags, 0xf)
339339

340340
-- print(format("code: %d", code))
341341

t/TestDNS.pm

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,15 @@ sub gen_dns_reply ($$) {
120120
my $tc = $t->{tc} // 0;
121121
my $rd = $t->{rd} // 1;
122122
my $ra = $t->{ra} // 1;
123+
124+
my $ad = $t->{ad} // 0;
125+
my $cd = $t->{cd} // 0;
126+
123127
my $rcode = $t->{rcode} // 0;
124128

125-
my $flags = ($qr << 15) + ($opcode << 11) + ($aa << 10) + ($tc << 9) + ($rd << 8) + ($ra << 7) + $rcode;
129+
my $flags = ($qr << 15) + ($opcode << 11) + ($aa << 10) + ($tc << 9)
130+
+ ($rd << 8) + ($ra << 7) + ($ad << 4) + ($cd << 5) + $rcode;
131+
126132
#warn sprintf("flags: %b", $flags);
127133

128134
$flags = pack("n", $flags);

t/mock.t

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use Cwd qw(cwd);
66

77
repeat_each(2);
88

9-
plan tests => repeat_each() * (3 * blocks() + 14);
9+
plan tests => repeat_each() * (3 * blocks() + 16);
1010

1111
my $pwd = cwd();
1212

@@ -1421,3 +1421,96 @@ records: [{"class":1,"name":"www.google.com","ttl":123456,"txt":["hello","world!
14211421
--- no_error_log
14221422
[error]
14231423

1424+
1425+
1426+
=== TEST 30: single answer reply, good A answer (AD is set)
1427+
--- http_config eval: $::HttpConfig
1428+
--- config
1429+
location /t {
1430+
content_by_lua '
1431+
local resolver = require "resty.dns.resolver"
1432+
1433+
local r, err = resolver:new{
1434+
nameservers = { {"127.0.0.1", 1953} }
1435+
}
1436+
if not r then
1437+
ngx.say("failed to instantiate resolver: ", err)
1438+
return
1439+
end
1440+
1441+
r._id = 125
1442+
1443+
local ans, err = r:query("www.google.com", { qtype = r.TYPE_A })
1444+
if not ans then
1445+
ngx.say("failed to query: ", err)
1446+
return
1447+
end
1448+
1449+
local cjson = require "cjson"
1450+
ngx.say("records: ", cjson.encode(ans))
1451+
';
1452+
}
1453+
--- udp_listen: 1953
1454+
--- udp_reply dns
1455+
{
1456+
id => 125,
1457+
opcode => 0,
1458+
qname => 'www.google.com',
1459+
ad => 1,
1460+
answer => [{ name => "www.google.com", ipv4 => "127.0.0.1", ttl => 123456 }],
1461+
}
1462+
--- request
1463+
GET /t
1464+
--- udp_query eval
1465+
"\x{00}}\x{01}\x{00}\x{00}\x{01}\x{00}\x{00}\x{00}\x{00}\x{00}\x{00}\x{03}www\x{06}google\x{03}com\x{00}\x{00}\x{01}\x{00}\x{01}"
1466+
--- response_body
1467+
records: [{"address":"127.0.0.1","type":1,"class":1,"name":"www.google.com","ttl":123456}]
1468+
--- no_error_log
1469+
[error]
1470+
1471+
1472+
1473+
=== TEST 31: single answer reply, good A answer (CD is set)
1474+
--- http_config eval: $::HttpConfig
1475+
--- config
1476+
location /t {
1477+
content_by_lua '
1478+
local resolver = require "resty.dns.resolver"
1479+
1480+
local r, err = resolver:new{
1481+
nameservers = { {"127.0.0.1", 1953} }
1482+
}
1483+
if not r then
1484+
ngx.say("failed to instantiate resolver: ", err)
1485+
return
1486+
end
1487+
1488+
r._id = 125
1489+
1490+
local ans, err = r:query("www.google.com", { qtype = r.TYPE_A })
1491+
if not ans then
1492+
ngx.say("failed to query: ", err)
1493+
return
1494+
end
1495+
1496+
local cjson = require "cjson"
1497+
ngx.say("records: ", cjson.encode(ans))
1498+
';
1499+
}
1500+
--- udp_listen: 1953
1501+
--- udp_reply dns
1502+
{
1503+
id => 125,
1504+
opcode => 0,
1505+
qname => 'www.google.com',
1506+
cd => 1,
1507+
answer => [{ name => "www.google.com", ipv4 => "127.0.0.1", ttl => 123456 }],
1508+
}
1509+
--- request
1510+
GET /t
1511+
--- udp_query eval
1512+
"\x{00}}\x{01}\x{00}\x{00}\x{01}\x{00}\x{00}\x{00}\x{00}\x{00}\x{00}\x{03}www\x{06}google\x{03}com\x{00}\x{00}\x{01}\x{00}\x{01}"
1513+
--- response_body
1514+
records: [{"address":"127.0.0.1","type":1,"class":1,"name":"www.google.com","ttl":123456}]
1515+
--- no_error_log
1516+
[error]

0 commit comments

Comments
 (0)