diff --git a/src/connection_pool.c b/src/connection_pool.c index 966cb7d..b82223c 100644 --- a/src/connection_pool.c +++ b/src/connection_pool.c @@ -76,7 +76,7 @@ static void cp_tls_destroy(void *ptr) int conn_pool_init_ex1(ConnectionPool *cp, int connect_timeout, 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 validate_func, void *validate_args, 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_idle_time = max_idle_time; cp->extra_data_size = extra_data_size; - cp->socket_domain = socket_domain; cp->connect_done_callback.func = connect_done_func; cp->connect_done_callback.args = connect_done_args; cp->validate_callback.func = validate_func; @@ -211,7 +210,7 @@ int conn_pool_connect_server_ex1(ConnectionInfo *conn, 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) { return result; @@ -247,9 +246,8 @@ int conn_pool_async_connect_server_ex(ConnectionInfo *conn, close(conn->sock); } - if ((conn->sock=socketCreateEx2(conn->socket_domain, - conn->ip_addr, O_NONBLOCK, bind_ipaddr, - &result)) < 0) + if ((conn->sock=socketCreateEx2(conn->af, conn->ip_addr, + O_NONBLOCK, bind_ipaddr, &result)) < 0) { return result; } @@ -353,7 +351,7 @@ static ConnectionInfo *get_connection(ConnectionPool *cp, memcpy(node->conn->ip_addr, conn->ip_addr, sizeof(conn->ip_addr)); node->conn->port = conn->port; 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->validate_flag = false; *err_no = G_COMMON_CONNECTION_CALLBACKS[conn->comm_type]. @@ -703,22 +701,22 @@ int conn_pool_parse_server_info(const char *pServerStr, pServerInfo->port = (int)strtol(parts[1], &endptr, 10); if ((endptr != NULL && *endptr != '\0') || pServerInfo->port <= 0) { logError("file: "__FILE__", line: %d, " - "host: %s, invalid port: %s!", - __LINE__, pServerStr, parts[1]); + "host: %s, invalid port: %s!", + __LINE__, pServerStr, parts[1]); return EINVAL; } } - if (getIpaddrByName(parts[0], pServerInfo->ip_addr, - sizeof(pServerInfo->ip_addr)) == INADDR_NONE) - { + if (getIpaddrByNameEx(parts[0], pServerInfo->ip_addr, + sizeof(pServerInfo->ip_addr), + &pServerInfo->af) == INADDR_NONE) + { logError("file: "__FILE__", line: %d, " - "host: %s, invalid hostname: %s!", - __LINE__, pServerStr, parts[0]); + "host: %s, invalid hostname: %s!", + __LINE__, pServerStr, parts[0]); return EINVAL; } - pServerInfo->socket_domain = AF_UNSPEC; pServerInfo->sock = -1; pServerInfo->comm_type = fc_comm_type_sock; return 0; diff --git a/src/connection_pool.h b/src/connection_pool.h index 32d7df0..83d660a 100644 --- a/src/connection_pool.h +++ b/src/connection_pool.h @@ -26,6 +26,7 @@ #include "fast_mblock.h" #include "ini_file_reader.h" #include "pthread_func.h" +#include "sockopt.h" #include "hash.h" #ifdef __cplusplus @@ -49,7 +50,7 @@ typedef enum { typedef struct { int sock; 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; bool validate_flag; //for connection pool char ip_addr[IP_ADDRESS_SIZE]; @@ -174,7 +175,6 @@ typedef struct tagConnectionPool { unit: second */ int max_idle_time; - int socket_domain; //socket domain struct fast_mblock_man manager_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 * max_count_per_entry: max connection count per host:port * 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 * connect_done_func: the 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, 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 validate_func, void *validate_args, 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 * max_count_per_entry: max connection count per host:port * max_idle_time: reconnect the server after max idle time in seconds -* socket_domain: the socket domain * return 0 for success, != 0 for error */ 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 socket_domain) + const int max_count_per_entry, const int max_idle_time) { const int htable_init_capacity = 0; const int extra_data_size = 0; const ConnectionExtraParams *extra_params = NULL; return conn_pool_init_ex1(cp, connect_timeout, max_count_per_entry, - max_idle_time, socket_domain, htable_init_capacity, - NULL, NULL, NULL, NULL, extra_data_size, extra_params); + max_idle_time, htable_init_capacity, NULL, NULL, NULL, NULL, + 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, 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 extra_data_size = 0; const ConnectionExtraParams *extra_params = NULL; return conn_pool_init_ex1(cp, connect_timeout, max_count_per_entry, - max_idle_time, socket_domain, htable_init_capacity, - NULL, NULL, NULL, NULL, extra_data_size, extra_params); + max_idle_time, htable_init_capacity, NULL, NULL, NULL, NULL, + 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), "%s", ip_addr); pServerInfo->port = port; - pServerInfo->socket_domain = AF_UNSPEC; + pServerInfo->af = is_ipv6_addr(ip_addr) ? AF_INET6 : AF_INET; pServerInfo->sock = -1; } diff --git a/src/local_ip_func.c b/src/local_ip_func.c index b529970..225ea4c 100644 --- a/src/local_ip_func.c +++ b/src/local_ip_func.c @@ -31,7 +31,7 @@ bool is_local_host_ip(const char *client_ip) char *p; char *pEnd; - pEnd = g_local_host_ip_addrs + \ + pEnd = g_local_host_ip_addrs + IP_ADDRESS_SIZE * g_local_host_ip_count; for (p=g_local_host_ip_addrs; pai_next) { + *af = p->ai_family; if (p->ai_family == AF_INET) // 处理IPv4地址 { 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); + *af = AF_UNSPEC; 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); diff --git a/src/sockopt.h b/src/sockopt.h index 3a0b02f..50a391d 100644 --- a/src/sockopt.h +++ b/src/sockopt.h @@ -61,7 +61,7 @@ typedef struct fast_if_config { typedef struct ip_addr_s { char ip_addr[IP_ADDRESS_SIZE]; - int socket_domain; + int af; } ip_addr_t; typedef struct sockaddr_convert_s { @@ -376,9 +376,18 @@ char *getHostnameByIp(const char *szIpAddr, char *buff, const int bufferSize); * name: the hostname * buff: buffer to store the ip address * bufferSize: the buffer size (max bytes) + * af: store the address family * 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 * parameters: