use remote_addr@local_addr replacing the old format: local_addr@remote_addr

This commit is contained in:
shuxin   zheng 2020-06-27 14:01:13 +08:00
parent 27c8892cec
commit 41ef6fef8f
9 changed files with 83 additions and 61 deletions

View File

@ -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, * @param addr {const char*} 192.168.0.1|80,
* : {local_ip}@{remote_addr}, * : remote_addr@local_ip,
* : 60.28.250.199@www.sina.com:80 * : www.sina.com|80@60.28.250.199
* @param blocking {int} , ACL_BLOCKING ACL_NON_BLOCKING * @param blocking {int} , ACL_BLOCKING ACL_NON_BLOCKING
* @param timeout {int} blocking ACL_NON_BLOCKING * @param timeout {int} blocking ACL_NON_BLOCKING
* @return {ACL_SOCKET} ACL_SOCKET_INVALID * @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_port192.168.1.1@211.150.111.12:80 * remote_ip|remote_port@local_ip211.150.111.12|80@192.168.1.1
* @param blocking {int} , ACL_BLOCKING ACL_NON_BLOCKING * @param blocking {int} , ACL_BLOCKING ACL_NON_BLOCKING
* @param timeout {int} blocking ACL_NON_BLOCKING * @param timeout {int} blocking ACL_NON_BLOCKING
* @param h_error {int*} * @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 blocking {int} , ACL_BLOCKING ACL_NON_BLOCKING
* @param timeout {int} blocking ACL_NON_BLOCKING * @param timeout {int} blocking ACL_NON_BLOCKING
* @return {ACL_SOCKET} ACL_SOCKET_INVALID * @return {ACL_SOCKET} ACL_SOCKET_INVALID

View File

