/
examples_test.go
776 lines (675 loc) · 21.2 KB
/
examples_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
package halfpike
import (
"context"
"fmt"
"net"
"strconv"
"strings"
"time"
"github.com/kylelemons/godebug/pretty"
)
// showBGPNeighbor is the output we are going to lex/parse.
var showBGPNeighbor = `
Peer: 10.10.10.2+179 AS 22 Local: 10.10.10.1+65406 AS 17
Type: External State: Established Flags: <Sync>
Last State: OpenConfirm Last Event: RecvKeepAlive
Last Error: None
Options: <Preference PeerAS Refresh>
Holdtime: 90 Preference: 170
Number of flaps: 0
Peer ID: 10.10.10.2 Local ID: 10.10.10.1 Active Holdtime: 90
Keepalive Interval: 30 Peer index: 0
BFD: disabled, down
Local Interface: ge-1/2/0.0
NLRI for restart configured on peer: inet-unicast
NLRI advertised by peer: inet-unicast
NLRI for this session: inet-unicast
Peer supports Refresh capability (2)
Restart time configured on the peer: 120
Stale routes from peer are kept for: 300
Restart time requested by this peer: 120
NLRI that peer supports restart for: inet-unicast
NLRI that restart is negotiated for: inet-unicast
NLRI of received end-of-rib markers: inet-unicast
NLRI of all end-of-rib markers sent: inet-unicast
Peer supports 4 byte AS extension (peer-as 22)
Peer does not support Addpath
Table inet.0 Bit: 10000
RIB State: BGP restart is complete
Send state: in sync
Active prefixes: 0
Received prefixes: 0
Accepted prefixes: 0
Suppressed due to damping: 2
Advertised prefixes: 0
Last traffic (seconds): Received 10 Sent 6 Checked 1
Input messages: Total 8522 Updates 1 Refreshes 0 Octets 161922
Output messages: Total 8433 Updates 0 Refreshes 0 Octets 160290
Output Queue[0]: 0
Peer: 10.10.10.6+54781 AS 22 Local: 10.10.10.5+179 AS 17
Type: External State: Established Flags: <Sync>
Last State: OpenConfirm Last Event: RecvKeepAlive
Last Error: None
Options: <Preference PeerAS Refresh>
Holdtime: 90 Preference: 170
Number of flaps: 0
Peer ID: 10.10.10.6 Local ID: 10.10.10.1 Active Holdtime: 90
Keepalive Interval: 30 Peer index: 1
BFD: disabled, down
Local Interface: ge-0/0/1.5
NLRI for restart configured on peer: inet-unicast
NLRI advertised by peer: inet-unicast
NLRI for this session: inet-unicast
Peer supports Refresh capability (2)
Restart time configured on the peer: 120
Stale routes from peer are kept for: 300
Restart time requested by this peer: 120
NLRI that peer supports restart for: inet-unicast
NLRI that restart is negotiated for: inet-unicast
NLRI of received end-of-rib markers: inet-unicast
NLRI of all end-of-rib markers sent: inet-unicast
Peer supports 4 byte AS extension (peer-as 22)
Peer does not support Addpath
Table inet.0 Bit: 10000
RIB State: BGP restart is complete
Send state: in sync
Active prefixes: 0
Received prefixes: 0
Accepted prefixes: 0
Suppressed due to damping: 0
Advertised prefixes: 0
Last traffic (seconds): Received 12 Sent 6 Checked 33
Input messages: Total 8527 Updates 1 Refreshes 0 Octets 162057
Output messages: Total 8430 Updates 0 Refreshes 0 Octets 160233
Output Queue[0]: 0
`
func Example_long() {
// A slice of structs that has a .Validate() method on it.
// This is where our data will be stored.
neighbors := BGPNeighbors{}
// Parses our content in showBGPNeighbor and begins parsing with BGPNeighbors.Start().
if err := Parse(context.Background(), showBGPNeighbor, &neighbors); err != nil {
panic(err)
}
fmt.Println(pretty.Sprint(neighbors.Peers))
// Leaving off the output: line, because getting the output to line up after vsc reformats
// is just horrible.
/*
[{PeerIP: 10.10.10.2,
PeerPort: 179,
PeerAS: 22,
LocalIP: 10.10.10.1,
LocalPort: 65406,
LocalAS: 22,
Type: 1,
State: 3,
LastState: 5,
HoldTime: 90000000000,
Preference: 170,
PeerID: 10.10.10.2,
LocalID: 10.10.10.1,
InetStats: {0: {ID: 0,
Bit: 10000,
RIBState: 2,
SendState: 1,
ActivePrefixes: 0,
RecvPrefixes: 0,
AcceptPrefixes: 0,
SurpressedPrefixes: 2,
AdvertisedPrefixes: 0}}},
{PeerIP: 10.10.10.6,
PeerPort: 54781,
PeerAS: 22,
LocalIP: 10.10.10.5,
LocalPort: 179,
LocalAS: 22,
Type: 1,
State: 3,
LastState: 5,
HoldTime: 90000000000,
Preference: 170,
PeerID: 10.10.10.6,
LocalID: 10.10.10.1,
InetStats: {0: {ID: 0,
Bit: 10000,
RIBState: 2,
SendState: 1,
ActivePrefixes: 0,
RecvPrefixes: 0,
AcceptPrefixes: 0,
SurpressedPrefixes: 0,
AdvertisedPrefixes: 0}}}]
*/
}
// PeerType is the type of peer the neighbor is.
type PeerType uint8
// BGP neighbor types.
const (
// PTUnknown indicates the neighbor type is unknown.
PTUnknown PeerType = 0
// PTExternal indicates the neighbor is external to the router's AS.
PTExternal PeerType = 1
// PTInternal indicates the neighbort is intneral to the router's AS.
PTInternal PeerType = 2
)
type BGPState uint8
// BGP connection states.
const (
NSUnknown BGPState = 0
NSActive BGPState = 1
NSConnect BGPState = 2
NSEstablished BGPState = 3
NSIdle BGPState = 4
NSOpenConfirm BGPState = 5
NSOpenSent BGPState = 6
NSRRClient BGPState = 7
)
type RIBState uint8
const (
RSUnknown RIBState = 0
RSComplete RIBState = 2
RSInProgress RIBState = 3
)
type SendState uint8
const (
RSSendUnknown SendState = 0
RSSendSync SendState = 1
RSSendNotSync SendState = 2
RSSendNoAdvertise SendState = 3
)
// BGPNeighbors is a collection of BGPNeighbors for a router.
type BGPNeighbors struct {
Peers []*BGPNeighbor
parser *Parser
}
// Validate implements Validator.Validate().
func (b *BGPNeighbors) Validate() error {
for _, p := range b.Peers {
if err := p.Validate(); err != nil {
return err
}
}
return nil
}
// Start implementns ParseObject.Start(), which begins our parsing of content.
func (b *BGPNeighbors) Start(ctx context.Context, p *Parser) ParseFn {
b.parser = p
return b.findPeer
}
// peerRecStart is used to locate the beginnering of a BGP Peer record. We list out the
// relevant fields we are looking for and use the special "Skip" value to ignore what it is
// set to, as this is used to simply find the beginning fo the record.
var peerRecStart = []string{"Peer:", Skip, "AS", Skip, "Local:", Skip, "AS", Skip}
// Peer: 10.10.10.2+179 AS 22 Local: 10.10.10.1+65406 AS 17
func (b *BGPNeighbors) findPeer(ctx context.Context, p *Parser) ParseFn {
const (
peerIPPort = 1
peerASNum = 3
localIPPort = 5
localASNum = 7
)
rec := &BGPNeighbor{parent: b}
rec.init()
line, err := p.FindStart(peerRecStart)
if err != nil {
if len(b.Peers) == 0 {
p.Errorf("did not locate the start of our list of peers within the output")
}
return nil
}
if p.EOF(line) {
return p.Errorf("received the start of a Peer statement, but then an EOF: %#+v", line)
}
// Get peer's ip and port.
ip, port, err := ipPort(line.Items[peerIPPort].Val)
if err != nil {
return p.Errorf("coud not retrieve a valid peer IP and port from: %#+v", line)
}
rec.PeerIP = ip
rec.PeerPort = uint32(port)
// Get peer's AS.
as, err := line.Items[peerASNum].ToInt()
if err != nil {
return p.Errorf("could not retrieve the peer AS num from: %#+v", line)
}
rec.PeerAS = as
// Get local ip and port.
ip, port, err = ipPort(line.Items[localIPPort].Val)
if err != nil {
return p.Errorf("coud not retrieve a valid local IP and port from: %#+v", line)
}
rec.LocalIP = ip
rec.LocalPort = uint32(port)
// Get local AS.
as, err = line.Items[peerASNum].ToInt()
if err != nil {
return p.Errorf("could not retrieve the peer AS num from: %#+v", line)
}
rec.LocalAS = as
b.Peers = append(b.Peers, rec)
return b.typeState
}
// toPeerType converts a string representing the peer type to an enumerated value.
var toPeerType = map[string]PeerType{
"Internal": PTInternal,
"External": PTExternal,
}
// toState converts a string representing the a BGP state to an enumerated value.
var toState = map[string]BGPState{
"Active": NSActive,
"Connect": NSConnect,
"Established": NSEstablished,
"Idle": NSIdle,
"OpenConfirm": NSOpenConfirm,
"OpenSent": NSOpenSent,
"route reflector client": NSRRClient,
}
// Type: External State: Established Flags: <Sync>
func (b *BGPNeighbors) typeState(ctx context.Context, p *Parser) ParseFn {
const (
peerType = 1
state = 3
)
line := p.Next()
rec := b.lastPeer()
if !p.IsAtStart(line, []string{"Type:", Skip, "State:", Skip}) {
return b.errorf("did not have the expected 'Type' and 'State' declarations following peer line")
}
t, ok := toPeerType[line.Items[peerType].Val]
if !ok {
return b.errorf("Type was not 'Internal' or 'External', was %s", line.Items[peerType].Val)
}
rec.Type = t
s, ok := toState[line.Items[state].Val]
if !ok {
return b.errorf("BGP State was not one of the accepted types (Active, Connect, ...), was %s", line.Items[state].Val)
}
rec.State = s
return b.lastState
}
// Last State: OpenConfirm Last Event: RecvKeepAlive
func (b *BGPNeighbors) lastState(ctx context.Context, p *Parser) ParseFn {
line := p.Next()
rec := b.lastPeer()
if !p.IsAtStart(line, []string{"Last", "State:", Skip}) {
return b.errorf("did not have the expected 'Last State:', got %#+v", line)
}
s, ok := toState[line.Items[2].Val]
if !ok {
return b.errorf("BGP last state was not one of the accepted types (Active, Connect, ...), was %s", line.Items[2].Val)
}
rec.LastState = s
return b.holdTimePref
}
// Holdtime: 90 Preference: 170
func (b *BGPNeighbors) holdTimePref(ctx context.Context, p *Parser) ParseFn {
const (
hold = 1
pref = 3
)
rec := b.lastPeer()
line, until, err := p.FindUntil([]string{"Holdtime:", Skip, "Preference:", Skip}, peerRecStart)
if err != nil {
return b.errorf("reached end of file before finding Holdtime and Preference line")
}
if until {
return b.errorf("reached next entry before finding Holdtime and Preference line")
}
ht, err := line.Items[hold].ToInt()
if err != nil {
return b.errorf("Holdtime was not an integer, was %s", line.Items[hold].Val)
}
prefVal, err := line.Items[pref].ToInt()
if err != nil {
return b.errorf("Preference was not an integer, was %s", line.Items[pref].Val)
}
rec.HoldTime = time.Duration(ht) * time.Second
rec.Preference = prefVal
return b.peerIDLocalID
}
// Peer ID: 10.10.10.6 Local ID: 10.10.10.1 Active Holdtime: 90
func (b *BGPNeighbors) peerIDLocalID(ctx context.Context, p *Parser) ParseFn {
const (
peer = 2
local = 5
)
rec := b.lastPeer()
line, until, err := p.FindUntil([]string{"Peer", "ID:", Skip, "Local", "ID:", Skip}, peerRecStart)
if err != nil {
return b.errorf("reached end of file before finding PeerID and LocalID")
}
if until {
return b.errorf("reached next entry before finding PeerID and LocalID")
}
pid := net.ParseIP(line.Items[peer].Val)
if pid == nil {
return b.errorf("PeerID does not appear to be an IP: was %s", line.Items[peer].Val)
}
loc := net.ParseIP(line.Items[local].Val)
if loc == nil {
return b.errorf("LocalID does not appear to be an IP: was %s", line.Items[local].Val)
}
rec.PeerID = pid
rec.LocalID = loc
return b.findTableStats
}
// Table inet.0 Bit: 10000
func (b *BGPNeighbors) findTableStats(ctx context.Context, p *Parser) ParseFn {
_, until, err := p.FindUntil([]string{"Table", Skip, "Bit:", Skip}, peerRecStart)
if err != nil {
return nil
}
if until {
return b.findPeer
}
p.Backup()
// Run a sub statemachine for getting table stats.
ts := &tableStats{peer: b.lastPeer()}
for state := ts.start; state != nil; {
state = state(ctx, p)
}
return b.findTableStats
}
// lastPeer returns the last *BgPNeighbor added to our *BGPNeighbors slice.
func (b *BGPNeighbors) lastPeer() *BGPNeighbor {
if len(b.Peers) == 0 {
return nil
}
return b.Peers[len(b.Peers)-1]
}
func (b *BGPNeighbors) errorf(s string, a ...interface{}) ParseFn {
rec := b.lastPeer()
if rec == nil {
return b.parser.Errorf(s, a...)
}
return b.parser.Errorf("Peer(%s+%d):Local(%s+%d) entry: %s", rec.PeerIP, rec.PeerPort, rec.LocalIP, rec.LocalPort, fmt.Sprintf(s, a...))
}
// BGPNeighbor provides information about a router's BGP Neighbor.
type BGPNeighbor struct {
// PeerIP is the IP address of the neighbor.
PeerIP net.IP
// PeerPort is the IP port of the peer.
PeerPort uint32
// PeerAS is the peers autonomous system number.
PeerAS int
// LocalIP is the IP address on this router the neighbor connects to.
LocalIP net.IP
// LocaPort is the IP port on this router the neighbor connects to.
LocalPort uint32
// LocalAS is the local autonomous system number.
LocalAS int
// Type is the type of peer.
Type PeerType
// State is the current state of the BGP peer.
State BGPState
// LastState is the previous state of the BGP peer.
LastState BGPState
// HoldTime is how long to consider the neighbor valid after not hearing a keep alive.
HoldTime time.Duration
// Preference is the BGP preference value.
Preference int
// PeerID is the ID the peer uses to identify itself.
PeerID net.IP
// LocalID is the ID the local router uses to identify itself.
LocalID net.IP
InetStats map[int]*InetStats
initCalled bool
// parent gives access to the parent object's methods.
parent *BGPNeighbors
}
func (b *BGPNeighbor) init() {
b.Preference = -1
b.initCalled = true
b.LocalAS, b.PeerAS = -1, -1
}
// Vaildate implements Validator.Validate().
func (b *BGPNeighbor) Validate() error {
if !b.initCalled {
return fmt.Errorf("internal error: BGPNeighbor.init() was not called")
}
switch {
case b.PeerIP == nil:
return fmt.Errorf("PeerIP was nil")
case b.LocalIP == nil:
return fmt.Errorf("LocalIP was nil")
case b.PeerID == nil:
return fmt.Errorf("PeerID was nil")
case b.LocalID == nil:
return fmt.Errorf("LocalID was nil")
}
switch uint32(0) {
case b.PeerPort:
return fmt.Errorf("PeerPort was 0")
case b.LocalPort:
return fmt.Errorf("LocalPort was 0")
}
switch 0 {
case int(b.Type):
return fmt.Errorf("Type was not set")
case int(b.LastState):
return fmt.Errorf("LastState was not set")
case int(b.State):
return fmt.Errorf("State was not set")
}
switch -1 {
case b.Preference:
return fmt.Errorf("Preference was not set")
case b.LocalAS:
return fmt.Errorf("LocalAS was not set")
case b.PeerAS:
return fmt.Errorf("PeerAS was not set")
}
for _, v := range b.InetStats {
if err := v.Validate(); err != nil {
return err
}
}
return nil
}
// InetStats contains information about the route table.
type InetStats struct {
ID int
Bit int
RIBState RIBState
SendState SendState
ActivePrefixes int
RecvPrefixes int
AcceptPrefixes int
SurpressedPrefixes int
AdvertisedPrefixes int
}
func NewInetStats() *InetStats {
i := &InetStats{}
i.init()
return i
}
func (b *InetStats) init() {
b.Bit = -1
b.ActivePrefixes = -1
b.RecvPrefixes = -1
b.AcceptPrefixes = -1
b.SurpressedPrefixes = -1
}
// Validate implements Validator.
func (b *InetStats) Validate() error {
switch -1 {
case b.Bit:
return fmt.Errorf("InetStats: Bit was not parsed from the input")
case b.ActivePrefixes:
return fmt.Errorf("InetStats(Bit==%d): ActivePrefixes was not parsed from the input", b.Bit)
case b.AcceptPrefixes:
return fmt.Errorf("InetStats(Bit==%d): AcceptPrefixes was not parsed from the input", b.Bit)
case b.SurpressedPrefixes:
return fmt.Errorf("InetStats(Bit==%d): SurpressedPrefixes was not parsed from the input", b.Bit)
}
switch {
case b.RIBState == RSUnknown:
return fmt.Errorf("InetStats(Bit==%d): RIBState was unknown, which indicates the parser is broken on input", b.Bit)
case b.SendState == RSSendUnknown:
return fmt.Errorf("InetStats(Bit==%d): SendState was unknown, which indicates the parser is broken on input", b.Bit)
}
return nil
}
/*
Table inet.0 Bit: 10000
RIB State: BGP restart is complete
Send state: in sync
Active prefixes: 0
Received prefixes: 0
Accepted prefixes: 0
Suppressed due to damping: 0
Advertised prefixes: 0
*/
type tableStats struct {
peer *BGPNeighbor
stats *InetStats
}
func (t *tableStats) errorf(s string, a ...interface{}) ParseFn {
if t.stats == nil {
return t.peer.parent.errorf("Table(unknown): %s", fmt.Sprintf(s, a...))
}
return t.peer.parent.errorf("Table(ID: %d, Bit: %d): %s", t.stats.ID, t.stats.Bit, fmt.Sprintf(s, a...))
}
// Table inet.0 Bit: 10000
func (t *tableStats) start(ctx context.Context, p *Parser) ParseFn {
const (
table = 1
bit = 3
)
line := p.Next()
tvals := strings.Split(line.Items[table].Val, `.`)
if len(tvals) != 2 {
return t.errorf("had Table entry with table id that wasn't in a format I understand: %s", line.Items[table].Val)
}
i, err := strconv.Atoi(tvals[1])
if err != nil {
return t.errorf("had Table entry with table id that wasn't an integer: %s", tvals[1])
}
b, err := line.Items[bit].ToInt()
if err != nil {
return t.errorf("had Table entry with bits id that wasn't an integer: %s", line.Items[bit].Val)
}
is := NewInetStats()
is.ID = i
is.Bit = b
t.stats = is
return t.ribState
}
var toRIBState = map[string]RIBState{
"restart is complete": RSComplete,
"estart in progress": RSInProgress,
}
// RIB State: BGP restart is complete
func (t *tableStats) ribState(ctx context.Context, p *Parser) ParseFn {
const begin = 3
line := p.Next()
if !p.IsAtStart(line, []string{"RIB", "State:", "BGP", Skip}) {
return t.errorf("did not have the RIB State as expected")
}
s := ItemJoin(line, begin, -1)
v, ok := toRIBState[s]
if !ok {
return t.errorf("did not have a valid RIB State, had: %q", s)
}
t.stats.RIBState = v
return t.sendState
}
var toSendState = map[string]SendState{
"in sync": RSSendSync,
"not in sync": RSSendNotSync,
"not advertising": RSSendNoAdvertise,
}
// Send state: in sync
func (t *tableStats) sendState(ctx context.Context, p *Parser) ParseFn {
const begin = 2
line := p.Next()
if !p.IsAtStart(line, []string{"Send", "state:", Skip}) {
return t.errorf("did not have the Send state as expected")
}
s := ItemJoin(line, begin, -1)
v, ok := toSendState[s]
if !ok {
return t.errorf("did not have recognized Send state, had %s", s)
}
t.stats.SendState = v
return t.active
}
// Active prefixes: 0
func (t *tableStats) active(ctx context.Context, p *Parser) ParseFn {
i, err := t.intKeyVal([]string{"Active", "prefixes:", Skip}, p)
if err != nil {
return t.errorf(err.Error())
}
t.stats.ActivePrefixes = i
return t.received
}
// Received prefixes: 0
func (t *tableStats) received(ctx context.Context, p *Parser) ParseFn {
i, err := t.intKeyVal([]string{"Received", "prefixes:", Skip}, p)
if err != nil {
return t.errorf(err.Error())
}
t.stats.RecvPrefixes = i
return t.accepted
}
// Accepted prefixes: 0
func (t *tableStats) accepted(ctx context.Context, p *Parser) ParseFn {
i, err := t.intKeyVal([]string{"Accepted", "prefixes:", Skip}, p)
if err != nil {
return t.errorf(err.Error())
}
t.stats.AcceptPrefixes = i
return t.supressed
}
// Suppressed due to damping: 0
func (t *tableStats) supressed(ctx context.Context, p *Parser) ParseFn {
i, err := t.intKeyVal([]string{"Suppressed", "due", "to", "damping:", Skip}, p)
if err != nil {
return t.errorf(err.Error())
}
t.stats.SurpressedPrefixes = i
return t.advertised
}
// Advertised prefixes: 0
func (t *tableStats) advertised(ctx context.Context, p *Parser) ParseFn {
i, err := t.intKeyVal([]string{"Advertised", "prefixes:", Skip}, p)
if err != nil {
return t.errorf(err.Error())
}
t.stats.AdvertisedPrefixes = i
return t.recordStats
}
func (t *tableStats) recordStats(ctx context.Context, p *Parser) ParseFn {
if t.peer.InetStats == nil {
t.peer.InetStats = map[int]*InetStats{}
}
t.peer.InetStats[t.stats.ID] = t.stats
return nil
}
func (t *tableStats) intKeyVal(name []string, p *Parser) (int, error) {
line := p.Next()
if !p.IsAtStart(line, name) {
return 0, fmt.Errorf("did not have %s as expected", strings.Join(name, " "))
}
item := line.Items[len(name)-1]
v, err := item.ToInt()
if err != nil {
return 0, fmt.Errorf("did not have %s value as a int, had %v", strings.Join(name, " "), item.Val)
}
return v, nil
}
func ipPort(s string) (net.IP, int, error) {
sp := strings.Split(s, `+`)
if len(sp) != 2 {
return nil, 0, fmt.Errorf("IP address and port could not be found with syntax <ip>+<port>: %s", s)
}
ip := net.ParseIP(sp[0])
if ip == nil {
return nil, 0, fmt.Errorf("IP address could not be parsed: %s", sp[0])
}
port, err := strconv.Atoi(sp[1])
if err != nil {
return nil, 0, fmt.Errorf("IP port could not be parsed from: %s", sp[1])
}
return ip, port, nil
}