check tracker server when set multi IP

multi_ipaddr
YuQing 2019-10-08 12:26:15 +08:00
parent b1adb8889d
commit f60d96989a
4 changed files with 166 additions and 56 deletions

View File

@ -36,17 +36,53 @@ static int storage_cmp_by_ip_and_port(const void *p1, const void *p2)
{
int res;
res = strcmp(((ConnectionInfo *)p1)->ip_addr, \
res = strcmp(((ConnectionInfo *)p1)->ip_addr,
((ConnectionInfo *)p2)->ip_addr);
if (res != 0)
{
return res;
}
return ((ConnectionInfo *)p1)->port - \
return ((ConnectionInfo *)p1)->port -
((ConnectionInfo *)p2)->port;
}
static int storage_cmp_server_info(const void *p1, const void *p2)
{
TrackerServerInfo *server1;
TrackerServerInfo *server2;
ConnectionInfo *pc1;
ConnectionInfo *pc2;
ConnectionInfo *end1;
int res;
server1 = (TrackerServerInfo *)p1;
server2 = (TrackerServerInfo *)p2;
res = server1->count - server2->count;
if (res != 0)
{
return res;
}
if (server1->count == 1)
{
return storage_cmp_by_ip_and_port(server1->connections + 0,
server2->connections + 0);
}
end1 = server1->connections + server1->count;
for (pc1=server1->connections,pc2=server2->connections; pc1<end1; pc1++,pc2++)
{
if ((res=storage_cmp_by_ip_and_port(pc1, pc2)) != 0)
{
return res;
}
}
return 0;
}
static void insert_into_sorted_servers(TrackerServerGroup *pTrackerGroup, \
TrackerServerInfo *pInsertedServer)
{
@ -54,8 +90,7 @@ static void insert_into_sorted_servers(TrackerServerGroup *pTrackerGroup, \
for (pDestServer=pTrackerGroup->servers+pTrackerGroup->server_count;
pDestServer>pTrackerGroup->servers; pDestServer--)
{
if (storage_cmp_by_ip_and_port(pInsertedServer,
pDestServer-1) > 0)
if (storage_cmp_server_info(pInsertedServer, pDestServer-1) > 0)
{
memcpy(pDestServer, pInsertedServer,
sizeof(TrackerServerInfo));
@ -89,10 +124,10 @@ static int copy_tracker_servers(TrackerServerGroup *pTrackerGroup,
return result;
}
if (bsearch(&destServer, pTrackerGroup->servers, \
pTrackerGroup->server_count, \
sizeof(TrackerServerInfo), \
storage_cmp_by_ip_and_port) == NULL)
if (bsearch(&destServer, pTrackerGroup->servers,
pTrackerGroup->server_count,
sizeof(TrackerServerInfo),
storage_cmp_server_info) == NULL)
{
insert_into_sorted_servers(pTrackerGroup, &destServer);
pTrackerGroup->server_count++;
@ -114,19 +149,42 @@ static int copy_tracker_servers(TrackerServerGroup *pTrackerGroup,
return 0;
}
int fdfs_load_tracker_group_ex(TrackerServerGroup *pTrackerGroup, \
static int fdfs_check_tracker_group(TrackerServerGroup *pTrackerGroup,
const char *conf_filename)
{
int result;
TrackerServerInfo *pServer;
TrackerServerInfo *pEnd;
char error_info[256];
pEnd = pTrackerGroup->servers + pTrackerGroup->server_count;
for (pServer=pTrackerGroup->servers; pServer<pEnd; pServer++)
{
if ((result=fdfs_check_server_ips(pServer,
error_info, sizeof(error_info))) != 0)
{
logError("file: "__FILE__", line: %d, "
"conf file: %s, tracker_server is invalid, "
"error info: %s", __LINE__, conf_filename, error_info);
return result;
}
}
return 0;
}
int fdfs_load_tracker_group_ex(TrackerServerGroup *pTrackerGroup,
const char *conf_filename, IniContext *pIniContext)
{
int result;
int bytes;
char *ppTrackerServers[FDFS_MAX_TRACKERS];
if ((pTrackerGroup->server_count=iniGetValues(NULL, "tracker_server", \
if ((pTrackerGroup->server_count=iniGetValues(NULL, "tracker_server",
pIniContext, ppTrackerServers, FDFS_MAX_TRACKERS)) <= 0)
{
logError("file: "__FILE__", line: %d, " \
"conf file \"%s\", " \
"get item \"tracker_server\" fail", \
logError("file: "__FILE__", line: %d, "
"conf file \"%s\", item \"tracker_server\" not exist",
__LINE__, conf_filename);
return ENOENT;
}
@ -142,7 +200,7 @@ int fdfs_load_tracker_group_ex(TrackerServerGroup *pTrackerGroup, \
}
memset(pTrackerGroup->servers, 0, bytes);
if ((result=copy_tracker_servers(pTrackerGroup, conf_filename, \
if ((result=copy_tracker_servers(pTrackerGroup, conf_filename,
ppTrackerServers)) != 0)
{
pTrackerGroup->server_count = 0;
@ -151,10 +209,10 @@ int fdfs_load_tracker_group_ex(TrackerServerGroup *pTrackerGroup, \
return result;
}
return 0;
return fdfs_check_tracker_group(pTrackerGroup, conf_filename);
}
int fdfs_load_tracker_group(TrackerServerGroup *pTrackerGroup, \
int fdfs_load_tracker_group(TrackerServerGroup *pTrackerGroup,
const char *conf_filename)
{
IniContext iniContext;
@ -162,14 +220,14 @@ int fdfs_load_tracker_group(TrackerServerGroup *pTrackerGroup, \
if ((result=iniLoadFromFile(conf_filename, &iniContext)) != 0)
{
logError("file: "__FILE__", line: %d, " \
"load conf file \"%s\" fail, ret code: %d", \
logError("file: "__FILE__", line: %d, "
"load conf file \"%s\" fail, ret code: %d",
__LINE__, conf_filename, result);
return result;
}
result = fdfs_load_tracker_group_ex(pTrackerGroup, conf_filename, \
&iniContext);
result = fdfs_load_tracker_group_ex(pTrackerGroup,
conf_filename, &iniContext);
iniFreeContext(&iniContext);
return result;
@ -177,28 +235,28 @@ int fdfs_load_tracker_group(TrackerServerGroup *pTrackerGroup, \
static int fdfs_get_params_from_tracker(bool *use_storage_id)
{
IniContext iniContext;
IniContext iniContext;
int result;
bool continue_flag;
continue_flag = false;
if ((result=fdfs_get_ini_context_from_tracker(&g_tracker_group, \
if ((result=fdfs_get_ini_context_from_tracker(&g_tracker_group,
&iniContext, &continue_flag, false, NULL)) != 0)
{
return result;
}
{
return result;
}
*use_storage_id = iniGetBoolValue(NULL, "use_storage_id", \
&iniContext, false);
iniFreeContext(&iniContext);
*use_storage_id = iniGetBoolValue(NULL, "use_storage_id",
&iniContext, false);
iniFreeContext(&iniContext);
if (*use_storage_id)
{
result = fdfs_get_storage_ids_from_tracker_group( \
result = fdfs_get_storage_ids_from_tracker_group(
&g_tracker_group);
}
return result;
return result;
}
static int fdfs_client_do_init_ex(TrackerServerGroup *pTrackerGroup, \

View File

@ -1147,8 +1147,8 @@ void fdfs_set_log_rotate_size(LogContext *pContext, const int64_t log_rotate_siz
}
}
int fdfs_parse_server_info(char *server_str, const int default_port,
TrackerServerInfo *pServer)
int fdfs_parse_server_info_ex(char *server_str, const int default_port,
TrackerServerInfo *pServer, const bool resolve)
{
char *pColon;
char *hosts[FDFS_MULTI_IP_MAX_COUNT];
@ -1173,24 +1173,23 @@ int fdfs_parse_server_info(char *server_str, const int default_port,
conn = pServer->connections;
pServer->count = splitEx(server_str, ',',
hosts, FDFS_MULTI_IP_MAX_COUNT);
if (pServer->count == 1)
{
if (getIpaddrByName(hosts[0], conn->ip_addr,
sizeof(conn->ip_addr)) == INADDR_NONE)
{
logError("file: "__FILE__", line: %d, "
"host \"%s\" is invalid, error info: %s",
__LINE__, hosts[0], hstrerror(h_errno));
return EINVAL;
}
conn->port = port;
conn->sock = -1;
return 0;
}
for (i=0; i<pServer->count; i++)
{
snprintf(conn->ip_addr, sizeof(conn->ip_addr), "%s", hosts[i]);
if (resolve)
{
if (getIpaddrByName(hosts[i], conn->ip_addr,
sizeof(conn->ip_addr)) == INADDR_NONE)
{
logError("file: "__FILE__", line: %d, "
"host \"%s\" is invalid, error info: %s",
__LINE__, hosts[i], hstrerror(h_errno));
return EINVAL;
}
}
else
{
snprintf(conn->ip_addr, sizeof(conn->ip_addr), "%s", hosts[i]);
}
conn->port = port;
conn->sock = -1;
conn++;
@ -1221,3 +1220,45 @@ int fdfs_server_info_to_string_ex(TrackerServerInfo *pServer,
len += snprintf(buff + len, buffSize - len, ":%d", port);
return len;
}
int fdfs_check_server_ips(TrackerServerInfo *pServer,
char *error_info, const int error_size)
{
int private0;
int private1;
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;
}
private0 = is_private_ip(pServer->connections[0].ip_addr) ? 1 : 0;
private1 = is_private_ip(pServer->connections[1].ip_addr) ? 1 : 0;
if ((private0 ^ private1) == 0)
{
snprintf(error_info, error_size,
"invalid ip addresses %s and %s, "
"one MUST be an inner IP and another is a outer IP",
pServer->connections[0].ip_addr,
pServer->connections[1].ip_addr);
return EINVAL;
}
*error_info = '\0';
return 0;
}

View File

@ -97,8 +97,16 @@ bool fdfs_server_equal(TrackerServerInfo *pServer1,
void fdfs_server_sock_reset(TrackerServerInfo *pServerInfo);
int fdfs_parse_server_info(char *server_str, const int default_port,
TrackerServerInfo *pServer);
int fdfs_parse_server_info_ex(char *server_str, const int default_port,
TrackerServerInfo *pServer, const bool resolve);
static inline int fdfs_parse_server_info(char *server_str, const int default_port,
TrackerServerInfo *pServer)
{
const bool resolve = true;
return fdfs_parse_server_info_ex(server_str, default_port,
pServer, resolve);
}
int fdfs_server_info_to_string_ex(TrackerServerInfo *pServer,
const int port, char *buff, const int buffSize);
@ -110,6 +118,9 @@ static inline int fdfs_server_info_to_string(TrackerServerInfo *pServer,
pServer->connections[0].port, buff, buffSize);
}
int fdfs_check_server_ips(TrackerServerInfo *pServer,
char *error_info, const int error_size);
#ifdef __cplusplus
}
#endif

View File

@ -1365,7 +1365,7 @@ static int tracker_deal_storage_join(struct fast_task_info *pTask)
if (pTask->length - sizeof(TrackerHeader) != \
sizeof(TrackerStorageJoinBody) + joinBody.tracker_count *\
FDFS_PROTO_IP_PORT_SIZE)
FDFS_PROTO_MULTI_IP_PORT_SIZE)
{
logError("file: "__FILE__", line: %d, " \
"cmd: %d, client ip: %s, " \
@ -1375,7 +1375,7 @@ static int tracker_deal_storage_join(struct fast_task_info *pTask)
pTask->client_ip, pTask->length - \
(int)sizeof(TrackerHeader),
(int)sizeof(TrackerStorageJoinBody) + \
joinBody.tracker_count * FDFS_PROTO_IP_PORT_SIZE);
joinBody.tracker_count * FDFS_PROTO_MULTI_IP_PORT_SIZE);
pTask->length = sizeof(TrackerHeader);
return EINVAL;
}
@ -1443,16 +1443,16 @@ static int tracker_deal_storage_join(struct fast_task_info *pTask)
for (pTrackerServer=joinBody.tracker_servers;
pTrackerServer<pTrackerEnd; pTrackerServer++)
{
* (p + FDFS_PROTO_IP_PORT_SIZE - 1) = '\0';
* (p + FDFS_PROTO_MULTI_IP_PORT_SIZE - 1) = '\0';
if ((result=fdfs_parse_server_info(p, FDFS_TRACKER_SERVER_DEF_PORT,
pTrackerServer)) != 0)
if ((result=fdfs_parse_server_info_ex(p, FDFS_TRACKER_SERVER_DEF_PORT,
pTrackerServer, false)) != 0)
{
pTask->length = sizeof(TrackerHeader);
return result;
}
p += FDFS_PROTO_IP_PORT_SIZE;
p += FDFS_PROTO_MULTI_IP_PORT_SIZE;
}
joinBody.upload_priority = (int)buff2long(pBody->upload_priority);