Changeset 72

Show
Ignore:
Timestamp:
04/15/04 17:22:58 (9 years ago)
Author:
alexcv
Message:

Reworked mutex locking to protect for the whole duration that the structure
are being used.

Location:
trunk/wifidog
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • trunk/wifidog/ChangeLog

    r71 r72  
    11# $Header$ 
     22004-04-15  Alexandre Carmel-Veilleux <acv@acv.ca> 
     3        * Changed the locking mechanism, now all access to t_node * structs 
     4        are properly protected. 
     5 
    262004-04-15  Alexandre Carmel-Veilleux <acv@acv.ca> 
    37        * Connection now closed if counter hasn't change for one full 
  • trunk/wifidog/src/auth.c

    r67 r72  
    2727 
    2828#include "common.h" 
     29 
     30pthread_mutex_t nodes_mutex = PTHREAD_MUTEX_INITIALIZER; 
    2931 
    3032s_config config; 
     
    5759        UserClasses     *tmp_uc; 
    5860        UserRights      *tmp_ur; 
     61        char    *ip, 
     62                *mac, 
     63                *token; 
    5964 
    60         node = (t_node *)ptr; 
     65        ip = (char *)ptr; 
    6166 
    62         if (node == NULL) 
     67        pthread_mutex_lock(&nodes_mutex); 
     68 
     69        node = node_find_by_ip(ip); 
     70 
     71        if (node == NULL) { 
     72                pthread_mutex_unlock(&nodes_mutex); 
    6373                return; /* Implicit pthread_exit() */ 
    64  
    65         profile = authenticate(node->ip, node->mac, node->token, 0); 
     74        } 
     75         
     76        mac = strdup(node->mac); 
     77        token = strdup(node->token); 
     78         
     79        pthread_mutex_unlock(&nodes_mutex); 
     80                 
     81        profile = authenticate(ip, mac, token, 0); 
     82         
     83        pthread_mutex_lock(&nodes_mutex); 
     84         
     85        /* can't trust the node to still exist */ 
     86        node = node_find_by_ip(ip); 
     87         
     88        /* don't need any of them anymore */ 
     89        free(ip); 
     90        free(token); 
     91        free(mac); 
     92         
     93        if (node == NULL) { 
     94                pthread_mutex_unlock(&nodes_mutex); 
     95                return; 
     96        } 
    6697 
    6798        if (profile == -1) { 
     
    73104                        "answer from the central server"); 
    74105                node->fd = 0; 
     106                pthread_mutex_unlock(&nodes_mutex); 
    75107                return; 
    76108        } else if (profile == 0) { 
     
    79111                        "timed-out.  Please re-login"); 
    80112                node->fd = 0; 
     113                pthread_mutex_unlock(&nodes_mutex); 
    81114                return; 
    82115        } 
     
    93126                _http_output(node->fd, "User Class not defined"); 
    94127                node->fd = 0; 
     128                pthread_mutex_unlock(&nodes_mutex); 
    95129                return; 
    96130        } else { 
     
    119153        node->fd = 0; 
    120154 
     155        pthread_mutex_unlock(&nodes_mutex); 
    121156        return; 
    122157} 
  • trunk/wifidog/src/firewall.c

    r71 r72  
    2828#include "common.h" 
    2929 
    30 pthread_mutex_t nodes_mutex = PTHREAD_MUTEX_INITIALIZER; 
    31  
    3230extern s_config config; 
     31 
     32pthread_mutex_t nodes_mutex; 
    3333 
    3434t_node *firstnode = NULL; 
     
    182182fw_counter(void) 
    183183{ 
    184         FILE *output; 
    185         long int counter; 
    186         int profile, rc; 
    187         char ip[255], mac[255]; 
    188         char script[MAX_BUF]; 
     184        FILE    *output; 
     185        long    int     counter; 
     186        int     profile, 
     187                rc; 
     188        char    ip[255], 
     189                mac[255], 
     190                script[MAX_BUF], 
     191                *token; 
    189192        t_node *p1; 
    190193 
     
    204207                                 * Maybe this should be done on the auth 
    205208                                 * server */ 
     209                         
     210                                pthread_mutex_lock(&nodes_mutex); 
    206211 
    207212                                p1 = node_find_by_ip(ip); 
     
    220225                                        p1->rights->last_checked = time(NULL); 
    221226                                        p1->counter = counter; 
    222  
    223                                         profile =  authenticate(p1->ip, 
    224                                                                 p1->mac,  
    225                                                                 p1->token, 
    226                                                                 p1->counter); 
    227227                                         
    228                                         if (profile <= 0) { 
     228                                        token = strdup(p1->token); 
     229                                         
     230                                        pthread_mutex_unlock(&nodes_mutex); 
     231 
     232                                        profile = authenticate(ip, mac, token, 
     233                                                                counter); 
     234                                         
     235                                        pthread_mutex_lock(&nodes_mutex); 
     236 
     237                                        free(token); 
     238                                         
     239                                        /* may have changed while we held the 
     240                                         * mutex */ 
     241                                        p1 = node_find_by_ip(ip); 
     242 
     243                                        if (p1 == NULL) {        
     244                                                debug(D_LOG_DEBUG, "Node was " 
     245                                                        "freed while being " 
     246                                                        "re-validated!"); 
     247                                        } else if (profile <= 0) { 
    229248                                                /* failed */ 
    230249                                                debug(D_LOG_DEBUG, "Auth " 
     
    248267                                        } 
    249268                                } 
     269                                pthread_mutex_unlock(&nodes_mutex); 
    250270                        } 
    251271                } 
     
    258278{ 
    259279 
    260         pthread_mutex_lock(&nodes_mutex); 
    261280        firstnode = NULL; 
    262         pthread_mutex_unlock(&nodes_mutex); 
    263281} 
    264282 
     
    269287                *prevnode; 
    270288 
    271         pthread_mutex_lock(&nodes_mutex); 
    272          
    273289        prevnode = NULL; 
    274290        curnode = firstnode; 
     
    303319                ip, token); 
    304320         
    305         pthread_mutex_unlock(&nodes_mutex); 
    306  
    307321        return curnode; 
    308322} 
     
    313327        t_node *ptr; 
    314328         
    315         pthread_mutex_lock(&nodes_mutex); 
    316  
    317329        ptr = firstnode; 
    318330        while (NULL != ptr) { 
    319                 if (0 == strcmp(ptr->ip, ip)) { 
    320                         pthread_mutex_unlock(&nodes_mutex); 
     331                if (0 == strcmp(ptr->ip, ip)) 
    321332                        return ptr; 
    322                 } 
    323333                ptr = ptr->next; 
    324334        } 
    325  
    326         pthread_mutex_unlock(&nodes_mutex); 
    327335 
    328336        return NULL; 
     
    334342        t_node *ptr; 
    335343 
    336         pthread_mutex_lock(&nodes_mutex); 
    337  
    338344        ptr = firstnode; 
    339345        while (NULL != ptr) { 
    340                 if (0 == strcmp(ptr->token, token)) { 
    341                         pthread_mutex_unlock(&nodes_mutex); 
     346                if (0 == strcmp(ptr->token, token)) 
    342347                        return ptr; 
    343                 } 
    344348                ptr = ptr->next; 
    345349        }  
    346350 
    347         pthread_mutex_unlock(&nodes_mutex); 
    348                          
    349351        return NULL; 
    350352} 
     
    374376        t_node  *ptr; 
    375377         
    376         pthread_mutex_lock(&nodes_mutex); 
    377  
    378378        ptr = firstnode; 
    379379 
     
    389389                } 
    390390        } 
    391  
    392         pthread_mutex_unlock(&nodes_mutex); 
    393391} 
    394392 
  • trunk/wifidog/src/http.c

    r68 r72  
    2929 
    3030extern s_config config; 
     31 
     32pthread_mutex_t nodes_mutex; 
    3133 
    3234void 
     
    7476        t_node  *node; 
    7577        httpVar * token; 
    76         char * mac; 
     78        char    *mac, 
     79                *ip; 
    7780        int profile; 
    7881        int temp; 
     
    9093                        // We have their MAC address 
    9194 
     95                        pthread_mutex_lock(&nodes_mutex); 
     96                         
    9297                        if ((node = node_find_by_ip(webserver->clientAddr)) 
    9398                                        == NULL) { 
     
    114119                        webserver->clientSock = -1; 
    115120 
     121                        pthread_mutex_unlock(&nodes_mutex); 
     122 
     123                        /* That clientAddr may be freed prior to the thread 
     124                         * finishing. */ 
     125                        ip = strdup(webserver->clientAddr); 
     126                         
    116127                        /* start sub process */ 
    117128                        pthread_create(&tid, NULL, (void *)auth_thread, 
    118                                         (void *)node); 
     129                                        (void *)ip); 
    119130                        pthread_detach(tid); 
    120131