protected void HandleDataReceiveD(Connection client, byte[] buffer, int read) { if (read != buffer.Length || BZFSProtocolConnectionAccepted == null) { client.ProtcolPassed = false; DisconnectPendingClient(client); Logger.Log4("Disconnecting abnormal connection from " + client.ClientConnection.Client.RemoteEndPoint.ToString()); } else { if (Encoding.ASCII.GetString(buffer) == Protocol.BZFSHailString) { client.ProtcolPassed = true; client.NetStream.Write(Protocol.DefaultBZFSVersion, 0, Protocol.DefaultBZFSVersion.Length); client.NetStream.Flush(); Logger.Log4("BZFS header from " + client.ClientConnection.Client.RemoteEndPoint.ToString()); BZFSProtocolConnectionAccepted?.Invoke(this, client); } } }
protected void ProcessPendingClients() { PendingClient[] clients = null; lock (PendingClients) clients = PendingClients.ToArray(); while (clients.Length > 0) { foreach (PendingClient c in clients) { if (!c.DNSStarted) { var address = ((IPEndPoint)c.ClientConnection.Client.RemoteEndPoint).Address.ToString(); Logger.Log3("DNS Lookup started for " + address); Dns.BeginGetHostEntry(address, DNSLookupCompleted, c); c.DNSStarted = true; } else if (!c.DNSPassed) { if (c.HostEntry != null) { Logger.Log3("Ban check started for " + c.HostEntry.HostName); // lookup the host in the ban list bool ban = false; string reason = string.Empty; if (CheckHostBan != null) { ban = CheckHostBan(c, ref reason); } if (!ban) { c.DNSPassed = true; } else { c.HostEntry = null; DisconnectPendingClient(c); } } } if (!c.Active) { continue; } int available = c.ClientConnection.Available; if (c.ClientConnection.Available > 0) { if (!c.ProtcolPassed && c.ClientConnection.Available >= Protocol.BZFSHail.Length) { c.DataRecieved = true; byte[] buffer = new byte[Protocol.BZFSHail.Length]; int read = c.NetStream.Read(buffer, 0, buffer.Length); if (read != buffer.Length) { c.ProtcolPassed = false; DisconnectPendingClient(c); Logger.Log4("Disconnecting abnormal connection from " + c.ClientConnection.Client.RemoteEndPoint.ToString()); } else { if (Encoding.ASCII.GetString(buffer) == Protocol.BZFSHailString) { c.ProtcolPassed = true; c.NetStream.Write(Protocol.DefaultBZFSVersion, 0, Protocol.DefaultBZFSVersion.Length); c.NetStream.Flush(); c.VersionPassed = true; Logger.Log4("BZFS header from " + c.ClientConnection.Client.RemoteEndPoint.ToString()); } } } } if (c.Active) { if (c.DNSStarted && c.DNSPassed && c.DataRecieved && c.ProtcolPassed && c.VersionPassed) { RemovePendingClient(c); Logger.Log4("Accepted BZFS connection from " + c.ClientConnection.Client.RemoteEndPoint.ToString()); // send them off to the next step if (BZFSProtocolConnectionAccepted != null) { BZFSProtocolConnectionAccepted.Invoke(this, c); } } } } Thread.Sleep(100); lock (PendingClients) clients = PendingClients.ToArray(); } WorkerThread = null; }