资源简介
设计一个DNS服务器程序,读入“域名-IP地址”对照表,当客户端查询域名对应的IP地址时,用域名检索该对照表,三种检索结果:
检索结果为ip地址0.0.0.0,则向客户端返回“域名不存在”的报错消息(不良网站拦截功能)
检索结果为普通IP地址,则向客户返回这个地址(服务器功能)
表中未检到该域名,则向因特网DNS服务器发出查询,并将结果返给客户端(中继功能)
考虑多个计算机上的客户端会同时查询,需要进行消息ID的转换
代码片段和文件信息
package com.kngxscn.dnsrelay;
public class DNSHeader {
/**
* DNS Header
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ID |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|QR| opcode |AA|TC|RD|RA| Z | RCODE |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| QDCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ANCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| NSCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ARCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
*/
/* 会话标识(2字节)*/
private short transID;
/* Flags(2字节)*/
private short flags;
/* QDCOUNT(2字节)*/
private short qdcount;
/* ANCOUNT(2字节)*/
private short ancount;
/* NSCOUNT(2字节)*/
private short nscount;
/* ARCOUNT(2字节)*/
private short arcount;
public DNSHeader() {}
public DNSHeader(short transID short flags short qdcount short ancount short nscount short arcount) {
this.transID = transID;
this.flags = flags;
this.qdcount = qdcount;
this.ancount = ancount;
this.nscount = nscount;
this.arcount = arcount;
}
public short getTransID() {
return transID;
}
public void setTransID(short transID) {
this.transID = transID;
}
public short getFlags() {
return flags;
}
public void setFlags(short flags) {
this.flags = flags;
}
public short getQdcount() {
return qdcount;
}
public void setQdcount(short qdcount) {
this.qdcount = qdcount;
}
public short getAncount() {
return ancount;
}
public void setAncount(short ancount) {
this.ancount = ancount;
}
public short getNscount() {
return nscount;
}
public void setNscount(short nscount) {
this.nscount = nscount;
}
public short getArcount() {
return arcount;
}
public void setArcount(short arcount) {
this.arcount = arcount;
}
/**
* 输出包含DNS协议头所有信息的字节数组
*/
public byte[] toByteArray() {
byte[] data = new byte[12];
int offset = 0;
byte[] byte_2 = new byte[2];
byte_2 = Utils.shortToByteArray(transID);
for (int i=0; i<2; i++) {
data[offset++] = byte_2[i];
}
byte_2 = Utils.shortToByteArray(flags);
for (int i=0; i<2; i++) {
data[offset++] = byte_2[i];
}
byte_2 = Utils.shortToByteArray(qdcount);
for (int i=0; i<2; i++) {
data[offset++] = byte_2[i];
}
byte_2 = Utils.shortToByteArray(ancount);
for (int i=0; i<2; i++) {
data[offset++] = byte_2[i];
}
byte_2 = Utils.shortToByteArray(nscount);
for (int i=0; i<2; i++) {
data[offset++] = byte_2[i];
}
byte_2 = Utils.shortToByteArray(arcount);
for (int i=0; i<2; i++) {
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-12-11 13:43 dnsrelay-master\
文件 562 2017-12-11 13:43 dnsrelay-master\README.md
文件 81 2017-12-11 13:43 dnsrelay-master\dnsrelay.txt
文件 758 2017-12-11 13:43 dnsrelay-master\pom.xm
目录 0 2017-12-11 13:43 dnsrelay-master\src\
目录 0 2017-12-11 13:43 dnsrelay-master\src\main\
目录 0 2017-12-11 13:43 dnsrelay-master\src\main\java\
目录 0 2017-12-11 13:43 dnsrelay-master\src\main\java\com\
目录 0 2017-12-11 13:43 dnsrelay-master\src\main\java\com\kngxscn\
目录 0 2017-12-11 13:43 dnsrelay-master\src\main\java\com\kngxscn\dnsrelay\
文件 3168 2017-12-11 13:43 dnsrelay-master\src\main\java\com\kngxscn\dnsrelay\DNSHeader.java
文件 1860 2017-12-11 13:43 dnsrelay-master\src\main\java\com\kngxscn\dnsrelay\DNSQuestion.java
文件 3443 2017-12-11 13:43 dnsrelay-master\src\main\java\com\kngxscn\dnsrelay\DNSRR.java
文件 2440 2017-12-11 13:43 dnsrelay-master\src\main\java\com\kngxscn\dnsrelay\DNSRelayServer.java
文件 7698 2017-12-11 13:43 dnsrelay-master\src\main\java\com\kngxscn\dnsrelay\QueryParser.java
文件 4818 2017-12-11 13:43 dnsrelay-master\src\main\java\com\kngxscn\dnsrelay\Utils.java
目录 0 2017-12-11 13:43 dnsrelay-master\src\test\
目录 0 2017-12-11 13:43 dnsrelay-master\src\test\java\
目录 0 2017-12-11 13:43 dnsrelay-master\src\test\java\com\
目录 0 2017-12-11 13:43 dnsrelay-master\src\test\java\com\kngxscn\
目录 0 2017-12-11 13:43 dnsrelay-master\src\test\java\com\kngxscn\dnsrelay\
文件 674 2017-12-11 13:43 dnsrelay-master\src\test\java\com\kngxscn\dnsrelay\DNSRelayServerTest.java
文件 577 2017-12-11 13:43 dnsrelay-master\src\test\java\com\kngxscn\dnsrelay\UtilsTest.java
评论
共有 条评论