/// <summary>
        /// Constructor
        /// </summary>
        /// <param name="config">The config, contains information about transport type, tcp listening port and so on</param>
        public Smb2Server(Smb2ServerConfig config)
        {
            decoder = new Smb2Decoder(Smb2Role.Server);
            decoder.TransportType = config.TransportType;

            clientEndpoints = new List<Smb2Endpoint>();
            context = new Smb2ServerContext();
            context.transportType = config.TransportType;
            context.requireMessageSigning = config.RequireMessageSigning;
            context.isDfsCapable = config.IsDfsCapable;

            transportType = config.TransportType;

            if (transportType == Smb2TransportType.NetBios)
            {
                NetbiosTransportConfig netbiosConfig = new NetbiosTransportConfig();
                netbiosConfig.BufferSize = Smb2Consts.MaxNetbiosBufferSize;

                netbiosConfig.LocalNetbiosName = config.LocalNetbiosName;
                netbiosConfig.MaxNames = Smb2Consts.MaxNames;
                netbiosConfig.MaxSessions = Smb2Consts.MaxSessions;
                netbiosConfig.Type = StackTransportType.Netbios;
                netbiosConfig.Role = Role.Server;

                transport = new TransportStack(netbiosConfig, decoder.Smb2DecodePacketCallback);
            }
            else if (transportType == Smb2TransportType.Tcp)
            {
                SocketTransportConfig socketConfig = new SocketTransportConfig();

                socketConfig.BufferSize = Smb2Consts.MaxNetbiosBufferSize;
                socketConfig.MaxConnections = Smb2Consts.MaxConnectionNumer;
                socketConfig.LocalIpAddress = IPAddress.Any;
                socketConfig.LocalIpPort = config.ServerTcpListeningPort;
                socketConfig.Role = Role.Server;
                socketConfig.Type = StackTransportType.Tcp;

                transport = new TransportStack(socketConfig, decoder.Smb2DecodePacketCallback);
            }
            else
            {
                throw new ArgumentException("config contains invalid transport type", "config");
            }
        }
