[master] e475ad019 Add an optional default port argument to std.ip()

Dridi Boukelmoune dridi.boukelmoune at gmail.com
Tue May 21 07:46:09 UTC 2019


commit e475ad0196812992d378ff24eef3ee54ec1a23be
Author: Dridi Boukelmoune <dridi.boukelmoune at gmail.com>
Date:   Wed May 15 11:13:20 2019 +0200

    Add an optional default port argument to std.ip()
    
    And sync the documentation with the current behavior, part of which used
    to be implicit.

diff --git a/lib/libvmod_std/vmod.vcc b/lib/libvmod_std/vmod.vcc
index 789c8c125..05ec3aec7 100644
--- a/lib/libvmod_std/vmod.vcc
+++ b/lib/libvmod_std/vmod.vcc
@@ -222,8 +222,7 @@ Returns ``true`` if the backend *be* is healthy.
 $Function INT port(IP ip)
 
 Returns the port number of the IP address *ip*. Always returns ``0``
-for a ``*.ip`` variable whose value is ``0.0.0.0`` because the listen
-address is a Unix domain socket.
+for a ``*.ip`` variable when the address is a Unix domain socket.
 
 Type Conversion functions
 =========================
@@ -328,15 +327,26 @@ Examples::
 	set resp.http.answer = std.integer(real=126.42/3);
 
 
-$Function IP ip(STRING s, [IP fallback], BOOL resolve = 1)
+$Function IP ip(STRING s, [IP fallback], BOOL resolve = 1, [STRING p])
 
-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.
+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``
-to avoid network lookups. This makes "numerical" IP strings cheaper
-to convert.
+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::
 
diff --git a/lib/libvmod_std/vmod_std_conversions.c b/lib/libvmod_std/vmod_std_conversions.c
index d38740ec0..b0c07298f 100644
--- a/lib/libvmod_std/vmod_std_conversions.c
+++ b/lib/libvmod_std/vmod_std_conversions.c
@@ -206,8 +206,11 @@ vmod_ip(VRT_CTX, struct VARGS(ip) *a)
 		return (NULL);
 	}
 
-	retval = VSS_ResolveFirst(p, a->s, "80", AF_UNSPEC, SOCK_STREAM,
-	    a->resolve ? 0 : AI_NUMERICHOST);
+	retval = VSS_ResolveFirst(
+	    p, a->s, a->valid_p ? a->p : "80",
+	    AF_UNSPEC, SOCK_STREAM,
+	    a->resolve ? 0 : AI_NUMERICHOST|AI_NUMERICSERV);
+
 	if (retval != NULL)
 		return (retval);
 


More information about the varnish-commit mailing list