NIONetworkInterface: Return if multicast is supported (#887)

this is a fixup and replacement of #650. We need to get this in because NIO converges.

Support for Interface to report if Multicast is supported. Channel can provide Error if Interface doesn't support Multicast.

Motivation:

Allows for checking if the interface you are setting up for Multicast even supports Multicast in the first place. In multiple projects we have done with Multicast it helps as we still run into some network managers that shutdown multicast support.

Modifications:

Interface exposes a new variable to determine if it supports Multicast.
performGroupOperation0 will check if interface is not nil and supports multicast, otherwise will throw error that the interface doesn't support Multicast

Result:

This should hopefully better inform if the Multicast attempt is going to even work.
This commit is contained in:
Johannes Weiss authored and GitHub committed 2019-03-08 16:53:55 +00:00
1 parent acb37911c0
commit 15c1522cb4
6 files changed
+137 -25

No files matched your search

+3
View File
@@ -337,6 +337,9 @@ public enum ChannelError: Error {
/// address.
case illegalMulticastAddress(SocketAddress)
/// Multicast is not supported on Interface
case multicastNotSupported(NIONetworkInterface)
/// An operation that was inappropriate given the current `Channel` state was attempted.
case inappropriateOperationForState
+9
View File
@@ -62,6 +62,9 @@ public final class NIONetworkInterface {
/// instead.
public let pointToPointDestinationAddress: SocketAddress?
/// If the Interface supports Multicast
public let multicastSupported: Bool
/// The index of the interface, as provided by `if_nametoindex`.
public let interfaceIndex: Int
@@ -94,6 +97,12 @@ public final class NIONetworkInterface {
self.pointToPointDestinationAddress = nil
}
if (caddr.ifa_flags & UInt32(IFF_MULTICAST)) != 0 {
self.multicastSupported = true
} else {
self.multicastSupported = false
}
do {
self.interfaceIndex = Int(try Posix.if_nametoindex(caddr.ifa_name))
} catch {
+8
View File
@@ -863,6 +863,14 @@ extension DatagramChannel: MulticastChannel {
return
}
/// Check if the interface supports multicast
if let interface = interface {
guard interface.multicastSupported else {
promise?.fail(ChannelError.multicastNotSupported(interface))
return
}
}
// We need to check that we have the appropriate address types in all cases. They all need to overlap with
// the address type of this channel, or this cannot work.
guard let localAddress = self.localAddress else {
+65 -6
View File
@@ -40,7 +40,7 @@ final class MulticastTest: XCTestCase {
}
override func tearDown() {
try? self.group.syncShutdownGracefully()
XCTAssertNoThrow(try self.group.syncShutdownGracefully())
}
struct NoSuchInterfaceError: Error { }
@@ -165,14 +165,40 @@ final class MulticastTest: XCTestCase {
func testCanJoinBasicMulticastGroupIPv4() throws {
let multicastInterface = try assertNoThrowWithValue(self.interfaceForAddress(address: "127.0.0.1"))
guard multicastInterface.multicastSupported else {
// alas, we don't support multicast, let's skip but test the right error is thrown
XCTAssertThrowsError(try self.bindMulticastChannel(host: "0.0.0.0",
port: 0,
multicastAddress: "224.0.2.66",
interface: multicastInterface).wait()) { error in
if case .some(.multicastNotSupported(let actualInterface)) = error as? ChannelError {
XCTAssertEqual(multicastInterface, actualInterface)
} else {
XCTFail("unexpected error: \(error)")
}
}
return
}
// We avoid the risk of interference due to our all-addresses bind by only joining this multicast
// group on the loopback.
let listenerChannel = try assertNoThrowWithValue(self.bindMulticastChannel(host: "0.0.0.0",
let listenerChannel: Channel
do {
listenerChannel = try assertNoThrowWithValue(self.bindMulticastChannel(host: "0.0.0.0",
port: 0,
multicastAddress: "224.0.2.66",
interface: multicastInterface).wait())
// no error, that's great
} catch {
if case .some(.multicastNotSupported(_)) = error as? ChannelError {
XCTFail("network interface (\(multicastInterface)) claims we support multicast but: \(error)")
} else {
XCTFail("unexpected error: \(error)")
}
return
}
defer {
XCTAssertNoThrow(try listenerChannel.close().wait())
}
@@ -200,13 +226,38 @@ final class MulticastTest: XCTestCase {
}
let multicastInterface = try assertNoThrowWithValue(self.interfaceForAddress(address: "::1"))
guard multicastInterface.multicastSupported else {
// alas, we don't support multicast, let's skip but test the right error is thrown
// We avoid the risk of interference due to our all-addresses bind by only joining this multicast
// group on the loopback.
let listenerChannel = try assertNoThrowWithValue(self.bindMulticastChannel(host: "::1",
XCTAssertThrowsError(try self.bindMulticastChannel(host: "::1",
port: 0,
multicastAddress: "ff12::beeb",
interface: multicastInterface).wait()) { error in
if case .some(.multicastNotSupported(let actualInterface)) = error as? ChannelError {
XCTAssertEqual(multicastInterface, actualInterface)
} else {
XCTFail("unexpected error: \(error)")
}
}
return
}
let listenerChannel: Channel
do {
// We avoid the risk of interference due to our all-addresses bind by only joining this multicast
// group on the loopback.
listenerChannel = try assertNoThrowWithValue(self.bindMulticastChannel(host: "::1",
port: 0,
multicastAddress: "ff12::beeb",
interface: multicastInterface).wait())
} catch {
if case .some(.multicastNotSupported(_)) = error as? ChannelError {
XCTFail("network interface (\(multicastInterface)) claims we support multicast but: \(error)")
} else {
XCTFail("unexpected error: \(error)")
}
return
}
defer {
XCTAssertNoThrow(try listenerChannel.close().wait())
}
@@ -229,6 +280,10 @@ final class MulticastTest: XCTestCase {
func testCanLeaveAnIPv4MulticastGroup() throws {
let multicastInterface = try assertNoThrowWithValue(self.interfaceForAddress(address: "127.0.0.1"))
guard multicastInterface.multicastSupported else {
// alas, we don't support multicast, let's skip
return
}
// We avoid the risk of interference due to our all-addresses bind by only joining this multicast
// group on the loopback.
@@ -268,6 +323,10 @@ final class MulticastTest: XCTestCase {
}
let multicastInterface = try assertNoThrowWithValue(self.interfaceForAddress(address: "::1"))
guard multicastInterface.multicastSupported else {
// alas, we don't support multicast, let's skip
return
}
// We avoid the risk of interference due to our all-addresses bind by only joining this multicast
// group on the loopback.
+3 -1
View File
@@ -26,6 +26,9 @@ class NIOAnyDebugTest: XCTestCase {
XCTAssertTrue(wrappedInNIOAnyBlock(bb).hasSuffix(" }"))
let fileHandle = NIOFileHandle(descriptor: 1)
defer {
XCTAssertNoThrow(_ = try fileHandle.takeDescriptorOwnership())
}
let fileRegion = FileRegion(fileHandle: fileHandle, readerIndex: 1, endIndex: 5)
XCTAssertEqual(wrappedInNIOAnyBlock(fileRegion), wrappedInNIOAnyBlock("""
FileRegion { \
@@ -37,7 +40,6 @@ class NIOAnyDebugTest: XCTestCase {
readerIndex: \(fileRegion.readerIndex), \
endIndex: \(fileRegion.endIndex) }
"""))
try fileHandle.close()
let socketAddress = try SocketAddress(unixDomainSocketPath: "socketAdress")
let envelopeByteBuffer = ByteBufferAllocator().byteBuffer(string: "envelope buffer")
+49 -18
View File
@@ -63,26 +63,34 @@ final class SocketOptionProviderTest: XCTestCase {
// joins/leaves that may eventually lead to an ENOMEM and a spurious test failure. As joining/leaving groups on loopback
// interfaces does not require IGMP joins/leaves, forcing these joins onto the loopback interface saves us from this
// risk.
let v4LoopbackAddress = try! SocketAddress(ipAddress: "127.0.0.1", port: 0)
let v6LoopbackAddress = try! SocketAddress(ipAddress: "::1", port: 0)
let v4LoopbackInterface = try! System.enumerateInterfaces().filter { $0.address == v4LoopbackAddress }.first!
let v4LoopbackAddress = try! assertNoThrowWithValue(SocketAddress(ipAddress: "127.0.0.1", port: 0))
let v6LoopbackAddress = try! assertNoThrowWithValue(SocketAddress(ipAddress: "::1", port: 0))
let v4LoopbackInterface = try! assertNoThrowWithValue(System.enumerateInterfaces().filter {
$0.address == v4LoopbackAddress
}.first)!
self.ipv4DatagramChannel = try? assertNoThrowWithValue(
DatagramBootstrap(group: group).bind(host: "127.0.0.1", port: 0).flatMap { channel in
return (channel as! MulticastChannel).joinGroup(try! SocketAddress(ipAddress: "224.0.2.66", port: 0), interface: v4LoopbackInterface).map { channel }
// Only run the setup if the loopback interface supports multicast
if v4LoopbackAddress.isMulticast {
self.ipv4DatagramChannel = try? assertNoThrowWithValue(
DatagramBootstrap(group: group).bind(host: "127.0.0.1", port: 0).flatMap { channel in
return (channel as! MulticastChannel).joinGroup(try! SocketAddress(ipAddress: "224.0.2.66", port: 0), interface: v4LoopbackInterface).map { channel }
}.wait()
)
}
// Only run the setup if the loopback interface supports multicast
if v6LoopbackAddress.isMulticast {
// The IPv6 setup is allowed to fail, some hosts don't have IPv6.
let v6LoopbackInterface = try? assertNoThrowWithValue(System.enumerateInterfaces().filter { $0.address == v6LoopbackAddress }.first)
self.ipv6DatagramChannel = try? DatagramBootstrap(group: group).bind(host: "::1", port: 0).flatMap { channel in
return (channel as! MulticastChannel).joinGroup(try! SocketAddress(ipAddress: "ff12::beeb", port: 0), interface: v6LoopbackInterface).map { channel }
}.wait()
)
// The IPv6 setup is allowed to fail, some hosts don't have IPv6.
let v6LoopbackInterface = try! System.enumerateInterfaces().filter { $0.address == v6LoopbackAddress }.first
self.ipv6DatagramChannel = try? DatagramBootstrap(group: group).bind(host: "::1", port: 0).flatMap { channel in
return (channel as! MulticastChannel).joinGroup(try! SocketAddress(ipAddress: "ff12::beeb", port: 0), interface: v6LoopbackInterface).map { channel }
}.wait()
}
}
override func tearDown() {
XCTAssertNoThrow(try ipv6DatagramChannel?.close().wait())
XCTAssertNoThrow(try ipv4DatagramChannel.close().wait())
XCTAssertNoThrow(try ipv4DatagramChannel?.close().wait())
XCTAssertNoThrow(try clientChannel.close().wait())
XCTAssertNoThrow(try serverChannel.close().wait())
XCTAssertNoThrow(try group.syncShutdownGracefully())
@@ -159,15 +167,18 @@ final class SocketOptionProviderTest: XCTestCase {
}
func testSoIpMulticastIf() throws {
let channel = self.ipv4DatagramChannel!
guard let channel = self.ipv4DatagramChannel else {
// no multicast support
return
}
let provider = try assertNoThrowWithValue(self.ipv4MulticastProvider())
let address: in_addr
switch channel.localAddress! {
case .v4(let addr):
switch channel.localAddress {
case .some(.v4(let addr)):
address = addr.address.sin_addr
default:
XCTFail("Local address must be IPv4!")
XCTFail("Local address must be IPv4, but is \(channel.localAddress.debugDescription)")
return
}
@@ -179,6 +190,10 @@ final class SocketOptionProviderTest: XCTestCase {
}
func testIpMulticastTtl() throws {
guard self.ipv4DatagramChannel != nil else {
// alas, no multicast, let's skip.
return
}
let provider = try assertNoThrowWithValue(self.ipv4MulticastProvider())
XCTAssertNoThrow(try provider.setIPMulticastTTL(6).flatMap {
provider.getIPMulticastTTL()
@@ -188,6 +203,10 @@ final class SocketOptionProviderTest: XCTestCase {
}
func testIpMulticastLoop() throws {
guard self.ipv4DatagramChannel != nil else {
// alas, no multicast, let's skip.
return
}
let provider = try assertNoThrowWithValue(self.ipv4MulticastProvider())
XCTAssertNoThrow(try provider.setIPMulticastLoop(1).flatMap {
provider.getIPMulticastLoop()
@@ -201,6 +220,10 @@ final class SocketOptionProviderTest: XCTestCase {
// Skip on systems without IPv6.
return
}
guard self.ipv6DatagramChannel != nil else {
// alas, no multicast, let's skip.
return
}
// TODO: test this when we know what the interface indices are.
let loopbackAddress = try assertNoThrowWithValue(SocketAddress(ipAddress: "::1", port: 0))
@@ -221,6 +244,10 @@ final class SocketOptionProviderTest: XCTestCase {
// Skip on systems without IPv6.
return
}
guard self.ipv6DatagramChannel != nil else {
// alas, no multicast, let's skip.
return
}
XCTAssertNoThrow(try provider.setIPv6MulticastHops(6).flatMap {
provider.getIPv6MulticastHops()
@@ -234,6 +261,10 @@ final class SocketOptionProviderTest: XCTestCase {
// Skip on systems without IPv6.
return
}
guard self.ipv6DatagramChannel != nil else {
// alas, no multicast, let's skip.
return
}
XCTAssertNoThrow(try provider.setIPv6MulticastLoop(1).flatMap {
provider.getIPv6MulticastLoop()