2024-09-04 01:20:12 +09:00
|
|
|
package xcb
|
|
|
|
|
|
|
|
//#include <stdlib.h>
|
|
|
|
//#include <xcb/xcb.h>
|
|
|
|
//#cgo linux LDFLAGS: -lxcb
|
|
|
|
import "C"
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
2024-09-22 00:29:36 +09:00
|
|
|
type ConnectionError struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ConnectionError) Error() string {
|
|
|
|
return e.err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ConnectionError) Unwrap() error {
|
|
|
|
return e.err
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrChangeHosts = errors.New("xcb_change_hosts() failed")
|
|
|
|
)
|
|
|
|
|
2024-09-04 01:20:12 +09:00
|
|
|
func ChangeHosts(mode, family C.uint8_t, address string) error {
|
2024-10-12 01:51:06 +09:00
|
|
|
c := C.xcb_connect(nil, nil)
|
2024-09-04 01:20:12 +09:00
|
|
|
defer C.xcb_disconnect(c)
|
|
|
|
|
|
|
|
if err := xcbHandleConnectionError(c); err != nil {
|
2024-09-22 00:29:36 +09:00
|
|
|
return &ConnectionError{err}
|
2024-09-04 01:20:12 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
addr := C.CString(address)
|
|
|
|
cookie := C.xcb_change_hosts_checked(c, mode, family, C.ushort(len(address)), (*C.uchar)(unsafe.Pointer(addr)))
|
|
|
|
C.free(unsafe.Pointer(addr))
|
|
|
|
|
|
|
|
if err := xcbHandleConnectionError(c); err != nil {
|
2024-09-22 00:29:36 +09:00
|
|
|
return &ConnectionError{err}
|
2024-09-04 01:20:12 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
e := C.xcb_request_check(c, cookie)
|
|
|
|
if e != nil {
|
|
|
|
defer C.free(unsafe.Pointer(e))
|
2024-09-22 00:29:36 +09:00
|
|
|
return ErrChangeHosts
|
2024-09-04 01:20:12 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|