-
Notifications
You must be signed in to change notification settings - Fork 4
/
ports.c
66 lines (54 loc) · 967 Bytes
/
ports.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ftpscan.h"
#define MAX_PORT 65535
static char port_map[MAX_PORT + 1];
static int nxt_port = 1;
static void process_tok(char *);
static void process_port(int n);
static void process_range(int, int);
int
ports_initialize(char *ports_string)
{
char *tok;
for(tok = strtok(ports_string, ","); tok; tok = strtok(NULL, ","))
process_tok(tok);
return 0;
}
static void
process_tok(char *tok)
{
char *p;
if((p = index(tok, '-')) != NULL) {
*p++ = 0;
process_range(atoi(tok), atoi(p));
return;
}
process_port(atoi(tok));
}
static void
process_port(int n)
{
if(n > 0 && n <= MAX_PORT)
port_map[n] = 1;
debug("added port %d", n);
}
static void
process_range(int low, int high)
{
int i;
for(i = low; i <= high; i++)
process_port(i);
}
int
next_port()
{
while(1) {
if(nxt_port > MAX_PORT)
return 0;
if(port_map[nxt_port])
return nxt_port++;
nxt_port++;
}
}