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

     1  //  Copyright (c) 2019 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 vpp2101
    16  
    17  import (
    18  	"fmt"
    19  	"net"
    20  
    21  	govppapi "go.fd.io/govpp/api"
    22  	"go.ligato.io/cn-infra/v2/logging"
    23  
    24  	vpp2101 "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2101"
    25  	"go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2101/ip_types"
    26  	vpp_ipsec "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2101/ipsec"
    27  	"go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/ifaceidx"
    28  	"go.ligato.io/vpp-agent/v3/plugins/vpp/ipsecplugin/vppcalls"
    29  )
    30  
    31  func init() {
    32  	var msgs []govppapi.Message
    33  	msgs = append(msgs, vpp_ipsec.AllMessages()...)
    34  
    35  	vppcalls.AddHandlerVersion(vpp2101.Version, msgs, NewIPSecVppHandler)
    36  }
    37  
    38  // IPSecVppHandler is accessor for IPSec-related vppcalls methods
    39  type IPSecVppHandler struct {
    40  	callsChannel govppapi.Channel
    41  	ifIndexes    ifaceidx.IfaceMetadataIndex
    42  	log          logging.Logger
    43  }
    44  
    45  func NewIPSecVppHandler(ch govppapi.Channel, ifIdx ifaceidx.IfaceMetadataIndex, log logging.Logger) vppcalls.IPSecVppAPI {
    46  	return &IPSecVppHandler{ch, ifIdx, log}
    47  }
    48  
    49  func ipsecAddrToIP(addr ip_types.Address) net.IP {
    50  	if addr.Af == ip_types.ADDRESS_IP6 {
    51  		addrIP := addr.Un.GetIP6()
    52  		return net.IP(addrIP[:])
    53  	}
    54  	addrIP := addr.Un.GetIP4()
    55  	return net.IP(addrIP[:])
    56  }
    57  
    58  func IPToAddress(ipstr string) (addr ip_types.Address, err error) {
    59  	netIP := net.ParseIP(ipstr)
    60  	if netIP == nil {
    61  		return ip_types.Address{}, fmt.Errorf("invalid IP: %q", ipstr)
    62  	}
    63  	if ip4 := netIP.To4(); ip4 == nil {
    64  		addr.Af = ip_types.ADDRESS_IP6
    65  		var ip6addr ip_types.IP6Address
    66  		copy(ip6addr[:], netIP.To16())
    67  		addr.Un.SetIP6(ip6addr)
    68  	} else {
    69  		addr.Af = ip_types.ADDRESS_IP4
    70  		var ip4addr ip_types.IP4Address
    71  		copy(ip4addr[:], ip4)
    72  		addr.Un.SetIP4(ip4addr)
    73  	}
    74  	return
    75  }