Skip to content

Commit 92a1b26

Browse files
authored
Add IPSocket#ipv6_only (#16347)
1 parent 157f8ca commit 92a1b26

File tree

20 files changed

+108
-0
lines changed

20 files changed

+108
-0
lines changed

spec/std/socket/tcp_server_spec.cr

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,5 +147,37 @@ describe TCPServer, tags: "network" do
147147
end
148148
end
149149
end
150+
151+
it "supports IPv6 dual stack" do
152+
server = TCPServer.new(:inet6)
153+
server.ipv6_only = false
154+
server.bind("::", 0)
155+
server.listen
156+
157+
TCPSocket.open("127.0.0.1", server.local_address.port) do |client|
158+
server.accept? do |sock|
159+
sock.ipv6_only?.should be_false
160+
161+
# should raise when changing ipv6_only when not applicable
162+
expect_raises(Socket::Error, /invalid argument/i) do
163+
sock.ipv6_only = true
164+
end
165+
end
166+
end
167+
168+
server.close
169+
end
170+
171+
it "supports IPv6 only" do
172+
server = TCPServer.new(:inet6)
173+
server.ipv6_only = true
174+
server.bind("::", 0)
175+
server.listen
176+
177+
expect_raises(Socket::ConnectError) do
178+
TCPSocket.new("127.0.0.1", server.local_address.port, connect_timeout: 1.second)
179+
end
180+
server.close
181+
end
150182
end
151183
end

spec/std/socket/udp_socket_spec.cr

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,24 @@ describe UDPSocket, tags: "network" do
1818
socket.close
1919
end
2020

21+
it "supports IPv6 dual stack" do
22+
socket = UDPSocket.new(:inet6)
23+
24+
socket.ipv6_only = false
25+
socket.ipv6_only?.should be_false
26+
27+
socket.ipv6_only = true
28+
socket.ipv6_only?.should be_true
29+
30+
socket = UDPSocket.new
31+
expect_raises(Socket::Error, "Unsupported IP address family: INET. For use with IPv6 only") do
32+
socket.ipv6_only?
33+
end
34+
expect_raises(Socket::Error, "Unsupported IP address family: INET. For use with IPv6 only") do
35+
socket.ipv6_only = false
36+
end
37+
end
38+
2139
each_ip_family do |family, address, unspecified_address|
2240
it "#bind" do
2341
port = unused_local_udp_port

src/lib_c/aarch64-android/c/netinet/in.cr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ lib LibC
5757
IP_DROP_MEMBERSHIP = 36
5858
IPV6_LEAVE_GROUP = 21
5959

60+
IPV6_V6ONLY = 26
61+
6062
struct IpMreq
6163
imr_multiaddr : InAddr
6264
imr_interface : InAddr

src/lib_c/aarch64-darwin/c/netinet/in.cr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ lib LibC
5858
IP_DROP_MEMBERSHIP = 13
5959
IPV6_LEAVE_GROUP = 13
6060

61+
IPV6_V6ONLY = 27
62+
6163
struct IpMreq
6264
imr_multiaddr : InAddr
6365
imr_interface : InAddr

src/lib_c/aarch64-linux-gnu/c/netinet/in.cr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ lib LibC
5656
IP_DROP_MEMBERSHIP = 36
5757
IPV6_LEAVE_GROUP = 21
5858

59+
IPV6_V6ONLY = 26
60+
5961
struct IpMreq
6062
imr_multiaddr : InAddr
6163
imr_interface : InAddr

src/lib_c/aarch64-linux-musl/c/netinet/in.cr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ lib LibC
5656
IP_DROP_MEMBERSHIP = 36
5757
IPV6_LEAVE_GROUP = 21
5858

59+
IPV6_V6ONLY = 26
60+
5961
struct IpMreq
6062
imr_multiaddr : InAddr
6163
imr_interface : InAddr

src/lib_c/arm-linux-gnueabihf/c/netinet/in.cr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ lib LibC
5656
IP_DROP_MEMBERSHIP = 36
5757
IPV6_LEAVE_GROUP = 21
5858

59+
IPV6_V6ONLY = 26
60+
5961
struct IpMreq
6062
imr_multiaddr : InAddr
6163
imr_interface : InAddr

src/lib_c/i386-linux-gnu/c/netinet/in.cr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ lib LibC
5656
IP_DROP_MEMBERSHIP = 36
5757
IPV6_LEAVE_GROUP = 21
5858

59+
IPV6_V6ONLY = 26
60+
5961
struct IpMreq
6062
imr_multiaddr : InAddr
6163
imr_interface : InAddr

src/lib_c/i386-linux-musl/c/netinet/in.cr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ lib LibC
5656
IP_DROP_MEMBERSHIP = 36
5757
IPV6_LEAVE_GROUP = 21
5858

59+
IPV6_V6ONLY = 26
60+
5961
struct IpMreq
6062
imr_multiaddr : InAddr
6163
imr_interface : InAddr

src/lib_c/wasm32-wasi/c/netinet/in.cr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ lib LibC
4949
IP_DROP_MEMBERSHIP = 36
5050
IPV6_LEAVE_GROUP = 21
5151

52+
IPV6_V6ONLY = 26
53+
5254
struct IpMreq
5355
imr_multiaddr : InAddr
5456
imr_interface : InAddr

0 commit comments

Comments
 (0)