All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.minidns.hla.SrvResolverResult Maven / Gradle / Ivy

There is a newer version: 1.1.1
Show newest version
/*
 * Copyright 2015-2018 the original author or authors
 *
 * This software is licensed under the Apache License, Version 2.0,
 * the GNU Lesser General Public License version 2 or later ("LGPL")
 * and the WTFPL.
 * You may choose either license to govern your use of this software only
 * upon the condition that you accept all of the terms of either
 * the Apache License 2.0, the LGPL 2.1+ or the WTFPL.
 */
package org.minidns.hla;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.minidns.AbstractDnsClient.IpVersionSetting;
import org.minidns.MiniDnsException.NullResultException;
import org.minidns.dnsname.DnsName;
import org.minidns.record.A;
import org.minidns.record.AAAA;
import org.minidns.record.InternetAddressRR;
import org.minidns.record.SRV;
import org.minidns.util.SrvUtil;

public class SrvResolverResult extends ResolverResult {

    private final ResolverApi resolver;
    private final IpVersionSetting ipVersion;

    private List sortedSrvResolvedAddresses;

    SrvResolverResult(ResolverResult srvResult, ResolverApi resolver) throws NullResultException {
        super(srvResult.question, srvResult.answer, srvResult.unverifiedReasons);
        this.resolver = resolver;
        this.ipVersion = resolver.getClient().getPreferedIpVersion();
    }

    public List getSortedSrvResolvedAddresses() throws IOException {
        if (sortedSrvResolvedAddresses != null) {
            return sortedSrvResolvedAddresses;
        }

        throwIseIfErrorResponse();

        List srvRecords = SrvUtil.sortSrvRecords(getAnswers());

        List res = new ArrayList<>(srvRecords.size());
        for (SRV srvRecord : srvRecords) {
            ResolverResult aRecordsResult = null;
            ResolverResult aaaaRecordsResult = null;
            Set aRecords = Collections.emptySet();
            if (ipVersion.v4) {
                aRecordsResult = resolver.resolve(srvRecord.target, A.class);
                if (aRecordsResult.wasSuccessful() && !aRecordsResult.hasUnverifiedReasons()) {
                    aRecords = aRecordsResult.getAnswers();
                }
            }

            Set aaaaRecords = Collections.emptySet();
            if (ipVersion.v6) {
                aaaaRecordsResult = resolver.resolve(srvRecord.target, AAAA.class);
                if (aaaaRecordsResult.wasSuccessful() && !aaaaRecordsResult.hasUnverifiedReasons()) {
                    aaaaRecords = aaaaRecordsResult.getAnswers();
                }
            }

            if (aRecords.isEmpty() && aaaaRecords.isEmpty()) {
                // TODO Possibly check for (C|D)NAME usage and throw a meaningful exception that it is not allowed for
                // the target of an SRV to be an alias as per RFC 2782.
                /*
                ResolverResult cnameRecordResult = resolve(srvRecord.name, CNAME.class);
                if (cnameRecordResult.wasSuccessful()) {
                }
                */
                continue;
            }

            List srvAddresses = new ArrayList<>(aRecords.size() + aaaaRecords.size());
            switch (ipVersion) {
            case v4only:
                srvAddresses.addAll(aRecords);
                break;
            case v6only:
                srvAddresses.addAll(aaaaRecords);
                break;
            case v4v6:
                srvAddresses.addAll(aRecords);
                srvAddresses.addAll(aaaaRecords);
                break;
            case v6v4:
                srvAddresses.addAll(aaaaRecords);
                srvAddresses.addAll(aRecords);
                break;
            }

            ResolvedSrvRecord resolvedSrvAddresses = new ResolvedSrvRecord(question.name, srvRecord, srvAddresses,
                    aRecordsResult, aaaaRecordsResult);
            res.add(resolvedSrvAddresses);
        }

        sortedSrvResolvedAddresses = res;

        return res;
    }

    public static class ResolvedSrvRecord {
        public final DnsName name;
        public final SRV srv;
        public final List addresses;
        public final ResolverResult aRecordsResult;
        public final ResolverResult aaaaRecordsResult;

        /**
         * The port announced by the SRV RR. This is simply a shortcut for srv.port.
         */
        public final int port;

        private ResolvedSrvRecord(DnsName name, SRV srv, List addresses, ResolverResult aRecordsResult, ResolverResult aaaaRecordsResult) {
            this.name = name;
            this.srv = srv;
            this.addresses = Collections.unmodifiableList(addresses);
            this.port = srv.port;
            this.aRecordsResult = aRecordsResult;
            this.aaaaRecordsResult = aaaaRecordsResult;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy