Monday, September 5, 2011

Name resolution testing with your APR app

Consider a typical network client or server which takes a server name as parameter, converts it to a list of socket addresses using apr_sockaddr_info_get(), and then proceeds by either listening on all of the returned addresses or attempting to connect to them in succession until arriving at one which works.

While developing this program logic to process the list of returned addresses it may be useful to carefully control the apr_sockaddr_info_get() results without having to muck with host files, resolver configuration files, or (shudder) the DNS server, especially if the test scenarios need to be replicated on multiple platforms.

I developed a small patch to APR to control resolution with an envvar. (The patch applies to r1165471 or so of the 1.4.x branch, and presumably to the recent 1.4.5 release.) With a build of APR which includes the patch, the environment variable APR_S_I_G can be set to a list of semicolon-delimited host definitions, each of which is a list of comma-delimited IPv4 or IPv6 numeric address strings, with square brackets around the IPv6 strings.


$ APR_S_I_G="myhost1=[::1],192.168.1.200;myhost2=[fe80::deadbeef],192.168.1.201" ./myproggie


A declaration of mixed IPv4 and IPv6 addresses will work only if the application passes APR_UNSPEC to apr_sockaddr_info_get(). If it passes a specific address family, declare a list of one or more addresses in that single address family.

Resolution proceeds normally if the hostname being resolved is not defined in the envvar.