1
2
3
4
5
6
7
8 package route
9
10
11 type Message interface {
12
13 Sys() []Sys
14 }
15
16
17 type Sys interface {
18
19
20 SysType() SysType
21 }
22
23
24
25 type SysType int
26
27 const (
28 SysMetrics SysType = iota
29 SysStats
30 )
31
32
33
34 func ParseRIB(typ RIBType, b []byte) ([]Message, error) {
35 if !typ.parseable() {
36 return nil, errUnsupportedMessage
37 }
38 var msgs []Message
39 nmsgs, nskips := 0, 0
40 for len(b) > 4 {
41 nmsgs++
42 l := int(nativeEndian.Uint16(b[:2]))
43 if l == 0 {
44 return nil, errInvalidMessage
45 }
46 if len(b) < l {
47 return nil, errMessageTooShort
48 }
49 if b[2] != rtmVersion {
50 b = b[l:]
51 continue
52 }
53 if w, ok := wireFormats[int(b[3])]; !ok {
54 nskips++
55 } else {
56 m, err := w.parse(typ, b[:l])
57 if err != nil {
58 return nil, err
59 }
60 if m == nil {
61 nskips++
62 } else {
63 msgs = append(msgs, m)
64 }
65 }
66 b = b[l:]
67 }
68
69 if nmsgs != len(msgs)+nskips {
70 return nil, errMessageMismatch
71 }
72 return msgs, nil
73 }
74
View as plain text