Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Patch] Bandwidth Utilization Patch. #202

Open
guyharris opened this issue Apr 16, 2013 · 5 comments
Open

[Patch] Bandwidth Utilization Patch. #202

guyharris opened this issue Apr 16, 2013 · 5 comments

Comments

@guyharris
Copy link
Member

Converted from SourceForge issue 1037376, submitted by ellzey

A very simple patch that add's a -M flag to see the
current bandwidth in bps/Kbps/Mbps.

@guyharris
Copy link
Member Author

Submitted by guy_harris

Logged In: YES
user_id=541179

"-M", in the current top-of-tree CVS version of tcpdump, is used to
specify a shared secret for validating digests for TCP segments with the
TCP-MD5 option (RFC 2385). You'll have to pick a different flag.

@infrastation
Copy link
Member

Contents of the original patch is below:

*** tcpdump-3.8.3/tcpdump.c 2004-03-17 14:47:48.000000000 -0500
--- tcpdump-3.8.3-mark/tcpdump.c    2004-09-29 19:34:18.000000000 -0400
***************
*** 74,79 ****
--- 74,80 ----
  int fflag;            /* don't translate "foreign" IP address */
  int Lflag;            /* list available data link types and exit */
  int nflag;            /* leave addresses as numbers */
+ int Mflag;            /* see bandwidth stats */
  int Nflag;            /* remove domains from printed host names */
  int Oflag = 1;            /* run filter code optimizer */
  int pflag;            /* don't go promiscuous */
***************
*** 104,109 ****
--- 105,111 ----

  int32_t thiszone;     /* seconds offset from gmt to local time */

+ time_t bw_gtime;      /* global time for bandwidth stats */
  /* Forwards */
  static RETSIGTYPE cleanup(int);
  static void usage(void) __attribute__((noreturn));
***************
*** 119,124 ****
--- 121,127 ----

  static void info(int);
  static u_int packets_captured;
+ static u_int bytes_captured;

  /* Length of saved portion of packet. */
  int snaplen = DEFAULT_SNAPLEN;
***************
*** 346,352 ****

    opterr = 0;
    while (
!       (op = getopt(argc, argv, "aA" B_FLAG "c:C:d" D_FLAG "eE:fF:i:lLm:nNOpqr:Rs:StT:u" U_FLAG "vw:xXy:Y")) != -1)
        switch (op) {

        case 'a':
--- 349,355 ----

    opterr = 0;
    while (
!       (op = getopt(argc, argv, "aA" B_FLAG "c:C:d" D_FLAG "eE:fF:i:lLMm:nNOpqr:Rs:StT:u" U_FLAG "vw:xXy:Y")) != -1)
        switch (op) {

        case 'a':
***************
*** 499,504 ****
--- 502,511 ----
  #endif
            break;

+       case 'M':
+           Mflag = 1;
+           time(&bw_gtime);
+           break;
        case 'O':
            Oflag = 0;
            break;
***************
*** 1000,1005 ****
--- 1007,1032 ----
    ++packets_captured;

    ++infodelay;
+ 
+   if(Mflag)
+   {
+       time_t bw_curtime;
+ 
+       bytes_captured += h->len;
+ 
+       time(&bw_curtime);
+       if((bw_curtime-bw_gtime) >= 1)
+       {
+           int Kbps = bytes_captured*8/1024;
+           int Mbps = Kbps/1048576;
+           ts_print(&h->ts);
+           printf("Bandwidth: %10dBytes/s | %8dKbps | %6dMbps\n",bytes_captured,Kbps,Mbps);
+               time(&bw_gtime);
+               packets_captured=0;
+       }
+       return;
+   }
+ 
    ts_print(&h->ts);

    print_info = (struct print_info *)user;

@infrastation
Copy link
Member

I had briefly looked into this and could note the following:

  1. Since getopt_long() is now guaranteed to be available, it is possible to use just the long option --bwonly[=seconds] with the default period of 1 second.
  2. The man page should explain the new option.
  3. The time calculation should remain correct when the measured period is much shorter or much longer than 1 second, this involves respective time units and makes the option depend on compile-time checks for clock_gettime() and CLOCK_MONOTONIC.

@mcr
Copy link
Member

mcr commented Feb 2, 2015

So I understand that this calculates bandwidth based upon pcap time stamps and packet sizes. That's kinda interesting.

@infrastation
Copy link
Member

This feature is very close to what iftop does.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants