DNS响应报文解析

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

public class SocketChannelInput extends AbstractInput {

	private SocketChannel channel;
	
	public SocketChannelInput(SocketChannel channel) {
		this.channel = channel;
	}
	
	private ByteBuffer readBuffer(int bytes) throws IOException {
		ByteBuffer dst = ByteBuffer.allocate(bytes);
		while (dst.hasRemaining()) {
			channel.read(dst);
		}
		dst.rewind();
		return dst;
	}
	
	private int readLength() throws IOException {
		ByteBuffer dst = readBuffer(2);
		return dst.getShort();
	}
	
	private ByteBuffer readMessage(int length) throws IOException {
		return readBuffer(length);
	}
	
	/**
	 * question section
	 */
	private void readSection2(ByteBuffer src, int n) {
		for (int i = 0; i < n; i++) {
			StringBuffer qname = new StringBuffer();
			int ll = 0;
			while ((ll = src.get()) != 0) {
				qname.append(ll);
				for (int j = 0; j < ll; j++) {
					qname.append((char) src.get());
				}
			}
			qname.append(0);
			
			int qtype = src.getShort();
			
			int qclass = src.getShort();
			
			Entry entry = new Entry(qname.toString(), (short) qtype, (short) qclass);
			System.out.println(ToStringBuilder.reflectionToString(entry, ToStringStyle.MULTI_LINE_STYLE));
		}
	}
	
	/**
	 * resource record
	 */
	private void readRecord(ByteBuffer src) {
		String name = null;
		int prefix = src.getShort();
		int flag = (prefix << 16) >>> 30;
		if (flag == 3) {
			int offset = (prefix << 18) >>> 18;
			
			int position = src.position();
			src.position(offset);
			
			StringBuffer sb = new StringBuffer();
			int ll = 0;
			while ((ll = src.get()) != 0) {
				sb.append(ll);
				for (int j = 0; j < ll; j++) {
					sb.append((char) src.get());
				}
			}
			sb.append(0);
			
			
			name = sb.toString();
			src.position(position);
		} else {
			
		}
		System.out.println(name);
		
		int type = src.getShort();
		System.out.println(type);
		
		int clazz = src.getShort();
		System.out.println(clazz);
		
		int ttl = src.getInt();
		System.out.println(ttl);
		
		int length = src.getShort();
		System.out.println(length);
		
		
		StringBuffer data = new StringBuffer();
		
		ByteBuffer db = readRecordData(src, length);
		int position = db.position();
		prefix = db.getShort();
		db.position(position);
		flag = (prefix << 16) >>> 30;
		if (flag == 3) {
			int offset = (prefix << 18) >>> 18;
			
			position = src.position();
			src.position(offset);
			
			int ll = 0;
			while ((ll = src.get()) != 0) {
				data.append(ll);
				for (int j = 0; j < ll; j++) {
					data.append((char) src.get());
				}
			}
			data.append(0);
			src.position(position);
		}
		
//		position = db.position();
		int suffix = db.getShort(db.limit() - 2);
		int term = db.get(db.limit() - 2);
//		db.position(position);
		flag = (suffix << 16) >>> 30;
		if (flag == 3) {
			int ll = 0;
			while ((ll = db.get()) != term) {
				data.append(ll);
				for (int j = 0; j < ll; j++) {
					data.append((char) db.get());
				}
			}
			
			int offset = (suffix << 18) >>> 18;
			
			position = src.position();
			src.position(offset);
			
			ll = 0;
			while ((ll = src.get()) != 0) {
				data.append(ll);
				for (int j = 0; j < ll; j++) {
					data.append((char) src.get());
				}
			}
			data.append(0);
			src.position(position);
		}
		
		flag = db.get(db.limit() - 1);
		if (flag == 0) {
			int ll = 0;
			while ((ll = db.get()) != 0) {
				data.append(ll);
				for (int j = 0; j < ll; j++) {
					data.append((char) db.get());
				}
			}
			data.append(0);
		}
		System.out.println(data);
	}
	
	/**
	 * answer, authority, additional section
	 */
	private void readSection(ByteBuffer src, int n) {
		for (int i = 0; i < n; i++) {
			readRecord(src);
		}
	}
	
