1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | // copied from http://www.codeproject.com/KB/edit/IPAddressTextBox.aspx
|
---|
23 |
|
---|
24 | using System;
|
---|
25 | using System.Collections;
|
---|
26 | using System.ComponentModel;
|
---|
27 | using System.Windows.Forms;
|
---|
28 | using System.Text.RegularExpressions;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Hive.Server.Console {
|
---|
31 | [
|
---|
32 | DefaultProperty("Text"),
|
---|
33 | DefaultEvent("TextChanged"),
|
---|
34 | ]
|
---|
35 | /// <summary>
|
---|
36 | /// IPAddressTextBox
|
---|
37 | /// Control to enter IP-Addresses manually
|
---|
38 | /// Supports Binary and Decimal Notation
|
---|
39 | /// Supports input of CIDR Notation (appending of Bitmask of Subnetmask devided by Slash)
|
---|
40 | /// Support input of IP-Address in IPv6 format
|
---|
41 | /// </summary>
|
---|
42 | public class IPAddressTextBox : System.Windows.Forms.TextBox {
|
---|
43 | /// <summary>
|
---|
44 | /// Required designer variable.
|
---|
45 | /// </summary>
|
---|
46 | private System.ComponentModel.Container components = null;
|
---|
47 |
|
---|
48 | private IPNotation m_ipNotation = IPNotation.IPv4Decimal;
|
---|
49 | private IPNotation m_newIPNotation = IPNotation.IPv4Decimal;
|
---|
50 | private bool m_bOverwrite = true;
|
---|
51 | private bool m_bPreventLeave = true;
|
---|
52 | private System.Windows.Forms.ErrorProvider error;
|
---|
53 | private Regex m_regexValidNumbers = new Regex("[0-9]");
|
---|
54 | private ArrayList m_arlDelimeter = new ArrayList(new char[] { '.' });
|
---|
55 |
|
---|
56 |
|
---|
57 | public enum IPNotation {
|
---|
58 | IPv4Decimal, //192.168.000.001
|
---|
59 | IPv4Binary, //11000000.10101000.00000000.00000001
|
---|
60 | IPv4DecimalCIDR, //192.168.000.001/16
|
---|
61 | IPv4BinaryCIDR, //11000000.10101000.00000000.00000001/16
|
---|
62 | }
|
---|
63 |
|
---|
64 | /// <summary>
|
---|
65 | /// Constructor
|
---|
66 | /// </summary>
|
---|
67 | public IPAddressTextBox()
|
---|
68 | : base() {
|
---|
69 | this.InitializeComponent();
|
---|
70 |
|
---|
71 | this.ResetText();
|
---|
72 | }
|
---|
73 |
|
---|
74 | private void InitializeComponent() {
|
---|
75 | this.error = new System.Windows.Forms.ErrorProvider();
|
---|
76 | }
|
---|
77 |
|
---|
78 | /// <summary>
|
---|
79 | /// Clean up any resources being used.
|
---|
80 | /// </summary>
|
---|
81 | protected override void Dispose(bool disposing) {
|
---|
82 | if (disposing) {
|
---|
83 | if (components != null)
|
---|
84 | components.Dispose();
|
---|
85 | }
|
---|
86 | base.Dispose(disposing);
|
---|
87 | }
|
---|
88 |
|
---|
89 | #region Properties
|
---|
90 | #region Not allowed Properties from BaseClass
|
---|
91 | /// <summary>
|
---|
92 | /// Multiline is not allowed
|
---|
93 | /// </summary>
|
---|
94 | [
|
---|
95 | Category("Behavior"),
|
---|
96 | Description("Multiline is not allowed"),
|
---|
97 | DefaultValue(false),
|
---|
98 | Browsable(false)
|
---|
99 | ]
|
---|
100 | public override bool Multiline {
|
---|
101 | get {
|
---|
102 | return base.Multiline;
|
---|
103 | }
|
---|
104 | set {
|
---|
105 | //base.Multiline = value;
|
---|
106 | base.Multiline = false;
|
---|
107 | }
|
---|
108 | }
|
---|
109 | [
|
---|
110 | Category("Behavior"),
|
---|
111 | Description("AllowDrop is not allowed"),
|
---|
112 | DefaultValue(false),
|
---|
113 | Browsable(false)
|
---|
114 | ]
|
---|
115 | public override bool AllowDrop {
|
---|
116 | get {
|
---|
117 | return base.AllowDrop;
|
---|
118 | }
|
---|
119 | set {
|
---|
120 | //base.AllowDrop = value;
|
---|
121 | base.AllowDrop = false;
|
---|
122 | }
|
---|
123 | }
|
---|
124 | [
|
---|
125 | Category("Behavior"),
|
---|
126 | Description("AcceptsReturn is not allowed"),
|
---|
127 | DefaultValue(false),
|
---|
128 | Browsable(false)
|
---|
129 | ]
|
---|
130 | public new bool AcceptsReturn {
|
---|
131 | get {
|
---|
132 | return base.AcceptsReturn;
|
---|
133 | }
|
---|
134 | set {
|
---|
135 | //base.AcceptsReturn = value;
|
---|
136 | base.AcceptsReturn = false;
|
---|
137 | }
|
---|
138 | }
|
---|
139 | [
|
---|
140 | Category("Behavior"),
|
---|
141 | Description("AcceptsTab is not allowed"),
|
---|
142 | DefaultValue(false),
|
---|
143 | Browsable(false)
|
---|
144 | ]
|
---|
145 | public new bool AcceptsTab {
|
---|
146 | get {
|
---|
147 | return base.AcceptsTab;
|
---|
148 | }
|
---|
149 | set {
|
---|
150 | //base.AcceptTab = value;
|
---|
151 | base.AcceptsTab = false;
|
---|
152 | }
|
---|
153 | }
|
---|
154 | [
|
---|
155 | Category("Behavior"),
|
---|
156 | Description("CharacterCasing is not allowed"),
|
---|
157 | DefaultValue(CharacterCasing.Normal),
|
---|
158 | Browsable(false)
|
---|
159 | ]
|
---|
160 | public new CharacterCasing CharacterCasing {
|
---|
161 | get {
|
---|
162 | return base.CharacterCasing;
|
---|
163 | }
|
---|
164 | set {
|
---|
165 | //base.CharacterCasing = value;
|
---|
166 | base.CharacterCasing = CharacterCasing.Normal;
|
---|
167 | }
|
---|
168 | }
|
---|
169 | [
|
---|
170 | Category("Behavior"),
|
---|
171 | Description("WordWrap is not allowed"),
|
---|
172 | DefaultValue(true),
|
---|
173 | Browsable(false)
|
---|
174 | ]
|
---|
175 | public new bool WordWrap {
|
---|
176 | get {
|
---|
177 | return base.WordWrap;
|
---|
178 | }
|
---|
179 | set {
|
---|
180 | //base.WordWrap = value;
|
---|
181 | base.WordWrap = true;
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | /// <summary>
|
---|
186 | /// Maxlength must not be changed by user
|
---|
187 | /// </summary>
|
---|
188 | [
|
---|
189 | Category("Behavior"),
|
---|
190 | Description("Specifies maximum length of a String. Change is not allowed"),
|
---|
191 | DefaultValue(15),
|
---|
192 | Browsable(false)
|
---|
193 | ]
|
---|
194 | public override int MaxLength {
|
---|
195 | get {
|
---|
196 | return base.MaxLength;
|
---|
197 | }
|
---|
198 | set {
|
---|
199 | //base.MaxLength = value;
|
---|
200 | base.MaxLength = this.Text.Length;
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | #endregion // Not allowed Properties from BaseClass
|
---|
205 |
|
---|
206 | /// <summary>
|
---|
207 | /// Specifies if IP-Address
|
---|
208 | /// </summary>
|
---|
209 | [
|
---|
210 | Category("Appearance"),
|
---|
211 | Description("Specifies the IP-Address"),
|
---|
212 | DefaultValue(" . . . ")
|
---|
213 | ]
|
---|
214 | public override string Text {
|
---|
215 | get {
|
---|
216 | return base.Text;
|
---|
217 | }
|
---|
218 | set {
|
---|
219 | try {
|
---|
220 | if (IPAddressTextBox.ValidateIP(value, this.m_newIPNotation, this.m_arlDelimeter))
|
---|
221 | base.Text = IPAddressTextBox.MakeValidSpaces(value, this.m_newIPNotation, this.m_arlDelimeter);
|
---|
222 | }
|
---|
223 | catch {
|
---|
224 |
|
---|
225 | }
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | /// <summary>
|
---|
230 | /// Specifies if Numbers should be overwritten
|
---|
231 | /// </summary>
|
---|
232 | [
|
---|
233 | Category("Behavior"),
|
---|
234 | Description("Specifies if Numbers should be overwritten"),
|
---|
235 | DefaultValue(true)
|
---|
236 | ]
|
---|
237 | public bool OverWriteMode {
|
---|
238 | get {
|
---|
239 | return this.m_bOverwrite;
|
---|
240 | }
|
---|
241 | set {
|
---|
242 | if (value != this.m_bOverwrite) {
|
---|
243 | this.m_bOverwrite = value;
|
---|
244 | this.OnOverWriteChanged(value);
|
---|
245 | }
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | /// <summary>
|
---|
250 | /// Prevents leaving of Control if there is an input-error
|
---|
251 | /// </summary>
|
---|
252 | [
|
---|
253 | Category("Behavior"),
|
---|
254 | Description("Prevents leaving of Control if there is an input-error"),
|
---|
255 | DefaultValue(true)
|
---|
256 | ]
|
---|
257 | public bool PreventLeaveAtError {
|
---|
258 | get {
|
---|
259 | return this.m_bPreventLeave;
|
---|
260 | }
|
---|
261 | set {
|
---|
262 | if (value != this.m_bPreventLeave) {
|
---|
263 | this.m_bPreventLeave = value;
|
---|
264 | this.OnPreventLeaveChanged(value);
|
---|
265 | }
|
---|
266 | }
|
---|
267 | }
|
---|
268 |
|
---|
269 | /// <summary>
|
---|
270 | /// Specifies if IP-Address Notation (IPv4, IPv6, Binary, Decimal, CIDR
|
---|
271 | /// </summary>
|
---|
272 | [
|
---|
273 | Category("Appearance"),
|
---|
274 | Description("Specifies if IP-Address Notation (IPv4, IPv6, Binary, Decimal, CIDR"),
|
---|
275 | DefaultValue(IPNotation.IPv4Decimal)
|
---|
276 | ]
|
---|
277 | public IPNotation Notation {
|
---|
278 | get {
|
---|
279 | return this.m_ipNotation;
|
---|
280 | }
|
---|
281 | set {
|
---|
282 | if (value != this.m_ipNotation) {
|
---|
283 | try {
|
---|
284 | this.m_newIPNotation = value;
|
---|
285 | this.ChangeNotation(this.m_ipNotation, this.m_newIPNotation);
|
---|
286 | this.m_ipNotation = this.m_newIPNotation;
|
---|
287 | this.OnNotationChanged(this.m_newIPNotation);
|
---|
288 | }
|
---|
289 | catch (Exception LastError) {
|
---|
290 | System.Diagnostics.Debug.WriteLine(LastError.Message);
|
---|
291 | throw LastError;
|
---|
292 | }
|
---|
293 | }
|
---|
294 | }
|
---|
295 | }
|
---|
296 |
|
---|
297 | /// <summary>
|
---|
298 | /// Specifies the Errorprovider that appears at invalid IPs
|
---|
299 | /// </summary>
|
---|
300 | [
|
---|
301 | Category("Appearance"),
|
---|
302 | Description("Specifies the Errorprovider that appears at invalid IPs"),
|
---|
303 | DefaultValue(false)
|
---|
304 | ]
|
---|
305 | public ErrorProvider IPError {
|
---|
306 | get {
|
---|
307 | return this.error;
|
---|
308 | }
|
---|
309 | }
|
---|
310 |
|
---|
311 |
|
---|
312 | #endregion //Properties
|
---|
313 |
|
---|
314 | #region Eventhandling
|
---|
315 |
|
---|
316 | /// <summary>
|
---|
317 | /// Delegate for Notation-Events
|
---|
318 | /// </summary>
|
---|
319 | public delegate void NotationChangedEventHandler(IPNotation arg_newValue);
|
---|
320 | /// <summary>
|
---|
321 | /// Event called if AppearanceMode Notation is changed
|
---|
322 | /// </summary>
|
---|
323 | public event NotationChangedEventHandler NotationChanged;
|
---|
324 | /// <summary>
|
---|
325 | /// Delegate for Bool-Properties-Events
|
---|
326 | /// </summary>
|
---|
327 | public delegate void BoolPropertyChangedEventHandler(bool arg_bNewValue);
|
---|
328 | /// <summary>
|
---|
329 | /// Event called if BehaviorMode OverWriteMode is changed
|
---|
330 | /// </summary>
|
---|
331 | public event BoolPropertyChangedEventHandler OverWriteModeChanged;
|
---|
332 | /// <summary>
|
---|
333 | /// Event called if BehaviorMode PreventLeave is changed
|
---|
334 | /// </summary>
|
---|
335 | public event BoolPropertyChangedEventHandler PreventLeaveChanged;
|
---|
336 |
|
---|
337 | /// <summary>
|
---|
338 | /// Occures when Appearance-Mode Notation was changed
|
---|
339 | /// </summary>
|
---|
340 | /// <param name="arg_Value">Value, Input IP-Address notation</param>
|
---|
341 | protected virtual void OnNotationChanged(IPNotation arg_Value) {
|
---|
342 | if (this.NotationChanged != null)
|
---|
343 | this.NotationChanged(arg_Value);
|
---|
344 | }
|
---|
345 |
|
---|
346 | private void ChangeNotation(IPNotation arg_oldValue, IPNotation arg_newValue) {
|
---|
347 | string sTo = "";
|
---|
348 | ArrayList arlFrom = new ArrayList(this.Text.Replace(" ", "").Split((char[])this.m_arlDelimeter.ToArray(typeof(char))));
|
---|
349 |
|
---|
350 | switch (arg_newValue) {
|
---|
351 | case IPNotation.IPv4Decimal:
|
---|
352 | this.m_regexValidNumbers = new Regex("[0-9]");
|
---|
353 | this.m_arlDelimeter = new ArrayList(new char[] { '.' });
|
---|
354 | break;
|
---|
355 | case IPNotation.IPv4DecimalCIDR:
|
---|
356 | this.m_regexValidNumbers = new Regex("[0-9]");
|
---|
357 | this.m_arlDelimeter = new ArrayList(new char[] { '.', '/' });
|
---|
358 | break;
|
---|
359 | case IPNotation.IPv4Binary:
|
---|
360 | this.m_regexValidNumbers = new Regex("[01]");
|
---|
361 | this.m_arlDelimeter = new ArrayList(new char[] { '.' });
|
---|
362 | break;
|
---|
363 | case IPNotation.IPv4BinaryCIDR:
|
---|
364 | this.m_regexValidNumbers = new Regex("[01]");
|
---|
365 | this.m_arlDelimeter = new ArrayList(new char[] { '.', '/' });
|
---|
366 | break;
|
---|
367 | default:
|
---|
368 | break;
|
---|
369 | }
|
---|
370 |
|
---|
371 | switch (arg_oldValue) {
|
---|
372 | case IPNotation.IPv4Decimal:
|
---|
373 | switch (arg_newValue) {
|
---|
374 | case IPNotation.IPv4Decimal:
|
---|
375 | break;
|
---|
376 | case IPNotation.IPv4DecimalCIDR:
|
---|
377 | for (int i = 0; i < arlFrom.Count; i++) {
|
---|
378 | sTo += arlFrom[i].ToString() +
|
---|
379 | //Add Slash if its the last IPPart, els add a dot
|
---|
380 | (i == arlFrom.Count - 1 ? "/ " : ".");
|
---|
381 | }
|
---|
382 | break;
|
---|
383 | case IPNotation.IPv4Binary:
|
---|
384 | for (int i = 0; i < arlFrom.Count; i++) {
|
---|
385 | //Convert Decimal to Binary
|
---|
386 | sTo += this.Dec2Bin(arlFrom[i].ToString()) +
|
---|
387 | //Add Dot if its not the last IPPart, else add nothing
|
---|
388 | (i == arlFrom.Count - 1 ? "" : ".");
|
---|
389 | }
|
---|
390 | break;
|
---|
391 | case IPNotation.IPv4BinaryCIDR:
|
---|
392 | for (int i = 0; i < arlFrom.Count; i++) {
|
---|
393 | //Convert Decimal to Binary
|
---|
394 | sTo += this.Dec2Bin(arlFrom[i].ToString()) +
|
---|
395 | //Add Slash if its the last IPPart, else add a dot
|
---|
396 | (i == arlFrom.Count - 1 ? "/ " : ".");
|
---|
397 | }
|
---|
398 | break;
|
---|
399 | default:
|
---|
400 | break;
|
---|
401 | }
|
---|
402 | break;
|
---|
403 | case IPNotation.IPv4DecimalCIDR:
|
---|
404 | switch (arg_newValue) {
|
---|
405 | case IPNotation.IPv4Decimal:
|
---|
406 | //do not use the last Item, its the Subnetmask
|
---|
407 | for (int i = 0; i < arlFrom.Count - 1; i++) {
|
---|
408 | sTo += arlFrom[i].ToString() +
|
---|
409 | //Add Dot if its not the last IPPart, else add nothing
|
---|
410 | (i == arlFrom.Count - 2 ? "" : ".");
|
---|
411 | }
|
---|
412 | break;
|
---|
413 | case IPNotation.IPv4DecimalCIDR:
|
---|
414 | break;
|
---|
415 | case IPNotation.IPv4Binary:
|
---|
416 | //do not use the last Item, its the Subnetmask
|
---|
417 | for (int i = 0; i < arlFrom.Count - 1; i++) {
|
---|
418 | //Convert Decimal to Binary
|
---|
419 | sTo += this.Dec2Bin(arlFrom[i].ToString()) +
|
---|
420 | //Add Dot if its not the last IPPart, else add nothing
|
---|
421 | (i == arlFrom.Count - 2 ? "" : ".");
|
---|
422 | }
|
---|
423 | break;
|
---|
424 | case IPNotation.IPv4BinaryCIDR:
|
---|
425 | //do not use the last Item, its the Subnetmask
|
---|
426 | for (int i = 0; i < arlFrom.Count - 1; i++) {
|
---|
427 | //Convert Decimal to Binary
|
---|
428 | sTo += this.Dec2Bin(arlFrom[i].ToString()) +
|
---|
429 | //Add Dot if its not the last IPPart, else add nothing
|
---|
430 | (i == arlFrom.Count - 2 ? "" : ".");
|
---|
431 | }
|
---|
432 | //Add Subnetmask
|
---|
433 | sTo += "/" + arlFrom[arlFrom.Count - 1];
|
---|
434 | break;
|
---|
435 | default:
|
---|
436 | break;
|
---|
437 | }
|
---|
438 | break;
|
---|
439 | case IPNotation.IPv4Binary:
|
---|
440 | switch (arg_newValue) {
|
---|
441 | case IPNotation.IPv4Decimal:
|
---|
442 | for (int i = 0; i < arlFrom.Count; i++) {
|
---|
443 | //Convert Binary to Decimal
|
---|
444 | sTo += this.Bin2Dec(arlFrom[i].ToString()) +
|
---|
445 | //Add Dot if its not the last IPPart, else add nothing
|
---|
446 | (i == arlFrom.Count - 1 ? "" : ".");
|
---|
447 | }
|
---|
448 | break;
|
---|
449 | case IPNotation.IPv4DecimalCIDR:
|
---|
450 | for (int i = 0; i < arlFrom.Count; i++) {
|
---|
451 | //Convert Binary to Decimal
|
---|
452 | sTo += this.Bin2Dec(arlFrom[i].ToString()) +
|
---|
453 | //Add Slash if its the last IPPart, els add a dot
|
---|
454 | (i == arlFrom.Count - 1 ? "/ " : ".");
|
---|
455 | }
|
---|
456 | break;
|
---|
457 | case IPNotation.IPv4Binary:
|
---|
458 | break;
|
---|
459 | case IPNotation.IPv4BinaryCIDR:
|
---|
460 | for (int i = 0; i < arlFrom.Count; i++) {
|
---|
461 | sTo += arlFrom[i].ToString() +
|
---|
462 | //Add Slash if its the last IPPart, else add a dot
|
---|
463 | (i == arlFrom.Count - 1 ? "/ " : ".");
|
---|
464 | }
|
---|
465 | break;
|
---|
466 | default:
|
---|
467 | break;
|
---|
468 | }
|
---|
469 | break;
|
---|
470 | case IPNotation.IPv4BinaryCIDR:
|
---|
471 | switch (arg_newValue) {
|
---|
472 | case IPNotation.IPv4Decimal:
|
---|
473 | //do not use the last Item, its the Subnetmask
|
---|
474 | for (int i = 0; i < arlFrom.Count - 1; i++) {
|
---|
475 | sTo += this.Bin2Dec(arlFrom[i].ToString()) +
|
---|
476 | //Add Dot if its not the last IPPart, else add nothing
|
---|
477 | (i == arlFrom.Count - 2 ? "" : ".");
|
---|
478 | }
|
---|
479 | break;
|
---|
480 | case IPNotation.IPv4DecimalCIDR:
|
---|
481 | //do not use the last Item, its the Subnetmask
|
---|
482 | for (int i = 0; i < arlFrom.Count - 1; i++) {
|
---|
483 | sTo += this.Bin2Dec(arlFrom[i].ToString()) +
|
---|
484 | //Add Dot if its not the last IPPart, else add nothing
|
---|
485 | (i == arlFrom.Count - 2 ? "" : ".");
|
---|
486 | }
|
---|
487 | //Add Subnetmask
|
---|
488 | sTo += "/" + arlFrom[arlFrom.Count - 1];
|
---|
489 | break;
|
---|
490 | case IPNotation.IPv4Binary:
|
---|
491 | //do not use the last Item, its the Subnetmask
|
---|
492 | for (int i = 0; i < arlFrom.Count - 1; i++) {
|
---|
493 | sTo += arlFrom[i].ToString() +
|
---|
494 | //Add Dot if its not the last IPPart, else add nothing
|
---|
495 | (i == arlFrom.Count - 2 ? "" : ".");
|
---|
496 | }
|
---|
497 | break;
|
---|
498 | case IPNotation.IPv4BinaryCIDR:
|
---|
499 | break;
|
---|
500 | default:
|
---|
501 | break;
|
---|
502 | }
|
---|
503 | break;
|
---|
504 | default:
|
---|
505 | break;
|
---|
506 | }
|
---|
507 |
|
---|
508 | this.Text = sTo;
|
---|
509 | this.MaxLength = this.TextLength;
|
---|
510 | }
|
---|
511 |
|
---|
512 | /// <summary>
|
---|
513 | /// Occures when Behaviour-Mode OverWriteMode was changed
|
---|
514 | /// </summary>
|
---|
515 | /// <param name="arg_bValue">Value, Overwrite Numbers in Editfield or not</param>
|
---|
516 | protected virtual void OnOverWriteChanged(bool arg_bValue) {
|
---|
517 | if (this.OverWriteModeChanged != null)
|
---|
518 | this.OverWriteModeChanged(arg_bValue);
|
---|
519 | }
|
---|
520 |
|
---|
521 | /// <summary>
|
---|
522 | /// Occures when Behaviour-Mode PreventLeave was changed
|
---|
523 | /// </summary>
|
---|
524 | /// <param name="arg_bValue">Value, leave control if there is an error or not</param>
|
---|
525 | protected virtual void OnPreventLeaveChanged(bool arg_bValue) {
|
---|
526 | if (this.PreventLeaveChanged != null)
|
---|
527 | this.PreventLeaveChanged(arg_bValue);
|
---|
528 | }
|
---|
529 |
|
---|
530 | #endregion //events
|
---|
531 |
|
---|
532 | #region Event-Overrides
|
---|
533 |
|
---|
534 | /// <summary>
|
---|
535 | /// Override standard KeyDownEventHandler
|
---|
536 | /// Catches Inputs of "." and "/" to jump to next positions
|
---|
537 | /// </summary>
|
---|
538 | /// <param name="e">KeyEventArgument</param>
|
---|
539 | protected override void OnKeyDown(KeyEventArgs e) {
|
---|
540 | //Zeichen an die richtige stelle schreiben
|
---|
541 | int iPos = this.SelectionStart;
|
---|
542 | char[] cText = this.Text.ToCharArray();
|
---|
543 |
|
---|
544 | if (e.Modifiers == Keys.None) {
|
---|
545 | if ((char.IsLetterOrDigit(Convert.ToChar(e.KeyValue)) || e.KeyCode == Keys.NumPad0)//Numpad0=96 --> `
|
---|
546 | && iPos < this.TextLength) {
|
---|
547 | if (this.m_arlDelimeter.Contains(cText[iPos]))
|
---|
548 | iPos += 1;
|
---|
549 | this.SelectionStart = iPos;
|
---|
550 | if (this.OverWriteMode) {
|
---|
551 | if (iPos < this.TextLength)
|
---|
552 | this.SelectionLength = 1;
|
---|
553 | } else {
|
---|
554 | if (iPos < this.TextLength)
|
---|
555 | if (cText[iPos] == ' ')
|
---|
556 | this.SelectionLength = 1;
|
---|
557 | }
|
---|
558 | }
|
---|
559 | }
|
---|
560 | base.OnKeyDown(e);
|
---|
561 | }
|
---|
562 |
|
---|
563 | /// <summary>
|
---|
564 | /// Override standard KeyUpEventHandler
|
---|
565 | /// Catches Inputs of "." and "/" to jump to next positions
|
---|
566 | /// </summary>
|
---|
567 | /// <param name="e">KeyEventArgument</param>
|
---|
568 | protected override void OnKeyUp(KeyEventArgs e) {
|
---|
569 | //Zeichen an die richtige stelle schreiben
|
---|
570 | int iPos = this.SelectionStart;
|
---|
571 | char[] cText = this.Text.ToCharArray();
|
---|
572 |
|
---|
573 | //Cursor hintern Punkt setzen
|
---|
574 | if ((char.IsLetterOrDigit(Convert.ToChar(e.KeyValue)) || e.KeyCode == Keys.NumPad0)//Numpad0=96 --> `
|
---|
575 | && iPos < this.TextLength) {
|
---|
576 | if (this.m_arlDelimeter.Contains(cText[iPos]))
|
---|
577 | iPos += 1;
|
---|
578 |
|
---|
579 | this.SelectionStart = iPos;
|
---|
580 | }
|
---|
581 | base.OnKeyUp(e);
|
---|
582 | }
|
---|
583 |
|
---|
584 |
|
---|
585 | /// <summary>
|
---|
586 | /// Override standard KeyPressEventHandler
|
---|
587 | /// Catches Inputs of "." and "/" to jump to next positions
|
---|
588 | /// </summary>
|
---|
589 | /// <param name="e">KeyPressEventArgument</param>
|
---|
590 | protected override void OnKeyPress(KeyPressEventArgs e) {
|
---|
591 | //valid input charachters
|
---|
592 | if (char.IsControl(e.KeyChar) ||
|
---|
593 | m_regexValidNumbers.IsMatch(e.KeyChar.ToString())) {
|
---|
594 | e.Handled = false;
|
---|
595 | } else {
|
---|
596 | switch (e.KeyChar) {
|
---|
597 | case '/':
|
---|
598 | this.JumpToSlash();
|
---|
599 | break;
|
---|
600 | case '.':
|
---|
601 | case ':':
|
---|
602 | this.JumpToNextDot();
|
---|
603 | break;
|
---|
604 | default:
|
---|
605 | break;
|
---|
606 | }
|
---|
607 | e.Handled = true;
|
---|
608 | }
|
---|
609 | base.OnKeyPress(e);
|
---|
610 | }
|
---|
611 |
|
---|
612 | /// <summary>
|
---|
613 | /// Override standard TextChangedEventHandler
|
---|
614 | /// Looks if inserted IP-Address is valid
|
---|
615 | /// </summary>
|
---|
616 | /// <param name="e">EventArgument</param>
|
---|
617 | protected override void OnTextChanged(EventArgs e) {
|
---|
618 | base.OnTextChanged(e);
|
---|
619 | if (this.Text.Length == 0)
|
---|
620 | this.ResetText();
|
---|
621 |
|
---|
622 | try {
|
---|
623 | if (!this.ValidateIP())
|
---|
624 | this.error.SetError(this, "Invalid IP-address");
|
---|
625 | else
|
---|
626 | this.error.SetError(this, "");
|
---|
627 | }
|
---|
628 | catch (Exception LastError) {
|
---|
629 | this.error.SetError(this, LastError.Message);
|
---|
630 | }
|
---|
631 | }
|
---|
632 |
|
---|
633 | /// <summary>
|
---|
634 | /// Override standard ValidatingEventHandler
|
---|
635 | /// Validates inserted IP-Address, and cancels Textbox if valid or PreventLeave=false
|
---|
636 | /// </summary>
|
---|
637 | /// <param name="e">CancelEventArgument</param>
|
---|
638 | protected override void OnValidating(CancelEventArgs e) {
|
---|
639 | //e.Cancel = true;//suppress cancel-signal = not validated
|
---|
640 | e.Cancel = (!this.ValidateIP() && this.m_bPreventLeave);
|
---|
641 | base.OnValidating(e);
|
---|
642 | }
|
---|
643 |
|
---|
644 | #endregion //Eventhandling
|
---|
645 |
|
---|
646 | #region Methods
|
---|
647 |
|
---|
648 | /// <summary>
|
---|
649 | /// Override standard ResetText
|
---|
650 | /// Fills Textbox with Dots and Slashes dependend on Properties
|
---|
651 | /// </summary>
|
---|
652 | public override void ResetText() {
|
---|
653 | base.ResetText();
|
---|
654 | switch (this.Notation) {
|
---|
655 | case IPNotation.IPv4Decimal:
|
---|
656 | this.Text = " . . . ";
|
---|
657 | break;
|
---|
658 | case IPNotation.IPv4DecimalCIDR:
|
---|
659 | this.Text = " . . . / ";
|
---|
660 | break;
|
---|
661 | case IPNotation.IPv4Binary:
|
---|
662 | this.Text = " . . . ";
|
---|
663 | break;
|
---|
664 | case IPNotation.IPv4BinaryCIDR:
|
---|
665 | this.Text = " . . . / ";
|
---|
666 | break;
|
---|
667 | default:
|
---|
668 | break;
|
---|
669 | }
|
---|
670 |
|
---|
671 | this.MaxLength = this.TextLength;
|
---|
672 | }
|
---|
673 |
|
---|
674 |
|
---|
675 | /// <summary>
|
---|
676 | /// Window-Message Constant
|
---|
677 | /// </summary>
|
---|
678 | protected const int WM_KEYDOWN = 0x0100;
|
---|
679 |
|
---|
680 | /// <summary>
|
---|
681 | /// Override standard PreProcessMessge
|
---|
682 | /// Catches Inputs of Backspaces and Deletes to remove IP-Digits at the right position
|
---|
683 | /// </summary>
|
---|
684 | /// <param name="msg">Process Message</param>
|
---|
685 | public override bool PreProcessMessage(ref Message msg) {
|
---|
686 | if (msg.Msg == WM_KEYDOWN) {
|
---|
687 | Keys keyData = ((Keys)(int)msg.WParam) | ModifierKeys;
|
---|
688 | Keys keyCode = ((Keys)(int)msg.WParam);
|
---|
689 |
|
---|
690 | int iPos = this.SelectionStart;
|
---|
691 | char[] cText = this.Text.ToCharArray();
|
---|
692 | switch (keyCode) {
|
---|
693 | case Keys.Delete:
|
---|
694 | if (iPos < this.TextLength) {
|
---|
695 | while (cText[iPos] == '.' || cText[iPos] == ':' || cText[iPos] == '/') {
|
---|
696 | if ((iPos += 1) >= cText.Length)
|
---|
697 | break;
|
---|
698 | }
|
---|
699 | if (iPos < this.TextLength) {
|
---|
700 | base.Text = this.Text.Substring(0, iPos) + " " + this.Text.Substring(iPos + 1);
|
---|
701 | this.SelectionStart = iPos + 1;
|
---|
702 | } else
|
---|
703 | this.SelectionStart = this.TextLength - 1;
|
---|
704 | }
|
---|
705 | return true;
|
---|
706 | case Keys.Back:
|
---|
707 | if (iPos > 0) {
|
---|
708 | while (cText[iPos - 1] == '.' || cText[iPos - 1] == ':' || cText[iPos - 1] == '/') {
|
---|
709 | if ((iPos -= 1) <= 0)
|
---|
710 | break;
|
---|
711 | }
|
---|
712 | if (iPos > 0) {
|
---|
713 | base.Text = this.Text.Substring(0, iPos - 1) + " " + this.Text.Substring(iPos);
|
---|
714 | this.SelectionStart = iPos - 1;
|
---|
715 | } else
|
---|
716 | this.SelectionStart = 0;
|
---|
717 | }
|
---|
718 | return true;
|
---|
719 | default:
|
---|
720 | break;
|
---|
721 | }
|
---|
722 | }
|
---|
723 | return base.PreProcessMessage(ref msg);
|
---|
724 | }
|
---|
725 |
|
---|
726 | /// <summary>
|
---|
727 | /// Returns the formatted IP-Addresses without spaces and Zeroes
|
---|
728 | /// </summary>
|
---|
729 | /// <returns>IP-Address without spaces and Zeroes</returns>
|
---|
730 | public string GetPureIPAddress() {
|
---|
731 | string s = "";
|
---|
732 | ArrayList arlIP = new ArrayList(this.Text.Replace(" ", "").Split((char[])this.m_arlDelimeter.ToArray(typeof(char))));
|
---|
733 | for (int i = 0; i < arlIP.Count; i++) {
|
---|
734 | while (arlIP[i].ToString().StartsWith("0"))
|
---|
735 | arlIP[i] = arlIP[i].ToString().Substring(1);
|
---|
736 | }
|
---|
737 | s = IPAddressTextBox.MakeIP((string[])arlIP.ToArray(typeof(string)), this.m_ipNotation);
|
---|
738 | return s;
|
---|
739 | }
|
---|
740 |
|
---|
741 | #endregion //Methods
|
---|
742 |
|
---|
743 | #region Helperfunctions
|
---|
744 |
|
---|
745 | /// <summary>
|
---|
746 | /// Sets Inputcursor to Subnet-Slash
|
---|
747 | /// </summary>
|
---|
748 | private void JumpToSlash() {
|
---|
749 | int iSelStart = this.Text.LastIndexOf("/");
|
---|
750 | if (iSelStart >= 0) {
|
---|
751 | this.Select(iSelStart + 1, 0);
|
---|
752 | }
|
---|
753 | }
|
---|
754 |
|
---|
755 | /// <summary>
|
---|
756 | /// Sets input cursour to next Dot
|
---|
757 | /// </summary>
|
---|
758 | private void JumpToNextDot() {
|
---|
759 | int iSelStart = this.Text.IndexOf('.', this.SelectionStart);
|
---|
760 | if (iSelStart >= 0) {
|
---|
761 | this.Select(iSelStart + 1, 0);
|
---|
762 | } else {
|
---|
763 | iSelStart = this.Text.IndexOf(':', this.SelectionStart);
|
---|
764 | if (iSelStart >= 0) {
|
---|
765 | this.Select(iSelStart + 1, 0);
|
---|
766 | }
|
---|
767 | }
|
---|
768 | }
|
---|
769 |
|
---|
770 | /// <summary>
|
---|
771 | /// Converts Decimal IP-Part to Binary (default IPv6 = false)
|
---|
772 | /// </summary>
|
---|
773 | /// <param name="arg_sDec">Decimal IP-Part</param>
|
---|
774 | /// <param name="arg_bIPv6">Binary for IPv6 (has 16 digits)</param>
|
---|
775 | /// <returns>Binary IP-Part</returns>
|
---|
776 | private string Dec2Bin(string arg_sDec) {
|
---|
777 | return this.Dec2Bin(arg_sDec, false);
|
---|
778 | }
|
---|
779 | /// <summary>
|
---|
780 | /// Converts Decimal IP-Part to Binary
|
---|
781 | /// </summary>
|
---|
782 | /// <param name="arg_sDec">Decimal IP-Part</param>
|
---|
783 | /// <param name="arg_bIPv6">Binary for IPv6 (has 16 digits)</param>
|
---|
784 | /// <returns>Binary IP-Part</returns>
|
---|
785 | private string Dec2Bin(string arg_sDec, bool arg_bIPv6) {
|
---|
786 | string sBin = (arg_bIPv6 ? "0000000000000000" : "00000000"), sSubnet = "";
|
---|
787 | arg_sDec = arg_sDec.Trim();
|
---|
788 | while (arg_sDec.Length < 3)
|
---|
789 | arg_sDec = "0" + arg_sDec;
|
---|
790 | if (arg_sDec.IndexOf("/") >= 0) {
|
---|
791 | sSubnet = arg_sDec.Substring(arg_sDec.IndexOf("/"));
|
---|
792 | arg_sDec = arg_sDec.Substring(0, arg_sDec.IndexOf("/"));
|
---|
793 | }
|
---|
794 | int iDec = Convert.ToInt32(arg_sDec, 10);
|
---|
795 | sBin = Convert.ToString(iDec, 2);
|
---|
796 | while (sBin.Length < (arg_bIPv6 ? 16 : 8))
|
---|
797 | sBin = "0" + sBin;
|
---|
798 | return sBin + sSubnet;
|
---|
799 | }
|
---|
800 |
|
---|
801 | /// <summary>
|
---|
802 | /// Converts Binary IP-Part to Decimal
|
---|
803 | /// </summary>
|
---|
804 | /// <param name="arg_sBin">Binary IP-Part</param>
|
---|
805 | /// <returns>Decimal IP-Part</returns>
|
---|
806 | private string Bin2Dec(string arg_sBin) {
|
---|
807 | string sDec = "000", sSubnet = "";
|
---|
808 | arg_sBin = arg_sBin.Trim();
|
---|
809 | while (arg_sBin.Length < 8)
|
---|
810 | arg_sBin = "0" + arg_sBin;
|
---|
811 | if (arg_sBin.IndexOf("/") >= 0) {
|
---|
812 | sSubnet = arg_sBin.Substring(arg_sBin.IndexOf("/"));
|
---|
813 | arg_sBin = arg_sBin.Substring(0, arg_sBin.IndexOf("/"));
|
---|
814 | }
|
---|
815 | int iBin = Convert.ToInt32(arg_sBin, 2);
|
---|
816 | if (iBin > 255)
|
---|
817 | throw new Exception(string.Format("Can't convert Binary to Decimal IP-Address\nbin:{0} is greater than 255", iBin));
|
---|
818 | sDec = Convert.ToString(iBin, 10);
|
---|
819 | while (sDec.Length < 3)
|
---|
820 | sDec = "0" + sDec;
|
---|
821 | return sDec + sSubnet;
|
---|
822 | }
|
---|
823 |
|
---|
824 | /// <summary>
|
---|
825 | /// Converts Binary IP-Part to Hexadecimal
|
---|
826 | /// </summary>
|
---|
827 | /// <param name="arg_sBin">Binary IP-Part</param>
|
---|
828 | /// <returns>Hexadecimal IP-Part</returns>
|
---|
829 | private string Bin2Hex(string arg_sBin) {
|
---|
830 | string sHex = "0000", sSubnet = "";
|
---|
831 | arg_sBin = arg_sBin.Trim();
|
---|
832 | while (arg_sBin.Length < 8)
|
---|
833 | arg_sBin = "0" + arg_sBin;
|
---|
834 | if (arg_sBin.IndexOf("/") >= 0) {
|
---|
835 | sSubnet = arg_sBin.Substring(arg_sBin.IndexOf("/"));
|
---|
836 | arg_sBin = arg_sBin.Substring(0, arg_sBin.IndexOf("/"));
|
---|
837 | }
|
---|
838 | int iBin = Convert.ToInt32(arg_sBin, 2);
|
---|
839 | sHex = Convert.ToString(iBin, 16);
|
---|
840 | while (sHex.Length < 4)
|
---|
841 | sHex = "0" + sHex;
|
---|
842 | return sHex + sSubnet;
|
---|
843 | }
|
---|
844 |
|
---|
845 | /// <summary>
|
---|
846 | /// Converts Hexadecimal IP-Part to Binary (default IPv6=true)
|
---|
847 | /// </summary>
|
---|
848 | /// <param name="arg_sHex">Hexadecimal IP-Part</param>
|
---|
849 | /// <returns>Binary IP-Part</returns>
|
---|
850 | private string Hex2Bin(string arg_sHex) {
|
---|
851 | return this.Hex2Bin(arg_sHex, true);
|
---|
852 | }
|
---|
853 | /// <summary>
|
---|
854 | /// Converts Hexadecimal IP-Part to Binary
|
---|
855 | /// </summary>
|
---|
856 | /// <param name="arg_sHex">Hexadecimal IP-Part</param>
|
---|
857 | /// <param name="arg_bIPv6">Binary for IPv6 (16 digits)</param>
|
---|
858 | /// <returns>Binary IP-Part</returns>
|
---|
859 | private string Hex2Bin(string arg_sHex, bool arg_bIPv6) {
|
---|
860 | string sBin = (arg_bIPv6 ? "0000000000000000" : "00000000"), sSubnet = "";
|
---|
861 | arg_sHex = arg_sHex.Trim();
|
---|
862 | while (arg_sHex.Length < 3)
|
---|
863 | arg_sHex = "0" + arg_sHex;
|
---|
864 | if (arg_sHex.IndexOf("/") >= 0) {
|
---|
865 | sSubnet = arg_sHex.Substring(arg_sHex.IndexOf("/"));
|
---|
866 | arg_sHex = arg_sHex.Substring(0, arg_sHex.IndexOf("/"));
|
---|
867 | }
|
---|
868 | int iHex = Convert.ToInt32(arg_sHex, 16);
|
---|
869 | if (iHex > 255 && !arg_bIPv6)
|
---|
870 | throw new Exception(string.Format("Can't convert Hexadecimal to Binary IP-Address\nhex:{0} is greater than 11111111", iHex));
|
---|
871 | sBin = Convert.ToString(iHex, 2);
|
---|
872 | while (sBin.Length < (arg_bIPv6 ? 16 : 8))
|
---|
873 | sBin = "0" + sBin;
|
---|
874 | return sBin + sSubnet;
|
---|
875 | }
|
---|
876 |
|
---|
877 | /// <summary>
|
---|
878 | /// Converts Decimal IP-Part to Hexadecimal
|
---|
879 | /// </summary>
|
---|
880 | /// <param name="arg_sDec">Decimal IP-Part</param>
|
---|
881 | /// <returns>Hexadecimal IP-Part</returns>
|
---|
882 | private string Dec2Hex(string arg_sDec) {
|
---|
883 | string sHex = "0000", sSubnet = "";
|
---|
884 | arg_sDec = arg_sDec.Trim();
|
---|
885 | while (arg_sDec.Length < 8)
|
---|
886 | arg_sDec = "0" + arg_sDec;
|
---|
887 | if (arg_sDec.IndexOf("/") >= 0) {
|
---|
888 | sSubnet = arg_sDec.Substring(arg_sDec.IndexOf("/"));
|
---|
889 | arg_sDec = arg_sDec.Substring(0, arg_sDec.IndexOf("/"));
|
---|
890 | }
|
---|
891 | int iDec = Convert.ToInt32(arg_sDec, 10);
|
---|
892 | sHex = Convert.ToString(iDec, 16);
|
---|
893 | while (sHex.Length < 4)
|
---|
894 | sHex = "0" + sHex;
|
---|
895 | return sHex + sSubnet;
|
---|
896 | }
|
---|
897 |
|
---|
898 | /// <summary>
|
---|
899 | /// Converts Hexadecimal IP-Part to Decimal
|
---|
900 | /// </summary>
|
---|
901 | /// <param name="arg_sHex">Hexadecimal IP-Part</param>
|
---|
902 | /// <returns>Decimal IP-Part</returns>
|
---|
903 | private string Hex2Dec(string arg_sHex) {
|
---|
904 | string sDec = "000", sSubnet = "";
|
---|
905 | arg_sHex = arg_sHex.Trim();
|
---|
906 | while (arg_sHex.Length < 8)
|
---|
907 | arg_sHex = "0" + arg_sHex;
|
---|
908 | if (arg_sHex.IndexOf("/") >= 0) {
|
---|
909 | sSubnet = arg_sHex.Substring(arg_sHex.IndexOf("/"));
|
---|
910 | arg_sHex = arg_sHex.Substring(0, arg_sHex.IndexOf("/"));
|
---|
911 | }
|
---|
912 | int iHex = Convert.ToInt32(arg_sHex, 16);
|
---|
913 | if (iHex > 255)
|
---|
914 | throw new Exception(string.Format("Can't convert Hexadecimal to Decimal IP-Address\nhex:{0} is greater than 255", iHex));
|
---|
915 | sDec = Convert.ToString(iHex, 10);
|
---|
916 | while (sDec.Length < 3)
|
---|
917 | sDec = "0" + sDec;
|
---|
918 | return sDec + sSubnet;
|
---|
919 | }
|
---|
920 |
|
---|
921 | /// <summary>
|
---|
922 | /// Checks if IP in Textfield is valid
|
---|
923 | /// </summary>
|
---|
924 | /// <returns>true/false valid/not</returns>
|
---|
925 | private bool ValidateIP() {
|
---|
926 | if (IPAddressTextBox.ValidateIP(this.Text, this.m_newIPNotation, this.m_arlDelimeter))
|
---|
927 | return true;
|
---|
928 | else
|
---|
929 | //if Control is not visible or enabled, it doesn't matter if IP is valid
|
---|
930 | return this.Enabled || this.Visible ? false : true;
|
---|
931 | }
|
---|
932 |
|
---|
933 | /// <summary>
|
---|
934 | /// Checks if the given String is an valid ip-address
|
---|
935 | /// </summary>
|
---|
936 | /// <param name="arg_sIP">IP-String</param>
|
---|
937 | /// <param name="arg_ipNotation">IP-notation</param>
|
---|
938 | /// <param name="arg_arlDelimeter">Delimeter to parse IPString</param>
|
---|
939 | /// <returns>true/false validated/not</returns>
|
---|
940 | protected static bool ValidateIP(string arg_sIP, IPNotation arg_ipNotation, ArrayList arg_arlDelimeter) {
|
---|
941 | bool bValidated = false;
|
---|
942 | ArrayList arlIP = new ArrayList(arg_sIP.Split((char[])arg_arlDelimeter.ToArray(typeof(char))));
|
---|
943 |
|
---|
944 | try {
|
---|
945 | switch (arg_ipNotation) {
|
---|
946 | case IPNotation.IPv4Decimal:
|
---|
947 | case IPNotation.IPv4Binary:
|
---|
948 | bValidated = arlIP.Count == 4;
|
---|
949 | break;
|
---|
950 | case IPNotation.IPv4DecimalCIDR:
|
---|
951 | case IPNotation.IPv4BinaryCIDR:
|
---|
952 | bValidated = arlIP.Count == 5;
|
---|
953 | break;
|
---|
954 | default:
|
---|
955 | break;
|
---|
956 | }
|
---|
957 | if (!bValidated) {
|
---|
958 | throw new Exception("IP-Address has wrong element count");
|
---|
959 | }
|
---|
960 |
|
---|
961 | //don't check the 1st 2 elemnt if its IPv4 in IPv6-notation
|
---|
962 | for (int i = (arg_ipNotation.ToString().IndexOf("IPv6IPv4") == 0 ? 2 : 0);
|
---|
963 | //don't check the subnet element
|
---|
964 | i < (arg_ipNotation.ToString().IndexOf("CIDR") > 0 ? arlIP.Count - 1 : arlIP.Count);
|
---|
965 | i++) {
|
---|
966 | string sIPPart = arlIP[i].ToString().Replace(" ", "");
|
---|
967 | int iIPPart = 0;
|
---|
968 | switch (arg_ipNotation) {
|
---|
969 | case IPNotation.IPv4Decimal:
|
---|
970 | case IPNotation.IPv4DecimalCIDR:
|
---|
971 | while (sIPPart.Length < 3)
|
---|
972 | sIPPart = "0" + sIPPart;
|
---|
973 | iIPPart = Convert.ToInt32(sIPPart, 10);
|
---|
974 | if (iIPPart < 256)
|
---|
975 | bValidated = true;
|
---|
976 | else
|
---|
977 | bValidated = false;
|
---|
978 | break;
|
---|
979 | case IPNotation.IPv4Binary:
|
---|
980 | case IPNotation.IPv4BinaryCIDR:
|
---|
981 | while (sIPPart.Length < 8)
|
---|
982 | sIPPart = "0" + sIPPart;
|
---|
983 | iIPPart = Convert.ToInt32(sIPPart, 2);
|
---|
984 | if (iIPPart < 256)
|
---|
985 | bValidated = true;
|
---|
986 | else
|
---|
987 | bValidated = false;
|
---|
988 | break;
|
---|
989 | default:
|
---|
990 | break;
|
---|
991 | }
|
---|
992 | if (!bValidated) {
|
---|
993 | throw new Exception(string.Format("IP-Address element {0}({1}) has wrong format", i, sIPPart));
|
---|
994 | }
|
---|
995 | }
|
---|
996 | }
|
---|
997 | catch (Exception LastError) {
|
---|
998 | System.Diagnostics.Debug.WriteLine(LastError.Message);
|
---|
999 | bValidated = false;
|
---|
1000 | throw LastError;
|
---|
1001 | }
|
---|
1002 | return bValidated;
|
---|
1003 | }
|
---|
1004 |
|
---|
1005 | /// <summary>
|
---|
1006 | /// Adds Spaces to given IP-Address, so it fits in the textfield
|
---|
1007 | /// </summary>
|
---|
1008 | /// <param name="arg_sIP">IP-String</param>
|
---|
1009 | /// <param name="arg_ipNotation">IP-notation</param>
|
---|
1010 | /// <param name="arg_arlDelimeter">Delimeter to parse IPString</param>
|
---|
1011 | /// <returns>IP-Address with Spaces</returns>
|
---|
1012 | protected static string MakeValidSpaces(string arg_sIP, IPNotation arg_ipNotation, ArrayList arg_arlDelimeter) {
|
---|
1013 | ArrayList arlIP = new ArrayList(arg_sIP.Split((char[])arg_arlDelimeter.ToArray(typeof(char))));
|
---|
1014 | //don't check the 1st 2 elemnt if its IPv4 in IPv6-notation
|
---|
1015 | for (int i = (arg_ipNotation.ToString().IndexOf("IPv6IPv4") == 0 ? 2 : 0);
|
---|
1016 | //don't check the subnet element
|
---|
1017 | i < (arg_ipNotation.ToString().IndexOf("CIDR") > 0 ? arlIP.Count - 1 : arlIP.Count);
|
---|
1018 | i++) {
|
---|
1019 | switch (arg_ipNotation) {
|
---|
1020 | case IPNotation.IPv4Decimal:
|
---|
1021 | case IPNotation.IPv4DecimalCIDR:
|
---|
1022 | while (arlIP[i].ToString().Length < 3)
|
---|
1023 | arlIP[i] = arlIP[i].ToString() + " ";
|
---|
1024 | break;
|
---|
1025 | case IPNotation.IPv4Binary:
|
---|
1026 | case IPNotation.IPv4BinaryCIDR:
|
---|
1027 | while (arlIP[i].ToString().Length < 8)
|
---|
1028 | arlIP[i] = arlIP[i].ToString() + " ";
|
---|
1029 | break;
|
---|
1030 | default:
|
---|
1031 | break;
|
---|
1032 | }
|
---|
1033 | }
|
---|
1034 |
|
---|
1035 | return IPAddressTextBox.MakeIP((string[])arlIP.ToArray(typeof(string)), arg_ipNotation);
|
---|
1036 | }
|
---|
1037 |
|
---|
1038 | /// <summary>
|
---|
1039 | /// Adds Zeroes to given IP-Address, so it fits in the textfield
|
---|
1040 | /// </summary>
|
---|
1041 | /// <param name="arg_sIP">IP-String</param>
|
---|
1042 | /// <param name="arg_ipNotation">IP-notation</param>
|
---|
1043 | /// <param name="arg_arlDelimeter">Delimeter to parse IPString</param>
|
---|
1044 | /// <returns>IP-Address with Spaces</returns>
|
---|
1045 | protected static string MakeValidZeroes(string arg_sIP, IPNotation arg_ipNotation, ArrayList arg_arlDelimeter) {
|
---|
1046 | ArrayList arlIP = new ArrayList(arg_sIP.Split((char[])arg_arlDelimeter.ToArray(typeof(char))));
|
---|
1047 | //don't check the 1st 2 elemnt if its IPv4 in IPv6-notation
|
---|
1048 | for (int i = (arg_ipNotation.ToString().IndexOf("IPv6IPv4") == 0 ? 2 : 0);
|
---|
1049 | //don't check the subnet element
|
---|
1050 | i < (arg_ipNotation.ToString().IndexOf("CIDR") > 0 ? arlIP.Count - 1 : arlIP.Count);
|
---|
1051 | i++) {
|
---|
1052 | switch (arg_ipNotation) {
|
---|
1053 | case IPNotation.IPv4Decimal:
|
---|
1054 | case IPNotation.IPv4DecimalCIDR:
|
---|
1055 | while (arlIP[i].ToString().Length < 3)
|
---|
1056 | arlIP[i] = "0" + arlIP[i].ToString();
|
---|
1057 | break;
|
---|
1058 | case IPNotation.IPv4Binary:
|
---|
1059 | case IPNotation.IPv4BinaryCIDR:
|
---|
1060 | while (arlIP[i].ToString().Length < 8)
|
---|
1061 | arlIP[i] = "0" + arlIP[i].ToString();
|
---|
1062 | break;
|
---|
1063 | default:
|
---|
1064 | break;
|
---|
1065 | }
|
---|
1066 | }
|
---|
1067 |
|
---|
1068 | return IPAddressTextBox.MakeIP((string[])arlIP.ToArray(typeof(string)), arg_ipNotation);
|
---|
1069 | }
|
---|
1070 |
|
---|
1071 | /// <summary>
|
---|
1072 | /// Creates IP-Addresstring from given StrignArray and Notation
|
---|
1073 | /// </summary>
|
---|
1074 | /// <param name="arg_sIP">String-Array with elements for IP-Address</param>
|
---|
1075 | /// <param name="arg_ipNotation">Notation of IP-Address</param>
|
---|
1076 | /// <returns>IPAddress-String</returns>
|
---|
1077 | protected static string MakeIP(string[] arg_sIP, IPNotation arg_ipNotation) {
|
---|
1078 | string s = "";
|
---|
1079 | for (int i = 0; i < arg_sIP.Length; i++) {
|
---|
1080 | switch (arg_ipNotation) {
|
---|
1081 | case IPNotation.IPv4Decimal:
|
---|
1082 | case IPNotation.IPv4Binary:
|
---|
1083 | s += (arg_sIP[i].Length > 0 ? arg_sIP[i] : "0") + (i < (arg_sIP.Length - 1) ? "." : "");
|
---|
1084 | break;
|
---|
1085 | case IPNotation.IPv4DecimalCIDR:
|
---|
1086 | case IPNotation.IPv4BinaryCIDR:
|
---|
1087 | s += (arg_sIP[i].Length > 0 ? arg_sIP[i] : "0") + (i < (arg_sIP.Length - 2) ? "." : (i < arg_sIP.Length - 1) ? "/" : "");
|
---|
1088 | break;
|
---|
1089 | default:
|
---|
1090 | break;
|
---|
1091 | }
|
---|
1092 | }
|
---|
1093 | return s;
|
---|
1094 | }
|
---|
1095 |
|
---|
1096 | #endregion //Helperfunctions
|
---|
1097 | }
|
---|
1098 | }
|
---|