Beispiel #1
0
        public static Transport Create(AddressEntry entry)
        {
            switch (entry.Method)
            {
            case "tcp":
            {
                Transport transport = new SocketTransport();
                transport.Open(entry);
                return(transport);
            }

#if !PORTABLE
            case "unix":
            {
                Transport transport = new UnixNativeTransport();
                transport.Open(entry);
                return(transport);
            }
#endif
#if ENABLE_PIPES
            case "win": {
                Transport transport = new PipeTransport();
                transport.Open(entry);
                return(transport);
            }
#endif
            default:
                throw new NotSupportedException("Transport method \"" + entry.Method + "\" not supported");
            }
        }
Beispiel #2
0
        public async Task <ConnectionContext> ConnectToServer(CancellationToken cancellationToken = default)
        {
            var pipeClient = new NamedPipeClientStream(".", _endpoint.PipeName, PipeDirection.InOut,
                                                       PipeOptions.Asynchronous);
            await pipeClient.ConnectAsync(cancellationToken);

            var transport = new PipeTransport(pipeClient, _loggerFactory.CreateLogger <PipeTransport>());

            transport.Start();
            return(transport);
        }
Beispiel #3
0
        bool AcceptClient(PipeStream client, out ServerConnection conn)
        {
            PipeTransport transport = new PipeTransport();

            //client.Client.Blocking = true;
            //transport.SocketHandle = (long)client.Client.Handle;
            transport.Stream = client;
            conn             = new ServerConnection(transport);
            conn.Server      = this;
            conn.Id          = Id;

            if (conn.Transport.Stream.ReadByte() != 0)
            {
                return(false);
            }

            conn.isConnected = true;

            SaslPeer remote = new SaslPeer();

            remote.stream = transport.Stream;
            SaslServer local = new SaslServer();

            local.stream = transport.Stream;
            local.Guid   = Id;

            local.Peer  = remote;
            remote.Peer = local;

            bool success = local.Authenticate();

            //bool success = true;

            Console.WriteLine("Success? " + success);

            if (!success)
            {
                return(false);
            }

            conn.UserId = ((SaslServer)local).uid;

            conn.isAuthenticated = true;

            return(true);
        }
Beispiel #4
0
        bool AcceptClient(PipeStream client, out ServerConnection conn)
        {
            PipeTransport transport = new PipeTransport ();
            //client.Client.Blocking = true;
            //transport.SocketHandle = (long)client.Client.Handle;
            transport.Stream = client;
            conn = new ServerConnection (transport);
            conn.Server = this;
            conn.Id = Id;

            if (conn.Transport.Stream.ReadByte () != 0)
                return false;

            conn.isConnected = true;

            SaslPeer remote = new SaslPeer ();
            remote.stream = transport.Stream;
            SaslServer local = new SaslServer ();
            local.stream = transport.Stream;
            local.Guid = Id;

            local.Peer = remote;
            remote.Peer = local;

            bool success = local.Authenticate ();
            //bool success = true;

            Console.WriteLine ("Success? " + success);

            if (!success)
                return false;

            conn.UserId = ((SaslServer)local).uid;

            conn.isAuthenticated = true;

            return true;
        }
