Esempio n. 1
0
        public static void ToString_Compare_Success()
        {
            SocketAddress sa1 = new SocketAddress(AddressFamily.InterNetwork, 64);
            SocketAddress sa2 = new SocketAddress(AddressFamily.InterNetwork, 64);
            SocketAddress sa3 = new SocketAddress(AddressFamily.InterNetwork, 48);

            SocketAddress sa4 = new SocketAddress(AddressFamily.InterNetworkV6, 48);

            Assert.Equal(sa1.ToString(), sa2.ToString());
            Assert.NotEqual(sa1.ToString(), sa3.ToString());
            Assert.NotEqual(sa1.ToString(), sa4.ToString());
        }
Esempio n. 2
0
        public void NetTest6_SocketAddressBasic()
        {
            Random random = new();

            for (int i = 0; i <= 30; i++)
            {
                int[] IPInts =
                {
                    random.Next(256),
                    random.Next(256),
                    random.Next(256),
                    random.Next(128)
                };

                Debug.WriteLine($"Random IP {IPInts[0]}.{IPInts[1]}.{IPInts[2]}.{IPInts[3]}");

                IPAddress address = new(
                    IPInts[0]
                    + IPInts[1] * 256
                    + IPInts[2] * 256 * 256
                    + IPInts[3] * 256 * 256 * 256);

                int portInt = random.Next(65536);

                IPEndPoint ipEndpoint1 = new(address, portInt);

                SocketAddress socketAddress1 = ipEndpoint1.Serialize();
                SocketAddress socketAddress2 = ipEndpoint1.Serialize();

                Assert.NotNull(socketAddress1, "socketAddress1 is null");
                Assert.NotNull(socketAddress2, "socketAddress2 is null");

                Type typeOfSocketAddress = socketAddress1.GetType();
                Assert.IsType(typeOfSocketAddress, Type.GetType("System.Net.SocketAddress"), "socketAddress1 Type is incorrect");

                typeOfSocketAddress = socketAddress2.GetType();
                Assert.IsType(typeOfSocketAddress, Type.GetType("System.Net.SocketAddress"), "socketAddress2 Type is incorrect");

                Assert.Equal(socketAddress1.ToString(), socketAddress2.ToString(), "ToString returns differently for same data");

                Assert.Equal(socketAddress1.GetHashCode(), socketAddress2.GetHashCode(), $"GetHashCode returns differently for same data");
                Assert.True(socketAddress1.Family == AddressFamily.InterNetwork, "socketAddress1 Family is incorrect");
                Assert.True(socketAddress2.Family == AddressFamily.InterNetwork, "socketAddress2 Family is incorrect");

                // Recreate a different Socket
                socketAddress2 = new SocketAddress(AddressFamily.Chaos, 8);

                Assert.NotEqual(socketAddress1.GetHashCode(), socketAddress2.GetHashCode(), "GetHashCode returns same for "
                                + socketAddress1.ToString() + " " + socketAddress1.GetHashCode()
                                + " as " + socketAddress2.ToString() + " " + socketAddress2.GetHashCode());
            }
        }
Esempio n. 3
0
        public void ToString_ValidSocketAddress_ExpectedFormat()
        {
            IPEndPoint    ipLocalEndPoint = new IPEndPoint(IPAddress.Loopback, Convert.ToInt32("cafe", 16));
            SocketAddress socketAddress   = ipLocalEndPoint.Serialize();

            Assert.Equal("InterNetwork:16:{202,254,127,0,0,1,0,0,0,0,0,0,0,0}", socketAddress.ToString());
        }
Esempio n. 4
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldThrowErrorWithRemoteAddressWhenClosed() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldThrowErrorWithRemoteAddressWhenClosed()
        {
            Channel          channel   = mock(typeof(Channel));
            ByteBufAllocator allocator = mock(typeof(ByteBufAllocator));

            when(allocator.buffer(anyInt())).thenReturn(Unpooled.buffer());
            when(channel.alloc()).thenReturn(allocator);
            SocketAddress remoteAddress       = mock(typeof(SocketAddress));
            string        remoteAddressString = "client.server.com:7687";

            when(remoteAddress.ToString()).thenReturn(remoteAddressString);
            when(channel.remoteAddress()).thenReturn(remoteAddress);

            ChunkedOutput output = new ChunkedOutput(channel, DEFAULT_TEST_BUFFER_SIZE, DEFAULT_TEST_BUFFER_SIZE, NO_THROTTLE);

            output.Dispose();

            try
            {
                output.WriteInt(42);
                fail("Exception expected");
            }
            catch (PackOutputClosedException e)
            {
                assertThat(e.Message, containsString(remoteAddressString));
            }
        }
