Skip to content

Commit

Permalink
Added Socket.address()
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Oct 3, 2022
1 parent be06591 commit 9306aa9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Sources/Socket/System/SocketOperations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -616,4 +616,27 @@ extension SocketDescriptor {
system_write(self.rawValue, buffer.baseAddress, buffer.count)
}
}

@_alwaysEmitIntoClient
public func address<Address: SocketAddress>(
_ address: Address.Type,
retryOnInterrupt: Bool = true
) throws -> Address {
return try _getAddress(address, retryOnInterrupt: retryOnInterrupt).get()
}

@usableFromInline
internal func _getAddress<Address: SocketAddress>(
_ address: Address.Type,
retryOnInterrupt: Bool
) -> Result<Address, Errno> {
var result: Result<CInt, Errno> = .success(0)
let address = Address.withUnsafePointer { socketPointer, socketLength in
var length = socketLength
result = valueOrErrno(retryOnInterrupt: retryOnInterrupt) {
system_getsockname(self.rawValue, socketPointer, &length)
}
}
return result.map { _ in address }
}
}
7 changes: 7 additions & 0 deletions Sources/Socket/System/Syscalls.swift
Original file line number Diff line number Diff line change
Expand Up @@ -467,3 +467,10 @@ internal func system_link_ntoa(_ address: UnsafePointer<sockaddr_dl>) -> UnsafeM
return link_ntoa(address)
}
#endif

internal func system_getsockname(_ fd: CInt, _ address: UnsafeMutablePointer<CInterop.SocketAddress>, _ length: UnsafeMutablePointer<UInt32>) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled { return _mock(fd, address) }
#endif
return getsockname(fd, address, length)
}
4 changes: 4 additions & 0 deletions Tests/SocketTests/SocketTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ final class SocketTests: XCTestCase {
IPv4Protocol.tcp,
bind: address
)
XCTAssertEqual(try server.fileDescriptor.address(IPv4SocketAddress.self), address)
defer { Task { await server.close() } }
NSLog("Server: Created server socket \(server.fileDescriptor)")
try server.fileDescriptor.listen(backlog: 10)
Expand All @@ -58,6 +59,7 @@ final class SocketTests: XCTestCase {
fileDescriptor: try await server.fileDescriptor.accept()
)
NSLog("Server: Got incoming connection \(newConnection.fileDescriptor)")
XCTAssertEqual(try newConnection.fileDescriptor.address(IPv4SocketAddress.self).address.rawValue, "127.0.0.1")
let _ = try await newConnection.write(data)
NSLog("Server: Wrote outgoing data")
} catch {
Expand All @@ -69,13 +71,15 @@ final class SocketTests: XCTestCase {
let client = try await Socket(
IPv4Protocol.tcp
)
XCTAssertEqual(try client.fileDescriptor.address(IPv4SocketAddress.self).address, .any)
defer { Task { await client.close() } }
NSLog("Client: Created client socket \(client.fileDescriptor)")

NSLog("Client: Will connect to server")
do { try await client.fileDescriptor.connect(to: address, sleep: 100_000_000) }
catch Errno.socketIsConnected { }
NSLog("Client: Connected to server")
XCTAssertEqual(try client.fileDescriptor.address(IPv4SocketAddress.self).address.rawValue, "127.0.0.1")
let read = try await client.read(data.count)
NSLog("Client: Read incoming data")
XCTAssertEqual(data, read)
Expand Down

0 comments on commit 9306aa9

Please sign in to comment.