1package de.gultsch.minidns;
2
3import androidx.annotation.NonNull;
4import com.google.common.base.MoreObjects;
5import com.google.common.base.Objects;
6import com.google.common.base.Preconditions;
7import com.google.common.collect.Iterables;
8import java.net.InetAddress;
9import java.util.Arrays;
10import java.util.Collections;
11import java.util.List;
12
13public final class DNSServer {
14
15 public final InetAddress inetAddress;
16 public final String hostname;
17 public final int port;
18 public final List<Transport> transports;
19
20 public DNSServer(InetAddress inetAddress, Integer port, Transport transport) {
21 this.inetAddress = inetAddress;
22 this.port = port == null ? 0 : port;
23 this.transports = Collections.singletonList(transport);
24 this.hostname = null;
25 }
26
27 public DNSServer(final String hostname, final Integer port, final Transport transport) {
28 Preconditions.checkArgument(
29 Arrays.asList(Transport.HTTPS, Transport.TLS).contains(transport),
30 "hostname validation only works with TLS based transports");
31 this.hostname = hostname;
32 this.port = port == null ? 0 : port;
33 this.transports = Collections.singletonList(transport);
34 this.inetAddress = null;
35 }
36
37 public DNSServer(final String hostname, final Transport transport) {
38 this(hostname, Transport.DEFAULT_PORTS.get(transport), transport);
39 }
40
41 public DNSServer(InetAddress inetAddress, Transport transport) {
42 this(inetAddress, Transport.DEFAULT_PORTS.get(transport), transport);
43 }
44
45 public DNSServer(final InetAddress inetAddress) {
46 this(inetAddress, 53, Arrays.asList(Transport.UDP, Transport.TCP));
47 }
48
49 public DNSServer(final InetAddress inetAddress, int port, List<Transport> transports) {
50 this(inetAddress, null, port, transports);
51 }
52
53 private DNSServer(
54 final InetAddress inetAddress,
55 final String hostname,
56 final int port,
57 final List<Transport> transports) {
58 this.inetAddress = inetAddress;
59 this.hostname = hostname;
60 this.port = port;
61 this.transports = transports;
62 }
63
64 public Transport uniqueTransport() {
65 return Iterables.getOnlyElement(this.transports);
66 }
67
68 public DNSServer asUniqueTransport(final Transport transport) {
69 Preconditions.checkArgument(
70 this.transports.contains(transport),
71 "This DNS server does not have transport ",
72 transport);
73 return new DNSServer(inetAddress, hostname, port, Collections.singletonList(transport));
74 }
75
76 @Override
77 @NonNull
78 public String toString() {
79 return MoreObjects.toStringHelper(this)
80 .add("inetAddress", inetAddress)
81 .add("hostname", hostname)
82 .add("port", port)
83 .add("transports", transports)
84 .toString();
85 }
86
87 @Override
88 public boolean equals(Object o) {
89 if (this == o) return true;
90 if (o == null || getClass() != o.getClass()) return false;
91 DNSServer dnsServer = (DNSServer) o;
92 return port == dnsServer.port
93 && Objects.equal(inetAddress, dnsServer.inetAddress)
94 && Objects.equal(hostname, dnsServer.hostname)
95 && Objects.equal(transports, dnsServer.transports);
96 }
97
98 @Override
99 public int hashCode() {
100 return Objects.hashCode(inetAddress, hostname, port, transports);
101 }
102}