go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/l3plugin/vppcalls/vpp2106/dhcpproxy_vppcalls.go (about) 1 // Copyright (c) 2021 PANTHEON.tech 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 vpp2106 16 17 import ( 18 "fmt" 19 "net" 20 21 "github.com/pkg/errors" 22 23 vpp_dhcp "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2106/dhcp" 24 "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2106/ip_types" 25 l3 "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/l3" 26 ) 27 28 func (h *DHCPProxyHandler) createDeleteDHCPProxy(entry *l3.DHCPProxy, delete bool) (err error) { 29 config := &vpp_dhcp.DHCPProxyConfig{ 30 RxVrfID: entry.RxVrfId, 31 IsAdd: !delete, 32 } 33 if config.DHCPSrcAddress, err = ipToDHCPAddress(entry.SourceIpAddress); err != nil { 34 return errors.Errorf("Invalid source IP address: %q", entry.SourceIpAddress) 35 } 36 for _, server := range entry.Servers { 37 config.ServerVrfID = server.VrfId 38 if config.DHCPServer, err = ipToDHCPAddress(server.IpAddress); err != nil { 39 return errors.Errorf("Invalid server IP address: %q", server.IpAddress) 40 } 41 reply := &vpp_dhcp.DHCPProxyConfigReply{} 42 if err := h.callsChannel.SendRequest(config).ReceiveReply(reply); err != nil { 43 return err 44 } 45 } 46 47 return nil 48 } 49 50 func (h *DHCPProxyHandler) CreateDHCPProxy(entry *l3.DHCPProxy) error { 51 return h.createDeleteDHCPProxy(entry, false) 52 } 53 54 func (h *DHCPProxyHandler) DeleteDHCPProxy(entry *l3.DHCPProxy) error { 55 return h.createDeleteDHCPProxy(entry, true) 56 } 57 58 func ipToDHCPAddress(address string) (dhcpAddr ip_types.Address, err error) { 59 netIP := net.ParseIP(address) 60 if netIP == nil { 61 return ip_types.Address{}, fmt.Errorf("invalid IP: %q", address) 62 } 63 if ip4 := netIP.To4(); ip4 == nil { 64 dhcpAddr.Af = ip_types.ADDRESS_IP6 65 var ip6addr ip_types.IP6Address 66 copy(ip6addr[:], netIP.To16()) 67 dhcpAddr.Un.SetIP6(ip6addr) 68 } else { 69 dhcpAddr.Af = ip_types.ADDRESS_IP4 70 var ip4addr ip_types.IP4Address 71 copy(ip4addr[:], ip4) 72 dhcpAddr.Un.SetIP4(ip4addr) 73 } 74 return 75 }