Help creating a function

From: Madison Kelly <linux(at)alteeve(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Help creating a function
Date: 2007-08-16 16:35:41
Message-ID: 46C47CDD.5050802@alteeve.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Note: This is being sent again (in case it shows up later). It never
seemed to have made it to the list.

Hi all,

I'm using ulogd with PostgreSQL which stores IP addresses as 32bit
unsigned integers. So when I select some data I get something like:

ulogd=> SELECT id, ip_saddr, ip_daddr, raw_pktlen, ip_totlen, tcp_window
FROM ulog LIMIT 20;
id | ip_saddr | ip_daddr | raw_pktlen | ip_totlen | tcp_window
----+------------+------------+------------+-----------+------------
1 | 3232235874 | 1074534522 | 46 | 46 | 25825

Where 'ip_saddr' and 'ip_daddr' are 'bigint'. I know I can convert
these numbers to dotted-decimal in perl with a small script like:

-=-=-
#!/usr/bin/perl

# This would be the number read from the DB
my $num=3232235874;

# Now do the math
my $temp=$num/256;
my $D=256*($temp-int($temp));
$temp=(int($temp))/256;
my $C=256*($temp-int($temp));
$temp=(int($temp))/256;
my $B=256*($temp-int($temp));
my $A=int($temp);
my $ip="$A.$B.$C.$D";

# Print the results
print "'num': [$num] -> 'IP': [$ip]\n";
-=-=-

What I would like to do is create a function that would do the same
thing so I could read out the IP addresses as standard dotted-decimal
format. Could anyone help me with this? I am quite the n00b when it
comes to functions. :)

Thanks all!

Madi

Responses

Browse pgsql-general by date

  From Date Subject
Next Message novnov 2007-08-16 16:51:00 Re: Running a stored procedure via pgagent, need an examp le
Previous Message Rainer Bauer 2007-08-16 16:28:58 Re: Yet Another COUNT(*)...WHERE...question