Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add ConnectUnix and DialUnix #399

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions conn_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package dbus

import (
"net"
"os"
)

Expand All @@ -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
}
8 changes: 8 additions & 0 deletions transport_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down