DHCP Client: Add IP to DNS server

My ISP assigns a CG-NAT IP via DHCP (over DSL) to my OpenWrt device. I need this IP to be accessible by hostname from the LAN side, so it would be sufficient to add this IP to the DHCP server provided by dnsmasq. Since it's dynamic, a static entry won't work. I couldn't find an easy way to do this (or I failed to come up with proper search terms).

How can I do this?

Background: I run an Asterisk server for telephony, also provided by the ISP. And Asterisk needs to know this CG-NAT IP (otherwise, calls drop after 32 seconds). And chan_pjsip requires dnsmgr to work with a dynamic IP.

Hotplug script when wan interface comes up to update the host entry for the IP of the wan interface.

3 Likes

Good idea that I could have thought of myself!
Solved it with this script in /etc/hotplug.d/iface/40-asterisk-hostname

#!/bin/sh

if [ "${ACTION}" = "ifup" -a "${INTERFACE}" = "wan" ]; then
  IP=`ifstatus wan | jsonfilter -e '@["ipv4-address"][0].address'`
  HOSTNAME="asterisk.lan"
  IDX=`uci show | grep ${HOSTNAME} | cut -d "[" -f2 | cut -d "]" -f0`

  if [ ${IDX} ]; then
    OLDIP=`uci get dhcp.@domain[${IDX}].ip`
    if [ "${OLDIP}" = "${IP}" ]; then
      logger "IP address not changed"
      exit 0
    fi
    uci set dhcp.${IDX}.ip="${IP}"
  else
    logger "Domain entry not found; add ${HOSTNAME} with ${IP}"
    uci add dhcp domain
    uci set dhcp.@domain[-1].name="${HOSTNAME}"
    uci set dhcp.@domain[-1].ip="${IP}"
  fi

  uci commit dhcp
  /etc/init.d/dnsmasq reload
fi
1 Like
uci set dhcp.@dnsmasq[0].add_local_fqdn="0"
uci set dhcp.@dnsmasq[0].add_wan_fqdn="1"
uci commit dhcp
/etc/init.d/dnsmasq restart
grep -e ${HOSTNAME} /tmp/hosts/dhcp.*
nslookup ${HOSTNAME} localhost
1 Like

Thanks, I wasn't aware of this, but it doesn't work. I do not get any host listed with my WAN IP (and /tmp/hosts/dhcp.dnsmasq does not exist, only /tmp/hosts/dhcp.cfg4011ec)

I suspect either of two reasons:

  1. My ISP does not assign a hostname
  2. My WAN IP is CG-NAT and not public

I will get back to this tomorrow evening when I have a bit more time.

EDIT: I cannot get this to work. I will use the hotplug script for the time being.

1 Like

I initially tested this in a VM with a single WAN interface, so it worked differently.
With LAN interface enabled, it needs one more option to take effect.

Nope, it still doesn't work. And the local FQDN is now gone, so that wouldn't be an option anyway.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.