]> git.tdb.fi Git - netvis.git/blob - source/netvis.cpp
ff05507a2fc04364d31d1814037b9c247b1bdbbe
[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                         disabled_hosts.erase(i++);
139                 }
140                 else if(i->second->get_activity()<del_limit)
141                 {
142                         delete i->second;
143                         disabled_hosts.erase(i++);
144                 }
145                 else
146                         ++i;
147         }
148
149         if(hosts.size()>max_visible_hosts)
150         {
151                 list<float> activity;
152                 for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
153                         activity.push_back(i->second->get_activity());
154                 activity.sort();
155
156                 list<float>::iterator j = activity.begin();
157                 advance(j, activity.size()-max_visible_hosts);
158                 float limit = *j;
159
160                 for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end();)
161                 {
162                         if(i->second->get_activity()<limit)
163                         {
164                                 i->second->set_active(false);
165                                 disabled_hosts.insert(*i);
166                                 hosts.erase(i++);
167                         }
168                         else
169                                 ++i;
170                 }
171         }
172
173         for(list<Packet *>::iterator i=packets.begin(); i!=packets.end();)
174         {
175                 (*i)->tick(dt);
176                 if((*i)->get_stale())
177                 {
178                         delete *i;
179                         i = packets.erase(i);
180                 }
181                 else
182                         ++i;
183         }
184
185         render();
186         wnd->swap_buffers();
187
188         ++frames;
189 }
190
191 void NetVis::render()
192 {
193         GL::clear(GL::COLOR_BUFFER_BIT);
194
195         GL::matrix_mode(GL::PROJECTION);
196         GL::load_identity();
197         GL::ortho_centered(1024, 768);
198         GL::matrix_mode(GL::MODELVIEW);
199         GL::load_identity();
200
201         for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
202                 i->second->render();
203         {
204                 GL::Immediate imm((GL::COLOR4_UBYTE, GL::VERTEX2));
205                 imm.begin(GL::QUADS);
206                 for(list<Packet *>::iterator i=packets.begin(); i!=packets.end(); ++i)
207                         (*i)->render(imm);
208                 imm.end();
209         }
210
211         GL::push_matrix();
212         GL::translate(-500, 360, 0);
213         unsigned n = 0;
214         for(map<unsigned, Port *>::iterator i=ports.begin(); (i!=ports.end() && n<50); ++i, ++n)
215         {
216                 i->second->render();
217                 GL::translate(0, -12, 0);
218         }
219         GL::pop_matrix();
220
221         GL::push_matrix();
222         GL::translate(-500, -360, 0);
223         GL::scale_uniform(10);
224         font->draw_string(format("%d hosts", hosts.size()+disabled_hosts.size()));
225         GL::translate(0, -1.2, 0);
226         font->draw_string(format("%.2f fps", fps));
227         GL::Texture::unbind();
228         GL::pop_matrix();
229
230         GL::push_matrix();
231         GL::translate(170, -370, 0);
232         history->render();
233         GL::pop_matrix();
234 }
235
236 Host &NetVis::get_host(unsigned a)
237 {
238         map<unsigned, Host *>::iterator i = hosts.find(a);
239         if(i!=hosts.end())
240                 return *i->second;
241
242         i = disabled_hosts.find(a);
243         if(i!=disabled_hosts.end())
244                 return *i->second;
245
246         Host *host = new Host(*this, a);
247         if((a&localnet_mask)==localnet)
248                 host->set_local(true);
249         resolver->push(host);
250         host->set_position(Vector2(rand()*400.0/RAND_MAX-200.0, rand()*400.0/RAND_MAX-200.0));
251         hosts[a] = host;
252         return *host;
253 }
254
255 const Port &NetVis::get_port(unsigned number)
256 {
257         map<unsigned, Port *>::iterator i = ports.find(number);
258         if(i!=ports.end())
259                 return *i->second;
260         Port *port = new Port(*this, number);
261         ports[number] = port;
262         return *port;
263 }
264
265 void NetVis::capture_handler(unsigned char *user, const pcap_pkthdr *, const unsigned char *data)
266 {
267         NetVis *self = reinterpret_cast<NetVis *>(user);
268
269         const ethhdr *eth = reinterpret_cast<const ethhdr *>(data);
270         if(ntohs(eth->h_proto)==ETH_P_IP)
271         {
272                 const iphdr *ip = reinterpret_cast<const iphdr *>(eth+1);
273
274                 unsigned size = ntohs(ip->tot_len);
275                 unsigned port = 0;
276                 if(ip->protocol==IPPROTO_TCP)
277                 {
278                         const tcphdr *tcp = reinterpret_cast<const tcphdr *>(ip+1);
279                         port = min(ntohs(tcp->source), ntohs(tcp->dest));
280                 }
281                 else if(ip->protocol==IPPROTO_UDP)
282                 {
283                         const udphdr *udp = reinterpret_cast<const udphdr *>(ip+1);
284                         port = min(ntohs(udp->source), ntohs(udp->dest));
285                 }
286                 Host &shost = self->get_host(ntohl(ip->saddr));
287                 Host *dhost = 0;
288                 if((ntohl(ip->daddr)&0xFF)!=0xFF)
289                         dhost = &self->get_host(ntohl(ip->daddr));
290
291                 float throttle = shost.send_packet();
292                 if(throttle<1)
293                 {
294                         self->packets.push_back(new Packet(shost, dhost, self->get_port(port).get_color(), size));
295                         self->packets.back()->tick(-throttle*Msp::Time::sec);
296                 }
297
298                 shost.add_activity(size);
299                 if(dhost)
300                         dhost->add_activity(size);
301
302                 if((ntohl(ip->saddr)&self->localnet_mask)==self->localnet)
303                         self->history->activity(0, size);
304                 else if((ntohl(ip->daddr)&self->localnet_mask)==self->localnet)
305                         self->history->activity(size, 0);
306         }
307 }
308
309 void NetVis::sighandler(int)
310 {
311         exit(0);
312 }
313
314 Application::RegApp<NetVis> NetVis::reg;