/**
* 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);
}
}
项目:bt
文件:AddressUtils.java
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});
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);
}
项目:mldht
文件:AddressUtils.java
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,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 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,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,interf);
}
}
项目:bt
文件:DHT.java
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;
}
项目:bt
文件:AnnounceGroupChannel.java
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;
}
项目:bt
文件:InternetProtocolUtils.java
/**
* @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);
}
}
项目:mldht
文件:DHT.java
private DHTtype(String shortName,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;
}
项目:netty4.0.27Learn
文件:ProtocolFamilyConverter.java
/**
* 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();
}
}
项目:netty4study
文件:ProtocolFamilyConverter.java
/**
* 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();
}
}
项目:simple-netty-source
文件:ProtocolFamilyConverter.java
/**
* 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();
}
}
项目:netty-netty-5.0.0.Alpha1
文件:ProtocolFamilyConverter.java
/**
* 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();
}
}
项目:cohorte-remote-services
文件:MulticastHandler.java
/**
* 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,bytes.length);
buffer.clear();
System.out.println ("packet: { " +
"\"src\": \"" + source + "\"" +
",\"data\": \"" + new String (bytes,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);
}
项目:jdk8u-jdk
文件:SelectorProviderImpl.java
public DatagramChannel openDatagramChannel(ProtocolFamily family) throws IOException {
return new DatagramChannelImpl(this,family);
}
项目:jdk8u-jdk
文件:RmidViaInheritedChannel.java
public DatagramChannel openDatagramChannel(ProtocolFamily family)
throws IOException
{
return provider.openDatagramChannel(family);
}
项目:jdk8u-jdk
文件:InheritedChannelNotServerSocket.java
public DatagramChannel openDatagramChannel(ProtocolFamily family)
throws IOException
{
return provider.openDatagramChannel(family);
}
项目:openjdk-jdk10
文件:SelectorProviderImpl.java
public DatagramChannel openDatagramChannel(ProtocolFamily family) throws IOException {
return new DatagramChannelImpl(this,family);
}
项目:openjdk-jdk10
文件:InheritedChannelNotServerSocket.java
public DatagramChannel openDatagramChannel(ProtocolFamily family)
throws IOException
{
return provider.openDatagramChannel(family);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。