Exemple #1
0
        /// <summary>
        /// Release resources.
        /// </summary>
        /// <param name = "disposing">
        /// If disposing equals true, Managed and unmanaged resources are disposed. if false, Only unmanaged resources
        /// can be disposed.
        /// </param>
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                // If disposing equals true, dispose all managed and unmanaged resources.
                if (disposing)
                {
                    // Free managed resources & other reference types:
                    if (this.transportStack != null)
                    {
                        this.transportStack.Dispose();
                        this.transportStack = null;
                    }
                    if (this.contextManager != null)
                    {
                        this.contextManager.Dispose();
                        this.contextManager = null;
                    }
                    this.encoderv2 = null;
                    this.encoderv3 = null;
                    this.decoder   = null;
                }

                // Call the appropriate methods to clean up unmanaged resources.
                // If disposing is false, only the following code is executed:

                this.disposed = true;
            }
        }
        /// <summary>
        /// Set up the TCP/UDP transport connection with KDC.
        /// </summary>
        /// <param name="localPort">The server port</param>
        /// <param name="transportType">Whether the transport is TCP or UDP transport.</param>
        /// <param name="ipType">Ip Version</param>
        /// <param name="transportSize">The buffer size of transport stack. </param>
        /// <exception cref="System.ArgumentException">Thrown when the transportType is neither TCP nor UDP.</exception>
        public void Start(ushort localPort, KileConnectionType transportType, KileIpType ipType, int transportSize)
        {
            SocketTransportConfig transportConfig = new SocketTransportConfig();

            transportConfig.Role           = Role.Server;
            transportConfig.MaxConnections = ConstValue.MAX_CONNECTIONS;
            transportConfig.BufferSize     = transportSize;

            if (ipType == KileIpType.Ipv4)
            {
                transportConfig.LocalIpAddress = IPAddress.Any;
            }
            else
            {
                transportConfig.LocalIpAddress = IPAddress.IPv6Any;
            }
            transportConfig.LocalIpPort = localPort;

            if (transportType == KileConnectionType.TCP)
            {
                transportConfig.Type = StackTransportType.Tcp;
            }
            else if (transportType == KileConnectionType.UDP)
            {
                transportConfig.Type = StackTransportType.Udp;
            }
            else
            {
                throw new ArgumentException("ConnectionType can only be TCP or UDP.");
            }
            decoder   = new KileDecoder(contextList, transportType);
            transport = new TransportStack(transportConfig, decoder.DecodePacketCallback);
            transport.Start();
        }
Exemple #3
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="localEp"></param>
        public RdpeudpServer(IPEndPoint localEp, bool autoHandle = true)
        {
            this.localEndPoint           = localEp;
            this.AutoHandle              = autoHandle;
            this.unprocessedPacketBuffer = new List <StackPacketInfo>();
            this.serverSocketDic         = new Dictionary <IPEndPoint, RdpeudpServerSocket>();

            UdpServerConfig config = new UdpServerConfig(localEp);

            config.Role  = Transport.Role.Server;
            udpTransport = new TransportStack(config, RdpeudpBasePacket.DecodePacketCallback);
        }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="localEp"></param>
        public RdpeudpClient(IPEndPoint localEp, IPEndPoint remoteEp, TransportMode mode, bool autoHandle = true)
        {
            this.localEndPoint  = localEp;
            this.remoteEndpoint = remoteEp;
            this.AutoHandle     = autoHandle;
            this.transMode      = mode;

            UdpClientConfig config = new UdpClientConfig(localEp.Port, remoteEp);

            udpTransport = new TransportStack(config, RdpeudpBasePacket.DecodePacketCallback);

            socket = new RdpeudpClientSocket(transMode, remoteEp, autoHandle, packetsender);
        }
Exemple #5
0
 /// <summary>
 /// Stops the server.
 /// </summary>
 public void Stop()
 {
     if (!this.disposed)
     {
         if (this.transportStack != null)
         {
             this.transportStack.Dispose();
             this.transportStack = null;
         }
         if (this.contextManager != null)
         {
             this.contextManager.Clear();
         }
     }
 }
