]> git.tdb.fi Git - netvis.git/commitdiff
Support IPv6
authorMikko Rasa <tdb@tdb.fi>
Mon, 16 Jun 2014 17:24:15 +0000 (20:24 +0300)
committerMikko Rasa <tdb@tdb.fi>
Mon, 16 Jun 2014 17:24:15 +0000 (20:24 +0300)
source/address.cpp
source/address.h
source/netvis.cpp
source/netvis.h

index 3a499716ec4720bfd24b8c297f25a3353bc6e977..4b56625ec1acd3a71b322d12484bbbdea3e72599 100644 (file)
@@ -21,6 +21,14 @@ Address::Address(uint32_t a):
                data[i] = a>>(24-i*8);
 }
 
+Address::Address(const in6_addr &a):
+       type(ETH_P_IPV6),
+       length(16),
+       mask_bits(128)
+{
+       copy(a.s6_addr, a.s6_addr+16, data);
+}
+
 Address::Address(unsigned short t, const unsigned char *d, unsigned char l):
        type(t),
        length(l)
@@ -53,6 +61,11 @@ string Address::str() const
                char buf[INET_ADDRSTRLEN];
                return inet_ntop(AF_INET, data, buf, INET_ADDRSTRLEN);
        }
+       else if(type==ETH_P_IPV6)
+       {
+               char buf[INET6_ADDRSTRLEN];
+               return inet_ntop(AF_INET6, data, buf, INET6_ADDRSTRLEN);
+       }
        else
                return "<unknown>";
 }
@@ -94,6 +107,15 @@ void Address::to_sockaddr(sockaddr_storage &sa) const
                sa_in.sin_port = 0;
                sa_in.sin_addr.s_addr = *(uint32_t *)data;
        }
+       else if(type==ETH_P_IPV6)
+       {
+               sockaddr_in6 &sa_in6 = reinterpret_cast<sockaddr_in6 &>(sa);
+               sa_in6.sin6_family = AF_INET6;
+               sa_in6.sin6_port = 0;
+               sa_in6.sin6_flowinfo = 0;
+               copy(data, data+16, sa_in6.sin6_addr.s6_addr);
+               sa_in6.sin6_scope_id = 0;
+       }
        else
                throw runtime_error("Unknown type");
 }
index 42e671f4cf6d4e8a9251c9ff2ead17fcd0d8e86d..eda93688d9949b8be8cacd3ad46f05dcc319f286 100644 (file)
@@ -2,6 +2,7 @@
 #define ADDRESS_H_
 
 #include <netinet/ip.h>
+#include <netinet/ip6.h>
 #include <string>
 
 struct Address
@@ -13,6 +14,7 @@ struct Address
 
        Address();
        Address(uint32_t);
+       Address(const in6_addr &);
        Address(unsigned short, const unsigned char *, unsigned char);
 
        void set_mask(const Address &);
index a8f6209142b2e12505e8017613bf5385c2d88d49..0e2378fd689ac6ee9efb96a6d77a0ff9af10efc9 100644 (file)
@@ -46,7 +46,11 @@ NetVis::NetVis(int argc, char **argv):
                throw usage_error("No interface given");
        iface = argv[1];
 
-       char err[1024];
+       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 runtime_error(err);
@@ -54,10 +58,29 @@ NetVis::NetVis(int argc, char **argv):
        if(pcap_setnonblock(pcap, true, err)==-1)
                throw runtime_error(err);
 
-       unsigned local_addr, local_mask;
-       pcap_lookupnet(iface.c_str(), &local_addr, &local_mask, err);
-       localnet = Address(ntohl(local_addr));
-       localnet.set_mask(Address(ntohl(local_mask)));
+       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<sockaddr_in *>(a->addr)->sin_addr.s_addr));
+                                       if(a->netmask)
+                                               addr.set_mask(Address(ntohl(reinterpret_cast<sockaddr_in *>(a->netmask)->sin_addr.s_addr)));
+                                       localnets.push_back(addr);
+                               }
+                               else if(a->addr->sa_family==AF_INET6)
+                               {
+                                       Address addr(reinterpret_cast<sockaddr_in6 *>(a->addr)->sin6_addr);
+                                       if(a->netmask)
+                                               addr.set_mask(Address(reinterpret_cast<sockaddr_in6 *>(a->netmask)->sin6_addr));
+                                       localnets.push_back(addr);
+                               }
+                       }
+               }
+
+       pcap_freealldevs(devs);
 
        resolver = new Resolver;
 
@@ -256,8 +279,9 @@ Host &NetVis::get_host(const Address &a)
                return *i->second;
 
        Host *host = new Host(*this, a);
-       if(localnet.masked_match(a))
-               host->set_local(true);
+       for(list<Address>::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)
@@ -296,6 +320,11 @@ void NetVis::handle_ethernet(CaptureContext &ctx, const ethhdr *eth, unsigned le
                const iphdr *ip = reinterpret_cast<const iphdr *>(eth+1);
                handle_ipv4(ctx, ip, len-sizeof(ethhdr));
        }
+       else if(proto==ETH_P_IPV6)
+       {
+               const ip6_hdr *ip6 = reinterpret_cast<const ip6_hdr *>(eth+1);
+               handle_ipv6(ctx, ip6, len-sizeof(ethhdr));
+       }
        else
                IO::print("Unknown protocol in eth: %d\n", proto);
 }
@@ -320,6 +349,25 @@ void NetVis::handle_ipv4(CaptureContext &ctx, const iphdr *ip, unsigned len)
                IO::print("Unknown protocol in ip: %d\n", ip->protocol);
 }
 
+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);
+
+       if(ip6->ip6_nxt==IPPROTO_TCP)
+       {
+               const tcphdr *tcp = reinterpret_cast<const tcphdr *>(ip6+1);
+               handle_tcp(ctx, tcp, len-sizeof(ip6_hdr));
+       }
+       else if(ip6->ip6_nxt==IPPROTO_UDP)
+       {
+               const udphdr *udp = reinterpret_cast<const udphdr *>(ip6+1);
+               handle_udp(ctx, udp, 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)
 {
index a59b65061f37141f30bacb88cf0145cf61b21d19..cd451339880c4fc5d40ceaf0666df937f34461f2 100644 (file)
@@ -13,6 +13,7 @@ Distributed unter the GPL
 #include <pcap.h>
 #include <netinet/ether.h>
 #include <netinet/ip.h>
+#include <netinet/ip6.h>
 #include <netinet/tcp.h>
 #include <netinet/udp.h>
 #include <msp/core/application.h>
@@ -49,7 +50,7 @@ private:
        std::string iface;
        pcap_t *pcap;
        Resolver *resolver;
-       Address localnet;
+       std::list<Address> localnets;
 
        Msp::Graphics::SimpleGLWindow *wnd;
        Msp::GL::Font *font;
@@ -85,6 +86,7 @@ private:
        static void capture_handler(unsigned char *, const pcap_pkthdr *, const unsigned char *);
        void handle_ethernet(CaptureContext &, const ethhdr *, unsigned);
        void handle_ipv4(CaptureContext &, const iphdr *, unsigned);
+       void handle_ipv6(CaptureContext &, const ip6_hdr *, unsigned);
        void handle_tcp(CaptureContext &, const tcphdr *, unsigned);
        void handle_udp(CaptureContext &, const udphdr *, unsigned);
        void handle_packet(CaptureContext &);