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