Example #2
0
        /// <summary>
        /// to update the config of transport at runtime.
        /// </summary>
        /// <param name="config">
        /// a TransportConfig object that contains the config to update
        /// </param>
        /// <exception cref="ArgumentException">
        /// thrown when transportConfig is not NetbiosTransportConfig
        /// </exception>
        /// <exception cref="ObjectDisposedException">
        /// thrown when this object is disposed.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// thrown when config is null.
        /// </exception>
        public void UpdateConfig(TransportConfig config)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("NetbiosClientTransport");
            }

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

            NetbiosTransportConfig netbiosTransportConfig = config as NetbiosTransportConfig;

            if (netbiosTransportConfig == null)
            {
                throw new ArgumentException("config must be NetbiosTransportConfig", "config");
            }

            this.netbiosConfig = netbiosTransportConfig;
        }
        /// <summary>
        /// to set up Netbios session with server, and add the connection into context.
        /// </summary>
        /// <param name="server">the server NetBios Name.</param>
        /// <param name="client">the local NetBios Name.</param>
        /// <param name="bufferSize">the size of buffer used for receiving data.</param>
        /// <param name="maxSessions">the max sessions supported by the transport.</param>
        /// <param name="maxNames">
        /// the max Netbios names used to initialize the NCB. It is only used in NetBios transport.
        /// </param>
        /// <returns>the Identity of the connection. if connected, is the session number 
        /// of the Netbios session; otherwise -1.</returns>
        /// <exception cref="System.ArgumentNullException">the server and client must not be null.</exception>
        public virtual void Connect(string server, string client, int bufferSize, int maxSessions, int maxNames)
        {
            if (server == null)
            {
                throw new ArgumentNullException("server");
            }

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

            NetbiosTransportConfig transportConfig = new NetbiosTransportConfig();
            transportConfig.Type = StackTransportType.Netbios;
            transportConfig.Role = Role.Client;
            transportConfig.BufferSize = bufferSize;
            transportConfig.MaxSessions = maxSessions;
            transportConfig.MaxNames = maxNames;
            transportConfig.RemoteNetbiosName = server;
            transportConfig.LocalNetbiosName = client + new Random().Next();

            this.transport = new TransportStack(transportConfig, new SmbClientDecodePacket(this).DecodePacket);

            int connectionId = (int)this.transport.Connect();
            this.ConnectionId = connectionId;

            SmbClientConnection connection = new SmbClientConnection();
            connection.ConnectionId = connectionId;
            connection.ConnectionState = StackTransportState.ConnectionEstablished;
            connection.ServerNetbiosName = server;
            connection.ClientNetbiosName = client;
            this.Context.AddOrUpdateConnection(connection);

            // set the transport type
            this.capability.TransportType = TransportType.NetBIOS;
        }
        public virtual void Start(
            string localNetbiosName, int adapterIndex, int bufferSize,
            int maxSessions, int maxNames, AccountCredential credential)
        {
            if (localNetbiosName == null)
            {
                throw new ArgumentNullException("localNetbiosName");
            }

            NetbiosTransportConfig config = new NetbiosTransportConfig();
            config.Type = StackTransportType.Netbios;
            config.Role = Role.Server;
            config.AdapterIndex = (byte)adapterIndex;
            config.BufferSize = bufferSize;
            config.MaxSessions = maxSessions;
            config.MaxNames = maxNames;
            config.LocalNetbiosName = localNetbiosName;

            SmbServerDecodePacket decoder = new SmbServerDecodePacket();
            decoder.Context = this.context;

            this.transport = new TransportStack(config, decoder.DecodePacket);
            this.transport.Start();

            this.transportType = TransportType.NetBIOS;
        }
        /// <summary>
        /// to set up Netbios session with server, and add the connection into context.
        /// </summary>
        /// <param name="server">the server NetBios Name.</param>
        /// <param name="client">the local NetBios Name.</param>
        /// <returns>the Identity of the connection. if connected, is the session number 
        /// of the Netbios session; otherwise -1.</returns>
        /// <exception cref="System.ArgumentNullException">the server and client must not be null.</exception>
        /// <exception cref="System.InvalidOperationException"> failed to connect for Netbios error. </exception>
        public int Connect(string server, string client)
        {
            if (server == null)
            {
                throw new ArgumentNullException("server");
            }

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

            NetbiosTransportConfig transportConfig = new NetbiosTransportConfig();
            transportConfig.Type = StackTransportType.Netbios;
            transportConfig.Role = Role.Client;
            transportConfig.BufferSize = this.ptfConfig.NcbBufferSize;
            transportConfig.MaxSessions = this.ptfConfig.NcbMaxSessions;
            transportConfig.MaxNames = this.ptfConfig.NcbMaxNames;
            transportConfig.RemoteNetbiosName = server;
            transportConfig.LocalNetbiosName = client;

            this.transport = new TransportStack(transportConfig, this.decoder.DecodePacket);

            this.connectionId = (int)this.transport.Connect();

            CifsClientPerConnection connection = new CifsClientPerConnection();
            connection.ConnectionId = this.connectionId;
            connection.ConnectionState = StackTransportState.ConnectionEstablished;
            connection.ServerNetbiosName = server;
            connection.ClientNetbiosName = client;
            this.context.AddOrUpdateConnection(connection);

            return this.connectionId;
        }
        /// <summary>
        /// to update the config of transport at runtime.
        /// </summary>
        /// <param name="config">
        /// a TransportConfig object that contains the config to update
        /// </param>
        /// <exception cref="ObjectDisposedException">
        /// thrown when this object is disposed.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// thrown when transportConfig is not NetbiosTransportConfig.
        /// </exception>
        public void UpdateConfig(TransportConfig config)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("NetbiosServerTransport");
            }

            NetbiosTransportConfig netbiosTransportConfig = config as NetbiosTransportConfig;

            if (netbiosTransportConfig == null)
            {
                throw new ArgumentException("transportConfig must be NetbiosTransportConfig", "config");
            }

            this.netbiosConfig = netbiosTransportConfig;
        }