Example #1
0
		[Test] // .ctor (IPAddress)
		public void Constructor1 ()
		{
			IPv6MulticastOption option;
			IPAddress group;

			group = IPAddress.Parse ("ff02::1");
			option = new IPv6MulticastOption (group);
			Assert.AreSame (group, option.Group, "#A:Group");
			Assert.AreEqual (0, option.InterfaceIndex, "#A:InterfaceIndex");

			group = IPAddress.Parse ("224.0.0.23");
			option = new IPv6MulticastOption (group);
			Assert.AreSame (group, option.Group, "#B:Group");
			Assert.AreEqual (0, option.InterfaceIndex, "#B:InterfaceIndex");
		}
Example #2
0
		[Test] // .ctor (IPAddress, Int64)
		public void Constructor2 ()
		{
			IPv6MulticastOption option;
			IPAddress group;
			long interfaceIndex;

			group = IPAddress.Parse ("239.255.255.250");
			interfaceIndex = 0;
			option = new IPv6MulticastOption (group, interfaceIndex);
			Assert.AreSame (group, option.Group, "#A:Group");
			Assert.AreEqual (interfaceIndex, option.InterfaceIndex, "#A:InterfaceIndex");

			group = IPAddress.Parse ("ff02::1");
			interfaceIndex = 0;
			option = new IPv6MulticastOption (group, interfaceIndex);
			Assert.AreSame (group, option.Group, "#B:Group");
			Assert.AreEqual (interfaceIndex, option.InterfaceIndex, "#B:InterfaceIndex");

			group = IPAddress.Parse ("239.255.255.250");
			interfaceIndex = 124;
			option = new IPv6MulticastOption (group, interfaceIndex);
			Assert.AreSame (group, option.Group, "#C:Group");
			Assert.AreEqual (interfaceIndex, option.InterfaceIndex, "#C:InterfaceIndex");

			group = IPAddress.Parse ("ff02::1");
			interfaceIndex = 124;
			option = new IPv6MulticastOption (group, interfaceIndex);
			Assert.AreSame (group, option.Group, "#D:Group");
			Assert.AreEqual (interfaceIndex, option.InterfaceIndex, "#D:InterfaceIndex");

			group = IPAddress.Parse ("239.255.255.250");
			interfaceIndex = 0xFFFFFFFF;
			option = new IPv6MulticastOption (group, interfaceIndex);
			Assert.AreSame (group, option.Group, "#E:Group");
			Assert.AreEqual (interfaceIndex, option.InterfaceIndex, "#E:InterfaceIndex");

			group = IPAddress.Parse ("ff02::1");
			interfaceIndex = 0xFFFFFFFF;
			option = new IPv6MulticastOption (group, interfaceIndex);
			Assert.AreSame (group, option.Group, "#F:Group");
			Assert.AreEqual (interfaceIndex, option.InterfaceIndex, "#F:InterfaceIndex");
		}
Example #3
0
		[Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
		public void SetSocketOption3_DropMembershipIPv6_IPv6MulticastOption ()
		{
			if (!Socket.OSSupportsIPv6)
				Assert.Ignore ("IPv6 not enabled.");

			using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
				IPv6MulticastOption option = new IPv6MulticastOption (
					IPAddress.Parse ("ff02::1"));

				s.Bind (new IPEndPoint (IPAddress.IPv6Any, 1902));
				s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
					option);
				s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
					option);
			}
		}
