File: tcp_conn.go

package info (click to toggle)
golang-dbus 5.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports, bookworm-proposed-updates, forky, sid, trixie
  • size: 592 kB
  • sloc: makefile: 2
file content (57 lines) | stat: -rw-r--r-- 1,458 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main

import (
	"fmt"
	"net"
	"os"

	"github.com/godbus/dbus/v5"
)

// In order to enable TCP connections add the following configuration
// file to /etc/dbus-1/system.d or /etc/dbus-1/session.d, the location
// depends on your OS (you may update your /etc/dbus-1/session.conf instead):
//
// <!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
//  "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
// <busconfig>
//     <listen>tcp:host=localhost,bind=*,port=12345,family=ipv4</listen>
//     <listen>unix:tmpdir=/tmp</listen>
//     <auth>ANONYMOUS</auth>
//     <allow_anonymous/>
// </busconfig>
//
// If you're using systemd you may also need to reconfigure dbus.socket,
// say put this into /etc/systemd/system/dbus.socket.d/overrides.conf:
//
// [Socket]
// ListenStream=/var/run/dbus/system_bus_socket
// ListenStream=12345
//
// Reboot.

func main() {
	if len(os.Args) != 2 {
		fmt.Fprintf(os.Stderr, "usage: %s DBUS_TCP_ADDRESS\n", os.Args[0])
		os.Exit(1)
	}
	if err := run(os.Args[1]); err != nil {
		fmt.Fprintf(os.Stderr, "error: %s\n", err)
		os.Exit(2)
	}
	fmt.Println("ok")
}

func run(addr string) error {
	host, port, err := net.SplitHostPort(addr)
	if err != nil {
		return err
	}
	conn, err := dbus.Connect("tcp:host="+host+",port="+port, dbus.WithAuth(dbus.AuthAnonymous()))
	if err != nil {
		return err
	}
	defer conn.Close()
	fmt.Println("connected to D-Bus over TCP")
	return nil
}