By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,834 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,834 IT Pros & Developers. It's quick & easy.

passing function's output into C function

P: n/a
I have table with column of type "inet". Then I have my own function
resolveip(text). It works okay when I store IP addres into column typed
as text or varchar. But with type of inet I run into this problem:
select host(acc_ip), resolveip(host(acc_ip)) from access_test limit 15

host | resolveip
----------------+-----------------------------------
212.80.81.141 | Bad IP address: '212.80.81.141.'
212.80.81.141 | Bad IP address: '212.80.81.141'
212.80.81.141 | Bad IP address: '212.80.81.141'
212.80.81.141 | Bad IP address: '212.80.81.141'
80.139.171.208 | Bad IP address: '80.139.171.208*'
212.80.81.141 | Bad IP address: '212.80.81.1418'
212.80.81.141 | Bad IP address: '212.80.81.1418'
211.22.169.82 | Bad IP address: '211.22.169.828'
81.0.234.52 |
62.24.89.106 | Bad IP address: '62.24.89.106`'
81.0.234.52 | Bad IP address: '81.0.234.526'
212.80.81.141 | Bad IP address: '212.80.81.1415'
62.84.131.161 | Bad IP address: '62.84.131.1615'
212.80.81.141 | Bad IP address: '212.80.81.1415'
195.122.204.15 | e0-a11.b1.lan.prg.vol.cz

Is there something I'm missing when data are passed from function to
function?

--
Kamil Kukura

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 12 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Kamil Kukura wrote:
Is there something I'm missing when data are passed from function to
function?


It is difficult to know what you might be missing when you haven't shown
us the function. Post the code.

Joe
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 12 '05 #2

P: n/a
Kamil Kukura <ka**@volny.cz> writes:
Is there something I'm missing when data are passed from function to
function?


I get the impression your function is expecting the contents of a text
datum to be null-terminated. It's not.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 12 '05 #3

P: n/a
It was problem with allocating needed space, now it's okay. Anyway, the
code of function is being attached..

--
Kamil Kukura

#include <stdio.h>
#include <netdb.h>
#include <sys/socket.h>

#include "postgres.h"
#include "fmgr.h"
#include "utils/palloc.h"
text *create_text(char *data) {
unsigned long r_size;
text *result;

r_size = strlen(data);
result = palloc(r_size + VARHDRSZ);
VARATT_SIZEP(result) = r_size + VARHDRSZ;
memcpy(VARDATA(result), data, r_size);
return(result);
}
PG_FUNCTION_INFO_V1(resolveip);

Datum resolveip(PG_FUNCTION_ARGS) {
struct hostent *host;
struct in_addr addr;
text *ipaddress = (text *)PG_GETARG_TEXT_P(0);
const char *bad_ip_txt = "Bad IP address: '";
char *message;

unsigned long r_size, a_size = VARSIZE(ipaddress) - VARHDRSZ;
char *saddress = (char *)palloc(a_size + 1);

memcpy(saddress, VARDATA(ipaddress), a_size);
saddress[a_size] = 0; /* terminate string */

if (! inet_aton(saddress, &addr)) {
message = palloc(strlen(bad_ip_txt) + a_size + 2);
message[0] = 0;
strcat(message, bad_ip_txt);
strcat(message, saddress);
strcat(message, "'");

PG_RETURN_TEXT_P(create_text(message));
}
host = gethostbyaddr(&addr, sizeof addr, AF_INET);

if (! host) {
PG_RETURN_NULL();
}

PG_RETURN_TEXT_P(create_text(host->h_name));
}
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.