Example #4
0
        //internal class SockInterfacePair
        //{
        //    internal UdpSocket sock;
        //    internal IPAddress extInterface;
        //    public bool Initialized;

        //    internal SockInterfacePair(UdpSocket sock, IPAddress extIntf)
        //    {
        //        this.sock = sock;
        //        this.extInterface = extIntf;
        //        Initialized = false;
        //    }
        //}


        // Apparently binding to the same ports on UDPSender and UDPListener causes problems in unicast.
        // Sharing the socket though, allows us to tunnel through firewalls as data is sent and received
        // on the same  endpoint.
        // This region of code enables sharing sockets between the two classes.

        //internal static SockInterfacePair GetSharedSocket(IPEndPoint endPoint)
        //{
        //    lock (socks)
        //    {
        //        object sockObj = socks[endPoint];
        //        if (sockObj != null)
        //        {
        //            SockInterfacePair sip = (SockInterfacePair)sockObj;
        //            ++sip.sock.refCount;

        //            return sip;
        //        }
        //        else
        //        {
        //            // Create the socket
        //            UdpSocket sock = new UdpSocket(endPoint.AddressFamily);

        //            // Get the External Interface, save it for future use
        //            IPAddress externalInterface = Utility.GetLocalRoutingInterface(endPoint.Address);

        //            if (externalInterface == null)
        //            {
        //                // Pri3: Do something more helpful here
        //                throw new Exception(Strings.UnableToFindLocalRoutingInterface);
        //            }

        //            if (Utility.IsMulticast(endPoint.Address))
        //            {
        //                // Allow multiple binds to this socket, as it will still function properly
        //                //  (this is only the case if it is a multicast socket.  Unicast sockets fail to
        //                //   receive all data on all sockets)
        //                sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, -1);

        //                // We don't join the multicast group here, because we may not want to listen
        //                // to our own data (halfing our throughput).  jasonv - 10/28/2004
        //            }

        //            // Add the socket to the hashtable
        //            SockInterfacePair sip = new SockInterfacePair(sock, externalInterface);
        //            socks.Add(endPoint, sip);

        //            // Increase the socket's reference count
        //            ++sock.refCount;

        //            return sip;
        //        }
        //    }
        //}

        //internal static void ReleaseSharedSocket(IPEndPoint endPoint, UdpSocket sock)
        //{
        //    object sockObj = socks[endPoint];
        //    if (sockObj == null)
        //        throw new InvalidOperationException(Strings.SockDoesNotExistAsASharedSocket);

        //    lock (socks)
        //    {
        //        if (--sock.refCount <= 0)
        //        {
        //            // Leave the multicast group
        //            if (Utility.IsMulticast(endPoint.Address))
        //            {
        //                try
        //                {
        //                    if (endPoint.AddressFamily == AddressFamily.InterNetworkV6)
        //                    {
        //                        IPv6MulticastOption mo = new IPv6MulticastOption(endPoint.Address);
        //                        sock.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.DropMembership, mo);
        //                    }
        //                    else
        //                    {
        //                        MulticastOption mo = new MulticastOption(endPoint.Address);
        //                        sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.DropMembership, mo);
        //                    }
        //                }
        //                catch { } // The user of the socket *may* not have joined the multicast group (?)
        //            }

        //            // Remove ourselves from the shared pool
        //            socks.Remove(endPoint);

        //            // Close the socket
        //            try
        //            {
        //                sock.Close();
        //            }
        //            catch (ObjectDisposedException) { }
        //        }
        //    }
        //}

        internal static void ReleaseSocket(IPEndPoint endPoint, UdpSocket sock)
        {
            // Leave the multicast group
            if (Utility.IsMulticast(endPoint.Address))
            {
                try
                {
                    if (endPoint.AddressFamily == AddressFamily.InterNetworkV6)
                    {
                        IPv6MulticastOption mo = new IPv6MulticastOption(endPoint.Address);
                        sock.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.DropMembership, mo);
                    }
                    else
                    {
                        MulticastOption mo = new MulticastOption(endPoint.Address);
                        sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.DropMembership, mo);
                    }
                }
                catch { } // The user of the socket *may* not have joined the multicast group (?)
            }

            // Close the socket
            try
            {
                sock.Close();
            }
            catch (ObjectDisposedException) { }
        }
Example #5
0
        public static unsafe SocketError SetIPv6MulticastOption(SafeCloseSocket handle, SocketOptionName optionName, IPv6MulticastOption optionValue)
        {
            Debug.Assert(optionName == SocketOptionName.AddMembership || optionName == SocketOptionName.DropMembership);

            Interop.Sys.MulticastOption optName = optionName == SocketOptionName.AddMembership ?
                Interop.Sys.MulticastOption.MULTICAST_ADD :
                Interop.Sys.MulticastOption.MULTICAST_DROP;

            var opt = new Interop.Sys.IPv6MulticastOption {
                Address = optionValue.Group.GetNativeIPAddress(),
                InterfaceIndex = (int)optionValue.InterfaceIndex
            };

            Interop.Error err = Interop.Sys.SetIPv6MulticastOption(handle.FileDescriptor, optName, &opt);
            return err == Interop.Error.SUCCESS ? SocketError.Success : GetSocketErrorForErrorCode(err);
        }
Example #6
0
        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;
        }
