Changeset 469

Show
Ignore:
Timestamp:
02/20/05 17:55:20 (8 years ago)
Author:
minaguib
Message:

* Partial merge from CaptiveDNS branch: Consolidated much of the networking calls to the auth servers into a magical function called connect_auth_server() that's responsible for dns lookup, connecting, marking servers bad, marking online/auth_online, and refreshing the firewall rules.
* Partial merge from CaptiveDNS branch: Added new functions mark_auth_online(), mark_auth_offline() and is_auth_online() - similar in nature to is_online() etc. except tailored to decide on auth servers status - currently being called by connect_auth_server()
* Partial merge from CaptiveDNS branch: Different apology in 404 handler depending on whether internet is down or just auth server is down
* Partial merge from CaptiveDNS branch: wdctl status now shows status of is_online and is_auth_online

Location:
trunk/wifidog
Files:
10 modified

Legend:

Unmodified
Added
Removed
  • trunk/wifidog/ChangeLog

    r467 r469  
    66        instances of original with safe versions in all files 
    77        * Fix memory leak in iptables_fw_counters_update 
     8        * Partial merge from CaptiveDNS branch: Consolidated much of the networking 
     9        calls to the auth servers into a magical function called connect_auth_server() 
     10        that's responsible for dns lookup, connecting, marking servers bad, marking 
     11        online/auth_online, and refreshing the firewall rules. 
     12        * Partial merge from CaptiveDNS branch: Added new functions mark_auth_online(), 
     13        mark_auth_offline() and is_auth_online() - similar in nature to is_online() 
     14        etc. except tailored to decide on auth servers status - currently being called by 
     15        connect_auth_server() 
     16        * Partial merge from CaptiveDNS branch: Different apology in 404 handler 
     17        depending on whether internet is down or just auth server is down 
     18        * Partial merge from CaptiveDNS branch: wdctl status now shows status of 
     19        is_online and is_auth_online 
    820 
    9212005-02-16 Philippe April <philippe@ilesansfil.org> 
  • trunk/wifidog/src/centralserver.c

    r424 r469  
    4040 
    4141#include "common.h" 
    42  
     42#include "safe.h" 
    4343#include "util.h" 
    4444#include "auth.h" 
     
    4747#include "centralserver.h" 
    4848#include "../config.h" 
     49 
     50extern pthread_mutex_t  config_mutex; 
    4951 
    5052/** Initiates a transaction with the auth server, either to authenticate or to update the traffic counters at the server 
     
    6062auth_server_request(t_authresponse *authresponse, char *request_type, char *ip, char *mac, char *token, long int incoming, long int outgoing) 
    6163{ 
    62         int sockfd, num_tries, done; 
    63         size_t  numbytes, totalbytes; 
     64        int sockfd; 
     65        size_t  numbytes, totalbytes; 
    6466        char buf[MAX_BUF]; 
    65         struct in_addr *h_addr; 
    66         struct sockaddr_in their_addr; 
    6767        char *tmp; 
    68         s_config *config = config_get_config(); 
    69         t_auth_serv *auth_server = NULL; 
    7068 
    7169        /* Blanket default is failed. */ 
    7270        authresponse->authcode = AUTH_VALIDATION_FAILED; 
    7371         
    74         if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { 
    75                 debug(LOG_ERR, "socket(): %s", strerror(errno)); 
    76                 exit(1); 
    77         } 
    78  
    79         num_tries = 0; 
    80         done = 0; 
    81         while (!done && ((auth_server = get_auth_server()) != NULL)) { 
    82                 if ((h_addr = wd_gethostbyname(auth_server->authserv_hostname)) == NULL) { 
    83                         debug(LOG_ERR, "Failed to resolve %s via gethostbyname" 
    84                                 "(): %s", auth_server->authserv_hostname,  
    85                                 strerror(errno)); 
    86                         close(sockfd); 
    87                         return(-1); 
    88                 } 
    89          
    90                 their_addr.sin_family = AF_INET; 
    91                 their_addr.sin_port = htons(auth_server->authserv_http_port); 
    92                 their_addr.sin_addr = *h_addr; 
    93                 memset(&(their_addr.sin_zero), '\0', sizeof(their_addr.sin_zero)); 
    94  
    95                 debug(LOG_INFO, "Connecting to auth server %s on port %d",  
    96                         auth_server->authserv_hostname,  
    97                         auth_server->authserv_http_port); 
    98  
    99                 if (connect(sockfd, (struct sockaddr *)&their_addr, 
    100                                         sizeof(struct sockaddr)) == -1) { 
    101                         debug(LOG_ERR, "connect(): %s", strerror(errno)); 
    102                         debug(LOG_ERR, "Trying next Auth Server (if any)"); 
    103                         mark_auth_server_bad(auth_server); 
    104                         if (++num_tries > config->authserv_maxtries) { 
    105                                 debug(LOG_ERR, "Aborting request"); 
    106                                 free(h_addr); 
    107                                 close(sockfd); 
    108                                 return(-1); /* non-fatal */ 
    109                         } 
    110                 } else { 
    111                         done = 1; 
    112                 } 
    113                 free(h_addr); 
    114         } 
    115  
    116         mark_online(); 
     72        sockfd = connect_auth_server(); 
     73        if (sockfd == -1) { 
     74                /* Could not connect to any auth server */ 
     75                return (-1); 
     76        } 
    11777 
    11878        /** 
     
    12686                "Host: %s\n" 
    12787                "\n", 
    128             auth_server->authserv_path, request_type, ip, mac,  
     88            config_get_config()->auth_servers->authserv_path, request_type, ip, mac,  
    12989            token, incoming, outgoing, VERSION,  
    130             auth_server->authserv_hostname); 
     90            config_get_config()->auth_servers->authserv_hostname); 
     91 
     92        debug(LOG_DEBUG, "Sending HTTP request to auth server: [%s]\n", buf); 
    13193        send(sockfd, buf, strlen(buf), 0); 
    132  
    133         debug(LOG_DEBUG, "Sending HTTP request to auth server: [%s]\n", buf); 
    13494 
    13595        numbytes = totalbytes = 0; 
     
    13999         
    140100        if (numbytes == -1) { 
    141                 debug(LOG_ERR, "read(): %s", strerror(errno)); 
     101                debug(LOG_ERR, "Error reading from auth server: %s", strerror(errno)); 
    142102                close(sockfd); 
    143103                return(-1); 
    144104        } 
    145  
    146         numbytes = totalbytes; 
    147          
    148         buf[numbytes] = '\0'; 
    149  
    150105        close(sockfd); 
    151106 
     107        buf[totalbytes] = '\0'; 
    152108        debug(LOG_DEBUG, "HTTP Response from Server: [%s]", buf); 
    153109         
    154110        if ((tmp = strstr(buf, "Auth: "))) { 
    155111                if (sscanf(tmp, "Auth: %d", (int *)&authresponse->authcode) == 1) { 
    156                         debug(LOG_INFO, "Auth server returned authentication code %d", 
    157                                 authresponse->authcode); 
     112                        debug(LOG_INFO, "Auth server returned authentication code %d", authresponse->authcode); 
    158113                        return(authresponse->authcode); 
    159114                } else { 
    160                         debug(LOG_WARNING, "Auth server did not return expected information"); 
     115                        debug(LOG_WARNING, "Auth server did not return expected authentication code"); 
    161116                        return(AUTH_ERROR); 
    162117                } 
     
    168123} 
    169124 
     125/* Tries really hard to connect to an auth server. Returns a file descriptor, -1 on error 
     126 */ 
     127int connect_auth_server() { 
     128        int sockfd; 
     129 
     130        LOCK_CONFIG(); 
     131        sockfd = _connect_auth_server(0); 
     132        UNLOCK_CONFIG(); 
     133 
     134        if (sockfd == -1) { 
     135                debug(LOG_ERR, "Failed to connect to any of the auth servers"); 
     136                mark_auth_offline(); 
     137        } 
     138        else { 
     139                debug(LOG_DEBUG, "Connected to auth server"); 
     140                mark_auth_online(); 
     141        } 
     142        return (sockfd); 
     143} 
     144 
     145/* Helper function called by connect_auth_server() to do the actual work including recursion - do not call directly 
     146@param level recursion level indicator 
     147 */ 
     148int _connect_auth_server(int level) { 
     149        s_config *config = config_get_config(); 
     150        t_auth_serv *auth_server = NULL; 
     151        struct in_addr *h_addr; 
     152        int num_servers = 0; 
     153        char * hostname = NULL; 
     154        char * popular_servers[] = { 
     155                  "www.google.com" 
     156                , "www.yahoo.com" 
     157                , NULL 
     158        }; 
     159        char ** popularserver; 
     160        char * ip; 
     161        struct sockaddr_in their_addr; 
     162        int sockfd; 
     163 
     164        level++; 
     165 
     166        /* 
     167         * Let's calculate the number of servers we have 
     168         */ 
     169        for (auth_server = config->auth_servers; auth_server; auth_server = auth_server->next) { 
     170                num_servers++; 
     171        } 
     172        debug(LOG_DEBUG, "Level %d: Calculated %d auth servers in list", level, num_servers); 
     173 
     174        if (level > num_servers) { 
     175                /* 
     176                 * We've called ourselves too many times 
     177                 * That means we've cycled through all the servers in the server list at least once and none are accessible 
     178                 */ 
     179                return (-1); 
     180        } 
     181 
     182        /* 
     183         * Let's resolve the hostname of the top server to an IP address 
     184         */ 
     185        auth_server = config->auth_servers; 
     186        hostname = auth_server->authserv_hostname; 
     187        debug(LOG_DEBUG, "Level %d: Resolving auth server [%s]", level, hostname); 
     188        h_addr = wd_gethostbyname(hostname); 
     189        if (!h_addr) { 
     190                /* 
     191                 * DNS resolving it failed 
     192                 * 
     193                 * Can we resolve any of the popular servers ? 
     194                 */ 
     195                debug(LOG_DEBUG, "Level %d: Resolving auth server [%s] failed", level, hostname); 
     196 
     197                for (popularserver = popular_servers; *popularserver; popularserver++) { 
     198                        debug(LOG_DEBUG, "Level %d: Resolving popular server [%s]", level, *popularserver); 
     199                        h_addr = wd_gethostbyname(*popularserver); 
     200                        if (h_addr) { 
     201                                debug(LOG_DEBUG, "Level %d: Resolving popular server [%s] succeeded = [%s]", level, *popularserver, inet_ntoa(*h_addr)); 
     202                                break; 
     203                        } 
     204                        else { 
     205                                debug(LOG_DEBUG, "Level %d: Resolving popular server [%s] failed", level, *popularserver); 
     206                        } 
     207                } 
     208 
     209                if (h_addr) { 
     210                        free (h_addr); 
     211                        /* 
     212                         * Yes 
     213                         * 
     214                         * The auth server's DNS server is probably dead. Try the next auth server 
     215                         */ 
     216                        debug(LOG_DEBUG, "Level %d: Marking auth server [%s] as bad and trying next if possible", level, hostname); 
     217                        if (auth_server->last_ip) { 
     218                                free(auth_server->last_ip); 
     219                                auth_server->last_ip = NULL; 
     220                        } 
     221                        mark_auth_server_bad(auth_server); 
     222                        return _connect_auth_server(level); 
     223                } 
     224                else { 
     225                        /* 
     226                         * No 
     227                         * 
     228                         * It's probably safe to assume that the internet connection is malfunctioning 
     229                         * and nothing we can do will make it work 
     230                         */ 
     231                        debug(LOG_DEBUG, "Level %d: Failed to resolve auth server and all popular servers. The internet connection is probably down", level); 
     232                        return(-1); 
     233                } 
     234        } 
     235        else { 
     236                /* 
     237                 * DNS resolving was successful 
     238                 */ 
     239                ip = safe_strdup(inet_ntoa(*h_addr)); 
     240                debug(LOG_DEBUG, "Level %d: Resolving auth server [%s] succeeded = [%s]", level, hostname, ip); 
     241 
     242                if (!auth_server->last_ip || strcmp(auth_server->last_ip, ip) != 0) { 
     243                        /* 
     244                         * But the IP address is different from the last one we knew 
     245                         * Update it 
     246                         */ 
     247                        debug(LOG_DEBUG, "Level %d: Updating last_ip IP of server [%s] to [%s]", level, hostname, ip); 
     248                        if (auth_server->last_ip) free(auth_server->last_ip); 
     249                        auth_server->last_ip = ip; 
     250 
     251                        /* Update firewall rules */ 
     252                        fw_clear_authservers(); 
     253                        fw_set_authservers(); 
     254                } 
     255                else { 
     256                        /* 
     257                         * IP is the same as last time 
     258                         */ 
     259                        free(ip); 
     260                } 
     261 
     262                /* 
     263                 * Connect to it 
     264                 */ 
     265                debug(LOG_DEBUG, "Level %d: Connecting to auth server %s:%d", level, hostname, auth_server->authserv_http_port); 
     266                their_addr.sin_family = AF_INET; 
     267                their_addr.sin_port = htons(auth_server->authserv_http_port); 
     268                their_addr.sin_addr = *h_addr; 
     269                memset(&(their_addr.sin_zero), '\0', sizeof(their_addr.sin_zero)); 
     270                free (h_addr); 
     271 
     272                if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { 
     273                        debug(LOG_ERR, "Level %d: Failed to create a new SOCK_STREAM socket: %s", strerror(errno)); 
     274                        return(-1); 
     275                } 
     276 
     277                if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) { 
     278                        /* 
     279                         * Failed to connect 
     280                         * Mark the server as bad and try the next one 
     281                         */ 
     282                        debug(LOG_DEBUG, "Level %d: Failed to connect to auth server %s:%d (%s). Marking it as bad and trying next if possible", level, hostname, auth_server->authserv_http_port, strerror(errno)); 
     283                        close(sockfd); 
     284                        mark_auth_server_bad(auth_server); 
     285                        return _connect_auth_server(level); 
     286                } 
     287                else { 
     288                        /* 
     289                         * We have successfully connected 
     290                         */ 
     291                        debug(LOG_DEBUG, "Level %d: Successfully connected to auth server %s:%d", level, hostname, auth_server->authserv_http_port); 
     292                        return sockfd; 
     293                } 
     294        } 
     295} 
     296 
     297/* config->authserv_maxtries */ 
  • trunk/wifidog/src/centralserver.h

    r137 r469  
    3838int auth_server_request(t_authresponse *authresponse, char *request_type, char *ip, char *mac, char *token, long int incoming, long int outgoing); 
    3939 
     40/** @brief Tries really hard to connect to an auth server.  Returns a connected file descriptor or -1 on error */ 
     41int connect_auth_server(); 
     42 
     43/** @brief Helper function called by connect_auth_server() to do the actual work including recursion - do not call directly */ 
     44int _connect_auth_server(int level); 
     45 
    4046#endif /* _CENTRALSERVER_H_ */ 
  • trunk/wifidog/src/conf.c

    r467 r469  
    746746        t_auth_serv     *tmp; 
    747747 
    748         /* lock mutex so two different threads both don't mark the same 
    749          * server as bad */ 
    750         pthread_mutex_lock(&config_mutex); 
    751  
    752748        if (config.auth_servers == bad_server && bad_server->next != NULL) { 
    753749                /* Go to the last */ 
     
    761757        } 
    762758 
    763         mark_offline(); 
    764  
    765         pthread_mutex_unlock(&config_mutex); 
    766  
    767         fw_clear_authservers(); 
    768         fw_set_authservers(); 
    769 } 
     759} 
  • trunk/wifidog/src/fw_iptables.c

    r467 r469  
    168168    config = config_get_config(); 
    169169     
    170     LOCK_CONFIG(); 
    171      
    172170    for (auth_server = config->auth_servers; auth_server != NULL; auth_server = auth_server->next) { 
    173             if (auth_server->last_ip == NULL || strcmp(auth_server->last_ip, "0.0.0.0") == 0) { 
    174                 iptables_do_command("-t filter -A " TABLE_WIFIDOG_AUTHSERVERS " -d %s -j ACCEPT", auth_server->authserv_hostname); 
    175             } else { 
     171            if (auth_server->last_ip && strcmp(auth_server->last_ip, "0.0.0.0") != 0) { 
    176172                iptables_do_command("-t filter -A " TABLE_WIFIDOG_AUTHSERVERS " -d %s -j ACCEPT", auth_server->last_ip); 
    177173            } 
    178174    } 
    179175 
    180     UNLOCK_CONFIG(); 
    181176} 
    182177 
  • trunk/wifidog/src/http.c

    r467 r469  
    7878         
    7979        if (!is_online()) { 
    80                 /* No point re-directing them to the auth server if we haven't been able to talk 
    81                  * to it for a while */ 
    82                 httpdOutput(r, "<html><head><title>Currently unavailable</title></head><body><h1>Uh oh!</h1>"); 
     80                /* The internet connection is down at the moment  - apologize and do not redirect anywhere */ 
     81                httpdOutput(r, "<html><head><title>Internet access currently unavailable</title></head><body><h1>Uh oh!</h1>"); 
     82                httpdOutput(r, "We apologize, but it seems that the internet connection that powers this hotspot is temporarily unavailable."); 
     83                httpdOutput(r, "<p>"); 
     84                httpdOutput(r, "If at all possible, please notify the owners of this hotspot that the internet connection is out of service."); 
     85                httpdOutput(r, "<p>"); 
     86                httpdOutput(r, "The maintainers of this network are aware of this disruption.  We hope that this situation will be resolved soon."); 
     87                httpdOutput(r, "<p>"); 
     88                httpdPrintf(r, "In a while please <a href='%s'>click here</a> to try your request again.", tmp_url); 
     89                httpdOutput(r, "</body></html>"); 
     90                debug(LOG_INFO, "Sent %s an apology since I am not online - no point sending them to auth server", r->clientAddr); 
     91        } 
     92        else if (!is_auth_online()) { 
     93                /* The auth server is down at the moment - apologize and do not redirect anywhere */ 
     94                httpdOutput(r, "<html><head><title>Login screen currently unavailable</title></head><body><h1>Uh oh!</h1>"); 
    8395                httpdOutput(r, "We apologize, but it seems that we are currently unable to re-direct you to the login screen."); 
    8496                httpdOutput(r, "<p>"); 
    8597                httpdOutput(r, "The maintainers of this network are aware of this disruption.  We hope that this situation will be resolved soon."); 
    8698                httpdOutput(r, "<p>"); 
    87                 httpdPrintf(r, "In a while please <a href='%s'>click here</a> to try again.", tmp_url); 
     99                httpdPrintf(r, "In a couple of minutes please <a href='%s'>click here</a> to try your request again.", tmp_url); 
    88100                httpdOutput(r, "</body></html>"); 
    89                 debug(LOG_INFO, "Sent %s an apology since I am not online - no point sending them to auth server", r->clientAddr); 
     101                debug(LOG_INFO, "Sent %s an apology since auth server not online - no point sending them to auth server", r->clientAddr); 
    90102        } 
    91103        else { 
     
    114126                                config->gw_id, 
    115127                                url); 
    116                 debug(LOG_INFO, "Captured %s and re-directed them to login " 
    117                         "page", r->clientAddr); 
     128                debug(LOG_INFO, "Captured %s and re-directed them to login page", r->clientAddr); 
    118129                free(newlocation); 
    119130        } 
  • trunk/wifidog/src/ping_thread.c

    r467 r469  
    9898        size_t                  numbytes, 
    9999                                totalbytes; 
    100         int                     sockfd, 
    101                                 nfds, 
    102                                 done, 
    103                                 i; 
    104         t_auth_serv             *auth_server; 
    105         char                    request[MAX_BUF], 
    106                                 *tmp_addr; 
    107         struct in_addr          *h_addr; 
    108         struct sockaddr_in      their_addr; 
     100        int                     sockfd, nfds, done; 
     101        char                    request[MAX_BUF]; 
    109102        fd_set                  readfds; 
    110103        struct timeval          timeout; 
     
    117110        debug(LOG_DEBUG, "Entering ping()"); 
    118111         
    119         if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { 
    120                 debug(LOG_ERR, "socket(): %s", strerror(errno)); 
    121                 exit(1); 
    122         } 
    123  
    124         auth_server = get_auth_server(); 
    125  
    126         debug(LOG_DEBUG, "Using auth server %s", 
    127                         auth_server->authserv_hostname); 
    128          
    129         debug(LOG_DEBUG, "Resolving IP"); 
    130         if ((h_addr = (struct in_addr *)wd_gethostbyname(auth_server->authserv_hostname)) == NULL) { 
    131                 debug(LOG_ERR, "Failed to resolve %s via gethostbyname" 
    132                                 "(): %s", auth_server->authserv_hostname,  
    133                                 strerror(errno)); 
    134                 debug(LOG_ERR, "Bumping auth server to last in line."); 
    135                 mark_auth_server_bad(auth_server); 
    136                 close(sockfd); 
     112        sockfd = connect_auth_server(); 
     113        if (sockfd == -1) { 
     114                /* 
     115                 * No auth servers for me to talk to 
     116                 */ 
    137117                return; 
    138118        } 
    139  
    140         if (auth_server->last_ip == NULL) { 
    141                 auth_server->last_ip = safe_strdup(inet_ntoa(*h_addr)); 
    142         } else { 
    143                 tmp_addr = safe_strdup(inet_ntoa(*h_addr)); 
    144                 if (strcmp(auth_server->last_ip, tmp_addr) != 0) { 
    145                         free(auth_server->last_ip); 
    146                         auth_server->last_ip = tmp_addr; 
    147                         fw_clear_authservers(); 
    148                         fw_set_authservers(); 
    149                 } else { 
    150                         free(tmp_addr); 
    151                 } 
    152         } 
    153  
    154         their_addr.sin_family = AF_INET; 
    155         their_addr.sin_port = htons(auth_server->authserv_http_port); 
    156         their_addr.sin_addr = *h_addr; 
    157         memset(&(their_addr.sin_zero), '\0', sizeof(their_addr.sin_zero)); 
    158  
    159         debug(LOG_INFO, "Connecting to auth server %s on port %d",  
    160                         auth_server->authserv_hostname,  
    161                         auth_server->authserv_http_port); 
    162  
    163         if (connect(sockfd, (struct sockaddr *)&their_addr, 
    164                                 sizeof(struct sockaddr)) == -1) { 
    165                 debug(LOG_ERR, "connect(): %s", strerror(errno)); 
    166                 debug(LOG_ERR, "Bumping auth server to last in line."); 
    167                 mark_auth_server_bad(auth_server); 
    168                 close(sockfd); 
    169                 free(h_addr); 
    170                 return; 
    171         } 
    172         free(h_addr); 
    173                  
    174         mark_online(); 
    175119 
    176120        /* 
     
    206150                        "Host: %s\n" 
    207151                        "\n", 
    208                         auth_server->authserv_path, 
     152                        config_get_config()->auth_servers->authserv_path, 
    209153                        config_get_config()->gw_id, 
    210154                        sys_uptime, 
     
    213157                        (long unsigned int)((long unsigned int)time(NULL) - (long unsigned int)started_time), 
    214158                        VERSION, 
    215                         auth_server->authserv_hostname); 
     159                        config_get_config()->auth_servers->authserv_hostname); 
    216160 
    217161        debug(LOG_DEBUG, "HTTP Request to Server: [%s]", request); 
     
    238182                                        MAX_BUF - (totalbytes + 1)); 
    239183                        if (numbytes < 0) { 
    240                                 debug(LOG_ERR, "read(): %s", strerror(errno)); 
    241                                 mark_auth_server_bad(auth_server); 
     184                                debug(LOG_ERR, "An error occurred while reading from auth server: %s", strerror(errno)); 
     185                                /* FIXME */ 
    242186                                close(sockfd); 
    243187                                return; 
    244                         } else if (numbytes == 0) { 
     188                        } 
     189                        else if (numbytes == 0) { 
    245190                                done = 1; 
    246                         } else { 
     191                        } 
     192                        else { 
    247193                                totalbytes += numbytes; 
    248                                 debug(LOG_DEBUG, "Read %d bytes, total now %d", 
    249                                                 numbytes, totalbytes); 
    250                         } 
    251                 } else if (nfds == 0) { 
    252                         debug(LOG_ERR, "select() timed out"); 
    253                         mark_auth_server_bad(auth_server); 
     194                                debug(LOG_DEBUG, "Read %d bytes, total now %d", numbytes, totalbytes); 
     195                        } 
     196                } 
     197                else if (nfds == 0) { 
     198                        debug(LOG_ERR, "Timed out reading data via select() from auth server"); 
     199                        /* FIXME */ 
    254200                        close(sockfd); 
    255201                        return; 
    256                 } else if (nfds < 0) { 
    257                         debug(LOG_ERR, "select(): %s", strerror(errno)); 
    258                         mark_auth_server_bad(auth_server); 
     202                } 
     203                else if (nfds < 0) { 
     204                        debug(LOG_ERR, "Error reading data via select() from auth server: %s", strerror(errno)); 
     205                        /* FIXME */ 
    259206                        close(sockfd); 
    260207                        return; 
    261208                } 
    262209        } while (!done); 
     210        close(sockfd); 
    263211 
    264212        debug(LOG_DEBUG, "Done reading reply, total %d bytes", totalbytes); 
    265213 
    266         numbytes = totalbytes; 
    267          
    268         request[numbytes] = '\0'; 
    269  
    270         close(sockfd); 
     214        request[totalbytes] = '\0'; 
    271215 
    272216        debug(LOG_DEBUG, "HTTP Response from Server: [%s]", request); 
    273217         
    274         if (!strstr(request, "Pong")) { 
    275                 debug(LOG_ERR, "Primary auth server offline"); 
    276                 debug(LOG_ERR, "Bumping auth server to last in line."); 
    277                 mark_auth_server_bad(auth_server); 
    278                 return; 
    279         } 
    280          
    281         debug(LOG_DEBUG, "Auth Server Says: Pong"); 
     218        if (strstr(request, "Pong") == 0) { 
     219                debug(LOG_WARNING, "Auth server did NOT say pong!"); 
     220                /* FIXME */ 
     221        } 
     222        else { 
     223                debug(LOG_DEBUG, "Auth Server Says: Pong"); 
     224        } 
     225 
    282226        return;  
    283227} 
  • trunk/wifidog/src/util.c

    r467 r469  
    5656static time_t last_online_time = 0; 
    5757static time_t last_offline_time = 0; 
     58static time_t last_auth_online_time = 0; 
     59static time_t last_auth_offline_time = 0; 
    5860 
    5961/** Fork a child and execute a shell command, the parent 
     
    110112        if (he == NULL) { 
    111113                free(h_addr); 
     114                UNLOCK_GHBN(); 
    112115                mark_offline(); 
    113                 UNLOCK_GHBN(); 
    114116                return NULL; 
    115117        } 
     
    154156 
    155157void mark_online() { 
     158        int isonline; 
     159        isonline = is_online(); 
    156160        time(&last_online_time); 
     161        if (!isonline) { 
     162                debug(LOG_INFO, "ONLINE status changed to ON"); 
     163        } 
    157164} 
    158165 
    159166void mark_offline() { 
     167        int isonline; 
     168        isonline = is_online(); 
    160169        time(&last_offline_time); 
     170        /* If we're offline it definately means the auth server is offline */ 
     171        mark_auth_offline(); 
     172        if (isonline) { 
     173                debug(LOG_INFO, "ONLINE status changed to OFF"); 
     174        } 
    161175} 
    162176 
     
    172186} 
    173187 
     188void mark_auth_online() { 
     189        int isauthonline; 
     190        isauthonline = is_auth_online(); 
     191        time(&last_auth_online_time); 
     192        /* If auth server is online it means we're definately online */ 
     193        mark_online(); 
     194        if (!isauthonline) { 
     195                debug(LOG_INFO, "AUTH_ONLINE status changed to ON"); 
     196        } 
     197} 
     198 
     199void mark_auth_offline() { 
     200        int isauthonline; 
     201        isauthonline = is_auth_online(); 
     202        time(&last_auth_offline_time); 
     203        if (isauthonline) { 
     204                debug(LOG_INFO, "AUTH_ONLINE status changed to OFF"); 
     205        } 
     206} 
     207 
     208int is_auth_online() { 
     209        if (!is_online()) { 
     210                /* If we're not online auth is definately not online :) */ 
     211                return (0); 
     212        } 
     213        else if (last_auth_online_time == 0 || (last_auth_offline_time - last_auth_online_time) >= (config_get_config()->checkinterval * 2) ) { 
     214                /* Auth is  probably offline */ 
     215                return (0); 
     216        } 
     217        else { 
     218                /* Auth is probably online */ 
     219                return (1); 
     220        } 
     221} 
     222 
  • trunk/wifidog/src/util.h

    r424 r469  
    4343int is_online(); 
    4444 
     45/* @brief Sets hint that an auth server online action succeeded */ 
     46void mark_auth_online(); 
     47/* @brief Sets hint that an auth server online action failed */ 
     48void mark_auth_offline(); 
     49/* @brief Returns a guess (true or false) on whether we're an auth server is online or not based on previous calls to mark_auth_online and mark_auth_offline */ 
     50int is_auth_online(); 
     51 
    4552#define LOCK_GHBN() do { \ 
    4653        debug(LOG_DEBUG, "Locking wd_gethostbyname()"); \ 
  • trunk/wifidog/src/wdctl_thread.c

    r463 r469  
    225225        uptime -= minutes * 60; 
    226226        seconds = uptime; 
    227         snprintf((buffer + len), (sizeof(buffer) - len), "Uptime: %ud %uh %um %us\n\n", days, hours, minutes, seconds); 
     227 
     228        snprintf((buffer + len), (sizeof(buffer) - len), "Uptime: %ud %uh %um %us\n", days, hours, minutes, seconds); 
     229        len = strlen(buffer); 
     230         
     231        snprintf((buffer + len), (sizeof(buffer) - len), "is_online: %s\n", (is_online() ? "yes" : "no")); 
     232        len = strlen(buffer); 
     233         
     234        snprintf((buffer + len), (sizeof(buffer) - len), "is_auth_online: %s\n\n", (is_auth_online() ? "yes" : "no")); 
    228235        len = strlen(buffer); 
    229236 
     
    277284 
    278285    UNLOCK_CONFIG(); 
    279          
     286 
    280287        write(fd, buffer, len); 
    281288}