234 Star 1.7K Fork 408

GVPhappyfish100/FastDFS

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
fdfs_shared_func.c 21.91 KB
一键复制 编辑 原始数据 按行查看 历史
happyfish100 提交于 2025-10-26 12:41 +08:00 . upgrade version to 6.15.0
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
/**
* Copyright (C) 2008 Happy Fish / YuQing
*
* FastDFS may be copied only under the terms of the GNU General
* Public License V3, which may be found in the FastDFS source kit.
* Please visit the FastDFS Home Page http://www.fastken.com/ for more detail.
**/
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <netdb.h>
#include <ctype.h>
#include "fastcommon/logger.h"
#include "fastcommon/sockopt.h"
#include "fastcommon/shared_func.h"
#include "fastcommon/local_ip_func.h"
#include "fastcommon/md5.h"
#include "tracker_proto.h"
#include "fdfs_global.h"
#include "fdfs_shared_func.h"
bool fdfs_server_contain(TrackerServerInfo *pServerInfo,
const char *target_ip, const int target_port)
{
ConnectionInfo *conn;
ConnectionInfo *end;
if (pServerInfo->count == 1)
{
return FC_CONNECTION_SERVER_EQUAL(pServerInfo->connections[0],
target_ip, target_port);
}
else if (pServerInfo->count == 2)
{
return FC_CONNECTION_SERVER_EQUAL(pServerInfo->connections[0],
target_ip, target_port) ||
FC_CONNECTION_SERVER_EQUAL(pServerInfo->connections[1],
target_ip, target_port);
}
end = pServerInfo->connections + pServerInfo->count;
for (conn=pServerInfo->connections; conn<end; conn++)
{
if (FC_CONNECTION_SERVER_EQUAL(*conn, target_ip, target_port))
{
return true;
}
}
return false;
}
bool fdfs_server_contain_ex(TrackerServerInfo *pServer1,
TrackerServerInfo *pServer2)
{
ConnectionInfo *conn;
ConnectionInfo *end;
if (pServer1->count == 1)
{
return fdfs_server_contain1(pServer2, pServer1->connections + 0);
}
else if (pServer1->count == 2)
{
if (fdfs_server_contain1(pServer2, pServer1->connections + 0))
{
return true;
}
return fdfs_server_contain1(pServer2, pServer1->connections + 1);
}
end = pServer1->connections + pServer1->count;
for (conn=pServer1->connections; conn<end; conn++)
{
if (fdfs_server_contain1(pServer2, conn))
{
return true;
}
}
return false;
}
bool fdfs_server_equal(TrackerServerInfo *pServer1,
TrackerServerInfo *pServer2)
{
ConnectionInfo *conn;
ConnectionInfo *end;
if (pServer1->count != pServer2->count)
{
return false;
}
if (pServer1->count == 1)
{
return (pServer1->connections->port == pServer2->connections->port &&
strcmp(pServer1->connections->ip_addr, pServer2->connections->ip_addr) == 0);
}
end = pServer1->connections + pServer1->count;
for (conn=pServer1->connections; conn<end; conn++)
{
if (!fdfs_server_contain1(pServer2, conn))
{
return false;
}
}
return true;
}
bool fdfs_server_contain_local_service(TrackerServerInfo *pServerInfo,
const int target_port)
{
const char *current_ip;
current_ip = get_first_local_ip();
while (current_ip != NULL)
{
if (fdfs_server_contain(pServerInfo, current_ip, target_port))
{
return true;
}
current_ip = get_next_local_ip(current_ip);
}
return false;
}
TrackerServerInfo *fdfs_tracker_group_get_server(TrackerServerGroup *pGroup,
const char *target_ip, const int target_port)
{
TrackerServerInfo *pServer;
TrackerServerInfo *pEnd;
pEnd = pGroup->servers + pGroup->server_count;
for (pServer=pGroup->servers; pServer<pEnd; pServer++)
{
if (fdfs_server_contain(pServer, target_ip, target_port))
{
return pServer;
}
}
return NULL;
}
void fdfs_server_sock_reset(TrackerServerInfo *pServerInfo)
{
ConnectionInfo *conn;
ConnectionInfo *end;
if (pServerInfo->count == 1)
{
pServerInfo->connections[0].sock = -1;
}
else if (pServerInfo->count == 2)
{
pServerInfo->connections[0].sock = -1;
pServerInfo->connections[1].sock = -1;
}
else
{
end = pServerInfo->connections + pServerInfo->count;
for (conn=pServerInfo->connections; conn<end; conn++)
{
conn->sock = -1;
}
}
}
int fdfs_get_tracker_leader_index_ex(TrackerServerGroup *pServerGroup,
const char *leaderIp, const int leaderPort)
{
TrackerServerInfo *pServer;
TrackerServerInfo *pEnd;
if (pServerGroup->server_count == 0)
{
return -1;
}
pEnd = pServerGroup->servers + pServerGroup->server_count;
for (pServer=pServerGroup->servers; pServer<pEnd; pServer++)
{
if (fdfs_server_contain(pServer, leaderIp, leaderPort))
{
return pServer - pServerGroup->servers;
}
}
return -1;
}
int fdfs_parse_storage_reserved_space(IniContext *pIniContext,
FDFSStorageReservedSpace *pStorageReservedSpace)
{
int result;
int len;
char *pReservedSpaceStr;
int64_t storage_reserved;
pReservedSpaceStr = iniGetStrValue(NULL,
"reserved_storage_space", pIniContext);
if (pReservedSpaceStr == NULL)
{
pStorageReservedSpace->flag =
TRACKER_STORAGE_RESERVED_SPACE_FLAG_MB;
pStorageReservedSpace->rs.mb = FDFS_DEF_STORAGE_RESERVED_MB;
return 0;
}
if (*pReservedSpaceStr == '\0')
{
logError("file: "__FILE__", line: %d, "
"item \"reserved_storage_space\" is empty!",
__LINE__);
return EINVAL;
}
len = strlen(pReservedSpaceStr);
if (*(pReservedSpaceStr + len - 1) == '%')
{
char *endptr;
pStorageReservedSpace->flag = TRACKER_STORAGE_RESERVED_SPACE_FLAG_RATIO;
endptr = NULL;
*(pReservedSpaceStr + len - 1) = '\0';
pStorageReservedSpace->rs.ratio = strtod(pReservedSpaceStr, &endptr);
if (endptr != NULL && *endptr != '\0')
{
logError("file: "__FILE__", line: %d, "
"item \"reserved_storage_space\": %s%%"
" is invalid!", __LINE__, pReservedSpaceStr);
return EINVAL;
}
if (pStorageReservedSpace->rs.ratio <= 0.00 ||
pStorageReservedSpace->rs.ratio >= 100.00)
{
logError("file: "__FILE__", line: %d, "
"item \"reserved_storage_space\": %s%%"
" is invalid!", __LINE__, pReservedSpaceStr);
return EINVAL;
}
pStorageReservedSpace->rs.ratio /= 100.00;
return 0;
}
if ((result=parse_bytes(pReservedSpaceStr, 1, &storage_reserved)) != 0)
{
return result;
}
pStorageReservedSpace->flag = TRACKER_STORAGE_RESERVED_SPACE_FLAG_MB;
pStorageReservedSpace->rs.mb = storage_reserved / FC_BYTES_ONE_MB;
return 0;
}
const char *fdfs_storage_reserved_space_to_string(FDFSStorageReservedSpace
*pStorageReservedSpace, char *buff)
{
int len;
char *p;
if (pStorageReservedSpace->flag ==
TRACKER_STORAGE_RESERVED_SPACE_FLAG_MB)
{
len = fc_itoa(pStorageReservedSpace->rs.mb, buff);
p = buff + len;
*p++ = 'M';
*p++ = 'B';
}
else
{
len = fc_ftoa(100.00 * pStorageReservedSpace->rs.ratio, 2, buff);
p = buff + len;
*p++ = '%';
}
*p = '\0';
return buff;
}
const char *fdfs_storage_reserved_space_to_string_ex(const bool flag,
const int64_t space_mb, const int64_t total_mb,
const double space_ratio, char *buff)
{
int len;
char *p;
if (flag == TRACKER_STORAGE_RESERVED_SPACE_FLAG_MB)
{
len = fc_itoa(space_mb, buff);
p = buff + len;
*p++ = ' ';
*p++ = 'M';
*p++ = 'B';
}
else
{
len = fc_itoa((int64_t)(total_mb * space_ratio), buff);
p = buff + len;
*p++ = ' ';
*p++ = 'M';
*p++ = 'B';
*p++ = '(';
p += fc_ftoa(100.00 * space_ratio, 2, p);
*p++ = '%';
*p++ = ')';
}
*p = '\0';
return buff;
}
int64_t fdfs_get_storage_reserved_space_mb(const int64_t total_mb,
FDFSStorageReservedSpace *pStorageReservedSpace)
{
if (pStorageReservedSpace->flag == \
TRACKER_STORAGE_RESERVED_SPACE_FLAG_MB)
{
return pStorageReservedSpace->rs.mb;
}
else
{
return (int64_t)(total_mb * pStorageReservedSpace->rs.ratio);
}
}
bool fdfs_check_reserved_space(FDFSGroupInfo *pGroup, \
FDFSStorageReservedSpace *pStorageReservedSpace)
{
if (pStorageReservedSpace->flag ==
TRACKER_STORAGE_RESERVED_SPACE_FLAG_MB)
{
return pGroup->free_mb > pStorageReservedSpace->rs.mb;
}
else
{
if (pGroup->total_mb == 0)
{
return false;
}
/*
logInfo("storage=%.4f, rs.ratio=%.4f",
((double)pGroup->free_mb / (double)pGroup->total_mb),
pStorageReservedSpace->rs.ratio);
*/
return ((double)pGroup->free_mb / (double)pGroup->total_mb) >
pStorageReservedSpace->rs.ratio;
}
}
bool fdfs_check_reserved_space_trunk(FDFSGroupInfo *pGroup, \
FDFSStorageReservedSpace *pStorageReservedSpace)
{
if (pStorageReservedSpace->flag ==
TRACKER_STORAGE_RESERVED_SPACE_FLAG_MB)
{
return (pGroup->free_mb + pGroup->trunk_free_mb >
pStorageReservedSpace->rs.mb);
}
else
{
if (pGroup->total_mb == 0)
{
return false;
}
/*
logInfo("storage trunk=%.4f, rs.ratio=%.4f",
((double)(pGroup->free_mb + pGroup->trunk_free_mb) /
(double)pGroup->total_mb), pStorageReservedSpace->rs.ratio);
*/
return ((double)(pGroup->free_mb + pGroup->trunk_free_mb) /
(double)pGroup->total_mb) > pStorageReservedSpace->rs.ratio;
}
}
bool fdfs_check_reserved_space_path(const int64_t total_mb,
const int64_t free_mb, const int64_t avg_mb,
FDFSStorageReservedSpace *pStorageReservedSpace)
{
if (pStorageReservedSpace->flag ==
TRACKER_STORAGE_RESERVED_SPACE_FLAG_MB)
{
return free_mb > avg_mb;
}
else
{
if (total_mb == 0)
{
return false;
}
/*
logInfo("storage path, free_mb=%"PRId64
", total_mb=%"PRId64", "
"real ratio=%.4f, rs.ratio=%.4f",
free_mb, total_mb, ((double)free_mb / total_mb),
pStorageReservedSpace->rs.ratio);
*/
return ((double)free_mb / (double)total_mb) >
pStorageReservedSpace->rs.ratio;
}
}
int fdfs_connection_pool_init(const char *config_filename, \
IniContext *pItemContext)
{
g_use_connection_pool = iniGetBoolValue(NULL, "use_connection_pool", \
pItemContext, false);
if (!g_use_connection_pool)
{
return 0;
}
g_connection_pool_max_idle_time = iniGetIntValue(NULL, \
"connection_pool_max_idle_time", \
pItemContext, 3600);
if (g_connection_pool_max_idle_time <= 0)
{
logError("file: "__FILE__", line: %d, " \
"connection_pool_max_idle_time: %d of conf " \
"filename: \"%s\" is invalid!", __LINE__, \
g_connection_pool_max_idle_time, config_filename);
return EINVAL;
}
return conn_pool_init(&g_connection_pool, SF_G_CONNECT_TIMEOUT, \
0, g_connection_pool_max_idle_time);
}
void fdfs_connection_pool_destroy()
{
conn_pool_destroy(&g_connection_pool);
}
int fdfs_parse_server_info_ex(char *server_str, const int default_port,
TrackerServerInfo *pServer, const bool resolve)
{
char *pSquare;
char *pColon;
char *hosts[FDFS_MULTI_IP_MAX_COUNT];
ConnectionInfo *conn;
int port;
int i;
memset(pServer, 0, sizeof(TrackerServerInfo));
if (*server_str == '[')
{
server_str++;
if ((pSquare=strchr(server_str, ']')) == NULL)
{
logError("file: "__FILE__", line: %d, "
"host \"%s\" is invalid",
__LINE__, server_str - 1);
return EINVAL;
}
*pSquare = '\0';
pColon = pSquare + 1; //skip ]
if (*pColon != ':')
{
pColon = NULL;
}
}
else
{
pColon = strrchr(server_str, ':');
}
if (pColon == NULL)
{
logInfo("file: "__FILE__", line: %d, "
"no port part in %s, set port to %d",
__LINE__, server_str, default_port);
port = default_port;
}
else
{
*pColon = '\0';
port = atoi(pColon + 1);
}
conn = pServer->connections;
pServer->count = splitEx(server_str, ',',
hosts, FDFS_MULTI_IP_MAX_COUNT);
for (i=0; i<pServer->count; i++)
{
if (resolve)
{
if (getIpaddrByNameEx(hosts[i], conn->ip_addr,
sizeof(conn->ip_addr), &conn->af) == INADDR_NONE)
{
logError("file: "__FILE__", line: %d, "
"host \"%s\" is invalid, error info: %s",
__LINE__, hosts[i], hstrerror(h_errno));
return EINVAL;
}
}
else
{
fc_safe_strcpy(conn->ip_addr, hosts[i]);
conn->af = is_ipv6_addr(conn->ip_addr) ? AF_INET6 : AF_INET;
}
conn->port = port;
conn->sock = -1;
conn++;
}
return 0;
}
int fdfs_server_info_to_string_ex(const TrackerServerInfo *pServer,
const int port, char *buff, const int buffSize)
{
const ConnectionInfo *conn;
const ConnectionInfo *end;
char *p;
int ip_len;
bool is_ipv6;
if (pServer->count <= 0)
{
*buff = '\0';
return 0;
}
ip_len = strlen(pServer->connections[0].ip_addr);
p = buff;
if (pServer->count == 1 || ip_len + 8 >= buffSize)
{
if (is_ipv6_addr(pServer->connections[0].ip_addr))
{
if (ip_len + 10 > buffSize)
{
return snprintf(buff, buffSize, "[%s]:%u",
pServer->connections[0].ip_addr, port);
}
*p++ = '[';
memcpy(p, pServer->connections[0].ip_addr, ip_len);
p += ip_len;
*p++ = ']';
}
else
{
if (ip_len + 8 > buffSize)
{
return snprintf(buff, buffSize, "%s:%u",
pServer->connections[0].ip_addr, port);
}
memcpy(p, pServer->connections[0].ip_addr, ip_len);
p += ip_len;
}
*p++ = ':';
p += fc_itoa(port, p);
*p = '\0';
return p - buff;
}
is_ipv6 = false;
end = pServer->connections + pServer->count;
for (conn=pServer->connections; conn<end; conn++)
{
if (is_ipv6_addr(conn->ip_addr))
{
is_ipv6 = true;
break;
}
}
if (is_ipv6)
{
*p++ = '[';
}
memcpy(p, pServer->connections[0].ip_addr, ip_len);
p += ip_len;
for (conn=pServer->connections + 1; conn<end; conn++)
{
ip_len = strlen(conn->ip_addr);
if (ip_len + 8 > (buff + buffSize) - p)
{
break;
}
*p++ = ',';
memcpy(p, conn->ip_addr, ip_len);
p += ip_len;
}
if (is_ipv6)
{
*p++ = ']';
}
*p++ = ':';
p += fc_itoa(port, p);
*p = '\0';
return p - buff;
}
int fdfs_get_ip_type(const char* ip)
{
if (ip == NULL || (int)strlen(ip) < 8)
{
return FDFS_IP_TYPE_UNKNOWN;
}
if (memcmp(ip, "10.", 3) == 0)
{
return FDFS_IP_TYPE_PRIVATE_10;
}
if (memcmp(ip, "192.168.", 8) == 0)
{
return FDFS_IP_TYPE_PRIVATE_192;
}
if (memcmp(ip, "172.", 4) == 0)
{
int b;
b = atoi(ip + 4);
if (b >= 16 && b < 32)
{
return FDFS_IP_TYPE_PRIVATE_172;
}
}
return FDFS_IP_TYPE_OUTER;
}
int fdfs_check_server_ips(const TrackerServerInfo *pServer,
char *error_info, const int error_size)
{
int type0;
int type1;
if (pServer->count == 1)
{
*error_info = '\0';
return 0;
}
if (pServer->count <= 0)
{
logError("file: "__FILE__", line: %d, "
"empty server", __LINE__);
return EINVAL;
}
if (pServer->count > FDFS_MULTI_IP_MAX_COUNT)
{
snprintf(error_info, error_size,
"too many server ip addresses: %d, exceeds %d",
pServer->count, FDFS_MULTI_IP_MAX_COUNT);
return EINVAL;
}
type0 = fdfs_get_ip_type(pServer->connections[0].ip_addr);
type1 = fdfs_get_ip_type(pServer->connections[1].ip_addr);
if (type0 == type1)
{
snprintf(error_info, error_size,
"invalid ip addresses %s and %s, "
"one MUST be an inner IP and another is a outer IP, "
"or two different types of inner IP addresses",
pServer->connections[0].ip_addr,
pServer->connections[1].ip_addr);
return EINVAL;
}
*error_info = '\0';
return 0;
}
int fdfs_parse_multi_ips_ex(char *ip_str, FDFSMultiIP *ip_addrs,
char *error_info, const int error_size, const bool resolve)
{
char *hosts[FDFS_MULTI_IP_MAX_COUNT];
int i;
ip_addrs->index = 0;
ip_addrs->count = splitEx(ip_str, ',', hosts, FDFS_MULTI_IP_MAX_COUNT);
for (i=0; i<ip_addrs->count; i++)
{
if (resolve)
{
if (getIpaddrByName(hosts[i], ip_addrs->ips[i].address,
sizeof(ip_addrs->ips[i].address)) == INADDR_NONE)
{
snprintf(error_info, error_size,
"host \"%s\" is invalid, error info: %s",
hosts[i], hstrerror(h_errno));
return EINVAL;
}
}
else
{
fc_safe_strcpy(ip_addrs->ips[i].address, hosts[i]);
}
ip_addrs->ips[i].type = fdfs_get_ip_type(ip_addrs->ips[i].address);
if (ip_addrs->ips[i].type == FDFS_IP_TYPE_UNKNOWN)
{
snprintf(error_info, error_size,
"ip address \"%s\" is invalid",
ip_addrs->ips[i].address);
return EINVAL;
}
}
*error_info = '\0';
return 0;
}
int fdfs_multi_ips_to_string_ex(const FDFSMultiIP *ip_addrs,
const char seperator, char *buff, const int buffSize)
{
int i;
int ip_len;
char *p;
if (ip_addrs->count <= 0)
{
*buff = '\0';
return 0;
}
if (ip_addrs->count == 1)
{
return fc_strlcpy(buff, ip_addrs->ips[0].address, buffSize);
}
ip_len = strlen(ip_addrs->ips[0].address);
if (ip_len >= buffSize) {
return fc_strlcpy(buff, ip_addrs->ips[0].address, buffSize);
}
memcpy(buff, ip_addrs->ips[0].address, ip_len);
p = buff + ip_len;
for (i=1; i<ip_addrs->count; i++)
{
ip_len = strlen(ip_addrs->ips[i].address);
if (2 + ip_len > (buff + buffSize) - p) {
break;
}
*p++ = seperator;
memcpy(p, ip_addrs->ips[i].address, ip_len);
p += ip_len;
}
*p = '\0';
return p - buff;
}
const char *fdfs_get_ipaddr_by_peer_ip(const FDFSMultiIP *ip_addrs,
const char *client_ip)
{
int ip_type;
int index;
if (ip_addrs->count == 1)
{
return ip_addrs->ips[0].address;
}
if (ip_addrs->count <= 0)
{
return "";
}
ip_type = fdfs_get_ip_type(client_ip);
index = ip_addrs->ips[FDFS_MULTI_IP_INDEX_OUTER].type == ip_type ?
FDFS_MULTI_IP_INDEX_OUTER : FDFS_MULTI_IP_INDEX_INNER;
return ip_addrs->ips[index].address;
}
int fdfs_check_and_format_ips(FDFSMultiIP *ip_addrs,
char *error_info, const int error_size)
{
FDFSIPInfo swap_ip;
if (ip_addrs->count == 1)
{
*error_info = '\0';
return 0;
}
if (ip_addrs->count <= 0)
{
logError("file: "__FILE__", line: %d, "
"empty server", __LINE__);
return EINVAL;
}
if (ip_addrs->count > FDFS_MULTI_IP_MAX_COUNT)
{
snprintf(error_info, error_size,
"too many server ip addresses: %d, exceeds %d",
ip_addrs->count, FDFS_MULTI_IP_MAX_COUNT);
return EINVAL;
}
if (ip_addrs->ips[FDFS_MULTI_IP_INDEX_INNER].type ==
ip_addrs->ips[FDFS_MULTI_IP_INDEX_OUTER].type)
{
snprintf(error_info, error_size,
"invalid ip addresses %s and %s, "
"one MUST be an inner IP and another is a outer IP, "
"or two different types of inner IP addresses",
ip_addrs->ips[0].address, ip_addrs->ips[1].address);
return EINVAL;
}
if (ip_addrs->ips[FDFS_MULTI_IP_INDEX_INNER].type == FDFS_IP_TYPE_OUTER)
{
swap_ip = ip_addrs->ips[FDFS_MULTI_IP_INDEX_INNER];
ip_addrs->ips[FDFS_MULTI_IP_INDEX_INNER] =
ip_addrs->ips[FDFS_MULTI_IP_INDEX_OUTER];
ip_addrs->ips[FDFS_MULTI_IP_INDEX_OUTER] = swap_ip;
}
*error_info = '\0';
return 0;
}
void fdfs_set_multi_ip_index(FDFSMultiIP *multi_ip, const char *target_ip)
{
int i;
if (multi_ip->count <= 1)
{
return;
}
for (i=0; i<multi_ip->count; i++)
{
if (strcmp(multi_ip->ips[i].address, target_ip) == 0)
{
multi_ip->index = i;
break;
}
}
}
void fdfs_set_server_info_index(TrackerServerInfo *pServer,
const char *target_ip, const int target_port)
{
int i;
if (pServer->count <= 1)
{
return;
}
for (i=0; i<pServer->count; i++)
{
if (FC_CONNECTION_SERVER_EQUAL(pServer->connections[i],
target_ip, target_port))
{
pServer->index = i;
break;
}
}
}
void fdfs_set_server_info(TrackerServerInfo *pServer,
const char *ip_addr, const int port)
{
pServer->count = 1;
pServer->index = 0;
conn_pool_set_server_info(pServer->connections + 0, ip_addr, port);
}
void fdfs_set_server_info_ex(TrackerServerInfo *pServer,
const FDFSMultiIP *ip_addrs, const int port)
{
int i;
pServer->count = ip_addrs->count;
pServer->index = 0;
for (i=0; i<ip_addrs->count; i++)
{
conn_pool_set_server_info(pServer->connections + i,
ip_addrs->ips[i].address, port);
}
}
char *fdfs_ip_to_shortcode(const char *ipAddr, char *shortCode)
{
int64_t crc64;
int len;
crc64 = CRC32_ex(ipAddr, strlen(ipAddr), CRC32_XINIT);
return base64_encode(&g_fdfs_base64_context, (const char *)&crc64,
sizeof(crc64), shortCode, &len);
}
bool fdfs_multi_ips_contain_ipv6(const FDFSMultiIP *ip_addrs)
{
int i;
if (ip_addrs->count <= 0)
{
return false;
}
if (ip_addrs->count == 1)
{
return is_ipv6_addr(ip_addrs->ips[0].address);
}
for (i=0; i<ip_addrs->count; i++)
{
if (is_ipv6_addr(ip_addrs->ips[i].address))
{
return true;
}
}
return false;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/fastdfs100/fastdfs.git
git@gitee.com:fastdfs100/fastdfs.git
fastdfs100
fastdfs
FastDFS
master

搜索帮助