Example #7
0
        /// <devdoc>
        ///     <para>
        ///         IPv6 setsockopt for JOIN / LEAVE multicast group
        ///     </para>
        /// </devdoc>
        private void setIPv6MulticastOption(SocketOptionName optionName, IPv6MulticastOption MR) {
            IPv6MulticastRequest ipmr = new IPv6MulticastRequest();

            ipmr.MulticastAddress = MR.Group.GetAddressBytes();
            ipmr.InterfaceIndex   = unchecked((int)MR.InterfaceIndex);

            GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::setIPv6MulticastOption(): optionName:" + optionName.ToString() + " MR:" + MR.ToString() + " ipmr:" + ipmr.ToString() + " IPv6MulticastRequest.Size:" + IPv6MulticastRequest.Size.ToString());

            // This can throw ObjectDisposedException.
            SocketError errorCode = UnsafeNclNativeMethods.OSSOCK.setsockopt(
                m_Handle,
                SocketOptionLevel.IPv6,
                optionName,
                ref ipmr,
                IPv6MulticastRequest.Size);

            GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::setIPv6MulticastOption() UnsafeNclNativeMethods.OSSOCK.setsockopt returns errorCode:" + errorCode);

            //
            // if the native call fails we'll throw a SocketException
            //
            if (errorCode==SocketError.SocketError) {
                //
                // update our internal state after this socket error and throw
                //
                SocketException socketException = new SocketException();
                UpdateStatusAfterSocketError(socketException);
                if(s_LoggingEnabled)Logging.Exception(Logging.Sockets, this, "setIPv6MulticastOption", socketException);
                throw socketException;
            }
        }
Example #8
0
        private static RegisterMessage ProcessJoinRequest(RegisterMessage joinRequestMessage, IPAddress senderIP)
        {
            try
            {
                // If the senderIP already exists, remove it by following the rule - the last one wins
                if (clientRegTable.ContainsKey(senderIP))
                {
                    lock (clientRegTable.SyncRoot)
                    {
                        clientRegTable.Remove(senderIP);
                    }
                }

                // Insert the new entry
                Random rnd = new Random();
                int confirmNumber = rnd.Next(1, Int32.MaxValue);

                lock (clientRegTable.SyncRoot)
                {
                    clientRegTable.Add(senderIP, new ClientEntry(senderIP, new IPEndPoint(joinRequestMessage.groupIP,
                        joinRequestMessage.groupPort), DateTime.Now, confirmNumber, DateTime.Now));

                    ReflectorMgr.PC[ReflectorPC.ID.TotalParticipants]++;
                    ReflectorMgr.PC[ReflectorPC.ID.CurrentParticipats] = clientRegTable.Count;
                }

                joinRequestMessage.msgType = MessageType.Confirm;
                joinRequestMessage.confirmNumber = confirmNumber;
                joinRequestMessage.unicastPort = ReflectorMgr.ReflectorUnicastRTPListenPort;

                if (joinRequestMessage.groupIP.AddressFamily == AddressFamily.InterNetwork)
                {
                    MulticastOption mo = new MulticastOption(joinRequestMessage.groupIP,ReflectorMgr.MulticastInterfaceIP);
                    ReflectorMgr.Sockets.SockMCv4RTP.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, mo);
                    ReflectorMgr.Sockets.SockMCv4RTCP.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, mo);
                }
                else
                {
                    IPv6MulticastOption mo = new IPv6MulticastOption(joinRequestMessage.groupIP);
                    ReflectorMgr.Sockets.SockMCv6RTP.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.AddMembership, mo);
                    ReflectorMgr.Sockets.SockMCv6RTCP.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.AddMembership, mo);
                }
            }

            catch
            {
                joinRequestMessage.msgType = MessageType.UnknownError;
            }

            return joinRequestMessage;
        }
