Skip to content

Commit

Permalink
Cleanup OpenWRT protocol handler to handle reloading in addition to r…
Browse files Browse the repository at this point in the history
…estarting when DHCP options are changed. Also remove redundant log messages.
  • Loading branch information
jheretic committed Mar 11, 2014
1 parent bd0d371 commit b56e048
Showing 1 changed file with 38 additions and 38 deletions.
76 changes: 38 additions & 38 deletions openwrt/lib/netifd/proto/commotion.sh
Expand Up @@ -53,6 +53,7 @@ proto_commotion_init_config() {
proto_config_add_string "profile"
proto_config_add_string "type"
proto_config_add_string "class"
proto_config_add_string "dhcp"
proto_config_add_string "ipaddr"
proto_config_add_string "netmask"
proto_config_add_string "dns"
Expand All @@ -69,23 +70,23 @@ proto_commotion_setup() {
local profile type ipaddr netmask dns domain announce lease_zone nolease_zone
json_get_vars profile type class ipaddr netmask dns domain announce lease_zone nolease_zone

logger -s -t commotion.proto "Running protocol handler."
logger -t commotion.proto "Running protocol handler."
[ "$class" == "mesh" ] && commotion_up "$iface" $(uci_get network $config profile)
case "$class" in
"mesh")
logger -t "commotion.proto" -s "Class: $class"
logger -t "commotion.proto" "Class: $class"
commotion_up "$iface" $(uci_get network $config profile)
logger -t "commotion.proto" -s "Upped $iface"
logger -t "commotion.proto" "Upped $iface"
uci_set_state network "$config" meshed "$(uci_get network "$config" meshed 1)"
uci_set_state network "$config" announced "$(uci_get network "$config" announced 0)"
;;
"client")
logger -t "commotion.proto" -s "Class: $class"
logger -t "commotion.proto" "Class: $class"
uci_set_state network "$config" meshed "$(uci_get network "$config" meshed 0)"
uci_set_state network "$config" announced "$(uci_get network "$config" announced 1)"
;;
"wired")
logger -t "commotion.proto" -s "Class: $class"
logger -t "commotion.proto" "Class: $class"
uci_set_state network "$config" meshed "$(uci_get network "$config" meshed 0)"
uci_set_state network "$config" announced "$(uci_get network "$config" announced 0)"
dhcp="$(uci_get network "$config" dhcp "auto")"
Expand All @@ -95,9 +96,12 @@ proto_commotion_setup() {
"auto")
local dhcp_status
local dhcp_timeout="$(uci_get commotiond @node[0] dhcp_timeout "$DHCP_TIMEOUT")"
logger -t "commotion.proto.dhcp" "DHCP type: $dhcp"

unset_bridge "$client_bridge" "$iface"
logger -t "commotion.proto" -s "Removing $iface from bridge $client_bridge"
logger -t "commotion.proto" "Removing $iface from bridge $client_bridge"
#Can't use unset_bridge here, short-circuits startup process by calling *_teardown
brctl delif br-"$client_bridge" "$iface"
logger -t "commotion.proto" "Successfully removed $iface from bridge $client_bridge"
export DHCP_INTERFACE="$config"
udhcpc -q -i ${iface} -p /var/run/udhcpc-${iface}.pid -t 2 -T "$dhcp_timeout" -n -s /lib/netifd/commotion.dhcp.script
dhcp_status=$?
Expand All @@ -106,65 +110,62 @@ proto_commotion_setup() {
# we got an IP
# see commotion.dhcp.script for the rest of
# the setup code.
have_ip=1

# get out of here early.
return
else
unset_fwzone "$config"
uci_commit firewall
set_bridge "$client_bridge" "$iface"
logger -t "commotion.proto" -s "Adding $iface to bridge $client_bridge"
logger -t "commotion.proto" "Adding $iface to bridge $client_bridge"

logger -t "commotion.proto" -s "Restarting $client_bridge interface"
logger -t "commotion.proto" "Restarting $client_bridge interface"
ubus call network.interface."$client_bridge" down
ubus call network.interface."$client_bridge" up
local bridge_ip="$(uci_get_state network $client_bridge ipaddr $(commotion_gen_ip $DEFAULT_CLIENT_SUBNET $DEFAULT_CLIENT_IPGENMASK gw))"
logger -t "commotion.proto" -s "Bridge IP state: $(uci_get_state $client_bridge ipaddr)"
logger -t "commotion.proto" -s "Bridge generated IP: $(commotion_gen_ip $DEFAULT_CLIENT_SUBNET $DEFAULT_CLIENT_IPGENMASK gw)"
logger -t "commotion.proto" "Bridge IP state: $(uci_get_state $client_bridge ipaddr)"
logger -t "commotion.proto" "Bridge generated IP: $(commotion_gen_ip $DEFAULT_CLIENT_SUBNET $DEFAULT_CLIENT_IPGENMASK gw)"
local bridge_netmask="$(uci_get_state network $client_bridge netmask $DEFAULT_CLIENT_NETMASK)"
logger -t "commotion.proto" -s "Bridge Netmask state: $(uci_get_state $client_bridge netmask)"
logger -t "commotion.proto" -s "Bridge Default netmask: $DEFAULT_CLIENT_NETMASK"
logger -t "commotion.proto" -s "Bridge: $client_bridge, IP: $bridge_ip, Netmask: $bridge_netmask"
logger -t "commotion.proto" "Bridge Netmask state: $(uci_get_state $client_bridge netmask)"
logger -t "commotion.proto" "Bridge Default netmask: $DEFAULT_CLIENT_NETMASK"
logger -t "commotion.proto" "Bridge: $client_bridge, IP: $bridge_ip, Netmask: $bridge_netmask"
uci_set network "$client_bridge" ipaddr "$bridge_ip"
uci_set_state network "$client_bridge" ipaddr "$bridge_ip"
uci_set network "$client_bridge" netmask "$bridge_netmask"
uci_set_state network "$client_bridge" netmask "$bridge_netmask"
logger -t "commotion.proto" -s "Restarting dnsmasq"
sleep 1
/etc/init.d/dnsmasq restart

return

have_ip=1
fi
;;
"server")
unset_fwzone "$config"
uci_commit firewall
set_bridge "$client_bridge" "$iface"
logger -t "commotion.proto" -s "Adding $iface to bridge $client_bridge"
logger -t "commotion.proto" "Adding $iface to bridge $client_bridge"
logger -t "commotion.proto.dhcp" "DHCP type: $dhcp"

