pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Donald Fraser <demolish@cwgsy.net>
To: [ADMIN] <pgsql-admin@postgresql.org>
Subject: Function to get client IP address
Date: Fri, 16 Jul 2004 18:35:37 +0100
Message-ID: <000b01c46b5b$4f0c5220$1664a8c0@DEMOLITION> (raw)

Hi,
I've been looking through the mail archives in the hope of finding a function to get the client IP address according to the backend.
Found lots of requests but no actual answers or examples. 
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 others can find and use it as they wish.

regards
Donald Fraser.


Place the following into an appropriate file and follow the c compiling rules 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);
   
 ptext = DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(&remote_addr_s[0])));
    if (ptext == NULL)
     PG_RETURN_NULL();
    PG_RETURN_TEXT_P(ptext);
}

Message-ID: <000b01c46b5b$4f0c5220$1664a8c0@DEMOLITION>
Permalink:  ../000b01c46b5b$4f0c5220$1664a8c0@DEMOLITION/
Also on:    postgresql.org/message-id/000b01c46b5b$4f0c5220$1664a8c0@DEMOLITION

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-admin@postgresql.org
  Cc: demolish@cwgsy.net
  Subject: Re: Function to get client IP address
  In-Reply-To: <000b01c46b5b$4f0c5220$1664a8c0@DEMOLITION>
  References: <000b01c46b5b$4f0c5220$1664a8c0@DEMOLITION>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox