From d121b564c00deb9a65c0c25007b0b477143422e7 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Sun, 10 Sep 2023 16:11:14 +0800 Subject: [PATCH] add rpc update interface --- include/libs/transport/trpc.h | 4 + source/dnode/mgmt/node_mgmt/src/dmTransport.c | 7 +- source/dnode/mnode/impl/src/mndUser.c | 2 +- source/libs/transport/inc/transComm.h | 17 ++++ source/libs/transport/src/trans.c | 5 + source/libs/transport/src/transComm.c | 67 +++++++++++++ source/libs/transport/src/transSvr.c | 96 +------------------ 7 files changed, 102 insertions(+), 96 deletions(-) diff --git a/include/libs/transport/trpc.h b/include/libs/transport/trpc.h index 3bf9b072c6..83dbf2c4af 100644 --- a/include/libs/transport/trpc.h +++ b/include/libs/transport/trpc.h @@ -167,6 +167,10 @@ int rpcSetDefaultAddr(void *thandle, const char *ip, const char *fqdn); void *rpcAllocHandle(); void rpcSetIpWhite(void *thandl, void *arg); +int32_t rpcUtilSIpRangeToStr(SIpV4Range *pRange, char *buf); + +int32_t rpcUtilSWhiteListToStr(SIpWhiteList *pWhiteList, char **ppBuf); + #ifdef __cplusplus } #endif diff --git a/source/dnode/mgmt/node_mgmt/src/dmTransport.c b/source/dnode/mgmt/node_mgmt/src/dmTransport.c index 682ed0cbe7..020bb0815e 100644 --- a/source/dnode/mgmt/node_mgmt/src/dmTransport.c +++ b/source/dnode/mgmt/node_mgmt/src/dmTransport.c @@ -92,10 +92,9 @@ static void dmProcessRpcMsg(SDnode *pDnode, SRpcMsg *pRpc, SEpSet *pEpSet) { } if (pRpc->info.forbiddenIp == 1) { - struct in_addr addr; - addr.s_addr = pRpc->info.conn.clientIp; - char tbuf[40] = {0}; - uv_inet_ntop(AF_INET, &addr, tbuf, 40); + char tbuf[36] = {0}; + SIpV4Range range = {.ip = pRpc->info.conn.clientIp, .mask = 0}; + rpcUtilSIpRangeToStr(&range, tbuf); dError("User %s host:%s not in ip white list", pRpc->info.conn.user, tbuf); diff --git a/source/dnode/mnode/impl/src/mndUser.c b/source/dnode/mnode/impl/src/mndUser.c index 08da8ea6eb..ba5a3c5a42 100644 --- a/source/dnode/mnode/impl/src/mndUser.c +++ b/source/dnode/mnode/impl/src/mndUser.c @@ -192,7 +192,7 @@ int64_t mndGetIpWhiteVer(SMnode *pMnode) { ver = ipWhiteMgt.ver; taosThreadRwlockUnlock(&ipWhiteMgt.rw); - mInfo("ip-white-mnode ver, %" PRId64 "", ver); + mDebug("ip-white-mnode ver, %" PRId64 "", ver); return ver; } diff --git a/source/libs/transport/inc/transComm.h b/source/libs/transport/inc/transComm.h index 012884abc3..b72745b018 100644 --- a/source/libs/transport/inc/transComm.h +++ b/source/libs/transport/inc/transComm.h @@ -437,6 +437,23 @@ int32_t transGetRefMgt(); int32_t transGetInstMgt(); void transHttpEnvDestroy(); + +typedef struct { + int32_t netmask; + int32_t address; + int32_t network; + int32_t broadcast; + char info[32]; + int8_t type; +} SubnetUtils; + +int32_t subnetInit(SubnetUtils* pUtils, SIpV4Range* pRange); +int32_t subnetCheckIp(SubnetUtils* pUtils, uint32_t ip); +int32_t subnetDebugInfoToBuf(SubnetUtils* pUtils, char* buf); + +int32_t transUtilSIpRangeToStr(SIpV4Range* pRange, char* buf); +int32_t transUtilSWhiteListToStr(SIpWhiteList* pWhiteList, char** ppBuf); + #ifdef __cplusplus } #endif diff --git a/source/libs/transport/src/trans.c b/source/libs/transport/src/trans.c index ca255ec69b..e9aa62eded 100644 --- a/source/libs/transport/src/trans.c +++ b/source/libs/transport/src/trans.c @@ -188,6 +188,11 @@ void rpcSetIpWhite(void* thandle, void* arg) { transSetIpWhiteList(thandle, arg, void* rpcAllocHandle() { return (void*)transAllocHandle(); } +int32_t rpcUtilSIpRangeToStr(SIpV4Range* pRange, char* buf) { return transUtilSIpRangeToStr(pRange, buf); } +int32_t rpcUtilSWhiteListToStr(SIpWhiteList* pWhiteList, char** ppBuf) { + return transUtilSWhiteListToStr(pWhiteList, ppBuf); +} + int32_t rpcInit() { transInit(); return 0; diff --git a/source/libs/transport/src/transComm.c b/source/libs/transport/src/transComm.c index 5e602b1ea2..f9da22c865 100644 --- a/source/libs/transport/src/transComm.c +++ b/source/libs/transport/src/transComm.c @@ -654,3 +654,70 @@ void transDestoryExHandle(void* handle) { } taosMemoryFree(handle); } + +int32_t subnetInit(SubnetUtils* pUtils, SIpV4Range* pRange) { + pUtils->address = pRange->ip; + + int32_t mask = 0; + for (int i = 0; i < pRange->mask; i++) { + mask |= (1 << (31 - i)); + } + pUtils->netmask = mask; + + pUtils->network = pUtils->address & pUtils->netmask; + pUtils->broadcast = (pUtils->network) | (pUtils->netmask ^ 0xFFFFFFFF); + pUtils->type = (pRange->mask == 0 ? 0 : 1); + + return 0; +} +int32_t subnetDebugInfoToBuf(SubnetUtils* pUtils, char* buf) { + sprintf(buf, "raw: %s, address: %d, netmask:%d, network:%d, broadcast:%d", pUtils->info, pUtils->address, + pUtils->netmask, pUtils->network, pUtils->broadcast); + return 0; +} +int32_t subnetCheckIp(SubnetUtils* pUtils, uint32_t ip) { + // impl later + if (pUtils == NULL) return false; + if (pUtils->type == 0) { + return pUtils->address == ip; + } else { + return pUtils->network >= ip && pUtils->broadcast <= ip; + } +} + +int32_t transUtilSIpRangeToStr(SIpV4Range* pRange, char* buf) { + int32_t len = 0; + + struct in_addr addr; + addr.s_addr = pRange->ip; + + uv_inet_ntop(AF_INET, &addr, buf, 32); + len = strlen(buf); + + if (pRange->mask != 0) { + len += sprintf(buf + len, "/%d", pRange->mask); + } + buf[len] = 0; + return len; +} + +int32_t transUtilSWhiteListToStr(SIpWhiteList* pList, char** ppBuf) { + if (pList->num == 0) { + *ppBuf = NULL; + return 0; + } + int32_t len = 0; + char* pBuf = taosMemoryCalloc(1, pList->num * 36); + + for (int i = 0; i < pList->num; i++) { + SIpV4Range* pRange = &pList->pIpRange[i]; + + char tbuf[32] = {0}; + int tlen = transUtilSIpRangeToStr(pRange, tbuf); + len += sprintf(pBuf + len, "%s,", tbuf); + } + pBuf[len] = 0; + + *ppBuf = pBuf; + return len; +} diff --git a/source/libs/transport/src/transSvr.c b/source/libs/transport/src/transSvr.c index d88b27623a..b36987d82c 100644 --- a/source/libs/transport/src/transSvr.c +++ b/source/libs/transport/src/transSvr.c @@ -209,74 +209,6 @@ static void uvHandleActivityTimeout(uv_timer_t* handle) { tDebug("%p timeout since no activity", conn); } -int32_t cvtIp2Int(char* ip, int16_t* dest) { - int k = 0; - char* start = ip; - char* end = start; - - for (k = 0; *start != 0; start = end) { - for (end = start; *end != '.' && *end != '/' && *end != 0; end++) { - } - if (*end == '.' || *end == '/') { - *end = 0; - end++; - } - dest[k++] = atoi(start); - } - return k; -} -typedef struct { - int32_t netmask; - int32_t address; - int32_t network; - int32_t broadcast; - char info[32]; - int8_t type; -} SubnetUtils; - -int32_t subnetInit(SubnetUtils* pUtils, SIpV4Range* pRange) { - // char buf[32] = {0}; - // strncpy(pUtils->info, range, strlen(range)); - // strncpy(buf, range, strlen(range)); - - // int16_t ip[5] = {0}; - // int8_t k = cvtIp2Int(buf, ip); - // if (k < 4) { - // return -1; - // } - - // for (int i = 0; i < 4; i++) { - // pUtils->address |= (ip[i] << (8 * (4 - i - 1))); - // } - pUtils->address = pRange->ip; - - int32_t mask = 0; - for (int i = 0; i < pRange->mask; i++) { - mask |= (1 << (31 - i)); - } - pUtils->netmask = mask; - - pUtils->network = pUtils->address & pUtils->netmask; - pUtils->broadcast = (pUtils->network) | (pUtils->netmask ^ 0xFFFFFFFF); - pUtils->type = (pRange->mask == 0 ? 0 : 1); - - return 0; -} -int32_t subnetDebugInfoToBuf(SubnetUtils* pUtils, char* buf) { - sprintf(buf, "raw: %s, address: %d, netmask:%d, network:%d, broadcast:%d", pUtils->info, pUtils->address, - pUtils->netmask, pUtils->network, pUtils->broadcast); - return 0; -} -int32_t subnetCheckIp(SubnetUtils* pUtils, uint32_t ip) { - // impl later - if (pUtils == NULL) return false; - if (pUtils->type == 0) { - return pUtils->address == ip; - } else { - return pUtils->network >= ip && pUtils->broadcast <= ip; - } -} - static bool uvCheckIp(SIpV4Range* pRange, int32_t ip) { // impl later SubnetUtils subnet = {0}; @@ -285,7 +217,6 @@ static bool uvCheckIp(SIpV4Range* pRange, int32_t ip) { } return subnetCheckIp(&subnet, ip); } - SWhiteList* uvWhiteListCreate() { SWhiteList* pWhiteList = taosMemoryCalloc(1, sizeof(SWhiteList)); @@ -308,30 +239,13 @@ void uvWhiteListDestroy(SWhiteList* pWhite) { } void uvWhiteListToStr(SWhiteUserList* plist, char* user, char** ppBuf) { - int32_t len = 0; - char* pBuf = taosMemoryCalloc(1, plist->pList->num * 36); - len = sprintf(pBuf, "user: %s, ver: %" PRId64 ", ip: {", user, plist->ver); + char* tmp = NULL; + int32_t tlen = transUtilSWhiteListToStr(plist->pList, &tmp); - for (int i = 0; i < plist->pList->num; i++) { - SIpV4Range* pRange = &plist->pList->pIpRange[i]; - { - char tbuf[32] = {0}; - struct in_addr addr; - addr.s_addr = pRange->ip; - uv_inet_ntop(AF_INET, &addr, tbuf, 32); + char* pBuf = taosMemoryCalloc(1, tlen + 64); + int32_t len = sprintf(pBuf, "user: %s, ver: %" PRId64 ", ip: {%s}", user, plist->ver, tmp); + taosMemoryFree(tmp); - len += sprintf(pBuf + len, "%s", tbuf); - if (pRange->mask != 0) { - len += sprintf(pBuf + len, "%d", pRange->mask); - } - } - if (i == plist->pList->num - 1) { - len += sprintf(pBuf + len, "}"); - } else { - len += sprintf(pBuf + len, ","); - } - } - pBuf[len] = 0; *ppBuf = pBuf; } void uvWhiteListDebug(SWhiteList* pWrite) {