diff options
author | IWASE Yusuke <iwase.yusuke0@gmail.com> | 2017-08-28 13:41:08 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2017-09-06 08:14:14 +0900 |
commit | 06d3e1585e33992bdaf2150171854f3e4fc1ef0c (patch) | |
tree | b99f3a8ba806d1b39a2ee2965bafd6847aca9cc9 /zebra | |
parent | 208e3d5e87be225b464122da82023e99430067a5 (diff) |
zebra/zapi: Define type for Zebra API message flags
Signed-off-by: IWASE Yusuke <iwase.yusuke0@gmail.com>
Diffstat (limited to 'zebra')
-rw-r--r-- | zebra/zapi.go | 43 | ||||
-rw-r--r-- | zebra/zapi_test.go | 16 |
2 files changed, 43 insertions, 16 deletions
diff --git a/zebra/zapi.go b/zebra/zapi.go index 573f11cc..42243b21 100644 --- a/zebra/zapi.go +++ b/zebra/zapi.go @@ -214,14 +214,41 @@ func RouteTypeFromString(typ string) (ROUTE_TYPE, error) { return t, fmt.Errorf("unknown route type: %s", typ) } +// API Message Flags. +type MESSAGE_FLAG uint8 + const ( - MESSAGE_NEXTHOP = 0x01 - MESSAGE_IFINDEX = 0x02 - MESSAGE_DISTANCE = 0x04 - MESSAGE_METRIC = 0x08 - MESSAGE_MTU = 0x10 + MESSAGE_NEXTHOP MESSAGE_FLAG = 0x01 + MESSAGE_IFINDEX MESSAGE_FLAG = 0x02 + MESSAGE_DISTANCE MESSAGE_FLAG = 0x04 + MESSAGE_METRIC MESSAGE_FLAG = 0x08 + MESSAGE_MTU MESSAGE_FLAG = 0x10 + MESSAGE_TAG MESSAGE_FLAG = 0x20 ) +func (t MESSAGE_FLAG) String() string { + var ss []string + if t&MESSAGE_NEXTHOP > 0 { + ss = append(ss, "NEXTHOP") + } + if t&MESSAGE_IFINDEX > 0 { + ss = append(ss, "IFINDEX") + } + if t&MESSAGE_DISTANCE > 0 { + ss = append(ss, "DISTANCE") + } + if t&MESSAGE_METRIC > 0 { + ss = append(ss, "METRIC") + } + if t&MESSAGE_MTU > 0 { + ss = append(ss, "MTU") + } + if t&MESSAGE_TAG > 0 { + ss = append(ss, "TAG") + } + return strings.Join(ss, "|") +} + // Message Flags type FLAG uint64 @@ -716,7 +743,7 @@ func (b *RouterIDUpdateBody) String() string { type IPRouteBody struct { Type ROUTE_TYPE Flags FLAG - Message uint8 + Message MESSAGE_FLAG SAFI SAFI Prefix net.IP PrefixLength uint8 @@ -732,7 +759,7 @@ func (b *IPRouteBody) Serialize() ([]byte, error) { buf := make([]byte, 5) buf[0] = uint8(b.Type) buf[1] = uint8(b.Flags) - buf[2] = b.Message + buf[2] = uint8(b.Message) binary.BigEndian.PutUint16(buf[3:], uint16(b.SAFI)) bitlen := b.PrefixLength bytelen := (int(b.PrefixLength) + 7) / 8 @@ -797,7 +824,7 @@ func (b *IPRouteBody) DecodeFromBytes(data []byte, version uint8) error { b.Type = ROUTE_TYPE(data[0]) b.Flags = FLAG(data[1]) - b.Message = data[2] + b.Message = MESSAGE_FLAG(data[2]) b.PrefixLength = data[3] b.SAFI = SAFI(SAFI_UNICAST) diff --git a/zebra/zapi_test.go b/zebra/zapi_test.go index fe1ffc80..59d15ac3 100644 --- a/zebra/zapi_test.go +++ b/zebra/zapi_test.go @@ -161,7 +161,7 @@ func Test_IPRouteBody_IPv4(t *testing.T) { buf := make([]byte, 26) buf[0] = byte(ROUTE_CONNECT) buf[1] = byte(FLAG_SELECTED) - buf[2] = MESSAGE_NEXTHOP | MESSAGE_DISTANCE | MESSAGE_METRIC | MESSAGE_MTU + buf[2] = byte(MESSAGE_NEXTHOP | MESSAGE_DISTANCE | MESSAGE_METRIC | MESSAGE_MTU) buf[3] = 24 ip := net.ParseIP("192.168.100.0").To4() copy(buf[4:7], []byte(ip)) @@ -181,7 +181,7 @@ func Test_IPRouteBody_IPv4(t *testing.T) { assert.Equal(nil, err) assert.Equal("192.168.100.0", r.Prefix.String()) assert.Equal(uint8(0x18), r.PrefixLength) - assert.Equal(uint8(MESSAGE_NEXTHOP|MESSAGE_DISTANCE|MESSAGE_METRIC|MESSAGE_MTU), r.Message) + assert.Equal(MESSAGE_NEXTHOP|MESSAGE_DISTANCE|MESSAGE_METRIC|MESSAGE_MTU, r.Message) assert.Equal("0.0.0.0", r.Nexthops[0].String()) assert.Equal(uint32(1), r.Ifindexs[0]) assert.Equal(uint8(0), r.Distance) @@ -209,7 +209,7 @@ func Test_IPRouteBody_IPv4(t *testing.T) { buf = make([]byte, 18) buf[0] = byte(ROUTE_CONNECT) buf[1] = byte(FLAG_SELECTED) - buf[2] = MESSAGE_NEXTHOP | MESSAGE_DISTANCE | MESSAGE_METRIC + buf[2] = byte(MESSAGE_NEXTHOP | MESSAGE_DISTANCE | MESSAGE_METRIC) buf[3] = 24 ip = net.ParseIP("192.168.100.0").To4() copy(buf[4:7], []byte(ip)) @@ -227,7 +227,7 @@ func Test_IPRouteBody_IPv4(t *testing.T) { buf = make([]byte, 12) buf[0] = byte(ROUTE_CONNECT) buf[1] = byte(FLAG_SELECTED) - buf[2] = MESSAGE_DISTANCE | MESSAGE_METRIC + buf[2] = byte(MESSAGE_DISTANCE | MESSAGE_METRIC) buf[3] = 24 ip = net.ParseIP("192.168.100.0").To4() copy(buf[4:7], []byte(ip)) @@ -246,7 +246,7 @@ func Test_IPRouteBody_IPv6(t *testing.T) { buf := make([]byte, 43) buf[0] = byte(ROUTE_CONNECT) buf[1] = byte(FLAG_SELECTED) - buf[2] = MESSAGE_NEXTHOP | MESSAGE_DISTANCE | MESSAGE_METRIC | MESSAGE_MTU + buf[2] = byte(MESSAGE_NEXTHOP | MESSAGE_DISTANCE | MESSAGE_METRIC | MESSAGE_MTU) buf[3] = 64 ip := net.ParseIP("2001:db8:0:f101::").To16() copy(buf[4:12], []byte(ip)) @@ -267,7 +267,7 @@ func Test_IPRouteBody_IPv6(t *testing.T) { assert.Equal(nil, err) assert.Equal("2001:db8:0:f101::", r.Prefix.String()) assert.Equal(uint8(64), r.PrefixLength) - assert.Equal(uint8(MESSAGE_NEXTHOP|MESSAGE_DISTANCE|MESSAGE_METRIC|MESSAGE_MTU), r.Message) + assert.Equal(MESSAGE_NEXTHOP|MESSAGE_DISTANCE|MESSAGE_METRIC|MESSAGE_MTU, r.Message) assert.Equal("::", r.Nexthops[0].String()) assert.Equal(uint32(1), r.Ifindexs[0]) assert.Equal(uint8(0), r.Distance) @@ -302,7 +302,7 @@ func Test_IPRouteBody_IPv6(t *testing.T) { buf = make([]byte, 50) buf[0] = byte(ROUTE_CONNECT) buf[1] = byte(FLAG_SELECTED) - buf[2] = MESSAGE_NEXTHOP | MESSAGE_DISTANCE | MESSAGE_METRIC + buf[2] = byte(MESSAGE_NEXTHOP | MESSAGE_DISTANCE | MESSAGE_METRIC) buf[3] = 24 ip = net.ParseIP("2001:db8:0:f101::").To4() copy(buf[4:12], []byte(ip)) @@ -320,7 +320,7 @@ func Test_IPRouteBody_IPv6(t *testing.T) { buf = make([]byte, 11) buf[0] = byte(ROUTE_CONNECT) buf[1] = byte(FLAG_SELECTED) - buf[2] = MESSAGE_DISTANCE | MESSAGE_METRIC + buf[2] = byte(MESSAGE_DISTANCE | MESSAGE_METRIC) buf[3] = 16 ip = net.ParseIP("2501::").To16() copy(buf[4:6], []byte(ip)) |