Example usage for java.net InetAddress getAddress

List of usage examples for java.net InetAddress getAddress

Introduction

In this page you can find the example usage for java.net InetAddress getAddress.

Prototype

public byte[] getAddress() 

Source Link

Document

Returns the raw IP address of this InetAddress object.

Usage

From source file:com.mytwitter.Network.NetworkHelper.java

/**
 * Converts an IP address to an integer.
 *//*from ww w  . ja v  a  2  s  .c o m*/
public static int lookupHost(String hostname) {
    InetAddress address;

    try {
        address = InetAddress.getByName(hostname);
    } catch (UnknownHostException e) {
        return -1;
    }

    byte[] addrBytes = address.getAddress();

    return ((addrBytes[3] & 0xff) << 24) | ((addrBytes[2] & 0xff) << 16) | ((addrBytes[1] & 0xff) << 8)
            | (addrBytes[0] & 0xff);
}

From source file:io.mycat.util.ByteBufferUtil.java

public static ByteBuffer bytes(InetAddress address) {
    return ByteBuffer.wrap(address.getAddress());
}

From source file:org.bml.util.sql.DBUtil.java

/**
 *
 * @param ps/*  ww  w  .j  a  v  a2s  .  c o m*/
 * @param keyName
 * @param fieldId
 * @param map
 * @param remove
 * @throws SQLException
 * @throws ParseException
 */
public static void setInetAddress(PreparedStatement ps, String keyName, int fieldId, Map<String, String> map,
        boolean remove) throws SQLException, ParseException, UnknownHostException {
    String val = map.get(keyName);
    if (remove) {
        map.remove(keyName);
    }
    if (val == null) {
        ps.setTimestamp(fieldId, null);
        return;
    }
    InetAddress ip;
    try {
        ip = InetAddress.getByName(val);
        ps.setInt(fieldId, ConversionUtils.byteArrayToUnsignedInt(ip.getAddress()));
    } catch (UnknownHostException ex) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("UnknownHostException Found while converting IPV4 adress to unsigned int.", ex);
        } else if (LOG.isWarnEnabled()) {
            LOG.warn("UnknownHostException Found while converting IPV4 adress to unsigned int. ADDRESS=" + val);
        }
        throw ex;
    }
}

From source file:com.vmware.vchs.publicapi.samples.GatewayRuleSample.java

/**
 * This method converts ip to long which is later used to find the ip within iprange
 * /*from w  ww. ja v  a 2 s  .  c o m*/
 * @param ip
 *            ip address
 * @return result
 */
private static final long ipToLong(InetAddress ip) {
    byte[] octets = ip.getAddress();
    long result = 0;
    for (byte octet : octets) {
        result <<= 8;
        result |= octet & 0xff;
    }

    return result;
}

From source file:com.xsdn.main.util.Ip4Network.java

/**
 * Create an IPv4 network address from the given IPv4 address and
 * prefix length.//from  w w w  .j  a  va2s .c  o  m
 *
 * @param iaddr   An {@link InetAddress} instance.
 * @param length  The IPv4 prefix length.
 *                Note that zero means "no mask". So zero is treated as if
 *                the maximum prefix length is specified.
 * @return  An {@link InetAddress} instance which represents the IPv4
 *          network address specified by the given pair of IPv4 address and
 *          prefix length.
 * @throws NullPointerException
 *    {@code iaddr} is {@code null}.
 * @throws IllegalArgumentException
 *    The given IPv4 address or prefix length is invalid.
 */
public static InetAddress getNetworkAddress(InetAddress iaddr, int length) {
    int addr = NumberUtils.toInteger(iaddr.getAddress());
    int mask = getNetMask(length);
    return getInetAddress(addr & mask);
}

From source file:org.thoughtcrime.securesms.mms.MmsConnection.java

protected static boolean checkRouteToHost(Context context, String host, boolean usingMmsRadio)
        throws IOException {
    InetAddress inetAddress = InetAddress.getByName(host);
    if (!usingMmsRadio) {
        if (inetAddress.isSiteLocalAddress()) {
            throw new IOException("RFC1918 address in non-MMS radio situation!");
        }//from   w w w.j av a2s  .com
        Log.w(TAG, "returning vacuous success since MMS radio is not in use");
        return true;
    }
    byte[] ipAddressBytes = inetAddress.getAddress();
    if (ipAddressBytes == null || ipAddressBytes.length != 4) {
        Log.w(TAG, "returning vacuous success since android.net package doesn't support IPv6");
        return true;
    }

    Log.w(TAG, "Checking route to address: " + host + ", " + inetAddress.getHostAddress());
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    int ipAddress = Conversions.byteArrayToIntLittleEndian(ipAddressBytes, 0);
    boolean routeToHostObtained = manager.requestRouteToHost(MmsRadio.TYPE_MOBILE_MMS, ipAddress);
    Log.w(TAG, "requestRouteToHost result: " + routeToHostObtained);
    return routeToHostObtained;
}

