X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fnetvis.cpp;h=771af7f5db93272ee673faffc481810f81ee1164;hb=HEAD;hp=711788b86062c6e2d388cd1025cc1d7c350f343e;hpb=229e1d6ab66a9e987ffe3cd4a8de7c7f874f6de1;p=netvis.git diff --git a/source/netvis.cpp b/source/netvis.cpp index 711788b..771af7f 100644 --- a/source/netvis.cpp +++ b/source/netvis.cpp @@ -1,192 +1,474 @@ -#include -#include -#include -#include -#include -#include +/* $Id$ + +This file is part of NetVis +Copyright @ 2008 Mikko Rasa, Mikkosoft Productions +Distributed unter the GPL +*/ + +#include +#include +#include +#include +#include +#include +#include +#include #include #include #include #include #include #include -#include +#include +#include +#include #include +#include "history.h" #include "host.h" #include "netvis.h" #include "packet.h" +#include "port.h" +#include "resolver.h" using namespace std; using namespace Msp; -NetVis::NetVis(int /*argc*/, char **argv) +NetVis::NetVis(int argc, char **argv): + pcap(0), + resolver(0), + wnd(0), + font(0), + max_hosts(1000), + max_visible_hosts(30), + frames(0) { - iface=argv[1]; -} + if(argc<2) + throw usage_error("No interface given"); + iface = argv[1]; -int NetVis::main() -{ - char err[1024]; - pcap=pcap_open_live(iface.c_str(), 128, true, 0, err); + char err[PCAP_ERRBUF_SIZE]; + pcap_if_t *devs; + if(pcap_findalldevs(&devs, err)==-1) + throw runtime_error(err); + + pcap = pcap_open_live(iface.c_str(), 128, true, 0, err); if(!pcap) - throw Exception(err); + throw runtime_error(err); if(pcap_setnonblock(pcap, true, err)==-1) - throw Exception(err); + throw runtime_error(err); + + for(pcap_if_t *d=devs; d; d=d->next) + if(iface==d->name) + { + for(pcap_addr_t *a=d->addresses; a; a=a->next) + { + if(a->addr->sa_family==AF_INET) + { + Address addr(ntohl(reinterpret_cast(a->addr)->sin_addr.s_addr)); + if(a->netmask) + addr.set_mask(Address(ntohl(reinterpret_cast(a->netmask)->sin_addr.s_addr))); + localnets.push_back(addr); + } + else if(a->addr->sa_family==AF_INET6) + { + Address addr(reinterpret_cast(a->addr)->sin6_addr); + if(a->netmask) + addr.set_mask(Address(reinterpret_cast(a->netmask)->sin6_addr)); + localnets.push_back(addr); + } + } + } - dpy=new Graphics::Display; - wnd=new Graphics::Window(*dpy, 1024, 768); - glc=new Graphics::GLContext(*wnd); + pcap_freealldevs(devs); + + resolver = new Resolver; + + wnd = new Graphics::SimpleGLWindow(1024, 768); wnd->set_title("NetVis"); + wnd->signal_close.connect(sigc::bind(sigc::mem_fun(this, &NetVis::exit), 0)); wnd->show(); - GL::enable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + GL::Blend::alpha().bind(); - font=new GL::Font; + font = new GL::Font; DataFile::load(*font, "dejavu-10.font"); - font_tex=new GL::Texture2D; - DataFile::load(*font_tex, "dejavu-10.tex"); - font->set_texture(*font_tex); - Application::main(); + history = new History(*this, 301, 100); + + catch_signal(SIGINT); +} - delete glc; +NetVis::~NetVis() +{ + delete history; + delete resolver; + + delete font; delete wnd; - delete dpy; - return exit_code; + pcap_close(pcap); + for(map::iterator i=hosts.begin(); i!=hosts.end(); ++i) + delete i->second; + for(map::iterator i=disabled_hosts.begin(); i!=disabled_hosts.end(); ++i) + delete i->second; + for(map::iterator i=ports.begin(); i!=ports.end(); ++i) + delete i->second; + for(list::iterator i=packets.begin(); i!=packets.end(); ++i) + delete *i; } void NetVis::tick() { - Msp::Time::TimeStamp t=Msp::Time::now(); - if(!last_tick) - last_tick=t; - Msp::Time::TimeDelta dt=t-last_tick; - last_tick=t; + Msp::Time::TimeStamp t = Msp::Time::now(); + Msp::Time::TimeDelta dt; + if(tick_t) + dt = t-tick_t; + tick_t = t; - dpy->tick(); + if(tick_t>fps_t+Msp::Time::sec) + { + fps = frames/((tick_t-fps_t)/Msp::Time::sec); + fps_t = tick_t; + frames = 0; + } + + wnd->get_display().tick(); - pcap_dispatch(pcap, -1, &capture_handler, reinterpret_cast(this)); + while(pcap_dispatch(pcap, -1, &capture_handler, reinterpret_cast(this))>0) ; - glClear(GL_COLOR_BUFFER_BIT); + resolver->tick(); + history->tick(tick_t); - GL::matrix_mode(GL::PROJECTION); - GL::load_identity(); - GL::ortho_centered(1024, 768); - GL::matrix_mode(GL::MODELVIEW); - GL::load_identity(); - for(map::iterator i=hosts.begin(); i!=hosts.end(); ++i) + float min_activity = numeric_limits::max(); + for(map::iterator i=hosts.begin(); i!=hosts.end(); ++i) { i->second->tick(dt); - i->second->render(); + min_activity = min(min_activity, i->second->get_activity()); + } + float del_limit = pow(10, 6-0.1*static_cast(max_hosts-hosts.size()-disabled_hosts.size())); + for(map::iterator i=disabled_hosts.begin(); i!=disabled_hosts.end();) + { + i->second->tick(dt); + + if(i->second->get_activity()>min_activity) + { + i->second->set_active(true); + hosts.insert(*i); + for(unsigned j=0; j<100; ++j) + i->second->tick(100*Time::msec); + disabled_hosts.erase(i++); + } + else if(i->second->get_activity()cancel(i->second); + delete i->second; + disabled_hosts.erase(i++); + } + else + ++i; + } + + if(hosts.size()>max_visible_hosts) + { + list activity; + for(map::iterator i=hosts.begin(); i!=hosts.end(); ++i) + activity.push_back(i->second->get_activity()); + activity.sort(); + + list::iterator j = activity.begin(); + advance(j, activity.size()-max_visible_hosts); + float limit = *j; + + for(map::iterator i=hosts.begin(); i!=hosts.end();) + { + if(i->second->get_activity()second->set_active(false); + disabled_hosts.insert(*i); + hosts.erase(i++); + } + else + ++i; + } + } + + for(map::iterator i=ports.begin(); i!=ports.end();) + { + i->second->tick(dt); + + if(!i->second->is_registered() && i->second->get_activity()<0.1) + { + delete i->second; + ports.erase(i++); + } + else + ++i; } + for(list::iterator i=packets.begin(); i!=packets.end();) { (*i)->tick(dt); - (*i)->render(); if((*i)->get_stale()) { delete *i; - i=packets.erase(i); + i = packets.erase(i); } else ++i; } - GL::translate(-500, 360, 0); - for(map::iterator i=port_colors.begin(); i!=port_colors.end(); ++i) - { - GL::Color &color=i->second; + render(); + wnd->swap_buffers(); - GL::push_matrix(); + ++frames; +} - GL::Immediate imm((GL::COLOR4_UBYTE,GL::VERTEX2)); +void NetVis::render() +{ + GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT); + + GL::MatrixStack::projection() = GL::Matrix::ortho_centered(1024, 768); + GL::MatrixStack::modelview() = GL::Matrix(); + + for(map::iterator i=hosts.begin(); i!=hosts.end(); ++i) + i->second->render(); + { + GL::Immediate imm((GL::COLOR4_UBYTE, GL::VERTEX2)); imm.begin(GL::QUADS); - imm.color(color.r, color.g, color.b, color.a); - for(float x=0; x<0.5; x+=0.2) - { - imm.vertex(x+0, 0); - imm.vertex(x+10, 0); - imm.vertex(x+10, 10); - imm.vertex(x+0, 10); - } + for(list::iterator i=packets.begin(); i!=packets.end(); ++i) + (*i)->render(imm); imm.end(); + } - GL::translate(15, 1, 0); - GL::scale_uniform(10); - glColor4f(1.0, 1.0, 1.0, 1.0); - font->draw_string(format("%d", i->first)); - GL::Texture::unbind(); - - GL::pop_matrix(); - GL::translate(0, -12, 0); + GL::MatrixStack::modelview() = GL::Matrix::translation(-500, 360, 0); + unsigned n = 0; + for(map::iterator i=ports.begin(); (i!=ports.end() && n<50); ++i) + { + float act = i->second->get_activity(); + if((i->second->is_registered() && act>1) || act>200) + { + i->second->render(); + GL::MatrixStack::modelview() *= GL::Matrix::translation(0, -12, 0); + ++n; + } } - glc->swap_buffers(); + GL::MatrixStack::modelview() = GL::Matrix::translation(-500, -348, 0); + GL::MatrixStack::modelview() *= GL::Matrix::scaling(10); + font->draw_string(format("%d hosts", hosts.size()+disabled_hosts.size())); + GL::MatrixStack::modelview() *= GL::Matrix::translation(0, -1.2, 0); + font->draw_string(format("%d ports", ports.size())); + GL::MatrixStack::modelview() *= GL::Matrix::translation(0, -1.2, 0); + font->draw_string(format("%.2f fps", fps)); + GL::Texture::unbind(); + + GL::MatrixStack::modelview() = GL::Matrix::translation(170, -370, 0); + history->render(); } -Host &NetVis::get_host(unsigned a) +Host &NetVis::get_host(const Address &a) { - map::iterator i=hosts.find(a); + map::iterator i = hosts.find(a); if(i!=hosts.end()) return *i->second; - Host *host=new Host(*this, a); - host->set_position(Vector2(rand()*30.0/RAND_MAX-15.0, rand()*20.0/RAND_MAX-10.0)); - hosts[a]=host; + i = disabled_hosts.find(a); + if(i!=disabled_hosts.end()) + return *i->second; + + Host *host = new Host(*this, a); + for(list
::const_iterator j=localnets.begin(); j!=localnets.end(); ++j) + if(j->masked_match(a)) + host->set_local(true); + resolver->push(host); + host->set_position(Vector2(rand()*400.0/RAND_MAX-200.0, rand()*400.0/RAND_MAX-200.0)); + for(unsigned j=0; j<100; ++j) + host->tick(100*Time::msec); + hosts[a] = host; return *host; } -GL::Color &NetVis::get_port_color(unsigned port) +Port &NetVis::get_port(unsigned number) { - map::iterator i=port_colors.find(port); - if(i!=port_colors.end()) - return i->second; + map::iterator i = ports.find(number); + if(i!=ports.end()) + return *i->second; + Port *port = new Port(*this, number); + if(number>=0x10000) + { + if((number&0xFF)==IPPROTO_ICMP) + port->set_name("icmp"); + else if((number&0xFF)==IPPROTO_ICMPV6) + port->set_name("icmp6"); + } + ports[number] = port; + return *port; +} - GL::Color color; - while(1) +void NetVis::capture_handler(unsigned char *user, const pcap_pkthdr *cap, const unsigned char *data) +{ + NetVis *self = reinterpret_cast(user); + + CaptureContext ctx; + ctx.cap_hdr = cap; + const ethhdr *eth = reinterpret_cast(data); + self->handle_ethernet(ctx, eth, cap->caplen); +} + +void NetVis::handle_ethernet(CaptureContext &ctx, const ethhdr *eth, unsigned len) +{ + ctx.size = ctx.cap_hdr->len-sizeof(ethhdr); + + int proto = ntohs(eth->h_proto); + if(proto==ETH_P_IP) { - color.r=rand()*1.0/RAND_MAX; - color.g=rand()*1.0/RAND_MAX; - color.b=rand()*1.0/RAND_MAX; - if(color.r>0.5 || color.g>0.5 || color.b>0.7) - break; + const iphdr *ip = reinterpret_cast(eth+1); + handle_ipv4(ctx, ip, len-sizeof(ethhdr)); } - color.a=0.4f; - return port_colors[port]=color; + else if(proto==ETH_P_IPV6) + { + const ip6_hdr *ip6 = reinterpret_cast(eth+1); + handle_ipv6(ctx, ip6, len-sizeof(ethhdr)); + } + else + IO::print("Unknown protocol in eth: %d\n", proto); } -void NetVis::capture_handler(unsigned char *user, const pcap_pkthdr *, const unsigned char *data) +void NetVis::handle_ipv4(CaptureContext &ctx, const iphdr *ip, unsigned len) { - NetVis *self=reinterpret_cast(user); + ctx.src_host = &get_host(ntohl(ip->saddr)); + if((ntohl(ip->daddr)&0xFF)!=0xFF) + ctx.dst_host = &get_host(ntohl(ip->daddr)); - const ethhdr *eth=reinterpret_cast(data); - if(ntohs(eth->h_proto)==ETH_P_IP) + if(ip->protocol==IPPROTO_TCP) + { + const tcphdr *tcp = reinterpret_cast(ip+1); + handle_tcp(ctx, tcp, len-sizeof(iphdr)); + } + else if(ip->protocol==IPPROTO_UDP) { - const iphdr *ip=reinterpret_cast(eth+1); + const udphdr *udp = reinterpret_cast(ip+1); + handle_udp(ctx, udp, len-sizeof(iphdr)); + } + else if(ip->protocol==IPPROTO_ICMP) + { + const icmphdr *icmp = reinterpret_cast(ip+1); + handle_icmp(ctx, icmp, len-sizeof(iphdr)); + } + else + IO::print("Unknown protocol in ip: %d\n", ip->protocol); +} - //cout<<"IP packet of "<len<<'/'<tot_len)<<" bytes\n"; +void NetVis::handle_ipv6(CaptureContext &ctx, const ip6_hdr *ip6, unsigned len) +{ + ctx.src_host = &get_host(ip6->ip6_src); + if(!IN6_IS_ADDR_MULTICAST(ip6->ip6_dst.s6_addr)) + ctx.dst_host = &get_host(ip6->ip6_dst); - unsigned port=0; - if(ip->protocol==IPPROTO_TCP) - { - const tcphdr *tcp=reinterpret_cast(ip+1); - port=min(ntohs(tcp->source), ntohs(tcp->dest)); - } - else if(ip->protocol==IPPROTO_UDP) + if(ip6->ip6_nxt==IPPROTO_TCP) + { + const tcphdr *tcp = reinterpret_cast(ip6+1); + handle_tcp(ctx, tcp, len-sizeof(ip6_hdr)); + } + else if(ip6->ip6_nxt==IPPROTO_UDP) + { + const udphdr *udp = reinterpret_cast(ip6+1); + handle_udp(ctx, udp, len-sizeof(ip6_hdr)); + } + else if(ip6->ip6_nxt==IPPROTO_ICMPV6) + { + const icmp6_hdr *icmp6 = reinterpret_cast(ip6+1); + handle_icmp6(ctx, icmp6, len-sizeof(ip6_hdr)); + } + else + IO::print("Unknown next header in ip6: %d\n", ip6->ip6_nxt); +} + +void NetVis::handle_tcp(CaptureContext &ctx, const tcphdr *tcp, unsigned) +{ + ctx.src_port = &get_port(ntohs(tcp->source)); + ctx.dst_port = &get_port(ntohs(tcp->dest)); + handle_packet(ctx); +} + +void NetVis::handle_udp(CaptureContext &ctx, const udphdr *udp, unsigned) +{ + ctx.src_port = &get_port(ntohs(udp->source)); + ctx.dst_port = &get_port(ntohs(udp->dest)); + handle_packet(ctx); +} + +void NetVis::handle_icmp(CaptureContext &ctx, const icmphdr *, unsigned) +{ + ctx.src_port = &get_port(0x10000|IPPROTO_ICMP); + ctx.dst_port = ctx.src_port; + handle_packet(ctx); +} + +void NetVis::handle_icmp6(CaptureContext &ctx, const icmp6_hdr *, unsigned) +{ + ctx.src_port = &get_port(0x10000|IPPROTO_ICMPV6); + ctx.dst_port = ctx.src_port; + handle_packet(ctx); +} + +void NetVis::handle_packet(CaptureContext &ctx) +{ + Port *port = 0; + if(ctx.src_port && ctx.dst_port) + { + if(ctx.src_port->is_registered()!=ctx.dst_port->is_registered()) { - const udphdr *udp=reinterpret_cast(ip+1); - port=min(ntohs(udp->source), ntohs(udp->dest)); + if(ctx.src_port->is_registered()) + port = ctx.src_port; + else + port = ctx.dst_port; } - Host &shost=self->get_host(ntohl(ip->saddr)); - Host *dhost=0; - if((ntohl(ip->daddr)&0xFF)!=0xFF) - dhost=&self->get_host(ntohl(ip->daddr)); - self->packets.push_back(new Packet(shost, dhost, self->get_port_color(port), ntohs(ip->tot_len))); + else if(ctx.src_port->get_number()get_number()) + port = ctx.src_port; + else + port = ctx.dst_port; + } + else + port = &get_port(0); + + float throttle = ctx.src_host->send_packet(); + if(throttle<1) + { + packets.push_back(new Packet(*ctx.src_host, ctx.dst_host, port->get_color(), ctx.size)); + packets.back()->tick(-throttle*Msp::Time::sec); } + + ctx.src_host->add_activity(ctx.size); + if(ctx.dst_host) + ctx.dst_host->add_activity(ctx.size); + + if(ctx.src_port) + ctx.src_port->add_activity(ctx.size); + if(ctx.dst_port) + ctx.dst_port->add_activity(ctx.size); + + bool local_src = ctx.src_host->is_local(); + bool local_dst = (ctx.dst_host && ctx.dst_host->is_local()); + if(local_src && !local_dst) + history->activity(0, ctx.size); + else if(local_dst && !local_src) + history->activity(ctx.size, 0); } -Application::RegApp NetVis::reg; +void NetVis::sighandler(int) +{ + exit(0); +} + + +NetVis::CaptureContext::CaptureContext(): + cap_hdr(0), + src_host(0), + src_port(0), + dst_host(0), + dst_port(0), + size(0) +{ }