Skip to content

Commit 9f2196a

Browse files
committed
Topology, optimize Dot1TpFdp handling
1 parent c969f60 commit 9f2196a

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

Protest/Tools/Topology.cs

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -344,38 +344,57 @@ private static byte[] ComputeDot1TpFdpResponse(string file, IList<Variable> dot1
344344
if (!pair.Key.StartsWith(Oid.INT_1D_TP_FDB)) continue;
345345
if (!int.TryParse(pair.Value, out int port)) continue;
346346

347-
string mac = String.Join(String.Empty, pair.Key.Split('.').TakeLast(6).Select(o=>int.Parse(o).ToString("x2")));
347+
string mac = ExtractMacFromOid(pair.Key);
348348

349-
if (macTable.ContainsKey(port)) {
350-
macTable[port].Add(mac);
351-
}
352-
else {
353-
macTable[port] = new List<string>() { mac };
349+
if (!macTable.TryGetValue(port, out List<string> list)) {
350+
list = new List<string>();
351+
macTable[port] = list;
354352
}
353+
list.Add(mac);
355354
}
356355

357356
Dictionary<int, string> dbEntry= new Dictionary<int, string>();
358357
foreach (KeyValuePair<int, List<string>> pair in macTable) {
358+
if (pair.Value.Count != 1) continue;
359+
359360
int port = pair.Key;
360361
List<string> list = pair.Value;
361362

362-
if (list.Count != 1) continue;
363-
364363
string match = GetDatabaseEntryByMac(list[0]);
365364
if (match is not null) {
366365
dbEntry[port] = match;
367366
}
368367
}
369368

370-
byte[] payload = JsonSerializer.SerializeToUtf8Bytes(new {
369+
return JsonSerializer.SerializeToUtf8Bytes(new {
371370
dot1tp = new {
372-
file = file,
371+
file,
373372
table = macTable,
374373
entry = dbEntry
375374
}
376375
});
376+
}
377377

378-
return payload;
378+
private static string ExtractMacFromOid(string oid) {
379+
Span<char> mac = stackalloc char[12];
380+
int end = oid.Length;
381+
382+
for (int part = 5; part >= 0; part--) {
383+
int dot = oid.LastIndexOf('.', end - 1);
384+
int start = dot + 1;
385+
386+
int value = int.Parse(oid.AsSpan(start, end - start));
387+
mac[part * 2] = ToHex((value >> 4) & 0xF);
388+
mac[part * 2 + 1] = ToHex(value & 0xF);
389+
390+
end = dot;
391+
}
392+
393+
return new string(mac);
394+
}
395+
396+
private static char ToHex(int value) {
397+
return (char)(value < 10 ? '0' + value : 'a' + (value - 10));
379398
}
380399

381400
private static byte[] ComputeDotQ1Response(string file, IList<Variable> dot1q) {

0 commit comments

Comments
 (0)