1
안녕하세요 저는 기존 라이브러리를 사용하지 않고 java를 사용하여 고유 한 DNS 클라이언트를 구축해야하는 학교 과제를 수행하고 있습니다. 응답을 구문 분석하는 방법을 알아 냈지만 응답 섹션에 여러 레코드가있는 경우 오류가 발생했습니다.NS 레코드가 여러 개있을 때 구문 분석 DNS 응답
public void processResponse(DatagramPacket packet) throws IOException{
DataInputStream din = new DataInputStream(new ByteArrayInputStream(packet.getData()));
din.readShort(); //Transaction ID
din.readShort(); //Flags
int questions = din.readShort();
int answers = din.readShort();
int authorities = din.readShort();
int additional = din.readShort();
int recLen = 0;
while ((recLen = din.readByte()) > 0) {
for (int i = 0; i < recLen; i++) {
din.readByte();
}
}
din.readShort(); // Record type for question
din.readShort(); // Class for question
//ANSWERS
//Just looping for one answer here
for(int i=0;i<1;i++){
din.readShort(); // Record name for Answer
String response = "";
int typeOfRec = din.readShort();
din.readShort(); // Class for answers
int ttl = din.readInt();
short resLength = din.readShort();
switch(typeOfRec){
//A
case 1:{
response+="IP\t";
String ip = "";
for (int j = 0; j < resLength; j++) {
ip+=("" + String.format("%d", (din.readByte() & 0xFF)) + ".");
}
response+=ip+"\t"+ttl;
System.out.println(response);
break;
}
//NS
case 2:{
String ip = "";
response+="NS\t";
while ((recLen = din.readByte()) > 0) {
byte[] record = new byte[recLen];
for (int j = 0; j < recLen; j++) {
record[j] = din.readByte();
}
ip+=new String(record, "UTF-8");
ip+=".";
}
response+=ip+"\t"+ttl;
System.out.println(response);
break;
}
//CNAME
case 5:{
response+="CNAME\t";
break;
}
//MX
case 15:{
response+="MX\t";
break;
}
default:{
break;
}
}
}
}
이것은 최종적으로 필요한 데이터를 얻기 위해 응답 패킷을 처리하는 기능입니다. 그러나이 코드를 실행하면 응답은 NS ns-358.awsdns-44입니다. 86399 .com이 누락되었습니다.