Show rate in bits, but allow to show in bytes by -B,--rate-bytes option.

Signed-off-by: Vadim Kochan <[email protected]>
---
 flowtop.8 |  3 +++
 flowtop.c | 29 ++++++++++++++++++++++++-----
 2 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/flowtop.8 b/flowtop.8
index 3f3f0f1..481b3b1 100644
--- a/flowtop.8
+++ b/flowtop.8
@@ -123,6 +123,9 @@ Do not perform hostname lookup
 .SS -G, --no-geoip
 Do not perform country & city lookup by GeoIP
 .PP
+.SS -B, --rate-bytes
+Show rate in bytes instead of bits
+.PP
 .SS -v, --version
 Show version information and exit.
 .PP
diff --git a/flowtop.c b/flowtop.c
index c5e3cc9..9d32da3 100644
--- a/flowtop.c
+++ b/flowtop.c
@@ -104,6 +104,11 @@ struct sysctl_params_ctx {
        int nfct_tstamp;
 };
 
+enum rate_units {
+       RATE_BITS,
+       RATE_BYTES
+};
+
 static volatile bool is_flow_collecting;
 static volatile sig_atomic_t sigint = 0;
 static int what = INCLUDE_IPV4 | INCLUDE_IPV6 | INCLUDE_TCP, show_src = 0;
@@ -113,8 +118,9 @@ static struct sysctl_params_ctx sysctl = { -1, -1 };
 static unsigned int interval = 1;
 static bool resolve_dns = true;
 static bool resolve_geoip = true;
+static enum rate_units rate_type = RATE_BITS;
 
-static const char *short_options = "vhTUsDIS46ut:nG";
+static const char *short_options = "vhTUsDIS46ut:nGB";
 static const struct option long_options[] = {
        {"ipv4",        no_argument,            NULL, '4'},
        {"ipv6",        no_argument,            NULL, '6'},
@@ -128,6 +134,7 @@ static const struct option long_options[] = {
        {"interval",    required_argument,      NULL, 't'},
        {"no-dns",      no_argument,            NULL, 'n'},
        {"no-geoip",    no_argument,            NULL, 'G'},
+       {"rate-bytes",  no_argument,            NULL, 'B'},
        {"version",     no_argument,            NULL, 'v'},
        {"help",        no_argument,            NULL, 'h'},
        {NULL, 0, NULL, 0}
@@ -263,6 +270,7 @@ static void help(void)
             "  -t|--interval <time>   Refresh time in seconds (default 1s)\n"
             "  -n|--no-dns            Do not perform hostname lookup\n"
             "  -G|--no-geoip          Do not perform country & city lookup by 
GeoIP\n"
+            "  -B|--rate-bytes        Show rate in bytes instead of bits\n"
             "  -v|--version           Print version and exit\n"
             "  -h|--help              Print this help and exit\n\n"
             "Examples:\n"
@@ -826,14 +834,22 @@ static char *bandw2str(double bytes, char *buf, size_t 
len)
 
 static char *rate2str(double rate, char *buf, size_t len)
 {
+       char *unit_fmt[2][4] = {
+               { "%.1fGbit/s", "%.1fMbit/s", "%.1fkbit/s", "%gbit/s" },
+               { "%.1fGB/s", "%.1fMB/s", "%.1fkB/s", "%gB/s" }
+       };
+
+       if (rate_type == RATE_BITS)
+               rate *= 8;
+
        if (rate > 1000000000.)
-               snprintf(buf, len, "%.1fGB/s", rate / 1000000000.);
+               snprintf(buf, len, unit_fmt[rate_type][0], rate / 1000000000.);
        else if (rate > 1000000.)
-               snprintf(buf, len, "%.1fMB/s", rate / 1000000.);
+               snprintf(buf, len, unit_fmt[rate_type][1], rate / 1000000.);
        else if (rate > 1000.)
-               snprintf(buf, len, "%.1fkB/s", rate / 1000.);
+               snprintf(buf, len, unit_fmt[rate_type][2], rate / 1000.);
        else
-               snprintf(buf, len, "%gB/s", rate);
+               snprintf(buf, len, unit_fmt[rate_type][3], rate);
 
        return buf;
 }
@@ -1551,6 +1567,9 @@ int main(int argc, char **argv)
                case 'G':
                        resolve_geoip = false;
                        break;
+               case 'B':
+                       rate_type = RATE_BYTES;
+                       break;
                case 'h':
                        help();
                        break;
-- 
2.6.1

-- 
You received this message because you are subscribed to the Google Groups 
"netsniff-ng" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to