detect.cpp

Go to the documentation of this file.
00001 /*
00002  * detect.cpp
00003  *
00004  * Basic host detection
00005  */
00006 
00007 // includes --------------------------------------------------------------------
00008 #include <iostream>
00009 
00010 #include "common/wave_ex.h"
00011 #include "netlib/wavesock.h"
00012 #include "perf/perf.h"
00013 
00014 
00015 
00016 ////////////////////////////////////////////////////////////////////////////////
00017 //
00018 //      static helper methods
00019 //
00020 ////////////////////////////////////////////////////////////////////////////////
00021 
00022 static void
00023 throwError
00024 (
00025 IN const char * msg
00026 )
00027 {
00028         ASSERT(msg, "null");
00029 
00030         const int bufsize = 1024;
00031         char buffer[bufsize];
00032 
00033         netlib::wsGetErrorMessage(buffer, bufsize);
00034 
00035         WAVE_EX(wex);
00036         wex << msg << "\n";
00037         wex << buffer;
00038 }
00039 
00040 
00041 
00042 static void
00043 doDetect
00044 (
00045 void
00046 )
00047 {
00048         const int bufSize = 1023;
00049         char buffer[bufSize + 1];
00050 
00051         if (gethostname(buffer, bufSize)) {
00052                 throwError("failed to get host name");
00053         }
00054 
00055         DPRINTF("Host name: %s", buffer);
00056 
00057         netlib::map_ip_addr_t ips;
00058         getLocalInterfaces(ips);
00059         DPRINTF("Found %d local interfaces", (int) ips.size());
00060         for (netlib::map_ip_addr_t::const_iterator i = ips.begin();
00061              i != ips.end(); ++i) {
00062                 const char * name = i->first.c_str();
00063                 const netlib::ip_addr_t& ip = i->second;
00064                 ip.dump(name);
00065         }
00066 }
00067 
00068 
00069 
00070 ////////////////////////////////////////////////////////////////////////////////
00071 //
00072 //      entry point
00073 //
00074 ////////////////////////////////////////////////////////////////////////////////
00075 
00076 int
00077 main
00078 (
00079 IN int argc,
00080 IN const char * argv[]
00081 )
00082 {
00083         // ASSERT(1 == argc, "Usage: netlib-detect");
00084         int retval = 0;
00085         try {
00086                 perf::Timer timer("overall timer");
00087                 doDetect();
00088         } catch (std::exception& e) {
00089                 DPRINTF("EXCEPTION: %s", e.what());
00090                 retval = 1;
00091         }
00092 
00093         perf::dumpTimingSummary(std::cerr);
00094         return 0;
00095 }
00096