Skip site navigation (1) Skip section navigation (2)

Function to get client IP address

From: "Donald Fraser" <demolish(at)cwgsy(dot)net>
To: "[ADMIN]" <pgsql-admin(at)postgresql(dot)org>
Subject: Function to get client IP address
Date: 2004-07-16 17:35:37
Message-ID: 000b01c46b5b$4f0c5220$1664a8c0@DEMOLITION (view raw, whole thread or download thread mbox)
Lists: pgsql-admin
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.

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);

extern Datum
 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),
 ptext = DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(&remote_addr_s[0])));
    if (ptext == NULL)

pgsql-admin by date

Next:From: mike gDate: 2004-07-17 04:26:37
Subject: Re: Disk usage
Previous:From: Cardoso PatrickDate: 2004-07-16 14:42:34
Subject: users connected

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group