From e72414459a2d79fd3a5e9603da77dadd4d8e1300 Mon Sep 17 00:00:00 2001 From: Nikos Mavrogiannopoulos Date: Sun, 2 Mar 2014 13:24:44 +0100 Subject: [PATCH] provide the bandwidth limit through d-bus --- src/main-ctl-handler.c | 20 +++++++++++++++++++- src/occtl-nl.c | 2 +- src/occtl.c | 37 +++++++++++++++++++++++++++++++++++-- src/occtl.h | 3 +++ 4 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src/main-ctl-handler.c b/src/main-ctl-handler.c index 4e2c066d..9040aeec 100644 --- a/src/main-ctl-handler.c +++ b/src/main-ctl-handler.c @@ -72,7 +72,7 @@ typedef struct { {name, sizeof(name)-1, iface, sizeof(iface)-1, desc, sizeof(desc)-1, func} #define LIST_USERS_SIG "(ussssssssusssss)" -#define LIST_SINGLE_USER_SIG "(ussssssssusssssasasasas)" +#define LIST_SINGLE_USER_SIG "(ussssssssusssssuuasasasas)" #define DESC_LIST \ " \n" \ @@ -563,6 +563,24 @@ static int append_user_info(main_server_st * s, DBusMessageIter * subs, } if (single > 0) { + if (ctmp->config.rx_per_sec > 0) + tmp = ctmp->config.rx_per_sec; + else + tmp = s->config->rx_per_sec; + tmp *= 1000; + if (dbus_message_iter_append_basic(subs, DBUS_TYPE_UINT32, &tmp) == 0) { + return -1; + } + + if (ctmp->config.tx_per_sec > 0) + tmp = ctmp->config.tx_per_sec; + else + tmp = s->config->tx_per_sec; + tmp *= 1000; + if (dbus_message_iter_append_basic(subs, DBUS_TYPE_UINT32, &tmp) == 0) { + return -1; + } + if (ctmp->config.dns_size > 0) { list = ctmp->config.dns; list_size = ctmp->config.dns_size; diff --git a/src/occtl-nl.c b/src/occtl-nl.c index 6e172ac3..99935b9f 100644 --- a/src/occtl-nl.c +++ b/src/occtl-nl.c @@ -68,7 +68,7 @@ error: return -1; } -static void +void bytes2human(unsigned long bytes, char* output, unsigned output_size, const char* suffix) { double data; diff --git a/src/occtl.c b/src/occtl.c index 563d2db6..ee753a1c 100644 --- a/src/occtl.c +++ b/src/occtl.c @@ -768,7 +768,7 @@ int print_list_entries(FILE* out, const char* name, DBusMessageIter * subs) int common_info_cmd(DBusMessageIter * args) { DBusMessageIter suba, subs; - dbus_uint32_t id = 0; + dbus_uint32_t id = 0, rx = 0, tx = 0; char *username = ""; dbus_uint32_t since = 0; char *groupname = "", *ip = ""; @@ -908,6 +908,20 @@ int common_info_cmd(DBusMessageIter * args) goto error_parse; dbus_message_iter_get_basic(&subs, &dtls_ciphersuite); + if (!dbus_message_iter_next(&subs)) + goto error_recv; + + if (dbus_message_iter_get_arg_type(&subs) != DBUS_TYPE_UINT32) + goto error_parse; + dbus_message_iter_get_basic(&subs, &rx); + + if (!dbus_message_iter_next(&subs)) + goto error_recv; + + if (dbus_message_iter_get_arg_type(&subs) != DBUS_TYPE_UINT32) + goto error_parse; + dbus_message_iter_get_basic(&subs, &tx); + if (!dbus_message_iter_next(&subs)) goto error_recv; @@ -940,6 +954,25 @@ int common_info_cmd(DBusMessageIter * args) else fprintf(out, "\n"); + if (rx > 0 || tx > 0) { + /* print limits */ + char buf[32]; + + if (rx > 0 && tx > 0) { + bytes2human(rx, buf, sizeof(buf), NULL); + fprintf(out, "\tLimit RX: %s/sec ", buf); + + bytes2human(tx, buf, sizeof(buf), NULL); + fprintf(out, "TX: %s/sec\n", buf); + } else if (tx > 0) { + bytes2human(tx, buf, sizeof(buf), NULL); + fprintf(out, "\tLimit TX: %s/sec\n", buf); + } else if (rx > 0) { + bytes2human(rx, buf, sizeof(buf), NULL); + fprintf(out, "\tLimit RX: %s/sec\n", buf); + } + } + print_iface_stats(device, since, out); if (hostname != NULL && hostname[0] != 0) @@ -979,10 +1012,10 @@ int common_info_cmd(DBusMessageIter * args) if (print_list_entries(out, "\tiRoutes:", &subs) < 0) goto error_parse; + at_least_one = 1; if (!dbus_message_iter_next(&suba)) break; - } ret = 0; diff --git a/src/occtl.h b/src/occtl.h index f832dc81..6248e687 100644 --- a/src/occtl.h +++ b/src/occtl.h @@ -9,6 +9,9 @@ void pager_stop(FILE* fp); void print_time_ival7(time_t t, FILE * fout); void print_iface_stats(const char *iface, time_t since, FILE * out); +void +bytes2human(unsigned long bytes, char* output, unsigned output_size, const char* suffix); + char* search_for_id(unsigned idx, const char* match, int match_size); char* search_for_user(unsigned idx, const char* match, int match_size); void entries_add(const char* user, unsigned user_size, unsigned id);