|
NetBurner 3.5.8
PDF Version |
DNS turns a name such as www.netburner.com into an IP address your application can connect to. The NetBurner API offers a blocking call for simple cases and a non-blocking pair for applications built around select().
This page covers what a lookup actually does, so you can size timeouts, read a packet capture, and choose between the two interfaces.
Use GetHostByName() unless your task already runs a select() loop and cannot afford to block. The non-blocking pair gives you the socket file descriptor so you can add it to your own fd_set.
By default a lookup asks for both an IPv4 address and an IPv6 address, and returns whichever the host publishes. On an IPv4 only build it asks for IPv4 alone. This suits most applications, which simply want a usable address.
The TYPE and TYPE2 parameters narrow the request. Pass the same type twice to ask for one record type:
Pass DNS_AAAA as both types to ask for IPv6 alone. The available record types are listed under DNS Record Types.
The same two parameters apply to fd_dns_part1(), and the types passed there must match the types passed to fd_dns_processresult().
The timeout you pass is a total timeout for the whole lookup, not a per-query value. Within that budget the resolver retransmits an outstanding query on a backoff, so a query lost in transit is re-sent rather than costing the entire timeout.
Size the budget for the round trip you expect plus room for at least one retry. Ten seconds is reasonable for a public resolver, less for one on the LAN.
| Code | Meaning |
|---|---|
| DNS_OK | A usable address was returned. |
| DNS_NOSUCHNAME | The server answered, and the name has no record of the requested type. |
| DNS_TIMEOUT | No usable answer arrived within the budget. The server may be unreachable, or the name may be unresolvable. |
| DNS_ERR | The request could not be made, for example a null name. |
The distinction between DNS_NOSUCHNAME and DNS_TIMEOUT is worth reading carefully when diagnosing a failure. DNS_NOSUCHNAME means something replied. DNS_TIMEOUT means the timeout expired.
DNS needs more than a link. The interface needs an active link and a DNS server address. It needs a gateway address as well if the DNS server is on another subnet.
WaitForActiveNetwork() waits for link only. An application that resolves a name immediately after startup should check the rest:
Pass IPADDR::NullIP() to use the DNS server the interface already has, whether it came from DHCP or static configuration. This is what most applications want.
GetHostByNameViaIfNum() restricts the query to one interface on a multi-homed device.
Successful lookups are cached, so a repeated lookup of the same name returns immediately without network traffic. Entries expire according to the TTL supplied by the DNS server.
A name resolved a moment ago costs nothing to resolve again, so applications are free to call GetHostByName() at the point of use rather than caching the address themselves.
An IP address written as text needs no lookup at all. GetHostByName("10.1.1.50", ...) returns that address directly, so a setting that accepts either a name or an address can be passed straight through.
The cache is enabled by DNS_CACHE in predef.h, and its size is set by NUM_DNS_CACHE in constants.h.
A name ending in .local is not sent to your DNS server. It is resolved by multicast DNS: the query goes to the multicast group 224.0.0.251 on port 5353, and any host on the local network that owns the name answers.
This means .local lookups work with no DNS server configured at all, and equally that a correctly configured DNS server has no effect on them. Only IPv4 .local lookups are supported.
fd_dns_part1() sends the query and returns a file descriptor to add to your fd_set. When select() reports the descriptor readable, fd_dns_processresult() parses whatever arrived and returns true once it has an address.
Three responsibilities move to you with this interface:
Start with the interface.** CanInterfaceDoDNS() above tests the prerequisites. A missing DNS server address is the common cause, followed by a missing gateway when the DNS server is on another subnet.
Read the return code.** DNS_TIMEOUT points at reachability, DNS_NOSUCHNAME means a server answered and the record does not exist.
Test with a name you have not resolved yet,** so the answer comes from the network rather than the cache.
Narrow the request to one record type** with DNS_A, DNS_A to confirm IPv4 resolution on its own.