]> git.tdb.fi Git - netvis.git/blob - source/netvis.cpp
Refactor the rendering code
[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 "port.h"
33 #include "resolver.h"
34
35 using namespace std;
36 using namespace Msp;
37
38 NetVis::NetVis(int argc, char **argv):
39         pcap(0),
40         resolver(0),
41         wnd(0),
42         font(0),
43         max_hosts(1000),
44         max_visible_hosts(30),
45         draw_labels(true),
46         blend(true),
47         frames(0)
48 {
49         if(argc<2)
50                 throw UsageError("No interface given");
51         iface = argv[1];
52
53         char err[1024];
54         pcap = pcap_open_live(iface.c_str(), 128, true, 0, err);
55         if(!pcap)
56                 throw Exception(err);
57
58         if(pcap_setnonblock(pcap, true, err)==-1)
59                 throw Exception(err);
60
61         pcap_lookupnet(iface.c_str(), &localnet, &localnet_mask, err);
62         localnet = ntohl(localnet);
63         localnet_mask = ntohl(localnet_mask);
64
65         resolver = new Resolver;
66
67         wnd = new Graphics::SimpleGLWindow(1024, 768);
68         wnd->set_title("NetVis");
69         wnd->signal_close.connect(sigc::bind(sigc::mem_fun(this, &NetVis::exit), 0));
70         wnd->signal_key_press.connect(sigc::mem_fun(this, &NetVis::key_press));
71         wnd->show();
72
73         GL::enable(GL::BLEND);
74         GL::blend_func(GL::SRC_ALPHA, GL::ONE_MINUS_SRC_ALPHA);
75
76         font = new GL::Font;
77         DataFile::load(*font, "dejavu-10.font");
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
121         float min_activity = numeric_limits<float>::max();
122         for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
123         {
124                 i->second->tick(dt);
125                 min_activity = min(min_activity, i->second->get_activity());
126         }
127         float del_limit = pow(10, 6-0.1*(max_hosts-hosts.size()-disabled_hosts.size()));
128         for(map<unsigned, Host *>::iterator i=disabled_hosts.begin(); i!=disabled_hosts.end();)
129         {
130                 i->second->tick(dt);
131
132                 if(i->second->get_activity()>min_activity)
133                 {
134                         i->second->set_active(true);
135                         hosts.insert(*i);
136                         disabled_hosts.erase(i++);
137                 }
138                 else if(i->second->get_activity()<del_limit)
139                 {
140                         delete i->second;
141                         disabled_hosts.erase(i++);
142                 }
143                 else
144                         ++i;
145         }
146
147         if(hosts.size()>max_visible_hosts)
148         {
149                 list<float> activity;
150                 for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
151                         activity.push_back(i->second->get_activity());
152                 activity.sort();
153
154                 list<float>::iterator j = activity.begin();
155                 advance(j, activity.size()-max_visible_hosts);
156                 float limit = *j;
157
158                 for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end();)
159                 {
160                         if(i->second->get_activity()<limit)
161                         {
162                                 i->second->set_active(false);
163                                 disabled_hosts.insert(*i);
164                                 hosts.erase(i++);
165                         }
166                         else
167                                 ++i;
168                 }
169         }
170
171         for(list<Packet *>::iterator i=packets.begin(); i!=packets.end();)
172         {
173                 (*i)->tick(dt);
174                 if((*i)->get_stale())
175                 {
176                         delete *i;
177                         i = packets.erase(i);
178                 }
179                 else
180                         ++i;
181         }
182
183         render();
184         wnd->swap_buffers();
185
186         ++frames;
187 }
188
189 void NetVis::render()
190 {
191         GL::clear(GL::COLOR_BUFFER_BIT);
192
193         GL::matrix_mode(GL::PROJECTION);
194         GL::load_identity();
195         GL::ortho_centered(1024, 768);
196         GL::matrix_mode(GL::MODELVIEW);
197         GL::load_identity();
198
199         for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
200                 i->second->render();
201         {
202                 GL::Immediate imm((GL::COLOR4_UBYTE, GL::VERTEX2));
203                 imm.begin(GL::QUADS);
204                 for(list<Packet *>::iterator i=packets.begin(); i!=packets.end(); ++i)
205                         (*i)->render(imm);
206                 imm.end();
207         }
208
209         GL::push_matrix();
210         GL::translate(-500, 360, 0);
211         unsigned n = 0;
212         for(map<unsigned, Port *>::iterator i=ports.begin(); (i!=ports.end() && n<50); ++i, ++n)
213         {
214                 i->second->render();
215                 GL::translate(0, -12, 0);
216         }
217         GL::pop_matrix();
218
219         GL::push_matrix();
220         GL::translate(-500, -360, 0);
221         GL::scale_uniform(10);
222         font->draw_string(format("%d hosts", hosts.size()+disabled_hosts.size()));
223         GL::translate(0, -1.2, 0);
224         font->draw_string(format("%.2f fps", fps));
225         GL::Texture::unbind();
226         GL::pop_matrix();
227 }
228
229 Host &NetVis::get_host(unsigned a)
230 {
231         map<unsigned, Host *>::iterator i = hosts.find(a);
232         if(i!=hosts.end())
233                 return *i->second;
234
235         i = disabled_hosts.find(a);
236         if(i!=disabled_hosts.end())
237                 return *i->second;
238
239         Host *host = new Host(*this, a);
240         if((a&localnet_mask)==localnet)
241                 host->set_local(true);
242         resolver->push(host);
243         host->set_position(Vector2(rand()*400.0/RAND_MAX-200.0, rand()*400.0/RAND_MAX-200.0));
244         hosts[a] = host;
245         return *host;
246 }
247
248 const Port &NetVis::get_port(unsigned number)
249 {
250         map<unsigned, Port *>::iterator i = ports.find(number);
251         if(i!=ports.end())
252                 return *i->second;
253         Port *port = new Port(*this, number);
254         ports[number] = port;
255         return *port;
256 }
257
258 void NetVis::key_press(unsigned key, unsigned, wchar_t)
259 {
260         if(key==46)
261                 draw_labels = !draw_labels;
262         else if(key==56)
263         {
264                 blend = !blend;
265                 GL::set(GL_BLEND, blend);
266         }
267 }
268
269 void NetVis::capture_handler(unsigned char *user, const pcap_pkthdr *, const unsigned char *data)
270 {
271         NetVis *self = reinterpret_cast<NetVis *>(user);
272
273         const ethhdr *eth = reinterpret_cast<const ethhdr *>(data);
274         if(ntohs(eth->h_proto)==ETH_P_IP)
275         {
276                 const iphdr *ip = reinterpret_cast<const iphdr *>(eth+1);
277
278                 unsigned size = ntohs(ip->tot_len);
279                 unsigned port = 0;
280                 if(ip->protocol==IPPROTO_TCP)
281                 {
282                         const tcphdr *tcp = reinterpret_cast<const tcphdr *>(ip+1);
283                         port = min(ntohs(tcp->source), ntohs(tcp->dest));
284                 }
285                 else if(ip->protocol==IPPROTO_UDP)
286                 {
287                         const udphdr *udp = reinterpret_cast<const udphdr *>(ip+1);
288                         port = min(ntohs(udp->source), ntohs(udp->dest));
289                 }
290                 Host &shost = self->get_host(ntohl(ip->saddr));
291                 Host *dhost = 0;
292                 if((ntohl(ip->daddr)&0xFF)!=0xFF)
293                         dhost = &self->get_host(ntohl(ip->daddr));
294
295                 float throttle = shost.send_packet();
296                 if(throttle<1)
297                 {
298                         self->packets.push_back(new Packet(shost, dhost, self->get_port(port).get_color(), size));
299                         self->packets.back()->tick(-throttle*Msp::Time::sec);
300                 }
301
302                 shost.add_activity(size);
303                 if(dhost)
304                         dhost->add_activity(size);
305         }
306 }
307
308 void NetVis::sighandler(int)
309 {
310         exit(0);
311 }
312
313 Application::RegApp<NetVis> NetVis::reg;