--- "Joshua D. Drake" <[EMAIL PROTECTED]> 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

---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Reply via email to