Changeset 438

Show
Ignore:
Timestamp:
02/09/05 00:45:46 (4 years ago)
Author:
minaguib
Message:

* 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.
* 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()
* Different apology in 404 handler depending on whether internet is down or just auth server is down
* wdctl status now shows status of is_online and is_auth_online

Still to do:
Create config section to set pass-through hostnames for fake DNS
Bugfix: Need to clear CONNTRACK in iptables for client<-udp->hotspot:53 after they authenticate so they don't keep hitting the fake DNS server

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/CaptiveDNS/wifidog/ChangeLog

    r431 r438  
    11# $Header$ 
     2 
     32005-02-09 Mina Naguib <mina@ilesansfil.org> 
     4        * Consolidated much of the networking calls to the auth servers into a 
     5        magical function called connect_auth_server() that's responsible for 
     6        dns lookup, connecting, marking servers bad, marking online/auth_online, 
     7        and refreshing the firewall rules. 
     8        * Added new functions mark_auth_online(), mark_auth_offline() and 
     9        is_auth_online() - similar in nature to is_online() etc. except tailored to 
     10        decide on auth servers status - currently being called by 
     11        connect_auth_server() 
     12        * Different apology in 404 handler depending on whether internet is down or 
     13        just auth server is down 
     14        * wdctl status now shows status of is_online and is_auth_online 
    215 
    3162005-02-06 Mina Naguib <mina@ilesansfil.org> 
  • branches/CaptiveDNS/wifidog/src/centralserver.c

    r424 r438  
    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 server's DNS server is probably dead. Try the next 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 = 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 */ 
  • branches/CaptiveDNS/wifidog/src/centralserver.h

    r137 r438  
    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_ */ 
  • branches/CaptiveDNS/wifidog/src/conf.c

    r431 r438  
    759759        t_auth_serv     *tmp; 
    760760 
    761         /* lock mutex so two different threads both don't mark the same 
    762          * server as bad */ 
    763         pthread_mutex_lock(&config_mutex); 
    764  
    765761        if (config.auth_servers == bad_server && bad_server->next != NULL) { 
    766762                /* Go to the last */ 
     
    774770        } 
    775771 
    776         mark_offline(); 
    777  
    778         pthread_mutex_unlock(&config_mutex); 
    779  
    780         fw_clear_authservers(); 
    781         fw_set_authservers(); 
    782 
     772
  • branches/CaptiveDNS/wifidog/src/fw_iptables.c

    r431 r438  
    160160    config = config_get_config(); 
    161161     
    162     LOCK_CONFIG(); 
    163      
    164162    for (auth_server = config->auth_servers; auth_server != NULL; auth_server = auth_server->next) { 
    165             if (auth_server->last_ip == NULL || strcmp(auth_server->last_ip, "0.0.0.0") == 0) { 
    166                 iptables_do_command("-t nat -A " TABLE_WIFIDOG_AUTHSERVERS " -d %s -j ACCEPT", auth_server->authserv_hostname); 
    167             } else { 
     163            if (auth_server->last_ip && strcmp(auth_server->last_ip, "0.0.0.0") != 0) { 
    168164                iptables_do_command("-t nat -A " TABLE_WIFIDOG_AUTHSERVERS " -d %s -j ACCEPT", auth_server->last_ip); 
    169165            } 
    170166    } 
    171167 
    172     UNLOCK_CONFIG(); 
    173168} 
    174169 
  • branches/CaptiveDNS/wifidog/src/http.c

    r428 r438  
    7777         
    7878        if (!is_online()) { 
    79                 /* No point re-directing them to the auth server if we haven't been able to talk 
    80                  * to it for a while */ 
    81                 httpdOutput(r, "<html><head><title>Currently unavailable</title></head><body><h1>Uh oh!</h1>"); 
     79                /* The internet connection is down at the moment  - apologize and do not redirect anywhere */ 
     80                httpdOutput(r, "<html><head><title>Internet access currently unavailable</title></head><body><h1>Uh oh!</h1>"); 
     81                httpdOutput(r, "We apologize, but it seems that the internet connection that powers this hotspot is temporarily unavailable."); 
     82                httpdOutput(r, "<p>"); 
     83                httpdOutput(r, "If at all possible, please notify the owners of this hotspot that the internet connection is out of service."); 
     84                httpdOutput(r, "<p>"); 
     85                httpdOutput(r, "The maintainers of this network are aware of this disruption.  We hope that this situation will be resolved soon."); 
     86                httpdOutput(r, "<p>"); 
     87                httpdPrintf(r, "In a while please <a href='%s'>click here</a> to try your request again.", tmp_url); 
     88                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); 
     90        } 
     91        else if (!is_auth_online()) { 
     92                /* The auth server is down at the moment - apologize and do not redirect anywhere */ 
     93                httpdOutput(r, "<html><head><title>Login screen currently unavailable</title></head><body><h1>Uh oh!</h1>"); 
    8294                httpdOutput(r, "We apologize, but it seems that we are currently unable to re-direct you to the login screen."); 
    8395                httpdOutput(r, "<p>"); 
    8496                httpdOutput(r, "The maintainers of this network are aware of this disruption.  We hope that this situation will be resolved soon."); 
    8597                httpdOutput(r, "<p>"); 
    86                 httpdPrintf(r, "In a while please <a href='%s'>click here</a> to try again.", tmp_url); 
     98                httpdPrintf(r, "In a couple of minutes please <a href='%s'>click here</a> to try your request again.", tmp_url); 
    8799                httpdOutput(r, "</body></html>"); 
    88                 debug(LOG_INFO, "Sent %s an apology since I am not online - no point sending them to auth server", r->clientAddr); 
     100                debug(LOG_INFO, "Sent %s an apology since auth server not online - no point sending them to auth server", r->clientAddr); 
    89101        } 
    90102        else if ((asprintf(&newlocation, "Location: %s://%s:%d%slogin?" 
     
    115127                                config->gw_id, 
    116128                                url); 
    117                 debug(LOG_INFO, "Captured %s and re-directed them to login " 
    118                         "page", r->clientAddr); 
     129                debug(LOG_INFO, "Captured %s and re-directed them to login page", r->clientAddr); 
    119130                free(newlocation); 
    120131        } 
  • branches/CaptiveDNS/wifidog/src/ping_thread.c

    r424 r438  
    9292        size_t                  numbytes, 
    9393                                totalbytes; 
    94         int                     sockfd, 
    95                                 nfds, 
    96                                 done, 
    97                                 i; 
    98         t_auth_serv             *auth_server; 
    99         char                    request[MAX_BUF], 
    100                                 *tmp_addr; 
    101         struct in_addr          *h_addr; 
    102         struct sockaddr_in      their_addr; 
     94        int                     sockfd, nfds, done; 
     95        char                    request[MAX_BUF]; 
    10396        fd_set                  readfds; 
    10497        struct timeval          timeout; 
     
    10699        debug(LOG_DEBUG, "Entering ping()"); 
    107100         
    108         if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { 
    109                 debug(LOG_ERR, "socket(): %s", strerror(errno)); 
    110                 exit(1); 
    111         } 
    112  
    113         auth_server = get_auth_server(); 
    114  
    115         debug(LOG_DEBUG, "Using auth server %s", 
    116                         auth_server->authserv_hostname); 
    117          
    118         debug(LOG_DEBUG, "Resolving IP"); 
    119         if ((h_addr = (struct in_addr *)wd_gethostbyname(auth_server->authserv_hostname)) == NULL) { 
    120                 debug(LOG_ERR, "Failed to resolve %s via gethostbyname" 
    121                                 "(): %s", auth_server->authserv_hostname,  
    122                                 strerror(errno)); 
    123                 debug(LOG_ERR, "Bumping auth server to last in line."); 
    124                 mark_auth_server_bad(auth_server); 
    125                 close(sockfd); 
     101        sockfd = connect_auth_server(); 
     102        if (sockfd == -1) { 
     103                /* 
     104                 * No auth servers for me to talk to 
     105                 */ 
    126106                return; 
    127107        } 
    128  
    129         if (auth_server->last_ip == NULL) { 
    130                 auth_server->last_ip = strdup(inet_ntoa(*h_addr)); 
    131                 if (auth_server->last_ip == NULL) { 
    132                         debug(LOG_CRIT, "Could not allocate memory, Banzai!"); 
    133                         exit(-1); 
    134                 } 
    135         } else { 
    136                 tmp_addr = strdup(inet_ntoa(*h_addr)); 
    137                 if (strcmp(auth_server->last_ip, tmp_addr) != 0) { 
    138                         free(auth_server->last_ip); 
    139                         auth_server->last_ip = tmp_addr; 
    140                         fw_clear_authservers(); 
    141                         fw_set_authservers(); 
    142                 } else { 
    143                         free(tmp_addr); 
    144                 } 
    145         } 
    146  
    147         their_addr.sin_family = AF_INET; 
    148         their_addr.sin_port = htons(auth_server->authserv_http_port); 
    149         their_addr.sin_addr = *h_addr; 
    150         memset(&(their_addr.sin_zero), '\0', sizeof(their_addr.sin_zero)); 
    151  
    152         debug(LOG_INFO, "Connecting to auth server %s on port %d",  
    153                         auth_server->authserv_hostname,  
    154                         auth_server->authserv_http_port); 
    155  
    156         if (connect(sockfd, (struct sockaddr *)&their_addr, 
    157                                 sizeof(struct sockaddr)) == -1) { 
    158                 debug(LOG_ERR, "connect(): %s", strerror(errno)); 
    159                 debug(LOG_ERR, "Bumping auth server to last in line."); 
    160                 mark_auth_server_bad(auth_server); 
    161                 close(sockfd); 
    162                 free(h_addr); 
    163                 return; 
    164         } 
    165         free(h_addr); 
    166                  
    167         mark_online(); 
    168108 
    169109        snprintf(request, sizeof(request) - 1, "GET %sping/?gw_id=%s HTTP/1.0\n" 
     
    171111                        "Host: %s\n" 
    172112                        "\n", 
    173                         auth_server->authserv_path, 
     113                        config_get_config()->auth_servers->authserv_path, 
    174114                        config_get_config()->gw_id, 
    175115                        VERSION, 
    176                         auth_server->authserv_hostname); 
     116                        config_get_config()->auth_servers->authserv_hostname); 
    177117 
    178118        debug(LOG_DEBUG, "HTTP Request to Server: [%s]", request); 
     
    199139                                        MAX_BUF - (totalbytes + 1)); 
    200140                        if (numbytes < 0) { 
    201                                 debug(LOG_ERR, "read(): %s", strerror(errno)); 
    202                                 mark_auth_server_bad(auth_server); 
     141                                debug(LOG_ERR, "An error occurred while reading from auth server: %s", strerror(errno)); 
     142                                /* FIXME */ 
    203143                                close(sockfd); 
    204144                                return; 
    205                         } else if (numbytes == 0) { 
     145                        } 
     146                        else if (numbytes == 0) { 
    206147                                done = 1; 
    207                         } else { 
     148                        } 
     149                        else { 
    208150                                totalbytes += numbytes; 
    209                                 debug(LOG_DEBUG, "Read %d bytes, total now %d", 
    210                                                 numbytes, totalbytes); 
     151                                debug(LOG_DEBUG, "Read %d bytes, total now %d", numbytes, totalbytes); 
    211152                        } 
    212                 } else if (nfds == 0) { 
    213                         debug(LOG_ERR, "select() timed out"); 
    214                         mark_auth_server_bad(auth_server); 
     153                } 
     154                else if (nfds == 0) { 
     155                        debug(LOG_ERR, "Timed out reading data via select() from auth server"); 
     156                        /* FIXME */ 
    215157                        close(sockfd); 
    216158                        return; 
    217                 } else if (nfds < 0) { 
    218                         debug(LOG_ERR, "select(): %s", strerror(errno)); 
    219                         mark_auth_server_bad(auth_server); 
     159                } 
     160                else if (nfds < 0) { 
     161                        debug(LOG_ERR, "Error reading data via select() from auth server: %s", strerror(errno)); 
     162                        /* FIXME */ 
    220163                        close(sockfd); 
    221164                        return; 
    222165                } 
    223166        } while (!done); 
     167        close(sockfd); 
    224168 
    225169        debug(LOG_DEBUG, "Done reading reply, total %d bytes", totalbytes); 
    226170 
    227         numbytes = totalbytes; 
    228          
    229         request[numbytes] = '\0'; 
    230  
    231         close(sockfd); 
     171        request[totalbytes] = '\0'; 
    232172 
    233173        debug(LOG_DEBUG, "HTTP Response from Server: [%s]", request); 
    234174         
    235         if (!strstr(request, "Pong")) { 
    236                 debug(LOG_ERR, "Primary auth server offline"); 
    237                 debug(LOG_ERR, "Bumping auth server to last in line."); 
    238                 mark_auth_server_bad(auth_server); 
    239                 return; 
     175        if (strstr(request, "Pong") == 0) { 
     176                debug(LOG_WARNING, "Auth server did NOT say pong!"); 
     177                /* FIXME */ 
    240178        } 
    241          
    242         debug(LOG_DEBUG, "Auth Server Says: Pong"); 
     179        else { 
     180                debug(LOG_DEBUG, "Auth Server Says: Pong"); 
     181        } 
     182 
    243183        return;  
    244184} 
  • branches/CaptiveDNS/wifidog/src/util.c

    r426 r438  
    5555static time_t last_online_time = 0; 
    5656static time_t last_offline_time = 0; 
     57static time_t last_auth_online_time = 0; 
     58static time_t last_auth_offline_time = 0; 
    5759 
    5860/** Fork a child and execute a shell command, the parent 
     
    112114        if (he == NULL) { 
    113115                free(h_addr); 
     116                UNLOCK_GHBN(); 
    114117                mark_offline(); 
    115                 UNLOCK_GHBN(); 
    116118                return NULL; 
    117119        } 
     
    156158 
    157159void mark_online() { 
     160        int isonline; 
     161        isonline = is_online(); 
    158162        time(&last_online_time); 
     163        if (!isonline) { 
     164                debug(LOG_INFO, "ONLINE status changed to ON"); 
     165        } 
    159166} 
    160167 
    161168void mark_offline() { 
     169        int isonline; 
     170        isonline = is_online(); 
    162171        time(&last_offline_time); 
     172        /* If we're offline it definately means the auth server is offline */ 
     173        mark_auth_offline(); 
     174        if (isonline) { 
     175                debug(LOG_INFO, "ONLINE status changed to OFF"); 
     176        } 
    163177} 
    164178 
     
    174188} 
    175189 
     190void mark_auth_online() { 
     191        int isauthonline; 
     192        isauthonline = is_auth_online(); 
     193        time(&last_auth_online_time); 
     194        /* If auth server is online it means we're definately online */ 
     195        mark_online(); 
     196        if (!isauthonline) { 
     197                debug(LOG_INFO, "AUTH_ONLINE status changed to ON"); 
     198        } 
     199} 
     200 
     201void mark_auth_offline() { 
     202        int isauthonline; 
     203        isauthonline = is_auth_online(); 
     204        time(&last_auth_offline_time); 
     205        if (isauthonline) { 
     206                debug(LOG_INFO, "AUTH_ONLINE status changed to OFF"); 
     207        } 
     208} 
     209 
     210int is_auth_online() { 
     211        if (!is_online()) { 
     212                /* If we're not online auth is definately not online :) */ 
     213                return (0); 
     214        } 
     215        else if (last_auth_online_time == 0 || (last_auth_offline_time - last_auth_online_time) >= (config_get_config()->checkinterval * 2) ) { 
     216                /* Auth is  probably offline */ 
     217                return (0); 
     218        } 
     219        else { 
     220                /* Auth is probably online */ 
     221                return (1); 
     222        } 
     223} 
     224 
  • branches/CaptiveDNS/wifidog/src/util.h

    r424 r438  
    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()"); \ 
  • branches/CaptiveDNS/wifidog/src/wdctl_thread.c

    r422 r438  
    263263    UNLOCK_CONFIG(); 
    264264         
     265        snprintf((buffer + len), (sizeof(buffer) - len), "\nis_online: %s\n", (is_online() ? "yes" : "no")); 
     266        len = strlen(buffer); 
     267         
     268        snprintf((buffer + len), (sizeof(buffer) - len), "is_auth_online: %s\n", (is_auth_online() ? "yes" : "no")); 
     269        len = strlen(buffer); 
     270 
    265271        write(fd, buffer, len); 
    266272}