Skip to content

Commit ba0f9ff

Browse files
committed
Code review for recently-added network functions. Get it to work when
log_hostname is enabled, clean up documentation.
1 parent 88961fc commit ba0f9ff

File tree

4 files changed

+192
-135
lines changed

4 files changed

+192
-135
lines changed

doc/src/sgml/func.sgml

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.206 2004/06/02 21:34:49 momjian Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.207 2004/06/13 19:56:49 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -6609,25 +6609,25 @@ SELECT NULLIF(value, '(none)') ...
66096609
</row>
66106610

66116611
<row>
6612-
<entry><function>inet_client_addr</function></entry>
6612+
<entry><function>inet_client_addr()</function></entry>
66136613
<entry><type>inet</type></entry>
66146614
<entry>address of the remote connection</entry>
66156615
</row>
66166616

66176617
<row>
6618-
<entry><function>inet_client_port</function></entry>
6618+
<entry><function>inet_client_port()</function></entry>
66196619
<entry><type>int4</type></entry>
66206620
<entry>port of the remote connection</entry>
66216621
</row>
66226622

66236623
<row>
6624-
<entry><function>inet_server_addr</function></entry>
6624+
<entry><function>inet_server_addr()</function></entry>
66256625
<entry><type>inet</type></entry>
66266626
<entry>address of the local connection</entry>
66276627
</row>
66286628

66296629
<row>
6630-
<entry><function>inet_server_port</function></entry>
6630+
<entry><function>inet_server_port()</function></entry>
66316631
<entry><type>int4</type></entry>
66326632
<entry>port of the local connection</entry>
66336633
</row>
@@ -6687,17 +6687,6 @@ SELECT NULLIF(value, '(none)') ...
66876687
</para>
66886688
</note>
66896689

6690-
<para>
6691-
<function>inet_client_addr</function> and
6692-
<function>inet_server_addr</function> return the IPv4 or IPv6 (if
6693-
configured) address of the remote or local host connecting to the
6694-
database, respectively. <function>inet_client_port</function>
6695-
and <function>inet_server_port</function> return the port number
6696-
of the remote or local host connecting to the database,
6697-
respectively. If the connection is not a network connection,
6698-
these functions will return <literal>NULL</literal>.
6699-
</para>
6700-
67016690
<para>
67026691
<function>current_schema</function> returns the name of the schema that is
67036692
at the front of the search path (or a null value if the search path is
@@ -6718,6 +6707,33 @@ SET search_path TO <replaceable>schema</> <optional>, <replaceable>schema</>, ..
67186707
</para>
67196708
</note>
67206709

6710+
<indexterm zone="functions-misc">
6711+
<primary>inet_client_addr</primary>
6712+
</indexterm>
6713+
6714+
<indexterm zone="functions-misc">
6715+
<primary>inet_client_port</primary>
6716+
</indexterm>
6717+
6718+
<indexterm zone="functions-misc">
6719+
<primary>inet_server_addr</primary>
6720+
</indexterm>
6721+
6722+
<indexterm zone="functions-misc">
6723+
<primary>inet_server_port</primary>
6724+
</indexterm>
6725+
6726+
<para>
6727+
<function>inet_client_addr</function> returns the IP address of the
6728+
current client, and <function>inet_client_port</function> returns the
6729+
port number.
6730+
<function>inet_server_addr</function> returns the IP address on which
6731+
the server accepted the current connection, and
6732+
<function>inet_server_port</function> returns the port number.
6733+
All these functions return NULL if the connection is via a Unix-domain
6734+
socket.
6735+
</para>
6736+
67216737
<indexterm zone="functions-misc">
67226738
<primary>version</primary>
67236739
</indexterm>

src/backend/utils/adt/network.c

Lines changed: 145 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* PostgreSQL type definitions for the INET and CIDR types.
33
*
4-
* $PostgreSQL: pgsql/src/backend/utils/adt/network.c,v 1.50 2004/05/26 18:35:38 momjian Exp $
4+
* $PostgreSQL: pgsql/src/backend/utils/adt/network.c,v 1.51 2004/06/13 19:56:50 tgl Exp $
55
*
66
* Jon Postel RIP 16 Oct 1998
77
*/
@@ -133,109 +133,6 @@ cidr_in(PG_FUNCTION_ARGS)
133133
PG_RETURN_INET_P(network_in(src, 1));
134134
}
135135

136-
/* INET that the client is connecting from */
137-
Datum
138-
inet_client_addr(PG_FUNCTION_ARGS)
139-
{
140-
Port *port = MyProcPort;
141-
142-
if (port == NULL)
143-
PG_RETURN_NULL();
144-
145-
switch (port->raddr.addr.ss_family) {
146-
case AF_INET:
147-
#ifdef HAVE_IPV6
148-
case AF_INET6:
149-
#endif
150-
break;
151-
default:
152-
PG_RETURN_NULL();
153-
}
154-
155-
PG_RETURN_INET_P(network_in(port->remote_host, 0));
156-
}
157-
158-
159-
/* port that the client is connecting from */
160-
Datum
161-
inet_client_port(PG_FUNCTION_ARGS)
162-
{
163-
Port *port = MyProcPort;
164-
165-
if (port == NULL)
166-
PG_RETURN_NULL();
167-
168-
PG_RETURN_INT32(DirectFunctionCall1(int4in, CStringGetDatum(port->remote_port)));
169-
}
170-
171-
172-
/* server INET that the client connected to */
173-
Datum
174-
inet_server_addr(PG_FUNCTION_ARGS)
175-
{
176-
Port *port = MyProcPort;
177-
char local_host[NI_MAXHOST];
178-
int ret;
179-
180-
if (port == NULL)
181-
PG_RETURN_NULL();
182-
183-
switch (port->laddr.addr.ss_family) {
184-
case AF_INET:
185-
#ifdef HAVE_IPV6
186-
case AF_INET6:
187-
#endif
188-
break;
189-
default:
190-
PG_RETURN_NULL();
191-
}
192-
193-
local_host[0] = '\0';
194-
195-
ret = getnameinfo_all(&port->laddr.addr, port->laddr.salen,
196-
local_host, sizeof(local_host),
197-
NULL, 0,
198-
NI_NUMERICHOST | NI_NUMERICSERV);
199-
if (ret)
200-
PG_RETURN_NULL();
201-
202-
PG_RETURN_INET_P(network_in(local_host, 0));
203-
}
204-
205-
206-
/* port that the server accepted the connection on */
207-
Datum
208-
inet_server_port(PG_FUNCTION_ARGS)
209-
{
210-
Port *port = MyProcPort;
211-
char local_port[NI_MAXSERV];
212-
int ret;
213-
214-
if (port == NULL)
215-
PG_RETURN_NULL();
216-
217-
switch (port->laddr.addr.ss_family) {
218-
case AF_INET:
219-
#ifdef HAVE_IPV6
220-
case AF_INET6:
221-
#endif
222-
break;
223-
default:
224-
PG_RETURN_NULL();
225-
}
226-
227-
local_port[0] = '\0';
228-
229-
ret = getnameinfo_all(&port->laddr.addr, port->laddr.salen,
230-
NULL, 0,
231-
local_port, sizeof(local_port),
232-
NI_NUMERICHOST | NI_NUMERICSERV);
233-
if (ret)
234-
PG_RETURN_NULL();
235-
236-
PG_RETURN_INT32(DirectFunctionCall1(int4in, CStringGetDatum(local_port)));
237-
}
238-
239136

