go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/ifplugin/vppcalls/vpp2101/helpers.go (about)

     1  package vpp2101
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"strings"
     7  
     8  	"go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2101/ip_types"
     9  )
    10  
    11  // IPToAddress converts string type IP address to VPP ip.api address representation
    12  func IPToAddress(ipStr string) (addr ip_types.Address, err error) {
    13  	netIP := net.ParseIP(ipStr)
    14  	if netIP == nil {
    15  		return ip_types.Address{}, fmt.Errorf("invalid IP: %q", ipStr)
    16  	}
    17  	if ip4 := netIP.To4(); ip4 == nil {
    18  		addr.Af = ip_types.ADDRESS_IP6
    19  		var ip6addr ip_types.IP6Address
    20  		copy(ip6addr[:], netIP.To16())
    21  		addr.Un.SetIP6(ip6addr)
    22  	} else {
    23  		addr.Af = ip_types.ADDRESS_IP4
    24  		var ip4addr ip_types.IP4Address
    25  		copy(ip4addr[:], ip4)
    26  		addr.Un.SetIP4(ip4addr)
    27  	}
    28  	return
    29  }
    30  
    31  func ipToAddress(address *net.IPNet, isIPv6 bool) (ipAddr ip_types.Address) {
    32  	if isIPv6 {
    33  		ipAddr.Af = ip_types.ADDRESS_IP6
    34  		var ip6addr ip_types.IP6Address
    35  		copy(ip6addr[:], address.IP.To16())
    36  		ipAddr.Un.SetIP6(ip6addr)
    37  	} else {
    38  		ipAddr.Af = ip_types.ADDRESS_IP4
    39  		var ip4addr ip_types.IP4Address
    40  		copy(ip4addr[:], address.IP.To4())
    41  		ipAddr.Un.SetIP4(ip4addr)
    42  	}
    43  	return
    44  }
    45  
    46  func boolToUint(input bool) uint8 {
    47  	if input {
    48  		return 1
    49  	}
    50  	return 0
    51  }
    52  
    53  func uintToBool(value uint8) bool {
    54  	return value != 0
    55  }
    56  
    57  func cleanString(s string) string {
    58  	return strings.SplitN(s, "\x00", 2)[0]
    59  }