Exemple #6
0
        /// <summary>
        /// Starts the server to listen on specified port.
        /// </summary>
        public void Start()
        {
            if (transportStack == null)
            {
                SocketTransportConfig transportConfig = new SocketTransportConfig();
                transportConfig.LocalIpAddress = IPAddress.Any;
                transportConfig.LocalIpPort    = this.listenPort;
                transportConfig.Role           = Role.Server;
                transportConfig.BufferSize     = DefaultBufferSize;
                transportConfig.MaxConnections = int.MaxValue;
                transportConfig.Type           = isTcp ? StackTransportType.Tcp : StackTransportType.Udp;

                this.transportStack = new TransportStack(transportConfig, decoder.DecodeLdapPacketCallBack);
            }

            this.transportStack.Start();
        }
        /// <summary>
        /// Set up the TCP/UDP transport connection with KDC.
        /// </summary>
        /// <param name="kdcAddress">The IP address of the KDC.</param>
        /// <param name="kdcPort">The port of the KDC.</param>
        /// <param name="transportType">Whether the transport is TCP or UDP transport.</param>
        /// <exception cref="System.ArgumentException">Thrown when the connection type is neither TCP nor UDP</exception>
        public void Connect(string kdcAddress, int kdcPort, KileConnectionType transportType)
        {
            SocketTransportConfig transportConfig = new SocketTransportConfig();

            transportConfig.Role            = Role.Client;
            transportConfig.MaxConnections  = 1;
            transportConfig.BufferSize      = transportBufferSize;
            transportConfig.RemoteIpPort    = kdcPort;
            transportConfig.RemoteIpAddress = IPAddress.Parse(kdcAddress);

            // For UDP bind
            if (transportConfig.RemoteIpAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
            {
                transportConfig.LocalIpAddress = IPAddress.Any;
            }
            else if (transportConfig.RemoteIpAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
            {
                transportConfig.LocalIpAddress = IPAddress.IPv6Any;
            }

            if (transportType == KileConnectionType.TCP)
            {
                transportConfig.Type = StackTransportType.Tcp;
            }
            else if (transportType == KileConnectionType.UDP)
            {
                transportConfig.Type = StackTransportType.Udp;
            }
            else
            {
                throw new ArgumentException("ConnectionType can only be TCP or UDP.");
            }

            kdcTransport = new TransportStack(transportConfig, decoder.DecodePacketCallback);
            if (transportType == KileConnectionType.TCP)
            {
                kdcTransport.Connect();
            }
            else
            {
                kdcTransport.Start();
            }

            context.TransportType = transportType;
        }
        /// <summary>
        /// Release resources.
        /// </summary>
        /// <param name="disposing">If disposing equals true, Managed and unmanaged resources are disposed.
        /// if false, Only unmanaged resources can be disposed.</param>
        protected override void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    //Release managed resource.
                    if (transport != null)
                    {
                        transport.Dispose();
                        transport = null;
                    }
                }

                //Note disposing has been done.
                disposed = true;
            }
        }
        /// <summary>
        /// Set up the TCP/UDP transport connection with KDC.
        /// </summary>
        /// <exception cref="System.ArgumentException">Thrown when the connection type is neither TCP nor UDP</exception>
        public virtual void Connect()
        {
            SocketTransportConfig transportConfig = new SocketTransportConfig();

            transportConfig.Role            = Role.Client;
            transportConfig.MaxConnections  = 1;
            transportConfig.BufferSize      = KerberosConstValue.TRANSPORT_BUFFER_SIZE;
            transportConfig.RemoteIpPort    = kdcPort;
            transportConfig.RemoteIpAddress = IPAddress.Parse(kdcAddress);

            // For UDP bind
            if (transportConfig.RemoteIpAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
            {
                transportConfig.LocalIpAddress = IPAddress.Any;
            }
            else if (transportConfig.RemoteIpAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
            {
                transportConfig.LocalIpAddress = IPAddress.IPv6Any;
            }

            if (transportType == TransportType.TCP)
            {
                transportConfig.Type = StackTransportType.Tcp;
            }
            else if (transportType == TransportType.UDP)
            {
                transportConfig.Type = StackTransportType.Udp;
            }
            else
            {
                throw new ArgumentException("ConnectionType can only be TCP or UDP.");
            }

            kdcTransport = new TransportStack(transportConfig, DecodePacketCallback);
            if (transportType == TransportType.TCP)
            {
                kdcTransport.Connect();
            }
            else
            {
                kdcTransport.Start();
            }
        }
        /// <summary>
        /// Initialize a new RdpeudpClient instance.
        /// </summary>
        /// <param name="localEp">Local endpoint.</param>
        /// <param name="remoteEp">Remote endpoint.</param>
        /// <param name="mode">Transport mode.</param>
        /// <param name="autoHandle">Auto handle transport.</param>
        /// <param name="securityCookie">The securityCookie field of the Initiate Multitransport Request PDU</param>
        public RdpeudpClient(IPEndPoint localEp, IPEndPoint remoteEp, TransportMode mode, bool autoHandle = true, byte[] securityCookie = null)
        {
            this.localEndPoint  = localEp;
            this.remoteEndPoint = remoteEp;
            this.AutoHandle     = autoHandle;
            this.transMode      = mode;

            UdpClientConfig config = new UdpClientConfig(localEp.Port, remoteEp);

            udpTransport = new TransportStack(config, RdpeudpBasePacket.DecodePacketCallback);

            if (securityCookie == null)
            {
                cookieHash = new byte[32];
            }
            else
            {
                cookieHash = SHA256.HashData(securityCookie);
            }

            socket = new RdpeudpClientSocket(transMode, remoteEp, autoHandle, PacketSender, cookieHash);
        }
        /// <summary>
        /// Connects to server.
        /// </summary>
        /// <param name="config">Transport configurations.</param>
        /// <exception cref="InvalidOperationException">
        /// thrown when TransportConifg is null!
        /// </exception>
        /// <exception cref="NotSupportedException">
        /// thrown when TransportConfig is not SocketTransportConfig,
        /// ADTS-LDAP supports socket transport only!
        /// </exception>
        /// <exception cref="NotSupportedException">
        /// thrown when Type of TransportConfig is not TCP/UDP.
        /// Only TCP and UDP are supported for StackTransportType
        /// </exception>
        public virtual void Connect()
        {
            if (this.config == null)
            {
                throw new InvalidOperationException("TransportConfig is null!");
            }

            SocketTransportConfig socketConfig = this.config as SocketTransportConfig;

            if (socketConfig == null)
            {
                throw new NotSupportedException("ADTS-LDAP supports socket transport only!");
            }

            // initialize IsTcp.
            if (socketConfig.Type == StackTransportType.Tcp)
            {
                this.isTcp = true;
            }
            else if (socketConfig.Type == StackTransportType.Udp)
            {
                this.isTcp = false;
            }
            else
            {
                throw new NotSupportedException("Only TCP and UDP are supported for StackTransportType");
            }

            // initialize context.
            if (this.context == null)
            {
                IPEndPoint remoteAddress = new IPEndPoint(socketConfig.RemoteIpAddress, socketConfig.RemoteIpPort);
                this.context = new AdtsLdapContext(ldapVersion, remoteAddress);
            }

            // initialize decorder.
            if (this.decoder == null)
            {
                this.decoder = new AdtsLdapClientDecoder(this);
            }

            // initialize transport stack.
            if (this.transportStack == null)
            {
                this.transportStack = new TransportStack(config, this.decoder.DecodeLdapPacketCallBack);
            }

            #region Transport Connect

            // TCP and UDP differs here. Connect method cannot be used for UDP connections.
            if (this.isTcp)
            {
                this.transportStack.Connect();
            }
            else
            {
                this.transportStack.Start();
            }

            #endregion
        }