#include <cstdlib>
#include <cmath>
#include <signal.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/core/getopt.h>
#include <msp/debug/profilingscope.h>
#include <msp/gl/blend.h>
#include <msp/gl/framebuffer.h>
#include <msp/gl/projection.h>
#include <msp/gl/texture2d.h>
#include <msp/gl/transform.h>
-#include <msp/strings/formatter.h>
+#include <msp/io/print.h>
+#include <msp/strings/format.h>
#include <msp/time/units.h>
#include <msp/time/utils.h>
+#include "history.h"
#include "host.h"
#include "netvis.h"
#include "packet.h"
font(0),
max_hosts(1000),
max_visible_hosts(30),
- draw_labels(true),
- blend(true),
frames(0)
{
if(argc<2)
- throw UsageError("No interface given");
+ throw usage_error("No interface given");
iface = argv[1];
char err[1024];
pcap = pcap_open_live(iface.c_str(), 128, true, 0, err);
if(!pcap)
- throw Exception(err);
+ throw runtime_error(err);
if(pcap_setnonblock(pcap, true, err)==-1)
- throw Exception(err);
+ throw runtime_error(err);
- pcap_lookupnet(iface.c_str(), &localnet, &localnet_mask, err);
- localnet = ntohl(localnet);
- localnet_mask = ntohl(localnet_mask);
+ unsigned local_addr, local_mask;
+ pcap_lookupnet(iface.c_str(), &local_addr, &local_mask, err);
+ localnet = Address(ntohl(local_addr));
+ localnet.set_mask(Address(ntohl(local_mask)));
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);
- GL::blend_func(GL::SRC_ALPHA, GL::ONE_MINUS_SRC_ALPHA);
+ GL::Blend::alpha().bind();
font = new GL::Font;
DataFile::load(*font, "dejavu-10.font");
+ history = new History(*this, 301, 100);
+
catch_signal(SIGINT);
}
NetVis::~NetVis()
{
+ delete history;
delete resolver;
delete font;
delete wnd;
pcap_close(pcap);
- for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
+ for(map<Address, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
+ delete i->second;
+ for(map<Address, Host *>::iterator i=disabled_hosts.begin(); i!=disabled_hosts.end(); ++i)
delete i->second;
- for(map<unsigned, Host *>::iterator i=disabled_hosts.begin(); i!=disabled_hosts.end(); ++i)
+ 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;
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<Address, 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<Address, Host *>::iterator i=disabled_hosts.begin(); i!=disabled_hosts.end();)
{
- Debug::ProfilingScope s(profiler, "tick");
+ i->second->tick(dt);
- resolver->tick();
-
- 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();
+ if(hosts.size()>max_visible_hosts)
+ {
+ list<float> activity;
+ for(map<Address, 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;
+ 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();)
- {
- if(i->second->get_activity()<limit)
- {
- i->second->set_active(false);
- disabled_hosts.insert(*i);
- hosts.erase(i++);
- }
- else
- ++i;
- }
- }
-
- for(list<Packet *>::iterator i=packets.begin(); i!=packets.end();)
+ for(map<Address, 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");
- GL::clear(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())
{
- 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::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT);
+
+ GL::MatrixStack::projection() = GL::Matrix::ortho_centered(1024, 768);
+ GL::MatrixStack::modelview() = GL::Matrix();
+
+ for(map<Address, 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, Port>::iterator i=ports.begin(); (i!=ports.end() && n<50); ++i, ++n)
- {
- const GL::Color &color = i->second.get_color();
-
- 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();
-
- GL::translate(0, -12, 0);
- }
- GL::pop_matrix();
-
- GL::push_matrix();
- if(draw_labels)
+ GL::MatrixStack::modelview() = GL::Matrix::translation(-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::push_matrix();
- GL::translate(-484, 361, 0);
- GL::scale_uniform(10);
- n = 0;
- for(map<unsigned, Port>::iterator i=ports.begin(); (i!=ports.end() && n<50); ++i, ++n)
- {
- 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);
- }
- GL::pop_matrix();
- GL::Texture::unbind();
+ i->second->render();
+ GL::MatrixStack::modelview() *= GL::Matrix::translation(0, -12, 0);
+ ++n;
}
- 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::MatrixStack::modelview() = GL::Matrix::translation(-500, -348, 0);
+ GL::MatrixStack::modelview() *= GL::Matrix::scaling(10);
+ font->draw_string(format("%d hosts", hosts.size()+disabled_hosts.size()));
+ GL::MatrixStack::modelview() *= GL::Matrix::translation(0, -1.2, 0);
+ font->draw_string(format("%d ports", ports.size()));
+ GL::MatrixStack::modelview() *= GL::Matrix::translation(0, -1.2, 0);
+ font->draw_string(format("%.2f fps", fps));
+ GL::Texture::unbind();
+
+ GL::MatrixStack::modelview() = GL::Matrix::translation(170, -370, 0);
+ history->render();
}
-Host &NetVis::get_host(unsigned a)
+Host &NetVis::get_host(const Address &a)
{
- map<unsigned, Host *>::iterator i = hosts.find(a);
+ map<Address, Host *>::iterator i = hosts.find(a);
if(i!=hosts.end())
return *i->second;
return *i->second;
Host *host = new Host(*this, a);
- if((a&localnet_mask)==localnet)
+ if(localnet.masked_match(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::generate_color(bool privileged) const
+Port &NetVis::get_port(unsigned number)
+{
+ 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 *cap, const unsigned char *data)
{
- 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)
+ NetVis *self = reinterpret_cast<NetVis *>(user);
+
+ CaptureContext ctx;
+ ctx.cap_hdr = cap;
+ const ethhdr *eth = reinterpret_cast<const ethhdr *>(data);
+ self->handle_ethernet(ctx, eth, cap->caplen);
+}
+
+void NetVis::handle_ethernet(CaptureContext &ctx, const ethhdr *eth, unsigned len)
+{
+ ctx.size = ctx.cap_hdr->len-sizeof(ethhdr);
+
+ int proto = ntohs(eth->h_proto);
+ if(proto==ETH_P_IP)
{
- float low = min(min(color.r, color.g), color.b);
- color = (color+-low)*(1/(1-low));
+ const iphdr *ip = reinterpret_cast<const iphdr *>(eth+1);
+ handle_ipv4(ctx, ip, len-sizeof(ethhdr));
}
else
- color = color*0.6+0.4;
- return color;
+ IO::print("Unknown protocol in eth: %d\n", proto);
}
-const Port &NetVis::get_port(unsigned number)
+void NetVis::handle_ipv4(CaptureContext &ctx, const iphdr *ip, unsigned len)
{
- map<unsigned, Port>::iterator i = ports.find(number);
- if(i!=ports.end())
- return i->second;
+ ctx.src_host = &get_host(ntohl(ip->saddr));
+ if((ntohl(ip->daddr)&0xFF)!=0xFF)
+ ctx.dst_host = &get_host(ntohl(ip->daddr));
- GL::Color best_color;
- float best_score = 0;
- for(unsigned j=0; (j<100 && best_score<1); ++j)
+ if(ip->protocol==IPPROTO_TCP)
{
- 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;
- }
+ const tcphdr *tcp = reinterpret_cast<const tcphdr *>(ip+1);
+ handle_tcp(ctx, tcp, len-sizeof(iphdr));
}
- best_color.a = 0.4f;
- i = ports.insert(map<unsigned, Port>::value_type(number, Port(number, best_color))).first;
- return i->second;
+ else if(ip->protocol==IPPROTO_UDP)
+ {
+ const udphdr *udp = reinterpret_cast<const udphdr *>(ip+1);
+ handle_udp(ctx, udp, len-sizeof(iphdr));
+ }
+ else
+ IO::print("Unknown protocol in ip: %d\n", ip->protocol);
}
-void NetVis::key_press(unsigned key, unsigned, wchar_t)
+
+void NetVis::handle_tcp(CaptureContext &ctx, const tcphdr *tcp, unsigned)
{
- if(key==46)
- draw_labels = !draw_labels;
- else if(key==56)
- {
- blend = !blend;
- GL::set(GL_BLEND, blend);
- }
+ ctx.src_port = &get_port(ntohs(tcp->source));
+ ctx.dst_port = &get_port(ntohs(tcp->dest));
+ handle_packet(ctx);
}
-void NetVis::capture_handler(unsigned char *user, const pcap_pkthdr *, const unsigned char *data)
+void NetVis::handle_udp(CaptureContext &ctx, const udphdr *udp, unsigned)
{
- NetVis *self = reinterpret_cast<NetVis *>(user);
+ ctx.src_port = &get_port(ntohs(udp->source));
+ ctx.dst_port = &get_port(ntohs(udp->dest));
+ handle_packet(ctx);
+}
- const ethhdr *eth = reinterpret_cast<const ethhdr *>(data);
- if(ntohs(eth->h_proto)==ETH_P_IP)
+void NetVis::handle_packet(CaptureContext &ctx)
+{
+ Port *port = 0;
+ if(ctx.src_port && ctx.dst_port)
{
- const iphdr *ip = reinterpret_cast<const iphdr *>(eth+1);
-
- unsigned size = ntohs(ip->tot_len);
- unsigned port = 0;
- if(ip->protocol==IPPROTO_TCP)
+ if(ctx.src_port->is_registered()!=ctx.dst_port->is_registered())
{
- 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));
- }
- Host &shost = self->get_host(ntohl(ip->saddr));
- Host *dhost = 0;
- if((ntohl(ip->daddr)&0xFF)!=0xFF)
- dhost = &self->get_host(ntohl(ip->daddr));
-
- 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.back()->tick(-throttle*Msp::Time::sec);
+ if(ctx.src_port->is_registered())
+ port = ctx.src_port;
+ else
+ port = ctx.dst_port;
}
+ else if(ctx.src_port->get_number()<ctx.dst_port->get_number())
+ port = ctx.src_port;
+ else
+ port = ctx.dst_port;
+ }
+ else
+ port = &get_port(0);
- shost.add_activity(size);
- if(dhost)
- dhost->add_activity(size);
+ float throttle = ctx.src_host->send_packet();
+ if(throttle<1)
+ {
+ packets.push_back(new Packet(*ctx.src_host, ctx.dst_host, port->get_color(), ctx.size));
+ packets.back()->tick(-throttle*Msp::Time::sec);
}
+
+ ctx.src_host->add_activity(ctx.size);
+ if(ctx.dst_host)
+ ctx.dst_host->add_activity(ctx.size);
+
+ if(ctx.src_port)
+ ctx.src_port->add_activity(ctx.size);
+ if(ctx.dst_port)
+ ctx.dst_port->add_activity(ctx.size);
+
+ bool local_src = ctx.src_host->is_local();
+ bool local_dst = (ctx.dst_host && ctx.dst_host->is_local());
+ if(local_src && !local_dst)
+ history->activity(0, ctx.size);
+ else if(local_dst && !local_src)
+ history->activity(ctx.size, 0);
}
void NetVis::sighandler(int)
exit(0);
}
-Application::RegApp<NetVis> NetVis::reg;
+
+NetVis::CaptureContext::CaptureContext():
+ cap_hdr(0),
+ src_host(0),
+ src_port(0),
+ dst_host(0),
+ dst_port(0),
+ size(0)
+{ }