...
1
2
3
4
5 package route
6
7 import (
8 "syscall"
9 )
10
11 func (m *RouteMessage) marshal() ([]byte, error) {
12 l := sizeofRtMsghdr + addrsSpace(m.Addrs)
13 b := make([]byte, l)
14 nativeEndian.PutUint16(b[:2], uint16(l))
15 if m.Version == 0 {
16 b[2] = syscall.RTM_VERSION
17 } else {
18 b[2] = byte(m.Version)
19 }
20 b[3] = byte(m.Type)
21 nativeEndian.PutUint16(b[4:6], uint16(sizeofRtMsghdr))
22 nativeEndian.PutUint32(b[16:20], uint32(m.Flags))
23 nativeEndian.PutUint16(b[6:8], uint16(m.Index))
24 nativeEndian.PutUint32(b[24:28], uint32(m.ID))
25 nativeEndian.PutUint32(b[28:32], uint32(m.Seq))
26 attrs, err := marshalAddrs(b[sizeofRtMsghdr:], m.Addrs)
27 if err != nil {
28 return nil, err
29 }
30 if attrs > 0 {
31 nativeEndian.PutUint32(b[12:16], uint32(attrs))
32 }
33 return b, nil
34 }
35
36 func (*wireFormat) parseRouteMessage(_ RIBType, b []byte) (Message, error) {
37 if len(b) < sizeofRtMsghdr {
38 return nil, errMessageTooShort
39 }
40 l := int(nativeEndian.Uint16(b[:2]))
41 if len(b) < l {
42 return nil, errInvalidMessage
43 }
44 m := &RouteMessage{
45 Version: int(b[2]),
46 Type: int(b[3]),
47 Flags: int(nativeEndian.Uint32(b[16:20])),
48 Index: int(nativeEndian.Uint16(b[6:8])),
49 ID: uintptr(nativeEndian.Uint32(b[24:28])),
50 Seq: int(nativeEndian.Uint32(b[28:32])),
51 raw: b[:l],
52 }
53 ll := int(nativeEndian.Uint16(b[4:6]))
54 if len(b) < ll {
55 return nil, errInvalidMessage
56 }
57 errno := syscall.Errno(nativeEndian.Uint32(b[32:36]))
58 if errno != 0 {
59 m.Err = errno
60 }
61 as, err := parseAddrs(uint(nativeEndian.Uint32(b[12:16])), parseKernelInetAddr, b[ll:])
62 if err != nil {
63 return nil, err
64 }
65 m.Addrs = as
66 return m, nil
67 }
68
View as plain text