Esempio n. 5
0
        public static void ToString_LegacyUnknownFamily_Success(AddressFamily family)
        {
            // For legacy reasons, unknown values in ushort range don't throw on Windows.
            var sa = new SocketAddress(family);

            Assert.NotNull(sa.ToString());
        }
Esempio n. 6
0
    public static void Main()
    {
        IPAddress  test1 = IPAddress.Parse("192.168.1.1");
        IPEndPoint ie    = new IPEndPoint(test1, 8000);

        ie.Port = 8000;
        SocketAddress sa = ie.Serialize();

        Console.WriteLine("The SocketAddress is: {0}", sa.ToString());
    }
        static void Main(string[] args)
        {
            IPAddress     ipAddress       = Dns.GetHostEntry("localhost").AddressList[0];
            IPEndPoint    ipLocalEndPoint = new IPEndPoint(ipAddress, 11000);
            SocketAddress socketAddress   = ipLocalEndPoint.Serialize();

            Console.WriteLine("Contents of the socketAddress are: " + socketAddress.ToString());
            Console.WriteLine("The address family of the socketAddress is: " + socketAddress.Family.ToString());
            Console.WriteLine("The size of the underlying buffer is: " + socketAddress.Size.ToString());
        }
Esempio n. 8
0
        public static void fSocketAddress()
        {
            IPAddress  ipAddr1    = IPAddress.Parse("127.0.0.1");
            IPAddress  ipAddr2    = IPAddress.Parse("::1");
            IPEndPoint ipEndPoint = new IPEndPoint(ipAddr1, 80);

            SocketAddress SocketAddr = ipEndPoint.Serialize();

            MessageBox.Show(SocketAddr.ToString());
        }
Esempio n. 9
0
        public static RubyArray /*!*/ Accept(RubyContext /*!*/ context, RubySocket /*!*/ self)
        {
            RubyArray  result = new RubyArray(2);
            RubySocket s      = new RubySocket(context, self.Socket.Accept());

            result.Add(s);
            SocketAddress addr = s.Socket.RemoteEndPoint.Serialize();

            result.Add(MutableString.CreateAscii(addr.ToString()));
            return(result);
        }
Esempio n. 10
0
        public static void ToString_Compare_Success()
        {
            SocketAddress sa1 = new SocketAddress(AddressFamily.InterNetwork, 64);
            SocketAddress sa2 = new SocketAddress(AddressFamily.InterNetwork, 64);
            SocketAddress sa3 = new SocketAddress(AddressFamily.InterNetwork, 48);

            SocketAddress sa4 = new SocketAddress(AddressFamily.InterNetworkV6, 48);

            Assert.Equal(sa1.ToString(), sa2.ToString());
            Assert.NotEqual(sa1.ToString(), sa3.ToString());
            Assert.NotEqual(sa1.ToString(), sa4.ToString());
        }
Esempio n. 11
0
        public static RubyArray /*!*/ SysAccept(RubyContext /*!*/ context, RubySocket /*!*/ self)
        {
            RubyArray result = new RubyArray(2);
            // TODO: Do we need some kind of strong reference to the socket
            // here to stop the RubySocket from being garbage collected?
            RubySocket s = new RubySocket(context, self.Socket.Accept());

            result.Add(s.GetFileDescriptor());
            SocketAddress addr = s.Socket.RemoteEndPoint.Serialize();

            result.Add(MutableString.CreateAscii(addr.ToString()));
            return(result);
        }
Esempio n. 12
0
        public static void ToString_Compare_Success()
        {
            SocketAddress sa1 = new SocketAddress(AddressFamily.InterNetwork, 64);
            SocketAddress sa2 = new SocketAddress(AddressFamily.InterNetwork, 64);
            SocketAddress sa3 = new SocketAddress(AddressFamily.InterNetwork, 48);

            SocketAddress sa4 = new SocketAddress(AddressFamily.InterNetworkV6, 48);

            Assert.Equal(sa1.ToString(), sa2.ToString());
            Assert.NotEqual(sa1.ToString(), sa3.ToString());
            Assert.NotEqual(sa1.ToString(), sa4.ToString());

            Assert.Equal("InterNetwork:64:{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}", sa1.ToString());
            Assert.Equal("InterNetworkV6:48:{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}", sa4.ToString());

            SocketAddress sa5 = new SocketAddress(AddressFamily.InterNetworkV6, 48);

            for (int i = 2; i < sa5.Size; i++)
            {
                sa5[i] = (byte)i;
            }
            Assert.EndsWith("2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47}", sa5.ToString());
        }