240137
/*
241138
* INET address output function.
@@ -1069,3 +966,147 @@ network_scan_last(Datum in)
1069966
DirectFunctionCall1(network_broadcast, in),
1070967
Int32GetDatum(-1));
1071968
}
969+
970+
971+
/*
972+
* IP address that the client is connecting from (NULL if Unix socket)
973+
*/
974+
Datum
975+
inet_client_addr(PG_FUNCTION_ARGS)
976+
{
977+
Port *port = MyProcPort;
978+
char remote_host[NI_MAXHOST];
979+
int ret;
980+
981+
if (port == NULL)
982+
PG_RETURN_NULL();
983+
984+
switch (port->raddr.addr.ss_family) {
985+
case AF_INET:
986+
#ifdef HAVE_IPV6
987+
case AF_INET6:
988+
#endif
989+
break;
990+
default:
991+
PG_RETURN_NULL();
992+
}
993+
994+
remote_host[0] = '\0';
995+
996+
ret = getnameinfo_all(&port->raddr.addr, port->raddr.salen,
997+
remote_host, sizeof(remote_host),
998+
NULL, 0,
999+
NI_NUMERICHOST | NI_NUMERICSERV);
1000+
if (ret)
1001+
PG_RETURN_NULL();
1002+
1003+
PG_RETURN_INET_P(network_in(remote_host, 0));
1004+
}
1005+
1006+
1007+
/*
1008+
* port that the client is connecting from (NULL if Unix socket)
1009+
*/
1010+
Datum
1011+
inet_client_port(PG_FUNCTION_ARGS)
1012+
{
1013+
Port *port = MyProcPort;
1014+
char remote_port[NI_MAXSERV];
1015+
int ret;
1016+
1017+
if (port == NULL)
1018+
PG_RETURN_NULL();
1019+
1020+
switch (port->raddr.addr.ss_family) {
1021+
case AF_INET:
1022+
#ifdef HAVE_IPV6
1023+
case AF_INET6:
1024+
#endif
1025+
break;
1026+
default:
1027+
PG_RETURN_NULL();
1028+
}
1029+
1030+
remote_port[0] = '\0';
1031+
1032+
ret = getnameinfo_all(&port->raddr.addr, port->raddr.salen,
1033+
NULL, 0,
1034+
remote_port, sizeof(remote_port),
1035+
NI_NUMERICHOST | NI_NUMERICSERV);
1036+
if (ret)
1037+
PG_RETURN_NULL();
1038+
1039+
PG_RETURN_DATUM(DirectFunctionCall1(int4in, CStringGetDatum(remote_port)));
1040+
}
1041+
1042+
1043+
/*
1044+
* IP address that the server accepted the connection on (NULL if Unix socket)
1045+
*/
1046+
Datum
1047+
inet_server_addr(PG_FUNCTION_ARGS)
1048+
{
1049+
Port *port = MyProcPort;
1050+
char local_host[NI_MAXHOST];
1051+
int ret;
1052+
1053+
if (port == NULL)
1054+
PG_RETURN_NULL();
1055+
1056+
switch (port->laddr.addr.ss_family) {
1057+
case AF_INET:
1058+
#ifdef HAVE_IPV6
1059+
case AF_INET6:
1060+
#endif
1061+
break;
1062+
default:
1063+
PG_RETURN_NULL();
1064+
}
1065+
1066+
local_host[0] = '\0';
1067+
1068+
ret = getnameinfo_all(&port->laddr.addr, port->laddr.salen,
1069+
local_host, sizeof(local_host),
1070+
NULL, 0,
1071+
NI_NUMERICHOST | NI_NUMERICSERV);
1072+
if (ret)
1073+
PG_RETURN_NULL();
1074+
1075+
PG_RETURN_INET_P(network_in(local_host, 0));
1076+
}
1077+
1078+
1079+
/*
1080+
* port that the server accepted the connection on (NULL if Unix socket)
1081+
*/
1082+
Datum
1083+
inet_server_port(PG_FUNCTION_ARGS)
1084+
{
1085+
Port *port = MyProcPort;
1086+
char local_port[NI_MAXSERV];
1087+
int ret;
1088+
1089+
if (port == NULL)
1090+
PG_RETURN_NULL();
1091+
1092+
switch (port->laddr.addr.ss_family) {
1093+
case AF_INET:
1094+
#ifdef HAVE_IPV6
1095+
case AF_INET6:
1096+
#endif
1097+
break;
1098+
default:
1099+
PG_RETURN_NULL();
1100+
}
1101+
1102+
local_port[0] = '\0';
1103+
1104+
ret = getnameinfo_all(&port->laddr.addr, port->laddr.salen,
1105+
NULL, 0,
1106+
local_port, sizeof(local_port),
1107+
NI_NUMERICHOST | NI_NUMERICSERV);
1108+
if (ret)
1109+
PG_RETURN_NULL();
1110+
1111+
PG_RETURN_DATUM(DirectFunctionCall1(int4in, CStringGetDatum(local_port)));
1112+
}

