Commit ce0ccf57 authored by Guillaume Quintard's avatar Guillaume Quintard Committed by guillaume quintard

Introduce optional resolve arg to std.ip

parent 918c848d
...@@ -15,6 +15,8 @@ varnish v1 -vcl+backend { ...@@ -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.foo3 = std.ip("1.2.3.5", "127.0.0.3");
set resp.http.foo4 = std.ip("2001:db8::", "[::1]"); set resp.http.foo4 = std.ip("2001:db8::", "[::1]");
set resp.http.foo5 = 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 } -start
...@@ -29,4 +31,6 @@ client c1 { ...@@ -29,4 +31,6 @@ client c1 {
expect resp.http.foo3 == "1.2.3.5" expect resp.http.foo3 == "1.2.3.5"
expect resp.http.foo4 == "2001:db8::" expect resp.http.foo4 == "2001:db8::"
expect resp.http.foo5 == "::1" expect resp.http.foo5 == "::1"
expect resp.http.foo6 == "0.0.0.0"
expect resp.http.foo7 == "1.2.3.4"
} -run } -run
...@@ -165,12 +165,17 @@ Example ...@@ -165,12 +165,17 @@ Example
| ... | ...
| } | }
$Function IP ip(STRING s, IP fallback) $Function IP ip(STRING s, IP fallback, BOOL resolve = 1)
Description Description
Converts the string *s* to the first IP number returned by Converts the string *s* to the first IP number returned by
the system library function getaddrinfo(3). If conversion the system library function getaddrinfo(3). If conversion
fails, *fallback* will be returned. 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 Example
| if (std.ip(req.http.X-forwarded-for, "0.0.0.0") ~ my_acl) { | if (std.ip(req.http.X-forwarded-for, "0.0.0.0") ~ my_acl) {
| ... | ...
......
...@@ -77,7 +77,7 @@ vmod_integer(VRT_CTX, VCL_STRING p, VCL_INT i) ...@@ -77,7 +77,7 @@ vmod_integer(VRT_CTX, VCL_STRING p, VCL_INT i)
} }
VCL_IP 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; struct addrinfo hints, *res0 = NULL;
const struct addrinfo *res; const struct addrinfo *res;
...@@ -101,6 +101,8 @@ vmod_ip(VRT_CTX, VCL_STRING s, VCL_IP d) ...@@ -101,6 +101,8 @@ vmod_ip(VRT_CTX, VCL_STRING s, VCL_IP d)
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC; hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
if (!n)
hints.ai_flags |= AI_NUMERICHOST;
error = getaddrinfo(s, "80", &hints, &res0); error = getaddrinfo(s, "80", &hints, &res0);
if (!error) { if (!error) {
for (res = res0; res != NULL; res = res->ai_next) { for (res = res0; res != NULL; res = res->ai_next) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment