use of com.biglybt.core.networkmanager.admin.NetworkAdminException in project BiglyBT by BiglySoftware.
the class NetworkAdminASNImpl method getCIDREndAddress.
protected InetAddress getCIDREndAddress() throws NetworkAdminException {
int pos = bgp_prefix.indexOf('/');
try {
InetAddress start = InetAddress.getByName(bgp_prefix.substring(0, pos));
int cidr_mask = Integer.parseInt(bgp_prefix.substring(pos + 1));
int rev_mask = 0;
for (int i = 0; i < 32 - cidr_mask; i++) {
rev_mask = (rev_mask << 1) | 1;
}
byte[] bytes = start.getAddress();
bytes[0] |= (rev_mask >> 24) & 0xff;
bytes[1] |= (rev_mask >> 16) & 0xff;
bytes[2] |= (rev_mask >> 8) & 0xff;
bytes[3] |= (rev_mask) & 0xff;
return (InetAddress.getByAddress(bytes));
} catch (Throwable e) {
throw (new NetworkAdminException("Parse failure for '" + bgp_prefix + "'", e));
}
}
use of com.biglybt.core.networkmanager.admin.NetworkAdminException in project BiglyBT by BiglySoftware.
the class NetworkAdminASNLookupImpl method lookupTCP.
protected NetworkAdminASNImpl lookupTCP(InetAddress address) throws NetworkAdminException {
try {
Socket socket = new Socket();
int timeout = TIMEOUT;
long start = SystemTime.getCurrentTime();
socket.connect(new InetSocketAddress(WHOIS_ADDRESS, WHOIS_PORT), timeout);
long end = SystemTime.getCurrentTime();
timeout -= (end - start);
if (timeout <= 0) {
throw (new NetworkAdminException("Timeout on connect"));
} else if (timeout > TIMEOUT) {
timeout = TIMEOUT;
}
socket.setSoTimeout(timeout);
try {
OutputStream os = socket.getOutputStream();
String command = "-u -p " + address.getHostAddress() + "\r\n";
os.write(command.getBytes());
os.flush();
InputStream is = socket.getInputStream();
byte[] buffer = new byte[1024];
String result = "";
while (true) {
int len = is.read(buffer);
if (len <= 0) {
break;
}
result += new String(buffer, 0, len);
}
return (processResult(result));
} finally {
socket.close();
}
} catch (Throwable e) {
throw (new NetworkAdminException("whois connection failed", e));
}
}
Aggregations