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.jagornet.dhcp.db.JdbcIaAddressDAO.java

public List<IaAddress> findAllByRange(final InetAddress startAddr, final InetAddress endAddr) {
    return getJdbcTemplate().query(
            "select * from iaaddress where ipaddress >= ? and ipaddress <= ? order by ipaddress",
            new PreparedStatementSetter() {
                @Override//from  w  w w  .j  av  a2s.  c  o m
                public void setValues(PreparedStatement ps) throws SQLException {
                    ps.setBytes(1, startAddr.getAddress());
                    ps.setBytes(2, endAddr.getAddress());
                }
            }, new IaAddrRowMapper());
}

From source file:org.slc.sli.ingestion.processors.JobReportingProcessor.java

private void writeSecurityLog(NewBatchJob job, LogLevelType messageType, String message) {
    byte[] ipAddr = null;
    try {//from w  w  w .j  av  a  2s . c  o m
        InetAddress addr = InetAddress.getLocalHost();

        // Get IP Address
        ipAddr = addr.getAddress();

    } catch (UnknownHostException e) {
        LOG.error("Error getting local host", e);
    }

    String edOrg = tenantDA.getTenantEdOrg(job.getTopLevelSourceId());
    if (edOrg == null) {
        edOrg = "";
    }

    List<String> userRoles = Collections.emptyList();
    SecurityEvent event = new SecurityEvent();
    event.setTenantId(""); // Alpha MH (tenantId - written in 'message')
    event.setUser("");
    event.setUserEdOrg(edOrg);
    event.setTargetEdOrgList(edOrg); //@TA10431 - change targetEdOrg from scalar to list
    event.setActionUri("writeLine");
    event.setAppId("Ingestion");
    event.setOrigin("");
    if (ipAddr != null) {
        event.setExecutedOn(ipAddr[0] + "." + ipAddr[1] + "." + ipAddr[2] + "." + ipAddr[3]);
    }
    event.setCredential("");
    event.setUserOrigin("");
    event.setTimeStamp(new Date());
    event.setProcessNameOrId(ManagementFactory.getRuntimeMXBean().getName());
    event.setClassName(this.getClass().getName());
    event.setLogLevel(messageType);
    event.setRoles(userRoles);
    event.setLogMessage(message);
    audit(event);
}

From source file:com.jagornet.dhcp.db.JdbcIaPrefixDAO.java

public List<IaPrefix> findAllByRange(final InetAddress startAddr, final InetAddress endAddr) {
    return getJdbcTemplate().query("select * from iaprefix" + " where prefixaddress >= ? and prefixaddress <= ?"
            + " order by prefixaddress", new PreparedStatementSetter() {
                @Override/*from   ww w  .ja  v a2s  .c  om*/
                public void setValues(PreparedStatement ps) throws SQLException {
                    ps.setBytes(1, startAddr.getAddress());
                    ps.setBytes(2, endAddr.getAddress());
                }
            }, new IaPrefixRowMapper());
}

From source file:com.jagornet.dhcp.db.JdbcIaAddressDAO.java

public List<InetAddress> findExistingIPs(final InetAddress startAddr, final InetAddress endAddr) {
    return getJdbcTemplate().query("select ipaddress from iaaddress"
            + " where ipaddress >= ? and ipaddress <= ?" + " order by ipaddress",
            new PreparedStatementSetter() {
                @Override/*from www .  jav a  2  s  . c  o m*/
                public void setValues(PreparedStatement ps) throws SQLException {
                    ps.setBytes(1, startAddr.getAddress());
                    ps.setBytes(2, endAddr.getAddress());
                }
            }, new RowMapper<InetAddress>() {
                @Override
                public InetAddress mapRow(ResultSet rs, int rowNum) throws SQLException {
                    InetAddress inetAddr = null;
                    try {
                        inetAddr = InetAddress.getByAddress(rs.getBytes("ipaddress"));
                    } catch (UnknownHostException e) {
                        // re-throw as SQLException
                        throw new SQLException("Unable to map ipaddress", e);
                    }
                    return inetAddr;
                }
            });
}

From source file:com.jagornet.dhcp.db.JdbcIaPrefixDAO.java

public List<InetAddress> findExistingIPs(final InetAddress startAddr, final InetAddress endAddr) {
    return getJdbcTemplate().query("select prefixaddress from iaprefix"
            + " where prefixaddress >= ? and prefixaddress <= ?" + " order by prefixaddress",
            new PreparedStatementSetter() {
                @Override//from   w  ww .  ja v  a2  s. co  m
                public void setValues(PreparedStatement ps) throws SQLException {
                    ps.setBytes(1, startAddr.getAddress());
                    ps.setBytes(2, endAddr.getAddress());
                }
            }, new RowMapper<InetAddress>() {
                @Override
                public InetAddress mapRow(ResultSet rs, int rowNum) throws SQLException {
                    InetAddress inetAddr = null;
                    try {
                        inetAddr = InetAddress.getByAddress(rs.getBytes("prefixaddress"));
                    } catch (UnknownHostException e) {
                        // re-throw as SQLException
                        throw new SQLException("Unable to map prefixaddress", e);
                    }
                    return inetAddr;
                }
            });
}

From source file:com.cloud.utils.net.NetUtils.java

