I don't know about by location, but by address should be possible using gai.conf
, assuming the application you are looking at is doing normal lookups using getaddrinfo
and that it's acceptable that this prioritization affects all lookups by applications running on that same machine.
If we have for instance these records in DNS:
test.example.com. 3600 IN AAAA 2001:db8:1::1
test.example.com. 3600 IN AAAA 2001:db8:ffff::1
test.example.com. 3600 IN A 192.0.2.1
test.example.com. 3600 IN A 198.51.100.1
And test the getaddrinfo
behavior using getent ahosts
, you get something like this with the default configuration:
$ getent ahosts test.example.com
2001:db8:1::1 STREAM test.example.com
2001:db8:1::1 DGRAM
2001:db8:1::1 RAW
2001:db8:ffff::1 STREAM
2001:db8:ffff::1 DGRAM
2001:db8:ffff::1 RAW
192.0.2.1 STREAM
192.0.2.1 DGRAM
192.0.2.1 RAW
198.51.100.1 STREAM
198.51.100.1 DGRAM
198.51.100.1 RAW
$
(With the records jumping around randomly among the v6 and v4 alternatives respectively if you run the command multiple times.)
If we say that your favorite addresses are 2001:db8:ffff::1
and 198.51.100.1
(based on being closer, more reliable, or whatever) you could put something like this in gai.conf
:
precedence ::ffff:198.51.100.1/128 99
precedence 2001:db8:ffff::1/128 100
And to not remove the default behavior also include:
precedence ::1/128 50
precedence ::/0 40
precedence 2002::/16 30
precedence ::/96 20
precedence ::ffff:0:0/96 10
Then you would get:
$ getent ahosts test.example.com
2001:db8:ffff::1 STREAM test.example.com
2001:db8:ffff::1 DGRAM
2001:db8:ffff::1 RAW
198.51.100.1 STREAM
198.51.100.1 DGRAM
198.51.100.1 RAW
2001:db8:1::1 STREAM
2001:db8:1::1 DGRAM
2001:db8:1::1 RAW
192.0.2.1 STREAM
192.0.2.1 DGRAM
192.0.2.1 RAW
$