1package dns
2
3import (
4 "net"
5)
6
7// IPDomain is an ip address, a domain, or empty.
8type IPDomain struct {
9 IP net.IP
10 Domain Domain
11}
12
13// IsZero returns if both IP and Domain are zero.
14func (d IPDomain) IsZero() bool {
15 return d.IP == nil && d.Domain == Domain{}
16}
17
18// String returns a string representation of either the IP or domain (with
19// UTF-8).
20func (d IPDomain) String() string {
21 if len(d.IP) > 0 {
22 return d.IP.String()
23 }
24 return d.Domain.Name()
25}
26
27// LogString returns a string with both ASCII-only and optional UTF-8
28// representation.
29func (d IPDomain) LogString() string {
30 if len(d.IP) > 0 {
31 return d.IP.String()
32 }
33 return d.Domain.LogString()
34}
35
36// XString is like String, but only returns UTF-8 domains if utf8 is true.
37func (d IPDomain) XString(utf8 bool) string {
38 if d.IsIP() {
39 // todo: check callers if this is valid syntax for them. should we add [] for ipv6? perhaps also ipv4? probably depends on context. in smtp, the syntax is [<ipv4>] and [IPv6:<ipv6>].
40 return d.IP.String()
41 }
42 return d.Domain.XName(utf8)
43}
44
45func (d IPDomain) IsIP() bool {
46 return len(d.IP) > 0
47}
48
49func (d IPDomain) IsDomain() bool {
50 return !d.Domain.IsZero()
51}
52