Skip to content

Commit

Permalink
range file first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
robertdavidgraham committed May 22, 2019
1 parent d13f037 commit 6e119b0
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 3 deletions.
226 changes: 226 additions & 0 deletions src/range-file.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
#include "range-file.h"
#include "ranges.h"
#include "ranges6.h"
#include "util-malloc.h"

struct RangeParser
{
unsigned long long line_number;
unsigned long long char_number;
unsigned state;
unsigned tmp;
unsigned char digit_count;
unsigned addr;
unsigned begin;
unsigned end;
};

static struct RangeParser *
rangeparse_create(void)
{
struct RangeParser *result;

result = CALLOC(1, sizeof(*result));

return result;
}
static void
rangeparse_destroy(struct RangeParser *p)
{
free(p);
}

static void
rangeparse_err(struct RangeParser *p, unsigned long long *line_number, unsigned long long *charindex)
{
*line_number = p->line_number;
*charindex = p->char_number;
}

static int
rangeparse_next(struct RangeParser *p, const unsigned char *buf, size_t *r_offset, size_t length,
unsigned *r_begin, unsigned *r_end)
{
size_t i = *r_offset;
unsigned state = p->state;
enum {
LINE_START, ADDR_START,
COMMENT,
NUMBER0, NUMBER1, NUMBER2, NUMBER3, NUMBER_ERR,
SECOND0, SECOND1, SECOND2, SECOND3, SECOND_ERR,
CIDR,
ERROR
};
int result = 0;

while (i < length) {
unsigned char c = buf[i++];
p->char_number++;
switch (state) {
case LINE_START:
case ADDR_START:
switch (c) {
case ' ': case '\t': case '\r':
/* ignore leading whitespace */
continue;
case '\n':
p->line_number++;
p->char_number = 0;
continue;
case '#': case ';': case '/': case '-':
state = COMMENT;
continue;

case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
p->tmp = (c - '0');
p->digit_count = 1;
state = NUMBER0;
break;
default:
state = ERROR;
length = i; /* break out of loop */
break;
}
break;
case COMMENT:
if (c == '\n') {
state = LINE_START;
p->line_number++;
p->char_number = 0;
}
break;
case NUMBER0:
case NUMBER1:
case NUMBER2:
case NUMBER3:
case SECOND0:
case SECOND1:
case SECOND2:
case SECOND3:
switch (c) {
case '.':
p->addr = (p->addr << 8) | p->tmp;
p->tmp = 0;
p->digit_count = 0;
state++;
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
if (p->digit_count == 3) {
state = ERROR;
length = i; /* break out of loop */
} else {
p->digit_count++;
p->tmp = p->tmp * 10 + (c - '0');
continue;
}
break;
case '-':
if (state == NUMBER3) {
p->begin = (p->addr << 8) | p->tmp;
p->tmp = 0;
p->digit_count = 0;
p->addr = 0;
state = SECOND0;
} else {
state = NUMBER_ERR;
length = i;
}
break;
case '/':
if (state == NUMBER3) {
p->begin = (p->addr << 8) | p->tmp;
p->tmp = 0;
p->digit_count = 0;
p->addr = 0;
state = CIDR;
} else {
state = NUMBER_ERR;
length = i; /* break out of loop */
}
break;
case ',':
case ' ':
case '\t':
case '\r':
case '\n':
if (state == NUMBER3) {
p->begin = (p->addr << 8) | p->tmp;
p->end = p->begin;
p->tmp = 0;
p->digit_count = 0;
p->addr = 0;
state = ADDR_START;
length = i; /* break out of loop */
if (c == '\n') {
p->line_number++;
p->char_number++;
}
*r_begin = p->begin;
*r_end = p->end;
result = 1;
} else if (state == SECOND3) {
p->end = (p->addr << 8) | p->tmp;
p->tmp = 0;
p->digit_count = 0;
p->addr = 0;
state = ADDR_START;
length = i; /* break out of loop */
if (c == '\n') {
p->line_number++;
p->char_number++;
}
*r_begin = p->begin;
*r_end = p->end;
result = 1;
} else {
state = NUMBER_ERR;
length = i;
}
break;
default:
state = ERROR;
length = i; /* break out of loop */
break;
}
break;

default:
case ERROR:
case NUMBER_ERR:
case SECOND_ERR:
state = ERROR;
length = i; /* break */
break;
}
}

*r_offset = i;
p->state = state;
if (state == ERROR || state == NUMBER_ERR || state == SECOND_ERR)
result = -1;
return result;
}

/***************************************************************************
***************************************************************************/
/***************************************************************************
***************************************************************************/
int
rangefile_test_buffer(const char *buf)
{
return 0;
}

/***************************************************************************
* Called during "make test" to run a regression test over this module.
***************************************************************************/
int
rangefile_test(void)
{
return 0;


}

21 changes: 21 additions & 0 deletions src/range-file.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef RANGE_FILE_H
#define RANGE_FILE_H

/*
range-file
Reads IP addresses and ranges from a file. Optimized to
read millions of addresses/ranges.
*/

struct RangeList;
struct Range6List;

