diff options
author | ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp> | 2016-02-07 09:41:04 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2017-07-04 09:52:08 +0900 |
commit | 0ef496c3159f48df6018031c6079ce3a3b6043f1 (patch) | |
tree | 17616951a9898d404556aa663a4cd96e253300a2 | |
parent | 04ce5ce2956d4e77dd4782adce7ab889625e3f08 (diff) |
packet: add marshalling option infra
Signed-off-by: ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>
-rw-r--r-- | packet/bgp/bgp.go | 386 |
1 files changed, 194 insertions, 192 deletions
diff --git a/packet/bgp/bgp.go b/packet/bgp/bgp.go index a32ce069..bdab5da1 100644 --- a/packet/bgp/bgp.go +++ b/packet/bgp/bgp.go @@ -28,6 +28,8 @@ import ( "strings" ) +type MarshallingOption struct{} + const ( AFI_IP = 1 AFI_IP6 = 2 @@ -891,7 +893,7 @@ type BGPOpen struct { OptParams []OptionParameterInterface } -func (msg *BGPOpen) DecodeFromBytes(data []byte) error { +func (msg *BGPOpen) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { msg.Version = data[0] msg.MyAS = binary.BigEndian.Uint16(data[1:3]) msg.HoldTime = binary.BigEndian.Uint16(data[3:5]) @@ -932,7 +934,7 @@ func (msg *BGPOpen) DecodeFromBytes(data []byte) error { return nil } -func (msg *BGPOpen) Serialize() ([]byte, error) { +func (msg *BGPOpen) Serialize(options ...*MarshallingOption) ([]byte, error) { buf := make([]byte, 10) buf[0] = msg.Version binary.BigEndian.PutUint16(buf[1:3], msg.MyAS) @@ -959,11 +961,11 @@ func NewBGPOpenMessage(myas uint16, holdtime uint16, id string, optparams []Opti } type AddrPrefixInterface interface { - DecodeFromBytes([]byte) error - Serialize() ([]byte, error) + DecodeFromBytes([]byte, ...*MarshallingOption) error + Serialize(...*MarshallingOption) ([]byte, error) AFI() uint16 SAFI() uint8 - Len() int + Len(...*MarshallingOption) int String() string MarshalJSON() ([]byte, error) @@ -1007,7 +1009,7 @@ func (r *IPAddrPrefixDefault) serializePrefix(bitlen uint8) ([]byte, error) { return buf, nil } -func (r *IPAddrPrefixDefault) Len() int { +func (r *IPAddrPrefixDefault) Len(options ...*MarshallingOption) int { return 1 + ((int(r.Length) + 7) / 8) } @@ -1028,7 +1030,7 @@ type IPAddrPrefix struct { addrlen uint8 } -func (r *IPAddrPrefix) DecodeFromBytes(data []byte) error { +func (r *IPAddrPrefix) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { if len(data) < 1 { eCode := uint8(BGP_ERROR_UPDATE_MESSAGE_ERROR) eSubCode := uint8(BGP_ERROR_SUB_MALFORMED_ATTRIBUTE_LIST) @@ -1041,7 +1043,7 @@ func (r *IPAddrPrefix) DecodeFromBytes(data []byte) error { return r.decodePrefix(data[1:], r.Length, r.addrlen) } -func (r *IPAddrPrefix) Serialize() ([]byte, error) { +func (r *IPAddrPrefix) Serialize(options ...*MarshallingOption) ([]byte, error) { buf := make([]byte, 1) buf[0] = r.Length pbuf, err := r.serializePrefix(r.Length) @@ -1465,7 +1467,7 @@ type LabeledVPNIPAddrPrefix struct { addrlen uint8 } -func (l *LabeledVPNIPAddrPrefix) DecodeFromBytes(data []byte) error { +func (l *LabeledVPNIPAddrPrefix) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { l.Length = uint8(data[0]) data = data[1:] l.Labels.DecodeFromBytes(data) @@ -1480,7 +1482,7 @@ func (l *LabeledVPNIPAddrPrefix) DecodeFromBytes(data []byte) error { return nil } -func (l *LabeledVPNIPAddrPrefix) Serialize() ([]byte, error) { +func (l *LabeledVPNIPAddrPrefix) Serialize(options ...*MarshallingOption) ([]byte, error) { buf := make([]byte, 1) buf[0] = l.Length lbuf, err := l.Labels.Serialize() @@ -1582,7 +1584,7 @@ func (r *LabeledIPAddrPrefix) SAFI() uint8 { return SAFI_MPLS_LABEL } -func (l *LabeledIPAddrPrefix) DecodeFromBytes(data []byte) error { +func (l *LabeledIPAddrPrefix) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { l.Length = uint8(data[0]) data = data[1:] l.Labels.DecodeFromBytes(data) @@ -1595,7 +1597,7 @@ func (l *LabeledIPAddrPrefix) DecodeFromBytes(data []byte) error { return nil } -func (l *LabeledIPAddrPrefix) Serialize() ([]byte, error) { +func (l *LabeledIPAddrPrefix) Serialize(options ...*MarshallingOption) ([]byte, error) { buf := make([]byte, 1) buf[0] = l.Length restbits := int(l.Length) - 8*(l.Labels.Len()) @@ -1662,7 +1664,7 @@ type RouteTargetMembershipNLRI struct { RouteTarget ExtendedCommunityInterface } -func (n *RouteTargetMembershipNLRI) DecodeFromBytes(data []byte) error { +func (n *RouteTargetMembershipNLRI) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { n.Length = data[0] data = data[1:] if len(data) == 0 { @@ -1679,7 +1681,7 @@ func (n *RouteTargetMembershipNLRI) DecodeFromBytes(data []byte) error { return nil } -func (n *RouteTargetMembershipNLRI) Serialize() ([]byte, error) { +func (n *RouteTargetMembershipNLRI) Serialize(options ...*MarshallingOption) ([]byte, error) { if n.RouteTarget == nil { return []byte{0}, nil } @@ -1702,7 +1704,7 @@ func (n *RouteTargetMembershipNLRI) SAFI() uint8 { return SAFI_ROUTE_TARGET_CONSTRAINTS } -func (n *RouteTargetMembershipNLRI) Len() int { +func (n *RouteTargetMembershipNLRI) Len(options ...*MarshallingOption) int { if n.AS == 0 && n.RouteTarget == nil { return 1 } @@ -2314,7 +2316,7 @@ type EVPNNLRI struct { RouteTypeData EVPNRouteTypeInterface } -func (n *EVPNNLRI) DecodeFromBytes(data []byte) error { +func (n *EVPNNLRI) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { if len(data) < 2 { return NewMessageError(BGP_ERROR_UPDATE_MESSAGE_ERROR, BGP_ERROR_SUB_MALFORMED_ATTRIBUTE_LIST, nil, "Not all EVPNNLRI bytes available") } @@ -2332,7 +2334,7 @@ func (n *EVPNNLRI) DecodeFromBytes(data []byte) error { return n.RouteTypeData.DecodeFromBytes(data[:n.Length]) } -func (n *EVPNNLRI) Serialize() ([]byte, error) { +func (n *EVPNNLRI) Serialize(options ...*MarshallingOption) ([]byte, error) { buf := make([]byte, 2) buf[0] = n.RouteType tbuf, err := n.RouteTypeData.Serialize() @@ -2353,7 +2355,7 @@ func (n *EVPNNLRI) SAFI() uint8 { return SAFI_EVPN } -func (n *EVPNNLRI) Len() int { +func (n *EVPNNLRI) Len(options ...*MarshallingOption) int { return int(n.Length) + 2 } @@ -2391,7 +2393,7 @@ type EncapNLRI struct { addrlen uint8 } -func (n *EncapNLRI) DecodeFromBytes(data []byte) error { +func (n *EncapNLRI) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { if len(data) < 4 { eCode := uint8(BGP_ERROR_UPDATE_MESSAGE_ERROR) eSubCode := uint8(BGP_ERROR_SUB_MALFORMED_ATTRIBUTE_LIST) @@ -2404,7 +2406,7 @@ func (n *EncapNLRI) DecodeFromBytes(data []byte) error { return n.decodePrefix(data[1:], n.Length, n.addrlen) } -func (n *EncapNLRI) Serialize() ([]byte, error) { +func (n *EncapNLRI) Serialize(options ...*MarshallingOption) ([]byte, error) { buf := make([]byte, 1) buf[0] = n.Length pbuf, err := n.serializePrefix(n.Length) @@ -2969,9 +2971,9 @@ func (t BGPFlowSpecType) String() string { } type FlowSpecComponentInterface interface { - DecodeFromBytes([]byte) error - Serialize() ([]byte, error) - Len() int + DecodeFromBytes([]byte, ...*MarshallingOption) error + Serialize(...*MarshallingOption) ([]byte, error) + Len(...*MarshallingOption) int Type() BGPFlowSpecType String() string } @@ -2981,22 +2983,22 @@ type flowSpecPrefix struct { type_ BGPFlowSpecType } -func (p *flowSpecPrefix) DecodeFromBytes(data []byte) error { +func (p *flowSpecPrefix) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { p.type_ = BGPFlowSpecType(data[0]) - return p.Prefix.DecodeFromBytes(data[1:]) + return p.Prefix.DecodeFromBytes(data[1:], options...) } -func (p *flowSpecPrefix) Serialize() ([]byte, error) { +func (p *flowSpecPrefix) Serialize(options ...*MarshallingOption) ([]byte, error) { buf := []byte{byte(p.Type())} - bbuf, err := p.Prefix.Serialize() + bbuf, err := p.Prefix.Serialize(options...) if err != nil { return nil, err } return append(buf, bbuf...), nil } -func (p *flowSpecPrefix) Len() int { - buf, _ := p.Serialize() +func (p *flowSpecPrefix) Len(options ...*MarshallingOption) int { + buf, _ := p.Serialize(options...) return len(buf) } @@ -3026,16 +3028,16 @@ type flowSpecPrefix6 struct { // draft-ietf-idr-flow-spec-v6-06 // <type (1 octet), prefix length (1 octet), prefix offset(1 octet), prefix> -func (p *flowSpecPrefix6) DecodeFromBytes(data []byte) error { +func (p *flowSpecPrefix6) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { p.type_ = BGPFlowSpecType(data[0]) p.Offset = data[2] prefix := append([]byte{data[1]}, data[3:]...) - return p.Prefix.DecodeFromBytes(prefix) + return p.Prefix.DecodeFromBytes(prefix, options...) } -func (p *flowSpecPrefix6) Serialize() ([]byte, error) { +func (p *flowSpecPrefix6) Serialize(options ...*MarshallingOption) ([]byte, error) { buf := []byte{byte(p.Type())} - bbuf, err := p.Prefix.Serialize() + bbuf, err := p.Prefix.Serialize(options...) if err != nil { return nil, err } @@ -3044,8 +3046,8 @@ func (p *flowSpecPrefix6) Serialize() ([]byte, error) { return append(buf, bbuf[1:]...), nil } -func (p *flowSpecPrefix6) Len() int { - buf, _ := p.Serialize() +func (p *flowSpecPrefix6) Len(options ...*MarshallingOption) int { + buf, _ := p.Serialize(options...) return len(buf) } @@ -3106,7 +3108,7 @@ type flowSpecMac struct { type_ BGPFlowSpecType } -func (p *flowSpecMac) DecodeFromBytes(data []byte) error { +func (p *flowSpecMac) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { if len(data) < 2 || len(data) < 2+int(data[1]) { return NewMessageError(BGP_ERROR_UPDATE_MESSAGE_ERROR, BGP_ERROR_SUB_MALFORMED_ATTRIBUTE_LIST, nil, "not all mac bits available") } @@ -3115,7 +3117,7 @@ func (p *flowSpecMac) DecodeFromBytes(data []byte) error { return nil } -func (p *flowSpecMac) Serialize() ([]byte, error) { +func (p *flowSpecMac) Serialize(options ...*MarshallingOption) ([]byte, error) { if len(p.Mac) == 0 { return nil, fmt.Errorf("mac unset") } @@ -3123,7 +3125,7 @@ func (p *flowSpecMac) Serialize() ([]byte, error) { return append(buf, []byte(p.Mac)...), nil } -func (p *flowSpecMac) Len() int { +func (p *flowSpecMac) Len(options ...*MarshallingOption) int { return 2 + len(p.Mac) } @@ -3224,7 +3226,7 @@ type FlowSpecComponent struct { type_ BGPFlowSpecType } -func (p *FlowSpecComponent) DecodeFromBytes(data []byte) error { +func (p *FlowSpecComponent) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { p.type_ = BGPFlowSpecType(data[0]) data = data[1:] p.Items = make([]*FlowSpecComponentItem, 0) @@ -3248,7 +3250,7 @@ func (p *FlowSpecComponent) DecodeFromBytes(data []byte) error { return nil } -func (p *FlowSpecComponent) Serialize() ([]byte, error) { +func (p *FlowSpecComponent) Serialize(options ...*MarshallingOption) ([]byte, error) { buf := []byte{byte(p.Type())} for i, v := range p.Items { //set end-of-list bit @@ -3266,7 +3268,7 @@ func (p *FlowSpecComponent) Serialize() ([]byte, error) { return buf, nil } -func (p *FlowSpecComponent) Len() int { +func (p *FlowSpecComponent) Len(options ...*MarshallingOption) int { l := 1 for _, item := range p.Items { l += (item.Len() + 1) @@ -3429,16 +3431,16 @@ type FlowSpecUnknown struct { Value []byte } -func (p *FlowSpecUnknown) DecodeFromBytes(data []byte) error { +func (p *FlowSpecUnknown) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { p.Value = data return nil } -func (p *FlowSpecUnknown) Serialize() ([]byte, error) { +func (p *FlowSpecUnknown) Serialize(options ...*MarshallingOption) ([]byte, error) { return p.Value, nil } -func (p *FlowSpecUnknown) Len() int { +func (p *FlowSpecUnknown) Len(options ...*MarshallingOption) int { return len(p.Value) } @@ -3483,7 +3485,7 @@ func (n *FlowSpecNLRI) RD() RouteDistinguisherInterface { return n.rd } -func (n *FlowSpecNLRI) decodeFromBytes(rf RouteFamily, data []byte) error { +func (n *FlowSpecNLRI) decodeFromBytes(rf RouteFamily, data []byte, options ...*MarshallingOption) error { var length int if (data[0]>>4) == 0xf && len(data) > 2 { length = int(binary.BigEndian.Uint16(data[0:2])) @@ -3555,19 +3557,19 @@ func (n *FlowSpecNLRI) decodeFromBytes(rf RouteFamily, data []byte) error { i = &FlowSpecUnknown{} } - err := i.DecodeFromBytes(data) + err := i.DecodeFromBytes(data, options...) if err != nil { i = &FlowSpecUnknown{data} } - l -= i.Len() - data = data[i.Len():] + l -= i.Len(options...) + data = data[i.Len(options...):] n.Value = append(n.Value, i) } return nil } -func (n *FlowSpecNLRI) Serialize() ([]byte, error) { +func (n *FlowSpecNLRI) Serialize(options ...*MarshallingOption) ([]byte, error) { buf := make([]byte, 0, 32) if n.SAFI() == SAFI_FLOW_SPEC_VPN { if n.rd == nil { @@ -3580,13 +3582,13 @@ func (n *FlowSpecNLRI) Serialize() ([]byte, error) { buf = append(buf, b...) } for _, v := range n.Value { - b, err := v.Serialize() + b, err := v.Serialize(options...) if err != nil { return nil, err } buf = append(buf, b...) } - length := n.Len() + length := n.Len(options...) if length > 0xfff { return nil, fmt.Errorf("Too large: %d", length) } else if length < 0xf0 { @@ -3602,13 +3604,13 @@ func (n *FlowSpecNLRI) Serialize() ([]byte, error) { return buf, nil } -func (n *FlowSpecNLRI) Len() int { +func (n *FlowSpecNLRI) Len(options ...*MarshallingOption) int { l := 0 if n.SAFI() == SAFI_FLOW_SPEC_VPN { l += n.RD().Len() } for _, v := range n.Value { - l += v.Len() + l += v.Len(options...) } if l < 0xf0 { return l + 1 @@ -3771,8 +3773,8 @@ type FlowSpecIPv4Unicast struct { FlowSpecNLRI } -func (n *FlowSpecIPv4Unicast) DecodeFromBytes(data []byte) error { - return n.decodeFromBytes(AfiSafiToRouteFamily(n.AFI(), n.SAFI()), data) +func (n *FlowSpecIPv4Unicast) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { + return n.decodeFromBytes(AfiSafiToRouteFamily(n.AFI(), n.SAFI()), data, options...) } func NewFlowSpecIPv4Unicast(value []FlowSpecComponentInterface) *FlowSpecIPv4Unicast { @@ -3783,8 +3785,8 @@ type FlowSpecIPv4VPN struct { FlowSpecNLRI } -func (n *FlowSpecIPv4VPN) DecodeFromBytes(data []byte) error { - return n.decodeFromBytes(AfiSafiToRouteFamily(n.AFI(), n.SAFI()), data) +func (n *FlowSpecIPv4VPN) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { + return n.decodeFromBytes(AfiSafiToRouteFamily(n.AFI(), n.SAFI()), data, options...) } func NewFlowSpecIPv4VPN(rd RouteDistinguisherInterface, value []FlowSpecComponentInterface) *FlowSpecIPv4VPN { @@ -3795,8 +3797,8 @@ type FlowSpecIPv6Unicast struct { FlowSpecNLRI } -func (n *FlowSpecIPv6Unicast) DecodeFromBytes(data []byte) error { - return n.decodeFromBytes(AfiSafiToRouteFamily(n.AFI(), n.SAFI()), data) +func (n *FlowSpecIPv6Unicast) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { + return n.decodeFromBytes(AfiSafiToRouteFamily(n.AFI(), n.SAFI()), data, options...) } func NewFlowSpecIPv6Unicast(value []FlowSpecComponentInterface) *FlowSpecIPv6Unicast { @@ -3810,8 +3812,8 @@ type FlowSpecIPv6VPN struct { FlowSpecNLRI } -func (n *FlowSpecIPv6VPN) DecodeFromBytes(data []byte) error { - return n.decodeFromBytes(AfiSafiToRouteFamily(n.AFI(), n.SAFI()), data) +func (n *FlowSpecIPv6VPN) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { + return n.decodeFromBytes(AfiSafiToRouteFamily(n.AFI(), n.SAFI()), data, options...) } func NewFlowSpecIPv6VPN(rd RouteDistinguisherInterface, value []FlowSpecComponentInterface) *FlowSpecIPv6VPN { @@ -3826,7 +3828,7 @@ type FlowSpecL2VPN struct { FlowSpecNLRI } -func (n *FlowSpecL2VPN) DecodeFromBytes(data []byte) error { +func (n *FlowSpecL2VPN) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { return n.decodeFromBytes(AfiSafiToRouteFamily(n.AFI(), n.SAFI()), data) } @@ -3844,7 +3846,7 @@ type OpaqueNLRI struct { Value []byte } -func (n *OpaqueNLRI) DecodeFromBytes(data []byte) error { +func (n *OpaqueNLRI) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { if len(data) < 2 { return NewMessageError(BGP_ERROR_UPDATE_MESSAGE_ERROR, BGP_ERROR_SUB_MALFORMED_ATTRIBUTE_LIST, nil, "Not all OpaqueNLRI bytes available") } @@ -3857,7 +3859,7 @@ func (n *OpaqueNLRI) DecodeFromBytes(data []byte) error { return nil } -func (n *OpaqueNLRI) Serialize() ([]byte, error) { +func (n *OpaqueNLRI) Serialize(options ...*MarshallingOption) ([]byte, error) { if len(n.Key) > math.MaxUint16 { return nil, fmt.Errorf("Key length too big") } @@ -3875,7 +3877,7 @@ func (n *OpaqueNLRI) SAFI() uint8 { return SAFI_KEY_VALUE } -func (n *OpaqueNLRI) Len() int { +func (n *OpaqueNLRI) Len(options ...*MarshallingOption) int { return 2 + len(n.Key) + len(n.Value) } @@ -4289,9 +4291,9 @@ var PathAttrFlags map[BGPAttrType]BGPAttrFlag = map[BGPAttrType]BGPAttrFlag{ } type PathAttributeInterface interface { - DecodeFromBytes([]byte) error - Serialize() ([]byte, error) - Len() int + DecodeFromBytes([]byte, ...*MarshallingOption) error + Serialize(...*MarshallingOption) ([]byte, error) + Len(...*MarshallingOption) int GetFlags() BGPAttrFlag GetType() BGPAttrType String() string @@ -4306,7 +4308,7 @@ type PathAttribute struct { Value []byte } -func (p *PathAttribute) Len() int { +func (p *PathAttribute) Len(options ...*MarshallingOption) int { if p.Length == 0 { p.Length = uint16(len(p.Value)) } @@ -4327,7 +4329,7 @@ func (p *PathAttribute) GetType() BGPAttrType { return p.Type } -func (p *PathAttribute) DecodeFromBytes(data []byte) error { +func (p *PathAttribute) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { odata := data eCode := uint8(BGP_ERROR_UPDATE_MESSAGE_ERROR) eSubCode := uint8(BGP_ERROR_SUB_ATTRIBUTE_LENGTH_ERROR) @@ -4359,19 +4361,19 @@ func (p *PathAttribute) DecodeFromBytes(data []byte) error { ok, eMsg := ValidateFlags(p.Type, p.Flags) if !ok { - return NewMessageError(eCode, BGP_ERROR_SUB_ATTRIBUTE_FLAGS_ERROR, odata[:p.Len()], eMsg) + return NewMessageError(eCode, BGP_ERROR_SUB_ATTRIBUTE_FLAGS_ERROR, odata[:p.Len(options...)], eMsg) } return nil } -func (p *PathAttribute) Serialize() ([]byte, error) { +func (p *PathAttribute) Serialize(options ...*MarshallingOption) ([]byte, error) { p.Length = uint16(len(p.Value)) if p.Length > 255 { p.Flags |= BGP_ATTR_FLAG_EXTENDED_LENGTH } else { p.Flags &^= BGP_ATTR_FLAG_EXTENDED_LENGTH } - buf := make([]byte, p.Len()) + buf := make([]byte, p.Len(options...)) buf[0] = uint8(p.Flags) buf[1] = uint8(p.Type) if p.Flags&BGP_ATTR_FLAG_EXTENDED_LENGTH != 0 { @@ -4688,8 +4690,8 @@ type PathAttributeAsPath struct { Value []AsPathParamInterface } -func (p *PathAttributeAsPath) DecodeFromBytes(data []byte) error { - err := p.PathAttribute.DecodeFromBytes(data) +func (p *PathAttributeAsPath) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { + err := p.PathAttribute.DecodeFromBytes(data, options...) if err != nil { return err } @@ -4699,7 +4701,7 @@ func (p *PathAttributeAsPath) DecodeFromBytes(data []byte) error { } as4Bytes, err := p.DefaultAsPath.isValidAspath(p.PathAttribute.Value) if err != nil { - err.(*MessageError).Data = data[:p.Len()] + err.(*MessageError).Data = data[:p.Len(options...)] return err } v := p.PathAttribute.Value @@ -4723,7 +4725,7 @@ func (p *PathAttributeAsPath) DecodeFromBytes(data []byte) error { return nil } -func (p *PathAttributeAsPath) Serialize() ([]byte, error) { +func (p *PathAttributeAsPath) Serialize(options ...*MarshallingOption) ([]byte, error) { buf := make([]byte, 0) for _, v := range p.Value { vbuf, err := v.Serialize() @@ -4733,7 +4735,7 @@ func (p *PathAttributeAsPath) Serialize() ([]byte, error) { buf = append(buf, vbuf...) } p.PathAttribute.Value = buf - return p.PathAttribute.Serialize() + return p.PathAttribute.Serialize(options...) } func (p *PathAttributeAsPath) String() string { @@ -4770,8 +4772,8 @@ type PathAttributeNextHop struct { Value net.IP } -func (p *PathAttributeNextHop) DecodeFromBytes(data []byte) error { - err := p.PathAttribute.DecodeFromBytes(data) +func (p *PathAttributeNextHop) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { + err := p.PathAttribute.DecodeFromBytes(data, options...) if err != nil { return err } @@ -4784,9 +4786,9 @@ func (p *PathAttributeNextHop) DecodeFromBytes(data []byte) error { return nil } -func (p *PathAttributeNextHop) Serialize() ([]byte, error) { +func (p *PathAttributeNextHop) Serialize(options ...*MarshallingOption) ([]byte, error) { p.PathAttribute.Value = p.Value - return p.PathAttribute.Serialize() + return p.PathAttribute.Serialize(options...) } func (p *PathAttributeNextHop) String() string { @@ -4823,8 +4825,8 @@ type PathAttributeMultiExitDisc struct { Value uint32 } -func (p *PathAttributeMultiExitDisc) DecodeFromBytes(data []byte) error { - err := p.PathAttribute.DecodeFromBytes(data) +func (p *PathAttributeMultiExitDisc) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { + err := p.PathAttribute.DecodeFromBytes(data, options...) if err != nil { return err } @@ -4837,11 +4839,11 @@ func (p *PathAttributeMultiExitDisc) DecodeFromBytes(data []byte) error { return nil } -func (p *PathAttributeMultiExitDisc) Serialize() ([]byte, error) { +func (p *PathAttributeMultiExitDisc) Serialize(options ...*MarshallingOption) ([]byte, error) { buf := make([]byte, 4) binary.BigEndian.PutUint32(buf, p.Value) p.PathAttribute.Value = buf - return p.PathAttribute.Serialize() + return p.PathAttribute.Serialize(options...) } func (p *PathAttributeMultiExitDisc) String() string { @@ -4874,8 +4876,8 @@ type PathAttributeLocalPref struct { Value uint32 } -func (p *PathAttributeLocalPref) DecodeFromBytes(data []byte) error { - err := p.PathAttribute.DecodeFromBytes(data) +func (p *PathAttributeLocalPref) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { + err := p.PathAttribute.DecodeFromBytes(data, options...) if err != nil { return err } @@ -4888,11 +4890,11 @@ func (p *PathAttributeLocalPref) DecodeFromBytes(data []byte) error { return nil } -func (p *PathAttributeLocalPref) Serialize() ([]byte, error) { +func (p *PathAttributeLocalPref) Serialize(options ...*MarshallingOption) ([]byte, error) { buf := make([]byte, 4) binary.BigEndian.PutUint32(buf, p.Value) p.PathAttribute.Value = buf - return p.PathAttribute.Serialize() + return p.PathAttribute.Serialize(options...) } func (p *PathAttributeLocalPref) String() string { @@ -4957,8 +4959,8 @@ type PathAttributeAggregator struct { Value PathAttributeAggregatorParam } -func (p *PathAttributeAggregator) DecodeFromBytes(data []byte) error { - err := p.PathAttribute.DecodeFromBytes(data) +func (p *PathAttributeAggregator) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { + err := p.PathAttribute.DecodeFromBytes(data, options...) if err != nil { return err } @@ -4979,7 +4981,7 @@ func (p *PathAttributeAggregator) DecodeFromBytes(data []byte) error { return nil } -func (p *PathAttributeAggregator) Serialize() ([]byte, error) { +func (p *PathAttributeAggregator) Serialize(options ...*MarshallingOption) ([]byte, error) { var buf []byte switch p.Value.Askind { case reflect.Uint16: @@ -4993,7 +4995,7 @@ func (p *PathAttributeAggregator) Serialize() ([]byte, error) { } p.PathAttribute.Value = buf - return p.PathAttribute.Serialize() + return p.PathAttribute.Serialize(options...) } func (p *PathAttributeAggregator) String() string { @@ -5033,8 +5035,8 @@ type PathAttributeCommunities struct { Value []uint32 } -func (p *PathAttributeCommunities) DecodeFromBytes(data []byte) error { - err := p.PathAttribute.DecodeFromBytes(data) +func (p *PathAttributeCommunities) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { + err := p.PathAttribute.DecodeFromBytes(data, options...) if err != nil { return err } @@ -5051,13 +5053,13 @@ func (p *PathAttributeCommunities) DecodeFromBytes(data []byte) error { return nil } -func (p *PathAttributeCommunities) Serialize() ([]byte, error) { +func (p *PathAttributeCommunities) Serialize(options ...*MarshallingOption) ([]byte, error) { buf := make([]byte, len(p.Value)*4) for i, v := range p.Value { binary.BigEndian.PutUint32(buf[i*4:], v) } p.PathAttribute.Value = buf - return p.PathAttribute.Serialize() + return p.PathAttribute.Serialize(options...) } type WellKnownCommunity uint32 @@ -5153,8 +5155,8 @@ type PathAttributeOriginatorId struct { Value net.IP } -func (p *PathAttributeOriginatorId) DecodeFromBytes(data []byte) error { - err := p.PathAttribute.DecodeFromBytes(data) +func (p *PathAttributeOriginatorId) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { + err := p.PathAttribute.DecodeFromBytes(data, options...) if err != nil { return err } @@ -5181,11 +5183,11 @@ func (p *PathAttributeOriginatorId) MarshalJSON() ([]byte, error) { }) } -func (p *PathAttributeOriginatorId) Serialize() ([]byte, error) { +func (p *PathAttributeOriginatorId) Serialize(options ...*MarshallingOption) ([]byte, error) { buf := make([]byte, 4) copy(buf, p.Value) p.PathAttribute.Value = buf - return p.PathAttribute.Serialize() + return p.PathAttribute.Serialize(options...) } func NewPathAttributeOriginatorId(value string) *PathAttributeOriginatorId { @@ -5205,8 +5207,8 @@ type PathAttributeClusterList struct { Value []net.IP } -func (p *PathAttributeClusterList) DecodeFromBytes(data []byte) error { - err := p.PathAttribute.DecodeFromBytes(data) +func (p *PathAttributeClusterList) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { + err := p.PathAttribute.DecodeFromBytes(data, options...) if err != nil { return err } @@ -5223,13 +5225,13 @@ func (p *PathAttributeClusterList) DecodeFromBytes(data []byte) error { return nil } -func (p *PathAttributeClusterList) Serialize() ([]byte, error) { +func (p *PathAttributeClusterList) Serialize(options ...*MarshallingOption) ([]byte, error) { buf := make([]byte, len(p.Value)*4) for i, v := range p.Value { copy(buf[i*4:], v) } p.PathAttribute.Value = buf - return p.PathAttribute.Serialize() + return p.PathAttribute.Serialize(options...) } func (p *PathAttributeClusterList) String() string { @@ -5275,8 +5277,8 @@ type PathAttributeMpReachNLRI struct { Value []AddrPrefixInterface } -func (p *PathAttributeMpReachNLRI) DecodeFromBytes(data []byte) error { - err := p.PathAttribute.DecodeFromBytes(data) +func (p *PathAttributeMpReachNLRI) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { + err := p.PathAttribute.DecodeFromBytes(data, options...) if err != nil { return err } @@ -5292,7 +5294,7 @@ func (p *PathAttributeMpReachNLRI) DecodeFromBytes(data []byte) error { p.SAFI = safi _, err = NewPrefixFromRouteFamily(afi, safi) if err != nil { - return NewMessageError(eCode, BGP_ERROR_SUB_ATTRIBUTE_FLAGS_ERROR, data[:p.PathAttribute.Len()], err.Error()) + return NewMessageError(eCode, BGP_ERROR_SUB_ATTRIBUTE_FLAGS_ERROR, data[:p.PathAttribute.Len(options...)], err.Error()) } nexthoplen := int(value[3]) if len(value) < 4+nexthoplen { @@ -5327,22 +5329,22 @@ func (p *PathAttributeMpReachNLRI) DecodeFromBytes(data []byte) error { for len(value) > 0 { prefix, err := NewPrefixFromRouteFamily(afi, safi) if err != nil { - return NewMessageError(eCode, BGP_ERROR_SUB_ATTRIBUTE_FLAGS_ERROR, data[:p.PathAttribute.Len()], err.Error()) + return NewMessageError(eCode, BGP_ERROR_SUB_ATTRIBUTE_FLAGS_ERROR, data[:p.PathAttribute.Len(options...)], err.Error()) } - err = prefix.DecodeFromBytes(value) + err = prefix.DecodeFromBytes(value, options...) if err != nil { return err } - if prefix.Len() > len(value) { + if prefix.Len(options...) > len(value) { return NewMessageError(eCode, eSubCode, value, "prefix length is incorrect") } - value = value[prefix.Len():] + value = value[prefix.Len(options...):] p.Value = append(p.Value, prefix) } return nil } -func (p *PathAttributeMpReachNLRI) Serialize() ([]byte, error) { +func (p *PathAttributeMpReachNLRI) Serialize(options ...*MarshallingOption) ([]byte, error) { afi := p.AFI safi := p.SAFI nexthoplen := 4 @@ -5376,14 +5378,14 @@ func (p *PathAttributeMpReachNLRI) Serialize() ([]byte, error) { } buf = append(buf, make([]byte, 1)...) for _, prefix := range p.Value { - pbuf, err := prefix.Serialize() + pbuf, err := prefix.Serialize(options...) if err != nil { return nil, err } buf = append(buf, pbuf...) } p.PathAttribute.Value = buf - return p.PathAttribute.Serialize() + return p.PathAttribute.Serialize(options...) } func (p *PathAttributeMpReachNLRI) MarshalJSON() ([]byte, error) { @@ -5445,8 +5447,8 @@ type PathAttributeMpUnreachNLRI struct { Value []AddrPrefixInterface } -func (p *PathAttributeMpUnreachNLRI) DecodeFromBytes(data []byte) error { - err := p.PathAttribute.DecodeFromBytes(data) +func (p *PathAttributeMpUnreachNLRI) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { + err := p.PathAttribute.DecodeFromBytes(data, options...) if err != nil { return err } @@ -5461,7 +5463,7 @@ func (p *PathAttributeMpUnreachNLRI) DecodeFromBytes(data []byte) error { safi := value[2] _, err = NewPrefixFromRouteFamily(afi, safi) if err != nil { - return NewMessageError(eCode, BGP_ERROR_SUB_ATTRIBUTE_FLAGS_ERROR, data[:p.PathAttribute.Len()], err.Error()) + return NewMessageError(eCode, BGP_ERROR_SUB_ATTRIBUTE_FLAGS_ERROR, data[:p.PathAttribute.Len(options...)], err.Error()) } value = value[3:] p.AFI = afi @@ -5469,34 +5471,34 @@ func (p *PathAttributeMpUnreachNLRI) DecodeFromBytes(data []byte) error { for len(value) > 0 { prefix, err := NewPrefixFromRouteFamily(afi, safi) if err != nil { - return NewMessageError(eCode, BGP_ERROR_SUB_ATTRIBUTE_FLAGS_ERROR, data[:p.PathAttribute.Len()], err.Error()) + return NewMessageError(eCode, BGP_ERROR_SUB_ATTRIBUTE_FLAGS_ERROR, data[:p.PathAttribute.Len(options...)], err.Error()) } - err = prefix.DecodeFromBytes(value) + err = prefix.DecodeFromBytes(value, options...) if err != nil { return err } - if prefix.Len() > len(value) { - return NewMessageError(eCode, eSubCode, data[:p.PathAttribute.Len()], "prefix length is incorrect") + if prefix.Len(options...) > len(value) { + return NewMessageError(eCode, eSubCode, data[:p.PathAttribute.Len(options...)], "prefix length is incorrect") } - value = value[prefix.Len():] + value = value[prefix.Len(options...):] p.Value = append(p.Value, prefix) } return nil } -func (p *PathAttributeMpUnreachNLRI) Serialize() ([]byte, error) { +func (p *PathAttributeMpUnreachNLRI) Serialize(options ...*MarshallingOption) ([]byte, error) { buf := make([]byte, 3) binary.BigEndian.PutUint16(buf, p.AFI) buf[2] = p.SAFI for _, prefix := range p.Value { - pbuf, err := prefix.Serialize() + pbuf, err := prefix.Serialize(options...) if err != nil { return nil, err } buf = append(buf, pbuf...) } p.PathAttribute.Value = buf - return p.PathAttribute.Serialize() + return p.PathAttribute.Serialize(options...) } func (p *PathAttributeMpUnreachNLRI) String() string { @@ -6598,8 +6600,8 @@ func ParseExtended(data []byte) (ExtendedCommunityInterface, error) { } } -func (p *PathAttributeExtendedCommunities) DecodeFromBytes(data []byte) error { - err := p.PathAttribute.DecodeFromBytes(data) +func (p *PathAttributeExtendedCommunities) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { + err := p.PathAttribute.DecodeFromBytes(data, options...) if err != nil { return err } @@ -6620,7 +6622,7 @@ func (p *PathAttributeExtendedCommunities) DecodeFromBytes(data []byte) error { return nil } -func (p *PathAttributeExtendedCommunities) Serialize() ([]byte, error) { +func (p *PathAttributeExtendedCommunities) Serialize(options ...*MarshallingOption) ([]byte, error) { buf := make([]byte, 0) for _, p := range p.Value { ebuf, err := p.Serialize() @@ -6630,7 +6632,7 @@ func (p *PathAttributeExtendedCommunities) Serialize() ([]byte, error) { buf = append(buf, ebuf...) } p.PathAttribute.Value = buf - return p.PathAttribute.Serialize() + return p.PathAttribute.Serialize(options...) } func (p *PathAttributeExtendedCommunities) String() string { @@ -6673,8 +6675,8 @@ type PathAttributeAs4Path struct { DefaultAsPath } -func (p *PathAttributeAs4Path) DecodeFromBytes(data []byte) error { - err := p.PathAttribute.DecodeFromBytes(data) +func (p *PathAttributeAs4Path) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { + err := p.PathAttribute.DecodeFromBytes(data, options...) if err != nil { return err } @@ -6700,7 +6702,7 @@ func (p *PathAttributeAs4Path) DecodeFromBytes(data []byte) error { return nil } -func (p *PathAttributeAs4Path) Serialize() ([]byte, error) { +func (p *PathAttributeAs4Path) Serialize(options ...*MarshallingOption) ([]byte, error) { buf := make([]byte, 0) for _, v := range p.Value { vbuf, err := v.Serialize() @@ -6710,7 +6712,7 @@ func (p *PathAttributeAs4Path) Serialize() ([]byte, error) { buf = append(buf, vbuf...) } p.PathAttribute.Value = buf - return p.PathAttribute.Serialize() + return p.PathAttribute.Serialize(options...) } func (p *PathAttributeAs4Path) String() string { @@ -6737,8 +6739,8 @@ type PathAttributeAs4Aggregator struct { Value PathAttributeAggregatorParam } -func (p *PathAttributeAs4Aggregator) DecodeFromBytes(data []byte) error { - err := p.PathAttribute.DecodeFromBytes(data) +func (p *PathAttributeAs4Aggregator) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { + err := p.PathAttribute.DecodeFromBytes(data, options...) if err != nil { return err } @@ -6752,12 +6754,12 @@ func (p *PathAttributeAs4Aggregator) DecodeFromBytes(data []byte) error { return nil } -func (p *PathAttributeAs4Aggregator) Serialize() ([]byte, error) { +func (p *PathAttributeAs4Aggregator) Serialize(options ...*MarshallingOption) ([]byte, error) { buf := make([]byte, 8) binary.BigEndian.PutUint32(buf[0:], p.Value.AS) copy(buf[4:], p.Value.Address.To4()) p.PathAttribute.Value = buf - return p.PathAttribute.Serialize() + return p.PathAttribute.Serialize(options...) } func NewPathAttributeAs4Aggregator(as uint32, address string) *PathAttributeAs4Aggregator { @@ -6918,8 +6920,8 @@ type PathAttributeTunnelEncap struct { Value []*TunnelEncapTLV } -func (p *PathAttributeTunnelEncap) DecodeFromBytes(data []byte) error { - err := p.PathAttribute.DecodeFromBytes(data) +func (p *PathAttributeTunnelEncap) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { + err := p.PathAttribute.DecodeFromBytes(data, options...) if err != nil { return err } @@ -6949,7 +6951,7 @@ func (p *PathAttributeTunnelEncap) DecodeFromBytes(data []byte) error { return nil } -func (p *PathAttributeTunnelEncap) Serialize() ([]byte, error) { +func (p *PathAttributeTunnelEncap) Serialize(options ...*MarshallingOption) ([]byte, error) { buf := make([]byte, 0) for _, t := range p.Value { bbuf, err := t.Serialize() @@ -6959,7 +6961,7 @@ func (p *PathAttributeTunnelEncap) Serialize() ([]byte, error) { buf = append(buf, bbuf...) } p.PathAttribute.Value = buf - return p.PathAttribute.Serialize() + return p.PathAttribute.Serialize(options...) } func NewPathAttributeTunnelEncap(value []*TunnelEncapTLV) *PathAttributeTunnelEncap { @@ -7013,8 +7015,8 @@ type PathAttributePmsiTunnel struct { TunnelID PmsiTunnelIDInterface } -func (p *PathAttributePmsiTunnel) DecodeFromBytes(data []byte) error { - err := p.PathAttribute.DecodeFromBytes(data) +func (p *PathAttributePmsiTunnel) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { + err := p.PathAttribute.DecodeFromBytes(data, options...) if err != nil { return err } @@ -7039,7 +7041,7 @@ func (p *PathAttributePmsiTunnel) DecodeFromBytes(data []byte) error { return nil } -func (p *PathAttributePmsiTunnel) Serialize() ([]byte, error) { +func (p *PathAttributePmsiTunnel) Serialize(options ...*MarshallingOption) ([]byte, error) { buf := make([]byte, 2) if p.IsLeafInfoRequired { buf[0] = 0x01 @@ -7054,7 +7056,7 @@ func (p *PathAttributePmsiTunnel) Serialize() ([]byte, error) { } buf = append(buf, ibuf...) p.PathAttribute.Value = buf - return p.PathAttribute.Serialize() + return p.PathAttribute.Serialize(options...) } func (p *PathAttributePmsiTunnel) String() string { @@ -7208,7 +7210,7 @@ func ParseIP6Extended(data []byte) (ExtendedCommunityInterface, error) { } } -func (p *PathAttributeIP6ExtendedCommunities) DecodeFromBytes(data []byte) error { +func (p *PathAttributeIP6ExtendedCommunities) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { err := p.PathAttribute.DecodeFromBytes(data) if err != nil { return err @@ -7230,7 +7232,7 @@ func (p *PathAttributeIP6ExtendedCommunities) DecodeFromBytes(data []byte) error return nil } -func (p *PathAttributeIP6ExtendedCommunities) Serialize() ([]byte, error) { +func (p *PathAttributeIP6ExtendedCommunities) Serialize(options ...*MarshallingOption) ([]byte, error) { buf := make([]byte, 0) for _, p := range p.Value { ebuf, err := p.Serialize() @@ -7277,8 +7279,8 @@ type PathAttributeAigp struct { Values []AigpTLV } -func (p *PathAttributeAigp) DecodeFromBytes(data []byte) error { - err := p.PathAttribute.DecodeFromBytes(data) +func (p *PathAttributeAigp) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { + err := p.PathAttribute.DecodeFromBytes(data, options...) if err != nil { return err } @@ -7317,7 +7319,7 @@ func (p *PathAttributeAigp) DecodeFromBytes(data []byte) error { return NewMessageError(eCode, eSubCode, nil, "Aigp length is incorrect") } -func (p *PathAttributeAigp) Serialize() ([]byte, error) { +func (p *PathAttributeAigp) Serialize(options ...*MarshallingOption) ([]byte, error) { buf := make([]byte, 0) for _, t := range p.Values { bbuf, err := t.Serialize() @@ -7327,7 +7329,7 @@ func (p *PathAttributeAigp) Serialize() ([]byte, error) { buf = append(buf, bbuf...) } p.PathAttribute.Value = buf - return p.PathAttribute.Serialize() + return p.PathAttribute.Serialize(options...) } func (p *PathAttributeAigp) String() string { @@ -7408,7 +7410,7 @@ type PathAttributeLargeCommunities struct { Values []*LargeCommunity } -func (p *PathAttributeLargeCommunities) DecodeFromBytes(data []byte) error { +func (p *PathAttributeLargeCommunities) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { err := p.PathAttribute.DecodeFromBytes(data) if err != nil { return err @@ -7434,7 +7436,7 @@ func (p *PathAttributeLargeCommunities) DecodeFromBytes(data []byte) error { return nil } -func (p *PathAttributeLargeCommunities) Serialize() ([]byte, error) { +func (p *PathAttributeLargeCommunities) Serialize(options ...*MarshallingOption) ([]byte, error) { buf := make([]byte, 0) for _, t := range p.Values { bbuf, err := t.Serialize() @@ -7543,7 +7545,7 @@ type BGPUpdate struct { NLRI []*IPAddrPrefix } -func (msg *BGPUpdate) DecodeFromBytes(data []byte) error { +func (msg *BGPUpdate) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { // cache error codes eCode := uint8(BGP_ERROR_UPDATE_MESSAGE_ERROR) @@ -7565,15 +7567,15 @@ func (msg *BGPUpdate) DecodeFromBytes(data []byte) error { msg.WithdrawnRoutes = make([]*IPAddrPrefix, 0, msg.WithdrawnRoutesLen) for routelen := msg.WithdrawnRoutesLen; routelen > 0; { w := &IPAddrPrefix{} - err := w.DecodeFromBytes(data) + err := w.DecodeFromBytes(data, options...) if err != nil { return err } - routelen -= uint16(w.Len()) - if len(data) < w.Len() { + routelen -= uint16(w.Len(options...)) + if len(data) < w.Len(options...) { return NewMessageError(eCode, eSubCode, nil, "Withdrawn route length is short") } - data = data[w.Len():] + data = data[w.Len(options...):] msg.WithdrawnRoutes = append(msg.WithdrawnRoutes, w) } @@ -7596,40 +7598,40 @@ func (msg *BGPUpdate) DecodeFromBytes(data []byte) error { if err != nil { return err } - err = p.DecodeFromBytes(data) + err = p.DecodeFromBytes(data, options...) if err != nil { return err } - pathlen -= uint16(p.Len()) - if len(data) < p.Len() { + pathlen -= uint16(p.Len(options...)) + if len(data) < p.Len(options...) { return NewMessageError(eCode, BGP_ERROR_SUB_ATTRIBUTE_LENGTH_ERROR, data, "attribute length is short") } - data = data[p.Len():] + data = data[p.Len(options...):] msg.PathAttributes = append(msg.PathAttributes, p) } msg.NLRI = make([]*IPAddrPrefix, 0) for restlen := len(data); restlen > 0; { n := &IPAddrPrefix{} - err := n.DecodeFromBytes(data) + err := n.DecodeFromBytes(data, options...) if err != nil { return err } - restlen -= n.Len() - if len(data) < n.Len() { + restlen -= n.Len(options...) + if len(data) < n.Len(options...) { return NewMessageError(eCode, BGP_ERROR_SUB_INVALID_NETWORK_FIELD, nil, "NLRI length is short") } - data = data[n.Len():] + data = data[n.Len(options...):] msg.NLRI = append(msg.NLRI, n) } return nil } -func (msg *BGPUpdate) Serialize() ([]byte, error) { +func (msg *BGPUpdate) Serialize(options ...*MarshallingOption) ([]byte, error) { wbuf := make([]byte, 2) for _, w := range msg.WithdrawnRoutes { - onewbuf, err := w.Serialize() + onewbuf, err := w.Serialize(options...) if err != nil { return nil, err } @@ -7640,7 +7642,7 @@ func (msg *BGPUpdate) Serialize() ([]byte, error) { pbuf := make([]byte, 2) for _, p := range msg.PathAttributes { - onepbuf, err := p.Serialize() + onepbuf, err := p.Serialize(options...) if err != nil { return nil, err } @@ -7651,7 +7653,7 @@ func (msg *BGPUpdate) Serialize() ([]byte, error) { buf := append(wbuf, pbuf...) for _, n := range msg.NLRI { - nbuf, err := n.Serialize() + nbuf, err := n.Serialize(options...) if err != nil { return nil, err } @@ -7706,7 +7708,7 @@ type BGPNotification struct { Data []byte } -func (msg *BGPNotification) DecodeFromBytes(data []byte) error { +func (msg *BGPNotification) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { if len(data) < 2 { return NewMessageError(BGP_ERROR_MESSAGE_HEADER_ERROR, BGP_ERROR_SUB_BAD_MESSAGE_LENGTH, nil, "Not all Notificaiton bytes available") } @@ -7718,7 +7720,7 @@ func (msg *BGPNotification) DecodeFromBytes(data []byte) error { return nil } -func (msg *BGPNotification) Serialize() ([]byte, error) { +func (msg *BGPNotification) Serialize(options ...*MarshallingOption) ([]byte, error) { buf := make([]byte, 2) buf[0] = msg.ErrorCode buf[1] = msg.ErrorSubcode @@ -7736,11 +7738,11 @@ func NewBGPNotificationMessage(errcode uint8, errsubcode uint8, data []byte) *BG type BGPKeepAlive struct { } -func (msg *BGPKeepAlive) DecodeFromBytes(data []byte) error { +func (msg *BGPKeepAlive) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { return nil } -func (msg *BGPKeepAlive) Serialize() ([]byte, error) { +func (msg *BGPKeepAlive) Serialize(options ...*MarshallingOption) ([]byte, error) { return nil, nil } @@ -7757,7 +7759,7 @@ type BGPRouteRefresh struct { SAFI uint8 } -func (msg *BGPRouteRefresh) DecodeFromBytes(data []byte) error { +func (msg *BGPRouteRefresh) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { if len(data) < 4 { return NewMessageError(BGP_ERROR_ROUTE_REFRESH_MESSAGE_ERROR, BGP_ERROR_SUB_INVALID_MESSAGE_LENGTH, nil, "Not all RouteRefresh bytes available") } @@ -7767,7 +7769,7 @@ func (msg *BGPRouteRefresh) DecodeFromBytes(data []byte) error { return nil } -func (msg *BGPRouteRefresh) Serialize() ([]byte, error) { +func (msg *BGPRouteRefresh) Serialize(options ...*MarshallingOption) ([]byte, error) { buf := make([]byte, 4) binary.BigEndian.PutUint16(buf[0:2], msg.AFI) buf[2] = msg.Demarcation @@ -7783,8 +7785,8 @@ func NewBGPRouteRefreshMessage(afi uint16, demarcation uint8, safi uint8) *BGPMe } type BGPBody interface { - DecodeFromBytes([]byte) error - Serialize() ([]byte, error) + DecodeFromBytes([]byte, ...*MarshallingOption) error + Serialize(...*MarshallingOption) ([]byte, error) } const ( @@ -7798,7 +7800,7 @@ type BGPHeader struct { Type uint8 } -func (msg *BGPHeader) DecodeFromBytes(data []byte) error { +func (msg *BGPHeader) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { // minimum BGP message length if uint16(len(data)) < BGP_HEADER_LENGTH { return NewMessageError(BGP_ERROR_MESSAGE_HEADER_ERROR, BGP_ERROR_SUB_BAD_MESSAGE_LENGTH, nil, "not all BGP message header") @@ -7811,7 +7813,7 @@ func (msg *BGPHeader) DecodeFromBytes(data []byte) error { return nil } -func (msg *BGPHeader) Serialize() ([]byte, error) { +func (msg *BGPHeader) Serialize(options ...*MarshallingOption) ([]byte, error) { buf := make([]byte, 19) for i := range buf[:16] { buf[i] = 0xff @@ -7826,7 +7828,7 @@ type BGPMessage struct { Body BGPBody } -func parseBody(h *BGPHeader, data []byte) (*BGPMessage, error) { +func parseBody(h *BGPHeader, data []byte, options ...*MarshallingOption) (*BGPMessage, error) { if len(data) < int(h.Len)-BGP_HEADER_LENGTH { return nil, NewMessageError(BGP_ERROR_MESSAGE_HEADER_ERROR, BGP_ERROR_SUB_BAD_MESSAGE_LENGTH, nil, "Not all BGP message bytes available") } @@ -7846,28 +7848,28 @@ func parseBody(h *BGPHeader, data []byte) (*BGPMessage, error) { default: return nil, NewMessageError(BGP_ERROR_MESSAGE_HEADER_ERROR, BGP_ERROR_SUB_BAD_MESSAGE_TYPE, nil, "unknown message type") } - err := msg.Body.DecodeFromBytes(data) + err := msg.Body.DecodeFromBytes(data, options...) if err != nil { return nil, err } return msg, nil } -func ParseBGPMessage(data []byte) (*BGPMessage, error) { +func ParseBGPMessage(data []byte, options ...*MarshallingOption) (*BGPMessage, error) { h := &BGPHeader{} - err := h.DecodeFromBytes(data) + err := h.DecodeFromBytes(data, options...) if err != nil { return nil, err } - return parseBody(h, data[19:h.Len]) + return parseBody(h, data[19:h.Len], options...) } -func ParseBGPBody(h *BGPHeader, data []byte) (*BGPMessage, error) { - return parseBody(h, data) +func ParseBGPBody(h *BGPHeader, data []byte, options ...*MarshallingOption) (*BGPMessage, error) { + return parseBody(h, data, options...) } -func (msg *BGPMessage) Serialize() ([]byte, error) { - b, err := msg.Body.Serialize() +func (msg *BGPMessage) Serialize(options ...*MarshallingOption) ([]byte, error) { + b, err := msg.Body.Serialize(options...) if err != nil { return nil, err } @@ -7877,7 +7879,7 @@ func (msg *BGPMessage) Serialize() ([]byte, error) { } msg.Header.Len = 19 + uint16(len(b)) } - h, err := msg.Header.Serialize() + h, err := msg.Header.Serialize(options...) if err != nil { return nil, err } |