package util;
import java.util.*;
import java.net.*;
public class Dns {
private Vector<String> answername = null;
private Vector<InetAddress> answerip = null;
static byte[] QueryDomain(String domain) {
String[] StringSplit = domain.split("\\.");
String DomainFormat = "";
for (int i = 0; i < StringSplit.length; i++) {
DomainFormat = DomainFormat
+ (Character.toString((char) StringSplit[i].length()) + StringSplit[i]);
}
return (DomainFormat + Character.toString((char) 0)).getBytes();
}
public void opet() {
InetAddress url = null;
DatagramSocket ds = null;
try {
url = ByteArray.strtourl("202.97.224.68");
} catch (Exception urlException) {
System.out.println(urlException);
}
try {
ds = new DatagramSocket(8888);
} catch (Exception BlindError) {
System.out.println(BlindError + "udp无法使用端口");
}
byte[] b = new byte[1024];
DatagramPacket dpr = new DatagramPacket(b, 1024);
short Flags = 0x0100;
short Questions = 0x0001;
short AnswerRRs = 0x0000;
short AuthorityRRs = 0x0000;
short AdditionalRRs = 0x0000;
short SEARCHTYPE = 1;
short SEARCHCLASS = 0x0001; // Class 一般为 1 指 Internet
// 构造请求报文
ByteArray ba = new ByteArray();
ba.add((short) 20);
ba.add(Flags);
ba.add(Questions);
ba.add(AnswerRRs);
ba.add(AuthorityRRs);
ba.add(AdditionalRRs);
ba.add(QueryDomain("www.baidu.com"));
ba.add(SEARCHTYPE);
ba.add(SEARCHCLASS);
byte[] Header = ba.getBytes();
ba = null;
try {
ds.setSoTimeout(6000);
ds.send(new DatagramPacket(Header, Header.length, url, 53));
ds.receive(dpr);
} catch (Exception e) {
System.out.println(e);
}
System.out.println("TID is OK");
if ((ByteArray.bytes2short(b, 2) & 0x000f) != 0) {
System.out
.print("Error the domain you request is can't to explain!!");
}
answername = new Vector<String>();
answerip = new Vector<InetAddress>();
int answers = ByteArray.bytes2short(b, 6);
System.out.println("Answers:" + answers);
int answercount = 0;
// [4:6]Questions 数 两个字节
// [6:8]Answer RRs数 两个字节
// [8:10]Authority RRs数 两个字节
// [10:12]Additional RRs数 两个字节
int bitflag = 12;
while (b[bitflag] != 0)
bitflag++;
bitflag++; // 域名结束
bitflag += 2; // 跳过Type
bitflag += 2; // 跳过Class
int i = 0;
while (i < answers) {
System.out.println("============" + (i + 1) + "==============");
// Name 两个字节 指针-指向域名的字符
bitflag += 2;
if (ByteArray.bytes2short(b, bitflag) == 1) {
// Type 两个字节
bitflag += 8; // Class两个字节
// TTL 四个字节
bitflag += 2; // Rdatalength 两个字节
System.out.println(ByteArray.unsignedByte(b[bitflag]) + "."
+ ByteArray.unsignedByte(b[bitflag + 1]) + "."
+ ByteArray.unsignedByte(b[bitflag + 2]) + "."
+ ByteArray.unsignedByte(b[bitflag + 3]));
answerip.add(ByteArray.strtourl(ByteArray
.unsignedByte(b[bitflag])
+ "."
+ ByteArray.unsignedByte(b[bitflag + 1])
+ "."
+ ByteArray.unsignedByte(b[bitflag + 2])
+ "."
+ ByteArray.unsignedByte(b[bitflag + 3])));
bitflag += 4;
} else if (ByteArray.bytes2short(b, bitflag) == 5) // CNAME
{
bitflag += 8;
int rdatalength = ByteArray.bytes2short(b, bitflag);
bitflag += 2;
StringBuffer domain = new StringBuffer();
getDomain(b, bitflag, domain);
System.out.println(domain.toString());
answername.add(domain.toString());
bitflag += rdatalength;
}
i++;
}
}
public void getDomain(byte[] b, int offset, StringBuffer domain) {
if ((b[offset] & 0xc0) == 0xc0) {
getDomain(b, ByteArray.bytes2short(b, offset) & 0x3fff, domain);
} else if (b[offset] == 0) {
} else {
domain.append(new String(b, offset + 1, ByteArray
.unsignedByte(b[offset])) + ".");
getDomain(b, offset + 1 + ByteArray.unsignedByte(b[offset]), domain);
}
}
}
jy_game_over
- 粉丝: 52
- 资源: 10
最新资源
- Unity In-game Debug Console
- (3292010)Java图书管理系统(源码)
- 新建 Microsoft Word 文档
- (176102016)MATLAB代码:考虑灵活性供需不确定性的储能参与电网调峰优化配置 关键词:储能优化配置 电网调峰 风电场景生成 灵活性供需不
- SINAMICS S120驱动第三方直线永磁同步电机系列视频-配置和优化.mp4
- (175601006)51单片机交通信号灯系统设计
- Starter SINAMICS S120驱动第三方直线永磁同步电机系列视频-调试演示.mp4
- (174755032)抽烟、烟雾检测voc数据集
- 基于滑膜控制的差动制动防侧翻稳定性控制,上层通过滑膜控制产生期望的横摆力矩,下层根据对应的paper实现对应的制动力矩分配,实现车辆的防侧翻稳定性控制,通过通过carsim和simulink联合仿真
- 伺服系统基于陷波滤波器双惯量伺服系统机械谐振抑制matlab Simulink仿真 1.模型简介 模型为基于陷波滤波器的双惯量伺服系统机械谐振抑制仿真,采用Matlab R2018a Simul
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
前往页