]> git.tdb.fi Git - netvis.git/blob - source/netvis.cpp
e49c28ebd9ee798127c15b27183d0979fa1fa042
[netvis.git] / source / netvis.cpp
1 /* $Id$
2
3 This file is part of NetVis
4 Copyright @ 2008 Mikko Rasa, Mikkosoft Productions
5 Distributed unter the GPL
6 */
7
8 #include <iostream>
9 #include <signal.h>
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>
25 #include "host.h"
26 #include "netvis.h"
27 #include "packet.h"
28 #include "resolver.h"
29
30 using namespace std;
31 using namespace Msp;
32
33 NetVis::NetVis(int argc, char **argv):
34         draw_labels(true),
35         blend(true),
36         frames(0)
37 {
38         if(argc<2)
39                 throw UsageError("No interface given");
40         iface=argv[1];
41 }
42
43 int NetVis::main()
44 {
45         char err[1024];
46         pcap=pcap_open_live(iface.c_str(), 128, true, 0, err);
47         if(!pcap)
48                 throw Exception(err);
49
50         if(pcap_setnonblock(pcap, true, err)==-1)
51                 throw Exception(err);
52
53         pcap_lookupnet(iface.c_str(), &localnet, &localnet_mask, err);
54         localnet=ntohl(localnet);
55         localnet_mask=ntohl(localnet_mask);
56
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));
63         wnd->show();
64
65         GL::enable(GL_BLEND);
66         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
67
68         font=new GL::Font;
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);
73
74         catch_signal(SIGINT);
75
76         resolver=new Resolver;
77
78         //set_loop_mode(TICK_BUSY);
79
80         Application::main();
81
82         delete resolver;
83
84         delete glc;
85         delete wnd;
86         delete dpy;
87
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';
92
93         return exit_code;
94 }
95
96 void NetVis::tick()
97 {
98         Msp::Time::TimeStamp t=Msp::Time::now();
99         Msp::Time::TimeDelta dt;
100         if(tick_t)
101                 dt=t-tick_t;
102         tick_t=t;
103
104         if(tick_t>fps_t+Msp::Time::sec)
105         {
106                 fps=frames/((tick_t-fps_t)/Msp::Time::sec);
107                 fps_t=tick_t;
108                 frames=0;
109         }
110
111         dpy->tick();
112
113         {
114                 Debug::ProfilingScope s(profiler, "capture");
115                 while(pcap_dispatch(pcap, -1, &capture_handler, reinterpret_cast<unsigned char *>(this))>0);
116         }
117
118         {
119                 Debug::ProfilingScope s(profiler, "tick");
120
121                 resolver->tick();
122
123                 float min_activity=1e7f;
124                 for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
125                 {
126                         i->second->tick(dt);
127                         min_activity=min(min_activity, i->second->get_activity());
128                 }
129                 for(map<unsigned, Host *>::iterator i=disabled_hosts.begin(); i!=disabled_hosts.end(); ++i)
130                         i->second->tick(dt);
131
132                 for(map<unsigned, Host *>::iterator i=disabled_hosts.begin(); i!=disabled_hosts.end();)
133                 {
134                         if(i->second->get_activity()>min_activity)
135                         {
136                                 i->second->set_active(true);
137                                 hosts.insert(*i);
138                                 disabled_hosts.erase(i++);
139                         }
140                         else
141                                 ++i;
142                 }
143                 if(hosts.size()>20)
144                 {
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());
148                         activity.sort();
149
150                         list<float>::iterator j=activity.begin();
151                         advance(j, activity.size()-20);
152                         float limit=*j;
153
154                         for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end();)
155                         {
156                                 if(i->second->get_activity()<limit)
157                                 {
158                                         i->second->set_active(false);
159                                         disabled_hosts.insert(*i);
160                                         hosts.erase(i++);
161                                 }
162                                 else
163                                         ++i;
164                         }
165                 }
166
167                 for(list<Packet *>::iterator i=packets.begin(); i!=packets.end();)
168                 {
169                         (*i)->tick(dt);
170                         if((*i)->get_stale())
171                         {
172                                 delete *i;
173                                 i=packets.erase(i);
174                         }
175                         else
176                                 ++i;
177                 }
178         }
179
180         {
181                 Debug::ProfilingScope s(profiler, "render");
182                 glClear(GL_COLOR_BUFFER_BIT);
183
184                 GL::matrix_mode(GL::PROJECTION);
185                 GL::load_identity();
186                 GL::ortho_centered(1024, 768);
187                 GL::matrix_mode(GL::MODELVIEW);
188                 GL::load_identity();
189
190                 for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
191                         i->second->render();
192                 if(draw_labels)
193                 {
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();
198                 }
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)
202                         (*i)->render(imm);
203                 imm.end();
204
205                 GL::push_matrix();
206                 GL::translate(-500, 360, 0);
207                 unsigned n=0;
208                 for(map<unsigned, GL::Color>::iterator i=port_colors.begin(); (i!=port_colors.end() && n<20); ++i, ++n)
209                 {
210                         GL::Color &color=i->second;
211
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)
215                         {
216                                 imm.vertex(x+0, 0);
217                                 imm.vertex(x+10, 0);
218                                 imm.vertex(x+10, 10);
219                                 imm.vertex(x+0, 10);
220                         }
221                         imm.end();
222
223                         GL::translate(0, -12, 0);
224                 }
225                 GL::pop_matrix();
226
227                 GL::push_matrix();
228                 if(draw_labels)
229                 {
230                         GL::push_matrix();
231                         GL::translate(-484, 361, 0);
232                         GL::scale_uniform(10);
233                         glColor4f(1.0, 1.0, 1.0, 1.0);
234                         n=0;
235                         for(map<unsigned, GL::Color>::iterator i=port_colors.begin(); (i!=port_colors.end() && n<20); ++i, ++n)
236                         {
237                                 font->draw_string(format("%d", i->first));
238
239                                 GL::translate(0, -1.2, 0);
240                         }
241                         GL::pop_matrix();
242                         GL::Texture::unbind();
243                 }
244                 GL::pop_matrix();
245
246                 GL::push_matrix();
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));
252                 GL::pop_matrix();
253                 GL::Texture::unbind();
254
255                 glc->swap_buffers();
256         }
257
258         ++frames;
259 }
260
261 Host &NetVis::get_host(unsigned a)
262 {
263         map<unsigned, Host *>::iterator i=hosts.find(a);
264         if(i!=hosts.end())
265                 return *i->second;
266
267         i=disabled_hosts.find(a);
268         if(i!=disabled_hosts.end())
269                 return *i->second;
270
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));
276         hosts[a]=host;
277         return *host;
278 }
279
280 GL::Color &NetVis::get_port_color(unsigned port)
281 {
282         map<unsigned, GL::Color>::iterator i=port_colors.find(port);
283         if(i!=port_colors.end())
284                 return i->second;
285
286         GL::Color color;
287         while(1)
288         {
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)
293                         break;
294         }
295         color.a=0.4f;
296         return port_colors[port]=color;
297 }
298
299 void NetVis::key_press(unsigned key, unsigned, wchar_t)
300 {
301         if(key==46)
302                 draw_labels=!draw_labels;
303         else if(key==56)
304         {
305                 blend=!blend;
306                 GL::set(GL_BLEND, blend);
307         }
308 }
309
310 void NetVis::capture_handler(unsigned char *user, const pcap_pkthdr *, const unsigned char *data)
311 {
312         NetVis *self=reinterpret_cast<NetVis *>(user);
313
314         const ethhdr *eth=reinterpret_cast<const ethhdr *>(data);
315         if(ntohs(eth->h_proto)==ETH_P_IP)
316         {
317                 const iphdr *ip=reinterpret_cast<const iphdr *>(eth+1);
318
319                 unsigned size=ntohs(ip->tot_len);
320                 unsigned port=0;
321                 if(ip->protocol==IPPROTO_TCP)
322                 {
323                         const tcphdr *tcp=reinterpret_cast<const tcphdr *>(ip+1);
324                         port=min(ntohs(tcp->source), ntohs(tcp->dest));
325                 }
326                 else if(ip->protocol==IPPROTO_UDP)
327                 {
328                         const udphdr *udp=reinterpret_cast<const udphdr *>(ip+1);
329                         port=min(ntohs(udp->source), ntohs(udp->dest));
330                 }
331                 Host &shost=self->get_host(ntohl(ip->saddr));
332                 Host *dhost=0;
333                 if((ntohl(ip->daddr)&0xFF)!=0xFF)
334                         dhost=&self->get_host(ntohl(ip->daddr));
335
336                 float throttle=shost.send_packet();
337                 if(throttle<1)
338                 {
339                         self->packets.push_back(new Packet(shost, dhost, self->get_port_color(port), size));
340                         self->packets.back()->tick(-throttle*Msp::Time::sec);
341                 }
342
343                 shost.add_activity(size);
344                 if(dhost)
345                         dhost->add_activity(size);
346         }
347 }
348
349 void NetVis::sighandler(int)
350 {
351         exit(0);
352 }
353
354 Application::RegApp<NetVis> NetVis::reg;