Esempio n. 13
0
        public static void ToString_AllFamilies_Success()
        {
            foreach (AddressFamily family in Enum.GetValues(typeof(AddressFamily)))
            {
                if ((int)family > (int)AddressFamily.Max)
                {
                    // Skip Linux specific protocols.
                    continue;
                }

                var sa = new SocketAddress(family);
                Assert.NotNull(sa.ToString());
            }
        }
Esempio n. 14
0
        public static void ToString_AllFamilies_Success()
        {
            foreach (AddressFamily family in Enum.GetValues(typeof(AddressFamily)))
            {
                if (family == AddressFamily.Packet || family == AddressFamily.ControllerAreaNetwork)
                {
                    // Skip Linux specific protocols.
                    continue;
                }

                var sa = new SocketAddress(family);
                Assert.NotNull(sa.ToString());
            }
        }
        public override EndPoint Create(SocketAddress sockAddr)
        {
            if (sockAddr == null ||
                sockAddr.Family != AF_HYPERV ||
                sockAddr.Size != 34)
            {
                return(null);
            }

            var sockAddress = sockAddr.ToString();

            return(new HyperVSocketEndPoint(
                       vmid: new Guid(sockAddress.Substring(4, 16)),
                       serviceid: new Guid(sockAddress.Substring(20, 16))));
        }
Esempio n. 16
0
        public static void Main()
        {
            IPAddress  test1 = IPAddress.Parse("192.168.1.1");
            IPEndPoint ie    = new IPEndPoint(test1, 8000);

            Console.WriteLine("The IPEndPoint is: {0}", ie.ToString());
            Console.WriteLine("The AddressFamily is: {0}", ie.AddressFamily);
            Console.WriteLine("The address is: {0}, and the port is: {1}\n", ie.Address, ie.Port);
            Console.WriteLine("The min port number is: {0}", IPEndPoint.MinPort);
            Console.WriteLine("The max port number is: {0}\n", IPEndPoint.MaxPort);
            ie.Port = 80;
            Console.WriteLine("The changed IPEndPoint value is: {0}", ie.ToString());
            SocketAddress sa = ie.Serialize();

            Console.WriteLine("The SocketAddress is: {0}", sa.ToString());
        }
        // The serializeEndpoint method serializes the endpoint and returns the
        // SocketAddress containing the serialized endpoint data.
        private static SocketAddress serializeEndpoint(IPEndPoint endpoint)
        {
            // Serialize IPEndPoint details to a SocketAddress instance.
            SocketAddress socketAddress = endpoint.Serialize();

            // Display the serialized endpoint information.
            Console.WriteLine("Endpoint.Serialize() : " + socketAddress.ToString());

            Console.WriteLine("Socket.Family : " + socketAddress.Family);
            Console.WriteLine("Socket.Size : " + socketAddress.Size);

            Console.WriteLine("Press any key to continue.");
            Console.ReadLine();

            return(socketAddress);
        }
