List of usage examples for java.net InetAddress getByAddress
public static InetAddress getByAddress(byte[] addr) throws UnknownHostException
From source file:com.offbynull.portmapper.pcp.FilterPcpOption.java
/** * Constructs a {@link FilterPcpOption} by parsing a buffer. * @param buffer buffer containing PCP option data * @throws NullPointerException if any argument is {@code null} * @throws BufferUnderflowException if not enough data is available in {@code buffer} * @throws IllegalArgumentException if option code is not {@code 3}, or if the field {@code prefixLength > 128} */// w ww.j a va 2 s .c o m public FilterPcpOption(ByteBuffer buffer) { super(buffer); Validate.isTrue(super.getCode() == 3); buffer.get(); // reserved prefixLength = buffer.get() & 0xFF; remotePeerPort = buffer.getShort() & 0xFFFF; Validate.inclusiveBetween(0, 128, prefixLength); // 0 indicates 'no filter' Validate.inclusiveBetween(0, 65535, remotePeerPort); // 0 indicates 'all ports', should never trigger byte[] addrArr = new byte[16]; buffer.get(addrArr); try { remotePeerIpAddress = InetAddress.getByAddress(addrArr); } catch (UnknownHostException uhe) { throw new IllegalStateException(uhe); // should never happen } }
From source file:org.apache.hadoop.hbase.TestIPv6NIOServerSocketChannel.java
/** * Checks whether we are effected by the JDK issue on windows, and if so * ensures that we are running with preferIPv4Stack=true. *///w ww.ja v a 2s. c o m @Test public void testServerSocket() throws IOException { byte[] addr = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }; InetAddress inetAddr = InetAddress.getByAddress(addr); try { bindServerSocket(inetAddr); bindNIOServerSocket(inetAddr); //if on *nix or windows JDK7, both will pass } catch (java.net.SocketException ex) { //On Windows JDK6, we will get expected exception: //java.net.SocketException: Address family not supported by protocol family //or java.net.SocketException: Protocol family not supported Assert.assertFalse(ex.getClass().isInstance(BindException.class)); Assert.assertTrue(ex.getMessage().toLowerCase().contains("protocol family")); LOG.info("Received expected exception:"); LOG.info(ex); //if this is the case, ensure that we are running on preferIPv4=true ensurePreferIPv4(); } }
From source file:org.opendaylight.netvirt.dhcpservice.api.DHCPOptions.java
public String getOptionStrAddr(byte code) { byte[] opt = this.getOptionBytes(code); try {/*from w w w . ja v a 2 s .c o m*/ return InetAddress.getByAddress(opt).getHostAddress(); } catch (UnknownHostException | NullPointerException e) { return null; } }
From source file:net.fenyo.gnetwatch.GenericTools.java
/** * Returns the class A/B/C network address containing an IP address. * @param addr_str IPv4 address./*from w ww . j a va 2 s . c om*/ * @return String network address. */ static public String getNetFromAddress(final String addr_str) { try { final InetAddress addr; addr = InetAddress.getByName(addr_str); if (Inet6Address.class.isInstance(addr)) { // rfc-4291 return "IPv6 range"; } else if (Inet4Address.class.isInstance(addr)) { byte bytes[] = ((Inet4Address) addr).getAddress(); if (unsignedByteToShort(bytes[0]) < 128) { // class A bytes[1] = 0; bytes[2] = 0; bytes[3] = 0; return InetAddress.getByAddress(bytes).toString().substring(1) + "/8"; } else if (unsignedByteToShort(bytes[0]) < 192) { // class B bytes[2] = 0; bytes[3] = 0; return InetAddress.getByAddress(bytes).toString().substring(1) + "/16"; } else if (unsignedByteToShort(bytes[0]) < 224) { // class C bytes[3] = 0; return InetAddress.getByAddress(bytes).toString().substring(1) + "/24"; } else if (unsignedByteToShort(bytes[0]) < 248) { // class D return "224.0.0.0/4"; } else { // class E return "248.0.0.0/4"; } } else return null; } catch (final UnknownHostException ex) { log.error("Exception (addr_str=" + addr_str + ")", ex); return null; } }
From source file:nl.sidn.pcap.util.GeoLookupUtil.java
public String lookupASN(byte[] ip, boolean v4) { try {/*from ww w. j a va2 s .co m*/ InetAddress addr = InetAddress.getByAddress(ip); return lookupASN(addr, v4); } catch (UnknownHostException e) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("No asn found for: " + ip); } return null; } }
From source file:org.opendaylight.controller.switchmanager.Subnet.java
private InetAddress getPrefixForAddress(InetAddress ip) { int bytes = this.subnetMaskLength / 8; int bits = this.subnetMaskLength % 8; byte modifiedByte; byte[] sn = ip.getAddress(); if (bits > 0) { modifiedByte = (byte) (sn[bytes] >> (8 - bits)); sn[bytes] = (byte) (modifiedByte << (8 - bits)); bytes++;//from ww w. ja v a 2 s .c om } for (; bytes < sn.length; bytes++) { sn[bytes] = (byte) (0); } try { return InetAddress.getByAddress(sn); } catch (UnknownHostException e) { return null; } }
From source file:com.bitsofproof.supernode.core.IRCDiscovery.java
@Override public List<InetSocketAddress> discover() { List<InetSocketAddress> al = new ArrayList<InetSocketAddress>(); try {/*from w w w . j a v a 2 s . c o m*/ log.trace("Connect to IRC server " + server); Socket socket = new Socket(server, port); PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8")); BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8")); String[] answers = new String[] { "Found your hostname", "using your IP address instead", "Couldn't look up your hostname", "ignoring hostname" }; String line; boolean stop = false; while (!stop && (line = reader.readLine()) != null) { log.trace("IRC receive " + line); for (int i = 0; i < answers.length; ++i) { if (line.contains(answers[i])) { stop = true; break; } } } String nick = "bop" + new SecureRandom().nextInt(Integer.MAX_VALUE); writer.println("NICK " + nick); writer.println("USER " + nick + " 8 * : " + nick); writer.flush(); log.trace("IRC send: I am " + nick); while ((line = reader.readLine()) != null) { log.trace("IRC receive " + line); if (hasCode(line, new String[] { " 004 ", " 433 " })) { break; } } log.trace("IRC send: joining " + channel); writer.println("JOIN " + channel); writer.println("NAMES"); writer.flush(); while ((line = reader.readLine()) != null) { log.trace("IRC receive " + line); if (hasCode(line, new String[] { " 353 " })) { StringTokenizer tokenizer = new StringTokenizer(line, ":"); String t = tokenizer.nextToken(); if (tokenizer.hasMoreElements()) { t = tokenizer.nextToken(); } tokenizer = new StringTokenizer(t); tokenizer.nextToken(); while (tokenizer.hasMoreTokens()) { String w = tokenizer.nextToken().substring(1); if (!tokenizer.hasMoreElements()) { continue; } try { byte[] m = ByteUtils.fromBase58WithChecksum(w); byte[] addr = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (byte) 0xff, (byte) 0xff, 0, 0, 0, 0 }; System.arraycopy(m, 0, addr, 12, 4); al.add(new InetSocketAddress(InetAddress.getByAddress(addr), chain.getPort())); } catch (ValidationException e) { log.trace(e.toString()); } } } if (hasCode(line, new String[] { " 366 " })) { break; } } writer.println("PART " + channel); writer.println("QUIT"); writer.flush(); socket.close(); } catch (UnknownHostException e) { log.error("Can not find IRC server " + server, e); } catch (IOException e) { log.error("Can not use IRC server " + server, e); } return al; }
From source file:us.nineworlds.serenity.core.services.GDMService.java
protected InetAddress getBroadcastAddress() throws IOException { WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); DhcpInfo dhcp = wifi.getDhcpInfo();//from w w w .j a v a 2 s. co m // handle null somehow int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask; byte[] quads = new byte[4]; for (int k = 0; k < 4; k++) quads[k] = (byte) (broadcast >> k * 8); return InetAddress.getByAddress(quads); }
From source file:com.rovemonteux.silvertunnel.netlib.layer.tor.common.TCPStreamProperties.java
public TCPStreamProperties(final TcpipNetAddress address) { if (address.getIpaddress() != null) { // use IP address (preferred over host name) this.hostname = null; try {/*from w w w. j a va2 s . c om*/ this.addr = InetAddress.getByAddress(address.getIpaddress()); } catch (final UnknownHostException e) { LOG.warn("invalid address=" + address, e); } this.port = address.getPort(); addrResolved = true; } else { // use host name this.hostname = address.getHostname(); this.addr = null; this.port = address.getPort(); addrResolved = false; } init(); }
From source file:com.cafbit.multicasttest.NetThread.java
/** * Transmit an mDNS query on the local network. * @param host/*from w w w . j ava 2 s .c o m*/ * @throws IOException */ private void query(String host) throws IOException { Log.i(TAG, "query essai 1: " + host); byte[] requestData = (new DNSMessage(host)).serialize(); DatagramPacket request = new DatagramPacket(requestData, requestData.length, InetAddress.getByAddress(MDNS_ADDR), MDNS_PORT); multicastSocket.send(request); }