Skip to content

Commit 1cca738

Browse files
committed
support ipv4 address
1 parent 2c4a51d commit 1cca738

2 files changed

Lines changed: 10 additions & 15 deletions

File tree

src/main/java/org/xbill/DNS/AAAARecord.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public class AAAARecord extends Record {
2525
*/
2626
public AAAARecord(Name name, int dclass, long ttl, InetAddress address) {
2727
super(name, Type.AAAA, dclass, ttl);
28-
if (Address.familyOf(address) != Address.IPv6) {
29-
throw new IllegalArgumentException("invalid IPv6 address");
28+
if (Address.familyOf(address) != Address.IPv4 && Address.familyOf(address) != Address.IPv6) {
29+
throw new IllegalArgumentException("invalid IPv4/IPv6 address");
3030
}
3131
this.address = address.getAddress();
3232
}
@@ -52,13 +52,7 @@ protected String rrToString() {
5252
}
5353
if (addr.getAddress().length == 4) {
5454
// Deal with Java's broken handling of mapped IPv4 addresses.
55-
StringBuilder sb = new StringBuilder("0:0:0:0:0:ffff:");
56-
int high = ((address[12] & 0xFF) << 8) + (address[13] & 0xFF);
57-
int low = ((address[14] & 0xFF) << 8) + (address[15] & 0xFF);
58-
sb.append(Integer.toHexString(high));
59-
sb.append(':');
60-
sb.append(Integer.toHexString(low));
61-
return sb.toString();
55+
return "::ffff:" + addr.getHostAddress();
6256
}
6357
return addr.getHostAddress();
6458
}

src/test/java/org/xbill/DNS/AAAARecordTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,15 @@ void ctor_4arg() {
8585

8686
// a relative name
8787
assertThrows(RelativeNameException.class, () -> new AAAARecord(m_rn, DClass.IN, m_ttl, m_addr));
88+
}
8889

89-
// an IPv4 address
90+
@Test
91+
void ctor_v4() {
9092
try {
91-
new AAAARecord(m_an, DClass.IN, m_ttl, InetAddress.getByName("192.168.0.1"));
92-
fail("IllegalArgumentException not thrown");
93-
} catch (IllegalArgumentException e) {
94-
} catch (UnknownHostException e) {
95-
fail(e.getMessage());
93+
AAAARecord ar = new AAAARecord(m_an, DClass.IN, m_ttl, InetAddress.getByName("192.168.1.1"));
94+
assertEquals("::ffff:192.168.1.1", ar.rrToString());
95+
} catch (UnknownHostException ignore){
96+
9697
}
9798
}
9899

0 commit comments

Comments
 (0)