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

wifidog源码分析 - 初始化阶段

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

点击(此处)折叠或打开

  1. //thread_wdctl_handler(void *arg)函数是wifidog启动后自动创建的控制线程,主要用于与wdctrl进行socket通信,根据wdctrl命令执行不同的操作。这里我们着重讲解的是wdctrl发送restart后wifidog的执行逻辑。
  2. static void *
  3. thread_wdctl_handler(void *arg)
  4. {
  5.     int fd,
  6.         done,
  7.         i;
  8.     char request[MAX_BUF];
  9.     ssize_t read_bytes,
  10.         len;

  11.     debug(LOG_DEBUG, "Entering thread_wdctl_handler....");

  12.     fd = (int)arg;
  13.     
  14.     debug(LOG_DEBUG, "Read bytes and stuff from %d", fd);

  15.     /* 初始化变量 */
  16.     read_bytes = 0;
  17.     done = 0;
  18.     memset(request, 0, sizeof(request));
  19.     
  20.     /* 读取命令 */
  21.     while (!done && read_bytes < (sizeof(request) - 1)) {
  22.         len = read(fd, request + read_bytes,
  23.                 sizeof(request) - read_bytes); //读取wdctrl发送的命令

  24.         /* 判断命令正确性 */
  25.         for (i = read_bytes; i < (read_bytes + len); i++) {
  26.             if (request[i] == 'r' || request[i] == 'n') {
  27.                 request[i] = '';
  28.                 done = 1;
  29.             }
  30.         }
  31.         
  32.         /* Increment position */
  33.         read_bytes += len;
  34.     }

  35.         //判断命令
  36.     if (strncmp(request, "status", 6) == 0) {
  37.         wdctl_status(fd);
  38.     } else if (strncmp(request, "stop", 4) == 0) {
  39.         wdctl_stop(fd);
  40.     } else if (strncmp(request, "reset", 5) == 0) {
  41.         wdctl_reset(fd, (request + 6));
  42.     } else if (strncmp(request, "restart", 7) == 0) {
  43.         wdctl_restart(fd); //执行wdctl_restart(int afd)函数
  44.     }

  45.     if (!done) {
  46.         debug(LOG_ERR, "Invalid wdctl request.");
  47.                 //关闭套接字
  48.         shutdown(fd, 2);
  49.         close(fd);
  50.         pthread_exit(NULL);
  51.     }

  52.     debug(LOG_DEBUG, "Request received: [%s]", request);
  53.     
  54.         //关闭套接字
  55.     shutdown(fd, 2);
  56.     close(fd);
  57.     debug(LOG_DEBUG, "Exiting thread_wdctl_handler....");

  58.     return NULL;
  59. }


  60. //wdctl_restart(int afd)函数详解
  61. static void
  62. wdctl_restart(int afd)
  63. {
  64.     int sock,
  65.         fd;
  66.     char *sock_name;
  67.     struct sockaddr_un sa_un;
  68.     s_config * conf = NULL;
  69.     t_client * client = NULL;
  70.     char * tempstring = NULL;
  71.     pid_t pid;
  72.     ssize_t written;
  73.     socklen_t len;

  74.     conf = config_get_config();

  75.     debug(LOG_NOTICE, "Will restart myself");

  76.     /*
  77.      * 准备内部连接socket
  78.      */
  79.     memset(&sa_un, 0, sizeof(sa_un));
  80.     sock_name = conf->internal_sock; //conf->internal_sock值为"/tmp/wifidog.sock"
  81.     debug(LOG_DEBUG, "Socket name: %s", sock_name);

  82.     if (strlen(sock_name) > (sizeof(sa_un.sun_path) - 1)) {
  83.        
  84.         debug(LOG_ERR, "INTERNAL socket name too long");
  85.         return;
  86.     }

  87.     debug(LOG_DEBUG, "Creating socket");
  88.     sock = socket(PF_UNIX, SOCK_STREAM, 0); //建立内部socket套接字

  89.     debug(LOG_DEBUG, "Got internal socket %d", sock);

  90.     /* 如果sock_name文件存在,则删除*/
  91.     unlink(sock_name);

  92.     debug(LOG_DEBUG, "Filling sockaddr_un");
  93.     strcpy(sa_un.sun_path, sock_name);
  94.     sa_un.sun_family = AF_UNIX;
  95.     
  96.     debug(LOG_DEBUG, "Binding socket (%s) (%d)", sa_un.sun_path, strlen(sock_name));
  97.     
  98.    
  99.     if (bind(sock, (struct sockaddr *)&sa_un, strlen(sock_name) + sizeof(sa_un.sun_family))) {
  100.         debug(LOG_ERR, "Could not bind internal socket: %s", strerror(errno));
  101.         return;
  102.     }

  103.     if (listen(sock, 5)) {
  104.         debug(LOG_ERR, "Could not listen on internal socket: %s", strerror(errno));
  105.         return;
  106.     }
  107.     
  108.     /*
  109.      * socket建立完成,创建子进程
  110.      */
  111.     debug(LOG_DEBUG, "Forking in preparation for exec()...");
  112.     pid = safe_fork();
  113.     if (pid > 0) {
  114.         /* 父进程 */

  115.         /* 等待子进程连接此socket :*/
  116.         debug(LOG_DEBUG, "Waiting for child to connect on internal socket");
  117.         len = sizeof(sa_un);
  118.         if ((fd = accept(sock, (struct sockaddr *)&sa_un, &len)) == -1){ //接受连接
  119.             debug(LOG_ERR, "Accept failed on internal socket: %s", strerror(errno));
  120.             close(sock);
  121.             return;
  122.         }

  123.         close(sock);

  124.         debug(LOG_DEBUG, "Received connection from child. Sending them all existing clients");

  125.         /*子进程已经完成连接,发送客户端列表 */
  126.         LOCK_CLIENT_LIST();
  127.         client = client_get_first_client(); //获取第一个客户端
  128.         while (client) {
  129.             /* Send this client */
  130.             safe_asprintf(&tempstring, "CLIENT|ip=%s|mac=%s|token=%s|fw_connection_state=%u|fd=%d|counters_incoming=%llu|counters_outgoing=%llu|counters_last_updated=%lun", client->ip, client->mac, client->token, client->fw_connection_state, client->fd, client->counters.incoming, client->counters.outgoing, client->counters.last_updated);
  131.             debug(LOG_DEBUG, "Sending to child client data: %s", tempstring);
  132.             len = 0;
  133.             while (len != strlen(tempstring)) {
  134.                 written = write(fd, (tempstring + len), strlen(tempstring) - len); //发送给子进程
  135.                 if (written == -1) {
  136.                     debug(LOG_ERR, "Failed to write client data to child: %s", strerror(errno));
  137.                     free(tempstring);
  138.                     break;
  139.                 }
  140.                 else {
  141.                     len += written;
  142.                 }
  143.             }
  144.             free(tempstring);
  145.             client = client->next;
  146.         }
  147.         UNLOCK_CLIENT_LIST();

  148.         close(fd);

  149.         debug(LOG_INFO, "Sent all existing clients to child. Committing suicide!");

  150.         shutdown(afd, 2);
  151.         close(afd);

  152.         
  153.         wdctl_stop(afd);
  154.     }
  155.     else {
  156.         /* 子进程,先关闭资源 */
  157.         close(wdctl_socket_server);
  158.         close(icmp_fd);
  159.         close(sock);
  160.         shutdown(afd, 2);
  161.         close(afd);
  162.         debug(LOG_NOTICE, "Re-executing myself (%s)", restartargv[0]);

  163.         setsid();
  164.         execvp(restartargv[0], restartargv); //执行外部命令,这里重新启动wifidog
  165.         
  166.         debug(LOG_ERR, "I failed to re-execute myself: %s", strerror(errno));
  167.         debug(LOG_ERR, "Exiting without cleanup");
  168.         exit(1);
  169.     }
  170. }

小结

  客户端列表只有在restart命令中才会执行,实际上流程就是

  • 父wifidog准备socket
  • 父wifidog启动子wifidog
  • 子wifidog连接父wifidog
  • 客户端列表传递
  • 子wifidog终止父wifidog



(编辑:常州站长网)

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

热点阅读