/// <summary> /// Some broken NATs will reply to TCP KeepAlive even after the client initiating the connection has long gone, /// This methods prevent such connections from hanging around indefinitely by sending an unsolicited ECHO response to make sure the connection is still alive. /// </summary> public void SendSMBKeepAlive(TimeSpan inactivityDuration) { List <ConnectionState> connections = new List <ConnectionState>(m_activeConnections); foreach (ConnectionState connection in connections) { if (connection.LastReceiveDT.Add(inactivityDuration) < DateTime.UtcNow && connection.LastSendDT.Add(inactivityDuration) < DateTime.UtcNow) { if (connection is SMB1ConnectionState) { // [MS-CIFS] Clients SHOULD, at minimum, send an SMB_COM_ECHO to the server every few minutes. // This means that an unsolicited SMB_COM_ECHO reply is not likely to be sent on a connection that is alive. SmbLibraryStd.SMB1.SMB1Message echoReply = SMB1.EchoHelper.GetUnsolicitedEchoReply(); SMBServer.EnqueueMessage(connection, echoReply); } else if (connection is SMB2ConnectionState) { SmbLibraryStd.SMB2.EchoResponse echoResponse = SMB2.EchoHelper.GetUnsolicitedEchoResponse(); SMBServer.EnqueueResponse(connection, echoResponse); } } } }
private void btnStart_Click(object sender, EventArgs e) { IPAddress serverAddress = (IPAddress)comboIPAddress.SelectedValue; SMBTransportType transportType; if (rbtNetBiosOverTCP.Checked) { transportType = SMBTransportType.NetBiosOverTCP; } else { transportType = SMBTransportType.DirectTCPTransport; } NTLMAuthenticationProviderBase authenticationMechanism; if (chkIntegratedWindowsAuthentication.Checked) { authenticationMechanism = new IntegratedNTLMAuthenticationProvider(); } else { UserCollection users; try { users = ReadUserSettings(); } catch { MessageBox.Show("Cannot read " + SettingsFileName, "Error"); return; } authenticationMechanism = new IndependentNTLMAuthenticationProvider(users.GetUserPassword); } SMBShareCollection shares; try { shares = ReadShareSettings(); } catch (Exception) { MessageBox.Show("Cannot read " + SettingsFileName, "Error"); return; } GSSProvider securityProvider = new GSSProvider(authenticationMechanism); m_server = new SmbLibraryStd.Server.SMBServer(shares, securityProvider); m_logWriter = new LogWriter(); // The provided logging mechanism will synchronously write to the disk during server activity. // To maximize server performance, you can disable logging by commenting out the following line. m_server.LogEntryAdded += new EventHandler <LogEntry>(m_logWriter.OnLogEntryAdded); try { m_server.Start(serverAddress, transportType, chkSMB1.Checked, chkSMB2.Checked); if (transportType == SMBTransportType.NetBiosOverTCP) { if (serverAddress.AddressFamily == AddressFamily.InterNetwork && !IPAddress.Equals(serverAddress, IPAddress.Any)) { IPAddress subnetMask = NetworkInterfaceHelper.GetSubnetMask(serverAddress); m_nameServer = new NameServer(serverAddress, subnetMask); m_nameServer.Start(); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); return; } btnStart.Enabled = false; btnStop.Enabled = true; comboIPAddress.Enabled = false; rbtDirectTCPTransport.Enabled = false; rbtNetBiosOverTCP.Enabled = false; chkSMB1.Enabled = false; chkSMB2.Enabled = false; chkIntegratedWindowsAuthentication.Enabled = false; }