From 38a3e6af03e638f82f72e5659588eaf17c78aafa Mon Sep 17 00:00:00 2001 From: Ophestra Date: Mon, 17 Feb 2025 19:07:53 +0900 Subject: system: make xcb internal This package is hauntingly ugly. Move this to internal until it is removed or replaced. Signed-off-by: Ophestra --- system/internal/xcb/c.go | 124 ++++++++++++++++++++++++++++++++++++++++++ system/internal/xcb/export.go | 22 ++++++++ system/xhost.go | 2 +- xcb/c.go | 124 ------------------------------------------ xcb/export.go | 22 -------- 5 files changed, 147 insertions(+), 147 deletions(-) create mode 100644 system/internal/xcb/c.go create mode 100644 system/internal/xcb/export.go delete mode 100644 xcb/c.go delete mode 100644 xcb/export.go diff --git a/system/internal/xcb/c.go b/system/internal/xcb/c.go new file mode 100644 index 00000000..f2703f94 --- /dev/null +++ b/system/internal/xcb/c.go @@ -0,0 +1,124 @@ +package xcb + +import ( + "runtime" + "unsafe" +) + +/* +#cgo linux pkg-config: --static xcb + +#include +#include + +static int _go_xcb_change_hosts_checked(xcb_connection_t *c, uint8_t mode, uint8_t family, uint16_t address_len, const uint8_t *address) { + xcb_void_cookie_t cookie = xcb_change_hosts_checked(c, mode, family, address_len, address); + free((void *)address); + + int errno = xcb_connection_has_error(c); + if (errno != 0) + return errno; + + xcb_generic_error_t *e = xcb_request_check(c, cookie); + if (e != NULL) { + // don't want to deal with xcb errors + free((void *)e); + return -1; + } + + return 0; +} +*/ +import "C" + +const ( + HostModeInsert = C.XCB_HOST_MODE_INSERT + HostModeDelete = C.XCB_HOST_MODE_DELETE + + FamilyInternet = C.XCB_FAMILY_INTERNET + FamilyDecnet = C.XCB_FAMILY_DECNET + FamilyChaos = C.XCB_FAMILY_CHAOS + FamilyServerInterpreted = C.XCB_FAMILY_SERVER_INTERPRETED + FamilyInternet6 = C.XCB_FAMILY_INTERNET_6 +) + +type ( + HostMode = C.xcb_host_mode_t + Family = C.xcb_family_t +) + +func (conn *connection) changeHostsChecked(mode HostMode, family Family, address string) error { + errno := C._go_xcb_change_hosts_checked( + conn.c, + C.uint8_t(mode), + C.uint8_t(family), + C.uint16_t(len(address)), + (*C.uint8_t)(unsafe.Pointer(C.CString(address))), + ) + switch errno { + case 0: + return nil + case -1: + return ErrChangeHosts + default: + return &ConnectionError{errno} + } +} + +type connection struct{ c *C.xcb_connection_t } + +func connect() (*connection, error) { + conn := newConnection(C.xcb_connect(nil, nil)) + return conn, conn.hasError() +} + +func newConnection(c *C.xcb_connection_t) *connection { + conn := &connection{c} + runtime.SetFinalizer(conn, (*connection).disconnect) + return conn +} + +const ( + ConnError = C.XCB_CONN_ERROR + ConnClosedExtNotSupported = C.XCB_CONN_CLOSED_EXT_NOTSUPPORTED + ConnClosedMemInsufficient = C.XCB_CONN_CLOSED_MEM_INSUFFICIENT + ConnClosedReqLenExceed = C.XCB_CONN_CLOSED_REQ_LEN_EXCEED + ConnClosedParseErr = C.XCB_CONN_CLOSED_PARSE_ERR + ConnClosedInvalidScreen = C.XCB_CONN_CLOSED_INVALID_SCREEN +) + +type ConnectionError struct{ errno C.int } + +func (ce *ConnectionError) Error() string { + switch ce.errno { + case ConnError: + return "connection error" + case ConnClosedExtNotSupported: + return "extension not supported" + case ConnClosedMemInsufficient: + return "memory not available" + case ConnClosedReqLenExceed: + return "request length exceeded" + case ConnClosedParseErr: + return "invalid display string" + case ConnClosedInvalidScreen: + return "server has no screen matching display" + default: + return "generic X11 failure" + } +} + +func (conn *connection) hasError() error { + errno := C.xcb_connection_has_error(conn.c) + if errno == 0 { + return nil + } + return &ConnectionError{errno} +} + +func (conn *connection) disconnect() { + C.xcb_disconnect(conn.c) + + // no need for a finalizer anymore + runtime.SetFinalizer(conn, nil) +} diff --git a/system/internal/xcb/export.go b/system/internal/xcb/export.go new file mode 100644 index 00000000..1ed9997f --- /dev/null +++ b/system/internal/xcb/export.go @@ -0,0 +1,22 @@ +// Package xcb implements X11 ChangeHosts via libxcb. +package xcb + +import ( + "errors" +) + +var ErrChangeHosts = errors.New("xcb_change_hosts() failed") + +func ChangeHosts(mode HostMode, family Family, address string) error { + var conn *connection + + if c, err := connect(); err != nil { + c.disconnect() + return err + } else { + defer c.disconnect() + conn = c + } + + return conn.changeHostsChecked(mode, family, address) +} diff --git a/system/xhost.go b/system/xhost.go index bbc0a914..a20e1991 100644 --- a/system/xhost.go +++ b/system/xhost.go @@ -3,7 +3,7 @@ package system import ( "fmt" - "git.gensokyo.uk/security/fortify/xcb" + "git.gensokyo.uk/security/fortify/system/internal/xcb" ) // ChangeHosts appends an X11 ChangeHosts command Op. diff --git a/xcb/c.go b/xcb/c.go deleted file mode 100644 index f2703f94..00000000 --- a/xcb/c.go +++ /dev/null @@ -1,124 +0,0 @@ -package xcb - -import ( - "runtime" - "unsafe" -) - -/* -#cgo linux pkg-config: --static xcb - -#include -#include - -static int _go_xcb_change_hosts_checked(xcb_connection_t *c, uint8_t mode, uint8_t family, uint16_t address_len, const uint8_t *address) { - xcb_void_cookie_t cookie = xcb_change_hosts_checked(c, mode, family, address_len, address); - free((void *)address); - - int errno = xcb_connection_has_error(c); - if (errno != 0) - return errno; - - xcb_generic_error_t *e = xcb_request_check(c, cookie); - if (e != NULL) { - // don't want to deal with xcb errors - free((void *)e); - return -1; - } - - return 0; -} -*/ -import "C" - -const ( - HostModeInsert = C.XCB_HOST_MODE_INSERT - HostModeDelete = C.XCB_HOST_MODE_DELETE - - FamilyInternet = C.XCB_FAMILY_INTERNET - FamilyDecnet = C.XCB_FAMILY_DECNET - FamilyChaos = C.XCB_FAMILY_CHAOS - FamilyServerInterpreted = C.XCB_FAMILY_SERVER_INTERPRETED - FamilyInternet6 = C.XCB_FAMILY_INTERNET_6 -) - -type ( - HostMode = C.xcb_host_mode_t - Family = C.xcb_family_t -) - -func (conn *connection) changeHostsChecked(mode HostMode, family Family, address string) error { - errno := C._go_xcb_change_hosts_checked( - conn.c, - C.uint8_t(mode), - C.uint8_t(family), - C.uint16_t(len(address)), - (*C.uint8_t)(unsafe.Pointer(C.CString(address))), - ) - switch errno { - case 0: - return nil - case -1: - return ErrChangeHosts - default: - return &ConnectionError{errno} - } -} - -type connection struct{ c *C.xcb_connection_t } - -func connect() (*connection, error) { - conn := newConnection(C.xcb_connect(nil, nil)) - return conn, conn.hasError() -} - -func newConnection(c *C.xcb_connection_t) *connection { - conn := &connection{c} - runtime.SetFinalizer(conn, (*connection).disconnect) - return conn -} - -const ( - ConnError = C.XCB_CONN_ERROR - ConnClosedExtNotSupported = C.XCB_CONN_CLOSED_EXT_NOTSUPPORTED - ConnClosedMemInsufficient = C.XCB_CONN_CLOSED_MEM_INSUFFICIENT - ConnClosedReqLenExceed = C.XCB_CONN_CLOSED_REQ_LEN_EXCEED - ConnClosedParseErr = C.XCB_CONN_CLOSED_PARSE_ERR - ConnClosedInvalidScreen = C.XCB_CONN_CLOSED_INVALID_SCREEN -) - -type ConnectionError struct{ errno C.int } - -func (ce *ConnectionError) Error() string { - switch ce.errno { - case ConnError: - return "connection error" - case ConnClosedExtNotSupported: - return "extension not supported" - case ConnClosedMemInsufficient: - return "memory not available" - case ConnClosedReqLenExceed: - return "request length exceeded" - case ConnClosedParseErr: - return "invalid display string" - case ConnClosedInvalidScreen: - return "server has no screen matching display" - default: - return "generic X11 failure" - } -} - -func (conn *connection) hasError() error { - errno := C.xcb_connection_has_error(conn.c) - if errno == 0 { - return nil - } - return &ConnectionError{errno} -} - -func (conn *connection) disconnect() { - C.xcb_disconnect(conn.c) - - // no need for a finalizer anymore - runtime.SetFinalizer(conn, nil) -} diff --git a/xcb/export.go b/xcb/export.go deleted file mode 100644 index 1ed9997f..00000000 --- a/xcb/export.go +++ /dev/null @@ -1,22 +0,0 @@ -// Package xcb implements X11 ChangeHosts via libxcb. -package xcb - -import ( - "errors" -) - -var ErrChangeHosts = errors.New("xcb_change_hosts() failed") - -func ChangeHosts(mode HostMode, family Family, address string) error { - var conn *connection - - if c, err := connect(); err != nil { - c.disconnect() - return err - } else { - defer c.disconnect() - conn = c - } - - return conn.changeHostsChecked(mode, family, address) -} -- cgit v1.3.1