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