1 | #Region "Microsoft.VisualBasic::11909a7468e2003223b5585e0bbdbfa4, Microsoft.VisualBasic.Core\Net\Tcp\IPTools\AddressEnumerator.vb" |
2 | |
3 | ' Author: |
4 | ' |
5 | ' asuka (amethyst.asuka@gcmodeller.org) |
6 | ' xie (genetics@smrucc.org) |
7 | ' xieguigang (xie.guigang@live.com) |
8 | ' |
9 | ' Copyright (c) 2018 GPL3 Licensed |
10 | ' |
11 | ' |
12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
13 | ' |
14 | ' |
15 | ' This program is free software: you can redistribute it and/or modify |
16 | ' it under the terms of the GNU General Public License as published by |
17 | ' the Free Software Foundation, either version 3 of the License, or |
18 | ' (at your option) any later version. |
19 | ' |
20 | ' This program is distributed in the hope that it will be useful, |
21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | ' GNU General Public License for more details. |
24 | ' |
25 | ' You should have received a copy of the GNU General Public License |
26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
27 | |
28 | |
29 | |
30 | ' /********************************************************************************/ |
31 | |
32 | ' Summaries: |
33 | |
34 | ' Module AddressEnumerator |
35 | ' |
36 | ' Function: (+3 Overloads) EnumerateAddress, Enumerates |
37 | ' |
38 | ' |
39 | ' /********************************************************************************/ |
40 | |
41 | #End Region |
42 | |
43 | Imports System.Runtime.CompilerServices |
44 | Imports System.Text.RegularExpressions |
45 | |
46 | Namespace Net |
47 | |
48 | Public Module AddressEnumerator |
49 | |
50 | Public Iterator Function EnumerateAddress(a$, b$, c$) As IEnumerable(Of String) |
51 | For i As Integer = 0 To 255 |
52 | Yield $"{a}.{b}.{c}.{i}" |
53 | Next |
54 | End Function |
55 | |
56 | Public Iterator Function EnumerateAddress(a$, b$) As IEnumerable(Of String) |
57 | For i As Integer = 0 To 255 |
58 | For Each ip$ In EnumerateAddress(a, b, i) |
59 | Yield ip |
60 | Next |
61 | Next |
62 | End Function |
63 | |
64 | ''' <summary> |
65 | ''' |
66 | ''' </summary> |
67 | ''' <param name="expression$"> |
68 | ''' Example as: |
69 | ''' |
70 | ''' ``` |
71 | ''' 192.168.100.* |
72 | ''' 192.168.*.233 |
73 | ''' 192.1-120.*.* |
74 | ''' ``` |
75 | ''' </param> |
76 | ''' <returns></returns> |
77 | ''' |
78 | <Extension> |
79 | Public Iterator Function EnumerateAddress(expression$) As IEnumerable(Of String) |
80 | Dim t$() = expression.Split("."c) |
81 | |
82 | For Each a$ In Enumerates(t(0)) |
83 | For Each b$ In Enumerates(t(1)) |
84 | For Each c$ In Enumerates(t(2)) |
85 | For Each d$ In Enumerates(t(3)) |
86 | Yield $"{a}.{b}.{c}.{d}" |
87 | Next |
88 | Next |
89 | Next |
90 | Next |
91 | End Function |
92 | |
93 | Private Iterator Function Enumerates(expression$) As IEnumerable(Of String) |
94 | Dim a%, b% |
95 | |
96 | If expression = "*" Then |
97 | a = 0 |
98 | b = 255 |
99 | ElseIf Regex.Match(expression, "\d+-\d+").Value = expression Then |
100 | Dim t$() = expression.Split("-"c) |
101 | a = CInt(Val(t(Scan0))) |
102 | b = CInt(Val(t(1))) |
103 | Else |
104 | a = CInt(Val(expression)) |
105 | b = a |
106 | End If |
107 | |
108 | For i As Integer = a To b |
109 | Yield CStr(i) |
110 | Next |
111 | End Function |
112 | End Module |
113 | End Namespace |