[master] ce0ccf5 Introduce optional resolve arg to std.ip
guillaume quintard
gquintard at users.noreply.github.com
Mon Feb 12 12:53:06 UTC 2018
commit ce0ccf578c9c102be831d7d08877b590d0c821e0
Author: Guillaume Quintard <guillaume at varnish-software.com>
Date: Wed Jan 31 10:35:22 2018 +0100
Introduce optional resolve arg to std.ip
diff --git a/bin/varnishtest/tests/m00011.vtc b/bin/varnishtest/tests/m00011.vtc
index acf3912..cd49806 100644
--- a/bin/varnishtest/tests/m00011.vtc
+++ b/bin/varnishtest/tests/m00011.vtc
@@ -15,6 +15,8 @@ varnish v1 -vcl+backend {
set resp.http.foo3 = std.ip("1.2.3.5", "127.0.0.3");
set resp.http.foo4 = std.ip("2001:db8::", "[::1]");
set resp.http.foo5 = std.ip("2001::db8::", "[::1]");
+ set resp.http.foo6 = std.ip("localhost", "0.0.0.0", resolve = false);
+ set resp.http.foo7 = std.ip("1.2.3.4", "0.0.0.0", resolve = false);
}
} -start
@@ -29,4 +31,6 @@ client c1 {
expect resp.http.foo3 == "1.2.3.5"
expect resp.http.foo4 == "2001:db8::"
expect resp.http.foo5 == "::1"
+ expect resp.http.foo6 == "0.0.0.0"
+ expect resp.http.foo7 == "1.2.3.4"
} -run
diff --git a/lib/libvmod_std/vmod.vcc b/lib/libvmod_std/vmod.vcc
index 178b0b7..67d41f1 100644
--- a/lib/libvmod_std/vmod.vcc
+++ b/lib/libvmod_std/vmod.vcc
@@ -165,12 +165,17 @@ Example
| ...
| }
-$Function IP ip(STRING s, IP fallback)
+$Function IP ip(STRING s, IP fallback, BOOL resolve = 1)
Description
Converts the string *s* to the first IP number returned by
the system library function getaddrinfo(3). If conversion
fails, *fallback* will be returned.
+
+ If *resolve* is true, getaddrinfo() is called using *AI_NUMERICHOST*
+ to avoid network lookups. This makes "pure" IP strings cheaper to
+ convert.
+
Example
| if (std.ip(req.http.X-forwarded-for, "0.0.0.0") ~ my_acl) {
| ...
diff --git a/lib/libvmod_std/vmod_std_conversions.c b/lib/libvmod_std/vmod_std_conversions.c
index 18b0b47..15cd9d1 100644
--- a/lib/libvmod_std/vmod_std_conversions.c
+++ b/lib/libvmod_std/vmod_std_conversions.c
@@ -77,7 +77,7 @@ vmod_integer(VRT_CTX, VCL_STRING p, VCL_INT i)
}
VCL_IP
-vmod_ip(VRT_CTX, VCL_STRING s, VCL_IP d)
+vmod_ip(VRT_CTX, VCL_STRING s, VCL_IP d, VCL_BOOL n)
{
struct addrinfo hints, *res0 = NULL;
const struct addrinfo *res;
@@ -101,6 +101,8 @@ vmod_ip(VRT_CTX, VCL_STRING s, VCL_IP d)
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
+ if (!n)
+ hints.ai_flags |= AI_NUMERICHOST;
error = getaddrinfo(s, "80", &hints, &res0);
if (!error) {
for (res = res0; res != NULL; res = res->ai_next) {
More information about the varnish-commit
mailing list