Example #9
0
        // Leaves a multicast address group.
        public void DropMulticastGroup(IPAddress multicastAddr)
        {
            // Validate input parameters.
            if (_cleanedUp)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }
            if (multicastAddr == null)
            {
                throw new ArgumentNullException("multicastAddr");
            }

            // IPv6 Changes: we need to create the correct MulticastOption and
            //               must also check for address family compatibility.
            if (multicastAddr.AddressFamily != _family)
            {
                throw new ArgumentException(SR.Format(SR.net_protocol_invalid_multicast_family, "UDP"), "multicastAddr");
            }

            if (_family == AddressFamily.InterNetwork)
            {
                MulticastOption mcOpt = new MulticastOption(multicastAddr);

                _clientSocket.SetSocketOption(
                    SocketOptionLevel.IP,
                    SocketOptionName.DropMembership,
                    mcOpt);
            }
            else
            {
                IPv6MulticastOption mcOpt = new IPv6MulticastOption(multicastAddr);

                _clientSocket.SetSocketOption(
                    SocketOptionLevel.IPv6,
                    SocketOptionName.DropMembership,
                    mcOpt);
            }
        }
        ///     <devdoc>
        ///         <para>
        ///             Joins a multicast address group.
        ///         </para>
        ///     </devdoc>
        public void JoinMulticastGroup(IPAddress multicastAddr) {
            //
            // parameter validation
            //
            if (m_CleanedUp){
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            //
            // IPv6 Changes: we need to create the correct MulticastOption and
            //               must also check for address family compatibility
            //
            if ( multicastAddr.AddressFamily != m_Family ) {
                throw new ArgumentException(SR.GetString(SR.net_protocol_invalid_multicast_family, "UDP"), "multicastAddr");
            }

            if ( m_Family == AddressFamily.InterNetwork ) {
                MulticastOption mcOpt = new MulticastOption(multicastAddr);

                Client.SetSocketOption(
                    SocketOptionLevel.IP,
                    SocketOptionName.AddMembership,
                    mcOpt );
            }
            else {
                IPv6MulticastOption mcOpt = new IPv6MulticastOption(multicastAddr);

                Client.SetSocketOption(
                    SocketOptionLevel.IPv6,
                    SocketOptionName.AddMembership,
                    mcOpt );
            }
        }
        public static SocketError SetIPv6MulticastOption(SafeCloseSocket handle, SocketOptionName optionName, IPv6MulticastOption optionValue)
        {
            Interop.Winsock.IPv6MulticastRequest ipmr = new Interop.Winsock.IPv6MulticastRequest();

            ipmr.MulticastAddress = optionValue.Group.GetAddressBytes();
            ipmr.InterfaceIndex   = unchecked ((int)optionValue.InterfaceIndex);

            // This can throw ObjectDisposedException.
            SocketError errorCode = Interop.Winsock.setsockopt(
                handle,
                SocketOptionLevel.IPv6,
                optionName,
                ref ipmr,
                Interop.Winsock.IPv6MulticastRequest.Size);

            return(errorCode == SocketError.SocketError ? GetLastSocketError() : SocketError.Success);
        }
Example #12
0
 private void setIPv6MulticastOption(SocketOptionName optionName, IPv6MulticastOption MR)
 {
     if (UnsafeNclNativeMethods.OSSOCK.setsockopt(this.m_Handle, SocketOptionLevel.IPv6, optionName, ref new IPv6MulticastRequest()
       {
     MulticastAddress = MR.Group.GetAddressBytes(),
     InterfaceIndex = (int) MR.InterfaceIndex
       }, IPv6MulticastRequest.Size) != SocketError.SocketError)
     return;
       SocketException socketException = new SocketException();
       this.UpdateStatusAfterSocketError(socketException);
       if (Socket.s_LoggingEnabled)
     Logging.Exception(Logging.Sockets, (object) this, "setIPv6MulticastOption", (Exception) socketException);
       throw socketException;
 }
Example #13
0
        public static SocketError GetIPv6MulticastOption(SafeCloseSocket handle, SocketOptionName optionName, out IPv6MulticastOption optionValue)
        {
            Interop.Winsock.IPv6MulticastRequest ipmr = new Interop.Winsock.IPv6MulticastRequest();

            int optlen = Interop.Winsock.IPv6MulticastRequest.Size;

            // This can throw ObjectDisposedException.
            SocketError errorCode = Interop.Winsock.getsockopt(
                handle,
                SocketOptionLevel.IP,
                optionName,
                out ipmr,
                ref optlen);

            if (errorCode == SocketError.SocketError)
            {
                optionValue = default(IPv6MulticastOption);
                return(GetLastSocketError());
            }

            optionValue = new IPv6MulticastOption(new IPAddress(ipmr.MulticastAddress), ipmr.InterfaceIndex);
            return(SocketError.Success);
        }
Example #14
0
        // IPv6 setsockopt for JOIN / LEAVE multicast group.
        private void SetIPv6MulticastOption(SocketOptionName optionName, IPv6MulticastOption MR)
        {
            SocketError errorCode = SocketPal.SetIPv6MulticastOption(_handle, optionName, MR);

            if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"Interop.Winsock.setsockopt returns errorCode:{errorCode}");

            // Throw an appropriate SocketException if the native call fails.
            if (errorCode != SocketError.Success)
            {
                // Update the internal state of this socket according to the error before throwing.
                SocketException socketException = new SocketException((int)errorCode);
                UpdateStatusAfterSocketError(socketException);
                if (NetEventSource.IsEnabled) NetEventSource.Error(this, socketException);
                throw socketException;
            }
        }