From source file:org.cc86.MMC.client.Main.java

public static void serverDiscoveryHack(String[] args) {
    try {/*from  w w  w.  j  ava 2s  .c o  m*/

        final boolean[] foundSomething = { false };
        List<InetAddress> addrList = new ArrayList<InetAddress>();
        Enumeration interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            try {
                NetworkInterface ifc = (NetworkInterface) interfaces.nextElement();

                if (ifc.isUp()) {
                    Enumeration addresses = ifc.getInetAddresses();
                    while (addresses.hasMoreElements()) {
                        InetAddress addr = (InetAddress) addresses.nextElement();
                        if (addr instanceof Inet6Address) {
                            continue;
                        }
                        byte[] addrraw = addr.getAddress();
                        for (int i = 1; i < 254; i++) {
                            try {
                                final InetAddress calculated = InetAddress.getByAddress(
                                        new byte[] { addrraw[0], addrraw[1], addrraw[2], (byte) i });
                                new Thread(() -> {
                                    //c=new TCPConnection(calculated.getHostAddress(), 0xCC86);
                                    try {
                                        Socket client = new Socket();
                                        client.connect(
                                                new InetSocketAddress(calculated.getHostAddress(), 0xcc87),
                                                2000);
                                        synchronized (foundSomething) {
                                            if (!foundSomething[0]) {
                                                foundSomething[0] = true;
                                                ArrayList<String> lst = new ArrayList(Arrays.asList(args));
                                                lst.add("-i");
                                                lst.add(calculated.getHostAddress());
                                                main(lst.toArray(args));
                                            }
                                        }
                                    } catch (IOException ex) {
                                        //System.out.println(ex.m);
                                        l.trace("Dead host");
                                        //System.exit(0);
                                    }
                                }).start();
                            } catch (UnknownHostException ex) {
                                ex.printStackTrace();
                            }
                        }
                    }
                }
            } catch (SocketException ex) {
                ex.printStackTrace();
            }
        }

    } catch (SocketException ex) {
        ex.printStackTrace();
    }
}

From source file:com.legstar.codegen.CodeGenUtil.java

/**
 * Retrieve the IP address of the generation machine .
 * /*from   w  w  w . j  av a2s  . c om*/
 * @return the local machine IP address
 */
public static String getLocalIPAddress() {
    try {
        InetAddress addr = InetAddress.getLocalHost();
        byte[] ipAddr = addr.getAddress();
        String ipAddrStr = "";
        for (int i = 0; i < ipAddr.length; i++) {
            if (i > 0) {
                ipAddrStr += ".";
            }
            ipAddrStr += ipAddr[i] & 0xFF;
        }
        return ipAddrStr;
    } catch (UnknownHostException e) {
        return "";
    }

}

From source file:org.fdroid.enigtext.mms.MmsCommunication.java

private static void checkRouteToHost(Context context, String host, boolean usingMmsRadio) throws IOException {
    InetAddress inetAddress = InetAddress.getByName(host);

    if (!usingMmsRadio) {
        if (inetAddress.isSiteLocalAddress()) {
            throw new IOException("RFC1918 address in non-MMS radio situation!");
        }//  w  ww .ja va  2  s . co m

        return;
    }

    Log.w("MmsCommunication", "Checking route to address: " + host + " , " + inetAddress.getHostAddress());

    byte[] ipAddressBytes = inetAddress.getAddress();

    if (ipAddressBytes != null && ipAddressBytes.length == 4) {
        int ipAddress = Conversions.byteArrayToIntLittleEndian(ipAddressBytes, 0);
        ConnectivityManager manager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        if (!manager.requestRouteToHost(MmsDownloader.TYPE_MOBILE_MMS, ipAddress))
            throw new IOException("Connection manager could not obtain route to host.");
    }
}

From source file:jp.takuo.android.mmsreq.Request.java

/**
 * Look up a host name and return the result as an int. Works if the argument
 * is an IP address in dot notation. Obviously, this can only be used for IPv4
 * addresses./*  w w w .j  ava  2 s. co  m*/
 * @param hostname the name of the host (or the IP address)
 * @return the IP address as an {@code int} in network byte order
 */
private static int lookupHost(String hostname) {
    InetAddress inetAddress;
    try {
        inetAddress = InetAddress.getByName(hostname);
    } catch (UnknownHostException e) {
        Log.d(LOG_TAG, "Failed to resolve address: " + hostname);
        return -1;
    }
    Log.d(LOG_TAG, "Resolved Address: " + inetAddress.toString());
    byte[] addrBytes;
    int addr;
    addrBytes = inetAddress.getAddress();
    addr = ((addrBytes[3] & 0xff) << 24) | ((addrBytes[2] & 0xff) << 16) | ((addrBytes[1] & 0xff) << 8)
            | (addrBytes[0] & 0xff);
    return addr;
}