logger -t "commotion.proto" -s "Restarting $client_bridge interface"
logger -t "commotion.proto" "Restarting $client_bridge interface"
ubus call network.interface."$client_bridge" down
ubus call network.interface."$client_bridge" up
local bridge_ip="$(uci_get_state $client_bridge ipaddr $(commotion_gen_ip $DEFAULT_CLIENT_SUBNET $DEFAULT_CLIENT_IPGENMASK gw))"
local bridge_netmask="$(uci_get_state $client_bridge netmask $DEFAULT_CLIENT_SUBNET)"
logger -t "commotion.proto" -s "Bridge: $client_bridge, IP: $bridge_ip, Netmask: $bridge_netmask"
logger -t "commotion.proto" "Bridge: $client_bridge, IP: $bridge_ip, Netmask: $bridge_netmask"
uci_set network "$client_bridge" ipaddr "$bridge_ip"
uci_set network "$client_bridge" netmask "$bridge_netmask"
logger -t "commotion.proto" -s "Restarting dnsmasq"
/etc/init.d/dnsmasq restart

return
have_ip=1
;;
"client")
local dhcp_status
local dhcp_timeout="$(uci_get commotiond @node[0] dhcp_timeout "$DHCP_TIMEOUT")"

unset_bridge "$client_bridge" "$iface"
logger -t "commotion.proto" -s "Removing $iface from bridge $client_bridge"
logger -t "commotion.proto" "Removing $iface from bridge $client_bridge"
#Can't use unset_bridge here, short-circuits startup process by calling *_teardown
brctl delif br-"$client_bridge" "$iface"
logger -t "commotion.proto" "Successfully removed $iface from bridge $client_bridge"
proto_export "DHCP_INTERFACE=$config"
logger -t "commotion.proto.dhcp" "DHCP type: $dhcp"
proto_run_command "$config" udhcpc -i ${iface} -f -T "$dhcp_timeout" -t 0 -p /var/run/udhcpc-"$iface".pid -s /lib/netifd/commotion.dhcp.script
have_ip=1
return
;;
"none")
unset_bridge "$client_bridge" "$iface"
Expand All @@ -185,7 +186,7 @@ proto_commotion_setup() {
local netmask=${netmask:-$DEFAULT_CLIENT_NETMASK}
fi
proto_add_ipv4_address $ip $netmask
logger -t "commotion.proto" -s "proto_add_ipv4_address: $ip $netmask"
logger -t "commotion.proto" "proto_add_ipv4_address: $ip $netmask"
uci_set_state network "$config" ipaddr "$ip"
uci_set_state network "$config" netmask "$netmask"
else
Expand All @@ -194,11 +195,11 @@ proto_commotion_setup() {
proto_add_ipv4_address $ip $netmask
uci_set_state network "$config" ipaddr "$ip"
uci_set_state network "$config" netmask "$netmask"
logger -t "commotion.proto" -s "proto_add_ipv4_address: $ip $netmask"
logger -t "commotion.proto" "proto_add_ipv4_address: $ip $netmask"
proto_add_dns_server "${dns:-$(commotion_get_dns $iface)}"
logger -t "commotion.proto" -s "proto_add_dns_server: $dns"
logger -t "commotion.proto" "proto_add_dns_server: $dns"
proto_add_dns_search ${domain:-$(commotion_get_domain $iface)}
logger -t "commotion.proto" -s "proto_add_dns_search: $domain"
logger -t "commotion.proto" "proto_add_dns_search: $domain"
fi
fi

Expand All @@ -213,24 +214,23 @@ proto_commotion_setup() {
local channel=$(uci_get wireless "$WIFI_DEVICE" channel)
uci_set wireless $WIFI_DEVICE channel ${channel:-$(commotion_get_channel $iface)}
uci_commit wireless
wifi up "$config"
fi
logger -t "commotion.proto" -s "Sending update for $config"
logger -t "commotion.proto" "Sending update for $config"
proto_send_update "$config"
}

proto_commotion_teardown() {
local config="$1"
local ifname="$2"

logger -t "commotion.proto" -s "Initiating teardown."
logger -t "commotion.proto" "Initiating teardown."

local class = "$(uci_get network "$config" class)"

if [ "$class" == "wired" ]; then
local client_bridge="$(uci_get network "$config" client_bridge "$DEFAULT_CLIENT_BRIDGE")"
unset_bridge "$client_bridge" "$ifname"
logger -t "commotion.proto" -s "Removing $ifname from bridge $client_bridge"
logger -t "commotion.proto" "Removing $ifname from bridge $client_bridge"

proto_kill_command "$config"
fi
Expand Down

0 comments on commit b56e048

Please sign in to comment.