玩转「Wi-Fi」系列之Connman剖析(六)

原文地址
译者:程序手艺人
转载请注明出处: http://blog.csdn.net/z2066411585

连接管理器(ConnMan)是一个连接管理守护进程(connmand),用于管理运行Linux操作系统的设备中的Internet连接。它提供低内存消耗,并对网络条件变化进行快速,连贯,同步的反应。

ConnMan有各种插件,是一个完全模块化的系统,可以扩展以支持各种有线或无线技术.插件方法可以轻松适应和修改各种用例。诸如DHCP和域名解析之类的配置方法是使用插件实现的。

所有应用程序都应该使用D-Bus与ConnMan进行通信。它与嵌入式Yocto构建系统一起使用,是GENIVI车载信息娱乐规范的一部分。

ConnMan根据GPL v2.0许可证分发。英特尔是ConnMan项目的主要贡献者。

ConnMan体系结构

image

ConnMan内部基础设施

image

功能和特点

以下功能内置于连接管理器中:

  • 通用插件基础结构
  • 设备和网络抽象(具有基本的存储支持)
  • IPv4,IPv4-LL(链路本地)和DHCP - IPv6,DHCPv6和6to4隧道
  • 高级路由和DNS配置
  • 内置DNS代理和智能缓存
  • 内置WISPr热点登录和门户检测
  • 时间和时区配置(使用NTP进行手动和自动)
  • 代理处理(手动和自动使用WPAD)
  • 共享支持(USB,蓝牙和WiFi AP模式)
  • 详细的统计处理(家庭和漫游)

各种插件可以启用网络支持:

  • 以太网插件
  • WiFi插件与WEP40 / WEP128和WPA / WPA2(个人和企业)
  • 蓝牙插件(使用BlueZ)
  • 2G /3G / 4G插件(使用oFono)

Pugins还提供其他功能:

  • 环回接口设置
  • PACrunner代理处理
  • PolicyKit授权支持

Connman D-BUS接口

D-Bus interfaces
  • 应用程序应该使用D-Bus与ConnMan进行通信。
  • 主要入口点是通过管理器对象。
  • Manager对象包含对服务对象的引用(接口)。
  • 其他接口包括VPN,时钟,任务,技术,会话,通知,对等,代理和计数器。

itemeditorimage_59ee6627ea6f6.png

管理D-BUS接口
  • 应用程序入口点是管理器对象
  • 所有的方法和属性都有解释

itemeditorimage_59ee666c6571e.png

管理属性

检索所有全局属性的简单方法如下所示:

  • bus = dbus.SystemBus()
  • manager = dbus.Interface(bus.get_object(“net.connman”, “/”), “net.connman.Manager”)
  • properties = manager.GetProperties()

更改全局属性也很简单。例如,启用所谓的离线模式(AKA飞行模式)就足以设置该属性: manager.SetProperty(“OfflineMode”, dbus.Boolean(1)).

itemeditorimage_59ee66d54b14f.png

服务界面

先进的界面使用户界面可以轻松访问网络细节和用户选择的偏好。这是服务列表和界面。ConnMan维护一个单一的平面和所有可用服务的排序列表。

image

服务顺序 

image

服务状态

  • 每项服务的基本状态都是“闲置”。这意味着此服务目前尚未使用。它也不是试图连接或做任何事情。
  • “关联”状态表示该服务试图建立与网络的低级连接 - 例如,与WiFi接入点关联/连接。
  • 在“配置”状态下,该服务表明它正在尝试检索/配置IP设置。
  • “就绪”状态表示成功连接的设备。这并不意味着它具有默认路由,但基本的IP操作将会成功。
  • 在“断开”状态下,服务指示它将要终止当前连接并返回到“空闲”状态。
  • 另外,“失败”状态表示错误的行为。由于服务未连接,因此与“空闲”状态类似。

image

服务D-Bus接口

以下是如何获取服务对象:

-properties = manager.GetProperties()
-for path in properties["Services"]:
service = dbus.Interface(bus.get_object("net.connman", path), "net.connman.Service")

它还包含连接或断开特定服务的方法。ConnMan还可以根据他的策略或通过外部事件(如插入以太网电缆)自动连接服务。手动连接(或断开)特定服务就像告诉它一样简单。

connect: service.Connect() or service.Disconnect()

image

服务属性

image

技术接口

基本的设备配置任务由技术接口完成。例如,打开和关闭设备(例如通过RFKILL)应通过技术接口完成。
image

技术属性

用户可以通过Manager OfflineMode属性激活离线(飞行)模式。在离线模式下,包括以太网在内的所有技术均关闭。在离线模式下,用户可以使用rfkill命令或某些笔记本电脑中的Fn-Fx组合键,通过Technology Powered属性临时激活单个技术。
image

Connman内部

启动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使用配置文件来调配现有服务。Connman将在/ var / lib / connman /下查找它的配置文件。配置文件名称不得包含除字母或数字之外的其他字符,并且必须具有.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

设置

设置和配置文件会自动为用户经常连接的网络创建。

# cat /var/lib/connman/settings 

[global]

OfflineMode=false

[WiFi]

Enable=true

Tethering=false

[Bluetooth]

Enable=false

Tethering=false

