Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static String ipToRDNS(String hostIp)
- {
- if (containsOnlyNumbers(hostIp))
- {
- Record opt = null;
- Resolver res;
- try
- {
- res = new ExtendedResolver();
- Name name = ReverseMap.fromAddress(hostIp);
- int type = Type.PTR;
- int dclass = DClass.IN;
- Record rec = Record.newRecord(name, type, dclass);
- Message query = Message.newQuery(rec);
- Message response = res.send(query);
- Record[] answers = response.getSectionArray(Section.ANSWER);
- if (answers.length == 0)//error
- {
- // sendMessage(channel, hostIp + " <-> " + hostIp);
- System.out.println("ipToRdns error length == 0");
- return "error or no rDNS";
- }
- else
- {
- String host = "" + answers[0].rdataToString();
- host = host.substring(0, host.length() - 1);//remove the . at the end
- return host;
- }
- } catch (UnknownHostException e)
- {
- return "Error: UnknownHost";
- } catch (IOException e)
- {
- e.printStackTrace();
- }
- System.out.println("ipToRdns final catch");
- return "error";
- }
- return "error";
- }
- public static boolean containsOnlyNumbers(String str)
- {
- //It can't contain only numbers if it's null or empty...
- if (str == null || str.length() == 0)
- return false;
- for (int i = 0; i < str.length(); i++)
- {
- //If we find a non-digit character we return false.
- if (!Character.isDigit(str.charAt(i)) && str.charAt(i) != '.')
- return false;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment