1.ppp 通过串行实现P2P协议达到互联网连接。
支持TCP/UDP
2.串口<->pppd之间进行数据交换
3.支持平台Linux & Solaris
4.ppp/scripts脚本用于建立连接基于PPP
5.[autopppd] Perl脚本
open(PIDFILE,">/var/run/autopppd.pid");
>>以写入的方式打开指定的文件
print(PIDFILE "$$");
close(PIDFILE);
>>将进程号写入文件,关闭文件
核心为执行pppd,然后分析脚本执行的结果,包括错误码、信号、coredump
my $code = {
0 => { message=>"pppd detached", action=> 2 },
1 => { message=>"fatal error", action=> 2 },
2 => { message=>"options error", action=> 2 },
3 => { message=>"not setuid-root error", action=> 2 },
4 => { message=>"no kernel support for PPP", action=> 2 },
5 => { message=>"SIGINT or SIGTERM or SIGHUP", action=> 1 },
6 => { message=>"Serial port locked", action=> 1 }, # should be 0
7 => { message=>"Serial port open error", action=> 1 },
8 => { message=>"Connect failed", action=> 1 },
9 => { message=>"Could not execute pty command", action=> 1 },
10 => { message=>"PPP negotiation failed", action=> 1 },
11 => { message=>"Peer failed to authenticate", action=> 1 },
12 => { message=>"Link was idle", action=> 1 },
13 => { message=>"Time limit exceeded", action=> 1 },
14 => { message=>"call back not implemented", action=> 2 },
15 => { message=>"peer not responding", action=> 1 },
16 => { message=>"modem hang up", action=> 1 },
17 => { message=>"Serial loopback detected", action=> 1 },
18 => { message=>"Init script failed", action=> 1 },
19 => { message=>"We failed to authenticate", action=> 1 },
};
错误码的定义,以及每种错误码对应的处理动作action(立即退出、retry、延时try等策略)
★autopppd脚本为何每隔一段时间又去重新执行一次pppd程序呢?
★chat协商(程序与参数,将协议赤裸裸的表现出来,极大的方便扩展性)
chat -v
TIMEOUT 3 //3s考虑
ABORT '\nBUSY\r' //如果modem很忙则abort,如果无dial基调则abort,如果无响应则abort,如果50s过去则abort.
ABORT '\nNO ANSWER\r'
ABORT '\nRINGING\r\n\r\nRINGING\r'//如果正则ringing则abort
'' AT //通过AT指令将modem进行静音mute
'OK-+++\c-OK' 'AT&C0&D2S0=0H0'
TIMEOUT 30 //30s执行考虑
OK ATDT$TELEPHONE
CONNECT '' //连接modem,根据name或是passwd的prompt提示,短暂延时,然后回复自己账户信息
assword: $MODEMPASS
"\nNO CARRIER\r"
★chat -v 等待remote的回调,然后根据结果的判断,并成功之下进行登录操作
TIMEOUT 30
ABORT '\nVOICE\r'
'\nRING\r' 'AT&C1A'
CONNECT ''
TIMEOUT 10
ogin:--ogin: $ACCOUNT
TIMEOUT 45
assword: $PASSWORD
★ppp程序基本通过串行的形式外加协议达到了一个顶峰的效果实现了p2p的协议
★chatchat.c 代码
chatchat <filename>
chatchat /var/tmp/p
>>通过将文件转换为管道的方式,传递的取值如同脚本传递参数一般。
>>尝试自己编写模块实现管道的几乎式功能.
★chat.c代码(自动会话建立的程序-登录/拨号)
ANSI C标准中有几个标准预定义宏:
__FILE__ __DATE__ __TIME___ __LINE__ 等
__LINE__:在源代码中插入当前源代码行号;
__FILE__:在源文件中插入当前源文件名;
__DATE__:在源文件中插入当前的编译日期
__TIME__:在源文件中插入当前编译时间;
__STDC__:当要求程序严格遵循ANSI C标准时该标识被赋值为1;
__cplusplus:当编写C++程序时该标识符被定义。
★特性开关,为了编译过去,将一些API进行空定义(编译过去)
1.不支持串口IO
#ifndef TERMIO
#undef TERMIOS
#define TERMIOS
#endif
2.根据运行环境特点进行兼容
#ifdef TERMIO
#include <termio.h>
#endif
#ifdef TERMIOS
#include <termios.h>
#endif
//类型兼容
#ifndef SIGTYPE
#define SIGTYPE void
#endif
//编译兼容ANSI C,C98,C++11等
#ifndef O_NONBLOCK
#define O_NONBLOCK O_NDELAY
#endif
//平台兼容,函数命名保持一致,转换接口使用
#ifdef SUNOS
extern int sys_nerr;
extern char *sys_errlist[];
#define memmove(to, from, n) bcopy(from, to, n)
#define strerror(n) ((unsigned)(n) < sys_nerr? sys_errlist[(n)] :\
"unknown error")
#endif
★特色初始化
FILE* report_fp = (FILE *) 0;
char *report_file = (char *) 0;
★借助宏进行统一底层的支持(上层接口和形势完全一致,方便后续的逻辑)
#ifdef TERMIO
#define term_parms struct termio
#define get_term_param(param) ioctl(0, TCGETA, param)
#define set_term_param(param) ioctl(0, TCSETA, param)
struct termio saved_tty_parameters;
#endif
#ifdef TERMIOS
#define term_parms struct termios
#define get_term_param(param) tcgetattr(0, param)
#define set_term_param(param) tcsetattr(0, TCSANOW, param)
struct termios saved_tty_parameters;
#endif
★几个特殊宏
#define __P(x) ()
#define __P(x) x
★chat定义的子例程
dup_mem、copy_of_、grow内存管理
vfmtmsg格式化、msgf
pack_array数组操作
terminate终止、clean清空
break_sequence断开序列:串口操作tcsendbreak (0, 0);
chat_send送信
write_char、get_char、put_char、get_string、put_string
do_file文件操作
echo_stderr错误显示
set_tty_parameters设置参数
init
unalarm信号、sigalrm、sigint、sigterm、sighup
fatel、
dup_mem(src,size):相当于strdup(src)重新分配堆空间然后拷贝一份返回
copy_of(src):同上
★chat的参数
e echo回显
v Verbose打印过程
s 错误到终端
S 错误到之日文件
f chat的配置文件
t 超时值
r 上报的文件
T 拨号值
U 号码2或是用法
核心流程
①解析参数
②init()
配置pty的参数
③do_file()处理chat的f参数文件
本质也是一读、一写的交互过程
④while(true){
chat_expect();//期待结果
chat_send();//发送结果
}
⑤terminate()退出
★chat的expect动作支持
HANGUP
ABORT
CLR_ABORT
REPORT
CLR_REPORT
TIMEOUT
ECHO
SAY
支持TCP/UDP
2.串口<->pppd之间进行数据交换
3.支持平台Linux & Solaris
4.ppp/scripts脚本用于建立连接基于PPP
5.[autopppd] Perl脚本
open(PIDFILE,">/var/run/autopppd.pid");
>>以写入的方式打开指定的文件
print(PIDFILE "$$");
close(PIDFILE);
>>将进程号写入文件,关闭文件
核心为执行pppd,然后分析脚本执行的结果,包括错误码、信号、coredump
my $code = {
0 => { message=>"pppd detached", action=> 2 },
1 => { message=>"fatal error", action=> 2 },
2 => { message=>"options error", action=> 2 },
3 => { message=>"not setuid-root error", action=> 2 },
4 => { message=>"no kernel support for PPP", action=> 2 },
5 => { message=>"SIGINT or SIGTERM or SIGHUP", action=> 1 },
6 => { message=>"Serial port locked", action=> 1 }, # should be 0
7 => { message=>"Serial port open error", action=> 1 },
8 => { message=>"Connect failed", action=> 1 },
9 => { message=>"Could not execute pty command", action=> 1 },
10 => { message=>"PPP negotiation failed", action=> 1 },
11 => { message=>"Peer failed to authenticate", action=> 1 },
12 => { message=>"Link was idle", action=> 1 },
13 => { message=>"Time limit exceeded", action=> 1 },
14 => { message=>"call back not implemented", action=> 2 },
15 => { message=>"peer not responding", action=> 1 },
16 => { message=>"modem hang up", action=> 1 },
17 => { message=>"Serial loopback detected", action=> 1 },
18 => { message=>"Init script failed", action=> 1 },
19 => { message=>"We failed to authenticate", action=> 1 },
};
错误码的定义,以及每种错误码对应的处理动作action(立即退出、retry、延时try等策略)
★autopppd脚本为何每隔一段时间又去重新执行一次pppd程序呢?
★chat协商(程序与参数,将协议赤裸裸的表现出来,极大的方便扩展性)
chat -v
TIMEOUT 3 //3s考虑
ABORT '\nBUSY\r' //如果modem很忙则abort,如果无dial基调则abort,如果无响应则abort,如果50s过去则abort.
ABORT '\nNO ANSWER\r'
ABORT '\nRINGING\r\n\r\nRINGING\r'//如果正则ringing则abort
'' AT //通过AT指令将modem进行静音mute
'OK-+++\c-OK' 'AT&C0&D2S0=0H0'
TIMEOUT 30 //30s执行考虑
OK ATDT$TELEPHONE
CONNECT '' //连接modem,根据name或是passwd的prompt提示,短暂延时,然后回复自己账户信息
assword: $MODEMPASS
"\nNO CARRIER\r"
★chat -v 等待remote的回调,然后根据结果的判断,并成功之下进行登录操作
TIMEOUT 30
ABORT '\nVOICE\r'
'\nRING\r' 'AT&C1A'
CONNECT ''
TIMEOUT 10
ogin:--ogin: $ACCOUNT
TIMEOUT 45
assword: $PASSWORD
★ppp程序基本通过串行的形式外加协议达到了一个顶峰的效果实现了p2p的协议
★chatchat.c 代码
chatchat <filename>
chatchat /var/tmp/p
>>通过将文件转换为管道的方式,传递的取值如同脚本传递参数一般。
>>尝试自己编写模块实现管道的几乎式功能.
★chat.c代码(自动会话建立的程序-登录/拨号)
ANSI C标准中有几个标准预定义宏:
__FILE__ __DATE__ __TIME___ __LINE__ 等
__LINE__:在源代码中插入当前源代码行号;
__FILE__:在源文件中插入当前源文件名;
__DATE__:在源文件中插入当前的编译日期
__TIME__:在源文件中插入当前编译时间;
__STDC__:当要求程序严格遵循ANSI C标准时该标识被赋值为1;
__cplusplus:当编写C++程序时该标识符被定义。
★特性开关,为了编译过去,将一些API进行空定义(编译过去)
1.不支持串口IO
#ifndef TERMIO
#undef TERMIOS
#define TERMIOS
#endif
2.根据运行环境特点进行兼容
#ifdef TERMIO
#include <termio.h>
#endif
#ifdef TERMIOS
#include <termios.h>
#endif
//类型兼容
#ifndef SIGTYPE
#define SIGTYPE void
#endif
//编译兼容ANSI C,C98,C++11等
#ifndef O_NONBLOCK
#define O_NONBLOCK O_NDELAY
#endif
//平台兼容,函数命名保持一致,转换接口使用
#ifdef SUNOS
extern int sys_nerr;
extern char *sys_errlist[];
#define memmove(to, from, n) bcopy(from, to, n)
#define strerror(n) ((unsigned)(n) < sys_nerr? sys_errlist[(n)] :\
"unknown error")
#endif
★特色初始化
FILE* report_fp = (FILE *) 0;
char *report_file = (char *) 0;
★借助宏进行统一底层的支持(上层接口和形势完全一致,方便后续的逻辑)
#ifdef TERMIO
#define term_parms struct termio
#define get_term_param(param) ioctl(0, TCGETA, param)
#define set_term_param(param) ioctl(0, TCSETA, param)
struct termio saved_tty_parameters;
#endif
#ifdef TERMIOS
#define term_parms struct termios
#define get_term_param(param) tcgetattr(0, param)
#define set_term_param(param) tcsetattr(0, TCSANOW, param)
struct termios saved_tty_parameters;
#endif
★几个特殊宏
#define __P(x) ()
#define __P(x) x
★chat定义的子例程
dup_mem、copy_of_、grow内存管理
vfmtmsg格式化、msgf
pack_array数组操作
terminate终止、clean清空
break_sequence断开序列:串口操作tcsendbreak (0, 0);
chat_send送信
write_char、get_char、put_char、get_string、put_string
do_file文件操作
echo_stderr错误显示
set_tty_parameters设置参数
init
unalarm信号、sigalrm、sigint、sigterm、sighup
fatel、
dup_mem(src,size):相当于strdup(src)重新分配堆空间然后拷贝一份返回
copy_of(src):同上
★chat的参数
e echo回显
v Verbose打印过程
s 错误到终端
S 错误到之日文件
f chat的配置文件
t 超时值
r 上报的文件
T 拨号值
U 号码2或是用法
核心流程
①解析参数
②init()
配置pty的参数
③do_file()处理chat的f参数文件
本质也是一读、一写的交互过程
④while(true){
chat_expect();//期待结果
chat_send();//发送结果
}
⑤terminate()退出
★chat的expect动作支持
HANGUP
ABORT
CLR_ABORT
REPORT
CLR_REPORT
TIMEOUT
ECHO
SAY