加入收藏 | 设为首页 | 会员中心 | 我要投稿 常州站长网 (https://www.0519zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > 交互 > 正文

wifidog源码分析 - 初始化阶段

发布时间:2016-10-31 00:31:30 所属栏目:交互 来源:站长网
导读:副标题#e# Wifidog是一个linux下开源的认证网关软件,它主要用于配合认证服务器实现无线路由器的认证放行功能。 wifidog是一个后台的服务程序,可以通过wdctrl命令对wifidog主程序进行控制。 本文解释wifidog在启动阶段所做的初始化主要工作( 代码片段1.1

点击(此处)折叠或打开

  1. void get_clients_from_parent(void) {
  2.     int sock;
  3.     struct sockaddr_un sa_un;
  4.     s_config * config = NULL;
  5.     char linebuffer[MAX_BUF];
  6.     int len = 0;
  7.     char *running1 = NULL;
  8.     char *running2 = NULL;
  9.     char *token1 = NULL;
  10.     char *token2 = NULL;
  11.     char onechar;
  12.     char *command = NULL;
  13.     char *key = NULL;
  14.     char *value = NULL;
  15.     t_client * client = NULL;
  16.     t_client * lastclient = NULL;

  17.     config = config_get_config();
  18.     
  19.     debug(LOG_INFO, "Connecting to parent to download clients");

  20.     /* 连接socket */
  21.     sock = socket(AF_UNIX, SOCK_STREAM, 0);
  22.     memset(&sa_un, 0, sizeof(sa_un));
  23.     sa_un.sun_family = AF_UNIX;
  24.     strncpy(sa_un.sun_path, config->internal_sock, (sizeof(sa_un.sun_path) - 1)); //config->internal_sock的值为"/tmp/wifidog.sock"

  25.     /* 连接已启动的wifidog */
  26.     if (connect(sock, (struct sockaddr *)&sa_un, strlen(sa_un.sun_path) + sizeof(sa_un.sun_family))) {
  27.         debug(LOG_ERR, "Failed to connect to parent (%s) - client list not downloaded", strerror(errno));
  28.         return;
  29.     }

  30.     debug(LOG_INFO, "Connected to parent. Downloading clients");

  31.     LOCK_CLIENT_LIST();

  32.     command = NULL;
  33.     memset(linebuffer, 0, sizeof(linebuffer));
  34.     len = 0;
  35.     client = NULL;
  36.     /* 接收数据,逐个字符接收 */
  37.     /* 数据包格式为 CLIENT|ip=%s|mac=%s|token=%s|fw_connection_state=%u|fd=%d|counters_incoming=%llu|counters_outgoing=%llu|counters_last_updated=%lun */
  38.     while (read(sock, &onechar, 1) == 1) {
  39.         if (onechar == 'n') {
  40.             /* 如果接收到末尾('n'),则转为'' */
  41.             onechar = '';
  42.         }
  43.         linebuffer[len++] = onechar;
  44.         
  45.         if (!onechar) {
  46.             /* 以下将数据转化为t_client结构体添加到客户端列表 */
  47.             debug(LOG_DEBUG, "Received from parent: [%s]", linebuffer);
  48.             running1 = linebuffer;
  49.             while ((token1 = strsep(&running1, "|")) != NULL) {
  50.                 if (!command) {
  51.                     /* The first token is the command */
  52.                     command = token1;
  53.                 }
  54.                 else {
  55.                 /* Token1 has something like "foo=bar" */
  56.                     running2 = token1;
  57.                     key = value = NULL;
  58.                     while ((token2 = strsep(&running2, "=")) != NULL) {
  59.                         if (!key) {
  60.                             key = token2;
  61.                         }
  62.                         else if (!value) {
  63.                             value = token2;
  64.                         }
  65.                     }
  66.                 }

  67.                 if (strcmp(command, "CLIENT") == 0) {
  68.                     /* This line has info about a client in the client list */
  69.                     if (!client) {
  70.                         /* Create a new client struct */
  71.                         client = safe_malloc(sizeof(t_client));
  72.                         memset(client, 0, sizeof(t_client));
  73.                     }
  74.                 }

  75.                 if (key && value) {
  76.                     if (strcmp(command, "CLIENT") == 0) {
  77.                         /* Assign the key into the appropriate slot in the connection structure */
  78.                         if (strcmp(key, "ip") == 0) {
  79.                             client->ip = safe_strdup(value);
  80.                         }
  81.                         else if (strcmp(key, "mac") == 0) {
  82.                             client->mac = safe_strdup(value);
  83.                         }
  84.                         else if (strcmp(key, "token") == 0) {
  85.                             client->token = safe_strdup(value);
  86.                         }
  87.                         else if (strcmp(key, "fw_connection_state") == 0) {
  88.                             client->fw_connection_state = atoi(value);
  89.                         }
  90.                         else if (strcmp(key, "fd") == 0) {
  91.                             client->fd = atoi(value);
  92.                         }
  93.                         else if (strcmp(key, "counters_incoming") == 0) {
  94.                             client->counters.incoming_history = atoll(value);
  95.                             client->counters.incoming = client->counters.incoming_history;
  96.                         }
  97.                         else if (strcmp(key, "counters_outgoing") == 0) {
  98.                             client->counters.outgoing_history = atoll(value);
  99.                             client->counters.outgoing = client->counters.outgoing_history;
  100.                         }
  101.                         else if (strcmp(key, "counters_last_updated") == 0) {
  102.                             client->counters.last_updated = atol(value);
  103.                         }
  104.                         else {
  105.                             debug(LOG_NOTICE, "I don't know how to inherit key [%s] value [%s] from parent", key, value);
  106.                         }
  107.                     }
  108.                 }
  109.             }

  110.             /* End of parsing this command */
  111.             if (client) {
  112.                 /* Add this client to the client list */
  113.                 if (!firstclient) {
  114.                     firstclient = client;
  115.                     lastclient = firstclient;
  116.                 }
  117.                 else {
  118.                     lastclient->next = client;
  119.                     lastclient = client;
  120.                 }
  121.             }

  122.             /* Clean up */
  123.             command = NULL;
  124.             memset(linebuffer, 0, sizeof(linebuffer));
  125.             len = 0;
  126.             client = NULL;
  127.         }
  128.     }

  129.     UNLOCK_CLIENT_LIST();
  130.     debug(LOG_INFO, "Client list downloaded successfully from parent");

  131.     close(sock);
  132. }

代码片段1.3(已启动的wifidog发送客户端列表到新启动的wifidog):

(编辑:常州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读