diff --git a/TargetGroup.cc b/TargetGroup.cc index c1ec3e154..9dd1def51 100644 --- a/TargetGroup.cc +++ b/TargetGroup.cc @@ -113,18 +113,18 @@ void TargetGroup::Initialize() { /* take the object back to the beginning without (mdmcl) * reinitalizing the data structures */ -int TargetGroup::rewind() { +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); + 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 */ @@ -168,7 +168,7 @@ int TargetGroup::parse_expr(const char * const target_expr, int af) { 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); @@ -181,7 +181,7 @@ int TargetGroup::parse_expr(const char * const target_expr, int af) { s = strchr(hostexp, '/'); /* Find the slash if there is one */ if (s) { *s = '\0'; /* Make sure target_net is terminated before the /## */ - s++; /* Point s at the netmask */ + s++; /* Point s at the netmask */ } netmask = ( s ) ? atoi(s) : 32; if ((int) netmask < 0 || netmask > 32) { @@ -190,26 +190,26 @@ int TargetGroup::parse_expr(const char * const target_expr, int af) { } for(i=0; *(hostexp + i); i++) if (isupper((int) *(hostexp +i)) || islower((int) *(hostexp +i))) { - namedhost = 1; - break; + namedhost = 1; + break; } if (netmask != 32 || namedhost) { targets_type = IPV4_NETMASK; if (!inet_pton(AF_INET, target_net, &(startaddr))) { - if ((target = gethostbyname(target_net))) { + if ((target = gethostbyname(target_net))) { int count=0; - memcpy(&(startaddr), target->h_addr_list[0], sizeof(struct in_addr)); + memcpy(&(startaddr), target->h_addr_list[0], sizeof(struct in_addr)); while (target->h_addr_list[count]) count++; if (count > 1) error("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; - } + } 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); @@ -224,9 +224,9 @@ int TargetGroup::parse_expr(const char * const target_expr, int af) { } currentaddr = startaddr; if (startaddr.s_addr <= endaddr.s_addr) { - ipsleft = ((unsigned long long) (endaddr.s_addr - startaddr.s_addr)) + 1; - free(hostexp); - return 0; + ipsleft = ((unsigned long long) (endaddr.s_addr - startaddr.s_addr)) + 1; + free(hostexp); + return 0; } fprintf(stderr, "Host specification invalid"); free(hostexp); @@ -237,50 +237,50 @@ int TargetGroup::parse_expr(const char * const target_expr, int af) { 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."); + 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."); } 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); + 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); - } + 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; @@ -378,7 +378,7 @@ int TargetGroup::get_next_host(struct sockaddr_storage *ss, size_t *sslen) { struct sockaddr_in *sin = (struct sockaddr_in *) ss; struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) ss; startover: /* to handle nmap --resume where I have already - scanned many of the IPs */ + scanned many of the IPs */ assert(ss); assert(sslen); @@ -414,19 +414,19 @@ int TargetGroup::get_next_host(struct sockaddr_storage *ss, size_t *sslen) { } /* 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]]); + 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; + /* 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; + /* This octet is finished so I reset it to the beginning */ + current[octet] = 0; } } if (octet == -1) { @@ -482,12 +482,12 @@ int TargetGroup::return_last_host() { } 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; + /* 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]; + /* This octet is already at the beginning, so I set it to the end */ + current[octet] = last[octet]; } } assert(octet != -1); @@ -505,7 +505,7 @@ int TargetGroup::return_last_host() { this class instance is used -- the array is NOT copied. */ HostGroupState::HostGroupState(int lookahead, int rnd, - char *expr[], int numexpr) { + char *expr[], int numexpr) { assert(lookahead > 0); hostbatch = (Target **) safe_zalloc(sizeof(Target *) * lookahead); max_batch_sz = lookahead;