Skip to content

Commit b63ebe4

Browse files
committed
Merge pull request #211 from t123yh/DeltaTraffic
Add delta traffic stats
2 parents 1188232 + 53b4bb4 commit b63ebe4

File tree

11 files changed

+59
-13
lines changed

11 files changed

+59
-13
lines changed

src/auth.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ logout_client(t_client * client)
104104
UNLOCK_CLIENT_LIST();
105105
auth_server_request(&authresponse, REQUEST_TYPE_LOGOUT,
106106
client->ip, client->mac, client->token,
107-
client->counters.incoming, client->counters.outgoing);
107+
client->counters.incoming, client->counters.outgoing, client->counters.incoming_delta, client->counters.outgoing_delta);
108108

109109
if (authresponse.authcode == AUTH_ERROR)
110110
debug(LOG_WARNING, "Auth server error when reporting logout");
@@ -154,7 +154,7 @@ authenticate_client(request * r)
154154
* take multiple seconds to do and the gateway would effectively be frozen if we
155155
* kept the lock.
156156
*/
157-
auth_server_request(&auth_response, REQUEST_TYPE_LOGIN, client->ip, client->mac, token, 0, 0);
157+
auth_server_request(&auth_response, REQUEST_TYPE_LOGIN, client->ip, client->mac, token, 0, 0, 0, 0);
158158

159159
LOCK_CLIENT_LIST();
160160

src/centralserver.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@
6565
*/
6666
t_authcode
6767
auth_server_request(t_authresponse * authresponse, const char *request_type, const char *ip, const char *mac,
68-
const char *token, unsigned long long int incoming, unsigned long long int outgoing)
68+
const char *token, unsigned long long int incoming, unsigned long long int outgoing, unsigned long long int incoming_delta, unsigned long long int outgoing_delta)
6969
{
70+
s_config *config = config_get_config();
7071
int sockfd;
7172
char buf[MAX_BUF];
7273
char *tmp;
@@ -85,7 +86,23 @@ auth_server_request(t_authresponse * authresponse, const char *request_type, con
8586
*/
8687
memset(buf, 0, sizeof(buf));
8788
safe_token = httpdUrlEncode(token);
88-
snprintf(buf, (sizeof(buf) - 1),
89+
if(config -> deltatraffic) {
90+
snprintf(buf, (sizeof(buf) - 1),
91+
"GET %s%sstage=%s&ip=%s&mac=%s&token=%s&incoming=%llu&outgoing=%llu&incomingdelta=%llu&outgoingdelta=%llu&gw_id=%s HTTP/1.0\r\n"
92+
"User-Agent: WiFiDog %s\r\n"
93+
"Host: %s\r\n"
94+
"\r\n",
95+
auth_server->authserv_path,
96+
auth_server->authserv_auth_script_path_fragment,
97+
request_type,
98+
ip, mac, safe_token,
99+
incoming,
100+
outgoing,
101+
incoming_delta,
102+
outgoing_delta,
103+
config->gw_id, VERSION, auth_server->authserv_hostname);
104+
} else {
105+
snprintf(buf, (sizeof(buf) - 1),
89106
"GET %s%sstage=%s&ip=%s&mac=%s&token=%s&incoming=%llu&outgoing=%llu&gw_id=%s HTTP/1.0\r\n"
90107
"User-Agent: WiFiDog %s\r\n"
91108
"Host: %s\r\n"
@@ -94,8 +111,8 @@ auth_server_request(t_authresponse * authresponse, const char *request_type, con
94111
auth_server->authserv_auth_script_path_fragment,
95112
request_type,
96113
ip,
97-
mac, safe_token, incoming, outgoing, config_get_config()->gw_id, VERSION, auth_server->authserv_hostname);
98-
114+
mac, safe_token, incoming, outgoing, config->gw_id, VERSION, auth_server->authserv_hostname);
115+
}
99116
free(safe_token);
100117

101118
char *res;

src/centralserver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ t_authcode auth_server_request(t_authresponse * authresponse,
5151
const char *request_type,
5252
const char *ip,
5353
const char *mac,
54-
const char *token, unsigned long long int incoming, unsigned long long int outgoing);
54+
const char *token, unsigned long long int incoming, unsigned long long int outgoing, unsigned long long int incoming_delta, unsigned long long int outgoing_delta);
5555

5656
/** @brief Tries really hard to connect to an auth server. Returns a connected file descriptor or -1 on error */
5757
int connect_auth_server(void);

src/client_list.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ client_list_add(const char *ip, const char *mac, const char *token)
122122
curclient->ip = safe_strdup(ip);
123123
curclient->mac = safe_strdup(mac);
124124
curclient->token = safe_strdup(token);
125-
curclient->counters.incoming = curclient->counters.incoming_history = curclient->counters.outgoing =
125+
curclient->counters.incoming_delta = curclient->counters.outgoing_delta =
126+
curclient->counters.incoming = curclient->counters.incoming_history = curclient->counters.outgoing =
126127
curclient->counters.outgoing_history = 0;
127128
curclient->counters.last_updated = time(NULL);
128129

@@ -190,8 +191,10 @@ client_dup(const t_client * src)
190191
new->token = safe_strdup(src->token);
191192
new->counters.incoming = src->counters.incoming;
192193
new->counters.incoming_history = src->counters.incoming_history;
194+
new->counters.incoming_delta = src->counters.incoming_delta;
193195
new->counters.outgoing = src->counters.outgoing;
194196
new->counters.outgoing_history = src->counters.outgoing_history;
197+
new->counters.outgoing_delta = src->counters.outgoing_delta;
195198
new->counters.last_updated = src->counters.last_updated;
196199
new->next = NULL;
197200

src/client_list.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ typedef struct _t_counters {
3838
unsigned long long outgoing; /**< @brief Outgoing data total*/
3939
unsigned long long incoming_history; /**< @brief Incoming data before wifidog restarted*/
4040
unsigned long long outgoing_history; /**< @brief Outgoing data before wifidog restarted*/
41+
/* Delta traffic stats by t123yh */
42+
unsigned long long incoming_delta; /**< @brief Incoming data after last report*/
43+
unsigned long long outgoing_delta; /**< @brief Outgoing data after last report*/
4144
time_t last_updated; /**< @brief Last update of the counters */
4245
} t_counters;
4346

src/conf.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ typedef enum {
7373
oGatewayInterface,
7474
oGatewayAddress,
7575
oGatewayPort,
76+
oDeltaTraffic,
7677
oAuthServer,
7778
oAuthServHostname,
7879
oAuthServSSLAvailable,
@@ -111,6 +112,7 @@ static const struct {
111112
OpCodes opcode;
112113
} keywords[] = {
113114
{
115+
"deltatraffic", oDeltaTraffic}, {
114116
"daemon", oDaemon}, {
115117
"debuglevel", oDebugLevel}, {
116118
"externalinterface", oExternalInterface}, {
@@ -199,6 +201,7 @@ config_init(void)
199201
config.proxy_port = 0;
200202
config.ssl_certs = safe_strdup(DEFAULT_AUTHSERVSSLCERTPATH);
201203
config.ssl_verify = DEFAULT_AUTHSERVSSLPEERVER;
204+
config.deltatraffic = DEFAULT_DELTATRAFFIC;
202205
config.ssl_cipher_list = NULL;
203206
config.arp_table_path = safe_strdup(DEFAULT_ARPTABLE);
204207

@@ -691,6 +694,9 @@ config_read(const char *filename)
691694
opcode = config_parse_token(s, filename, linenum);
692695

693696
switch (opcode) {
697+
case oDeltaTraffic:
698+
config.deltatraffic = parse_boolean_value(p1);
699+
break;
694700
case oDaemon:
695701
if (config.daemon == -1 && ((value = parse_boolean_value(p1)) != -1)) {
696702
config.daemon = value;

src/conf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#define DEFAULT_AUTHSERVSSLCERTPATH "/etc/ssl/certs/"
6666
/** Note that DEFAULT_AUTHSERVSSLNOPEERVER must be 0 or 1, even if the config file syntax is yes or no */
6767
#define DEFAULT_AUTHSERVSSLPEERVER 1 /* 0 means: Enable peer verification */
68+
#define DEFAULT_DELTATRAFFIC 0 /* 0 means: Enable peer verification */
6869
#define DEFAULT_ARPTABLE "/proc/net/arp"
6970
/*@}*/
7071

@@ -159,6 +160,7 @@ typedef struct {
159160
char *htmlmsgfile; /**< @brief name of the HTML file used for messages */
160161
char *wdctl_sock; /**< @brief wdctl path to socket */
161162
char *internal_sock; /**< @brief internal path to socket */
163+
int deltatraffic; /**< @brief reset each user's traffic (Outgoing and Incoming) value after each Auth operation. */
162164
int daemon; /**< @brief if daemon > 0, use daemon mode */
163165
char *external_interface; /**< @brief External network interface name for
164166
firewall rules */

src/firewall.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ fw_sync_with_authserver(void)
286286
/* Update the counters on the remote server only if we have an auth server */
287287
if (config->auth_servers != NULL) {
288288
auth_server_request(&authresponse, REQUEST_TYPE_COUNTERS, p1->ip, p1->mac, p1->token, p1->counters.incoming,
289-
p1->counters.outgoing);
289+
p1->counters.outgoing, p1->counters.incoming_delta, p1->counters.outgoing_delta);
290290
}
291291

292292
time_t current_time = time(NULL);
@@ -347,7 +347,10 @@ fw_sync_with_authserver(void)
347347
//fw_deny(tmp->ip, tmp->mac, tmp->fw_connection_state); /* XXX this was possibly to avoid dupes. */
348348

349349
if (tmp->fw_connection_state != FW_MARK_PROBATION) {
350-
tmp->counters.incoming = tmp->counters.outgoing = 0;
350+
tmp->counters.incoming_delta =
351+
tmp->counters.outgoing_delta =
352+
tmp->counters.incoming =
353+
tmp->counters.outgoing = 0;
351354
} else {
352355
//We don't want to clear counters if the user was in validation, it probably already transmitted data..
353356
debug(LOG_INFO,

src/fw_iptables.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -660,10 +660,11 @@ iptables_fw_counters_update(void)
660660
LOCK_CLIENT_LIST();
661661
if ((p1 = client_list_find_by_ip(ip))) {
662662
if ((p1->counters.outgoing - p1->counters.outgoing_history) < counter) {
663+
p1->counters.outgoing_delta = p1->counters.outgoing_history + counter - p1->counters.outgoing;
663664
p1->counters.outgoing = p1->counters.outgoing_history + counter;
664665
p1->counters.last_updated = time(NULL);
665-
debug(LOG_DEBUG, "%s - Updated counter.outgoing to %llu bytes. Updated last_updated to %d", ip,
666-
counter, p1->counters.last_updated);
666+
debug(LOG_DEBUG, "%s - Outgoing traffic %llu bytes, updated counter.outgoing to %llu bytes. Updated last_updated to %d", ip,
667+
counter, p1->counters.outgoing, p1->counters.last_updated);
667668
}
668669
} else {
669670
debug(LOG_ERR,
@@ -704,8 +705,9 @@ iptables_fw_counters_update(void)
704705
LOCK_CLIENT_LIST();
705706
if ((p1 = client_list_find_by_ip(ip))) {
706707
if ((p1->counters.incoming - p1->counters.incoming_history) < counter) {
708+
p1->counters.incoming_delta = p1->counters.incoming_history + counter - p1->counters.incoming;
707709
p1->counters.incoming = p1->counters.incoming_history + counter;
708-
debug(LOG_DEBUG, "%s - Updated counter.incoming to %llu bytes", ip, counter);
710+
debug(LOG_DEBUG, "%s - Incoming traffic %llu bytes, Updated counter.incoming to %llu bytes", ip, counter, p1->counters.incoming);
709711
}
710712
} else {
711713
debug(LOG_ERR,

src/gateway.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,11 @@ get_clients_from_parent(void)
197197
} else if (strcmp(key, "counters_incoming") == 0) {
198198
client->counters.incoming_history = (unsigned long long)atoll(value);
199199
client->counters.incoming = client->counters.incoming_history;
200+
client->counters.incoming_delta = 0;
200201
} else if (strcmp(key, "counters_outgoing") == 0) {
201202
client->counters.outgoing_history = (unsigned long long)atoll(value);
202203
client->counters.outgoing = client->counters.outgoing_history;
204+
client->counters.outgoing_delta = 0;
203205
} else if (strcmp(key, "counters_last_updated") == 0) {
204206
client->counters.last_updated = atol(value);
205207
} else {

0 commit comments

Comments
 (0)