记录: 判断经过batman-adv协议的mesh自组网拓扑节点设备个数
1、可以通过二层网络工具包batctl确定与当前节点无线组网的节点个数,batctl n命令可以展示当前节点无线组网的邻居节点的mac地址
2、但是面临一个问题是不够准确,断掉一个设备很长时间里还是会在列表中出现,为了解决这个问题就用了ping的方式来实时判断设备在线与否
pox_system("iw mesh0 mpath dump|awk {'print $2'}|sort -u |grep -v ADDR > /tmp/mesh0");
modefd = popen("uci get wireless.default_radio0.mode", "r");
if (fgets(mac_line, 256, modefd) != NULL)
{
mac_line[strlen(mac_line) - 1] = '\0';
}
pclose(modefd);
//mode is mesh
if (strcmp(mac_line,"mesh") == 0)
{
info = fopen("/tmp/mesh0", "r");
if (NULL != info)
{
memset(mac_buf, 0, sizeof(mac_buf));
memset(line, 0, sizeof(line));
while (fgets(line, _LINE_LENGTH, info) != NULL)
{
*(line + strlen(line) -1) = '\0';
//Check whether the MAC address is correct
if ((strlen(line) != 17) && (line[2] != ':') && (line[5] != ':'))
{
break;
}
strcat(mac_buf, line);
sprintf(node_buf, "batctl ping -c 3 %s|grep packets|awk {'print $6'}", line);
fd = popen(node_buf,"r");
if ( NULL == fd )
{
debug(LOG_DEBUG, "ping other node fail,mac_buf host is not online");
}
memset(str, 0, sizeof(str));
if (fgets(str, sizeof(str), fd) == NULL)
{
debug(LOG_DEBUG,"the results of ping error!");
}
else
{
str[strlen(str) - 1] = '\0';
if (strcmp(str, "100%") == 0)
{
debug(LOG_DEBUG, "device in offline");
}
else
{
device_count++; //The number of Nodes in the Network
}
}
pclose(fd);
}
}
else
{
debug(LOG_DEBUG, "popen iw mesh0 mpath dump is NULL.");
}
fclose(info);
}
else //mode is ap/station
{
device_count = 0;
}
3、但是后续组网设备数量过多(二三十台以上)会有网络环路问题,思考如何既能实时显示又能减少网络负担,所以根据最后一次收到包的时间来判断设备是否在线
FILE *time_info;
pox_system("batctl n |awk {'print $2'} |grep -v Neighbor > /tmp/recvpak_time");
time_info = fopen("/tmp/recvpak_time", "r");
if (NULL != time_info)
{
memset(line, 0, sizeof(line));
while (fgets(line, _LINE_LENGTH, time_info) != NULL)
{
*(line + strlen(line) -1) = '\0';
recvtime = strtod(line, NULL);
if (recvtime < 15)
{
device_count++;
}
memset(line, 0, sizeof(line));
}
}
else
{
debug(LOG_DEBUG, "batctl n get neighbor node time is NULL!");
}
fclose(time_info);