Skip to content

Commit

Permalink
style(dns): style and typo fixes for dns client (#13389)
Browse files Browse the repository at this point in the history
KAG-5001

two blank lines between blocks
optimize table.insert
family should compare to domain, not name.

---------

Co-authored-by: Xiaochen Wang <wangxiaochen0@gmail.com>
  • Loading branch information
2 people authored and pull[bot] committed Jan 23, 2025
1 parent 97b341c commit c59098b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
2 changes: 1 addition & 1 deletion kong/dns/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ We evaluated the capacity of DNS records using the following resources:
* Record: ~80 bytes json string, e.g., `{address = "127.0.0.1", name = <domain>, ttl = 3600, class = 1, type = 1}`.
* Domain: ~36 bytes string, e.g., `example<n>.long.long.long.long.test`. Domain names with lengths between 10 and 36 bytes yield similar results.

The results of ) are as follows:
The results of evaluation are as follows:

| shared memory size | number of records per response | number of loaded responses |
|--------------------|-------------------|----------|
Expand Down
23 changes: 16 additions & 7 deletions kong/dns/utils.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
local utils = require("kong.resty.dns.utils")


local log = ngx.log


local NOTICE = ngx.NOTICE


local type = type
local ipairs = ipairs
local tonumber = tonumber
Expand All @@ -12,24 +15,28 @@ local table_clear = require("table.clear")
local table_insert = table.insert
local table_remove = table.remove


local readlines = require("pl.utils").readlines


local DEFAULT_HOSTS_FILE = "/etc/hosts"
local DEFAULT_RESOLV_CONF = "/etc/resolv.conf"


local LOCALHOST = {
ipv4 = "127.0.0.1",
ipv6 = "[::1]",
}

local DEFAULT_HOSTS = { localhost = LOCALHOST }

local DEFAULT_HOSTS = { localhost = LOCALHOST, }


local _M = {}


-- checks the hostname type
-- @return "ipv4", "ipv6", or "name"
-- @return "ipv4", "ipv6", or "domain"
function _M.hostname_type(name)
local remainder, colons = name:gsub(":", "")
if colons > 1 then
Expand All @@ -47,7 +54,7 @@ end
-- parses a hostname with an optional port
-- IPv6 addresses are always returned in square brackets
-- @param name the string to check (this may contain a port number)
-- @return `name/ip` + `port (or nil)` + `type ("ipv4", "ipv6" or "name")`
-- @return `name/ip` + `port (or nil)` + `type ("ipv4", "ipv6" or "domain")`
function _M.parse_hostname(name)
local t = _M.hostname_type(name)
if t == "ipv4" or t == "domain" then
Expand Down Expand Up @@ -86,20 +93,22 @@ function _M.parse_hosts(path, enable_ipv6)
for _, line in ipairs(lines) do
-- Remove leading/trailing whitespaces and split by whitespace
local parts = {}
local n = 0
for part in line:gmatch("%S+") do
if part:sub(1, 1) == '#' then
break
end

table_insert(parts, part:lower())
n = n + 1
parts[n] = part:lower()
end

-- Check if the line contains an IP address followed by hostnames
if #parts >= 2 then
if n >= 2 then
local ip, _, family = _M.parse_hostname(parts[1])

if family ~= "name" then -- ipv4/ipv6
for i = 2, #parts do
if family ~= "domain" then -- ipv4/ipv6
for i = 2, n do
local host = parts[i]
local v = hosts[host]

Expand Down
48 changes: 48 additions & 0 deletions spec/01-unit/30-new-dns-client/01-utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,54 @@ describe("[utils]", function ()
end)
end)

describe("parsing hostname", function ()
it("hostname_type()", function ()
assert.equal(utils.hostname_type("10.0.0.1"), "ipv4")
assert.equal(utils.hostname_type("127.0.0.1"), "ipv4")

assert.equal(utils.hostname_type("::1"), "ipv6")
assert.equal(utils.hostname_type("[::1]"), "ipv6")
assert.equal(utils.hostname_type("2001:db8::1"), "ipv6")
assert.equal(utils.hostname_type("[2001:db8::1]"), "ipv6")

assert.equal(utils.hostname_type("localhost"), "domain")
assert.equal(utils.hostname_type("example.test"), "domain")
assert.equal(utils.hostname_type("example.org"), "domain")
assert.equal(utils.hostname_type("example.com"), "domain")
assert.equal(utils.hostname_type("10.0.0.1.example.test"), "domain")
end)

it("parse_hostname()", function ()
local function check(name, expected_name, expected_port, expected_name_type)
local name_ip, port, name_type = utils.parse_hostname(name)

assert.equal(name_ip, expected_name, "checking the returned name/ip of " .. name)
assert.equal(port, expected_port, "checking the returned port of " .. name)
assert.equal(name_type, expected_name_type, "checking the returned type of " .. name)
end

check("127.0.0.1", "127.0.0.1", nil, "ipv4")
check("127.0.0.1:", "127.0.0.1", nil, "ipv4")
check("127.0.0.1:0", "127.0.0.1", 0, "ipv4")
check("127.0.0.1:80", "127.0.0.1", 80, "ipv4")

check("::1", "[::1]", nil, "ipv6")
check("[::1]:", "[::1]", nil, "ipv6")
check("[::1]:0", "[::1]", 0, "ipv6")
check("[::1]:80", "[::1]", 80, "ipv6")

check("www.example.test", "www.example.test", nil, "domain")
check("www.example.test:", "www.example.test", nil, "domain")
check("www.example.test:0", "www.example.test", 0, "domain")
check("www.example.test:80", "www.example.test", 80, "domain")

check("localhost", "localhost", nil, "domain")
check("localhost:", "localhost", nil, "domain")
check("localhost:0", "localhost", 0, "domain")
check("localhost:80", "localhost", 80, "domain")
end)
end)

describe("ipv6_bracket()", function ()
it("IPv6 address", function ()
assert.equal(utils.ipv6_bracket("::1"), "[::1]")
Expand Down

0 comments on commit c59098b

Please sign in to comment.