wifidog源码分析 - 认证服务器心跳检测线程
引言
但wifidog启动时,会自动启动认证服务器心跳检测线程,此线程默认每隔60s与认证服务器交互一次,会将路由器的信息(系统启动时长,内存使用情况和系统平均负载)告知认证服务器,并通过一个"ping"字符串作为信号,而当认证服务器接收到此数据包后,会返回一个"pong"给路由器,具体我们看看代码。
代码片段1.1
此段代码很简单,就是调用ping函数,然后等待60s
void
thread_ping(void *arg)
{
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t cond_mutex = PTHREAD_MUTEX_INITIALIZER;
struct timespec timeout; while () {
/* 调用ping,具体代码看 代码片段1.2 */
debug(LOG_DEBUG, "Running ping()");
ping(); /* 睡眠一个checkinterval,默认为60s */
timeout.tv_sec = time(NULL) + config_get_config()->checkinterval;
timeout.tv_nsec = ; pthread_mutex_lock(&cond_mutex); pthread_cond_timedwait(&cond, &cond_mutex, &timeout); pthread_mutex_unlock(&cond_mutex);
}
代码片段1.2
static void
ping(void)
{
ssize_t numbytes;
size_t totalbytes;
int sockfd, nfds, done;
char request[MAX_BUF];
fd_set readfds;
struct timeval timeout;
FILE * fh;
unsigned long int sys_uptime = ;
unsigned int sys_memfree = ;
float sys_load = ;
t_auth_serv *auth_server = NULL;
auth_server = get_auth_server(); debug(LOG_DEBUG, "Entering ping()"); /* 其实认证服务器就是一个web服务器,路由器跟他做通信行为就是通过发送http请求进行通信,首先先连接认证服务器的http端口,获取其socket */
sockfd = connect_auth_server();
if (sockfd == -) {
/* 无法连接认证服务器,connect_auth_server分析见 代码片段1.3 */
return;
} /*
* 从/proc文件系统获取路由器信息
*/
if ((fh = fopen("/proc/uptime", "r"))) {
fscanf(fh, "%lu", &sys_uptime);
fclose(fh);
}
if ((fh = fopen("/proc/meminfo", "r"))) {
while (!feof(fh)) {
if (fscanf(fh, "MemFree: %u", &sys_memfree) == ) {
while (!feof(fh) && fgetc(fh) != '\n');
}
else {
break;
}
}
fclose(fh);
}
if ((fh = fopen("/proc/loadavg", "r"))) {
fscanf(fh, "%f", &sys_load);
fclose(fh);
} /*
* 准备http请求包
*/
snprintf(request, sizeof(request) - ,
"GET %s%sgw_id=%s&sys_uptime=%lu&sys_memfree=%u&sys_load=%.2f&wifidog_uptime=%lu HTTP/1.0\r\n"
"User-Agent: WiFiDog %s\r\n"
"Host: %s\r\n"
"\r\n",
auth_server->authserv_path,
auth_server->authserv_ping_script_path_fragment,
config_get_config()->gw_id,
sys_uptime,
sys_memfree,
sys_load,
(long unsigned int)((long unsigned int)time(NULL) - (long unsigned int)started_time),
VERSION,
auth_server->authserv_hostname); debug(LOG_DEBUG, "HTTP Request to Server: [%s]", request);
/* 发送 */
send(sockfd, request, strlen(request), ); debug(LOG_DEBUG, "Reading response"); numbytes = totalbytes = ;
done = ;
do {
FD_ZERO(&readfds);
FD_SET(sockfd, &readfds);
/* 设置超时30s */
timeout.tv_sec = ;
timeout.tv_usec = ;
nfds = sockfd + ; nfds = select(nfds, &readfds, NULL, NULL, &timeout); if (nfds > ) {
/* 多路复用 */
numbytes = read(sockfd, request + totalbytes, MAX_BUF - (totalbytes + ));
if (numbytes < ) {
debug(LOG_ERR, "An error occurred while reading from auth server: %s", strerror(errno));
close(sockfd);
return;
}
else if (numbytes == ) {
done = ;
}
else {
totalbytes += numbytes;
debug(LOG_DEBUG, "Read %d bytes, total now %d", numbytes, totalbytes);
}
}
else if (nfds == ) {
debug(LOG_ERR, "Timed out reading data via select() from auth server");
close(sockfd);
return;
}
else if (nfds < ) {
debug(LOG_ERR, "Error reading data via select() from auth server: %s", strerror(errno));
close(sockfd);
return;
}
} while (!done);
close(sockfd); debug(LOG_DEBUG, "Done reading reply, total %d bytes", totalbytes); request[totalbytes] = '\0'; debug(LOG_DEBUG, "HTTP Response from Server: [%s]", request);
/* 判断认证服务器返回包中有没有"Pong"字符串 */
if (strstr(request, "Pong") == ) {
debug(LOG_WARNING, "Auth server did NOT say pong!"); }
else {
debug(LOG_DEBUG, "Auth Server Says: Pong");
} return;
}
代码片段1.3
connect_auth_server函数用于连接认证服务器并返回socket套接字,其具体实现是通过_connect_auth_server实现的,而在_connect_auth_server中,递归认证服务器列表,每次递归中首先会根据认证服务器域名获取ip,如果失败,会通过公共网站判断是否为DNS问题,再判断是否为认证服务器问题,如果都失败,继续递归,否则返回认证服务器socket。
int connect_auth_server() {
int sockfd; LOCK_CONFIG();
/* 连接认证服务器 */
sockfd = _connect_auth_server();
UNLOCK_CONFIG(); if (sockfd == -) {
debug(LOG_ERR, "Failed to connect to any of the auth servers");
/* 标记认证服务器离线 */
mark_auth_offline();
}
else {
debug(LOG_DEBUG, "Connected to auth server");
/* 标记认证服务器在线 */
mark_auth_online();
}
return (sockfd);
} int _connect_auth_server(int level) {
s_config *config = config_get_config();
t_auth_serv *auth_server = NULL;
struct in_addr *h_addr;
int num_servers = ;
char * hostname = NULL;
/* 公共网站,用于判断DNS问题 */
char * popular_servers[] = {
"www.google.com",
"www.yahoo.com",
NULL
};
char ** popularserver;
char * ip;
struct sockaddr_in their_addr;
int sockfd; /* 用于递归,因为可能会有多个认证服务器,如果第一个认证服务器无法连接,会递归尝试连接后面的认证服务器,此参数用于递归判断的,当成功连接任意一个认证服务器后停止 */
level++; /*
* 获取认证服务器数量
*/
for (auth_server = config->auth_servers; auth_server; auth_server = auth_server->next) {
num_servers++;
}
debug(LOG_DEBUG, "Level %d: Calculated %d auth servers in list", level, num_servers);
/* 已经尝试递归连接所有认证服务器,都不能连接 */
if (level > num_servers) {
return (-);
} /*
* 获取认证服务器列表中的第一个认证服务器
*/
auth_server = config->auth_servers;
hostname = auth_server->authserv_hostname;
debug(LOG_DEBUG, "Level %d: Resolving auth server [%s]", level, hostname);
h_addr = wd_gethostbyname(hostname);
if (!h_addr) {
/*
* DNS解析错误,尝试解析公共网站判断是否为DNS错误
*/
debug(LOG_DEBUG, "Level %d: Resolving auth server [%s] failed", level, hostname); for (popularserver = popular_servers; *popularserver; popularserver++) {
debug(LOG_DEBUG, "Level %d: Resolving popular server [%s]", level, *popularserver);
h_addr = wd_gethostbyname(*popularserver);
/* 公共网站DNS解析正确 */
if (h_addr) {
debug(LOG_DEBUG, "Level %d: Resolving popular server [%s] succeeded = [%s]", level, *popularserver, inet_ntoa(*h_addr));
break;
}
else {
debug(LOG_DEBUG, "Level %d: Resolving popular server [%s] failed", level, *popularserver);
}
} if (h_addr) {
/* DNS正确,尝试递归下一个认证服务器 */
free (h_addr); debug(LOG_DEBUG, "Level %d: Marking auth server [%s] as bad and trying next if possible", level, hostname);
if (auth_server->last_ip) {
free(auth_server->last_ip);
auth_server->last_ip = NULL;
}
/* 将此认证服务器放入bad_server链表,并将config->auth_server指向认证服务器的下一个节点 */
mark_auth_server_bad(auth_server);
/* 递归 */
return _connect_auth_server(level);
}
else {
/* DNS问题,标记路由器离线 */
mark_offline();
debug(LOG_DEBUG, "Level %d: Failed to resolve auth server and all popular servers. "
"The internet connection is probably down", level);
return(-);
}
}
else {
/* DNS解析成功 */
ip = safe_strdup(inet_ntoa(*h_addr));
debug(LOG_DEBUG, "Level %d: Resolving auth server [%s] succeeded = [%s]", level, hostname, ip); if (!auth_server->last_ip || strcmp(auth_server->last_ip, ip) != ) {
/* DNS解析到的IP与我们上一次连接的IP不同,更新上一次连接的IP */
debug(LOG_DEBUG, "Level %d: Updating last_ip IP of server [%s] to [%s]", level, hostname, ip);
if (auth_server->last_ip) free(auth_server->last_ip);
auth_server->last_ip = ip; /* 将此新的认证服务器IP添加到iptables中的可访问外网地址中 */
fw_clear_authservers();
fw_set_authservers();
}
else {
/*
* DNS解析到的IP与我们上一次连接的IP相同
*/
free(ip);
} /*
* 连接
*/
debug(LOG_DEBUG, "Level %d: Connecting to auth server %s:%d", level, hostname, auth_server->authserv_http_port);
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(auth_server->authserv_http_port);
their_addr.sin_addr = *h_addr;
memset(&(their_addr.sin_zero), '\0', sizeof(their_addr.sin_zero));
free (h_addr); if ((sockfd = socket(AF_INET, SOCK_STREAM, )) == -) {
debug(LOG_ERR, "Level %d: Failed to create a new SOCK_STREAM socket: %s", strerror(errno));
return(-);
} if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -) {
/*
* 连接失败
* 将此认证服务器放入bad_server链表,并将config->auth_server指向认证服务器的下一个节点
*/
debug(LOG_DEBUG, "Level %d: Failed to connect to auth server %s:%d (%s). Marking it as bad and trying next if possible", level, hostname, auth_server->authserv_http_port, strerror(errno));
close(sockfd);
mark_auth_server_bad(auth_server);
return _connect_auth_server(level); /* Yay recursion! */
}
else {
/*
* 连接成功
*/
debug(LOG_DEBUG, "Level %d: Successfully connected to auth server %s:%d", level, hostname, auth_server->authserv_http_port);
return sockfd;
}
}
}
wifidog源码分析 - 认证服务器心跳检测线程的更多相关文章
- wifidog源码分析 - 用户连接过程
引言 之前的文章已经描述wifidog大概的一个工作流程,这里我们具体说说wifidog是怎么把一个新用户重定向到认证服务器中的,它又是怎么对一个已认证的用户实行放行操作的.我们已经知道wifidog ...
- wifidog源码分析 - wifidog原理
wifidog是一个用于配合认证服务器实现无线网页认证功能的程序,常见的情景就是使用于公共场合的无线wifi接入点,首先移动设备会连接公共wifi接入点,之后会弹出网页要求输入用户名密码,认证过后才能 ...
- wifidog源码分析 - wifidog原理 tiger
转:http://www.cnblogs.com/tolimit/p/4223644.html wifidog源码分析 - wifidog原理 wifidog是一个用于配合认证服务器实现无线网页认证功 ...
- Solr4.8.0源码分析(3)之index的线程池管理
Solr4.8.0源码分析(3)之index的线程池管理 Solr建索引时候是有最大的线程数限制的,它由solrconfig.xml的<maxIndexingThreads>8</m ...
- HDFS源码分析数据块复制监控线程ReplicationMonitor(二)
HDFS源码分析数据块复制监控线程ReplicationMonitor(二)
- 【Zookeeper】源码分析之服务器(二)
一.前言 前面阐述了服务器的总体框架,下面来分析服务器的所有父类ZooKeeperServer. 二.ZooKeeperServer源码分析 2.1 类的继承关系 public class ZooKe ...
- 【Zookeeper】源码分析之服务器(二)之ZooKeeperServer
一.前言 前面阐述了服务器的总体框架,下面来分析服务器的所有父类ZooKeeperServer. 二.ZooKeeperServer源码分析 2.1 类的继承关系 public class ZooKe ...
- 【Zookeeper】源码分析之服务器(四)
一.前言 前面分析了LeaderZooKeeperServer,接着分析FollowerZooKeeperServer. 二.FollowerZooKeeperServer源码分析 2.1 类的继承关 ...
- Django-rest-framework源码分析----认证
一.前言 1.1.安装 两种方式: github pip直接安装 pip install django-rest-framework 1.2.需要先了解的一些知识 理解下面两个知识点非常重要,djan ...
随机推荐
- Python 练习: 打印0到99小于50或大于70的数字
for i in range(100): if i < 50 or i > 70: print(i) 注意: range(100) 表示 0 到 99 个数字
- 创建一个背景为蓝色的pygame窗口
import sys import pygame def creat_screen(): #初始化pygame pygame.init() #设置窗口大小并保存在screen对象中 screen = ...
- 2017-12-01 中英文代码对比之ZLOGO 4 & LOGO
基于前文中文编程语言之Z语言初尝试: ZLOGO 4的一些评论, 此文尝试作一个非常简单的代码对比, 使讨论更加有实例根据. 下图是节选自前文最后的示例代码, 由于选取的对照LOGO版本 (alanc ...
- JS中数组去重的九方法
数组去重方法 方法一:运用set结构特点:存储的数据没有重复的,结果为对象,再用Array.from()转换成数组 var arr = [1,1,2,1,3,4,5] ...
- link标签链接CSS和@import加载的区别
link:基本语法 <link rel="stylesheet" href="路径"> @import 基本语法 <style> @im ...
- AngularJS ui-router刷新子页面路由
网上有各种刷新子页面路由的方法,但是不知道为什么放到我的页面就不行了,尴尬! 网上的方法有: <a href="#" ui-sref="app.toMenu&quo ...
- Android Stuido代码混淆
一.Android Studio 代码混淆基本配置首先我们要在build.gradle里设置 miifyEnabled 里改为true,表示可以混淆 proguardFiles getDefaultP ...
- Android View体系(四)从源码解析Scroller
在Android View体系(二)实现View滑动的六种方法这篇文章中我们讲到了用Scroller来实现View的滑动,所以这篇文章我们就不介绍Scroller是如何使用的了,本篇就从源码来分析下S ...
- Android spinner默认样式不支持换行和修改字体样式的解决方法
在spinner中显示的数据过多,需要换行,而Android自身提供的android.R.layout.simple_spinner_dropdown_item样式不支持换行,因此参考android提 ...
- HttpClient与浏览器调用服务接口差异
我用httpclient访问接口,统计图有些不均匀,差距较大 ,有时只有几十毫秒,下图看到这种情况占多数,600-800毫秒之间的算是浏览器正常的产生调用接口的时间耗时 然后用jmeter跑时都是均值 ...