diff options
author | zhenwei pi <pizhenwei@bytedance.com> | 2022-07-27 10:08:32 +0800 |
---|---|---|
committer | zhenwei pi <pizhenwei@bytedance.com> | 2022-08-22 15:01:40 +0800 |
commit | bff7ecc7864716c14fbb399f19acaee364975b29 (patch) | |
tree | 7f342f044ee6e39769b55e1a1754abb4ffc39dac /src/networking.c | |
parent | b9d77288243aeab306b99ff72a71d8490a91e0a8 (diff) | |
download | redis-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/networking.c')
-rw-r--r-- | src/networking.c | 14 |
1 files changed, 7 insertions, 7 deletions
diff --git a/src/networking.c b/src/networking.c index 6bf39d11a..04ba54768 100644 --- a/src/networking.c +++ b/src/networking.c @@ -1196,7 +1196,7 @@ int islocalClient(client *c) { /* tcp */ char cip[NET_IP_STR_LEN+1] = { 0 }; - connPeerToString(c->conn, cip, sizeof(cip)-1, NULL); + connAddrPeerName(c->conn, cip, sizeof(cip)-1, NULL); return !strcmp(cip,"127.0.0.1") || !strcmp(cip,"::1"); } @@ -2700,13 +2700,13 @@ done: * you want to relax error checking or need to display something anyway (see * anetFdToString implementation for more info). */ void genClientAddrString(client *client, char *addr, - size_t addr_len, int fd_to_str_type) { + size_t addr_len, int remote) { if (client->flags & CLIENT_UNIX_SOCKET) { /* Unix socket client. */ snprintf(addr,addr_len,"%s:0",server.unixsocket); } else { /* TCP client. */ - connFormatFdAddr(client->conn,addr,addr_len,fd_to_str_type); + connFormatAddr(client->conn,addr,addr_len,remote); } } @@ -2715,10 +2715,10 @@ void genClientAddrString(client *client, char *addr, * The Peer ID never changes during the life of the client, however it * is expensive to compute. */ char *getClientPeerId(client *c) { - char peerid[NET_ADDR_STR_LEN]; + char peerid[NET_ADDR_STR_LEN] = {0}; if (c->peerid == NULL) { - genClientAddrString(c,peerid,sizeof(peerid),FD_TO_PEER_NAME); + genClientAddrString(c,peerid,sizeof(peerid),1); c->peerid = sdsnew(peerid); } return c->peerid; @@ -2729,10 +2729,10 @@ char *getClientPeerId(client *c) { * The Socket Name never changes during the life of the client, however it * is expensive to compute. */ char *getClientSockname(client *c) { - char sockname[NET_ADDR_STR_LEN]; + char sockname[NET_ADDR_STR_LEN] = {0}; if (c->sockname == NULL) { - genClientAddrString(c,sockname,sizeof(sockname),FD_TO_SOCK_NAME); + genClientAddrString(c,sockname,sizeof(sockname),0); c->sockname = sdsnew(sockname); } return c->sockname; |