From 16e0ffd9bfcee7741595d8f048569310de8b2aed Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Tue, 8 Aug 2017 15:43:47 +0200 Subject: [PATCH] Allow to obtain remote and local address --- Sources/NIO/BaseSocket.swift | 30 ++++++++++++++++++++++++++++-- Sources/NIOEchoServer/main.swift | 2 +- Sources/NIOHTTP1Server/main.swift | 2 +- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/Sources/NIO/BaseSocket.swift b/Sources/NIO/BaseSocket.swift index 0e0c8800e..1c92fb41e 100644 --- a/Sources/NIO/BaseSocket.swift +++ b/Sources/NIO/BaseSocket.swift @@ -59,16 +59,42 @@ class BaseSocket : Selectable { final var localAddress: SocketAddress? { get { - return nil + return get_addr { getsockname($0, $1, $2) } } } final var remoteAddress: SocketAddress? { get { - return nil + return get_addr { getpeername($0, $1, $2) } } } + private func get_addr(_ fn: (Int32, UnsafeMutablePointer, UnsafeMutablePointer) -> Int32) -> SocketAddress? { + var addr = sockaddr_storage() + var len: socklen_t = socklen_t(MemoryLayout.size) + + return withUnsafeMutablePointer(to: &addr) { + $0.withMemoryRebound(to: sockaddr.self, capacity: 1, { address in + guard fn(descriptor, address, &len) == 0 else { + return nil + } + switch Int32(address.pointee.sa_family) { + case AF_INET: + return address.withMemoryRebound(to: sockaddr_in.self, capacity: 1, { ipv4 in + var ipAddressString = [CChar](repeating: 0, count: Int(INET_ADDRSTRLEN)) + return SocketAddress(IPv4Address: ipv4.pointee, host: String(cString: inet_ntop(AF_INET, &ipv4.pointee.sin_addr, &ipAddressString, socklen_t(INET_ADDRSTRLEN)))) + }) + case AF_INET6: + return address.withMemoryRebound(to: sockaddr_in6.self, capacity: 1, { ipv6 in + var ipAddressString = [CChar](repeating: 0, count: Int(INET6_ADDRSTRLEN)) + return SocketAddress(IPv6Address: ipv6.pointee, host: String(cString: inet_ntop(AF_INET6, &ipv6.pointee.sin6_addr, &ipAddressString, socklen_t(INET6_ADDRSTRLEN)))) + }) + default: + fatalError("address family \(address.pointee.sa_family) not supported") + } + }) + } + } static func newSocket(protocolFamily: Int32) throws -> Int32 { let sock = try wrapSyscall({ $0 >= 0 }, function: "socket") { () -> Int32 in sysSocket(protocolFamily, Int32(sysSOCK_STREAM), 0) diff --git a/Sources/NIOEchoServer/main.swift b/Sources/NIOEchoServer/main.swift index 4e4732510..c7d4021da 100644 --- a/Sources/NIOEchoServer/main.swift +++ b/Sources/NIOEchoServer/main.swift @@ -85,7 +85,7 @@ default: let channel = try bootstrap.bind(to: host, on: port).wait() -print("Server started and listening on \(host):\(port)") +print("Server started and listening on \(channel.localAddress)") // This will never unblock as we not close the ServerChannel try channel.closeFuture.wait() diff --git a/Sources/NIOHTTP1Server/main.swift b/Sources/NIOHTTP1Server/main.swift index c69ff0946..843e7c934 100644 --- a/Sources/NIOHTTP1Server/main.swift +++ b/Sources/NIOHTTP1Server/main.swift @@ -106,7 +106,7 @@ default: let channel = try bootstrap.bind(to: host, on: port).wait() -print("Server started and listening on \(host):\(port)") +print("Server started and listening on \(channel.localAddress)") // This will never unblock as we not close the ServerChannel try channel.closeFuture.wait()