1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
/*
* Sniff network traffic to guess network data, and print it out as
* an /etc/network/interfaces configuration profile
*
* Copyright (C) 2003 Enrico Zini <enrico@debian.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#define APPNAME PACKAGE
#else
#warning No config.h found: using fallback values
#define APPNAME __FILE__
#define VERSION "unknown"
#endif
#include "scanner/TrafficScanner.h"
#include "Environment.h"
#include "IFace.h"
#include <wibble/sys/mutex.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h> /* errno */
#include <string.h> // memcpy
#include <sys/types.h> // socket
#include <sys/socket.h> // socket
#include <sys/ioctl.h> // ioctl
#include <net/if.h>
#include <unistd.h> // close
#include <set>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <wibble/commandline/parser.h>
namespace wibble {
namespace commandline {
struct GuessnetOptions : public StandardParserWithManpage
{
public:
BoolOption* verbose;
BoolOption* debug;
IntOption* timeout;
IntOption* inittime;
GuessnetOptions()
: StandardParserWithManpage(APPNAME, VERSION, 8, "enrico@enricozini.org")
{
usage = "[options] [iface]";
description = "Guess the current network location";
verbose = add<BoolOption>("verbose", 'v', "verbose", "",
"enable verbose output");
debug = add<BoolOption>("debug", 0, "debug", "",
"enable debugging output (including verbose output)");
timeout = add<IntOption>("timeout", 't', "timeout", "seconds",
"timeout (in seconds) used to wait for response packets"
" (defaults to 5 seconds)");
inittime = add<IntOption>("inittime", 0, "init-timeout", "seconds",
"time (in seconds) to wait for the interface to initialize"
" when not found already up (defaults to 3 seconds)");
}
};
}
}
using namespace std;
using namespace wibble::sys;
class MainScanner
{
protected:
Mutex waitMutex;
Condition waitCond;
string name;
// Scanning services
NetSender sender;
NetWatcher watcher;
TrafficScanner trafficScanner;
int cand_count;
inline string fmt_ip(unsigned int ip) throw ()
{
unsigned char ipfmt[4];
memcpy(ipfmt, &ip, 4);
stringstream str;
str << (int)ipfmt[0] << '.' << (int)ipfmt[1] << '.' << (int)ipfmt[2] << '.' << (int)ipfmt[3];
return str.str();
}
inline string fmt_mac(long long int mac) throw ()
{
unsigned char macfmt[6];
memcpy(macfmt, &mac, 6);
stringstream str;
str << hex << setfill('0') << setw(2)
<< (int)macfmt[0] << ':' << (int)macfmt[1] << ':' << (int)macfmt[2] << ':'
<< (int)macfmt[3] << ':' << (int)macfmt[4] << ':' << (int)macfmt[5];
return str.str();
}
unsigned int netaddr_to_netmask(unsigned int na) throw ()
{
unsigned int res = 0;
na = ntohl(na);
// Count the trailing zeros
int i = 0;
for ( ; i < 32 && (na & (1 << i)) == 0; i++)
;
// Add 1s to the start of res
for ( ; i < 32; i++)
res |= (1 << i);
return htonl(res);
}
public:
MainScanner() :
sender(Environment::get().iface()),
watcher(Environment::get().iface()),
trafficScanner(sender),
cand_count(0)
{
watcher.addEthernetListener(&trafficScanner);
}
int candidateCount() const throw () { return cand_count; }
void shutdown()
{
watcher.shutdown();
}
void printResults() throw ()
{
string s;
cout << "iface <name> inet static" << endl;
cout << "\taddress <addr>" << endl;
unsigned int network = trafficScanner.guessed_netaddr;
s = fmt_ip(network);
cout << "\tnetwork " << s << endl;
unsigned int netmask = netaddr_to_netmask(network);
s = fmt_ip(netmask);
cout << "\tnetmask " << s << endl;
s = fmt_ip(network | ~netmask);
cout << "\tbroadcast " << s << endl;
for (set<TrafficScanner::mac_key>::const_iterator i = trafficScanner.guessed_gateways.begin();
i != trafficScanner.guessed_gateways.end(); i++)
{
TrafficScanner::HostData& od = trafficScanner.scanData[*i];
s = fmt_ip(od.addr);
cout << "\tgateway " << s << endl;
string m = fmt_mac(*i);
cout << "\ttest-peer address " << s << " mac " << m << endl;
}
}
};
int main (int argc, const char *argv[])
{
// Access the interface
try {
wibble::commandline::GuessnetOptions opts;
// Process the commandline
if (opts.parse(argc, argv))
return 0;
// Set verbosity
Environment::get().verbose(opts.verbose->boolValue());
Environment::get().debug(opts.debug->boolValue());
// Check user id
if (geteuid() != 0)
fatal_error("You must run this command as root.");
// Find out the interface to be tested
if (opts.hasNext())
Environment::get().iface(opts.next());
// Find out the test timeout
if (opts.timeout->boolValue())
Environment::get().timeout(opts.timeout->intValue());
// Find out the init timeout
if (opts.inittime->boolValue())
Environment::get().initTimeout(opts.inittime->intValue());
IFace iface(Environment::get().iface());
bool iface_was_down;
if_params saved_iface_cfg;
try {
// Install the handler for unexpected exceptions
wibble::exception::InstallUnexpected installUnexpected;
//FILE* input = 0;
/* Check if we have to bring up the interface; if yes, do it */
//iface_was_down = iface_init(Environment::get().iface(), op_init_time);
iface.update();
iface_was_down = !iface.up();
if (iface_was_down)
saved_iface_cfg = iface.initBroadcast(Environment::get().initTimeout());
// Let the signals be caught by some other process
sigset_t sigs, oldsigs;
sigfillset(&sigs);
sigdelset(&sigs, SIGFPE);
sigdelset(&sigs, SIGILL);
sigdelset(&sigs, SIGSEGV);
sigdelset(&sigs, SIGBUS);
sigdelset(&sigs, SIGABRT);
sigdelset(&sigs, SIGIOT);
sigdelset(&sigs, SIGTRAP);
sigdelset(&sigs, SIGSYS);
// Don't block the termination signals: we need them
sigdelset(&sigs, SIGTERM);
sigdelset(&sigs, SIGINT);
sigdelset(&sigs, SIGQUIT);
pthread_sigmask(SIG_BLOCK, &sigs, &oldsigs);
// Scanning methods
MainScanner scanner;
debug("Started test subsystems\n");
sleep(Environment::get().timeout());
scanner.printResults();
/*
// Wait for the first result from the tests
string profile;
if (scanner.candidateCount() > 0)
profile = scanner.getResult(Environment::get().timeout() * 1000);
*/
// Shutdown the tests
scanner.shutdown();
// We've shutdown the threads: restore original signals
pthread_sigmask(SIG_SETMASK, &oldsigs, &sigs);
} catch (std::exception& e) {
fatal_error("%s", e.what());
return 1;
}
/* Bring down the interface if we need it */
if (iface_was_down)
iface.setConfiguration(saved_iface_cfg);
} catch (std::exception& e) {
fatal_error("%s", e.what());
return 1;
}
return 0;
}
// vim:set ts=4 sw=4:
|