1 /*
    2 
    3 parallel_ntp_scan
    4 =================
    5 
    6 About
    7 -----
    8 Scan a network for NTP servers. For each NTP server found,
    9 investigate how susceptible is is to network traffic amplification.
   10 NTP-based network amplification can be used for DoS attacks, see
   11 https://www.us-cert.gov/ncas/alerts/TA14-013A
   12 
   13 Version/license/...
   14 -------------------
   15 Latest version at:
   16 http://troels.arvin.dk/code/parallel_ntp_scan/
   17 
   18 Copyright 2014 Troels Arvin <troels@arvin.dk>.
   19 License: GPL 3 or later.
   20 See the end of the file for license details.
   21 
   22 Building
   23 --------
   24 The source code has only been compiled on Linux. And you will need 
   25 a Boost installation which has threads enabled (RHEL 6 doesn't).
   26 To a certain extent, you may compile on a newer platform and have it
   27 execute on an older one, provided you compile statically, see below.
   28 For example: If you compile statically on Fedora 20, the binary will
   29 run on RHEL 6 and SLES 11.2 (but not on RHEL 5 and SLES 10).
   30 
   31 Building with dynamic linking:
   32 g++ -O2 -Wall -pedantic \
   33  -lboost_system -lboost_regex -lboost_thread -lboost_filesystem \
   34  -o parallel_ntp_scan parallel_ntp_scan.cc
   35 
   36 Building with static linking:
   37 g++ -O2 -Wall -pedantic -pthread -static \
   38  -o parallel_ntp_scan parallel_ntp_scan.cc \
   39  -lboost_system -lboost_regex -lboost_thread -lboost_filesystem
   40 
   41 Note: If you run the statically compiled version on Linux and turn on 
   42 name resolving, the "nscd" service needs to be running -- otherwise the 
   43 application may segfault, as libnss does not always like to be loaded by
   44 a statically linked application:
   45 https://sourceware.org/bugzilla/show_bug.cgi?id=10652
   46 And on some systems, the statically compiled version will not work at 
   47 all, when name resolving is on. This seems to be similar to what's 
   48 described at 
   49 https://bugs.launchpad.net/ubuntu/+source/eglibc/+bug/647527
   50 
   51 */
   52 
   53 #include <boost/array.hpp>
   54 #include <boost/asio.hpp>
   55 #include <boost/bind.hpp>
   56 #include <boost/filesystem.hpp>
   57 #include <boost/lexical_cast.hpp>
   58 #include <boost/regex.hpp>
   59 #include <boost/thread/thread.hpp>
   60 #include <getopt.h>
   61 
   62 using namespace std;
   63 
   64 void print_version_and_exit()
   65 {
   66     cout << "1.1 ($Revision: 52 $)" << endl;
   67     exit(0);
   68 }
   69 
   70 const static boost::array<unsigned char, 192> loopinfo_payload =
   71 {
   72     0x17, // Flags: NTP version 2 request
   73     0x00, // Auth bit
   74     0x03, // Implementation (XNTPD)
   75     0x08, // Request code = LOOP_INFO
   76     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   77     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
   78     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
   79     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
   80     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
   81     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
   82     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
   83     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
   84     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
   85     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
   86     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
   87     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
   88     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
   89     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
   90     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
   91     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
   92     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
   93     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
   94     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
   95 };
   96 
   97 const static boost::array<unsigned char, 192> monlist_payload =
   98 {
   99     0x17, // Flags: NTP version 2 request
  100     0x00, // Auth bit
  101     0x03, // Implementation (XNTPD)
  102     0x2A, // Request code: MON_GETLIST_1
  103     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  104     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  105     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  106     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  107     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  108     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  109     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  110     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  111     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  112     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  113     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  114     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  115     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  116     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  117     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  118     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  119     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  120     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  121     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  122 };
  123 
  124 static boost::array<unsigned char, 48> query_payload =
  125 {
  126     0xD3, // Flags: NTP version 2 request
  127     0x00, // Peer clock stratum unspecified/invalid
  128     0x03, // Peer polling interval: 3=invalid
  129     0xFA, // Peer clock Precision = 0.015625 sec, seen in packet dump
  130     0x00, 0x01, 0x00, 0x00, // Root delay : 1 sec
  131     0x00, 0x01, 0x00, 0x00, // Root dispersion: 1 sec
  132     0x00, 0x00, 0x00, 0x00, // Reference ID: NULL
  133     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Reference
  134                                                     // timestamp:
  135                                                     // 1900-01-01 00:00:00
  136     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Origin timestamp:
  137                                                     // 1900-01-01 00:00:00
  138     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Received
  139                                                     // timestamp:
  140                                                     // 1900-01-01 00:00:00
  141     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00  // Transmit
  142                                                     // timestamp:
  143                                                     // 1900-01-01 00:00:00
  144 };
  145 
  146 enum skip_status { SKIP_ALL_UNUSUAL , SKIP_FIRST_AND_LAST, SKIP_NONE };
  147 static boost::mutex mtx;
  148 static string argv0;
  149 
  150 // Defaults
  151 const static unsigned long default_num_threads = 20;
  152 const static unsigned short default_timeout = 1; // seconds
  153 static skip_status skip = SKIP_ALL_UNUSUAL;
  154 static bool debug = false;
  155 static bool confirmed = false;
  156 
  157 void err(const string& msg)
  158 {
  159     cerr << "Error: " << msg << endl;
  160     exit(1);
  161 }
  162 
  163 void parse_addr_and_prefix(
  164     const string& cidr_str,
  165     unsigned long& addr,
  166     unsigned int& prefix
  167 )
  168 {
  169     const boost::regex expct_patrn("([[:digit:]]+\\.[[:digit:]]+\\."
  170         "[[:digit:]]+\\.[[:digit:]]+)/([[:digit:]][[:digit:]]?)");
  171     boost::smatch matches;
  172     bool match_found = boost::regex_match(cidr_str, matches, expct_patrn);
  173     string addr_part;
  174     if (!match_found)
  175     {
  176         err("Invalid CIDR expression");
  177     }
  178 
  179     boost::asio::ip::address_v4 address;
  180     try {
  181         address = boost::asio::ip::address_v4::from_string(matches[1]);
  182     }
  183     catch (boost::system::system_error& e)
  184     {
  185         err("Invalid CIDR expression");
  186     }
  187 
  188     addr = address.to_ulong();
  189     istringstream prefix_str(matches[2]);
  190     prefix_str >> prefix;
  191 }
  192 
  193 void first_and_last_addr(
  194     unsigned long addr,         // input
  195     unsigned int prefix,        // input
  196     unsigned long& first_addr,  // output
  197     unsigned long& last_addr    // output
  198 )
  199 {
  200     bitset<32> addr_bs(addr);
  201     unsigned long all_ones =~ 0;
  202     bitset<32> all_ones_bs(all_ones);
  203     bitset<32> mask_bs = all_ones_bs<<= (32-prefix);
  204     bitset<32> first_addr_bs = addr_bs & mask_bs;
  205     bitset<32> work_set_bs = mask_bs.flip();
  206     bitset<32> last_addr_bs = first_addr_bs | work_set_bs;
  207     last_addr = last_addr_bs.to_ulong();
  208     first_addr = first_addr_bs.to_ulong();
  209 }
  210 
  211 void debug_print_addresses_from_prefix(const string& cidr_str)
  212 {
  213     unsigned long int_addr;
  214     unsigned int prefix;
  215     unsigned long first_addr;
  216     unsigned long last_addr;
  217 
  218     parse_addr_and_prefix(cidr_str, int_addr, prefix);
  219     first_and_last_addr(int_addr, prefix, first_addr, last_addr);
  220 
  221     for (unsigned long x = first_addr; x <= last_addr; x++)
  222     {
  223         boost::asio::ip::address_v4 addr = boost::asio::ip::address_v4(x);
  224         bitset<32> as_bitset(x);
  225         cout << "debug: " << x << " " << addr << " " << as_bitset
  226           << endl;
  227     }
  228 }
  229 
  230 // Also prints results of the query
  231 void query(
  232     const unsigned long target,
  233     unsigned short timeout,
  234     bool throttle,
  235     bool display_nonresponders,
  236     bool resolve=false
  237 )
  238 {
  239     int sock;
  240     ssize_t bytes_recv;
  241     struct sockaddr_in target_addr, local_addr;
  242     socklen_t sin_size = (socklen_t)sizeof(target_addr);
  243     char recv_data[10];
  244     const void *payload;
  245     size_t payload_size;
  246     short res = 0;
  247 
  248     // Fill out target address
  249     memset(&target_addr, 0, sizeof(target_addr));
  250     target_addr.sin_family = AF_INET;
  251     target_addr.sin_port = htons(123);
  252     in_addr ia;
  253     ia.s_addr = htonl(target);
  254     target_addr.sin_addr = ia;
  255 
  256     // Create socket, and set a timeout on it
  257     sock=socket(AF_INET,SOCK_DGRAM,0);
  258     struct timeval tv;
  259     tv.tv_sec = timeout;
  260     tv.tv_usec = 0;
  261     if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv)) < 0) {
  262         perror("Error");
  263     }
  264 
  265     if (throttle)
  266     {
  267         int msecs = rand() % 20;
  268         if (debug)
  269         {
  270             cout << "debug: sleeping for " << msecs << " ms" << endl;
  271         }
  272         boost::this_thread::sleep(boost::posix_time::milliseconds(msecs));
  273     }
  274 
  275     // First query step: A basic time query
  276     payload = &query_payload;
  277     payload_size = sizeof(query_payload);
  278     sendto(sock,payload,payload_size,0,
  279              (const struct sockaddr *)&target_addr,sin_size);
  280     bytes_recv = recvfrom(sock, recv_data, sizeof(recv_data), 0,
  281         (struct sockaddr *)&local_addr,&sin_size);
  282     if (bytes_recv > 0)
  283     {
  284         res = 1;
  285 
  286         // Next, see if a loopinfo query succeeds
  287         payload = &loopinfo_payload;
  288         payload_size = sizeof(loopinfo_payload);
  289         sendto(sock,payload,payload_size,0,
  290                  (const struct sockaddr *)&target_addr,sin_size);
  291         bytes_recv = recvfrom(sock, recv_data, sizeof(recv_data), 0,
  292             (struct sockaddr *)&local_addr,&sin_size);
  293         if (bytes_recv > 0) { res = 2; }
  294 
  295         // Finally, see if a monlist query succeeds
  296         payload = &monlist_payload;
  297         payload_size = sizeof(monlist_payload);
  298         sendto(sock,payload,payload_size,0,
  299                  (const struct sockaddr *)&target_addr,sin_size);
  300         bytes_recv = recvfrom(sock, recv_data, sizeof(recv_data), 0,
  301             (struct sockaddr *)&local_addr,&sin_size);
  302         if (bytes_recv > 8) { res=3; }
  303     }
  304 
  305     close(sock);
  306 
  307     // output result, if needed
  308     char hostname[255];
  309     stringstream add_to_addr("");
  310     if (display_nonresponders || res!=0)
  311     {
  312         // NS lookup
  313         if (resolve)
  314         {
  315             int error = getnameinfo
  316             (
  317                 (struct sockaddr*)&target_addr,
  318                 sizeof target_addr,
  319                 hostname,
  320                 sizeof hostname,
  321                 NULL,
  322                 0,
  323                 0
  324             );
  325             if (error != 0)
  326             {
  327                 err("Could not perform getnameinfo");   
  328             }
  329             add_to_addr << '\t' << hostname;
  330         }
  331 
  332         boost::asio::ip::address_v4 as_addr = 
  333             boost::asio::ip::address_v4(target);
  334         boost::lock_guard<boost::mutex> guard(mtx);
  335         cout << target << '\t' << as_addr << add_to_addr.str() << '\t'
  336           << res << endl;
  337     }
  338 }
  339 
  340 // Helper function which determines whether an address
  341 // is either the first or the last address in a /24
  342 // network.
  343 // Negated operation, in order to be easy to use in the 
  344 // specific case where it's used.
  345 bool is_not_c_class_first_or_last(unsigned long addr_int)
  346 {
  347     unsigned short last_octet = addr_int & 255;
  348     if (0 == last_octet || 255 == last_octet)
  349     {
  350         return false;
  351     }
  352     else
  353     {
  354         return true;
  355     }
  356 }
  357 
  358 // Sets up a thread pool and assigns work to it.
  359 void scan_and_print(
  360     const string& cidr_str,
  361     unsigned long num_threads,
  362     unsigned short timeout,
  363     bool display_nonresponders,
  364     bool resolve_to_hostnames
  365 )
  366 {
  367     unsigned long addr;
  368     unsigned int prefix;
  369     unsigned long first_addr;
  370     unsigned long last_addr;
  371     unsigned long num_addresses_to_check;
  372     if (debug)
  373     {
  374         cout << "debug: num_threads asked for: " << num_threads << endl;
  375     }
  376 
  377     parse_addr_and_prefix(cidr_str, addr, prefix);
  378     first_and_last_addr(addr, prefix, first_addr, last_addr);
  379     num_addresses_to_check = last_addr - first_addr + 1;
  380     if (num_addresses_to_check < num_threads)
  381     {
  382         num_threads = num_addresses_to_check;
  383     }
  384     if (debug)
  385     {
  386         cout << "debug: num_threads, effectively: " << num_threads
  387           << endl;
  388     }
  389     if (debug)
  390     {
  391         cout << "debug: number of addresses to potentially check: "
  392           << num_addresses_to_check << endl;
  393     }
  394 
  395     boost::asio::io_service io_srv;
  396     boost::thread_group threadpool;
  397     {
  398         boost::asio::io_service::work work(io_srv);
  399         for (unsigned int n = 0; n < num_threads; n++)
  400         {
  401             threadpool.create_thread(
  402                 boost::bind(&boost::asio::io_service::run, &io_srv)
  403             );
  404         }
  405 
  406         // At first, tell the query function to throttle for a short,
  407         // random period.
  408         unsigned long querycount = 0;
  409         bool throttle = true;
  410         // -- unless there's no risk of sending too many packages
  411         // simultaneously:
  412         if (num_threads < 30 || num_addresses_to_check < 30)
  413         {
  414             throttle = false;
  415         }
  416 
  417         for (unsigned long x = first_addr; x <= last_addr; x++)
  418         {
  419             if
  420             (
  421                 skip==SKIP_NONE
  422                 ||
  423                 num_addresses_to_check < 3
  424                 ||
  425                 (
  426                     skip==SKIP_FIRST_AND_LAST
  427                     &&
  428                     (x != first_addr && x != last_addr)
  429                 )
  430                 ||
  431                 (
  432                     skip==SKIP_ALL_UNUSUAL
  433                     &&
  434                     (
  435                         x != first_addr
  436                         &&
  437                         x != last_addr
  438                         &&
  439                         is_not_c_class_first_or_last(x)
  440                     )
  441                 )
  442             )
  443             {
  444                 if (throttle && querycount < num_threads)
  445                 {
  446                     throttle = true;
  447                 }
  448                 else
  449                 {
  450                     throttle = false;
  451                 }
  452                 io_srv.post(
  453                     boost::bind(
  454                         query,
  455                         x,
  456                         timeout,
  457                         throttle,
  458                         display_nonresponders,
  459                         resolve_to_hostnames
  460                     )
  461                 );
  462                 querycount++;
  463             }
  464             else
  465             {
  466                 if (debug)
  467                 {
  468                     cout
  469                       << "skipped " << boost::asio::ip::address_v4(x)
  470                       << ", as it is the first or last address in the "
  471                       << "network and/or c-class, and the network "
  472                       << "consists of more than two hosts"
  473                       << endl
  474                     ;
  475                 }
  476             }
  477         }
  478     }
  479     threadpool.join_all();
  480     io_srv.stop();
  481 }
  482 
  483 void usage(bool exit_error=true)
  484 {
  485     boost::filesystem::path p(argv0);
  486     stringstream msg;
  487     string this_binary(p.filename().string());
  488     msg
  489      << "Usage:" << endl
  490      << " " << this_binary << " [options...] addr/mask [threads]" << endl
  491      << endl
  492      << " addr   : IP addresses in dotted notation. Only IPv4 is "
  493             "supported." << endl
  494      << " mask   : Indicated as an address prefix, such " << endl
  495      << "          as 24 (corresponding to 255.255.255.0)." << endl
  496      << " threads: Use up to this number of threads. Default: " << 
  497             default_num_threads << "." << endl
  498      << "          If you set this too high (e.g. more than 1000, " << endl
  499      << "          depending on OS settings), the application will" << endl
  500      << "          immediately fail, complaining about lack of " << endl
  501      << "          ressources." << endl
  502      << endl
  503      << "Output has three or four columns, separated by tabs." << endl
  504      << endl
  505      << "If the resolv_to_hostnames option is NOT used," << endl
  506      << "the three columns are:" << endl
  507      << "int\taddr\tresult" <<endl
  508      << endl
  509      << "If the resolv_to_hostnames option is used," << endl
  510      << "the three columns are:" << endl
  511      << "int\taddr\thostname\tresult" << endl
  512      << endl
  513      << "The output values represent:" << endl
  514      << " int     : 32-bit integer representation of the address (useful"
  515             << endl
  516      << "           for subsequent sorting of the output)." << endl
  517      << " addr :    IP address in dotted notation." << endl
  518      << " hostname: The resolved hostname. If the IP address could not"
  519             << endl
  520      << "           be resolved to a name, this column will contain"
  521             << endl
  522      << "           the IP address, once again." << endl
  523      << " result  : 0, 1, 2 or 3, meaning:" << endl
  524      << "             0: The host did not responded to any NTP queries."
  525             << endl
  526      << "                Such hosts are only mentioned, if the -n option"
  527             << endl
  528      << "                is used." << endl
  529      << "             1: The host responded to a basic NTP time query,"
  530             << endl
  531      << "                but none of the other queries. OK." << endl
  532      << "             2: The host responded to a basic NTP time query,"
  533             << endl
  534      << "                and a 'loopinfo' query, but not a 'monlist' "
  535             "query." << endl
  536      << "                Such a host has limited potential for "
  537             "amplification." << endl
  538      << "             3: In addition to a basic NTP time query, the host "
  539             "responded" << endl
  540      << "                to a 'monlist' (and possibly a 'loopinfo') "
  541             "query. Such a" << endl
  542      << "                host is risky, as it may tricked into "
  543             "participating in" << endl
  544      << "                amplification attacks." << endl
  545      << endl
  546      << "Options:" << endl
  547      << " --debug|-d                      : Print various debugging "
  548             "information." << endl
  549      << " --display_nonresponders|-n      : Output information about "
  550             "addresses " << endl
  551      << "                                   which don't respond." << endl
  552      << " --help|h                        : Display this message and "
  553             "exit." << endl
  554      << " --resolve_to_hostnames|-r       : Resolve IP addresses to "
  555             << "hostnames, if" << endl
  556      << "                                   possible." << endl
  557      << " --skip|-s <none|first_and_last> : By default, addresses ending "
  558             "with 0 or" << endl
  559      << "                                   255 are not scanned; neither "
  560             "are the " << endl
  561      << "                                   first and last addresses in "
  562             "a network." << endl
  563      << "                                   Allowed values:" << endl
  564      << "                                     none          : No "
  565             "addresses are" << endl
  566      << "                                                     skipped."
  567             << endl
  568      << "                                     first_and_last: Only the "
  569             << "first and" << endl
  570      << "                                                     last "
  571             << "addresses of a" << endl
  572      << "                                                     network "
  573             << "are skipped." << endl
  574      << " --timeout|-t                    : How long to wait for "
  575             "responses." << endl
  576      << "                                   Unit: Seconds. Default: "
  577             << default_timeout << "." << endl
  578      << " --version|v                     : Display version and exit."
  579             << endl
  580      << " --yes|y                         : Don't ask for confirmation."
  581      << endl
  582      << "Examples:" << endl
  583      << endl
  584      << "Scan 192.168.1.1 through 192.168.1.254, using 100 threads:"
  585             << endl
  586      << " ./" << this_binary << " 192.168.1.0/24 100" << endl
  587      << endl
  588      << "Scan 192.168.0.1 through 192.168.1.254 (including 192.168.0."
  589             "255 " << endl
  590      << "and 192.168.1.0):" << endl
  591      << " ./" << this_binary << " 192.168.0.0/23" << endl
  592      << endl
  593      << "Scan 192.168.1.0 through 192.168.1.255, using default thread-"
  594             "count (" <<  default_num_threads << "):" << endl
  595      << " ./" << this_binary << " --skip none 192.168.1.0/24" << endl
  596     ;
  597     if (exit_error)
  598     {
  599         cerr << msg.str();
  600         exit(1);
  601     }
  602     else
  603     {
  604         cout << msg.str();
  605     }
  606 }
  607 
  608 int main(int argc, char* argv[])
  609 {
  610     argv0 = argv[0];
  611     bool display_nonresponders = false;
  612     int shortopt;
  613     bool resolve_to_hostnames = false;
  614     unsigned short timeout = default_timeout;
  615 
  616     srand (time(NULL));
  617 
  618     while (1)
  619     {
  620         int opt_idx = 0;
  621         static struct option long_options[] = {
  622             {"debug",                 no_argument,       0, 'd' },
  623             {"help",                  no_argument,       0, 'h' },
  624             {"display_nonresponders", no_argument,       0, 'n' },
  625             {"resolve_to_hostnames",  no_argument,       0, 'r' },
  626             {"skip",                  required_argument, 0, 's' },
  627             {"timeout",               required_argument, 0, 't' },
  628             {"version",               no_argument,       0, 'v' },
  629             {"yes",                   no_argument,       0, 'y' },
  630             {0,                       0,                 0,  0  }
  631         };
  632 
  633         shortopt = getopt_long(
  634             argc,
  635             argv,
  636             "hdnrs:t:vy",
  637             long_options,
  638             &opt_idx
  639         );
  640         if (shortopt == -1)
  641             break;
  642 
  643         switch(shortopt)
  644         {
  645             case 0:
  646                 printf("option %s", long_options[opt_idx].name);
  647                 if (optarg)
  648                     printf(" with arg %s", optarg);
  649                 printf("\n");
  650                 break;
  651             case 'v':
  652                 print_version_and_exit();
  653             case 'd':
  654                 debug = true;
  655                 break;
  656             case 'h':
  657                 usage(false);
  658                 exit(0);
  659             case 'n':
  660                 display_nonresponders = true;
  661                 break;
  662             case 'r':
  663                 resolve_to_hostnames = true;
  664                 break;
  665             case 's':
  666                 if (strcmp(optarg,"none") == 0)
  667                 {
  668                     skip = SKIP_NONE;
  669                 }
  670                 if (strcmp(optarg,"first_and_last") == 0)
  671                 {
  672                     skip = SKIP_FIRST_AND_LAST;
  673                 }
  674                 break;
  675             case 't':
  676                 if (string(optarg).find_first_not_of("0123456789")!=
  677                     string::npos)
  678                 {
  679                     err("Invalid timeout");
  680                 }
  681                 try
  682                 {
  683                     timeout = boost::lexical_cast<unsigned short>(optarg);
  684                 }
  685                 catch(boost::bad_lexical_cast&)
  686                 {
  687                     err("Could not parse the timeout option as an unsigned short");
  688                 }
  689                 if (timeout < 1)
  690                 {
  691                     err("Timeout must be at least 1 (seconds)");
  692                 }
  693                 break;
  694             case 'y':
  695                 confirmed = true;
  696                 break;
  697             default:
  698                 usage();
  699         }
  700     }
  701     int optdiff = argc-optind;
  702     if (optdiff == 0 || optdiff > 2)
  703     {
  704         usage();
  705     }
  706     string address_and_prefix(argv[optind]);
  707 
  708     unsigned long num_threads = default_num_threads;
  709     if (optdiff == 2)
  710     {
  711         string num_threads_input(argv[optind+1]);
  712         if (num_threads_input.find_first_not_of("0123456789")!=
  713             string::npos)
  714         {
  715             err("Invalid number of threads");
  716         }
  717         istringstream (num_threads_input) >> num_threads;
  718         if (0==num_threads)
  719         {
  720             err("number of threads must be at least 0");
  721         }
  722     }
  723 
  724     if (!confirmed)
  725     {
  726         cerr
  727           << "Note: Some may find it offensive to scan their network, so "
  728                 "make sure " << endl
  729           << "you ask for permission before you scan, if you are not "
  730                 "the administrator " << endl
  731           << "of the indicated network." << endl
  732           << endl
  733           << "Enter 'y' to continue. "
  734         ;
  735         string user_in;
  736         cin >> user_in;
  737         if (user_in != "y")
  738         {
  739             err("Aborted");
  740         }
  741     }
  742 
  743     if (debug)
  744     {
  745         debug_print_addresses_from_prefix(address_and_prefix);
  746     }
  747     scan_and_print(
  748         address_and_prefix,
  749         num_threads,
  750         timeout,
  751         display_nonresponders,
  752         resolve_to_hostnames
  753     );
  754 
  755     exit(0);
  756 }
  757 
  758 /*
  759 
  760 History
  761 =======
  762 
  763 1.1, 2014-03-23: Add timeout option. Fixed logic, so that monlist
  764                  is checked, even though loopinfo might not respond.
  765 
  766 1.0, 2014-03-20: Initial release.
  767 
  768 */
  769 
  770 
  771 
  772 /*
  773 License details:
  774 
  775     This program is free software: you can redistribute it and/or modify
  776     it under the terms of the GNU General Public License as published by
  777     the Free Software Foundation, either version 3 of the License, or
  778     (at your option) any later version.
  779 
  780     This program is distributed in the hope that it will be useful,
  781     but WITHOUT ANY WARRANTY; without even the implied warranty of
  782     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  783     GNU General Public License for more details:
  784 
  785 
  786                     GNU GENERAL PUBLIC LICENSE
  787                        Version 3, 29 June 2007
  788 
  789  Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
  790  Everyone is permitted to copy and distribute verbatim copies
  791  of this license document, but changing it is not allowed.
  792 
  793                             Preamble
  794 
  795   The GNU General Public License is a free, copyleft license for
  796 software and other kinds of works.
  797 
  798   The licenses for most software and other practical works are designed
  799 to take away your freedom to share and change the works.  By contrast,
  800 the GNU General Public License is intended to guarantee your freedom to
  801 share and change all versions of a program--to make sure it remains free
  802 software for all its users.  We, the Free Software Foundation, use the
  803 GNU General Public License for most of our software; it applies also to
  804 any other work released this way by its authors.  You can apply it to
  805 your programs, too.
  806 
  807   When we speak of free software, we are referring to freedom, not
  808 price.  Our General Public Licenses are designed to make sure that you
  809 have the freedom to distribute copies of free software (and charge for
  810 them if you wish), that you receive source code or can get it if you
  811 want it, that you can change the software or use pieces of it in new
  812 free programs, and that you know you can do these things.
  813 
  814   To protect your rights, we need to prevent others from denying you
  815 these rights or asking you to surrender the rights.  Therefore, you have
  816 certain responsibilities if you distribute copies of the software, or if
  817 you modify it: responsibilities to respect the freedom of others.
  818 
  819   For example, if you distribute copies of such a program, whether
  820 gratis or for a fee, you must pass on to the recipients the same
  821 freedoms that you received.  You must make sure that they, too, receive
  822 or can get the source code.  And you must show them these terms so they
  823 know their rights.
  824 
  825   Developers that use the GNU GPL protect your rights with two steps:
  826 (1) assert copyright on the software, and (2) offer you this License
  827 giving you legal permission to copy, distribute and/or modify it.
  828 
  829   For the developers' and authors' protection, the GPL clearly explains
  830 that there is no warranty for this free software.  For both users' and
  831 authors' sake, the GPL requires that modified versions be marked as
  832 changed, so that their problems will not be attributed erroneously to
  833 authors of previous versions.
  834 
  835   Some devices are designed to deny users access to install or run
  836 modified versions of the software inside them, although the manufacturer
  837 can do so.  This is fundamentally incompatible with the aim of
  838 protecting users' freedom to change the software.  The systematic
  839 pattern of such abuse occurs in the area of products for individuals to
  840 use, which is precisely where it is most unacceptable.  Therefore, we
  841 have designed this version of the GPL to prohibit the practice for those
  842 products.  If such problems arise substantially in other domains, we
  843 stand ready to extend this provision to those domains in future versions
  844 of the GPL, as needed to protect the freedom of users.
  845 
  846   Finally, every program is threatened constantly by software patents.
  847 States should not allow patents to restrict development and use of
  848 software on general-purpose computers, but in those that do, we wish to
  849 avoid the special danger that patents applied to a free program could
  850 make it effectively proprietary.  To prevent this, the GPL assures that
  851 patents cannot be used to render the program non-free.
  852 
  853   The precise terms and conditions for copying, distribution and
  854 modification follow.
  855 
  856                        TERMS AND CONDITIONS
  857 
  858   0. Definitions.
  859 
  860   "This License" refers to version 3 of the GNU General Public License.
  861 
  862   "Copyright" also means copyright-like laws that apply to other kinds of
  863 works, such as semiconductor masks.
  864 
  865   "The Program" refers to any copyrightable work licensed under this
  866 License.  Each licensee is addressed as "you".  "Licensees" and
  867 "recipients" may be individuals or organizations.
  868 
  869   To "modify" a work means to copy from or adapt all or part of the work
  870 in a fashion requiring copyright permission, other than the making of an
  871 exact copy.  The resulting work is called a "modified version" of the
  872 earlier work or a work "based on" the earlier work.
  873 
  874   A "covered work" means either the unmodified Program or a work based
  875 on the Program.
  876 
  877   To "propagate" a work means to do anything with it that, without
  878 permission, would make you directly or secondarily liable for
  879 infringement under applicable copyright law, except executing it on a
  880 computer or modifying a private copy.  Propagation includes copying,
  881 distribution (with or without modification), making available to the
  882 public, and in some countries other activities as well.
  883 
  884   To "convey" a work means any kind of propagation that enables other
  885 parties to make or receive copies.  Mere interaction with a user through
  886 a computer network, with no transfer of a copy, is not conveying.
  887 
  888   An interactive user interface displays "Appropriate Legal Notices"
  889 to the extent that it includes a convenient and prominently visible
  890 feature that (1) displays an appropriate copyright notice, and (2)
  891 tells the user that there is no warranty for the work (except to the
  892 extent that warranties are provided), that licensees may convey the
  893 work under this License, and how to view a copy of this License.  If
  894 the interface presents a list of user commands or options, such as a
  895 menu, a prominent item in the list meets this criterion.
  896 
  897   1. Source Code.
  898 
  899   The "source code" for a work means the preferred form of the work
  900 for making modifications to it.  "Object code" means any non-source
  901 form of a work.
  902 
  903   A "Standard Interface" means an interface that either is an official
  904 standard defined by a recognized standards body, or, in the case of
  905 interfaces specified for a particular programming language, one that
  906 is widely used among developers working in that language.
  907 
  908   The "System Libraries" of an executable work include anything, other
  909 than the work as a whole, that (a) is included in the normal form of
  910 packaging a Major Component, but which is not part of that Major
  911 Component, and (b) serves only to enable use of the work with that
  912 Major Component, or to implement a Standard Interface for which an
  913 implementation is available to the public in source code form.  A
  914 "Major Component", in this context, means a major essential component
  915 (kernel, window system, and so on) of the specific operating system
  916 (if any) on which the executable work runs, or a compiler used to
  917 produce the work, or an object code interpreter used to run it.
  918 
  919   The "Corresponding Source" for a work in object code form means all
  920 the source code needed to generate, install, and (for an executable
  921 work) run the object code and to modify the work, including scripts to
  922 control those activities.  However, it does not include the work's
  923 System Libraries, or general-purpose tools or generally available free
  924 programs which are used unmodified in performing those activities but
  925 which are not part of the work.  For example, Corresponding Source
  926 includes interface definition files associated with source files for
  927 the work, and the source code for shared libraries and dynamically
  928 linked subprograms that the work is specifically designed to require,
  929 such as by intimate data communication or control flow between those
  930 subprograms and other parts of the work.
  931 
  932   The Corresponding Source need not include anything that users
  933 can regenerate automatically from other parts of the Corresponding
  934 Source.
  935 
  936   The Corresponding Source for a work in source code form is that
  937 same work.
  938 
  939   2. Basic Permissions.
  940 
  941   All rights granted under this License are granted for the term of
  942 copyright on the Program, and are irrevocable provided the stated
  943 conditions are met.  This License explicitly affirms your unlimited
  944 permission to run the unmodified Program.  The output from running a
  945 covered work is covered by this License only if the output, given its
  946 content, constitutes a covered work.  This License acknowledges your
  947 rights of fair use or other equivalent, as provided by copyright law.
  948 
  949   You may make, run and propagate covered works that you do not
  950 convey, without conditions so long as your license otherwise remains
  951 in force.  You may convey covered works to others for the sole purpose
  952 of having them make modifications exclusively for you, or provide you
  953 with facilities for running those works, provided that you comply with
  954 the terms of this License in conveying all material for which you do
  955 not control copyright.  Those thus making or running the covered works
  956 for you must do so exclusively on your behalf, under your direction
  957 and control, on terms that prohibit them from making any copies of
  958 your copyrighted material outside their relationship with you.
  959 
  960   Conveying under any other circumstances is permitted solely under
  961 the conditions stated below.  Sublicensing is not allowed; section 10
  962 makes it unnecessary.
  963 
  964   3. Protecting Users' Legal Rights From Anti-Circumvention Law.
  965 
  966   No covered work shall be deemed part of an effective technological
  967 measure under any applicable law fulfilling obligations under article
  968 11 of the WIPO copyright treaty adopted on 20 December 1996, or
  969 similar laws prohibiting or restricting circumvention of such
  970 measures.
  971 
  972   When you convey a covered work, you waive any legal power to forbid
  973 circumvention of technological measures to the extent such circumvention
  974 is effected by exercising rights under this License with respect to
  975 the covered work, and you disclaim any intention to limit operation or
  976 modification of the work as a means of enforcing, against the work's
  977 users, your or third parties' legal rights to forbid circumvention of
  978 technological measures.
  979 
  980   4. Conveying Verbatim Copies.
  981 
  982   You may convey verbatim copies of the Program's source code as you
  983 receive it, in any medium, provided that you conspicuously and
  984 appropriately publish on each copy an appropriate copyright notice;
  985 keep intact all notices stating that this License and any
  986 non-permissive terms added in accord with section 7 apply to the code;
  987 keep intact all notices of the absence of any warranty; and give all
  988 recipients a copy of this License along with the Program.
  989 
  990   You may charge any price or no price for each copy that you convey,
  991 and you may offer support or warranty protection for a fee.
  992 
  993   5. Conveying Modified Source Versions.
  994 
  995   You may convey a work based on the Program, or the modifications to
  996 produce it from the Program, in the form of source code under the
  997 terms of section 4, provided that you also meet all of these conditions:
  998 
  999     a) The work must carry prominent notices stating that you modified
 1000     it, and giving a relevant date.
 1001 
 1002     b) The work must carry prominent notices stating that it is
 1003     released under this License and any conditions added under section
 1004     7.  This requirement modifies the requirement in section 4 to
 1005     "keep intact all notices".
 1006 
 1007     c) You must license the entire work, as a whole, under this
 1008     License to anyone who comes into possession of a copy.  This
 1009     License will therefore apply, along with any applicable section 7
 1010     additional terms, to the whole of the work, and all its parts,
 1011     regardless of how they are packaged.  This License gives no
 1012     permission to license the work in any other way, but it does not
 1013     invalidate such permission if you have separately received it.
 1014 
 1015     d) If the work has interactive user interfaces, each must display
 1016     Appropriate Legal Notices; however, if the Program has interactive
 1017     interfaces that do not display Appropriate Legal Notices, your
 1018     work need not make them do so.
 1019 
 1020   A compilation of a covered work with other separate and independent
 1021 works, which are not by their nature extensions of the covered work,
 1022 and which are not combined with it such as to form a larger program,
 1023 in or on a volume of a storage or distribution medium, is called an
 1024 "aggregate" if the compilation and its resulting copyright are not
 1025 used to limit the access or legal rights of the compilation's users
 1026 beyond what the individual works permit.  Inclusion of a covered work
 1027 in an aggregate does not cause this License to apply to the other
 1028 parts of the aggregate.
 1029 
 1030   6. Conveying Non-Source Forms.
 1031 
 1032   You may convey a covered work in object code form under the terms
 1033 of sections 4 and 5, provided that you also convey the
 1034 machine-readable Corresponding Source under the terms of this License,
 1035 in one of these ways:
 1036 
 1037     a) Convey the object code in, or embodied in, a physical product
 1038     (including a physical distribution medium), accompanied by the
 1039     Corresponding Source fixed on a durable physical medium
 1040     customarily used for software interchange.
 1041 
 1042     b) Convey the object code in, or embodied in, a physical product
 1043     (including a physical distribution medium), accompanied by a
 1044     written offer, valid for at least three years and valid for as
 1045     long as you offer spare parts or customer support for that product
 1046     model, to give anyone who possesses the object code either (1) a
 1047     copy of the Corresponding Source for all the software in the
 1048     product that is covered by this License, on a durable physical
 1049     medium customarily used for software interchange, for a price no
 1050     more than your reasonable cost of physically performing this
 1051     conveying of source, or (2) access to copy the
 1052     Corresponding Source from a network server at no charge.
 1053 
 1054     c) Convey individual copies of the object code with a copy of the
 1055     written offer to provide the Corresponding Source.  This
 1056     alternative is allowed only occasionally and noncommercially, and
 1057     only if you received the object code with such an offer, in accord
 1058     with subsection 6b.
 1059 
 1060     d) Convey the object code by offering access from a designated
 1061     place (gratis or for a charge), and offer equivalent access to the
 1062     Corresponding Source in the same way through the same place at no
 1063     further charge.  You need not require recipients to copy the
 1064     Corresponding Source along with the object code.  If the place to
 1065     copy the object code is a network server, the Corresponding Source
 1066     may be on a different server (operated by you or a third party)
 1067     that supports equivalent copying facilities, provided you maintain
 1068     clear directions next to the object code saying where to find the
 1069     Corresponding Source.  Regardless of what server hosts the
 1070     Corresponding Source, you remain obligated to ensure that it is
 1071     available for as long as needed to satisfy these requirements.
 1072 
 1073     e) Convey the object code using peer-to-peer transmission, provided
 1074     you inform other peers where the object code and Corresponding
 1075     Source of the work are being offered to the general public at no
 1076     charge under subsection 6d.
 1077 
 1078   A separable portion of the object code, whose source code is excluded
 1079 from the Corresponding Source as a System Library, need not be
 1080 included in conveying the object code work.
 1081 
 1082   A "User Product" is either (1) a "consumer product", which means any
 1083 tangible personal property which is normally used for personal, family,
 1084 or household purposes, or (2) anything designed or sold for incorporation
 1085 into a dwelling.  In determining whether a product is a consumer product,
 1086 doubtful cases shall be resolved in favor of coverage.  For a particular
 1087 product received by a particular user, "normally used" refers to a
 1088 typical or common use of that class of product, regardless of the status
 1089 of the particular user or of the way in which the particular user
 1090 actually uses, or expects or is expected to use, the product.  A product
 1091 is a consumer product regardless of whether the product has substantial
 1092 commercial, industrial or non-consumer uses, unless such uses represent
 1093 the only significant mode of use of the product.
 1094 
 1095   "Installation Information" for a User Product means any methods,
 1096 procedures, authorization keys, or other information required to install
 1097 and execute modified versions of a covered work in that User Product from
 1098 a modified version of its Corresponding Source.  The information must
 1099 suffice to ensure that the continued functioning of the modified object
 1100 code is in no case prevented or interfered with solely because
 1101 modification has been made.
 1102 
 1103   If you convey an object code work under this section in, or with, or
 1104 specifically for use in, a User Product, and the conveying occurs as
 1105 part of a transaction in which the right of possession and use of the
 1106 User Product is transferred to the recipient in perpetuity or for a
 1107 fixed term (regardless of how the transaction is characterized), the
 1108 Corresponding Source conveyed under this section must be accompanied
 1109 by the Installation Information.  But this requirement does not apply
 1110 if neither you nor any third party retains the ability to install
 1111 modified object code on the User Product (for example, the work has
 1112 been installed in ROM).
 1113 
 1114   The requirement to provide Installation Information does not include a
 1115 requirement to continue to provide support service, warranty, or updates
 1116 for a work that has been modified or installed by the recipient, or for
 1117 the User Product in which it has been modified or installed.  Access to a
 1118 network may be denied when the modification itself materially and
 1119 adversely affects the operation of the network or violates the rules and
 1120 protocols for communication across the network.
 1121 
 1122   Corresponding Source conveyed, and Installation Information provided,
 1123 in accord with this section must be in a format that is publicly
 1124 documented (and with an implementation available to the public in
 1125 source code form), and must require no special password or key for
 1126 unpacking, reading or copying.
 1127 
 1128   7. Additional Terms.
 1129 
 1130   "Additional permissions" are terms that supplement the terms of this
 1131 License by making exceptions from one or more of its conditions.
 1132 Additional permissions that are applicable to the entire Program shall
 1133 be treated as though they were included in this License, to the extent
 1134 that they are valid under applicable law.  If additional permissions
 1135 apply only to part of the Program, that part may be used separately
 1136 under those permissions, but the entire Program remains governed by
 1137 this License without regard to the additional permissions.
 1138 
 1139   When you convey a copy of a covered work, you may at your option
 1140 remove any additional permissions from that copy, or from any part of
 1141 it.  (Additional permissions may be written to require their own
 1142 removal in certain cases when you modify the work.)  You may place
 1143 additional permissions on material, added by you to a covered work,
 1144 for which you have or can give appropriate copyright permission.
 1145 
 1146   Notwithstanding any other provision of this License, for material you
 1147 add to a covered work, you may (if authorized by the copyright holders of
 1148 that material) supplement the terms of this License with terms:
 1149 
 1150     a) Disclaiming warranty or limiting liability differently from the
 1151     terms of sections 15 and 16 of this License; or
 1152 
 1153     b) Requiring preservation of specified reasonable legal notices or
 1154     author attributions in that material or in the Appropriate Legal
 1155     Notices displayed by works containing it; or
 1156 
 1157     c) Prohibiting misrepresentation of the origin of that material, or
 1158     requiring that modified versions of such material be marked in
 1159     reasonable ways as different from the original version; or
 1160 
 1161     d) Limiting the use for publicity purposes of names of licensors or
 1162     authors of the material; or
 1163 
 1164     e) Declining to grant rights under trademark law for use of some
 1165     trade names, trademarks, or service marks; or
 1166 
 1167     f) Requiring indemnification of licensors and authors of that
 1168     material by anyone who conveys the material (or modified versions of
 1169     it) with contractual assumptions of liability to the recipient, for
 1170     any liability that these contractual assumptions directly impose on
 1171     those licensors and authors.
 1172 
 1173   All other non-permissive additional terms are considered "further
 1174 restrictions" within the meaning of section 10.  If the Program as you
 1175 received it, or any part of it, contains a notice stating that it is
 1176 governed by this License along with a term that is a further
 1177 restriction, you may remove that term.  If a license document contains
 1178 a further restriction but permits relicensing or conveying under this
 1179 License, you may add to a covered work material governed by the terms
 1180 of that license document, provided that the further restriction does
 1181 not survive such relicensing or conveying.
 1182 
 1183   If you add terms to a covered work in accord with this section, you
 1184 must place, in the relevant source files, a statement of the
 1185 additional terms that apply to those files, or a notice indicating
 1186 where to find the applicable terms.
 1187 
 1188   Additional terms, permissive or non-permissive, may be stated in the
 1189 form of a separately written license, or stated as exceptions;
 1190 the above requirements apply either way.
 1191 
 1192   8. Termination.
 1193 
 1194   You may not propagate or modify a covered work except as expressly
 1195 provided under this License.  Any attempt otherwise to propagate or
 1196 modify it is void, and will automatically terminate your rights under
 1197 this License (including any patent licenses granted under the third
 1198 paragraph of section 11).
 1199 
 1200   However, if you cease all violation of this License, then your
 1201 license from a particular copyright holder is reinstated (a)
 1202 provisionally, unless and until the copyright holder explicitly and
 1203 finally terminates your license, and (b) permanently, if the copyright
 1204 holder fails to notify you of the violation by some reasonable means
 1205 prior to 60 days after the cessation.
 1206 
 1207   Moreover, your license from a particular copyright holder is
 1208 reinstated permanently if the copyright holder notifies you of the
 1209 violation by some reasonable means, this is the first time you have
 1210 received notice of violation of this License (for any work) from that
 1211 copyright holder, and you cure the violation prior to 30 days after
 1212 your receipt of the notice.
 1213 
 1214   Termination of your rights under this section does not terminate the
 1215 licenses of parties who have received copies or rights from you under
 1216 this License.  If your rights have been terminated and not permanently
 1217 reinstated, you do not qualify to receive new licenses for the same
 1218 material under section 10.
 1219 
 1220   9. Acceptance Not Required for Having Copies.
 1221 
 1222   You are not required to accept this License in order to receive or
 1223 run a copy of the Program.  Ancillary propagation of a covered work
 1224 occurring solely as a consequence of using peer-to-peer transmission
 1225 to receive a copy likewise does not require acceptance.  However,
 1226 nothing other than this License grants you permission to propagate or
 1227 modify any covered work.  These actions infringe copyright if you do
 1228 not accept this License.  Therefore, by modifying or propagating a
 1229 covered work, you indicate your acceptance of this License to do so.
 1230 
 1231   10. Automatic Licensing of Downstream Recipients.
 1232 
 1233   Each time you convey a covered work, the recipient automatically
 1234 receives a license from the original licensors, to run, modify and
 1235 propagate that work, subject to this License.  You are not responsible
 1236 for enforcing compliance by third parties with this License.
 1237 
 1238   An "entity transaction" is a transaction transferring control of an
 1239 organization, or substantially all assets of one, or subdividing an
 1240 organization, or merging organizations.  If propagation of a covered
 1241 work results from an entity transaction, each party to that
 1242 transaction who receives a copy of the work also receives whatever
 1243 licenses to the work the party's predecessor in interest had or could
 1244 give under the previous paragraph, plus a right to possession of the
 1245 Corresponding Source of the work from the predecessor in interest, if
 1246 the predecessor has it or can get it with reasonable efforts.
 1247 
 1248   You may not impose any further restrictions on the exercise of the
 1249 rights granted or affirmed under this License.  For example, you may
 1250 not impose a license fee, royalty, or other charge for exercise of
 1251 rights granted under this License, and you may not initiate litigation
 1252 (including a cross-claim or counterclaim in a lawsuit) alleging that
 1253 any patent claim is infringed by making, using, selling, offering for
 1254 sale, or importing the Program or any portion of it.
 1255 
 1256   11. Patents.
 1257 
 1258   A "contributor" is a copyright holder who authorizes use under this
 1259 License of the Program or a work on which the Program is based.  The
 1260 work thus licensed is called the contributor's "contributor version".
 1261 
 1262   A contributor's "essential patent claims" are all patent claims
 1263 owned or controlled by the contributor, whether already acquired or
 1264 hereafter acquired, that would be infringed by some manner, permitted
 1265 by this License, of making, using, or selling its contributor version,
 1266 but do not include claims that would be infringed only as a
 1267 consequence of further modification of the contributor version.  For
 1268 purposes of this definition, "control" includes the right to grant
 1269 patent sublicenses in a manner consistent with the requirements of
 1270 this License.
 1271 
 1272   Each contributor grants you a non-exclusive, worldwide, royalty-free
 1273 patent license under the contributor's essential patent claims, to
 1274 make, use, sell, offer for sale, import and otherwise run, modify and
 1275 propagate the contents of its contributor version.
 1276 
 1277   In the following three paragraphs, a "patent license" is any express
 1278 agreement or commitment, however denominated, not to enforce a patent
 1279 (such as an express permission to practice a patent or covenant not to
 1280 sue for patent infringement).  To "grant" such a patent license to a
 1281 party means to make such an agreement or commitment not to enforce a
 1282 patent against the party.
 1283 
 1284   If you convey a covered work, knowingly relying on a patent license,
 1285 and the Corresponding Source of the work is not available for anyone
 1286 to copy, free of charge and under the terms of this License, through a
 1287 publicly available network server or other readily accessible means,
 1288 then you must either (1) cause the Corresponding Source to be so
 1289 available, or (2) arrange to deprive yourself of the benefit of the
 1290 patent license for this particular work, or (3) arrange, in a manner
 1291 consistent with the requirements of this License, to extend the patent
 1292 license to downstream recipients.  "Knowingly relying" means you have
 1293 actual knowledge that, but for the patent license, your conveying the
 1294 covered work in a country, or your recipient's use of the covered work
 1295 in a country, would infringe one or more identifiable patents in that
 1296 country that you have reason to believe are valid.
 1297 
 1298   If, pursuant to or in connection with a single transaction or
 1299 arrangement, you convey, or propagate by procuring conveyance of, a
 1300 covered work, and grant a patent license to some of the parties
 1301 receiving the covered work authorizing them to use, propagate, modify
 1302 or convey a specific copy of the covered work, then the patent license
 1303 you grant is automatically extended to all recipients of the covered
 1304 work and works based on it.
 1305 
 1306   A patent license is "discriminatory" if it does not include within
 1307 the scope of its coverage, prohibits the exercise of, or is
 1308 conditioned on the non-exercise of one or more of the rights that are
 1309 specifically granted under this License.  You may not convey a covered
 1310 work if you are a party to an arrangement with a third party that is
 1311 in the business of distributing software, under which you make payment
 1312 to the third party based on the extent of your activity of conveying
 1313 the work, and under which the third party grants, to any of the
 1314 parties who would receive the covered work from you, a discriminatory
 1315 patent license (a) in connection with copies of the covered work
 1316 conveyed by you (or copies made from those copies), or (b) primarily
 1317 for and in connection with specific products or compilations that
 1318 contain the covered work, unless you entered into that arrangement,
 1319 or that patent license was granted, prior to 28 March 2007.
 1320 
 1321   Nothing in this License shall be construed as excluding or limiting
 1322 any implied license or other defenses to infringement that may
 1323 otherwise be available to you under applicable patent law.
 1324 
 1325   12. No Surrender of Others' Freedom.
 1326 
 1327   If conditions are imposed on you (whether by court order, agreement or
 1328 otherwise) that contradict the conditions of this License, they do not
 1329 excuse you from the conditions of this License.  If you cannot convey a
 1330 covered work so as to satisfy simultaneously your obligations under this
 1331 License and any other pertinent obligations, then as a consequence you may
 1332 not convey it at all.  For example, if you agree to terms that obligate you
 1333 to collect a royalty for further conveying from those to whom you convey
 1334 the Program, the only way you could satisfy both those terms and this
 1335 License would be to refrain entirely from conveying the Program.
 1336 
 1337   13. Use with the GNU Affero General Public License.
 1338 
 1339   Notwithstanding any other provision of this License, you have
 1340 permission to link or combine any covered work with a work licensed
 1341 under version 3 of the GNU Affero General Public License into a single
 1342 combined work, and to convey the resulting work.  The terms of this
 1343 License will continue to apply to the part which is the covered work,
 1344 but the special requirements of the GNU Affero General Public License,
 1345 section 13, concerning interaction through a network will apply to the
 1346 combination as such.
 1347 
 1348   14. Revised Versions of this License.
 1349 
 1350   The Free Software Foundation may publish revised and/or new versions of
 1351 the GNU General Public License from time to time.  Such new versions will
 1352 be similar in spirit to the present version, but may differ in detail to
 1353 address new problems or concerns.
 1354 
 1355   Each version is given a distinguishing version number.  If the
 1356 Program specifies that a certain numbered version of the GNU General
 1357 Public License "or any later version" applies to it, you have the
 1358 option of following the terms and conditions either of that numbered
 1359 version or of any later version published by the Free Software
 1360 Foundation.  If the Program does not specify a version number of the
 1361 GNU General Public License, you may choose any version ever published
 1362 by the Free Software Foundation.
 1363 
 1364   If the Program specifies that a proxy can decide which future
 1365 versions of the GNU General Public License can be used, that proxy's
 1366 public statement of acceptance of a version permanently authorizes you
 1367 to choose that version for the Program.
 1368 
 1369   Later license versions may give you additional or different
 1370 permissions.  However, no additional obligations are imposed on any
 1371 author or copyright holder as a result of your choosing to follow a
 1372 later version.
 1373 
 1374   15. Disclaimer of Warranty.
 1375 
 1376   THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
 1377 APPLICABLE LAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
 1378 HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
 1379 OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
 1380 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 1381 PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
 1382 IS WITH YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
 1383 ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
 1384 
 1385   16. Limitation of Liability.
 1386 
 1387   IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
 1388 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
 1389 THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
 1390 GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
 1391 USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
 1392 DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
 1393 PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
 1394 EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
 1395 SUCH DAMAGES.
 1396 
 1397   17. Interpretation of Sections 15 and 16.
 1398 
 1399   If the disclaimer of warranty and limitation of liability provided
 1400 above cannot be given local legal effect according to their terms,
 1401 reviewing courts shall apply local law that most closely approximates
 1402 an absolute waiver of all civil liability in connection with the
 1403 Program, unless a warranty or assumption of liability accompanies a
 1404 copy of the Program in return for a fee.
 1405 
 1406                      END OF TERMS AND CONDITIONS
 1407 */