public void Init() { TestRuntime.AssertXcodeVersion(11, 0); // connect so that we can later when the report and test with it connectedEvent = new AutoResetEvent(false); reportEvent = new AutoResetEvent(false); host = NetworkResources.MicrosoftUri.Host; // we create a connection which we are going to use to get the availabe // interfaces, that way we can later test protperties of the NWParameters class. using (var parameters = NWParameters.CreateUdp()) using (var endpoint = NWEndpoint.Create(host, "80")) { using (var protocolStack = parameters.ProtocolStack) { var ipOptions = protocolStack.InternetProtocol; ipOptions.IPSetVersion(NWIPVersion.Version4); } connection = new NWConnection(endpoint, parameters); connection.SetQueue(DispatchQueue.DefaultGlobalQueue); // important, else we will get blocked connection.SetStateChangeHandler(ConnectionStateHandler); connection.Start(); Assert.True(connectedEvent.WaitOne(20000), "Connection timed out."); connection.GetEstablishmentReport(DispatchQueue.DefaultGlobalQueue, (r) => { report = r; reportEvent.Set(); }); Assert.True(reportEvent.WaitOne(20000), "Connection timed out."); } }
public void Init() { TestRuntime.AssertXcodeVersion(10, 0); // we want to use a single connection, since it is expensive connectedEvent = new AutoResetEvent(false); interfaces = new List <NWInterface> (); host = NetworkResources.MicrosoftUri.Host; // we create a connection which we are going to use to get the availabe // interfaces, that way we can later test protperties of the NWParameters class. using (var parameters = NWParameters.CreateUdp()) using (var endpoint = NWEndpoint.Create(host, "80")) { using (var protocolStack = parameters.ProtocolStack) { var ipOptions = protocolStack.InternetProtocol; #if NET ipOptions.SetVersion(NWIPVersion.Version4); #else ipOptions.IPSetVersion(NWIPVersion.Version4); #endif } connection = new NWConnection(endpoint, parameters); connection.SetQueue(DispatchQueue.DefaultGlobalQueue); // important, else we will get blocked connection.SetStateChangeHandler(ConnectionStateHandler); connection.Start(); Assert.True(connectedEvent.WaitOne(20000), "Connection timed out."); } }
static void StartConnection(NWConnection connection) { connection.SetQueue(DispatchQueue.MainQueue); warn($"Start Connection on {connection.Handle} with {connection.Endpoint}"); connection.SetStateChangeHandler((state, error) => { var remote = connection.Endpoint; var errno = (SslStatus)(error != null ? error.ErrorCode : 0); switch (state) { case NWConnectionState.Waiting: warn($"Connect to {remote.Hostname} port {remote.Port} udp={useUdp} failed, is waiting"); break; case NWConnectionState.Failed: warn($"Connect to {remote.Hostname} port {remote.Port} udp={useUdp} failed, error {errno}"); break; case NWConnectionState.Ready: if (verbose) { warn($"Connect to {remote.Hostname} port {remote.Port} udp={useUdp} succeeded"); } break; case NWConnectionState.Cancelled: // Release reference connection = null; break; } }); connection.Start(); }
void ConnectionStateHandler(NWConnectionState state, NWError error) { switch (state) { case NWConnectionState.Ready: connectedEvent.Set(); break; case NWConnectionState.Cancelled: connection?.Dispose(); connection = null; stack?.Dispose(); stack = null; foreach (var o in options) { o.Dispose(); } break; case NWConnectionState.Invalid: case NWConnectionState.Failed: Assert.Inconclusive("Network connection could not be performed."); break; } }
public void Init() { TestRuntime.AssertXcodeVersion(10, 0); // we want to use a single connection, since it is expensive connectedEvent = new AutoResetEvent(false); host = "www.google.com"; using (var parameters = NWParameters.CreateTcp()) using (var endpoint = NWEndpoint.Create(host, "80")) { connection = new NWConnection(endpoint, parameters); connection.SetQueue(DispatchQueue.DefaultGlobalQueue); // important, else we will get blocked connection.SetStateChangeHandler(ConnectionStateHandler); connection.Start(); Assert.True(connectedEvent.WaitOne(20000), "Connection timed out."); stack = parameters.ProtocolStack; using (var ipOptions = stack.InternetProtocol) { if (ipOptions != null) { #if NET ipOptions.SetVersion(NWIPVersion.Version4); #else ipOptions.IPSetVersion(NWIPVersion.Version4); #endif stack.PrependApplicationProtocol(ipOptions); } } } }
static NWConnection CreateOutboundConnection(string name, string port) { NWEndpoint endpoint; if (bonjour) { endpoint = NWEndpoint.CreateBonjourService(name, GetServiceType(), "local"); } else { endpoint = NWEndpoint.Create(name, port); } Action <NWProtocolOptions> configureTls = SetupTls(); NWParameters parameters; if (useUdp) { if (useTls) { parameters = NWParameters.CreateSecureUdp(configureTls: configureTls, configureUdp: null); } else { parameters = NWParameters.CreateUdp(configureUdp: null); } } else { if (useTls) { parameters = NWParameters.CreateSecureTcp(configureTls: configureTls, configureTcp: null); } else { parameters = NWParameters.CreateTcp(configureTcp: null); } } using (NWProtocolStack protocolStack = parameters.ProtocolStack){ if (ipv4 || ipv6) { NWProtocolOptions ipOptions = protocolStack.InternetProtocol; ipOptions.IPSetVersion(ipv4 ? NWIPVersion.Version4 : NWIPVersion.Version6); } } if (localAddr != null || localPort != null) { using (NWEndpoint localEndpoint = NWEndpoint.Create(localAddr != null ? localAddr : "::", port == null ? "0" : port)) parameters.LocalEndpoint = localEndpoint; } var connection = new NWConnection(endpoint, parameters); endpoint.Dispose(); parameters.Dispose(); return(connection); }
public void CreateSecureTcpTestDoNotSetUpTls() { var setUpProtocol = CreateConfigureProtocolHandler(); using (var parameters = NWParameters.CreateSecureTcp(configureTls: null, configureTcp: setUpProtocol)) using (var endpoint = NWEndpoint.Create("wwww.google.com", "80")) using (var connection = new NWConnection(endpoint, parameters)) { connection.SetQueue(DispatchQueue.MainQueue); connection.Start(); configureEvent.WaitOne(); connection.Cancel(); Assert.False(secureConnectionWasSet, "Configure TLS handler was called."); Assert.True(protocolConfigured, "Protocol configure handler was not called."); } }
static void StartSendReceiveLoop(NWConnection connection) { // Start reading from stdin if (!detached) { SendLoop(connection); } if (verbose) { warn("Start Receive Loop"); } // Start reading from the connection ReceiveLoop(connection); }
static void ReceiveLoop(NWConnection connection) { connection.ReceiveData(1, uint.MaxValue, (DispatchData dispatchData, NWContentContext context, bool isComplete, NWError error) => { Action scheduleNext = () => { // If the context is marked as complete, and is the final context, // we're read-closed. if (isComplete) { if (context != null && context.IsFinal) { if (verbose) { warn("Exiting because isComplete && context.IsFinal"); } Environment.Exit(0); } if (dispatchData == null) { if (verbose) { warn($"Exiting because isComplete && data == zero; error={error}"); } Environment.Exit(0); } } if (error == null) { ReceiveLoop(connection); } }; if (dispatchData != null) { const int STDOUT_FILENO = 1; DispatchIO.Write(STDOUT_FILENO, dispatchData, DispatchQueue.MainQueue, (data, stdoutError) => { if (stdoutError != 0) { warn("stdout write error"); } scheduleNext(); }); } else { scheduleNext(); } }); }
public void Init() { TestRuntime.AssertXcodeVersion(10, 0); // we want to use a single connection, since it is expensive connectedEvent = new AutoResetEvent(false); host = NetworkResources.MicrosoftUri.Host; interfaces = new List <NWInterface> (); using (var parameters = NWParameters.CreateUdp()) using (var endpoint = NWEndpoint.Create(host, "80")) { connection = new NWConnection(endpoint, parameters); connection.SetQueue(DispatchQueue.DefaultGlobalQueue); // important, else we will get blocked connection.SetStateChangeHandler(ConnectionStateHandler); connection.Start(); Assert.True(connectedEvent.WaitOne(20000), "Connection timed out."); using (var path = connection.CurrentPath) { path.EnumerateInterfaces(EnumerateInterfacesHandler); } } }
public void SetUp() { // connect and once the connection is done, deal with the diff tests connectedEvent = new AutoResetEvent(false); host = NetworkResources.MicrosoftUri.Host; // we create a connection which we are going to use to get the availabe // interfaces, that way we can later test protperties of the NWParameters class. using (var parameters = NWParameters.CreateUdp()) using (var endpoint = NWEndpoint.Create(host, "80")) { using (var protocolStack = parameters.ProtocolStack) { var ipOptions = protocolStack.InternetProtocol; ipOptions.IPSetVersion(NWIPVersion.Version4); } connection = new NWConnection(endpoint, parameters); connection.SetQueue(DispatchQueue.DefaultGlobalQueue); // important, else we will get blocked connection.SetStateChangeHandler(ConnectionStateHandler); connection.Start(); Assert.True(connectedEvent.WaitOne(20000), "Connection timed out."); } }
static void SendLoop(NWConnection connection) { const int STDIN_FILENO = 0; DispatchIO.Read(STDIN_FILENO, 8192, DispatchQueue.MainQueue, (DispatchData readData, int stdinError) => { if (stdinError != 0) { warn($"Standard input error: {stdinError}"); } else if (readData == null || readData.Size == 0) { // Null data represent EOF // Send a "write close" on the connection, by sending // null data with the final message context marked as // complete. Note that it is valid to send with null // data but a non-null context. connection.Send((byte [] )null, context: NWContentContext.FinalMessage, isComplete: true, callback: (NWError error) => { if (error != null) { warn($"send error: {error.ErrorCode}"); } }); // Stop reading from stdin, do not schedule another SendLoop } else { connection.Send(readData, context: NWContentContext.DefaultMessage, isComplete: true, callback: (NWError error) => { if (error != null) { warn($"send error: {error.ErrorCode}"); } else { // continue reading from stdin SendLoop(connection); } }); } }); }
void ConnectionStateHandler(NWConnectionState state, NWError error) { Console.WriteLine($"State is {state} and error {error}"); switch (state) { case NWConnectionState.Ready: connectedEvent.Set(); break; case NWConnectionState.Cancelled: connection?.Dispose(); connection = null; foreach (var i in interfaces) { i.Dispose(); } break; case NWConnectionState.Invalid: case NWConnectionState.Failed: Assert.Inconclusive("Network connection could not be performed."); break; } }
public void TlsDefaults() { using (var ep = NWEndpoint.Create("www.microsoft.com", "https")) using (var parameters = NWParameters.CreateSecureTcp()) using (var queue = new DispatchQueue(GetType().FullName)) { var connection = new NWConnection(ep, parameters); var ready = new ManualResetEvent(false); connection.SetStateChangeHandler((state, error) => { Console.WriteLine(state); switch (state) { case NWConnectionState.Cancelled: case NWConnectionState.Failed: // We can't dispose until the connection has been closed or it failed. connection.Dispose(); break; case NWConnectionState.Invalid: case NWConnectionState.Preparing: case NWConnectionState.Waiting: break; case NWConnectionState.Ready: ready.Set(); break; default: break; } }); connection.SetQueue(queue); connection.Start(); // Wait until the connection is ready. Assert.True(ready.WaitOne(TimeSpan.FromSeconds(10)), "Connection is ready"); using (var m = connection.GetProtocolMetadata(NWProtocolDefinition.TlsDefinition)) { var s = m.TlsSecProtocolMetadata; Assert.False(s.EarlyDataAccepted, "EarlyDataAccepted"); Assert.That(s.NegotiatedCipherSuite, Is.Not.EqualTo(SslCipherSuite.SSL_NULL_WITH_NULL_NULL), "NegotiatedCipherSuite"); Assert.Null(s.NegotiatedProtocol, "NegotiatedProtocol"); Assert.That(s.NegotiatedProtocolVersion, Is.EqualTo(SslProtocol.Tls_1_2).Or.EqualTo(SslProtocol.Tls_1_3), "NegotiatedProtocolVersion"); Assert.NotNull(s.PeerPublicKey, "PeerPublicKey"); Assert.True(SecProtocolMetadata.ChallengeParametersAreEqual(s, s), "ChallengeParametersAreEqual"); Assert.True(SecProtocolMetadata.PeersAreEqual(s, s), "PeersAreEqual"); if (TestRuntime.CheckXcodeVersion(11, 0)) { using (var d = s.CreateSecret("Xamarin", 128)) { Assert.That(d.Size, Is.EqualTo((nuint)128), "CreateSecret-1"); } using (var d = s.CreateSecret("Microsoft", new byte [1], 256)) { Assert.That(d.Size, Is.EqualTo((nuint)256), "CreateSecret-2"); } Assert.That(s.NegotiatedTlsProtocolVersion, Is.EqualTo(TlsProtocolVersion.Tls12).Or.EqualTo(TlsProtocolVersion.Tls13), "NegotiatedTlsProtocolVersion"); // we want to test the binding/API - not the exact value which can vary depending on the negotiation between the client (OS) and server... Assert.That(s.NegotiatedTlsCipherSuite, Is.Not.EqualTo(0), "NegotiatedTlsCipherSuite"); Assert.That(s.ServerName, Is.EqualTo("www.microsoft.com"), "ServerName"); // we don't have a TLS-PSK enabled server to test this Assert.False(s.AccessPreSharedKeys((psk, pskId) => { }), "AccessPreSharedKeys"); } } connection.Cancel(); } }
static NWListener CreateAndStartListener(string host, string port) { Action <NWProtocolOptions> configureTls = SetupTls(); NWParameters parameters; // Create the parameters, either TLS or no TLS, and with UDP or no UDP if (useUdp) { if (useTls) { parameters = NWParameters.CreateSecureUdp(configureTls: configureTls, configureUdp: null); } else { parameters = NWParameters.CreateUdp(configureUdp: null); } } else { if (useTls) { parameters = NWParameters.CreateSecureTcp(configureTls: configureTls, configureTcp: null); } else { parameters = NWParameters.CreateTcp(configureTcp: null); } } // If specified, set the IP version using (NWProtocolStack protocolStack = parameters.ProtocolStack){ if (ipv4 || ipv6) { NWProtocolOptions ipOptions = protocolStack.InternetProtocol; ipOptions.IPSetVersion(ipv4 ? NWIPVersion.Version4 : NWIPVersion.Version6); } } // Bind to local address and port string address = bonjour ? null : host; if (address != null || port != null) { NWEndpoint localEndpoint = NWEndpoint.Create(address != null ? address : "::", port != null ? port : "0"); Console.WriteLine("Getting {0} and {1}", address != null ? address : "::", port != null ? port : "0"); parameters.LocalEndpoint = localEndpoint; Console.WriteLine("With port: " + localEndpoint.Port); } var listener = NWListener.Create(parameters); if (bonjour && host != null) { listener.SetAdvertiseDescriptor(NWAdvertiseDescriptor.CreateBonjourService(host, GetServiceType(), "local")); listener.SetAdvertisedEndpointChangedHandler((NWEndpoint advertisedEndpoint, bool added) => { if (verbose) { var astr = added ? "added" : "removed"; warn($"Listener {astr} on {advertisedEndpoint.BonjourServiceName} on ({advertisedEndpoint.BonjourServiceName}.{GetServiceType()}.local"); } }); } listener.SetQueue(DispatchQueue.MainQueue); listener.SetStateChangedHandler((listenerState, error) => { var errno = (SslStatus)(error == null ? 0 : error.ErrorCode); switch (listenerState) { case NWListenerState.Waiting: if (verbose) { warn($"Listener on port {listener.Port} udp={useUdp} waiting"); } break; case NWListenerState.Failed: warn($"Listener on port {listener.Port} udp={useUdp} failed"); break; case NWListenerState.Ready: if (verbose) { warn($"Listener on port {listener.Port} udp={useUdp} ready"); } break; case NWListenerState.Cancelled: listener = null; break; } }); listener.SetNewConnectionHandler((connection) => { if (inboundConnection != null) { // We only support one connection at a time, so if we already // have one, reject the incoming connection. connection.Cancel(); } else { if (verbose) { warn($"New Connection on {connection.Handle} with {connection.Endpoint}"); } // Accept the incoming connection and start sending and receiving on it inboundConnection = connection; StartConnection(inboundConnection); StartSendReceiveLoop(inboundConnection); } }); listener.Start(); return(listener); }
public iOSWifiService() { var conn = new NWConnection(new IntPtr(), true); }