3 This file is part of NetVis
4 Copyright @ 2008 Mikko Rasa, Mikkosoft Productions
5 Distributed unter the GPL
10 #include <netinet/ip.h>
11 #include <netinet/tcp.h>
12 #include <netinet/udp.h>
13 #include <linux/if_ether.h>
14 #include <msp/core/except.h>
15 #include <msp/debug/profilingscope.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/strings/formatter.h>
23 #include <msp/time/units.h>
24 #include <msp/time/utils.h>
33 NetVis::NetVis(int argc, char **argv):
39 throw UsageError("No interface given");
46 pcap=pcap_open_live(iface.c_str(), 128, true, 0, err);
50 if(pcap_setnonblock(pcap, true, err)==-1)
53 pcap_lookupnet(iface.c_str(), &localnet, &localnet_mask, err);
54 localnet=ntohl(localnet);
55 localnet_mask=ntohl(localnet_mask);
57 dpy=new Graphics::Display;
58 wnd=new Graphics::Window(*dpy, 1024, 768);
59 glc=new Graphics::GLContext(*wnd);
60 wnd->set_title("NetVis");
61 wnd->signal_close.connect(sigc::bind(sigc::mem_fun(this, &NetVis::exit), 0));
62 wnd->signal_key_press.connect(sigc::mem_fun(this, &NetVis::key_press));
66 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
69 DataFile::load(*font, "dejavu-10.font");
70 font_tex=new GL::Texture2D;
71 DataFile::load(*font_tex, "dejavu-10.tex");
72 font->set_texture(*font_tex);
76 resolver=new Resolver;
78 //set_loop_mode(TICK_BUSY);
88 cout<<hosts.size()+disabled_hosts.size()<<" different hosts seen\n";
89 cout<<"capture: "<<profiler.scope("capture").total_time<<'\n';
90 cout<<"tick: "<<profiler.scope("tick").total_time<<'\n';
91 cout<<"render: "<<profiler.scope("render").total_time<<'\n';
98 Msp::Time::TimeStamp t=Msp::Time::now();
99 Msp::Time::TimeDelta dt;
104 if(tick_t>fps_t+Msp::Time::sec)
106 fps=frames/((tick_t-fps_t)/Msp::Time::sec);
114 Debug::ProfilingScope s(profiler, "capture");
115 while(pcap_dispatch(pcap, -1, &capture_handler, reinterpret_cast<unsigned char *>(this))>0);
119 Debug::ProfilingScope s(profiler, "tick");
123 float min_activity=1e7f;
124 for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
127 min_activity=min(min_activity, i->second->get_activity());
129 for(map<unsigned, Host *>::iterator i=disabled_hosts.begin(); i!=disabled_hosts.end(); ++i)
132 for(map<unsigned, Host *>::iterator i=disabled_hosts.begin(); i!=disabled_hosts.end();)
134 if(i->second->get_activity()>min_activity)
136 i->second->set_active(true);
138 disabled_hosts.erase(i++);
145 list<float> activity;
146 for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
147 activity.push_back(i->second->get_activity());
150 list<float>::iterator j=activity.begin();
151 advance(j, activity.size()-20);
154 for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end();)
156 if(i->second->get_activity()<limit)
158 i->second->set_active(false);
159 disabled_hosts.insert(*i);
167 for(list<Packet *>::iterator i=packets.begin(); i!=packets.end();)
170 if((*i)->get_stale())
181 Debug::ProfilingScope s(profiler, "render");
182 glClear(GL_COLOR_BUFFER_BIT);
184 GL::matrix_mode(GL::PROJECTION);
186 GL::ortho_centered(1024, 768);
187 GL::matrix_mode(GL::MODELVIEW);
190 for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
194 glColor4f(1.0, 1.0, 1.0, 1.0);
195 for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
196 i->second->render_label();
197 GL::Texture::unbind();
199 GL::Immediate imm((GL::COLOR4_UBYTE, GL::VERTEX2));
200 imm.begin(GL::QUADS);
201 for(list<Packet *>::iterator i=packets.begin(); i!=packets.end(); ++i)
206 GL::translate(-500, 360, 0);
208 for(map<unsigned, GL::Color>::iterator i=port_colors.begin(); (i!=port_colors.end() && n<20); ++i, ++n)
210 GL::Color &color=i->second;
212 imm.begin(GL::QUADS);
213 imm.color(color.r, color.g, color.b, color.a);
214 for(unsigned x=0; x<=4; x+=2)
218 imm.vertex(x+10, 10);
223 GL::translate(0, -12, 0);
231 GL::translate(-484, 361, 0);
232 GL::scale_uniform(10);
233 glColor4f(1.0, 1.0, 1.0, 1.0);
235 for(map<unsigned, GL::Color>::iterator i=port_colors.begin(); (i!=port_colors.end() && n<20); ++i, ++n)
237 font->draw_string(format("%d", i->first));
239 GL::translate(0, -1.2, 0);
242 GL::Texture::unbind();
247 GL::translate(-500, -360, 0);
248 GL::scale_uniform(10);
249 font->draw_string(format("%d hosts", hosts.size()+disabled_hosts.size()));
250 GL::translate(0, -1.2, 0);
251 font->draw_string(format("%.2f fps", fps));
253 GL::Texture::unbind();
261 Host &NetVis::get_host(unsigned a)
263 map<unsigned, Host *>::iterator i=hosts.find(a);
267 i=disabled_hosts.find(a);
268 if(i!=disabled_hosts.end())
271 Host *host=new Host(*this, a);
272 if((a&localnet_mask)==localnet)
273 host->set_local(true);
274 resolver->push(host);
275 host->set_position(Vector2(rand()*400.0/RAND_MAX-200.0, rand()*400.0/RAND_MAX-200.0));
280 GL::Color &NetVis::get_port_color(unsigned port)
282 map<unsigned, GL::Color>::iterator i=port_colors.find(port);
283 if(i!=port_colors.end())
289 color.r=rand()*1.0/RAND_MAX;
290 color.g=rand()*1.0/RAND_MAX;
291 color.b=rand()*1.0/RAND_MAX;
292 if(color.r>0.5 || color.g>0.5 || color.b>0.7)
296 return port_colors[port]=color;
299 void NetVis::key_press(unsigned key, unsigned, wchar_t)
302 draw_labels=!draw_labels;
306 GL::set(GL_BLEND, blend);
310 void NetVis::capture_handler(unsigned char *user, const pcap_pkthdr *, const unsigned char *data)
312 NetVis *self=reinterpret_cast<NetVis *>(user);
314 const ethhdr *eth=reinterpret_cast<const ethhdr *>(data);
315 if(ntohs(eth->h_proto)==ETH_P_IP)
317 const iphdr *ip=reinterpret_cast<const iphdr *>(eth+1);
319 unsigned size=ntohs(ip->tot_len);
321 if(ip->protocol==IPPROTO_TCP)
323 const tcphdr *tcp=reinterpret_cast<const tcphdr *>(ip+1);
324 port=min(ntohs(tcp->source), ntohs(tcp->dest));
326 else if(ip->protocol==IPPROTO_UDP)
328 const udphdr *udp=reinterpret_cast<const udphdr *>(ip+1);
329 port=min(ntohs(udp->source), ntohs(udp->dest));
331 Host &shost=self->get_host(ntohl(ip->saddr));
333 if((ntohl(ip->daddr)&0xFF)!=0xFF)
334 dhost=&self->get_host(ntohl(ip->daddr));
336 float throttle=shost.send_packet();
339 self->packets.push_back(new Packet(shost, dhost, self->get_port_color(port), size));
340 self->packets.back()->tick(-throttle*Msp::Time::sec);
343 shost.add_activity(size);
345 dhost->add_activity(size);
349 void NetVis::sighandler(int)
354 Application::RegApp<NetVis> NetVis::reg;