1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.resolver.dns;
17
18 import java.net.IDN;
19 import java.net.InetAddress;
20 import java.net.UnknownHostException;
21
22 import io.netty.buffer.ByteBuf;
23 import io.netty.buffer.ByteBufHolder;
24 import io.netty.handler.codec.dns.DnsRawRecord;
25 import io.netty.handler.codec.dns.DnsRecord;
26
27
28
29
30 final class DnsAddressDecoder {
31
32 private static final int INADDRSZ4 = 4;
33 private static final int INADDRSZ6 = 16;
34
35
36
37
38
39
40
41
42
43
44
45 static InetAddress decodeAddress(DnsRecord record, String name, boolean decodeIdn) {
46 if (!(record instanceof DnsRawRecord)) {
47 return null;
48 }
49 final ByteBuf content = ((ByteBufHolder) record).content();
50 final int contentLen = content.readableBytes();
51 if (contentLen != INADDRSZ4 && contentLen != INADDRSZ6) {
52 return null;
53 }
54
55 final byte[] addrBytes = new byte[contentLen];
56 content.getBytes(content.readerIndex(), addrBytes);
57
58 try {
59 return InetAddress.getByAddress(decodeIdn ? IDN.toUnicode(name) : name, addrBytes);
60 } catch (UnknownHostException e) {
61
62 throw new Error(e);
63 }
64 }
65
66 private DnsAddressDecoder() { }
67 }