Example #15
0
		public void InterfaceIndex_Value_OutOfRange ()
		{
			IPAddress group = IPAddress.Parse ("239.255.255.250");
			IPv6MulticastOption option = new IPv6MulticastOption (group, 10);

			try {
				option.InterfaceIndex = -1;
				Assert.Fail ("#A1");
			} catch (ArgumentOutOfRangeException ex) {
				// Specified argument was out of the range of valid values
				Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
				Assert.IsNull (ex.InnerException, "#A3");
				Assert.IsNotNull (ex.Message, "#A4");
				Assert.AreEqual ("value", ex.ParamName, "#A5");
			}

			try {
				option.InterfaceIndex = 0x100000000;
				Assert.Fail ("#B1");
			} catch (ArgumentOutOfRangeException ex) {
				// Specified argument was out of the range of valid values
				Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
				Assert.IsNull (ex.InnerException, "#B3");
				Assert.IsNotNull (ex.Message, "#B4");
				Assert.AreEqual ("value", ex.ParamName, "#B5");
			}

			// ensure original value was retained
			Assert.AreEqual (10, option.InterfaceIndex, "#C");
		}
        ///     <devdoc>
        ///         <para>
        ///             Joins an IPv6 multicast address group.
        ///         </para>
        ///     </devdoc>
        public void JoinMulticastGroup(int ifindex,IPAddress multicastAddr) {
            //
            // parameter validation
            //
            if ( m_CleanedUp ){
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            if ( multicastAddr==null ) {
                throw new ArgumentNullException("multicastAddr");
            }

            if ( ifindex < 0 ) {
                throw new ArgumentException(SR.GetString(SR.net_value_cannot_be_negative), "ifindex");
            }

            //
            // Ensure that this is an IPv6 client, otherwise throw WinSock 
            // Operation not supported socked exception.
            //
            if ( m_Family != AddressFamily.InterNetworkV6 ) {
                throw new SocketException(SocketError.OperationNotSupported);
            }

            IPv6MulticastOption mcOpt = new IPv6MulticastOption(multicastAddr,ifindex);

            Client.SetSocketOption(
                SocketOptionLevel.IPv6,
                SocketOptionName.AddMembership,
                mcOpt );
        }
 public void JoinMulticastGroup(int ifindex, IPAddress multicastAddr)
 {
     if (this.m_CleanedUp)
     {
         throw new ObjectDisposedException(base.GetType().FullName);
     }
     if (multicastAddr == null)
     {
         throw new ArgumentNullException("multicastAddr");
     }
     if (ifindex < 0)
     {
         throw new ArgumentException(SR.GetString("net_value_cannot_be_negative"), "ifindex");
     }
     if (this.m_Family != AddressFamily.InterNetworkV6)
     {
         throw new SocketException(SocketError.OperationNotSupported);
     }
     IPv6MulticastOption optionValue = new IPv6MulticastOption(multicastAddr, (long) ifindex);
     this.Client.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.AddMembership, optionValue);
 }
Example #18
0
        private void SetIPv6MulticastOption(SocketOptionName optionName, IPv6MulticastOption mr)
        {
            var optionValue = new IPv6MulticastRequest()
            {
                MulticastAddress = mr.Group.GetAddressBytes(),
                InterfaceIndex = (int)mr.InterfaceIndex
            };

            if (UnsafeMethods.setsockopt(Handle, SocketOptionLevel.IPv6, optionName, ref optionValue, IPv6MulticastRequest.Size) ==
                SocketError.SocketError)
            {
                throw new SocketException();
            }
        }
