Skip to content

Commit

Permalink
fix bind to device on linux
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnzhou committed Oct 1, 2024
1 parent a1f7981 commit 74e4a79
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Sources/LCLPing/HTTP/NIOHTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ final class NIOHTTPClient: Pingable {
()
}
#elseif canImport(Glibc)
return channel.setOption(.socketOption(.so_bindtodevice), value: device.interfaceIndex)
return (channel as! SocketOptionProvider).setBindToDevice(device.name)
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/LCLPing/ICMP/ICMPPingClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public final class ICMPPingClient: Pingable {
()
}
#elseif canImport(Glibc)
return channel.setOption(.socketOption(.so_bindtodevice), value: device.interfaceIndex)
return (channel as! SocketOptionProvider).setBindToDevice(device.name)
#endif
}

Expand Down
14 changes: 14 additions & 0 deletions Sources/LCLPing/Utilities/LCLPing+SocketOption.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,17 @@ extension NIOBSDSocket.Option {
public static let so_bindtodevice = Self(rawValue: SO_BINDTODEVICE)
#endif
}

extension SocketOptionProvider {
#if canImport(Glibc)
/// Sets the socket option SO_BINDTODEVICE to `value`.
///
/// - parameters:
/// - value: The value to set SO_BINDTODEVICE to.
/// - returns: An `EventLoopFuture` that fires when the option has been set,
/// or if an error has occurred.
public func setBindToDevice(_ value: String) -> EventLoopFuture<Void> {
self.unsafeSetSocketOption(level: .socket, name: .so_bindtodevice, value: value)
}
#endif
}

0 comments on commit 74e4a79

Please sign in to comment.