From: Mikko Rasa Date: Mon, 16 Jun 2014 17:35:21 +0000 (+0300) Subject: Handle ICMP packets X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=HEAD;p=netvis.git Handle ICMP packets --- diff --git a/source/netvis.cpp b/source/netvis.cpp index 0e2378f..771af7f 100644 --- a/source/netvis.cpp +++ b/source/netvis.cpp @@ -296,6 +296,13 @@ Port &NetVis::get_port(unsigned 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; } @@ -345,6 +352,11 @@ void NetVis::handle_ipv4(CaptureContext &ctx, const iphdr *ip, unsigned len) 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); } @@ -365,6 +377,11 @@ void NetVis::handle_ipv6(CaptureContext &ctx, const ip6_hdr *ip6, unsigned len) 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); } @@ -383,6 +400,20 @@ void NetVis::handle_udp(CaptureContext &ctx, const udphdr *udp, unsigned) 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; diff --git a/source/netvis.h b/source/netvis.h index cd45133..1c8c287 100644 --- a/source/netvis.h +++ b/source/netvis.h @@ -12,7 +12,9 @@ Distributed unter the GPL #include #include #include +#include #include +#include #include #include #include @@ -89,6 +91,8 @@ private: void handle_ipv6(CaptureContext &, const ip6_hdr *, unsigned); void handle_tcp(CaptureContext &, const tcphdr *, unsigned); void handle_udp(CaptureContext &, const udphdr *, unsigned); + void handle_icmp(CaptureContext &, const icmphdr *, unsigned); + void handle_icmp6(CaptureContext &, const icmp6_hdr *, unsigned); void handle_packet(CaptureContext &); void sighandler(int); diff --git a/source/port.cpp b/source/port.cpp index a96e151..678be06 100644 --- a/source/port.cpp +++ b/source/port.cpp @@ -92,6 +92,11 @@ Port::Port(NetVis &v, unsigned n): bld.end(); } +void Port::set_name(const string &n) +{ + name = n; +} + void Port::add_activity(unsigned bytes) { activity.add_bytes(bytes); diff --git a/source/port.h b/source/port.h index 8877ed9..7fdd87e 100644 --- a/source/port.h +++ b/source/port.h @@ -31,6 +31,7 @@ public: unsigned get_number() const { return number; } bool is_registered() const { return registered; } + void set_name(const std::string &); const std::string &get_name() const { return name; } const Msp::GL::Color &get_color() const { return color; }