原文地址
譯者:程序手藝人
轉(zhuǎn)載請注明出處: http://blog.csdn.net/z2066411585
連接管理器(ConnMan)是一個連接管理守護(hù)進(jìn)程(connmand),用于管理運(yùn)行Linux操作系統(tǒng)的設(shè)備中的Internet連接。它提供低內(nèi)存消耗,并對網(wǎng)絡(luò)條件變化進(jìn)行快速,連貫,同步的反應(yīng)。
ConnMan有各種插件,是一個完全模塊化的系統(tǒng),可以擴(kuò)展以支持各種有線或無線技術(shù).插件方法可以輕松適應(yīng)和修改各種用例。諸如DHCP和域名解析之類的配置方法是使用插件實現(xiàn)的。
所有應(yīng)用程序都應(yīng)該使用D-Bus與ConnMan進(jìn)行通信。它與嵌入式Y(jié)octo構(gòu)建系統(tǒng)一起使用,是GENIVI車載信息娛樂規(guī)范的一部分。
ConnMan根據(jù)GPL v2.0許可證分發(fā)。英特爾是ConnMan項目的主要貢獻(xiàn)者。
ConnMan體系結(jié)構(gòu)
ConnMan內(nèi)部基礎(chǔ)設(shè)施
功能和特點(diǎn)
以下功能內(nèi)置于連接管理器中:
- 通用插件基礎(chǔ)結(jié)構(gòu)
- 設(shè)備和網(wǎng)絡(luò)抽象(具有基本的存儲支持)
- IPv4,IPv4-LL(鏈路本地)和DHCP - IPv6,DHCPv6和6to4隧道
- 高級路由和DNS配置
- 內(nèi)置DNS代理和智能緩存
- 內(nèi)置WISPr熱點(diǎn)登錄和門戶檢測
- 時間和時區(qū)配置(使用NTP進(jìn)行手動和自動)
- 代理處理(手動和自動使用WPAD)
- 共享支持(USB,藍(lán)牙和WiFi AP模式)
- 詳細(xì)的統(tǒng)計處理(家庭和漫游)
各種插件可以啟用網(wǎng)絡(luò)支持:
- 以太網(wǎng)插件
- WiFi插件與WEP40 / WEP128和WPA / WPA2(個人和企業(yè))
- 藍(lán)牙插件(使用BlueZ)
- 2G /3G / 4G插件(使用oFono)
Pugins還提供其他功能:
- 環(huán)回接口設(shè)置
- PACrunner代理處理
- PolicyKit授權(quán)支持
Connman D-BUS接口
D-Bus interfaces
- 應(yīng)用程序應(yīng)該使用D-Bus與ConnMan進(jìn)行通信。
- 主要入口點(diǎn)是通過管理器對象。
- Manager對象包含對服務(wù)對象的引用(接口)。
- 其他接口包括VPN,時鐘,任務(wù),技術(shù),會話,通知,對等,代理和計數(shù)器。

管理D-BUS接口
- 應(yīng)用程序入口點(diǎn)是管理器對象
- 所有的方法和屬性都有解釋

管理屬性
檢索所有全局屬性的簡單方法如下所示:
- bus = dbus.SystemBus()
- manager = dbus.Interface(bus.get_object("net.connman", "/"), "net.connman.Manager")
- properties = manager.GetProperties()
更改全局屬性也很簡單。例如,啟用所謂的離線模式(AKA飛行模式)就足以設(shè)置該屬性: manager.SetProperty("OfflineMode", dbus.Boolean(1)).

