Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for vl3_dns #1332

Merged
merged 1 commit into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 2 additions & 32 deletions pkg/networkservice/connectioncontext/dnscontext/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/networkservicemesh/api/pkg/api/networkservice"

"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/tools/dnsutils"
)

// GetDNSConfigsFunc gets dns configs
Expand All @@ -47,7 +48,7 @@ func (d *dnsContextServer) Request(ctx context.Context, request *networkservice.
}

for _, config := range d.configs {
if !contains(request.GetConnection().GetContext().GetDnsContext().Configs, config) {
if !dnsutils.ContainsDNSConfig(request.GetConnection().GetContext().GetDnsContext().Configs, config) {
request.GetConnection().GetContext().GetDnsContext().Configs = append(request.GetConnection().GetContext().GetDnsContext().Configs, config)
}
}
Expand All @@ -57,34 +58,3 @@ func (d *dnsContextServer) Request(ctx context.Context, request *networkservice.
func (d *dnsContextServer) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) {
return next.Server(ctx).Close(ctx, conn)
}

func contains(array []*networkservice.DNSConfig, value *networkservice.DNSConfig) bool {
for i := range array {
if equal(array[i].DnsServerIps, value.DnsServerIps) && equal(array[i].SearchDomains, value.SearchDomains) {
return true
}
}
return false
}

func equal(a, b []string) bool {
if len(a) != len(b) {
return false
}

diff := make(map[string]int, len(a))
for _, v := range a {
diff[v]++
}

for _, v := range b {
if _, ok := diff[v]; !ok {
return false
}
diff[v]--
if diff[v] == 0 {
delete(diff, v)
}
}
return len(diff) == 0
}
41 changes: 28 additions & 13 deletions pkg/networkservice/connectioncontext/dnscontext/vl3dns/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,24 +89,15 @@ func (n *vl3DNSServer) Request(ctx context.Context, request *networkservice.Netw
request.Connection.Context.DnsContext = new(networkservice.DNSContext)
}

var dnsContext = request.GetConnection().GetContext().GetDnsContext()
var clientsConfigs = dnsContext.GetConfigs()

dnsContext.Configs = append(dnsContext.Configs, &networkservice.DNSConfig{
DnsServerIps: []string{n.getDNSServerIP().String()},
})
var clientsConfigs = request.GetConnection().GetContext().GetDnsContext().GetConfigs()

dnsServerIPStr, added := n.addDNSContext(request.GetConnection())
var recordNames, err = n.buildSrcDNSRecords(request.GetConnection())

if err != nil {
return nil, err
}

var ips []net.IP
for _, srcIPNet := range request.GetConnection().GetContext().GetIpContext().GetSrcIPNets() {
ips = append(ips, srcIPNet.IP)
}

if v, ok := metadata.Map(ctx, false).LoadAndDelete(clientDNSNameKey{}); ok {
var previousNames = v.([]string)
if !compareStringSlices(previousNames, recordNames) {
Expand All @@ -115,6 +106,8 @@ func (n *vl3DNSServer) Request(ctx context.Context, request *networkservice.Netw
}
}
}

ips := getSrcIPs(request.GetConnection())
if len(ips) > 0 {
for _, recordName := range recordNames {
n.dnsServerRecords.Store(recordName, ips)
Expand All @@ -127,11 +120,11 @@ func (n *vl3DNSServer) Request(ctx context.Context, request *networkservice.Netw

if err == nil {
configs := make([]*networkservice.DNSConfig, 0)
if srcRoutes := resp.GetContext().GetIpContext().GetSrcIPRoutes(); len(srcRoutes) > 0 {
if srcRoutes := resp.GetContext().GetIpContext().GetSrcRoutes(); len(srcRoutes) > 0 {
var lastPrefix = srcRoutes[len(srcRoutes)-1].Prefix
for _, config := range clientsConfigs {
for _, serverIP := range config.DnsServerIps {
if serverIP == n.getDNSServerIP().String() {
if added && dnsServerIPStr == serverIP {
continue
}
if withinPrefix(serverIP, lastPrefix) {
Expand All @@ -158,6 +151,20 @@ func (n *vl3DNSServer) Close(ctx context.Context, conn *networkservice.Connectio
return next.Server(ctx).Close(ctx, conn)
}

func (n *vl3DNSServer) addDNSContext(c *networkservice.Connection) (added string, ok bool) {
if dnsServerIP := n.getDNSServerIP(); dnsServerIP != nil {
var dnsContext = c.GetContext().GetDnsContext()
configToAdd := &networkservice.DNSConfig{
DnsServerIps: []string{dnsServerIP.String()},
}
if !dnsutils.ContainsDNSConfig(dnsContext.Configs, configToAdd) {
dnsContext.Configs = append(dnsContext.Configs, configToAdd)
}
return dnsServerIP.String(), true
}
return "", false
}

func (n *vl3DNSServer) buildSrcDNSRecords(c *networkservice.Connection) ([]string, error) {
var result []string
for _, templ := range n.domainSchemeTemplates {
Expand Down Expand Up @@ -190,3 +197,11 @@ func withinPrefix(ipAddr, prefix string) bool {
var pool = ippool.NewWithNet(ipNet)
return pool.ContainsString(ipAddr)
}

func getSrcIPs(c *networkservice.Connection) []net.IP {
var ips []net.IP
for _, srcIPNet := range c.GetContext().GetIpContext().GetSrcIPNets() {
ips = append(ips, srcIPNet.IP)
}
return ips
}
34 changes: 34 additions & 0 deletions pkg/tools/dnsutils/dnsutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (

"github.com/miekg/dns"

"github.com/networkservicemesh/api/pkg/api/networkservice"

"github.com/networkservicemesh/sdk/pkg/tools/log"
)

Expand Down Expand Up @@ -56,3 +58,35 @@ func ListenAndServe(ctx context.Context, handler Handler, listenOn string) {
}()
}
}

// ContainsDNSConfig returns true if array contains a specific dns config
func ContainsDNSConfig(array []*networkservice.DNSConfig, value *networkservice.DNSConfig) bool {
for i := range array {
if equal(array[i].DnsServerIps, value.DnsServerIps) && equal(array[i].SearchDomains, value.SearchDomains) {
return true
}
}
return false
}

func equal(a, b []string) bool {
if len(a) != len(b) {
return false
}

diff := make(map[string]int, len(a))
for _, v := range a {
diff[v]++
}

for _, v := range b {
if _, ok := diff[v]; !ok {
return false
}
diff[v]--
if diff[v] == 0 {
delete(diff, v)
}
}
return len(diff) == 0
}