Selaa lähdekoodia

Some Go reorg.

Adam Ierymenko 5 vuotta sitten
vanhempi
commit
65ef40b091
3 muutettua tiedostoa jossa 51 lisäystä ja 38 poistoa
  1. 36 37
      pkg/zerotier/identity.go
  2. 4 1
      pkg/zerotier/locator.go
  3. 11 0
      pkg/zerotier/misc.go

+ 36 - 37
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.
 // NewIdentity generates a new identity of the selected type.
 func NewIdentity(identityType int) (*Identity, error) {
 func NewIdentity(identityType int) (*Identity, error) {
 	var cid unsafe.Pointer
 	var cid unsafe.Pointer
@@ -168,6 +131,42 @@ func NewIdentityFromString(s string) (*Identity, error) {
 	return id, nil
 	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.
 // Address returns this identity's address.
 func (id *Identity) Address() Address { return id.address }
 func (id *Identity) Address() Address { return id.address }
 
 

+ 4 - 1
pkg/zerotier/locator.go

@@ -129,7 +129,10 @@ func (loc *Locator) UnmarshalJSON(j []byte) error {
 
 
 func locatorFinalizer(obj interface{}) {
 func locatorFinalizer(obj interface{}) {
 	if obj != nil {
 	if obj != nil {
-		C.ZT_Locator_delete(obj.(Locator).cl)
+		cl := obj.(Locator).cl
+		if cl != nil {
+			C.ZT_Locator_delete(cl)
+		}
 	}
 	}
 }
 }
 
 

+ 11 - 0
pkg/zerotier/misc.go

@@ -289,3 +289,14 @@ func cStrCopy(dest unsafe.Pointer, destSize int, src string) {
 	}
 	}
 	*((*byte)(dp)) = 0
 	*((*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}
+	}
+}