Initial commit.
This commit is contained in:
commit
1cec318025
10
.editorconfig
Normal file
10
.editorconfig
Normal file
@ -0,0 +1,10 @@
|
||||
root = true
|
||||
|
||||
[*]
|
||||
indent_size = 2
|
||||
indent_style = space
|
||||
end_of_line = lf
|
||||
insert_final_newline = true
|
||||
|
||||
[Makefile]
|
||||
indent_style = tab
|
10
Dockerfile
Normal file
10
Dockerfile
Normal file
@ -0,0 +1,10 @@
|
||||
FROM postgres:14
|
||||
|
||||
RUN apt-get update
|
||||
RUN apt-get install -y build-essential
|
||||
RUN apt-get install -y postgresql-server-dev-14
|
||||
COPY rdns.c Makefile rdns.control rdns--1.0.sql /usr/src/rdns/
|
||||
RUN cd /usr/src/rdns/; \
|
||||
make; \
|
||||
make install; \
|
||||
echo "shared_preload_libraries = 'rdns'" >> $PGDATA/postgresql.conf;
|
9
Makefile
Normal file
9
Makefile
Normal file
@ -0,0 +1,9 @@
|
||||
OBJS = rdns.o
|
||||
|
||||
EXTENSION = rdns
|
||||
DATA = rdns--1.0.sql
|
||||
MODULES = rdns
|
||||
|
||||
PG_CONFIG = pg_config
|
||||
PGXS := $(shell $(PG_CONFIG) --pgxs)
|
||||
include $(PGXS)
|
4
docker-compose.yml
Normal file
4
docker-compose.yml
Normal file
@ -0,0 +1,4 @@
|
||||
services:
|
||||
postgres:
|
||||
build: .
|
||||
image: registry.xnopyt.com/postgres-rdns:14
|
9
rdns--1.0.sql
Normal file
9
rdns--1.0.sql
Normal file
@ -0,0 +1,9 @@
|
||||
CREATE FUNCTION rdns_lookup(text)
|
||||
RETURNS text
|
||||
AS '$libdir/rdns'
|
||||
LANGUAGE C STRICT;
|
||||
|
||||
CREATE FUNCTION circular_rdns_lookup(text)
|
||||
RETURNS text
|
||||
AS '$libdir/rdns'
|
||||
LANGUAGE C STRICT;
|
89
rdns.c
Normal file
89
rdns.c
Normal file
@ -0,0 +1,89 @@
|
||||
#include "postgres.h"
|
||||
#include "fmgr.h"
|
||||
#include "utils/builtins.h"
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
PG_MODULE_MAGIC;
|
||||
|
||||
|
||||
PG_FUNCTION_INFO_V1(rdns_lookup);
|
||||
Datum rdns_lookup(PG_FUNCTION_ARGS) {
|
||||
struct in_addr iaddr;
|
||||
char host[512];
|
||||
struct sockaddr_in sa;
|
||||
int r;
|
||||
char* ip = text_to_cstring(PG_GETARG_TEXT_PP(0));
|
||||
|
||||
memset(&iaddr, 0, sizeof(struct in_addr));
|
||||
memset(&sa, 0, sizeof(struct sockaddr_in));
|
||||
memset(host, 0, 512);
|
||||
|
||||
inet_aton(ip, &iaddr);
|
||||
|
||||
sa.sin_family = AF_INET;
|
||||
sa.sin_addr = iaddr;
|
||||
|
||||
r = getnameinfo((struct sockaddr*)(&sa), sizeof(struct sockaddr_in), host, 512, 0, 0, NI_NAMEREQD);
|
||||
if (r == EAI_NONAME)
|
||||
PG_RETURN_NULL();
|
||||
if (r) {
|
||||
ereport(ERROR, (errcode(ERRCODE_SYSTEM_ERROR), errmsg("getnameinfo failed! %s", gai_strerror(r))));
|
||||
}
|
||||
|
||||
PG_RETURN_TEXT_P(cstring_to_text(host));
|
||||
}
|
||||
|
||||
PG_FUNCTION_INFO_V1(circular_rdns_lookup);
|
||||
Datum circular_rdns_lookup(PG_FUNCTION_ARGS) {
|
||||
struct in_addr iaddr;
|
||||
char host[512];
|
||||
struct sockaddr_in sa;
|
||||
int r;
|
||||
struct addrinfo hint;
|
||||
struct addrinfo* results;
|
||||
char* ip = text_to_cstring(PG_GETARG_TEXT_PP(0));
|
||||
struct addrinfo* c;
|
||||
|
||||
memset(&iaddr, 0, sizeof(struct in_addr));
|
||||
memset(&sa, 0, sizeof(struct sockaddr_in));
|
||||
memset(host, 0, 512);
|
||||
memset(&hint, 0, sizeof(struct addrinfo));
|
||||
|
||||
inet_aton(ip, &iaddr);
|
||||
|
||||
sa.sin_family = AF_INET;
|
||||
sa.sin_addr = iaddr;
|
||||
|
||||
r = getnameinfo((struct sockaddr*)(&sa), sizeof(struct sockaddr_in), host, 512, 0, 0, NI_NAMEREQD);
|
||||
if (r == EAI_NONAME)
|
||||
PG_RETURN_NULL();
|
||||
if (r) {
|
||||
ereport(ERROR, (errcode(ERRCODE_SYSTEM_ERROR), errmsg("getnameinfo failed! %s", gai_strerror(r))));
|
||||
}
|
||||
|
||||
hint.ai_family = AF_INET;
|
||||
|
||||
r = getaddrinfo(host, 0, &hint, &results);
|
||||
if (r) {
|
||||
ereport(ERROR, (errcode(ERRCODE_SYSTEM_ERROR), errmsg("getaddrinfo failed! %s", gai_strerror(r))));
|
||||
}
|
||||
|
||||
c = results;
|
||||
while (c) {
|
||||
struct addrinfo* current = c;
|
||||
c = current->ai_next;
|
||||
if (current->ai_family != AF_INET)
|
||||
continue;
|
||||
|
||||
if (((struct sockaddr_in*)current->ai_addr)->sin_addr.s_addr == iaddr.s_addr) {
|
||||
freeaddrinfo(results);
|
||||
PG_RETURN_TEXT_P(cstring_to_text(host));
|
||||
}
|
||||
}
|
||||
|
||||
freeaddrinfo(results);
|
||||
PG_RETURN_NULL();
|
||||
}
|
3
rdns.control
Normal file
3
rdns.control
Normal file
@ -0,0 +1,3 @@
|
||||
comment = 'Reverse DNS Extension for PostgreSQL'
|
||||
default_version = '1.0'
|
||||
relocatable = true
|
Loading…
x
Reference in New Issue
Block a user