]> git.tdb.fi Git - netvis.git/blobdiff - source/netvis.cpp
Resolve port names
[netvis.git] / source / netvis.cpp
index 3a0f09219449f46dc9bb5d1be56f116cffc0ad0b..cece749e9be6a2ab6003e5d02447673a5ad2d983 100644 (file)
@@ -5,7 +5,9 @@ Copyright @ 2008 Mikko Rasa, Mikkosoft Productions
 Distributed unter the GPL
 */
 
-#include <iostream>
+#include <limits>
+#include <cstdlib>
+#include <cmath>
 #include <signal.h>
 #include <netinet/ip.h>
 #include <netinet/tcp.h>
@@ -13,6 +15,8 @@ Distributed unter the GPL
 #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>
@@ -25,25 +29,29 @@ Distributed unter the GPL
 #include "host.h"
 #include "netvis.h"
 #include "packet.h"
+#include "port.h"
 #include "resolver.h"
 
 using namespace std;
 using namespace Msp;
 
 NetVis::NetVis(int argc, char **argv):
+       pcap(0),
+       resolver(0),
+       wnd(0),
+       font(0),
+       max_hosts(1000),
+       max_visible_hosts(30),
        draw_labels(true),
        blend(true),
        frames(0)
 {
        if(argc<2)
                throw UsageError("No interface given");
-       iface=argv[1];
-}
+       iface = argv[1];
 
-int NetVis::main()
-{
        char err[1024];
-       pcap=pcap_open_live(iface.c_str(), 128, true, 0, err);
+       pcap = pcap_open_live(iface.c_str(), 128, true, 0, err);
        if(!pcap)
                throw Exception(err);
 
@@ -51,46 +59,32 @@ int NetVis::main()
                throw Exception(err);
 
        pcap_lookupnet(iface.c_str(), &localnet, &localnet_mask, err);
-       localnet=ntohl(localnet);
-       localnet_mask=ntohl(localnet_mask);
+       localnet = ntohl(localnet);
+       localnet_mask = ntohl(localnet_mask);
 
-       dpy=new Graphics::Display;
-       wnd=new Graphics::Window(*dpy, 1024, 768);
-       glc=new Graphics::GLContext(*wnd);
+       resolver = new Resolver;
+
+       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;
+       font = new GL::Font;
        DataFile::load(*font, "dejavu-10.font");
-       font_tex=new GL::Texture2D;
-       DataFile::load(*font_tex, "dejavu-10.tex");
-       font->set_texture(*font_tex);
 
        catch_signal(SIGINT);
+}
 