Example #19
0
        // Leaves an IPv6 multicast address group.
        public void DropMulticastGroup(IPAddress multicastAddr, int ifindex)
        {
            // Validate input parameters.
            if (_cleanedUp)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            if (multicastAddr == null)
            {
                throw new ArgumentNullException("multicastAddr");
            }

            if (ifindex < 0)
            {
                throw new ArgumentException(SR.net_value_cannot_be_negative, "ifindex");
            }

            // Ensure that this is an IPv6 client, otherwise throw WinSock 
            // Operation not supported socked exception.
            if (_family != AddressFamily.InterNetworkV6)
            {
                throw new SocketException((int)SocketError.OperationNotSupported);
            }

            IPv6MulticastOption mcOpt = new IPv6MulticastOption(multicastAddr, ifindex);

            _clientSocket.SetSocketOption(
                SocketOptionLevel.IPv6,
                SocketOptionName.DropMembership,
                mcOpt);
        }
Example #20
0
        public void DropMulticastGroup(IPAddress address)
        {
            Assert.AreEqual(address.AddressFamily, this.LocalEp.AddressFamily, "address.AddressFamily");

            if (this.LocalEp.AddressFamily == AddressFamily.InterNetwork)
            {
                var option1 = new MulticastOption(address);
                this._client.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.DropMembership, option1);
            }
            else
            {
                var option2 = new IPv6MulticastOption(address);
                this._client.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.DropMembership, option2);
            }
        }
Example #21
0
        private static RegisterMessage ProcessLeaveRequest(RegisterMessage leaveRequestMessage, IPAddress senderIP)
        {
            try
            {
                lock (clientRegTable.SyncRoot)
                {
                    if (clientRegTable.ContainsKey(senderIP))
                    {
                        bool drop = true;
                        clientRegTable.Remove(senderIP);
                        ReflectorMgr.PC[ReflectorPC.ID.CurrentParticipats] = clientRegTable.Count;

                        // Drop membership if no other member exists
                        foreach (ClientEntry entry in clientRegTable.Values)
                        {
                            if (entry.GroupEP.Address.Equals(leaveRequestMessage.groupIP))
                            {
                                drop = false;
                                break;
                            }
                        }

                        if (drop)
                        {
                            if (leaveRequestMessage.groupIP.AddressFamily == AddressFamily.InterNetwork)
                            {
                                MulticastOption mo = new MulticastOption(leaveRequestMessage.groupIP,ReflectorMgr.MulticastInterfaceIP);
                                ReflectorMgr.Sockets.SockMCv4RTP.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.DropMembership, mo);
                                ReflectorMgr.Sockets.SockMCv4RTCP.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.DropMembership, mo);
                            }
                            else
                            {
                                IPv6MulticastOption mo = new IPv6MulticastOption(leaveRequestMessage.groupIP);
                                ReflectorMgr.Sockets.SockMCv6RTP.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.DropMembership, mo);
                                ReflectorMgr.Sockets.SockMCv6RTCP.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.DropMembership, mo);
                            }
                        }

                        return new RegisterMessage(MessageType.Confirm, leaveRequestMessage.groupIP, leaveRequestMessage.groupPort, leaveRequestMessage.confirmNumber);
                    }
                    else
                    {   // No entries found for the leave request
                        return new RegisterMessage(MessageType.LeaveWithoutJoinError, IPAddress.Any);
                    }
                }
            }

            catch
            {
                return new RegisterMessage(MessageType.UnknownError, IPAddress.Any);
            }
        }
Example #22
0
        public void JoinMulticastGroup(IPAddress address, int ttl)
        {
            Assert.AreEqual(address.AddressFamily, this.LocalEp.AddressFamily, "address.AddressFamily");
            Assert.That(this.IsStarted, () => new InvalidOperationException("UdpListener must be started to join multicast group"));

            switch (this.LocalEp.AddressFamily)
            {
                case AddressFamily.InterNetwork:
                    var option1 = new MulticastOption(address, this.LocalEp.Address);
                    this._client.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, option1);
                    this._client.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, ttl);
                    break;
                case AddressFamily.InterNetworkV6:
                    //FIXME: this should be using the interface index of the interface we are bound to.
                    var option2 = new IPv6MulticastOption(address);
                    this._client.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.AddMembership, option2);
                    this._client.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.MulticastTimeToLive, ttl);
                    break;
                default:
                    Assert.That(false, () => new NotSupportedException("UdpListener only supports IPv4 and IPv6."));
                    break;
            }
        }
