aboutsummaryrefslogtreecommitdiff
path: root/netaddr.go
blob: a6316891e85d0f451db6979d5dba79268a09b777 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package salticidae

// #include <stdlib.h>
// #include "salticidae/netaddr.h"
import "C"

type NetAddr = *C.netaddr_t
type NetAddrArray = *C.netaddr_array_t

func NewAddrFromIPPortString(addr string) (res NetAddr) {
    c_str := C.CString(addr)
    res = C.netaddr_new_from_sipport(c_str)
    C.free(rawptr_t(c_str))
    return
}

func (self NetAddr) Free() { C.netaddr_free(self) }

func (self NetAddr) IsEq(other NetAddr) bool { return bool(C.netaddr_is_eq(self, other)) }

func (self NetAddr) IsNull() bool { return bool(C.netaddr_is_null(self)) }

func (self NetAddr) GetIP() uint32 { return uint32(C.netaddr_get_ip(self)) }

func (self NetAddr) GetPort() uint16 { return uint16(C.netaddr_get_port(self)) }

func NewAddrArrayFromAddrs(arr []NetAddr) NetAddrArray {
    size := len(arr)
    if size > 0 {
        base := (**C.netaddr_t)(&arr[0])
        return C.netaddr_array_new_from_addrs(base, C.size_t(size))
    } else {
        return C.netaddr_array_new()
    }
}