88namespace Protest . Protocols . Snmp ;
99
1010internal static partial class Polling {
11- public static byte [ ] SwitchInterface ( HttpListenerContext ctx , Dictionary < string , string > parameters ) {
11+ public static byte [ ] SwitchInterface ( Dictionary < string , string > parameters ) {
1212 if ( parameters is null ) { return Data . CODE_INVALID_ARGUMENT . Array ; }
1313
1414 parameters . TryGetValue ( "file" , out string file ) ;
@@ -36,13 +36,16 @@ public static byte[] SwitchInterface(HttpListenerContext ctx, Dictionary<string,
3636 return Data . CODE_INVALID_ARGUMENT . Array ;
3737 }
3838
39+ return SwitchInterface ( _ipAddress , _snmpProfile ) ;
40+ }
41+
42+ public static byte [ ] SwitchInterface ( IPAddress target , SnmpProfiles . Profile snmpProfile ) {
3943 try {
40- IList < Variable > snmpResult = Polling . SnmpQuery ( _ipAddress , _snmpProfile , Oid . SWITCH_OID , Polling . SnmpOperation . Walk ) ;
44+ IList < Variable > snmpResult = Polling . SnmpQuery ( target , snmpProfile , Oid . SWITCH_OID , Polling . SnmpOperation . Walk ) ;
4145 Dictionary < string , string > interfaces = Polling . ParseResponse ( snmpResult ) ;
4246
4347 if ( interfaces is null ) return "{\" error\" :\" Failed to fetch interfaces\" }"u8 . ToArray ( ) ;
4448
45-
4649 Dictionary < int , string > descriptor = new Dictionary < int , string > ( ) ;
4750 Dictionary < int , string > alias = new Dictionary < int , string > ( ) ;
4851 Dictionary < int , string > type = new Dictionary < int , string > ( ) ;
@@ -69,15 +72,15 @@ public static byte[] SwitchInterface(HttpListenerContext ctx, Dictionary<string,
6972 for ( int j = startIndex ; j < maxIndex ; j ++ ) {
7073 byte b = raw [ j ] ;
7174 for ( int k = 0 ; k < 8 ; k ++ ) {
72- if ( ( b & ( 1 << ( 7 - k ) ) ) != 0 ) {
73- int portIndex = 8 * ( j - startIndex ) + ( k + 1 ) ;
74- if ( ! taggedMap . TryGetValue ( vlanId , out var ports ) ) {
75- ports = new List < int > ( ) ;
76- taggedMap [ vlanId ] = ports ;
77- }
78- if ( ! ports . Contains ( portIndex ) ) {
79- ports . Add ( portIndex ) ;
75+ if ( ( b & ( 1 << ( 7 - k ) ) ) == 0 ) continue ;
76+
77+ int portIndex = 8 * ( j - startIndex ) + ( k + 1 ) ;
78+ if ( ! taggedMap . TryGetValue ( vlanId , out var ports ) ) {
79+ ports = new List < int > ( ) ;
80+ taggedMap [ vlanId ] = ports ;
8081 }
82+ if ( ! ports . Contains ( portIndex ) ) {
83+ ports . Add ( portIndex ) ;
8184 }
8285 }
8386 }
@@ -124,20 +127,20 @@ public static byte[] SwitchInterface(HttpListenerContext ctx, Dictionary<string,
124127 }
125128
126129 return JsonSerializer . SerializeToUtf8Bytes (
127- type . Where ( o=> o . Value == "6" )
130+ type . Where ( o => o . Value == "6" )
128131 . Select ( pair => new {
129- number = descriptor . GetValueOrDefault ( pair . Key , null ) ,
130- port = speed . GetValueOrDefault ( pair . Key , "N/A" ) switch {
132+ number = descriptor . GetValueOrDefault ( pair . Key , null ) ,
133+ port = speed . GetValueOrDefault ( pair . Key , "N/A" ) switch {
131134 "10000" => "SFP+" ,
132135 "25000" => "SFP+" ,
133136 "40000" => "QSFP" ,
134137 "100000" => "QSFP" ,
135138 "200000" => "QSFP" ,
136139 "400000" => "QSFP" ,
137140 "800000" => "QSFP" ,
138- _ => "Ethernet"
141+ _ => "Ethernet"
139142 } ,
140- speed = speed . GetValueOrDefault ( pair . Key , "N/A" ) switch {
143+ speed = speed . GetValueOrDefault ( pair . Key , "N/A" ) switch {
141144 "10" => "10 Mbps" ,
142145 "100" => "100 Mbps" ,
143146 "1000" => "1 Gbps" ,
@@ -150,12 +153,12 @@ public static byte[] SwitchInterface(HttpListenerContext ctx, Dictionary<string,
150153 "200000" => "200 Gbps" ,
151154 "400000" => "400 Gbps" ,
152155 "800000" => "800 Gbps" ,
153- _ => "N/A"
156+ _ => "N/A"
154157 } ,
155158 untagged = untagged . GetValueOrDefault ( pair . Key , "" ) ,
156- tagged = tagged . GetValueOrDefault ( pair . Key , "" ) ,
157- comment = alias . GetValueOrDefault ( pair . Key , String . Empty ) ,
158- link = DatabaseInstances . FindDeviceByMac ( macTable . GetValueOrDefault ( pair . Key , null ) ) ,
159+ tagged = tagged . GetValueOrDefault ( pair . Key , "" ) ,
160+ comment = alias . GetValueOrDefault ( pair . Key , String . Empty ) ,
161+ link = DatabaseInstances . FindDeviceByMac ( macTable . GetValueOrDefault ( pair . Key , null ) ) ,
159162 } )
160163 ) ;
161164 }
0 commit comments