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