Example #23
0
        /// <devdoc>
        ///     <para>
        ///         IPv6 getsockopt for JOIN / LEAVE multicast group
        ///     </para>
        /// </devdoc>
        private IPv6MulticastOption getIPv6MulticastOpt(SocketOptionName optionName) {
            IPv6MulticastRequest ipmr = new IPv6MulticastRequest();

            int optlen = IPv6MulticastRequest.Size;

            // This can throw ObjectDisposedException.
            SocketError errorCode = UnsafeNclNativeMethods.OSSOCK.getsockopt(
                m_Handle,
                SocketOptionLevel.IP,
                optionName,
                out ipmr,
                ref optlen);

            GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::getIPv6MulticastOpt() UnsafeNclNativeMethods.OSSOCK.getsockopt returns errorCode:" + errorCode);

            //
            // if the native call fails we'll throw a SocketException
            //
            if (errorCode==SocketError.SocketError) {
                //
                // update our internal state after this socket error and throw
                //
                SocketException socketException = new SocketException();
                UpdateStatusAfterSocketError(socketException);
                if(s_LoggingEnabled)Logging.Exception(Logging.Sockets, this, "getIPv6MulticastOpt", socketException);
                throw socketException;
            }

            IPv6MulticastOption multicastOption = new IPv6MulticastOption(new IPAddress(ipmr.MulticastAddress),ipmr.InterfaceIndex);

            return multicastOption;
        }
Example #24
0
        public void JoinMulticastGroup(IPAddress address)
        {
            Assert.That(this.IsStarted, () => new InvalidOperationException("UdpListener must be started to join multicast group"));
            Assert.AreEqual(address.AddressFamily, this.LocalEp.AddressFamily, "address.AddressFamily");

            switch (this.LocalEp.AddressFamily)
            {
                case AddressFamily.InterNetwork:
                    {
                        var option1 = new MulticastOption(address, this.LocalEp.Address);
                        this._client.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, option1);
                    }
                    break;
                case AddressFamily.InterNetworkV6:
                    {
                        var option2 = new IPv6MulticastOption(address);
                        this._client.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.AddMembership, option2);
                    }
                    break;
                default:
                    Assert.That(false,() => new NotSupportedException("UdpListener only supports IPv4 and IPv6."));
                    break;
            }
        }
Example #25
0
        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;
        }
Example #26
0
        public static SocketError SetIPv6MulticastOption(SafeCloseSocket handle, SocketOptionName optionName, IPv6MulticastOption optionValue)
        {
            Interop.Winsock.IPv6MulticastRequest ipmr = new Interop.Winsock.IPv6MulticastRequest();

            ipmr.MulticastAddress = optionValue.Group.GetAddressBytes();
            ipmr.InterfaceIndex = unchecked((int)optionValue.InterfaceIndex);

            // This can throw ObjectDisposedException.
            SocketError errorCode = Interop.Winsock.setsockopt(
                handle,
                SocketOptionLevel.IPv6,
                optionName,
                ref ipmr,
                Interop.Winsock.IPv6MulticastRequest.Size);
            return errorCode == SocketError.SocketError ? GetLastSocketError() : SocketError.Success;
        }
Example #27
0
        public static unsafe SocketError GetIPv6MulticastOption(SafeCloseSocket handle, SocketOptionName optionName, out IPv6MulticastOption optionValue)
        {
            Debug.Assert(optionName == SocketOptionName.AddMembership || optionName == SocketOptionName.DropMembership);

            Interop.Sys.MulticastOption optName = optionName == SocketOptionName.AddMembership ?
                Interop.Sys.MulticastOption.MULTICAST_ADD :
                Interop.Sys.MulticastOption.MULTICAST_DROP;

            Interop.Sys.IPv6MulticastOption opt;
            Interop.Error err = Interop.Sys.GetIPv6MulticastOption(handle.FileDescriptor, optName, &opt);
            if (err != Interop.Error.SUCCESS)
            {
                optionValue = default(IPv6MulticastOption);
                return GetSocketErrorForErrorCode(err);
            }

            optionValue = new IPv6MulticastOption(opt.Address.GetIPAddress(), opt.InterfaceIndex);
            return SocketError.Success;
        }
