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'): """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
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
class DBusProtocol(asyncio.Protocol): def __init__(self): self.auth_parser = SASLParser() self.parser = Parser() self.router = Router(asyncio.Future) self.authentication = asyncio.Future() self.unique_name = None def connection_made(self, transport): self.transport = transport self.transport.write(b'\0' + make_auth_external()) def _authenticated(self): self.transport.write(BEGIN) self.authentication.set_result(True) self.data_received = self.data_received_post_auth self.data_received(self.auth_parser.buffer) def data_received(self, data): self.auth_parser.feed(data) if self.auth_parser.authenticated: self._authenticated() elif self.auth_parser.error: self.authentication.set_exception( AuthenticationError(self.auth_parser.error)) def data_received_post_auth(self, data): for msg in self.parser.feed(data): self.router.incoming(msg) def send_message(self, message): if not self.authentication.done(): raise RuntimeError( "Wait for authentication before sending messages") future = self.router.outgoing(message) data = message.serialise() self.transport.write(data) return future async def send_and_get_reply(self, message): if message.header.message_type != MessageType.method_call: raise TypeError("Only method call messages have replies") return await self.send_message(message)
class DBusConnection: def __init__(self, bus_addr): self.auth_parser = SASLParser() self.parser = Parser() self.router = Router(Future) self.authentication = Future() self.unique_name = None self._sock = socket.socket(family=socket.AF_UNIX) self.stream = IOStream(self._sock, read_chunk_size=4096) def connected(): self.stream.write(b'\0' + make_auth_external()) self.stream.connect(bus_addr, connected) self.stream.read_until_close(streaming_callback=self.data_received) def _authenticated(self): self.stream.write(BEGIN) self.authentication.set_result(True) self.data_received_post_auth(self.auth_parser.buffer) def data_received(self, data): if self.authentication.done(): return self.data_received_post_auth(data) self.auth_parser.feed(data) if self.auth_parser.authenticated: self._authenticated() elif self.auth_parser.error: self.authentication.set_exception(AuthenticationError(self.auth_parser.error)) def data_received_post_auth(self, data): for msg in self.parser.feed(data): self.router.incoming(msg) def send_message(self, message): if not self.authentication.done(): raise RuntimeError("Wait for authentication before sending messages") future = self.router.outgoing(message) data = message.serialise() self.stream.write(data) return future
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