X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 9053CD1B379 for ; Fri, 16 Jul 2004 14:38:34 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 49542-06 for ; Fri, 16 Jul 2004 17:38:26 +0000 (GMT) Received: from CISX-Linux4.cisx.com (unknown [213.133.202.58]) by svr1.postgresql.org (Postfix) with ESMTP id 13805D1B37B for ; Fri, 16 Jul 2004 14:38:23 -0300 (ADT) Received: from DEMOLITION ([192.168.100.22]) by CISX-Linux4.cisx.com (8.12.8/8.12.8) with SMTP id i6GHRWp7025167 for ; Fri, 16 Jul 2004 18:27:32 +0100 Message-ID: <000b01c46b5b$4f0c5220$1664a8c0@DEMOLITION> From: "Donald Fraser" To: "[ADMIN]" Subject: Function to get client IP address Date: Fri, 16 Jul 2004 18:35:37 +0100 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_0008_01C46B63.AFFC7A30" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2800.1106 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.8 tagged_above=0.0 required=5.0 tests=HTML_30_40, HTML_MESSAGE X-Spam-Level: X-Archive-Number: 200407/188 X-Sequence-Number: 14256 This is a multi-part message in MIME format. ------=_NextPart_000_0008_01C46B63.AFFC7A30 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hi, I've been looking through the mail archives in the hope of finding a functi= on to get the client IP address according to the backend. Found lots of requests but no actual answers or examples.=20 I've trawled through the backend code and as a result created a function to= do the job. I thought I would post it here (may be it should go into hackers) so that o= thers can find and use it as they wish. regards Donald Fraser. Place the following into an appropriate file and follow the c compiling rul= es as with contrib examples. #include "postgres.h" #include "miscadmin.h" #include "libpq/libpq.h" extern Datum get_ip_address(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(get_ip_address); extern Datum get_ip_address(PG_FUNCTION_ARGS) { char remote_addr_s[NI_MAXHOST]; char remote_port[NI_MAXSERV]; text *ptext; getnameinfo_all(&(MyProcPort->raddr.addr), MyProcPort->raddr.salen, remote_addr_s, sizeof(remote_addr_s), remote_port, sizeof(remote_port), NI_NUMERICHOST | NI_NUMERICSERV); =20=20=20 ptext =3D DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(&remot= e_addr_s[0]))); if (ptext =3D=3D NULL) PG_RETURN_NULL(); PG_RETURN_TEXT_P(ptext); } ------=_NextPart_000_0008_01C46B63.AFFC7A30 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
Hi,
I've been looking through the mail archive= s in the=20 hope of finding a function to get the client IP address according to the=20 backend.
Found lots of requests but no actual answe= rs or=20 examples.
I've trawled through the backend code and = as a=20 result created a function to do the job.
I thought I would post it here (may be it = should go=20 into hackers) so that others can find and use it as they wish.
 
regards
Donald Fraser.
 
 
Place the following into an appropriate fi= le and=20 follow the c compiling rules as with contrib examples.
 
#include "postgres.h"
#include=20 "miscadmin.h"
#include=20 "libpq/libpq.h"
extern Datum=20 get_ip_address(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(get_ip_address);
 
extern=20 Datum
get_ip_address(PG_FUNCTION_ARGS)
{
 char  rem= ote_addr_s[NI_MAXHOST];
 char  remote_port[NI_MAXSERV]; text      =20 *ptext;
 
 getnameinfo_all(&(MyProcPort->raddr.addr),=20 MyProcPort->raddr.salen,
     remote_addr_s,= =20 sizeof(remote_addr_s),
     remote_port,=20 sizeof(remote_port),
     NI_NUMERICHOST |=20 NI_NUMERICSERV);
  
 ptext =3D=20 DatumGetTextP(DirectFunctionCall1(textin,=20 CStringGetDatum(&remote_addr_s[0])));
    if (ptext = =3D=3D=20 NULL)
     PG_RETURN_NULL();
  =20  PG_RETURN_TEXT_P(ptext);
}
------=_NextPart_000_0008_01C46B63.AFFC7A30--