src/include/catalog/pg_proc.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.335 2004/06/06 19:07:00 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.336 2004/06/13 19:56:51 tgl Exp $
1111
*
1212
* NOTES
1313
* The script catalog/genbki.sh reads this file and generates .bki
@@ -2346,15 +2346,6 @@ DESCR("I/O");
23462346
DATA(insert OID = 911 ( inet_out PGNSP PGUID 12 f f t f i 1 2275 "869" _null_ inet_out - _null_ ));
23472347
DESCR("I/O");
23482348

2349-
DATA(insert OID = 2196 ( inet_client_addr PGNSP PGUID 12 f f f f s 0 869 "" _null_ inet_client_addr - _null_ ));
2350-
DESCR("Returns the INET address of the client connected to the backend");
2351-
DATA(insert OID = 2197 ( inet_client_port PGNSP PGUID 12 f f f f s 0 23 "" _null_ inet_client_port - _null_ ));
2352-
DESCR("Returns the client's port number for this connection");
2353-
DATA(insert OID = 2198 ( inet_server_addr PGNSP PGUID 12 f f f f s 0 869 "" _null_ inet_server_addr - _null_ ));
2354-
DESCR("Returns the INET address that the backend is using to service the connection");
2355-
DATA(insert OID = 2199 ( inet_server_port PGNSP PGUID 12 f f f f s 0 23 "" _null_ inet_server_port - _null_ ));
2356-
DESCR("Returns the servers's port number for this connection");
2357-
23582349
/* for cidr type support */
23592350
DATA(insert OID = 1267 ( cidr_in PGNSP PGUID 12 f f t f i 1 650 "2275" _null_ cidr_in - _null_ ));
23602351
DESCR("I/O");
@@ -2411,6 +2402,15 @@ DESCR("text to cidr");
24112402
DATA(insert OID = 1715 ( set_masklen PGNSP PGUID 12 f f t f i 2 869 "869 23" _null_ inet_set_masklen - _null_ ));
24122403
DESCR("change the netmask of an inet");
24132404

2405+
DATA(insert OID = 2196 ( inet_client_addr PGNSP PGUID 12 f f f f s 0 869 "" _null_ inet_client_addr - _null_ ));
2406+
DESCR("INET address of the client");
2407+
DATA(insert OID = 2197 ( inet_client_port PGNSP PGUID 12 f f f f s 0 23 "" _null_ inet_client_port - _null_ ));
2408+
DESCR("client's port number for this connection");
2409+
DATA(insert OID = 2198 ( inet_server_addr PGNSP PGUID 12 f f f f s 0 869 "" _null_ inet_server_addr - _null_ ));
2410+
DESCR("INET address of the server");
2411+
DATA(insert OID = 2199 ( inet_server_port PGNSP PGUID 12 f f f f s 0 23 "" _null_ inet_server_port - _null_ ));
2412+
DESCR("server's port number for this connection");
2413+
24142414
DATA(insert OID = 1686 ( numeric PGNSP PGUID 12 f f t f i 1 1700 "25" _null_ text_numeric - _null_ ));
24152415
DESCR("(internal)");
24162416
DATA(insert OID = 1688 ( text PGNSP PGUID 12 f f t f i 1 25 "1700" _null_ numeric_text - _null_ ));

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy