format ip address for IPv6

use_iouring
YuQing 2024-03-05 10:58:09 +08:00
parent 9a720533ce
commit 226fd0d378
8 changed files with 146 additions and 82 deletions

View File

@ -1,7 +1,8 @@
Version 1.73 2024-02-20 Version 1.73 2024-03-05
* add macro FC_SET_STRING_EMPTY * add macro FC_SET_STRING_EMPTY
* struct fast_task_info remove fields: connect_timeout and network_timeout * struct fast_task_info remove fields: connect_timeout and network_timeout
* format ip address for IPv6
Version 1.72 2024-01-21 Version 1.72 2024-01-21
* call fast_mblock_ref_counter_dec for delay free node correctly * call fast_mblock_ref_counter_dec for delay free node correctly

View File

@ -119,7 +119,8 @@ extern int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int kind);
#endif #endif
// 由于要支持IPv6所以将IP_ADDRESS_SIZE的值由16修改为46 // 由于要支持IPv6所以将IP_ADDRESS_SIZE的值由16修改为46
#define IP_ADDRESS_SIZE INET6_ADDRSTRLEN //46 #define IP_ADDRESS_SIZE INET6_ADDRSTRLEN //46
#define FORMATTED_IP_SIZE (IP_ADDRESS_SIZE + 2)
#define INFINITE_FILE_SIZE (256 * 1024LL * 1024 * 1024 * 1024 * 1024LL) #define INFINITE_FILE_SIZE (256 * 1024LL * 1024 * 1024 * 1024 * 1024LL)
#define FILE_RESOURCE_TAG_STR "file://" #define FILE_RESOURCE_TAG_STR "file://"

View File

