您的位置:首页 > 其它

A working DHCPCD Hook system

2016-04-04 09:32 459 查看
原文链接:http://forum.xda-developers.com/showthread.php?t=1392343

if you like to play around with the shell on Android, it might come in handy to have some proper hooks for
the wifi and dhcp. By default (in custom roms anyway), we have hooks for dhcp. However this does not invoke anything on the dhcp state "STOP". Also there are no hooks for when wifi is turned on, only when dhcp configurations are made to it. Which means that
you cannot invoke hooks when turning on/off wifi tethering. 

Personally I have an SSHd and SMBd running in the background. These exists as android apps, but I preferer not having android classes running in the background
for a such simple task. Specially when I always have these running on wifi. And both SSHd and SMBd works locally on wifi tethering, which is why this hook is a good tool to have.

Also on regular wifi with dhcp, it would be great to have these turn off when wifi connection is off. 
The below code will provide these missing features by using some small property hacks in the init.<device>.rc file.

init.<device>.rc 

Code:

# This provides the missing 'STOP' state invoke in dhcpcd
on property:init.svc.dhcpcd_eth0=stopped
restart dhcpcdstate

# This provides the wifi on/off hook invoke
on property:wlan.driver.status=*
restart wifistate

service dhcpcdstate /system/etc/dhcpcd/dhcpcd-hook STOP eth0
disabled
oneshot

service wifistate /system/etc/wifi/wifi-hook eth0
disabled
oneshot

# ------------------------------------
# The below should be changed according to your device.
# The important thing here is the path to --config and --script

service dhcpcd_eth0 /system/bin/dhcpcd -ABKL --config /system/etc/dhcpcd/dhcpcd.conf --script /system/etc/dhcpcd/dhcpcd-hook
class main
disabled
oneshot

service iprenew_eth0 /system/bin/dhcpcd -n --config /system/etc/dhcpcd/dhcpcd.conf --script /system/etc/dhcpcd/dhcpcd-hook
class main
disabled
oneshot

/system/etc/dhcpcd/dhcpcd-hook 

Code:

#!/system/bin/sh

if [ -z "${reason}" ] || -z "${interface}"; then
reason="$1"
interface="$2"
fi

for i in /system/etc/dhcpcd/dhcpcd.d/*; do
if [ -f $i ] || [ -L $i ]; then
. "$i"
fi
done

/system/etc/dhcpcd/dhcpcd.d/01-configure 

Code:

case "${reason}" in
BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT)
setprop dhcp.${interface}.ipaddress "${new_ip_address}"
setprop dhcp.${interface}.gateway   "${new_routers%% *}"
setprop dhcp.${interface}.mask      "${new_subnet_mask}"
setprop dhcp.${interface}.leasetime "${new_dhcp_lease_time}"
setprop dhcp.${interface}.server    "${new_dhcp_server_identifier}"
setprop dhcp.${interface}.ndns      "${new_domain_name_servers}"
setprop dhcp.${interface}.reason    "${reason}"
setprop dhcp.${interface}.result    "ok"

if [ ! -z "${new_domain_name_servers}" ]; then
for i in 1 2 3 4; do
setprop dhcp.${interface}.dns$i ""
done

count=1
for dnsaddr in "${new_domain_name_servers}"; do
setprop dhcp.${interface}.dns$count $dnsaddr
count=$(($count + 1))
done
fi
;;

EXPIRE|FAIL|IPV4LL|RELEASE|STOP)
setprop dhcp.${interface}.result "failed"

for i in 1 2 3 4; do
setprop dhcp.${interface}.dns$i ""
done
;;

RELEASE)
setprop dhcp.${interface}.result "released"
;;
esac

/system/etc/wifi/wifi-hook 

Code:

#!/system/bin/sh

status=`getprop wlan.driver.status`
interface="$1"

for i in /system/etc/wifi/wifi.d/*; do
if [ -f $i ] || [ -L $i ]; then
. "$i"
fi
done
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: