aboutsummaryrefslogtreecommitdiff
path: root/netaddr.go
diff options
context:
space:
mode:
Diffstat (limited to 'netaddr.go')
-rw-r--r--netaddr.go94
1 files changed, 48 insertions, 46 deletions
diff --git a/netaddr.go b/netaddr.go
index cc69313..096be06 100644
--- a/netaddr.go
+++ b/netaddr.go
@@ -8,9 +8,10 @@ import "runtime"
// The C pointer type for a NetAddr object
type CNetAddr = *C.netaddr_t
type netAddr struct {
- inner CNetAddr
- autoFree bool
+ inner CNetAddr
+ autoFree bool
}
+
// Network address object.
type NetAddr = *netAddr
@@ -20,30 +21,30 @@ type NetAddr = *netAddr
// deallocated when the go object is finalized by GC. This applies to all other
// "FromC" functions.
func NetAddrFromC(ptr CNetAddr) NetAddr {
- return &netAddr{ inner: ptr }
+ return &netAddr{inner: ptr}
}
func netAddrSetFinalizer(res NetAddr, autoFree bool) {
- res.autoFree = autoFree
- if res != nil && autoFree {
- runtime.SetFinalizer(res, func(self NetAddr) { self.Free() })
- }
+ res.autoFree = autoFree
+ if res != nil && autoFree {
+ runtime.SetFinalizer(res, func(self NetAddr) { self.Free() })
+ }
}
// Create NetAddr from a TCP socket format string (e.g. 127.0.0.1:8888).
func NewNetAddrFromIPPortString(addr string, autoFree bool, err *Error) (res NetAddr) {
- c_str := C.CString(addr)
- res = &netAddr{ inner: C.netaddr_new_from_sipport(c_str, err) }
- C.free(rawptr_t(c_str))
- netAddrSetFinalizer(res, autoFree)
- return
+ c_str := C.CString(addr)
+ res = &netAddr{inner: C.netaddr_new_from_sipport(c_str, err)}
+ C.free(rawptr_t(c_str))
+ netAddrSetFinalizer(res, autoFree)
+ return
}
func (self NetAddr) Free() {
- C.netaddr_free(self.inner)
- if self.autoFree {
- runtime.SetFinalizer(self, nil)
- }
+ C.netaddr_free(self.inner)
+ if self.autoFree {
+ runtime.SetFinalizer(self, nil)
+ }
}
// Check if two addresses are the same.
@@ -62,54 +63,55 @@ func (self NetAddr) GetPort() uint16 { return uint16(C.netaddr_get_port(self.inn
// returned (or passed as a callback parameter) by other salticidae methods
// (such like MsgNetwork/PeerNetwork), unless those method return a moved object.
func (self NetAddr) Copy(autoFree bool) NetAddr {
- res := &netAddr{ inner: C.netaddr_copy(self.inner) }
- netAddrSetFinalizer(res, autoFree)
- return res
+ res := &netAddr{inner: C.netaddr_copy(self.inner)}
+ netAddrSetFinalizer(res, autoFree)
+ return res
}
// The C pointer type for a NetAddrArray object.
type CNetAddrArray = *C.netaddr_array_t
type netAddrArray struct {
- inner CNetAddrArray
- autoFree bool
+ inner CNetAddrArray
+ autoFree bool
}
+
// An array of network address.
type NetAddrArray = *netAddrArray
func NetAddrArrayFromC(ptr CNetAddrArray) NetAddrArray {
- return &netAddrArray{ inner: ptr }
+ return &netAddrArray{inner: ptr}
}
func netAddrArraySetFinalizer(res NetAddrArray, autoFree bool) {
- res.autoFree = autoFree
- if res != nil && autoFree {
- runtime.SetFinalizer(res, func(self NetAddrArray) { self.Free() })
- }
+ res.autoFree = autoFree
+ if res != nil && autoFree {
+ runtime.SetFinalizer(res, func(self NetAddrArray) { self.Free() })
+ }
}
// Convert a Go slice of net addresses to NetAddrArray.
func NewNetAddrArrayFromAddrs(arr []NetAddr, autoFree bool) (res NetAddrArray) {
- size := len(arr)
- _arr := make([]CNetAddr, size)
- for i, v := range arr {
- _arr[i] = v.inner
- }
- if size > 0 {
- // FIXME: here we assume struct of a single pointer has the same memory
- // footprint the pointer
- base := (**C.netaddr_t)(rawptr_t(&_arr[0]))
- res = NetAddrArrayFromC(C.netaddr_array_new_from_addrs(base, C.size_t(size)))
- } else {
- res = NetAddrArrayFromC(C.netaddr_array_new())
- }
- runtime.KeepAlive(_arr)
- netAddrArraySetFinalizer(res, autoFree)
- return
+ size := len(arr)
+ _arr := make([]CNetAddr, size)
+ for i, v := range arr {
+ _arr[i] = v.inner
+ }
+ if size > 0 {
+ // FIXME: here we assume struct of a single pointer has the same memory
+ // footprint the pointer
+ base := (**C.netaddr_t)(rawptr_t(&_arr[0]))
+ res = NetAddrArrayFromC(C.netaddr_array_new_from_addrs(base, C.size_t(size)))
+ } else {
+ res = NetAddrArrayFromC(C.netaddr_array_new())
+ }
+ runtime.KeepAlive(_arr)
+ netAddrArraySetFinalizer(res, autoFree)
+ return
}
func (self NetAddrArray) Free() {
- C.netaddr_array_free(self.inner)
- if self.autoFree {
- runtime.SetFinalizer(self, nil)
- }
+ C.netaddr_array_free(self.inner)
+ if self.autoFree {
+ runtime.SetFinalizer(self, nil)
+ }
}