-
-
Notifications
You must be signed in to change notification settings - Fork 885
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Host/domain name, dns servers and default gateways information for Win/Linux #294
Changes from 1 commit
72fe23f
7fb3fbe
110764a
4e420f6
56d5f30
092e65b
af3fc3b
0eea0d5
a1c5bb2
f6ecd61
19e6546
366fc2d
cf19954
9b8058d
4bf0d57
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,7 +41,9 @@ public class WindowsNetworkParams implements NetworkParams { | |
|
||
private static final Logger LOG = LoggerFactory.getLogger(WindowsNetworkParams.class); | ||
|
||
private static final WmiUtil.ValueType[] GATEWAY_TYPES = {WmiUtil.ValueType.STRING, WmiUtil.ValueType.UINT16}; | ||
private static final WmiUtil.ValueType[] GATEWAY_TYPES = { WmiUtil.ValueType.STRING, WmiUtil.ValueType.UINT16 }; | ||
private static final String IPV4_DEFAULT_DEST = "0.0.0.0/0"; | ||
private static final String IPV6_DEFAULT_DEST = "::/0"; | ||
|
||
/** | ||
* {@inheritDoc} | ||
|
@@ -76,8 +78,10 @@ public String getDomainName() { | |
*/ | ||
@Override | ||
public String[] getDnsServers() { | ||
// this may be done by iterating WMI instances ROOT\CIMV2\Win32_NetworkAdapterConfiguration | ||
// then sort by IPConnectionMetric, but current JNA release does not have string array support | ||
// this may be done by iterating WMI instances | ||
// ROOT\CIMV2\Win32_NetworkAdapterConfiguration | ||
// then sort by IPConnectionMetric, but current JNA release does not | ||
// have string array support | ||
// for Variant (it's merged but not release yet). | ||
WinDef.ULONGByReference bufferSize = new WinDef.ULONGByReference(); | ||
int ret = IPHlpAPI.INSTANCE.GetNetworkParams(null, bufferSize); | ||
|
@@ -95,31 +99,31 @@ public String[] getDnsServers() { | |
|
||
List<String> list = new ArrayList<>(); | ||
IPHlpAPI.IP_ADDR_STRING dns = buffer.DnsServerList; | ||
while(dns != null) { | ||
while (dns != null) { | ||
String addr = new String(dns.IpAddress.String); | ||
int nullPos = addr.indexOf(0); | ||
if(nullPos != -1) { | ||
if (nullPos != -1) { | ||
addr = addr.substring(0, nullPos); | ||
} | ||
list.add(addr); | ||
dns = dns.Next; | ||
} | ||
|
||
return list.toArray(new String[0]); | ||
return list.toArray(new String[list.size()]); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public String getIpv4DefaultGateway() { | ||
String dest = "0.0.0.0/0"; | ||
String dest = IPV4_DEFAULT_DEST; | ||
return getNextHop(dest); | ||
} | ||
|
||
private String getNextHop(String dest) { | ||
Map<String, List<Object>> vals = WmiUtil.selectObjectsFrom("ROOT\\StandardCimv2", "MSFT_NetRoute", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The We need to provide a fallback method for finding the default gateway on Win7. My suggestion would be to parse the output of |
||
"NextHop,RouteMetric", "WHERE DestinationPrefix=\"" + dest + "\"", GATEWAY_TYPES); | ||
"NextHop,RouteMetric", "WHERE DestinationPrefix=\"" + dest + "\"", GATEWAY_TYPES); | ||
List<Object> metrics = vals.get("RouteMetric"); | ||
if (vals.get("RouteMetric").size() == 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/readability: use |
||
return ""; | ||
|
@@ -142,7 +146,7 @@ private String getNextHop(String dest) { | |
*/ | ||
@Override | ||
public String getIpv6DefaultGateway() { | ||
String dest = "::/0"; | ||
String dest = IPV6_DEFAULT_DEST; | ||
return getNextHop(dest); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer native calls to reading files or parsing command lines. In this case, there is a
gethostname()
function inlibc
.