X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fnetvis.cpp;h=22231b150a4b62356c312d403534fed34ff6f2ff;hb=597a794cd648dd7b4c9d442fdc39f5961a38aba0;hp=7bb49bb458e507cb52d3f33cfcd17872f3537142;hpb=049518d751b3e2bada5af0bd1af63273e782775f;p=netvis.git diff --git a/source/netvis.cpp b/source/netvis.cpp index 7bb49bb..22231b1 100644 --- a/source/netvis.cpp +++ b/source/netvis.cpp @@ -9,11 +9,7 @@ Distributed unter the GPL #include #include #include -#include -#include -#include -#include -#include +#include #include #include #include @@ -23,7 +19,8 @@ Distributed unter the GPL #include #include #include -#include +#include +#include #include #include #include "history.h" @@ -46,16 +43,16 @@ NetVis::NetVis(int argc, char **argv): frames(0) { if(argc<2) - throw UsageError("No interface given"); + throw usage_error("No interface given"); iface = argv[1]; char err[1024]; 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); pcap_lookupnet(iface.c_str(), &localnet, &localnet_mask, err); localnet = ntohl(localnet); @@ -68,8 +65,7 @@ NetVis::NetVis(int argc, char **argv): wnd->signal_close.connect(sigc::bind(sigc::mem_fun(this, &NetVis::exit), 0)); wnd->show(); - GL::enable(GL::BLEND); - GL::blend_func(GL::SRC_ALPHA, GL::ONE_MINUS_SRC_ALPHA); + GL::Blend::alpha().bind(); font = new GL::Font; DataFile::load(*font, "dejavu-10.font"); @@ -81,6 +77,7 @@ NetVis::NetVis(int argc, char **argv): NetVis::~NetVis() { + delete history; delete resolver; delete font; @@ -206,13 +203,10 @@ void NetVis::tick() void NetVis::render() { - GL::clear(GL::COLOR_BUFFER_BIT); + GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT); - GL::matrix_mode(GL::PROJECTION); - GL::load_identity(); - GL::ortho_centered(1024, 768); - GL::matrix_mode(GL::MODELVIEW); - GL::load_identity(); + 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(); @@ -224,8 +218,7 @@ void NetVis::render() imm.end(); } - GL::push_matrix(); - GL::translate(-500, 360, 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) { @@ -233,27 +226,22 @@ void NetVis::render() if((i->second->is_registered() && act>1) || act>200) { i->second->render(); - GL::translate(0, -12, 0); + GL::MatrixStack::modelview() *= GL::Matrix::translation(0, -12, 0); ++n; } } - GL::pop_matrix(); - GL::push_matrix(); - GL::translate(-500, -348, 0); - GL::scale_uniform(10); + 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::translate(0, -1.2, 0); + GL::MatrixStack::modelview() *= GL::Matrix::translation(0, -1.2, 0); font->draw_string(format("%d ports", ports.size())); - GL::translate(0, -1.2, 0); + GL::MatrixStack::modelview() *= GL::Matrix::translation(0, -1.2, 0); font->draw_string(format("%.2f fps", fps)); GL::Texture::unbind(); - GL::pop_matrix(); - GL::push_matrix(); - GL::translate(170, -370, 0); + GL::MatrixStack::modelview() = GL::Matrix::translation(170, -370, 0); history->render(); - GL::pop_matrix(); } Host &NetVis::get_host(unsigned a) @@ -287,76 +275,107 @@ Port &NetVis::get_port(unsigned number) return *port; } -void NetVis::capture_handler(unsigned char *user, const pcap_pkthdr *, const unsigned char *data) +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); - if(ntohs(eth->h_proto)==ETH_P_IP) + 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) { const iphdr *ip = reinterpret_cast(eth+1); + handle_ipv4(ctx, ip, len-sizeof(ethhdr)); + } + else + IO::print("Unknown protocol in eth: %d\n", proto); +} - unsigned size = ntohs(ip->tot_len); +void NetVis::handle_ipv4(CaptureContext &ctx, const iphdr *ip, unsigned len) +{ + ctx.src_host = &get_host(ntohl(ip->saddr)); + if((ntohl(ip->daddr)&0xFF)!=0xFF) + ctx.dst_host = &get_host(ntohl(ip->daddr)); - Port *sport = 0; - Port *dport = 0; - if(ip->protocol==IPPROTO_TCP) - { - const tcphdr *tcp = reinterpret_cast(ip+1); - sport = &self->get_port(ntohs(tcp->source)); - dport = &self->get_port(ntohs(tcp->dest)); - } - else if(ip->protocol==IPPROTO_UDP) - { - const udphdr *udp = reinterpret_cast(ip+1); - sport = &self->get_port(ntohs(udp->source)); - dport = &self->get_port(ntohs(udp->dest)); - } + 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 udphdr *udp = reinterpret_cast(ip+1); + handle_udp(ctx, udp, len-sizeof(iphdr)); + } + else + IO::print("Unknown protocol in ip: %d\n", ip->protocol); +} - Port *port = 0; - if(sport && dport) - { - if(sport->is_registered()!=dport->is_registered()) - { - if(sport->is_registered()) - port = sport; - else - port = dport; - } - else if(sport->get_number()get_number()) - port = sport; - else - port = dport; - } - else - port = &self->get_port(0); - Host &shost = self->get_host(ntohl(ip->saddr)); - Host *dhost = 0; - if((ntohl(ip->daddr)&0xFF)!=0xFF) - dhost = &self->get_host(ntohl(ip->daddr)); +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); +} - float throttle = shost.send_packet(); - if(throttle<1) +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_packet(CaptureContext &ctx) +{ + Port *port = 0; + if(ctx.src_port && ctx.dst_port) + { + if(ctx.src_port->is_registered()!=ctx.dst_port->is_registered()) { - self->packets.push_back(new Packet(shost, dhost, port->get_color(), size)); - self->packets.back()->tick(-throttle*Msp::Time::sec); + if(ctx.src_port->is_registered()) + port = ctx.src_port; + else + port = ctx.dst_port; } + else if(ctx.src_port->get_number()get_number()) + port = ctx.src_port; + else + port = ctx.dst_port; + } + else + port = &get_port(0); - shost.add_activity(size); - if(dhost) - dhost->add_activity(size); - - if(sport) - sport->add_activity(size); - if(dport) - dport->add_activity(size); - - if((ntohl(ip->saddr)&self->localnet_mask)==self->localnet) - self->history->activity(0, size); - else if((ntohl(ip->daddr)&self->localnet_mask)==self->localnet) - self->history->activity(size, 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); } void NetVis::sighandler(int) @@ -364,4 +383,12 @@ void NetVis::sighandler(int) exit(0); } -Application::RegApp NetVis::reg; + +NetVis::CaptureContext::CaptureContext(): + cap_hdr(0), + src_host(0), + src_port(0), + dst_host(0), + dst_port(0), + size(0) +{ }