mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-06-05 20:13:44 +02:00
Some Go reorg.
This commit is contained in:
parent
15f5125c8c
commit
65ef40b091
3 changed files with 51 additions and 38 deletions
|
@ -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 }
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue