From f84c9487bd81c6466dbdce7b7006595a13c251b7 Mon Sep 17 00:00:00 2001 From: Stephen von Takach Date: Sun, 16 Apr 2023 15:37:59 +1000 Subject: [PATCH 1/4] add multicast support to win32 target --- spec/std/socket/udp_socket_spec.cr | 4 +--- src/lib_c/x86_64-windows-msvc/c/ws2def.cr | 18 ++++++++++++++++++ src/lib_c/x86_64-windows-msvc/c/ws2ipdef.cr | 12 ++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/spec/std/socket/udp_socket_spec.cr b/spec/std/socket/udp_socket_spec.cr index 24236c2109cf..95876487b919 100644 --- a/spec/std/socket/udp_socket_spec.cr +++ b/spec/std/socket/udp_socket_spec.cr @@ -72,7 +72,6 @@ describe UDPSocket, tags: "network" do server.close end - {% unless flag?(:win32) %} if {{ flag?(:darwin) }} && family == Socket::Family::INET6 # Darwin is failing to join IPv6 multicast groups on older versions. # However this is known to work on macOS Mojave with Darwin 18.2.0. @@ -163,10 +162,9 @@ describe UDPSocket, tags: "network" do expect_raises(IO::Error, "Closed stream") { udp.receive } end end - {% end %} end - {% if flag?(:linux) %} + {% if flag?(:linux) || flag?(:win32) %} it "sends broadcast message" do port = unused_local_port diff --git a/src/lib_c/x86_64-windows-msvc/c/ws2def.cr b/src/lib_c/x86_64-windows-msvc/c/ws2def.cr index 1e5b45071742..2ece91309621 100644 --- a/src/lib_c/x86_64-windows-msvc/c/ws2def.cr +++ b/src/lib_c/x86_64-windows-msvc/c/ws2def.cr @@ -112,6 +112,24 @@ lib LibC SIO_GET_EXTENSION_FUNCTION_POINTER = IOC_INOUT | IOC_WS2 | 6 IPPROTO_IP = 0 + IPPROTO_IPV6 = 41 + + IP_MULTICAST_TTL = 10 + IPV6_MULTICAST_HOPS = 10 + + IP_MULTICAST_LOOP = 11 + IPV6_MULTICAST_LOOP = 11 + + IP_ADD_MEMBERSHIP = 12 + IP_DROP_MEMBERSHIP = 13 + + # JOIN and LEAVE are the same as ADD and DROP + # https://learn.microsoft.com/en-us/windows/win32/winsock/ipproto-ipv6-socket-options + IPV6_ADD_MEMBERSHIP = 5 + IPV6_JOIN_GROUP = 5 + + IPV6_DROP_MEMBERSHIP = 6 + IPV6_LEAVE_GROUP = 6 enum IPPROTO IPPROTO_HOPOPTS = 0 # IPv6 Hop-by-Hop options diff --git a/src/lib_c/x86_64-windows-msvc/c/ws2ipdef.cr b/src/lib_c/x86_64-windows-msvc/c/ws2ipdef.cr index dcf609ba2c04..d9da08387e33 100644 --- a/src/lib_c/x86_64-windows-msvc/c/ws2ipdef.cr +++ b/src/lib_c/x86_64-windows-msvc/c/ws2ipdef.cr @@ -17,6 +17,18 @@ lib LibC sin_zero : StaticArray(CHAR, 8) end + # https://learn.microsoft.com/en-us/windows/win32/api/ws2ipdef/ns-ws2ipdef-ip_mreq + struct IpMreq + imr_multiaddr : InAddr + imr_interface : InAddr + end + + # https://learn.microsoft.com/en-us/windows/win32/api/ws2ipdef/ns-ws2ipdef-ipv6_mreq + struct Ipv6Mreq + ipv6mr_multiaddr : In6Addr + ipv6mr_interface : ULong + end + TCP_EXPEDITED_1122 = 0x0002 TCP_KEEPALIVE = 3 TCP_MAXSEG = 4 From 9cdc4dd1b05f32b291dbd6757f699b1b6752a025 Mon Sep 17 00:00:00 2001 From: Stephen von Takach Date: Sun, 16 Apr 2023 15:57:45 +1000 Subject: [PATCH 2/4] add missing definitions Also cross referenced with the rust documentation https://microsoft.github.io/windows-docs-rs/doc/windows/Win32/Networking/WinSock/constant.IPV6_DROP_MEMBERSHIP.html --- src/lib_c/x86_64-windows-msvc/c/ws2def.cr | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/lib_c/x86_64-windows-msvc/c/ws2def.cr b/src/lib_c/x86_64-windows-msvc/c/ws2def.cr index 2ece91309621..f539dbb2d95a 100644 --- a/src/lib_c/x86_64-windows-msvc/c/ws2def.cr +++ b/src/lib_c/x86_64-windows-msvc/c/ws2def.cr @@ -114,6 +114,9 @@ lib LibC IPPROTO_IP = 0 IPPROTO_IPV6 = 41 + IP_MULTICAST_IF = 9 + IPV6_MULTICAST_IF = 9 + IP_MULTICAST_TTL = 10 IPV6_MULTICAST_HOPS = 10 @@ -125,11 +128,11 @@ lib LibC # JOIN and LEAVE are the same as ADD and DROP # https://learn.microsoft.com/en-us/windows/win32/winsock/ipproto-ipv6-socket-options - IPV6_ADD_MEMBERSHIP = 5 - IPV6_JOIN_GROUP = 5 + IPV6_ADD_MEMBERSHIP = 12 + IPV6_JOIN_GROUP = 12 - IPV6_DROP_MEMBERSHIP = 6 - IPV6_LEAVE_GROUP = 6 + IPV6_DROP_MEMBERSHIP = 13 + IPV6_LEAVE_GROUP = 13 enum IPPROTO IPPROTO_HOPOPTS = 0 # IPv6 Hop-by-Hop options From 6e330cf06714f7af7c8f7fd01dba7c6eb6cf15b2 Mon Sep 17 00:00:00 2001 From: Stephen von Takach Date: Sun, 16 Apr 2023 16:21:44 +1000 Subject: [PATCH 3/4] simplify IO error check as windows and posix error messages are different --- spec/std/socket/udp_socket_spec.cr | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/std/socket/udp_socket_spec.cr b/spec/std/socket/udp_socket_spec.cr index 95876487b919..f767e2ee26db 100644 --- a/spec/std/socket/udp_socket_spec.cr +++ b/spec/std/socket/udp_socket_spec.cr @@ -159,7 +159,8 @@ describe UDPSocket, tags: "network" do sleep 100.milliseconds udp.close end - expect_raises(IO::Error, "Closed stream") { udp.receive } + expect_raises(IO::Error) { udp.receive } + udp.closed?.should be_true end end end From bcdbb9e27326a4aeb64f971e436b7c517f04b6af Mon Sep 17 00:00:00 2001 From: Stephen von Takach Date: Sun, 16 Apr 2023 16:48:20 +1000 Subject: [PATCH 4/4] crystal tool format --- src/lib_c/x86_64-windows-msvc/c/ws2def.cr | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/lib_c/x86_64-windows-msvc/c/ws2def.cr b/src/lib_c/x86_64-windows-msvc/c/ws2def.cr index f539dbb2d95a..9fc19857f4a3 100644 --- a/src/lib_c/x86_64-windows-msvc/c/ws2def.cr +++ b/src/lib_c/x86_64-windows-msvc/c/ws2def.cr @@ -111,28 +111,28 @@ lib LibC SIO_GET_EXTENSION_FUNCTION_POINTER = IOC_INOUT | IOC_WS2 | 6 - IPPROTO_IP = 0 + IPPROTO_IP = 0 IPPROTO_IPV6 = 41 - IP_MULTICAST_IF = 9 + IP_MULTICAST_IF = 9 IPV6_MULTICAST_IF = 9 - IP_MULTICAST_TTL = 10 + IP_MULTICAST_TTL = 10 IPV6_MULTICAST_HOPS = 10 - IP_MULTICAST_LOOP = 11 + IP_MULTICAST_LOOP = 11 IPV6_MULTICAST_LOOP = 11 - IP_ADD_MEMBERSHIP = 12 + IP_ADD_MEMBERSHIP = 12 IP_DROP_MEMBERSHIP = 13 # JOIN and LEAVE are the same as ADD and DROP # https://learn.microsoft.com/en-us/windows/win32/winsock/ipproto-ipv6-socket-options IPV6_ADD_MEMBERSHIP = 12 - IPV6_JOIN_GROUP = 12 + IPV6_JOIN_GROUP = 12 IPV6_DROP_MEMBERSHIP = 13 - IPV6_LEAVE_GROUP = 13 + IPV6_LEAVE_GROUP = 13 enum IPPROTO IPPROTO_HOPOPTS = 0 # IPv6 Hop-by-Hop options