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

The following examples show how to use io.netty.handler.codec.dns.DefaultDnsRecordDecoder. 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: DnsNameResolverContext.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
static String decodeDomainName(ByteBuf in) {
    in.markReaderIndex();
    try {
        return DefaultDnsRecordDecoder.decodeName(in);
    } catch (CorruptedFrameException e) {
        // In this case we just return null.
        return null;
    } finally {
        in.resetReaderIndex();
    }
}
 
Example #2
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 #3
Source File: DnsServiceEndpointGroup.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
ImmutableSortedSet<Endpoint> onDnsRecords(List<DnsRecord> records, int ttl) throws Exception {
    final ImmutableSortedSet.Builder<Endpoint> builder = ImmutableSortedSet.naturalOrder();
    for (DnsRecord r : records) {
        if (!(r instanceof DnsRawRecord) || r.type() != DnsRecordType.SRV) {
            continue;
        }

        final ByteBuf content = ((ByteBufHolder) r).content();
        if (content.readableBytes() <= 6) { // Too few bytes
            warnInvalidRecord(DnsRecordType.SRV, content);
            continue;
        }

        content.markReaderIndex();
        content.skipBytes(2);  // priority unused
        final int weight = content.readUnsignedShort();
        final int port = content.readUnsignedShort();

        final Endpoint endpoint;
        try {
            final String target = stripTrailingDot(DefaultDnsRecordDecoder.decodeName(content));
            endpoint = port > 0 ? Endpoint.of(target, port) : Endpoint.of(target);
        } catch (Exception e) {
            content.resetReaderIndex();
            warnInvalidRecord(DnsRecordType.SRV, content);
            continue;
        }

        builder.add(endpoint.withWeight(weight));
    }

    final ImmutableSortedSet<Endpoint> endpoints = builder.build();
    if (logger().isDebugEnabled()) {
        logger().debug("{} Resolved: {} (TTL: {})",
                       logPrefix(),
                       endpoints.stream()
                                .map(e -> e.authority() + '/' + e.weight())
                                .collect(Collectors.joining(", ")),
                       ttl);
    }

    return endpoints;
}
 
Example #4
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();
        }
    }
}