Skip to content

Fix bug with BoringSSLError.invalidSNIName exception #702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 21, 2020
Merged
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
20 changes: 19 additions & 1 deletion Sources/GRPC/ClientConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ extension ClientConnection {
logger: Logger
) -> ClientBootstrapProtocol {
// Provide a server hostname if we're using TLS. Prefer the override.
let serverHostname: String? = configuration.tls.map {
var serverHostname: String? = configuration.tls.map {
if let hostnameOverride = $0.hostnameOverride {
logger.debug("using hostname override for TLS", metadata: ["server-hostname": "\(hostnameOverride)"])
return hostnameOverride
Expand All @@ -371,6 +371,11 @@ extension ClientConnection {
return host
}
}

if let hostname = serverHostname, hostname.isIPAddress {
logger.debug("IP address cannot be used for TLS SNI extension. No host used", metadata: ["server-hostname": "\(hostname)"])
serverHostname = nil
}

let bootstrap = PlatformSupport.makeClientBootstrap(group: eventLoop)
// Enable SO_REUSEADDR and TCP_NODELAY.
Expand Down Expand Up @@ -576,3 +581,16 @@ extension HTTP2ToHTTP1ClientCodec.HTTPProtocol {
}
}
}

fileprivate extension String {
var isIPAddress: Bool {
// We need some scratch space to let inet_pton write into.
var ipv4Addr = in_addr()
var ipv6Addr = in6_addr()

return self.withCString { ptr in
return inet_pton(AF_INET, ptr, &ipv4Addr) == 1 ||
inet_pton(AF_INET6, ptr, &ipv6Addr) == 1
}
}
}