/** * Create a new broadcaster * * @param address - multicast group address * @param srcAddress - address of interface we should use to broadcast. * @param port - udp port to use * @param ttl - packet ttl * @throws IOException */ public JdpBroadcaster(InetAddress address, InetAddress srcAddress, int port, int ttl) throws IOException, JdpException { this.addr = address; this.port = port; ProtocolFamily family = (address instanceof Inet6Address) ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET; channel = DatagramChannel.open(family); channel.setOption(StandardSocketOptions.SO_REUSEADDR, true); channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl); // with srcAddress equal to null, this constructor do exactly the same as // if srcAddress is not passed if (srcAddress != null) { // User requests particular interface to bind to NetworkInterface interf = NetworkInterface.getByInetAddress(srcAddress); try { channel.bind(new InetSocketAddress(srcAddress, 0)); } catch (UnsupportedAddressTypeException ex) { throw new JdpException("Unable to bind to source address"); } channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf); } }
public static InetAddress getDefaultRoute(Class<? extends InetAddress> type) { InetAddress target = null; ProtocolFamily family = type == Inet6Address.class ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET; try(DatagramChannel chan=DatagramChannel.open(family)) { if(type == Inet4Address.class) target = InetAddress.getByAddress(new byte[] {8,8,8,8}); if(type == Inet6Address.class) target = InetAddress.getByName("2001:4860:4860::8888"); chan.connect(new InetSocketAddress(target,63)); InetSocketAddress soa = (InetSocketAddress) chan.getLocalAddress(); InetAddress local = soa.getAddress(); if(type.isInstance(local) && !local.isAnyLocalAddress()) return local; return null; } catch (IOException e) { e.printStackTrace(); return null; } }
public LocalServiceDiscoveryInfo( Set<SocketChannelConnectionAcceptor> socketAcceptors, Collection<AnnounceGroup> announceGroups) { this.localPorts = unmodifiableSet(collectLocalPorts(socketAcceptors)); Collection<NetworkInterface> networkInterfaces = new HashSet<>(); boolean acceptIP4 = false; boolean acceptIP6 = false; for (SocketChannelConnectionAcceptor acceptor : socketAcceptors) { networkInterfaces.add(acceptor.getNetworkInterface()); InetSocketAddress address = acceptor.getLocalAddress(); ProtocolFamily protocolFamily = InternetProtocolUtils.getProtocolFamily(address.getAddress()); if (protocolFamily == StandardProtocolFamily.INET) { acceptIP4 = true; } else { acceptIP6 = true; } if (acceptIP4 && acceptIP6) { break; // no need to look further } } this.compatibleGroups = unmodifiableCollection(collectCompatibleGroups(announceGroups, acceptIP4, acceptIP6)); this.networkInterfaces = unmodifiableCollection(networkInterfaces); }
public static SocketBuffer create (ProtocolFamily family, int flags) { int tpdu_length = Packet.SIZEOF_PGM_HEADER; if (StandardProtocolFamily.INET6 == family) tpdu_length += SIZEOF_SPM6_HEADER; else tpdu_length += SIZEOF_SPM_HEADER; if (Packet.PGM_OPT_FIN == flags) { tpdu_length += Packet.SIZEOF_PGM_OPT_LENGTH; /* End of session */ if (Packet.PGM_OPT_FIN == flags) tpdu_length += Packet.SIZEOF_PGM_OPT_HEADER + Packet.SIZEOF_PGM_OPT_FIN; } SocketBuffer skb = new SocketBuffer (tpdu_length); skb.setHeaderOffset (0); skb.getHeader().setType (Packet.PGM_SPM); skb.reserve (Packet.SIZEOF_PGM_HEADER); return skb; }
public static InetAddress getMulticastEnabledNodeAddress (ProtocolFamily family) throws UnknownHostException, SocketException { InetAddress res[] = getNodeAddress (family); /* iff one address return that independent of multicast support */ if (res.length == 1) return res[0]; for (InetAddress addr : res) { /* For each node address find matching interface and test flags */ java.net.NetworkInterface ni = java.net.NetworkInterface.getByInetAddress (addr); if (ni.supportsMulticast()) return addr; } /* Use last address as fallback */ return res[res.length - 1]; }
public Socket (ProtocolFamily family) throws IOException { LOG.debug ("Socket (family: {})", family); this.family = family; this.canSendData = true; this.canSendNak = true; this.canReceiveData = true; this.dataDestinationPort = Packet.DEFAULT_DATA_DESTINATION_PORT; this.tsi = new TransportSessionId (null, Packet.DEFAULT_DATA_SOURCE_PORT); LOG.trace (NETWORK_MARKER, "Opening UDP encapsulated sockets."); this.recv_sock = DatagramChannel.open (this.family); LOG.trace (NETWORK_MARKER, "Set socket sharing."); this.recv_sock.setOption (StandardSocketOptions.SO_REUSEADDR, true); this.recv_sock.configureBlocking (false); this.send_sock = new MulticastSocket (); LOG.debug ("PGM socket successfully created."); }
/** * Create a new broadcaster * * @param address - multicast group address * @param srcAddress - address of interface we should use to broadcast. * @param port - udp port to use * @param ttl - packet ttl * @throws IOException */ public JdpBroadcaster(InetAddress address, InetAddress srcAddress, int port, int ttl) throws IOException, JdpException { this.addr = address; this.port = port; ProtocolFamily family = (address instanceof Inet6Address) ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET; channel = DatagramChannel.open(family); channel.setOption(StandardSocketOptions.SO_REUSEADDR, true); channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl); // with srcAddress equal to null, this constructor do exactly the same as // if srcAddress is not passed if (srcAddress != null) { // User requests particular interface to bind to NetworkInterface interf = NetworkInterface.getByInetAddress(srcAddress); if (interf == null) { throw new JdpException("Unable to get network interface for " + srcAddress.toString()); } if (!interf.isUp()) { throw new JdpException(interf.getName() + " is not up."); } if (!interf.supportsMulticast()) { throw new JdpException(interf.getName() + " does not support multicast."); } try { channel.bind(new InetSocketAddress(srcAddress, 0)); } catch (UnsupportedAddressTypeException ex) { throw new JdpException("Unable to bind to source address"); } channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf); } }
private DHTtype(String shortName, int nodeslength, int addresslength, Class<? extends InetAddress> addresstype, int header, int maxSize, ProtocolFamily family) { this.shortName = shortName; this.NODES_ENTRY_LENGTH = nodeslength; this.PREFERRED_ADDRESS_TYPE = addresstype; this.ADDRESS_ENTRY_LENGTH = addresslength; this.HEADER_LENGTH = header; this.MAX_PACKET_SIZE = maxSize; this.PROTO_FAMILY = family; }
private synchronized DatagramChannel getChannel() throws IOException { if (channel == null || !channel.isOpen()) { if (shutdown.get()) { throw new IllegalStateException("Channel has been shut down"); } ProtocolFamily protocolFamily = InternetProtocolUtils.getProtocolFamily(group.getAddress().getAddress()); DatagramChannel _channel = selector.provider().openDatagramChannel(protocolFamily); _channel.setOption(StandardSocketOptions.SO_REUSEADDR, true); // bind to any-local before setting TTL int port = group.getAddress().getPort(); if (protocolFamily == StandardProtocolFamily.INET) { _channel.bind(new InetSocketAddress(Inet4Address.getByName("0.0.0.0"), port)); } else { _channel.bind(new InetSocketAddress(Inet6Address.getByName("[::]"), port)); } int timeToLive = group.getTimeToLive(); if (timeToLive != 1) { _channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, timeToLive); } for (NetworkInterface iface : networkInterfaces) { _channel.join(group.getAddress().getAddress(), iface); } _channel.configureBlocking(false); channel = _channel; } return channel; }
/** * @return {@link StandardProtocolFamily#INET} for IPv4 address or {@link StandardProtocolFamily#INET6} for IPv6 address * @throws IllegalArgumentException if the address is neither IPv4 or IPv6 * @since 1.6 */ public static ProtocolFamily getProtocolFamily(InetAddress address) { if (address.getAddress().length == IP4_BYTES) { return StandardProtocolFamily.INET; } else if (address.getAddress().length == IP6_BYTES) { return StandardProtocolFamily.INET6; } else { throw new IllegalArgumentException("Can't determine protocol family for address: " + address); } }
/** * Convert the {@link InternetProtocolFamily}. This MUST only be called on jdk version >= 7. */ public static ProtocolFamily convert(InternetProtocolFamily family) { switch (family) { case IPv4: return StandardProtocolFamily.INET; case IPv6: return StandardProtocolFamily.INET6; default: throw new IllegalArgumentException(); } }
/** * Sets up the multicast channel * * @throws IOException * Something wrong occurred (bad address, bad port, ...) */ private void setupMulticast() throws IOException { // Compute the address family final ProtocolFamily family; if (pAddress instanceof Inet4Address) { // IPv4 family = StandardProtocolFamily.INET; } else if (pAddress instanceof Inet6Address) { // IPv6 family = StandardProtocolFamily.INET6; } else { // Unknown throw new SocketException("Unknown multicast group family"); } // Create the UDP channel pChannel = DatagramChannel.open(family); pChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true); pChannel.bind(new InetSocketAddress(pPort)); try { // Join the group on all interfaces for (final NetworkInterface itf : getMulticastInterfaces()) { pJoinedGroups.add(pChannel.join(pAddress, itf)); } } catch (final SocketException ex) { // Be nice... pChannel.close(); throw ex; } }
public static InetAddress[] getNodeAddress (ProtocolFamily family) throws UnknownHostException { LinkedList<InetAddress> list = new LinkedList<> (); String nodename = InetAddress.getLocalHost().getHostName(); for (InetAddress addr : InetAddress.getAllByName (nodename)) { if (StandardProtocolFamily.INET == family && addr instanceof Inet4Address) list.add (addr); else if (StandardProtocolFamily.INET6 == family && addr instanceof Inet6Address) list.add (addr); } return list.toArray (new InetAddress[list.size()]); }
public static SocketBuffer create (ProtocolFamily family, int tsdu_length) { int tpdu_length = Packet.calculateOffset (false, null) + tsdu_length; SocketBuffer skb = new SocketBuffer (tpdu_length); skb.setHeaderOffset (0); skb.getHeader().setType (Packet.PGM_ODATA); skb.getHeader().setTsduLength (tsdu_length); skb.reserve (Packet.calculateOffset (false, null)); skb.put (tsdu_length); skb.setOriginalDataOffset (Packet.SIZEOF_PGM_HEADER); return skb; }
public static int calculateOffset (boolean canFragment, @Nullable ProtocolFamily pgmcc_family) { int data_size = SIZEOF_PGM_HEADER + SIZEOF_PGM_DATA; int pkt_size = data_size; if (canFragment || (null != pgmcc_family)) pkt_size += SIZEOF_PGM_OPT_LENGTH + SIZEOF_PGM_OPT_HEADER; if (canFragment) pkt_size += SIZEOF_PGM_OPT_FRAGMENT; if (StandardProtocolFamily.INET == pgmcc_family) pkt_size += SIZEOF_PGM_OPT_PGMCC_DATA; else if (StandardProtocolFamily.INET6 == pgmcc_family) pkt_size += SIZEOF_PGM_OPT6_PGMCC_DATA; return pkt_size; }
public channelrecv (String[] args) throws IOException { InetAddress group = InetAddress.getByName (this.group); ProtocolFamily pf = group instanceof Inet4Address ? StandardProtocolFamily.INET : StandardProtocolFamily.INET6; NetworkInterface ni = NetworkInterface.getByName (this.adapter); if (null == ni) ni = NetworkInterface.getByInetAddress (InetAddress.getByName (this.adapter)); DatagramChannel dc = DatagramChannel.open (pf) .setOption (StandardSocketOptions.SO_REUSEADDR, true) .bind (new InetSocketAddress (this.port)) .setOption (StandardSocketOptions.IP_MULTICAST_IF, ni); dc.configureBlocking (false); @SuppressWarnings("unused") MembershipKey key = dc.join (group, ni); ByteBuffer buffer = ByteBuffer.allocateDirect (this.max_tpdu); Selector selector = Selector.open(); @SuppressWarnings("unused") SelectionKey sk = dc.register (selector, SelectionKey.OP_READ); while (true) { int keyCount = selector.select (1000); if (keyCount > 0) { selector.selectedKeys().clear(); SocketAddress source = dc.receive (buffer); buffer.flip(); byte[] bytes = new byte[buffer.remaining()]; buffer.get (bytes, 0, bytes.length); buffer.clear(); System.out.println ("packet: { " + "\"src\": \"" + source + "\"" + ", \"data\": \"" + new String (bytes, 0, bytes.length) + "\"" + ", \"length\": " + bytes.length + "" + " }"); } } }
RegistryKey(SocketOption<?> name, ProtocolFamily family) { this.name = name; this.family = family; }
public static OptionKey findOption(SocketOption<?> name, ProtocolFamily family) { RegistryKey key = new RegistryKey(name, family); return LazyInitialization.options.get(key); }
public DatagramChannel openDatagramChannel(ProtocolFamily family) throws IOException { return new DatagramChannelImpl(this, family); }
public DatagramChannel openDatagramChannel(ProtocolFamily family) throws IOException { return provider.openDatagramChannel(family); }