Skip to content

Commit

Permalink
Add Socket::Address.from without addrlen
Browse files Browse the repository at this point in the history
  • Loading branch information
mamantoha committed Oct 4, 2024
1 parent 46ca8fb commit d3ea37a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
3 changes: 3 additions & 0 deletions spec/std/socket/address_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ describe Socket::IPAddress do
addr2.port.should eq(addr1.port)
typeof(addr2.address).should eq(String)
addr2.address.should eq(addr1.address)
addr2.should eq(Socket::IPAddress.from(addr1_c))
end

it "transforms an IPv6 address into a C struct and back" do
Expand All @@ -64,6 +65,7 @@ describe Socket::IPAddress do
addr2.port.should eq(addr1.port)
typeof(addr2.address).should eq(String)
addr2.address.should eq(addr1.address)
addr2.should eq(Socket::IPAddress.from(addr1_c))
end

it "won't resolve domains" do
Expand Down Expand Up @@ -431,6 +433,7 @@ end
addr2.family.should eq(addr1.family)
addr2.path.should eq(addr1.path)
addr2.to_s.should eq(path)
addr2 = Socket::UNIXAddress.from(addr1.to_unsafe)
end

it "raises when path is too long" do
Expand Down
47 changes: 47 additions & 0 deletions src/socket/address.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ class Socket
end
end

# :ditto:
def self.from(sockaddr : LibC::Sockaddr*) : Address
case family = Family.new(sockaddr.value.sa_family)
when Family::INET6
sockaddr = sockaddr.as(LibC::SockaddrIn6*)

IPAddress.new(sockaddr, sizeof(typeof(sockaddr)))
when Family::INET
sockaddr = sockaddr.as(LibC::SockaddrIn*)

IPAddress.new(sockaddr, sizeof(typeof(sockaddr)))
when Family::UNIX
sockaddr = sockaddr.as(LibC::SockaddrUn*)

UNIXAddress.new(sockaddr, sizeof(typeof(sockaddr)))
else
raise "Unsupported family type: #{family} (#{family.value})"
end
end

# Parses a `Socket::Address` from an URI.
#
# Supported formats:
Expand Down Expand Up @@ -113,6 +133,22 @@ class Socket
end
end

# :ditto:
def self.from(sockaddr : LibC::Sockaddr*) : IPAddress
case family = Family.new(sockaddr.value.sa_family)
when Family::INET6
sockaddr = sockaddr.as(LibC::SockaddrIn6*)

new(sockaddr, sizeof(typeof(sockaddr)))
when Family::INET
sockaddr = sockaddr.as(LibC::SockaddrIn*)

new(sockaddr, sizeof(typeof(sockaddr)))
else
raise "Unsupported family type: #{family} (#{family.value})"
end
end

# Parses a `Socket::IPAddress` from an URI.
#
# It expects the URI to include `<scheme>://<host>:<port>` where `scheme` as
Expand Down Expand Up @@ -750,6 +786,17 @@ class Socket
{% end %}
end

# :ditto:
def self.from(sockaddr : LibC::Sockaddr*) : UNIXAddress
{% if flag?(:wasm32) %}
raise NotImplementedError.new "Socket::UNIXAddress.from"
{% else %}
sockaddr = sockaddr.as(LibC::SockaddrUn*)

new(sockaddr, sizeof(typeof(sockaddr)))
{% end %}
end

# Parses a `Socket::UNIXAddress` from an URI.
#
# It expects the URI to include `<scheme>://<path>` where `scheme` as well
Expand Down

0 comments on commit d3ea37a

Please sign in to comment.