@ -204,6 +204,7 @@ int conn_pool_connect_server_ex1(ConnectionInfo *conn,
const char *bind_ipaddr, const bool log_connect_error) const char *bind_ipaddr, const bool log_connect_error)
{ {
int result; int result;
char formatted_ip[FORMATTED_IP_SIZE];
if (conn->sock >= 0) if (conn->sock >= 0)
{ {
@ -221,12 +222,12 @@ int conn_pool_connect_server_ex1(ConnectionInfo *conn,
{ {
if (log_connect_error) if (log_connect_error)
{ {
FC_FORMAT_IP_ADDRESS(conn->ip_addr, new_ip_addr); format_ip_address(conn->ip_addr, formatted_ip);
logError("file: "__FILE__", line: %d, " logError("file: "__FILE__", line: %d, "
"connect to %s%sserver %s:%u fail, errno: %d, " "connect to %s%sserver %s:%u fail, errno: %d, "
"error info: %s", __LINE__, service_name != NULL ? "error info: %s", __LINE__, service_name != NULL ?
service_name : "", service_name != NULL ? " " : "", service_name : "", service_name != NULL ? " " : "",
new_ip_addr, conn->port, result, STRERROR(result)); formatted_ip, conn->port, result, STRERROR(result));
} }
close(conn->sock); close(conn->sock);
@ -241,6 +242,7 @@ int conn_pool_async_connect_server_ex(ConnectionInfo *conn,
const char *bind_ipaddr) const char *bind_ipaddr)
{ {
int result; int result;
char formatted_ip[FORMATTED_IP_SIZE];
if (conn->sock >= 0) if (conn->sock >= 0)
{ {
@ -256,10 +258,10 @@ int conn_pool_async_connect_server_ex(ConnectionInfo *conn,
result = asyncconnectserverbyip(conn->sock, conn->ip_addr, conn->port); result = asyncconnectserverbyip(conn->sock, conn->ip_addr, conn->port);
if (!(result == 0 || result == EINPROGRESS)) if (!(result == 0 || result == EINPROGRESS))
{ {
FC_FORMAT_IP_ADDRESS(conn->ip_addr, new_ip_addr); format_ip_address(conn->ip_addr, formatted_ip);
logError("file: "__FILE__", line: %d, " logError("file: "__FILE__", line: %d, "
"connect to server %s:%u fail, errno: %d, " "connect to server %s:%u fail, errno: %d, "
"error info: %s", __LINE__, new_ip_addr, "error info: %s", __LINE__, formatted_ip,
conn->port, result, STRERROR(result)); conn->port, result, STRERROR(result));
close(conn->sock); close(conn->sock);
conn->sock = -1; conn->sock = -1;
@ -281,6 +283,7 @@ static ConnectionInfo *get_connection(ConnectionPool *cp,
ConnectionManager *cm; ConnectionManager *cm;
ConnectionNode *node; ConnectionNode *node;
ConnectionInfo *ci; ConnectionInfo *ci;
char formatted_ip[FORMATTED_IP_SIZE];
time_t current_time; time_t current_time;
pthread_mutex_lock(&cp->lock); pthread_mutex_lock(&cp->lock);
@ -321,13 +324,13 @@ static ConnectionInfo *get_connection(ConnectionPool *cp,
if ((cp->max_count_per_entry > 0) && if ((cp->max_count_per_entry > 0) &&
(cm->total_count >= cp->max_count_per_entry)) (cm->total_count >= cp->max_count_per_entry))
{ {
FC_FORMAT_IP_ADDRESS(conn->ip_addr, new_ip_addr); format_ip_address(conn->ip_addr, formatted_ip);
*err_no = ENOSPC; *err_no = ENOSPC;
logError("file: "__FILE__", line: %d, " logError("file: "__FILE__", line: %d, "
"connections: %d of %s%sserver %s:%u exceed limit: %d", "connections: %d of %s%sserver %s:%u exceed limit: %d",
__LINE__, cm->total_count, service_name != NULL ? __LINE__, cm->total_count, service_name != NULL ?
service_name : "", service_name != NULL ? " " : "", service_name : "", service_name != NULL ? " " : "",
new_ip_addr, conn->port, cp->max_count_per_entry); formatted_ip, conn->port, cp->max_count_per_entry);
pthread_mutex_unlock(&cm->lock); pthread_mutex_unlock(&cm->lock);
return NULL; return NULL;
} }
@ -377,12 +380,15 @@ static ConnectionInfo *get_connection(ConnectionPool *cp,
return NULL; return NULL;
} }
logDebug("file: "__FILE__", line: %d, " \ if (FC_LOG_BY_LEVEL(LOG_DEBUG)) {
"server %s:%u, new connection: %d, " \ format_ip_address(conn->ip_addr, formatted_ip);
"total_count: %d, free_count: %d", \ logDebug("file: "__FILE__", line: %d, "
__LINE__, conn->ip_addr, conn->port, \ "server %s:%u, new connection: %d, "
node->conn->sock, cm->total_count, \ "total_count: %d, free_count: %d",
cm->free_count); __LINE__, formatted_ip, conn->port,
node->conn->sock, cm->total_count,
cm->free_count);
}
return node->conn; return node->conn;
} }
else else
@ -425,13 +431,16 @@ static ConnectionInfo *get_connection(ConnectionPool *cp,
{ {
cm->total_count--; cm->total_count--;
logDebug("file: "__FILE__", line: %d, " if (FC_LOG_BY_LEVEL(LOG_DEBUG)) {
"server %s:%u, connection: %d idle " format_ip_address(conn->ip_addr, formatted_ip);
"time: %d exceeds max idle time: %d, " logDebug("file: "__FILE__", line: %d, "
"total_count: %d, free_count: %d", __LINE__, "server %s:%u, connection: %d idle "
conn->ip_addr, conn->port, ci->sock, (int) "time: %d exceeds max idle time: %d, "
(current_time - node->atime), cp->max_idle_time, "total_count: %d, free_count: %d", __LINE__,
cm->total_count, cm->free_count); formatted_ip, conn->port, ci->sock, (int)
(current_time - node->atime), cp->max_idle_time,
cm->total_count, cm->free_count);
}
G_COMMON_CONNECTION_CALLBACKS[ci->comm_type]. G_COMMON_CONNECTION_CALLBACKS[ci->comm_type].
close_connection(ci); close_connection(ci);
@ -440,11 +449,16 @@ static ConnectionInfo *get_connection(ConnectionPool *cp,
} }
pthread_mutex_unlock(&cm->lock); pthread_mutex_unlock(&cm->lock);
logDebug("file: "__FILE__", line: %d, " \
"server %s:%u, reuse connection: %d, " \ if (FC_LOG_BY_LEVEL(LOG_DEBUG)) {
"total_count: %d, free_count: %d", format_ip_address(conn->ip_addr, formatted_ip);
__LINE__, conn->ip_addr, conn->port, logDebug("file: "__FILE__", line: %d, "
ci->sock, cm->total_count, cm->free_count); "server %s:%u, reuse connection: %d, "
"total_count: %d, free_count: %d",
__LINE__, formatted_ip, conn->port,
ci->sock, cm->total_count, cm->free_count);
}
*err_no = 0; *err_no = 0;
return ci; return ci;
} }
@ -526,26 +540,27 @@ static int close_connection(ConnectionPool *cp, ConnectionInfo *conn,
{ {
ConnectionManager *cm; ConnectionManager *cm;
ConnectionNode *node; ConnectionNode *node;
char formatted_ip[FORMATTED_IP_SIZE];
pthread_mutex_lock(&cp->lock); pthread_mutex_lock(&cp->lock);
cm = (ConnectionManager *)fc_hash_find(&cp->hash_array, key->str, key->len); cm = (ConnectionManager *)fc_hash_find(&cp->hash_array, key->str, key->len);
pthread_mutex_unlock(&cp->lock); pthread_mutex_unlock(&cp->lock);
if (cm == NULL) if (cm == NULL)
{ {
FC_FORMAT_IP_ADDRESS(conn->ip_addr, new_ip_addr); format_ip_address(conn->ip_addr, formatted_ip);
logError("file: "__FILE__", line: %d, " logError("file: "__FILE__", line: %d, "
"hash entry of server %s:%u not exist", "hash entry of server %s:%u not exist",
__LINE__, new_ip_addr, conn->port); __LINE__, formatted_ip, conn->port);
return ENOENT; return ENOENT;
} }
node = (ConnectionNode *)((char *)conn - sizeof(ConnectionNode)); node = (ConnectionNode *)((char *)conn - sizeof(ConnectionNode));
if (node->manager != cm) if (node->manager != cm)
{ {
FC_FORMAT_IP_ADDRESS(conn->ip_addr, new_ip_addr); format_ip_address(conn->ip_addr, formatted_ip);
logError("file: "__FILE__", line: %d, " logError("file: "__FILE__", line: %d, "
"manager of server entry %s:%u is invalid!", "manager of server entry %s:%u is invalid!",
__LINE__, new_ip_addr, conn->port); __LINE__, formatted_ip, conn->port);
return EINVAL; return EINVAL;
} }
@ -554,11 +569,14 @@ static int close_connection(ConnectionPool *cp, ConnectionInfo *conn,
{ {
cm->total_count--; cm->total_count--;
logDebug("file: "__FILE__", line: %d, " if (FC_LOG_BY_LEVEL(LOG_DEBUG)) {
"server %s:%u, release connection: %d, " format_ip_address(conn->ip_addr, formatted_ip);
"total_count: %d, free_count: %d", logDebug("file: "__FILE__", line: %d, "
__LINE__, conn->ip_addr, conn->port, "server %s:%u, release connection: %d, "
conn->sock, cm->total_count, cm->free_count); "total_count: %d, free_count: %d",
__LINE__, formatted_ip, conn->port,
conn->sock, cm->total_count, cm->free_count);
}
G_COMMON_CONNECTION_CALLBACKS[conn->comm_type]. G_COMMON_CONNECTION_CALLBACKS[conn->comm_type].
close_connection(conn); close_connection(conn);
@ -578,11 +596,14 @@ static int close_connection(ConnectionPool *cp, ConnectionInfo *conn,
cm->head = node; cm->head = node;
cm->free_count++; cm->free_count++;
logDebug("file: "__FILE__", line: %d, " \ if (FC_LOG_BY_LEVEL(LOG_DEBUG)) {
"server %s:%u, free connection: %d, " \ format_ip_address(conn->ip_addr, formatted_ip);
"total_count: %d, free_count: %d", logDebug("file: "__FILE__", line: %d, "
__LINE__, conn->ip_addr, conn->port, "server %s:%u, free connection: %d, "
conn->sock, cm->total_count, cm->free_count); "total_count: %d, free_count: %d",
__LINE__, formatted_ip, conn->port,
conn->sock, cm->total_count, cm->free_count);
}
} }
pthread_mutex_unlock(&cm->lock); pthread_mutex_unlock(&cm->lock);

View File

@ -128,6 +128,7 @@ static int fast_multi_sock_client_do_send(FastMultiSockClient *client,
{ {
int bytes; int bytes;
int result; int result;
char formatted_ip[FORMATTED_IP_SIZE];
result = 0; result = 0;
while (entry->remain > 0) { while (entry->remain > 0) {
@ -139,27 +140,26 @@ static int fast_multi_sock_client_do_send(FastMultiSockClient *client,
break; break;
} else if (errno == EINTR) { //should retry } else if (errno == EINTR) { //should retry
logDebug("file: "__FILE__", line: %d, " logDebug("file: "__FILE__", line: %d, "
"server: %s:%u, ignore interupt signal", "server: %s:%u, ignore interupt signal", __LINE__,
__LINE__, entry->conn->ip_addr, format_ip_address(entry->conn->ip_addr, formatted_ip),
entry->conn->port); entry->conn->port);
continue; continue;
} else { } else {
result = errno != 0 ? errno : ECONNRESET; result = errno != 0 ? errno : ECONNRESET;
logError("file: "__FILE__", line: %d, " logError("file: "__FILE__", line: %d, "
"send to server %s:%u fail, " "send to server %s:%u fail, "
"errno: %d, error info: %s", "errno: %d, error info: %s", __LINE__,
__LINE__, entry->conn->ip_addr, format_ip_address(entry->conn->ip_addr, formatted_ip),
entry->conn->port, entry->conn->port, result, strerror(result));
result, strerror(result));
break; break;
} }
} else if (bytes == 0) { } else if (bytes == 0) {
logError("file: "__FILE__", line: %d, " logError("file: "__FILE__", line: %d, "
"send to server %s:%u, sock: %d fail, " "send to server %s:%u, sock: %d fail, "
"connection disconnected", "connection disconnected", __LINE__,
__LINE__, entry->conn->ip_addr, entry->conn->port, format_ip_address(entry->conn->ip_addr, formatted_ip),
entry->conn->sock); entry->conn->port, entry->conn->sock);
result = ECONNRESET; result = ECONNRESET;
break; break;
@ -190,6 +190,7 @@ static int fast_multi_sock_client_send_data(FastMultiSockClient *client,
{ {
int i; int i;
int result; int result;
char formatted_ip[FORMATTED_IP_SIZE];
for (i=0; i<client->entry_count; i++) { for (i=0; i<client->entry_count; i++) {
client->entries[i].remain = send_buffer->length; client->entries[i].remain = send_buffer->length;
@ -202,9 +203,9 @@ static int fast_multi_sock_client_send_data(FastMultiSockClient *client,
client->entries[i].error_no = ENOTCONN; client->entries[i].error_no = ENOTCONN;
client->entries[i].done = true; client->entries[i].done = true;
logError("file: "__FILE__", line: %d, " logError("file: "__FILE__", line: %d, "
"NOT connected to %s:%u", "NOT connected to %s:%u", __LINE__,
__LINE__, client->entries[i].conn->ip_addr, format_ip_address(client->entries[i].conn->ip_addr,
client->entries[i].conn->port); formatted_ip), client->entries[i].conn->port);
continue; continue;
} }
@ -244,6 +245,7 @@ static int fast_multi_sock_client_do_recv(FastMultiSockClient *client,
{ {
int bytes; int bytes;
int result; int result;
char formatted_ip[FORMATTED_IP_SIZE];
result = 0; result = 0;
while (entry->remain > 0) { while (entry->remain > 0) {
@ -254,27 +256,26 @@ static int fast_multi_sock_client_do_recv(FastMultiSockClient *client,
break; break;
} else if (errno == EINTR) { //should retry } else if (errno == EINTR) { //should retry
logDebug("file: "__FILE__", line: %d, " logDebug("file: "__FILE__", line: %d, "
"server: %s:%u, ignore interupt signal", "server: %s:%u, ignore interupt signal", __LINE__,
__LINE__, entry->conn->ip_addr, format_ip_address(entry->conn->ip_addr, formatted_ip),
entry->conn->port); entry->conn->port);
continue; continue;
} else { } else {
result = errno != 0 ? errno : ECONNRESET; result = errno != 0 ? errno : ECONNRESET;
logError("file: "__FILE__", line: %d, " logError("file: "__FILE__", line: %d, "
"server: %s:%u, recv failed, " "server: %s:%u, recv failed, "
"errno: %d, error info: %s", "errno: %d, error info: %s", __LINE__,
__LINE__, entry->conn->ip_addr, format_ip_address(entry->conn->ip_addr, formatted_ip),
entry->conn->port, entry->conn->port, result, strerror(result));
result, strerror(result));
break; break;
} }
} else if (bytes == 0) { } else if (bytes == 0) {
logError("file: "__FILE__", line: %d, " logError("file: "__FILE__", line: %d, "
"server: %s:%u, sock: %d, recv failed, " "server: %s:%u, sock: %d, recv failed, "
"connection disconnected", "connection disconnected", __LINE__,
__LINE__, entry->conn->ip_addr, entry->conn->port, format_ip_address(entry->conn->ip_addr, formatted_ip),
entry->conn->sock); entry->conn->port, entry->conn->sock);
result = ECONNRESET; result = ECONNRESET;
break; break;
@ -289,8 +290,8 @@ static int fast_multi_sock_client_do_recv(FastMultiSockClient *client,
body_length = client->get_body_length_func(&entry->recv_buffer); body_length = client->get_body_length_func(&entry->recv_buffer);
if (body_length < 0) { if (body_length < 0) {
logError("file: "__FILE__", line: %d, " logError("file: "__FILE__", line: %d, "
"server: %s:%u, body_length: %d < 0", "server: %s:%u, body_length: %d < 0", __LINE__,
__LINE__, entry->conn->ip_addr, format_ip_address(entry->conn->ip_addr, formatted_ip),
entry->conn->port, body_length); entry->conn->port, body_length);
result = EPIPE; result = EPIPE;
break; break;
@ -320,6 +321,7 @@ static int fast_multi_sock_client_deal_io(FastMultiSockClient *client)
int index; int index;
int remain_timeout; int remain_timeout;
FastMultiSockEntry *entry; FastMultiSockEntry *entry;
char formatted_ip[FORMATTED_IP_SIZE];
while (client->pulling_count > 0) { while (client->pulling_count > 0) {
remain_timeout = client->deadline_time_ms - remain_timeout = client->deadline_time_ms -
@ -329,7 +331,7 @@ static int fast_multi_sock_client_deal_io(FastMultiSockClient *client)
} }
count = ioevent_poll_ex(&client->ioevent, remain_timeout); count = ioevent_poll_ex(&client->ioevent, remain_timeout);
logInfo("poll count: %d\n", count); //logInfo("poll count: %d\n", count);
for (index=0; index<count; index++) { for (index=0; index<count; index++) {
event = IOEVENT_GET_EVENTS(&client->ioevent, index); event = IOEVENT_GET_EVENTS(&client->ioevent, index);
entry = (FastMultiSockEntry *)IOEVENT_GET_DATA( entry = (FastMultiSockEntry *)IOEVENT_GET_DATA(
@ -337,15 +339,15 @@ static int fast_multi_sock_client_deal_io(FastMultiSockClient *client)
if (event & IOEVENT_ERROR) { if (event & IOEVENT_ERROR) {
logError("file: "__FILE__", line: %d, " logError("file: "__FILE__", line: %d, "
"server: %s:%u, recv error event: %d, " "server: %s:%u, recv error event: %d, connection "
"connection reset", __LINE__, "reset", __LINE__, format_ip_address(entry->conn->
entry->conn->ip_addr, entry->conn->port, event); ip_addr, formatted_ip), entry->conn->port, event);
fast_multi_sock_client_finish(client, entry, ECONNRESET); fast_multi_sock_client_finish(client, entry, ECONNRESET);
continue; continue;
} }
logInfo("sock: %d, event: %d", entry->conn->sock, event); //logInfo("sock: %d, event: %d", entry->conn->sock, event);
result = entry->io_callback(client, entry); result = entry->io_callback(client, entry);
if (result != 0 || entry->remain == 0) { if (result != 0 || entry->remain == 0) {
@ -366,9 +368,9 @@ static int fast_multi_sock_client_deal_io(FastMultiSockClient *client)
fast_multi_sock_client_finish(client, fast_multi_sock_client_finish(client,
client->entries + i, ETIMEDOUT); client->entries + i, ETIMEDOUT);
logError("file: "__FILE__", line: %d, " logError("file: "__FILE__", line: %d, "
"recv from %s:%u timedout", "recv from %s:%u timedout", __LINE__,
__LINE__, client->entries[i].conn->ip_addr, format_ip_address(client->entries[i].conn->ip_addr,
client->entries[i].conn->port); formatted_ip), client->entries[i].conn->port);
} }
} }
} }

View File

@ -244,6 +244,7 @@ static int fc_server_check_ip_port(FCServerConfig *ctx,
FCServerMap *previous; FCServerMap *previous;
FCServerMap *current; FCServerMap *current;
FCServerMap *end; FCServerMap *end;
char formatted_ip[FORMATTED_IP_SIZE];
int id1; int id1;
int id2; int id2;
@ -261,8 +262,8 @@ static int fc_server_check_ip_port(FCServerConfig *ctx,
logError("file: "__FILE__", line: %d, " logError("file: "__FILE__", line: %d, "
"config file: %s, duplicate ip:port %s:%u, " "config file: %s, duplicate ip:port %s:%u, "
"the server ids: %d, %d", __LINE__, "the server ids: %d, %d", __LINE__,
config_filename, previous->ip_addr.str, config_filename, format_ip_address(previous->ip_addr.str,
previous->port, id1, id2); formatted_ip), previous->port, id1, id2);
return EEXIST; return EEXIST;
} }
@ -698,6 +699,7 @@ static int check_addresses_duplicate(FCServerConfig *ctx,
FCAddressInfo **ppaddr; FCAddressInfo **ppaddr;
FCAddressInfo **ppend; FCAddressInfo **ppend;
FCAddressInfo **pprevious; FCAddressInfo **pprevious;
char formatted_ip[FORMATTED_IP_SIZE];
if (group_addr->address_array.count <= 1) { if (group_addr->address_array.count <= 1) {
return 0; return 0;
@ -715,7 +717,8 @@ static int check_addresses_duplicate(FCServerConfig *ctx,
config_filename, section_name, config_filename, section_name,
group_addr->server_group->group_name.len, group_addr->server_group->group_name.len,
group_addr->server_group->group_name.str, group_addr->server_group->group_name.str,
(*ppaddr)->conn.ip_addr, (*ppaddr)->conn.port); format_ip_address((*ppaddr)->conn.ip_addr, formatted_ip),
(*ppaddr)->conn.port);
return EEXIST; return EEXIST;
} }
pprevious = ppaddr; pprevious = ppaddr;
@ -899,6 +902,7 @@ static int fc_server_set_host(FCServerConfig *ctx, FCServerInfo *server,
FCGroupAddresses *group_addr; FCGroupAddresses *group_addr;
const FCAddressInfo *new_addr; const FCAddressInfo *new_addr;
FCAddressInfo addr_holder; FCAddressInfo addr_holder;
char formatted_ip[FORMATTED_IP_SIZE];
int result; int result;
int count; int count;
int group_index; int group_index;
@ -964,7 +968,8 @@ static int fc_server_set_host(FCServerConfig *ctx, FCServerInfo *server,
"config filename: %s, section: %s, " "config filename: %s, section: %s, "
"host %s:%u belongs to %d groups", "host %s:%u belongs to %d groups",
__LINE__, config_filename, section_name, __LINE__, config_filename, section_name,
addr->conn.ip_addr, addr->conn.port, count); format_ip_address(addr->conn.ip_addr, formatted_ip),
addr->conn.port, count);
return EEXIST; return EEXIST;
} }
@ -1523,6 +1528,7 @@ static void fc_group_servers_to_string(FCServerConfig *ctx,
{ {
FCAddressInfo **addr; FCAddressInfo **addr;
FCAddressInfo **end; FCAddressInfo **end;
char formatted_ip[FORMATTED_IP_SIZE];
end = gaddr->address_array.addrs + gaddr->address_array.count; end = gaddr->address_array.addrs + gaddr->address_array.count;
for (addr=gaddr->address_array.addrs; addr<end; addr++) { for (addr=gaddr->address_array.addrs; addr<end; addr++) {
@ -1536,7 +1542,8 @@ static void fc_group_servers_to_string(FCServerConfig *ctx,
SERVER_ITEM_HOST_AFFIX_STR); SERVER_ITEM_HOST_AFFIX_STR);
} }
fast_buffer_append(buffer, " = %s:%u\n", fast_buffer_append(buffer, " = %s:%u\n",
(*addr)->conn.ip_addr, (*addr)->conn.port); format_ip_address((*addr)->conn.ip_addr, formatted_ip),
(*addr)->conn.port);
} }
} }
@ -1641,11 +1648,13 @@ static void fc_server_log_group_servers(FCGroupAddresses *gaddr)
{ {
FCAddressInfo **addr; FCAddressInfo **addr;
FCAddressInfo **end; FCAddressInfo **end;
char formatted_ip[FORMATTED_IP_SIZE];
end = gaddr->address_array.addrs + gaddr->address_array.count; end = gaddr->address_array.addrs + gaddr->address_array.count;
for (addr=gaddr->address_array.addrs; addr<end; addr++) { for (addr=gaddr->address_array.addrs; addr<end; addr++) {
logInfo(" %d. %s:%u", (int)(addr - gaddr->address_array.addrs + 1), logInfo(" %d. %s:%u", (int)(addr - gaddr->address_array.addrs + 1),
(*addr)->conn.ip_addr, (*addr)->conn.port); format_ip_address((*addr)->conn.ip_addr, formatted_ip),
(*addr)->conn.port);
} }
} }

View File

@ -816,7 +816,6 @@ bool is_private_ip(const char* ip);
*/ */
int parseAddress(char *src, char *parts[2]); int parseAddress(char *src, char *parts[2]);
/** get current time in ns /** get current time in ns
* return: current time * return: current time
*/ */

View File

@ -1056,6 +1056,7 @@ int socketClientEx2(int af, const char *server_ip,
{ {
int sock; int sock;
bool auto_detect; bool auto_detect;
char formatted_ip[FORMATTED_IP_SIZE];
sock = socketCreateEx2(af, server_ip, sock = socketCreateEx2(af, server_ip,
flags, bind_ipaddr, err_no); flags, bind_ipaddr, err_no);
@ -1069,11 +1070,11 @@ int socketClientEx2(int af, const char *server_ip,
server_port, timeout, auto_detect); server_port, timeout, auto_detect);
if (*err_no != 0) if (*err_no != 0)
{ {
FC_FORMAT_IP_ADDRESS(server_ip, new_ip_addr); format_ip_address(server_ip, formatted_ip);
logError("file: "__FILE__", line: %d, " logError("file: "__FILE__", line: %d, "
"connect to %s:%u fail, " "connect to %s:%u fail, "
"errno: %d, error info: %s", "errno: %d, error info: %s",
__LINE__, new_ip_addr, server_port, __LINE__, formatted_ip, server_port,
*err_no, STRERROR(*err_no)); *err_no, STRERROR(*err_no));
close(sock); close(sock);
return -4; return -4;

View File

@ -18,9 +18,10 @@
#ifndef _SOCKETOPT_H_ #ifndef _SOCKETOPT_H_
#define _SOCKETOPT_H_ #define _SOCKETOPT_H_
#include <net/if.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <fcntl.h> #include <fcntl.h>
#include <net/if.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
@ -53,7 +54,7 @@
#define FAST_WRITE_BUFF_SIZE (256 * 1024) #define FAST_WRITE_BUFF_SIZE (256 * 1024)
#define FC_FORMAT_IP_ADDRESS(old_ip_addr, new_ip_addr) \ #define FC_FORMAT_IP_ADDRESS(old_ip_addr, new_ip_addr) \
char new_ip_addr[IP_ADDRESS_SIZE + 2]; \ char new_ip_addr[FORMATTED_IP_SIZE]; \
do { \ do { \
if (is_ipv6_addr(old_ip_addr)) { \ if (is_ipv6_addr(old_ip_addr)) { \
sprintf(new_ip_addr, "[%s]", old_ip_addr); \ sprintf(new_ip_addr, "[%s]", old_ip_addr); \
@ -714,6 +715,35 @@ static inline bool is_ipv6_addr(const char *ip)
return (*ip == ':' || strchr(ip, ':') != NULL); //ipv6 return (*ip == ':' || strchr(ip, ':') != NULL); //ipv6
} }
static inline const char *format_ip_address(const char *ip, char *buff)
{
if (is_ipv6_addr(ip))
{
sprintf(buff, "[%s]", ip);
}
else
{
strcpy(buff, ip);
}
return buff;
}
static inline const char *format_ip_port(const char *ip,
const int port, char *buff)
{
if (is_ipv6_addr(ip))
{
sprintf(buff, "[%s]:%u", ip, port);
}
else
{
sprintf(buff, "%s:%u", ip, port);
}
return buff;
}
void tcp_set_try_again_when_interrupt(const bool value); void tcp_set_try_again_when_interrupt(const bool value);
static inline void tcp_dont_try_again_when_interrupt() static inline void tcp_dont_try_again_when_interrupt()