summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2014-01-10 15:02:39 +0100
committerantirez <antirez@gmail.com>2014-01-10 15:02:39 +0100
commit774f0bd45e04caf606c09ca774283f36252998c6 (patch)
tree46918abf57f36653069ebc7227a9e70d779629f9
parentc42e4bd0b62d5e542d9ff100b2a9ab374f84fb1a (diff)
downloadredis-774f0bd45e04caf606c09ca774283f36252998c6.tar.gz
anetResolveIP() added to anet.c.
The new function is used when we want to normalize an IP address without performing a DNS lookup if the string to resolve is not a valid IP. This is useful every time only IPs are valid inputs or when we want to skip DNS resolution that is slow during runtime operations if we are required to block.
-rw-r--r--src/anet.c19
-rw-r--r--src/anet.h4
2 files changed, 22 insertions, 1 deletions
diff --git a/src/anet.c b/src/anet.c
index ef52d955d..fc585c8d1 100644
--- a/src/anet.c
+++ b/src/anet.c
@@ -163,12 +163,21 @@ int anetTcpKeepAlive(char *err, int fd)
return ANET_OK;
}
-int anetResolve(char *err, char *host, char *ipbuf, size_t ipbuf_len)
+/* anetGenericResolve() is called by anetResolve() and anetResolveIP() to
+ * do the actual work. It resolves the hostname "host" and set the string
+ * representation of the IP address into the buffer pointed by "ipbuf".
+ *
+ * If flags is set to ANET_IP_ONLY the function only resolves hostnames
+ * that are actually already IPv4 or IPv6 addresses. This turns the function
+ * into a validating / normalizing function. */
+int anetGenericResolve(char *err, char *host, char *ipbuf, size_t ipbuf_len,
+ int flags)
{
struct addrinfo hints, *info;
int rv;
memset(&hints,0,sizeof(hints));
+ if (flags & ANET_IP_ONLY) hints.ai_flags = AI_NUMERICHOST;
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM; /* specify socktype to avoid dups */
@@ -188,6 +197,14 @@ int anetResolve(char *err, char *host, char *ipbuf, size_t ipbuf_len)
return ANET_OK;
}
+int anetResolve(char *err, char *host, char *ipbuf, size_t ipbuf_len) {
+ return anetGenericResolve(err,host,ipbuf,ipbuf_len,ANET_NONE);
+}
+
+int anetResolveIP(char *err, char *host, char *ipbuf, size_t ipbuf_len) {
+ return anetGenericResolve(err,host,ipbuf,ipbuf_len,ANET_IP_ONLY);
+}
+
static int anetSetReuseAddr(char *err, int fd) {
int yes = 1;
/* Make sure connection-intensive things like the redis benckmark
diff --git a/src/anet.h b/src/anet.h
index b23411cbb..f0ab63ab7 100644
--- a/src/anet.h
+++ b/src/anet.h
@@ -35,6 +35,10 @@
#define ANET_ERR -1
#define ANET_ERR_LEN 256
+/* Flags used with certain functions. */
+#define ANET_NONE 0
+#define ANET_IP_ONLY (1<<0)
+
#if defined(__sun)
#define AF_LOCAL AF_UNIX
#endif