-       resolver=new Resolver;
-
-       //set_loop_mode(TICK_BUSY);
-
-       Application::main();
-
+NetVis::~NetVis()
+{
        delete resolver;
 
        delete font;
-       delete font_tex;
-       delete glc;
        delete wnd;
-       delete dpy;
-
-       cout<<hosts.size()+disabled_hosts.size()<<" different hosts seen\n";
-       cout<<"capture: "<<profiler.scope("capture").total_time<<'\n';
-       cout<<"tick:    "<<profiler.scope("tick").total_time<<'\n';
-       cout<<"render:  "<<profiler.scope("render").total_time<<'\n';
 
        pcap_close(pcap);
        for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
@@ -99,30 +93,28 @@ int NetVis::main()
                delete i->second;
        for(list<Packet *>::iterator i=packets.begin(); i!=packets.end(); ++i)
                delete *i;
-
-       return exit_code;
 }
 
 void NetVis::tick()
 {
-       Msp::Time::TimeStamp t=Msp::Time::now();
+       Msp::Time::TimeStamp t = Msp::Time::now();
        Msp::Time::TimeDelta dt;
        if(tick_t)
-               dt=t-tick_t;
-       tick_t=t;
+               dt = t-tick_t;
+       tick_t = t;
 
        if(tick_t>fps_t+Msp::Time::sec)
        {
-               fps=frames/((tick_t-fps_t)/Msp::Time::sec);
-               fps_t=tick_t;
-               frames=0;
+               fps = frames/((tick_t-fps_t)/Msp::Time::sec);
+               fps_t = tick_t;
+               frames = 0;
        }
 
-       dpy->tick();
+       wnd->get_display().tick();
 
        {
                Debug::ProfilingScope s(profiler, "capture");
-               while(pcap_dispatch(pcap, -1, &capture_handler, reinterpret_cast<unsigned char *>(this))>0);
+               while(pcap_dispatch(pcap, -1, &capture_handler, reinterpret_cast<unsigned char *>(this))>0) ;
        }
 
        {
@@ -130,36 +122,42 @@ void NetVis::tick()
 
                resolver->tick();
 
-               float min_activity=1e7f;
+               float min_activity = numeric_limits<float>::max();
                for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
                {
                        i->second->tick(dt);
-                       min_activity=min(min_activity, i->second->get_activity());
+                       min_activity = min(min_activity, i->second->get_activity());
                }
-               for(map<unsigned, Host *>::iterator i=disabled_hosts.begin(); i!=disabled_hosts.end(); ++i)
-                       i->second->tick(dt);
-
+               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();)
                {
+                       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;
                }
-               if(hosts.size()>20)
+
+               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()-20);
-                       float limit=*j;
+                       list<float>::iterator j = activity.begin();
+                       advance(j, activity.size()-max_visible_hosts);
+                       float limit = *j;
 
                        for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end();)
                        {
@@ -180,7 +178,7 @@ void NetVis::tick()
                        if((*i)->get_stale())
                        {
                                delete *i;
-                               i=packets.erase(i);
+                               i = packets.erase(i);
                        }
                        else
                                ++i;
@@ -189,7 +187,7 @@ void NetVis::tick()
 
        {
                Debug::ProfilingScope s(profiler, "render");
-               glClear(GL_COLOR_BUFFER_BIT);
+               GL::clear(GL::COLOR_BUFFER_BIT);
 
                GL::matrix_mode(GL::PROJECTION);
                GL::load_identity();
@@ -201,7 +199,6 @@ void NetVis::tick()
                        i->second->render();
                if(draw_labels)
                {
-                       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();
@@ -214,10 +211,10 @@ void NetVis::tick()
 
                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)
+               unsigned n = 0;
+               for(map<unsigned, Port>::iterator i=ports.begin(); (i!=ports.end() && n<50); ++i, ++n)
                {
-                       GL::Color &color=i->second;
+                       const GL::Color &color = i->second.get_color();
 
                        imm.begin(GL::QUADS);
                        imm.color(color.r, color.g, color.b, color.a);
@@ -240,11 +237,12 @@ void NetVis::tick()
                        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)
+                       n = 0;
+                       for(map<unsigned, Port>::iterator i=ports.begin(); (i!=ports.end() && n<50); ++i, ++n)
                        {
-                               font->draw_string(format("%d", i->first));
+                               GL::Immediate imm2((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
+                               imm.color(1.0f, 1.0f, 1.0f);
+                               font->draw_string(i->second.get_name(), imm2);
 
                                GL::translate(0, -1.2, 0);
                        }
@@ -262,7 +260,7 @@ void NetVis::tick()
                GL::pop_matrix();
                GL::Texture::unbind();
 
-               glc->swap_buffers();
+               wnd->swap_buffers();
        }
 
        ++frames;
@@ -270,83 +268,114 @@ void NetVis::tick()
 
 Host &NetVis::get_host(unsigned a)
 {
-       map<unsigned, Host *>::iterator i=hosts.find(a);
+       map<unsigned, Host *>::iterator i = hosts.find(a);
        if(i!=hosts.end())
                return *i->second;
 
-       i=disabled_hosts.find(a);
+       i = disabled_hosts.find(a);
        if(i!=disabled_hosts.end())
                return *i->second;
 
-       Host *host=new Host(*this, a);
+       Host *host = new Host(*this, a);
        if((a&localnet_mask)==localnet)
                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));
-       hosts[a]=host;
+       hosts[a] = host;
        return *host;
 }
 
-GL::Color &NetVis::get_port_color(unsigned port)
+GL::Color NetVis::generate_color(bool privileged) const
 {
-       map<unsigned, GL::Color>::iterator i=port_colors.find(port);
-       if(i!=port_colors.end())
+       GL::Color color;
+       color.r = rand()*1.0/RAND_MAX;
+       color.g = rand()*1.0/RAND_MAX;
+       color.b = rand()*1.0/RAND_MAX;
+       float high = max(max(color.r, color.g), color.b);
+       color = color*(1.0/high);
+       if(privileged)
+       {
+               float low = min(min(color.r, color.g), color.b);
+               color = (color+-low)*(1/(1-low));
+       }
+       else
+               color = color*0.6+0.4;
+       return color;
+}
+
+const Port &NetVis::get_port(unsigned number)
+{
+       map<unsigned, Port>::iterator i = ports.find(number);
+       if(i!=ports.end())
                return i->second;
 
-       GL::Color color;
-       while(1)
+       GL::Color best_color;
+       float best_score = 0;
+       for(unsigned j=0; (j<100 && best_score<1); ++j)
        {
-               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;
+               GL::Color color = generate_color(number<1024);
+
+               float score = 2;
+               for(i=ports.begin(); i!=ports.end(); ++i)
+               {
+                       const GL::Color &other = i->second.get_color();
+                       float dr = color.r-other.r;
+                       float dg = color.g-other.g;
+                       float db = color.b-other.b;
+                       score = min(score, dr*dr+dg*dg+db*db);
+               }
+               if(score>best_score)
+               {
+                       best_score = score;
+                       best_color = color;
+               }
        }
-       color.a=0.4f;
-       return port_colors[port]=color;
+       best_color.a = 0.4f;
+       i = ports.insert(map<unsigned, Port>::value_type(number, Port(number, best_color))).first;
+       return i->second;
 }
 
 void NetVis::key_press(unsigned key, unsigned, wchar_t)
 {
        if(key==46)
-               draw_labels=!draw_labels;
+               draw_labels = !draw_labels;
        else if(key==56)
        {
-               blend=!blend;
+               blend = !blend;
                GL::set(GL_BLEND, blend);
        }
 }
 
 void NetVis::capture_handler(unsigned char *user, const pcap_pkthdr *, const unsigned char *data)
 {
-       NetVis *self=reinterpret_cast<NetVis *>(user);
+       NetVis *self = reinterpret_cast<NetVis *>(user);
 
-       const ethhdr *eth=reinterpret_cast<const ethhdr *>(data);
+       const ethhdr *eth = reinterpret_cast<const ethhdr *>(data);
        if(ntohs(eth->h_proto)==ETH_P_IP)
        {
-               const iphdr *ip=reinterpret_cast<const iphdr *>(eth+1);
+               const iphdr *ip = reinterpret_cast<const iphdr *>(eth+1);
 
-               unsigned size=ntohs(ip->tot_len);
-               unsigned port=0;
+               unsigned size = ntohs(ip->tot_len);
+               unsigned port = 0;
                if(ip->protocol==IPPROTO_TCP)
                {
-                       const tcphdr *tcp=reinterpret_cast<const tcphdr *>(ip+1);
-                       port=min(ntohs(tcp->source), ntohs(tcp->dest));
+                       const tcphdr *tcp = reinterpret_cast<const tcphdr *>(ip+1);
+                       port = min(ntohs(tcp->source), 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));
+                       const udphdr *udp = reinterpret_cast<const udphdr *>(ip+1);
+                       port = min(ntohs(udp->source), ntohs(udp->dest));
                }
-               Host &shost=self->get_host(ntohl(ip->saddr));
-               Host *dhost=0;
+               Host &shost = self->get_host(ntohl(ip->saddr));
+               Host *dhost = 0;
                if((ntohl(ip->daddr)&0xFF)!=0xFF)
-                       dhost=&self->get_host(ntohl(ip->daddr));
+                       dhost = &self->get_host(ntohl(ip->daddr));
 
-               float throttle=shost.send_packet();
+               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, self->get_port(port).get_color(), size));
                        self->packets.back()->tick(-throttle*Msp::Time::sec);
                }