#include "utl_tcp.hpp"
|
#include "dbms_network_acl_admin/dbms_network_acl_admin.hpp"
|
|
extern "C" {
|
|
#include "access/htup_details.h"
|
#include "catalog/pg_type_d.h"
|
#include "funcapi.h"
|
#include "miscadmin.h"
|
#include "utils/builtins.h"
|
#include "utils/hsearch.h"
|
#include "utils/memutils.h"
|
#include "utils/lsyscache.h"
|
#include "lib/stringinfo.h"
|
#include "storage/ipc.h"
|
}
|
|
#include <sys/types.h>
|
#include <sys/socket.h>
|
#include <sys/ioctl.h>
|
#include <netdb.h>
|
#include <unistd.h>
|
#include <fcntl.h>
|
#include <poll.h>
|
#include <errno.h>
|
#include <string.h>
|
#include <string>
|
#include <vector>
|
#include <algorithm>
|
|
#ifdef USE_OPENSSL
|
#include <openssl/ssl.h>
|
#include <openssl/err.h>
|
#endif
|
|
/// @brief Версия RUDB пакета UTL_TCP (Oracle UTL_TCP пакет)
|
#define RUDB_UTL_TCP_VERSION "1.0.0"
|
|
static inline bytea* raw_from_buf(const void* data, size_t len)
|
{
|
bytea* b = (bytea*) palloc(VARHDRSZ + len);
|
SET_VARSIZE(b, VARHDRSZ + len);
|
if (len && data) memcpy(VARDATA(b), data, len);
|
return b;
|
}
|
|
static inline text* text_from_buf(const char* data, size_t len)
|
{
|
text* t = (text*) palloc(VARHDRSZ + len);
|
SET_VARSIZE(t, VARHDRSZ + len);
|
if (len) memcpy(VARDATA(t), data, len);
|
return t;
|
}
|
|
static const void* memmem_fallback(const void* hay, size_t hlen, const void* ndl, size_t nlen)
|
{
|
if (nlen == 0 || !hay || !ndl) return nullptr;
|
if (nlen > hlen) return nullptr;
|
const unsigned char* h = (const unsigned char*)hay;
|
const unsigned char* n = (const unsigned char*)ndl;
|
for (size_t i = 0; i + nlen <= hlen; ++i)
|
if (h[i] == n[0] && memcmp(h + i, n, nlen) == 0)
|
return h + i;
|
return nullptr;
|
}
|
#ifndef _GNU_SOURCE
|
#define memmem memmem_fallback
|
#endif
|
|
// ---------- connection ----------
|
|
struct InBuf {
|
char* data{nullptr};
|
size_t cap{0};
|
size_t len{0};
|
size_t pos{0};
|
|
void init(size_t capacity)
|
{
|
cap = std::max<size_t>(capacity, 4096);
|
data = (char*) MemoryContextAlloc(TopMemoryContext, cap);
|
len = pos = 0;
|
}
|
|
void reset() { len = pos = 0; }
|
size_t avail() const { return len - pos; }
|
|
void compact()
|
{
|
if (pos == 0) return;
|
if (pos < len) memmove(data, data + pos, len - pos);
|
len -= pos;
|
pos = 0;
|
}
|
|
void ensure_tail(size_t need)
|
{
|
if (cap - len >= need) return;
|
size_t newcap = cap;
|
while (newcap - len < need) newcap *= 2;
|
char* nd = (char*) MemoryContextAlloc(TopMemoryContext, newcap);
|
memcpy(nd, data, len);
|
pfree(data);
|
data = nd;
|
cap = newcap;
|
}
|
};
|
|
struct TcpConn {
|
int fd{-1};
|
bool use_tls{false};
|
#ifdef USE_OPENSSL
|
SSL_CTX* ssl_ctx{nullptr};
|
SSL* ssl{nullptr};
|
#endif
|
int tx_timeout{-1}; // seconds; -1 = forever, 0 = no-wait
|
std::string newline{"\r\n"};
|
std::string charset;
|
InBuf inbuf;
|
size_t inbuf_cfg{8192};
|
size_t outbuf_cfg{8192};
|
|
std::string remote_host;
|
int remote_port{0};
|
};
|
|
struct ConnEntry {
|
int key;
|
TcpConn* conn;
|
};
|
|
static HTAB* g_conn_tab = nullptr;
|
static int g_next_handle = 1;
|
|
|
static void ensure_conn_tab()
|
{
|
if (g_conn_tab) return;
|
|
HASHCTL ctl{};
|
ctl.keysize = sizeof(int);
|
ctl.entrysize = sizeof(ConnEntry);
|
ctl.hcxt = TopMemoryContext;
|
g_conn_tab = hash_create("UTL_TCP connections", 64, &ctl, HASH_ELEM | HASH_CONTEXT | HASH_BLOBS);
|
|
on_proc_exit([](int, Datum) {
|
if (!g_conn_tab) return;
|
HASH_SEQ_STATUS scan;
|
hash_seq_init(&scan, g_conn_tab);
|
ConnEntry* e;
|
while ((e = (ConnEntry*) hash_seq_search(&scan)) != nullptr) {
|
if (e->conn) {
|
#ifdef USE_OPENSSL
|
if (e->conn->ssl) { SSL_shutdown(e->conn->ssl); SSL_free(e->conn->ssl); }
|
if (e->conn->ssl_ctx) SSL_CTX_free(e->conn->ssl_ctx);
|
#endif
|
if (e->conn->fd >= 0) close(e->conn->fd);
|
if (e->conn->inbuf.data) pfree(e->conn->inbuf.data);
|
delete e->conn;
|
}
|
}
|
hash_destroy(g_conn_tab);
|
g_conn_tab = nullptr;
|
}, 0);
|
}
|
|
static TcpConn* conn_lookup(int handle, bool must_exist = true)
|
{
|
ensure_conn_tab();
|
ConnEntry* e = (ConnEntry*) hash_search(g_conn_tab, &handle, HASH_FIND, nullptr);
|
if (!e || !e->conn) {
|
if (must_exist)
|
ereport(ERROR, (errcode(ERRCODE_CONNECTION_DOES_NOT_EXIST),
|
errmsg("UTL_TCP: connection handle %d does not exist", handle)));
|
return nullptr;
|
}
|
return e->conn;
|
}
|
|
static int conn_register(TcpConn* c)
|
{
|
ensure_conn_tab();
|
int handle = g_next_handle++;
|
bool found = false;
|
ConnEntry* e = (ConnEntry*) hash_search(g_conn_tab, &handle, HASH_ENTER, &found);
|
if (!e || found) ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR), errmsg("UTL_TCP: handle allocation failed")));
|
e->key = handle;
|
e->conn = c;
|
return handle;
|
}
|
|
static void conn_remove(int handle)
|
{
|
ensure_conn_tab();
|
bool found = false;
|
ConnEntry* e = (ConnEntry*) hash_search(g_conn_tab, &handle, HASH_REMOVE, &found);
|
if (found && e && e->conn) {
|
#ifdef USE_OPENSSL
|
if (e->conn->ssl) { SSL_shutdown(e->conn->ssl); SSL_free(e->conn->ssl); }
|
if (e->conn->ssl_ctx) SSL_CTX_free(e->conn->ssl_ctx);
|
#endif
|
if (e->conn->fd >= 0) close(e->conn->fd);
|
if (e->conn->inbuf.data) pfree(e->conn->inbuf.data);
|
delete e->conn;
|
}
|
}
|
|
// ---------- sockets / timeouts / TLS ----------
|
|
static void set_blocking(int fd, bool blocking)
|
{
|
int flags = fcntl(fd, F_GETFL, 0);
|
if (flags < 0) return;
|
if (blocking) flags &= ~O_NONBLOCK; else flags |= O_NONBLOCK;
|
(void) fcntl(fd, F_SETFL, flags);
|
}
|
|
static int wait_io(int fd, short events, int timeout_sec)
|
{
|
struct pollfd pfd{};
|
pfd.fd = fd;
|
pfd.events = events;
|
int tout = (timeout_sec < 0 ? -1 : timeout_sec * 1000);
|
int rc;
|
do { rc = poll(&pfd, 1, tout); } while (rc < 0 && errno == EINTR);
|
return rc; // -1 err, 0 timeout, >0 ready
|
}
|
|
static ssize_t tcp_read(TcpConn* c, void* buf, size_t len, int timeout_sec)
|
{
|
#ifdef USE_OPENSSL
|
if (c->use_tls && c->ssl) {
|
if (timeout_sec > 0) {
|
int w = wait_io(c->fd, POLLIN, timeout_sec);
|
if (w <= 0) return w;
|
}
|
int n = SSL_read(c->ssl, buf, (int)len);
|
if (n <= 0) {
|
int e = SSL_get_error(c->ssl, n);
|
if (e == SSL_ERROR_WANT_READ || e == SSL_ERROR_WANT_WRITE) return 0;
|
return -1;
|
}
|
return n;
|
}
|
#endif
|
if (timeout_sec > 0) {
|
int w = wait_io(c->fd, POLLIN, timeout_sec);
|
if (w <= 0) return w;
|
}
|
ssize_t n = recv(c->fd, buf, len, 0);
|
if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) return 0;
|
return n;
|
}
|
|
static ssize_t tcp_write(TcpConn* c, const void* buf, size_t len, int timeout_sec)
|
{
|
#ifdef USE_OPENSSL
|
if (c->use_tls && c->ssl) {
|
if (timeout_sec > 0) {
|
int w = wait_io(c->fd, POLLOUT, timeout_sec);
|
if (w <= 0) return w;
|
}
|
int n = SSL_write(c->ssl, buf, (int)len);
|
if (n <= 0) return -1;
|
return n;
|
}
|
#endif
|
if (timeout_sec > 0) {
|
int w = wait_io(c->fd, POLLOUT, timeout_sec);
|
if (w <= 0) return w;
|
}
|
ssize_t n = send(c->fd, buf, len, 0);
|
return n;
|
}
|
|
static void resolve_and_connect(TcpConn* c, const char* host, int port)
|
{
|
char portstr[32];
|
snprintf(portstr, sizeof(portstr), "%d", port);
|
|
struct addrinfo hints{}, *res = nullptr, *ai = nullptr;
|
hints.ai_family = AF_UNSPEC;
|
hints.ai_socktype = SOCK_STREAM;
|
|
int rc = getaddrinfo((host && *host) ? host : "127.0.0.1", portstr, &hints, &res);
|
if (rc != 0)
|
ereport(ERROR, (errcode(ERRCODE_CONNECTION_FAILURE),
|
errmsg("UTL_TCP: getaddrinfo failed: %s", gai_strerror(rc))));
|
|
int fd = -1;
|
for (ai = res; ai != nullptr; ai = ai->ai_next) {
|
fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
|
if (fd < 0) continue;
|
set_blocking(fd, true);
|
if (connect(fd, ai->ai_addr, ai->ai_addrlen) == 0) break;
|
close(fd);
|
fd = -1;
|
}
|
freeaddrinfo(res);
|
|
if (fd < 0)
|
ereport(ERROR, (errcode(ERRCODE_CONNECTION_FAILURE),
|
errmsg("UTL_TCP: connect to %s:%d failed", host ? host : "127.0.0.1", port)));
|
|
c->fd = fd;
|
}
|
|
#ifdef USE_OPENSSL
|
static void init_tls_if_requested(TcpConn* c, const char* wallet_path, const char* /*wallet_pwd*/)
|
{
|
if (!wallet_path) return;
|
|
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
|
OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
|
#else
|
SSL_library_init();
|
SSL_load_error_strings();
|
#endif
|
|
const SSL_METHOD* method = TLS_client_method();
|
c->ssl_ctx = SSL_CTX_new(method);
|
if (!c->ssl_ctx)
|
ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR), errmsg("UTL_TCP: SSL_CTX_new failed")));
|
|
SSL_CTX_set_default_verify_paths(c->ssl_ctx);
|
(void) SSL_CTX_load_verify_locations(c->ssl_ctx, nullptr, wallet_path);
|
|
c->ssl = SSL_new(c->ssl_ctx);
|
if (!c->ssl)
|
ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR), errmsg("UTL_TCP: SSL_new failed")));
|
|
SSL_set_fd(c->ssl, c->fd);
|
|
int rc = SSL_connect(c->ssl);
|
if (rc != 1) {
|
long e = ERR_get_error();
|
ereport(ERROR, (errcode(ERRCODE_CONNECTION_FAILURE),
|
errmsg("UTL_TCP: SSL_connect failed: %s", ERR_error_string(e, nullptr))));
|
}
|
|
c->use_tls = true;
|
}
|
#endif
|
|
static ssize_t fill_inbuf(TcpConn* c, size_t min_bytes)
|
{
|
c->inbuf.compact();
|
size_t want = std::max(min_bytes, (size_t)1024);
|
c->inbuf.ensure_tail(want);
|
|
int tmo = (c->tx_timeout < 0 ? -1 : c->tx_timeout);
|
ssize_t n = tcp_read(c, c->inbuf.data + c->inbuf.len, want, tmo);
|
if (n > 0) c->inbuf.len += (size_t)n;
|
return n;
|
}
|
|
static bool fetch_until(TcpConn* c, const std::string& term, bool include_term, std::string& out, bool peek)
|
{
|
for (;;) {
|
size_t start = c->inbuf.pos;
|
const char* base = c->inbuf.data + start;
|
const size_t have = c->inbuf.avail();
|
if (have > 0) {
|
const char* p = (const char*) memmem(base, have, term.data(), term.size());
|
if (p) {
|
size_t off = (size_t)(p - (c->inbuf.data + c->inbuf.pos));
|
size_t take = off + (include_term ? term.size() : 0);
|
out.assign(c->inbuf.data + c->inbuf.pos, take);
|
if (!peek) c->inbuf.pos += take;
|
return true;
|
}
|
}
|
ssize_t n = fill_inbuf(c, 1);
|
if (n == 0) {
|
if (c->inbuf.avail() == 0) return false; // EOF и ничего нет
|
out.assign(c->inbuf.data + c->inbuf.pos, c->inbuf.avail());
|
if (!peek) c->inbuf.pos = c->inbuf.len;
|
return true;
|
}
|
if (n < 0)
|
ereport(ERROR, (errcode(ERRCODE_CONNECTION_FAILURE), errmsg("UTL_TCP: network read error")));
|
}
|
}
|
|
static void consume_from_inbuf(TcpConn* c, size_t want, std::string& out, bool peek)
|
{
|
while (c->inbuf.avail() < want) {
|
ssize_t n = fill_inbuf(c, want - c->inbuf.avail());
|
if (n == 0) break;
|
if (n < 0)
|
ereport(ERROR, (errcode(ERRCODE_CONNECTION_FAILURE), errmsg("UTL_TCP: network read error")));
|
}
|
size_t take = std::min(want, c->inbuf.avail());
|
out.assign(c->inbuf.data + c->inbuf.pos, take);
|
if (!peek) c->inbuf.pos += take;
|
}
|
|
// ---------- RECORD(rc, data) builders ----------
|
|
static Datum make_record_int_text(FunctionCallInfo fcinfo, int rc, text* data)
|
{
|
TupleDesc tupdesc;
|
if (get_call_result_type(fcinfo, nullptr, &tupdesc) != TYPEFUNC_COMPOSITE)
|
ereport(ERROR, (errcode(ERRCODE_DATATYPE_MISMATCH), errmsg("expected RECORD(rc, data)")));
|
BlessTupleDesc(tupdesc);
|
Datum values[2];
|
bool nulls[2] = {false, data == nullptr};
|
values[0] = Int32GetDatum(rc);
|
values[1] = PointerGetDatum(data ? data : cstring_to_text(""));
|
HeapTuple tuple = heap_form_tuple(tupdesc, values, nulls);
|
PG_RETURN_DATUM(HeapTupleGetDatum(tuple));
|
}
|
|
static Datum make_record_int_raw(FunctionCallInfo fcinfo, int rc, bytea* data)
|
{
|
TupleDesc tupdesc;
|
if (get_call_result_type(fcinfo, nullptr, &tupdesc) != TYPEFUNC_COMPOSITE)
|
ereport(ERROR, (errcode(ERRCODE_DATATYPE_MISMATCH), errmsg("expected RECORD(rc, data)")));
|
BlessTupleDesc(tupdesc);
|
Datum values[2];
|
bool nulls[2] = {false, data == nullptr};
|
values[0] = Int32GetDatum(rc);
|
values[1] = PointerGetDatum(data ? data : raw_from_buf(nullptr, 0));
|
HeapTuple tuple = heap_form_tuple(tupdesc, values, nulls);
|
PG_RETURN_DATUM(HeapTupleGetDatum(tuple));
|
}
|
|
|
extern "C" {
|
|
PG_FUNCTION_INFO_V1(utl_tcp_available);
|
/*
|
* @qdb_func: utl_tcp_available(int4, int4)
|
* @ora_func: UTL_TCP.AVAILABLE(C PL/SQL_RECORD, BINARY_INTEGER)
|
*/
|
Datum utl_tcp_available(PG_FUNCTION_ARGS)
|
{
|
int32 handle = PG_GETARG_INT32(0);
|
int32 timeout = PG_GETARG_INT32(1);
|
TcpConn* c = conn_lookup(handle);
|
|
if (!PG_ARGISNULL(1) && timeout > 0) {
|
int rc = wait_io(c->fd, POLLIN, timeout);
|
if (rc <= 0) PG_RETURN_INT32(0);
|
}
|
|
int inq = DatumGetInt32(DirectFunctionCall1(utl_tcp_bytes_available_fd, Int32GetDatum(c->fd)));
|
int have = (int) c->inbuf.avail();
|
int total = have + std::max(inq, 0);
|
if (total == 0 && wait_io(c->fd, POLLIN, 0) > 0)
|
total = 1;
|
PG_RETURN_INT32(total);
|
}
|
|
PG_FUNCTION_INFO_V1(utl_tcp_close_all_connections);
|
/*
|
* @qdb_func: utl_tcp_close_all_connections()
|
* @ora_func: UTL_TCP.CLOSE_ALL_CONNECTIONS()
|
*/
|
Datum utl_tcp_close_all_connections(PG_FUNCTION_ARGS)
|
{
|
ensure_conn_tab();
|
if (g_conn_tab) {
|
HASH_SEQ_STATUS scan;
|
hash_seq_init(&scan, g_conn_tab);
|
ConnEntry* e;
|
std::vector<int> keys;
|
while ((e = (ConnEntry*) hash_seq_search(&scan)) != nullptr)
|
keys.push_back(e->key);
|
for (int k : keys) conn_remove(k);
|
}
|
PG_RETURN_VOID();
|
}
|
|
PG_FUNCTION_INFO_V1(utl_tcp_close_connection);
|
/*
|
* @qdb_func: utl_tcp_close_connection(int4)
|
* @ora_func: UTL_TCP.CLOSE_CONNECTION(C PL/SQL_RECORD)
|
*/
|
Datum utl_tcp_close_connection(PG_FUNCTION_ARGS)
|
{
|
int32 handle = PG_GETARG_INT32(0);
|
conn_remove(handle);
|
PG_RETURN_VOID();
|
}
|
|
PG_FUNCTION_INFO_V1(utl_tcp_flush);
|
/*
|
* @qdb_func: utl_tcp_flush(int4)
|
* @ora_func: UTL_TCP.FLUSH(C PL/SQL_RECORD)
|
*/
|
Datum utl_tcp_flush(PG_FUNCTION_ARGS)
|
{
|
(void) conn_lookup(PG_GETARG_INT32(0));
|
// нет своего буфера записи, отправляем сразу — ничего делать
|
PG_RETURN_VOID();
|
}
|
|
static text* strip_crlf(text* t)
|
{
|
if (!t) return nullptr;
|
int len = VARSIZE_ANY_EXHDR(t);
|
char* p = VARDATA_ANY(t);
|
while (len > 0 && (p[len-1] == '\n' || p[len-1] == '\r')) --len;
|
text* out = (text*) palloc(VARHDRSZ + len);
|
SET_VARSIZE(out, VARHDRSZ + len);
|
if (len) memcpy(VARDATA(out), p, len);
|
return out;
|
}
|
|
PG_FUNCTION_INFO_V1(utl_tcp_get_line);
|
/*
|
* @qdb_func: utl_tcp_get_line(int4, boolean, boolean)
|
* @ora_func: UTL_TCP.GET_LINE(C PL/SQL_RECORD, BOOLEAN, BOOLEAN)
|
*/
|
Datum utl_tcp_get_line(PG_FUNCTION_ARGS)
|
{
|
int32 handle = PG_GETARG_INT32(0);
|
bool remove_crlf= PG_GETARG_BOOL(1);
|
bool peek = PG_GETARG_BOOL(2);
|
TcpConn* c = conn_lookup(handle);
|
|
std::string line;
|
bool ok = fetch_until(c, "\n", true, line, peek);
|
if (!ok) PG_RETURN_TEXT_P(cstring_to_text(""));
|
|
text* t = text_from_buf(line.data(), line.size());
|
if (remove_crlf) t = strip_crlf(t);
|
PG_RETURN_TEXT_P(t);
|
}
|
|
PG_FUNCTION_INFO_V1(utl_tcp_get_line_nchar);
|
/*
|
* @qdb_func: utl_tcp_get_line_nchar(int4, boolean, boolean)
|
* @ora_func: UTL_TCP.GET_LINE_NCHAR(C PL/SQL_RECORD, BOOLEAN, BOOLEAN)
|
*/
|
Datum utl_tcp_get_line_nchar(PG_FUNCTION_ARGS)
|
{
|
int32 handle = PG_GETARG_INT32(0);
|
bool remove_crlf= PG_GETARG_BOOL(1);
|
bool peek = PG_GETARG_BOOL(2);
|
TcpConn* c = conn_lookup(handle);
|
|
std::string line;
|
bool ok = fetch_until(c, "\n", true, line, peek);
|
if (!ok) PG_RETURN_TEXT_P(cstring_to_text(""));
|
|
text* t = text_from_buf(line.data(), line.size());
|
if (remove_crlf) t = strip_crlf(t);
|
PG_RETURN_TEXT_P(t);
|
}
|
|
PG_FUNCTION_INFO_V1(utl_tcp_get_raw);
|
/*
|
* @qdb_func: utl_tcp_get_raw(int4, int4, boolean)
|
* @ora_func: UTL_TCP.GET_RAW(C PL/SQL_RECORD, BINARY_INTEGER, BOOLEAN)
|
*/
|
Datum utl_tcp_get_raw(PG_FUNCTION_ARGS)
|
{
|
int32 handle = PG_GETARG_INT32(0);
|
int32 len = PG_GETARG_INT32(1);
|
bool peek = PG_GETARG_BOOL(2);
|
TcpConn* c = conn_lookup(handle);
|
|
if (len <= 0) PG_RETURN_BYTEA_P(raw_from_buf(nullptr, 0));
|
|
std::string data;
|
consume_from_inbuf(c, (size_t)len, data, peek);
|
PG_RETURN_BYTEA_P(raw_from_buf(data.data(), data.size()));
|
}
|
|
PG_FUNCTION_INFO_V1(utl_tcp_get_text);
|
/*
|
* @qdb_func: utl_tcp_get_text(int4, int4, boolean)
|
* @ora_func: UTL_TCP.GET_TEXT(C PL/SQL_RECORD, BINARY_INTEGER, BOOLEAN)
|
*/
|
Datum utl_tcp_get_text(PG_FUNCTION_ARGS)
|
{
|
int32 handle = PG_GETARG_INT32(0);
|
int32 len = PG_GETARG_INT32(1);
|
bool peek = PG_GETARG_BOOL(2);
|
TcpConn* c = conn_lookup(handle);
|
|
if (len <= 0) PG_RETURN_TEXT_P(cstring_to_text(""));
|
|
std::string data;
|
consume_from_inbuf(c, (size_t)len, data, peek);
|
PG_RETURN_TEXT_P(text_from_buf(data.data(), data.size()));
|
}
|
|
PG_FUNCTION_INFO_V1(utl_tcp_get_text_nchar);
|
/*
|
* @qdb_func: utl_tcp_get_text_nchar(int4, int4, boolean)
|
* @ora_func: UTL_TCP.GET_TEXT_NCHAR(C PL/SQL_RECORD, BINARY_INTEGER, BOOLEAN)
|
*/
|
Datum utl_tcp_get_text_nchar(PG_FUNCTION_ARGS)
|
{
|
int32 handle = PG_GETARG_INT32(0);
|
int32 len = PG_GETARG_INT32(1);
|
bool peek = PG_GETARG_BOOL(2);
|
TcpConn* c = conn_lookup(handle);
|
|
if (len <= 0) PG_RETURN_TEXT_P(cstring_to_text(""));
|
|
std::string data;
|
consume_from_inbuf(c, (size_t)len, data, peek);
|
PG_RETURN_TEXT_P(text_from_buf(data.data(), data.size()));
|
}
|
|
PG_FUNCTION_INFO_V1(utl_tcp_open_connection);
|
/*
|
* @qdb_func: utl_tcp_open_connection(text, int4, text, int4, int4, int4, text, text, int4, text, text)
|
* @ora_func: UTL_TCP.OPEN_CONNECTION(VARCHAR2, BINARY_INTEGER, VARCHAR2, BINARY_INTEGER, BINARY_INTEGER, BINARY_INTEGER, VARCHAR2, VARCHAR2, BINARY_INTEGER, VARCHAR2, VARCHAR2)
|
*/
|
Datum utl_tcp_open_connection(PG_FUNCTION_ARGS)
|
{
|
char* remote_host = TextDatumGetCString(PG_GETARG_DATUM(0));
|
int32 remote_port = PG_GETARG_INT32(1);
|
int32 in_buf_sz = PG_ARGISNULL(4) ? 8192 : PG_GETARG_INT32(4);
|
int32 out_buf_sz = PG_ARGISNULL(5) ? 8192 : PG_GETARG_INT32(5);
|
char* charset = PG_ARGISNULL(6) ? nullptr : TextDatumGetCString(PG_GETARG_DATUM(6));
|
char* newline = PG_ARGISNULL(7) ? const_cast<char*>("\r\n") : TextDatumGetCString(PG_GETARG_DATUM(7));
|
int32 tx_timeout = PG_ARGISNULL(8) ? -1 : PG_GETARG_INT32(8);
|
char* wallet_path = PG_ARGISNULL(9) ? nullptr : TextDatumGetCString(PG_GETARG_DATUM(9));
|
char* wallet_pwd = PG_ARGISNULL(10)? nullptr : TextDatumGetCString(PG_GETARG_DATUM(10));
|
|
if (remote_port <= 0)
|
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("UTL_TCP: remote_port must be > 0")));
|
|
dbms_network_acl_admin_check_connect(
|
(remote_host && remote_host[0]) ? remote_host : "127.0.0.1", remote_port);
|
|
TcpConn* c = new TcpConn();
|
c->inbuf_cfg = std::max<size_t>((size_t)std::max(1, in_buf_sz), (size_t)1024);
|
c->outbuf_cfg = std::max<size_t>((size_t)std::max(1, out_buf_sz), (size_t)1024);
|
c->tx_timeout = (tx_timeout < 0 ? -1 : tx_timeout);
|
c->charset = charset ? std::string(charset) : std::string();
|
c->newline = newline ? std::string(newline) : std::string("\r\n");
|
c->remote_host = remote_host ? std::string(remote_host) : std::string();
|
c->remote_port = remote_port;
|
|
resolve_and_connect(c, remote_host, remote_port);
|
c->inbuf.init(c->inbuf_cfg);
|
|
#ifdef USE_OPENSSL
|
if (wallet_path) init_tls_if_requested(c, wallet_path, wallet_pwd);
|
#endif
|
|
int handle = conn_register(c);
|
PG_RETURN_INT32(handle);
|
}
|
|
PG_FUNCTION_INFO_V1(utl_tcp_read_line);
|
Datum utl_tcp_read_line(PG_FUNCTION_ARGS)
|
{
|
int32 handle = PG_GETARG_INT32(0);
|
bool peek = PG_GETARG_BOOL(1);
|
TcpConn* c = conn_lookup(handle);
|
|
std::string line;
|
bool ok = fetch_until(c, "\n", false, line, peek);
|
if (!ok) return make_record_int_text(fcinfo, 0, cstring_to_text(""));
|
|
int rc = (int) line.size();
|
text* t = text_from_buf(line.data(), line.size());
|
return make_record_int_text(fcinfo, rc, t);
|
}
|
|
PG_FUNCTION_INFO_V1(utl_tcp_read_raw);
|
Datum utl_tcp_read_raw(PG_FUNCTION_ARGS)
|
{
|
int32 handle = PG_GETARG_INT32(0);
|
int32 len = PG_GETARG_INT32(1);
|
bool peek = PG_GETARG_BOOL(2);
|
TcpConn* c = conn_lookup(handle);
|
|
if (len <= 0) return make_record_int_raw(fcinfo, 0, raw_from_buf(nullptr, 0));
|
|
std::string data;
|
consume_from_inbuf(c, (size_t)len, data, peek);
|
return make_record_int_raw(fcinfo, (int)data.size(), raw_from_buf(data.data(), data.size()));
|
}
|
|
PG_FUNCTION_INFO_V1(utl_tcp_read_text);
|
Datum utl_tcp_read_text(PG_FUNCTION_ARGS)
|
{
|
int32 handle = PG_GETARG_INT32(0);
|
int32 len = PG_GETARG_INT32(1);
|
bool peek = PG_GETARG_BOOL(2);
|
TcpConn* c = conn_lookup(handle);
|
|
if (len <= 0) return make_record_int_text(fcinfo, 0, cstring_to_text(""));
|
|
std::string data;
|
consume_from_inbuf(c, (size_t)len, data, peek);
|
return make_record_int_text(fcinfo, (int)data.size(), text_from_buf(data.data(), data.size()));
|
}
|
|
PG_FUNCTION_INFO_V1(utl_tcp_secure_connection);
|
Datum utl_tcp_secure_connection(PG_FUNCTION_ARGS)
|
{
|
int32 handle = PG_GETARG_INT32(0);
|
TcpConn* c = conn_lookup(handle);
|
#ifdef USE_OPENSSL
|
if (!c->use_tls) init_tls_if_requested(c, "", nullptr);
|
PG_RETURN_VOID();
|
#else
|
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
errmsg("UTL_TCP: built without OpenSSL; rebuild with -DUSE_OPENSSL -lssl -lcrypto")));
|
PG_RETURN_VOID();
|
#endif
|
}
|
|
PG_FUNCTION_INFO_V1(utl_tcp_write_line);
|
/*
|
* @qdb_func: utl_tcp_write_line(int4, text)
|
* @ora_func: UTL_TCP.WRITE_LINE(C PL/SQL_RECORD, VARCHAR2)
|
*/
|
Datum utl_tcp_write_line(PG_FUNCTION_ARGS)
|
{
|
int32 handle = PG_GETARG_INT32(0);
|
char* data = PG_ARGISNULL(1) ? nullptr : TextDatumGetCString(PG_GETARG_DATUM(1));
|
TcpConn* c = conn_lookup(handle);
|
|
std::string msg = (data ? std::string(data) : std::string());
|
msg += c->newline;
|
|
int sent = 0;
|
int tmo = (c->tx_timeout < 0 ? -1 : c->tx_timeout);
|
const char* p = msg.data();
|
size_t left = msg.size();
|
while (left > 0) {
|
ssize_t n = tcp_write(c, p, left, tmo);
|
if (n <= 0) break;
|
p += n; left -= (size_t)n; sent += (int)n;
|
}
|
PG_RETURN_INT32(sent);
|
}
|
|
PG_FUNCTION_INFO_V1(utl_tcp_write_raw);
|
/*
|
* @qdb_func: utl_tcp_write_raw(int4, raw, int4)
|
* @ora_func: UTL_TCP.WRITE_RAW(C PL/SQL_RECORD, RAW, BINARY_INTEGER)
|
*/
|
Datum utl_tcp_write_raw(PG_FUNCTION_ARGS)
|
{
|
int32 handle = PG_GETARG_INT32(0);
|
bytea* b = (bytea*) PG_GETARG_BYTEA_PP(1);
|
bool has_len = !PG_ARGISNULL(2);
|
int32 len = has_len ? PG_GETARG_INT32(2) : -1;
|
|
TcpConn* c = conn_lookup(handle);
|
|
size_t blen = VARSIZE_ANY_EXHDR(b);
|
const char* p = VARDATA_ANY(b);
|
size_t tosend = (len < 0) ? blen : std::min((size_t)len, blen);
|
|
int sent = 0;
|
int tmo = (c->tx_timeout < 0 ? -1 : c->tx_timeout);
|
while (tosend > 0) {
|
ssize_t n = tcp_write(c, p, tosend, tmo);
|
if (n <= 0) break;
|
p += n; tosend -= (size_t)n; sent += (int)n;
|
}
|
PG_RETURN_INT32(sent);
|
}
|
|
PG_FUNCTION_INFO_V1(utl_tcp_write_text);
|
/*
|
* @qdb_func: utl_tcp_write_text(int4, text, int4)
|
* @ora_func: UTL_TCP.WRITE_TEXT(C PL/SQL_RECORD, VARCHAR2, BINARY_INTEGER)
|
*/
|
Datum utl_tcp_write_text(PG_FUNCTION_ARGS)
|
{
|
int32 handle = PG_GETARG_INT32(0);
|
char* data = TextDatumGetCString(PG_GETARG_DATUM(1));
|
bool has_len= !PG_ARGISNULL(2);
|
int32 len = has_len ? PG_GETARG_INT32(2) : -1;
|
|
TcpConn* c = conn_lookup(handle);
|
|
size_t dlen = strlen(data);
|
size_t tosend = (len < 0) ? dlen : std::min((size_t)len, dlen);
|
const char* p = data;
|
|
int sent = 0;
|
int tmo = (c->tx_timeout < 0 ? -1 : c->tx_timeout);
|
while (tosend > 0) {
|
ssize_t n = tcp_write(c, p, tosend, tmo);
|
if (n <= 0) break;
|
p += n; tosend -= (size_t)n; sent += (int)n;
|
}
|
PG_RETURN_INT32(sent);
|
}
|
|
PG_FUNCTION_INFO_V1(utl_tcp_bytes_available_fd);
|
Datum utl_tcp_bytes_available_fd(PG_FUNCTION_ARGS) {
|
int fd = PG_GETARG_INT32(0);
|
|
int n = 0;
|
#ifdef FIONREAD
|
if (ioctl(fd, FIONREAD, &n) == 0) {
|
if (n < 0) n = 0;
|
return n;
|
}
|
#endif
|
int rc = wait_io(fd, POLLIN, 0);
|
return (rc > 0) ? 1 : 0;
|
}
|
|
PG_FUNCTION_INFO_V1(utl_tcp_get_version);
|
/// @brief Возвращает версию RUDB пакета UTL_TCP (Oracle UTL_TCP пакет)
|
Datum utl_tcp_get_version(PG_FUNCTION_ARGS) {
|
PG_RETURN_TEXT_P(cstring_to_text(RUDB_UTL_TCP_VERSION));
|
}
|
|
} // extern "C"
|