diff --git a/client/client_func.c b/client/client_func.c index 99ab50f..43b1d3d 100644 --- a/client/client_func.c +++ b/client/client_func.c @@ -241,7 +241,7 @@ static int fdfs_get_params_from_tracker(bool *use_storage_id) continue_flag = false; if ((result=fdfs_get_ini_context_from_tracker(&g_tracker_group, - &iniContext, &continue_flag, false, NULL)) != 0) + &iniContext, &continue_flag)) != 0) { return result; } diff --git a/conf/storage.conf b/conf/storage.conf index 273b1f7..3b2691c 100644 --- a/conf/storage.conf +++ b/conf/storage.conf @@ -28,6 +28,14 @@ client_bind = true # the storage server port port = 23000 +# the address family of service, value list: +## IPv4: IPv4 stack +## IPv6: IPv6 stack +## auto: auto detect, IPv4 first, then IPv6 +## both: IPv4 and IPv6 dual stacks +# default value is auto +address_family = auto + # connect timeout in seconds # default value is 30 # Note: in the intranet network (LAN), 2 seconds is enough. diff --git a/conf/tracker.conf b/conf/tracker.conf index 8280838..84c830f 100644 --- a/conf/tracker.conf +++ b/conf/tracker.conf @@ -15,6 +15,14 @@ bind_addr = # the tracker server port port = 22122 +# the address family of service, value list: +## IPv4: IPv4 stack +## IPv6: IPv6 stack +## auto: auto detect, IPv4 first, then IPv6 +## both: IPv4 and IPv6 dual stacks +# default value is auto +address_family = auto + # connect timeout in seconds # default value is 30 # Note: in the intranet network (LAN), 2 seconds is enough. diff --git a/storage/fdfs_storaged.c b/storage/fdfs_storaged.c index e0ab1a7..643d4a7 100644 --- a/storage/fdfs_storaged.c +++ b/storage/fdfs_storaged.c @@ -351,14 +351,30 @@ static void sigAlarmHandler(int sig) logDebug("file: "__FILE__", line: %d, " \ "signal server to quit...", __LINE__); - if (*SF_G_INNER_BIND_ADDR != '\0') - { - strcpy(server.ip_addr, SF_G_INNER_BIND_ADDR); - } - else - { - strcpy(server.ip_addr, "127.0.0.1"); - } + if (SF_G_IPV4_ENABLED) + { + server.af = AF_INET; + if (*SF_G_INNER_BIND_ADDR4 != '\0') + { + strcpy(server.ip_addr, SF_G_INNER_BIND_ADDR4); + } + else + { + strcpy(server.ip_addr, LOCAL_LOOPBACK_IPv4); + } + } + else + { + server.af = AF_INET6; + if (*SF_G_INNER_BIND_ADDR6 != '\0') + { + strcpy(server.ip_addr, SF_G_INNER_BIND_ADDR6); + } + else + { + strcpy(server.ip_addr, LOCAL_LOOPBACK_IPv6); + } + } server.port = SF_G_INNER_PORT; server.sock = -1; diff --git a/storage/storage_dump.c b/storage/storage_dump.c index 8be021b..dc18e7a 100644 --- a/storage/storage_dump.c +++ b/storage/storage_dump.c @@ -93,7 +93,8 @@ static int fdfs_dump_global_vars(char *buff, const int buffSize) "g_check_file_duplicate=%d\n" "g_key_namespace=%s\n" "g_namespace_len=%d\n" - "SF_G_INNER_BIND_ADDR=%s\n" + "bind_addr_ipv4=%s\n" + "bind_addr_ipv6=%s\n" "g_client_bind_addr=%d\n" "g_storage_ip_changed_auto_adjust=%d\n" "g_thread_kill_done=%d\n" @@ -188,7 +189,8 @@ static int fdfs_dump_global_vars(char *buff, const int buffSize) , g_check_file_duplicate , g_key_namespace , g_namespace_len - , SF_G_INNER_BIND_ADDR + , SF_G_INNER_BIND_ADDR4 + , SF_G_INNER_BIND_ADDR6 , g_client_bind_addr , g_storage_ip_changed_auto_adjust , g_thread_kill_done diff --git a/storage/storage_ip_changed_dealer.c b/storage/storage_ip_changed_dealer.c index 5e58505..a8ac10f 100644 --- a/storage/storage_ip_changed_dealer.c +++ b/storage/storage_ip_changed_dealer.c @@ -146,7 +146,8 @@ int storage_get_my_tracker_client_ip() for (i=0; i < 3; i++) { conn = tracker_connect_server_no_pool_ex(pTServer, - g_client_bind_addr ? SF_G_INNER_BIND_ADDR : NULL, + (g_client_bind_addr ? SF_G_INNER_BIND_ADDR4 : NULL), + (g_client_bind_addr ? SF_G_INNER_BIND_ADDR6 : NULL), &result, false); if (conn != NULL) { @@ -234,7 +235,8 @@ static int storage_report_storage_ip_addr() for (i=0; i < 3; i++) { conn = tracker_connect_server_no_pool_ex(pTServer, - g_client_bind_addr ? SF_G_INNER_BIND_ADDR : NULL, + (g_client_bind_addr ? SF_G_INNER_BIND_ADDR4 : NULL), + (g_client_bind_addr ? SF_G_INNER_BIND_ADDR6 : NULL), &result, false); if (conn != NULL) { @@ -304,7 +306,8 @@ int storage_changelog_req() for (i=0; i < 3; i++) { conn = tracker_connect_server_no_pool_ex(pTServer, - g_client_bind_addr ? SF_G_INNER_BIND_ADDR : NULL, + (g_client_bind_addr ? SF_G_INNER_BIND_ADDR4 : NULL), + (g_client_bind_addr ? SF_G_INNER_BIND_ADDR6 : NULL), &result, false); if (conn != NULL) { diff --git a/storage/storage_param_getter.c b/storage/storage_param_getter.c index e59a3b0..d9f72c8 100644 --- a/storage/storage_param_getter.c +++ b/storage/storage_param_getter.c @@ -74,9 +74,10 @@ int storage_get_params_from_tracker() char reserved_space_str[32]; char *pIdType; - if ((result=fdfs_get_ini_context_from_tracker(&g_tracker_group, - &iniContext, (bool * volatile)&SF_G_CONTINUE_FLAG, - g_client_bind_addr, SF_G_INNER_BIND_ADDR)) != 0) + if ((result=fdfs_get_ini_context_from_tracker_ex(&g_tracker_group, + &iniContext, (bool * volatile)&SF_G_CONTINUE_FLAG, + g_client_bind_addr, SF_G_INNER_BIND_ADDR4, + SF_G_INNER_BIND_ADDR6)) != 0) { return result; } diff --git a/storage/storage_sync.c b/storage/storage_sync.c index 26a275b..2c468ff 100644 --- a/storage/storage_sync.c +++ b/storage/storage_sync.c @@ -2050,7 +2050,9 @@ int storage_report_storage_status(const char *storage_id, \ for (i=0; i < 3; i++) { conn = tracker_connect_server_no_pool_ex(pTServer, - g_client_bind_addr ? SF_G_INNER_BIND_ADDR : NULL, &result, false); + (g_client_bind_addr ? SF_G_INNER_BIND_ADDR4 : NULL), + (g_client_bind_addr ? SF_G_INNER_BIND_ADDR6 : NULL), + &result, false); if (conn != NULL) { break; @@ -2146,7 +2148,9 @@ static int storage_reader_sync_init_req(StorageBinLogReader *pReader) while (SF_G_CONTINUE_FLAG) { conn = tracker_connect_server_no_pool_ex(pTServer, - g_client_bind_addr ? SF_G_INNER_BIND_ADDR : NULL, &result, true); + (g_client_bind_addr ? SF_G_INNER_BIND_ADDR4 : NULL), + (g_client_bind_addr ? SF_G_INNER_BIND_ADDR6 : NULL), + &result, true); if (conn != NULL) { break; diff --git a/storage/storage_sync_func.c b/storage/storage_sync_func.c index b679281..08fb4e1 100644 --- a/storage/storage_sync_func.c +++ b/storage/storage_sync_func.c @@ -39,6 +39,7 @@ void storage_sync_connect_storage_server_ex(const FDFSStorageBrief *pStorage, int i; FDFSMultiIP ip_addrs; FDFSMultiIP *multi_ip; + const char *bind_addr; multi_ip = NULL; if (g_use_storage_id) @@ -83,9 +84,17 @@ void storage_sync_connect_storage_server_ex(const FDFSStorageBrief *pStorage, for (i=0; iip_addr, ip_addrs.ips[i].address); + if (g_client_bind_addr) + { + bind_addr = is_ipv6_addr(conn->ip_addr) ? + SF_G_INNER_BIND_ADDR6 : SF_G_INNER_BIND_ADDR4; + } + else + { + bind_addr = NULL; + } conn->sock = socketCreateExAuto(conn->ip_addr, - O_NONBLOCK, g_client_bind_addr ? - SF_G_INNER_BIND_ADDR : NULL, &result); + O_NONBLOCK, bind_addr, &result); if (conn->sock < 0) { logCrit("file: "__FILE__", line: %d, " diff --git a/storage/tracker_client_thread.c b/storage/tracker_client_thread.c index 4f05434..c034b41 100644 --- a/storage/tracker_client_thread.c +++ b/storage/tracker_client_thread.c @@ -257,9 +257,10 @@ static void *tracker_report_thread_entrance(void *arg) } conn = tracker_connect_server_no_pool_ex(pTrackerServer, - g_client_bind_addr ? SF_G_INNER_BIND_ADDR : NULL, + (g_client_bind_addr ? SF_G_INNER_BIND_ADDR4 : NULL), + (g_client_bind_addr ? SF_G_INNER_BIND_ADDR6 : NULL), &result, false); - if (conn == NULL) + if (conn == NULL) { if (previousCode != result) { diff --git a/test/test_types.h b/test/test_types.h index 4ff6fa2..2dfb2dc 100644 --- a/test/test_types.h +++ b/test/test_types.h @@ -13,7 +13,6 @@ #define FILENAME_FILE_ID "file_id" #define FILENAME_FAIL "fail" -#define IP_ADDRESS_SIZE 16 #define SRAND_SEED 1225420780 #define TIME_SUB_MS(tv1, tv2) ((tv1.tv_sec - tv2.tv_sec) * 1000 + (tv1.tv_usec - tv2.tv_usec) / 1000) diff --git a/tracker/fdfs_trackerd.c b/tracker/fdfs_trackerd.c index 16b55c8..688a7e4 100644 --- a/tracker/fdfs_trackerd.c +++ b/tracker/fdfs_trackerd.c @@ -423,14 +423,30 @@ static void sigAlarmHandler(int sig) logDebug("file: "__FILE__", line: %d, " \ "signal server to quit...", __LINE__); - if (*g_sf_context.inner_bind_addr != '\0') - { - strcpy(server.ip_addr, g_sf_context.inner_bind_addr); - } - else - { - strcpy(server.ip_addr, "127.0.0.1"); - } + if (SF_G_IPV4_ENABLED) + { + server.af = AF_INET; + if (*SF_G_INNER_BIND_ADDR4 != '\0') + { + strcpy(server.ip_addr, SF_G_INNER_BIND_ADDR4); + } + else + { + strcpy(server.ip_addr, LOCAL_LOOPBACK_IPv4); + } + } + else + { + server.af = AF_INET6; + if (*SF_G_INNER_BIND_ADDR6 != '\0') + { + strcpy(server.ip_addr, SF_G_INNER_BIND_ADDR6); + } + else + { + strcpy(server.ip_addr, LOCAL_LOOPBACK_IPv6); + } + } server.port = SF_G_INNER_PORT; server.sock = -1; diff --git a/tracker/tracker_proto.c b/tracker/tracker_proto.c index 8208563..d7586c2 100644 --- a/tracker/tracker_proto.c +++ b/tracker/tracker_proto.c @@ -488,7 +488,8 @@ ConnectionInfo *tracker_connect_server_ex(TrackerServerInfo *pServerInfo, } ConnectionInfo *tracker_connect_server_no_pool_ex(TrackerServerInfo *pServerInfo, - const char *bind_addr, int *err_no, const bool log_connect_error) + const char *bind_addr4, const char *bind_addr6, int *err_no, + const bool log_connect_error) { ConnectionInfo *conn; ConnectionInfo *end; @@ -500,12 +501,12 @@ ConnectionInfo *tracker_connect_server_no_pool_ex(TrackerServerInfo *pServerInfo return pServerInfo->connections + pServerInfo->index; } - *err_no = conn_pool_connect_server_ex(pServerInfo->connections - + pServerInfo->index, SF_G_CONNECT_TIMEOUT * 1000, - bind_addr, log_connect_error); + conn = pServerInfo->connections + pServerInfo->index; + *err_no = conn_pool_connect_server_ex(conn, SF_G_CONNECT_TIMEOUT * 1000, + conn->af == AF_INET ? bind_addr4 : bind_addr6, log_connect_error); if (*err_no == 0) { - return pServerInfo->connections + pServerInfo->index; + return conn; } if (pServerInfo->count == 1) @@ -520,11 +521,12 @@ ConnectionInfo *tracker_connect_server_no_pool_ex(TrackerServerInfo *pServerInfo if (current_index != pServerInfo->index) { if ((*err_no=conn_pool_connect_server_ex(conn, - SF_G_CONNECT_TIMEOUT * 1000, bind_addr, - log_connect_error)) == 0) + SF_G_CONNECT_TIMEOUT * 1000, + conn->af == AF_INET ? bind_addr4 : + bind_addr6, log_connect_error)) == 0) { pServerInfo->index = current_index; - return pServerInfo->connections + pServerInfo->index; + return conn; } } } @@ -633,9 +635,10 @@ static int fdfs_do_parameter_req(ConnectionInfo *pTrackerServer, \ return 0; } -int fdfs_get_ini_context_from_tracker(TrackerServerGroup *pTrackerGroup, \ - IniContext *iniContext, bool * volatile continue_flag, \ - const bool client_bind_addr, const char *bind_addr) +int fdfs_get_ini_context_from_tracker_ex(TrackerServerGroup *pTrackerGroup, + IniContext *iniContext, bool * volatile continue_flag, + const bool client_bind_addr, const char *bind_addr4, + const char *bind_addr6) { ConnectionInfo *conn; TrackerServerInfo *pGlobalServer; @@ -664,7 +667,14 @@ int fdfs_get_ini_context_from_tracker(TrackerServerGroup *pTrackerGroup, \ if (!client_bind_addr) { - bind_addr = NULL; + if (bind_addr4 != NULL) + { + bind_addr4 = NULL; + } + if (bind_addr6 != NULL) + { + bind_addr6 = NULL; + } } do @@ -678,7 +688,7 @@ int fdfs_get_ini_context_from_tracker(TrackerServerGroup *pTrackerGroup, \ for (i=0; i < 3; i++) { conn = tracker_connect_server_no_pool_ex(pTServer, - bind_addr, &result, false); + bind_addr4, bind_addr6, &result, false); if (conn != NULL) { break; diff --git a/tracker/tracker_proto.h b/tracker/tracker_proto.h index 5383848..19c8674 100644 --- a/tracker/tracker_proto.h +++ b/tracker/tracker_proto.h @@ -240,13 +240,15 @@ ConnectionInfo *tracker_connect_server_ex(TrackerServerInfo *pServerInfo, * connect to the tracker server directly without connection pool * params: * pTrackerServer: tracker server -* bind_ipaddr: the ip address to bind, NULL or empty for any +* bind_addr4: the ipv4 address to bind, NULL or empty for any +* bind_addr6: the ipv6 address to bind, NULL or empty for any * err_no: return the error no * log_connect_error: if log error info when connect fail * return: ConnectionInfo pointer for success, NULL for fail **/ ConnectionInfo *tracker_connect_server_no_pool_ex(TrackerServerInfo *pServerInfo, - const char *bind_addr, int *err_no, const bool log_connect_error); + const char *bind_addr4, const char *bind_addr6, int *err_no, + const bool log_connect_error); /** * connect to the tracker server directly without connection pool @@ -258,9 +260,10 @@ ConnectionInfo *tracker_connect_server_no_pool_ex(TrackerServerInfo *pServerInfo static inline ConnectionInfo *tracker_connect_server_no_pool( TrackerServerInfo *pServerInfo, int *err_no) { - const char *bind_addr = NULL; + const char *bind_addr4 = NULL; + const char *bind_addr6 = NULL; return tracker_connect_server_no_pool_ex(pServerInfo, - bind_addr, err_no, true); + bind_addr4, bind_addr6, err_no, true); } #define tracker_close_connection(pTrackerServer) \ @@ -316,15 +319,21 @@ int fdfs_deal_no_body_cmd_ex(const char *ip_addr, const int port, const int cmd) fdfs_split_metadata_ex(meta_buff, FDFS_RECORD_SEPERATOR, \ FDFS_FIELD_SEPERATOR, meta_count, err_no) -char *fdfs_pack_metadata(const FDFSMetaData *meta_list, const int meta_count, \ +char *fdfs_pack_metadata(const FDFSMetaData *meta_list, const int meta_count, char *meta_buff, int *buff_bytes); -FDFSMetaData *fdfs_split_metadata_ex(char *meta_buff, \ - const char recordSeperator, const char filedSeperator, \ +FDFSMetaData *fdfs_split_metadata_ex(char *meta_buff, + const char recordSeperator, const char filedSeperator, int *meta_count, int *err_no); -int fdfs_get_ini_context_from_tracker(TrackerServerGroup *pTrackerGroup, \ - IniContext *iniContext, bool * volatile continue_flag, \ - const bool client_bind_addr, const char *bind_addr); +int fdfs_get_ini_context_from_tracker_ex(TrackerServerGroup *pTrackerGroup, + IniContext *iniContext, bool * volatile continue_flag, + const bool client_bind_addr, const char *bind_addr4, + const char *bind_addr6); + +#define fdfs_get_ini_context_from_tracker(pTrackerGroup, \ + iniContext, continue_flag) \ + fdfs_get_ini_context_from_tracker_ex(pTrackerGroup, \ + iniContext, continue_flag, false, NULL, NULL) int fdfs_get_tracker_status(TrackerServerInfo *pTrackerServer, TrackerRunningStatus *pStatus);