diff --git a/HISTORY b/HISTORY index 67fc9e7..b2e392f 100644 --- a/HISTORY +++ b/HISTORY @@ -1,6 +1,6 @@ -Version 1.41 2019-09-29 +Version 1.41 2019-09-30 * change CIDR network_bits range from [16, 32) to [10, 32) * ini_file_reader.c: fix empty string compare * multi_socket_client.c: code refine diff --git a/src/connection_pool.c b/src/connection_pool.c index aedc1de..46687ba 100644 --- a/src/connection_pool.c +++ b/src/connection_pool.c @@ -85,8 +85,8 @@ void conn_pool_disconnect_server(ConnectionInfo *pConnection) } } -int conn_pool_connect_server(ConnectionInfo *pConnection, \ - const int connect_timeout) +int conn_pool_connect_server_ex(ConnectionInfo *pConnection, + const int connect_timeout, const char *bind_ipaddr) { int result; int domain; @@ -114,6 +114,11 @@ int conn_pool_connect_server(ConnectionInfo *pConnection, \ return errno != 0 ? errno : EPERM; } + if (bind_ipaddr != NULL && *bind_ipaddr != '\0') + { + socketBind2(domain, pConnection->sock, bind_ipaddr, 0); + } + SET_SOCKOPT_NOSIGPIPE(pConnection->sock); if ((result=tcpsetnonblockopt(pConnection->sock)) != 0) { diff --git a/src/connection_pool.h b/src/connection_pool.h index 6fcfe4d..a72be85 100644 --- a/src/connection_pool.h +++ b/src/connection_pool.h @@ -24,6 +24,10 @@ extern "C" { #endif +#define FC_CONNECTION_SERVER_EQUAL(conn, target_ip, target_port) \ + (strcmp((conn).ip_addr, target_ip) == 0 && \ + (conn).port == target_port) + typedef struct { int sock; @@ -134,11 +138,28 @@ void conn_pool_disconnect_server(ConnectionInfo *pConnection); * parameters: * pConnection: the connection * connect_timeout: the connect timeout in seconds +* bind_ipaddr: the ip address to bind, NULL or empty for any * NOTE: pConnection->sock will be closed when it >= 0 before connect * return 0 for success, != 0 for error */ -int conn_pool_connect_server(ConnectionInfo *pConnection, \ - const int connect_timeout); +int conn_pool_connect_server_ex(ConnectionInfo *pConnection, + const int connect_timeout, const char *bind_ipaddr); + +/** +* connect to the server +* parameters: +* pConnection: the connection +* connect_timeout: the connect timeout in seconds +* NOTE: pConnection->sock will be closed when it >= 0 before connect +* return 0 for success, != 0 for error +*/ +static inline int conn_pool_connect_server(ConnectionInfo *pConnection, + const int connect_timeout) +{ + const char *bind_ipaddr = NULL; + return conn_pool_connect_server_ex(pConnection, + connect_timeout, bind_ipaddr); +} /** * get connection count of the pool