]> git.tdb.fi Git - netvis.git/blob - source/netvis.cpp
Track and display the activity of ports
[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(list<Packet *>::iterator i=packets.begin(); i!=packets.end();)
177         {
178                 (*i)->tick(dt);
179                 if((*i)->get_stale())
180                 {
181                         delete *i;
182                         i = packets.erase(i);
183                 }
184                 else
185                         ++i;
186         }
187
188         render();
189         wnd->swap_buffers();
190
191         ++frames;
192 }
193
194 void NetVis::render()
195 {
196         GL::clear(GL::COLOR_BUFFER_BIT);
197
198         GL::matrix_mode(GL::PROJECTION);
199         GL::load_identity();
200         GL::ortho_centered(1024, 768);
201         GL::matrix_mode(GL::MODELVIEW);
202         GL::load_identity();
203
204         for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
205                 i->second->render();
206         {
207                 GL::Immediate imm((GL::COLOR4_UBYTE, GL::VERTEX2));
208                 imm.begin(GL::QUADS);
209                 for(list<Packet *>::iterator i=packets.begin(); i!=packets.end(); ++i)
210                         (*i)->render(imm);
211                 imm.end();
212         }
213
214         GL::push_matrix();
215         GL::translate(-500, 360, 0);
216         unsigned n = 0;
217         for(map<unsigned, Port *>::iterator i=ports.begin(); (i!=ports.end() && n<50); ++i)
218         {
219                 float act = i->second->get_activity();
220                 if((i->second->is_registered() && act>1) || act>200)
221                 {
222                         i->second->render();
223                         GL::translate(0, -12, 0);
224                         ++n;
225                 }
226         }
227         GL::pop_matrix();
228
229         GL::push_matrix();
230         GL::translate(-500, -360, 0);
231         GL::scale_uniform(10);
232         font->draw_string(format("%d hosts", hosts.size()+disabled_hosts.size()));
233         GL::translate(0, -1.2, 0);
234         font->draw_string(format("%.2f fps", fps));
235         GL::Texture::unbind();
236         GL::pop_matrix();
237
238         GL::push_matrix();
239         GL::translate(170, -370, 0);
240         history->render();
241         GL::pop_matrix();
242 }
243
244 Host &NetVis::get_host(unsigned a)
245 {
246         map<unsigned, Host *>::iterator i = hosts.find(a);
247         if(i!=hosts.end())
248                 return *i->second;
249
250         i = disabled_hosts.find(a);
251         if(i!=disabled_hosts.end())
252                 return *i->second;
253
254         Host *host = new Host(*this, a);
255         if((a&localnet_mask)==localnet)
256                 host->set_local(true);
257         resolver->push(host);
258         host->set_position(Vector2(rand()*400.0/RAND_MAX-200.0, rand()*400.0/RAND_MAX-200.0));
259         for(unsigned j=0; j<100; ++j)
260                 host->tick(100*Time::msec);
261         hosts[a] = host;
262         return *host;
263 }
264
265 Port &NetVis::get_port(unsigned number)
266 {
267         map<unsigned, Port *>::iterator i = ports.find(number);
268         if(i!=ports.end())
269                 return *i->second;
270         Port *port = new Port(*this, number);
271         ports[number] = port;
272         return *port;
273 }
274
275 void NetVis::capture_handler(unsigned char *user, const pcap_pkthdr *, const unsigned char *data)
276 {
277         NetVis *self = reinterpret_cast<NetVis *>(user);
278
279         const ethhdr *eth = reinterpret_cast<const ethhdr *>(data);
280         if(ntohs(eth->h_proto)==ETH_P_IP)
281         {
282                 const iphdr *ip = reinterpret_cast<const iphdr *>(eth+1);
283
284                 unsigned size = ntohs(ip->tot_len);
285
286                 Port *sport = 0;
287                 Port *dport = 0;
288                 if(ip->protocol==IPPROTO_TCP)
289                 {
290                         const tcphdr *tcp = reinterpret_cast<const tcphdr *>(ip+1);
291                         sport = &self->get_port(ntohs(tcp->source));
292                         dport = &self->get_port(ntohs(tcp->dest));
293                 }
294                 else if(ip->protocol==IPPROTO_UDP)
295                 {
296                         const udphdr *udp = reinterpret_cast<const udphdr *>(ip+1);
297                         sport = &self->get_port(ntohs(udp->source));
298                         dport = &self->get_port(ntohs(udp->dest));
299                 }
300
301                 Port *port = 0;
302                 if(sport && dport)
303                 {
304                         if(sport->is_registered()!=dport->is_registered())
305                         {
306                                 if(sport->is_registered())
307                                         port = sport;
308                                 else
309                                         port = dport;
310                         }
311                         else if(sport->get_number()<dport->get_number())
312                                 port = sport;
313                         else
314                                 port = dport;
315                 }
316                 else
317                         port = &self->get_port(0);
318
319                 Host &shost = self->get_host(ntohl(ip->saddr));
320                 Host *dhost = 0;
321                 if((ntohl(ip->daddr)&0xFF)!=0xFF)
322                         dhost = &self->get_host(ntohl(ip->daddr));
323
324                 float throttle = shost.send_packet();
325                 if(throttle<1)
326                 {
327                         self->packets.push_back(new Packet(shost, dhost, port->get_color(), size));
328                         self->packets.back()->tick(-throttle*Msp::Time::sec);
329                 }
330
331                 shost.add_activity(size);
332                 if(dhost)
333                         dhost->add_activity(size);
334
335                 if(sport)
336                         sport->add_activity(size);
337                 if(dport)
338                         dport->add_activity(size);
339
340                 if((ntohl(ip->saddr)&self->localnet_mask)==self->localnet)
341                         self->history->activity(0, size);
342                 else if((ntohl(ip->daddr)&self->localnet_mask)==self->localnet)
343                         self->history->activity(size, 0);
344         }
345 }
346
347 void NetVis::sighandler(int)
348 {
349         exit(0);
350 }
351
352 Application::RegApp<NetVis> NetVis::reg;