Index: doc/src/sgml/func.sgml
===================================================================
RCS file: /projects/cvsroot/pgsql-server/doc/src/sgml/func.sgml,v
retrieving revision 1.202
diff -u -r1.202 func.sgml
--- doc/src/sgml/func.sgml 14 May 2004 21:42:27 -0000 1.202
+++ doc/src/sgml/func.sgml 18 May 2004 18:59:37 -0000
@@ -6593,6 +6593,30 @@
+ inet_client_addr
+ inet
+ address of the remote connection
+
+
+
+ inet_client_port
+ int4
+ port of the remote connection
+
+
+
+ inet_server_addr
+ inet
+ address of the local connection
+
+
+
+ inet_server_port
+ int4
+ port of the local connection
+
+
+
session_user
name
session user name
@@ -6646,6 +6670,17 @@
they must be called without trailing parentheses.
+
+
+ inet_client_addr and
+ inet_server_addr return the IPv4 or IPv6 (if
+ configured) address of the remote or local host connecting to the
+ database, respectively. inet_client_port
+ and inet_server_port return the port number
+ of the remote or local host connecting to the database,
+ respectively. If the connection is not a network connection,
+ these functions will return NULL.
+
current_schema returns the name of the schema that is
Index: src/backend/utils/adt/network.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/utils/adt/network.c,v
retrieving revision 1.49
diff -u -r1.49 network.c
--- src/backend/utils/adt/network.c 1 Dec 2003 18:50:19 -0000 1.49
+++ src/backend/utils/adt/network.c 18 May 2004 18:59:40 -0000
@@ -14,7 +14,10 @@
#include
#include "catalog/pg_type.h"
+#include "libpq/ip.h"
+#include "libpq/libpq-be.h"
#include "libpq/pqformat.h"
+#include "miscadmin.h"
#include "utils/builtins.h"
#include "utils/inet.h"
@@ -129,6 +132,110 @@
PG_RETURN_INET_P(network_in(src, 1));
}
+
+/* INET that the client is connecting from */
+Datum
+inet_client_addr(PG_FUNCTION_ARGS)
+{
+ Port *port = MyProcPort;
+
+ if (port == NULL)
+ PG_RETURN_NULL();
+
+ switch (port->raddr.addr.ss_family) {
+ case AF_INET:
+#ifdef HAVE_IPV6
+ case AF_INET6:
+#endif
+ break;
+ default:
+ PG_RETURN_NULL();
+ }
+
+ PG_RETURN_INET_P(network_in(port->remote_host, 0));
+}
+
+
+/* port that the client is connecting from */
+Datum
+inet_client_port(PG_FUNCTION_ARGS)
+{
+ Port *port = MyProcPort;
+
+ if (port == NULL)
+ PG_RETURN_NULL();
+
+ PG_RETURN_INT32(DirectFunctionCall1(int4in, CStringGetDatum(port->remote_port)));
+}
+
+
+/* server INET that the client connected to */
+Datum
+inet_server_addr(PG_FUNCTION_ARGS)
+{
+ Port *port = MyProcPort;
+ char local_host[NI_MAXHOST];
+ int ret;
+
+ if (port == NULL)
+ PG_RETURN_NULL();
+
+ switch (port->laddr.addr.ss_family) {
+ case AF_INET:
+#ifdef HAVE_IPV6
+ case AF_INET6:
+#endif
+ break;
+ default:
+ PG_RETURN_NULL();
+ }
+
+ local_host[0] = '\0';
+
+ ret = getnameinfo_all(&port->laddr.addr, port->laddr.salen,
+ local_host, sizeof(local_host),
+ NULL, 0,
+ NI_NUMERICHOST | NI_NUMERICSERV);
+ if (ret)
+ PG_RETURN_NULL();
+
+ PG_RETURN_INET_P(network_in(local_host, 0));
+}
+
+
+/* port that the server accepted the connection on */
+Datum
+inet_server_port(PG_FUNCTION_ARGS)
+{
+ Port *port = MyProcPort;
+ char local_port[NI_MAXSERV];
+ int ret;
+
+ if (port == NULL)
+ PG_RETURN_NULL();
+
+ switch (port->laddr.addr.ss_family) {
+ case AF_INET:
+#ifdef HAVE_IPV6
+ case AF_INET6:
+#endif
+ break;
+ default:
+ PG_RETURN_NULL();
+ }
+
+ local_port[0] = '\0';
+
+ ret = getnameinfo_all(&port->laddr.addr, port->laddr.salen,
+ NULL, 0,
+ local_port, sizeof(local_port),
+ NI_NUMERICHOST | NI_NUMERICSERV);
+ if (ret)
+ PG_RETURN_NULL();
+
+ PG_RETURN_INT32(DirectFunctionCall1(int4in, CStringGetDatum(local_port)));
+}
+
/*
* INET address output function.
Index: src/include/catalog/pg_proc.h
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/include/catalog/pg_proc.h,v
retrieving revision 1.329
diff -u -r1.329 pg_proc.h
--- src/include/catalog/pg_proc.h 14 May 2004 21:42:28 -0000 1.329
+++ src/include/catalog/pg_proc.h 18 May 2004 18:59:41 -0000
@@ -2343,6 +2345,15 @@
DESCR("I/O");
DATA(insert OID = 911 ( inet_out PGNSP PGUID 12 f f t f i 1 2275 "869" _null_ inet_out - _null_ ));
DESCR("I/O");
+
+DATA(insert OID = 912 ( inet_client_addr PGNSP PGUID 12 f f f f s 0 869 "" _null_ inet_client_addr - _null_ ));
+DESCR("Returns the INET address of the client connected to the backend");
+DATA(insert OID = 913 ( inet_client_port PGNSP PGUID 12 f f f f s 0 23 "" _null_ inet_client_port - _null_ ));
+DESCR("Returns the client's port number for this connection");
+DATA(insert OID = 914 ( inet_server_addr PGNSP PGUID 12 f f f f s 0 869 "" _null_ inet_server_addr - _null_ ));
+DESCR("Returns the INET address that the backend is using to service the connection");
+DATA(insert OID = 915 ( inet_server_port PGNSP PGUID 12 f f f f s 0 23 "" _null_ inet_server_port - _null_ ));
+DESCR("Returns the servers's port number for this connection");
/* for cidr type support */
DATA(insert OID = 1267 ( cidr_in PGNSP PGUID 12 f f t f i 1 650 "2275" _null_ cidr_in - _null_ ));
Index: src/include/utils/builtins.h
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/include/utils/builtins.h,v
retrieving revision 1.238
diff -u -r1.238 builtins.h
--- src/include/utils/builtins.h 14 May 2004 21:42:30 -0000 1.238
+++ src/include/utils/builtins.h 18 May 2004 18:59:41 -0000
@@ -645,6 +645,10 @@
void *dst, size_t size);
/* network.c */
+extern Datum inet_client_addr(PG_FUNCTION_ARGS);
+extern Datum inet_client_port(PG_FUNCTION_ARGS);
+extern Datum inet_server_addr(PG_FUNCTION_ARGS);
+extern Datum inet_server_port(PG_FUNCTION_ARGS);
extern Datum inet_in(PG_FUNCTION_ARGS);
extern Datum inet_out(PG_FUNCTION_ARGS);
extern Datum inet_recv(PG_FUNCTION_ARGS);