summaryrefslogtreecommitdiff
path: root/src/anet.c
diff options
context:
space:
mode:
authorzhenwei pi <pizhenwei@bytedance.com>2022-07-27 10:08:32 +0800
committerzhenwei pi <pizhenwei@bytedance.com>2022-08-22 15:01:40 +0800
commitbff7ecc7864716c14fbb399f19acaee364975b29 (patch)
tree7f342f044ee6e39769b55e1a1754abb4ffc39dac /src/anet.c
parentb9d77288243aeab306b99ff72a71d8490a91e0a8 (diff)
downloadredis-bff7ecc7864716c14fbb399f19acaee364975b29.tar.gz
Introduce connAddr
Originally, connPeerToString is designed to get the address info from socket only(for both TCP & TLS), and the API 'connPeerToString' is oriented to operate a FD like: int connPeerToString(connection *conn, char *ip, size_t ip_len, int *port) { return anetFdToString(conn ? conn->fd : -1, ip, ip_len, port, FD_TO_PEER_NAME); } Introduce connAddr and implement .addr method for socket and TLS, thus the API 'connAddr' and 'connFormatAddr' become oriented to a connection like: static inline int connAddr(connection *conn, char *ip, size_t ip_len, int *port, int remote) { if (conn && conn->type->addr) { return conn->type->addr(conn, ip, ip_len, port, remote); } return -1; } Also remove 'FD_TO_PEER_NAME' & 'FD_TO_SOCK_NAME', use a boolean type 'remote' to get local/remote address of a connection. With these changes, it's possible to support the other connection types which does not use socket(Ex, RDMA). Thanks to Oran for suggestions! Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
Diffstat (limited to 'src/anet.c')
-rw-r--r--src/anet.c21
1 files changed, 2 insertions, 19 deletions
diff --git a/src/anet.c b/src/anet.c
index d4411382c..00c30b83d 100644
--- a/src/anet.c
+++ b/src/anet.c
@@ -579,11 +579,11 @@ int anetUnixAccept(char *err, int s) {
return fd;
}
-int anetFdToString(int fd, char *ip, size_t ip_len, int *port, int fd_to_str_type) {
+int anetFdToString(int fd, char *ip, size_t ip_len, int *port, int remote) {
struct sockaddr_storage sa;
socklen_t salen = sizeof(sa);
- if (fd_to_str_type == FD_TO_PEER_NAME) {
+ if (remote) {
if (getpeername(fd, (struct sockaddr *)&sa, &salen) == -1) goto error;
} else {
if (getsockname(fd, (struct sockaddr *)&sa, &salen) == -1) goto error;
@@ -627,23 +627,6 @@ error:
return -1;
}
-/* Format an IP,port pair into something easy to parse. If IP is IPv6
- * (matches for ":"), the ip is surrounded by []. IP and port are just
- * separated by colons. This the standard to display addresses within Redis. */
-int anetFormatAddr(char *buf, size_t buf_len, char *ip, int port) {
- return snprintf(buf,buf_len, strchr(ip,':') ?
- "[%s]:%d" : "%s:%d", ip, port);
-}
-
-/* Like anetFormatAddr() but extract ip and port from the socket's peer/sockname. */
-int anetFormatFdAddr(int fd, char *buf, size_t buf_len, int fd_to_str_type) {
- char ip[INET6_ADDRSTRLEN];
- int port;
-
- anetFdToString(fd,ip,sizeof(ip),&port,fd_to_str_type);
- return anetFormatAddr(buf, buf_len, ip, port);
-}
-
/* Create a pipe buffer with given flags for read end and write end.
* Note that it supports the file flags defined by pipe2() and fcntl(F_SETFL),
* and one of the use cases is O_CLOEXEC|O_NONBLOCK. */