Changeset 64

Show
Ignore:
Timestamp:
04/14/04 17:14:10 (9 years ago)
Author:
alexcv
Message:

Switched to threads

Location:
trunk/wifidog
Files:
2 added
2 removed
9 modified

Legend:

Unmodified
Added
Removed
  • trunk/wifidog/ChangeLog

    r62 r64  
    11# $Header$ 
     22004-04-14  Alexandre Carmel-Veilleux <acv@acv.ca> 
     3        * Switched to threads. Alpha quality build, at best 
     4 
    252004-04-12  Alexandre Carmel-Veilleux <acv@acv.ca> 
    36        * Changed child return value handling, again. Now it's actually 
  • trunk/wifidog/configure.in

    r30 r64  
    8484BB_ENABLE_DOXYGEN 
    8585 
     86# check for pthread 
     87AC_CHECK_HEADER(pthread.h, , AC_MSG_ERROR(You need the pthread headers) ) 
     88AC_CHECK_LIB(pthread, pthread_create, , AC_MSG_ERROR(You need the pthread library) ) 
     89 
    8690# check for libhttpd 
    8791AC_CHECK_HEADER(httpd.h, , AC_MSG_ERROR(You do not seem to have the libhttpd headers - please obtain libhttpd from http://www.hughes.com.au/products/libhttpd/ , patch it with http://www.topfx.com/dist/libhttpd.custom404.patch and install it) ) 
  • trunk/wifidog/src/Makefile.am

    r43 r64  
    1616        centralserver.c \ 
    1717        http.c \ 
    18         child.c \ 
     18        auth.c \ 
    1919        userclasses.c 
    2020 
     
    2727        centralserver.h \ 
    2828        http.h \ 
    29         child.h \ 
     29        auth.h \ 
    3030        userclasses.h 
    3131 
  • trunk/wifidog/src/common.h

    r46 r64  
    4444#include <errno.h> 
    4545 
     46#include <pthread.h> 
     47 
    4648#include "httpd.h" 
    4749 
     
    5456#include "http.h" 
    5557#include "centralserver.h" 
    56 #include "child.h" 
     58#include "auth.h" 
    5759 
    5860#define MAX_BUF 4096 
  • trunk/wifidog/src/firewall.c

    r59 r64  
    2828#include "common.h" 
    2929 
     30pthread_mutex_t nodes_mutex = PTHREAD_MUTEX_INITIALIZER; 
     31 
    3032extern s_config config; 
    3133 
     
    186188        char script[MAX_BUF]; 
    187189        t_node *p1; 
    188         ChildInfo       *ci; 
    189         pid_t   pid; 
    190190 
    191191        sprintf(script, "%s/%s/%s", config.fwscripts_path, config.fwtype,  
     
    212212                                        p1->counter = counter; 
    213213 
    214                                         ci = new_childinfo(); 
    215                                         ci->ip = strdup(p1->ip); 
    216                                         ci->mac = strdup(p1->mac); 
    217                                         register_child(ci); 
    218  
    219                                         if ((pid = fork()) == 0) { 
    220                                                 profile = authenticate(p1->ip, 
     214                                        profile =  authenticate(p1->ip, 
    221215                                                                p1->mac,  
    222216                                                                p1->token, 
    223217                                                                p1->counter); 
    224                                                  
    225                                                 /* no negatives */ 
    226                                                 if (profile <= 0) 
    227                                                         profile = 0; 
    228                                                  
    229                                                 /* SIGCHLD handler will 
    230                                                  * clean up the mess 
    231                                                  * afterwards */ 
    232                                                 exit(profile); 
     218                                         
     219                                        if (profile <= 0) { 
     220                                                /* failed */ 
     221                                                debug(D_LOG_DEBUG, "Auth " 
     222                                                        "failed for client %s", 
     223                                                        ip); 
     224                                                fw_deny(p1->ip, p1->mac, 
     225                                                        p1->rights->profile); 
     226                                                node_delete(p1); 
     227                                        } else { 
     228                                                /* successful */ 
     229                                                debug(D_LOG_DEBUG, "Updated " 
     230                                                        "client %s counter to " 
     231                                                        "%ld bytes", ip, 
     232                                                        counter); 
     233 
     234                                                if (!check_userrights(p1)) { 
     235                                                        fw_deny(p1->ip, p1->mac, 
     236                                                           p1->rights->profile); 
     237                                                        node_delete(p1); 
     238                                                } 
    233239                                        } 
    234                                         debug(D_LOG_DEBUG, "Forked sub-process" 
    235                                                 " with pid %d", (int)pid); 
    236                                         debug(D_LOG_DEBUG, "Updated client %s " 
    237                                                 "counter to %ld bytes", ip,  
    238                                                 counter); 
    239                                         free_childinfo(ci); 
    240240                                } 
    241241                        } 
     
    248248node_init(void) 
    249249{ 
     250 
     251        pthread_mutex_lock(&nodes_mutex); 
    250252        firstnode = NULL; 
     253        pthread_mutex_unlock(&nodes_mutex); 
    251254} 
    252255 
     
    254257node_add(char *ip, char *mac, char *token, long int counter, int active) 
    255258{ 
    256         t_node *curnode, 
    257         *prevnode; 
    258  
     259        t_node  *curnode, 
     260                *prevnode; 
     261 
     262        pthread_mutex_lock(&nodes_mutex); 
     263         
    259264        prevnode = NULL; 
    260265        curnode = firstnode; 
     
    288293        debug(D_LOG_DEBUG, "Added a new node to linked list: IP: %s Token: %s", 
    289294                ip, token); 
     295         
     296        pthread_mutex_unlock(&nodes_mutex); 
    290297 
    291298        return curnode; 
     
    296303{ 
    297304        t_node *ptr; 
     305         
     306        pthread_mutex_lock(&nodes_mutex); 
    298307 
    299308        ptr = firstnode; 
    300309        while (NULL != ptr) { 
    301                 if (0 == strcmp(ptr->ip, ip)) 
     310                if (0 == strcmp(ptr->ip, ip)) { 
     311                        pthread_mutex_unlock(&nodes_mutex); 
    302312                        return ptr; 
     313                } 
     314                ptr = ptr->next; 
     315        } 
     316 
     317        pthread_mutex_unlock(&nodes_mutex); 
     318 
     319        return NULL; 
     320} 
     321 
     322t_node * 
     323node_find_by_token(char *token) 
     324{ 
     325        t_node *ptr; 
     326 
     327        pthread_mutex_lock(&nodes_mutex); 
     328 
     329        ptr = firstnode; 
     330        while (NULL != ptr) { 
     331                if (0 == strcmp(ptr->token, token)) { 
     332                        pthread_mutex_unlock(&nodes_mutex); 
     333                        return ptr; 
     334                } 
    303335                ptr = ptr->next; 
    304336        }  
    305337 
    306         return NULL; 
    307 } 
    308  
    309 t_node * 
    310 node_find_by_token(char *token) 
    311 { 
    312         t_node *ptr; 
    313  
    314         ptr = firstnode; 
    315         while (NULL != ptr) { 
    316                 if (0 == strcmp(ptr->token, token)) 
    317                         return ptr; 
    318                 ptr = ptr->next; 
    319         }  
    320  
     338        pthread_mutex_unlock(&nodes_mutex); 
     339                         
    321340        return NULL; 
    322341} 
     
    345364{ 
    346365        t_node  *ptr; 
     366         
     367        pthread_mutex_lock(&nodes_mutex); 
    347368 
    348369        ptr = firstnode; 
     
    359380                } 
    360381        } 
    361 } 
     382 
     383        pthread_mutex_unlock(&nodes_mutex); 
     384} 
     385 
     386int 
     387check_userrights(t_node *node) 
     388{ 
     389        if (node->rights->end_time <= time(NULL)) { 
     390                debug(D_LOG_DEBUG, "Connection %s has expired", node->ip); 
     391                return 0; 
     392        } 
     393 
     394        return 1; 
     395} 
     396 
  • trunk/wifidog/src/firewall.h

    r45 r64  
    2929 
    3030typedef struct _t_node { 
    31         void    *next; 
     31        struct  _t_node *next; 
    3232        char    *ip, 
    3333                *mac, 
    3434                *token; 
    35         int     active; /* boolean */ 
     35        int     active, /* boolean */ 
     36                fd;     /* socket */ 
    3637        long    int     counter; 
    3738        UserRights      *rights; 
     
    5455void free_node(t_node *node); 
    5556 
     57int check_userrights(t_node *node); 
     58 
    5659#endif /* _FIREWALL_H_ */ 
  • trunk/wifidog/src/gateway.c

    r58 r64  
    3737        httpd * webserver; 
    3838        int result; 
     39        pthread_t       tid; 
    3940 
    4041        /* Initialize the linked list */ 
     
    6263        fw_init(); 
    6364 
    64         last_checked = time(NULL); 
    65  
     65        /* start clean up thread */ 
     66        pthread_create(&tid, NULL, (void *)cleanup_thread, NULL); 
     67        pthread_detach(tid); 
     68         
    6669        debug(D_LOG_DEBUG, "Waiting for connections"); 
    6770        while(1) { 
     
    104107                        httpdEndRequest(webserver); 
    105108                } 
    106  
    107                 if (time(NULL) - last_checked > config.checkinterval) { 
    108                         fw_counter(); 
    109                         last_checked = time(NULL); 
    110                 } 
    111109        } 
    112110 
     
    153151 
    154152void 
     153sigchld_handler(int s) 
     154{ 
     155        int     status; 
     156         
     157        wait(&status); 
     158} 
     159 
     160void 
    155161termination_handler(int s) 
    156162{ 
  • trunk/wifidog/src/gateway.h

    r57 r64  
    2929 
    3030void termination_handler(int s); 
     31void sigchld_handler(int s); 
    3132void init_signals(void); 
    3233void check_counters(void); 
  • trunk/wifidog/src/http.c

    r62 r64  
    7272http_callback_auth(httpd * webserver) 
    7373{ 
    74         ChildInfo * ci; 
     74        t_node  *node; 
    7575        httpVar * token; 
    7676        char * mac; 
    7777        int profile; 
    7878        int temp; 
    79         pid_t pid; 
     79        pthread_t tid; 
    8080 
    8181        if (token = httpdGetVariableByName(webserver, "token")) { 
     
    9191                        // We have their MAC address 
    9292 
    93                         /* register child info */ 
    94                         ci = new_childinfo(); 
    95                         ci->ip = strdup(webserver->clientAddr); 
    96                         ci->mac = strdup(mac); 
    97                         register_child(ci); 
    98  
    9993                        if (!node_find_by_ip(webserver->clientAddr)) { 
    10094                                node_add(webserver->clientAddr, mac,  
     
    10296                        } 
    10397 
    104                         if ((pid = fork()) == 0) { 
    105                                 profile = authenticate(webserver->clientAddr,  
    106                                                 mac, token->value, 0); 
    107                                 if (profile == -1) { 
    108                                         // Error talking to central server 
    109                                         debug(D_LOG_ERR, "Got %d from central " 
    110                                                 "server authenticating token " 
    111                                                 "%s from %s at %s", profile,  
    112                                                 token->value,  
    113                                                 webserver->clientAddr, mac); 
    114                                         httpdOutput(webserver, "Access denied:" 
    115                                                 "We did not get a valid " 
    116                                                 "answer from the central " 
    117                                                 "server"); 
    118                                         httpdEndRequest(webserver); 
    119                                         exit(0); 
    120                                 } 
    121                                 else if (profile == 0) { 
    122                                         // Central server said invalid token 
    123                                         httpdOutput(webserver, "Your " 
    124                                                 "authentication has failed or " 
    125                                                 "timed-out.  Please re-login"); 
    126                                         httpdEndRequest(webserver); 
    127                                         exit(0); 
    128                                 } 
    129                                 else { 
    130                                         // Successfull, what do we do here? 
    131                                         httpdEndRequest(webserver); 
    132                                         exit(profile); 
    133                                 } 
    134                         } else { 
    135                                 debug(D_LOG_DEBUG, "Forked sub process with " 
    136                                         "pid %d", (int)pid); 
    137                                 free(mac); 
    138                                 free_childinfo(ci); 
    139                                 webserver->clientSock = -1; 
    140                                 /* So we don't get shutdown, 
    141                                  * there's no error handling in 
    142                                  * httpdEndRequest */ 
    143                         } 
     98                        node = node_find_by_ip(webserver->clientAddr); 
     99 
     100                        node->fd = webserver->clientSock; 
     101                        webserver->clientSock = -1; 
     102 
     103                        /* start sub process */ 
     104                        pthread_create(&tid, NULL, (void *)auth_thread, 
     105                                        (void *)node); 
     106                        pthread_detach(tid); 
     107 
     108                        free(mac); 
    144109                } 
    145110        } else { 
     
    148113        } 
    149114} 
    150