]> 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 e4d30579dac6e3352a38d9ccc69114c62d929649..7bb49bb458e507cb52d3f33cfcd17872f3537142 100644 (file)
@@ -9,12 +9,14 @@ 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>
+#include <msp/gl/framebuffer.h>
 #include <msp/gl/immediate.h>
 #include <msp/gl/matrix.h>
 #include <msp/gl/misc.h>
@@ -24,9 +26,11 @@ 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"
+#include "port.h"
 #include "resolver.h"
 
 using namespace std;
@@ -39,8 +43,6 @@ NetVis::NetVis(int argc, char **argv):
        font(0),
        max_hosts(1000),
        max_visible_hosts(30),
-       draw_labels(true),
-       blend(true),
        frames(0)
 {
        if(argc<2)
@@ -64,15 +66,16 @@ NetVis::NetVis(int argc, char **argv):
        wnd = new Graphics::SimpleGLWindow(1024, 768);
        wnd->set_title("NetVis");
        wnd->signal_close.connect(sigc::bind(sigc::mem_fun(this, &NetVis::exit), 0));
-       wnd->signal_key_press.connect(sigc::mem_fun(this, &NetVis::key_press));
        wnd->show();
 
-       GL::enable(GL_BLEND);
-       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+       GL::enable(GL::BLEND);
+       GL::blend_func(GL::SRC_ALPHA, GL::ONE_MINUS_SRC_ALPHA);
 
        font = new GL::Font;
        DataFile::load(*font, "dejavu-10.font");
 
+       history = new History(*this, 301, 100);
+
        catch_signal(SIGINT);
 }
 
@@ -88,6 +91,8 @@ NetVis::~NetVis()
                delete i->second;
        for(map<unsigned, Host *>::iterator i=disabled_hosts.begin(); i!=disabled_hosts.end(); ++i)
                delete i->second;
+       for(map<unsigned, Port *>::iterator i=ports.begin(); i!=ports.end(); ++i)
+               delete i->second;
        for(list<Packet *>::iterator i=packets.begin(); i!=packets.end(); ++i)
                delete *i;
 }
@@ -109,158 +114,146 @@ void NetVis::tick()
 
        wnd->get_display().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)
        {
-               Debug::ProfilingScope s(profiler, "capture");
-               while(pcap_dispatch(pcap, -1, &capture_handler, reinterpret_cast<unsigned char *>(this))>0) ;
+               i->second->tick(dt);
+               min_activity = min(min_activity, i->second->get_activity());
        }
-
+       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();)
        {
-               Debug::ProfilingScope s(profiler, "tick");
-
-               resolver->tick();
+               i->second->tick(dt);
 
-               float min_activity = numeric_limits<float>::max();
-               for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
+               if(i->second->get_activity()>min_activity)
                {
-                       i->second->tick(dt);
-                       min_activity = min(min_activity, i->second->get_activity());
+                       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++);
                }
-               float del_limit = pow(10, 6-0.1*(max_hosts-hosts.size()-disabled_hosts.size()));
-               for(map<unsigned, Host *>::iterator i=disabled_hosts.begin(); i!=disabled_hosts.end();)
+               else if(i->second->get_activity()<del_limit)
                {
-                       i->second->tick(dt);
-
-                       if(i->second->get_activity()>min_activity)
-                       {
-                               i->second->set_active(true);
-                               hosts.insert(*i);
-                               disabled_hosts.erase(i++);
-                       }
-                       else if(i->second->get_activity()<del_limit)
-                       {
-                               delete i->second;
-                               disabled_hosts.erase(i++);
-                       }
-                       else
-                               ++i;
+                       resolver->cancel(i->second);
+                       delete i->second;
+                       disabled_hosts.erase(i++);
                }
+               else
+                       ++i;
+       }
 
-               if(hosts.size()>max_visible_hosts)
-               {
-                       list<float> activity;
-                       for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
-                               activity.push_back(i->second->get_activity());
-                       activity.sort();
-
-                       list<float>::iterator j = activity.begin();
-                       advance(j, activity.size()-max_visible_hosts);
-                       float limit = *j;
+       if(hosts.size()>max_visible_hosts)
+       {
+               list<float> activity;
+               for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
+                       activity.push_back(i->second->get_activity());
+               activity.sort();
 
-                       for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end();)
-                       {
-                               if(i->second->get_activity()<limit)
-                               {
-                                       i->second->set_active(false);
-                                       disabled_hosts.insert(*i);
-                                       hosts.erase(i++);
-                               }
-                               else
-                                       ++i;
-                       }
-               }
+               list<float>::iterator j = activity.begin();
+               advance(j, activity.size()-max_visible_hosts);
+               float limit = *j;
 
-               for(list<Packet *>::iterator i=packets.begin(); i!=packets.end();)
+               for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end();)
                {
-                       (*i)->tick(dt);
-                       if((*i)->get_stale())
+                       if(i->second->get_activity()<limit)
                        {
-                               delete *i;
-                               i = packets.erase(i);
+                               i->second->set_active(false);
+                               disabled_hosts.insert(*i);
+                               hosts.erase(i++);
                        }
                        else
                                ++i;
                }
        }
 
