|
1 | 1 | /*
|
2 | 2 | * PostgreSQL type definitions for the INET and CIDR types.
|
3 | 3 | *
|
4 |
| - * $PostgreSQL: pgsql/src/backend/utils/adt/network.c,v 1.49 2003/12/01 18:50:19 tgl Exp $ |
| 4 | + * $PostgreSQL: pgsql/src/backend/utils/adt/network.c,v 1.50 2004/05/26 18:35:38 momjian Exp $ |
5 | 5 | *
|
6 | 6 | * Jon Postel RIP 16 Oct 1998
|
7 | 7 | */
|
|
14 | 14 | #include <arpa/inet.h>
|
15 | 15 |
|
16 | 16 | #include "catalog/pg_type.h"
|
| 17 | +#include "libpq/ip.h" |
| 18 | +#include "libpq/libpq-be.h" |
17 | 19 | #include "libpq/pqformat.h"
|
| 20 | +#include "miscadmin.h" |
18 | 21 | #include "utils/builtins.h"
|
19 | 22 | #include "utils/inet.h"
|
20 | 23 |
|
@@ -130,6 +133,110 @@ cidr_in(PG_FUNCTION_ARGS)
|
130 | 133 | PG_RETURN_INET_P(network_in(src, 1));
|
131 | 134 | }
|
132 | 135 |
|
| 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 | + |
| 239 | + |
133 | 240 | /*
|
134 | 241 | * INET address output function.
|
135 | 242 | */
|
|
0 commit comments