summaryrefslogtreecommitdiff
path: root/src/anet.c
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2014-12-11 18:20:30 +0100
committerantirez <antirez@gmail.com>2014-12-11 18:20:30 +0100
commitce269ad3c5a13636b2c87aa69473c30410b06c36 (patch)
treecfa3d8c4dd4299e7c07b5917be5eb0be094e613f /src/anet.c
parent491881e13b73ab1686328efdceb4cdf8bde64046 (diff)
downloadredis-ce269ad3c5a13636b2c87aa69473c30410b06c36.tar.gz
AnetFormatIP(): renamed, commented, now sticks to IP:port format.
A few code style changes + consistent format: not nice for humans but better for parsers.
Diffstat (limited to 'src/anet.c')
-rw-r--r--src/anet.c19
1 files changed, 10 insertions, 9 deletions
diff --git a/src/anet.c b/src/anet.c
index 3e39751d7..76e9b67ae 100644
--- a/src/anet.c
+++ b/src/anet.c
@@ -589,20 +589,21 @@ error:
return -1;
}
-int anetFormatIP(char *fmt, size_t fmt_len, char *ip, int port) {
- if (port >= 0)
- return snprintf(fmt,fmt_len,
- strchr(ip,':') ? "[%s]:%d" : "%s:%d", ip, port);
- else
- return snprintf(fmt, fmt_len, strchr(ip,':') ? "[%s]" : "%s", ip);
+/* 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);
}
-int anetFormatPeer(int fd, char *fmt, size_t fmt_len) {
+/* Like anetFormatAddr() but extract ip and port from the socket's peer. */
+int anetFormatPeer(int fd, char *buf, size_t buf_len) {
char ip[INET6_ADDRSTRLEN];
int port;
anetPeerToString(fd,ip,sizeof(ip),&port);
- return anetFormatIP(fmt, fmt_len, ip, port);
+ return anetFormatAddr(buf, buf_len, ip, port);
}
int anetSockName(int fd, char *ip, size_t ip_len, int *port) {
@@ -632,5 +633,5 @@ int anetFormatSock(int fd, char *fmt, size_t fmt_len) {
int port;
anetSockName(fd,ip,sizeof(ip),&port);
- return anetFormatIP(fmt, fmt_len, ip, port);
+ return anetFormatAddr(fmt, fmt_len, ip, port);
}