2017-10-17 4 views
1

Bonjour Je fais une affectation pour l'école qui nécessite que vous construisiez votre propre DNS CLient en utilisant Java sans utiliser de bibliothèques existantes. J'ai trouvé comment analyser la réponse mais il y a une erreur quand plusieurs enregistrements sont dans la section Réponse.parse dns réponse quand il y a plusieurs enregistrements NS

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; 
     } 
     } 
    }   
} 

Ceci est la fonction qui traite finalement le paquet de réponse pour obtenir les données requises. Cependant, lorsque j'exécute ce code, la réponse est seulement NS ns-358.awsdns-44. 86399 le .com est manquant

Répondre

0

Il semble que vous ayez besoin de lire sur la façon dont les noms sont codés dans le format de fil DNS. Particulièrement comment ils sont comprimés. La section 4.1.4 de la RFC 1035 sera un bon point de départ.