Skip to content

Commit

Permalink
Merge pull request #302 from nodogsplash/fix_counter_reset
Browse files Browse the repository at this point in the history
Fix counter reset
  • Loading branch information
mwarning committed Sep 26, 2018
2 parents 0fff1e2 + 07cbfdd commit 246d453
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/auth.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ static int auth_change_state(t_client *client, const unsigned int new_state, con
if (new_state == FW_MARK_PREAUTHENTICATED) {
iptables_fw_deauthenticate(client);
binauth_action(client, reason);
client_renew_token(client);
client_reset(client);
} else if (new_state == FW_MARK_BLOCKED) {
return -1;
} else if (new_state == FW_MARK_TRUSTED) {
Expand Down
33 changes: 17 additions & 16 deletions src/client_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@
static int client_count = 0;
static int client_id = 1;

/** Time last client added */
static unsigned long int last_client_time = 0;

/** Global mutex to protect access to the client list */
pthread_mutex_t client_list_mutex = PTHREAD_MUTEX_INITIALIZER;

Expand Down Expand Up @@ -122,7 +119,9 @@ _client_list_append(const char mac[], const char ip[])

client->mac = safe_strdup(mac);
client->ip = safe_strdup(ip);
client_renew_token(client);

// Reset volatile fields
client_reset(client);

// Blocked or Trusted client do not trigger the splash page.
// They must access the splash or status page manually.
Expand All @@ -134,15 +133,6 @@ _client_list_append(const char mac[], const char ip[])
client->fw_connection_state = FW_MARK_PREAUTHENTICATED;
}

client->counters.incoming = 0;
client->counters.incoming_history = 0;
client->counters.outgoing = 0;
client->counters.outgoing_history = 0;
last_client_time = time(NULL);
client->counters.last_updated = last_client_time;
/* Session has not started and not ended yet */
client->session_start = 0;
client->session_end = 0;
client->id = client_id;

debug(LOG_NOTICE, "Adding %s %s token %s to client list",
Expand All @@ -161,11 +151,22 @@ _client_list_append(const char mac[], const char ip[])
}

/** @internal
* Set a new client token.
* We just generate a random string of 8 hex digits.
* Reset volatile fields
*/
void client_renew_token(t_client *client)
void client_reset(t_client *client)
{
// Reset traffic counters
client->counters.incoming = 0;
client->counters.incoming_history = 0;
client->counters.outgoing = 0;
client->counters.outgoing_history = 0;
client->counters.last_updated = time(NULL);

// Reset seesion time
client->session_start = 0;
client->session_end = 0;

// Reset token
free(client->token);
safe_asprintf(&client->token, "%04hx%04hx", rand16(), rand16());
}
Expand Down
12 changes: 6 additions & 6 deletions src/client_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
/** Counters struct for a client's bandwidth usage (in bytes)
*/
typedef struct _t_counters {
unsigned long long incoming; /**< @brief Incoming data total*/
unsigned long long outgoing; /**< @brief Outgoing data total*/
unsigned long long incoming_history; /**< @brief Incoming data before nodogsplash restarted*/
unsigned long long outgoing_history; /**< @brief Outgoing data before nodogsplash restarted*/
unsigned long long incoming; /**< @brief Incoming data total */
unsigned long long outgoing; /**< @brief Outgoing data total */
unsigned long long incoming_history; /**< @brief Incoming data before nodogsplash restarted, always 0 atm. */
unsigned long long outgoing_history; /**< @brief Outgoing data before nodogsplash restarted, always 0 atm. */
time_t last_updated; /**< @brief Last update of the counters */
} t_counters;

Expand Down Expand Up @@ -84,8 +84,8 @@ t_client *client_list_find_by_mac(const char mac[]); /* needed by ndsctl_thread.
/** @brief Finds a client by its token */
t_client *client_list_find_by_token(const char token[]);

/** @brief Renew token of a client */
void client_renew_token(t_client *client);
/** @brief Reset volatile client fields */
void client_reset(t_client *client);

/** @brief Deletes a client from the client list */
void client_list_delete(t_client *client);
Expand Down
2 changes: 1 addition & 1 deletion src/fw_iptables.c
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,7 @@ iptables_fw_counters_update(void)
config = config_get_config();
af = config->ip6 ? AF_INET6 : AF_INET;

/* Look for outgoing traffic */
/* Look for outgoing traffic of authenticated clients. */
safe_asprintf(&script, "%s %s", "iptables", "-v -n -x -t mangle -L " CHAIN_OUTGOING);
output = popen(script, "r");
free(script);
Expand Down
12 changes: 10 additions & 2 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,11 @@ ndsctl_clients(FILE *fp)
fprintf(fp, "ip=%s\nmac=%s\n", client->ip, client->mac);
fprintf(fp, "added=%lld\n", (long long) client->session_start);
fprintf(fp, "active=%lld\n", (long long) client->counters.last_updated);
fprintf(fp, "duration=%lu\n", now - client->session_start);
if (client->session_start) {
fprintf(fp, "duration=%lu\n", now - client->session_start);
} else {
fprintf(fp, "duration=%lu\n", 0ul);
}
fprintf(fp, "token=%s\n", client->token ? client->token : "none");
fprintf(fp, "state=%s\n", fw_connection_state_as_string(client->fw_connection_state));

Expand Down Expand Up @@ -649,7 +653,11 @@ ndsctl_json_client(FILE *fp, const t_client *client, time_t now)
fprintf(fp, "\"mac\":\"%s\",\n", client->mac);
fprintf(fp, "\"added\":%lld,\n", (long long) client->session_start);
fprintf(fp, "\"active\":%lld,\n", (long long) client->counters.last_updated);
fprintf(fp, "\"duration\":%lu,\n", now - client->session_start);
if (client->session_start) {
fprintf(fp, "\"duration\":%lu,\n", now - client->session_start);
} else {
fprintf(fp, "\"duration\":%lu,\n", 0ul);
}
fprintf(fp, "\"token\":\"%s\",\n", client->token ? client->token : "none");
fprintf(fp, "\"state\":\"%s\",\n", fw_connection_state_as_string(client->fw_connection_state));

Expand Down

0 comments on commit 246d453

Please sign in to comment.