add rpc update interface

This commit is contained in:
yihaoDeng 2023-09-10 16:11:14 +08:00
parent 8ea284f914
commit d121b564c0
7 changed files with 102 additions and 96 deletions

View File

@ -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

View File

@ -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);

View File

@ -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;
}

View File

@ -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

View File

@ -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;

View File

@ -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;
}

View File

@ -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) {