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