From: Mikko Rasa Date: Sun, 15 Jun 2014 08:23:35 +0000 (+0300) Subject: Track and display the activity of ports X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=afeb198dfbe4929c8d95487bca64a84ec3b92148;p=netvis.git Track and display the activity of ports --- diff --git a/source/netvis.cpp b/source/netvis.cpp index dce0837..17f113a 100644 --- a/source/netvis.cpp +++ b/source/netvis.cpp @@ -214,10 +214,15 @@ void NetVis::render() GL::push_matrix(); GL::translate(-500, 360, 0); unsigned n = 0; - for(map::iterator i=ports.begin(); (i!=ports.end() && n<50); ++i, ++n) + for(map::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::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(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(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(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()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) diff --git a/source/netvis.h b/source/netvis.h index 989e060..a205d05 100644 --- a/source/netvis.h +++ b/source/netvis.h @@ -63,7 +63,7 @@ private: void render(); Host &get_host(unsigned); Msp::GL::Color generate_color(bool) const; - const Port &get_port(unsigned); + Port &get_port(unsigned); void create_history_texture(); static void capture_handler(unsigned char *, const pcap_pkthdr *, const unsigned char *); diff --git a/source/port.cpp b/source/port.cpp index 1e4219d..5c0bfa6 100644 --- a/source/port.cpp +++ b/source/port.cpp @@ -92,13 +92,36 @@ Port::Port(NetVis &v, unsigned n): bld.end(); } +void Port::add_activity(unsigned bytes) +{ + activity.add_bytes(bytes); +} + +void Port::tick(const Time::TimeDelta &dt) +{ + activity.tick(dt); +} + void Port::render() const { GL::PushMatrix push_; mesh.draw(); + { + GL::Immediate imm((GL::COLOR4_UBYTE, GL::VERTEX2)); + imm.begin(GL::QUADS); + imm.color(color.r, color.g, color.b, color.a); + unsigned x = static_cast(activity.get_average()/4096); + imm.vertex(14, 0); + imm.vertex(14+x, 0); + imm.vertex(14+x, 10); + imm.vertex(14, 10); + imm.end(); + } GL::translate(16, 1, 0); GL::scale_uniform(10); - GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2)); - netvis.get_font().draw_string(name, imm); + { + GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2)); + netvis.get_font().draw_string(name, imm); + } GL::Texture::unbind(); } diff --git a/source/port.h b/source/port.h index 4ce9427..8877ed9 100644 --- a/source/port.h +++ b/source/port.h @@ -11,6 +11,7 @@ Distributed unter the GPL #include #include #include +#include "activity.h" class NetVis; @@ -23,6 +24,7 @@ private: std::string name; Msp::GL::Color color; Msp::GL::Mesh mesh; + Activity activity; public: Port(NetVis &, unsigned); @@ -32,6 +34,11 @@ public: const std::string &get_name() const { return name; } const Msp::GL::Color &get_color() const { return color; } + void add_activity(unsigned); + float get_activity() const { return activity.get_average(); } + + void tick(const Msp::Time::TimeDelta &); + void render() const; };