@ -57,8 +57,10 @@ ACL_API ACL_VSTREAM *acl_vstream_accept(ACL_VSTREAM *listen_stream,
/** /**
* *
* @param addr {const char*} , (UNIX平台), * @param addr {const char*} , (UNIX平台),
* /tmp/test.sock; TCP服务器: * /tmp/test.sock Linux '@'
* [${local_ip}@]${remote_addr}, : 60.28.250.199@www.sina.com:80, * 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 * : 60.28.250.199, www.sina.com 80 , OS
* IP www.sina.com:80 * IP www.sina.com:80
* @param block_mode {int} ACL_BLOCKING, ACL_NON_BLOCKING * @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 * @param addr {const char*}
* /tmp/test.sock Linux '@'
* Linux abstract unix domain path.
* @param block_mode {int} ACL_BLOCKING, ACL_NON_BLOCKING * @param block_mode {int} ACL_BLOCKING, ACL_NON_BLOCKING
* @param connect_timeout {int} () * @param connect_timeout {int} ()
* @param rw_timeout {int} * @param rw_timeout {int}

View File

@ -39,15 +39,17 @@ static int bind_local(ACL_SOCKET sock, int family, const struct addrinfo *res0)
const struct addrinfo *res; const struct addrinfo *res;
for (res = res0; res != NULL; res = res->ai_next) { for (res = res0; res != NULL; res = res->ai_next) {
if (res->ai_family != family) if (res->ai_family != family) {
continue; continue;
}
#ifdef ACL_WINDOWS #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 #else
if (bind(sock, res->ai_addr, res->ai_addrlen) == 0) if (bind(sock, res->ai_addr, res->ai_addrlen) == 0) {
#endif #endif
return 0; return 0;
}
} }
return -1; return -1;
@ -70,8 +72,10 @@ static ACL_SOCKET inet_connect_one(const struct addrinfo *peer,
return ACL_SOCKET_INVALID; return ACL_SOCKET_INVALID;
} }
/*
acl_tcp_set_rcvbuf(sock, ACL_SOCKET_RBUF_SIZE); acl_tcp_set_rcvbuf(sock, ACL_SOCKET_RBUF_SIZE);
acl_tcp_set_sndbuf(sock, ACL_SOCKET_WBUF_SIZE); acl_tcp_set_sndbuf(sock, ACL_SOCKET_WBUF_SIZE);
*/
on = 1; on = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, 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); acl_non_blocking(sock, ACL_NON_BLOCKING);
#ifdef ACL_WINDOWS #ifdef ACL_WINDOWS
if (acl_timed_connect(sock, peer->ai_addr, if (acl_timed_connect(sock, peer->ai_addr,
(socklen_t) peer->ai_addrlen, timeout) < 0) (socklen_t) peer->ai_addrlen, timeout) < 0) {
#else #else
if (acl_timed_connect(sock, peer->ai_addr, if (acl_timed_connect(sock, peer->ai_addr,
peer->ai_addrlen, timeout) < 0) peer->ai_addrlen, timeout) < 0) {
#endif #endif
{
#ifdef ACL_WINDOWS #ifdef ACL_WINDOWS
int err = acl_last_error(); int err = acl_last_error();
#endif #endif
@ -108,8 +111,9 @@ static ACL_SOCKET inet_connect_one(const struct addrinfo *peer,
#endif #endif
return ACL_SOCKET_INVALID; return ACL_SOCKET_INVALID;
} }
if (blocking != ACL_NON_BLOCKING) if (blocking != ACL_NON_BLOCKING) {
acl_non_blocking(sock, blocking); acl_non_blocking(sock, blocking);
}
return sock; return sock;
} }
@ -135,8 +139,9 @@ static ACL_SOCKET inet_connect_one(const struct addrinfo *peer,
* connect and just returns EPIPE. Create a fake * connect and just returns EPIPE. Create a fake
* error message for connect. -- fenner@parc.xerox.com * error message for connect. -- fenner@parc.xerox.com
*/ */
if (errno == EPIPE) if (errno == EPIPE) {
acl_set_error(ACL_ENOTCONN); acl_set_error(ACL_ENOTCONN);
}
#endif #endif
#ifdef ACL_WINDOWS #ifdef ACL_WINDOWS
err = acl_last_error(); err = acl_last_error();
@ -152,11 +157,12 @@ static ACL_SOCKET inet_connect_one(const struct addrinfo *peer,
} }
#ifdef ACL_WINDOWS #ifdef ACL_WINDOWS
if (errnum == ACL_EINPROGRESS || errnum == ACL_EWOULDBLOCK) if (errnum == ACL_EINPROGRESS || errnum == ACL_EWOULDBLOCK) {
return sock; return sock;
#elif defined(ACL_UNIX) #elif defined(ACL_UNIX)
if (errnum == ACL_EINPROGRESS || errnum == EISCONN) if (errnum == ACL_EINPROGRESS || errnum == EISCONN) {
return sock; return sock;
}
#endif #endif
acl_socket_close(sock); acl_socket_close(sock);
#ifdef ACL_WINDOWS #ifdef ACL_WINDOWS
@ -185,17 +191,27 @@ ACL_SOCKET acl_inet_connect_ex(const char *addr, int blocking,
const char *peer, *local, *port; const char *peer, *local, *port;
struct addrinfo hints, *peer_res0, *res, *local_res0; struct addrinfo hints, *peer_res0, *res, *local_res0;
if (h_error) if (h_error) {
*h_error = 0; *h_error = 0;
}
snprintf(buf, sizeof(buf) - 1, "%s", addr); 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)) { if (acl_valid_ipv6_hostaddr(peer, 0)) {
ptr = strrchr(buf, ACL_ADDR_SEP); ptr = strrchr(peer, ACL_ADDR_SEP);
} else if (acl_valid_ipv4_hostaddr(buf, 0)) { } else if (acl_valid_ipv4_hostaddr(peer, 0)) {
ptr = strrchr(buf, ACL_ADDR_SEP); ptr = strrchr(peer, ACL_ADDR_SEP);
if (ptr == NULL) if (ptr == NULL) {
ptr = strrchr(buf, ':'); ptr = strrchr(peer, ':');
}
} else if (!(ptr = strrchr(buf, ACL_ADDR_SEP))) { } else if (!(ptr = strrchr(buf, ACL_ADDR_SEP))) {
ptr = strrchr(buf, ':'); ptr = strrchr(buf, ':');
} }
@ -215,16 +231,6 @@ ACL_SOCKET acl_inet_connect_ex(const char *addr, int blocking,
return ACL_SOCKET_INVALID; 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) { if (strlen(peer) == 0) {
acl_msg_error("%s, %s(%d): ip buf's length is 0", acl_msg_error("%s, %s(%d): ip buf's length is 0",
__FILE__, myname, __LINE__); __FILE__, myname, __LINE__);
@ -255,9 +261,9 @@ ACL_SOCKET acl_inet_connect_ex(const char *addr, int blocking,
return ACL_SOCKET_INVALID; return ACL_SOCKET_INVALID;
} }
if (local == NULL) if (local == NULL) {
local_res0 = 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", acl_msg_error("%s(%d), %s: getaddrinfo error %s, local=%s",
__FILE__, __LINE__, myname, gai_strerror(err), local); __FILE__, __LINE__, myname, gai_strerror(err), local);
return ACL_SOCKET_INVALID; 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) { for (res = peer_res0; res != NULL ; res = res->ai_next) {
sock = inet_connect_one(res, local_res0, blocking, timeout); sock = inet_connect_one(res, local_res0, blocking, timeout);
if (sock != ACL_SOCKET_INVALID) if (sock != ACL_SOCKET_INVALID) {
break; break;
}
} }
if (sock == ACL_SOCKET_INVALID) if (sock == ACL_SOCKET_INVALID) {
acl_msg_error("%s(%d) %s: connect error %s, addr=%s", acl_msg_error("%s(%d) %s: connect error %s, addr=%s",
__FILE__, __LINE__, myname, acl_last_serror(), addr); __FILE__, __LINE__, myname, acl_last_serror(), addr);
}
if (peer_res0) if (peer_res0) {
freeaddrinfo(peer_res0); freeaddrinfo(peer_res0);
if (local_res0) }
if (local_res0) {
freeaddrinfo(local_res0); freeaddrinfo(local_res0);
}
return sock; return sock;
} }

