]> git.tdb.fi Git - netvis.git/blob - source/netvis.cpp
83dbd1a78d53933bef55d1948fef43dc9024d3e0
[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/ether.h>
13 #include <netinet/ip.h>
14 #include <netinet/tcp.h>
15 #include <netinet/udp.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 "history.h"
30 #include "host.h"
31 #include "netvis.h"
32 #include "packet.h"
33 #include "port.h"
34 #include "resolver.h"
35
36 using namespace std;
37 using namespace Msp;
38
39 NetVis::NetVis(int argc, char **argv):
40         pcap(0),
41         resolver(0),
42         wnd(0),
43         font(0),
44         max_hosts(1000),
45         max_visible_hosts(30),
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->show();
70
71         GL::enable(GL::BLEND);
72         GL::blend_func(GL::SRC_ALPHA, GL::ONE_MINUS_SRC_ALPHA);
73
74         font = new GL::Font;
75         DataFile::load(*font, "dejavu-10.font");
76
77         history = new History(*this, 301, 100);
78
79         catch_signal(SIGINT);
80 }
81
82 NetVis::~NetVis()
83 {
84         delete resolver;
85
86         delete font;
87         delete wnd;
88
89         pcap_close(pcap);
90         for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
91                 delete i->second;
92         for(map<unsigned, Host *>::iterator i=disabled_hosts.begin(); i!=disabled_hosts.end(); ++i)
93                 delete i->second;
94         for(map<unsigned, Port *>::iterator i=ports.begin(); i!=ports.end(); ++i)
95                 delete i->second;
96         for(list<Packet *>::iterator i=packets.begin(); i!=packets.end(); ++i)
97                 delete *i;
98 }
99
100 void NetVis::tick()
101 {
102         Msp::Time::TimeStamp t = Msp::Time::now();
103         Msp::Time::TimeDelta dt;
104         if(tick_t)
105                 dt = t-tick_t;
106         tick_t = t;
107
108         if(tick_t>fps_t+Msp::Time::sec)
109         {
110                 fps = frames/((tick_t-fps_t)/Msp::Time::sec);
111                 fps_t = tick_t;
112                 frames = 0;
113         }
114
115         wnd->get_display().tick();
116
117         while(pcap_dispatch(pcap, -1, &capture_handler, reinterpret_cast<unsigned char *>(this))>0) ;
118
119         resolver->tick();
120         history->tick(tick_t);
121
122
123         float min_activity = numeric_limits<float>::max();
124         for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
125         {
126                 i->second->tick(dt);
127                 min_activity = min(min_activity, i->second->get_activity());
128         }
129         float del_limit = pow(10, 6-0.1*(max_hosts-hosts.size()-disabled_hosts.size()));
130         for(map<unsigned, Host *>::iterator i=disabled_hosts.begin(); i!=disabled_hosts.end();)
131         {
132                 i->second->tick(dt);
133
134                 if(i->second->get_activity()>min_activity)
135                 {
136                         i->second->set_active(true);
137                         hosts.insert(*i);
138                         for(unsigned j=0; j<100; ++j)
139                                 i->second->tick(100*Time::msec);
140                         disabled_hosts.erase(i++);
141                 }
142                 else if(i->second->get_activity()<del_limit)
143                 {
144                         resolver->cancel(i->second);
145                         delete i->second;
146                         disabled_hosts.erase(i++);
147                 }
148                 else
149                         ++i;
150         }
151
152         if(hosts.size()>max_visible_hosts)
153         {
154                 list<float> activity;
155                 for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
156                         activity.push_back(i->second->get_activity());
157                 activity.sort();
158
159                 list<float>::iterator j = activity.begin();
160                 advance(j, activity.size()-max_visible_hosts);
161                 float limit = *j;
162
163                 for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end();)
164                 {
165                         if(i->second->get_activity()<limit)
166                         {
167                                 i->second->set_active(false);
168                                 disabled_hosts.insert(*i);
169                                 hosts.erase(i++);
170                         }
171                         else
172                                 ++i;
173                 }
174         }
175
176         for(map<unsigned, Port *>::iterator i=ports.begin(); i!=ports.end();)
177         {
178                 i->second->tick(dt);
179
180                 if(!i->second->is_registered() && i->second->get_activity()<0.1)
181                 {
182                         delete i->second;
183                         ports.erase(i++);
184                 }
185                 else
186                         ++i;
187         }
188
189         for(list<Packet *>::iterator i=packets.begin(); i!=packets.end();)
190         {
191                 (*i)->tick(dt);
192                 if((*i)->get_stale())
193                 {
194                         delete *i;
195                         i = packets.erase(i);
196                 }
197                 else
198                         ++i;
199         }
200
201         render();
202         wnd->swap_buffers();
203
204         ++frames;
205 }
206
207 void NetVis::render()
208 {
209         GL::clear(GL::COLOR_BUFFER_BIT);
210
211         GL::matrix_mode(GL::PROJECTION);
212         GL::load_identity();
213         GL::ortho_centered(1024, 768);
214         GL::matrix_mode(GL::MODELVIEW);
215         GL::load_identity();
216
217         for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
218                 i->second->render();
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
227         GL::push_matrix();
228         GL::translate(-500, 360, 0);
229         unsigned n = 0;
230         for(map<unsigned, Port *>::iterator i=ports.begin(); (i!=ports.end() && n<50); ++i)
231         {
232                 float act = i->second->get_activity();
233                 if((i->second->is_registered() && act>1) || act>200)
234                 {
235                         i->second->render();
236                         GL::translate(0, -12, 0);
237                         ++n;
238                 }
239         }
240         GL::pop_matrix();
241
242         GL::push_matrix();
243         GL::translate(-500, -348, 0);
244         GL::scale_uniform(10);
245         font->draw_string(format("%d hosts", hosts.size()+disabled_hosts.size()));
246         GL::translate(0, -1.2, 0);
247         font->draw_string(format("%d ports", ports.size()));
248         GL::translate(0, -1.2, 0);
249         font->draw_string(format("%.2f fps", fps));
250         GL::Texture::unbind();
251         GL::pop_matrix();
252
253         GL::push_matrix();
254         GL::translate(170, -370, 0);
255         history->render();
256         GL::pop_matrix();
257 }
258
259 Host &NetVis::get_host(unsigned a)
260 {
261         map<unsigned, Host *>::iterator i = hosts.find(a);
262         if(i!=hosts.end())
263                 return *i->second;
264
265         i = disabled_hosts.find(a);
266         if(i!=disabled_hosts.end())
267                 return *i->second;
268
269         Host *host = new Host(*this, a);
270         if((a&localnet_mask)==localnet)
271                 host->set_local(true);
272         resolver->push(host);
273         host->set_position(Vector2(rand()*400.0/RAND_MAX-200.0, rand()*400.0/RAND_MAX-200.0));
274         for(unsigned j=0; j<100; ++j)
275                 host->tick(100*Time::msec);
276         hosts[a] = host;
277         return *host;
278 }
279
280 Port &NetVis::get_port(unsigned number)
281 {
282         map<unsigned, Port *>::iterator i = ports.find(number);
283         if(i!=ports.end())
284                 return *i->second;
285         Port *port = new Port(*this, number);
286         ports[number] = port;
287         return *port;
288 }
289
290 void NetVis::capture_handler(unsigned char *user, const pcap_pkthdr *, const unsigned char *data)
291 {
292         NetVis *self = reinterpret_cast<NetVis *>(user);
293
294         const ethhdr *eth = reinterpret_cast<const ethhdr *>(data);
295         if(ntohs(eth->h_proto)==ETH_P_IP)
296         {
297                 const iphdr *ip = reinterpret_cast<const iphdr *>(eth+1);
298
299                 unsigned size = ntohs(ip->tot_len);
300
301                 Port *sport = 0;
302                 Port *dport = 0;
303                 if(ip->protocol==IPPROTO_TCP)
304                 {
305                         const tcphdr *tcp = reinterpret_cast<const tcphdr *>(ip+1);
306                         sport = &self->get_port(ntohs(tcp->source));
307                         dport = &self->get_port(ntohs(tcp->dest));
308                 }
309                 else if(ip->protocol==IPPROTO_UDP)
310                 {
311                         const udphdr *udp = reinterpret_cast<const udphdr *>(ip+1);
312                         sport = &self->get_port(ntohs(udp->source));
313                         dport = &self->get_port(ntohs(udp->dest));
314                 }
315
316                 Port *port = 0;
317                 if(sport && dport)
318                 {
319                         if(sport->is_registered()!=dport->is_registered())
320                         {
321                                 if(sport->is_registered())
322                                         port = sport;
323                                 else
324                                         port = dport;
325                         }
326                         else if(sport->get_number()<dport->get_number())
327                                 port = sport;
328                         else
329                                 port = dport;
330                 }
331                 else
332                         port = &self->get_port(0);
333
334                 Host &shost = self->get_host(ntohl(ip->saddr));
335                 Host *dhost = 0;
336                 if((ntohl(ip->daddr)&0xFF)!=0xFF)
337                         dhost = &self->get_host(ntohl(ip->daddr));
338
339                 float throttle = shost.send_packet();
340                 if(throttle<1)
341                 {
342                         self->packets.push_back(new Packet(shost, dhost, port->get_color(), size));
343                         self->packets.back()->tick(-throttle*Msp::Time::sec);
344                 }
345
346                 shost.add_activity(size);
347                 if(dhost)
348                         dhost->add_activity(size);
349
350                 if(sport)
351                         sport->add_activity(size);
352                 if(dport)
353                         dport->add_activity(size);
354
355                 if((ntohl(ip->saddr)&self->localnet_mask)==self->localnet)
356                         self->history->activity(0, size);
357                 else if((ntohl(ip->daddr)&self->localnet_mask)==self->localnet)
358                         self->history->activity(size, 0);
359         }
360 }
361
362 void NetVis::sighandler(int)
363 {
364         exit(0);
365 }
366
367 Application::RegApp<NetVis> NetVis::reg;