mirror of
https://github.com/nmap/nmap.git
synced 2026-06-30 21:24:01 +00:00
Use NetBlock/massdns for targets in Nping
This commit is contained in:
parent
1641d13cf9
commit
dba42c8529
4 changed files with 89 additions and 616 deletions
|
|
@ -68,18 +68,13 @@
|
|||
#include "NpingTargets.h"
|
||||
#include "common.h"
|
||||
#include "common_modified.h"
|
||||
#include <algorithm>
|
||||
|
||||
extern NpingOps o;
|
||||
|
||||
NpingTargets::NpingTargets(){
|
||||
memset(specs, 0, 1024*(sizeof(char *)) );
|
||||
memset(skipspec, 0, 1024*(sizeof(bool)) );
|
||||
speccount=0;
|
||||
current_spec=-1;
|
||||
finished=false;
|
||||
targets_fetched=0;
|
||||
current_target=0;
|
||||
ready=false;
|
||||
NpingTargets::NpingTargets()
|
||||
: ready(false), current_target(0)
|
||||
{
|
||||
} /* End of NpingTargets constructor */
|
||||
|
||||
|
||||
|
|
@ -91,82 +86,42 @@ NpingTargets::~NpingTargets(){
|
|||
int NpingTargets::addSpec(char *spec){
|
||||
if(spec==NULL)
|
||||
return OP_FAILURE;
|
||||
if( this->speccount >= 1024 )
|
||||
int family= (o.getIPVersion()==IP_VERSION_6) ? AF_INET6 : AF_INET;
|
||||
NetBlock *nb = NetBlock::parse_expr(spec, family, requests, false,
|
||||
o.issetDevice() ? o.getDevice() : NULL);
|
||||
if (nb == NULL)
|
||||
return OP_FAILURE;
|
||||
specs[ this->speccount ] = spec;
|
||||
this->speccount++;
|
||||
netblocks.push_back(nb);
|
||||
return OP_SUCCESS;
|
||||
} /* End of NpingTargets */
|
||||
|
||||
|
||||
/** Returns next target */
|
||||
int NpingTargets::getNextTargetAddressAndName(struct sockaddr_storage *t, size_t *tlen, char *hname, size_t hlen){
|
||||
struct sockaddr_storage next;
|
||||
memset(&next, 0, sizeof(struct sockaddr_storage));
|
||||
size_t nextlen=0;
|
||||
int r=0;
|
||||
int family= (o.getIPVersion()==IP_VERSION_6) ? AF_INET6 : AF_INET;
|
||||
|
||||
if( t==NULL || tlen==NULL )
|
||||
nping_fatal(QT_3,"getNextTarget(): NULL values supplied.");
|
||||
|
||||
/* Return failure if there are no specs or we noticed that we were finished in
|
||||
* a previous call. */
|
||||
if ( this->speccount <= 0 || finished==true )
|
||||
NetBlock *nb = NULL;
|
||||
while (!netblocks.empty()) {
|
||||
nb = netblocks.front();
|
||||
if (nb->next(t, tlen)) {
|
||||
break;
|
||||
}
|
||||
// Ran out of hosts in that block. Remove it.
|
||||
netblocks.pop_front();
|
||||
delete nb;
|
||||
nb = NULL;
|
||||
}
|
||||
if (nb == NULL) {
|
||||
// Ran out of netblocks
|
||||
return OP_FAILURE;
|
||||
|
||||
/* If this is the first time we call to this method */
|
||||
if (this->current_spec == -1 ){
|
||||
|
||||
current_spec=0;
|
||||
if ( !skipspec[ current_spec ] ){
|
||||
if ( current_group.parse_expr( specs[ current_spec ], family ) != 0 ){
|
||||
skipspec[ current_spec ]=true; /* Make sure we skip it next time */
|
||||
return OP_FAILURE;
|
||||
|
||||
}
|
||||
}
|
||||
else{ /* We are skipping current target, return the next one */
|
||||
return this->getNextTargetAddressAndName(t, tlen, hname, hlen);
|
||||
}
|
||||
}
|
||||
|
||||
r=current_group.get_next_host(&next, &nextlen);
|
||||
|
||||
if ( r!=0 ){ /* We exhausted current group */
|
||||
/* Is there any other group? */
|
||||
if (++current_spec == speccount){ /* No more specs to parse */
|
||||
finished=true;
|
||||
return OP_FAILURE;
|
||||
}
|
||||
/* Ok, there are more groups, so let's go with the next spec */
|
||||
if ( !skipspec[ current_spec ] ){
|
||||
if ( current_group.parse_expr( specs[ current_spec ], family ) != 0 ){
|
||||
skipspec[ current_spec ]=true;
|
||||
return this->getNextTargetAddressAndName(t, tlen, hname, hlen);
|
||||
|
||||
}
|
||||
}
|
||||
else{ /* We are skipping current target, return the next one */
|
||||
return this->getNextTargetAddressAndName(t, tlen, hname, hlen);
|
||||
}
|
||||
|
||||
r=current_group.get_next_host(&next, &nextlen);
|
||||
|
||||
if (r != 0)
|
||||
nping_fatal(QT_3,"BUG: TargetGroups are supposed to contain at least one IP! ");
|
||||
}
|
||||
memcpy( t, &next, sizeof( struct sockaddr_storage ) );
|
||||
/* If current spec is a named host (not a range), store name in supplied buff */
|
||||
if(current_group.get_namedhost()){
|
||||
if( hname!=NULL && hlen>0 )
|
||||
strncpy(hname, specs[ current_spec ], hlen);
|
||||
}else{ /* If current spec is not a named host, insert NULL in the first position */
|
||||
if( hname!=NULL && hlen>0 )
|
||||
hname[0]='\0';
|
||||
}
|
||||
*tlen=nextlen;
|
||||
targets_fetched++;
|
||||
/* If current spec is not a named host, c_str() is "\0" */
|
||||
if( hname!=NULL && hlen>0 )
|
||||
strncpy(hname, nb->hostname.c_str(), hlen);
|
||||
|
||||
return OP_SUCCESS;
|
||||
} /* End of getNextTarget() */
|
||||
|
||||
|
|
@ -188,24 +143,48 @@ int NpingTargets::getNextIPv4Address(u32 *addr){
|
|||
} /* End of getNextIPv4Address() */
|
||||
|
||||
|
||||
int NpingTargets::rewindSpecs(){
|
||||
current_spec=-1;
|
||||
finished=false;
|
||||
targets_fetched=0;
|
||||
return OP_SUCCESS;
|
||||
} /* End of rewind() */
|
||||
|
||||
|
||||
unsigned long int NpingTargets::getTargetsFetched(){
|
||||
return this->Targets.size();
|
||||
} /* getTargetsFetched() */
|
||||
|
||||
|
||||
int NpingTargets::getTargetSpecCount(){
|
||||
return this->speccount;
|
||||
return this->netblocks.size();
|
||||
} /* End of getTargetSpecCount() */
|
||||
|
||||
|
||||
static void nping_mass_dns(DNS::Request requests[], int num_requests) {
|
||||
static DNS::Resolver resolver;
|
||||
static bool initialized = false;
|
||||
if (!initialized) {
|
||||
int family= (o.getIPVersion()==IP_VERSION_6) ? AF_INET6 : AF_INET;
|
||||
resolver.setAF(family);
|
||||
// TODO: resolver.setStatusCallback();
|
||||
// TODO: resolver.setLogFunc();
|
||||
if (o.issetDevice() || o.spoofSource()) {
|
||||
resolver.setSource(o.getDevice(), o.getSourceSockAddr(), sizeof(sockaddr_storage), o.spoofSource());
|
||||
}
|
||||
if (o.issetIPOptions()) {
|
||||
// TODO: NpingOps.ip_options should have a length, not be null-terminated.
|
||||
const char *ipopts = o.getIPOptions();
|
||||
resolver.setIpOptions((const u8 *)ipopts, strlen(ipopts));
|
||||
}
|
||||
// TODO: NpingOps may need to support --dns-servers and --system-dns
|
||||
}
|
||||
|
||||
const char *errstr = NULL;
|
||||
bool use_systemdns = false;
|
||||
|
||||
resolver.Init(requests, num_requests);
|
||||
|
||||
if (!resolver.isMassDnsOK(&errstr)) {
|
||||
error("%s. Falling back to System DNS resolver.", errstr);
|
||||
use_systemdns = true;
|
||||
}
|
||||
|
||||
resolver.Resolve(use_systemdns);
|
||||
}
|
||||
|
||||
/** This method should be called when all the target specs have been entered
|
||||
* using addSpec(). What it does is to create a NpingTarget objects for
|
||||
* each IP address extracted from the specs. Objects are stored in an internal
|
||||
|
|
@ -220,8 +199,31 @@ int NpingTargets::processSpecs(){
|
|||
memset(&ss, 0, sizeof(struct sockaddr_storage));
|
||||
memset(buff, 0, MAX_NPING_HOSTNAME_LEN+1);
|
||||
|
||||
/* Rewind spec index just in case someone has been playing around with it */
|
||||
o.targets.rewindSpecs();
|
||||
if (requests.size() > 0) {
|
||||
nping_mass_dns(requests.data(), requests.size());
|
||||
std::list<NetBlock *>::iterator nb_it = netblocks.begin();
|
||||
for (std::vector<DNS::Request>::const_iterator rit = requests.begin();
|
||||
rit != requests.end(); rit++) {
|
||||
const DNS::Request &req = *rit;
|
||||
NetBlock *nb_old = (NetBlock *) req.userdata;
|
||||
assert(nb_old != NULL);
|
||||
NetBlock *nb_new = nb_old->resolve(req);
|
||||
nb_it = std::find(nb_it, netblocks.end(), nb_old);
|
||||
assert(nb_it != netblocks.end());
|
||||
|
||||
if (nb_new == NULL) {
|
||||
// Resolution failed; remove the NetBlock
|
||||
nb_it = netblocks.erase(nb_it);
|
||||
}
|
||||
else {
|
||||
assert (nb_new != nb_old);
|
||||
// Resolution succeeded; replace the NetBlock
|
||||
*nb_it = nb_new;
|
||||
}
|
||||
delete nb_old;
|
||||
}
|
||||
requests.clear();
|
||||
}
|
||||
|
||||
/* Get next host IP address and, if it is a named host, its hostname */
|
||||
while ( this->getNextTargetAddressAndName(&ss, &slen, buff, MAX_NPING_HOSTNAME_LEN) == OP_SUCCESS ){
|
||||
|
|
@ -314,7 +316,7 @@ int NpingTargets::processSpecs(){
|
|||
this->ready=true;
|
||||
o.targets.rewind();
|
||||
return OP_SUCCESS;
|
||||
} /* End of getTargetSpecCount() */
|
||||
} /* End of processSpecs() */
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -66,7 +66,9 @@
|
|||
/* TODO: Needs to be changed if we move TargetGroup to another source file */
|
||||
#include "common_modified.h"
|
||||
#include "NpingTarget.h"
|
||||
#include "../libnetutil/NetBlock.h"
|
||||
#include <vector>
|
||||
#include <list>
|
||||
|
||||
#define MAX_NPING_HOSTNAME_LEN 512 /**< Max length for named hosts */
|
||||
|
||||
|
|
@ -74,16 +76,10 @@ class NpingTargets {
|
|||
|
||||
private:
|
||||
|
||||
char *specs[1024];
|
||||
bool skipspec[1024];
|
||||
int speccount;
|
||||
int current_spec;
|
||||
bool lastwaslastingroup;
|
||||
bool finished;
|
||||
TargetGroup current_group;
|
||||
std::vector<DNS::Request> requests;
|
||||
std::list<NetBlock *> netblocks;
|
||||
|
||||
bool ready;
|
||||
unsigned long int targets_fetched;
|
||||
unsigned long int current_target;
|
||||
|
||||
public:
|
||||
|
|
|
|||
|
|
@ -63,465 +63,6 @@
|
|||
#include "common.h"
|
||||
#include "common_modified.h"
|
||||
#include "output.h"
|
||||
#include "../libnetutil/netutil.h"
|
||||
/*****************************************************************************
|
||||
* STUFF FROM TargetGroup.cc
|
||||
****************************************************************************
|
||||
|
||||
CHANGES:
|
||||
Modified parse_expr.
|
||||
Modified get_next_host:
|
||||
|
||||
*/
|
||||
|
||||
|
||||
TargetGroup::TargetGroup() {
|
||||
Initialize();
|
||||
}
|
||||
|
||||
// Bring back (or start with) original state
|
||||
void TargetGroup::Initialize() {
|
||||
targets_type = TYPE_NONE;
|
||||
memset(addresses, 0, sizeof(addresses));
|
||||
memset(current, 0, sizeof(current));
|
||||
memset(last, 0, sizeof(last));
|
||||
ipsleft = 0;
|
||||
}
|
||||
|
||||
/* take the object back to the beginning without (mdmcl)
|
||||
* reinitializing the data structures */
|
||||
int TargetGroup::rewind() {
|
||||
|
||||
/* For netmasks we must set the current address to the
|
||||
* starting address and calculate the ips by distance */
|
||||
if (targets_type == IPV4_NETMASK) {
|
||||
currentaddr = startaddr;
|
||||
if (startaddr.s_addr <= endaddr.s_addr) {
|
||||
ipsleft = ((unsigned long long) (endaddr.s_addr - startaddr.s_addr)) + 1;
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
assert(0);
|
||||
}
|
||||
/* For ranges, we easily set current to zero and calculate
|
||||
* the ips by the number of values in the columns */
|
||||
else if (targets_type == IPV4_RANGES) {
|
||||
memset((char *)current, 0, sizeof(current));
|
||||
ipsleft = (unsigned long long) (last[0] + 1) *
|
||||
(unsigned long long) (last[1] + 1) *
|
||||
(unsigned long long) (last[2] + 1) *
|
||||
(unsigned long long) (last[3] + 1);
|
||||
return 0;
|
||||
}
|
||||
#if HAVE_IPV6
|
||||
/* For IPV6 there is only one address, this function doesn't
|
||||
* make much sense for IPv6 does it? */
|
||||
else if (targets_type == IPV6_ADDRESS) {
|
||||
ipsleft = 1;
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* If we got this far there must be an error, wrong type */
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* For ranges, skip all hosts in an octet, (mdmcl)
|
||||
* get_next_host should be used for skipping the last octet :-)
|
||||
* returns: number of hosts skipped */
|
||||
int TargetGroup::skip_range(_octet_nums octet) {
|
||||
u32 hosts_skipped = 0, /* number of hosts skipped */
|
||||
oct = 0; /* octect number */
|
||||
int i = 0; /* simple lcv */
|
||||
|
||||
/* This function is only supported for RANGES! */
|
||||
if (targets_type != IPV4_RANGES)
|
||||
return -1;
|
||||
|
||||
switch (octet) {
|
||||
case FIRST_OCTET:
|
||||
oct = 0;
|
||||
hosts_skipped = (u32)(last[1] + 1) * (last[2] + 1) * (last[3] + 1);
|
||||
break;
|
||||
case SECOND_OCTET:
|
||||
oct = 1;
|
||||
hosts_skipped = (u32)(last[2] + 1) * (last[3] + 1);
|
||||
break;
|
||||
case THIRD_OCTET:
|
||||
oct = 2;
|
||||
hosts_skipped = (last[3] + 1);
|
||||
break;
|
||||
default: /* Hmm, how did you do that? */
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* catch if we try to take more than are left */
|
||||
assert(ipsleft + 1>= hosts_skipped);
|
||||
|
||||
/* increment the next octect that we can above us */
|
||||
for (i = oct; i >= 0; i--) {
|
||||
if (current[i] < last[i]) {
|
||||
current[i]++;
|
||||
break;
|
||||
}
|
||||
else
|
||||
current[i] = 0;
|
||||
}
|
||||
|
||||
/* reset all the ones below us to zero */
|
||||
for (i = oct+1; i <= 3; i++) {
|
||||
current[i] = 0;
|
||||
}
|
||||
|
||||
/* we actually don't skip the current, it was accounted for
|
||||
* by get_next_host */
|
||||
ipsleft -= hosts_skipped - 1;
|
||||
|
||||
return hosts_skipped;
|
||||
}
|
||||
|
||||
/* Grab the next host from this expression (if any) and updates its internal
|
||||
state to reflect that the IP was given out. Returns 0 and
|
||||
fills in ss if successful. ss must point to a pre-allocated
|
||||
sockaddr_storage structure */
|
||||
int TargetGroup::get_next_host(struct sockaddr_storage *ss, size_t *sslen) {
|
||||
|
||||
int octet;
|
||||
struct sockaddr_in *sin = (struct sockaddr_in *) ss;
|
||||
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) ss;
|
||||
|
||||
/* CHANGE: Commented out. See note at the end of the method */
|
||||
//startover: /* to handle nmap --resume where I have already
|
||||
// * scanned many of the IPs */
|
||||
assert(ss);
|
||||
assert(sslen);
|
||||
|
||||
|
||||
if (ipsleft == 0)
|
||||
return -1;
|
||||
|
||||
if (targets_type == IPV4_NETMASK) {
|
||||
memset(sin, 0, sizeof(struct sockaddr_in));
|
||||
sin->sin_family = AF_INET;
|
||||
*sslen = sizeof(struct sockaddr_in);
|
||||
#if HAVE_SOCKADDR_SA_LEN
|
||||
sin->sin_len = *sslen;
|
||||
#endif
|
||||
|
||||
if (currentaddr.s_addr <= endaddr.s_addr) {
|
||||
sin->sin_addr.s_addr = htonl(currentaddr.s_addr++);
|
||||
} else {
|
||||
error("Bogus target structure passed to %s", __func__);
|
||||
ipsleft = 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else if (targets_type == IPV4_RANGES) {
|
||||
memset(sin, 0, sizeof(struct sockaddr_in));
|
||||
sin->sin_family = AF_INET;
|
||||
*sslen = sizeof(struct sockaddr_in);
|
||||
#if HAVE_SOCKADDR_SA_LEN
|
||||
sin->sin_len = *sslen;
|
||||
#endif
|
||||
//if (o.debugging > 2) { /* CHANGE: Do not use NmapOps and do not use log_Write*/
|
||||
// log_write(LOG_STDOUT, "doing %d.%d.%d.%d = %d.%d.%d.%d\n", current[0], current[1], current[2], current[3], addresses[0][current[0]],addresses[1][current[1]],addresses[2][current[2]],addresses[3][current[3]]);
|
||||
//}
|
||||
//nping_print(DBG_2, "doing %d.%d.%d.%d = %d.%d.%d.%d", current[0], current[1], current[2], current[3], addresses[0][current[0]],addresses[1][current[1]],addresses[2][current[2]],addresses[3][current[3]]);
|
||||
|
||||
|
||||
/* Set the IP to the current value of everything */
|
||||
sin->sin_addr.s_addr = htonl(addresses[0][current[0]] << 24 |
|
||||
addresses[1][current[1]] << 16 |
|
||||
addresses[2][current[2]] << 8 |
|
||||
addresses[3][current[3]]);
|
||||
|
||||
/* Now we nudge up to the next IP */
|
||||
for(octet = 3; octet >= 0; octet--) {
|
||||
if (current[octet] < last[octet]) {
|
||||
/* OK, this is the column I have room to nudge upwards */
|
||||
current[octet]++;
|
||||
break;
|
||||
} else {
|
||||
/* This octet is finished so I reset it to the beginning */
|
||||
current[octet] = 0;
|
||||
}
|
||||
}
|
||||
if (octet == -1) {
|
||||
/* It didn't find anything to bump up, I must have taken the last IP */
|
||||
assert(ipsleft == 1);
|
||||
/* So I set current to last with the very final octet up one ... */
|
||||
/* Note that this may make current[3] == 256 */
|
||||
current[0] = last[0]; current[1] = last[1];
|
||||
current[2] = last[2]; current[3] = last[3] + 1;
|
||||
} else {
|
||||
assert(ipsleft > 1); /* There must be at least one more IP left */
|
||||
}
|
||||
} else {
|
||||
assert(targets_type == IPV6_ADDRESS);
|
||||
assert(ipsleft == 1);
|
||||
#if HAVE_IPV6
|
||||
*sslen = sizeof(struct sockaddr_in6);
|
||||
memset(sin6, 0, *sslen);
|
||||
sin6->sin6_family = AF_INET6;
|
||||
#ifdef SIN_LEN
|
||||
sin6->sin6_len = *sslen;
|
||||
#endif /* SIN_LEN */
|
||||
memcpy(sin6->sin6_addr.s6_addr, ip6.sin6_addr.s6_addr, 16);
|
||||
sin6->sin6_scope_id = ip6.sin6_scope_id;
|
||||
#else
|
||||
fatal("IPV6 not supported on this platform");
|
||||
#endif // HAVE_IPV6
|
||||
}
|
||||
ipsleft--;
|
||||
|
||||
|
||||
/* CHANGE: These lines have been commented out to make this code
|
||||
* independent from NmapOps */
|
||||
/* If we are resuming from a previous scan, we have already finished
|
||||
scans up to o.resume_ip. */
|
||||
// if (sin->sin_family == AF_INET && o.resume_ip.s_addr) {
|
||||
// if (o.resume_ip.s_addr == sin->sin_addr.s_addr)
|
||||
// o.resume_ip.s_addr = 0; /* So that we will KEEP the next one */
|
||||
// goto startover; /* Try again */
|
||||
// }
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Returns the last given host, so that it will be given again next
|
||||
time get_next_host is called. Obviously, you should only call
|
||||
this if you have fetched at least 1 host since parse_expr() was
|
||||
called */
|
||||
int TargetGroup::return_last_host() {
|
||||
int octet;
|
||||
|
||||
ipsleft++;
|
||||
if (targets_type == IPV4_NETMASK) {
|
||||
assert(currentaddr.s_addr > startaddr.s_addr);
|
||||
currentaddr.s_addr--;
|
||||
} else if (targets_type == IPV4_RANGES) {
|
||||
for(octet = 3; octet >= 0; octet--) {
|
||||
if (current[octet] > 0) {
|
||||
/* OK, this is the column I have room to nudge downwards */
|
||||
current[octet]--;
|
||||
break;
|
||||
} else {
|
||||
/* This octet is already at the beginning, so I set it to the end */
|
||||
current[octet] = last[octet];
|
||||
}
|
||||
}
|
||||
assert(octet != -1);
|
||||
} else {
|
||||
assert(targets_type == IPV6_ADDRESS);
|
||||
assert(ipsleft == 1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* TODO: WARNING: This functions has been modified for portability. Check
|
||||
* for label "CHANGE:" in the code to see the actual changes.
|
||||
*
|
||||
* UPDATE: Added support for DNS resolution caching. Using function
|
||||
* gethostbynameCached() instead of gethostbyname()
|
||||
*/
|
||||
/* Initializes (or reinitializes) the object with a new expression, such
|
||||
as 192.168.0.0/16 , 10.1.0-5.1-254 , or fe80::202:e3ff:fe14:1102 .
|
||||
Returns 0 for success */
|
||||
int TargetGroup::parse_expr(const char * const target_expr, int af) {
|
||||
|
||||
int i=0,j=0,k=0;
|
||||
int start, end;
|
||||
char *r,*s, *target_net;
|
||||
char *addy[5];
|
||||
char *hostexp = strdup(target_expr);
|
||||
struct hostent *target;
|
||||
namedhost = 0;
|
||||
|
||||
if (targets_type != TYPE_NONE)
|
||||
Initialize();
|
||||
|
||||
ipsleft = 0;
|
||||
|
||||
if (af == AF_INET) {
|
||||
|
||||
if (strchr(hostexp, ':'))
|
||||
fatal("Invalid host expression: %s -- colons only allowed in IPv6 addresses, and then you need the -6 switch", hostexp);
|
||||
|
||||
/*struct in_addr current_in;*/
|
||||
addy[0] = addy[1] = addy[2] = addy[3] = addy[4] = NULL;
|
||||
addy[0] = r = hostexp;
|
||||
/* First we break the expression up into the four parts of the IP address
|
||||
+ the optional '/mask' */
|
||||
target_net = hostexp;
|
||||
s = strchr(hostexp, '/'); /* Find the slash if there is one */
|
||||
if (s) {
|
||||
char *tail;
|
||||
long netmask_long;
|
||||
|
||||
*s = '\0'; /* Make sure target_net is terminated before the /## */
|
||||
s++; /* Point s at the netmask */
|
||||
if (!isdigit(*s)) {
|
||||
error("Illegal netmask value, must be /0 - /32 . Assuming /32 (one host)");
|
||||
netmask = 32;
|
||||
} else {
|
||||
netmask_long = strtol(s, (char**) &tail, 10);
|
||||
if (*tail != '\0' || tail == s || netmask_long < 0 || netmask_long > 32) {
|
||||
error("Illegal netmask value, must be /0 - /32 . Assuming /32 (one host)");
|
||||
netmask = 32;
|
||||
} else
|
||||
netmask = (u32) netmask_long;
|
||||
}
|
||||
} else
|
||||
netmask = 32;
|
||||
for(i=0; *(hostexp + i); i++)
|
||||
if (isupper((int) *(hostexp +i)) || islower((int) *(hostexp +i))) {
|
||||
namedhost = 1;
|
||||
break;
|
||||
}
|
||||
if (netmask != 32 || namedhost) {
|
||||
targets_type = IPV4_NETMASK;
|
||||
if (!inet_pton(AF_INET, target_net, &(startaddr))) {
|
||||
|
||||
/* There is a bug report on the use of gethostbynameCached()
|
||||
* <http://seclists.org/nmap-dev/2010/q1/803>
|
||||
* I haven't been able to find any problem with that code but
|
||||
* still, the fact that DNS queries are cached does not improve
|
||||
* performance a lot. It may save one DNS query per execution
|
||||
* in those cases where NpingOps::validateOptions() grabs the
|
||||
* first target and uses it to determine output network interface.
|
||||
* It would also save some queries in the case where a user
|
||||
* specified the same host twice in the commandlined, something
|
||||
* that does not make much sense anyway. However, since the call
|
||||
* to gethostbynameCached() seems to cause denial of service
|
||||
* for some people, I think it's ok to disable its use for now
|
||||
* and enable it later if there is a good reason for it.
|
||||
*
|
||||
* Luis MartinGarcia. */
|
||||
//if ((target = gethostbynameCached(target_net))) {
|
||||
if ((target = gethostbyname(target_net))) {
|
||||
int count=0;
|
||||
|
||||
memcpy(&(startaddr), target->h_addr_list[0], sizeof(struct in_addr));
|
||||
|
||||
while (target->h_addr_list[count]) count++;
|
||||
|
||||
if (count > 1)
|
||||
nping_print(DBG_2,"Warning: Hostname %s resolves to %d IPs. Using %s.", target_net, count, inet_ntoa(*((struct in_addr *)target->h_addr_list[0])));
|
||||
} else {
|
||||
error("Failed to resolve given hostname/IP: %s. Note that you can't use '/mask' AND '1-4,7,100-' style IP ranges", target_net);
|
||||
free(hostexp);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
if (netmask) {
|
||||
unsigned long longtmp = ntohl(startaddr.s_addr);
|
||||
startaddr.s_addr = longtmp & (unsigned long) (0 - (1<<(32 - netmask)));
|
||||
endaddr.s_addr = longtmp | (unsigned long) ((1<<(32 - netmask)) - 1);
|
||||
} else {
|
||||
/* The above calculations don't work for a /0 netmask, though at first
|
||||
* glance it appears that they would
|
||||
*/
|
||||
startaddr.s_addr = 0;
|
||||
endaddr.s_addr = 0xffffffff;
|
||||
}
|
||||
currentaddr = startaddr;
|
||||
if (startaddr.s_addr <= endaddr.s_addr) {
|
||||
ipsleft = ((unsigned long long) (endaddr.s_addr - startaddr.s_addr)) + 1;
|
||||
free(hostexp);
|
||||
return 0;
|
||||
}
|
||||
fprintf(stderr, "Host specification invalid");
|
||||
free(hostexp);
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
targets_type = IPV4_RANGES;
|
||||
i=0;
|
||||
|
||||
while(*r) {
|
||||
if (*r == '.' && ++i < 4) {
|
||||
*r = '\0';
|
||||
addy[i] = r + 1;
|
||||
}
|
||||
else if (*r != '*' && *r != ',' && *r != '-' && !isdigit((int)*r))
|
||||
fatal("Invalid character in host specification. Note in particular that square brackets [] are no longer allowed. They were redundant and can simply be removed.");
|
||||
r++;
|
||||
}
|
||||
if (i != 3) fatal("Invalid target host specification: %s", target_expr);
|
||||
|
||||
for(i=0; i < 4; i++) {
|
||||
j=0;
|
||||
do {
|
||||
s = strchr(addy[i],',');
|
||||
if (s) *s = '\0';
|
||||
if (*addy[i] == '*') { start = 0; end = 255; }
|
||||
else if (*addy[i] == '-') {
|
||||
start = 0;
|
||||
if (*(addy[i] + 1) == '\0') end = 255;
|
||||
else end = atoi(addy[i]+ 1);
|
||||
}
|
||||
else {
|
||||
start = end = atoi(addy[i]);
|
||||
if ((r = strchr(addy[i],'-')) && *(r+1) ) end = atoi(r + 1);
|
||||
else if (r && !*(r+1)) end = 255;
|
||||
}
|
||||
/* if (o.debugging > 2)
|
||||
* log_write(LOG_STDOUT, "The first host is %d, and the last one is %d\n", start, end); */
|
||||
if (start < 0 || start > end || start > 255 || end > 255)
|
||||
fatal("Your host specifications are illegal!");
|
||||
if (j + (end - start) > 255)
|
||||
fatal("Your host specifications are illegal!");
|
||||
for(k=start; k <= end; k++)
|
||||
addresses[i][j++] = k;
|
||||
last[i] = j-1;
|
||||
if (s) addy[i] = s + 1;
|
||||
} while (s);
|
||||
}
|
||||
}
|
||||
memset((char *)current, 0, sizeof(current));
|
||||
ipsleft = (unsigned long long) (last[0] + 1) *
|
||||
(unsigned long long) (last[1] + 1) *
|
||||
(unsigned long long) (last[2] + 1) *
|
||||
(unsigned long long) (last[3] + 1);
|
||||
}
|
||||
else {
|
||||
#if HAVE_IPV6
|
||||
int rc = 0;
|
||||
assert(af == AF_INET6);
|
||||
if (strchr(hostexp, '/')) {
|
||||
fatal("Invalid host expression: %s -- slash not allowed. IPv6 addresses can currently only be specified individually", hostexp);
|
||||
}
|
||||
targets_type = IPV6_ADDRESS;
|
||||
struct addrinfo hints;
|
||||
struct addrinfo *result = NULL;
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = PF_INET6;
|
||||
rc = getaddrinfo(hostexp, NULL, &hints, &result);
|
||||
if (rc != 0 || result == NULL) {
|
||||
error("Failed to resolve given IPv6 hostname/IP: %s. Note that you can't use '/mask' or '[1-4,7,100-]' style ranges for IPv6. Error code %d: %s", hostexp, rc, gai_strerror(rc));
|
||||
free(hostexp);
|
||||
if (result) freeaddrinfo(result);
|
||||
return 1;
|
||||
}
|
||||
assert(result->ai_addrlen == sizeof(struct sockaddr_in6));
|
||||
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) result->ai_addr;
|
||||
memcpy(&ip6, sin6, sizeof(struct sockaddr_in6));
|
||||
ipsleft = 1;
|
||||
freeaddrinfo(result);
|
||||
#else // HAVE_IPV6
|
||||
fatal("IPv6 not supported on your platform");
|
||||
#endif // HAVE_IPV6
|
||||
}
|
||||
|
||||
free(hostexp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
/* getpts() and getpts_simple() (see above) are wrappers for this function */
|
||||
|
|
|
|||
|
|
@ -66,72 +66,6 @@
|
|||
#include "nping.h"
|
||||
#include "common.h"
|
||||
|
||||
/*****************************************************************************
|
||||
* STUFF FROM TargetGroup.h
|
||||
****************************************************************************/
|
||||
|
||||
class TargetGroup {
|
||||
public:
|
||||
/* used by get_target_types */
|
||||
enum _targets_types { TYPE_NONE, IPV4_NETMASK, IPV4_RANGES, IPV6_ADDRESS };
|
||||
/* used as input to skip range */
|
||||
enum _octet_nums { FIRST_OCTET, SECOND_OCTET, THIRD_OCTET };
|
||||
TargetGroup();
|
||||
|
||||
/* Initializes (or reinitializes) the object with a new expression,
|
||||
such as 192.168.0.0/16 , 10.1.0-5.1-254 , or
|
||||
fe80::202:e3ff:fe14:1102 . The af parameter is AF_INET or
|
||||
AF_INET6 Returns 0 for success */
|
||||
int parse_expr(const char * const target_expr, int af);
|
||||
/* Reset the object without reinitializing it */
|
||||
int rewind();
|
||||
/* Grab the next host from this expression (if any). Returns 0 and
|
||||
fills in ss if successful. ss must point to a pre-allocated
|
||||
sockaddr_storage structure */
|
||||
int get_next_host(struct sockaddr_storage *ss, size_t *sslen);
|
||||
/* Returns the last given host, so that it will be given again next
|
||||
time get_next_host is called. Obviously, you should only call
|
||||
this if you have fetched at least 1 host since parse_expr() was
|
||||
called */
|
||||
int return_last_host();
|
||||
/* return the target type */
|
||||
char get_targets_type() {return targets_type;};
|
||||
/* get the netmask */
|
||||
int get_mask() {return netmask;};
|
||||
/* is the current expression a named host */
|
||||
int get_namedhost() {return namedhost;};
|
||||
/* Skip an octet in the range array */
|
||||
int skip_range(_octet_nums octet);
|
||||
private:
|
||||
enum _targets_types targets_type;
|
||||
void Initialize();
|
||||
|
||||
#if HAVE_IPV6
|
||||
struct sockaddr_in6 ip6;
|
||||
#endif
|
||||
|
||||
/* These 4 are used for the '/mask' style of specifying target
|
||||
net (IPV4_NETMASK) */
|
||||
u32 netmask;
|
||||
struct in_addr startaddr;
|
||||
struct in_addr currentaddr;
|
||||
struct in_addr endaddr;
|
||||
|
||||
// These three are for the '138.[1-7,16,91-95,200-].12.1' style (IPV4_RANGES)
|
||||
u8 addresses[4][256];
|
||||
unsigned int current[4];
|
||||
u8 last[4];
|
||||
|
||||
/* Number of IPs left in this structure -- set to 0 if
|
||||
the fields are not valid */
|
||||
unsigned long long ipsleft;
|
||||
|
||||
// is the current target expression a named host
|
||||
int namedhost;
|
||||
};
|
||||
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* STUFF FROM tcpip.cc
|
||||
****************************************************************************/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue