]> git.tdb.fi Git - netvis.git/blobdiff - source/netvis.cpp
Track and display the activity of ports
[netvis.git] / source / netvis.cpp
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)