Setting a MX record in the local DNS?

For @hyper_ch and anyone wanting a simple MX override, here’s an example for knot-resolver’s config:

modules = { 'policy' }


local mydname = todname('some.domain.test')

local function ans_mx(state, req)
	local qry = req:current()
	local answer = req.answer

	local is_exact = ffi.C.knot_dname_is_equal(qry.sname, mydname)
	if (not is_exact) or qry.stype ~= kres.type.MX then
		return nil
	end

	ffi.C.kr_pkt_make_auth_header(answer)
	answer:rcode(kres.rcode.NOERROR)
	answer:begin(kres.section.ANSWER)

	-- You can repeat these lines multiple times to have multiple MX records.
	local mx_prio = 10
	local mx_TTL = 900
	answer:put(qry.sname, mx_TTL, answer:qclass(), kres.type.MX,
		string.char(mx_prio / 256, mx_prio % 256)
		.. todname('my.mx.target'))

	return kres.DONE
end

policy.add(policy.suffix(ans_mx, {mydname}))

For adding custom config see the wiki.

Note: knot-resolver isn’t meant for authoritative data, so this example only answers direct queries and doesn’t in any way change other related queries, e.g. if this addition created an empty non-terminal, it might be “incorrectly” answering NXDOMAIN for it, etc.