/// <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); }
/// <summary> /// Constructor /// </summary> public CifsServer(NetbiosServerConfig config) { this.context = new CifsServerContext(); if (config == null) { throw new ArgumentNullException("config"); } if (config.LocalNetbiosName == null || config.LocalNetbiosName.Length == 0) { config.LocalNetbiosName = this.context.ServerName; } this.context.ServerName = config.LocalNetbiosName; this.context.MaxBufferSize = config.BufferSize; CifsServerDecodePacket decoder = new CifsServerDecodePacket(this.Context); this.transport = new TransportStack(config, decoder.DecodePacketCallback); }
/// <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"); } }
/// <summary> /// to set up the tcp connection, and add the connection into context. Exception will be thrown if failed to /// set up connection with server. /// </summary> /// <param name = "serverName">the server name or server ip address to connect to </param> /// <param name = "serverPort">the port of server to connect to </param> /// <param name = "ipVersion">the ipversion to connect to server </param> /// <param name = "bufferSize">the buffer size of transport </param> /// <exception cref="InvalidOperationException"> /// Failed to get the IP address of SMB server in SmbClient(). /// </exception> public virtual void Connect(string serverName, int serverPort, IpVersion ipVersion, int bufferSize) { // initialize the config for transport SocketTransportConfig config = new SocketTransportConfig(); config.Role = Role.Client; config.Type = StackTransportType.Tcp; config.BufferSize = bufferSize; // init remote address of config #region Lookup the ip address from server name. IPHostEntry ipHostEntry = Dns.GetHostEntry(serverName); if (ipHostEntry != null) { foreach (IPAddress address in ipHostEntry.AddressList) { if (ipVersion != IpVersion.Ipv4 && address.AddressFamily == AddressFamily.InterNetworkV6) { config.LocalIpAddress = IPAddress.IPv6Any; config.RemoteIpAddress = address; break; } else if (ipVersion != IpVersion.Ipv6 && address.AddressFamily == AddressFamily.InterNetwork) { config.LocalIpAddress = IPAddress.Any; config.RemoteIpAddress = address; break; } else { continue; } } } if (config.RemoteIpAddress == null) { throw new InvalidOperationException("Failed to get the IP address of SMB server in SmbClient()."); } #endregion // init remote port config.RemoteIpPort = serverPort; // init local address of config config.LocalIpAddress = IPAddress.Any; config.LocalIpPort = 0; // init transport this.transport = new TransportStack(config, new SmbClientDecodePacket(this).DecodePacket); // connect to server. object endPointIdentity = this.transport.Connect(); // initialize the connection SmbClientConnection connection = new SmbClientConnection(); connection.ConnectionId = this.Context.GetConnectionID(endPointIdentity as IPEndPoint); connection.ConnectionState = StackTransportState.ConnectionEstablished; connection.MaxBufferSize = (uint)bufferSize; // update the context this.cifsClient.Context.AddOrUpdateConnection(connection); // update the connection id, to identity the connection instance. this.ConnectionId = connection.ConnectionId; // set the transport type this.capability.TransportType = TransportType.TCP; }
/// <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.transport != null) { this.transport.Dispose(); this.transport = null; } if (this.cifsClient != null) { // dispose the gssapi of connection if (this.Context != null && this.Context.Connection != null) { this.Context.Connection.DisposeGssApi(); } this.cifsClient.Dispose(); this.cifsClient = null; } } // Call the appropriate methods to clean up unmanaged resources. // If disposing is false, only the following code is executed: this.disposed = true; } }
/// <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; }
/// <summary> /// to start the smbserver, tcp transport. /// </summary> /// <param name="serverAddress">the address of server</param> /// <param name="localPort">the local port to bind for server</param> /// <param name="maxConnections">the max connections of server capability</param> /// <param name="bufferSize">the buffer size of transport </param> /// <param name="accountCredential">the credential to authenticate client, spn is always cifs/machine</param> public virtual void Start(IPAddress serverAddress, int localPort, int maxConnections, int bufferSize, AccountCredential accountCredential) { this.credential = accountCredential; SocketTransportConfig config = new SocketTransportConfig(); config.Type = StackTransportType.Tcp; config.Role = Role.Server; config.LocalIpAddress = serverAddress; config.LocalIpPort = localPort; config.MaxConnections = maxConnections; config.BufferSize = bufferSize; SmbServerDecodePacket decoder = new SmbServerDecodePacket(); decoder.Context = this.context; this.transport = new TransportStack(config, decoder.DecodePacket); this.transport.Start(); this.transportType = TransportType.TCP; }
/// <summary> /// Close Server, release all resource. /// </summary> /// <exception cref="InvalidOperationException"> /// The transport is null for not started. Please invoke Start() first /// </exception> public virtual void Close() { if (this.transport == null) { throw new InvalidOperationException( "The transport is null for not started. Please invoke Start() first."); } this.transport.Dispose(); this.transport = null; }
/// <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; } }
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> /// Close Server, release all resource. /// </summary> /// <exception cref="InvalidOperationException"> /// The transport is not started. Please invoke Start() first /// </exception> public virtual void Close() { this.transport.Dispose(); this.transport = null; }
/// <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(); } } }
public virtual RpceServerContext StartTcp(ushort port) { lock (this.tcpThreadLocker) { RpceServerContext serverContext = this.serverContextManager.LookupServerContext( RpceUtility.RPC_OVER_TCPIP_PROTOCOL_SEQUENCE, port.ToString()); if (serverContext != null) { throw new InvalidOperationException("The server with the port has been started. Please try other port."); } serverContext = new RpceServerContext( RpceUtility.RPC_OVER_TCPIP_PROTOCOL_SEQUENCE, port.ToString()); this.serverContextManager.AddServerContext(serverContext); if (this.openedTcpPortList == null) { this.openedTcpPortList = new List<ushort>(); } this.openedTcpPortList.Add(port); bool ipv4Started = false; bool ipv6Started = false; Exception ex = null; try { if (this.tcpTransport == null) { SocketTransportConfig config = new SocketTransportConfig(); config.Type = StackTransportType.Tcp; config.Role = Role.Server; config.LocalIpAddress = IPAddress.Any; config.MaxConnections = RpceServerContext.DEFAULT_MAX_CONNECTIONS; config.BufferSize = Math.Max(serverContext.MaxReceiveFragmentSize, serverContext.MaxTransmitFragmentSize); this.tcpTransport = new TransportStack(config, RpceDecodePduCallback); } } catch (Exception e) { ex = e; this.tcpTransport = null; } try { //Start IPv4 IPEndPoint ipv4Endpoint = new IPEndPoint(IPAddress.Any, port); this.tcpTransport.Start(ipv4Endpoint); ipv4Started = true; } catch (Exception e) { ex = e; } //Start IPv6 try { IPEndPoint ipv6Endpoint = new IPEndPoint(IPAddress.IPv6Any, port); this.tcpTransport.Start(ipv6Endpoint); ipv6Started = true; } catch (Exception e) { ex = e; } if (!ipv4Started && !ipv6Started) { this.serverContextManager.RemoveServerContext(serverContext); this.openedTcpPortList.Remove(port); throw new InvalidOperationException("TCP server failed to start.", ex); } if (this.tcpReceiveThread == null) { this.tcpReceiveThread = new Thread(TcpReceiveLoop); this.tcpReceiveThread.Start(); } return serverContext; } }
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(); }
/// <summary> /// Dispose method. /// </summary> /// <param name="disposing"> /// True to release both managed and unmanaged resources.<para/> /// False to release unmanaged resources only. /// </param> protected virtual void Dispose(bool disposing) { if (!this.disposed) { Thread tcpThread = this.tcpReceiveThread; Thread smbThread = this.smbReceiveThread; this.StopAll(); if (tcpThread != null) { tcpThread.Join(); } if (smbThread != null) { smbThread.Join(); } if (disposing) { //Release managed resources. if (this.tcpTransport != null) { this.tcpTransport.Dispose(); this.tcpTransport = null; } if (this.smbTransport != null) { this.smbTransport.Dispose(); this.smbTransport = null; } this.receivedTransportEvents.Dispose(); } this.disposed = true; } }
/// <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 }
/// <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> /// 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> private 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: } // Call the appropriate methods to clean up unmanaged resources. // If disposing is false, only the following code is executed: if (this.transport != null) { this.transport.Dispose(); this.transport = null; } this.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 = 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 == 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> /// 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> /// 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(); }