配置文件设置存储在/ var / lib / connman /目录下的服务名称下。VPN设置可以在/ var / lib / connman-vpn /中找到。

编写插件

基本插件包含通过CONNMAN_PLUGIN_DEFINE的插件描述以及通过该描述定义的init / exit回调。

#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)

插件使用技术,网络和设备以及其他基础设施与ConnMan的核心功能进行交互。这里是技术基础设施:

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插件,我们需要通过调用connman_technology_driver_register()来注册它的驱动程序。

connman_technology_driver_register(&tech_driver);

设备基础设施

设备代表给定技术的真实设备。每种技术可能有很多设备。

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,
    };

要注册驱动程序:

connman_device_driver_register(&device_driver);

此外,需要为每个插件编写处理新设备检测的代码;蓝牙插件通过注册BlueZ D-Bus接口的观察器来实现。一旦出现新的蓝牙设备,插件需要通过调用connman_device_create()来通知ConnMan核心。对于蓝牙插件,通话将是:

struct connman_device *device;
 device = connman_device_create("bluetooth", CONNMAN_DEVICE_TYPE_BLUETOOTH)

网络基础设施

连接管理器提供了一种插件来处理为每种类型的技术建立/处理连接的细节。对于蓝牙插件,需要注册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,
    };

然后,调用注册函数:

 connman_network_driver_register(&network_driver);

Connman源文件/基础设施

在运行主循环之前,init函数将在src / main.c中调用。

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有一个标准的命令行客户端connmanctl。它可以运行在两种模式下:

  • 在命令模式下,命令作为connmanctl命令的参数输入,就像systemctl一样。
  • 通过输入不带参数的connmanctl来启动交互模式。提示将变为connmanctl>,表示它正在等待用户命令,就像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 向上。ConnMan将自动处理有线连接。

示例2. 连接到一个开放的接入点

本节中的命令显示如何在命令模式下运行connmanctl。扫描网络connmanctl接受称为技术的简单名称。扫描附近的WiFi网络:

$ 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

您现在应该连接到网络。使用ip addr或connmanctl状态进行检查。

示例3. 连接到受保护的接入点

对于受保护的访问点,向ConnMan守护进程提供一些信息,至少一个密码。

$ 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 

代理程序要求获取更多信息,具体取决于您要连接的网络类型。该代理还将打印有关其所需信息的其他数据,如以下示例中所示。

Agent RequestInput wifi_dc85de828967_38303944616e69656c73_managed_psk Passphrase = [ Type=psk, Requirement=mandatory ] Passphrase?

提供请求的信息(在本例中为密码),然后键入:

connmanctl> quit

如果您提供的信息是正确的,您现在应该连接到受保护的接入点。

Connmanctl其他用法

ConnMan将各种硬件接口称为Technologies。要列出可用技术,请运行: 

$ connmanctl technologies

技术可以通过以下方式打开/关闭:

$ connmanctl enable technology_type

$ connmanctl disable technology_type 

例如,要关闭WiFi:

$ connmanctl disable wifi

ConnMan测试脚本

Connman还提供了基本的Python脚本(例如test-connman,list-services等)。诸如连接,断开连接,自动连接,服务,扫描,启用,禁用,脱机模式,状态等基本操作均通过这些脚本得到支持。

ConnMan 调试 

激活调试

我们可以使用-d命令行选项在ConnMan中激活调试打印。

-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. 

子组件调试

ConnMan的某些组件具有环境变量激活的调试打印。如果设置了环境变量,那么相应的组件会打印一些额外的调试信息。

可以使用以下环境变量:

  • CONNMAN_DHCP_DEBUG:与DHCPv4相关的调试信息
  • CONNMAN_DHCPV6_DEBUG:与DHCPv6相关的调试信息
  • CONNMAN_IPTABLES_DEBUG:使用iptables时的额外信息
  • CONNMAN_RESOLV_DEBUG:名称解析器调试打印。当ConnMan解析自己使用的主机名时使用这些调试打印。请注意,DNS代理调试打印不使用此环境变量。为此,可以使用“-d src / dnsproxy.c”命令行选项。
  • CONNMAN_SUPPLICANT_DEBUG:调试connmand和wpa_supplicant进程之间的通信打印。
  • CONNMAN_WEB_DEBUG:ConnMan在Wispr和6to4组件中进行Internet连接检查时的调试信息。

例如:

CONNMAN_SUPPLICANT_DEBUG=1 src/connmand -n
示例:获取CONNMAN_IPTABLES_DEBUG日志

确保在内核中启用iptables支持:

#zcat /proc/config.gz | grep "CONFIG_IP_NF_IPTABLES"

在内核中必须启用Iptables:

CONFIG_IP_NF_IPTABLES=m or CONFIG_IP_NF_IPTABLES=y

清除所有iptable过滤器:

# iptables –F

在iptable规则下面设置以过滤ICMP数据包(输入,输出和转发数据包):

# 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:"

导出下面的环境变量来启用ConnMan的iptables调试:

# export CONNMAN_IPTABLES_DEBUG=1

启用调试选项启用Connmand连接管理器:

# /usr/sbin/connmand -d -n -I eth0 

ConnMan守护进程消息日志应该在涉及ICMP数据包时显示。

  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值