-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
193 additions
and
74 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,52 @@ | ||
require "spec" | ||
require "../src/port_checker" | ||
|
||
describe PortChecker do | ||
it "normalizes IPv6 addresses" do | ||
pc = PortChecker.new "[::1]" | ||
pc.ipaddress.address.should eq "::1" | ||
end | ||
|
||
{"127.0.0.1", "::1"}.each do |host| | ||
{ | ||
PortChecker.new(host, tcp: true), | ||
PortChecker.new(host, udp: true), | ||
PortChecker.new(host, tcp: true, udp: true), | ||
}.each do |pc| | ||
msg = host | ||
msg += " TCP" if pc.tcp | ||
msg += " UDP" if pc.udp | ||
if !pc.first_available_port | ||
puts "#{msg} not supported" | ||
next | ||
end | ||
|
||
describe msg do | ||
describe "available port" do | ||
it "returns true when available" do | ||
port = pc.first_available_port.as Int32 | ||
pc.available_port?(port).should be_true | ||
end | ||
|
||
it "returns false when not available" do | ||
tcp_socket = TCPSocket.new pc.ipaddress.family if pc.tcp | ||
udp_socket = UDPSocket.new pc.ipaddress.family if pc.udp | ||
port = pc.first_available_port.as Int32 | ||
begin | ||
tcp_socket.try &.bind pc.ipaddress.address, port | ||
udp_socket.try &.bind pc.ipaddress.address, port | ||
pc.available_port?(port).should be_false | ||
ensure | ||
tcp_socket.try &.close | ||
udp_socket.try &.close | ||
end | ||
end | ||
end | ||
|
||
it "returns the first available port" do | ||
pc.first_available_port.should be_a Int32 | ||
end | ||
end | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require "socket" | ||
|
||
# Creates local Sockets to know local available ports. | ||
struct PortChecker | ||
property tcp : Bool | ||
property udp : Bool | ||
getter ipaddress : Socket::IPAddress | ||
|
||
def address=(host : String) | ||
@ipaddress = address_normalizer host | ||
end | ||
|
||
def initialize(host : String, @tcp : Bool = false, @udp : Bool = false) | ||
@ipaddress = address_normalizer host | ||
end | ||
|
||
private def address_normalizer(host : String) : Socket::IPAddress | ||
Socket::IPAddress.new host.lchop('[').rchop(']'), 1 | ||
end | ||
|
||
# Returns an available port. | ||
def available_port?(port : Int32) : Bool | ||
if @tcp | ||
socket = TCPSocket.new @ipaddress.family | ||
return false if !internal_available_port? socket, port | ||
end | ||
if @udp | ||
socket = UDPSocket.new @ipaddress.family | ||
return false if !internal_available_port? socket, port | ||
end | ||
|
||
true | ||
end | ||
|
||
private def internal_available_port?(socket : IPSocket, port : Int32) : Bool | ||
socket.bind @ipaddress.address, port | ||
available = true | ||
rescue ex : Errno | ||
available = false | ||
ensure | ||
socket.close | ||
available | ||
end | ||
|
||
# Returns the first available port. | ||
def first_available_port(start_port : Int32 = @ipaddress.port) : Int32? | ||
(start_port..UInt16::MAX).each do |port| | ||
return port if available_port? port | ||
end | ||
end | ||
end |
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
Oops, something went wrong.