[Guessnet-devel] [svn] r128 - in trunk: . src tests
Enrico Zini
enrico at costa.debian.org
Sat Oct 22 18:21:33 UTC 2005
Author: enrico
Date: Sat Oct 22 18:21:32 2005
New Revision: 128
Added:
trunk/src/Scans.cc
trunk/tests/test-utils.h
trunk/tests/tut-main.cpp
Modified:
trunk/autogen.sh
trunk/configure.ac
trunk/src/IfaceParser.cc
trunk/src/Makefile.am
trunk/src/PacketMaker.cc
trunk/src/PacketMaker.h
trunk/src/PeerScanner.cc
trunk/src/Scans.h
trunk/src/nettypes.cc
trunk/src/nettypes.h
trunk/tests/Makefile.am
Log:
IP address in peer scans is now optional, but mac-only scan is still not
working. I'm probably building the wrong kind of probe packet.
Added unit test infrastructure and some unit tests. It will be good to test
regressions.
Modified: trunk/autogen.sh
==============================================================================
--- trunk/autogen.sh (original)
+++ trunk/autogen.sh Sat Oct 22 18:21:32 2005
@@ -1,22 +1,4 @@
#!/bin/sh
# Rebuild the build system
-
-ACLOCAL=aclocal
-AUTOMAKE=automake
-
-for i in 1.9 1.8 1.7
-do
- if which $ACLOCAL-$i > /dev/null
- then
- ACLOCAL=aclocal-$i
- AUTOMAKE=automake-$i
- break
- fi
-done
-
-$ACLOCAL
-autoheader
-libtoolize --force -c
-$AUTOMAKE --foreign -a -f -c
-autoconf
+autoreconf -i
Modified: trunk/configure.ac
==============================================================================
--- trunk/configure.ac (original)
+++ trunk/configure.ac Sat Oct 22 18:21:32 2005
@@ -1,7 +1,8 @@
+dnl Process this file with autoconf to produce a configure script.
AC_INIT(guessnet, 0.36, [enrico at debian.org])
AC_CONFIG_SRCDIR([configure.ac])
AM_CONFIG_HEADER(config.h)
-AM_INIT_AUTOMAKE
+AM_INIT_AUTOMAKE([foreign])
dnl Add option to specify a nonstandard location of libnet
LIBNET_CONFIG=no
Modified: trunk/src/IfaceParser.cc
==============================================================================
--- trunk/src/IfaceParser.cc (original)
+++ trunk/src/IfaceParser.cc Sat Oct 22 18:21:32 2005
@@ -172,26 +172,29 @@
map<string, string>::const_iterator ip = args.find("address");
map<string, string>::const_iterator mac = args.find("mac");
map<string, string>::const_iterator src = args.find("source");
+
+ PeerScan* s = new PeerScan(profileName);
- if (ip != args.end())
+ if (ip == args.end() && mac == args.end())
{
- IPAddress ipAddr(ip->second);
+ warning("Missing IP or MAC address at line %d: skipping line\n", linenum);
+ } else {
+ if (ip != args.end())
+ s->setIP(IPAddress(ip->second));
- struct ether_addr macAddr;
if (mac != args.end())
+ {
+ struct ether_addr macAddr;
parse_mac(&macAddr, mac->second);
- else {
- debug("Missing mac at line %d: only check for the IP\n", linenum);
- bzero(&macAddr, sizeof(struct ether_addr));
- }
+ s->setMAC(macAddr);
+ } else
+ warning("No mac provided at line %d: scans will be less accurate\n", linenum);
if (src != args.end())
- sc.handleScan(new PeerScan(profileName, macAddr, ipAddr, IPAddress(src->second)));
- else
- sc.handleScan(new PeerScan(profileName, macAddr, ipAddr));
+ s->setSource(IPAddress(src->second));
+
+ sc.handleScan(s);
found++;
- } else {
- warning("Missing address at line %d: skipping line\n", linenum);
}
}
}
Modified: trunk/src/Makefile.am
==============================================================================
--- trunk/src/Makefile.am (original)
+++ trunk/src/Makefile.am Sat Oct 22 18:21:32 2005
@@ -20,6 +20,7 @@
ChildProcess.cc \
Regexp.cc \
Parser.cc \
+ Scans.cc \
ScanBag.cc \
CommandlineParser.cc \
IFace.cc \
@@ -58,7 +59,7 @@
#install-exec-hook:
# ln -s guessnet $(sbindir)/guessnet-ifupdown
-INCLUDES=@LIBNET_CFLAGS@ -DSCRIPTDIR=\"@scriptdir@\"
+INCLUDES=@LIBNET_CFLAGS@ -DSCRIPTDIR=\"@scriptdir@\" -DCOMPILE_TESTSUITE
EXTRA_DIST = \
Buffer.h \
Modified: trunk/src/PacketMaker.cc
==============================================================================
--- trunk/src/PacketMaker.cc (original)
+++ trunk/src/PacketMaker.cc Sat Oct 22 18:21:32 2005
@@ -26,7 +26,6 @@
return pkt;
}
-
Buffer PacketMaker::makeARPRequest(const IPAddress& ip, const IPAddress& src) throw ()
{
unsigned char ether_broadcast_addr[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
@@ -58,6 +57,40 @@
// Construct the packet
return buffer_from_libnet(ln_context);
}
+
+Buffer PacketMaker::makeARPRequest(const ether_addr& mac, const IPAddress& src) throw ()
+{
+ unsigned char ether_broadcast_addr[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
+ //unsigned char ether_no_addr[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+ struct libnet_ether_addr* localmac = sender.getMACAddress();
+ libnet_t* ln_context = sender.getLibnetContext();
+ IPAddress noaddr("0.0.0.0");
+
+ // Build the pieces of the packet
+ libnet_build_arp (
+ ARPHRD_ETHER,
+ ETHERTYPE_IP,
+ ETHER_ADDR_LEN,
+ 4,
+ ARPOP_REVREQUEST,
+ localmac->ether_addr_octet,
+ (u_char *)src.s_addr_p(), // FIXME: what's in here? The broadcast addr maybe?
+ (u_char *)&mac,
+ (u_char *)noaddr.s_addr_p(),
+ NULL,
+ 0,
+ ln_context, 0);
+ libnet_build_ethernet (
+ ether_broadcast_addr,
+ localmac->ether_addr_octet,
+ ETHERTYPE_ARP,
+ NULL, 0,
+ ln_context, 0);
+
+ // Construct the packet
+ return buffer_from_libnet(ln_context);
+}
+
Buffer PacketMaker::makeDHCPRequest() throw ()
{
Modified: trunk/src/PacketMaker.h
==============================================================================
--- trunk/src/PacketMaker.h (original)
+++ trunk/src/PacketMaker.h Sat Oct 22 18:21:32 2005
@@ -14,6 +14,7 @@
PacketMaker(NetSender& sender) : sender(sender) {}
Buffer makeARPRequest(const IPAddress& ip, const IPAddress& src) throw ();
+ Buffer makeARPRequest(const ether_addr& mac, const IPAddress& src) throw ();
Buffer makeDHCPRequest() throw ();
};
Modified: trunk/src/PeerScanner.cc
==============================================================================
--- trunk/src/PeerScanner.cc (original)
+++ trunk/src/PeerScanner.cc Sat Oct 22 18:21:32 2005
@@ -55,25 +55,21 @@
for (list<const PeerScan*>::const_iterator i = candidates.begin();
i != candidates.end(); i++)
{
- // Check if IP and MAC addresses match
- if (IPv4_MATCHES(&(*i)->ip(), ipv4_him))
+ const PeerScan* scan = *i;
+ bool match = true;
+
+ // Check if IP matches
+ if (scan->hasIP() && ! IPv4_MATCHES(&scan->ip(), ipv4_him))
+ match = false;
+
+ // Check if MAC matches
+ if (scan->hasMAC() && ! MAC_MATCHES(&scan->mac(), mac_him))
+ match = false;
+
+ if (match)
{
- if (MAC_MATCHES(&(*i)->mac(), mac_him))
- {
- debug("ARP reply from %.*s %.*s matches\n", PFSTR(fmt(IPAddress(*ipv4_him))), PFSTR(fmt(*mac_him)));
- succeeded(*i);
- }
- else
- {
- // If only the IP matches, check if the test is IP-only
- struct ether_addr zeroAddr;
- bzero(&zeroAddr, sizeof(struct ether_addr));
- if (MAC_MATCHES(&(*i)->mac(), &zeroAddr))
- {
- debug("ARP reply from %.*s %.*s matches\n", PFSTR(fmt(IPAddress(*ipv4_him))), PFSTR(fmt(*mac_him)));
- succeeded(*i);
- }
- }
+ debug("ARP reply from %.*s %.*s matches\n", PFSTR(fmt(IPAddress(*ipv4_him))), PFSTR(fmt(*mac_him)));
+ succeeded(*i);
}
}
}
@@ -90,9 +86,19 @@
// Build and send the arp probe
PacketMaker pm(sender);
- Buffer pkt = pm.makeARPRequest(scan->ip(), scan->source());
+ Buffer pkt;
+
+ if (scan->hasIP())
+ {
+ pkt = pm.makeARPRequest(scan->ip(), scan->source());
+ verbose("Sending 10 ARP probes, 1 every second...\n");
+ }
+ else
+ {
+ pkt = pm.makeARPRequest(scan->mac(), scan->source());
+ verbose("Sending 10 RARP probes, 1 every second...\n");
+ }
- verbose("Sending 10 ARP probes, 1 every second...\n");
// Enqueue the packet for sending
sender.post(pkt, 1000, 10000);
Modified: trunk/src/Scans.h
==============================================================================
--- trunk/src/Scans.h (original)
+++ trunk/src/Scans.h Sat Oct 22 18:21:32 2005
@@ -46,6 +46,8 @@
IPAddress _source;
public:
+ PeerScan(const std::string& name) throw ();
+
PeerScan(const std::string& name, const ether_addr& mac, const IPAddress& ip) throw ()
: Scan(name), _mac(mac), _ip(ip), _source("0.0.0.0") {}
@@ -56,6 +58,14 @@
const IPAddress& ip() const throw () { return _ip; }
const IPAddress& source() const throw () { return _source; }
+ void setMAC(const ether_addr& mac) { _mac = mac; }
+ void setIP(const IPAddress& ip) { _ip = ip; }
+ void setSource(const IPAddress& source) { _source = source; }
+
+ bool hasMAC() const;
+ bool hasIP() const { return _ip != IPAddress("0.0.0.0"); }
+ bool hasSource() const { return _source != IPAddress("0.0.0.0"); }
+
virtual std::string signature() const throw ();
};
Modified: trunk/src/nettypes.cc
==============================================================================
--- trunk/src/nettypes.cc (original)
+++ trunk/src/nettypes.cc Sat Oct 22 18:21:32 2005
@@ -60,6 +60,17 @@
throw ConsistencyCheckException("parsing IP address \"" + str + "\"");
}
+bool IPAddress::operator==(const IPAddress& ip) const
+{
+ return IPv4_MATCHES(&this->addr, &ip.addr);
+}
+
+bool IPAddress::operator!=(const IPAddress& ip) const
+{
+ return !IPv4_MATCHES(&this->addr, &ip.addr);
+}
+
+
/* Parse a MAC address from its canonical string representation */
bool parse_mac(struct ether_addr* target, const string& str) throw ()
{
@@ -78,4 +89,40 @@
}
+#ifdef COMPILE_TESTSUITE
+
+#include <tests/test-utils.h>
+
+namespace tut {
+using namespace tut_guessnet;
+
+struct guessnet_nettypes_shar {
+};
+TESTGRP(guessnet_nettypes);
+
+template<> template<>
+void to::test<1>()
+{
+ IPAddress zero("0.0.0.0");
+ IPAddress host("1.2.3.4");
+
+ gen_ensure(zero == IPAddress("0.0.0.0"));
+ gen_ensure(zero != IPAddress("1.2.3.4"));
+ //ensure_equals(zero, IPAddress("0.0.0.0"));
+ gen_ensure(host == IPAddress("1.2.3.4"));
+ gen_ensure(host != IPAddress("0.0.0.0"));
+ //ensure_equals(host, IPAddress("1.2.3.4"));
+
+ gen_ensure(zero == zero);
+ gen_ensure(host == host);
+ gen_ensure(zero != host);
+
+ gen_ensure(fmt(zero) == "0.0.0.0");
+ gen_ensure(fmt(host) == "1.2.3.4");
+}
+
+}
+
+#endif
+
// vim:set ts=4 sw=4:
Modified: trunk/src/nettypes.h
==============================================================================
--- trunk/src/nettypes.h (original)
+++ trunk/src/nettypes.h Sat Oct 22 18:21:32 2005
@@ -98,6 +98,8 @@
const in_addr_t* s_addr_p() const throw () { return &(addr.s_addr); }
operator const struct in_addr*() const { return &addr; }
+ bool operator==(const IPAddress& addr) const;
+ bool operator!=(const IPAddress& addr) const;
};
/* Format an IPv4 address in a static char buffer */
Modified: trunk/tests/Makefile.am
==============================================================================
--- trunk/tests/Makefile.am (original)
+++ trunk/tests/Makefile.am Sat Oct 22 18:21:32 2005
@@ -1,3 +1,34 @@
+TESTS = guessnet-test
+check_PROGRAMS = guessnet-test
+guessnet_test_SOURCES = tut-main.cpp
+guessnet_test_LDADD = @LIBNET_LIBS@ \
+ ../src/Buffer.o \
+ ../src/ChildProcess.o \
+ ../src/CommandlineParser.o \
+ ../src/DHCPScanner.o \
+ ../src/Environment.o \
+ ../src/Exception.o \
+ ../src/Exec.o \
+ ../src/GuessnetEnvironment.o \
+ ../src/GuessnetParser.o \
+ ../src/IFace.o \
+ ../src/IfaceParser.o \
+ ../src/NetSender.o \
+ ../src/nettypes.o \
+ ../src/NetWatcher.o \
+ ../src/PacketMaker.o \
+ ../src/Parser.o \
+ ../src/PeerScanner.o \
+ ../src/ProcessRunner.o \
+ ../src/Regexp.o \
+ ../src/ScanBag.o \
+ ../src/Scans.o \
+ ../src/ScriptScanner.o \
+ ../src/stringf.o \
+ ../src/Thread.o \
+ ../src/TrafficScanner.o
+
+
#DEFINES := $(shell libnet-config --defines)
#LIBS := $(shell libnet-config --libs) -lpcap -lpthread -lpopt
#CFLAGS := $(shell libnet-config --cflags) $(CFLAGS)
More information about the Guessnet-devel
mailing list