| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5112315 commit 22efc05
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -207,6 +207,23 @@ Local<Array> AddrTTLToArray( | |||
| 207 | 207 | return Array::New(env->isolate(), ttls.out(), naddrttls); | |
| 208 | 208 | } | |
| 209 | 209 | ||
| 210 | + int GetAnswerCountForTTLBuffer(const unsigned char* buf, int len) { | ||
| 211 | + static constexpr int kDNSAnswerCountOffset = 6; | ||
| 212 | + static constexpr int kAresDefaultTTLBufferLength = 256; | ||
| 213 | + if (len <= kDNSAnswerCountOffset + 1) { | ||
| 214 | + return kAresDefaultTTLBufferLength; | ||
| 215 | + } | ||
| 216 | + | ||
| 217 | + const int answer_count = (static_cast<int>(buf[kDNSAnswerCountOffset]) << 8) | | ||
| 218 | + static_cast<int>(buf[kDNSAnswerCountOffset + 1]); | ||
| 219 | + return answer_count == 0 ? 1 : answer_count; | ||
| 220 | + } | ||
| 221 | + | ||
| 222 | + template <typename T> | ||
| 223 | + std::vector<T> MakeAddrTTLBuffer(const unsigned char* buf, int len) { | ||
| 224 | + return std::vector<T>(GetAnswerCountForTTLBuffer(buf, len)); | ||
| 225 | + } | ||
| 226 | + | ||
| 210 | 227 | Maybe<int> ParseGeneralReply(Environment* env, | |
| 211 | 228 | const unsigned char* buf, | |
| 212 | 229 | int len, | |
@@ -1068,11 +1085,12 @@ Maybe<int> AnyTraits::Parse(QueryAnyWrap* wrap, | |||
| 1068 | 1085 | int type, status, old_count; | |
| 1069 | 1086 | ||
| 1070 | 1087 | /* Parse A records or CNAME records */ | |
| 1071 | - ares_addrttl addrttls[256]; | ||
| 1072 | - int naddrttls = arraysize(addrttls); | ||
| 1088 | + std::vector<ares_addrttl> addrttls = | ||
| 1089 | + MakeAddrTTLBuffer<ares_addrttl>(buf, len); | ||
| 1090 | + int naddrttls = static_cast<int>(addrttls.size()); | ||
| 1073 | 1091 | ||
| 1074 | 1092 | type = ns_t_cname_or_a; | |
| 1075 | - if (!ParseGeneralReply(env, buf, len, &type, ret, addrttls, &naddrttls) | ||
| 1093 | + if (!ParseGeneralReply(env, buf, len, &type, ret, addrttls.data(), &naddrttls) | ||
| 1076 | 1094 | .To(&status)) { | |
| 1077 | 1095 | return Nothing<int>(); | |
| 1078 | 1096 | } | |
@@ -1114,11 +1132,13 @@ Maybe<int> AnyTraits::Parse(QueryAnyWrap* wrap, | |||
| 1114 | 1132 | } | |
| 1115 | 1133 | ||
| 1116 | 1134 | /* Parse AAAA records */ | |
| 1117 | - ares_addr6ttl addr6ttls[256]; | ||
| 1118 | - int naddr6ttls = arraysize(addr6ttls); | ||
| 1135 | + std::vector<ares_addr6ttl> addr6ttls = | ||
| 1136 | + MakeAddrTTLBuffer<ares_addr6ttl>(buf, len); | ||
| 1137 | + int naddr6ttls = static_cast<int>(addr6ttls.size()); | ||
| 1119 | 1138 | ||
| 1120 | 1139 | type = ns_t_aaaa; | |
| 1121 | - if (!ParseGeneralReply(env, buf, len, &type, ret, addr6ttls, &naddr6ttls) | ||
| 1140 | + if (!ParseGeneralReply( | ||
| 1141 | + env, buf, len, &type, ret, addr6ttls.data(), &naddr6ttls) | ||
| 1122 | 1142 | .To(&status)) { | |
| 1123 | 1143 | return Nothing<int>(); | |
| 1124 | 1144 | } | |
@@ -1270,20 +1290,22 @@ Maybe<int> ATraits::Parse(QueryAWrap* wrap, | |||
| 1270 | 1290 | HandleScope handle_scope(env->isolate()); | |
| 1271 | 1291 | Context::Scope context_scope(env->context()); | |
| 1272 | 1292 | ||
| 1273 | - ares_addrttl addrttls[256]; | ||
| 1274 | - int naddrttls = arraysize(addrttls), status; | ||
| 1293 | + std::vector<ares_addrttl> addrttls = | ||
| 1294 | + MakeAddrTTLBuffer<ares_addrttl>(buf, len); | ||
| 1295 | + int naddrttls = static_cast<int>(addrttls.size()), status; | ||
| 1275 | 1296 | Local<Array> ret = Array::New(env->isolate()); | |
| 1276 | 1297 | ||
| 1277 | 1298 | int type = ns_t_a; | |
| 1278 | - if (!ParseGeneralReply(env, buf, len, &type, ret, addrttls, &naddrttls) | ||
| 1299 | + if (!ParseGeneralReply(env, buf, len, &type, ret, addrttls.data(), &naddrttls) | ||
| 1279 | 1300 | .To(&status)) { | |
| 1280 | 1301 | return Nothing<int>(); | |
| 1281 | 1302 | } | |
| 1282 | 1303 | if (status != ARES_SUCCESS) { | |
| 1283 | 1304 | return Just<int>(status); | |
| 1284 | 1305 | } | |
| 1285 | 1306 | ||
| 1286 | - Local<Array> ttls = AddrTTLToArray<ares_addrttl>(env, addrttls, naddrttls); | ||
| 1307 | + Local<Array> ttls = | ||
| 1308 | + AddrTTLToArray<ares_addrttl>(env, addrttls.data(), naddrttls); | ||
| 1287 | 1309 | ||
| 1288 | 1310 | wrap->CallOnComplete(ret, ttls); | |
| 1289 | 1311 | return Just<int>(ARES_SUCCESS); | |
@@ -1302,20 +1324,22 @@ Maybe<int> AaaaTraits::Parse(QueryAaaaWrap* wrap, | |||
| 1302 | 1324 | HandleScope handle_scope(env->isolate()); | |
| 1303 | 1325 | Context::Scope context_scope(env->context()); | |
| 1304 | 1326 | ||
| 1305 | - ares_addr6ttl addrttls[256]; | ||
| 1306 | - int naddrttls = arraysize(addrttls), status; | ||
| 1327 | + std::vector<ares_addr6ttl> addrttls = | ||
| 1328 | + MakeAddrTTLBuffer<ares_addr6ttl>(buf, len); | ||
| 1329 | + int naddrttls = static_cast<int>(addrttls.size()), status; | ||
| 1307 | 1330 | Local<Array> ret = Array::New(env->isolate()); | |
| 1308 | 1331 | ||
| 1309 | 1332 | int type = ns_t_aaaa; | |
| 1310 | - if (!ParseGeneralReply(env, buf, len, &type, ret, addrttls, &naddrttls) | ||
| 1333 | + if (!ParseGeneralReply(env, buf, len, &type, ret, addrttls.data(), &naddrttls) | ||
| 1311 | 1334 | .To(&status)) { | |
| 1312 | 1335 | return Nothing<int>(); | |
| 1313 | 1336 | } | |
| 1314 | 1337 | if (status != ARES_SUCCESS) { | |
| 1315 | 1338 | return Just<int>(status); | |
| 1316 | 1339 | } | |
| 1317 | 1340 | ||
| 1318 | - Local<Array> ttls = AddrTTLToArray<ares_addr6ttl>(env, addrttls, naddrttls); | ||
| 1341 | + Local<Array> ttls = | ||
| 1342 | + AddrTTLToArray<ares_addr6ttl>(env, addrttls.data(), naddrttls); | ||
| 1319 | 1343 | ||
| 1320 | 1344 | wrap->CallOnComplete(ret, ttls); | |
| 1321 | 1345 | return Just<int>(ARES_SUCCESS); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,69 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + const dnstools = require('../common/dns'); | ||
| 4 | + const assert = require('assert'); | ||
| 5 | + const dgram = require('dgram'); | ||
| 6 | + const dns = require('dns'); | ||
| 7 | + | ||
| 8 | + const dnsPromises = dns.promises; | ||
| 9 | + | ||
| 10 | + const kRecordCount = 257; | ||
| 11 | + const kADomain = 'many-a.example.org'; | ||
| 12 | + | ||
| 13 | + const server = dgram.createSocket('udp4'); | ||
| 14 | + | ||
| 15 | + server.on('message', common.mustCall((msg, { address, port }) => { | ||
| 16 | + const parsed = dnstools.parseDNSPacket(msg); | ||
| 17 | + const question = parsed.questions[0]; | ||
| 18 | + const { domain } = question; | ||
| 19 | + | ||
| 20 | + assert.strictEqual(question.type, 'ANY'); | ||
| 21 | + assert.strictEqual(domain, kADomain); | ||
| 22 | + | ||
| 23 | + server.send(dnstools.writeDNSPacket({ | ||
| 24 | + id: parsed.id, | ||
| 25 | + questions: parsed.questions, | ||
| 26 | + answers: createARecords(domain), | ||
| 27 | + }), port, address); | ||
| 28 | + }, 2)); | ||
| 29 | + | ||
| 30 | + server.bind(0, common.mustCall(async () => { | ||
| 31 | + const { port } = server.address(); | ||
| 32 | + const callbackResolver = new dns.Resolver({ timeout: 1000, tries: 1 }); | ||
| 33 | + const promiseResolver = new dnsPromises.Resolver({ timeout: 1000, tries: 1 }); | ||
| 34 | + callbackResolver.setServers([`127.0.0.1:${port}`]); | ||
| 35 | + promiseResolver.setServers([`127.0.0.1:${port}`]); | ||
| 36 | + | ||
| 37 | + validateRecords(await promiseResolver.resolveAny(kADomain), 'A'); | ||
| 38 | + validateRecords(await resolveAny(callbackResolver, kADomain), 'A'); | ||
| 39 | + | ||
| 40 | + server.close(); | ||
| 41 | + })); | ||
| 42 | + | ||
| 43 | + function createARecords(domain) { | ||
| 44 | + return Array.from({ length: kRecordCount }, (_, i) => ({ | ||
| 45 | + type: 'A', | ||
| 46 | + address: `10.0.${i >> 8}.${i & 0xff}`, | ||
| 47 | + ttl: 60 + i, | ||
| 48 | + domain, | ||
| 49 | + })); | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + function resolveAny(resolver, domain) { | ||
| 53 | + return new Promise((resolve) => { | ||
| 54 | + resolver.resolveAny(domain, common.mustSucceed(resolve)); | ||
| 55 | + }); | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + function validateRecords(records, type) { | ||
| 59 | + assert.strictEqual(records.length, kRecordCount); | ||
| 60 | + for (const record of records) { | ||
| 61 | + assert.strictEqual(record.type, type); | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + assert.strictEqual(records[0].ttl, 60); | ||
| 65 | + assert.strictEqual(records[255].ttl, 315); | ||
| 66 | + assert.strictEqual(records[256].ttl, 316); | ||
| 67 | + assert.strictEqual(records[0].address, '10.0.0.0'); | ||
| 68 | + assert.strictEqual(records[256].address, '10.0.1.0'); | ||
| 69 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments