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
|
|
| 199 | 199 | * @param client Points to the client to be freed |
| 200 | 200 | */ |
| 201 | 201 | void |
| 202 | | _client_list_free_node(t_client * client) |
| | 202 | client_free_node(t_client * client) |
| 203 | 203 | { |
| 204 | 204 | |
| 205 | 205 | if (client->mac != NULL) |
| … |
… |
|
| 224 | 224 | void |
| 225 | 225 | client_list_delete(t_client * client) |
| 226 | 226 | { |
| | 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 | */ |
| | 237 | void |
| | 238 | client_list_remove(t_client * client) |
| | 239 | { |
| 227 | 240 | t_client *ptr; |
| 228 | 241 | |
| 229 | 242 | ptr = firstclient; |
| … |
… |
|
| 232 | 245 | debug(LOG_ERR, "Node list empty!"); |
| 233 | 246 | } else if (ptr == client) { |
| 234 | 247 | firstclient = ptr->next; |
| 235 | | _client_list_free_node(client); |
| 236 | 248 | } else { |
| 237 | 249 | /* Loop forward until we reach our point in the list. */ |
| 238 | 250 | while (ptr->next != NULL && ptr->next != client) { |
| 239 | 251 | ptr = ptr->next; |
| 240 | 252 | } |
| 241 | 253 | /* If we reach the end before finding out element, complain. */ |
| 242 | | if (ptr->next == NULL) { |
| | 254 | if (ptr->next == NULL) |
| 243 | 255 | debug(LOG_ERR, "Node to delete could not be found."); |
| 244 | | /* Free element. */ |
| 245 | | } else { |
| | 256 | else |
| 246 | 257 | ptr->next = client->next; |
| 247 | | _client_list_free_node(client); |
| 248 | | } |
| 249 | 258 | } |
| 250 | 259 | } |
| | 260 | |
diff --git a/src/client_list.h b/src/client_list.h
index ad62c9d..fd860a8 100644
|
a
|
b
|
|
| 76 | 76 | /** @brief Finds a client by its token */ |
| 77 | 77 | t_client *client_list_find_by_token(char *token); |
| 78 | 78 | |
| 79 | | /** @brief Deletes a client from the connections list */ |
| | 79 | /** @brief Deletes a client from the connections list and frees its memoery*/ |
| 80 | 80 | void client_list_delete(t_client *client); |
| 81 | 81 | |
| | 82 | /** @brief Removes a client from the connections list */ |
| | 83 | void client_list_remove(t_client *client); |
| | 84 | |
| | 85 | /** @brief Free memory associated with a client */ |
| | 86 | void client_free_node(t_client *client); |
| | 87 | |
| 82 | 88 | #define LOCK_CLIENT_LIST() do { \ |
| 83 | 89 | debug(LOG_DEBUG, "Locking client list"); \ |
| 84 | 90 | pthread_mutex_lock(&client_list_mutex); \ |
diff --git a/src/firewall.c b/src/firewall.c
index 9c32f0b..6794998 100644
|
a
|
b
|
|
| 269 | 269 | /* Timing out user */ |
| 270 | 270 | debug(LOG_INFO, "%s - Inactive for more than %ld seconds, removing client and denying in firewall", |
| 271 | 271 | 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); |
| 281 | 273 | } else { |
| 282 | 274 | /* |
| 283 | 275 | * This handles any change in |
| … |
… |
|
| 348 | 340 | UNLOCK_CLIENT_LIST(); |
| 349 | 341 | } |
| 350 | 342 | |
| | 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 | */ |
| | 352 | void |
| | 353 | logout_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 | |
| 351 | 377 | void |
| 352 | 378 | icmp_ping(char *host) |
| 353 | 379 | { |
diff --git a/src/firewall.h b/src/firewall.h
index 5c59240..03cd128 100644
|
a
|
b
|
|
| 27 | 27 | #ifndef _FIREWALL_H_ |
| 28 | 28 | #define _FIREWALL_H_ |
| 29 | 29 | |
| | 30 | #include "client_list.h" |
| | 31 | |
| 30 | 32 | int icmp_fd; |
| 31 | 33 | |
| 32 | 34 | /** Used by fw_iptables.c */ |
| … |
… |
|
| 67 | 69 | /** @brief cheap random */ |
| 68 | 70 | unsigned short rand16(void); |
| 69 | 71 | |
| | 72 | /** @brief Logout a client and report to auth server. */ |
| | 73 | void logout_client(t_client *client); |
| | 74 | |
| 70 | 75 | #endif /* _FIREWALL_H_ */ |
diff --git a/src/http.c b/src/http.c
index d10c107..391ddd2 100644
|
a
|
b
|
|
| 296 | 296 | return -1; |
| 297 | 297 | } |
| 298 | 298 | |
| 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); |
| 307 | 301 | UNLOCK_CLIENT_LIST(); |
| 308 | 302 | |
| 309 | 303 | } else { |
diff --git a/src/wdctl_thread.c b/src/wdctl_thread.c
index 0cfadbb..c64b668 100644
|
a
|
b
|
|
| 381 | 381 | debug(LOG_DEBUG, "Got node %x.", node); |
| 382 | 382 | |
| 383 | 383 | /* 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); |
| 388 | 385 | |
| 389 | 386 | UNLOCK_CLIENT_LIST(); |
| 390 | 387 | |