下面列出了 io.netty.handler.codec.dns.DefaultDnsRecordDecoder #io.netty.handler.codec.dns.DefaultDnsRawRecord 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static DnsRecord newSrvRecord(String hostname, int weight, int port, String target) {
final ByteBuf content = Unpooled.buffer();
content.writeShort(1); // priority unused
content.writeShort(weight);
content.writeShort(port);
DnsNameEncoder.encodeName(target, content);
return new DefaultDnsRawRecord(hostname, SRV, 60, content);
}
private static DnsRecord newMappedAddressRecord(String name, String ipV4Addr) {
final ByteBuf content = Unpooled.buffer();
content.writeZero(10);
content.writeShort(0xFFFF);
content.writeBytes(NetUtil.createByteArrayFromIpAddressString(ipV4Addr));
return new DefaultDnsRawRecord(name, AAAA, 60, content);
}
List<TransportAddress> parseRecords(List<DnsRecord> records) {
List<TransportAddress> addresses = new ArrayList<>(records.size());
for (DnsRecord record : records) {
if (record instanceof DefaultDnsRawRecord) {
DefaultDnsRawRecord rawRecord = (DefaultDnsRawRecord) record;
ByteBuf content = rawRecord.content();
// first is "priority", we don't use it
content.readUnsignedShort();
// second is "weight", we don't use it
content.readUnsignedShort();
int port = content.readUnsignedShort();
String hostname = DefaultDnsRecordDecoder.decodeName(content).replaceFirst("\\.$", "");
String address = hostname + ":" + port;
try {
for (TransportAddress transportAddress : transportService.addressesFromString(address)) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("adding {}, transport_address {}", address, transportAddress);
}
addresses.add(transportAddress);
}
} catch (Exception e) {
LOGGER.warn("failed to add " + address, e);
}
}
}
return addresses;
}
@Test
public void testParseRecords() {
ByteBuf buf = Unpooled.buffer();
buf.writeShort(0); // priority
buf.writeShort(0); // weight
buf.writeShort(993); // port
encodeName("localhost.", buf);
DnsRecord record = new DefaultDnsRawRecord("_myprotocol._tcp.crate.io.", DnsRecordType.SRV, 30, buf);
List<TransportAddress> addresses = srvUnicastHostsProvider.parseRecords(Collections.singletonList(record));
assertThat(addresses.get(0).getAddress(), is("127.0.0.1"));
assertThat(addresses.get(0).getPort(), is(993));
}
private static DnsRecord newTxtRecord(String hostname, String text) {
final ByteBuf content = Unpooled.buffer();
content.writeByte(text.length());
content.writeBytes(text.getBytes(StandardCharsets.US_ASCII));
return new DefaultDnsRawRecord(hostname, TXT, 60, content);
}
private static DnsRecord newTooShortTxtRecord(String hostname) {
return new DefaultDnsRawRecord(hostname, TXT, 60, Unpooled.EMPTY_BUFFER);
}
private static DnsRecord newTooLongTxtRecord(String hostname) {
return new DefaultDnsRawRecord(hostname, TXT, 60, Unpooled.wrappedBuffer(new byte[] {
1, 0, 0 // Contains one more byte than expected
}));
}
public static DnsRecord newAddressRecord(String name, String ipAddr, long ttl) {
return new DefaultDnsRawRecord(
name, NetUtil.isValidIpV4Address(ipAddr) ? A : AAAA,
ttl, Unpooled.wrappedBuffer(NetUtil.createByteArrayFromIpAddressString(ipAddr)));
}
private static DnsRecord newCnameRecord(String name, String actualName) {
final ByteBuf content = Unpooled.buffer();
DnsNameEncoder.encodeName(actualName, content);
return new DefaultDnsRawRecord(name, CNAME, 60, content);
}
private static DnsRecord newTooShortSrvRecord(String hostname) {
return new DefaultDnsRawRecord(hostname, SRV, 60, Unpooled.wrappedBuffer(new byte[4]));
}
private static DnsRecord newBadNameSrvRecord(String hostname) {
return new DefaultDnsRawRecord(hostname, SRV, 60, Unpooled.wrappedBuffer(new byte[] {
0, 0, 0, 0, 0, 0, 127, 127, 127
}));
}
private static DnsRecord newCompatibleAddressRecord(String name, String ipV4Addr) {
final ByteBuf content = Unpooled.buffer();
content.writeZero(12);
content.writeBytes(NetUtil.createByteArrayFromIpAddressString(ipV4Addr));
return new DefaultDnsRawRecord(name, AAAA, 60, content);
}
private static DnsRecord newBadAddressRecord(String name, boolean ipV4) {
return new DefaultDnsRawRecord(
name, ipV4 ? A : AAAA, 60, Unpooled.EMPTY_BUFFER);
}
private static DnsRecord newCnameRecord(String name, String actualName) {
final ByteBuf content = Unpooled.buffer();
DnsNameEncoder.encodeName(actualName, content);
return new DefaultDnsRawRecord(name, CNAME, 60, content);
}
private void resolveAddress() {
boolean debug = getFlag(BuiltinFlags.PRINT_DEBUG, false);
String name = this.getPacketProtocol().getSRVRecordPrefix() + "._tcp." + this.getHost();
if(debug) {
System.out.println("[PacketLib] Attempting SRV lookup for \"" + name + "\".");
}
AddressedEnvelope<DnsResponse, InetSocketAddress> envelope = null;
try(DnsNameResolver resolver = new DnsNameResolverBuilder(this.group.next())
.channelType(NioDatagramChannel.class)
.build()) {
envelope = resolver.query(new DefaultDnsQuestion(name, DnsRecordType.SRV)).get();
DnsResponse response = envelope.content();
if(response.count(DnsSection.ANSWER) > 0) {
DefaultDnsRawRecord record = response.recordAt(DnsSection.ANSWER, 0);
if(record.type() == DnsRecordType.SRV) {
ByteBuf buf = record.content();
buf.skipBytes(4); // Skip priority and weight.
int port = buf.readUnsignedShort();
String host = DefaultDnsRecordDecoder.decodeName(buf);
if(host.endsWith(".")) {
host = host.substring(0, host.length() - 1);
}
if(debug) {
System.out.println("[PacketLib] Found SRV record containing \"" + host + ":" + port + "\".");
}
this.host = host;
this.port = port;
} else if(debug) {
System.out.println("[PacketLib] Received non-SRV record in response.");
}
} else if(debug) {
System.out.println("[PacketLib] No SRV record found.");
}
} catch(Exception e) {
if(debug) {
System.out.println("[PacketLib] Failed to resolve SRV record.");
e.printStackTrace();
}
} finally {
if(envelope != null) {
envelope.release();
}
}
}