Mercurial > prosody-hg
comparison util/dns.lua @ 12289:3a655adf1d0d
util.dns: Remove compat for pre-0.11 lack of inet_ntop binding
The inet_ntop binding was added in 8b612ec00e4a and included in 0.11.0
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Tue, 15 Feb 2022 13:04:24 +0100 |
| parents | 08a933450922 |
| children | a0ff5c438e9d |
comparison
equal
deleted
inserted
replaced
| 12288:08a933450922 | 12289:3a655adf1d0d |
|---|---|
| 8 local table = table; | 8 local table = table; |
| 9 local t_concat = table.concat; | 9 local t_concat = table.concat; |
| 10 local t_insert = table.insert; | 10 local t_insert = table.insert; |
| 11 local s_byte = string.byte; | 11 local s_byte = string.byte; |
| 12 local s_format = string.format; | 12 local s_format = string.format; |
| 13 local s_gsub = string.gsub; | |
| 14 local s_sub = string.sub; | 13 local s_sub = string.sub; |
| 15 local s_match = string.match; | |
| 16 local s_gmatch = string.gmatch; | |
| 17 | |
| 18 local have_net, net_util = pcall(require, "util.net"); | |
| 19 | 14 |
| 20 local iana_data = require "util.dnsregistry"; | 15 local iana_data = require "util.dnsregistry"; |
| 21 if have_net and not net_util.ntop then -- Added in Prosody 0.11 | |
| 22 have_net = false; | |
| 23 end | |
| 24 | |
| 25 local tohex = require "util.hex".to; | 16 local tohex = require "util.hex".to; |
| 17 local inet_ntop = require "util.net".ntop; | |
| 26 | 18 |
| 27 -- Simplified versions of Waqas DNS parsers | 19 -- Simplified versions of Waqas DNS parsers |
| 28 -- Only the per RR parsers are needed and only feed a single RR | 20 -- Only the per RR parsers are needed and only feed a single RR |
| 29 | 21 |
| 30 local parsers = {}; | 22 local parsers = {}; |
| 76 expire = e1*0x1000000 + e2*0x10000 + e3*0x100 + e4; | 68 expire = e1*0x1000000 + e2*0x10000 + e3*0x100 + e4; |
| 77 minimum = m1*0x1000000 + m2*0x10000 + m3*0x100 + m4; | 69 minimum = m1*0x1000000 + m2*0x10000 + m3*0x100 + m4; |
| 78 }, soa_mt); | 70 }, soa_mt); |
| 79 end | 71 end |
| 80 | 72 |
| 81 function parsers.A(packet) | 73 parsers.A = inet_ntop; |
| 82 return s_format("%d.%d.%d.%d", s_byte(packet, 1, 4)); | 74 parsers.AAAA = inet_ntop; |
| 83 end | |
| 84 | |
| 85 local aaaa = { nil, nil, nil, nil, nil, nil, nil, nil, }; | |
| 86 function parsers.AAAA(packet) | |
| 87 local hi, lo, ip, len, token; | |
| 88 for i = 1, 8 do | |
| 89 hi, lo = s_byte(packet, i * 2 - 1, i * 2); | |
| 90 aaaa[i] = s_format("%x", hi * 256 + lo); -- skips leading zeros | |
| 91 end | |
| 92 ip = t_concat(aaaa, ":", 1, 8); | |
| 93 len = (s_match(ip, "^0:[0:]+()") or 1) - 1; | |
| 94 for s in s_gmatch(ip, ":0:[0:]+") do | |
| 95 if len < #s then len, token = #s, s; end -- find longest sequence of zeros | |
| 96 end | |
| 97 return (s_gsub(ip, token or "^0:[0:]+", "::", 1)); | |
| 98 end | |
| 99 | |
| 100 if have_net then | |
| 101 parsers.A = net_util.ntop; | |
| 102 parsers.AAAA = net_util.ntop; | |
| 103 end | |
| 104 | 75 |
| 105 local mx_mt = { | 76 local mx_mt = { |
| 106 __tostring = function(rr) | 77 __tostring = function(rr) |
| 107 return s_format("%d %s", rr.pref, rr.mx) | 78 return s_format("%d %s", rr.pref, rr.mx) |
| 108 end | 79 end |
