Ticket #465: 0002-Refactor-logout-logic-so-we-can-share-code.patch

File 0002-Refactor-logout-logic-so-we-can-share-code.patch, 6.4 KB (added by wichert@…, 4 years ago)
  • src/client_list.c

    From 2c843ae3d1a1f9a77bf4e906ffb96ef318bb18cc Mon Sep 17 00:00:00 2001
    From: Wichert Akkerman <wichert@wiggy.net>
    Date: Mon, 28 Apr 2008 17:32:26 +0200
    Subject: [PATCH] Refactor logout logic so we can share code
    
    An extra free.. oops
    ---
     src/client_list.c  |   24 +++++++++++++++++-------
     src/client_list.h  |    8 +++++++-
     src/firewall.c     |   44 +++++++++++++++++++++++++++++++++++---------
     src/firewall.h     |    5 +++++
     src/http.c         |   10 ++--------
     src/wdctl_thread.c |    5 +----
     6 files changed, 67 insertions(+), 29 deletions(-)
    
    diff --git a/src/client_list.c b/src/client_list.c
    index 65ab88c..de51975 100644
    a b  
    199199 * @param client Points to the client to be freed 
    200200 */ 
    201201void 
    202 _client_list_free_node(t_client * client) 
     202client_free_node(t_client * client) 
    203203{ 
    204204 
    205205    if (client->mac != NULL) 
     
    224224void 
    225225client_list_delete(t_client * client) 
    226226{ 
     227        client_list_remove(client); 
     228        client_free_node(client); 
     229} 
     230 
     231 
     232/** 
     233 * @brief Removes a client from the connections list 
     234 * 
     235 * @param client Points to the client to be deleted 
     236 */ 
     237void 
     238client_list_remove(t_client * client) 
     239{ 
    227240    t_client         *ptr; 
    228241 
    229242    ptr = firstclient; 
     
    232245        debug(LOG_ERR, "Node list empty!"); 
    233246    } else if (ptr == client) { 
    234247        firstclient = ptr->next; 
    235         _client_list_free_node(client); 
    236248    } else { 
    237249        /* Loop forward until we reach our point in the list. */ 
    238250        while (ptr->next != NULL && ptr->next != client) { 
    239251            ptr = ptr->next; 
    240252        } 
    241253        /* If we reach the end before finding out element, complain. */ 
    242         if (ptr->next == NULL) { 
     254        if (ptr->next == NULL) 
    243255            debug(LOG_ERR, "Node to delete could not be found."); 
    244         /* Free element. */ 
    245         } else { 
     256        else 
    246257            ptr->next = client->next; 
    247             _client_list_free_node(client); 
    248         } 
    249258    } 
    250259} 
     260 
  • src/client_list.h

    diff --git a/src/client_list.h b/src/client_list.h
    index ad62c9d..fd860a8 100644
    a b  
    7676/** @brief Finds a client by its token */ 
    7777t_client *client_list_find_by_token(char *token); 
    7878 
    79 /** @brief Deletes a client from the connections list */ 
     79/** @brief Deletes a client from the connections list and frees its memoery*/ 
    8080void client_list_delete(t_client *client); 
    8181 
     82/** @brief Removes a client from the connections list */ 
     83void client_list_remove(t_client *client); 
     84 
     85/** @brief Free memory associated with a client */ 
     86void client_free_node(t_client *client); 
     87 
    8288#define LOCK_CLIENT_LIST() do { \ 
    8389        debug(LOG_DEBUG, "Locking client list"); \ 
    8490        pthread_mutex_lock(&client_list_mutex); \ 
  • src/firewall.c

    diff --git a/src/firewall.c b/src/firewall.c
    index 9c32f0b..6794998 100644
    a b  
    269269                /* Timing out user */ 
    270270                debug(LOG_INFO, "%s - Inactive for more than %ld seconds, removing client and denying in firewall", 
    271271                        p1->ip, config->checkinterval * config->clienttimeout); 
    272                 fw_deny(p1->ip, p1->mac, p1->fw_connection_state); 
    273                 client_list_delete(p1); 
    274  
    275                 /* Advertise the logout if we have an auth server */ 
    276                 if (config->auth_servers != NULL) { 
    277                                         UNLOCK_CLIENT_LIST(); 
    278                                         auth_server_request(&authresponse, REQUEST_TYPE_LOGOUT, ip, mac, token, 0, 0); 
    279                                         LOCK_CLIENT_LIST(); 
    280                 } 
     272                logout_client(p1); 
    281273            } else { 
    282274                /* 
    283275                 * This handles any change in 
     
    348340    UNLOCK_CLIENT_LIST(); 
    349341} 
    350342 
     343/** 
     344 * @brief Logout a client and report to auth server. 
     345 * 
     346 * This function assumes it is being called with the client lock held! This 
     347 * function remove the client from the client list and free its memory, so 
     348 * client is no langer valid when this method returns. 
     349 * 
     350 * @param client Points to the client to be logged out 
     351 */ 
     352void 
     353logout_client(t_client *client) 
     354{ 
     355        t_authresponse  authresponse; 
     356        const s_config *config = config_get_config(); 
     357        fw_deny(client->ip, client->mac, client->fw_connection_state); 
     358        client_list_remove(client); 
     359 
     360        /* Advertise the logout if we have an auth server */ 
     361        if (config->auth_servers != NULL) { 
     362                UNLOCK_CLIENT_LIST(); 
     363                auth_server_request(&authresponse, REQUEST_TYPE_LOGOUT, 
     364                                client->ip, client->mac, client->token, 
     365                                client->counters.incoming, 
     366                                client->counters.outgoing); 
     367 
     368                if (authresponse.authcode==AUTH_ERROR)  
     369                        debug(LOG_WARNING, "Auth server error when reporting logout"); 
     370                LOCK_CLIENT_LIST(); 
     371        } 
     372 
     373        client_free_node(client); 
     374} 
     375 
     376 
    351377void 
    352378icmp_ping(char *host) 
    353379{ 
  • src/firewall.h

    diff --git a/src/firewall.h b/src/firewall.h
    index 5c59240..03cd128 100644
    a b  
    2727#ifndef _FIREWALL_H_ 
    2828#define _FIREWALL_H_ 
    2929 
     30#include "client_list.h" 
     31 
    3032int icmp_fd; 
    3133 
    3234/** Used by fw_iptables.c */ 
     
    6769/** @brief cheap random */ 
    6870unsigned short rand16(void); 
    6971 
     72/** @brief Logout a client and report to auth server. */ 
     73void logout_client(t_client *client); 
     74 
    7075#endif /* _FIREWALL_H_ */ 
  • src/http.c

    diff --git a/src/http.c b/src/http.c
    index d10c107..391ddd2 100644
    a b  
    296296                        return -1; 
    297297                } 
    298298 
    299                 /* TODO: get current firewall counters, set counters to auth server, 
    300                  * send disconnect to auth server. 
    301                  * 
    302                  * XXX: this should share code with wdctl_reset 
    303                  */ 
    304                 fw_deny(client->ip, client->mac, client->fw_connection_state); 
    305                 client_list_delete(client); 
    306  
     299                /* TODO: get current firewall counters */ 
     300                logout_client(client); 
    307301                UNLOCK_CLIENT_LIST(); 
    308302 
    309303        } else { 
  • src/wdctl_thread.c

    diff --git a/src/wdctl_thread.c b/src/wdctl_thread.c
    index 0cfadbb..c64b668 100644
    a b  
    381381        debug(LOG_DEBUG, "Got node %x.", node); 
    382382         
    383383        /* deny.... */ 
    384         /* TODO: maybe just deleting the connection is not best... But this 
    385          * is a manual command, I don't anticipate it'll be that useful. */ 
    386         fw_deny(node->ip, node->mac, node->fw_connection_state); 
    387         client_list_delete(node); 
     384        logout_client(node); 
    388385         
    389386        UNLOCK_CLIENT_LIST(); 
    390387