Esempio n. 18
0
        void Socket_Address()
        {
            //Creates an IpEndPoint.
            IPAddress  ipAddress       = Dns.Resolve("http://localhost:50973/").AddressList[0];
            IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, 11000);

            //Serializes the IPEndPoint.
            SocketAddress socketAddress = ipLocalEndPoint.Serialize();

            //Verifies that ipLocalEndPoint is now serialized by printing its contents.
            Console.WriteLine("Contents of the socketAddress are: " + socketAddress.ToString());
            //Checks the Family property.
            Console.WriteLine("The address family of the socketAddress is: " + socketAddress.Family.ToString());
            //Checks the underlying buffer size.
            Console.WriteLine("The size of the underlying buffer is: " + socketAddress.Size.ToString());
        }
 /// <summary>
 /// Match record against address
 /// </summary>
 /// <param name="record"></param>
 /// <param name="address"></param>
 /// <param name="type"></param>
 /// <returns></returns>
 public static bool Matches(this INameRecord record, SocketAddress address,
                            NameRecordType type)
 {
     if (address.Family == AddressFamily.Collection)
     {
         return(Matches(record, (SocketAddressCollection)address, type));
     }
     if (Matches(record, address.ToString(), type))
     {
         return(true);
     }
     if (address.Family == AddressFamily.InterNetworkV6)
     {
         return(Matches(record, Reference.FromSocketAddress((Inet6SocketAddress)address), type));
     }
     return(false);
 }
        public override EndPoint Create(SocketAddress SockAddr)
        {
            if (SockAddr == null ||
                SockAddr.Family != AF_HYPERV ||
                SockAddr.Size != 34)
            {
                return(null);
            }

            HyperVSocketEndPoint endpoint = new HyperVSocketEndPoint(SockAddr.Family, Guid.Empty, Guid.Empty);

            string sockAddress = SockAddr.ToString();

            endpoint.VmId      = new Guid(sockAddress.Substring(4, 16));
            endpoint.ServiceId = new Guid(sockAddress.Substring(20, 16));

            return(endpoint);
        }
Esempio n. 21
0
        private ChannelContext AcquireChannelContext(RequestType type)
        {
            if (_channelPool == null)
            {
                throw new ComException(string.Format("Client for {0} is stopped", _destination.ToString()));
            }

            // Calling acquire is dangerous since it may be a blocking call... and if this
            // thread holds a lock which others may want to be able to communicate with
            // the server things go stiff.
            ChannelContext result = _channelPool.acquire();

            if (result == null)
            {
                _msgLog.error("Unable to acquire new channel for " + type);
                throw TraceComException(new ComException("Unable to acquire new channel for " + type), "Client.acquireChannelContext");
            }
            return(result);
        }
Esempio n. 22
0
    public static void MySerializeIPEndPointClassMethod()
    {
//<Snippet1>

//Creates an IpEndPoint.
        IPAddress  ipAddress       = Dns.Resolve("www.contoso.com").AddressList[0];
        IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, 11000);

//Serializes the IPEndPoint.
        SocketAddress socketAddress = ipLocalEndPoint.Serialize();

//Verifies that ipLocalEndPoint is now serialized by printing its contents.
        Console.WriteLine("Contents of the socketAddress are: " + socketAddress.ToString());
//Checks the Family property.
        Console.WriteLine("The address family of the socketAddress is: " + socketAddress.Family.ToString());
//Checks the underlying buffer size.
        Console.WriteLine("The size of the underlying buffer is: " + socketAddress.Size.ToString());

//</Snippet1>
    }
