New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support UDP multicast. #618

Merged
merged 2 commits into from Sep 24, 2018

Conversation

Projects
None yet
3 participants
@Lukasa
Contributor

Lukasa commented Sep 19, 2018

Motivation:

A large number of very useful protocols are implemented using multicast
with UDP. As a result, it would be helpful to add support for joining and
leaving IP multicast groups using SwiftNIO.

Modifications:

  • Defines a MulticastChannel protocol for channels that support joining and
    leaving multicast groups.
  • Adds an implementation of MulticastChannel to DatagramChannel.
  • Adds a interfaceIndex property to NIONetworkInterface.
  • Adds if_nametoindex to the Posix enum.
  • Adds a isMulticast computed property to SocketAddress
  • Adds a demo multicast chat application.
  • Add a number of multicast-related socket options to SocketOptionProvider.

Result:

NIO users will be able to write channels that handle multicast UDP.

@Lukasa Lukasa added this to the 1.10.0 milestone Sep 19, 2018

@Lukasa Lukasa requested review from normanmaurer and weissi Sep 19, 2018

@weissi

looks really good, didn't manage to go through everything but need to head to a meeting now

@@ -337,6 +337,23 @@ public enum ChannelLifecycleError: Error {
case inappropriateOperationForState
}
/// This should be inside of `ChannelError` but we keep it separate to not break API.
// TODO: For 2.0: bring this inside of `ChannelError`.

This comment has been minimized.

@weissi

weissi Sep 19, 2018

Contributor

mind filing an issue for that with the 2.0 milestone? Just so we won't forget and then we get nice github support of how many outstanding things

This comment has been minimized.

@Lukasa

Lukasa Sep 19, 2018

Contributor

Sure, see #620.

/// An attempt was made to join a multicast group that does not correspond to a multicast
/// address.
case notAMulticastAddress(SocketAddress)

This comment has been minimized.

@weissi

weissi Sep 19, 2018

Contributor

maybe illegalMulticastAddress?

This comment has been minimized.

@normanmaurer

normanmaurer Sep 20, 2018

Member

or invalidMulticastAddress

/// - parameters:
/// - group: The IP address corresponding to the relevant multicast group.
/// - returns: An `EventLoopFuture` that will be notified once the operation is complete.
func joinGroup(_ group: SocketAddress) -> EventLoopFuture<Void>

This comment has been minimized.

@weissi

weissi Sep 19, 2018

Contributor

shouldn't this be only in the proto extension? There's only one good implementation for that

/// - group: The IP address corresponding to the relevant multicast group.
/// - interface: The interface on which to join the given group, or `nil` to allow the kernel to choose.
/// - returns: An `EventLoopFuture` that will be notified once the operation is complete.
func joinGroup(_ group: SocketAddress, interface: NIONetworkInterface?) -> EventLoopFuture<Void>

This comment has been minimized.

@weissi

weissi Sep 19, 2018

Contributor

shouldn't this be only in the proto extension? There's only one good implementation for that

/// - parameters:
/// - group: The IP address corresponding to the relevant multicast group.
/// - returns: An `EventLoopFuture` that will be notified once the operation is complete.
func leaveGroup(_ group: SocketAddress) -> EventLoopFuture<Void>

This comment has been minimized.

@weissi

weissi Sep 19, 2018

Contributor

shouldn't this be only in the proto extension? There's only one good implementation for that

