Skip to content

Commit 71db171

Browse files
committed
fix: handle bird ospf unreachable field
1 parent bb6cff2 commit 71db171

File tree

10 files changed

+280
-227
lines changed

10 files changed

+280
-227
lines changed

backend/graph/parse/birdospf/BirdOSPF.g4

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,20 @@ routerEntry : ('router' IP
1919
| 'xrouter' IP )
2020
( Metric | Metric2 ) INT ( 'via' IP )?
2121
;
22-
distance : Distance INT;
22+
distance : Distance INT
23+
| Unreachable;
2324

2425
// Lexer Rules
25-
DR : 'dr';
26-
Distance: 'distance';
27-
Metric : 'metric';
28-
Metric2 : 'metric2';
29-
Prefix : IP '/' INT;
30-
VERSION : 'BIRD' WS+ INT '.' INT ('.' INT)? WS+ 'ready.' -> skip;
31-
IP : [0-9]+ '.' [0-9]+ '.' [0-9]+ '.' [0-9]+;
32-
STRING : '"' .*? '"'
33-
| [a-zA-Z][a-zA-Z0-9_-]*
34-
;
35-
INT : [0-9]+;
36-
WS : [ \t\r\n]+ -> skip;
26+
DR : 'dr';
27+
Distance : 'distance';
28+
Unreachable : 'unreachable';
29+
Metric : 'metric';
30+
Metric2 : 'metric2';
31+
Prefix : IP '/' INT;
32+
VERSION : 'BIRD' WS+ INT '.' INT ('.' INT)? WS+ 'ready.' -> skip;
33+
IP : [0-9]+ '.' [0-9]+ '.' [0-9]+ '.' [0-9]+;
34+
STRING : '"' .*? '"'
35+
| [a-zA-Z][a-zA-Z0-9_-]*
36+
;
37+
INT : [0-9]+;
38+
WS : [ \t\r\n]+ -> skip;

backend/graph/parse/birdospf/bird_ospf.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ func (p *BirdOSPF) ParseAndMerge(drawing *parse.Drawing) (err error) {
5454
return fmt.Errorf("parse as bird ospf state failed")
5555
}
5656
visitor.visitState(state)
57+
5758
drawing.Lock()
58-
defer drawing.Unlock()
5959
drawing.OSPF[p.asn] = visitor.graph
60+
drawing.Unlock()
6061

