3 This file is part of NetVis
4 Copyright @ 2008 Mikko Rasa, Mikkosoft Productions
5 Distributed unter the GPL
12 #include <msp/core/getopt.h>
13 #include <msp/debug/profilingscope.h>
14 #include <msp/gl/blend.h>
15 #include <msp/gl/framebuffer.h>
16 #include <msp/gl/immediate.h>
17 #include <msp/gl/matrix.h>
18 #include <msp/gl/misc.h>
19 #include <msp/gl/projection.h>
20 #include <msp/gl/texture2d.h>
21 #include <msp/gl/transform.h>
22 #include <msp/io/print.h>
23 #include <msp/strings/format.h>
24 #include <msp/time/units.h>
25 #include <msp/time/utils.h>
36 NetVis::NetVis(int argc, char **argv):
42 max_visible_hosts(30),
46 throw usage_error("No interface given");
50 pcap = pcap_open_live(iface.c_str(), 128, true, 0, err);
52 throw runtime_error(err);
54 if(pcap_setnonblock(pcap, true, err)==-1)
55 throw runtime_error(err);
57 unsigned local_addr, local_mask;
58 pcap_lookupnet(iface.c_str(), &local_addr, &local_mask, err);
59 localnet = Address(ntohl(local_addr));
60 localnet.set_mask(Address(ntohl(local_mask)));
62 resolver = new Resolver;
64 wnd = new Graphics::SimpleGLWindow(1024, 768);
65 wnd->set_title("NetVis");
66 wnd->signal_close.connect(sigc::bind(sigc::mem_fun(this, &NetVis::exit), 0));
69 GL::Blend::alpha().bind();
72 DataFile::load(*font, "dejavu-10.font");
74 history = new History(*this, 301, 100);
88 for(map<Address, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
90 for(map<Address, Host *>::iterator i=disabled_hosts.begin(); i!=disabled_hosts.end(); ++i)
92 for(map<unsigned, Port *>::iterator i=ports.begin(); i!=ports.end(); ++i)
94 for(list<Packet *>::iterator i=packets.begin(); i!=packets.end(); ++i)
100 Msp::Time::TimeStamp t = Msp::Time::now();
101 Msp::Time::TimeDelta dt;
106 if(tick_t>fps_t+Msp::Time::sec)
108 fps = frames/((tick_t-fps_t)/Msp::Time::sec);
113 wnd->get_display().tick();
115 while(pcap_dispatch(pcap, -1, &capture_handler, reinterpret_cast<unsigned char *>(this))>0) ;
118 history->tick(tick_t);
121 float min_activity = numeric_limits<float>::max();
122 for(map<Address, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
125 min_activity = min(min_activity, i->second->get_activity());
127 float del_limit = pow(10, 6-0.1*static_cast<int>(max_hosts-hosts.size()-disabled_hosts.size()));
128 for(map<Address, Host *>::iterator i=disabled_hosts.begin(); i!=disabled_hosts.end();)
132 if(i->second->get_activity()>min_activity)
134 i->second->set_active(true);
136 for(unsigned j=0; j<100; ++j)
137 i->second->tick(100*Time::msec);
138 disabled_hosts.erase(i++);
140 else if(i->second->get_activity()<del_limit)
142 resolver->cancel(i->second);
144 disabled_hosts.erase(i++);
150 if(hosts.size()>max_visible_hosts)
152 list<float> activity;
153 for(map<Address, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
154 activity.push_back(i->second->get_activity());
157 list<float>::iterator j = activity.begin();
158 advance(j, activity.size()-max_visible_hosts);
161 for(map<Address, Host *>::iterator i=hosts.begin(); i!=hosts.end();)
163 if(i->second->get_activity()<limit)
165 i->second->set_active(false);
166 disabled_hosts.insert(*i);
174 for(map<unsigned, Port *>::iterator i=ports.begin(); i!=ports.end();)
178 if(!i->second->is_registered() && i->second->get_activity()<0.1)
187 for(list<Packet *>::iterator i=packets.begin(); i!=packets.end();)
190 if((*i)->get_stale())
193 i = packets.erase(i);
205 void NetVis::render()
207 GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT);
209 GL::MatrixStack::projection() = GL::Matrix::ortho_centered(1024, 768);
210 GL::MatrixStack::modelview() = GL::Matrix();
212 for(map<Address, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
215 GL::Immediate imm((GL::COLOR4_UBYTE, GL::VERTEX2));
216 imm.begin(GL::QUADS);
217 for(list<Packet *>::iterator i=packets.begin(); i!=packets.end(); ++i)
222 GL::MatrixStack::modelview() = GL::Matrix::translation(-500, 360, 0);
224 for(map<unsigned, Port *>::iterator i=ports.begin(); (i!=ports.end() && n<50); ++i)
226 float act = i->second->get_activity();
227 if((i->second->is_registered() && act>1) || act>200)
230 GL::MatrixStack::modelview() *= GL::Matrix::translation(0, -12, 0);
235 GL::MatrixStack::modelview() = GL::Matrix::translation(-500, -348, 0);
236 GL::MatrixStack::modelview() *= GL::Matrix::scaling(10);
237 font->draw_string(format("%d hosts", hosts.size()+disabled_hosts.size()));
238 GL::MatrixStack::modelview() *= GL::Matrix::translation(0, -1.2, 0);
239 font->draw_string(format("%d ports", ports.size()));
240 GL::MatrixStack::modelview() *= GL::Matrix::translation(0, -1.2, 0);
241 font->draw_string(format("%.2f fps", fps));
242 GL::Texture::unbind();
244 GL::MatrixStack::modelview() = GL::Matrix::translation(170, -370, 0);
248 Host &NetVis::get_host(const Address &a)
250 map<Address, Host *>::iterator i = hosts.find(a);
254 i = disabled_hosts.find(a);
255 if(i!=disabled_hosts.end())
258 Host *host = new Host(*this, a);
259 if(localnet.masked_match(a))
260 host->set_local(true);
261 resolver->push(host);
262 host->set_position(Vector2(rand()*400.0/RAND_MAX-200.0, rand()*400.0/RAND_MAX-200.0));
263 for(unsigned j=0; j<100; ++j)
264 host->tick(100*Time::msec);
269 Port &NetVis::get_port(unsigned number)
271 map<unsigned, Port *>::iterator i = ports.find(number);
274 Port *port = new Port(*this, number);
275 ports[number] = port;
279 void NetVis::capture_handler(unsigned char *user, const pcap_pkthdr *cap, const unsigned char *data)
281 NetVis *self = reinterpret_cast<NetVis *>(user);
285 const ethhdr *eth = reinterpret_cast<const ethhdr *>(data);
286 self->handle_ethernet(ctx, eth, cap->caplen);
289 void NetVis::handle_ethernet(CaptureContext &ctx, const ethhdr *eth, unsigned len)
291 ctx.size = ctx.cap_hdr->len-sizeof(ethhdr);
293 int proto = ntohs(eth->h_proto);
296 const iphdr *ip = reinterpret_cast<const iphdr *>(eth+1);
297 handle_ipv4(ctx, ip, len-sizeof(ethhdr));
300 IO::print("Unknown protocol in eth: %d\n", proto);
303 void NetVis::handle_ipv4(CaptureContext &ctx, const iphdr *ip, unsigned len)
305 ctx.src_host = &get_host(ntohl(ip->saddr));
306 if((ntohl(ip->daddr)&0xFF)!=0xFF)
307 ctx.dst_host = &get_host(ntohl(ip->daddr));
309 if(ip->protocol==IPPROTO_TCP)
311 const tcphdr *tcp = reinterpret_cast<const tcphdr *>(ip+1);
312 handle_tcp(ctx, tcp, len-sizeof(iphdr));
314 else if(ip->protocol==IPPROTO_UDP)
316 const udphdr *udp = reinterpret_cast<const udphdr *>(ip+1);
317 handle_udp(ctx, udp, len-sizeof(iphdr));
320 IO::print("Unknown protocol in ip: %d\n", ip->protocol);
324 void NetVis::handle_tcp(CaptureContext &ctx, const tcphdr *tcp, unsigned)
326 ctx.src_port = &get_port(ntohs(tcp->source));
327 ctx.dst_port = &get_port(ntohs(tcp->dest));
331 void NetVis::handle_udp(CaptureContext &ctx, const udphdr *udp, unsigned)
333 ctx.src_port = &get_port(ntohs(udp->source));
334 ctx.dst_port = &get_port(ntohs(udp->dest));
338 void NetVis::handle_packet(CaptureContext &ctx)
341 if(ctx.src_port && ctx.dst_port)
343 if(ctx.src_port->is_registered()!=ctx.dst_port->is_registered())
345 if(ctx.src_port->is_registered())
350 else if(ctx.src_port->get_number()<ctx.dst_port->get_number())
358 float throttle = ctx.src_host->send_packet();
361 packets.push_back(new Packet(*ctx.src_host, ctx.dst_host, port->get_color(), ctx.size));
362 packets.back()->tick(-throttle*Msp::Time::sec);
365 ctx.src_host->add_activity(ctx.size);
367 ctx.dst_host->add_activity(ctx.size);
370 ctx.src_port->add_activity(ctx.size);
372 ctx.dst_port->add_activity(ctx.size);
374 bool local_src = ctx.src_host->is_local();
375 bool local_dst = (ctx.dst_host && ctx.dst_host->is_local());
376 if(local_src && !local_dst)
377 history->activity(0, ctx.size);
378 else if(local_dst && !local_src)
379 history->activity(ctx.size, 0);
382 void NetVis::sighandler(int)
388 NetVis::CaptureContext::CaptureContext():