private static StructGroupReq makeGroupReq(InetAddress gr_group, NetworkInterface networkInterface) { int gr_interface = (networkInterface != null) ? networkInterface.getIndex() : 0; return new StructGroupReq(gr_interface, gr_group); }
/** * Sets the outgoing network interface used by this socket to the given * {@code networkInterface}. * * @throws SocketException if an error occurs. */ public void setNetworkInterface(NetworkInterface networkInterface) throws SocketException { checkOpen(); if (networkInterface == null) { throw new SocketException("networkInterface == null"); } impl.setOption(SocketOptions.IP_MULTICAST_IF2, networkInterface.getIndex()); this.setAddress = null; }
/** * Sets the outgoing network interface used by this socket. The interface used is the first * interface found to have the given {@code address}. To avoid inherent unpredictability, * new code should use {@link #getNetworkInterface} instead. * * @throws SocketException if an error occurs. */ public void setInterface(InetAddress address) throws SocketException { checkOpen(); if (address == null) { throw new NullPointerException("address == null"); } NetworkInterface networkInterface = NetworkInterface.getByInetAddress(address); if (networkInterface == null) { throw new SocketException("Address not associated with an interface: " + address); } impl.setOption(SocketOptions.IP_MULTICAST_IF2, networkInterface.getIndex()); this.setAddress = address; }
public static void bind(FileDescriptor fd, InetAddress address, int port) throws SocketException { if (address instanceof Inet6Address && ((Inet6Address) address).getScopeId() == 0) { // Linux won't let you bind a link-local address without a scope id. Find one. NetworkInterface nif = NetworkInterface.getByInetAddress(address); if (nif == null) { throw new SocketException("Can't bind to a link-local address without a scope id: " + address); } try { address = Inet6Address.getByAddress(address.getHostName(), address.getAddress(), nif.getIndex()); } catch (UnknownHostException ex) { throw new AssertionError(ex); // Can't happen. } } try { Libcore.os.bind(fd, address, port); } catch (ErrnoException errnoException) { throw new BindException(errnoException.getMessage(), errnoException); } }
/** format network interface flags */ private static String formatFlags(NetworkInterface nic) throws SocketException { StringBuilder flags = new StringBuilder(); if (nic.isUp()) { flags.append("UP "); } if (nic.supportsMulticast()) { flags.append("MULTICAST "); } if (nic.isLoopback()) { flags.append("LOOPBACK "); } if (nic.isPointToPoint()) { flags.append("POINTOPOINT "); } if (nic.isVirtual()) { flags.append("VIRTUAL "); } flags.append("mtu:").append(nic.getMTU()); flags.append(" index:").append(nic.getIndex()); return flags.toString(); } }
/** Return all interfaces (and subinterfaces) on the system */ static List<NetworkInterface> getInterfaces() throws SocketException { List<NetworkInterface> all = new ArrayList<>(); addAllInterfaces(all, Collections.list(NetworkInterface.getNetworkInterfaces())); Collections.sort(all, (left, right) -> Integer.compare(left.getIndex(), right.getIndex())); return all; }
private static StructGroupReq makeGroupReq(InetAddress gr_group, NetworkInterface networkInterface) { int gr_interface = (networkInterface != null) ? networkInterface.getIndex() : 0; return new StructGroupReq(gr_interface, gr_group); }
private static StructGroupReq makeGroupReq(InetAddress gr_group, NetworkInterface networkInterface) { int gr_interface = (networkInterface != null) ? networkInterface.getIndex() : 0; return new StructGroupReq(gr_interface, gr_group); }
private static StructGroupReq makeGroupReq(InetAddress gr_group, NetworkInterface networkInterface) { int gr_interface = (networkInterface != null) ? networkInterface.getIndex() : 0; return new StructGroupReq(gr_interface, gr_group); }
/** * @param network Network interface index * @throws UnknownHostException */ private Inet6Address getByAddressForInterface(final NetworkInterface network, final InetAddress address) throws UnknownHostException { // Append network interface. Workaround for issue #8802 return Inet6Address.getByAddress(address.getHostAddress(), IPAddressUtil.textToNumericFormatV6(address.getHostAddress()), network.getIndex()); }
public static NetworkInterface getByIndex(int index) throws SocketException { if (index < 0) throw new IllegalArgumentException("Interface index can't be negative"); for(NetworkInterfaceState nis : VirtualNetwork.getInstance().getAllNetworkInterfaceStates()){ if(nis.getNetworkInterface().getIndex()==index){ return nis.getNetworkInterface(); } } return null; }