author | aefimov |
Thu, 07 Nov 2019 18:46:06 +0000 | |
branch | aefimov-dns-client-branch |
changeset 58971 | 465a15dd6bed |
parent 58870 | 35c438a6d45c |
permissions | -rw-r--r-- |
58870 | 1 |
/* |
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.dns.client.internal; |
|
27 |
||
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
28 |
import jdk.dns.client.ex.DnsResolverException; |
58870 | 29 |
|
30 |
public class ResourceClassType { |
|
31 |
int rrclass; |
|
32 |
int rrtype; |
|
33 |
||
34 |
ResourceClassType(int rrtype) { |
|
35 |
this.rrclass = ResourceRecord.CLASS_INTERNET; |
|
36 |
this.rrtype = rrtype; |
|
37 |
} |
|
38 |
||
39 |
public int getRecordClass() { |
|
40 |
return rrclass; |
|
41 |
} |
|
42 |
||
43 |
public int getRecordType() { |
|
44 |
return rrtype; |
|
45 |
} |
|
46 |
||
47 |
public static ResourceClassType fromAddressFamily(AddressFamily addressFamily) { |
|
48 |
ResourceClassType[] cts; |
|
49 |
try { |
|
50 |
cts = ResourceClassType.attrIdsToClassesAndTypes(typeFromAddressFamily(addressFamily)); |
|
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
51 |
} catch (DnsResolverException e) { |
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
52 |
return new ResourceClassType(ResourceRecord.TYPE_ANY); |
58870 | 53 |
} |
54 |
return ResourceClassType.getClassAndTypeToQuery(cts); |
|
55 |
} |
|
56 |
||
57 |
private static String[] typeFromAddressFamily(AddressFamily addressFamily) { |
|
58 |
switch (addressFamily) { |
|
59 |
case IPv4: |
|
60 |
return new String[]{"A", "CNAME"}; |
|
61 |
case IPv6: |
|
62 |
return new String[]{"AAAA", "CNAME"}; |
|
63 |
default: |
|
64 |
return new String[]{"A", "AAAA", "CNAME"}; |
|
65 |
} |
|
66 |
} |
|
67 |
||
68 |
||
69 |
public static ResourceClassType[] attrIdsToClassesAndTypes(String[] attrIds) |
|
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
70 |
throws DnsResolverException { |
58870 | 71 |
if (attrIds == null) { |
72 |
return null; |
|
73 |
} |
|
74 |
ResourceClassType[] cts = new ResourceClassType[attrIds.length]; |
|
75 |
||
76 |
for (int i = 0; i < attrIds.length; i++) { |
|
77 |
cts[i] = fromAttrId(attrIds[i]); |
|
78 |
} |
|
79 |
return cts; |
|
80 |
} |
|
81 |
||
82 |
private static ResourceClassType fromAttrId(String attrId) |
|
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
83 |
throws DnsResolverException { |
58870 | 84 |
|
85 |
if (attrId.isEmpty()) { |
|
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
86 |
throw new DnsResolverException( |
58870 | 87 |
"Attribute ID cannot be empty"); |
88 |
} |
|
89 |
int rrclass; |
|
90 |
int rrtype; |
|
91 |
int space = attrId.indexOf(' '); |
|
92 |
||
93 |
// class |
|
94 |
if (space < 0) { |
|
95 |
rrclass = ResourceRecord.CLASS_INTERNET; |
|
96 |
} else { |
|
97 |
String className = attrId.substring(0, space); |
|
98 |
rrclass = ResourceRecord.getRrclass(className); |
|
99 |
if (rrclass < 0) { |
|
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
100 |
throw new DnsResolverException( |
58870 | 101 |
"Unknown resource record class '" + className + '\''); |
102 |
} |
|
103 |
} |
|
104 |
||
105 |
// type |
|
106 |
String typeName = attrId.substring(space + 1); |
|
107 |
rrtype = ResourceRecord.getType(typeName); |
|
108 |
if (rrtype < 0) { |
|
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
109 |
throw new DnsResolverException( |
58870 | 110 |
"Unknown resource record type '" + typeName + '\''); |
111 |
} |
|
112 |
||
113 |
return new ResourceClassType(rrtype); |
|
114 |
} |
|
115 |
||
116 |
/* |
|
117 |
* Returns the most restrictive resource record class and type |
|
118 |
* that may be used to query for records matching cts. |
|
119 |
* See classAndTypeMatch() for matching rules. |
|
120 |
*/ |
|
121 |
public static ResourceClassType getClassAndTypeToQuery(ResourceClassType[] cts) { |
|
122 |
int rrclass; |
|
123 |
int rrtype; |
|
124 |
||
125 |
if (cts == null) { |
|
126 |
// Query all records. |
|
127 |
throw new RuntimeException("Internal DNS resolver error"); |
|
128 |
} else if (cts.length == 0) { |
|
129 |
// No records are requested, but we need to ask for something. |
|
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
130 |
rrtype = ResourceRecord.TYPE_ANY; |
58870 | 131 |
} else { |
132 |
rrclass = ResourceRecord.CLASS_INTERNET; |
|
133 |
rrtype = cts[0].rrtype; |
|
134 |
for (int i = 1; i < cts.length; i++) { |
|
135 |
if (rrclass != cts[i].rrclass) { |
|
136 |
throw new RuntimeException("Internal error: Only CLASS_INTERNET is supported"); |
|
137 |
} |
|
138 |
if (rrtype != cts[i].rrtype) { |
|
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
139 |
rrtype = ResourceRecord.TYPE_ANY; |
58870 | 140 |
} |
141 |
} |
|
142 |
} |
|
143 |
return new ResourceClassType(rrtype); |
|
144 |
} |
|
145 |
} |