diff --git a/alternative.go b/alternative.go new file mode 100644 index 0000000..b31f17b --- /dev/null +++ b/alternative.go @@ -0,0 +1,32 @@ +package avahi + +// #include +// #include +import "C" +import "unsafe" + +func AlternativeHostname(hostname string) (string, error) { + chostname := C.CString(hostname) + defer C.free(unsafe.Pointer(chostname)) + + alt := C.avahi_alternative_host_name(chostname) + if alt == nil { + return "", ErrNoMemory + } + + defer C.free(unsafe.Pointer(alt)) + return C.GoString(alt), nil +} + +func AlternativeServiceName(name string) (string, error) { + cname := C.CString(name) + defer C.free(unsafe.Pointer(cname)) + + alt := C.avahi_alternative_service_name(cname) + if alt == nil { + return "", ErrNoMemory + } + + defer C.free(unsafe.Pointer(alt)) + return C.GoString(alt), nil +} diff --git a/alternative_test.go b/alternative_test.go new file mode 100644 index 0000000..cd5dce0 --- /dev/null +++ b/alternative_test.go @@ -0,0 +1,43 @@ +//go:build linux || freebsd + +package avahi + +import "testing" + +func TestAlternativeHostname(t *testing.T) { + hostname := "test-hostname" + + for i := 0; i < 10; i++ { + alt, err := AlternativeHostname(hostname) + if err != nil { + t.Fatalf("AlternativeHostname failed: %v", err) + } + + t.Logf("Alternative hostname: %s", alt) + + if alt == hostname { + t.Fatalf("AlternativeHostname did not return an alternative name") + } + + hostname = alt + } +} + +func TestAlternativeServiceName(t *testing.T) { + serviceName := "test-service" + + for i := 0; i < 10; i++ { + alt, err := AlternativeServiceName(serviceName) + if err != nil { + t.Fatalf("AlternativeServiceName failed: %v", err) + } + + t.Logf("Alternative service name: %s", alt) + + if alt == serviceName { + t.Fatalf("AlternativeServiceName did not return an alternative name") + } + + serviceName = alt + } +}