6162
if len(p.errL.errs) != 0 {
6263
err := fmt.Errorf("parse fail")
@@ -91,6 +92,9 @@ func (v *birdOSPFVisitor) visitArea(ctx *parser.AreaContext) {
9192
}
9293

9394
func (v *birdOSPFVisitor) visitRouter(ctx *parser.RouterContext, area *parse.Area) {
95+
if ctx.Distance().Unreachable() != nil {
96+
return
97+
}
9498
routerID := ctx.IP().GetText()
9599
area.AddRouter(routerID)
96100
router := area.GetRouter(routerID)
@@ -131,6 +135,9 @@ func (v *birdOSPFVisitor) visitRouterEntry(ctx *parser.RouterEntryContext, area
131135
}
132136

133137
func (v *birdOSPFVisitor) visitNetwork(ctx *parser.NetworkContext, area *parse.Area) {
138+
if ctx.Distance().Unreachable() != nil {
139+
return
140+
}
134141
c, err := strconv.ParseUint(ctx.Distance().INT().GetText(), 10, 64)
135142
if err != nil {
136143
fmt.Printf("invalid distance %v", err)

backend/graph/parse/birdospf/bird_ospf_test.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,6 @@ import (
99
"github.com/BaiMeow/NetworkMonitor/graph/parse"
1010
)
1111

12-
func TestBirdParser(t *testing.T) {
13-
var p BirdOSPF
14-
p.asn = 4242424242
15-
p.Init([]byte(birdOutput))
16-
var drawing parse.Drawing
17-
drawing.OSPF = make(map[uint32]*parse.OSPF)
18-
err := p.ParseAndMerge(&drawing)
19-
if err != nil {
20-
t.Error(err)
21-
return
22-
}
23-
t.Log(drawing.OSPF[4242424242])
24-
}
25-
26-
//go:embed testdata/birdospf.txt
27-
var birdOutput string
28-
2912
func TestMUSTParse(t *testing.T) {
3013
entries, err := os.ReadDir("testdata")
3114
if err != nil {

backend/graph/parse/birdospf/parser/BirdOSPF.interp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ null
1010
'via'
1111
'dr'
1212
'distance'
13+
'unreachable'
1314
'metric'
1415
'metric2'
1516
null
@@ -31,6 +32,7 @@ null
3132
null
3233
DR
3334
Distance
35+
Unreachable
3436
Metric
3537
Metric2
3638
Prefix
@@ -50,4 +52,4 @@ distance
5052

5153

5254
atn:
53-
[4, 1, 18, 73, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 2, 5, 7, 5, 1, 0, 4, 0, 14, 8, 0, 11, 0, 12, 0, 15, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 5, 1, 24, 8, 1, 10, 1, 12, 1, 27, 9, 1, 1, 2, 1, 2, 1, 2, 1, 2, 5, 2, 33, 8, 2, 10, 2, 12, 2, 36, 9, 2, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 5, 3, 45, 8, 3, 10, 3, 12, 3, 48, 9, 3, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 3, 4, 62, 8, 4, 1, 4, 1, 4, 1, 4, 1, 4, 3, 4, 68, 8, 4, 1, 5, 1, 5, 1, 5, 1, 5, 0, 0, 6, 0, 2, 4, 6, 8, 10, 0, 1, 1, 0, 11, 12, 77, 0, 13, 1, 0, 0, 0, 2, 19, 1, 0, 0, 0, 4, 28, 1, 0, 0, 0, 6, 37, 1, 0, 0, 0, 8, 61, 1, 0, 0, 0, 10, 69, 1, 0, 0, 0, 12, 14, 3, 2, 1, 0, 13, 12, 1, 0, 0, 0, 14, 15, 1, 0, 0, 0, 15, 13, 1, 0, 0, 0, 15, 16, 1, 0, 0, 0, 16, 17, 1, 0, 0, 0, 17, 18, 5, 0, 0, 1, 18, 1, 1, 0, 0, 0, 19, 20, 5, 1, 0, 0, 20, 25, 5, 15, 0, 0, 21, 24, 3, 4, 2, 0, 22, 24, 3, 6, 3, 0, 23, 21, 1, 0, 0, 0, 23, 22, 1, 0, 0, 0, 24, 27, 1, 0, 0, 0, 25, 23, 1, 0, 0, 0, 25, 26, 1, 0, 0, 0, 26, 3, 1, 0, 0, 0, 27, 25, 1, 0, 0, 0, 28, 29, 5, 2, 0, 0, 29, 30, 5, 15, 0, 0, 30, 34, 3, 10, 5, 0, 31, 33, 3, 8, 4, 0, 32, 31, 1, 0, 0, 0, 33, 36, 1, 0, 0, 0, 34, 32, 1, 0, 0, 0, 34, 35, 1, 0, 0, 0, 35, 5, 1, 0, 0, 0, 36, 34, 1, 0, 0, 0, 37, 38, 5, 3, 0, 0, 38, 39, 5, 13, 0, 0, 39, 40, 5, 9, 0, 0, 40, 41, 5, 15, 0, 0, 41, 46, 3, 10, 5, 0, 42, 43, 5, 2, 0, 0, 43, 45, 5, 15, 0, 0, 44, 42, 1, 0, 0, 0, 45, 48, 1, 0, 0, 0, 46, 44, 1, 0, 0, 0, 46, 47, 1, 0, 0, 0, 47, 7, 1, 0, 0, 0, 48, 46, 1, 0, 0, 0, 49, 50, 5, 2, 0, 0, 50, 62, 5, 15, 0, 0, 51, 52, 5, 4, 0, 0, 52, 62, 5, 13, 0, 0, 53, 54, 5, 5, 0, 0, 54, 62, 5, 13, 0, 0, 55, 56, 5, 3, 0, 0, 56, 62, 5, 13, 0, 0, 57, 58, 5, 6, 0, 0, 58, 62, 5, 13, 0, 0, 59, 60, 5, 7, 0, 0, 60, 62, 5, 15, 0, 0, 61, 49, 1, 0, 0, 0, 61, 51, 1, 0, 0, 0, 61, 53, 1, 0, 0, 0, 61, 55, 1, 0, 0, 0, 61, 57, 1, 0, 0, 0, 61, 59, 1, 0, 0, 0, 62, 63, 1, 0, 0, 0, 63, 64, 7, 0, 0, 0, 64, 67, 5, 17, 0, 0, 65, 66, 5, 8, 0, 0, 66, 68, 5, 15, 0, 0, 67, 65, 1, 0, 0, 0, 67, 68, 1, 0, 0, 0, 68, 9, 1, 0, 0, 0, 69, 70, 5, 10, 0, 0, 70, 71, 5, 17, 0, 0, 71, 11, 1, 0, 0, 0, 7, 15, 23, 25, 34, 46, 61, 67]
55+
[4, 1, 19, 75, 2, 0, 7, 0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 31, 33, 3, 8, 4, 0, 32, 31, 1, 0, 0, 0, 33, 36, 1, 0, 0, 0, 34, 32, 1, 0, 0, 0, 34, 35, 1, 0, 0, 0, 35, 5, 1, 0, 0, 0, 36, 34, 1, 0, 0, 0, 37, 38, 5, 3, 0, 0, 38, 39, 5, 14, 0, 0, 39, 40, 5, 9, 0, 0, 40, 41, 5, 16, 0, 0, 41, 46, 3, 10, 5, 0, 42, 43, 5, 2, 0, 0, 43, 45, 5, 16, 0, 0, 44, 42, 1, 0, 0, 0, 45, 48, 1, 0, 0, 0, 46, 44, 1, 0, 0, 0, 46, 47, 1, 0, 0, 0, 47, 7, 1, 0, 0, 0, 48, 46, 1, 0, 0, 0, 49, 50, 5, 2, 0, 0, 50, 62, 5, 16, 0, 0, 51, 52, 5, 4, 0, 0, 52, 62, 5, 14, 0, 0, 53, 54, 5, 5, 0, 0, 54, 62, 5, 14, 0, 0, 55, 56, 5, 3, 0, 0, 56, 62, 5, 14, 0, 0, 57, 58, 5, 6, 0, 0, 58, 62, 5, 14, 0, 0, 59, 60, 5, 7, 0, 0, 60, 62, 5, 16, 0, 0, 61, 49, 1, 0, 0, 0, 61, 51, 1, 0, 0, 0, 61, 53, 1, 0, 0, 0, 61, 55, 1, 0, 0, 0, 61, 57, 1, 0, 0, 0, 61, 59, 1, 0, 0, 0, 62, 63, 1, 0, 0, 0, 63, 64, 7, 0, 0, 0, 64, 67, 5, 18, 0, 0, 65, 66, 5, 8, 0, 0, 66, 68, 5, 16, 0, 0, 67, 65, 1, 0, 0, 0, 67, 68, 1, 0, 0, 0, 68, 9, 1, 0, 0, 0, 69, 70, 5, 10, 0, 0, 70, 73, 5, 18, 0, 0, 71, 73, 5, 11, 0, 0, 72, 69, 1, 0, 0, 0, 72, 71, 1, 0, 0, 0, 73, 11, 1, 0, 0, 0, 8, 15, 23, 25, 34, 46, 61, 67, 72]

backend/graph/parse/birdospf/parser/BirdOSPF.tokens

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ T__6=7
88
T__7=8
99
DR=9
1010
Distance=10
11-
Metric=11
12-
Metric2=12
13-
Prefix=13
14-
VERSION=14
15-
IP=15
16-
STRING=16
17-
INT=17
18-
WS=18
11+
Unreachable=11
12+
Metric=12
13+
Metric2=13
14+
Prefix=14
15+
VERSION=15
16+
IP=16
17+
STRING=17
18+
INT=18
19+
WS=19
1920
'area'=1
2021
'router'=2
2122
'network'=3
@@ -26,5 +27,6 @@ WS=18
2627
'via'=8
2728
'dr'=9
2829
'distance'=10
29-
'metric'=11
30-
'metric2'=12
30+
'unreachable'=11
31+
'metric'=12
32+
'metric2'=13

0 commit comments

Comments
 (0)