View File

@ -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 requested file system object must exist, otherwise we can't reach
* the server. * the server.
*/ */
if ((fifo = open(path, O_WRONLY | O_NONBLOCK, 0)) < 0) if ((fifo = open(path, O_WRONLY | O_NONBLOCK, 0)) < 0) {
return -1; return -1;
}
/* /*
* Create a pipe, and send one pipe end to the server. * 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()); 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", acl_msg_fatal("%s: send file descriptor: %s",
myname, acl_last_serror()); myname, acl_last_serror());
}
close(pair[1]); close(pair[1]);
/* /*
* This is for {unix,inet}_connect() compatibility. * 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); acl_non_blocking(pair[0], ACL_NON_BLOCKING);
}
/* /*
* Cleanup. * Cleanup.

View File

@ -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 * Sanity check. Just like with timed_wait(), the timeout must be a
* positive number. * positive number.
*/ */
if (timeout < 0) if (timeout < 0) {
acl_msg_panic("timed_connect: bad timeout: %d", timeout); acl_msg_panic("timed_connect: bad timeout: %d", timeout);
}
/* /*
* Start the connection, and handle all possible results. * 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; return 0;
}
errno = acl_last_error(); errno = acl_last_error();
#ifdef ACL_UNIX #ifdef ACL_UNIX
if (errno != ACL_EINPROGRESS) if (errno != ACL_EINPROGRESS) {
return -1; return -1;
}
#elif defined(ACL_WINDOWS) #elif defined(ACL_WINDOWS)
if (errno != ACL_EINPROGRESS && errno != ACL_EWOULDBLOCK) if (errno != ACL_EINPROGRESS && errno != ACL_EWOULDBLOCK) {
return -1; return -1;
}
#endif #endif
/* /*
* A connection is in progress. Wait for a limited amount of time for * A connection is in progress. Wait for a limited amount of time for
* something to happen. If nothing happens, report an error. * 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; return -1;
}
/* /*
* Something happened. Some Solaris 2 versions have getsockopt() itself * 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 * connect and just returns EPIPE. Create a fake
* error message for connect. -- fenner@parc.xerox.com * error message for connect. -- fenner@parc.xerox.com
*/ */
if (errno == EPIPE) if (errno == EPIPE) {
acl_set_error(ACL_ENOTCONN); acl_set_error(ACL_ENOTCONN);
}
#endif #endif
return -1; return -1;
} }
@ -78,6 +84,7 @@ int acl_timed_connect(ACL_SOCKET sock, const struct sockaddr * sa,
if (err != 0) { if (err != 0) {
acl_set_error(err); acl_set_error(err);
return -1; return -1;
} else } else {
return 0; return 0;
}
} }

View File

@ -60,7 +60,7 @@ public:
/** /**
* HTTP * HTTP
* @param addr {const char*} IP:PORT DOMAIN:PORT * @param addr {const char*} IP|PORT DOMAIN|PORT
* @param conn_timeout {int} () * @param conn_timeout {int} ()
* @param rw_timeout {int} () * @param rw_timeout {int} ()
* @param unzip {bool} * @param unzip {bool}

View File

@ -41,7 +41,8 @@ public:
/** /**
* socket_stream * socket_stream
* @param addr {const char*} WEB * @param addr {const char*} WEB domain|port
* www.baidu.com|80
* @param conn_timeout {int} () * @param conn_timeout {int} ()
* @param rw_timeout {int} IO () * @param rw_timeout {int} IO ()
* @param unzip {bool} * @param unzip {bool}

View File

@ -47,8 +47,8 @@ public:
* acl @ Linux * acl @ Linux
* abstract unix domain socket Linux , * abstract unix domain socket Linux ,
* @/tmp/test.sock; * @/tmp/test.sock;
* TCP服务器: [${local_ip}@]${remote_addr}, * TCP服务器: remote_addr[@local_ip],
* : 60.28.250.199@www.sina.com:80, * : www.sina.com:80@60.28.250.199,
* : 60.28.250.199, www.sina.com 80, * : 60.28.250.199, www.sina.com 80,
* OS自动绑定本地 IP www.sina.com:80 * OS自动绑定本地 IP www.sina.com:80
* @param conn_timeout {int} () * @param conn_timeout {int} ()

View File

@ -99,8 +99,7 @@ static void fiber_main(ACL_FIBER *fiber acl_unused, void *ctx)
char *addr = (char *) ctx; char *addr = (char *) ctx;
int i; 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_create(fiber_connect, addr, 327680);
//acl_fiber_sleep(1); //acl_fiber_sleep(1);
} }