-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecurityAddUnit.pas
237 lines (222 loc) · 8.18 KB
/
SecurityAddUnit.pas
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
unit SecurityAddUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, Math, UEditIPAddr;
type
TSecurityAddForm = class(TForm)
IPRadio: TRadioButton;
SubnetRadio: TRadioButton;
Label1: TLabel;
Label2: TLabel;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
NetEdit: CEditIPAddr;
MaskEdit: CEditIPAddr;
procedure IPRadioClick(Sender: TObject);
procedure SubnetRadioClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
private
procedure CheckOptions;
Function CheckSubnet(octsused : Integer) : Boolean;
public
{ Public declarations }
end;
var
SecurityAddForm : TSecurityAddForm;
NetOct1,NetOct2,NetOct3,NetOct4 : Integer;
MaskOct1,MaskOct2,MaskOct3,MaskOct4 : Integer;
implementation
{$R *.dfm}
procedure TSecurityAddForm.CheckOptions;
Begin
If (IPRadio.Checked) Then
Begin
Label1.Caption := 'IP Address:';
Label2.Hide;
MaskEdit.Hide;
End Else
Begin
Label1.Caption := 'Network ID:';
Label2.Show;
MaskEdit.Show;
End;
End;
procedure TSecurityAddForm.IPRadioClick(Sender: TObject);
begin
CheckOptions;
end;
procedure TSecurityAddForm.SubnetRadioClick(Sender: TObject);
begin
CheckOptions;
end;
procedure TSecurityAddForm.FormShow(Sender: TObject);
begin
CheckOptions;
end;
Function TSecurityAddForm.CheckSubnet(octsused : Integer) : Boolean;
Var
StartSubnet : Extended;
MaxSubnets : Extended;
IsValid : Boolean;
i : Extended;
Begin
IsValid := True;
StartSubnet := 0;
MaxSubnets := 0;
Case octsused of
1 : StartSubnet := 256 - MaskOct1;
2 : StartSubnet := 256 - MaskOct2;
3 : StartSubnet := 256 - MaskOct3;
4 : StartSubnet := 256 - MaskOct4;
End;
Case octsused of
1 : Case MaskOct1 of
254 : MaxSubnets := IntPower(2,7) - 2; // 7 bits = 128 64 32 16 8 4 2
252 : MaxSubnets := IntPower(2,6) - 2; // 6 bits = 128 64 32 16 8 4
248 : MaxSubnets := IntPower(2,5) - 2; // 5 bits = 128 64 32 16 8
240 : MaxSubnets := IntPower(2,4) - 2; // 4 bits = 128 64 32 16
224 : MaxSubnets := IntPower(2,3) - 2; // 3 bits = 128 64 32
192 : MaxSubnets := IntPower(2,2) - 2; // 2 bits = 128 64
128 : MaxSubnets := IntPower(2,1) - 1; // 1 bits = 128 = only minus 1 otherwise = 0 = bad !!
End;
2 : Case MaskOct2 of
254 : MaxSubnets := IntPower(2,7) - 2; // 7 bits = 128 64 32 16 8 4 2
252 : MaxSubnets := IntPower(2,6) - 2; // 6 bits = 128 64 32 16 8 4
248 : MaxSubnets := IntPower(2,5) - 2; // 5 bits = 128 64 32 16 8
240 : MaxSubnets := IntPower(2,4) - 2; // 4 bits = 128 64 32 16
224 : MaxSubnets := IntPower(2,3) - 2; // 3 bits = 128 64 32
192 : MaxSubnets := IntPower(2,2) - 2; // 2 bits = 128 64
128 : MaxSubnets := IntPower(2,1) - 1; // 1 bits = 128 = only minus 1 otherwise = 0 = bad !!
End;
3 : Case MaskOct3 of
254 : MaxSubnets := IntPower(2,7) - 2; // 7 bits = 128 64 32 16 8 4 2
252 : MaxSubnets := IntPower(2,6) - 2; // 6 bits = 128 64 32 16 8 4
248 : MaxSubnets := IntPower(2,5) - 2; // 5 bits = 128 64 32 16 8
240 : MaxSubnets := IntPower(2,4) - 2; // 4 bits = 128 64 32 16
224 : MaxSubnets := IntPower(2,3) - 2; // 3 bits = 128 64 32
192 : MaxSubnets := IntPower(2,2) - 2; // 2 bits = 128 64
128 : MaxSubnets := IntPower(2,1) - 1; // 1 bits = 128 = only minus 1 otherwise = 0 = bad !!
End;
4 : Case MaskOct4 of
254 : MaxSubnets := IntPower(2,7) - 2; // 7 bits = 128 64 32 16 8 4 2
252 : MaxSubnets := IntPower(2,6) - 2; // 6 bits = 128 64 32 16 8 4
248 : MaxSubnets := IntPower(2,5) - 2; // 5 bits = 128 64 32 16 8
240 : MaxSubnets := IntPower(2,4) - 2; // 4 bits = 128 64 32 16
224 : MaxSubnets := IntPower(2,3) - 2; // 3 bits = 128 64 32
192 : MaxSubnets := IntPower(2,2) - 2; // 2 bits = 128 64
128 : MaxSubnets := IntPower(2,1) - 1; // 1 bits = 128 = only minus 1 otherwise = 0 = bad !!
End;
End;
I := 0;
While (I < (StartSubnet*MaxSubnets)) and Not(IsValid) Do
Begin
i := i + StartSubnet;
Case octsused of
1 : If (NetOct1 = I) Then IsValid := True;
2 : If (NetOct2 = I) Then IsValid := True;
3 : If (NetOct3 = I) Then IsValid := True;
4 : If (NetOct4 = I) Then IsValid := True;
End;
End;
If Not(IsValid) Then Result := False
Else Result := True;
End;
procedure TSecurityAddForm.BitBtn1Click(Sender: TObject);
Var
OkOct1, OkOct2, OkOct3, OkOct4 : Boolean;
IsValid : Boolean;
a,b : Integer;
tmpstr : String;
begin
OkOct1 := False;
OkOct2 := False;
OkOct3 := False;
OkOct4 := False;
If (SubnetRadio.Checked) Then
Begin
tmpstr := MaskEdit.Text;
a := pos('.',tmpstr);
If strtoint(copy(tmpstr,1,(a-1))) In [255,254,252,248,240,224,192,128,0] Then
Begin
OkOct1 := True;
MaskOct1 := strtoint(copy(tmpstr,1,(a-1)));
End;
tmpstr[a] := ',';
b := pos('.',tmpstr);
If strtoint(copy(tmpstr,a+1,(b-a-1))) In [255,254,252,248,240,224,192,128,0] Then
Begin
OkOct2 := True;
MaskOct2 := strtoint(copy(tmpstr,a+1,(b-a-1)));
End;
a := b;
tmpstr[a] := ',';
b := pos('.',tmpstr);
If strtoint(copy(tmpstr,a+1,(b-a-1))) In [255,254,252,248,240,224,192,128,0] Then
Begin
OkOct3 := True;
MaskOct3 := strtoint(copy(tmpstr,a+1,(b-a-1)));
End;
tmpstr[b] := ',';
If strtoint(copy(tmpstr,b+1,length(tmpstr))) In [255,254,252,248,240,224,192,128,0] Then
Begin
OkOct4 := True;
MaskOct4 := strtoint(copy(tmpstr,b+1,length(tmpstr)));
End;
If Not(OkOct1) or Not(OkOct2) or Not(OkOct3) or Not(OkOct4) Then
Begin
MessageDlg('Invalid Subnet mask!'+#13+'Please type in a correct subnet mask.', mtError, [mbOk], 0);
Exit;
End;
// Get IP Address
tmpstr := NetEdit.Text;
a := pos('.',tmpstr);
NetOct1 := strtoint(copy(tmpstr,1,(a-1)));
tmpstr[a] := ',';
b := pos('.',tmpstr);
NetOct2 := strtoint(copy(tmpstr,a+1,(b-a-1)));
a := b;
tmpstr[a] := ',';
b := pos('.',tmpstr);
NetOct3 := strtoint(copy(tmpstr,a+1,(b-a-1)));
tmpstr[b] := ',';
NetOct4 := strtoint(copy(tmpstr,b+1,length(tmpstr)));
If (MaskOct1 <> 255) Then IsValid := CheckSubnet(1)
Else
If (MaskOct1 = 255) and (MaskOct2 <> 255) Then IsValid := CheckSubnet(2)
Else
If (MaskOct1 = 255) and (MaskOct2 = 255) and
(MaskOct3 <> 255) Then IsValid := CheckSubnet(3)
Else IsValid := CheckSubnet(4);
If Not(IsValid) Then
Begin
MessageDlg('Invalid Subnet ID!'+#13+'Please type in a correct subnet ID.', mtError, [mbOk], 0);
Exit;
End;
ModalResult := MrOk;
Exit;
End Else
Begin
tmpstr := NetEdit.Text;
a := pos('.',tmpstr);
NetOct1 := strtoint(copy(tmpstr,1,(a-1)));
tmpstr[a] := ',';
b := pos('.',tmpstr);
NetOct2 := strtoint(copy(tmpstr,a+1,(b-a-1)));
a := b;
tmpstr[a] := ',';
b := pos('.',tmpstr);
NetOct3 := strtoint(copy(tmpstr,a+1,(b-a-1)));
tmpstr[b] := ',';
NetOct4 := strtoint(copy(tmpstr,b+1,length(tmpstr)));
If (NetOct1 in [0,255]) or (NetOct2 = 255) or (NetOct3 = 255) or (NetOct4 in [0,255]) Then
Begin
MessageDlg('Invalid IP Address!'+#13+'Please type in a correct IP Address.', mtError, [mbOk], 0);
Exit;
End;
End;
ModalResult := MrOk;
Exit;
end;
end.