Beispiel #5
0
        public static Transport Create(AddressEntry entry)
        {
            switch (entry.Method)
            {
            case "tcp":
            {
                Transport transport = new SocketTransport();
                transport.Open(entry);
                return(transport);
            }

            case "unix":
            {
                if (OSHelpers.PlatformIsUnixoid)
                {
                    Transport transport;
#if MONO_46
                    if (UnixSendmsgTransport.Available())
                    {
                        transport = new UnixSendmsgTransport();
                    }
                    else
                    {
                        if (ProtocolInformation.Verbose)
                        {
                            Console.Error.WriteLine("Warning: Syscall.sendmsg() not available, transfering unix FDs will not work");
                        }
#endif
                    transport = new UnixNativeTransport();
#if MONO_46
                }
#endif
                    transport.Open(entry);
                    return(transport);
                }
                break;
            }

#if ENABLE_PIPES
            case "win":
            {
                Transport transport = new PipeTransport();
                transport.Open(entry);
                return(transport);
            }
#endif

            case "launchd":
            {
                string env = entry.Properties["env"];
                var    psi = new ProcessStartInfo();
                psi.FileName               = "launchctl";
                psi.Arguments              = "getenv '" + env.Replace("'", "'\\''") + "'";
                psi.UseShellExecute        = false;
                psi.RedirectStandardOutput = true;
                var process = Process.Start(psi);
                var output  = process.StandardOutput.ReadToEnd();
                process.WaitForExit();
                if (process.ExitCode != 0)
                {
                    throw new Exception("Calling " + psi.FileName + " " + psi.Arguments + " failed with error code " + process.ExitCode);
                }
                var path   = output.Trim();
                var entry2 = new AddressEntry();
                entry2.Method             = "unix";
                entry2.Properties["path"] = path;
                return(Create(entry2));
            }

            // "autolaunch:" means: the first client user of the dbus library shall spawn the daemon on itself, see dbus 1.7.8 from http://dbus.freedesktop.org/releases/dbus/
            case "autolaunch":
            {
                if (OSHelpers.PlatformIsUnixoid)
                {
                    break;
                }

                string addr = Address.GetSessionBusAddressFromSharedMemory();

                if (string.IsNullOrEmpty(addr))                            // we have to launch the daemon ourselves
                {
                    string oldDir = Directory.GetCurrentDirectory();
                    // Without this, the "current" folder for the new process will be the one where the current
                    // executable resides, and as a consequence,that folder cannot be relocated/deleted unless the daemon is stopped
                    Directory.SetCurrentDirectory(Environment.GetFolderPath(Environment.SpecialFolder.System));

                    Process process = Process.Start(DBUS_DAEMON_LAUNCH_COMMAND);
                    if (process == null)
                    {
                        Directory.SetCurrentDirectory(oldDir);
                        throw new NotSupportedException("Transport method \"autolaunch:\" - cannot launch dbus daemon '" + DBUS_DAEMON_LAUNCH_COMMAND + "'");
                    }

                    // wait for daemon
                    Stopwatch stopwatch = new Stopwatch();
                    stopwatch.Start();
                    do
                    {
                        addr = Address.GetSessionBusAddressFromSharedMemory();
                        if (String.IsNullOrEmpty(addr))
                        {
                            Thread.Sleep(100);
                        }
                    } while (String.IsNullOrEmpty(addr) && stopwatch.ElapsedMilliseconds <= 5000);

                    Directory.SetCurrentDirectory(oldDir);
                }

                if (string.IsNullOrEmpty(addr))
                {
                    throw new NotSupportedException("Transport method \"autolaunch:\" - timeout during access to freshly launched dbus daemon");
                }
                return(Create(AddressEntry.Parse(addr)));
            }
            }

            throw new NotSupportedException("Transport method \"" + entry.Method + "\" not supported");
        }
Beispiel #6
0
        public static Transport Create(AddressEntry entry)
        {
            switch (entry.Method)
            {
            case "tcp":
            {
                Transport transport = new SocketTransport();
                transport.Open(entry);
                return(transport);
            }

            case "unix":
            {
                if (OSHelpers.PlatformIsUnixoid)
                {
                    Transport transport = new UnixNativeTransport();
                    transport.Open(entry);
                    return(transport);
                }
                break;
            }

#if ENABLE_PIPES
            case "win":
            {
                Transport transport = new PipeTransport();
                transport.Open(entry);
                return(transport);
            }
#endif

            // "autolaunch:" means: the first client user of the dbus library shall spawn the daemon on itself, see dbus 1.7.8 from http://dbus.freedesktop.org/releases/dbus/
            case "autolaunch":
            {
                if (OSHelpers.PlatformIsUnixoid)
                {
                    break;
                }

                string addr = Address.GetSessionBusAddressFromSharedMemory();

                if (string.IsNullOrEmpty(addr))                            // we have to launch the daemon ourselves
                {
                    string oldDir = Directory.GetCurrentDirectory();
                    // Without this, the "current" folder for the new process will be the one where the current
                    // executable resides, and as a consequence,that folder cannot be relocated/deleted unless the daemon is stopped
                    Directory.SetCurrentDirectory(Environment.GetFolderPath(Environment.SpecialFolder.System));

                    Process process = Process.Start(DBUS_DAEMON_LAUNCH_COMMAND);
                    if (process == null)
                    {
                        Directory.SetCurrentDirectory(oldDir);
                        throw new NotSupportedException("Transport method \"autolaunch:\" - cannot launch dbus daemon '" + DBUS_DAEMON_LAUNCH_COMMAND + "'");
                    }

                    // wait for daemon
                    Stopwatch stopwatch = new Stopwatch();
                    stopwatch.Start();
                    do
                    {
                        addr = Address.GetSessionBusAddressFromSharedMemory();
                        if (String.IsNullOrEmpty(addr))
                        {
                            Thread.Sleep(100);
                        }
                    } while (String.IsNullOrEmpty(addr) && stopwatch.ElapsedMilliseconds <= 5000);

                    Directory.SetCurrentDirectory(oldDir);
                }

                if (string.IsNullOrEmpty(addr))
                {
                    throw new NotSupportedException("Transport method \"autolaunch:\" - timeout during access to freshly launched dbus daemon");
                }
                return(Create(AddressEntry.Parse(addr)));
            }
            }

            throw new NotSupportedException("Transport method \"" + entry.Method + "\" not supported");
        }