点击(此处)折叠或打开
-
void get_clients_from_parent(void) {
-
int sock;
-
struct sockaddr_un sa_un;
-
s_config * config = NULL;
-
char linebuffer[MAX_BUF];
-
int len = 0;
-
char *running1 = NULL;
-
char *running2 = NULL;
-
char *token1 = NULL;
-
char *token2 = NULL;
-
char onechar;
-
char *command = NULL;
-
char *key = NULL;
-
char *value = NULL;
-
t_client * client = NULL;
-
t_client * lastclient = NULL;
-
-
config = config_get_config();
-
-
debug(LOG_INFO, "Connecting to parent to download clients");
-
-
/* 连接socket */
-
sock = socket(AF_UNIX, SOCK_STREAM, 0);
-
memset(&sa_un, 0, sizeof(sa_un));
-
sa_un.sun_family = AF_UNIX;
-
strncpy(sa_un.sun_path, config->internal_sock, (sizeof(sa_un.sun_path) - 1)); //config->internal_sock的值为"/tmp/wifidog.sock"
-
-
/* 连接已启动的wifidog */
-
if (connect(sock, (struct sockaddr *)&sa_un, strlen(sa_un.sun_path) + sizeof(sa_un.sun_family))) {
-
debug(LOG_ERR, "Failed to connect to parent (%s) - client list not downloaded", strerror(errno));
-
return;
-
}
-
-
debug(LOG_INFO, "Connected to parent. Downloading clients");
-
-
LOCK_CLIENT_LIST();
-
-
command = NULL;
-
memset(linebuffer, 0, sizeof(linebuffer));
-
len = 0;
-
client = NULL;
-
/* 接收数据,逐个字符接收 */
-
/* 数据包格式为 CLIENT|ip=%s|mac=%s|token=%s|fw_connection_state=%u|fd=%d|counters_incoming=%llu|counters_outgoing=%llu|counters_last_updated=%lun */
-
while (read(sock, &onechar, 1) == 1) {
-
if (onechar == 'n') {
-
/* 如果接收到末尾('n'),则转为' ' */
-
onechar = ' ';
-
}
-
linebuffer[len++] = onechar;
-
-
if (!onechar) {
-
/* 以下将数据转化为t_client结构体添加到客户端列表 */
-
debug(LOG_DEBUG, "Received from parent: [%s]", linebuffer);
-
running1 = linebuffer;
-
while ((token1 = strsep(&running1, "|")) != NULL) {
-
if (!command) {
-
/* The first token is the command */
-
command = token1;
-
}
-
else {
-
/* Token1 has something like "foo=bar" */
-
running2 = token1;
-
key = value = NULL;
-
while ((token2 = strsep(&running2, "=")) != NULL) {
-
if (!key) {
-
key = token2;
-
}
-
else if (!value) {
-
value = token2;
-
}
-
}
-
}
-
-
if (strcmp(command, "CLIENT") == 0) {
-
/* This line has info about a client in the client list */
-
if (!client) {
-
/* Create a new client struct */
-
client = safe_malloc(sizeof(t_client));
-
memset(client, 0, sizeof(t_client));
-
}
-
}
-
-
if (key && value) {
-
if (strcmp(command, "CLIENT") == 0) {
-
/* Assign the key into the appropriate slot in the connection structure */
-
if (strcmp(key, "ip") == 0) {
-
client->ip = safe_strdup(value);
-
}
-
else if (strcmp(key, "mac") == 0) {
-
client->mac = safe_strdup(value);
-
}
-
else if (strcmp(key, "token") == 0) {
-
client->token = safe_strdup(value);
-
}
-
else if (strcmp(key, "fw_connection_state") == 0) {
-
client->fw_connection_state = atoi(value);
-
}
-
else if (strcmp(key, "fd") == 0) {
-
client->fd = atoi(value);
-
}
-
else if (strcmp(key, "counters_incoming") == 0) {
-
client->counters.incoming_history = atoll(value);
-
client->counters.incoming = client->counters.incoming_history;
-
}
-
else if (strcmp(key, "counters_outgoing") == 0) {
-
client->counters.outgoing_history = atoll(value);
-
client->counters.outgoing = client->counters.outgoing_history;
-
}
-
else if (strcmp(key, "counters_last_updated") == 0) {
-
client->counters.last_updated = atol(value);
-
}
-
else {
-
debug(LOG_NOTICE, "I don't know how to inherit key [%s] value [%s] from parent", key, value);
-
}
-
}
-
}
-
}
-
-
/* End of parsing this command */
-
if (client) {
-
/* Add this client to the client list */
-
if (!firstclient) {
-
firstclient = client;
-
lastclient = firstclient;
-
}
-
else {
-
lastclient->next = client;
-
lastclient = client;
-
}
-
}
-
-
/* Clean up */
-
command = NULL;
-
memset(linebuffer, 0, sizeof(linebuffer));
-
len = 0;
-
client = NULL;
-
}
-
}
-
-
UNLOCK_CLIENT_LIST();
-
debug(LOG_INFO, "Client list downloaded successfully from parent");
-
-
close(sock);
-
}
代码片段1.3(已启动的wifidog发送客户端列表到新启动的wifidog):
(编辑:淮北站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|