diff --git a/Sources/CNIODarwin/include/CNIODarwin.h b/Sources/CNIODarwin/include/CNIODarwin.h index c2599c308..9ca2e45f4 100644 --- a/Sources/CNIODarwin/include/CNIODarwin.h +++ b/Sources/CNIODarwin/include/CNIODarwin.h @@ -48,6 +48,7 @@ extern const int CNIODarwin_IPTOS_ECN_ECT1; extern const int CNIODarwin_IPTOS_ECN_CE; extern const int CNIODarwin_IPV6_RECVPKTINFO; extern const int CNIODarwin_IPV6_PKTINFO; +extern const int CNIODarwin_IPV6_DONTFRAG; int CNIODarwin_sendmmsg(int sockfd, CNIODarwin_mmsghdr *msgvec, unsigned int vlen, int flags); int CNIODarwin_recvmmsg(int sockfd, CNIODarwin_mmsghdr *msgvec, unsigned int vlen, int flags, struct timespec *timeout); diff --git a/Sources/CNIODarwin/shim.c b/Sources/CNIODarwin/shim.c index 30b81bcb6..81c2cbd2d 100644 --- a/Sources/CNIODarwin/shim.c +++ b/Sources/CNIODarwin/shim.c @@ -90,6 +90,7 @@ const int CNIODarwin_IPTOS_ECN_ECT1 = IPTOS_ECN_ECT1; const int CNIODarwin_IPTOS_ECN_CE = IPTOS_ECN_CE; const int CNIODarwin_IPV6_RECVPKTINFO = IPV6_RECVPKTINFO; const int CNIODarwin_IPV6_PKTINFO = IPV6_PKTINFO; +const int CNIODarwin_IPV6_DONTFRAG = IPV6_DONTFRAG; const unsigned long CNIODarwin_IOCTL_VM_SOCKETS_GET_LOCAL_CID = IOCTL_VM_SOCKETS_GET_LOCAL_CID; diff --git a/Sources/NIOCore/BSDSocketAPI.swift b/Sources/NIOCore/BSDSocketAPI.swift index e655e7a81..2d9a766ef 100644 --- a/Sources/NIOCore/BSDSocketAPI.swift +++ b/Sources/NIOCore/BSDSocketAPI.swift @@ -102,6 +102,7 @@ private let sysInet_ntop: inet_ntop private let sysInet_pton: @convention(c) (CInt, UnsafePointer?, UnsafeMutableRawPointer?) -> CInt = inet_pton #elseif canImport(Darwin) +import CNIODarwin import Darwin private let sysInet_ntop: @@ -407,6 +408,28 @@ extension NIOBSDSocket.Option { } } +#if canImport(Darwin) +extension NIOBSDSocket.Option { + /// Set the don't fragment bit on outgoing IPv4 packets. + @inlinable + public static var ip_dontfrag: NIOBSDSocket.Option { + NIOBSDSocket.Option(rawValue: IP_DONTFRAG) + } +} +#endif + +#if os(Linux) +extension NIOBSDSocket.Option { + /// Set the path MTU discovery mode for IPv4, one of the `IP_PMTUDISC_*` values. + /// + /// - Seealso: `man 7 ip` (Linux) + @inlinable + public static var ip_mtu_discover: NIOBSDSocket.Option { + NIOBSDSocket.Option(rawValue: IP_MTU_DISCOVER) + } +} +#endif + // IPv6 Options extension NIOBSDSocket.Option { /// Add an IPv6 group membership. @@ -447,6 +470,34 @@ extension NIOBSDSocket.Option { } } +#if canImport(Darwin) || os(Linux) +extension NIOBSDSocket.Option { + /// Don't fragment outgoing IPv6 packets. + /// + /// - Seealso: https://datatracker.ietf.org/doc/html/rfc3542#section-11.2 + @inlinable + public static var ipv6_dontfrag: NIOBSDSocket.Option { + #if canImport(Darwin) + NIOBSDSocket.Option(rawValue: CNIODarwin_IPV6_DONTFRAG) + #else + NIOBSDSocket.Option(rawValue: IPV6_DONTFRAG) + #endif + } +} +#endif + +#if os(Linux) +extension NIOBSDSocket.Option { + /// Set the path MTU discovery mode for IPv6, one of the `IPV6_PMTUDISC_*` values. + /// + /// - Seealso: `man 7 ipv6` (Linux) + @inlinable + public static var ipv6_mtu_discover: NIOBSDSocket.Option { + NIOBSDSocket.Option(rawValue: IPV6_MTU_DISCOVER) + } +} +#endif + // TCP Options extension NIOBSDSocket.Option { /// Disables the Nagle algorithm for send coalescing. diff --git a/Tests/NIOPosixTests/DatagramChannelTests.swift b/Tests/NIOPosixTests/DatagramChannelTests.swift index 8b51627eb..ea17bd853 100644 --- a/Tests/NIOPosixTests/DatagramChannelTests.swift +++ b/Tests/NIOPosixTests/DatagramChannelTests.swift @@ -1013,6 +1013,44 @@ class DatagramChannelTests: XCTestCase { ) } + #if canImport(Darwin) + func testSetGetIPDontFragOption() throws { + let option = ChannelOptions.ipOption(.ip_dontfrag) + try self.firstChannel.setOption(option, value: 1).wait() + XCTAssertEqual(try self.firstChannel.getOption(option).wait(), 1) + } + #endif + + #if canImport(Darwin) || os(Linux) + func testSetGetIPv6DontFragOption() throws { + guard System.supportsIPv6 else { + return // need to skip IPv6 tests if we don't support it. + } + let channel = try self.buildChannel(group: self.group, host: "::1") + let option = ChannelOptions.Types.SocketOption(level: .ipv6, name: .ipv6_dontfrag) + try channel.setOption(option, value: 1).wait() + XCTAssertEqual(try channel.getOption(option).wait(), 1) + } + #endif + + #if os(Linux) + func testSetGetIPMTUDiscoverOption() throws { + let option = ChannelOptions.ipOption(.ip_mtu_discover) + try self.firstChannel.setOption(option, value: SocketOptionValue(IP_PMTUDISC_PROBE)).wait() + XCTAssertEqual(try self.firstChannel.getOption(option).wait(), SocketOptionValue(IP_PMTUDISC_PROBE)) + } + + func testSetGetIPv6MTUDiscoverOption() throws { + guard System.supportsIPv6 else { + return // need to skip IPv6 tests if we don't support it. + } + let channel = try self.buildChannel(group: self.group, host: "::1") + let option = ChannelOptions.Types.SocketOption(level: .ipv6, name: .ipv6_mtu_discover) + try channel.setOption(option, value: SocketOptionValue(IPV6_PMTUDISC_PROBE)).wait() + XCTAssertEqual(try channel.getOption(option).wait(), SocketOptionValue(IPV6_PMTUDISC_PROBE)) + } + #endif + private func testSimpleReceivePacketInfo(address: String) throws { // Fake sending packet to self on the loopback interface let expectedPacketInfo = try constructNIOPacketInfo(address: address)