]> git.tdb.fi Git - netvis.git/commitdiff
Track and display the activity of ports
authorMikko Rasa <tdb@tdb.fi>
Sun, 15 Jun 2014 08:23:35 +0000 (11:23 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 15 Jun 2014 08:23:35 +0000 (11:23 +0300)
source/netvis.cpp
source/netvis.h
source/port.cpp
source/port.h

index dce083706470e17b1600765d4a94ac9a415c433f..17f113a369a2ba7c879439b0577435077280f172 100644 (file)
@@ -214,10 +214,15 @@ void NetVis::render()
        GL::push_matrix();
        GL::translate(-500, 360, 0);
        unsigned n = 0;
-       for(map<unsigned, Port *>::iterator i=ports.begin(); (i!=ports.end() && n<50); ++i, ++n)
+       for(map<unsigned, Port *>::iterator i=ports.begin(); (i!=ports.end() && n<50); ++i)
        {
-               i->second->render();
-               GL::translate(0, -12, 0);
+               float act = i->second->get_activity();
+               if((i->second->is_registered() && act>1) || act>200)
+               {
+                       i->second->render();
+                       GL::translate(0, -12, 0);
+                       ++n;
+               }
        }
        GL::pop_matrix();
 
@@ -257,7 +262,7 @@ Host &NetVis::get_host(unsigned a)
        return *host;
 }
 
-const Port &NetVis::get_port(unsigned number)
+Port &NetVis::get_port(unsigned number)
 {
        map<unsigned, Port *>::iterator i = ports.find(number);
        if(i!=ports.end())
@@ -277,17 +282,40 @@ void NetVis::capture_handler(unsigned char *user, const pcap_pkthdr *, const uns
                const iphdr *ip = reinterpret_cast<const iphdr *>(eth+1);
 
                unsigned size = ntohs(ip->tot_len);
-               unsigned port = 0;
+
+               Port *sport = 0;
+               Port *dport = 0;
                if(ip->protocol==IPPROTO_TCP)
                {
                        const tcphdr *tcp = reinterpret_cast<const tcphdr *>(ip+1);
-                       port = min(ntohs(tcp->source), ntohs(tcp->dest));
+                       sport = &self->get_port(ntohs(tcp->source));
+                       dport = &self->get_port(ntohs(tcp->dest));
                }
                else if(ip->protocol==IPPROTO_UDP)
                {
                        const udphdr *udp = reinterpret_cast<const udphdr *>(ip+1);
-                       port = min(ntohs(udp->source), ntohs(udp->dest));
+                       sport = &self->get_port(ntohs(udp->source));
+                       dport = &self->get_port(ntohs(udp->dest));
                }
+
+               Port *port = 0;
+               if(sport && dport)
+               {
+                       if(sport->is_registered()!=dport->is_registered())
+                       {
+                               if(sport->is_registered())
+                                       port = sport;
+                               else
+                                       port = dport;
+                       }
+                       else if(sport->get_number()<dport->get_number())
+                               port = sport;
+                       else
+                               port = dport;
+               }
+               else
+                       port = &self->get_port(0);
+
                Host &shost = self->get_host(ntohl(ip->saddr));
                Host *dhost = 0;
                if((ntohl(ip->daddr)&0xFF)!=0xFF)
@@ -296,7 +324,7 @@ void NetVis::capture_handler(unsigned char *user, const pcap_pkthdr *, const uns
                float throttle = shost.send_packet();
                if(throttle<1)
                {
-                       self->packets.push_back(new Packet(shost, dhost, self->get_port(port).get_color(), size));
+                       self->packets.push_back(new Packet(shost, dhost, port->get_color(), size));
                        self->packets.back()->tick(-throttle*Msp::Time::sec);
                }
 
@@ -304,6 +332,11 @@ void NetVis::capture_handler(unsigned char *user, const pcap_pkthdr *, const uns
                if(dhost)
                        dhost->add_activity(size);
 
+               if(sport)
+                       sport->add_activity(size);
+               if(dport)
+                       dport->add_activity(size);
+
                if((ntohl(ip->saddr)&self->localnet_mask)==self->localnet)
                        self->history->activity(0, size);
                else if((ntohl(ip->daddr)&self->localnet_mask)==self->localnet)
index 989e06054555e3da34bd6f0a4e775c52bd8b51f1..a205d0564af72b89e340173b212614cc54a5c404 100644 (file)
@@ -63,7 +63,7 @@ private:
        void render();
        Host &get_host(unsigned);
        Msp::GL::Color generate_color(bool) const;
-       const Port &get_port(unsigned);
+       Port &get_port(unsigned);
        void create_history_texture();
 
        static void capture_handler(unsigned char *, const pcap_pkthdr *, const unsigned char *);
index 1e4219da1025fbff77823a4aebd949178a851bce..5c0bfa62435634892f970f282887e8ef006b41a8 100644 (file)
@@ -92,13 +92,36 @@ Port::Port(NetVis &v, unsigned n):
        bld.end();
 }
 
+void Port::add_activity(unsigned bytes)
+{
+       activity.add_bytes(bytes);
+}
+
+void Port::tick(const Time::TimeDelta &dt)
+{
+       activity.tick(dt);
+}
+
 void Port::render() const
 {
        GL::PushMatrix push_;
        mesh.draw();
+       {
+               GL::Immediate imm((GL::COLOR4_UBYTE, GL::VERTEX2));
+               imm.begin(GL::QUADS);
+               imm.color(color.r, color.g, color.b, color.a);
+               unsigned x = static_cast<unsigned>(activity.get_average()/4096);
+               imm.vertex(14, 0);
+               imm.vertex(14+x, 0);
+               imm.vertex(14+x, 10);
+               imm.vertex(14, 10);
+               imm.end();
+       }
        GL::translate(16, 1, 0);
        GL::scale_uniform(10);
-       GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
-       netvis.get_font().draw_string(name, imm);
+       {
+               GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
+               netvis.get_font().draw_string(name, imm);
+       }
        GL::Texture::unbind();
 }
index 4ce9427fc6df5b6913afd0743e8a8fb38dbe9a22..8877ed9067df827cf8a332503d0bf1b72b42b835 100644 (file)
@@ -11,6 +11,7 @@ Distributed unter the GPL
 #include <string>
 #include <msp/gl/color.h>
 #include <msp/gl/mesh.h>
+#include "activity.h"
 
 class NetVis;
 
@@ -23,6 +24,7 @@ private:
        std::string name;
        Msp::GL::Color color;
        Msp::GL::Mesh mesh;
+       Activity activity;
 
 public:
        Port(NetVis &, unsigned);
@@ -32,6 +34,11 @@ public:
        const std::string &get_name() const { return name; }
        const Msp::GL::Color &get_color() const { return color; }
 
+       void add_activity(unsigned);
+       float get_activity() const { return activity.get_average(); }
+
+       void tick(const Msp::Time::TimeDelta &);
+
        void render() const;
 };