Exemple #1
0
        public static int GetNextAvailablePortTcp(int start, int end)
        {
            IPGlobalProperties iPGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
            HashSet <int>      hashSet            = new HashSet <int>();

            IPEndPoint[] activeTcpListeners = iPGlobalProperties.GetActiveTcpListeners();
            for (int i = 0; i < activeTcpListeners.Length; i++)
            {
                IPEndPoint iPEndPoint = activeTcpListeners[i];
                hashSet.Add(iPEndPoint.Port);
            }
            TcpConnectionInformation[] activeTcpConnections = iPGlobalProperties.GetActiveTcpConnections();
            for (int j = 0; j < activeTcpConnections.Length; j++)
            {
                TcpConnectionInformation tcpConnectionInformation = activeTcpConnections[j];
                hashSet.Add(tcpConnectionInformation.LocalEndPoint.Port);
            }
            for (int k = start; k <= end; k++)
            {
                if (!hashSet.Contains(k))
                {
                    return(k);
                }
            }
            return(-1);
        }
Exemple #2
0
        private unsafe TcpConnectionInformation[] GetTcpConnections(bool listeners)
        {
            int realCount = Interop.Sys.GetEstimatedTcpConnectionCount();
            int infoCount = realCount * 2;

            Interop.Sys.NativeTcpConnectionInformation *infos = stackalloc Interop.Sys.NativeTcpConnectionInformation[infoCount];
            if (Interop.Sys.GetActiveTcpConnectionInfos(infos, &infoCount) == -1)
            {
                throw new NetworkInformationException(SR.net_PInvokeError);
            }

            TcpConnectionInformation[] connectionInformations = new TcpConnectionInformation[infoCount];
            int nextResultIndex = 0;

            for (int i = 0; i < infoCount; i++)
            {
                Interop.Sys.NativeTcpConnectionInformation nativeInfo = infos[i];
                TcpState state = nativeInfo.State;

                if (listeners != (state == TcpState.Listen))
                {
                    continue;
                }

                byte[] localBytes = new byte[nativeInfo.LocalEndPoint.NumAddressBytes];
                fixed(byte *localBytesPtr = localBytes)
                {
                    Buffer.MemoryCopy(nativeInfo.LocalEndPoint.AddressBytes, localBytesPtr, localBytes.Length, localBytes.Length);
                }

                IPAddress  localIPAddress = new IPAddress(localBytes);
                IPEndPoint local          = new IPEndPoint(localIPAddress, (int)nativeInfo.LocalEndPoint.Port);

                IPAddress remoteIPAddress;
                if (nativeInfo.RemoteEndPoint.NumAddressBytes == 0)
                {
                    remoteIPAddress = IPAddress.Any;
                }
                else
                {
                    byte[] remoteBytes = new byte[nativeInfo.RemoteEndPoint.NumAddressBytes];
                    fixed(byte *remoteBytesPtr = &remoteBytes[0])
                    {
                        Buffer.MemoryCopy(nativeInfo.RemoteEndPoint.AddressBytes, remoteBytesPtr, remoteBytes.Length, remoteBytes.Length);
                    }

                    remoteIPAddress = new IPAddress(remoteBytes);
                }

                IPEndPoint remote = new IPEndPoint(remoteIPAddress, (int)nativeInfo.RemoteEndPoint.Port);
                connectionInformations[nextResultIndex++] = new SimpleTcpConnectionInformation(local, remote, state);
            }

            if (nextResultIndex != connectionInformations.Length)
            {
                Array.Resize(ref connectionInformations, nextResultIndex);
            }

            return(connectionInformations);
        }
 private void printTCP(TcpConnectionInformation conn)
 {
     Console.Write(conn.LocalEndPoint.Address.ToString() + ":" + conn.LocalEndPoint.Port.ToString());
     Console.Write(" | ");
     Console.Write(conn.RemoteEndPoint.Address.ToString() + ":" + conn.RemoteEndPoint.Port.ToString());
     Console.WriteLine();
 }
