-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPortRunner.cpp
198 lines (164 loc) · 3.99 KB
/
PortRunner.cpp
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*******************************************************
* PortSpy©
*
* @author TheAbstractCompany, YNOP([email protected])
* @vertion beta
* @date October 19 1999
*******************************************************/
#include <String.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "PortRunner.h"
/*******************************************************
*
*******************************************************/
PortRunner::PortRunner(const char *str){
Set(str);
}
/*******************************************************
*
*******************************************************/
PortRunner::~PortRunner(){
// delte everthing
RemoveList();
}
/*******************************************************
*
*******************************************************/
void PortRunner::Set(const char *str){
BString s(str);
RemoveList();
char name[20];
char *ptr = s.LockBuffer(0);
while(sscanf(ptr, "%[-0-9]", name) == 1) {
//printf("\"%s\"\n",name);
int start,end;
if(sscanf(name,"%i-%i",&start,&end) == 2){
//printf("Adding range %i -> %i\n",start,end);
AddPortRange(start,end);
}else{
//printf("Adding %i\n",atoi(name));
AddPort(atoi(name));
}
ptr += strlen(name)+1;
}
cur = head;
s.UnlockBuffer();
}
/*******************************************************
*
*******************************************************/
void PortRunner::AddPort(int p){
PortItem *i = new PortItem;
i->next = NULL;
i->multi = false;
i->sport = p;
i->eport = p;
i->curp = -1;
if(head){
tail->next = i;
tail = tail->next;
}else{
head = i;
tail = head;
}
if(cur == NULL){
cur = head;
}
count++;
}
/*******************************************************
*
*******************************************************/
void PortRunner::AddPortRange(int s, int e){
if(e < s){ int t = s; s = e; e = t; }
PortItem *i = new PortItem;
i->next = NULL;
i->multi = true;
i->sport = s;
i->eport = e;
i->curp = -1;
if(head){
tail->next = i;
tail = tail->next;
}else{
head = i;
tail = head;
}
if(cur == NULL){
cur = head;
}
count += e - s + 1;
}
/*******************************************************
*
*******************************************************/
void PortRunner::PrintPorts(){
PortItem *p = head; // dont desturb cur pointer :P
while(p){
if(p->multi){
printf("%i -> %i, ",p->sport,p->eport);
}else{
printf("%i, ",p->sport);
}
p = p->next;
}
printf("\n");
int a = 0;
while((a = GetNextPort()) != 0){
printf("%i,",a);
}
printf(" (%i)\n",count);
}
/*******************************************************
*
*******************************************************/
int PortRunner::GetNextPort(){
if(cur == NULL){
return 0;
}
if(cur->multi){
// check to see if we have gone though
// all the ones in this range
if(cur->curp == -1){
// havent started yet
cur->curp = cur->sport;
return cur->curp++;
}else if(cur->curp >= cur->eport){
// already finished ..
cur->curp = -1;
int port = cur->eport;
cur = cur->next;
return port;
}else{
// somewher in the middle
return cur->curp++;
}
}else{
int port = cur->sport;
cur = cur->next;
return port;
}
return 0; // never get here :P
}
/*******************************************************
*
*******************************************************/
int PortRunner::CountPorts(){
return 1;
}
/*******************************************************
*
*******************************************************/
void PortRunner::RemoveList(){
PortItem *p = head;
cur = p;
while(p){
p = p->next;
delete cur;
cur = p;
}
head = tail = cur = NULL;
count = 0;
}