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