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.
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
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:
My ISP does not assign a hostname
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.
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.