Esempio n. 23
0
 private static string GetHostString(SocketAddress address)
 {
     return(address is InetSocketAddress ? (( InetSocketAddress )address).HostString : address.ToString());
 }
        public IncomingUDPmessageBoundle(SocketAddress remSktAddr, string message)
        {
            srcUDPmessage       = message;
            remoteSocketAddress = remSktAddr;
            string addrStr = remoteSocketAddress.ToString(); //InterNetwork:16:{21,179,192,168,192,221,0,0,0,0,0,0,0,0}

            addrStr = addrStr.Substring(addrStr.IndexOf("{") + 1);
            addrStr = addrStr.Substring(0, addrStr.Length - 1);
            char[]   splitChar       = { ',' };
            string[] sktAddrStrArray = addrStr.Split(splitChar);
            ipAddrString = sktAddrStrArray[2] + "." + sktAddrStrArray[3] + "." + sktAddrStrArray[4] + "." + sktAddrStrArray[5];


            if ((message.IndexOf(plusplusSign) > -1) || (message.IndexOf(plusSign) > -1))
            {
                isPartOfMessage = true;

                if ((message.Substring(0, 3) == plusSign) && (message.Substring(message.Length - 3) == plusSign))
                {
                    // середина кортежа
                    udpMessage = message.Replace(plusSign, "");
                }
                else if ((message.Substring(0, 4) == plusplusSign) && (message.Substring(message.Length - 3) == plusSign))
                {
                    // начало кортежа
                    udpMessage     = message.Replace(plusSign, "");
                    udpMessage     = udpMessage.Replace(plusplusSign, "");
                    isStartOfTuple = true;
                }
                else if ((message.Substring(0, 3) == plusSign) && (message.Substring(message.Length - 4) == plusplusSign))
                {
                    // конец кортежа
                    udpMessage   = message.Replace(plusSign, "");
                    udpMessage   = udpMessage.Replace(plusplusSign, "");
                    isEndOfTuple = true;
                }


                if ((udpMessage.Length >= msgIDsign.Length) && (udpMessage.IndexOf(msgIDsign) >= 0))
                {
                    int    idxStartMsgIDtag = udpMessage.IndexOf(msgIDsign);
                    int    idxEndMsgIDtag   = udpMessage.IndexOf('>', idxStartMsgIDtag);
                    string strMsgIDTag      = udpMessage.Substring(idxStartMsgIDtag, idxEndMsgIDtag - idxStartMsgIDtag + 1); // "<mid:615253478172>"

                    try
                    {
                        strMsgIDTag = strMsgIDTag.Replace(msgIDsign, "");
                        strMsgIDTag = strMsgIDTag.Replace(">", "");
                        mID         = Convert.ToInt32(strMsgIDTag);
                    }
                    catch (Exception)
                    {
                        mID = 0;
                    }

                    udpMessage = udpMessage.Replace(msgIDsign + mID + ">", "");
                }
                else
                {
                    mID = 0;
                }



                if ((udpMessage.Length >= msgSubIDsign.Length) && (udpMessage.IndexOf(msgSubIDsign) >= 0))
                {
                    int    idxStartMsgSubIDtag = udpMessage.IndexOf(msgSubIDsign);
                    int    idxEndMsgSubIDtag   = udpMessage.IndexOf('>', idxStartMsgSubIDtag);
                    string strMsgSubIDTag      = udpMessage.Substring(idxStartMsgSubIDtag, idxEndMsgSubIDtag - idxStartMsgSubIDtag + 1); // "<mid:615253478172>"

                    try
                    {
                        strMsgSubIDTag = strMsgSubIDTag.Replace(msgSubIDsign, "");
                        strMsgSubIDTag = strMsgSubIDTag.Replace(">", "");
                        mSubID         = Convert.ToInt32(strMsgSubIDTag);
                    }
                    catch (Exception)
                    {
                        mSubID = 0;
                    }

                    udpMessage = udpMessage.Replace(msgSubIDsign + mSubID + ">", "");
                }
                else
                {
                    mSubID = 0;
                }
            }
            else
            {
                udpMessage    = message;
                srcUDPmessage = message;
            }



            if ((udpMessage.Length >= 5) && (udpMessage.IndexOf("<id") >= 0))
            {
                int    idxStartDevIDtag = udpMessage.IndexOf("<id");
                string strDevIDTag      = udpMessage.Substring(idxStartDevIDtag, 5); // "<id2>"

                try
                {
                    strDevIDTag = strDevIDTag.Replace("<id", "");
                    strDevIDTag = strDevIDTag.Replace(">", "");
                    devID       = Convert.ToInt32(strDevIDTag);
                }
                catch (Exception)
                {
                    devID = 0;
                }

                udpMessage = udpMessage.Replace("<id" + devID + ">", "");
            }
            else
            {
                devID = 0;
            }


            /// TODO: The 'then' statement is equivalent to the 'else' statement
            if (isPartOfMessage)
            {
                if ((udpMessage.Length >= 6) && (udpMessage.IndexOf("<repl>") >= 0))
                {
                    udpMessage     = udpMessage.Replace("<repl>", "");
                    isReplyMessage = true;
                }

                //if ((udpMessage.Length >= 5) && (udpMessage.IndexOf("<err>") >= 0))
                //{
                //    udpMessage = udpMessage.Replace("<err>", "");
                //    isErrorMessage = true;
                //}
            }
            else
            {
                if ((udpMessage.Length >= 6) && (udpMessage.IndexOf("<repl>") >= 0))
                {
                    udpMessage     = udpMessage.Replace("<repl>", "");
                    isReplyMessage = true;
                }
            }
        }