服務(wù)界面
先進(jìn)的界面使用戶界面可以輕松訪問網(wǎng)絡(luò)細(xì)節(jié)和用戶選擇的偏好。這是服務(wù)列表和界面。ConnMan維護(hù)一個單一的平面和所有可用服務(wù)的排序列表。
服務(wù)順序
服務(wù)狀態(tài)
- 每項服務(wù)的基本狀態(tài)都是“閑置”。這意味著此服務(wù)目前尚未使用。它也不是試圖連接或做任何事情。
- “關(guān)聯(lián)”狀態(tài)表示該服務(wù)試圖建立與網(wǎng)絡(luò)的低級連接 - 例如,與WiFi接入點(diǎn)關(guān)聯(lián)/連接。
- 在“配置”狀態(tài)下,該服務(wù)表明它正在嘗試檢索/配置IP設(shè)置。
- “就緒”狀態(tài)表示成功連接的設(shè)備。這并不意味著它具有默認(rèn)路由,但基本的IP操作將會成功。
- 在“斷開”狀態(tài)下,服務(wù)指示它將要終止當(dāng)前連接并返回到“空閑”狀態(tài)。
- 另外,“失敗”狀態(tài)表示錯誤的行為。由于服務(wù)未連接,因此與“空閑”狀態(tài)類似。
服務(wù)D-Bus接口
以下是如何獲取服務(wù)對象:
-properties = manager.GetProperties()
-for path in properties["Services"]:
service = dbus.Interface(bus.get_object("net.connman", path), "net.connman.Service")
它還包含連接或斷開特定服務(wù)的方法。ConnMan還可以根據(jù)他的策略或通過外部事件(如插入以太網(wǎng)電纜)自動連接服務(wù)。手動連接(或斷開)特定服務(wù)就像告訴它一樣簡單。
connect: service.Connect() or service.Disconnect()
服務(wù)屬性
技術(shù)接口
基本的設(shè)備配置任務(wù)由技術(shù)接口完成。例如,打開和關(guān)閉設(shè)備(例如通過RFKILL)應(yīng)通過技術(shù)接口完成。
技術(shù)屬性
用戶可以通過Manager OfflineMode屬性激活離線(飛行)模式。在離線模式下,包括以太網(wǎng)在內(nèi)的所有技術(shù)均關(guān)閉。在離線模式下,用戶可以使用rfkill命令或某些筆記本電腦中的Fn-Fx組合鍵,通過Technology Powered屬性臨時激活單個技術(shù)。
Connman內(nèi)部
啟動connmand
選項:
-c, --config=FILE Load the specified configuration file instead of /usr/local/etc/connman/main.conf
-d, --debug=DEBUG Specify debug options to enable
-i, --device=DEV Specify networking device or interface
-I, --nodevice=DEV Specify networking interface to ignore
-p, --plugin=NAME,... Specify plugins to load
-P, --noplugin=NAME,... Specify plugins not to load
-W, --wifi=NAME Specify driver for WiFi/Supplicant
-n, --nodaemon Don't fork daemon to background
-r, --nodnsproxy Don't enable DNS Proxy
--nobacktrace Don't print out backtrace information
用法示例:
# CONNMAN_SUPPLICANT_DEBUG=1 connmand -n -d &> connman.log
#connmand -i wlan0 -I eth0 --nodnsproxy --nodaemon >& connman.log
主要配置文件
main.conf
InputRequestTimeout = 120 (default)---------> input request ( ex. passphrase) timeout
BrowserLaunchTimeout = 300 ( default) -----> The request for launching a browser for portal pages
BackgroundScanning = true --------------------> option for background scanning
FallbackTimeservers = --------------------------> List of fallback timeservers ( used by NTP sync) separated by ","
FallbackNameservers = -------------------------> List of fallback nameservers
DefaultAutoConnectTechnologies = -----------> List of technologies that are marked autoconnectable by default.
PreferredTechnologies = ------------------------> List of preferred technologies from - most preferred one to least preferred
NetworkInterfaceBlacklist = vmnet,vboxnet,virbr,ifb,ve-,vb- ---> List of blacklisted network interfaces
AllowHostnameUpdates = true -----------------> Allow connman to change the system hostname ( ex. dhcp hostname option)
SingleConnectedTechnology = false -----------> Keep only a single connected technology at any time
TetheringTechnologies = wifi,bluetooth,gadget
PersistentTetheringMode = false --------------> Restore earlier tethering status when returning from offline mode
Enable6to4 = false ------------------------------> Automatically enable Anycast 6to4 if possible (not recommended. see RFC6343 )
EnableOnlineCheck = false --------------------> Enable use of http get as on online status check
AlwaysConnectedTechnologies = -------------> List of technologies with AutoConnect = true which are always connected regardless of PreferredTechnologies setting
Connman配置文件
Connman使用配置文件來調(diào)配現(xiàn)有服務(wù)。Connman將在/ var / lib / connman /下查找它的配置文件。配置文件名稱不得包含除字母或數(shù)字之外的其他字符,并且必須具有.config后綴。
Ex. #cat /var/lib/connman/example.config
[global]
Name = Example
Description = Example network configuration
[service_home_ethernet]
Type = ethernet
IPv4 = 192.168.1.42/255.255.255.0/192.168.1.1
IPv6 = 2001:db8::42/64/2001:db8::1
MAC = 01:02:03:04:05:06
Nameservers = 10.2.3.4,192.168.1.99
SearchDomains = my.home,isp.net
Timeservers = 10.172.2.1,ntp.my.isp.net
Domain = my.home
[service_home_wifi]
Type = wifi
Name = my_home_wifi
Passphrase = secret
IPv4 = 192.168.2.2/255.255.255.0/192.168.2.1
MAC = 06:05:04:03:02:01Ex. #cat /var/lib/connman/example.config
[global]
Name = Example
Description = Example network configuration
[service_home_ethernet]
Type = ethernet
IPv4 = 192.168.1.42/255.255.255.0/192.168.1.1
IPv6 = 2001:db8::42/64/2001:db8::1
MAC = 01:02:03:04:05:06
Nameservers = 10.2.3.4,192.168.1.99
SearchDomains = my.home,isp.net
Timeservers = 10.172.2.1,ntp.my.isp.net
Domain = my.home
[service_home_wifi]
Type = wifi
Name = my_home_wifi
Passphrase = secret
IPv4 = 192.168.2.2/255.255.255.0/192.168.2.1
MAC = 06:05:04:03:02:01
設(shè)置
設(shè)置和配置文件會自動為用戶經(jīng)常連接的網(wǎng)絡(luò)創(chuàng)建。
# cat /var/lib/connman/settings
[global]
OfflineMode=false
[WiFi]
Enable=true
Tethering=false
[Bluetooth]
Enable=false
Tethering=false
配置文件設(shè)置存儲在/ var / lib / connman /目錄下的服務(wù)名稱下。VPN設(shè)置可以在/ var / lib / connman-vpn /中找到。
編寫插件
基本插件包含通過CONNMAN_PLUGIN_DEFINE的插件描述以及通過該描述定義的init / exit回調(diào)。
#include <connman/plugin.h>
static int example_init(void)
{
return 0;
}
static void example_exit(void)
{
}
CONNMAN_PLUGIN_DEFINE(example, "Example plugin", CONNMAN_VERSION,
example_init, example_exit)
插件使用技術(shù),網(wǎng)絡(luò)和設(shè)備以及其他基礎(chǔ)設(shè)施與ConnMan的核心功能進(jìn)行交互。這里是技術(shù)基礎(chǔ)設(shè)施:
ex. bluetooth plugin
static struct connman_technology_driver tech_driver = {
.name = "bluetooth",
.type = CONNMAN_SERVICE_TYPE_BLUETOOTH,
.probe = bluetooth_tech_probe,
.remove = bluetooth_tech_remove,
.set_tethering = bluetooth_tech_set_tethering,
};
為了使Connection Manager知道新的Technology插件,我們需要通過調(diào)用connman_technology_driver_register()來注冊它的驅(qū)動程序。
connman_technology_driver_register(&tech_driver);
設(shè)備基礎(chǔ)設(shè)施
設(shè)備代表給定技術(shù)的真實設(shè)備。每種技術(shù)可能有很多設(shè)備。
static struct connman_device_driver device_driver = {
.name = "bluetooth",
.type = CONNMAN_DEVICE_TYPE_BLUETOOTH,
.probe = bluetooth_device_probe,
.remove = bluetooth_device_remove,
.enable = bluetooth_device_enable,
.disable = bluetooth_device_disable,
};
要注冊驅(qū)動程序:
connman_device_driver_register(&device_driver);
此外,需要為每個插件編寫處理新設(shè)備檢測的代碼;藍(lán)牙插件通過注冊BlueZ D-Bus接口的觀察器來實現(xiàn)。一旦出現(xiàn)新的藍(lán)牙設(shè)備,插件需要通過調(diào)用connman_device_create()來通知ConnMan核心。對于藍(lán)牙插件,通話將是:
struct connman_device *device;
device = connman_device_create("bluetooth", CONNMAN_DEVICE_TYPE_BLUETOOTH)
網(wǎng)絡(luò)基礎(chǔ)設(shè)施
連接管理器提供了一種插件來處理為每種類型的技術(shù)建立/處理連接的細(xì)節(jié)。對于藍(lán)牙插件,需要注冊connman_network_driver:
static struct connman_network_driver network_driver = {
.name = "bluetooth",
.type = CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN,
.probe = bluetooth_pan_probe,
.remove = bluetooth_pan_remove,
.connect = bluetooth_pan_connect,
.disconnect = bluetooth_pan_disconnect,
};
然后,調(diào)用注冊函數(shù):
connman_network_driver_register(&network_driver);
Connman源文件/基礎(chǔ)設(shè)施
在運(yùn)行主循環(huán)之前,init函數(shù)將在src / main.c中調(diào)用。
src/util.c - /dev/urandom support
src/inotify.c - used by the core (config.c) and the session policy plugin
src/technology.c – used for technology interface
src/notifier.c – basic notifier infrastructure
src/agent.c – agent interface
src/service.c – used for service interface
src/peer_service.c – p2p peer service API
src/peer.c – p2p peer object’s core logic
src/provider.c – provider infrastructure
src/network.c – network infrastructure used by plug-ins
src/config.c – framework for configuration files
src/device.c – device infrastructure used by plug-ins
src/iptables.c – iptables support (netfilter chains and rules)
src/firewall-iptables.c, nftables.c (older firewall.c) – firewall infrastructure.
src/nat.c – used for NAT
src/tethering.c – tethering infrastructure
src/manager.c – Manager interface
src/stats.c – used for storing service stats ( mmaped into memory)
src/ipconfig.c – IP configuration framework
src/rtnl.c – netlink support
src/session.c – Allows 3rd party applications to request a network session
src/resolver.c – resolver framework
src/dhcp.c, dhcpv6.c – dhcp framework
src/rfkill.c – rfkill interface support
src/machine.c - basic systemd-hostnamed ( machine-type- “chassis”) support
Connmanctl
ConnMan有一個標(biāo)準(zhǔn)的命令行客戶端connmanctl。它可以運(yùn)行在兩種模式下:
- 在命令模式下,命令作為connmanctl命令的參數(shù)輸入,就像systemctl一樣。
- 通過輸入不帶參數(shù)的connmanctl來啟動交互模式。提示將變?yōu)閏onnmanctl>,表示它正在等待用戶命令,就像python交互模式一樣。
Connmanctl例子
示例1. 啟用和禁用WiFi
$ connmanctl technologies - check for the line that says Powered: True/False.
$ connmanctl enable wifi - To power the wifi on.
$ connmanctl disable wifi – To power off the wifi.
其他啟用wifi的方式可能包括 - ip link set <interface>向上。ConnMan將自動處理有線連接。
示例2. 連接到一個開放的接入點(diǎn)
本節(jié)中的命令顯示如何在命令模式下運(yùn)行connmanctl。掃描網(wǎng)絡(luò)connmanctl接受稱為技術(shù)的簡單名稱。掃描附近的WiFi網(wǎng)絡(luò):
$ connmanctl scan wifi To list the available networks found after a scan run (example output):
$ connmanctl services
*AO MyNetwork wifi_dc85de828967_68756773616d_managed_psk OtherNET wifi_dc85de828967_38303944616e69656c73_managed_psk AnotherOne wifi_dc85de828967_3257495245363836_managed_wep FourthNetwork wifi_dc85de828967_4d7572706879_managed_wep AnO6penNetwork wifi_dc85de828967_4d6568657272696e_managed_none
$ connmanctl connect wifi_dc85de828967_4d6568657272696e_managed_none
您現(xiàn)在應(yīng)該連接到網(wǎng)絡(luò)。使用ip addr或connmanctl狀態(tài)進(jìn)行檢查。
示例3. 連接到受保護(hù)的接入點(diǎn)
對于受保護(hù)的訪問點(diǎn),向ConnMan守護(hù)進(jìn)程提供一些信息,至少一個密碼。
$ connmanctl
connmanctl> scan wifi ----- To list services:
connmanctl> services
connmanctl> agent on --- register the agent to handle user requests.
connmanctl> connect wifi_dc85de828967_38303944616e69656c73_managed_psk
代理程序要求獲取更多信息,具體取決于您要連接的網(wǎng)絡(luò)類型。該代理還將打印有關(guān)其所需信息的其他數(shù)據(jù),如以下示例中所示。
Agent RequestInput wifi_dc85de828967_38303944616e69656c73_managed_psk Passphrase = [ Type=psk, Requirement=mandatory ] Passphrase?
提供請求的信息(在本例中為密碼),然后鍵入:
connmanctl> quit
如果您提供的信息是正確的,您現(xiàn)在應(yīng)該連接到受保護(hù)的接入點(diǎn)。
Connmanctl其他用法
ConnMan將各種硬件接口稱為Technologies。要列出可用技術(shù),請運(yùn)行:
$ connmanctl technologies
技術(shù)可以通過以下方式打開/關(guān)閉:
$ connmanctl enable technology_type
$ connmanctl disable technology_type
例如,要關(guān)閉WiFi:
$ connmanctl disable wifi
ConnMan測試腳本
Connman還提供了基本的Python腳本(例如test-connman,list-services等)。諸如連接,斷開連接,自動連接,服務(wù),掃描,啟用,禁用,脫機(jī)模式,狀態(tài)等基本操作均通過這些腳本得到支持。
ConnMan 調(diào)試
激活調(diào)試
我們可以使用-d命令行選項在ConnMan中激活調(diào)試打印。
-d Activate all normal debug prints
-d src/service.c This prints debugging info from src/service.c file only
-d src/network.c:src/ipconfig.c This activates debug prints in src/network.c and src/ipconfig.c files.
-d 'src/n*.c' This would activate debug print from all the C source files starting with letter 'n' in src directory. Note the quotation marks around option, that is to prevent shell expansion.
-d '*/n*.c:*/i*.c' Activate debug prints for all C source files starting with letters 'n' or 'i' in any sub-directory.
子組件調(diào)試
ConnMan的某些組件具有環(huán)境變量激活的調(diào)試打印。如果設(shè)置了環(huán)境變量,那么相應(yīng)的組件會打印一些額外的調(diào)試信息。
可以使用以下環(huán)境變量:
- CONNMAN_DHCP_DEBUG:與DHCPv4相關(guān)的調(diào)試信息
- CONNMAN_DHCPV6_DEBUG:與DHCPv6相關(guān)的調(diào)試信息
- CONNMAN_IPTABLES_DEBUG:使用iptables時的額外信息
- CONNMAN_RESOLV_DEBUG:名稱解析器調(diào)試打印。當(dāng)ConnMan解析自己使用的主機(jī)名時使用這些調(diào)試打印。請注意,DNS代理調(diào)試打印不使用此環(huán)境變量。為此,可以使用“-d src / dnsproxy.c”命令行選項。
- CONNMAN_SUPPLICANT_DEBUG:調(diào)試connmand和wpa_supplicant進(jìn)程之間的通信打印。
- CONNMAN_WEB_DEBUG:ConnMan在Wispr和6to4組件中進(jìn)行Internet連接檢查時的調(diào)試信息。
例如:
CONNMAN_SUPPLICANT_DEBUG=1 src/connmand -n
示例:獲取CONNMAN_IPTABLES_DEBUG日志
確保在內(nèi)核中啟用iptables支持:
#zcat /proc/config.gz | grep "CONFIG_IP_NF_IPTABLES"
在內(nèi)核中必須啟用Iptables:
CONFIG_IP_NF_IPTABLES=m or CONFIG_IP_NF_IPTABLES=y
清除所有iptable過濾器:
# iptables –F
在iptable規(guī)則下面設(shè)置以過濾ICMP數(shù)據(jù)包(輸入,輸出和轉(zhuǎn)發(fā)數(shù)據(jù)包):
# iptables -t filter -A INPUT -p icmp --icmp-type echo-request -j LOG --log-prefix="filter INPUT:"
# iptables -t filter -A INPUT -p icmp --icmp-type echo-reply -j LOG --log-prefix="filter INPUT:“
# iptables -t filter -A OUTPUT -p icmp --icmp-type echo-request -j LOG --log-prefix="filter OUTPUT:“
# iptables -t filter -A OUTPUT -p icmp --icmp-type echo-reply -j LOG --log-prefix="filter OUTPUT:“
# iptables -t filter -A FORWARD -p icmp --icmp-type echo-request -j LOG --log-prefix="filter FORWARD:“
# iptables -t filter -A FORWARD -p icmp --icmp-type echo-reply -j LOG --log-prefix="filter FORWARD:"
導(dǎo)出下面的環(huán)境變量來啟用ConnMan的iptables調(diào)試:
# export CONNMAN_IPTABLES_DEBUG=1
Start Connmand Connection Manager with debug option enabled:
# /usr/sbin/connmand -d -n -I eth0
ConnMan守護(hù)進(jìn)程消息日志應(yīng)該在涉及ICMP數(shù)據(jù)包時顯示。