def open_dbus_connection(bus='SESSION'): """Open a plain D-Bus connection :return: :class:`DBusConnection` """ bus_addr = get_bus(bus) sock = socket.socket(family=socket.AF_UNIX) sock.connect(bus_addr) sock.sendall(b'\0' + make_auth_external()) auth_parser = SASLParser() while not auth_parser.authenticated: auth_parser.feed(unwrap_read(sock.recv(1024))) if auth_parser.error: raise AuthenticationError(auth_parser.error) sock.sendall(BEGIN) conn = DBusConnection(sock) conn.parser.buf = auth_parser.buffer with DBusRouter(conn) as router: reply_body = Proxy(message_bus, router, timeout=10).Hello() conn.unique_name = reply_body[0] return conn
async def open_dbus_connection(bus='SESSION', *, enable_fds=False) -> DBusConnection: """Open a plain D-Bus connection :return: :class:`DBusConnection` """ bus_addr = get_bus(bus) sock: trio.SocketStream = await trio.open_unix_socket(bus_addr) # Authentication authr = Authenticator(enable_fds=enable_fds) for req_data in authr: await sock.send_all(req_data) authr.feed(await sock.receive_some()) await sock.send_all(BEGIN) conn = DBusConnection(sock.socket, enable_fds=enable_fds) # Say *Hello* to the message bus - this must be the first message, and the # reply gives us our unique name. async with conn.router() as router: reply = await router.send_and_get_reply(message_bus.Hello()) conn.unique_name = reply.body[0] return conn
async def open_dbus_connection(bus='SESSION'): """Open a plain D-Bus connection :return: :class:`DBusConnection` """ bus_addr = get_bus(bus) reader, writer = await asyncio.open_unix_connection(bus_addr) # Authentication flow authr = Authenticator() for req_data in authr: writer.write(req_data) await writer.drain() b = await reader.read(1024) if not b: raise EOFError("Socket closed before authentication") authr.feed(b) writer.write(BEGIN) await writer.drain() # Authentication finished conn = DBusConnection(reader, writer) # Say *Hello* to the message bus - this must be the first message, and the # reply gives us our unique name. async with DBusRouter(conn) as router: reply_body = await asyncio.wait_for( Proxy(message_bus, router).Hello(), 10) conn.unique_name = reply_body[0] return conn
async def open_dbus_connection(bus='SESSION'): """Open a plain D-Bus connection :return: :class:`DBusConnection` """ bus_addr = get_bus(bus) reader, writer = await asyncio.open_unix_connection(bus_addr) # Authentication flow writer.write(b'\0' + make_auth_external()) await writer.drain() auth_parser = SASLParser() while not auth_parser.authenticated: b = await reader.read(1024) if not b: raise EOFError("Socket closed before authentication") auth_parser.feed(b) if auth_parser.error: raise AuthenticationError(auth_parser.error) writer.write(BEGIN) await writer.drain() # Authentication finished conn = DBusConnection(reader, writer) conn.parser.add_data(auth_parser.buffer) # Say *Hello* to the message bus - this must be the first message, and the # reply gives us our unique name. async with DBusRouter(conn) as router: reply_body = await asyncio.wait_for( Proxy(message_bus, router).Hello(), 10) conn.unique_name = reply_body[0] return conn
async def open_dbus_connection(bus='SESSION') -> DBusConnection: """Open a plain D-Bus connection :return: :class:`DBusConnection` """ bus_addr = get_bus(bus) sock: trio.SocketStream = await trio.open_unix_socket(bus_addr) # Authentication flow await sock.send_all(b'\0' + make_auth_external()) auth_parser = SASLParser() while not auth_parser.authenticated: b = await sock.receive_some() auth_parser.feed(b) if auth_parser.error: raise AuthenticationError(auth_parser.error) await sock.send_all(BEGIN) # Authentication finished conn = DBusConnection(sock) conn.parser.add_data(auth_parser.buffer) # Say *Hello* to the message bus - this must be the first message, and the # reply gives us our unique name. async with conn.router() as router: reply = await router.send_and_get_reply(message_bus.Hello()) conn.unique_name = reply.body[0] return conn
async def connect_and_authenticate(bus='SESSION', loop=None): if loop is None: loop = asyncio.get_event_loop() (t, p) = await loop.create_unix_connection(DBusProtocol, path=get_bus(bus)) await p.authentication bus = Proxy(message_bus, p) hello_reply = await bus.Hello() p.unique_name = hello_reply[0] return (t, p)
def connect_and_authenticate(bus='SESSION'): bus_addr = get_bus(bus) sock = socket.socket(family=socket.AF_UNIX) sock.connect(bus_addr) sock.sendall(b'\0' + make_auth_external()) auth_parser = SASLParser() while not auth_parser.authenticated: auth_parser.feed(sock.recv(1024)) if auth_parser.error: raise AuthenticationError(auth_parser.error) sock.sendall(BEGIN) conn = DBusConnection(sock) conn.parser.buf = auth_parser.buffer return conn
def open_dbus_connection(bus='SESSION') -> DBusConnection: """Connect to a D-Bus message bus""" bus_addr = get_bus(bus) sock = socket.socket(family=socket.AF_UNIX) sock.connect(bus_addr) sock.sendall(b'\0' + make_auth_external()) auth_parser = SASLParser() while not auth_parser.authenticated: auth_parser.feed(unwrap_read(sock.recv(1024))) if auth_parser.error: raise AuthenticationError(auth_parser.error) sock.sendall(BEGIN) conn = DBusConnection(sock) conn.parser.add_data(auth_parser.buffer) return conn
def open_dbus_connection(bus='SESSION', enable_fds=False, auth_timeout=1.): """Open a plain D-Bus connection D-Bus has an authentication step before sending or receiving messages. This takes < 1 ms in normal operation, but there is a timeout so that client code won't get stuck if the server doesn't reply. *auth_timeout* configures this timeout in seconds. :return: :class:`DBusConnection` """ bus_addr = get_bus(bus) sock = prep_socket(bus_addr, enable_fds, timeout=auth_timeout) conn = DBusConnection(sock, enable_fds) with DBusRouter(conn) as router: reply_body = Proxy(message_bus, router, timeout=10).Hello() conn.unique_name = reply_body[0] return conn
def open_dbus_connection( bus='SESSION', enable_fds=False, auth_timeout=1., ) -> DBusConnection: """Connect to a D-Bus message bus Pass ``enable_fds=True`` to allow sending & receiving file descriptors. An error will be raised if the bus does not allow this. For simplicity, it's advisable to leave this disabled unless you need it. D-Bus has an authentication step before sending or receiving messages. This takes < 1 ms in normal operation, but there is a timeout so that client code won't get stuck if the server doesn't reply. *auth_timeout* configures this timeout in seconds. """ bus_addr = get_bus(bus) sock = prep_socket(bus_addr, enable_fds, timeout=auth_timeout) conn = DBusConnection(sock, enable_fds) return conn
async def open_dbus_connection(bus='SESSION'): bus_addr = get_bus(bus) stream = IOStream(socket.socket(family=socket.AF_UNIX)) await stream.connect(bus_addr) await stream.write(b'\0' + make_auth_external()) auth_parser = SASLParser() while not auth_parser.authenticated: auth_parser.feed(await stream.read_bytes(1024, partial=True)) if auth_parser.error: raise AuthenticationError(auth_parser.error) await stream.write(BEGIN) conn = DBusConnection(stream) with DBusRouter(conn) as router: reply_body = await wait_for(Proxy(message_bus, router).Hello(), 10) conn.unique_name = reply_body[0] return conn
def connect_and_authenticate(bus='SESSION'): bus_addr = get_bus(bus) conn = DBusConnection(bus_addr) yield conn.authentication conn.unique_name = (yield Proxy(message_bus, conn).Hello())[0] return conn