-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from depoon/update-modifier
Introduce initializeUsingRandomPortNumber
- Loading branch information
Showing
5 changed files
with
80 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// | ||
// LocalhostPort.swift | ||
// SwiftLocalhost | ||
// | ||
// Created by Kenneth Poon on 27/12/18. | ||
// Copyright © 2018 Kenneth Poon. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public class LocalhostPort { | ||
|
||
public static func availablePortNumber() -> UInt16 { | ||
for i in 50000..<65000 { | ||
let (isFree, _) = checkTcpPortForListen(port: UInt16(i)) | ||
if isFree == true { | ||
return UInt16(i) | ||
} | ||
} | ||
return 0 | ||
} | ||
|
||
static func checkTcpPortForListen(port: in_port_t) -> (Bool, descr: String) { | ||
|
||
let socketFileDescriptor = socket(AF_INET, SOCK_STREAM, 0) | ||
if socketFileDescriptor == -1 { | ||
return (false, "SocketCreationFailed, \(descriptionOfLastError())") | ||
} | ||
|
||
var addr = sockaddr_in() | ||
let sizeOfSockkAddr = MemoryLayout<sockaddr_in>.size | ||
addr.sin_len = __uint8_t(sizeOfSockkAddr) | ||
addr.sin_family = sa_family_t(AF_INET) | ||
addr.sin_port = Int(OSHostByteOrder()) == OSLittleEndian ? _OSSwapInt16(port) : port | ||
addr.sin_addr = in_addr(s_addr: inet_addr("0.0.0.0")) | ||
addr.sin_zero = (0, 0, 0, 0, 0, 0, 0, 0) | ||
var bind_addr = sockaddr() | ||
memcpy(&bind_addr, &addr, Int(sizeOfSockkAddr)) | ||
|
||
if Darwin.bind(socketFileDescriptor, &bind_addr, socklen_t(sizeOfSockkAddr)) == -1 { | ||
let details = descriptionOfLastError() | ||
release(socket: socketFileDescriptor) | ||
return (false, "\(port), BindFailed, \(details)") | ||
} | ||
if listen(socketFileDescriptor, SOMAXCONN ) == -1 { | ||
let details = descriptionOfLastError() | ||
release(socket: socketFileDescriptor) | ||
return (false, "\(port), ListenFailed, \(details)") | ||
} | ||
release(socket: socketFileDescriptor) | ||
return (true, "\(port) is free for use") | ||
} | ||
|
||
static func release(socket: Int32) { | ||
Darwin.shutdown(socket, SHUT_RDWR) | ||
close(socket) | ||
} | ||
|
||
static func descriptionOfLastError() -> String { | ||
return String.init(cString: (UnsafePointer(strerror(errno)))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters