11using System ;
2+ using System . Collections . Generic ;
23using System . Net . Sockets ;
4+ using System . Threading . Tasks ;
35
46namespace Advanced_PortChecker . Classes
57{
@@ -8,13 +10,116 @@ namespace Advanced_PortChecker.Classes
810 /// </summary>
911 internal static class PortChecker
1012 {
13+ // ReSharper disable once InconsistentNaming
14+ /// <summary>
15+ /// Determine which ports are open on a certain address using a TCP and UDP Client.
16+ /// </summary>
17+ /// <param name="address">The IP address that needs to be scanned.</param>
18+ /// <param name="startPort">The starting point of ports that needs to be scanned.</param>
19+ /// <param name="stopPort">The final port in a range of ports that needs to be scanned.</param>
20+ /// <param name="oi">The operation information regarding this scan.</param>
21+ /// <returns>A list of information regarding the ports and address that was scanned.</returns>
22+ internal static async Task < List < LvCheck > > CheckTCPUDP ( string address , int startPort , int stopPort , OperationInformation oi )
23+ {
24+ List < LvCheck > lv = new List < LvCheck > ( ) ;
25+ await Task . Run ( ( ) =>
26+ {
27+ for ( int i = startPort ; i <= stopPort ; i ++ )
28+ {
29+ if ( oi . IsCancelled ) return ;
30+
31+ lv . AddRange ( CheckTCP ( address , i , i , oi , false ) . Result ) ;
32+ lv . AddRange ( CheckUDP ( address , i , i , oi , false ) . Result ) ;
33+
34+ oi . Progress . Report ( i ) ;
35+ }
36+ } ) ;
37+
38+ return lv ;
39+ }
40+
41+ // ReSharper disable once InconsistentNaming
42+ /// <summary>
43+ /// Determine which ports are open on a certain address using a TCP Client.
44+ /// </summary>
45+ /// <param name="address">The IP address that needs to be scanned.</param>
46+ /// <param name="startPort">The starting point of ports that needs to be scanned.</param>
47+ /// <param name="stopPort">The final port in a range of ports that needs to be scanned.</param>
48+ /// <param name="oi">The operation information regarding this scan.</param>
49+ /// <param name="reportProgress">A boolean to represent whether this method should report the current progress or not.</param>
50+ /// <returns>A list of information regarding the ports and address that was scanned.</returns>
51+ internal static async Task < List < LvCheck > > CheckTCP ( string address , int startPort , int stopPort , OperationInformation oi , bool reportProgress )
52+ {
53+ List < LvCheck > lv = new List < LvCheck > ( ) ;
54+ await Task . Run ( ( ) =>
55+ {
56+ for ( int i = startPort ; i <= stopPort ; i ++ )
57+ {
58+ if ( oi . IsCancelled ) return ;
59+
60+ // ReSharper disable once UseObjectOrCollectionInitializer
61+ LvCheck check = new LvCheck ( ) ;
62+ check . Address = address ;
63+ check . Port = i ;
64+ check . Type = "TCP" ;
65+ check . Description = IsTcpOpen ( address , i ) ? "Open" : "Closed" ;
66+ lv . Add ( check ) ;
67+
68+ if ( reportProgress )
69+ {
70+ oi . Progress . Report ( i ) ;
71+ }
72+ oi . Preview . Report ( check ) ;
73+ }
74+ } ) ;
75+ return lv ;
76+ }
77+
78+ // ReSharper disable once InconsistentNaming
79+ /// <summary>
80+ /// Determine which ports are open on a certain address using an UDP Client.
81+ /// </summary>
82+ /// <param name="address">The IP address that needs to be scanned.</param>
83+ /// <param name="startPort">The starting point of ports that needs to be scanned.</param>
84+ /// <param name="stopPort">The final port in a range of ports that needs to be scanned.</param>
85+ /// <param name="oi">The operation information regarding this scan.</param>
86+ /// <param name="reportProgress">A boolean to represent whether this method should report the current progress or not.</param>
87+ /// <returns>A list of information regarding the ports and address that was scanned.</returns>
88+ internal static async Task < List < LvCheck > > CheckUDP ( string address , int startPort , int stopPort , OperationInformation oi , bool reportProgress )
89+ {
90+ List < LvCheck > lv = new List < LvCheck > ( ) ;
91+ await Task . Run ( ( ) =>
92+ {
93+ for ( int i = startPort ; i <= stopPort ; i ++ )
94+ {
95+ if ( oi . IsCancelled ) return ;
96+
97+ // ReSharper disable once UseObjectOrCollectionInitializer
98+ LvCheck check = new LvCheck ( ) ;
99+ check . Address = address ;
100+ check . Port = i ;
101+ check . Type = "UDP" ;
102+ check . Description = IsUdpOpen ( address , i ) ? "Open" : "Closed" ;
103+ lv . Add ( check ) ;
104+
105+ if ( reportProgress )
106+ {
107+ oi . Progress . Report ( i ) ;
108+ }
109+ oi . Preview . Report ( check ) ;
110+ }
111+ } ) ;
112+ return lv ;
113+ }
114+
115+
11116 /// <summary>
12117 /// Determine whether a certain port is open or not on a certain address using a TCP Client.
13118 /// </summary>
14119 /// <param name="address">The IP address that needs to be scanned.</param>
15120 /// <param name="port">The port that needs to be scanned.</param>
16121 /// <returns>Returns true if the port is open for connections.</returns>
17- internal static bool IsTcpOpen ( string address , int port )
122+ private static bool IsTcpOpen ( string address , int port )
18123 {
19124 try
20125 {
@@ -37,7 +142,7 @@ internal static bool IsTcpOpen(string address, int port)
37142 /// <param name="address">The IP address that needs to be scanned.</param>
38143 /// <param name="port">The port that needs to be scanned.</param>
39144 /// <returns>Returns true if the port is open for connections.</returns>
40- internal static bool IsUdpOpen ( string address , int port )
145+ private static bool IsUdpOpen ( string address , int port )
41146 {
42147 try
43148 {
0 commit comments