openwrt無線連接互聯網的實現原理【2】

 前面講的配置方式對於上級無線固定不變而且永久存在的情況是好使的。但是如果上級無線找不到的話,OpenWRT讓人噁心的地方是下級無線,也就是703n路由器自身提供的無線也無法正常啓動。這樣如果你沒有隨身帶着網線的話就沒有辦法登錄到703n的web界面了,也就沒有辦法修改或者禁用上級無線。這不得不說是一個很令人遺憾的事情。

fqrouter的解決方式是在/etc/config/wireless中配置一個或者多個上級無線interface。默認這些上級無線的interface都是被禁用的。這樣哪怕是上級無線不存在的話,703n自己的無線也能啓動起來。這個默認的禁用動作是由一個啓動腳本完成的(/etc/init.d/disable_sta_mode_wifi_interfaces)

然後掃描附近有的無線,如果發現了已經保存的上級無線,就啓用對應的無線interface。這個時候會等待10秒鐘,如果10秒內沒有完成上級無線的連接,這個無線interface又會被重新禁用。這是因爲上級無線的密碼可能輸入錯誤。這個智能檢測並啓用無線的動作是由一個hotplug腳本完成(/etc/hotplug.d/net/00-only-enable-connectable-sta-mode-wifi-interface)

智能檢測聽起來挺容易的,應該是先掃描附近的無線,然後把無線的配置文件更新了,再讓無線正常啓動起來。但是,詭異的地方在於,如果無線自己沒有啓動,是沒有辦法掃描附近的無線的。所以就需要現讓本地無線網絡按照一個配置啓動起來,掃描附近的無線,修改配置文件,再用修改後的配置文件把無線網絡重啓。

———————————————————————-
if [ -f /tmp/skip-only-enable-connectable-sta-mode-wifi-interface ] ; then
logger -s -t fqrouter skip-only-enable-connectable-sta-mode-wifi-interface found
return
fi

if [ "remove" == "$ACTION" -a "wlan0" == "$INTERFACE" ] ; then
/etc/init.d/disable_sta_mode_wifi_interfaces start
fi
if [ "add" == "$ACTION" -a "wlan0" == "$INTERFACE" ] ; then
logger -s -t fqrouter try to bring up sta mode wifi interface
sta-mode-wifi-interface-up &
fi
——————————————————————————

————————————————————————-
#!/bin/sh /etc/rc.common

START=19 # just before network start

disable_sta_mode_wifi_interface() {
local section=”$1″
config_get mode $section ‘mode’
config_get ssid $section ‘ssid’
if [ sta == $mode ] ; then
logger -s -t fqrouter disable sta mode wifi interface $ssid
uci_set wireless $section disabled 1
fi
}

start() {
config_load ‘wireless’
config_foreach disable_sta_mode_wifi_interface ‘wifi-iface’
uci_commit
logger -s -t fqrouter all sta mode wifi interfaces disabled
}
————————————————————————

————————————————————————–
#!/usr/bin/lua
require ‘uci’
require ‘dumper’
require ‘nixio’

local pid_file = io.open(‘/tmp/sta-mode-wifi-interface-up.pid’, ‘r’)
if pid_file ~= nil then
local that_pid = pid_file:read(‘*a’)
io.close(pid_file)
os.execute(‘logger -s -t fqrouter sta-mode-wifi-interface-up that pid is: ‘ .. that_pid)
local cmdline_file = io.open(‘/proc/’ .. that_pid .. ‘/cmdline’, ‘r’)
if cmdline_file ~= nil then
io.close(cmdline_file)
os.execute(‘logger -s -t fqrouter sta-mode-wifi-interface-up found another instance ‘ .. that_pid)
os.exit()
end
end
local this_pid = nixio.getpid()
os.execute(‘echo -n ‘ .. this_pid .. ‘ > /tmp/sta-mode-wifi-interface-up.pid’)
os.execute(‘logger -s -t fqrouter sta-mode-wifi-interface-up.pid: ‘ .. this_pid)
x = uci.cursor()
function exit_if_sta_mode_wifi_interface_enabled(section)
if ‘sta’ == section.mode and ’0′ == section.disabled then
os.execute(‘logger -s -t fqrouter std mod wifi interface ‘ .. section.ssid .. ‘ already enabled’)
os.exit()
end
end
x:foreach(‘wireless’, ‘wifi-iface’, exit_if_sta_mode_wifi_interface_enabled)
os.execute(‘logger -s -t fqrouter no sta mode wifi interface enabled’)

function is_interface_up(ifname)
local f = assert(io.popen(‘ifconfig ‘..ifname, ‘r’))
local output = assert(f:read(‘*a’))
return output:find(‘RUNNING’)
end
for count=1,10 do
if is_interface_up(‘wlan0′) then
break
end
os.execute(‘sleep 1′)
end
if not is_interface_up(‘wlan0′) then
os.execute(‘logger -s -t fqrouter wlan0 not up in given time’)
os.exit()
end
os.execute(‘logger -s -t fqrouter wlan0 is up’)

local ssid_list = {}
local ssid_present = {}
for i = 1, 3 do
local iw = require’luci.sys’.wifi.getiwinfo(‘radio0′)
for k, v in ipairs(iw.scanlist or {}) do
if v.ssid ~= nil and ssid_present[v.ssid] == nil then
table.insert(ssid_list, v.ssid)
ssid_present[v.ssid] = v
end
end
os.execute(‘logger -s -t fqrouter “ssid list: ‘ .. table.concat(ssid_list, ‘, ‘) .. ‘”‘)
end
local no_sta_mode_wifi_interface_configured = true
function enable_sta_mode_wifi_interface(section)
if ‘sta’ == section.mode then
no_sta_mode_wifi_interface_configured = false
if ssid_present[section.ssid] ~= nil then
os.execute(‘logger -s -t fqrouter found ‘ .. section.ssid)
x:set(‘wireless’, section['.name'], ‘disabled’, 0)
x:commit(‘wireless’)
os.execute(‘touch /tmp/skip-only-enable-connectable-sta-mode-wifi-interface’)
os.execute(‘wifi up’)
os.execute(‘rm /tmp/skip-only-enable-connectable-sta-mode-wifi-interface’)
os.execute(‘sleep 10′)
if is_interface_up(‘wlan0-1′) then
os.execute(‘rm /tmp/upstream-status’)
else
os.execute(‘echo “UPSTREAM_TIMEOUT” > /tmp/upstream-status’)
os.execute(‘logger -s -t fqrouter sta mode wifi interface not up in given time’)
x:set(‘wireless’, section['.name'], ‘disabled’, 1)
x:commit(‘wireless’)
os.execute(‘touch /tmp/skip-only-enable-connectable-sta-mode-wifi-interface’)
os.execute(‘wifi down’)
os.execute(‘wifi up’)
os.execute(‘rm /tmp/skip-only-enable-connectable-sta-mode-wifi-interface’)
end
os.exit()
end
end
end
x = uci.cursor()
x:foreach(‘wireless’, ‘wifi-iface’, enable_sta_mode_wifi_interface)
if no_sta_mode_wifi_interface_configured then
os.execute(‘echo “UPSTREAM_NOT_CONFIGURED” > /tmp/upstream-status’)
os.execute(‘logger -s -t fqrouter no sta mode wifi interface configured’)
else
os.execute(‘echo “UPSTREAM_NOT_AVAILABLE” > /tmp/upstream-status’)
os.execute(‘logger -s -t fqrouter no sta mode wifi interface available’)
end
——————————————————————————————–

www.fuyufu.com

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章