]> git.tdb.fi Git - netvis.git/blob - source/netvis.cpp
Adapt to MSP library changes
[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/getopt.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/format.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 usage_error("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 runtime_error(err);
56
57         if(pcap_setnonblock(pcap, true, err)==-1)
58                 throw runtime_error(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::Blend::alpha().bind();
72
73         font = new GL::Font;
74         DataFile::load(*font, "dejavu-10.font");
75
76         history = new History(*this, 301, 100);
77
78         catch_signal(SIGINT);
79 }
80
81 NetVis::~NetVis()
82 {
83         delete history;
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*static_cast<int>(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::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT);
210
211         GL::MatrixStack::projection() = GL::Matrix::ortho_centered(1024, 768);
212         GL::MatrixStack::modelview() = GL::Matrix();
213
214         for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
215                 i->second->render();
216         {
217                 GL::Immediate imm((GL::COLOR4_UBYTE, GL::VERTEX2));
218                 imm.begin(GL::QUADS);
219                 for(list<Packet *>::iterator i=packets.begin(); i!=packets.end(); ++i)
220                         (*i)->render(imm);
221                 imm.end();
222         }
223
224         GL::MatrixStack::modelview() = GL::Matrix::translation(-500, 360, 0);
225         unsigned n = 0;
226         for(map<unsigned, Port *>::iterator i=ports.begin(); (i!=ports.end() && n<50); ++i)
227         {
228                 float act = i->second->get_activity();
229                 if((i->second->is_registered() && act>1) || act>200)
230                 {
231                         i->second->render();
232                         GL::MatrixStack::modelview() *= GL::Matrix::translation(0, -12, 0);
233                         ++n;
234                 }
235         }
236
237         GL::MatrixStack::modelview() = GL::Matrix::translation(-500, -348, 0);
238         GL::MatrixStack::modelview() *= GL::Matrix::scaling(10);
239         font->draw_string(format("%d hosts", hosts.size()+disabled_hosts.size()));
240         GL::MatrixStack::modelview() *= GL::Matrix::translation(0, -1.2, 0);
241         font->draw_string(format("%d ports", ports.size()));
242         GL::MatrixStack::modelview() *= GL::Matrix::translation(0, -1.2, 0);
243         font->draw_string(format("%.2f fps", fps));
244         GL::Texture::unbind();
245
246         GL::MatrixStack::modelview() = GL::Matrix::translation(170, -370, 0);
247         history->render();
248 }
249
250 Host &NetVis::get_host(unsigned a)
251 {
252         map<unsigned, Host *>::iterator i = hosts.find(a);
253         if(i!=hosts.end())
254                 return *i->second;
255
256         i = disabled_hosts.find(a);
257         if(i!=disabled_hosts.end())
258                 return *i->second;
259
260         Host *host = new Host(*this, a);
261         if((a&localnet_mask)==localnet)
262                 host->set_local(true);
263         resolver->push(host);
264         host->set_position(Vector2(rand()*400.0/RAND_MAX-200.0, rand()*400.0/RAND_MAX-200.0));
265         for(unsigned j=0; j<100; ++j)
266                 host->tick(100*Time::msec);
267         hosts[a] = host;
268         return *host;
269 }
270
271 Port &NetVis::get_port(unsigned number)
272 {
273         map<unsigned, Port *>::iterator i = ports.find(number);
274         if(i!=ports.end())
275                 return *i->second;
276         Port *port = new Port(*this, number);
277         ports[number] = port;
278         return *port;
279 }
280
281 void NetVis::capture_handler(unsigned char *user, const pcap_pkthdr *, const unsigned char *data)
282 {
283         NetVis *self = reinterpret_cast<NetVis *>(user);
284
285         const ethhdr *eth = reinterpret_cast<const ethhdr *>(data);
286         if(ntohs(eth->h_proto)==ETH_P_IP)
287         {
288                 const iphdr *ip = reinterpret_cast<const iphdr *>(eth+1);
289
290                 unsigned size = ntohs(ip->tot_len);
291
292                 Port *sport = 0;
293                 Port *dport = 0;
294                 if(ip->protocol==IPPROTO_TCP)
295                 {
296                         const tcphdr *tcp = reinterpret_cast<const tcphdr *>(ip+1);
297                         sport = &self->get_port(ntohs(tcp->source));
298                         dport = &self->get_port(ntohs(tcp->dest));
299                 }
300                 else if(ip->protocol==IPPROTO_UDP)
301                 {
302                         const udphdr *udp = reinterpret_cast<const udphdr *>(ip+1);
303                         sport = &self->get_port(ntohs(udp->source));
304                         dport = &self->get_port(ntohs(udp->dest));
305                 }
306
307                 Port *port = 0;
308                 if(sport && dport)
309                 {
310                         if(sport->is_registered()!=dport->is_registered())
311                         {
312                                 if(sport->is_registered())
313                                         port = sport;
314                                 else
315                                         port = dport;
316                         }
317                         else if(sport->get_number()<dport->get_number())
318                                 port = sport;
319                         else
320                                 port = dport;
321                 }
322                 else
323                         port = &self->get_port(0);
324
325                 Host &shost = self->get_host(ntohl(ip->saddr));
326                 Host *dhost = 0;
327                 if((ntohl(ip->daddr)&0xFF)!=0xFF)
328                         dhost = &self->get_host(ntohl(ip->daddr));
329
330                 float throttle = shost.send_packet();
331                 if(throttle<1)
332                 {
333                         self->packets.push_back(new Packet(shost, dhost, port->get_color(), size));
334                         self->packets.back()->tick(-throttle*Msp::Time::sec);
335                 }
336
337                 shost.add_activity(size);
338                 if(dhost)
339                         dhost->add_activity(size);
340
341                 if(sport)
342                         sport->add_activity(size);
343                 if(dport)
344                         dport->add_activity(size);
345
346                 if((ntohl(ip->saddr)&self->localnet_mask)==self->localnet)
347                         self->history->activity(0, size);
348                 else if((ntohl(ip->daddr)&self->localnet_mask)==self->localnet)
349                         self->history->activity(size, 0);
350         }
351 }
352
353 void NetVis::sighandler(int)
354 {
355         exit(0);
356 }