| 
                        副标题[/!--empirenews.page--]
                         
	    Wifidog是一个linux下开源的认证网关软件,它主要用于配合认证服务器实现无线路由器的认证放行功能。
 
	wifidog是一个后台的服务程序,可以通过wdctrl命令对wifidog主程序进行控制。
 
	本文解释wifidog在启动阶段所做的初始化主要工作(代码片段1.1)
 
	- 
		初始化配置(先将配置结构体初始化为默认值,在读取配置文件修改配置结构体)
	
 
	- 
		初始化已连接客户端列表(如果是通过wdctrl重启wifidog,将会读取之前wifidog的已连接客户端列表 代码片段1.2 代码片段1.3)
	
 
	- 
		如无特殊情况,分离进程,建立守护进程 (代码片段1.1)
	
 
	- 
		添加多个http请求回调函数(包括404错误回调函数) (见之后章节)
	
 
	- 
		摧毁删除现有的iptables路由表规则 (见之后章节)
	
 
	- 
		建立新的iptables路由表规则 (见之后章节)
	
 
	- 
		启动多个功能线程 (见之后章节)
	
 
	- 
		循环等待客户端连接 (见之后章节)
	
 
 
	 
代码片段1.1 
	
		
			
				点击(此处)折叠或打开
			 
		
		
			
				- 
					int main(int argc, char **argv) {
 
 
				 
				- 
					
 
				 
				- 
					    s_config *config = config_get_config(); //就是返回全局变量config结构体的地址
 
				 
				- 
					    config_init(); //初始化全局变量config结构体为默认值
 
				 
				- 
					
 
				 
				- 
					    parse_commandline(argc, argv); //根据传入参数执行操作(如果参数有-x则会设置restart_orig_pid为已运行的wifidog的pid)
 
				 
				- 
					
 
				 
				- 
					    /* Initialize the config */
 
				 
				- 
					    config_read(config->configfile); //根据配置文件设置全局变量config结构体
 
				 
				- 
					    config_validate(); //判断GatewayInterface和AuthServer是否为空,空则无效退出程序。
 
				 
				- 
					
 
				 
				- 
					    /* Initializes the linked list of connected clients */
 
				 
				- 
					    client_list_init(); //将已连接客户端链表置空。
 
				 
				- 
					
 
				 
				- 
					    /* Init the signals to catch chld/quit/etc */
 
				 
				- 
					    init_signals(); //初始化一些信号
 
				 
				- 
					
 
				 
				- 
					    if (restart_orig_pid) { //用于restart,如果有已运行的wifidog,先会kill它
 
				 
				- 
					        /*
 
				 
				- 
					         * We were restarted and our parent is waiting for us to talk to it over the socket
 
				 
				- 
					         */
 
				 
				- 
					        get_clients_from_parent(); //从已运行的wifidog中获取客户端列表,详见 代码片段1.2
 
				 
				- 
					
 
				 
				- 
					        /*
 
				 
				- 
					         * 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) { //kill已运行的wifidog
 
				 
				- 
					            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) { //创建为守护进程,config->daemon默认值为-1
 
				 
				- 
					
 
				 
				- 
					        debug(LOG_INFO, "Forking into background");
 
				 
				- 
					
 
				 
				- 
					        switch(safe_fork()) {
 
				 
				- 
					            case 0: /* child */
 
				 
				- 
					                setsid(); //创建新会话,脱离此终端,实现守护进程
 
				 
				- 
					                append_x_restartargv();
 
				 
				- 
					                main_loop(); //进入主循环(核心代码在此)。
 
				 
				- 
					                break;
 
				 
				- 
					
 
				 
				- 
					            default: /* parent */
 
				 
				- 
					                exit(0);
 
				 
				- 
					                break;
 
				 
				- 
					        }
 
				 
				- 
					    }
 
				 
				- 
					    else {
 
				 
				- 
					        append_x_restartargv();
 
				 
				- 
					        main_loop();
 
				 
				- 
					    }
 
				 
				- 
					
 
				 
				- 
					    return(0); /* never reached */
 
				 
				- 
					} 
				
 
			 
		
	
 
	
		代码片段1.2(获取已启动的wifidog的客户端列表):
	 
	
		此段代表描述了新启动的wifidog如何从已启动的wifidog程序中获取已连接的客户端列表。发送端见 代码片段1.3 
	 
 
	
		
			
                                                (编辑:滁州站长网) 
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! 
                     |