private void ListenLoop() { if (scheduler.Verbose) { Console.WriteLine("Starting to listen on {0}", myEndpoint); } listener = new TcpListener(myEndpoint); listener.ExclusiveAddressUse = true; listener.Start(); while (true) { if (scheduler.Verbose) { Console.WriteLine("Waiting for the next incoming connection"); } TcpClient client = listener.AcceptTcpClient(); CertificateValidator myValidator = new CertificateValidator(scheduler); SslStream sslStream = new SslStream(client.GetStream(), leaveInnerStreamOpen: false, myValidator.ValidateSSLCertificate); sslStream.AuthenticateAsServer(scheduler.MyCert, clientCertificateRequired: true, checkCertificateRevocation: false); var remoteCert = sslStream.RemoteCertificate as X509Certificate2; if (scheduler.Verbose) { Console.WriteLine("Received an incoming connection from remote certified as {0}", IoScheduler.CertificateToString(remoteCert)); } ReceiverThread.Create(scheduler, sslStream); ServerSenderThread.Create(scheduler, sslStream); } }
public static ReceiverThread Create(IoScheduler scheduler, SslStream stream) { ReceiverThread receiverThread = new ReceiverThread(scheduler, stream); Thread t = new Thread(receiverThread.Run); t.Start(); return(receiverThread); }
protected override bool Connect() { var destinationPublicIdentity = scheduler.LookupPublicKey(destinationPublicKey); if (destinationPublicIdentity == null) { if (scheduler.Verbose) { Console.Error.WriteLine("Could not connect to destination public key {0} because we don't know its address.", IoScheduler.PublicKeyToString(destinationPublicKey)); } return(false); } if (scheduler.Verbose) { Console.WriteLine("Starting connection to {0}", IoScheduler.PublicIdentityToString(destinationPublicIdentity)); } TcpClient client; try { client = new TcpClient(destinationPublicIdentity.HostNameOrAddress, destinationPublicIdentity.Port); } catch (Exception e) { scheduler.ReportException(e, "connecting to " + IoScheduler.PublicIdentityToString(destinationPublicIdentity)); return(false); } var myCertificateCollection = new X509CertificateCollection(); myCertificateCollection.Add(scheduler.MyCert); var myValidator = new CertificateValidator(scheduler, destinationPublicIdentity); try { stream = new SslStream(client.GetStream(), leaveInnerStreamOpen: false, myValidator.ValidateSSLCertificate); stream.AuthenticateAsClient(destinationPublicIdentity.FriendlyName, myCertificateCollection, checkCertificateRevocation: false); } catch (Exception e) { scheduler.ReportException(e, "authenticating connection to " + IoScheduler.PublicIdentityToString(destinationPublicIdentity)); return(false); } remoteCert = stream.RemoteCertificate as X509Certificate2; if (!ByteArrayComparer.Default().Equals(IoScheduler.GetCertificatePublicKey(remoteCert), destinationPublicKey)) { Console.Error.WriteLine("Connected to {0} expecting public key {1} but found public key {2}, so disconnecting.", IoScheduler.PublicIdentityToString(destinationPublicIdentity), IoScheduler.PublicKeyToString(destinationPublicKey), IoScheduler.PublicKeyToString(IoScheduler.GetCertificatePublicKey(remoteCert))); return(false); } if (scheduler.Verbose) { Console.WriteLine("Successfully connected to {0} and got certificate identifying it as {1}", IoScheduler.PublicIdentityToString(destinationPublicIdentity), IoScheduler.CertificateToString(remoteCert)); } // Now that the connection is successful, create a thread to // receive packets on it. ReceiverThread receiverThread = ReceiverThread.Create(scheduler, stream); return(true); }