Path: blob/master/test/jdk/com/sun/jndi/dns/lib/DNSTracer.java
41155 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import sun.security.util.HexDumpEncoder;2425import java.io.PrintStream;26import java.net.DatagramPacket;27import java.net.DatagramSocket;28import java.net.InetAddress;29import java.net.InetSocketAddress;30import java.net.SocketAddress;31import java.net.SocketException;32import java.nio.ByteBuffer;33import java.util.Arrays;3435/*36* A DNS UDP message tracer.37*38* It listens for DNS UDP requests, forward request to real DNS server, receives39* response message and sends back to requester, at same time dump all messages40* into capture file41*42* The capture file contains an DNS protocol exchange in the hexadecimal43* dump format emitted by HexDumpEncoder:44*45* xxxx: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ................46*47* Typically, the capture file data will be used by DNSServer for playback48*/49public class DNSTracer extends Thread implements Server {50public static final int DNS_DEFAULT_PORT = 53;51public static final int DNS_PACKET_SIZE = 512;52static HexDumpEncoder encoder = new HexDumpEncoder();5354private DatagramSocket inSocket;55private SocketAddress dnsServerAddress;56private ByteBuffer reqBuffer = ByteBuffer.allocate(DNS_PACKET_SIZE);57private ByteBuffer resBuffer = ByteBuffer.allocate(DNS_PACKET_SIZE);58private PrintStream out = null;59private volatile boolean isRunning;6061public DNSTracer(String dnsHostname) throws SocketException {62this(dnsHostname, DNS_DEFAULT_PORT);63}6465public DNSTracer(PrintStream outStream, String dnsHostname)66throws SocketException {67this(outStream, dnsHostname, DNS_DEFAULT_PORT);68}6970public DNSTracer(String dnsHostname, int dnsPort) throws SocketException {71this(System.out, dnsHostname, dnsPort);72}7374public DNSTracer(PrintStream outStream, String dnsHostname, int dnsPort)75throws SocketException {76inSocket = new DatagramSocket(0, InetAddress.getLoopbackAddress());77out = outStream;78dnsServerAddress = new InetSocketAddress(dnsHostname, dnsPort);79}8081public void run() {82isRunning = true;83System.out.println(84"DNSTracer: listening on port " + inSocket.getLocalPort());8586System.out.println("DNSTracer: will forward request to server "87+ dnsServerAddress);8889try (DatagramSocket outSocket = new DatagramSocket()) {90while (true) {91DatagramPacket reqPacket = new DatagramPacket(reqBuffer.array(),92reqBuffer.array().length);93inSocket.receive(reqPacket);9495out.println("-> " + reqPacket.getSocketAddress());96out.println();97// dump dns request data98out.println(encoder.encodeBuffer(99Arrays.copyOf(reqPacket.getData(),100reqPacket.getLength())));101out.println();102103outSocket.send(new DatagramPacket(reqPacket.getData(),104reqPacket.getLength(), dnsServerAddress));105DatagramPacket resPacket = new DatagramPacket(resBuffer.array(),106resBuffer.array().length);107outSocket.receive(resPacket);108109out.println("<- " + resPacket.getSocketAddress());110out.println();111// dump dns response data112out.println(encoder.encodeBuffer(113Arrays.copyOf(resPacket.getData(),114resPacket.getLength())));115out.println();116117inSocket.send(new DatagramPacket(resPacket.getData(),118resPacket.getLength(), reqPacket.getSocketAddress()));119}120} catch (SocketException se) {121if (!isRunning) {122out.flush();123System.out.println("DNSTracer: Exit");124} else {125se.printStackTrace();126}127} catch (Exception e) {128e.printStackTrace();129}130}131132@Override public void stopServer() {133isRunning = false;134if (inSocket != null) {135try {136inSocket.close();137} catch (Exception e) {138// ignore139}140}141}142143@Override public int getPort() {144if (inSocket != null) {145return inSocket.getLocalPort();146} else {147return -1;148}149}150}151152153