3 This file is part of NetVis
4 Copyright @ 2008 Mikko Rasa, Mikkosoft Productions
5 Distributed unter the GPL
13 #include <netinet/ip.h>
14 #include <netinet/tcp.h>
15 #include <netinet/udp.h>
16 #include <linux/if_ether.h>
17 #include <msp/core/except.h>
18 #include <msp/debug/profilingscope.h>
19 #include <msp/gl/immediate.h>
20 #include <msp/gl/matrix.h>
21 #include <msp/gl/misc.h>
22 #include <msp/gl/projection.h>
23 #include <msp/gl/texture2d.h>
24 #include <msp/gl/transform.h>
25 #include <msp/strings/formatter.h>
26 #include <msp/time/units.h>
27 #include <msp/time/utils.h>
36 NetVis::NetVis(int argc, char **argv):
38 max_visible_hosts(30),
44 throw UsageError("No interface given");
51 pcap = pcap_open_live(iface.c_str(), 128, true, 0, err);
55 if(pcap_setnonblock(pcap, true, err)==-1)
58 pcap_lookupnet(iface.c_str(), &localnet, &localnet_mask, err);
59 localnet = ntohl(localnet);
60 localnet_mask = ntohl(localnet_mask);
62 dpy = new Graphics::Display;
63 wnd = new Graphics::Window(*dpy, 1024, 768);
64 glc = new Graphics::GLContext(*wnd);
65 wnd->set_title("NetVis");
66 wnd->signal_close.connect(sigc::bind(sigc::mem_fun(this, &NetVis::exit), 0));
67 wnd->signal_key_press.connect(sigc::mem_fun(this, &NetVis::key_press));
71 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
74 DataFile::load(*font, "dejavu-10.font");
78 resolver = new Resolver;
80 //set_loop_mode(TICK_BUSY);
91 cout<<hosts.size()+disabled_hosts.size()<<" different hosts seen\n";
92 cout<<"capture: "<<profiler.scope("capture").total_time<<'\n';
93 cout<<"tick: "<<profiler.scope("tick").total_time<<'\n';
94 cout<<"render: "<<profiler.scope("render").total_time<<'\n';
97 for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
99 for(map<unsigned, Host *>::iterator i=disabled_hosts.begin(); i!=disabled_hosts.end(); ++i)
101 for(list<Packet *>::iterator i=packets.begin(); i!=packets.end(); ++i)
109 Msp::Time::TimeStamp t = Msp::Time::now();
110 Msp::Time::TimeDelta dt;
115 if(tick_t>fps_t+Msp::Time::sec)
117 fps = frames/((tick_t-fps_t)/Msp::Time::sec);
125 Debug::ProfilingScope s(profiler, "capture");
126 while(pcap_dispatch(pcap, -1, &capture_handler, reinterpret_cast<unsigned char *>(this))>0) ;
130 Debug::ProfilingScope s(profiler, "tick");
134 float min_activity = numeric_limits<float>::max();
135 for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
138 min_activity = min(min_activity, i->second->get_activity());
140 float del_limit = pow(10, 6-0.1*(max_hosts-hosts.size()-disabled_hosts.size()));
141 for(map<unsigned, Host *>::iterator i=disabled_hosts.begin(); i!=disabled_hosts.end();)
145 if(i->second->get_activity()>min_activity)
147 i->second->set_active(true);
149 disabled_hosts.erase(i++);
151 else if(i->second->get_activity()<del_limit)
154 disabled_hosts.erase(i++);
160 if(hosts.size()>max_visible_hosts)
162 list<float> activity;
163 for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
164 activity.push_back(i->second->get_activity());
167 list<float>::iterator j = activity.begin();
168 advance(j, activity.size()-max_visible_hosts);
171 for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end();)
173 if(i->second->get_activity()<limit)
175 i->second->set_active(false);
176 disabled_hosts.insert(*i);
184 for(list<Packet *>::iterator i=packets.begin(); i!=packets.end();)
187 if((*i)->get_stale())
190 i = packets.erase(i);
198 Debug::ProfilingScope s(profiler, "render");
199 glClear(GL_COLOR_BUFFER_BIT);
201 GL::matrix_mode(GL::PROJECTION);
203 GL::ortho_centered(1024, 768);
204 GL::matrix_mode(GL::MODELVIEW);
207 for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
211 glColor4f(1.0, 1.0, 1.0, 1.0);
212 for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
213 i->second->render_label();
214 GL::Texture::unbind();
216 GL::Immediate imm((GL::COLOR4_UBYTE, GL::VERTEX2));
217 imm.begin(GL::QUADS);
218 for(list<Packet *>::iterator i=packets.begin(); i!=packets.end(); ++i)
223 GL::translate(-500, 360, 0);
225 for(map<unsigned, GL::Color>::iterator i=port_colors.begin(); (i!=port_colors.end() && n<20); ++i, ++n)
227 GL::Color &color = i->second;
229 imm.begin(GL::QUADS);
230 imm.color(color.r, color.g, color.b, color.a);
231 for(unsigned x=0; x<=4; x+=2)
235 imm.vertex(x+10, 10);
240 GL::translate(0, -12, 0);
248 GL::translate(-484, 361, 0);
249 GL::scale_uniform(10);
250 glColor4f(1.0, 1.0, 1.0, 1.0);
252 for(map<unsigned, GL::Color>::iterator i=port_colors.begin(); (i!=port_colors.end() && n<20); ++i, ++n)
254 font->draw_string(format("%d", i->first));
256 GL::translate(0, -1.2, 0);
259 GL::Texture::unbind();
264 GL::translate(-500, -360, 0);
265 GL::scale_uniform(10);
266 font->draw_string(format("%d hosts", hosts.size()+disabled_hosts.size()));
267 GL::translate(0, -1.2, 0);
268 font->draw_string(format("%.2f fps", fps));
270 GL::Texture::unbind();
278 Host &NetVis::get_host(unsigned a)
280 map<unsigned, Host *>::iterator i = hosts.find(a);
284 i = disabled_hosts.find(a);
285 if(i!=disabled_hosts.end())
288 Host *host = new Host(*this, a);
289 if((a&localnet_mask)==localnet)
290 host->set_local(true);
291 resolver->push(host);
292 host->set_position(Vector2(rand()*400.0/RAND_MAX-200.0, rand()*400.0/RAND_MAX-200.0));
297 GL::Color &NetVis::get_port_color(unsigned port)
299 map<unsigned, GL::Color>::iterator i = port_colors.find(port);
300 if(i!=port_colors.end())
306 color.r = rand()*1.0/RAND_MAX;
307 color.g = rand()*1.0/RAND_MAX;
308 color.b = rand()*1.0/RAND_MAX;
309 if(color.r>0.5 || color.g>0.5 || color.b>0.7)
313 return port_colors[port] = color;
316 void NetVis::key_press(unsigned key, unsigned, wchar_t)
319 draw_labels = !draw_labels;
323 GL::set(GL_BLEND, blend);
327 void NetVis::capture_handler(unsigned char *user, const pcap_pkthdr *, const unsigned char *data)
329 NetVis *self = reinterpret_cast<NetVis *>(user);
331 const ethhdr *eth = reinterpret_cast<const ethhdr *>(data);
332 if(ntohs(eth->h_proto)==ETH_P_IP)
334 const iphdr *ip = reinterpret_cast<const iphdr *>(eth+1);
336 unsigned size = ntohs(ip->tot_len);
338 if(ip->protocol==IPPROTO_TCP)
340 const tcphdr *tcp = reinterpret_cast<const tcphdr *>(ip+1);
341 port = min(ntohs(tcp->source), ntohs(tcp->dest));
343 else if(ip->protocol==IPPROTO_UDP)
345 const udphdr *udp = reinterpret_cast<const udphdr *>(ip+1);
346 port = min(ntohs(udp->source), ntohs(udp->dest));
348 Host &shost = self->get_host(ntohl(ip->saddr));
350 if((ntohl(ip->daddr)&0xFF)!=0xFF)
351 dhost = &self->get_host(ntohl(ip->daddr));
353 float throttle = shost.send_packet();
356 self->packets.push_back(new Packet(shost, dhost, self->get_port_color(port), size));
357 self->packets.back()->tick(-throttle*Msp::Time::sec);
360 shost.add_activity(size);
362 dhost->add_activity(size);
366 void NetVis::sighandler(int)
371 Application::RegApp<NetVis> NetVis::reg;