Modify

Opened 5 years ago

Last modified 3 years ago

#12722 reopened enhancement

Feature request: dynamically add an "address=/hostname/ip" line for each hostname (on generated /tmp/etc/dnsmasq.conf)

Reported by: maniqui@… Owned by: developers
Priority: normal Milestone: Barrier Breaker 14.07
Component: packages Version: Trunk
Keywords: dnsmasq, subdomain, address, dhcp_domain_add Cc:

Description

Short version

Could you consider adding config_foreach dhcp_domain_add host to /etc/init.d/dnsmasq?
This will add an address option -one for each host defined in /etc/config/dhcp to /tmp/etc/dnsmasq.conf

If this short version doesn't make sense, then please, I will appreciate if you could keep reading. Thanks.


Background

I was looking on how to configure dnsmasq to resolve local network requests for subdomains for each hostname.
For example, to solve a request for projectA.webserver.domain.lan, where:

  • domain.lan is the local domain
  • webserver is a host (a computer on the network), which could be running a webserver like Apache, and that has a static lease (an static IP) configured in dnsmasq.
  • projectA is a subdomain that is configured as a virtual host on the webserver.

dnsmasq easily allows us, via the --address option, to "specify an IP address to return for any host in the given domains" (see man page).

Thus, adding something like this:

address=/webserver.domain.lan/192.168.0.2

to /etc/dnsmasq.conf would do the trick: any request to whatever.webserver.domain.lan will be resolved to 192.168.0.2.


Problem

Well, "problem" is that I'd like this {{address}} configuration option to be added automagically -one for each host with an static lease on my LAN- instead of having to do it manually (which I tried and worked flawlessly).


Findings while looking for a solution

1st finding: a command like ping webserver worked flawlessly, while a command like ping test.webserver: it would just return ping: unknown host test.webserver.

2nd finding: previous finding lead me to test a command like ping OpenWrt (where OpenWrt is the router hostname), and, as expected, it worked flawlessly. Then, I tested with a command like ping whatever.OpenWrt and -to my surprise, guess what?-- it worked flawlessly too. That is, I got a reply from 192.168.0.1

3rd finding: previous one led me to take a look at /tmp/etc/dnsmasq.conf, where I found this line at the very bottom:

address=/OpenWrt.domain.lan/192.168.0.1

which explained why dnsmasq solved requests to whatever.OpenWrt.

So, now, I could have simply added similar lines for other hostnames (address=/webserver.domain.lan/192.168.0.2, address=/laptop.domain.lan/192.168.0.3 and so on) and call it a day.
But no, I wanted to check how/where OpenWRT generated that line, and see if I could find a way to add similar lines automagically, one for each host.

4th finding: taking a peek at /etc/init.d/dnsmasq I found this on lines 505-509:

        # add own hostname                 
        [ $ADD_LOCAL_HOSTNAME -eq 1 ] && [ -n "$lanaddr" ] && {
                local hostname="$(uci_get system.@system[0].hostname)"
                dhcp_domain_add "" "${hostname:-OpenWrt}" "$lanaddr"  
        }    

which add the address line I mentioned above.
Also, that code led me to find the dhcp_domain_add() function and another place (line 494) where that function is called:

config_foreach dhcp_domain_add domain

which gave me the idea of trying a solution...


Solution

Step 1) I added this line to /etc/init.d/dnsmasq, just below line 494:
config_foreach dhcp_domain_add host

Step 2) I restarted dnsmasq.

I really didn't expect that to work, as a domain "object" is different (is it?) than host "object".
But guess what? It worked.
These lines were added to the generated /tmp/etc/dnsmasq.conf:

address=/webserver.domain.lan/192.168.0.2
ptr-record=2.0.168.192.in-addr.arpa,webserver.domain.lan
address=/laptop.domain.lan/192.168.0.3
ptr-record=3.0.168.192.in-addr.arpa,laptop.domain.lan
address=/desktop.domain.lan/192.168.0.4
ptr-record=4.0.168.192.in-addr.arpa,desktop.domain.lan
...
...

Then, I tested some stuff (ping, request a web page, etc) on different subdomains for each host and it Just worked :D


Drawbacks

The only drawback I found is pretty obvious: I had to hack /etc/init.d/dnsmasq.
I'm pretty new to OpenWRT (and networking, Linux, hacking, for that matter) so I don't know if there is a better way to achieve the same.
I know that modifying that file is asking for a "maintenance nightmare". In this case, the modification is just one line, but I'm pretty sure I will forget about it next time I update OpenWRT.


Final questions

1) Do you spot any other issue or drawback?

2) Is there a better solution for what I tried to achieve? (mainly, solving DNS requests for subdomains on hosts on LAN)

3) I wonder why function dhcp_domain_add worked with host. I mean, that function was expected to receive a domain, but passing a host to it also worked (which made me happy). But now I'm afraid this solution/approach could just stop working on future OpenWRT releases, as function dhcp_domain_add could just change...

Attachments (0)

Change History (4)

comment:1 Changed 5 years ago by jogo

  • Resolution set to invalid
  • Status changed from new to closed

Feel free to submit a patch to the mailing list according to SubmittingPatches. I'll close this ticket to keep trac mostly for actual bugs.

comment:2 Changed 5 years ago by maniqui@…

I've submitted a patch that tries to address this feature request.
http://patchwork.openwrt.org/patch/3828/

comment:3 Changed 4 years ago by jow

  • Milestone changed from Attitude Adjustment 12.09 to Barrier Breaker 14.07

Milestone Attitude Adjustment 12.09 deleted

comment:4 Changed 3 years ago by maniqui@…

  • Resolution invalid deleted
  • Status changed from closed to reopened

I've just upgraded to Barrier Breaker 14.07 and applied again the proposed patch (http://patchwork.openwrt.org/patch/3828/).

Add Comment

Modify Ticket

Action
as reopened .
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.