aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/network.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/backend/utils/adt/network.c')
-rw-r--r--src/backend/utils/adt/network.c109
1 files changed, 108 insertions, 1 deletions
diff --git a/src/backend/utils/adt/network.c b/src/backend/utils/adt/network.c
index acad79dec63..49e2694261a 100644
--- a/src/backend/utils/adt/network.c
+++ b/src/backend/utils/adt/network.c
@@ -1,7 +1,7 @@
/*
* PostgreSQL type definitions for the INET and CIDR types.
*
- * $PostgreSQL: pgsql/src/backend/utils/adt/network.c,v 1.49 2003/12/01 18:50:19 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/adt/network.c,v 1.50 2004/05/26 18:35:38 momjian Exp $
*
* Jon Postel RIP 16 Oct 1998
*/
@@ -14,7 +14,10 @@
#include <arpa/inet.h>
#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"
@@ -130,6 +133,110 @@ cidr_in(PG_FUNCTION_ARGS)
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.
*/