Temporary fix for the problem that pg_stat_activity, inet_client_addr(),
authorTom Lane <[email protected]>
Thu, 17 May 2007 23:31:59 +0000 (23:31 +0000)
committerTom Lane <[email protected]>
Thu, 17 May 2007 23:31:59 +0000 (23:31 +0000)
and inet_server_addr() fail if the client connected over a "scoped" IPv6
address.  In this case getnameinfo() will return a string ending with
a poorly-standardized "%something" zone specifier, which these functions
try to feed to network_in(), which won't take it.  So that we don't lose
functionality altogether, suppress the zone specifier before giving the
string to network_in().  Per report from Brian Hirt.

TODO: probably someday the inet type should support scoped IPv6 addresses,
and then this patch should be reverted.

Backpatch to 8.2 ... is it worth going further?

src/backend/utils/adt/network.c
src/backend/utils/adt/pgstatfuncs.c
src/include/utils/builtins.h

index 4520ceceab995b266b96f19ba651b5d1ae089b13..f0d8b0f9cbe625722d79d096b91a98416b8f026b 100644 (file)
@@ -1138,6 +1138,8 @@ inet_client_addr(PG_FUNCTION_ARGS)
        if (ret)
                PG_RETURN_NULL();
 
+       clean_ipv6_addr(port->raddr.addr.ss_family, remote_host);
+
        PG_RETURN_INET_P(network_in(remote_host, false));
 }
 
@@ -1212,6 +1214,8 @@ inet_server_addr(PG_FUNCTION_ARGS)
        if (ret)
                PG_RETURN_NULL();
 
+       clean_ipv6_addr(port->laddr.addr.ss_family, local_host);
+
        PG_RETURN_INET_P(network_in(local_host, false));
 }
 
@@ -1483,3 +1487,32 @@ inetmi(PG_FUNCTION_ARGS)
 
        PG_RETURN_INT64(res);
 }
+
+
+/*
+ * clean_ipv6_addr --- remove any '%zone' part from an IPv6 address string
+ *
+ * XXX This should go away someday!
+ *
+ * This is a kluge needed because we don't yet support zones in stored inet
+ * values.  Since the result of getnameinfo() might include a zone spec,
+ * call this to remove it anywhere we want to feed getnameinfo's output to
+ * network_in.  Beats failing entirely.
+ *
+ * An alternative approach would be to let network_in ignore %-parts for
+ * itself, but that would mean we'd silently drop zone specs in user input,
+ * which seems not such a good idea.
+ */
+void
+clean_ipv6_addr(int addr_family, char *addr)
+{
+#ifdef HAVE_IPV6
+       if (addr_family == AF_INET6)
+       {
+               char *pct = strchr(addr, '%');
+
+               if (pct)
+                       *pct = '\0';
+       }
+#endif
+}
index a3305e2f3203ad7e4ff6b0782930881f3608e4cd..6b7416d79309919f64cda9928c243e57d362ced8 100644 (file)
@@ -485,6 +485,8 @@ pg_stat_get_backend_client_addr(PG_FUNCTION_ARGS)
        if (ret)
                PG_RETURN_NULL();
 
+       clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
+
        PG_RETURN_INET_P(DirectFunctionCall1(inet_in,
                                                                                 CStringGetDatum(remote_host)));
 }
index 8cc05547b53b3ce3304be99f0f127f3f48f69c9e..bb04e7b03e6dd6c0b3b68bb01d340cfad8de5b0c 100644 (file)
@@ -767,6 +767,7 @@ extern Datum inetor(PG_FUNCTION_ARGS);
 extern Datum inetpl(PG_FUNCTION_ARGS);
 extern Datum inetmi_int8(PG_FUNCTION_ARGS);
 extern Datum inetmi(PG_FUNCTION_ARGS);
+extern void clean_ipv6_addr(int addr_family, char *addr);
 
 /* mac.c */
 extern Datum macaddr_in(PG_FUNCTION_ARGS);