认证流程【2】-main_loop函数

本文详细阐述了智能网络管理的核心组件与流程,包括配置管理、监控与警报、故障诊断与修复,以及服务质量保障。重点介绍了如何通过自动化工具与算法优化网络性能,提升用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

static void main_loop(void)
{
    int result;
    pthread_t   tid;
    s_config *config = config_get_config();
    request *r;
    void **params;

    int ret;

    /* Set the time when wifidog started */
    if (!started_time) {
        debug(LOG_INFO, "Setting started_time");
        started_time = time(NULL);
    } else if (started_time < MINIMUM_STARTED_TIME) {
        debug(LOG_WARNING, "Detected possible clock skew - re-setting started_time");
        started_time = time(NULL);
    }


/******************************************************************************************************************/
/* 检查config 的 gw_address 和 gw_id 有没有值,如果没有值,通过get_iface_ip 和get_iface_mac 来获取,这里gw_id 设置成网关gw_interface 的mac 地址,gw_address 是网关 gw_interface 的IP,一般gw_interface 是br0, 在wifidog.conf 里设置*/
    /* If we don't have the Gateway IP address, get it. Can't fail. */
    if (!config->gw_address) {
        debug(LOG_DEBUG, "Finding IP address of %s", config->gw_interface);
        if ((config->gw_address = get_iface_ip(config->gw_interface)) == NULL) {
            debug(LOG_ERR, "Could not get IP address information of %s, exiting...", config->gw_interface);
            exit(1);
        }
        debug(LOG_DEBUG, "%s = %s", config->gw_interface, config->gw_address);
    }

    /* If we don't have the Gateway ID, construct it from the internal MAC address.
     * "Can't fail" so exit() if the impossible happens. */
    if (!config->gw_id) {
        debug(LOG_DEBUG, "Finding MAC address of %s", config->gw_interface);
        if ((config->gw_id = get_iface_mac(config->gw_interface)) == NULL) {
            debug(LOG_ERR, "Could not get MAC address information of %s, exiting...", config->gw_interface);
            exit(1);
        }
        debug(LOG_DEBUG, "%s = %s", config->gw_interface, config->gw_id);
    }
/******************************************************************************************************************/

     /* 在网关 gw_interface 的IP 和 gw_port 上用libhttpd 库运行一个http server*/
    /* Initializes the web server */
    debug(LOG_NOTICE, "Creating web server on %s:%d", config->gw_address, config->gw_port);
    if ((webserver = httpdCreate(config->gw_address, config->gw_port)) == NULL) {
        debug(LOG_ERR, "Could not create web server: %s", strerror(errno));
        exit(1);
    }

    g_auth_user_id = 1;

/* 创建对于文件'wifidog'、''、'about'、'auth'、'status'的访问回调函数:http_callback_wifidog、http_callback_wifidog、http_callback_about、http_callback_auth以及http_callback_status,这里所谓的访问回调函数可以理解成当客户端访问auth 文件时会执行http_callback_auth函数,其他函数同理*/
    debug(LOG_DEBUG, "Assigning callbacks to web server");
    httpdAddCContent(webserver, "/", "wifidog", 0, NULL, http_callback_wifidog);

    /*weixing auth*/
    httpdAddCContent(webserver, "/", "redirect", 0, NULL, http_callback_weixing_redirect);
    httpdAddCContent(webserver, "/", "pclogin", 0, NULL, http_callback_weixing_redirect_pc);

    httpdAddCContent(webserver, "/", "wxAttentionLogon", 0, NULL, http_callback_weixingattention);
    httpdAddCContent(webserver, "/auth/wxwifiAuth/", "logon", 0, NULL, http_callback_weixing_logon);

    httpdAddCContent(webserver, "/wifidog", "", 0, NULL, http_callback_wifidog);
    httpdAddCContent(webserver, "/wifidog", "about", 0, NULL, http_callback_about);
    httpdAddCContent(webserver, "/wifidog", "status", 0, NULL, http_callback_status);
    httpdAddCContent(webserver, "/wifidog", "auth", 0, NULL, http_callback_auth);
    httpdAddCContent(webserver, "/wifidog", "info", 0, NULL, http_callback_info);
    httpdAddCContent(webserver, "/wifidog", "api", 0, NULL, http_callback_api);

     /* libhttpd的函数,httpdAddC404Content,这个函数是用来处理客户端第一次http 访问外网时执行回调函数,回调函数就是这个函数的第二个参数http_callback_404*/
    httpdAddC404Content(webserver, http_callback_404);
/******************************************************************************************************************/

    /* Reset the firewall (if WiFiDog crashed) */
     /*清掉和wifidog 有关的iptables 规则*/
    fw_destroy();
    qos_destroy();

    nslookup_init();

    /* Then initialize it */
     /*添加wifidog 相关的iptables 链及规则*/
     /*规则:
          (1) wifidog.conf 里设置的firewall rule set, global/validating-users/known-users/unknown-users/locked-users,这里unknown-users 默认的四条关于53/67 port 的allow 规则需要打开
          (2)认证服务器也会被加入白名单,以使得客户端能够正常和认证服务器交互*/
    if (!fw_init()) {
        debug(LOG_ERR, "FATAL: Failed to initialize firewall");
        exit(1);
    }

    qos_init();
    wifidog_started();

    /* Start clean up thread */
     /* 超时检查线程*/
     /* thread_client_timeout_check 调用了fw_sync_with_authserver,其他部分代码是线程锁 */
     /* fw_sync_with_authserver 函数功能:网关每隔config.checkinterval 时间检查,把所有客户端链表的信息发送给认证服务器进行流量更新,并且计算每一个客户端距离上次认证成功之后所经过的时间,如果距离上次认证成功经过的时间超过config.checkinterval * config.clienttimeout,即认为已经超时,网关会发送此客户端的logout 通知认证服务器,并将此客户端的iptables 白名单移除,客户端需要重新认证*/
    result = pthread_create(&tid_fw_counter, NULL, (void *)thread_client_timeout_check, NULL);
    if (result != 0) {
        debug(LOG_ERR, "FATAL: Failed to create a new thread (fw_counter) - exiting");
        termination_handler(0);
    }
    pthread_detach(tid_fw_counter);

    /* Start control thread */
    result = pthread_create(&tr069_id, NULL, (void *)thread_wifidog_recv_msg, (void *)safe_strdup(WIFIDOG_RECV_PATH));
    if (result != 0) {
        debug(LOG_ERR, "FATAL: Failed to create a new thread (wdctl) - exiting");
        termination_handler(0);
    }
    pthread_detach(tr069_id);

    /* Start Wifidog msg control thread */
     /* 和wdctl 交互的线程,wdctl 是控制wifidog 结束,重启的程序*/
    result = pthread_create(&tid2, NULL, (void *)thread_wdctl, (void *)safe_strdup(config->wdctl_sock));
    if (result != 0) {
        debug(LOG_ERR, "FATAL: Failed to create a new thread (wdctl) - exiting");
        termination_handler(0);
    }
    pthread_detach(tid2);

    /* Start heartbeat thread */
     /* ping协议线程(心跳)*/
     /* 函数thread_ping 调用ping 函数。
     ping()函数功能:
     网关定时发送Ping 协议包通知认证服务器证明自己是活的,
     发包的时间间隔也是config.checkinterval,
     ping协议格式:
     这些都是网关的参数,和客户端没有的关系*/
    result = pthread_create(&tid_ping, NULL, (void *)thread_ping, NULL);
    if (result != 0) {
        debug(LOG_ERR, "FATAL: Failed to create a new thread (ping) - exiting");
        termination_handler(0);
    }
    pthread_detach(tid_ping);

    result = pthread_create(&tid_app_msg, NULL, (void *)thread_listen_appmsg, NULL);
    if (result != 0) {
        debug(LOG_ERR, "FATAL: Failed to create a new thread (app_msg) - exiting");
        termination_handler(0);
    }
    pthread_detach(tid_app_msg);

    debug(LOG_NOTICE, "Waiting for connections");

    /*如果配置微信连WIFI, 发送保活报文*/
    if (wx_link_wifi == 1) {
        /* Start keepalive thread */
        result = pthread_create(&tid_keepalive, NULL, (void *)thread_keepalive, NULL);
        if (result != 0) {
            debug(LOG_ERR, "FATAL: Failed to create a new thread (keepalive) - exiting");
            termination_handler(0);
        }
        pthread_detach(tid_keepalive);
    }

    ///* Start nslookup thread
    result = pthread_create(&tid_nslookup, NULL, (void *)thread_nslookup_running, NULL);
    if (result != 0) {
        debug(LOG_ERR, "FATAL: Failed to create a new thread (nslookup) - exiting");
        termination_handler(0);
    }
    pthread_detach(tid_nslookup);
    //*/
    
    while(1) {
        r = httpdGetConnection(webserver, NULL);

        /* We can't convert this to a switch because there might be
         * values that are not -1, 0 or 1. */
        if (webserver->lastError == -1) {
            /* Interrupted system call */
            continue; /* restart loop */
        } else if (webserver->lastError < -1) {
            /*
             * FIXME
             * An error occurred - should we abort?
             * reboot the device ?
             */
            debug(LOG_ERR, "FATAL: httpdGetConnection returned unexpected value %d, exiting.", webserver->lastError);
            termination_handler(0);
        } else if (r != NULL) {
            /*
             * We got a connection
             *
             * We should create another thread
             */
            debug(LOG_INFO, "Received connection from %s, spawning worker thread", r->clientAddr);
            /* The void**'s are a simulation of the normal C
             * function calling sequence. */
            params = safe_malloc(2 * sizeof(void *));
            *params = webserver;
            *(params + 1) = r;
                /* 在一个while 死循环里接受http 连接然后处理连接的线程,可以读取http 请求的具体内容,主要调用了两个函数httpdReadRequest/httpdProcessRequest,读取和处理http 请求*/
            result = pthread_create(&tid, NULL, (void *)thread_httpd, (void *)params);
            if (result != 0) {
                debug(LOG_ERR, "FATAL: Failed to create a new thread (httpd) - exiting");
                termination_handler(0);
            }
            pthread_detach(tid);
        } else {
            /* webserver->lastError should be 2 */
            /* XXX We failed an ACL.... No handling because
             * we don't set any... */
        }

    }

    /* never reached */
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值