diff --git a/HISTORY b/HISTORY index d289d6d..434453a 100644 --- a/HISTORY +++ b/HISTORY @@ -1,4 +1,7 @@ +Version 1.40 2018-08-14 + * add function conn_pool_parse_server_info and conn_pool_load_server_info + Version 1.39 2018-07-31 * add #@function REPLACE_VARS * #@set value can embed %{VARIABLE} diff --git a/src/connection_pool.c b/src/connection_pool.c index 3c0195d..090c96c 100644 --- a/src/connection_pool.c +++ b/src/connection_pool.c @@ -376,3 +376,73 @@ int conn_pool_get_connection_count(ConnectionPool *cp) return count; } +int conn_pool_parse_server_info(const char *pServerStr, + ConnectionInfo *pServerInfo, const int default_port) +{ + char *parts[2]; + char server_info[256]; + int len; + int count; + + len = strlen(pServerStr); + if (len == 0) { + logError("file: "__FILE__", line: %d, " + "pServerStr \"%s\" is empty!", + __LINE__, pServerStr); + return EINVAL; + } + if (len >= sizeof(server_info)) { + logError("file: "__FILE__", line: %d, " + "pServerStr \"%s\" is too long!", + __LINE__, pServerStr); + return ENAMETOOLONG; + } + + memcpy(server_info, pServerStr, len); + *(server_info + len) = '\0'; + + count = splitEx(server_info, ':', parts, 2); + if (count == 1) { + pServerInfo->port = default_port; + } + else { + char *endptr = NULL; + pServerInfo->port = (int)strtol(parts[1], &endptr, 10); + if ((endptr != NULL && *endptr != '\0') || pServerInfo->port <= 0) { + logError("file: "__FILE__", line: %d, " + "pServerStr: %s, invalid port: %s!", + __LINE__, pServerStr, parts[1]); + return EINVAL; + } + } + + if (getIpaddrByName(parts[0], pServerInfo->ip_addr, + sizeof(pServerInfo->ip_addr)) == INADDR_NONE) + { + logError("file: "__FILE__", line: %d, " + "pServerStr: %s, invalid hostname: %s!", + __LINE__, pServerStr, parts[0]); + return EINVAL; + } + + pServerInfo->socket_domain = AF_INET; + pServerInfo->sock = -1; + return 0; +} + +int conn_pool_load_server_info(IniContext *pIniContext, const char *filename, + const char *item_name, ConnectionInfo *pServerInfo, + const int default_port) +{ + char *pServerStr; + + pServerStr = iniGetStrValue(NULL, item_name, pIniContext); + if (pServerStr == NULL) { + logError("file: "__FILE__", line: %d, " + "config file: %s, item \"%s\" not exist!", + __LINE__, filename, item_name); + return ENOENT; + } + + return conn_pool_parse_server_info(pServerStr, pServerInfo, default_port); +} diff --git a/src/connection_pool.h b/src/connection_pool.h index ac0d070..6fcfe4d 100644 --- a/src/connection_pool.h +++ b/src/connection_pool.h @@ -18,6 +18,7 @@ #include "common_define.h" #include "pthread_func.h" #include "hash.h" +#include "ini_file_reader.h" #ifdef __cplusplus extern "C" { @@ -147,6 +148,31 @@ int conn_pool_connect_server(ConnectionInfo *pConnection, \ */ int conn_pool_get_connection_count(ConnectionPool *cp); +/** +* load server info from config file +* parameters: +* pIniContext: the ini context +* filename: the config filename +* item_name: the item name in config file, format item_name=server:port +* pServerInfo: store server info +* default_port: the default port +* return 0 for success, != 0 for error +*/ +int conn_pool_load_server_info(IniContext *pIniContext, const char *filename, + const char *item_name, ConnectionInfo *pServerInfo, + const int default_port); + +/** +* parse server info from string +* parameters: +* pServerStr: server and port string as server:port +* pServerInfo: store server info +* default_port: the default port +* return 0 for success, != 0 for error +*/ +int conn_pool_parse_server_info(const char *pServerStr, + ConnectionInfo *pServerInfo, const int default_port); + #ifdef __cplusplus } #endif