001/*
002 * Copyright 2015-2020 the original author or authors
003 *
004 * This software is licensed under the Apache License, Version 2.0,
005 * the GNU Lesser General Public License version 2 or later ("LGPL")
006 * and the WTFPL.
007 * You may choose either license to govern your use of this software only
008 * upon the condition that you accept all of the terms of either
009 * the Apache License 2.0, the LGPL 2.1+ or the WTFPL.
010 */
011package org.minidns.dnsname;
012
013import java.io.ByteArrayOutputStream;
014import java.io.DataInputStream;
015import java.io.IOException;
016import java.io.OutputStream;
017import java.io.Serializable;
018import java.nio.charset.StandardCharsets;
019import java.util.Arrays;
020import java.util.HashSet;
021import java.util.Locale;
022
023import org.minidns.dnslabel.DnsLabel;
024import org.minidns.idna.MiniDnsIdna;
025
026/**
027 * A DNS name, also called "domain name". A DNS name consists of multiple 'labels' and is subject to certain restrictions (see
028 * for example <a href="https://tools.ietf.org/html/rfc3696#section-2">RFC 3696 § 2.</a>).
029 * <p>
030 * Instances of this class can be created by using {@link #from(String)}.
031 * </p>
032 * <p>
033 * This class holds three representations of a DNS name: ACE, raw ACE and IDN. ACE (ASCII Compatible Encoding), which
034 * can be accessed via {@link #ace}, represents mostly the data that got send over the wire. But since DNS names are
035 * case insensitive, the ACE value is normalized to lower case. You can use {@link #getRawAce()} to get the raw ACE data
036 * that was received, which possibly includes upper case characters. The IDN (Internationalized Domain Name), that is
037 * the DNS name as it should be shown to the user, can be retrieved using {@link #asIdn()}.
038 * </p>
039 * More information about Internationalized Domain Names can be found at:
040 * <ul>
041 * <li><a href="https://unicode.org/reports/tr46/">UTS #46 - Unicode IDNA Compatibility Processing</a>
042 * <li><a href="https://tools.ietf.org/html/rfc8753">RFC 8753 - Internationalized Domain Names for Applications (IDNA) Review for New Unicode Versions</a>
043 * </ul>
044 *
045 * @see <a href="https://tools.ietf.org/html/rfc3696">RFC 3696</a>
046 * @author Florian Schmaus
047 *
048 */
049public final class DnsName implements CharSequence, Serializable, Comparable<DnsName> {
050
051    /**
052     * 
053     */
054    private static final long serialVersionUID = 1L;
055
056    /**
057     * @see <a href="https://www.ietf.org/rfc/rfc3490.txt">RFC 3490 § 3.1 1.</a>
058     */
059    private static final String LABEL_SEP_REGEX = "[.\u3002\uFF0E\uFF61]";
060
061    /**
062     * @see <a href="https://tools.ietf.org/html/rfc1035">RFC 1035 § 2.3.4.</a<
063     */
064    static final int MAX_DNSNAME_LENGTH_IN_OCTETS = 255;
065
066    public static final int MAX_LABELS = 128;
067
068    public static final DnsName ROOT = new DnsName(".");
069
070    public static final DnsName IN_ADDR_ARPA = new DnsName("in-addr.arpa");
071
072    public static final DnsName IP6_ARPA = new DnsName("ip6.arpa");
073
074    /**
075     * Whether or not the DNS name is validated on construction.
076     */
077    public static boolean VALIDATE = true;
078
079    /**
080     * The DNS name in ASCII Compatible Encoding (ACE).
081     */
082    public final String ace;
083
084    /**
085     * The DNS name in raw format, i.e. as it was received from the remote server. This means that compared to
086     * {@link #ace}, this String may not be lower-cased.
087     */
088    private final String rawAce;
089
090    private transient byte[] bytes;
091
092    private transient byte[] rawBytes;
093
094    private transient String idn;
095
096    private transient String domainpart;
097
098    private transient String hostpart;
099
100    /**
101     * The labels in <b>reverse</b> order.
102     */
103    private transient DnsLabel[] labels;
104
105    private transient DnsLabel[] rawLabels;
106
107    private transient int hashCode;
108
109    private int size = -1;
110
111    private DnsName(String name) {
112        this(name, true);
113    }
114
115    private DnsName(String name, boolean inAce) {
116        if (name.isEmpty()) {
117            rawAce = ROOT.rawAce;
118        } else {
119            final int nameLength = name.length();
120            final int nameLastPos = nameLength - 1;
121
122            // Strip potential trailing dot. N.B. that we require nameLength > 2, because we don't want to strip the one
123            // character string containing only a single dot to the empty string.
124            if (nameLength >= 2 && name.charAt(nameLastPos) == '.') {
125                name = name.subSequence(0, nameLastPos).toString();
126            }
127
128            if (inAce) {
129                // Name is already in ACE format.
130                rawAce = name;
131            } else {
132                rawAce = MiniDnsIdna.toASCII(name);
133            }
134        }
135
136        ace = rawAce.toLowerCase(Locale.US);
137
138        if (!VALIDATE) {
139            return;
140        }
141
142        // Validate the DNS name.
143        validateMaxDnsnameLengthInOctets();
144    }
145
146    private DnsName(DnsLabel[] rawLabels, boolean validateMaxDnsnameLength) {
147        this.rawLabels = rawLabels;
148        this.labels = new DnsLabel[rawLabels.length];
149
150        int size = 0;
151        for (int i = 0; i < rawLabels.length; i++) {
152            size += rawLabels[i].length() + 1;
153            labels[i] = rawLabels[i].asLowercaseVariant();
154        }
155
156        rawAce = labelsToString(rawLabels, size);
157        ace    = labelsToString(labels,    size);
158
159        // The following condition is deliberately designed that VALIDATE=false causes the validation to be skipped even
160        // if validateMaxDnsnameLength is set to true. There is no need to validate even if this constructor is called
161        // with validateMaxDnsnameLength set to true if VALIDATE is globally set to false.
162        if (!validateMaxDnsnameLength || !VALIDATE) {
163            return;
164        }
165
166        validateMaxDnsnameLengthInOctets();
167    }
168
169    private static String labelsToString(DnsLabel[] labels, int stringLength) {
170        StringBuilder sb = new StringBuilder(stringLength);
171        for (int i = labels.length - 1; i >= 0; i--) {
172            sb.append(labels[i]).append('.');
173        }
174        sb.setLength(sb.length() - 1);
175        return sb.toString();
176    }
177
178    private void validateMaxDnsnameLengthInOctets() {
179        setBytesIfRequired();
180        if (bytes.length > MAX_DNSNAME_LENGTH_IN_OCTETS) {
181            throw new InvalidDnsNameException.DNSNameTooLongException(ace, bytes);
182        }
183    }
184
185    public void writeToStream(OutputStream os) throws IOException {
186        setBytesIfRequired();
187        os.write(bytes);
188    }
189
190    /**
191     * Serialize a domain name under IDN rules.
192     *
193     * @return The binary domain name representation.
194     */
195    public byte[] getBytes() {
196        setBytesIfRequired();
197        return bytes.clone();
198    }
199
200    public byte[] getRawBytes() {
201        if (rawBytes == null) {
202            setLabelsIfRequired();
203            rawBytes = toBytes(rawLabels);
204        }
205
206        return rawBytes.clone();
207    }
208
209    private void setBytesIfRequired() {
210        if (bytes != null)
211            return;
212
213        setLabelsIfRequired();
214        bytes = toBytes(labels);
215    }
216
217    private static byte[] toBytes(DnsLabel[] labels) {
218        ByteArrayOutputStream baos = new ByteArrayOutputStream(64);
219        for (int i = labels.length - 1; i >= 0; i--) {
220            labels[i].writeToBoas(baos);
221        }
222
223        baos.write(0);
224
225        assert baos.size() <= MAX_DNSNAME_LENGTH_IN_OCTETS;
226
227        return baos.toByteArray();
228    }
229
230    private void setLabelsIfRequired() {
231        if (labels != null && rawLabels != null) return;
232
233        if (isRootLabel()) {
234            rawLabels = labels = new DnsLabel[0];
235            return;
236        }
237
238        labels = getLabels(ace);
239        rawLabels = getLabels(rawAce);
240    }
241
242    private static DnsLabel[] getLabels(String ace) {
243        String[] labels = ace.split(LABEL_SEP_REGEX, MAX_LABELS);
244
245        // Reverse the labels, so that 'foo, example, org' becomes 'org, example, foo'.
246        for (int i = 0; i < labels.length / 2; i++) {
247            String t = labels[i];
248            int j = labels.length - i - 1;
249            labels[i] = labels[j];
250            labels[j] = t;
251        }
252
253        try {
254            return DnsLabel.from(labels);
255        } catch (DnsLabel.LabelToLongException e) {
256            throw new InvalidDnsNameException.LabelTooLongException(ace, e.label);
257        }
258    }
259
260    public String getRawAce() {
261        return rawAce;
262    }
263
264    public String asIdn() {
265        if (idn != null)
266            return idn;
267
268        idn = MiniDnsIdna.toUnicode(ace);
269        return idn;
270    }
271
272    /**
273     * Domainpart in ACE representation.
274     *
275     * @return the domainpart in ACE representation.
276     */
277    public String getDomainpart() {
278        setHostnameAndDomainpartIfRequired();
279        return domainpart;
280    }
281
282    /**
283     * Hostpart in ACE representation.
284     *
285     * @return the hostpart in ACE representation.
286     */
287    public String getHostpart() {
288        setHostnameAndDomainpartIfRequired();
289        return hostpart;
290    }
291
292    public DnsLabel getHostpartLabel() {
293        setLabelsIfRequired();
294        return labels[labels.length];
295    }
296
297    private void setHostnameAndDomainpartIfRequired() {
298        if (hostpart != null) return;
299
300        String[] parts = ace.split(LABEL_SEP_REGEX, 2);
301        hostpart = parts[0];
302        if (parts.length > 1) {
303            domainpart = parts[1];
304        } else {
305            domainpart = "";
306        }
307    }
308
309    public int size() {
310        if (size < 0) {
311            if (isRootLabel()) {
312                size = 1;
313            } else {
314                size = ace.length() + 2;
315            }
316        }
317        return size;
318    }
319
320    @Override
321    public int length() {
322        return ace.length();
323    }
324
325    @Override
326    public char charAt(int index) {
327        return ace.charAt(index);
328    }
329
330    @Override
331    public CharSequence subSequence(int start, int end) {
332        return ace.subSequence(start, end);
333    }
334
335    @Override
336    public String toString() {
337        return ace;
338    }
339
340    public static DnsName from(CharSequence name) {
341        return from(name.toString());
342    }
343
344    public static DnsName from(String name) {
345        return new DnsName(name, false);
346    }
347
348    /**
349     * Create a DNS name by "concatenating" the child under the parent name. The child can also be seen as the "left"
350     * part of the resulting DNS name and the parent is the "right" part.
351     * <p>
352     * For example using "i.am.the.child" as child and "of.this.parent.example" as parent, will result in a DNS name:
353     * "i.am.the.child.of.this.parent.example".
354     * </p>
355     *
356     * @param child the child DNS name.
357     * @param parent the parent DNS name.
358     * @return the resulting of DNS name.
359     */
360    public static DnsName from(DnsName child, DnsName parent) {
361        child.setLabelsIfRequired();
362        parent.setLabelsIfRequired();
363
364        DnsLabel[] rawLabels = new DnsLabel[child.rawLabels.length + parent.rawLabels.length];
365        System.arraycopy(parent.rawLabels, 0, rawLabels, 0, parent.rawLabels.length);
366        System.arraycopy(child.rawLabels, 0, rawLabels, parent.rawLabels.length, child.rawLabels.length);
367        return new DnsName(rawLabels, true);
368    }
369
370    public static DnsName from(DnsLabel child, DnsName parent) {
371        parent.setLabelsIfRequired();
372
373        DnsLabel[] rawLabels = new DnsLabel[parent.rawLabels.length + 1];
374        System.arraycopy(parent.rawLabels, 0, rawLabels, 0, parent.rawLabels.length);
375        rawLabels[rawLabels.length] = child;
376        return new DnsName(rawLabels, true);
377    }
378
379    public static DnsName from(DnsLabel grandchild, DnsLabel child, DnsName parent) {
380        parent.setBytesIfRequired();
381
382        DnsLabel[] rawLabels = new DnsLabel[parent.rawLabels.length + 2];
383        System.arraycopy(parent.rawLabels, 0, rawLabels, 0, parent.rawLabels.length);
384        rawLabels[parent.rawLabels.length] = child;
385        rawLabels[parent.rawLabels.length + 1] = grandchild;
386        return new DnsName(rawLabels, true);
387    }
388
389    public static DnsName from(DnsName... nameComponents) {
390        int labelCount = 0;
391        for (DnsName component : nameComponents) {
392            component.setLabelsIfRequired();
393            labelCount += component.rawLabels.length;
394        }
395
396        DnsLabel[] rawLabels = new DnsLabel[labelCount];
397        int destLabelPos = 0;
398        for (int i = nameComponents.length - 1; i >= 0; i--) {
399            DnsName component = nameComponents[i];
400            System.arraycopy(component.rawLabels, 0, rawLabels, destLabelPos, component.rawLabels.length);
401            destLabelPos += component.rawLabels.length;
402        }
403
404        return new DnsName(rawLabels, true);
405    }
406
407    public static DnsName from(String[] parts) {
408        DnsLabel[] rawLabels = DnsLabel.from(parts);
409
410        return new DnsName(rawLabels, true);
411    }
412
413    /**
414     * Parse a domain name starting at the current offset and moving the input
415     * stream pointer past this domain name (even if cross references occure).
416     *
417     * @param dis  The input stream.
418     * @param data The raw data (for cross references).
419     * @return The domain name string.
420     * @throws IOException Should never happen.
421     */
422    public static DnsName parse(DataInputStream dis, byte[] data)
423            throws IOException {
424        int c = dis.readUnsignedByte();
425        if ((c & 0xc0) == 0xc0) {
426            c = ((c & 0x3f) << 8) + dis.readUnsignedByte();
427            HashSet<Integer> jumps = new HashSet<Integer>();
428            jumps.add(c);
429            return parse(data, c, jumps);
430        }
431        if (c == 0) {
432            return DnsName.ROOT;
433        }
434        byte[] b = new byte[c];
435        dis.readFully(b);
436
437        String childLabelString = new String(b, StandardCharsets.US_ASCII);
438        DnsName child = new DnsName(childLabelString);
439
440        DnsName parent = parse(dis, data);
441        return DnsName.from(child, parent);
442    }
443
444    /**
445     * Parse a domain name starting at the given offset.
446     *
447     * @param data   The raw data.
448     * @param offset The offset.
449     * @param jumps  The list of jumps (by now).
450     * @return The parsed domain name.
451     * @throws IllegalStateException on cycles.
452     */
453    private static DnsName parse(byte[] data, int offset, HashSet<Integer> jumps)
454            throws IllegalStateException {
455        int c = data[offset] & 0xff;
456        if ((c & 0xc0) == 0xc0) {
457            c = ((c & 0x3f) << 8) + (data[offset + 1] & 0xff);
458            if (jumps.contains(c)) {
459                throw new IllegalStateException("Cyclic offsets detected.");
460            }
461            jumps.add(c);
462            return parse(data, c, jumps);
463        }
464        if (c == 0) {
465            return DnsName.ROOT;
466        }
467
468        String childLabelString = new String(data, offset + 1, c, StandardCharsets.US_ASCII);
469        DnsName child = new DnsName(childLabelString);
470
471        DnsName parent = parse(data, offset + 1 + c, jumps);
472        return DnsName.from(child, parent);
473    }
474
475    @Override
476    public int compareTo(DnsName other) {
477        return ace.compareTo(other.ace);
478    }
479
480    @Override
481    public boolean equals(Object other) {
482        if (other == null) return false;
483
484        if (other instanceof DnsName) {
485            DnsName otherDnsName = (DnsName) other;
486            setBytesIfRequired();
487            otherDnsName.setBytesIfRequired();
488            return Arrays.equals(bytes, otherDnsName.bytes);
489        }
490
491        return false;
492    }
493
494    @Override
495    public int hashCode() {
496        if (hashCode == 0 && !isRootLabel()) {
497            setBytesIfRequired();
498            hashCode = Arrays.hashCode(bytes);
499        }
500        return hashCode;
501    }
502
503    public boolean isDirectChildOf(DnsName parent) {
504        setLabelsIfRequired();
505        parent.setLabelsIfRequired();
506        int parentLabelsCount = parent.labels.length;
507
508        if (labels.length - 1 != parentLabelsCount)
509            return false;
510
511        for (int i = 0; i < parent.labels.length; i++) {
512            if (!labels[i].equals(parent.labels[i]))
513                return false;
514        }
515
516        return true;
517    }
518
519    public boolean isChildOf(DnsName parent) {
520        setLabelsIfRequired();
521        parent.setLabelsIfRequired();
522
523        if (labels.length < parent.labels.length)
524            return false;
525
526        for (int i = 0; i < parent.labels.length; i++) {
527            if (!labels[i].equals(parent.labels[i]))
528                return false;
529        }
530
531        return true;
532    }
533
534    public int getLabelCount() {
535        setLabelsIfRequired();
536        return labels.length;
537    }
538
539    /**
540     * Get a copy of the labels of this DNS name. The resulting array will contain the labels in reverse order, that is,
541     * the top-level domain will be at res[0].
542     *
543     * @return an array of the labels in reverse order.
544     */
545    public DnsLabel[] getLabels() {
546        setLabelsIfRequired();
547        return labels.clone();
548    }
549
550
551    public DnsLabel getLabel(int labelNum) {
552        setLabelsIfRequired();
553        return labels[labelNum];
554    }
555
556    /**
557     * Get a copy of the raw labels of this DNS name. The resulting array will contain the labels in reverse order, that is,
558     * the top-level domain will be at res[0].
559     *
560     * @return an array of the raw labels in reverse order.
561     */
562    public DnsLabel[] getRawLabels() {
563        setLabelsIfRequired();
564        return rawLabels.clone();
565    }
566
567    public DnsName stripToLabels(int labelCount) {
568        setLabelsIfRequired();
569
570        if (labelCount > labels.length) {
571            throw new IllegalArgumentException();
572        }
573
574        if (labelCount == labels.length) {
575            return this;
576        }
577
578        if (labelCount == 0) {
579            return ROOT;
580        }
581
582        DnsLabel[] stripedLabels = Arrays.copyOfRange(rawLabels, 0, labelCount);
583
584        return new DnsName(stripedLabels, false);
585    }
586
587    /**
588     * Return the parent of this DNS label. Will return the root label if this label itself is the root label (because there is no parent of root).
589     * <p>
590     * For example:
591     * </p>
592     * <ul>
593     *  <li><code>"foo.bar.org".getParent() == "bar.org"</code></li>
594     *  <li><code> ".".getParent() == "."</code></li>
595     * </ul>
596     * @return the parent of this DNS label.
597     */
598    public DnsName getParent() {
599        if (isRootLabel()) return ROOT;
600        return stripToLabels(getLabelCount() - 1);
601    }
602
603    public boolean isRootLabel() {
604        return ace.isEmpty() || ace.equals(".");
605    }
606}