@@ -13,6 +13,17 @@ namespace Protest.Tools;
1313
1414internal static class Topology {
1515
16+ private static bool Push ( this Dictionary < int , List < string > > dic , int key , string value ) {
17+ if ( dic . TryGetValue ( key , out List < string > list ) ) {
18+ list . Add ( value ) ;
19+ }
20+ else {
21+ dic . Add ( key , new List < string > { value } ) ;
22+ }
23+
24+ return true ;
25+ }
26+
1627 private static async Task WsWriteText ( WebSocket ws , string data ) {
1728 if ( ws . State == WebSocketState . Open ) {
1829 await ws . SendAsync ( new ArraySegment < byte > ( Encoding . ASCII . GetBytes ( data ) , 0 , data . Length ) , WebSocketMessageType . Text , true , CancellationToken . None ) ;
@@ -96,7 +107,7 @@ internal static async void WebSocketHandler(HttpListenerContext ctx) {
96107 //Dictionary<string, string> remote = Polling.ParseResponse(rawRemote, true);
97108
98109 if ( rawLocal is null || rawLocal . Count == 0 || rawRemote is null || rawRemote . Count == 0 ) {
99- await WsWriteText ( ws , System . Text . Encoding . UTF8 . GetBytes ( $ "{{\" nosnmp \" :\" { candidates [ i ] . filename } \" }}") ) ;
110+ await WsWriteText ( ws , System . Text . Encoding . UTF8 . GetBytes ( $ "{{\" nolldp \" :\" { candidates [ i ] . filename } \" }}") ) ;
100111 }
101112 else {
102113 byte [ ] response = ComputeSnmpResponse ( candidates [ i ] . filename , rawLocal , rawRemote ) ;
@@ -133,17 +144,17 @@ private static byte[] ComputeSnmpResponse(string file, IList<Variable> rawLocal,
133144 local . TryGetValue ( "1.0.8802.1.1.2.1.3.1.0" , out Variable localChassisIdSuptype ) ;
134145 local . TryGetValue ( "1.0.8802.1.1.2.1.3.2.0" , out Variable localChassisId ) ;
135146 local . TryGetValue ( "1.0.8802.1.1.2.1.3.3.0" , out Variable localHostname ) ;
136- local . TryGetValue ( "1.0.8802.1.1.2.1.3.4.0" , out Variable localDescription ) ;
147+ // local.TryGetValue("1.0.8802.1.1.2.1.3.4.0", out Variable localDescription);
137148
138149 List < ( int , string ) > localPortIdSuptype = new List < ( int , string ) > ( ) ;
139150 List < ( int , string ) > localPortId = new List < ( int , string ) > ( ) ;
140151 List < ( int , string ) > localPortName = new List < ( int , string ) > ( ) ;
141152
142- List < ( int , string ) > remoteChassisIdSuptype = new List < ( int , string ) > ( ) ;
143- List < ( int , string ) > remoteChassisId = new List < ( int , string ) > ( ) ;
144- List < ( int , string ) > remotePortIdSuptype = new List < ( int , string ) > ( ) ;
145- List < ( int , string ) > remotePortId = new List < ( int , string ) > ( ) ;
146- List < ( int , string ) > remoteSystemName = new List < ( int , string ) > ( ) ;
153+ Dictionary < int , List < string > > remoteChassisIdSuptype = new Dictionary < int , List < string > > ( ) ;
154+ Dictionary < int , List < string > > remoteChassisId = new Dictionary < int , List < string > > ( ) ;
155+ Dictionary < int , List < string > > remotePortIdSuptype = new Dictionary < int , List < string > > ( ) ;
156+ Dictionary < int , List < string > > remotePortId = new Dictionary < int , List < string > > ( ) ;
157+ Dictionary < int , List < string > > remoteSystemName = new Dictionary < int , List < string > > ( ) ;
147158
148159 foreach ( KeyValuePair < string , Variable > pair in local ) {
149160 if ( ! int . TryParse ( pair . Key . Split ( '.' ) [ ^ 1 ] , out int index ) ) continue ;
@@ -152,7 +163,13 @@ private static byte[] ComputeSnmpResponse(string file, IList<Variable> rawLocal,
152163 localPortIdSuptype . Add ( ( index , pair . Value . Data . ToString ( ) ) ) ;
153164 }
154165 else if ( pair . Key . StartsWith ( "1.0.8802.1.1.2.1.3.7.1.3" ) ) {
155- localPortId . Add ( ( index , pair . Value . Data . ToString ( ) ) ) ;
166+ string typeString = pair . Key . Replace ( "1.0.8802.1.1.2.1.3.7.1.3" , "1.0.8802.1.1.2.1.3.7.1.2" ) ;
167+ if ( local . TryGetValue ( typeString , out Variable typeValue ) ) {
168+ localPortId . Add ( ( index , GetPortId ( typeValue . Data . ToString ( ) , pair . Value . Data ) ) ) ;
169+ }
170+ else {
171+ localPortId . Add ( ( index , pair . Value . Data . ToString ( ) ) ) ;
172+ }
156173 }
157174 else if ( pair . Key . StartsWith ( "1.0.8802.1.1.2.1.3.7.1.4" ) ) {
158175 localPortName . Add ( ( index , pair . Value . Data . ToString ( ) ) ) ;
@@ -163,29 +180,51 @@ private static byte[] ComputeSnmpResponse(string file, IList<Variable> rawLocal,
163180 if ( ! int . TryParse ( pair . Key . Split ( '.' ) [ ^ 2 ] , out int index ) ) continue ;
164181
165182 if ( pair . Key . StartsWith ( "1.0.8802.1.1.2.1.4.1.1.4" ) ) {
166- remoteChassisIdSuptype . Add ( ( index , pair . Value . Data . ToString ( ) ) ) ;
183+ remoteChassisIdSuptype . Push ( index - 1 , pair . Value . Data . ToString ( ) ) ;
167184 }
168185 else if ( pair . Key . StartsWith ( "1.0.8802.1.1.2.1.4.1.1.5" ) ) {
169- remoteChassisId . Add ( ( index , pair . Value . Data . ToString ( ) ) ) ;
186+ string typeString = pair . Key . Replace ( "1.0.8802.1.1.2.1.4.1.1.5" , "1.0.8802.1.1.2.1.4.1.1.4" ) ;
187+ if ( remote . TryGetValue ( typeString , out Variable typeValue ) ) {
188+ remoteChassisId . Push ( index - 1 , GetChassisId ( typeValue . Data . ToString ( ) , pair . Value . Data ) ) ;
189+ }
190+ else {
191+ remoteChassisId . Push ( index - 1 , pair . Value . Data . ToString ( ) ) ;
192+ }
170193 }
171194 if ( pair . Key . StartsWith ( "1.0.8802.1.1.2.1.4.1.1.6" ) ) {
172- remotePortIdSuptype . Add ( ( index , pair . Value . Data . ToString ( ) ) ) ;
195+ remotePortIdSuptype . Push ( index - 1 , pair . Value . Data . ToString ( ) ) ;
173196 }
174197 else if ( pair . Key . StartsWith ( "1.0.8802.1.1.2.1.4.1.1.7" ) ) {
175- remotePortId . Add ( ( index , pair . Value . Data . ToString ( ) ) ) ;
198+ string typeString = pair . Key . Replace ( "1.0.8802.1.1.2.1.4.1.1.7" , "1.0.8802.1.1.2.1.4.1.1.6" ) ;
199+ if ( remote . TryGetValue ( typeString , out Variable typeValue ) ) {
200+ remotePortId . Push ( index - 1 , GetPortId ( typeValue . Data . ToString ( ) , pair . Value . Data ) ) ;
201+ }
202+ else {
203+ remotePortId . Push ( index - 1 , pair . Value . Data . ToString ( ) ) ;
204+ }
176205 }
177206 else if ( pair . Key . StartsWith ( "1.0.8802.1.1.2.1.4.1.1.9" ) ) {
178- remoteSystemName . Add ( ( index , pair . Value . Data . ToString ( ) ) ) ;
207+ remoteSystemName . Push ( index - 1 , pair . Value . Data . ToString ( ) ) ;
179208 }
180209 }
181210
182211 byte [ ] payload = JsonSerializer . SerializeToUtf8Bytes ( new {
183- snmp = new {
184- file = file ,
212+ lldp = new {
213+ file = file ,
214+
185215 localChassisId = GetChassisId ( localChassisIdSuptype . Data . ToString ( ) , localChassisId . Data ) ,
216+ localHostname = localHostname . Data . ToString ( ) ,
217+ //localDescription = localDescription.Data.ToString(),
218+
186219 localPortIdSuptype = localPortIdSuptype . Select ( o=> o . Item2 ) ,
187220 localPortId = localPortId . Select ( o=> o . Item2 ) ,
188- localPortName = localPortName . Select ( o=> o . Item2 )
221+ localPortName = localPortName . Select ( o=> o . Item2 ) ,
222+
223+ remoteChassisIdSuptype = remoteChassisIdSuptype ,
224+ remoteChassisId = remoteChassisId ,
225+ remotePortIdSuptype = remotePortIdSuptype ,
226+ remotePortId = remotePortId ,
227+ remoteSystemName = remoteSystemName ,
189228 }
190229 } ) ;
191230
@@ -198,7 +237,7 @@ private static string GetChassisId(string subtype, ISnmpData value) {
198237 switch ( subtype ) {
199238 case "4" : //mac address
200239 if ( bytes . Length - 2 == 6 ) {
201- return $ "{ ( bytes [ 2 ] ) . ToString ( "X2 " ) } : { ( bytes [ 3 ] ) . ToString ( "X2 " ) } : { ( bytes [ 4 ] ) . ToString ( "X2 " ) } : { ( bytes [ 5 ] ) . ToString ( "X2 " ) } : { ( bytes [ 6 ] ) . ToString ( "X2 " ) } : { ( bytes [ 7 ] ) . ToString ( "X2 " ) } ";
240+ return $ "{ ( bytes [ 2 ] ) . ToString ( "x2 " ) } { ( bytes [ 3 ] ) . ToString ( "x2 " ) } { ( bytes [ 4 ] ) . ToString ( "x2 " ) } { ( bytes [ 5 ] ) . ToString ( "x2 " ) } { ( bytes [ 6 ] ) . ToString ( "x2 " ) } { ( bytes [ 7 ] ) . ToString ( "x2 " ) } ";
202241 }
203242 return value . ToString ( ) ;
204243
@@ -224,4 +263,37 @@ private static string GetChassisId(string subtype, ISnmpData value) {
224263 }
225264 }
226265
266+ private static string GetPortId ( string subtype , ISnmpData value ) {
267+ byte [ ] bytes = value . ToBytes ( ) ;
268+
269+ switch ( subtype ) {
270+ case "3" : // mac address
271+ if ( bytes . Length - 2 == 6 ) {
272+ return $ "{ ( bytes [ 2 ] ) . ToString ( "x2" ) } { ( bytes [ 3 ] ) . ToString ( "x2" ) } { ( bytes [ 4 ] ) . ToString ( "x2" ) } { ( bytes [ 5 ] ) . ToString ( "x2" ) } { ( bytes [ 6 ] ) . ToString ( "x2" ) } { ( bytes [ 7 ] ) . ToString ( "x2" ) } ";
273+ }
274+ return value . ToString ( ) ;
275+
276+ case "4" : //network address
277+ if ( bytes . Length - 2 == 4 ) {
278+ return $ "{ bytes [ 2 ] } .{ bytes [ 3 ] } .{ bytes [ 4 ] } .{ bytes [ 5 ] } ";
279+ }
280+ else if ( bytes . Length - 2 == 16 ) {
281+ return $ """
282+ { bytes [ 2 ] . ToString ( "x2" ) } ){ bytes [ 3 ] . ToString ( "x2" ) } :
283+ { bytes [ 4 ] . ToString ( "x2" ) } ){ bytes [ 5 ] . ToString ( "x2" ) } :
284+ { bytes [ 6 ] . ToString ( "x2" ) } ){ bytes [ 7 ] . ToString ( "x2" ) } :
285+ { bytes [ 8 ] . ToString ( "x2" ) } ){ bytes [ 9 ] . ToString ( "x2" ) } :
286+ { bytes [ 10 ] . ToString ( "x2" ) } ){ bytes [ 11 ] . ToString ( "x2" ) } :
287+ { bytes [ 12 ] . ToString ( "x2" ) } ){ bytes [ 13 ] . ToString ( "x2" ) } :
288+ { bytes [ 14 ] . ToString ( "x2" ) } ){ bytes [ 15 ] . ToString ( "x2" ) } :
289+ { bytes [ 16 ] . ToString ( "x2" ) } ){ bytes [ 17 ] . ToString ( "x2" ) }
290+ """ ;
291+ }
292+ return value . ToString ( ) ;
293+
294+ default :
295+ return value . ToString ( ) ;
296+ }
297+ }
298+
227299}
0 commit comments