From e54b8f8d4fd29fc275065b0cb3369f5c5a2644c7 Mon Sep 17 00:00:00 2001 From: Emir Aganovic Date: Sat, 6 Apr 2024 14:45:29 +0200 Subject: [PATCH] feat: add option to prefer SRV resolution on transport layer of user agent --- sip/transport_layer.go | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/sip/transport_layer.go b/sip/transport_layer.go index 42bace2..addbf43 100644 --- a/sip/transport_layer.go +++ b/sip/transport_layer.go @@ -42,6 +42,9 @@ type TransportLayer struct { // ConnectionReuse will force connection reuse when passing request ConnectionReuse bool + + // PreferSRV does always SRV lookup first + DNSPreferSRV bool } // NewLayer creates transport layer. @@ -415,17 +418,41 @@ func (l *TransportLayer) resolveAddr(ctx context.Context, network string, host s } }(time.Now()) - l.log.Debug().Str("host", host).Msg("DNS Resolving") - // We need to try local resolving. - ip, err := net.ResolveIPAddr("ip", host) + if l.DNSPreferSRV { + err := l.resolveAddrSRV(ctx, network, host, addr) + if err == nil { + return nil + } + log.Warn().Str("host", host).Err(err).Msg("Doing SRV lookup failed.") + return l.resolveAddrIP(ctx, host, addr) + } + + err := l.resolveAddrIP(ctx, host, addr) if err == nil { - addr.IP = ip.IP return nil } - log.Debug().Err(err).Msg("IP addr resolving failed, doing via dns SRV resolver...") + + log.Info().Err(err).Msg("IP addr resolving failed, doing via dns SRV resolver...") return l.resolveAddrSRV(ctx, network, host, addr) } +func (l *TransportLayer) resolveAddrIP(ctx context.Context, hostname string, addr *Addr) error { + l.log.Debug().Str("host", hostname).Msg("DNS Resolving") + + // Do local resolving + ips, err := l.dnsResolver.LookupIPAddr(ctx, hostname) + if err != nil { + return err + } + if len(ips) == 0 { + // Should not happen + return fmt.Errorf("lookup ip addr did not return any ip addr") + } + + addr.IP = ips[0].IP + return nil +} + func (l *TransportLayer) resolveAddrSRV(ctx context.Context, network string, hostname string, addr *Addr) error { log := &l.log var proto string