int
rangefile_read(const char *filename, struct RangeList *targets_ipv4, struct Range6List *targets_ipv6);

int
rangefile_test(void);

#endif

6 changes: 3 additions & 3 deletions src/ranges.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ rangelist_add_range(struct RangeList *targets, unsigned begin, unsigned end)
* we often read in sequential addresses */
if (range_is_overlap(targets->list[targets->count - 1], range)) {
range_combine(&targets->list[targets->count - 1], range);

targets->is_sorted = 0;
return;
}

Expand Down Expand Up @@ -429,6 +429,7 @@ parse_ipv4(const char *line, unsigned *inout_offset, unsigned max, unsigned *ipv
return 0; /* parse ok */
}


/****************************************************************************
* Parse from text an IPv4 address range. This can be in one of several
* formats:
Expand Down Expand Up @@ -859,9 +860,8 @@ rangelist_parse_ports(struct RangeList *ports, const char *string, unsigned *is_
return p;
}


/***************************************************************************
* Called during "make regress" to run a regression test over this module.
* Called during "make test" to run a regression test over this module.
***************************************************************************/
int
ranges_selftest(void)
Expand Down
1 change: 1 addition & 0 deletions src/ranges.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef RANGES_H
#define RANGES_H
#include <stdint.h>
#include <stdio.h>

/**
* A range of either IP addresses or ports
Expand Down
1 change: 1 addition & 0 deletions src/util-malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#ifndef UTIL_MALLOC_H
#define UTIL_MALLOC_H
#include <stdio.h>
#include <stdlib.h>

void *
REALLOCARRAY(void *p, size_t count, size_t size);
Expand Down
6 changes: 6 additions & 0 deletions xcode4/masscan.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
1106827120DF863100E612AB /* ranges-avl.c in Sources */ = {isa = PBXBuildFile; fileRef = 1106826F20DF863000E612AB /* ranges-avl.c */; };
1107E9B2228DF7BB003384B3 /* proto-tcp-rdp.c in Sources */ = {isa = PBXBuildFile; fileRef = 1107E9B1228DF7BB003384B3 /* proto-tcp-rdp.c */; };
1107E9B52294DF22003384B3 /* range-file.c in Sources */ = {isa = PBXBuildFile; fileRef = 1107E9B32294DF22003384B3 /* range-file.c */; };
110C79B022833AFA003FAC94 /* proto-oproto.c in Sources */ = {isa = PBXBuildFile; fileRef = 110C79AF22833AF9003FAC94 /* proto-oproto.c */; };
110ED16820CB0BC200690C91 /* proto-ntlmssp.c in Sources */ = {isa = PBXBuildFile; fileRef = 110ED16720CB0BC200690C91 /* proto-ntlmssp.c */; };
11126597197A086B00DC5987 /* out-unicornscan.c in Sources */ = {isa = PBXBuildFile; fileRef = 11126596197A086B00DC5987 /* out-unicornscan.c */; };
Expand Down Expand Up @@ -126,6 +127,8 @@
1106827020DF863000E612AB /* ranges-avl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "ranges-avl.h"; sourceTree = "<group>"; };
1107E9B0228DF7BB003384B3 /* proto-tcp-rdp.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "proto-tcp-rdp.h"; sourceTree = "<group>"; };
1107E9B1228DF7BB003384B3 /* proto-tcp-rdp.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "proto-tcp-rdp.c"; sourceTree = "<group>"; };
1107E9B32294DF22003384B3 /* range-file.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "range-file.c"; sourceTree = "<group>"; };
1107E9B42294DF22003384B3 /* range-file.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "range-file.h"; sourceTree = "<group>"; };
110C79AE22833AF9003FAC94 /* proto-oproto.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "proto-oproto.h"; sourceTree = "<group>"; };
110C79AF22833AF9003FAC94 /* proto-oproto.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "proto-oproto.c"; sourceTree = "<group>"; };
110ED16520CA6A8300690C91 /* proto-spnego.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "proto-spnego.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -559,6 +562,8 @@
11B360CD1F90174B0020F3A3 /* misc */ = {
isa = PBXGroup;
children = (
1107E9B32294DF22003384B3 /* range-file.c */,
1107E9B42294DF22003384B3 /* range-file.h */,
11B5898021C0841100DD6248 /* ranges6.c */,
11B5897F21C0841100DD6248 /* ranges6.h */,
1106826F20DF863000E612AB /* ranges-avl.c */,
Expand Down Expand Up @@ -699,6 +704,7 @@
11DD364B2108E0CB00CBE1DE /* scripting-banner.c in Sources */,
11A921F417DBCC7E00DDFD32 /* rte-ring.c in Sources */,
11A921F517DBCC7E00DDFD32 /* smack1.c in Sources */,
1107E9B52294DF22003384B3 /* range-file.c in Sources */,
11A921F617DBCC7E00DDFD32 /* smackqueue.c in Sources */,
11A921F717DBCC7E00DDFD32 /* string_s.c in Sources */,
11A921F817DBCC7E00DDFD32 /* syn-cookie.c in Sources */,
Expand Down

0 comments on commit 6e119b0

Please sign in to comment.