-
Notifications
You must be signed in to change notification settings - Fork 0
DNS Resolver
DNS resolver for hostname-to-address lookups, bridging DHCP-configured nameservers to application network calls.
JNode's DNS resolver is a singleton service that translates hostnames into IPv4 addresses. It integrates with DHCP for dynamic server configuration and uses the dnsjava library (xbill) for protocol handling. Applications access name resolution through Resolver.getByName(), which is called from NetAPIImpl and the IPv4 network layer.
| Class/File | Location | Purpose |
|---|---|---|
Resolver |
net/src/net/org/jnode/net/Resolver.java |
Public interface for name resolution |
ResolverImpl |
net/src/net/org/jnode/net/ipv4/util/ResolverImpl.java |
Singleton implementation using dnsjava |
DHCPClient |
net/src/net/org/jnode/net/ipv4/dhcp/DHCPClient.java |
Configures DNS servers from DHCP lease |
IPv4NetworkLayer |
net/src/net/org/jnode/net/ipv4/layer/IPv4NetworkLayer.java |
Uses Resolver for address resolution |
NetAPIImpl |
net/src/net/org/jnode/net/service/NetAPIImpl.java |
Exposes Resolver to application layer |
The Resolver interface defines two operations:
public interface Resolver {
ProtocolAddress[] getByName(String hostname) throws UnknownHostException;
String[] getByAddress(ProtocolAddress address) throws UnknownHostException;
}The first method resolves a hostname to IP addresses; the second (reverse DNS) is stubbed out.
ResolverImpl is a @SharedStatics class implementing the Resolver interface. It uses the dnsjava library (org.xbill.DNS) for DNS protocol operations. The singleton is obtained via getInstance():
@SharedStatics
public class ResolverImpl implements Resolver {
private static ExtendedResolver resolver;
private static Map<String, org.xbill.DNS.Resolver> resolvers;
private static Map<String, ProtocolAddress[]> hosts; // /etc/hosts-like
private static Resolver res = null;
public static Resolver getInstance() {
if (res == null) {
AccessController.doPrivileged(new PrivilegedAction<Object>() {
public Object run() {
System.setProperty("dns.server", "127.0.0.1");
System.setProperty("dns.search", "localdomain");
return null;
}
});
res = new ResolverImpl();
}
return res;
}
}sequenceDiagram
participant App as Application
participant NetAPI as NetAPIImpl / IPv4NetworkLayer
participant Resolver as ResolverImpl
participant DNS as dnsjava (ExtendedResolver)
App->>NetAPI: getByName("example.com")
NetAPI->>Resolver: ResolverImpl.getInstance().getByName(hostname)
alt hostname in hosts map
Resolver->>Resolver: return cached IPv4Address[]
else DNS query
Resolver->>DNS: Lookup.setDefaultResolver(resolver)
Resolver->>DNS: new Lookup(hostname)
DNS->>DNS: lookup.run()
alt SUCCESS
DNS-->>Resolver: Record[] answers
Resolver->>Resolver: convert to ProtocolAddress[]
else FAILURE
Resolver--xNetAPI: UnknownHostException
end
end
NetAPI-->>App: IPv4Address[]
DNS servers are added dynamically via addDnsServer():
public static void addDnsServer(ProtocolAddress _dnsserver) throws NetworkException {
if (resolver == null) {
final String[] server = new String[] {_dnsserver.toString()};
AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
public Object run() throws Exception {
resolver = new ExtendedResolver(server);
Lookup.setDefaultResolver(resolver);
return null;
}
});
}
// Add to ExtendedResolver for failover
SimpleResolver simpleResolver = new SimpleResolver(key);
resolver.addResolver(simpleResolver);
}When DHCP assigns a lease, DHCPClient extracts DNS server addresses and adds them:
final byte[] dnsValue = msg.getOption(DHCPMessage.DNS_OPTION);
if (dnsValue != null) {
for (int i = 0; i < dnsValue.length; i += 4) {
final IPv4Address dnsIP = new IPv4Address(dnsValue, i);
try {
ResolverImpl.addDnsServer(dnsIP);
} catch (NetworkException ex) {
log.error("Failed to configure DNS server");
}
}
}-
getByAddressis stubbed — The reverse DNS method returns an empty array. This meansInetAddress.getByAddress()won't produce a hostname. -
Hardcoded default DNS — On first access,
getInstance()setsdns.server=127.0.0.1anddns.search=localdomain. If no servers are configured, lookups fail. -
No DNS cache — Each
getByName()call queries the DNS server. There is no TTL-based caching layer. -
@SharedStaticsusage — The class is marked@SharedStatics, meaning static fields are shared across isolates. This is appropriate since DNS is a system-wide service. -
dnsjava version — The code references
org.xbill.DNS.ExtendedResolverfrom the dnsjava library. The FIXME comment notes the library may need upgrading. -
Hosts file is minimal — Only
localhostis pre-populated in thehostsmap. There is no/etc/hostsfile parsing. -
Singleton pattern inconsistency — The FIXME notes that management methods are
staticwhile state is static, but the class isn't a proper singleton (no private constructor enforcement, multiple static maps).
- Network-Stack — High-level network architecture
- NetAPI-Implementation — Application-facing network API including DNS
- IPv4-Protocol — IPv4 packet processing
- DHCP — DHCP client that configures DNS servers (DHCPClient link TBD)