Exemple #4
0
        public static TcpState GetState(this TcpClient tcpClient)
        {
            if (tcpClient.Client.LocalEndPoint == null)
            {
                return(TcpState.Unknown);
            }

            try
            {
                var       endPoint = (IPEndPoint)tcpClient.Client.LocalEndPoint;
                IPAddress ipv4     = endPoint.Address;
                if (ipv4.IsIPv4MappedToIPv6)
                {
                    ipv4 = ipv4.MapToIPv4();
                }

                TcpConnectionInformation foo = IPGlobalProperties.GetIPGlobalProperties()
                                               .GetActiveTcpConnections()
                                               .SingleOrDefault(x => x.LocalEndPoint.Address.Equals(ipv4) && x.LocalEndPoint.Port == endPoint.Port);
                return(foo != null ? foo.State : TcpState.Unknown);
            }
            catch (ObjectDisposedException e)
            {
                return(TcpState.Closed);
            }
        }
Exemple #5
0
        internal TcpState GetTcpState()
        {
            TcpConnectionInformation conInfo = IPGlobalProperties.GetIPGlobalProperties()
                                               .GetActiveTcpConnections()
                                               .SingleOrDefault(con => con.LocalEndPoint.Equals(this.Client.Client.LocalEndPoint));

            return(conInfo?.State ?? TcpState.Unknown);
        }
Exemple #6
0
 public ConnectionItem(TcpConnectionInformation information)
 {
     Startpoint = information.LocalEndPoint.ToString();
     Endpoint   = information.RemoteEndPoint.ToString();
     getDescription(information);
     State       = information.State;
     DisplayName = FilterString = string.Concat(Startpoint, " - ", Endpoint);
 }
Exemple #7
0
        public static void newConnection(TcpConnectionInformation conn)
        {
            IPRecord ipR = new IPRecord();

            ipR.RemoteEndPoint = conn.RemoteEndPoint.ToString();
            GetInfo(ipR);
            ipList.Add(ipR);
        }
Exemple #8
0
        private TcpState GetState()
        {
            // Check our Tcp's state.
            TcpConnectionInformation info = IPGlobalProperties.GetIPGlobalProperties()
                                            .GetActiveTcpConnections()
                                            .SingleOrDefault(x => x.LocalEndPoint.Equals(this.tcp.Client.LocalEndPoint));

            return((info != null) ? info.State : TcpState.Unknown);
        }
 public NetConnection(TcpConnectionInformation tcp)
 {
     Protocol      = "TCP";
     RemoteAddress = tcp.RemoteEndPoint.Address.ToString();
     RemotePort    = tcp.RemoteEndPoint.Port;
     LocalAddress  = tcp.LocalEndPoint.Address.ToString();
     LocalPort     = tcp.LocalEndPoint.Port;
     State         = tcp.State.ToString();
 }