	/**
	 * answer section
	 */
	private void readSection3(ByteBuffer src, int n) {
		readSection(src, n);
	}
	
	/**
	 * authority section
	 */
	private void readSection4(ByteBuffer src, int n) {
		readSection(src, n);
	}
	
	/**
	 * additional section
	 */
	private void readSection5(ByteBuffer src, int n) {
		readSection(src, n);
	}
	
	public void reply() throws IOException {
		// 0, 12, 0, 17, -128, 1, 0, 0, 0, 0, 0, 0, 0, 0
		// 2, 8, 0, 17, -127, 0, 0, 1, 0, 0, 0, 13, 0, 15, 3, 119, 119, 119, 6, 104, 97, 111, 49, 50, 51, 3, 99, 111, 109, 0, 0, 1, 0, 1, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 20, 1, 104, 12, 103, 116, 108, 100, 45, 115, 101, 114, 118, 101, 114, 115, 3, 110, 101, 116, 0, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 99, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 101, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 98, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 109, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 97, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 103, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 108, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 105, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 102, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 100, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 107, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 106, -64, 46, -64, -116, 0, 1, 0, 1, 0, 2, -93, 0, 0, 4, -64, 5, 6, 30, -64, 108, 0, 1, 0, 1, 0, 2, -93, 0, 0, 4, -64, 33, 14, 30, -64, 76, 0, 1, 0, 1, 0, 2, -93, 0, 0, 4, -64, 26, 92, 30, -64, -36, 0, 1, 0, 1, 0, 2, -93, 0, 0, 4, -64, 31, 80, 30, -64, 92, 0, 1, 0, 1, 0, 2, -93, 0, 0, 4, -64, 12, 94, 30, -64, -52, 0, 1, 0, 1, 0, 2, -93, 0, 0, 4, -64, 35, 51, 30, -64, -100, 0, 1, 0, 1, 0, 2, -93, 0, 0, 4, -64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, 3, 99, 111, 109, 0, 0, 1, 0, 1, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 20, 1, 99, 12, 103, 116, 108, 100, 45, 115, 101, 114, 118, 101, 114, 115, 3, 110, 101, 116, 0, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 108, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 100, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 107, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 103, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 104, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 97, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 109, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 106, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 98, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 101, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 102, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 105, -64, 46, -64, -100, 0, 1, 0, 1, 0, 2, -93, 0, 0, 4, -64, 5, 6, 30, -64, -52, 0, 1, 0, 1, 0, 2, -93, 0, 0, 4, -64, 33, 14, 30, -64, 44, 0, 1, 0, 1, 0, 2, -93, 0, 0, 4, -64, 26, 92, 30, -64, 92, 0, 1, 0, 1, 0, 2, -93, 0, 0, 4, -64, 31, 80, 30, -64, -36, 0, 1, 0, 1, 0, 2, -93, 0, 0, 4, -64, 12, 94, 30, -64, -20, 0, 1, 0, 1, 0, 2, -93, 0, 0, 4, -64, 35, 51, 30, -64, 124, 0, 1, 0, 1, 0, 2, -93, 0, 0, 4,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qr == 0 ? true : false;
		int opcode = (b << 25) >> 28;
		int aa = (b << 29) >> 31;
		int tc = (b << 30) >> 31;
		int rd = (b << 31) >> 31;
		
		
		b = bb.get();
		int ra = b >> 7;
		int rcode = b & 0x0F;
		
		// number of entries in the question section
		int n2 = bb.getShort();
		System.out.println(n2);
		
		// number of resource records in the answer section
		int n3 = bb.getShort();
		System.out.println(n3);
		
		// number of name server resource records in the authority 
		// records section
		int n4 = bb.getShort();
		System.out.println(n4);
		
		int n5 = bb.getShort();
		System.out.println(n5);
		
		readSection2(bb, n2);
		readSection3(bb, n3);
		readSection4(bb, n4);
		readSection5(bb, n5);
	}
	
	private ByteBuffer readRecordData(ByteBuffer src, int length) {
		ByteBuffer dst = ByteBuffer.allocate(length);
		while (dst.hasRemaining()) {
			dst.put(src.get());
		}
		dst.rewind();
		return dst;
	}
}

 

你可能感兴趣的:(DNS响应报文解析)