Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enh: support whitelist #197

Merged
merged 1 commit into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion common/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,7 @@ const (

const ReqIDKey = "taos_req_id"

const TAOS_NOTIFY_PASSVER = 0
const (
TAOS_NOTIFY_PASSVER = 0
TAOS_NOTIFY_WHITELIST_VER = 1
)
21 changes: 21 additions & 0 deletions wrapper/notify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,25 @@ func TestNotify(t *testing.T) {
case <-timeout.Done():
t.Error("wait for notify callback timeout")
}
{
notify := make(chan int64, 1)
handler := cgo.NewHandle(notify)
errCode := TaosSetNotifyCB(conn2, handler, common.TAOS_NOTIFY_WHITELIST_VER)
if errCode != 0 {
errStr := TaosErrorStr(nil)
t.Error(errCode, errStr)
}
err = exec(conn, "ALTER USER t_notify ADD HOST '192.168.1.98/0','192.168.1.98/32'")
assert.NoError(t, err)
timeout, cancel = context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
now := time.Now()
select {
case version := <-notify:
fmt.Println(time.Now().Sub(now))
t.Log(version)
case <-timeout.Done():
t.Error("wait for notify callback timeout")
}
}
}
5 changes: 5 additions & 0 deletions wrapper/notifycb.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ import (
//export NotifyCallback
func NotifyCallback(p unsafe.Pointer, ext unsafe.Pointer, notifyType C.int) {
defer func() {
// channel may be closed
recover()
}()
if int(notifyType) == common.TAOS_NOTIFY_PASSVER {
version := int32(*(*C.int32_t)(ext))
c := (*(*cgo.Handle)(p)).Value().(chan int32)
c <- version
} else if int(notifyType) == common.TAOS_NOTIFY_WHITELIST_VER {
version := int64(*(*C.int64_t)(ext))
c := (*(*cgo.Handle)(p)).Value().(chan int64)
c <- version
}
}
5 changes: 5 additions & 0 deletions wrapper/tmq.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,8 @@ func TMQCommitOffsetAsync(consumer unsafe.Pointer, topic string, vGroupID int32,
defer C.free(unsafe.Pointer(topicName))
C.tmq_commit_offset_async((*C.tmq_t)(consumer), topicName, (C.int32_t)(vGroupID), (C.int64_t)(offset), (*C.tmq_commit_cb)(C.TMQCommitOffsetCB), h.Pointer())
}

// TMQGetConnect TAOS *tmq_get_connect(tmq_t *tmq)
func TMQGetConnect(consumer unsafe.Pointer) unsafe.Pointer {
return unsafe.Pointer(C.tmq_get_connect((*C.tmq_t)(consumer)))
}
31 changes: 31 additions & 0 deletions wrapper/whitelist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package wrapper

/*
#cgo CFLAGS: -IC:/TDengine/include -I/usr/include
#cgo linux LDFLAGS: -L/usr/lib -ltaos
#cgo windows LDFLAGS: -LC:/TDengine/driver -ltaos
#cgo darwin LDFLAGS: -L/usr/local/lib -ltaos
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <taos.h>
extern void WhitelistCallback(void *param, int code, TAOS *taos, int numOfWhiteLists, uint64_t* pWhiteLists);
void taos_fetch_whitelist_a_wrapper(TAOS *taos, void *param){
return taos_fetch_whitelist_a(taos, WhitelistCallback, param);
};
*/
import "C"
import (
"unsafe"

"github.com/taosdata/driver-go/v3/wrapper/cgo"
)

const TAOS_NOTIFY_WHITELIST_VER = 1

// typedef void (*__taos_async_whitelist_fn_t)(void *param, int code, TAOS *taos, int numOfWhiteLists, uint64_t* pWhiteLists);

// TaosFetchWhitelistA DLL_EXPORT void taos_fetch_whitelist_a(TAOS *taos, __taos_async_whitelist_fn_t fp, void *param);
func TaosFetchWhitelistA(taosConnect unsafe.Pointer, caller cgo.Handle) {
C.taos_fetch_whitelist_a_wrapper(taosConnect, caller.Pointer())
}
21 changes: 21 additions & 0 deletions wrapper/whitelist_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package wrapper

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/taosdata/driver-go/v3/wrapper/cgo"
)

func TestGetWhiteList(t *testing.T) {
conn, err := TaosConnect("", "root", "taosdata", "", 0)
assert.NoError(t, err)
defer TaosClose(conn)
c := make(chan *WhitelistResult, 1)
handler := cgo.NewHandle(c)
TaosFetchWhitelistA(conn, handler)
data := <-c
assert.Equal(t, int32(0), data.ErrCode)
assert.Equal(t, 1, len(data.IPNets))
assert.Equal(t, "0.0.0.0/0", data.IPNets[0].String())
}
35 changes: 35 additions & 0 deletions wrapper/whitelistcb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package wrapper

import "C"
import (
"net"
"unsafe"

"github.com/taosdata/driver-go/v3/wrapper/cgo"
)

type WhitelistResult struct {
ErrCode int32
IPNets []*net.IPNet
}

//export WhitelistCallback
func WhitelistCallback(param unsafe.Pointer, code int, taosConnect unsafe.Pointer, numOfWhiteLists int, pWhiteLists unsafe.Pointer) {
c := (*(*cgo.Handle)(param)).Value().(chan *WhitelistResult)
if code != 0 {
c <- &WhitelistResult{ErrCode: int32(code)}
return
}
ips := make([]*net.IPNet, 0, numOfWhiteLists)
for i := 0; i < numOfWhiteLists; i++ {
ipNet := make([]byte, 8)
for j := 0; j < 8; j++ {
ipNet[j] = *(*byte)(unsafe.Pointer(uintptr(pWhiteLists) + uintptr(i*8) + uintptr(j)))
}
ip := net.IP{ipNet[0], ipNet[1], ipNet[2], ipNet[3]}
ones := int(ipNet[4])
ipMask := net.CIDRMask(ones, 32)
ips = append(ips, &net.IPNet{IP: ip, Mask: ipMask})
}
c <- &WhitelistResult{IPNets: ips}
}