-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHostnameFetcher.java
68 lines (61 loc) · 1.54 KB
/
HostnameFetcher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.net.InetAddress;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
public class HostnameFetcher{
public HostnameFetcher() {}
private String resolveWithLLMNR(InetAddress ip) {
try {
LLMNRResolver resolver = new LLMNRResolver(1000);
String name = resolver.resolve(ip);
resolver.close();
return name;
}
catch (SocketTimeoutException | SocketException e) {
return null;
}
catch (Exception e) {
return null;
}
}
private String resolveWithMulticastDNS(InetAddress ip) {
try {
MDNSResolver resolver = new MDNSResolver(1000);
String name = resolver.resolve(ip);
resolver.close();
return name;
}
catch (SocketTimeoutException | SocketException e) {
return null;
}
catch (Exception e) {
return null;
}
}
private String resolveWithNetBIOS(InetAddress ip) {
try {
NetBIOSResolver resolver = new NetBIOSResolver(1000);
String[] names = resolver.resolve(ip);
resolver.close();
return names == null ? null : names[0];
}
catch (SocketTimeoutException | SocketException e) {
return null;
}
catch (Exception e) {
return null;
}
}
public String resolveHostname(String ipAddress) {
try {
InetAddress ip = InetAddress.getByName(ipAddress);
// String name = resolveWithMulticastDNS(ip);
String name = resolveWithLLMNR(ip);
if (name == null) name = resolveWithMulticastDNS(ip);
if (name == null) name = resolveWithNetBIOS(ip);
return name;
} catch (UnknownHostException e) {
return null;
}
}
}