Files
swift-nio/Sources/Sockets/SocketAddresses.swift
T

70 lines
2.0 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Foundation
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
import Darwin
#elseif os(Linux)
import Glibc
#endif
public class SocketAddresses {
public class func newAddress(for host: String, on port: Int32) throws -> SocketAddress {
var info: UnsafeMutablePointer<addrinfo>?
/* FIXME: this is blocking! */
if getaddrinfo(host, String(port), nil, &info) != 0 {
// TODO: May may be able to return a bit more info to the caller. Let us just keep it simple for now
throw SocketAddressError.unknown
}
defer {
if info != nil {
freeaddrinfo(info)
}
}
if let info = info {
switch info.pointee.ai_family {
case AF_INET:
return info.pointee.ai_addr.withMemoryRebound(to: sockaddr_in.self, capacity: 1) { ptr in
return .v4(address: ptr.pointee)
}
case AF_INET6:
return info.pointee.ai_addr.withMemoryRebound(to: sockaddr_in6.self, capacity: 1) { ptr in
return .v6(address: ptr.pointee)
}
default:
throw SocketAddressError.unsupported
}
} else {
/* this is odd, getaddrinfo returned NULL */
throw SocketAddressError.unsupported
}
}
}
public enum SocketAddressError: Error {
case unknown
case unsupported
}
public enum SocketAddress {
case v4(address: sockaddr_in)
case v6(address: sockaddr_in6)
}