This commit is contained in:
sushen339
2025-11-18 17:12:31 +08:00
parent 0a4ec757f5
commit b8034ec3c9
2 changed files with 30 additions and 3 deletions
BIN
View File
Binary file not shown.
+30 -3
View File
@@ -182,11 +182,38 @@ void show_subnet_aggregation(void) {
} }
fclose(fp); fclose(fp);
/* 排序:先按count降序,再按mask升序 */ /* 排序:先按count降序,再按subnet前缀分组(层级显示),最后按mask升序 */
for (int i = 0; i < agg_count - 1; ++i) { for (int i = 0; i < agg_count - 1; ++i) {
for (int j = i + 1; j < agg_count; ++j) { for (int j = i + 1; j < agg_count; ++j) {
if (agg[j].count > agg[i].count || bool should_swap = false;
(agg[j].count == agg[i].count && agg[j].mask < agg[i].mask)) {
if (agg[j].count > agg[i].count) {
/* count大的排前面 */
should_swap = true;
} else if (agg[j].count == agg[i].count) {
/* count相同时,检查是否有包含关系 */
int prefix_match = 0;
const char *p1 = agg[i].subnet, *p2 = agg[j].subnet;
while (*p1 && *p2 && *p1 == *p2) {
prefix_match++;
p1++;
p2++;
}
if (prefix_match > 0 && (*p1 == '\0' || *p2 == '\0' || *p1 == '.' || *p2 == '.')) {
/* 有前缀匹配,mask小的(大段)排前面 */
if (agg[j].mask < agg[i].mask) {
should_swap = true;
}
} else {
/* 无关系,按字典序 */
if (strcmp(agg[j].subnet, agg[i].subnet) < 0) {
should_swap = true;
}
}
}
if (should_swap) {
char tmp_subnet[64]; char tmp_subnet[64];
int tmp_count = agg[i].count, tmp_mask = agg[i].mask; int tmp_count = agg[i].count, tmp_mask = agg[i].mask;
strcpy(tmp_subnet, agg[i].subnet); strcpy(tmp_subnet, agg[i].subnet);