Debian Codegraph @

minidns-core/src/main/java/org/minidns/dnslabel/DnsLabel.java

raw
1 /*
2 * Copyright 2015-2020 the original author or authors
3 *
4 * This software is licensed under the Apache License, Version 2.0,
5 * the GNU Lesser General Public License version 2 or later ("LGPL")
6 * and the WTFPL.
7 * You may choose either license to govern your use of this software only
8 * upon the condition that you accept all of the terms of either
9 * the Apache License 2.0, the LGPL 2.1+ or the WTFPL.
10 */
11 package org.minidns.dnslabel;
12
13 import java.io.ByteArrayOutputStream;
14 import java.nio.charset.StandardCharsets;
15 import java.util.Locale;
16
17 /**
18 * A DNS label is an individual component of a DNS name. Labels are usually shown separated by dots.
19 * <p>
20 * This class implements {@link Comparable} which compares DNS labels according to the Canonical DNS Name Order as
21 * specified in <a href="https://tools.ietf.org/html/rfc4034#section-6.1">RFC 4034 § 6.1</a>.
22 * </p>
23 * <p>
24 * Note that as per <a href="https://tools.ietf.org/html/rfc2181#section-11">RFC 2181 § 11</a> DNS labels may contain
25 * any byte.
26 * </p>
27 *
28 * @see <a href="https://tools.ietf.org/html/rfc5890#section-2.2">RFC 5890 § 2.2. DNS-Related Terminology</a>
29 * @author Florian Schmaus
30 *
31 */
32 public abstract class DnsLabel implements CharSequence, Comparable<DnsLabel> {
33
34 /**
35 * The maximum length of a DNS label in octets.
36 *
37 * @see <a href="https://tools.ietf.org/html/rfc1035">RFC 1035 § 2.3.4.</a>
38 */
39 public static final int MAX_LABEL_LENGTH_IN_OCTETS = 63;
40
41 public static final DnsLabel WILDCARD_LABEL = DnsLabel.from("*");
42
43 /**
44 * Whether or not the DNS label is validated on construction.
45 */
46 public static boolean VALIDATE = true;
47
48 public final String label;
49
50 protected DnsLabel(String label) {
51 this.label = label;
52
53 if (!VALIDATE) {
54 return;
55 }
56
57 setBytesIfRequired();
58 if (byteCache.length > MAX_LABEL_LENGTH_IN_OCTETS) {
59 throw new LabelToLongException(label);
60 }
61 }
62
63 private transient String internationalizedRepresentation;
64
65 public final String getInternationalizedRepresentation() {
66 if (internationalizedRepresentation == null) {
67 internationalizedRepresentation = getInternationalizedRepresentationInternal();
68 }
69 return internationalizedRepresentation;
70 }
71
72 protected String getInternationalizedRepresentationInternal() {
73 return label;
74 }
75
76 public final String getLabelType() {
77 return getClass().getSimpleName();
78 }
79
80 @Override
81 public final int length() {
82 return label.length();
83 }
84
85 @Override
86 public final char charAt(int index) {
87 return label.charAt(index);
88 }
89
90 @Override
91 public final CharSequence subSequence(int start, int end) {
92 return label.subSequence(start, end);
93 }
94
95 @Override
96 public final String toString() {
97 return label;
98 }
99
100 @Override
101 public final boolean equals(Object other) {
102 if (!(other instanceof DnsLabel)) {
103 return false;
104 }
105 DnsLabel otherDnsLabel = (DnsLabel) other;
106 return label.equals(otherDnsLabel.label);
107 }
108
109 @Override
110 public final int hashCode() {
111 return label.hashCode();
112 }
113
114 private transient DnsLabel lowercasedVariant;
115
116 public final DnsLabel asLowercaseVariant() {
117 if (lowercasedVariant == null) {
118 String lowercaseLabel = label.toLowerCase(Locale.US);
119 lowercasedVariant = DnsLabel.from(lowercaseLabel);
120 }
121 return lowercasedVariant;
122 }
123
124 private transient byte[] byteCache;
125
126 private void setBytesIfRequired() {
127 if (byteCache == null) {
128 byteCache = label.getBytes(StandardCharsets.US_ASCII);
129 }
130 }
131
132 public final void writeToBoas(ByteArrayOutputStream byteArrayOutputStream) {
133 setBytesIfRequired();
134
135 byteArrayOutputStream.write(byteCache.length);
136 byteArrayOutputStream.write(byteCache, 0, byteCache.length);
137 }
138
139 @Override
140 public final int compareTo(DnsLabel other) {
141 String myCanonical = asLowercaseVariant().label;
142 String otherCanonical = other.asLowercaseVariant().label;
143
144 return myCanonical.compareTo(otherCanonical);
145 }
146
147 public static DnsLabel from(String label) {
148 if (label == null || label.isEmpty()) {
149 throw new IllegalArgumentException("Label is null or empty");
150 }
151
152 if (LdhLabel.isLdhLabel(label)) {
153 return LdhLabel.fromInternal(label);
154 }
155
156 return NonLdhLabel.fromInternal(label);
157 }
158
159 public static DnsLabel[] from(String[] labels) {
160 DnsLabel[] res = new DnsLabel[labels.length];
161
162 for (int i = 0; i < labels.length; i++) {
163 res[i] = DnsLabel.from(labels[i]);
164 }
165
166 return res;
167 }
168
169 public static boolean isIdnAcePrefixed(String string) {
170 return string.toLowerCase(Locale.US).startsWith("xn--");
171 }
172
173 public static class LabelToLongException extends IllegalArgumentException {
174
175 /**
176 *
177 */
178 private static final long serialVersionUID = 1L;
179
180 public final String label;
181
182 LabelToLongException(String label) {
183 this.label = label;
184 }
185 }
186 }