Search in sources :

Example 6 with NetworkAdminException

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));
    }
}
Also used : NetworkAdminException(com.biglybt.core.networkmanager.admin.NetworkAdminException) InetAddress(java.net.InetAddress)

Example 7 with NetworkAdminException

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));
    }
}
Also used : NetworkAdminException(com.biglybt.core.networkmanager.admin.NetworkAdminException) InetSocketAddress(java.net.InetSocketAddress) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Socket(java.net.Socket)

Aggregations

NetworkAdminException (com.biglybt.core.networkmanager.admin.NetworkAdminException)7 InetSocketAddress (java.net.InetSocketAddress)4 ProtocolEndpoint (com.biglybt.core.networkmanager.ProtocolEndpoint)3 AESemaphore (com.biglybt.core.util.AESemaphore)2 SocketChannel (java.nio.channels.SocketChannel)2 VirtualChannelSelector (com.biglybt.core.networkmanager.VirtualChannelSelector)1 NetworkAdminASN (com.biglybt.core.networkmanager.admin.NetworkAdminASN)1 PRUDPPacketHandler (com.biglybt.net.udp.uc.PRUDPPacketHandler)1 PRUDPReleasablePacketHandler (com.biglybt.net.udp.uc.PRUDPReleasablePacketHandler)1 PluginInterface (com.biglybt.pif.PluginInterface)1 UPnPPlugin (com.biglybt.plugin.upnp.UPnPPlugin)1 UPnPPluginService (com.biglybt.plugin.upnp.UPnPPluginService)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 InetAddress (java.net.InetAddress)1 Socket (java.net.Socket)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1