public static String[] getNetworkParams(final NetworkInterface nic) {
    final List<InterfaceAddress> addrs = nic.getInterfaceAddresses();
    if (addrs == null || addrs.size() == 0) {
        return null;
    }/*w  w  w  . ja va2s. c  om*/
    InterfaceAddress addr = null;
    for (final InterfaceAddress iaddr : addrs) {
        final InetAddress inet = iaddr.getAddress();
        if (!inet.isLinkLocalAddress() && !inet.isLoopbackAddress() && !inet.isMulticastAddress()
                && inet.getAddress().length == 4) {
            addr = iaddr;
            break;
        }
    }
    if (addr == null) {
        return null;
    }
    final String[] result = new String[3];
    result[0] = addr.getAddress().getHostAddress();
    try {
        final byte[] mac = nic.getHardwareAddress();
        result[1] = byte2Mac(mac);
    } catch (final SocketException e) {
        s_logger.debug("Caught exception when trying to get the mac address ", e);
    }

    result[2] = prefix2Netmask(addr.getNetworkPrefixLength());
    return result;
}

From source file:com.cloud.utils.net.NetUtils.java

private static BigInteger convertIPv6AddressToBigInteger(final IPv6Address addr) {
    InetAddress inetAddr;
    try {//from  w w  w .  ja v  a2 s  .c  o  m
        inetAddr = addr.toInetAddress();
    } catch (final UnknownHostException e) {
        return null;
    }
    return new BigInteger(inetAddr.getAddress());
}

From source file:com.jagornet.dhcp.db.JdbcLeaseManager.java

/**
 * Find dhcp lease for InetAddr./*from   ww  w  .j  a  v  a 2  s.c  o m*/
 *
 * @param inetAddr the InetAddr
 * @return the DhcpLease
 */
protected DhcpLease findDhcpLeaseForInetAddr(final InetAddress inetAddr) {
    List<DhcpLease> leases = getJdbcTemplate().query("select * from dhcplease" + " where ipaddress = ?",
            new PreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement ps) throws SQLException {
                    ps.setBytes(1, inetAddr.getAddress());
                }
            }, new DhcpLeaseRowMapper());
    if ((leases != null) && (leases.size() > 0)) {
        if (leases.size() == 1) {
            return leases.get(0);
        } else {
            //TODO: this really should be impossible because of the unique
            //      constraint on the IP address
            log.error("Found more than one lease for IP=" + inetAddr.getHostAddress());
        }
    }
    return null;
}

From source file:org.openmrs.module.webservices.rest.web.RestUtil.java

/**
 * Tests whether or not there is a match between the given IP address and the candidates.
 * // w  ww  . j a  va  2  s .c o  m
 * @param ip
 * @param candidateIps
 * @return <code>true</code> if there is a match
 * @should return true if list is empty
 * @should return false if there is no match
 * @should return true for exact match
 * @should return true for match with submask
 * @should return false if there is no match with submask
 * @should return true for exact ipv6 match
 * @should throw IllegalArgumentException for invalid mask
 */
public static boolean ipMatches(String ip, List<String> candidateIps) {
    if (candidateIps.isEmpty()) {
        return true;
    }

    InetAddress address;
    try {
        address = InetAddress.getByName(ip);
    } catch (UnknownHostException e) {
        throw new IllegalArgumentException("Invalid IP in the ip parameter" + ip, e);
    }

    for (String candidateIp : candidateIps) {
        // split IP and mask
        String[] candidateIpPattern = candidateIp.split("/");

        InetAddress candidateAddress;
        try {
            candidateAddress = InetAddress.getByName(candidateIpPattern[0]);
        } catch (UnknownHostException e) {
            throw new IllegalArgumentException("Invalid IP in the candidateIps parameter", e);
        }

        if (candidateIpPattern.length == 1) { // there's no mask
            if (address.equals(candidateAddress)) {
                return true;
            }
        } else {
            if (address.getAddress().length != candidateAddress.getAddress().length) {
                continue;
            }

            int bits = Integer.parseInt(candidateIpPattern[1]);
            if (candidateAddress.getAddress().length < Math.ceil((double) bits / 8)) {
                throw new IllegalArgumentException(
                        "Invalid mask " + bits + " for IP " + candidateIp + " in the candidateIps parameter");
            }

            // compare bytes based on the given mask
            boolean matched = true;
            for (int bytes = 0; bits > 0; bytes++, bits -= 8) {
                int mask = 0x000000FF; // mask the entire byte
                if (bits < 8) {
                    // mask only some first bits of a byte
                    mask = (mask << (8 - bits));
                }
                if ((address.getAddress()[bytes] & mask) != (candidateAddress.getAddress()[bytes] & mask)) {
                    matched = false;
                    break;
                }
            }
            if (matched) {
                return true;
            }
        }

    }
    return false;
}

From source file:com.limegroup.gnutella.RouterService.java

/**
 * Creates a new outgoing messaging connection to the given host and port. 
 * Returns immediately without blocking.  If hostname would connect
 * us to ourselves, returns immediately.
 *///from  w w  w  .  j  a v a 2  s . c  o  m
public static void connectToHostAsynchronously(String hostname, int portnum) {
    //Don't allow connections to yourself.  We have to special
    //case connections to "localhost" or "127.0.0.1" since
    //they are aliases for this machine.

    byte[] cIP = null;
    InetAddress addr;
    try {
        addr = InetAddress.getByName(hostname);
        cIP = addr.getAddress();
    } catch (UnknownHostException e) {
        return;
    }
    if ((cIP[0] == 127) && (portnum == acceptor.getPort(true))
            && ConnectionSettings.LOCAL_IS_PRIVATE.getValue()) {
        return;
    } else {
        byte[] managerIP = acceptor.getAddress(true);
        if (Arrays.equals(cIP, managerIP) && portnum == acceptor.getPort(true))
            return;
    }

    if (!acceptor.isBannedIP(cIP)) {
        manager.createConnectionAsynchronously(hostname, portnum);
    }
}