#!/sbin/runscript # Copyright 1999-2008 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 # $Header: $ depend() { need net } source /lib/rc/net/iproute2.sh _checks() { if [ -z "${IFACE}" ]; then eerror 'You need to set $IFACE!' return 1 fi } _flush_routes() { while true; do # using 'route' instead of 'ip route', because its output can be parsed easier ROUTER="$(route -n | awk '$8 == "'"${IFACE}"'"&& $5 == "'"${METRIC}"'" && $1 == "0.0.0.0" && $2 != "0.0.0.0" { print $2 }')" [ -z "${ROUTER}" ] && break ebegin "Disabling route through ${ROUTER}" ip route del default via "${ROUTER}" dev "${IFACE}" # not provided by iproute2.sh RET=$? eend ${RET} [ ${RET} -ne 0 ] && return ${RET} done } start() { _checks || return $? _flush_routes || return $? # we really always want to clear old route 'in case' SSDPADDR="${SSDPADDR:-$(_get_inet_address)}" # upnpc if [ -z "${IFACE}" ]; then eerror "I failed to determine IP of ${IFACE}. Please set \$SSDPADDR!" return 1 fi ebegin "Trying to find any router using UPnP" eval "$(upnpc -m "${SSDPADDR%/*}" -s 2>/dev/null | perl -e ' local $/; <> =~ /Found valid IGD\s+:\s+http:\/\/([\d\.]{7,}).*?ExternalIPAddress = ([\d\.]{7,})/s or exit 1; print "ROUTER=$1; MYIP=$2" ')" [[ $? -eq 0 && -n "${ROUTER}" && -n "${MYIP}" ]] RET=$? eend ${RET} if [ "${ROUTER}" = "${SSDPADDR%/*}" ]; then eerror "Found UPnP router on local machine!" return 1 fi [ ${RET} -ne 0 ] && return ${RET} ebegin "Adding route through ${ROUTER}" _add_route -net default via "${ROUTER}" metric "${METRIC}" # dev ${IFACE} implicit RET=$? eend ${RET} einfo "External IP: ${MYIP}" return ${RET} } stop() { _checks || return $? _flush_routes return $? }