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