Debian Codegraph @

cmd/atomdns/internal/localaddr/localaddr.go

raw
1 package localaddr
2
3 import (
4 "net"
5
6 "codeberg.org/miekg/dns/dnsutil"
7 )
8
9 // Source returns the address from sources that matches the family. If none match, nil is returned.
10 // Sources may be bare IP addresses or host:port combos.
11 func Source(family int, sources []string) net.IP {
12 for _, s := range sources {
13 sip := net.ParseIP(s)
14 if sip == nil {
15 s1, _, _ := net.SplitHostPort(s)
16 sip = net.ParseIP(s1)
17 }
18 if sip == nil {
19 continue
20 }
21 switch family {
22 case dnsutil.IPv4Family:
23 if x := sip.To4(); x != nil {
24 return x
25 }
26 case dnsutil.IPv6Family:
27 if sip.To4() == nil {
28 return sip
29 }
30 }
31 }
32 return nil
33 }