Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Sources/SwiftNetwork/Endpoint/NetlinkAddress.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct NetlinkAddress: IPAddress, Hashable, CustomDebugStringConvertible {
/// Creates a Netlink address from raw bytes.
init?(_ bytes: [UInt8]) {
if bytes.count >= NetlinkAddress.layoutSize {
#if os(Linux)
#if os(Linux) && NETLINK_ENABLED

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this and other changes actually widening the enablement of this code path rather than disabling it?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thought here was that enabling this flag would allow us to selectively re-enable this when adding it back in the future.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, for some reason I read the diff in reverse🙃. Makes sense.

let nl = bytes.withUnsafeBytes { $0.load(as: sockaddr_nl.self) }
self.init(nl)
#else
Expand All @@ -47,12 +47,12 @@ struct NetlinkAddress: IPAddress, Hashable, CustomDebugStringConvertible {
}
}

#if os(Linux)
#if os(Linux) && NETLINK_ENABLED
// Linux has a specific type: sockaddr_nl
init(_ sockaddr_nl: sockaddr_nl) {
self.address = sockaddr_nl
}
#elseif canImport(Darwin)
#else
// rtsock on Darwin uses sockaddr
// struct sockaddr route_dst = { .sa_len = 2, .sa_family = PF_ROUTE, .sa_data = { 0, } };
init(_ sockaddr: sockaddr) {
Expand All @@ -68,14 +68,14 @@ struct NetlinkAddress: IPAddress, Hashable, CustomDebugStringConvertible {
"NetlinkAddress"
}

#if os(Linux)
#if os(Linux) && NETLINK_ENABLED
var address: sockaddr_nl
var nl_pid: pid_t = 0
#else
var address: sockaddr
#endif

#if os(Linux)
#if os(Linux) && NETLINK_ENABLED
private static let layoutSize = MemoryLayout<sockaddr_nl>.size
#else
private static let layoutSize = MemoryLayout<sockaddr>.size
Expand Down
8 changes: 7 additions & 1 deletion Sources/SwiftNetwork/System/Linux/LinuxRoute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//
//===----------------------------------------------------------------------===//

#if os(Linux)
#if os(Linux) && canImport(Glibc) && NETLINK_ENABLED
import Glibc
internal import Logging
internal import SwiftNetworkLinuxShim
Expand Down Expand Up @@ -536,4 +536,10 @@ internal enum SystemRoute {
}
}
}
#elseif os(Linux)
internal enum SystemRoute {
static func routeGetInterfaceIndex(dst: any IPAddress, scopedIndex: UInt32 = 0) throws -> UInt32 {
0
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
#ifdef __linux__

#include <stdio.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#ifdef NETLINK_ENABLED
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#endif
#include <arpa/inet.h>

// Not exposed in Glibc.swiftmodule
Expand Down
6 changes: 5 additions & 1 deletion Tests/SwiftNetworkTests/SwiftNetworkInterfaceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ final class SwiftNetworkInterfaceTests: NetTestCase {
)
}

func testRouteGetInterfaceIndex() {
func testRouteGetInterfaceIndex() throws {
#if os(Linux) && !NETLINK_ENABLED
try XCTSkipIf(true)
#else
// Very basic localhost test
let v4Bytes: [UInt8] = [0x7F, 0x00, 0x00, 0x01]

Expand Down Expand Up @@ -154,5 +157,6 @@ final class SwiftNetworkInterfaceTests: NetTestCase {
// Use a v6 loopback address with a scoped interface index
let routeIndex4 = try! System.routeGetInterfaceIndex(dst: IPv6Address.loopback, scopedIndex: 1)
XCTAssertEqual(routeIndex4, 1)
#endif
}
}
Loading