Mattie

Untitled

Jun 20th, 2011
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.48 KB | None | 0 0
  1.     public static String ipToRDNS(String hostIp)
  2.     {
  3.  
  4.         if (containsOnlyNumbers(hostIp))
  5.         {
  6.             Record opt = null;
  7.             Resolver res;
  8.             try
  9.             {
  10.                 res = new ExtendedResolver();
  11.                 Name name = ReverseMap.fromAddress(hostIp);
  12.                 int type = Type.PTR;
  13.                 int dclass = DClass.IN;
  14.                 Record rec = Record.newRecord(name, type, dclass);
  15.                 Message query = Message.newQuery(rec);
  16.                 Message response = res.send(query);
  17.                 Record[] answers = response.getSectionArray(Section.ANSWER);
  18.                 if (answers.length == 0)//error
  19.                 {
  20. //                  sendMessage(channel, hostIp + " <-> " + hostIp);
  21.                     System.out.println("ipToRdns error length ==  0");
  22.                     return "error or no rDNS";
  23.                 }
  24.                 else
  25.                 {
  26.                     String host = "" + answers[0].rdataToString();
  27.                     host = host.substring(0, host.length() - 1);//remove the . at the end
  28.                     return host;
  29.                 }
  30.  
  31.             } catch (UnknownHostException e)
  32.             {
  33.                 return "Error: UnknownHost";
  34.             } catch (IOException e)
  35.             {
  36.                 e.printStackTrace();
  37.             }
  38.  
  39.             System.out.println("ipToRdns final catch");
  40.             return "error";
  41.         }
  42.  
  43.         return "error";
  44.     }
  45.  
  46.     public static boolean containsOnlyNumbers(String str)
  47.     {
  48.  
  49.         //It can't contain only numbers if it's null or empty...
  50.         if (str == null || str.length() == 0)
  51.             return false;
  52.  
  53.         for (int i = 0; i < str.length(); i++)
  54.         {
  55.  
  56.             //If we find a non-digit character we return false.
  57.             if (!Character.isDigit(str.charAt(i)) && str.charAt(i) != '.')
  58.                 return false;
  59.         }
  60.  
  61.         return true;
  62.     }
Advertisement
Add Comment
Please, Sign In to add comment