From: | Dennis Jenkins <dennis(dot)jenkins(at)sbcglobal(dot)net> |
---|---|
To: | "Joshua D(dot) Drake" <jd(at)commandprompt(dot)com>, Bruno Wolff III <bruno(at)wolff(dot)to> |
Cc: | Marcel Gsteiger <Marcel(dot)Gsteiger(at)milprog(dot)ch>, pgsql-general(at)postgresql(dot)org |
Subject: | Re: function that resolves IP addresses |
Date: | 2005-10-31 16:59:42 |
Message-ID: | 20051031165942.40835.qmail@web81306.mail.yahoo.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
--- "Joshua D. Drake" <jd(at)commandprompt(dot)com> wrote:
It is not fully debugged, but this is what I wrote a
few months ago for sh*ts and grins.
/* djenkins, 2005-7-22
Implements poor-man's reverse DNS lookup tool for use
in
Postgresql SQL functions.
CREATE FUNCTION reverse_dns_lookup(text) RETURNS text
AS 'dns_tools.so', 'reverse_dns_lookup'
LANGUAGE C STRICT;
CREATE FUNCTION forward_dns_lookup(text) RETURNS text
AS 'dns_tools.so', 'forward_dns_lookup'
LANGUAGE C STRICT;
*/
#include "postgres.h"
#include <string.h>
#include "fmgr.h"
#include <stdlib.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <syslog.h>
PG_FUNCTION_INFO_V1(forward_dns_lookup);
Datum forward_dns_lookup(PG_FUNCTION_ARGS)
{
text *t = PG_GETARG_TEXT_P(0);
struct hostent *he = NULL;
int ret_len = 0;
text *ret_text = NULL;
char *in_str = VARDATA(t);
int in_len = VARSIZE(t) - VARHDRSZ;
char temp[256];
if (!in_str || (in_len > sizeof(temp)-1))
{
PG_RETURN_NULL();
}
strncpy(temp, in_str, in_len);
temp[in_len] = 0;
he = gethostbyname(temp);
if (!he)
{
PG_RETURN_NULL();
}
strncpy(temp, inet_ntoa(*((struct in_addr
*)he->h_addr)), sizeof(temp));
ret_len = strlen(temp);
// syslog(LOG_DEBUG, "'%s'[%d] = '%s'[%d]\n", in_str,
strlen(in_str), temp, ret_len);
ret_text = (text*)palloc(ret_len + VARHDRSZ);
VARATT_SIZEP(ret_text) = ret_len + VARHDRSZ;
memcpy(VARDATA(ret_text), temp, ret_len);
PG_RETURN_TEXT_P(ret_text);
}
PG_FUNCTION_INFO_V1(reverse_dns_lookup);
Datum reverse_dns_lookup(PG_FUNCTION_ARGS)
{
text *t = PG_GETARG_TEXT_P(0);
struct in_addr in;
struct hostent *he = NULL;
unsigned long *l = (unsigned long*)((void*)&in);
int ret_len = 0;
text *ret_text = NULL;
char *in_str = VARDATA(t);
int in_len = VARSIZE(t) - VARHDRSZ;
char temp[16];
if (!in_str || (in_len > sizeof(temp)-1))
{
PG_RETURN_NULL();
}
memcpy(temp, in_str, in_len);
temp[in_len] = 0;
// First, convert the string to IPV4 'long'
memset(&in, 0, sizeof(in));
if (!inet_aton(temp, &in))
{
// syslog(LOG_DEBUG, "inet_aton('%s'[%d]) failed: %d
{%08lx}", in_str, strlen(in_str), errno, *l);
PG_RETURN_NULL();
}
he = gethostbyaddr((char*)l, 4, AF_INET);
if (!he)
{
// syslog(LOG_DEBUG, "gethostbyaddr('%s') failed:
%d", in_str, errno);
PG_RETURN_NULL();
}
// return string is in 'he->h_name'
ret_len = strlen(he->h_name);
ret_text = palloc(ret_len + VARHDRSZ);
VARATT_SIZEP(ret_text) = ret_len + VARHDRSZ;
memcpy(VARDATA(ret_text), he->h_name, ret_len);
PG_RETURN_TEXT_P(ret_text);
}
Dennis Jenkins
From | Date | Subject | |
---|---|---|---|
Next Message | Wes Williams | 2005-10-31 17:02:07 | Re: Oracle 10g Express - any danger for Postgres? |
Previous Message | Simon Riggs | 2005-10-31 16:58:54 | Re: Starting PostgreSQL 8.0.4 with more memory [FreeBSD |