public static unsafe SocketError GetIPv6MulticastOption(SafeCloseSocket handle, SocketOptionName optionName, out IPv6MulticastOption optionValue) { int optLevel, optName; GetPlatformOptionInfo(SocketOptionLevel.IPv6, optionName, out optLevel, out optName); var mreq = new Interop.libc.ipv6_mreq(); var optLen = (uint)sizeof(Interop.libc.ipv6_mreq); int err = Interop.libc.getsockopt(handle.FileDescriptor, optLevel, optName, &mreq, &optLen); if (err == -1) { optionValue = default(IPv6MulticastOption); return GetLastSocketError(); } var multicastAddress = new byte[sizeof(Interop.libc.in6_addr)]; for (int i = 0; i < multicastAddress.Length; i++) { multicastAddress[i] = mreq.ipv6mr_multiaddr.s6_addr[i]; } optionValue = new IPv6MulticastOption(new IPAddress(multicastAddress), mreq.ipv6mr_ifindex); return SocketError.Success; }
public static unsafe SocketError SetIPv6MulticastOption(SafeCloseSocket handle, SocketOptionName optionName, IPv6MulticastOption optionValue) { int optLevel, optName; GetPlatformOptionInfo(SocketOptionLevel.IPv6, optionName, out optLevel, out optName); var mreq = new Interop.libc.ipv6_mreq { // TODO: what is the endianness of ipv6mr_ifindex? ipv6mr_ifindex = checked((int)optionValue.InterfaceIndex) }; byte[] multicastAddress = optionValue.Group.GetAddressBytes(); Debug.Assert(multicastAddress.Length == sizeof(Interop.libc.in6_addr)); for (int i = 0; i < multicastAddress.Length; i++) { mreq.ipv6mr_multiaddr.s6_addr[i] = multicastAddress[i]; } int err = Interop.libc.setsockopt(handle.FileDescriptor, optLevel, optName, &mreq, (uint)sizeof(Interop.libc.ipv6_mreq)); return err == -1 ? GetLastSocketError() : SocketError.Success; }