-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathNAPTRRecord.java
More file actions
188 lines (168 loc) · 5.31 KB
/
Copy pathNAPTRRecord.java
File metadata and controls
188 lines (168 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2000-2004 Brian Wellington (bwelling@xbill.org)
package org.xbill.DNS;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
/**
* Name Authority Pointer Record - specifies rewrite rule, that when applied to an existing string
* will produce a new domain.
*
* @author Chuck Santos
* @see <a href="https://datatracker.ietf.org/doc/html/rfc3403">RFC 3403: Dynamic Delegation
* Discovery System (DDDS) Part Three: The Domain Name System (DNS) Database</a>
*/
public class NAPTRRecord extends Record {
private int order;
private int preference;
private byte[] flags;
private byte[] service;
private byte[] regexp;
private Name replacement;
NAPTRRecord() {}
/**
* Creates an NAPTR Record from the given data
*
* @param order The order of this NAPTR. Records with lower order are preferred.
* @param preference The preference, used to select between records at the same order.
* @param flags The control aspects of the NAPTRRecord.
* @param service The service or protocol available down the rewrite path.
* @param regexp The regular/substitution expression.
* @param replacement The domain-name to query for the next DNS resource record, depending on the
* value of the flags field.
* @throws IllegalArgumentException One of the strings has invalid escapes
*/
public NAPTRRecord(
Name name,
int dclass,
long ttl,
int order,
int preference,
String flags,
String service,
String regexp,
Name replacement) {
super(name, Type.NAPTR, dclass, ttl);
this.order = checkU16("order", order);
this.preference = checkU16("preference", preference);
try {
this.flags = byteArrayFromString(flags);
this.service = byteArrayFromString(service);
this.regexp = byteArrayFromString(regexp);
} catch (TextParseException e) {
throw new IllegalArgumentException(e.getMessage());
}
this.replacement = checkName("replacement", replacement);
}
@Override
protected void rrFromWire(DNSInput in) throws IOException {
order = in.readU16();
preference = in.readU16();
flags = in.readCountedString();
service = in.readCountedString();
regexp = in.readCountedString();
replacement = new Name(in);
}
@Override
protected void rdataFromString(Tokenizer st, Name origin) throws IOException {
order = st.getUInt16();
preference = st.getUInt16();
try {
flags = byteArrayFromString(st.getString());
service = byteArrayFromString(st.getString());
regexp = byteArrayFromString(st.getString());
} catch (TextParseException e) {
throw st.exception(e.getMessage());
}
replacement = st.getName(origin);
}
/** Converts rdata to a String */
@Override
protected String rrToString() {
StringBuilder sb = new StringBuilder();
sb.append(order);
sb.append(" ");
sb.append(preference);
sb.append(" ");
sb.append(byteArrayToString(flags, true));
sb.append(" ");
sb.append(byteArrayToString(service, true));
sb.append(" ");
sb.append(byteArrayToString(regexp, true));
sb.append(" ");
sb.append(replacement);
return sb.toString();
}
/** Returns the order */
public int getOrder() {
return order;
}
/** Returns the preference */
public int getPreference() {
return preference;
}
/** Returns flags */
public String getFlags() {
return new String(flags, StandardCharsets.US_ASCII);
}
/**
* Returns the service as a string.
*
* @param escape if true, returns the RR textual representation of the underlying bytes. If false,
* returns just the simple string using the UTF-8 charset with no additional escaping.
* @since 3.6.5
*/
public String getService(boolean escape) {
return escape ? byteArrayToString(service, false) : new String(service, StandardCharsets.UTF_8);
}
/** Returns the service as a string, escaped for RR textual representation */
public String getService() {
return getService(true);
}
/**
* Returns the service as a raw byte-array
*
* @since 3.6.5
*/
public byte[] getServiceAsByteArray() {
return service;
}
/**
* Returns regexp as a string.
*
* @param escape if true, returns the RR textual representation of the underlying bytes. If false,
* returns just the simple string using the UTF-8 charset with no additional escaping.
* @since 3.6.5
*/
public String getRegexp(boolean escape) {
return escape ? byteArrayToString(regexp, false) : new String(regexp, StandardCharsets.UTF_8);
}
/** Returns regexp as a string, escaped for RR textual representation */
public String getRegexp() {
return getRegexp(true);
}
/**
* Returns regexp as a raw byte-array
*
* @since 3.6.5
*/
public byte[] getRegexpAsByteArray() {
return regexp;
}
/** Returns the replacement domain-name */
public Name getReplacement() {
return replacement;
}
@Override
protected void rrToWire(DNSOutput out, Compression c, boolean canonical) {
out.writeU16(order);
out.writeU16(preference);
out.writeCountedString(flags);
out.writeCountedString(service);
out.writeCountedString(regexp);
replacement.toWire(out, null, canonical);
}
@Override
public Name getAdditionalName() {
return replacement;
}
}