field socket_domain rename to af

use_iouring
YuQing 2023-11-29 18:35:00 +08:00
parent 700a5bcaec
commit dd77da144f
6 changed files with 62 additions and 41 deletions

View File

@ -76,7 +76,7 @@ static void cp_tls_destroy(void *ptr)
int conn_pool_init_ex1(ConnectionPool *cp, int connect_timeout, int conn_pool_init_ex1(ConnectionPool *cp, int connect_timeout,
const int max_count_per_entry, const int max_idle_time, const int max_count_per_entry, const int max_idle_time,
const int socket_domain, const int htable_init_capacity, const int htable_init_capacity,
fc_connection_callback_func connect_done_func, void *connect_done_args, fc_connection_callback_func connect_done_func, void *connect_done_args,
fc_connection_callback_func validate_func, void *validate_args, fc_connection_callback_func validate_func, void *validate_args,
const int extra_data_size, const ConnectionExtraParams *extra_params) const int extra_data_size, const ConnectionExtraParams *extra_params)
@ -95,7 +95,6 @@ int conn_pool_init_ex1(ConnectionPool *cp, int connect_timeout,
cp->max_count_per_entry = max_count_per_entry; cp->max_count_per_entry = max_count_per_entry;
cp->max_idle_time = max_idle_time; cp->max_idle_time = max_idle_time;
cp->extra_data_size = extra_data_size; cp->extra_data_size = extra_data_size;
cp->socket_domain = socket_domain;
cp->connect_done_callback.func = connect_done_func; cp->connect_done_callback.func = connect_done_func;
cp->connect_done_callback.args = connect_done_args; cp->connect_done_callback.args = connect_done_args;
cp->validate_callback.func = validate_func; cp->validate_callback.func = validate_func;
@ -211,7 +210,7 @@ int conn_pool_connect_server_ex1(ConnectionInfo *conn,
close(conn->sock); close(conn->sock);
} }
if ((conn->sock=socketCreateEx2(conn->socket_domain, conn->ip_addr, if ((conn->sock=socketCreateEx2(conn->af, conn->ip_addr,
O_NONBLOCK, bind_ipaddr, &result)) < 0) O_NONBLOCK, bind_ipaddr, &result)) < 0)
{ {
return result; return result;
@ -247,9 +246,8 @@ int conn_pool_async_connect_server_ex(ConnectionInfo *conn,
close(conn->sock); close(conn->sock);
} }
if ((conn->sock=socketCreateEx2(conn->socket_domain, if ((conn->sock=socketCreateEx2(conn->af, conn->ip_addr,
conn->ip_addr, O_NONBLOCK, bind_ipaddr, O_NONBLOCK, bind_ipaddr, &result)) < 0)
&result)) < 0)
{ {
return result; return result;
} }
@ -353,7 +351,7 @@ static ConnectionInfo *get_connection(ConnectionPool *cp,
memcpy(node->conn->ip_addr, conn->ip_addr, sizeof(conn->ip_addr)); memcpy(node->conn->ip_addr, conn->ip_addr, sizeof(conn->ip_addr));
node->conn->port = conn->port; node->conn->port = conn->port;
node->conn->comm_type = conn->comm_type; node->conn->comm_type = conn->comm_type;
node->conn->socket_domain = cp->socket_domain; node->conn->af = conn->af;
node->conn->sock = -1; node->conn->sock = -1;
node->conn->validate_flag = false; node->conn->validate_flag = false;
*err_no = G_COMMON_CONNECTION_CALLBACKS[conn->comm_type]. *err_no = G_COMMON_CONNECTION_CALLBACKS[conn->comm_type].
@ -709,8 +707,9 @@ int conn_pool_parse_server_info(const char *pServerStr,
} }
} }
if (getIpaddrByName(parts[0], pServerInfo->ip_addr, if (getIpaddrByNameEx(parts[0], pServerInfo->ip_addr,
sizeof(pServerInfo->ip_addr)) == INADDR_NONE) sizeof(pServerInfo->ip_addr),
&pServerInfo->af) == INADDR_NONE)
{ {
logError("file: "__FILE__", line: %d, " logError("file: "__FILE__", line: %d, "
"host: %s, invalid hostname: %s!", "host: %s, invalid hostname: %s!",
@ -718,7 +717,6 @@ int conn_pool_parse_server_info(const char *pServerStr,
return EINVAL; return EINVAL;
} }
pServerInfo->socket_domain = AF_UNSPEC;
pServerInfo->sock = -1; pServerInfo->sock = -1;
pServerInfo->comm_type = fc_comm_type_sock; pServerInfo->comm_type = fc_comm_type_sock;
return 0; return 0;

View File

@ -26,6 +26,7 @@
#include "fast_mblock.h" #include "fast_mblock.h"
#include "ini_file_reader.h" #include "ini_file_reader.h"
#include "pthread_func.h" #include "pthread_func.h"
#include "sockopt.h"
#include "hash.h" #include "hash.h"
#ifdef __cplusplus #ifdef __cplusplus
@ -49,7 +50,7 @@ typedef enum {
typedef struct { typedef struct {
int sock; int sock;
uint16_t port; uint16_t port;
short socket_domain; //socket domain, AF_INET, AF_INET6 or AF_UNSPEC for auto dedect short af; //address family, AF_INET, AF_INET6 or AF_UNSPEC for auto dedect
FCCommunicationType comm_type; FCCommunicationType comm_type;
bool validate_flag; //for connection pool bool validate_flag; //for connection pool
char ip_addr[IP_ADDRESS_SIZE]; char ip_addr[IP_ADDRESS_SIZE];
@ -174,7 +175,6 @@ typedef struct tagConnectionPool {
unit: second unit: second
*/ */
int max_idle_time; int max_idle_time;
int socket_domain; //socket domain
struct fast_mblock_man manager_allocator; struct fast_mblock_man manager_allocator;
struct fast_mblock_man node_allocator; struct fast_mblock_man node_allocator;
@ -208,7 +208,7 @@ int conn_pool_global_init_for_rdma();
* connect_timeout: the connect timeout in seconds * connect_timeout: the connect timeout in seconds
* max_count_per_entry: max connection count per host:port * max_count_per_entry: max connection count per host:port
* max_idle_time: reconnect the server after max idle time in seconds * max_idle_time: reconnect the server after max idle time in seconds
* socket_domain: the socket domain * af: the socket domain
* htable_init_capacity: the init capacity of connection hash table * htable_init_capacity: the init capacity of connection hash table
* connect_done_func: the connect done connection callback * connect_done_func: the connect done connection callback
* connect_done_args: the args for connect done connection callback * connect_done_args: the args for connect done connection callback
@ -220,7 +220,7 @@ int conn_pool_global_init_for_rdma();
*/ */
int conn_pool_init_ex1(ConnectionPool *cp, int connect_timeout, int conn_pool_init_ex1(ConnectionPool *cp, int connect_timeout,
const int max_count_per_entry, const int max_idle_time, const int max_count_per_entry, const int max_idle_time,
const int socket_domain, const int htable_init_capacity, const int htable_init_capacity,
fc_connection_callback_func connect_done_func, void *connect_done_args, fc_connection_callback_func connect_done_func, void *connect_done_args,
fc_connection_callback_func validate_func, void *validate_args, fc_connection_callback_func validate_func, void *validate_args,
const int extra_data_size, const ConnectionExtraParams *extra_params); const int extra_data_size, const ConnectionExtraParams *extra_params);
@ -232,19 +232,17 @@ int conn_pool_init_ex1(ConnectionPool *cp, int connect_timeout,
* connect_timeout: the connect timeout in seconds * connect_timeout: the connect timeout in seconds
* max_count_per_entry: max connection count per host:port * max_count_per_entry: max connection count per host:port
* max_idle_time: reconnect the server after max idle time in seconds * max_idle_time: reconnect the server after max idle time in seconds
* socket_domain: the socket domain
* return 0 for success, != 0 for error * return 0 for success, != 0 for error
*/ */
static inline int conn_pool_init_ex(ConnectionPool *cp, int connect_timeout, static inline int conn_pool_init_ex(ConnectionPool *cp, int connect_timeout,
const int max_count_per_entry, const int max_idle_time, const int max_count_per_entry, const int max_idle_time)
const int socket_domain)
{ {
const int htable_init_capacity = 0; const int htable_init_capacity = 0;
const int extra_data_size = 0; const int extra_data_size = 0;
const ConnectionExtraParams *extra_params = NULL; const ConnectionExtraParams *extra_params = NULL;
return conn_pool_init_ex1(cp, connect_timeout, max_count_per_entry, return conn_pool_init_ex1(cp, connect_timeout, max_count_per_entry,
max_idle_time, socket_domain, htable_init_capacity, max_idle_time, htable_init_capacity, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, extra_data_size, extra_params); extra_data_size, extra_params);
} }
/** /**
@ -259,13 +257,12 @@ static inline int conn_pool_init_ex(ConnectionPool *cp, int connect_timeout,
static inline int conn_pool_init(ConnectionPool *cp, int connect_timeout, static inline int conn_pool_init(ConnectionPool *cp, int connect_timeout,
const int max_count_per_entry, const int max_idle_time) const int max_count_per_entry, const int max_idle_time)
{ {
const int socket_domain = AF_UNSPEC;
const int htable_init_capacity = 0; const int htable_init_capacity = 0;
const int extra_data_size = 0; const int extra_data_size = 0;
const ConnectionExtraParams *extra_params = NULL; const ConnectionExtraParams *extra_params = NULL;
return conn_pool_init_ex1(cp, connect_timeout, max_count_per_entry, return conn_pool_init_ex1(cp, connect_timeout, max_count_per_entry,
max_idle_time, socket_domain, htable_init_capacity, max_idle_time, htable_init_capacity, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, extra_data_size, extra_params); extra_data_size, extra_params);
} }
/** /**
@ -444,7 +441,7 @@ static inline void conn_pool_set_server_info(ConnectionInfo *pServerInfo,
snprintf(pServerInfo->ip_addr, sizeof(pServerInfo->ip_addr), snprintf(pServerInfo->ip_addr, sizeof(pServerInfo->ip_addr),
"%s", ip_addr); "%s", ip_addr);
pServerInfo->port = port; pServerInfo->port = port;
pServerInfo->socket_domain = AF_UNSPEC; pServerInfo->af = is_ipv6_addr(ip_addr) ? AF_INET6 : AF_INET;
pServerInfo->sock = -1; pServerInfo->sock = -1;
} }

View File

@ -31,7 +31,7 @@ bool is_local_host_ip(const char *client_ip)
char *p; char *p;
char *pEnd; char *pEnd;
pEnd = g_local_host_ip_addrs + \ pEnd = g_local_host_ip_addrs +
IP_ADDRESS_SIZE * g_local_host_ip_count; IP_ADDRESS_SIZE * g_local_host_ip_count;
for (p=g_local_host_ip_addrs; p<pEnd; p+=IP_ADDRESS_SIZE) for (p=g_local_host_ip_addrs; p<pEnd; p+=IP_ADDRESS_SIZE)
{ {
@ -56,9 +56,8 @@ int insert_into_local_host_ip(const char *client_ip)
return -1; return -1;
} }
strcpy(g_local_host_ip_addrs + \ strcpy(g_local_host_ip_addrs + IP_ADDRESS_SIZE *
IP_ADDRESS_SIZE * g_local_host_ip_count, \ g_local_host_ip_count, client_ip);
client_ip);
g_local_host_ip_count++; g_local_host_ip_count++;
return 1; return 1;
} }
@ -217,3 +216,17 @@ const char *get_first_local_private_ip()
return NULL; return NULL;
} }
void stat_local_host_ip(int *ipv4_count, int *ipv6_count)
{
const char *ip_addr;
*ipv4_count = *ipv6_count = 0;
ip_addr = NULL;
while ((ip_addr=get_next_local_ip(ip_addr)) != NULL) {
if (is_ipv6_addr(ip_addr)) {
++(*ipv6_count);
} else {
++(*ipv4_count);
}
}
}

View File

@ -35,13 +35,15 @@ extern "C" {
#endif #endif
extern int g_local_host_ip_count; extern int g_local_host_ip_count;
extern char g_local_host_ip_addrs[FAST_MAX_LOCAL_IP_ADDRS * \ extern char g_local_host_ip_addrs[FAST_MAX_LOCAL_IP_ADDRS *
IP_ADDRESS_SIZE]; IP_ADDRESS_SIZE];
extern char g_if_alias_prefix[FAST_IF_ALIAS_PREFIX_MAX_SIZE]; extern char g_if_alias_prefix[FAST_IF_ALIAS_PREFIX_MAX_SIZE];
void load_local_host_ip_addrs(); void load_local_host_ip_addrs();
bool is_local_host_ip(const char *client_ip); bool is_local_host_ip(const char *client_ip);
void stat_local_host_ip(int *ipv4_count, int *ipv6_count);
const char *get_first_local_ip(); const char *get_first_local_ip();
const char *get_next_local_ip(const char *previous_ip); const char *get_next_local_ip(const char *previous_ip);

View File

@ -1176,22 +1176,23 @@ char *getHostnameByIp(const char *szIpAddr, char *buff, const int bufferSize)
return buff; return buff;
} }
in_addr_64_t getIpaddrByName(const char *name, char *buff, const int bufferSize) in_addr_64_t getIpaddrByNameEx(const char *name, char *buff,
const int bufferSize, short *af)
{ {
struct addrinfo hints, *res, *p; struct addrinfo hints, *res, *p;
int status;
in_addr_64_t ip_addr; in_addr_64_t ip_addr;
memset(&hints, 0, sizeof hints); memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // 支持IPv4和IPv6 hints.ai_family = AF_UNSPEC; // 支持IPv4和IPv6
status = getaddrinfo(name, NULL, &hints, &res); if (getaddrinfo(name, NULL, &hints, &res) != 0)
if (status != 0)
{ {
*af = AF_UNSPEC;
return INADDR_NONE; return INADDR_NONE;
} }
for (p = res; p != NULL; p = p->ai_next) for (p = res; p != NULL; p = p->ai_next)
{ {
*af = p->ai_family;
if (p->ai_family == AF_INET) // 处理IPv4地址 if (p->ai_family == AF_INET) // 处理IPv4地址
{ {
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr; struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
@ -1225,6 +1226,7 @@ in_addr_64_t getIpaddrByName(const char *name, char *buff, const int bufferSize)
} }
freeaddrinfo(res); freeaddrinfo(res);
*af = AF_UNSPEC;
return INADDR_NONE; return INADDR_NONE;
} }
@ -1279,7 +1281,7 @@ int getIpaddrsByName(const char *name,
} }
} }
ip_addr_arr[ip_count++].socket_domain = res->ai_family; ip_addr_arr[ip_count++].af = res->ai_family;
} }
freeaddrinfo(res0); freeaddrinfo(res0);

View File

@ -61,7 +61,7 @@ typedef struct fast_if_config {
typedef struct ip_addr_s { typedef struct ip_addr_s {
char ip_addr[IP_ADDRESS_SIZE]; char ip_addr[IP_ADDRESS_SIZE];
int socket_domain; int af;
} ip_addr_t; } ip_addr_t;
typedef struct sockaddr_convert_s { typedef struct sockaddr_convert_s {
@ -376,9 +376,18 @@ char *getHostnameByIp(const char *szIpAddr, char *buff, const int bufferSize);
* name: the hostname * name: the hostname
* buff: buffer to store the ip address * buff: buffer to store the ip address
* bufferSize: the buffer size (max bytes) * bufferSize: the buffer size (max bytes)
* af: store the address family
* return: in_addr_64_t, INADDR_NONE for fail * return: in_addr_64_t, INADDR_NONE for fail
*/ */
in_addr_64_t getIpaddrByName(const char *name, char *buff, const int bufferSize); in_addr_64_t getIpaddrByNameEx(const char *name, char *buff,
const int bufferSize, short *af);
static inline in_addr_64_t getIpaddrByName(const char *name,
char *buff, const int bufferSize)
{
short af;
return getIpaddrByNameEx(name, buff, bufferSize, &af);
}
/** get by ip addresses by it's hostname /** get by ip addresses by it's hostname
* parameters: * parameters: