]> git.tdb.fi Git - netvis.git/blob - source/netvis.h
Restructure packet handling
[netvis.git] / source / netvis.h
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 #ifndef NETVIS_H_
9 #define NETVIS_H_
10
11 #include <list>
12 #include <map>
13 #include <pcap.h>
14 #include <netinet/ether.h>
15 #include <netinet/ip.h>
16 #include <netinet/tcp.h>
17 #include <netinet/udp.h>
18 #include <msp/core/application.h>
19 #include <msp/debug/profiler.h>
20 #include <msp/graphics/display.h>
21 #include <msp/graphics/glcontext.h>
22 #include <msp/graphics/simplewindow.h>
23 #include <msp/gl/color.h>
24 #include <msp/gl/font.h>
25 #include <msp/time/timestamp.h>
26
27 class History;
28 class Host;
29 class Packet;
30 class Port;
31 class Resolver;
32
33 class NetVis: public Msp::RegisteredApplication<NetVis>
34 {
35 private:
36         struct CaptureContext
37         {
38                 const pcap_pkthdr *cap_hdr;
39                 Host *src_host;
40                 Port *src_port;
41                 Host *dst_host;
42                 Port *dst_port;
43                 unsigned size;
44
45                 CaptureContext();
46         };
47
48         std::string iface;
49         pcap_t *pcap;
50         Resolver *resolver;
51         unsigned localnet;
52         unsigned localnet_mask;
53
54         Msp::Graphics::SimpleGLWindow *wnd;
55         Msp::GL::Font *font;
56
57         unsigned max_hosts;
58         unsigned max_visible_hosts;
59         std::map<unsigned, Host *> hosts;
60         std::map<unsigned, Host *> disabled_hosts;
61         std::list<Packet *> packets;
62         std::map<unsigned, Port *> ports;
63         History *history;
64
65         Msp::Time::TimeStamp tick_t;
66         Msp::Time::TimeStamp fps_t;
67         unsigned frames;
68         float fps;
69
70 public:
71         NetVis(int, char **);
72         ~NetVis();
73
74         const Msp::GL::Font &get_font() const { return *font; }
75         const std::map<unsigned, Host *> &get_hosts() const { return hosts; }
76         const std::map<unsigned, Port *> &get_ports() const { return ports; }
77 private:
78         virtual void tick();
79         void render();
80         Host &get_host(unsigned);
81         Msp::GL::Color generate_color(bool) const;
82         Port &get_port(unsigned);
83         void create_history_texture();
84
85         static void capture_handler(unsigned char *, const pcap_pkthdr *, const unsigned char *);
86         void handle_ethernet(CaptureContext &, const ethhdr *, unsigned);
87         void handle_ipv4(CaptureContext &, const iphdr *, unsigned);
88         void handle_tcp(CaptureContext &, const tcphdr *, unsigned);
89         void handle_udp(CaptureContext &, const udphdr *, unsigned);
90         void handle_packet(CaptureContext &);
91
92         void sighandler(int);
93 };
94
95 #endif