From aa6d0a06823c4df91ba3481f3165282b3cdadaeb Mon Sep 17 00:00:00 2001 From: anonymix007 <48598263+anonymix007@users.noreply.github.com> Date: Thu, 12 Dec 2024 14:34:36 +0300 Subject: [PATCH 1/2] transport_unix: add newUnixTransportFromConn --- transport_unix.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/transport_unix.go b/transport_unix.go index 6840387..146ccd4 100644 --- a/transport_unix.go +++ b/transport_unix.go @@ -55,6 +55,14 @@ type unixTransport struct { hasUnixFDs bool } +func newUnixTransportFromConn(conn *net.UnixConn) (transport, error) { + t := new(unixTransport) + t.UnixConn = conn + t.hasUnixFDs = true + + return t, nil +} + func newUnixTransport(keys string) (transport, error) { var err error From b1d708f5cb33e96bfb45b80260937c11b3319697 Mon Sep 17 00:00:00 2001 From: anonymix007 <48598263+anonymix007@users.noreply.github.com> Date: Thu, 12 Dec 2024 14:35:02 +0300 Subject: [PATCH 2/2] conn_unix: add DialUnix and ConnectUnix These can be used to create D-Bus connections from already existing *net.UnixConn --- conn_unix.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/conn_unix.go b/conn_unix.go index 1a0daa6..a982e7f 100644 --- a/conn_unix.go +++ b/conn_unix.go @@ -4,6 +4,7 @@ package dbus import ( + "net" "os" ) @@ -16,3 +17,28 @@ func getSystemBusPlatformAddress() string { } return defaultSystemBusAddress } + +// DialUnix establishes a new private connection to the message bus specified by UnixConn. +func DialUnix(conn *net.UnixConn, opts ...ConnOption) (*Conn, error) { + tr, err := newUnixTransportFromConn(conn) + if err != nil { + return nil, err + } + return newConn(tr, opts...) +} + +func ConnectUnix(uconn *net.UnixConn, opts ...ConnOption) (*Conn, error) { + conn, err := DialUnix(uconn, opts...) + if err != nil { + return nil, err + } + if err = conn.Auth(conn.auth); err != nil { + _ = conn.Close() + return nil, err + } + if err = conn.Hello(); err != nil { + _ = conn.Close() + return nil, err + } + return conn, nil +}