void mytunetsvc_main() { BYTE tmpbuf[1024]; BOOL bDot1xSuccess = FALSE; BOOL bDot1xFailure = FALSE; BOOL bNetworkError = FALSE; BOOL bTunetFailure = FALSE; BOOL bTunetSuccess = FALSE; BOOL bKeepAliveError = FALSE; BOOL deep_sleep; LOG *log = NULL; TICK *retry_timer = os_tick_new(10000, TRUE); TICK *state_timer = os_tick_new(1000, TRUE); INT nDot1xRetryCount = 0, nTunetRetryCount = 0; INT nRetryDelay = 5000; INT i; mytunetsvc_login(); while(!mytunetsvc_stop_flag) { /***************************************************************/ // Main loop for service. /***************************************************************/ //如果要重新登陆,则清除原来的登陆状态 deep_sleep=TRUE; if(g_Goal == GOAL_RELOGIN) { tunet_stop(); g_DelayRetry = DELAY_RETRY_NONE; nDot1xRetryCount = 0; nTunetRetryCount = 0; //userconfig = g_UserConfig; tunet_reset(); dot1x_reset(); g_Goal = GOAL_LOGIN; } if(os_tick_check(state_timer)) { g_logs = logs_append(g_logs, "MYTUNETSVC_LIMITATION", NULL, (BYTE *)&g_UserConfig.limitation, sizeof(g_UserConfig.limitation)); tmpbuf[0] = dot1x_get_state(); tmpbuf[1] = tunet_get_state(); g_logs = logs_append(g_logs, "MYTUNETSVC_STATE", NULL, tmpbuf, 2); } // 如果需要重试,则等待这个重试,而不再进行其他操作。 if(g_DelayRetry != DELAY_RETRY_NONE) { if(os_tick_check(retry_timer)) { switch(g_DelayRetry) { case DELAY_RETRY_DOT1X: if( dot1x_get_state() == DOT1X_STATE_NONE || dot1x_get_state() == DOT1X_STATE_FAILURE) { nDot1xRetryCount ++; dot1x_start(&g_UserConfig); } break; case DELAY_RETRY_TUNET: if( tunet_get_state() == TUNET_STATE_NONE || tunet_get_state() == TUNET_STATE_FAILURE || tunet_get_state() == TUNET_STATE_ERROR ) { nTunetRetryCount ++; tunet_start(&g_UserConfig); } break; } g_DelayRetry = DELAY_RETRY_NONE; } else { os_sleep(100); continue; } } if(g_Goal == GOAL_LOGIN) { //dprintf("bUseDot1x %d\n", g_UserConfig.bUseDot1x); if(g_UserConfig.bUseDot1x) { if( dot1x_get_state() == DOT1X_STATE_NONE ) { dot1x_start(&g_UserConfig); } if( dot1x_get_state() == DOT1X_STATE_SUCCESS ) { if( tunet_get_state() == TUNET_STATE_NONE ) { tunet_start(&g_UserConfig); } } else { if(dot1x_is_timeout()) { dot1x_start(&g_UserConfig); } } } else { if( tunet_get_state() == TUNET_STATE_NONE ) { tunet_start(&g_UserConfig); } } if(tunet_is_keepalive_timeout()) { tunet_reset(); if(g_UserConfig.bUseDot1x) dot1x_start(&g_UserConfig); else tunet_start(&g_UserConfig); } } /* else if(g_Goal == GOAL_LOGOUT) { tunet_stop(); g_Goal = GOAL_NONE; } */ RefetchLog: log = logs_fetch(g_logs, 0); if(log!=NULL) { if(mytunetsvc_transmit_log) mytunetsvc_transmit_log(log); #define TagIs(s) (strcmp(log->tag, s) == 0) #define StrIs(s) (strcmp(log->str, s) == 0) bDot1xSuccess = FALSE; bDot1xFailure = FALSE; bNetworkError = FALSE; bTunetFailure = FALSE; bTunetSuccess = FALSE; bKeepAliveError = FALSE; if (TagIs("DOT1X_RECV_PACK" )) { if (StrIs("EAP_SUCCESS" )) bDot1xSuccess = TRUE; if (StrIs("EAP_FAILURE" )) bDot1xFailure = TRUE; } if (TagIs("TUNET_NETWORK_ERROR" )) bNetworkError = TRUE; if (TagIs("TUNET_LOGON_ERROR" )) bTunetFailure = TRUE; if (TagIs("TUNET_LOGON_KEEPALIVE_SERVER" )) bTunetSuccess = TRUE; //if (TagIs("TUNET_KEEPALIVE_MONEY" )) AccountMoney = str //if (TagIs("TUNET_LOGON_MONEY" )) AccountMoney = str if (TagIs("TUNET_KEEPALIVE_ERROR" )) bKeepAliveError = TRUE; #undef TagIs #undef StrIs if(bDot1xSuccess) { nDot1xRetryCount = 0; } if(bTunetSuccess) { nTunetRetryCount = 0; } if(bDot1xFailure && g_UserConfig.bUseDot1x) { dot1x_reset(); g_DelayRetry = DELAY_RETRY_DOT1X; nRetryDelay = 5 + nDot1xRetryCount * 2; if(nRetryDelay >= 30) nRetryDelay = 30; nRetryDelay *= 1000; os_tick_set_delay(retry_timer, nRetryDelay); os_tick_clear(retry_timer); } if(bTunetFailure) { dot1x_reset(); g_DelayRetry = DELAY_RETRY_TUNET; nRetryDelay = 120 + nTunetRetryCount * 60; if(nRetryDelay >= 5 * 60) nRetryDelay = 5 * 60; nRetryDelay *= 1000; os_tick_set_delay(retry_timer, nRetryDelay); os_tick_clear(retry_timer); if(nTunetRetryCount >= 10) { tunet_reset(); g_Goal = GOAL_NONE; } } if( (bNetworkError || bKeepAliveError) && g_Goal == GOAL_LOGIN) { tunet_reset(); dot1x_reset(); if(g_UserConfig.bUseDot1x) { //网络错误要重新连接 802.1x nDot1xRetryCount = 0; g_DelayRetry = DELAY_RETRY_DOT1X; nRetryDelay = 5; } else { //网络错误要重新连接 Tunet //网络错误并不增加tunet的重试次数 nTunetRetryCount = 0; g_DelayRetry = DELAY_RETRY_TUNET; nRetryDelay = 1; } nRetryDelay *= 1000; os_tick_set_delay(retry_timer, nRetryDelay); os_tick_clear(retry_timer); } log = log_free(log); goto RefetchLog; } if (tunet_get_state()!=TUNET_STATE_KEEPALIVE) { deep_sleep=FALSE; } if (deep_sleep) { for (i=0; i<20; i++) { if (mytunetsvc_stop_flag) { break; } os_sleep(100); } }else { os_sleep(20); } } // printf("Loop out.\n"); tunet_stop(); //printf("tunet stopped.\n"); if(g_UserConfig.bUseDot1x) dot1x_stop(); tmpbuf[0] = DOT1X_STATE_NONE; tmpbuf[1] = DOT1X_STATE_NONE; g_logs = logs_append(g_logs, "MYTUNETSVC_STATE", NULL, tmpbuf, 2); while(1) { log = logs_fetch(g_logs, 0); if(log!=NULL) { if(mytunetsvc_transmit_log) mytunetsvc_transmit_log(log); log = log_free(log); } else { break; } } retry_timer = os_tick_free(retry_timer); state_timer = os_tick_free(state_timer); }
BOOL tunet_is_keepalive_timeout() { return (tunet_get_state() == TUNET_STATE_KEEPALIVE && os_tick_is_active(keepalive_timeout)); }
THREADRET tunet_thread(THREAD *self) { BOOL hasErr = FALSE; fd_set rset; fd_set wset; fd_set eset; struct timeval tv; time_t keepalive_watchDog=0; tv.tv_sec=2; tv.tv_usec=0; int ret,flags; int maxsock; int main_retry=0; char flag,connected=0; logs_append(g_logs, "TUNET_THREAD_STARTING", NULL, NULL, 0); while(os_thread_is_running(self) || os_thread_is_paused(self)) { // printf("main_socket=%d\n",main_socket); if (keepalive_watchDog) { if (time(NULL)-keepalive_watchDog>=36) { if(tunet_keepalive()==ERR) break; } } if (tunet_state==TUNET_STATE_RECV_REMAINING_DATA || tunet_state==TUNET_STATE_RECV_WELCOME) { keepalive_watchDog=time(NULL); } hasErr=FALSE; if(tunet_get_state() == TUNET_STATE_NONE) { break; } if(tunet_get_state() == TUNET_STATE_ERROR) { //an server-side error occurs when trying to login/logout logs_append(g_logs, "TUNET_ERROR", NULL, NULL, 0); break; } if (tunet_state ==TUNET_STATE_LOGOUT) { if (!connected) { break; } tunet_connect_logout_server(); } if (tunet_state==TUNET_STATE_LOGOUT||tunet_state==TUNET_STATE_LOGOUT_RECV_LOGOUT) { hasErr |= (tunet_logout_send_logout() == ERR); hasErr |= (tunet_logout_recv_logout() == ERR); if(hasErr) { //if an error occurs when LOGOUT, we can just omit it, and exit the thread. break; } os_thread_test_paused(self); os_sleep(20); }else { if(tunet_state == TUNET_STATE_LOGIN){ tunet_connect_main_server(); } if(tunet_connect_keepalive_server()==ERR){ break; } if (tunet_state == TUNET_STATE_KEEPALIVE||tunet_state==TUNET_STATE_RECV_REMAINING_DATA) { connected=1; } FD_ZERO(&rset); FD_ZERO(&wset); FD_ZERO(&eset); maxsock=0; flag=0; if (main_socket) { FD_SET(main_socket, &rset); FD_SET(main_socket, &wset); FD_SET(main_socket, &eset); if (main_socket>=maxsock) { maxsock=main_socket; } flag=1; } if (keepalive_socket) { FD_SET(keepalive_socket,&rset); FD_SET(keepalive_socket,&eset); if (keepalive_socket>=maxsock) { maxsock=keepalive_socket; } } ret=select(maxsock+1,&rset,&wset,&eset,&tv); if (ret<0) { logs_append(g_logs, "TUNET_NETWORK_ERROR", "UNKNOWN", NULL, 0); break; }else if (ret==0) { if (tunet_state==TUNET_STATE_LOGIN) { if (main_retry>=3) { logs_append(g_logs, "TUNET_NETWORK_ERROR", "MAIN", NULL, 0); break; } main_retry++; } os_thread_test_paused(self); continue; } if (flag) { if (FD_ISSET(main_socket,&wset)) { tunet_logon_send_tunet_user(); tunet_logon_reply_welcome(); } if (FD_ISSET(main_socket,&rset)) { if(tunet_logon_recv_welcome()==ERR){ logs_append(g_logs, "TUNET_NETWORK_ERROR", "RECV_WELCOME", NULL, 0); break; } if(tunet_logon_recv_remaining_data()==ERR){ logs_append(g_logs, "TUNET_NETWORK_ERROR", "RECV_REMAINING_DATA", NULL, 0); break; } } if (FD_ISSET(main_socket, &eset)) { logs_append(g_logs, "TUNET_NETWORK_ERROR", "MAIN", NULL, 0); break; } } if (FD_ISSET(keepalive_socket,&rset)) { if(tunet_keepalive()==ERR){ logs_append(g_logs, "TUNET_NETWORK_ERROR", "KEEPALIVE", NULL, 0); break; } keepalive_watchDog=time(NULL); } if (FD_ISSET(keepalive_socket, &eset)) { logs_append(g_logs, "TUNET_NETWORK_ERROR", "KEEPALIVE", NULL, 0); break; } os_thread_test_paused(self); } } main_socket = os_socket_tcp_close(main_socket); keepalive_socket = os_socket_tcp_close(keepalive_socket); logout_socket = os_socket_tcp_close(logout_socket); main_socket_buffer = buffer_clear(main_socket_buffer); keepalive_socket_buffer = buffer_clear(keepalive_socket_buffer); logout_socket_buffer = buffer_clear(logout_socket_buffer); tunet_state = TUNET_STATE_NONE; logs_append(g_logs, "TUNET_THREAD_EXITING", NULL, NULL, 0); thread_tunet = os_thread_free(thread_tunet); return 0; }
THREADRET tunet_thread(THREAD *self) { BOOL hasErr = FALSE; logs_append(g_logs, "TUNET_THREAD_STARTING", NULL, NULL, 0); while(os_thread_is_running(self) || os_thread_is_paused(self)) { if(tunet_state == TUNET_STATE_LOGIN) tunet_connect_main_server(); hasErr = FALSE; hasErr |= (tunet_logon_send_tunet_user() == ERR); hasErr |= (tunet_logon_recv_welcome() == ERR); hasErr |= (tunet_logon_reply_welcome() == ERR); hasErr |= (tunet_logon_recv_remaining_data() == ERR); hasErr |= (tunet_connect_keepalive_server() == ERR); hasErr |= (tunet_keepalive() == ERR); if(hasErr) { /* COMMENT: We won't help the user to retry for some network error shoud be dealt by the users. //a network error occurs when try to LOGIN main_socket = os_socket_tcp_close(main_socket); keepalive_socket = os_socket_tcp_close(keepalive_socket); tunet_state = TUNET_STATE_LOGIN; continue; */ break; } hasErr = FALSE; if(tunet_state == TUNET_STATE_LOGOUT) tunet_connect_logout_server(); hasErr |= (tunet_logout_send_logout() == ERR); hasErr |= (tunet_logout_recv_logout() == ERR); if(hasErr) { //if an error occurs when LOGOUT, we can just omit it, and exit the thread. break; } if(tunet_get_state() == TUNET_STATE_NONE) { //nothing to do , exit the thread break; } if(tunet_get_state() == TUNET_STATE_ERROR) { //an server-side error occurs when trying to login/logout logs_append(g_logs, "TUNET_ERROR", NULL, NULL, 0); break; } os_thread_test_paused(self); os_sleep(20); } main_socket = os_socket_tcp_close(main_socket); keepalive_socket = os_socket_tcp_close(keepalive_socket); logout_socket = os_socket_tcp_close(logout_socket); main_socket_buffer = buffer_clear(main_socket_buffer); keepalive_socket_buffer = buffer_clear(keepalive_socket_buffer); logout_socket_buffer = buffer_clear(logout_socket_buffer); tunet_state = TUNET_STATE_NONE; logs_append(g_logs, "TUNET_THREAD_EXITING", NULL, NULL, 0); thread_tunet = os_thread_free(thread_tunet); return 0; }