]> git.tdb.fi Git - netvis.git/blobdiff - source/netvis.cpp
Avoid unsigned int overflow if total number of hosts exceeds the maximum
[netvis.git] / source / netvis.cpp
index fb02c0dde99d13828a35d07c38e5b5ecf6c8bd9f..7bb49bb458e507cb52d3f33cfcd17872f3537142 100644 (file)
@@ -9,10 +9,10 @@ Distributed unter the GPL
 #include <cstdlib>
 #include <cmath>
 #include <signal.h>
+#include <netinet/ether.h>
 #include <netinet/ip.h>
 #include <netinet/tcp.h>
 #include <netinet/udp.h>
-#include <linux/if_ether.h>
 #include <msp/core/except.h>
 #include <msp/debug/profilingscope.h>
 #include <msp/gl/blend.h>
@@ -26,6 +26,7 @@ Distributed unter the GPL
 #include <msp/strings/formatter.h>
 #include <msp/time/units.h>
 #include <msp/time/utils.h>
+#include "history.h"
 #include "host.h"
 #include "netvis.h"
 #include "packet.h"
@@ -73,6 +74,8 @@ NetVis::NetVis(int argc, char **argv):
        font = new GL::Font;
        DataFile::load(*font, "dejavu-10.font");
 
+       history = new History(*this, 301, 100);
+
        catch_signal(SIGINT);
 }
 
@@ -114,6 +117,8 @@ void NetVis::tick()
        while(pcap_dispatch(pcap, -1, &capture_handler, reinterpret_cast<unsigned char *>(this))>0) ;
 
        resolver->tick();
+       history->tick(tick_t);
+
 
        float min_activity = numeric_limits<float>::max();
        for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
@@ -121,7 +126,7 @@ void NetVis::tick()
                i->second->tick(dt);
                min_activity = min(min_activity, i->second->get_activity());
        }
-       float del_limit = pow(10, 6-0.1*(max_hosts-hosts.size()-disabled_hosts.size()));
+       float del_limit = pow(10, 6-0.1*static_cast<int>(max_hosts-hosts.size()-disabled_hosts.size()));
        for(map<unsigned, Host *>::iterator i=disabled_hosts.begin(); i!=disabled_hosts.end();)
        {
                i->second->tick(dt);
@@ -130,10 +135,13 @@ void NetVis::tick()
                {
                        i->second->set_active(true);
                        hosts.insert(*i);
+                       for(unsigned j=0; j<100; ++j)
+                               i->second->tick(100*Time::msec);
                        disabled_hosts.erase(i++);
                }
                else if(i->second->get_activity()<del_limit)
                {
+                       resolver->cancel(i->second);
                        delete i->second;
                        disabled_hosts.erase(i++);
                }
@@ -165,6 +173,19 @@ void NetVis::tick()
                }
        }
 
+       for(map<unsigned, Port *>::iterator i=ports.begin(); i!=ports.end();)
+       {
+               i->second->tick(dt);
+
+               if(!i->second->is_registered() && i->second->get_activity()<0.1)
+               {
+                       delete i->second;
+                       ports.erase(i++);
+               }
+               else
+                       ++i;
+       }
+
        for(list<Packet *>::iterator i=packets.begin(); i!=packets.end();)
        {
                (*i)->tick(dt);
@@ -206,21 +227,33 @@ 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();
 
        GL::push_matrix();
-       GL::translate(-500, -360, 0);
+       GL::translate(-500, -348, 0);
        GL::scale_uniform(10);
        font->draw_string(format("%d hosts", hosts.size()+disabled_hosts.size()));
        GL::translate(0, -1.2, 0);
+       font->draw_string(format("%d ports", ports.size()));
+       GL::translate(0, -1.2, 0);
        font->draw_string(format("%.2f fps", fps));
        GL::Texture::unbind();
        GL::pop_matrix();
+
+       GL::push_matrix();
+       GL::translate(170, -370, 0);
+       history->render();
+       GL::pop_matrix();
 }
 
 Host &NetVis::get_host(unsigned a)
@@ -238,11 +271,13 @@ Host &NetVis::get_host(unsigned a)
                host->set_local(true);
        resolver->push(host);
        host->set_position(Vector2(rand()*400.0/RAND_MAX-200.0, rand()*400.0/RAND_MAX-200.0));
+       for(unsigned j=0; j<100; ++j)
+               host->tick(100*Time::msec);
        hosts[a] = host;
        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())
@@ -262,17 +297,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)
@@ -281,13 +339,23 @@ 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);
                }
 
                shost.add_activity(size);
                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)
+                       self->history->activity(size, 0);
        }
 }