io.netty.handler.codec.dns.DefaultDnsRawRecord Java Examples

The following examples show how to use io.netty.handler.codec.dns.DefaultDnsRawRecord. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DnsServiceEndpointGroupTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #2
Source File: DnsAddressEndpointGroupTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #3
Source File: SrvUnicastHostsProvider.java    From crate with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #4
Source File: SrvUnicastHostsProviderTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #5
Source File: DnsTextEndpointGroupTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
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);
}
 
Example #6
Source File: DnsTextEndpointGroupTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
private static DnsRecord newTooShortTxtRecord(String hostname) {
    return new DefaultDnsRawRecord(hostname, TXT, 60, Unpooled.EMPTY_BUFFER);
}
 
Example #7
Source File: DnsTextEndpointGroupTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
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
    }));
}
 
Example #8
Source File: TestDnsServer.java    From armeria with Apache License 2.0 4 votes vote down vote up
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)));
}
 
Example #9
Source File: DnsServiceEndpointGroupTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
private static DnsRecord newCnameRecord(String name, String actualName) {
    final ByteBuf content = Unpooled.buffer();
    DnsNameEncoder.encodeName(actualName, content);
    return new DefaultDnsRawRecord(name, CNAME, 60, content);
}
 
Example #10
Source File: DnsServiceEndpointGroupTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
private static DnsRecord newTooShortSrvRecord(String hostname) {
    return new DefaultDnsRawRecord(hostname, SRV, 60, Unpooled.wrappedBuffer(new byte[4]));
}
 
Example #11
Source File: DnsServiceEndpointGroupTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
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
    }));
}
 
Example #12
Source File: DnsAddressEndpointGroupTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
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);
}
 
Example #13
Source File: DnsAddressEndpointGroupTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
private static DnsRecord newBadAddressRecord(String name, boolean ipV4) {
    return new DefaultDnsRawRecord(
            name, ipV4 ? A : AAAA, 60, Unpooled.EMPTY_BUFFER);
}
 
Example #14
Source File: DnsAddressEndpointGroupTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
private static DnsRecord newCnameRecord(String name, String actualName) {
    final ByteBuf content = Unpooled.buffer();
    DnsNameEncoder.encodeName(actualName, content);
    return new DefaultDnsRawRecord(name, CNAME, 60, content);
}
 
Example #15
Source File: TcpClientSession.java    From PacketLib with MIT License 4 votes vote down vote up
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();
        }
    }
}