Skip to content

Commit

Permalink
add ipv6_bound_if option and fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnzhou committed Sep 30, 2024
1 parent d2a7b7f commit 10d3417
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
3 changes: 3 additions & 0 deletions Sources/LCLPing/HTTP/HTTPPingClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ extension HTTPPingClient {
continue
}
}
if self.device != nil {
break
}
}
}

Expand Down
16 changes: 6 additions & 10 deletions Sources/LCLPing/HTTP/NIOHTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,27 +200,23 @@ final class NIOHTTPClient: Pingable {
}

if let device = self.configuration.device {
#if canImport(Darwin)
switch device.address {
case .v4:
#if canImport(Darwin)
return channel.setOption(.ipOption(.ip_bound_if), value: CInt(device.interfaceIndex))
#elseif canImport(Glibc)
return channel.setOption(.ipOption(.so_bindtodevice), value: device.interfaceIndex)
#endif
case .v6:
#if canImport(Darwin)
return channel.setOption(.ipv6Option(.ip_bound_if), value: CInt(device.interfaceIndex))
#elseif canImport(Glibc)
return channel.setOption(.ipv6Option(.so_bindtodevice), value: device.interfaceIndex)
#endif
return channel.setOption(.ipv6Option(.ipv6_bound_if), value: CInt(device.interfaceIndex))
case .unixDomainSocket:
self.stateLock.withLock {
self.state = .error
}
return channel.eventLoop.makeFailedFuture(PingError.httpBindToUnixDomainSocket)
return channel.eventLoop.makeFailedFuture(PingError.icmpBindToUnixDomainSocket)
default:
()
}
#elseif canImport(Glibc)
return channel.setOption(.socketOption(.so_bindtodevice), value: device.interfaceIndex)
#endif
}

return channel.eventLoop.makeSucceededVoidFuture()
Expand Down
17 changes: 8 additions & 9 deletions Sources/LCLPing/ICMP/ICMPPingClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,12 @@ public final class ICMPPingClient: Pingable {
}

if let device = self.configuration.device {
#if canImport(Darwin)
switch device.address {
case .v4:
#if canImport(Darwin)
return channel.setOption(.ipOption(.ip_bound_if), value: CInt(device.interfaceIndex))
#elseif canImport(Glibc)
return channel.setOption(.ipOption(.so_bindtodevice), value: device.interfaceIndex)
#endif
case .v6:
#if canImport(Darwin)
return channel.setOption(.ipv6Option(.ip_bound_if), value: CInt(device.interfaceIndex))
#elseif canImport(Glibc)
return channel.setOption(.ipv6Option(.so_bindtodevice), value: device.interfaceIndex)
#endif
return channel.setOption(.ipv6Option(.ipv6_bound_if), value: CInt(device.interfaceIndex))
case .unixDomainSocket:
self.stateLock.withLock {
self.state = .error
Expand All @@ -197,6 +190,9 @@ public final class ICMPPingClient: Pingable {
default:
()
}
#elseif canImport(Glibc)
return channel.setOption(.socketOption(.so_bindtodevice), value: device.interfaceIndex)
#endif
}

return channel.eventLoop.makeSucceededVoidFuture()
Expand Down Expand Up @@ -270,6 +266,9 @@ extension ICMPPingClient {
continue
}
}
if self.device != nil {
break
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions Sources/LCLPing/Utilities/LCLPing+SocketOption.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import NIOCore
extension NIOBSDSocket.Option {
#if canImport(Darwin)
public static let ip_bound_if: NIOBSDSocket.Option = Self(rawValue: IP_BOUND_IF)
public static let ipv6_bound_if: NIOBSDSocket.Option = Self(rawValue: IPV6_BOUND_IF)
#elseif canImport(Glibc)
public static let so_bindtodevice = Self(rawValue: SO_BINDTODEVICE)
#endif
Expand Down

0 comments on commit 10d3417

Please sign in to comment.