Commit 24c586ce authored by Dridi Boukelmoune's avatar Dridi Boukelmoune

Add an optional default port argument to std.ip()

And sync the documentation with the current behavior, part of which used
to be implicit.

Conflicts:
	lib/libvmod_std/vmod.vcc
	lib/libvmod_std/vmod_std_conversions.c
parent 5618bade
......@@ -164,16 +164,29 @@ Example
| ...
| }
$Function IP ip(STRING s, IP fallback, BOOL resolve = 1)
$Function IP ip(STRING s, IP fallback, BOOL resolve = 1, STRING p = "80")
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 false, getaddrinfo() is called using *AI_NUMERICHOST*
to avoid network lookups. This makes "pure" IP strings cheaper to
convert.
Converts the string *s* to the first IP number returned by the
system library function `getaddrinfo(3)`. If conversion fails,
*fallback* will be returned or VCL failure will happen.
The IP address includes a port number that can be found with
``std.port()`` that defaults to 80. The default port can be set
to a different value with the *p* argument. It will be overriden
if *s* contains both an IP address and a port number or service
name.
When *s* contains both, the syntax is either ``address:port`` or
``address port``. If the address is a numerical IPv6 address it
must be enclosed between brackets, for example ``[::1] 80`` or
``[::1]:http``. The *fallback* may also contain both an address
and a port.
If *resolve* is false, `getaddrinfo(3)` is called using
``AI_NUMERICHOST`` and ``AI_NUMERICSERV`` to avoid network lookups
depending on the system's `getaddrinfo(3)` or nsswitch configuration.
This makes "numerical" IP strings and services cheaper to convert.
Example
| if (std.ip(req.http.X-forwarded-for, "0.0.0.0") ~ my_acl) {
......
......@@ -78,7 +78,7 @@ vmod_integer(VRT_CTX, VCL_STRING p, VCL_INT i)
}
VCL_IP
vmod_ip(VRT_CTX, VCL_STRING s, VCL_IP d, VCL_BOOL n)
vmod_ip(VRT_CTX, VCL_STRING s, VCL_IP d, VCL_BOOL n, VCL_STRING default_port)
{
void *p;
VCL_IP retval;
......@@ -94,8 +94,9 @@ vmod_ip(VRT_CTX, VCL_STRING s, VCL_IP d, VCL_BOOL n)
return (NULL);
}
retval = VSS_ResolveFirst(p, s, "80", AF_UNSPEC, SOCK_STREAM,
n ? 0 : AI_NUMERICHOST);
retval = VSS_ResolveFirst(p, s, default_port, AF_UNSPEC, SOCK_STREAM,
n ? 0 : AI_NUMERICHOST|AI_NUMERICSERV);
if (retval != NULL)
return (retval);
......
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