Esempio n. 25
0
        public MFTestResults NetTest6_SocketAddressBasic()
        {
            /// <summary>
            /// 1. Creates 30 Random IPs between 0.0.0.0 and 255.255.255.127
            /// 2. Verifies that they can be constructed as SocketAddress
            /// 3. Verifies that they have the correct data (GetAddressBytes)
            /// 4. Verifies ToString and GetHashcode
            /// </summary>
            ///
            bool testResult = true;

            try
            {
                Random random = new Random();
                for (int i = 0; i <= 30; i++)
                {
                    int[] IPInts = { random.Next(256), random.Next(256),
                                     random.Next(256), random.Next(128) };
                    Log.Comment("Random IP " + IPInts[0] + "." + IPInts[1]
                                + "." + IPInts[2] + "." + IPInts[3]);
                    IPAddress address = new IPAddress((long)(
                                                          IPInts[0]
                                                          + IPInts[1] * 256
                                                          + IPInts[2] * 256 * 256
                                                          + IPInts[3] * 256 * 256 * 256));
                    int           portInt        = random.Next(65536);
                    IPEndPoint    ipEndpoint1    = new IPEndPoint(address, portInt);
                    SocketAddress socketAddress1 = ipEndpoint1.Serialize();
                    SocketAddress socketAddress2 = ipEndpoint1.Serialize();
                    if (socketAddress1 == null)
                    {
                        throw new Exception("socketAddress1 is null");
                    }
                    if (socketAddress2 == null)
                    {
                        throw new Exception("socketAddress2 is null");
                    }

                    Type typeOfSocketAddress = socketAddress1.GetType();
                    if (typeOfSocketAddress != Type.GetType("System.Net.SocketAddress"))
                    {
                        throw new Exception("socketAddress1 Type is incorrect");
                    }
                    typeOfSocketAddress = socketAddress2.GetType();
                    if (typeOfSocketAddress != Type.GetType("System.Net.SocketAddress"))
                    {
                        throw new Exception("socketAddress2 Type is incorrect");
                    }

                    if (socketAddress1.ToString() != socketAddress2.ToString())
                    {
                        throw new Exception("ToString returns differently for same data");
                    }

                    //21295	GetHashCode returns differently for cloned data
                    if (socketAddress1.GetHashCode() != socketAddress2.GetHashCode())
                    {
                        throw new Exception("GetHashCode returns differently for same data");
                    }

                    if (socketAddress1.Family != AddressFamily.InterNetwork)
                    {
                        throw new Exception("socketAddress1 Family is incorrect");
                    }

                    if (socketAddress2.Family != AddressFamily.InterNetwork)
                    {
                        throw new Exception("socketAddress2 Family is incorrect");
                    }

                    /*
                     * Pending Resolution of 17428
                     *
                     * Log.Comment("Recreating socketAddress2 with new data");
                     * int portInt2 = portInt % 2 + 1;
                     * long addressLong2 = (long)(
                     *  (IPInts[0] % 2 + 1)
                     + (IPInts[1] % 2 + 1) * 256
                     + (IPInts[2] % 2 + 1) * 256 * 256
                     + (IPInts[3] % 2 + 1) * 256 * 256 * 256);
                     +
                     + IPEndPoint ipEndpoint2 = new IPEndPoint(addressLong2, portInt2);
                     + socketAddress2 = ipEndpoint2.Serialize();
                     + socketAddress2.Family = AddressFamily.Chaos;
                     */
                    socketAddress2 = new SocketAddress(AddressFamily.Chaos, 8);
                    if (socketAddress1.GetHashCode() == socketAddress2.GetHashCode())
                    {
                        throw new Exception("GetHashCode returns same for "
                                            + socketAddress1.ToString()
                                            + " as " + socketAddress2.ToString());
                    }

                    if (socketAddress1.ToString() == socketAddress2.ToString())
                    {
                        throw new Exception("ToString returns same for different data");
                    }
                }
            }
            catch (Exception e)
            {
                Log.Comment("Caught exception: " + e.Message);
                testResult = false;
            }
            return(testResult ? MFTestResults.Pass : MFTestResults.KnownFailure);
        }
Esempio n. 26
0
 public override string ToString()
 {
     return(string.Concat(m_Family, "/", ProtocolType, "\\", ProtocolType, "\\",
                          string.Join(Media.Common.Extensions.IPEndPoint.IPEndPointExtensions.SchemeSeperator, ProtocolType.ToString().ToLowerInvariant(), SocketAddress == null ? string.Empty : SocketAddress.ToString()),
                          Media.Common.Extensions.IPEndPoint.IPEndPointExtensions.PortSeperator, m_Port));
 }