Exemple #10
0
        static private void Worker()
        {
            //TcpListener server = new TcpListener(IPAddress.Parse("127.0.0.1"), 59049);
            TcpListener server = new TcpListener(IPAddress.Any, 59049);

            try
            {
                server.Start();

                while (true)
                {
                    // Wait until New Connection
                    while (!server.Pending())
                    {
                        Thread.Sleep(2000);
                    }

                    // Clean up
                    lock (ResponseQueue) ResponseQueue.Clear();

                    using (TcpClient connection = server.AcceptTcpClient())
                        using (NetworkStream networkStream = new NetworkStream(connection.Client))
                        {
                            //Congestion Control
                            connection.ReceiveBufferSize = 256;

                            //Split a Seperate Thread for Reading
                            WorkerReadingStart(networkStream);

                            //Split a Seperate Thread for Writing
                            WorkerWritingStart(networkStream);

                            //Wait until Disconnected or New Connection
                            TcpState state;
                            do
                            {
                                Thread.Sleep(5000);
                                TcpConnectionInformation tcpinfo = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections().SingleOrDefault(x => (x.LocalEndPoint.Equals(connection.Client.LocalEndPoint) & x.RemoteEndPoint.Equals(connection.Client.RemoteEndPoint)));
                                state = tcpinfo != null ? tcpinfo.State : TcpState.Unknown;
                            } while (!server.Pending() && state != TcpState.Closed && state != TcpState.CloseWait && state != TcpState.Closing);

                            //Disconnect
                            WorkerWritingStop();
                            WorkerReadingStop();
                            networkStream.Close();
                            connection.Close();
                        }
                }
            }
            catch (Exception) //Including Thread Abort
            {
                WorkerWritingStop();
                WorkerReadingStop();
                server.Stop();
            }
        }
        private static TcpState GetState(TcpClient tcpClient)
        {
            TcpConnectionInformation foo = IPGlobalProperties.GetIPGlobalProperties()
                                           .GetActiveTcpConnections()
                                           .SingleOrDefault(x => x.LocalEndPoint.Equals(tcpClient.Client.LocalEndPoint) &&
                                                            x.RemoteEndPoint.Equals(tcpClient.Client.RemoteEndPoint)
                                                            );

            return(foo != null ? foo.State : TcpState.Unknown);
        }
        public static SortedList GetOpenTCPPorts()
        {
            SortedList OpenTCPPorts = new SortedList();

            IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();

            TcpConnectionInformation[] TCPconnections = properties.GetActiveTcpConnections();

            TcpConnectionInformation tcpConnectionInfo = default(TcpConnectionInformation);


            foreach (TcpConnectionInformation tempLoopVar_tcpConnectionInfo in TCPconnections)
            {
                tcpConnectionInfo = tempLoopVar_tcpConnectionInfo;
                if (!OpenTCPPorts.ContainsKey(tcpConnectionInfo.LocalEndPoint.Port))
                {
                    OpenTCPPorts.Add(tcpConnectionInfo.LocalEndPoint.Port, tcpConnectionInfo.LocalEndPoint.Port);
                }

                string localIP  = tcpConnectionInfo.LocalEndPoint.Address.ToString();
                string remoteIP = tcpConnectionInfo.RemoteEndPoint.Address.ToString();
                if (localIP == remoteIP)
                {
                    if (!OpenTCPPorts.ContainsKey(tcpConnectionInfo.RemoteEndPoint.Port))
                    {
                        OpenTCPPorts.Add(tcpConnectionInfo.RemoteEndPoint.Port, tcpConnectionInfo.RemoteEndPoint.Port);
                    }
                }
            }

            System.Net.IPEndPoint[] endPoints = null;

            endPoints = properties.GetActiveTcpListeners();

            foreach (var IPEndPoint in endPoints)
            {
                if (!OpenTCPPorts.ContainsKey(IPEndPoint.Port))
                {
                    OpenTCPPorts.Add(IPEndPoint.Port, IPEndPoint.Port);
                }
            }

            endPoints = properties.GetActiveUdpListeners();

            foreach (var IPEndPoint in endPoints)
            {
                if (!OpenTCPPorts.ContainsKey(IPEndPoint.Port))
                {
                    OpenTCPPorts.Add(IPEndPoint.Port, IPEndPoint.Port);
                }
            }

            return(OpenTCPPorts);
        }
Exemple #13
0
 public bool PlayerInList(TcpConnectionInformation info)
 {
     foreach (AccessListEntry e in accessList)
     {
         if (e.Matches(info.RemoteEndPoint.Address))
         {
             return(true);
         }
     }
     return(false);
 }
Exemple #14
0
        public Connection(TcpConnectionInformation info)
        {
            this.RemoteAddress = info.RemoteEndPoint.Address;
            this.LocalAddress  = info.LocalEndPoint.Address;
            this.LocalPort     = info.LocalEndPoint.Port;
            this.RemotePort    = info.RemoteEndPoint.Port;
            this.State         = info.State;
            double?a, b;

            GeoDatabase.GetPosition(RemoteAddress.ToString(), out a, out b);
            this.Longitude = a;
            this.Latitude  = b;
        }
