OpenWrt Forum Archive

Topic: Proposal - patch for dynamically generated hosts file

The content of this topic has been archived on 9 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

I'm considering writing a patch that would cause a hosts file to be created containing the ip addressess, ifnames and hostnames of all network interfaces.

For example, consider an OpenWRT box with these NVRAM values set:

wan_hostname=MyRouter
wan_proto=dhcp
wan_ifname=vlan1
lan_hostname=MyLanRouter
lan_proto=static
lan_ifname=br0
lan_ipaddr=192.168.201.1
dmz_hostname=DMZGateway
dmz proto=static
dmz_ifname=vlan2
dmz_ipaddr=192.168.222.1
ifnames=dmz

Note that the "ifnames" NVRAM variable is new in Kamikaze.  It tells the network hotplug script which non-default interfaces to initialise.  lan, wan and wifi are initialised by default, according to their "<name>_proto=" variable.

The hotplug script would use a little awk routine to get the actual IP address of an interface so that interfaces configured via DHCP could also have their DHCP-assigned IP address in the hosts file.

If people want to add their own entries to their hosts file, they add them to /etc/hosts.static.  The contents of this file are appended to /tmp/hosts.localhost when it is created or updated.

/etc/hosts would be symlinked to /tmp/hosts.localhost.  After initialisation, /tmp/hosts.localhost would look like this:

#/tmp/hosts.localhost
#this file is dynamically generated by OpenWRT - don't bother editing it!
127.0.0.1         localhost
144.129.30.62     wan        MyRouter
192.168.201.1     lan        MyLanRouter
192.168.222.1     dmz        DMZGateway
#contents of /etc/hosts.static are automatically added below this line

Having a default configuration where the names "wan" and "lan" always point at the correct IP address would be very handy.  Many programs allow hostnames instead of ip addresses in their config files.  Being able to substitue "lan" instead of "192.168.1.1" or "br0" in a config file makes it possible to package programs with default settings that can withstand a wide range of hardware and custom network configs.

By default, dnsmasq reads the contents of /etc/hosts and makes the contents available for name resolution via it's dns server.  However, we would not want it to serve the contents of /tmp/hosts.localhost because the interface names (wan, lan, dmz) would not be appropriate for other dns clients on our network.  To solve this, the network initialisation script also genrates a file called /tmp/hosts.dnsmasq that looks like this:

#/tmp/hosts.dnsmasq
#this file is dynamically generated by OpenWRT - don't bother editing it!
144.129.30.62     MyRouter
192.168.201.1     MyLanRouter
192.168.222.1     DMZGateway

We would configure dnsmasq to read /tmp/hosts.dnsmasq and /etc/hosts.static but not /etc/hosts (the symlink to /tmp/hosts.localhost).

Of course a scheme like this won't be useful in every circumstance, but I think it'd make using openwrt a lot nicer and it opens the door for further configs based on hostname rather than interface name or IP address.

Before I spend time writing the patch, I'd like some feedback on the idea - from developers and users.  Cheers!

Many programs only resolve the hostname to an ip address and then use the ip address from that point on, ignoring any changes to the host file.

Yes, in that respect I suppose you'd be better off supplying an interface name (eth1, vlan0, etc).  But not all programs accept interface names, they want a hard-coded IP address.  So for those programs, a hostname might be an acceptable substitute.  A program that reads a hardcoded IP address from a config file is in no better position than if it had read a hostname - and in fact, the IP address in the config file would need to be updated if the interface it refers to changes it's IP address, whereas a hostname that is automatically updated has a better chance of being correct without needing to edit the config file - at the time of program execution, anyway.

It might be better if I released the patch as a package so people can try it without being forced to use it.

As ad-hoc mesh networks become more popular, I believe automatic naming will become more important - IP addressess will become more dynamic so people will need a static, non-IP way of referring to a particular node.  Using centralised DNS servers with manually updated hosts files will become impractical in large mesh networks.  A patch/package like this one hopefully will help enable dynamic IP addressing on a larger scale.

I've been following the development of Kamikaze - mainly by watching the timeline - https://dev.openwrt.org/timeline.  It's interesting to watch the developent - and I notice that the networking and hotplug scripts are still being perfected.  I might wait until things settle down before I have a go at the dynamic hosts file package.

The discussion might have continued from here.