| 1 |
// Copyright 2016 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
//go:build darwin || dragonfly || freebsd || netbsd || openbsd |
| 6 |
|
| 7 |
package routebsd |
| 8 |
|
| 9 |
// A Message represents a routing message. |
| 10 |
type Message interface { |
| 11 |
message() |
| 12 |
} |
| 13 |
|
| 14 |
// parseRIB parses b as a routing information base and returns a list |
| 15 |
// of routing messages. |
| 16 |
func parseRIB(b []byte) ([]Message, error) { |
| 17 |
var msgs []Message |
| 18 |
nmsgs, nskips := 0, 0 |
| 19 |
for len(b) > 4 { |
| 20 |
nmsgs++ |
| 21 |
l := int(nativeEndian.Uint16(b[:2])) |
| 22 |
if l == 0 { |
| 23 |
return nil, errInvalidMessage |
| 24 |
} |
| 25 |
if len(b) < l { |
| 26 |
return nil, errMessageTooShort |
| 27 |
} |
| 28 |
if b[2] != rtmVersion { |
| 29 |
b = b[l:] |
| 30 |
continue |
| 31 |
} |
| 32 |
if w, ok := wireFormats[int(b[3])]; !ok { |
| 33 |
nskips++ |
| 34 |
} else { |
| 35 |
m, err := w.parse(b[:l]) |
| 36 |
if err != nil { |
| 37 |
return nil, err |
| 38 |
} |
| 39 |
if m == nil { |
| 40 |
nskips++ |
| 41 |
} else { |
| 42 |
msgs = append(msgs, m) |
| 43 |
} |
| 44 |
} |
| 45 |
b = b[l:] |
| 46 |
} |
| 47 |
|
| 48 |
// We failed to parse any of the messages - version mismatch? |
| 49 |
if nmsgs != len(msgs)+nskips { |
| 50 |
return nil, errMessageMismatch |
| 51 |
} |
| 52 |
return msgs, nil |
| 53 |
} |