Exemple #15
0
        private async void getDescription(TcpConnectionInformation information)
        {
            try
            {
                var entry = await Dns.GetHostEntryAsync(information.RemoteEndPoint.Address.ToString());

                Description = entry.HostName;
                DisplayName = FilterString = string.Concat(Startpoint, " - ", Endpoint, "(", Description, ")");
            }
            catch
            {
                Description = "";
            }
        }
        //List used tcp port
        static void ListAvailableTCPPort(ref ArrayList usedPort)
        {
            IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();

            TcpConnectionInformation[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections();
            IEnumerator myEnum = tcpConnInfoArray.GetEnumerator();

            while (myEnum.MoveNext())
            {
                TcpConnectionInformation TCPInfo = (TcpConnectionInformation)myEnum.Current;
                Console.WriteLine("Port {0} {1} {2} ", TCPInfo.LocalEndPoint, TCPInfo.RemoteEndPoint, TCPInfo.State);
                usedPort.Add(TCPInfo.LocalEndPoint.Port);
            }
        }
Exemple #17
0
        public static void ListNotAllowedPort(ref List <ExceptionPort> usedPort)
        {
            IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();

            TcpConnectionInformation[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections();
            IEnumerator myEnum = tcpConnInfoArray.GetEnumerator();

            while (myEnum.MoveNext())
            {
                TcpConnectionInformation TCPInfo = (TcpConnectionInformation)myEnum.Current;
                Console.WriteLine("Port {0} {1} {2} ", TCPInfo.LocalEndPoint, TCPInfo.RemoteEndPoint, TCPInfo.State);
                usedPort.Add(new ExceptionPort {
                    Port = TCPInfo.LocalEndPoint.Port
                });
            }
        }
        /// <summary>
        /// Returns the TcpConnectionInformation for a remote endpoint, or null if one
        /// could not be found.
        /// </summary>
        /// <param name="localEndPoint"></param>
        /// <param name="remoteEndPoint"></param>
        /// <returns></returns>
        private TcpConnectionInformation GetRemoteConnection(IPEndPoint localEndPoint, IPEndPoint remoteEndPoint)
        {
            TcpConnectionInformation result = null;

            if (_TcpConnections != null && _TcpConnections.Length > 0)
            {
                var localEndPointText  = localEndPoint.ToString();
                var remoteEndPointText = remoteEndPoint.ToString();
                result = _TcpConnections.FirstOrDefault(r =>
                                                        r.RemoteEndPoint != null && r.LocalEndPoint != null &&
                                                        r.RemoteEndPoint.ToString() == remoteEndPointText &&
                                                        r.LocalEndPoint.ToString() == localEndPointText
                                                        );
            }

            return(result);
        }
Exemple #19
0
        /// <summary>
        /// Gets the TCP endpoints list on localhost.
        /// </summary>
        /// <returns></returns>
        public static List <IPEndPoint> GetTcpEndpoints()
        {
            var tcpEndpoints = new List <IPEndPoint>();

            IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();

            TcpConnectionInformation[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections();
            IEnumerator myEnum = tcpConnInfoArray.GetEnumerator();

            while (myEnum.MoveNext())
            {
                TcpConnectionInformation tcpInfo = (TcpConnectionInformation)myEnum.Current;
                tcpEndpoints.Add(new IPEndPoint(IPAddress.Loopback, tcpInfo.LocalEndPoint.Port));
            }

            return(tcpEndpoints);
        }
        protected int RemotePortInUseCount(int port)
        {
            int num = 0;
            IPGlobalProperties iPGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();

            TcpConnectionInformation[] activeTcpConnections = iPGlobalProperties.GetActiveTcpConnections();
            TcpConnectionInformation[] array = activeTcpConnections;
            for (int i = 0; i < array.Length; i++)
            {
                TcpConnectionInformation tcpConnectionInformation = array[i];
                if (tcpConnectionInformation.get_RemoteEndPoint().get_Port() == port)
                {
                    num++;
                }
            }
            return(num);
        }
Exemple #21
0
        //function to check if the port sent is in use
        //returns false if the port is in use or else return true
        private bool PortAvailable(int port)
        {
            IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();

            TcpConnectionInformation[]     tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections();
            System.Collections.IEnumerator myEnum           = tcpConnInfoArray.GetEnumerator();
            while (myEnum.MoveNext())
            {
                TcpConnectionInformation tcpi = (TcpConnectionInformation)myEnum.Current;

                if (tcpi.LocalEndPoint.Port == port)
                {
                    return(false);
                }
            }

            return(true);
        }
Exemple #22
0
        public static bool IsPortAvailableTcp(int port)
        {
            IPGlobalProperties iPGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
            HashSet <int>      hashSet            = new HashSet <int>();

            IPEndPoint[] activeTcpListeners = iPGlobalProperties.GetActiveTcpListeners();
            for (int i = 0; i < activeTcpListeners.Length; i++)
            {
                IPEndPoint iPEndPoint = activeTcpListeners[i];
                hashSet.Add(iPEndPoint.Port);
            }
            TcpConnectionInformation[] activeTcpConnections = iPGlobalProperties.GetActiveTcpConnections();
            for (int j = 0; j < activeTcpConnections.Length; j++)
            {
                TcpConnectionInformation tcpConnectionInformation = activeTcpConnections[j];
                hashSet.Add(tcpConnectionInformation.LocalEndPoint.Port);
            }
            return(hashSet.Contains(port));
        }
Exemple #23
0
        public Boolean checkConnection()
        {
            // 연결 상태 체크 TcpClient형의 객체명 : client
            IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();

            TcpConnectionInformation[] tcpConnections = new TcpConnectionInformation[1];
            Boolean return_value = false;

            try
            {
                tcpConnections = ipProperties.GetActiveTcpConnections().Where(x => x.LocalEndPoint.Equals(client.LocalEndPoint) &&
                                                                              x.RemoteEndPoint.Equals(client.RemoteEndPoint)).ToArray();
            }
            catch
            {
                // Exception 처리 -> 여기서는 Disconnected된 것으로 보면 된다.
                return_value = false;
            }

            if (tcpConnections != null && tcpConnections.Length > 0)

            {
                TcpState stateOfConnection = tcpConnections.First().State;
                if (stateOfConnection == TcpState.Established)
                {
                    //Connected ~~
                    return_value = true;
                }
                else
                {
                    //Disconnected ~~
                    return_value = false;
                }
            }

            if (return_value == true && client.Connected == false)
            {
                return_value = false;
            }

            return(return_value);
        }
Exemple #24
0
        public TcpState GetState(TcpClient tcpClient)
        {
            //var foo = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections().SingleOrDefault(x => x.LocalEndPoint.Equals(tcpClient.Client.LocalEndPoint));
            //return foo != null ? foo.State : TcpState.Unknown;

            try
            {
                TcpConnectionInformation tcpConnectionInformation = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections().SingleOrDefault(x => x.LocalEndPoint.Equals(tcpClient.Client.LocalEndPoint) && x.RemoteEndPoint.Equals(tcpClient.Client.RemoteEndPoint));

                return(tcpConnectionInformation != null ? tcpConnectionInformation.State : TcpState.Unknown);

                //TcpConnectionInformation[] tcpConnectionInformation = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections().(x => x.LocalEndPoint.Equals(tcpClient.Client.LocalEndPoint) && x.RemoteEndPoint.Equals(tcpClient.Client.RemoteEndPoint)).ToArray();
                //
                //if (tcpConnectionInformation != null && tcpConnectionInformation.Length > 0)
                //{
                //    TcpState tcpState = tcpConnectionInformation.First().State;
                //
                //    return tcpState;
                //}
                //else
                //    return TcpState.Unknown;
            }
            catch (InvalidOperationException Ie)
            {
                //share.EventLog(string.Format("{0} (0) > {1}", MethodBase.GetCurrentMethod().Name, Ie.Message), false);
                share.EventLog(null, string.Empty, string.Format("\t{0} (0) > {1}", MethodBase.GetCurrentMethod().Name, Ie.Message), false);

                return(TcpState.Unknown);
            }
            catch (Exception ex)
            {
                //share.EventLog(string.Format("{0} (1) > {1}", MethodBase.GetCurrentMethod().Name, ex.Message), false);
                share.EventLog(null, string.Empty, string.Format("\t{0} (1) > {1}", MethodBase.GetCurrentMethod().Name, ex.Message), false);

                return(TcpState.Unknown);
            }
        }
Exemple #25
0
        public void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            string con;

            if (e.ProgressPercentage == 1)
            {
                button1.Enabled = true;
            }

            if (checkBox1.Checked == true)
            {
                try
                {
                    transfer tr = (transfer)e.UserState;
                    con           = tr.SourceDns + "     " + tr.DestDns + " " + tr.state + "\r\n";
                    textBox1.Text = textBox1.Text + con;
                }
                catch
                {
                    textBox1.Text = " ";
                }
            }
            else
            {
                try
                {
                    TcpConnectionInformation tcp = (TcpConnectionInformation)e.UserState;
                    con           = tcp.LocalEndPoint + " : " + "  " + tcp.RemoteEndPoint + "     " + tcp.State + "\r\n";
                    textBox1.Text = textBox1.Text + con;
                }
                catch
                {
                    textBox1.Text = " ";
                }
            }
        }
Exemple #26
0
        private static int[] GetPortFree()
        {
            //int port = 0;

            List <int> ls_port_ok = new List <int>()
            {
            };

            for (int k = 3000; k < 65000; k++)
            {
                ls_port_ok.Add(k);
            }

            List <int> ls_port = new List <int>()
            {
            };

            IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();

            TcpConnectionInformation[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections();
            IEnumerator myEnum = tcpConnInfoArray.GetEnumerator();

            while (myEnum.MoveNext())
            {
                TcpConnectionInformation TCPInfo = (TcpConnectionInformation)myEnum.Current;
                //Console.WriteLine("Port {0} {1} {2} ", TCPInfo.LocalEndPoint, TCPInfo.RemoteEndPoint, TCPInfo.State);
                ls_port.Add(TCPInfo.LocalEndPoint.Port);
            }

            ls_port    = ls_port.OrderBy(x => x).ToList();
            ls_port_ok = ls_port_ok.Where(p => !ls_port.Any(x => x == p)).ToList();

            //if (ls_port_ok.Count > 0) port = ls_port_ok[0];

            return(ls_port_ok.ToArray());
        }
 private static void ValidateInfo(TcpConnectionInformation tcpConnectionInformation, IPEndPoint localEP, IPEndPoint remoteEP, TcpState state)
 {
     Assert.Equal(localEP, tcpConnectionInformation.LocalEndPoint);
     Assert.Equal(remoteEP, tcpConnectionInformation.RemoteEndPoint);
     Assert.Equal(state, tcpConnectionInformation.State);
 }
Exemple #28
0
        public void Enforce()
        {
            if (AccessList.Count > 0)
            {
                IPGlobalProperties         ipGlobalProperties    = IPGlobalProperties.GetIPGlobalProperties();
                TcpConnectionInformation[] connectionInformation = ipGlobalProperties.GetActiveTcpConnections();

                IEnumerator enumerator = connectionInformation.GetEnumerator();

                Logging.OnLogMessage(String.Format("Enforcing {0}", Mode), MessageType.Info);

                while (enumerator.MoveNext())
                {
                    TcpConnectionInformation info = (TcpConnectionInformation)enumerator.Current;

                    if (info.LocalEndPoint.Port == Settings.Instance.Port && info.State == TcpState.Established)
                    {
                        int  ret         = 0;
                        bool actionTaken = false;
                        bool playerFound = PlayerInList(info);

                        switch (Mode)
                        {
                        case AccessMode.Whitelist:

                            if (!playerFound)
                            {
                                ret         = Helper.DisconnectWrapper.CloseRemoteIP(info.RemoteEndPoint.Address.ToString(), info.RemoteEndPoint.Port);
                                actionTaken = true;
                            }

                            break;

                        case AccessMode.Blacklist:

                            if (playerFound)
                            {
                                ret         = Helper.DisconnectWrapper.CloseRemoteIP(info.RemoteEndPoint.Address.ToString(), info.RemoteEndPoint.Port);
                                actionTaken = true;
                            }

                            break;
                        }

                        if (actionTaken)
                        {
                            switch (ret)
                            {
                            case 0:
                                Logging.OnLogMessage(String.Format("The user {0} has been kicked", info.RemoteEndPoint.Address.ToString()), MessageType.Info);
                                break;

                            case 317:
                                Logging.OnLogMessage(String.Format("Could not kick user {0}: Access denied", info.RemoteEndPoint.Address.ToString()), MessageType.Warning);
                                break;

                            case 0x5:
                                Logging.OnLogMessage(String.Format("Could not kick user {0}. You might lack the the required privileges.", info.RemoteEndPoint.Address.ToString()), MessageType.Warning);
                                break;

                            case 0x57:
                                Logging.OnLogMessage("Internal error: wrong parameter", MessageType.Error);
                                break;

                            case 0x32:
                                Logging.OnLogMessage("IPv4 Transport is not configured properly", MessageType.Error);
                                break;

                            default:
                                Logging.OnLogMessage(String.Format("Disconnecting returned with {0}", ret), MessageType.Info);
                                break;
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Updates the current players
        /// </summary>
        /// <returns></returns>
        public void UpdatePlayers()
        {
            IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();

            TcpConnectionInformation[] connectionInformation = ipGlobalProperties.GetActiveTcpConnections();

            IEnumerator enumerator = connectionInformation.GetEnumerator();

            int count = 0;

            ObservableCollection <Helper.PlayerInfo> current = new ObservableCollection <Helper.PlayerInfo>();
            ObservableCollection <Helper.PlayerInfo> all     = new ObservableCollection <Helper.PlayerInfo>(Players);

            while (enumerator.MoveNext())
            {
                TcpConnectionInformation info = (TcpConnectionInformation)enumerator.Current;

                if (info.LocalEndPoint.Port == 12345 && info.State == TcpState.Established)
                {
                    count++;

                    if (!all.Contains(info.RemoteEndPoint.Address))
                    {
                        all.Add(info.RemoteEndPoint.Address);
                    }

                    current.Add(info.RemoteEndPoint.Address);
                }
            }

#if DEBUG
            //Time for some debug data
            Random rnd = new Random();
            int    playersToGenerate = rnd.Next(1, 10);

            for (int i = 0; i < playersToGenerate; i++)
            {
                int a = rnd.Next(0, 255);


                IPAddress tmp;
                string    ip = "127.0.0." + a.ToString();

                if (IPAddress.TryParse(ip, out tmp))
                {
                    if (!current.Contains(tmp))
                    {
                        current.Add(tmp);
                    }

                    if (!all.Contains(tmp))
                    {
                        all.Add(tmp);
                    }
                }
            }

            current.Add(IPAddress.Parse("192.168.178.1"));
#endif

            ConnectedPlayers = current;
            Players          = all;
        }
 private static void ValidateInfo(TcpConnectionInformation tcpConnectionInformation, IPEndPoint localEP, IPEndPoint remoteEP, TcpState state)
 {
     Assert.Equal(localEP, tcpConnectionInformation.LocalEndPoint);
     Assert.Equal(remoteEP, tcpConnectionInformation.RemoteEndPoint);
     Assert.Equal(state, tcpConnectionInformation.State);
 }
        public static SortedList GetOpenTCPPortsInARangeOfPorts(int lowPortNumber, int uppperPortNumber)
        {
            int lowerLimit = 0;
            int upperLimit = 0;

            if (lowPortNumber <= uppperPortNumber)
            {
                lowerLimit = lowPortNumber;
                upperLimit = uppperPortNumber;
            }
            else
            {
                lowerLimit = uppperPortNumber;
                upperLimit = lowPortNumber;
            }

            SortedList OpenTCPPorts = new SortedList();

            IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();

            TcpConnectionInformation[] TCPconnections = properties.GetActiveTcpConnections();

            TcpConnectionInformation tcpConnectionInfo = default(TcpConnectionInformation);

            foreach (TcpConnectionInformation tempLoopVar_tcpConnectionInfo in TCPconnections)
            {
                tcpConnectionInfo = tempLoopVar_tcpConnectionInfo;

                if (tcpConnectionInfo.LocalEndPoint.Port >= lowerLimit & tcpConnectionInfo.LocalEndPoint.Port <= upperLimit)
                {
                    if (!OpenTCPPorts.ContainsKey(tcpConnectionInfo.LocalEndPoint.Port))
                    {
                        OpenTCPPorts.Add(tcpConnectionInfo.LocalEndPoint.Port, tcpConnectionInfo.LocalEndPoint.Port);
                    }
                }
            }

            System.Net.IPEndPoint[] endPoints = null;

            endPoints = properties.GetActiveTcpListeners();
            System.Net.IPEndPoint ep = default(System.Net.IPEndPoint);
            foreach (System.Net.IPEndPoint tempLoopVar_ep in endPoints)
            {
                ep = tempLoopVar_ep;
                if (ep.Port >= lowerLimit & ep.Port <= upperLimit)
                {
                    if (!OpenTCPPorts.ContainsKey(ep.Port))
                    {
                        OpenTCPPorts.Add(ep.Port, ep.Port);
                    }
                }
            }

            endPoints = properties.GetActiveUdpListeners();
            foreach (System.Net.IPEndPoint tempLoopVar_ep in endPoints)
            {
                ep = tempLoopVar_ep;
                if (ep.Port >= lowerLimit & ep.Port <= upperLimit)
                {
                    if (!OpenTCPPorts.ContainsKey(ep.Port))
                    {
                        OpenTCPPorts.Add(ep.Port, ep.Port);
                    }
                }
            }
            return(OpenTCPPorts);
        }