"Fossies" - the Fresh Open Source Software Archive

Member "tcpflow-1.6.1/src/netviz/address_histogram.cpp" (19 Feb 2021, 2365 Bytes) of package /linux/misc/tcpflow-1.6.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "address_histogram.cpp" see the Fossies "Dox" file reference documentation.

    1 /**
    2  * address_histogram.cpp: 
    3  *
    4  * This source file is public domain, as it is not based on the original tcpflow.
    5  *
    6  * Author: Michael Shick <mike@shick.in>
    7  *
    8  */
    9 
   10 #include "config.h"
   11 
   12 #ifdef HAVE_LIBCAIRO
   13 #include "tcpflow.h"
   14 #include "tcpip.h"
   15 
   16 #include <math.h>
   17 #include <iomanip>
   18 #include <algorithm>
   19 
   20 #include "address_histogram.h"
   21 
   22 using namespace std;
   23 
   24 address_histogram::address_histogram(const iptree &tree) :
   25     buckets(), datagrams_ingested(0)
   26 {
   27     // convert iptree to suitable vector for count histogram
   28     iptree::histogram_t addresses;
   29 
   30     tree.get_histogram(addresses);
   31 
   32     if(addresses.size() <= bucket_count) {
   33         sort(addresses.begin(), addresses.end(), iptree_node_comparator());
   34     }
   35     else {
   36         partial_sort(addresses.begin(), addresses.begin() + bucket_count,
   37                 addresses.end(), iptree_node_comparator());
   38     }
   39     buckets.clear();
   40 
   41     vector<iptree::addr_elem>::const_iterator it = addresses.begin();
   42     for(size_t ii = 0; ii < bucket_count && it != addresses.end(); ii++, it++) {
   43         buckets.push_back(*it);
   44     }
   45 
   46     datagrams_ingested = tree.sum();
   47 }
   48 
   49 const size_t address_histogram::bucket_count = 10;
   50 
   51 const iptree::addr_elem &address_histogram::at(size_t index) const
   52 {
   53     return buckets.at(index);
   54 }
   55 
   56 size_t address_histogram::size() const
   57 {
   58     return buckets.size();
   59 }
   60 
   61 uint64_t address_histogram::ingest_count() const
   62 {
   63     return datagrams_ingested;
   64 }
   65 
   66 address_histogram::ipt_addrs::const_iterator address_histogram::begin() const
   67 {
   68     return buckets.begin();
   69 }
   70 address_histogram::ipt_addrs::const_iterator address_histogram::end() const
   71 {
   72     return buckets.end();
   73 }
   74 address_histogram::ipt_addrs::const_reverse_iterator address_histogram::rbegin() const
   75 {
   76     return buckets.rbegin();
   77 }
   78 address_histogram::ipt_addrs::const_reverse_iterator address_histogram::rend() const
   79 {
   80     return buckets.rend();
   81 }
   82 
   83 bool address_histogram::iptree_node_comparator::operator()(const iptree::addr_elem &a,
   84         const iptree::addr_elem &b)
   85 {
   86     if(a.count > b.count) {
   87         return true;
   88     }
   89     else if(a.count < b.count) {
   90         return false;
   91     }
   92     for(size_t ii = 0; ii < sizeof(a.addr); ii++) {
   93         if(a.addr[ii] > b.addr[ii]) {
   94             return true;
   95         }
   96         else if(a.addr[ii] < b.addr[ii]) {
   97             return false;
   98         }
   99     }
  100     return false;
  101 }
  102 
  103 #endif