]> git.tdb.fi Git - netvis.git/blob - source/netvis.cpp
Free stuff on exit
[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 font;
85         delete font_tex;
86         delete glc;
87         delete wnd;
88         delete dpy;
89
90         cout<<hosts.size()+disabled_hosts.size()<<" different hosts seen\n";
91         cout<<"capture: "<<profiler.scope("capture").total_time<<'\n';
92         cout<<"tick:    "<<profiler.scope("tick").total_time<<'\n';
93         cout<<"render:  "<<profiler.scope("render").total_time<<'\n';
94
95         pcap_close(pcap);
96         for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
97                 delete i->second;
98         for(map<unsigned, Host *>::iterator i=disabled_hosts.begin(); i!=disabled_hosts.end(); ++i)
99                 delete i->second;
100         for(list<Packet *>::iterator i=packets.begin(); i!=packets.end(); ++i)
101                 delete *i;
102
103         return exit_code;
104 }
105
106 void NetVis::tick()
107 {
108         Msp::Time::TimeStamp t=Msp::Time::now();
109         Msp::Time::TimeDelta dt;
110         if(tick_t)
111                 dt=t-tick_t;
112         tick_t=t;
113
114         if(tick_t>fps_t+Msp::Time::sec)
115         {
116                 fps=frames/((tick_t-fps_t)/Msp::Time::sec);
117                 fps_t=tick_t;
118                 frames=0;
119         }
120
121         dpy->tick();
122
123         {
124                 Debug::ProfilingScope s(profiler, "capture");
125                 while(pcap_dispatch(pcap, -1, &capture_handler, reinterpret_cast<unsigned char *>(this))>0);
126         }
127
128         {
129                 Debug::ProfilingScope s(profiler, "tick");
130
131                 resolver->tick();
132
133                 float min_activity=1e7f;
134                 for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
135                 {
136                         i->second->tick(dt);
137                         min_activity=min(min_activity, i->second->get_activity());
138                 }
139                 for(map<unsigned, Host *>::iterator i=disabled_hosts.begin(); i!=disabled_hosts.end(); ++i)
140                         i->second->tick(dt);
141
142                 for(map<unsigned, Host *>::iterator i=disabled_hosts.begin(); i!=disabled_hosts.end();)
143                 {
144                         if(i->second->get_activity()>min_activity)
145                         {
146                                 i->second->set_active(true);
147                                 hosts.insert(*i);
148                                 disabled_hosts.erase(i++);
149                         }
150                         else
151                                 ++i;
152                 }
153                 if(hosts.size()>20)
154                 {
155                         list<float> activity;
156                         for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
157                                 activity.push_back(i->second->get_activity());
158                         activity.sort();
159
160                         list<float>::iterator j=activity.begin();
161                         advance(j, activity.size()-20);
162                         float limit=*j;
163
164                         for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end();)
165                         {
166                                 if(i->second->get_activity()<limit)
167                                 {
168                                         i->second->set_active(false);
169                                         disabled_hosts.insert(*i);
170                                         hosts.erase(i++);
171                                 }
172                                 else
173                                         ++i;
174                         }
175                 }
176
177                 for(list<Packet *>::iterator i=packets.begin(); i!=packets.end();)
178                 {
179                         (*i)->tick(dt);
180                         if((*i)->get_stale())
181                         {
182                                 delete *i;
183                                 i=packets.erase(i);
184                         }
185                         else
186                                 ++i;
187                 }
188         }
189
190         {
191                 Debug::ProfilingScope s(profiler, "render");
192                 glClear(GL_COLOR_BUFFER_BIT);
193
194                 GL::matrix_mode(GL::PROJECTION);
195                 GL::load_identity();
196                 GL::ortho_centered(1024, 768);
197                 GL::matrix_mode(GL::MODELVIEW);
198                 GL::load_identity();
199
200                 for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
201                         i->second->render();
202                 if(draw_labels)
203                 {
204                         glColor4f(1.0, 1.0, 1.0, 1.0);
205                         for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
206                                 i->second->render_label();
207                         GL::Texture::unbind();
208                 }
209                 GL::Immediate imm((GL::COLOR4_UBYTE, GL::VERTEX2));
210                 imm.begin(GL::QUADS);
211                 for(list<Packet *>::iterator i=packets.begin(); i!=packets.end(); ++i)
212                         (*i)->render(imm);
213                 imm.end();
214
215                 GL::push_matrix();
216                 GL::translate(-500, 360, 0);
217                 unsigned n=0;
218                 for(map<unsigned, GL::Color>::iterator i=port_colors.begin(); (i!=port_colors.end() && n<20); ++i, ++n)
219                 {
220                         GL::Color &color=i->second;
221
222                         imm.begin(GL::QUADS);
223                         imm.color(color.r, color.g, color.b, color.a);
224                         for(unsigned x=0; x<=4; x+=2)
225                         {
226                                 imm.vertex(x+0, 0);
227                                 imm.vertex(x+10, 0);
228                                 imm.vertex(x+10, 10);
229                                 imm.vertex(x+0, 10);
230                         }
231                         imm.end();
232
233                         GL::translate(0, -12, 0);
234                 }
235                 GL::pop_matrix();
236
237                 GL::push_matrix();
238                 if(draw_labels)
239                 {
240                         GL::push_matrix();
241                         GL::translate(-484, 361, 0);
242                         GL::scale_uniform(10);
243                         glColor4f(1.0, 1.0, 1.0, 1.0);
244                         n=0;
245                         for(map<unsigned, GL::Color>::iterator i=port_colors.begin(); (i!=port_colors.end() && n<20); ++i, ++n)
246                         {
247                                 font->draw_string(format("%d", i->first));
248
249                                 GL::translate(0, -1.2, 0);
250                         }
251                         GL::pop_matrix();
252                         GL::Texture::unbind();
253                 }
254                 GL::pop_matrix();
255
256                 GL::push_matrix();
257                 GL::translate(-500, -360, 0);
258                 GL::scale_uniform(10);
259                 font->draw_string(format("%d hosts", hosts.size()+disabled_hosts.size()));
260                 GL::translate(0, -1.2, 0);
261                 font->draw_string(format("%.2f fps", fps));
262                 GL::pop_matrix();
263                 GL::Texture::unbind();
264
265                 glc->swap_buffers();
266         }
267
268         ++frames;
269 }
270
271 Host &NetVis::get_host(unsigned a)
272 {
273         map<unsigned, Host *>::iterator i=hosts.find(a);
274         if(i!=hosts.end())
275                 return *i->second;
276
277         i=disabled_hosts.find(a);
278         if(i!=disabled_hosts.end())
279                 return *i->second;
280
281         Host *host=new Host(*this, a);
282         if((a&localnet_mask)==localnet)
283                 host->set_local(true);
284         resolver->push(host);
285         host->set_position(Vector2(rand()*400.0/RAND_MAX-200.0, rand()*400.0/RAND_MAX-200.0));
286         hosts[a]=host;
287         return *host;
288 }
289
290 GL::Color &NetVis::get_port_color(unsigned port)
291 {
292         map<unsigned, GL::Color>::iterator i=port_colors.find(port);
293         if(i!=port_colors.end())
294                 return i->second;
295
296         GL::Color color;
297         while(1)
298         {
299                 color.r=rand()*1.0/RAND_MAX;
300                 color.g=rand()*1.0/RAND_MAX;
301                 color.b=rand()*1.0/RAND_MAX;
302                 if(color.r>0.5 || color.g>0.5 || color.b>0.7)
303                         break;
304         }
305         color.a=0.4f;
306         return port_colors[port]=color;
307 }
308
309 void NetVis::key_press(unsigned key, unsigned, wchar_t)
310 {
311         if(key==46)
312                 draw_labels=!draw_labels;
313         else if(key==56)
314         {
315                 blend=!blend;
316                 GL::set(GL_BLEND, blend);
317         }
318 }
319
320 void NetVis::capture_handler(unsigned char *user, const pcap_pkthdr *, const unsigned char *data)
321 {
322         NetVis *self=reinterpret_cast<NetVis *>(user);
323
324         const ethhdr *eth=reinterpret_cast<const ethhdr *>(data);
325         if(ntohs(eth->h_proto)==ETH_P_IP)
326         {
327                 const iphdr *ip=reinterpret_cast<const iphdr *>(eth+1);
328
329                 unsigned size=ntohs(ip->tot_len);
330                 unsigned port=0;
331                 if(ip->protocol==IPPROTO_TCP)
332                 {
333                         const tcphdr *tcp=reinterpret_cast<const tcphdr *>(ip+1);
334                         port=min(ntohs(tcp->source), ntohs(tcp->dest));
335                 }
336                 else if(ip->protocol==IPPROTO_UDP)
337                 {
338                         const udphdr *udp=reinterpret_cast<const udphdr *>(ip+1);
339                         port=min(ntohs(udp->source), ntohs(udp->dest));
340                 }
341                 Host &shost=self->get_host(ntohl(ip->saddr));
342                 Host *dhost=0;
343                 if((ntohl(ip->daddr)&0xFF)!=0xFF)
344                         dhost=&self->get_host(ntohl(ip->daddr));
345
346                 float throttle=shost.send_packet();
347                 if(throttle<1)
348                 {
349                         self->packets.push_back(new Packet(shost, dhost, self->get_port_color(port), size));
350                         self->packets.back()->tick(-throttle*Msp::Time::sec);
351                 }
352
353                 shost.add_activity(size);
354                 if(dhost)
355                         dhost->add_activity(size);
356         }
357 }
358
359 void NetVis::sighandler(int)
360 {
361         exit(0);
362 }
363
364 Application::RegApp<NetVis> NetVis::reg;