Example #28
0
        public static SocketError GetIPv6MulticastOption(SafeCloseSocket handle, SocketOptionName optionName, out IPv6MulticastOption optionValue)
        {
            Interop.Winsock.IPv6MulticastRequest ipmr = new Interop.Winsock.IPv6MulticastRequest();

            int optlen = Interop.Winsock.IPv6MulticastRequest.Size;

            // This can throw ObjectDisposedException.
            SocketError errorCode = Interop.Winsock.getsockopt(
                handle,
                SocketOptionLevel.IP,
                optionName,
                out ipmr,
                ref optlen);

            if (errorCode == SocketError.SocketError)
            {
                optionValue = default(IPv6MulticastOption);
                return GetLastSocketError();
            }

            optionValue = new IPv6MulticastOption(new IPAddress(ipmr.MulticastAddress), ipmr.InterfaceIndex);
            return SocketError.Success;
        }
Example #29
0
        // IPv6 setsockopt for JOIN / LEAVE multicast group.
        private void SetIPv6MulticastOption(SocketOptionName optionName, IPv6MulticastOption MR)
        {
            SocketError errorCode = SocketPal.SetIPv6MulticastOption(_handle, optionName, MR);

            GlobalLog.Print("Socket#" + Logging.HashString(this) + "::setIPv6MulticastOption() Interop.Winsock.setsockopt returns errorCode:" + errorCode);

            // Throw an appropriate SocketException if the native call fails.
            if (errorCode != SocketError.Success)
            {
                // Update the internal state of this socket according to the error before throwing.
                SocketException socketException = new SocketException((int)errorCode);
                UpdateStatusAfterSocketError(socketException);
                if (s_loggingEnabled)
                {
                    Logging.Exception(Logging.Sockets, this, "setIPv6MulticastOption", socketException);
                }
                throw socketException;
            }
        }
 public void JoinMulticastGroup(IPAddress multicastAddr)
 {
     if (this.m_CleanedUp)
     {
         throw new ObjectDisposedException(base.GetType().FullName);
     }
     if (multicastAddr == null)
     {
         throw new ArgumentNullException("multicastAddr");
     }
     if (multicastAddr.AddressFamily != this.m_Family)
     {
         throw new ArgumentException(SR.GetString("net_protocol_invalid_multicast_family", new object[] { "UDP" }), "multicastAddr");
     }
     if (this.m_Family == AddressFamily.InterNetwork)
     {
         MulticastOption optionValue = new MulticastOption(multicastAddr);
         this.Client.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, optionValue);
     }
     else
     {
         IPv6MulticastOption option2 = new IPv6MulticastOption(multicastAddr);
         this.Client.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.AddMembership, option2);
     }
 }
Example #31
0
        internal static void ReleaseSharedSocket(IPEndPoint endPoint, LstSocks.Socket sock)
        {
            object sockObj = socks[endPoint];
            if( sockObj == null )
                throw new InvalidOperationException("Sock does not exist as a shared socket.");

            lock(socks)
            {
                if( --sock.refCount <= 0 )
                {
                    // Leave the multicast group
                    if( Utility.IsMulticast(endPoint.Address) )
                    {
                        try
                        {
                            if (endPoint.AddressFamily == AddressFamily.InterNetworkV6)
                            {
                                IPv6MulticastOption mo = new IPv6MulticastOption(endPoint.Address);
                                sock.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.DropMembership, mo);
                            } 
                            else
                            {
                                MulticastOption mo = new MulticastOption(endPoint.Address);
                                sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.DropMembership, mo);
                            }
                        }
                        catch {} // The user of the socket *may* not have joined the multicast group (?)
                    }

                    // Remove ourselves from the shared pool
                    socks.Remove(endPoint);

                    // Close the socket
                    try
                    {
                        sock.Close();
                    }
                    catch(ObjectDisposedException) {}
                }
            }
        }
Example #32
0
		public void InterfaceIndex ()
		{
			IPAddress group;
			IPv6MulticastOption option;

			group = IPAddress.Parse ("239.255.255.250");
			option = new IPv6MulticastOption (group, 10);
			option.InterfaceIndex = 0;
			Assert.AreSame (group, option.Group, "#A1");
			Assert.AreEqual (0, option.InterfaceIndex, "#A2");
			option.InterfaceIndex = 124;
			Assert.AreSame (group, option.Group, "#B1");
			Assert.AreEqual (124, option.InterfaceIndex, "#B2");
			option.InterfaceIndex = 0xFFFFFFFF;
			Assert.AreSame (group, option.Group, "#C1");
			Assert.AreEqual (0xFFFFFFFF, option.InterfaceIndex, "#C3");
		}