Re: passing function's output into C function
От | Kamil Kukura |
---|---|
Тема | Re: passing function's output into C function |
Дата | |
Msg-id | 3FC9F754.9020208@volny.cz обсуждение исходный текст |
Ответ на | Re: passing function's output into C function (Joe Conway <mail@joeconway.com>) |
Список | pgsql-general |
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)); }
В списке pgsql-general по дате отправления: