源码文件夹src
从gateway,c 中的man函数开始:
int main(int argc, char **argv)
{
{
s_config *config = config_get_config();//
是一个获取全局变量config的函数
/*
全局变量config 是关于网关全部配置的结构体,config 的成员变量都在wifidog.conf 文件里,这个变量时贯穿全部流程的一个全局变量*/
char mcode = '0';
memset(PRODUCT_SERIALNO, 0, sizeof(PRODUCT_SERIALNO));
/*
设置config 成员的默认初始值,有些设置成NULL,详细的看conf.c 的config_init函数,对照config 的结构体定义*/
config_init();
/*
通过命令行获取config 结构体的某些成员值,如wifidog -c /etc/wifidog.conf, 通过-c 设置configfile 的值*/
parse_commandline(argc, argv, &mcode);
/* Initialize the config */
/*
读取configfile, 设置config 的成员值。这里要注意,config_read会覆盖parse_commandline 设置的config 成员值,parse_commandline会覆盖config_init设置的config 成员值*/
config_read(config->configfile, mcode);
/*
检查config 的auth_server 和 gw_interface 值是否为空,因为这两个变量值最为重要*/
config_validate();
/*display mcode */
if('1' == mcode) {
printf("%s", PRODUCT_SERIALNO);
exit(0);
}
/*display mcode */
if('1' == mcode) {
printf("%s", PRODUCT_SERIALNO);
exit(0);
}
/* Initializes the linked list of connected clients */
/*
这个函数涉及到另一个全局变量firstclient, 这个结构体主要保存客户端的信息,如ip, mac, token, incoming, outgoing等,客户端信息被存储到一个链表里,firstclient是链表头*/
client_list_init();
/* Init the signals to catch chld/quit/etc */
/*
处理一些信号,如quit 信号,会清理一些iptables 规则,杀掉ping 协议线程和counter 协议线程,前者是网关不断发ping 包给认证服务器证明自己是活的,后者是网关向认证服务器更新客户端信息*/
init_signals();
if (restart_orig_pid) {
/*
* We were restarted and our parent is waiting for us to talk to it over the socket
*/
get_clients_from_parent();
/*
* At this point the parent will start destroying itself and the firewall. Let it finish it's job before we continue
*/
while (kill(restart_orig_pid, 0) != -1) {
debug(LOG_INFO, "Waiting for parent PID %d to die before continuing loading", restart_orig_pid);
sleep(1);
}
debug(LOG_INFO, "Parent PID %d seems to be dead. Continuing loading.");
}
if (config->daemon) {
debug(LOG_INFO, "Forking into background");
switch(safe_fork()) {
case 0: /* child */
setsid();
append_x_restartargv();
if (restart_orig_pid) {
/*
* We were restarted and our parent is waiting for us to talk to it over the socket
*/
get_clients_from_parent();
/*
* At this point the parent will start destroying itself and the firewall. Let it finish it's job before we continue
*/
while (kill(restart_orig_pid, 0) != -1) {
debug(LOG_INFO, "Waiting for parent PID %d to die before continuing loading", restart_orig_pid);
sleep(1);
}
debug(LOG_INFO, "Parent PID %d seems to be dead. Continuing loading.");
}
if (config->daemon) {
debug(LOG_INFO, "Forking into background");
switch(safe_fork()) {
case 0: /* child */
setsid();
append_x_restartargv();
main_loop();//
wifidog 流程开始
break;
default: /* parent */
exit(0);
break;
}
} else {
append_x_restartargv();
default: /* parent */
exit(0);
break;
}
} else {
append_x_restartargv();
main_loop();//
wifidog 流程开始
}
return(0); /* never reached */
return(0); /* never reached */
}