Esempio n. 27
0
        /// <summary>
        /// This is the thread function that waits for messages from the CDS
        /// net.
        /// </summary>
        protected void listen()
        {
            this.listenSocket = new Socket(AddressFamily.InterNetwork,
                                           SocketType.Dgram, ProtocolType.IP);
            byte[]        recvBuffer    = new byte[HEADER_LENGTH + MAX_USER_DATA];
            int           bytesReceived = 0;
            EndPoint      peerAddr      = new IPEndPoint(IPAddress.Any, 0);
            SocketAddress peerId        = new SocketAddress(
                AddressFamily.InterNetwork, ADDRESS_LENGTH);
            string peerName = "";

            /* Configure the socket and bind it to the discovery address. */
            this.listenSocket.SetSocketOption(SocketOptionLevel.Socket,
                                              SocketOptionName.Broadcast, true);
            // TODO: The following socket options are possibly a security issue.
            this.listenSocket.SetSocketOption(SocketOptionLevel.Socket,
                                              SocketOptionName.ExclusiveAddressUse, false);
            this.listenSocket.SetSocketOption(SocketOptionLevel.Socket,
                                              SocketOptionName.ReuseAddress, true);
            this.listenSocket.Bind(this.bindAddr);

            /* Enter the receiver loop. */
            Debug.WriteLine("ClusterDiscoveryObserver now starts listening on "
                            + this.bindAddr.ToString() + " ...");
            while (true)
            {
                try {
                    bytesReceived = this.listenSocket.ReceiveFrom(recvBuffer,
                                                                  (int)(HEADER_LENGTH + MAX_USER_DATA), SocketFlags.None,
                                                                  ref peerAddr);
                    Debug.WriteLine("Received " + bytesReceived.ToString()
                                    + " Bytes from " + peerAddr.ToString());

                    // Message header consists of
                    // - 2 byte magic number
                    // - 2 byte message type ID.
                    if (BitConverter.ToUInt32(recvBuffer, 0) == MAGIC_NUMBER)
                    {
                        UInt32 msgType = BitConverter.ToUInt32(recvBuffer, 2);
                        Debug.WriteLine("Type of message received is "
                                        + msgType.ToString());

                        /* Unpack the default message. */
                        switch (msgType)
                        {
                        case MSG_TYPE_IAMALIVE:
                        /* falls through. */
                        case MSG_TYPE_IAMHERE:
                        /* falls through. */
                        case MSG_TYPE_SAYONARA:
                            // The message body consists of
                            // - ADDRESS_LENGTH bytes of an IPv4 address
                            // - MAX_NAME_LEN bytes of the name string.
                            for (int i = 0; i < ADDRESS_LENGTH; i++)
                            {
                                peerId[i] = recvBuffer[HEADER_LENGTH + i];
                            }
                            peerName = new ASCIIEncoding().GetString(
                                recvBuffer,
                                (int)HEADER_LENGTH,
                                (int)MAX_NAME_LEN);
                            Debug.WriteLine("Peer response address (ID) is "
                                            + peerId.ToString());
                            Debug.WriteLine("Cluster name is " + peerName);
                            break;

                        default:
                            /* Other messages should not be unpacked. */
                            break;
                        }

                        /* Take appropriate action for the message. */
                        switch (msgType)
                        {
                        case MSG_TYPE_IAMALIVE:
                        /* falls through. */
                        case MSG_TYPE_IAMHERE:
                            if (peerName == this.name)
                            {
                                this.addPeerNode(peerId, peerAddr);
                            }
                            break;

                        case MSG_TYPE_SAYONARA:
                            if (peerName == this.name)
                            {
                                this.removePeerNode(peerId);
                            }
                            break;

                        default:
                            if (msgType >= MSG_TYPE_USER)
                            {
                                this.fireUserMessage(peerAddr, msgType,
                                                     recvBuffer, HEADER_LENGTH);
                            }
                            break;
                        }
                    } /* end if (BitConverter.ToUInt32(recvBuffer, 0) ... */
                } catch (SocketException e) {
                    Debug.WriteLine("A communication error occurred in the "
                                    + "cluster discovery listener thread: " + e.Message);
                    lock (this.listener) {
                        if (!this.isListening)
                        {
                            Debug.WriteLine("The cluster discovery listener "
                                            + "thread is leaving, because a stop was "
                                            + "requested.");
                            return;
                        }
                    }
                } /* end try */
            }     /* end while (true) */
        }