private void StartServer(PrivateIdentity myIdentity, string localHostNameOrAddress, int localPort) { onlyClient = false; try { myCert = new X509Certificate2(myIdentity.Pkcs12, "" /* empty password */, X509KeyStorageFlags.Exportable); } catch (Exception e) { Console.Error.WriteLine("Could not import private key. Exception:{0}", e); throw new Exception("Can't start server because private key not decryptable"); } // The `local` parameters override the parameters in // `myIdentity`, unless they're empty or zero. if (localHostNameOrAddress == null || localHostNameOrAddress.Length == 0) { localHostNameOrAddress = myIdentity.HostNameOrAddress; } if (localPort == 0) { localPort = myIdentity.Port; } var address = LookupHostNameOrAddress(localHostNameOrAddress); if (address == null) { Console.Error.WriteLine("ERROR: Could not find any addresses when resolving {0}, which I'm supposed to bind to."); throw new Exception("Can't resolve binding address"); } var myEndpoint = new IPEndPoint(address, localPort); if (verbose) { Console.WriteLine("Starting I/O scheduler as server listening to {0} certified as {1}", myEndpoint, IoScheduler.CertificateToString(myCert)); } sendDispatchThread = new SendDispatchThread(this); Thread st = new Thread(sendDispatchThread.Run); st.Start(); // Start a thread to listen on my binding endpoint. listenerThread = new ListenerThread(this, myEndpoint); Thread lt = new Thread(listenerThread.Run); lt.Start(); }
private IoScheduler(PrivateIdentity myIdentity, string localHostNameOrAddress, int localPort, List <PublicIdentity> knownIdentities, bool i_verbose = false, int i_maxSendTries = 3) { verbose = i_verbose; maxSendTries = i_maxSendTries; receiveQueue = new BufferBlock <ReceivedPacket>(); destinationPublicKeyToSenderThreadMap = new Dictionary <byte[], List <SenderThread> >(ByteArrayComparer.Default()); publicKeyToPublicIdentityMap = new Dictionary <byte[], PublicIdentity>(ByteArrayComparer.Default()); foreach (var knownIdentity in knownIdentities) { publicKeyToPublicIdentityMap[knownIdentity.PublicKey] = knownIdentity; } if (myIdentity == null) { StartClient(); } else { StartServer(myIdentity, localHostNameOrAddress, localPort); } }
public static void CreateNewIdentity(string friendlyName, string hostNameOrAddress, int port, out PublicIdentity publicIdentity, out PrivateIdentity privateIdentity) { var key = RSA.Create(4096); var subject = string.Format("CN = {0}", friendlyName); var req = new CertificateRequest(subject, key, HashAlgorithmName.SHA256, RSASignaturePadding.Pss); var now = DateTime.Now; var expiry = now.AddYears(10); var cert = req.CreateSelfSigned(now, expiry); var pkcs12 = cert.Export(X509ContentType.Pkcs12, "" /* empty password */); publicIdentity = new PublicIdentity { FriendlyName = friendlyName, PublicKey = IoScheduler.GetCertificatePublicKey(cert), HostNameOrAddress = hostNameOrAddress, Port = port }; privateIdentity = new PrivateIdentity { FriendlyName = friendlyName, Pkcs12 = pkcs12, HostNameOrAddress = hostNameOrAddress, Port = port }; }
public static IoScheduler CreateServer(PrivateIdentity myIdentity, string localHostNameOrAddress, int localPort, List <PublicIdentity> knownIdentities, bool verbose = false, int maxSendTries = 3) { return(new IoScheduler(myIdentity, localHostNameOrAddress, localPort, knownIdentities, verbose, maxSendTries)); }