peer.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright (C)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2025-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. package zerotier
  14. // #include "../../serviceiocore/GoGlue.h"
  15. import "C"
  16. import "unsafe"
  17. // Peer is another ZeroTier node
  18. type Peer struct {
  19. Address Address `json:"address"`
  20. Identity *Identity `json:"identity"`
  21. Fingerprint *Fingerprint `json:"fingerprint"`
  22. Version [3]int `json:"version"`
  23. Latency int `json:"latency"`
  24. Root bool `json:"root"`
  25. Paths []Path `json:"paths,omitempty"`
  26. Locator *Locator `json:"locator,omitempty"`
  27. }
  28. func newPeerFromCPeer(cp *C.ZT_Peer) (p *Peer, err error) {
  29. p = new(Peer)
  30. p.Address = Address(cp.address)
  31. p.Identity, err = newIdentityFromCIdentity(cp.identity)
  32. if err != nil {
  33. return
  34. }
  35. p.Fingerprint = newFingerprintFromCFingerprint(cp.fingerprint)
  36. p.Version[0] = int(cp.versionMajor)
  37. p.Version[1] = int(cp.versionMinor)
  38. p.Version[2] = int(cp.versionRev)
  39. p.Latency = int(cp.latency)
  40. p.Root = cp.root != 0
  41. p.Paths = make([]Path, int(cp.pathCount))
  42. for i := range p.Paths {
  43. p.Paths[i].setFromCPath((*C.ZT_Path)(unsafe.Pointer(uintptr(unsafe.Pointer(cp.paths)) + (uintptr(C.sizeof_ZT_Path) * uintptr(i)))))
  44. }
  45. p.Locator, err = NewLocatorFromBytes(C.GoBytes(unsafe.Pointer(cp.locator), C.int(cp.locatorSize)))
  46. return
  47. }