22using System . Collections . Generic ;
33using System . Net ;
44using System . Net . Sockets ;
5- using System . Threading . Tasks ;
6- using Advanced_PortChecker . Classes . Controls ;
5+ using Advanced_PortChecker . Classes . Objects ;
76
87namespace Advanced_PortChecker . Classes . Scanner
98{
109 /// <summary>
1110 /// Static class to determine whether a certain port is open or not
1211 /// </summary>
13- internal static class PortChecker
12+ internal static class PortScanner
1413 {
1514 // ReSharper disable once InconsistentNaming
1615 /// <summary>
@@ -19,26 +18,23 @@ internal static class PortChecker
1918 /// <param name="address">The IP address that needs to be scanned</param>
2019 /// <param name="startPort">The starting point of ports that needs to be scanned</param>
2120 /// <param name="stopPort">The final port in a range of ports that needs to be scanned</param>
22- /// <param name="timeout">The amount of time before the operation cancels </param>
23- /// <param name="oi ">The operation information regarding this scan</param>
24- /// <returns>A list of information regarding the ports and address that was scanned</returns>
21+ /// <param name="timeout">The amount of time before a connection times out </param>
22+ /// <param name="scanOperation ">The operation information regarding this scan</param>
23+ /// <returns>A list of LvCheck objects containing information regarding the ports and address that were scanned</returns>
2524 // ReSharper disable once IdentifierTypo
26- internal static async Task < List < LvCheck > > CheckTCPUDP ( string address , int startPort , int stopPort , int timeout , OperationInformation oi )
25+ internal static List < LvCheck > CheckTCPUDP ( string address , int startPort , int stopPort , int timeout , ScanOperation scanOperation )
2726 {
2827 List < LvCheck > lv = new List < LvCheck > ( ) ;
29- await Task . Run ( ( ) =>
28+ for ( int i = startPort ; i <= stopPort ; i ++ )
3029 {
31- for ( int i = startPort ; i <= stopPort ; i ++ )
32- {
33- if ( oi . IsCancelled ) return ;
34-
35- lv . AddRange ( CheckTCP ( address , i , i , timeout , oi , false ) . Result ) ;
36- lv . AddRange ( CheckUDP ( address , i , i , timeout , oi , false ) . Result ) ;
30+ if ( scanOperation . IsCancelled ) break ;
3731
38- oi . Progress . Report ( i ) ;
39- }
40- } ) ;
32+ lv . AddRange ( CheckTCP ( address , i , i , timeout , scanOperation , false ) ) ;
33+ lv . AddRange ( CheckUDP ( address , i , i , timeout , scanOperation , false ) ) ;
4134
35+ scanOperation . Progress . Report ( 1 ) ;
36+ }
37+ scanOperation . ScanCompletedEvent ? . Invoke ( ) ;
4238 return lv ;
4339 }
4440
@@ -49,37 +45,39 @@ await Task.Run(() =>
4945 /// <param name="address">The IP address that needs to be scanned</param>
5046 /// <param name="startPort">The starting point of ports that needs to be scanned</param>
5147 /// <param name="stopPort">The final port in a range of ports that needs to be scanned</param>
52- /// <param name="timeout">The amount of time before the operation cancels </param>
53- /// <param name="oi ">The operation information regarding this scan</param>
48+ /// <param name="timeout">The amount of time before the connection times out </param>
49+ /// <param name="scanOperation ">The operation information regarding this scan</param>
5450 /// <param name="reportProgress">A boolean to represent whether this method should report the current progress or not</param>
55- /// <returns>A list of information regarding the ports and address that was scanned</returns>
56- internal static async Task < List < LvCheck > > CheckTCP ( string address , int startPort , int stopPort , int timeout , OperationInformation oi , bool reportProgress )
51+ /// <returns>A list of LvCheck objects containing information regarding the ports and address that were scanned</returns>
52+ internal static List < LvCheck > CheckTCP ( string address , int startPort , int stopPort , int timeout , ScanOperation scanOperation , bool reportProgress )
5753 {
5854 List < LvCheck > lv = new List < LvCheck > ( ) ;
59- await Task . Run ( ( ) =>
55+ for ( int i = startPort ; i <= stopPort ; i ++ )
6056 {
61- for ( int i = startPort ; i <= stopPort ; i ++ )
57+ if ( scanOperation . IsCancelled ) break ;
58+
59+ LvCheck check = new LvCheck
6260 {
63- if ( oi . IsCancelled ) return ;
64-
65- // ReSharper disable once UseObjectOrCollectionInitializer
66- LvCheck check = new LvCheck
67- {
68- Address = address ,
69- Port = i ,
70- HostName = GetMachineNameFromIpAddress ( address ) ,
71- Type = "TCP" ,
72- Description = IsTcpOpen ( address , i , timeout ) ? "Open" : "Closed"
73- } ;
74- lv . Add ( check ) ;
75-
76- if ( reportProgress )
77- {
78- oi . Progress . Report ( i ) ;
79- }
80- oi . ItemProgress . Report ( check ) ;
61+ Address = address ,
62+ Port = i ,
63+ HostName = GetMachineNameFromIpAddress ( address ) ,
64+ Type = "TCP" ,
65+ Description = IsTcpOpen ( address , i , timeout ) ? "Open" : "Closed"
66+ } ;
67+ lv . Add ( check ) ;
68+
69+ if ( reportProgress )
70+ {
71+ scanOperation . Progress . Report ( 1 ) ;
8172 }
82- } ) ;
73+ scanOperation . ItemProgress . Report ( check ) ;
74+ }
75+
76+ if ( reportProgress )
77+ {
78+ scanOperation . ScanCompletedEvent ? . Invoke ( ) ;
79+ }
80+
8381 return lv ;
8482 }
8583
@@ -90,37 +88,38 @@ await Task.Run(() =>
9088 /// <param name="address">The IP address that needs to be scanned</param>
9189 /// <param name="startPort">The starting point of ports that needs to be scanned</param>
9290 /// <param name="stopPort">The final port in a range of ports that needs to be scanned</param>
93- /// <param name="timeout">The amount of time before the operation cancels </param>
94- /// <param name="oi ">The operation information regarding this scan</param>
91+ /// <param name="timeout">The amount of time before the connection times out </param>
92+ /// <param name="scanOperation ">The operation information regarding this scan</param>
9593 /// <param name="reportProgress">A boolean to represent whether this method should report the current progress or not</param>
96- /// <returns>A list of information regarding the ports and address that was scanned</returns>
97- internal static async Task < List < LvCheck > > CheckUDP ( string address , int startPort , int stopPort , int timeout , OperationInformation oi , bool reportProgress )
94+ /// <returns>A list of LvCheck objects containing information regarding the ports and address that were scanned</returns>
95+ internal static List < LvCheck > CheckUDP ( string address , int startPort , int stopPort , int timeout , ScanOperation scanOperation , bool reportProgress )
9896 {
9997 List < LvCheck > lv = new List < LvCheck > ( ) ;
100- await Task . Run ( ( ) =>
98+ for ( int i = startPort ; i <= stopPort ; i ++ )
10199 {
102- for ( int i = startPort ; i <= stopPort ; i ++ )
100+ if ( scanOperation . IsCancelled ) break ;
101+
102+ LvCheck check = new LvCheck
103+ {
104+ Address = address ,
105+ Port = i ,
106+ HostName = GetMachineNameFromIpAddress ( address ) ,
107+ Type = "UDP" ,
108+ Description = IsUdpOpen ( address , i , timeout ) ? "Open" : "Closed"
109+ } ;
110+ lv . Add ( check ) ;
111+
112+ if ( reportProgress )
103113 {
104- if ( oi . IsCancelled ) return ;
105-
106- // ReSharper disable once UseObjectOrCollectionInitializer
107- LvCheck check = new LvCheck
108- {
109- Address = address ,
110- Port = i ,
111- HostName = GetMachineNameFromIpAddress ( address ) ,
112- Type = "UDP" ,
113- Description = IsUdpOpen ( address , i , timeout ) ? "Open" : "Closed"
114- } ;
115- lv . Add ( check ) ;
116-
117- if ( reportProgress )
118- {
119- oi . Progress . Report ( i ) ;
120- }
121- oi . ItemProgress . Report ( check ) ;
114+ scanOperation . Progress . Report ( 1 ) ;
122115 }
123- } ) ;
116+ scanOperation . ItemProgress . Report ( check ) ;
117+ }
118+
119+ if ( reportProgress )
120+ {
121+ scanOperation . ScanCompletedEvent ? . Invoke ( ) ;
122+ }
124123 return lv ;
125124 }
126125
@@ -130,8 +129,8 @@ await Task.Run(() =>
130129 /// </summary>
131130 /// <param name="address">The IP address that needs to be scanned</param>
132131 /// <param name="port">The port that needs to be scanned</param>
133- /// <param name="timeout">The amount of time before the operation cancels </param>
134- /// <returns>Returns true if the port is open for connections </returns>
132+ /// <param name="timeout">The amount of time before the connection times out </param>
133+ /// <returns>Returns true if the port is open</returns>
135134 private static bool IsTcpOpen ( string address , int port , int timeout )
136135 {
137136 try
@@ -156,8 +155,8 @@ private static bool IsTcpOpen(string address, int port, int timeout)
156155 /// </summary>
157156 /// <param name="address">The IP address that needs to be scanned</param>
158157 /// <param name="port">The port that needs to be scanned</param>
159- /// <param name="timeout">The amount of time before the operation cancels </param>
160- /// <returns>Returns true if the port is open for connections </returns>
158+ /// <param name="timeout">The amount of time before the connection times out </param>
159+ /// <returns>Returns true if the port is open</returns>
161160 private static bool IsUdpOpen ( string address , int port , int timeout )
162161 {
163162 try
@@ -168,14 +167,21 @@ private static bool IsUdpOpen(string address, int port, int timeout)
168167 udpClient . Client . SendTimeout = timeout ;
169168
170169 udpClient . Connect ( address , port ) ;
171- return true ;
170+
171+ byte [ ] sendBytes = new byte [ 4 ] ;
172+ new Random ( ) . NextBytes ( sendBytes ) ;
173+ udpClient . Send ( sendBytes , sendBytes . Length ) ;
174+
175+ IPEndPoint remoteIpEndPoint = new IPEndPoint ( IPAddress . Any , 0 ) ;
176+
177+ byte [ ] result = udpClient . Receive ( ref remoteIpEndPoint ) ;
178+ return result . Length != 0 ;
172179 }
173180 }
174181 catch ( Exception )
175182 {
176- // ignored
183+ return false ;
177184 }
178- return false ;
179185 }
180186
181187 /// <summary>
0 commit comments