1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-30 19:39:07 +00:00

Replace redundant function, permit ranges in udp payload specs.

This commit is contained in:
dmiller
2019-12-30 04:48:50 +00:00
parent 697a22fd63
commit 499c07d9f9
2 changed files with 7 additions and 49 deletions

View File

@@ -144,6 +144,7 @@
#include "payload.h"
#include "utils.h"
#include "nmap_error.h"
#include "scan_lists.h"
extern NmapOps o;
@@ -186,49 +187,6 @@ struct token {
size_t len;
};
/* Returns a malloc-allocated list of the ports in portlist. portlist must
contain one or more integers 0 <= p < 65536, separated by commas. */
static unsigned short *parse_portlist(const char *portlist, unsigned int *count) {
uint32_t bitmap[65536 / 32];
unsigned short *result;
unsigned int i;
unsigned int p;
memset(bitmap, 0, sizeof(bitmap));
*count = 0;
for (;;) {
long l;
char *tail;
errno = 0;
l = strtol(portlist, &tail, 10);
if (portlist == tail || errno != 0 || l < 0 || l > 65535)
return NULL;
if (!(bitmap[l / 32] & (1 << (l % 32)))) {
bitmap[l / 32] |= (1 << (l % 32));
(*count)++;
}
if (*tail == '\0')
break;
else if (*tail == ',')
portlist = tail + 1;
else
return NULL;
}
assert(*count < 65536);
result = (unsigned short *) malloc(sizeof(*result) * *count);
if (result == NULL)
return NULL;
i = 0;
for (p = 0; p < 65536 && i < *count; p++) {
if (bitmap[p / 32] & (1 << (p % 32)))
result[i++] = p;
}
return result;
}
static unsigned long line_no;
/* Get the next token from fp. The return value is the token type, or -1 on
@@ -284,7 +242,7 @@ static int next_token(FILE *fp, struct token *token) {
} else {
i = 0;
token->text[i++] = c;
while ((c = fgetc(fp)) != EOF && (isalnum(c) || c == ',')) {
while ((c = fgetc(fp)) != EOF && (isalnum(c) || c == ',' || c == '-')) {
if (i + 1 >= sizeof(token->text))
return -1;
token->text[i++] = c;
@@ -308,7 +266,7 @@ static int load_payloads_from_file(FILE *fp) {
type = next_token(fp, &token);
for (;;) {
unsigned short *ports;
unsigned int count, p;
int count;
std::string payload_data;
while (type == TOKEN_NEWLINE)
@@ -325,7 +283,7 @@ static int load_payloads_from_file(FILE *fp) {
fprintf(stderr, "Expected a port list at line %lu of %s.\n", line_no, PAYLOAD_FILENAME);
return -1;
}
ports = parse_portlist(token.text, &count);
getpts_simple(token.text, SCAN_UDP_PORT, &ports, &count);
if (ports == NULL) {
fprintf(stderr, "Can't parse port list \"%s\" at line %lu of %s.\n", token.text, line_no, PAYLOAD_FILENAME);
return -1;
@@ -348,7 +306,7 @@ static int load_payloads_from_file(FILE *fp) {
type = next_token(fp, &token);
}
for (p = 0; p < count; p++) {
for (int p = 0; p < count; p++) {
struct proto_dport key(IPPROTO_UDP, ports[p]);
struct payload payload;