From 41ef6fef8ff9cf9aa77fe032133abc31e8f8a9ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?shuxin=20=E3=80=80=E3=80=80zheng?= Date: Sat, 27 Jun 2020 14:01:13 +0800 Subject: [PATCH] use remote_addr@local_addr replacing the old format: local_addr@remote_addr --- lib_acl/include/net/acl_connect.h | 13 ++-- lib_acl/include/net/acl_vstream_net.h | 10 +-- lib_acl/src/net/connect/acl_inet_connect.c | 76 +++++++++++-------- lib_acl/src/net/connect/acl_stream_connect.c | 12 ++- lib_acl/src/net/connect/acl_timed_connect.c | 21 +++-- .../include/acl_cpp/http/http_client.hpp | 2 +- .../include/acl_cpp/http/http_request.hpp | 3 +- .../include/acl_cpp/stream/socket_stream.hpp | 4 +- lib_fiber/samples/client/main.c | 3 +- 9 files changed, 83 insertions(+), 61 deletions(-) diff --git a/lib_acl/include/net/acl_connect.h b/lib_acl/include/net/acl_connect.h index ca5c8d9f2..a9f90aead 100644 --- a/lib_acl/include/net/acl_connect.h +++ b/lib_acl/include/net/acl_connect.h @@ -42,9 +42,9 @@ ACL_API int acl_timed_connect(ACL_SOCKET fd, const struct sockaddr * sa, /** * 远程连接网络服务器地址 - * @param addr {const char*} 远程服务器的监听地址,如:192.168.0.1:80, 如果需 - * 要绑定本地的地址,则格式为: {local_ip}@{remote_addr}, - * 如: 60.28.250.199@www.sina.com:80 + * @param addr {const char*} 远程服务器的监听地址,如:192.168.0.1|80, 如果需 + * 要绑定本地的地址,则格式为: remote_addr@local_ip, + * 如: www.sina.com|80@60.28.250.199 * @param blocking {int} 阻塞模式还是非阻塞模式, ACL_BLOCKING 或 ACL_NON_BLOCKING * @param timeout {int} 连接超时时间,如果 blocking 为 ACL_NON_BLOCKING 则该值将被忽略 * @return {ACL_SOCKET} 如果返回 ACL_SOCKET_INVALID 表示连接失败 @@ -53,9 +53,9 @@ ACL_API ACL_SOCKET acl_inet_connect(const char *addr, int blocking, int timeout) /** * 远程连接网络服务器地址 - * @param addr {const char*} 远程服务器的监听地址,如:192.168.0.1:80, + * @param addr {const char*} 远程服务器的监听地址,如:192.168.0.1|80, * 当本机有多个网卡地址且想通过某个指定网卡连接服务器时的地址格式: - * local_ip@remote_ip:remote_port,如:192.168.1.1@211.150.111.12:80 + * remote_ip|remote_port@local_ip,如:211.150.111.12|80@192.168.1.1 * @param blocking {int} 阻塞模式还是非阻塞模式, ACL_BLOCKING 或 ACL_NON_BLOCKING * @param timeout {int} 连接超时时间,如果 blocking 为 ACL_NON_BLOCKING 则该值将被忽略 * @param h_error {int*} 当连接失败时存储失败原因错误号 @@ -70,7 +70,8 @@ ACL_API ACL_SOCKET acl_inet_connect_ex(const char *addr, int blocking, /** * 连接监听域套接字服务器 - * @param addr {const char*} 服务器监听的域套接字全路径, 如: /tmp/test.sock + * @param addr {const char*} 服务器监听的域套接字全路径, 如: /tmp/test.sock, + * 针对 Linux 平台,如果首字母为 @ 则内部自动采用抽象 UNIX 域套接口进行连接 * @param blocking {int} 阻塞模式还是非阻塞模式, ACL_BLOCKING 或 ACL_NON_BLOCKING * @param timeout {int} 连接超时时间,如果 blocking 为 ACL_NON_BLOCKING 则该值将被忽略 * @return {ACL_SOCKET} 如果返回 ACL_SOCKET_INVALID 表示连接失败 diff --git a/lib_acl/include/net/acl_vstream_net.h b/lib_acl/include/net/acl_vstream_net.h index ecff95b28..c6eb1c268 100644 --- a/lib_acl/include/net/acl_vstream_net.h +++ b/lib_acl/include/net/acl_vstream_net.h @@ -57,8 +57,10 @@ ACL_API ACL_VSTREAM *acl_vstream_accept(ACL_VSTREAM *listen_stream, /** * 远程连接服务器 * @param addr {const char*} 服务器地址, 如果连接一个域套接口服务器(仅UNIX平台), - * 域套接地址:/tmp/test.sock; 如果连接一个TCP服务器,则地址格式为: - * [${local_ip}@]${remote_addr}, 如: 60.28.250.199@www.sina.com:80, 意思是绑定 + * 域套接地址:/tmp/test.sock,对于 Linux 平台,如果首字母为 '@' 则表示连接本 + * 地的抽象UNIX域套接口(Linux abstract unix domain socket); + * 如果连接一个TCP服务器,则地址格式为: + * remote_addr[@local_ip], 如: www.sina.com|80@60.28.250.199, 意思是绑定 * 本的网卡地址为: 60.28.250.199, 远程连接 www.sina.com 的 80 端口, 如果由OS * 自动绑定本地 IP 地址,则可以写为:www.sina.com:80 * @param block_mode {int} 阻塞连接还是非阻塞连接,ACL_BLOCKING, ACL_NON_BLOCKING @@ -73,9 +75,7 @@ ACL_API ACL_VSTREAM *acl_vstream_connect_ex(const char *addr, int block_mode, /** * 远程连接服务器 - * @param addr {const char*} 服务器地址,格式如:127.0.0.1, - * 或域套接地址:/tmp/test.sock,对于 Linux 平台,如果首字母为 '@',则认为是 - * Linux abstract unix domain path. + * @param addr {const char*} 服务器地址,含义同上 * @param block_mode {int} 阻塞连接还是非阻塞连接,ACL_BLOCKING, ACL_NON_BLOCKING * @param connect_timeout {int} 连接超时时间(秒) * @param rw_timeout {int} 连接流成功后的读写超时时间,单位为秒 diff --git a/lib_acl/src/net/connect/acl_inet_connect.c b/lib_acl/src/net/connect/acl_inet_connect.c index 69dacdc07..d3d337fee 100644 --- a/lib_acl/src/net/connect/acl_inet_connect.c +++ b/lib_acl/src/net/connect/acl_inet_connect.c @@ -39,15 +39,17 @@ static int bind_local(ACL_SOCKET sock, int family, const struct addrinfo *res0) const struct addrinfo *res; for (res = res0; res != NULL; res = res->ai_next) { - if (res->ai_family != family) + if (res->ai_family != family) { continue; + } #ifdef ACL_WINDOWS - if (bind(sock, res->ai_addr, (int) res->ai_addrlen) == 0) + if (bind(sock, res->ai_addr, (int) res->ai_addrlen) == 0) { #else - if (bind(sock, res->ai_addr, res->ai_addrlen) == 0) + if (bind(sock, res->ai_addr, res->ai_addrlen) == 0) { #endif return 0; + } } return -1; @@ -70,8 +72,10 @@ static ACL_SOCKET inet_connect_one(const struct addrinfo *peer, return ACL_SOCKET_INVALID; } + /* acl_tcp_set_rcvbuf(sock, ACL_SOCKET_RBUF_SIZE); acl_tcp_set_sndbuf(sock, ACL_SOCKET_WBUF_SIZE); + */ on = 1; if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, @@ -93,12 +97,11 @@ static ACL_SOCKET inet_connect_one(const struct addrinfo *peer, acl_non_blocking(sock, ACL_NON_BLOCKING); #ifdef ACL_WINDOWS if (acl_timed_connect(sock, peer->ai_addr, - (socklen_t) peer->ai_addrlen, timeout) < 0) + (socklen_t) peer->ai_addrlen, timeout) < 0) { #else if (acl_timed_connect(sock, peer->ai_addr, - peer->ai_addrlen, timeout) < 0) + peer->ai_addrlen, timeout) < 0) { #endif - { #ifdef ACL_WINDOWS int err = acl_last_error(); #endif @@ -108,8 +111,9 @@ static ACL_SOCKET inet_connect_one(const struct addrinfo *peer, #endif return ACL_SOCKET_INVALID; } - if (blocking != ACL_NON_BLOCKING) + if (blocking != ACL_NON_BLOCKING) { acl_non_blocking(sock, blocking); + } return sock; } @@ -135,8 +139,9 @@ static ACL_SOCKET inet_connect_one(const struct addrinfo *peer, * connect and just returns EPIPE. Create a fake * error message for connect. -- fenner@parc.xerox.com */ - if (errno == EPIPE) + if (errno == EPIPE) { acl_set_error(ACL_ENOTCONN); + } #endif #ifdef ACL_WINDOWS err = acl_last_error(); @@ -152,11 +157,12 @@ static ACL_SOCKET inet_connect_one(const struct addrinfo *peer, } #ifdef ACL_WINDOWS - if (errnum == ACL_EINPROGRESS || errnum == ACL_EWOULDBLOCK) + if (errnum == ACL_EINPROGRESS || errnum == ACL_EWOULDBLOCK) { return sock; #elif defined(ACL_UNIX) - if (errnum == ACL_EINPROGRESS || errnum == EISCONN) + if (errnum == ACL_EINPROGRESS || errnum == EISCONN) { return sock; + } #endif acl_socket_close(sock); #ifdef ACL_WINDOWS @@ -185,17 +191,27 @@ ACL_SOCKET acl_inet_connect_ex(const char *addr, int blocking, const char *peer, *local, *port; struct addrinfo hints, *peer_res0, *res, *local_res0; - if (h_error) + if (h_error) { *h_error = 0; + } snprintf(buf, sizeof(buf) - 1, "%s", addr); + peer = buf; + ptr = strchr(buf, '@'); + if (ptr != NULL) { + *ptr = 0; + local = *++ptr == 0 ? NULL : ptr; + } else { + local = NULL; + } - if (acl_valid_ipv6_hostaddr(buf, 0)) { - ptr = strrchr(buf, ACL_ADDR_SEP); - } else if (acl_valid_ipv4_hostaddr(buf, 0)) { - ptr = strrchr(buf, ACL_ADDR_SEP); - if (ptr == NULL) - ptr = strrchr(buf, ':'); + if (acl_valid_ipv6_hostaddr(peer, 0)) { + ptr = strrchr(peer, ACL_ADDR_SEP); + } else if (acl_valid_ipv4_hostaddr(peer, 0)) { + ptr = strrchr(peer, ACL_ADDR_SEP); + if (ptr == NULL) { + ptr = strrchr(peer, ':'); + } } else if (!(ptr = strrchr(buf, ACL_ADDR_SEP))) { ptr = strrchr(buf, ':'); } @@ -215,16 +231,6 @@ ACL_SOCKET acl_inet_connect_ex(const char *addr, int blocking, return ACL_SOCKET_INVALID; } - ptr = strchr(buf, '@'); - if (ptr != NULL) { - *ptr++ = 0; - local = buf; - peer = ptr; - } else { - local = NULL; - peer = buf; - } - if (strlen(peer) == 0) { acl_msg_error("%s, %s(%d): ip buf's length is 0", __FILE__, myname, __LINE__); @@ -255,9 +261,9 @@ ACL_SOCKET acl_inet_connect_ex(const char *addr, int blocking, return ACL_SOCKET_INVALID; } - if (local == NULL) + if (local == NULL) { local_res0 = NULL; - else if ((err = getaddrinfo(local, "0", &hints, &local_res0))) { + } else if ((err = getaddrinfo(local, "0", &hints, &local_res0))) { acl_msg_error("%s(%d), %s: getaddrinfo error %s, local=%s", __FILE__, __LINE__, myname, gai_strerror(err), local); return ACL_SOCKET_INVALID; @@ -267,18 +273,22 @@ ACL_SOCKET acl_inet_connect_ex(const char *addr, int blocking, for (res = peer_res0; res != NULL ; res = res->ai_next) { sock = inet_connect_one(res, local_res0, blocking, timeout); - if (sock != ACL_SOCKET_INVALID) + if (sock != ACL_SOCKET_INVALID) { break; + } } - if (sock == ACL_SOCKET_INVALID) + if (sock == ACL_SOCKET_INVALID) { acl_msg_error("%s(%d) %s: connect error %s, addr=%s", __FILE__, __LINE__, myname, acl_last_serror(), addr); + } - if (peer_res0) + if (peer_res0) { freeaddrinfo(peer_res0); - if (local_res0) + } + if (local_res0) { freeaddrinfo(local_res0); + } return sock; } diff --git a/lib_acl/src/net/connect/acl_stream_connect.c b/lib_acl/src/net/connect/acl_stream_connect.c index 5887c7582..a59798109 100644 --- a/lib_acl/src/net/connect/acl_stream_connect.c +++ b/lib_acl/src/net/connect/acl_stream_connect.c @@ -59,24 +59,28 @@ int acl_stream_connect(const char *path, int block_mode, int unused_timeout) * The requested file system object must exist, otherwise we can't reach * the server. */ - if ((fifo = open(path, O_WRONLY | O_NONBLOCK, 0)) < 0) + if ((fifo = open(path, O_WRONLY | O_NONBLOCK, 0)) < 0) { return -1; + } /* * Create a pipe, and send one pipe end to the server. */ - if (pipe(pair) < 0) + if (pipe(pair) < 0) { acl_msg_fatal("%s: pipe: %s", myname, acl_last_serror()); - if (ioctl(fifo, I_SENDFD, pair[1]) < 0) + } + if (ioctl(fifo, I_SENDFD, pair[1]) < 0) { acl_msg_fatal("%s: send file descriptor: %s", myname, acl_last_serror()); + } close(pair[1]); /* * This is for {unix,inet}_connect() compatibility. */ - if (block_mode == ACL_NON_BLOCKING) + if (block_mode == ACL_NON_BLOCKING) { acl_non_blocking(pair[0], ACL_NON_BLOCKING); + } /* * Cleanup. diff --git a/lib_acl/src/net/connect/acl_timed_connect.c b/lib_acl/src/net/connect/acl_timed_connect.c index d7cff9185..0c566646c 100644 --- a/lib_acl/src/net/connect/acl_timed_connect.c +++ b/lib_acl/src/net/connect/acl_timed_connect.c @@ -30,31 +30,36 @@ int acl_timed_connect(ACL_SOCKET sock, const struct sockaddr * sa, * Sanity check. Just like with timed_wait(), the timeout must be a * positive number. */ - if (timeout < 0) + if (timeout < 0) { acl_msg_panic("timed_connect: bad timeout: %d", timeout); + } /* * Start the connection, and handle all possible results. */ - if (acl_sane_connect(sock, sa, len) == 0) + if (acl_sane_connect(sock, sa, len) == 0) { return 0; + } errno = acl_last_error(); #ifdef ACL_UNIX - if (errno != ACL_EINPROGRESS) + if (errno != ACL_EINPROGRESS) { return -1; + } #elif defined(ACL_WINDOWS) - if (errno != ACL_EINPROGRESS && errno != ACL_EWOULDBLOCK) + if (errno != ACL_EINPROGRESS && errno != ACL_EWOULDBLOCK) { return -1; + } #endif /* * A connection is in progress. Wait for a limited amount of time for * something to happen. If nothing happens, report an error. */ - if (acl_write_wait(sock, timeout) < 0) + if (acl_write_wait(sock, timeout) < 0) { return -1; + } /* * Something happened. Some Solaris 2 versions have getsockopt() itself @@ -69,8 +74,9 @@ int acl_timed_connect(ACL_SOCKET sock, const struct sockaddr * sa, * connect and just returns EPIPE. Create a fake * error message for connect. -- fenner@parc.xerox.com */ - if (errno == EPIPE) + if (errno == EPIPE) { acl_set_error(ACL_ENOTCONN); + } #endif return -1; } @@ -78,6 +84,7 @@ int acl_timed_connect(ACL_SOCKET sock, const struct sockaddr * sa, if (err != 0) { acl_set_error(err); return -1; - } else + } else { return 0; + } } diff --git a/lib_acl_cpp/include/acl_cpp/http/http_client.hpp b/lib_acl_cpp/include/acl_cpp/http/http_client.hpp index c16f443ad..44e3b06a3 100644 --- a/lib_acl_cpp/include/acl_cpp/http/http_client.hpp +++ b/lib_acl_cpp/include/acl_cpp/http/http_client.hpp @@ -60,7 +60,7 @@ public: /** * 连接远程 HTTP 服务器 - * @param addr {const char*} 服务器地址,格式:IP:PORT 或 DOMAIN:PORT + * @param addr {const char*} 服务器地址,格式:IP|PORT 或 DOMAIN|PORT * @param conn_timeout {int} 连接超时时间(秒) * @param rw_timeout {int} 读写超时时间(秒) * @param unzip {bool} 当服务器返回的数据体为压缩数据时是否自动解压缩 diff --git a/lib_acl_cpp/include/acl_cpp/http/http_request.hpp b/lib_acl_cpp/include/acl_cpp/http/http_request.hpp index b69f9960b..8e61b6a2f 100644 --- a/lib_acl_cpp/include/acl_cpp/http/http_request.hpp +++ b/lib_acl_cpp/include/acl_cpp/http/http_request.hpp @@ -41,7 +41,8 @@ public: /** * 构造函数:该构造函数内部创建的 socket_stream 流会自行关闭 - * @param addr {const char*} WEB 服务器地址 + * @param addr {const char*} WEB 服务器地址,地址格式:domain|port, + * 如:www.baidu.com|80 * @param conn_timeout {int} 远程连接服务器超时时间(秒) * @param rw_timeout {int} IO 读写超时时间(秒) * @param unzip {bool} 是否对服务器响应的数据自动进行解压 diff --git a/lib_acl_cpp/include/acl_cpp/stream/socket_stream.hpp b/lib_acl_cpp/include/acl_cpp/stream/socket_stream.hpp index 4e796f03b..ab0df45ef 100644 --- a/lib_acl_cpp/include/acl_cpp/stream/socket_stream.hpp +++ b/lib_acl_cpp/include/acl_cpp/stream/socket_stream.hpp @@ -47,8 +47,8 @@ public: * 在 acl 库中规定如果地址第一个字节为 @,则认为是 Linux 抽象域套接字 * (abstract unix domain socket)不过需注意该功能仅有 Linux 平台支持, * 举例,如:@/tmp/test.sock;; - * 如果连接一个TCP服务器,则地址格式为: [${local_ip}@]${remote_addr}, - * 如: 60.28.250.199@www.sina.com:80, + * 如果连接一个TCP服务器,则地址格式为: remote_addr[@local_ip], + * 如: www.sina.com:80@60.28.250.199, * 意思是绑定本的网卡地址为: 60.28.250.199, 远程连接 www.sina.com 的 80, * 如果由OS自动绑定本地 IP 地址,则可以写为:www.sina.com:80 * @param conn_timeout {int} 连接超时时间(秒) diff --git a/lib_fiber/samples/client/main.c b/lib_fiber/samples/client/main.c index 46655c336..d95a26a29 100644 --- a/lib_fiber/samples/client/main.c +++ b/lib_fiber/samples/client/main.c @@ -99,8 +99,7 @@ static void fiber_main(ACL_FIBER *fiber acl_unused, void *ctx) char *addr = (char *) ctx; int i; - for (i = 0; i < __max_fibers; i++) - { + for (i = 0; i < __max_fibers; i++) { acl_fiber_create(fiber_connect, addr, 327680); //acl_fiber_sleep(1); }