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 9d1f281 commit c308210
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
14 changes: 14 additions & 0 deletions Sources/WebSocketKit/WebSocket+SocketOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,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
}
2 changes: 1 addition & 1 deletion Sources/WebSocketKit/WebSocketClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public final class WebSocketClient: Sendable {
return channel.eventLoop.makeFailedFuture(WebSocketClient.Error.invalidAddress)
}
#elseif canImport(Glibc)
return channel.setOption(.socketOption(.so_bindtodevice), value: device.interfaceIndex)
return (channel as! SocketOptionProvider).setBindToDevice(device.name)
#endif
} else {
return channel.eventLoop.makeSucceededVoidFuture()
Expand Down

0 comments on commit c308210

Please sign in to comment.