diff --git a/pkg/zerotier/identity.go b/pkg/zerotier/identity.go index f07d7abe4..08a541866 100644 --- a/pkg/zerotier/identity.go +++ b/pkg/zerotier/identity.go @@ -52,43 +52,6 @@ func identityFinalizer(obj interface{}) { } } -func newIdentityFromCIdentity(cid unsafe.Pointer) (*Identity, error) { - if cid == nil { - return nil, ErrInvalidParameter - } - - var idStrBuf [4096]byte - idStr := C.ZT_Identity_toString(cid, (*C.char)(unsafe.Pointer(&idStrBuf[0])), 4096, 1) - if uintptr(unsafe.Pointer(idStr)) == 0 { - return nil, ErrInternal - } - - id, err := NewIdentityFromString(C.GoString(idStr)) - if err != nil { - return nil, err - } - - runtime.SetFinalizer(id, identityFinalizer) - - return id, nil -} - -// initCIdentityPtr returns a pointer to the core ZT_Identity instance or nil/0 on error. -func (id *Identity) cIdentity() unsafe.Pointer { - if id.cid == nil { - str := []byte(id.PrivateKeyString()) - if len(str) == 0 { - str = []byte(id.String()) - } - if len(str) == 0 { - return nil - } - str = append(str, byte(0)) - id.cid = C.ZT_Identity_fromString((*C.char)(unsafe.Pointer(&str[0]))) - } - return id.cid -} - // NewIdentity generates a new identity of the selected type. func NewIdentity(identityType int) (*Identity, error) { var cid unsafe.Pointer @@ -168,6 +131,42 @@ func NewIdentityFromString(s string) (*Identity, error) { return id, nil } +func newIdentityFromCIdentity(cid unsafe.Pointer) (*Identity, error) { + if cid == nil { + return nil, ErrInvalidParameter + } + + var idStrBuf [4096]byte + idStr := C.ZT_Identity_toString(cid, (*C.char)(unsafe.Pointer(&idStrBuf[0])), 4096, 1) + if uintptr(unsafe.Pointer(idStr)) == 0 { + return nil, ErrInternal + } + + id, err := NewIdentityFromString(C.GoString(idStr)) + if err != nil { + return nil, err + } + + runtime.SetFinalizer(id, identityFinalizer) + + return id, nil +} + +func (id *Identity) cIdentity() unsafe.Pointer { + if id.cid == nil { + str := []byte(id.PrivateKeyString()) + if len(str) == 0 { + str = []byte(id.String()) + } + if len(str) == 0 { + return nil + } + str = append(str, byte(0)) + id.cid = C.ZT_Identity_fromString((*C.char)(unsafe.Pointer(&str[0]))) + } + return id.cid +} + // Address returns this identity's address. func (id *Identity) Address() Address { return id.address } diff --git a/pkg/zerotier/locator.go b/pkg/zerotier/locator.go index 0926066b8..14a28a27b 100644 --- a/pkg/zerotier/locator.go +++ b/pkg/zerotier/locator.go @@ -129,7 +129,10 @@ func (loc *Locator) UnmarshalJSON(j []byte) error { func locatorFinalizer(obj interface{}) { if obj != nil { - C.ZT_Locator_delete(obj.(Locator).cl) + cl := obj.(Locator).cl + if cl != nil { + C.ZT_Locator_delete(cl) + } } } diff --git a/pkg/zerotier/misc.go b/pkg/zerotier/misc.go index 33222b397..d4babb982 100644 --- a/pkg/zerotier/misc.go +++ b/pkg/zerotier/misc.go @@ -289,3 +289,14 @@ func cStrCopy(dest unsafe.Pointer, destSize int, src string) { } *((*byte)(dp)) = 0 } + +// cStr returns an always zero-terminated byte array. +// It's like C.CString but doesn't do a malloc or need a free. +func cStr(s string) []byte { + sb := []byte(s) + if len(sb) > 0 { + return append(append(make([]byte, 0, len(sb)+1), sb...), byte(0)) + } else { + return []byte{0} + } +}