Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multicast support to UDPSocket on Windows #13325

Merged
merged 4 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions spec/std/socket/udp_socket_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -160,13 +159,13 @@ 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 %}
end

{% if flag?(:linux) %}
{% if flag?(:linux) || flag?(:win32) %}
it "sends broadcast message" do
port = unused_local_port

Expand Down
23 changes: 22 additions & 1 deletion src/lib_c/x86_64-windows-msvc/c/ws2def.cr
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +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
IPV6_MULTICAST_IF = 9

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 = 12
IPV6_JOIN_GROUP = 12

IPV6_DROP_MEMBERSHIP = 13
IPV6_LEAVE_GROUP = 13

enum IPPROTO
IPPROTO_HOPOPTS = 0 # IPv6 Hop-by-Hop options
Expand Down
12 changes: 12 additions & 0 deletions src/lib_c/x86_64-windows-msvc/c/ws2ipdef.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down