+       for(map<unsigned, Port *>::iterator i=ports.begin(); i!=ports.end();)
        {
-               Debug::ProfilingScope s(profiler, "render");
-               glClear(GL_COLOR_BUFFER_BIT);
+               i->second->tick(dt);
 
-               GL::matrix_mode(GL::PROJECTION);
-               GL::load_identity();
-               GL::ortho_centered(1024, 768);
-               GL::matrix_mode(GL::MODELVIEW);
-               GL::load_identity();
+               if(!i->second->is_registered() && i->second->get_activity()<0.1)
+               {
+                       delete i->second;
+                       ports.erase(i++);
+               }
+               else
+                       ++i;
+       }
 
-               for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
-                       i->second->render();
-               if(draw_labels)
+       for(list<Packet *>::iterator i=packets.begin(); i!=packets.end();)
+       {
+               (*i)->tick(dt);
+               if((*i)->get_stale())
                {
-                       glColor4f(1.0, 1.0, 1.0, 1.0);
-                       for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
-                               i->second->render_label();
-                       GL::Texture::unbind();
+                       delete *i;
+                       i = packets.erase(i);
                }
+               else
+                       ++i;
+       }
+
+       render();
+       wnd->swap_buffers();
+
+       ++frames;
+}
+
+void NetVis::render()
+{
+       GL::clear(GL::COLOR_BUFFER_BIT);
+
+       GL::matrix_mode(GL::PROJECTION);
+       GL::load_identity();
+       GL::ortho_centered(1024, 768);
+       GL::matrix_mode(GL::MODELVIEW);
+       GL::load_identity();
+
+       for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
+               i->second->render();
+       {
                GL::Immediate imm((GL::COLOR4_UBYTE, GL::VERTEX2));
                imm.begin(GL::QUADS);
                for(list<Packet *>::iterator i=packets.begin(); i!=packets.end(); ++i)
                        (*i)->render(imm);
                imm.end();
+       }
 
-               GL::push_matrix();
-               GL::translate(-500, 360, 0);
-               unsigned n = 0;
-               for(map<unsigned, GL::Color>::iterator i=port_colors.begin(); (i!=port_colors.end() && n<20); ++i, ++n)
+       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)
+       {
+               float act = i->second->get_activity();
+               if((i->second->is_registered() && act>1) || act>200)
                {
-                       GL::Color &color = i->second;
-
-                       imm.begin(GL::QUADS);
-                       imm.color(color.r, color.g, color.b, color.a);
-                       for(unsigned x=0; x<=4; x+=2)
-                       {
-                               imm.vertex(x+0, 0);
-                               imm.vertex(x+10, 0);
-                               imm.vertex(x+10, 10);
-                               imm.vertex(x+0, 10);
-                       }
-                       imm.end();
-
+                       i->second->render();
                        GL::translate(0, -12, 0);
+                       ++n;
                }
-               GL::pop_matrix();
-
-               GL::push_matrix();
-               if(draw_labels)
-               {
-                       GL::push_matrix();
-                       GL::translate(-484, 361, 0);
-                       GL::scale_uniform(10);
-                       glColor4f(1.0, 1.0, 1.0, 1.0);
-                       n = 0;
-                       for(map<unsigned, GL::Color>::iterator i=port_colors.begin(); (i!=port_colors.end() && n<20); ++i, ++n)
-                       {
-                               font->draw_string(format("%d", i->first));
-
-                               GL::translate(0, -1.2, 0);
-                       }
-                       GL::pop_matrix();
-                       GL::Texture::unbind();
-               }
-               GL::pop_matrix();
-
-               GL::push_matrix();
-               GL::translate(-500, -360, 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("%.2f fps", fps));
-               GL::pop_matrix();
-               GL::Texture::unbind();
-
-               wnd->swap_buffers();
        }
-
-       ++frames;
+       GL::pop_matrix();
+
+       GL::push_matrix();
+       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)
@@ -278,38 +271,20 @@ 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;
 }
 
-GL::Color &NetVis::get_port_color(unsigned port)
-{
-       map<unsigned, GL::Color>::iterator i = port_colors.find(port);
-       if(i!=port_colors.end())
-               return i->second;
-
-       GL::Color color;
-       while(1)
-       {
-               color.r = rand()*1.0/RAND_MAX;
-               color.g = rand()*1.0/RAND_MAX;
-               color.b = rand()*1.0/RAND_MAX;
-               if(color.r>0.5 || color.g>0.5 || color.b>0.7)
-                       break;
-       }
-       color.a = 0.4f;
-       return port_colors[port] = color;
-}
-
-void NetVis::key_press(unsigned key, unsigned, wchar_t)
+Port &NetVis::get_port(unsigned number)
 {
-       if(key==46)
-               draw_labels = !draw_labels;
-       else if(key==56)
-       {
-               blend = !blend;
-               GL::set(GL_BLEND, blend);
-       }
+       map<unsigned, Port *>::iterator i = ports.find(number);
+       if(i!=ports.end())
+               return *i->second;
+       Port *port = new Port(*this, number);
+       ports[number] = port;
+       return *port;
 }
 
 void NetVis::capture_handler(unsigned char *user, const pcap_pkthdr *, const unsigned char *data)
@@ -322,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)
@@ -341,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_color(port), 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);
        }
 }