Add -b,--bits command line option for show rate in bits.

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 d85284a..292f6e0 100644
--- a/flowtop.8
+++ b/flowtop.8
@@ -125,6 +125,9 @@ separated by a newline.
 .SS -t <time>, --interval <time>
 Flow info refresh interval in seconds, default is 1s.
 .PP
+.SS -b, --bits
+Show rate in bits instead of bytes
+.PP
 .SS -v, --version
 Show version information and exit.
 .PP
diff --git a/flowtop.c b/flowtop.c
index 7573780..eddf233 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;
@@ -114,8 +119,9 @@ static unsigned int interval = 1;
 static bool show_src = false;
 static bool resolve_dns = true;
 static bool resolve_geoip = true;
+static enum rate_units rate_type = RATE_BYTES;
 
-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'},
@@ -129,6 +135,7 @@ static const struct option long_options[] = {
        {"show-src",    no_argument,            NULL, 's'},
        {"update",      no_argument,            NULL, 'u'},
        {"interval",    required_argument,      NULL, 't'},
+       {"bits",        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)
             "  -s|--show-src          Also show source, not only dest\n"
             "  -u|--update            Update GeoIP databases\n"
             "  -t|--interval <time>   Refresh time in seconds (default 1s)\n"
+            "  -b|--bits              Show rate in bits instead of bytes\n"
             "  -v|--version           Print version and exit\n"
             "  -h|--help              Print this help and exit\n\n"
             "Examples:\n"
@@ -821,14 +829,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;
 }
@@ -1546,6 +1562,9 @@ int main(int argc, char **argv)
                case 'G':
                        resolve_geoip = false;
                        break;
+               case 'b':
+                       rate_type = RATE_BITS;
+                       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