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