summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorIWASE Yusuke <iwase.yusuke0@gmail.com>2017-08-25 14:15:55 +0900
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2017-11-02 00:14:09 +0900
commit880c54ba096c91d12cebb4de4903a56e6a989088 (patch)
treef352f6f08f7baaea26df5643c7d9a3d1df9ea6a6
parent25ea69e4d3378ee90b142c1cb528e0d3d20629ca (diff)
zebra/zapi: Serialize messages based on API version
Because the Zebra message formats are different between API versions, this patch enables to detect API version when serialization. Signed-off-by: IWASE Yusuke <iwase.yusuke0@gmail.com>
-rw-r--r--zebra/zapi.go26
-rw-r--r--zebra/zapi_test.go12
2 files changed, 19 insertions, 19 deletions
diff --git a/zebra/zapi.go b/zebra/zapi.go
index 88c786c5..361b36ec 100644
--- a/zebra/zapi.go
+++ b/zebra/zapi.go
@@ -597,7 +597,7 @@ func (h *Header) DecodeFromBytes(data []byte) error {
type Body interface {
DecodeFromBytes([]byte, uint8) error
- Serialize() ([]byte, error)
+ Serialize(uint8) ([]byte, error)
String() string
}
@@ -610,7 +610,7 @@ func (b *UnknownBody) DecodeFromBytes(data []byte, version uint8) error {
return nil
}
-func (b *UnknownBody) Serialize() ([]byte, error) {
+func (b *UnknownBody) Serialize(version uint8) ([]byte, error) {
return b.Data, nil
}
@@ -627,7 +627,7 @@ func (b *HelloBody) DecodeFromBytes(data []byte, version uint8) error {
return nil
}
-func (b *HelloBody) Serialize() ([]byte, error) {
+func (b *HelloBody) Serialize(version uint8) ([]byte, error) {
return []byte{uint8(b.RedistDefault)}, nil
}
@@ -644,7 +644,7 @@ func (b *RedistributeBody) DecodeFromBytes(data []byte, version uint8) error {
return nil
}
-func (b *RedistributeBody) Serialize() ([]byte, error) {
+func (b *RedistributeBody) Serialize(version uint8) ([]byte, error) {
return []byte{uint8(b.Redist)}, nil
}
@@ -694,7 +694,7 @@ func (b *InterfaceUpdateBody) DecodeFromBytes(data []byte, version uint8) error
return nil
}
-func (b *InterfaceUpdateBody) Serialize() ([]byte, error) {
+func (b *InterfaceUpdateBody) Serialize(version uint8) ([]byte, error) {
return []byte{}, nil
}
@@ -733,7 +733,7 @@ func (b *InterfaceAddressUpdateBody) DecodeFromBytes(data []byte, version uint8)
return nil
}
-func (b *InterfaceAddressUpdateBody) Serialize() ([]byte, error) {
+func (b *InterfaceAddressUpdateBody) Serialize(version uint8) ([]byte, error) {
return []byte{}, nil
}
@@ -762,7 +762,7 @@ func (b *RouterIDUpdateBody) DecodeFromBytes(data []byte, version uint8) error {
return nil
}
-func (b *RouterIDUpdateBody) Serialize() ([]byte, error) {
+func (b *RouterIDUpdateBody) Serialize(version uint8) ([]byte, error) {
return []byte{}, nil
}
@@ -785,7 +785,7 @@ type IPRouteBody struct {
Api API_TYPE
}
-func (b *IPRouteBody) Serialize() ([]byte, error) {
+func (b *IPRouteBody) Serialize(version uint8) ([]byte, error) {
buf := make([]byte, 5)
buf[0] = uint8(b.Type)
buf[1] = uint8(b.Flags)
@@ -1048,7 +1048,7 @@ func decodeNexthopsFromBytes(nexthops *[]*Nexthop, data []byte, isV4 bool) (int,
return offset, nil
}
-func (b *NexthopLookupBody) Serialize() ([]byte, error) {
+func (b *NexthopLookupBody) Serialize(version uint8) ([]byte, error) {
isV4 := b.Api == IPV4_NEXTHOP_LOOKUP
buf := make([]byte, 0)
@@ -1116,7 +1116,7 @@ type ImportLookupBody struct {
Nexthops []*Nexthop
}
-func (b *ImportLookupBody) Serialize() ([]byte, error) {
+func (b *ImportLookupBody) Serialize(version uint8) ([]byte, error) {
buf := make([]byte, 1)
buf[0] = b.PrefixLength
buf = append(buf, b.Addr.To4()...)
@@ -1240,7 +1240,7 @@ type NexthopRegisterBody struct {
Nexthops []*RegisteredNexthop
}
-func (b *NexthopRegisterBody) Serialize() ([]byte, error) {
+func (b *NexthopRegisterBody) Serialize(version uint8) ([]byte, error) {
buf := make([]byte, 0)
// List of Registered Nexthops
@@ -1297,7 +1297,7 @@ type NexthopUpdateBody struct {
Nexthops []*Nexthop
}
-func (b *NexthopUpdateBody) Serialize() ([]byte, error) {
+func (b *NexthopUpdateBody) Serialize(version uint8) ([]byte, error) {
// Address Family (2 bytes)
buf := make([]byte, 3)
binary.BigEndian.PutUint16(buf, b.Family)
@@ -1372,7 +1372,7 @@ func (m *Message) Serialize() ([]byte, error) {
var body []byte
if m.Body != nil {
var err error
- body, err = m.Body.Serialize()
+ body, err = m.Body.Serialize(m.Header.Version)
if err != nil {
return nil, err
}
diff --git a/zebra/zapi_test.go b/zebra/zapi_test.go
index 57924812..733feb93 100644
--- a/zebra/zapi_test.go
+++ b/zebra/zapi_test.go
@@ -189,7 +189,7 @@ func Test_IPRouteBody_IPv4(t *testing.T) {
assert.Equal(uint32(1), r.Mtu)
//Serialize
- buf, err = r.Serialize()
+ buf, err = r.Serialize(2)
assert.Equal(nil, err)
assert.Equal([]byte{0x2, 0x10, 0x1d}, buf[0:3])
assert.Equal([]byte{0x0, 0x1}, buf[3:5])
@@ -275,7 +275,7 @@ func Test_IPRouteBody_IPv6(t *testing.T) {
assert.Equal(uint32(1), r.Mtu)
//Serialize
- buf, err = r.Serialize()
+ buf, err = r.Serialize(2)
assert.Equal(nil, err)
assert.Equal([]byte{0x2, 0x10, 0x1d}, buf[0:3])
assert.Equal([]byte{0x0, 0x1}, buf[3:5])
@@ -362,7 +362,7 @@ func Test_NexthopLookupBody(t *testing.T) {
assert.Equal("172.16.1.101", b.Nexthops[0].Addr.String())
//Serialize
- buf, err = b.Serialize()
+ buf, err = b.Serialize(2)
ip = net.ParseIP("192.168.50.0").To4()
assert.Equal(nil, err)
assert.Equal([]byte(ip)[0:4], buf[0:4])
@@ -401,7 +401,7 @@ func Test_NexthopLookupBody(t *testing.T) {
assert.Equal("2001:db8:0:1111::1", b.Nexthops[0].Addr.String())
//Serialize
- buf, err = b.Serialize()
+ buf, err = b.Serialize(2)
ip = net.ParseIP("2001:db8:0:f101::").To16()
assert.Equal(nil, err)
assert.Equal([]byte(ip)[0:16], buf[0:16])
@@ -444,7 +444,7 @@ func Test_ImportLookupBody(t *testing.T) {
//Serialize
b.PrefixLength = uint8(24)
- buf, err = b.Serialize()
+ buf, err = b.Serialize(2)
ip = net.ParseIP("192.168.50.0").To4()
assert.Equal(nil, err)
assert.Equal(uint8(24), buf[0])
@@ -487,7 +487,7 @@ func Test_NexthopRegisterBody(t *testing.T) {
assert.Equal(net.ParseIP("2001:db8:1:1::1").To16(), b.Nexthops[1].Prefix)
// Test Serialize()
- bufOut, err := b.Serialize()
+ bufOut, err := b.Serialize(3)
assert.Nil(err)
// Test serialised value