diff options
author | Vladimir Lavor <vlavor@cisco.com> | 2020-07-01 12:18:54 +0200 |
---|---|---|
committer | Vladimir Lavor <vlavor@cisco.com> | 2020-07-02 15:23:07 +0200 |
commit | c7ae74a95d1bd6fefcbb061f5f045c60c11e32fc (patch) | |
tree | f04013af76e1e3c38266cc2606312cd0c9f443d3 /examples/binapi/ip_types/ip_types.ba.go | |
parent | df67791c6ffc96331f75aec7d3addfe2efca7739 (diff) |
Binary API generator improvements
* Many aliases removed, aliased types reference original types via import instead
* Added various helper methods for simpler conversion between go and vpp types
Change-Id: I7999ac8d524cece4da03e6447b13421659765095
Signed-off-by: Vladimir Lavor <vlavor@cisco.com>
Diffstat (limited to 'examples/binapi/ip_types/ip_types.ba.go')
-rw-r--r-- | examples/binapi/ip_types/ip_types.ba.go | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/examples/binapi/ip_types/ip_types.ba.go b/examples/binapi/ip_types/ip_types.ba.go index 23ae315..fc7fa87 100644 --- a/examples/binapi/ip_types/ip_types.ba.go +++ b/examples/binapi/ip_types/ip_types.ba.go @@ -19,9 +19,12 @@ import ( "bytes" "context" "encoding/binary" + "fmt" "io" "math" + "net" "strconv" + "strings" api "git.fd.io/govpp.git/api" codec "git.fd.io/govpp.git/codec" @@ -270,6 +273,38 @@ type Address struct { func (*Address) GetTypeName() string { return "address" } +func ParseAddress(ip string) (Address, error) { + var address Address + netIP := net.ParseIP(ip) + if netIP == nil { + return address, fmt.Errorf("invalid address: %s", ip) + } + if ip4 := netIP.To4(); ip4 == nil { + address.Af = ADDRESS_IP6 + var ip6addr IP6Address + copy(ip6addr[:], netIP.To16()) + address.Un.SetIP6(ip6addr) + } else { + address.Af = ADDRESS_IP4 + var ip4addr IP4Address + copy(ip4addr[:], netIP.To4()) + address.Un.SetIP4(ip4addr) + } + return address, nil +} + +func (a *Address) ToString() string { + var ip string + if a.Af == ADDRESS_IP6 { + ip6Address := a.Un.GetIP6() + ip = net.IP(ip6Address[:]).To16().String() + } else { + ip4Address := a.Un.GetIP4() + ip = net.IP(ip4Address[:]).To4().String() + } + return ip +} + // IP4Prefix represents VPP binary API type 'ip4_prefix'. type IP4Prefix struct { Address IP4Address `binapi:"ip4_address,name=address" json:"address,omitempty"` @@ -304,6 +339,39 @@ type Prefix struct { func (*Prefix) GetTypeName() string { return "prefix" } +func ParsePrefix(ip string) (prefix Prefix, err error) { + hasPrefix := strings.Contains(ip, "/") + if hasPrefix { + netIP, network, err := net.ParseCIDR(ip) + if err != nil { + return Prefix{}, fmt.Errorf("invalid IP %s: %v", ip, err) + } + maskSize, _ := network.Mask.Size() + prefix.Len = byte(maskSize) + prefix.Address, err = ParseAddress(netIP.String()) + if err != nil { + return Prefix{}, fmt.Errorf("invalid IP %s: %v", ip, err) + } + } else { + netIP := net.ParseIP(ip) + defaultMaskSize, _ := net.CIDRMask(32, 32).Size() + if netIP.To4() == nil { + defaultMaskSize, _ = net.CIDRMask(128, 128).Size() + } + prefix.Len = byte(defaultMaskSize) + prefix.Address, err = ParseAddress(netIP.String()) + if err != nil { + return Prefix{}, fmt.Errorf("invalid IP %s: %v", ip, err) + } + } + return prefix, nil +} + +func (p *Prefix) ToString() string { + ip := p.Address.ToString() + return ip + "/" + strconv.Itoa(int(p.Len)) +} + // PrefixMatcher represents VPP binary API type 'prefix_matcher'. type PrefixMatcher struct { Le uint8 `binapi:"u8,name=le" json:"le,omitempty"` @@ -360,6 +428,9 @@ var _ = bytes.NewBuffer var _ = context.Background var _ = io.Copy var _ = strconv.Itoa +var _ = strings.Contains var _ = struc.Pack var _ = binary.BigEndian var _ = math.Float32bits +var _ = net.ParseIP +var _ = fmt.Errorf |