MT7688学习笔记(13)——C++编程配置PPPoE拨号

194 阅读3分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 4 月更文挑战」的第 8 天,点击查看活动详情

一、PPPoE拨号上网语法

在设置pppoe时要确保设备WAN口连接的外网具备PPPoE的服务器,比如小区宽带、光猫或上联ADSL猫。PPPoE拨号上网语法如下:

选项说明可选值及说明必填
proto协议类型pppoe
ifname设备名称eth0.2
macaddrWAN口MAC地址首次数据根据factory分区内参数自动生成
mtu修改最大数据包大小,默认不用设置数值
username拨号用的账号字符串
password拨号用的密码字符串
ac使用指定的访问集中器进行连接字符串
service连接的服务名称字符串
connect连接时执行的外部脚本字符串
disconnect断开连接时执行的外部脚本字符串
demand等待多久没有活动就断开PPPoE连接数字,单位为秒
dnsDNS服务器地址字符串
pppd_options用于pppd进程执行时的附加参数字符串

二、修改编译前PPPoE配置文件

2.1 在openwrt-hiwooya-stable-master中

配置文件位置/work/openwrt-hiwooya-stable-master/files/etc/config/network

config interface 'loopback'
        option ifname 'lo'
        option proto 'static'
        option ipaddr '127.0.0.1'
        option netmask '255.0.0.0'

config globals 'globals'
        option ula_prefix 'fdd8:c0ec:8d29::/48'

config interface 'lan'
        option ifname 'eth0.1'
        option force_link '1'
        option macaddr '0c:ef:af:cf:e1:b3'
        option type 'bridge'
        option proto 'static'
        option ipaddr '192.168.100.1'
        option netmask '255.255.255.0'
        option ip6assign '60'

config interface 'wan'
        option ifname 'eth0.2'
        option _orig_ifname 'eth0.2'
        option _orig_bridge 'false'
        option proto 'dhcp'

config interface 'wan6'
        option ifname 'eth0.2'
        option proto 'dhcpv6'

config interface 'wwan'
        option ifname 'apcli0'
        option proto 'dhcp'

config switch
        option name 'switch0'
        option reset '1'
        option enable_vlan '1'

config switch_vlan
        option device 'switch0'
        option vlan '1'
        option ports '1 2 3 4 6t'

config switch_vlan
        option device 'switch0'
        option vlan '2'
        option ports '0 6t'

config interface '4g'
        option proto 'dhcp'
        option ifname 'eth1'
        option dns '114.114.114.114'

在wan配置中添加 option usernameoption password

config interface 'wan'
        option ifname 'eth0.2'
        option _orig_ifname 'eth0.2'
        option _orig_bridge 'false'
        option proto 'dhcp'
        option username
        option password

2.2 在OpenWrt 1505中

配置文件位置/work/openwrt/package/base-files/files/bin/config_generate

wan) uci -q batch <<EOF
set network.$1.proto='dhcp'
delete network.wan6
set network.wan6='interface'
set network.wan6.ifname='$ifname'
set network.wan6.proto='dhcpv6'
EOF

在wan配置中添加 option usernameoption password

wan) uci -q batch <<EOF
set network.$1.proto='dhcp'
set network.$1.username=' '
set network.$1.password=' '
delete network.wan6
set network.wan6='interface'
set network.wan6.ifname='$ifname'
set network.wan6.proto='dhcpv6'
EOF

三、查看编译后PPPoE配置文件

root@hi-wooya:/etc/config# vim network

config interface 'loopback'
        option ifname 'lo'
        option proto 'static'
        option ipaddr '127.0.0.1'
        option netmask '255.0.0.0'

config globals 'globals'
        option ula_prefix 'fdd8:c0ec:8d29::/48'

config interface 'lan'
        option ifname 'eth0.1'
        option force_link '1'
        option macaddr '0c:ef:af:cf:e1:b3'
        option type 'bridge'
        option proto 'static'
        option ipaddr '192.168.100.1'
        option netmask '255.255.255.0'
        option ip6assign '60'

config interface 'wan'
        option ifname 'eth0.2'
        option _orig_ifname 'eth0.2'
        option _orig_bridge 'false'
        option proto dhcp
        option username  
        option password  
  • proto 位于24行
  • username 位于25行
  • password 位于26行

四、C++程序

/**
 @brief 修改文件某行内容
 @param pFileName	-[in] 文件名
 @param lineNum		-[in] 行号 
 @param content		-[in] 修改的内容
 @return 无
*/
static void modifyContentInFile(char *pFileName, int lineNum, string content)
{
	if(!pFileName)
	{
		return ;
	}
	ifstream in;
	char line[1024] = {'\0'};
	in.open(pFileName);
	int i = 0;
	string tempStr;
	while(in.getline(line, sizeof(line)))
	{
		i++;
		if(lineNum != i)
		{
			tempStr += Char2Str(line);
		}
		else
		{
	       tempStr += content;
		}
		tempStr += '\n';
	}
	in.close();
	ofstream out;
	out.open(pFileName);
	out.flush();
	out<<tempStr;
	out.close(); 
}

void pppoe(void)
{
    string protocol = "\toption proto pppoe";
    string username = "\toption username 12345678";
	string password = "\toption password 12345678";
	unsigned char protocolLine = 24;														// 在文件中第24行
	unsigned char usernameLine = 25;														// 在文件中第25行
	unsigned char passwordLine = 26;														// 在文件中第26行
	char fileName[] = "/etc/config/network";

    modifyContentInFile(fileName, protocolLine, protocol);
    modifyContentInFile(fileName, usernameLine, username);
    modifyContentInFile(fileName, passwordLine, password);

    system("/etc/init.d/network restart");								// 重启network服务进程,使配置生效
}

执行pppoe函数,重启网络后拨号要等待30秒~5分钟,如果参数没错,就会拨号成功。


• 由 Leung 写于 2019 年 6 月 19 日

• 参考:OpenWrt智能路由器系统开发——跟hoowa学智能路由