go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/ifplugin/vppcalls/vpp2202/helpers.go (about) 1 // Copyright (c) 2022 Cisco and/or its affiliates. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at: 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package vpp2202 16 17 import ( 18 "fmt" 19 "net" 20 "strings" 21 22 "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2202/ip_types" 23 ) 24 25 // IPToAddress converts string type IP address to VPP ip.api address representation 26 func IPToAddress(ipStr string) (addr ip_types.Address, err error) { 27 netIP := net.ParseIP(ipStr) 28 if netIP == nil { 29 return ip_types.Address{}, fmt.Errorf("invalid IP: %q", ipStr) 30 } 31 if ip4 := netIP.To4(); ip4 == nil { 32 addr.Af = ip_types.ADDRESS_IP6 33 var ip6addr ip_types.IP6Address 34 copy(ip6addr[:], netIP.To16()) 35 addr.Un.SetIP6(ip6addr) 36 } else { 37 addr.Af = ip_types.ADDRESS_IP4 38 var ip4addr ip_types.IP4Address 39 copy(ip4addr[:], ip4) 40 addr.Un.SetIP4(ip4addr) 41 } 42 return 43 } 44 45 func ipToAddress(address *net.IPNet, isIPv6 bool) (ipAddr ip_types.Address) { 46 if isIPv6 { 47 ipAddr.Af = ip_types.ADDRESS_IP6 48 var ip6addr ip_types.IP6Address 49 copy(ip6addr[:], address.IP.To16()) 50 ipAddr.Un.SetIP6(ip6addr) 51 } else { 52 ipAddr.Af = ip_types.ADDRESS_IP4 53 var ip4addr ip_types.IP4Address 54 copy(ip4addr[:], address.IP.To4()) 55 ipAddr.Un.SetIP4(ip4addr) 56 } 57 return 58 } 59 60 func boolToUint(input bool) uint8 { 61 if input { 62 return 1 63 } 64 return 0 65 } 66 67 func uintToBool(value uint8) bool { 68 return value != 0 69 } 70 71 func cleanString(s string) string { 72 return strings.SplitN(s, "\x00", 2)[0] 73 }