if eventLoop.inEventLoop {
self.performGroupOperation0(group, interface: interface, promise: promise, operation: .join)
} else {
eventLoop.execute { self.performGroupOperation0(group, interface: interface, promise: promise, operation: .join) }

This comment has been minimized.

@weissi

weissi Sep 19, 2018

Contributor

newlines?

if eventLoop.inEventLoop {
self.performGroupOperation0(group, interface: interface, promise: promise, operation: .leave)
} else {
eventLoop.execute { self.performGroupOperation0(group, interface: interface, promise: promise, operation: .leave) }

This comment has been minimized.

@weissi

weissi Sep 19, 2018

Contributor

newlines?

// IPv6 binding with no specific interface requested.
let multicastRequest = ipv6_mreq(ipv6mr_multiaddr: groupAddress.address.sin6_addr, ipv6mr_interface: 0)
try self.socket.setOption(level: CInt(IPPROTO_IPV6), name: operation.optionName(level: IPPROTO_IPV6), value: multicastRequest)
case (.v4, .some(.v6)), (.v6, .some(.v4)), (.v4, .some(.unixDomainSocket)), (.v6, .some(.unixDomainSocket)):

This comment has been minimized.

@weissi

weissi Sep 19, 2018

Contributor

what's the odds that the 4.0 or 4.1 compiler can't see this is exhaustive ;)

//===----------------------------------------------------------------------===//
import NIO

This comment has been minimized.

@weissi

weissi Sep 19, 2018

Contributor

nit: extra newline

// guard case .v4(let targetAddress) = targetInterface.address else {
// fatalError("Parsed target address as non-IPv4: \(targetInterface.address)")
// }
// datagramBootstrap = datagramBootstrap.channelOption(ChannelOptions.socket(SocketOptionLevel(IPPROTO_IP), IP_MULTICAST_IF), value: targetAddress.address.sin_addr)

This comment has been minimized.

@weissi

weissi Sep 19, 2018

Contributor

do we want this commented out code?

@Lukasa

This comment has been minimized.

Contributor

Lukasa commented Sep 20, 2018

Ok, ready for re-review.

@weissi

almost there, just one minor naming thing

///
/// - returns: An `EventLoopFuture` containing the value of the socket option, or
/// any error that occurred while retrieving the socket option.
public func getIpMulticastIf() -> EventLoopFuture<in_addr> {

This comment has been minimized.

@weissi

weissi Sep 20, 2018

Contributor

Swift naming conventions say getIPMulticastIF as abbreviations are all caps.

///
/// - returns: An `EventLoopFuture` containing the value of the socket option, or
/// any error that occurred while retrieving the socket option.
public func getIpv6MulticastLoop() -> EventLoopFuture<CUnsignedInt> {

This comment has been minimized.

@weissi

weissi Sep 20, 2018

Contributor

here IPV6

case unknownLocalAddress
/// The address family of the multicast group was not valid for this `Channel`.
case badMulticastGroupAddressFamily

This comment has been minimized.

@normanmaurer

normanmaurer Sep 20, 2018

Member

should this also take the family as parameter (just like we did for notAMulticastAddress) ?

This comment has been minimized.

@Lukasa

Lukasa Sep 20, 2018

Contributor

It could, but it would really need both the channel address family and the provided address family to make much sense. Should we do that?

This comment has been minimized.

@normanmaurer

normanmaurer Sep 24, 2018

Member

Nah... I think that would be too much :) Ship it !

case badMulticastGroupAddressFamily
/// The address family of the provided multicast group join is not valid for this `Channel`.
case badInterfaceAddressFamily

This comment has been minimized.

@normanmaurer

normanmaurer Sep 20, 2018

Member

Same question as above

/// An attempt was made to join a multicast group that does not correspond to a multicast
/// address.
case notAMulticastAddress(SocketAddress)

This comment has been minimized.

@normanmaurer

normanmaurer Sep 20, 2018

Member

or invalidMulticastAddress

Support UDP multicast.
Motivation:

A large number of very useful protocols are implemented using multicast
with UDP. As a result, it would be helpful to add support for joining and
leaving IP multicast groups using SwiftNIO.

Modifications:

- Defines a MulticastChannel protocol for channels that support joining and
  leaving multicast groups.
- Adds an implementation of MulticastChannel to DatagramChannel.
- Adds a interfaceIndex property to NIONetworkInterface.
- Adds if_nametoindex to the Posix enum.
- Adds a isMulticast computed property to SocketAddress
- Adds a demo multicast chat application.
- Add a number of multicast-related socket options to SocketOptionProvider.

Result:

NIO users will be able to write channels that handle multicast UDP.
@weissi

weissi approved these changes Sep 21, 2018

thanks very much, I'm happy this!

@Lukasa Lukasa merged commit c73eb57 into apple:master Sep 24, 2018

3 checks passed

pull request validation (4.0.3) Build finished.
Details
pull request validation (4.1) Build finished.
Details
pull request validation (4.2) Build finished.
Details

@Lukasa Lukasa deleted the Lukasa:cb-udp-multicast branch Sep 24, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment