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)
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>";
}
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");
}
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);
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;
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)
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);
}
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)
{
#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>
std::string iface;
pcap_t *pcap;
Resolver *resolver;
- Address localnet;
+ std::list<Address> localnets;
Msp::Graphics::SimpleGLWindow *wnd;
Msp::GL::Font *font;
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 &);