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