diff --git a/GNUmakefile.in b/GNUmakefile.in index bf6c66664..075e9951f 100644 --- a/GNUmakefile.in +++ b/GNUmakefile.in @@ -111,6 +111,7 @@ install: @if test -d contrib/gms_xmlgen; then $(MAKE) -C contrib/gms_xmlgen $@; fi @if test -d contrib/gms_output; then $(MAKE) -C contrib/gms_output $@; fi @if test -d contrib/gms_i18n; then $(MAKE) -C contrib/gms_i18n $@; fi + @if test -d contrib/gms_inaddr; then $(MAKE) -C contrib/gms_inaddr $@; fi @if test -d contrib/timescaledb; then (./contrib/timescaledb/run_to_build.sh && $(MAKE) -C contrib/timescaledb/build $@); fi @if test -d contrib/chparser; then \ if command -v scws &> /dev/null; then \ diff --git a/build/script/aarch64_lite_list b/build/script/aarch64_lite_list index 54f136f9a..fd01590bc 100644 --- a/build/script/aarch64_lite_list +++ b/build/script/aarch64_lite_list @@ -75,6 +75,8 @@ ./share/postgresql/extension/gms_sql.control ./share/postgresql/extension/gms_i18n--1.0.sql ./share/postgresql/extension/gms_i18n.control +./share/postgresql/extension/gms_inaddr--1.0.sql +./share/postgresql/extension/gms_inaddr.control ./share/postgresql/timezone/GB-Eire ./share/postgresql/timezone/Turkey ./share/postgresql/timezone/Kwajalein @@ -770,6 +772,7 @@ ./lib/postgresql/gms_lob.so ./lib/postgresql/gms_sql.so ./lib/postgresql/gms_i18n.so +./lib/postgresql/gms_inaddr.so ./include/postgresql/server/postgres_ext.h ./include/postgresql/server/pg_config_os.h ./include/postgresql/server/pgtime.h diff --git a/build/script/aarch64_opengauss_list b/build/script/aarch64_opengauss_list index bf7ec04ce..0c4ca97c4 100644 --- a/build/script/aarch64_opengauss_list +++ b/build/script/aarch64_opengauss_list @@ -125,6 +125,8 @@ ./share/postgresql/extension/gms_compress.control ./share/postgresql/extension/gms_output--1.0.sql ./share/postgresql/extension/gms_output.control +./share/postgresql/extension/gms_inaddr--1.0.sql +./share/postgresql/extension/gms_inaddr.control ./share/postgresql/extension/gms_lob--1.0.sql ./share/postgresql/extension/gms_lob.control ./share/postgresql/extension/gms_stats--1.0.sql @@ -840,6 +842,7 @@ ./lib/postgresql/assessment.so ./lib/postgresql/gms_compress.so ./lib/postgresql/gms_output.so +./lib/postgresql/gms_inaddr.so ./lib/postgresql/gms_lob.so ./lib/postgresql/gms_stats.so ./lib/postgresql/gms_tcp.so diff --git a/build/script/loongarch64_lite_list b/build/script/loongarch64_lite_list index 3ae5157aa..03168f684 100644 --- a/build/script/loongarch64_lite_list +++ b/build/script/loongarch64_lite_list @@ -56,6 +56,8 @@ ./share/postgresql/extension/log_fdw.control ./share/postgresql/extension/gms_output--1.0.sql ./share/postgresql/extension/gms_output.control +./share/postgresql/extension/gms_inaddr--1.0.sql +./share/postgresql/extension/gms_inaddr.control ./share/postgresql/extension/gms_stats--1.0.sql ./share/postgresql/extension/gms_stats.control ./share/postgresql/extension/gms_profiler--1.0.sql @@ -754,6 +756,7 @@ ./lib/postgresql/euc2004_sjis2004.so ./lib/postgresql/pgoutput.so ./lib/postgresql/gms_output.so +./lib/postgresql/gms_inaddr.so ./lib/postgresql/gms_stats.so ./lib/postgresql/gms_profiler.so ./lib/postgresql/gms_lob.so diff --git a/build/script/opengauss_release_list_mini b/build/script/opengauss_release_list_mini index c4e4bbe91..5776d553c 100644 --- a/build/script/opengauss_release_list_mini +++ b/build/script/opengauss_release_list_mini @@ -86,6 +86,8 @@ ./share/postgresql/extension/dblink.control ./share/postgresql/extension/gms_output--1.0.sql ./share/postgresql/extension/gms_output.control +./share/postgresql/extension/gms_inaddr--1.0.sql +./share/postgresql/extension/gms_inaddr.control ./share/postgresql/extension/gms_stats--1.0.sql ./share/postgresql/extension/gms_stats.control ./share/postgresql/extension/gms_profiler--1.0.sql @@ -772,6 +774,7 @@ ./lib/postgresql/gms_compress.so ./lib/postgresql/dblink.so ./lib/postgresql/gms_output.so +./lib/postgresql/gms_inaddr.so ./lib/postgresql/gms_stats.so ./lib/postgresql/gms_profiler.so ./lib/postgresql/gms_lob.so diff --git a/build/script/opengauss_release_list_ubuntu_single b/build/script/opengauss_release_list_ubuntu_single index 78ab6d165..48e7a8433 100644 --- a/build/script/opengauss_release_list_ubuntu_single +++ b/build/script/opengauss_release_list_ubuntu_single @@ -113,6 +113,8 @@ ./share/postgresql/extension/gms_compress.control ./share/postgresql/extension/gms_output--1.0.sql ./share/postgresql/extension/gms_output.control +./share/postgresql/extension/gms_inaddr--1.0.sql +./share/postgresql/extension/gms_inaddr.control ./share/postgresql/extension/gms_lob--1.0.sql ./share/postgresql/extension/gms_lob.control ./share/postgresql/extension/gms_stats--1.0.sql @@ -810,6 +812,7 @@ ./lib/postgresql/dblink.so ./lib/postgresql/gms_compress.so ./lib/postgresql/gms_output.so +./lib/postgresql/gms_inaddr.so ./lib/postgresql/gms_lob.so ./lib/postgresql/gms_stats.so ./lib/postgresql/gms_profiler.so diff --git a/build/script/x86_64_lite_list b/build/script/x86_64_lite_list index 74504a282..0cbf41d7e 100644 --- a/build/script/x86_64_lite_list +++ b/build/script/x86_64_lite_list @@ -75,6 +75,8 @@ ./share/postgresql/extension/gms_sql.control ./share/postgresql/extension/gms_i18n--1.0.sql ./share/postgresql/extension/gms_i18n.control +./share/postgresql/extension/gms_inaddr--1.0.sql +./share/postgresql/extension/gms_inaddr.control ./share/postgresql/timezone/GB-Eire ./share/postgresql/timezone/Turkey ./share/postgresql/timezone/Kwajalein @@ -769,6 +771,7 @@ ./lib/postgresql/gms_lob.so ./lib/postgresql/gms_sql.so ./lib/postgresql/gms_i18n.so +./lib/postgresql/gms_inaddr.so ./lib/libxgboost.so ./include/postgresql/server/postgres_ext.h ./include/postgresql/server/pg_config_os.h diff --git a/build/script/x86_64_opengauss_list b/build/script/x86_64_opengauss_list index ab2c11280..df6868a4b 100644 --- a/build/script/x86_64_opengauss_list +++ b/build/script/x86_64_opengauss_list @@ -125,6 +125,8 @@ ./share/postgresql/extension/gms_compress.control ./share/postgresql/extension/gms_output--1.0.sql ./share/postgresql/extension/gms_output.control +./share/postgresql/extension/gms_inaddr--1.0.sql +./share/postgresql/extension/gms_inaddr.control ./share/postgresql/extension/gms_lob--1.0.sql ./share/postgresql/extension/gms_lob.control ./share/postgresql/extension/gms_stats--1.0.sql @@ -843,6 +845,7 @@ ./lib/postgresql/assessment.so ./lib/postgresql/gms_compress.so ./lib/postgresql/gms_output.so +./lib/postgresql/gms_inaddr.so ./lib/postgresql/gms_profiler.so ./lib/postgresql/gms_sql.so ./lib/postgresql/gms_xmlgen.so diff --git a/contrib/CMakeLists.txt b/contrib/CMakeLists.txt index c1cedeeac..6d228b7a4 100644 --- a/contrib/CMakeLists.txt +++ b/contrib/CMakeLists.txt @@ -16,6 +16,7 @@ set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/pg_upgrade_support ${CMAKE_CURRENT_SOURCE_DIR}/postgres_fdw ${CMAKE_CURRENT_SOURCE_DIR}/gms_output + ${CMAKE_CURRENT_SOURCE_DIR}/gms_inaddr ${CMAKE_CURRENT_SOURCE_DIR}/security_plugin ${CMAKE_CURRENT_SOURCE_DIR}/dummy_seclabel ${CMAKE_CURRENT_SOURCE_DIR}/pagehack @@ -52,6 +53,7 @@ if("${ENABLE_MULTIPLE_NODES}" STREQUAL "ON" OR "${ENABLE_PRIVATEGAUSS}" STREQUAL endif() add_subdirectory(postgres_fdw) add_subdirectory(gms_output) +add_subdirectory(gms_inaddr) add_subdirectory(security_plugin) add_subdirectory(dummy_seclabel) add_subdirectory(pagehack) diff --git a/contrib/Makefile b/contrib/Makefile index 1e80222a6..533ff5c57 100644 --- a/contrib/Makefile +++ b/contrib/Makefile @@ -58,6 +58,7 @@ SUBDIRS = \ security_plugin \ ndpplugin \ gms_profiler \ + gms_inaddr \ gms_output \ gms_stats \ gms_lob \ diff --git a/contrib/gms_inaddr/CMakeLists.txt b/contrib/gms_inaddr/CMakeLists.txt new file mode 100644 index 000000000..1dc4f4c87 --- /dev/null +++ b/contrib/gms_inaddr/CMakeLists.txt @@ -0,0 +1,18 @@ +#This is the main CMAKE for build all components. +# gms_inaddr.so +AUX_SOURCE_DIRECTORY(${PROJECT_OPENGS_DIR}/contrib/gms_inaddr TGT_gms_inaddr_SRC) + +set(gms_inaddr_DEF_OPTIONS ${MACRO_OPTIONS}) +set(gms_inaddr_COMPILE_OPTIONS ${OPTIMIZE_OPTIONS} ${OS_OPTIONS} ${PROTECT_OPTIONS} ${WARNING_OPTIONS} ${LIB_SECURE_OPTIONS} ${CHECK_OPTIONS}) +set(gms_inaddr_LINK_OPTIONS ${LIB_LINK_OPTIONS}) +add_shared_libtarget(gms_inaddr TGT_gms_inaddr_SRC "" "${gms_inaddr_DEF_OPTIONS}" "${gms_inaddr_COMPILE_OPTIONS}" "${gms_inaddr_LINK_OPTIONS}") +set_target_properties(gms_inaddr PROPERTIES PREFIX "") + +install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/gms_inaddr.control + DESTINATION share/postgresql/extension/ +) +install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/gms_inaddr--1.0.sql + DESTINATION share/postgresql/extension/ +) +install(TARGETS gms_inaddr LIBRARY DESTINATION lib/postgresql) + diff --git a/contrib/gms_inaddr/Makefile b/contrib/gms_inaddr/Makefile new file mode 100644 index 000000000..152f84d94 --- /dev/null +++ b/contrib/gms_inaddr/Makefile @@ -0,0 +1,25 @@ +# contrib/gms_inaddr/Makefile +MODULE_big = gms_inaddr +OBJS = gms_inaddr.o + +EXTENSION = gms_inaddr +DATA = gms_inaddr--1.0.sql + +exclude_option = -fPIE +override CPPFLAGS := -fstack-protector-strong $(filter-out $(exclude_option),$(CPPFLAGS)) + +REGRESS = gms_inaddr +ifdef USE_PGXS +PG_CONFIG = pg_config +PGXS := $(shell $(PG_CONFIG) --pgxs) +include $(PGXS) +else +subdir = contrib/gms_inaddr +top_builddir = ../.. +include $(top_builddir)/src/Makefile.global +regress_home = $(top_builddir)/src/test/regress +REGRESS_OPTS = -c 0 -d 1 -r 1 -p 25632 --single_node -w --keep_last_data=false \ + --regconf=$(regress_home)/regress.conf \ + --temp-config=$(regress_home)/make_fastcheck_postgresql.conf +include $(top_srcdir)/contrib/contrib-global.mk +endif diff --git a/contrib/gms_inaddr/data/dummy.txt b/contrib/gms_inaddr/data/dummy.txt new file mode 100644 index 000000000..7f015cede --- /dev/null +++ b/contrib/gms_inaddr/data/dummy.txt @@ -0,0 +1 @@ +The openGauss regression needs this file to run. \ No newline at end of file diff --git a/contrib/gms_inaddr/expected/gms_inaddr.out b/contrib/gms_inaddr/expected/gms_inaddr.out new file mode 100644 index 000000000..22f43dbe8 --- /dev/null +++ b/contrib/gms_inaddr/expected/gms_inaddr.out @@ -0,0 +1,29 @@ +create extension gms_output; +create extension gms_inaddr; +begin +gms_output.enable; +gms_output.put_line(gms_inaddr.get_host_address('localhost')); +gms_output.put_line(gms_inaddr.get_host_name('127.0.0.1')); +end +/ +127.0.0.1 +localhost +begin +gms_output.enable; +gms_output.put_line(gms_inaddr.get_host_address('localhostxx')); +gms_output.put_line(gms_inaddr.get_host_name('127.0.0.1')); +end; +/ +ERROR: could not translate host name "localhostxx" to address: Name or service not known +CONTEXT: SQL statement "CALL gms_output.put_line(gms_inaddr.get_host_address('localhostxx'))" +PL/pgSQL function inline_code_block line 3 at PERFORM +begin +gms_output.enable; +gms_output.put_line(gms_inaddr.get_host_name('10.254.180.400')); +end; +/ +ERROR: error happen when fetch hostname: Name or service not known +CONTEXT: SQL statement "CALL gms_output.put_line(gms_inaddr.get_host_name('10.254.180.400'))" +PL/pgSQL function inline_code_block line 3 at PERFORM +drop extension gms_output; +drop extension gms_inaddr; diff --git a/contrib/gms_inaddr/gms_inaddr--1.0.sql b/contrib/gms_inaddr/gms_inaddr--1.0.sql new file mode 100644 index 000000000..dbd66e99b --- /dev/null +++ b/contrib/gms_inaddr/gms_inaddr--1.0.sql @@ -0,0 +1,17 @@ +/* contrib/gms_output/gms_output--1.0.sql */ + +-- complain if script is sourced in psql, rather than via CREATE EXTENSION +\echo Use "CREATE EXTENSION gms_output" to load this file. \quit + +CREATE SCHEMA gms_inaddr; +GRANT USAGE ON SCHEMA gms_inaddr TO PUBLIC; + +CREATE OR REPLACE FUNCTION gms_inaddr.get_host_address(text default 'localhost') +RETURNS text +AS 'MODULE_PATHNAME','gms_inaddr_get_host_address' +LANGUAGE C; + +CREATE OR REPLACE FUNCTION gms_inaddr.get_host_name(text default '127.0.0.1') +RETURNS text +AS 'MODULE_PATHNAME','gms_inaddr_get_host_name' +LANGUAGE C; diff --git a/contrib/gms_inaddr/gms_inaddr.control b/contrib/gms_inaddr/gms_inaddr.control new file mode 100644 index 000000000..67dbae58a --- /dev/null +++ b/contrib/gms_inaddr/gms_inaddr.control @@ -0,0 +1,5 @@ +# gms_inaddr extension +comment = 'gms_inaddr package' +default_version = '1.0' +module_pathname = '$libdir/gms_inaddr' +relocatable = false diff --git a/contrib/gms_inaddr/gms_inaddr.cpp b/contrib/gms_inaddr/gms_inaddr.cpp new file mode 100644 index 000000000..af92098ed --- /dev/null +++ b/contrib/gms_inaddr/gms_inaddr.cpp @@ -0,0 +1,99 @@ +#include +#include "postgres.h" +#include "funcapi.h" +#include "commands/extension.h" +#include "libpq/libpq.h" +#include "libpq/pqformat.h" +#include "utils/lsyscache.h" +#include "utils/builtins.h" +#include "getaddrinfo.h" + +#include "gms_inaddr.h" + +PG_MODULE_MAGIC; + +PG_FUNCTION_INFO_V1(gms_inaddr_get_host_address); +PG_FUNCTION_INFO_V1(gms_inaddr_get_host_name); + +#define MAXLINE 8192 +#define ADDRESSSIZE 40 + +Datum +gms_inaddr_get_host_address(PG_FUNCTION_ARGS) +{ + struct addrinfo *gai_result = NULL, *gai = NULL; + int ret = -1; + char result[ADDRESSSIZE] = {0}; + + char* hostname = text_to_cstring(PG_GETARG_TEXT_PP(0)); + if(PG_ARGISNULL(0)){ + char hostname[256] = {0}; + + (void)gethostname(hostname, 255); + ereport(WARNING, + (errcode(ERRCODE_WARNING), + errmsg("hostname %s!", hostname))); + } + + ret = getaddrinfo(hostname, NULL, NULL, &gai_result); + if (ret != 0) { + ereport(ERROR, + (errcode(ERRCODE_CONFIG_FILE_ERROR), + errmsg( + "could not translate host name \"%s\" to address: %s", hostname, gai_strerror(ret)))); + } + for (gai = gai_result; gai; gai = gai->ai_next) { + errno_t rt; + if (gai->ai_addr->sa_family == AF_INET) { + struct sockaddr_in* h = (struct sockaddr_in*)gai->ai_addr; + char* address = inet_ntoa(h->sin_addr); + rt = strcpy_s(result, ADDRESSSIZE, address); + securec_check(rt, "\0", "\0"); + break; + } + #ifdef HAVE_IPV6 + else if (gai->ai_addr->sa_family == AF_INET6) { + struct sockaddr_in6* h = (struct sockaddr_in6*)gai->ai_addr; + inet_net_ntop(AF_INET6, &(h)->sin6_addr, 128, result, ADDRESSSIZE); + break; + } + #endif + } + + + + if (gai_result != NULL) + freeaddrinfo(gai_result); + + text* result_text = cstring_to_text(result); + PG_RETURN_TEXT_P(result_text); +} + + +Datum +gms_inaddr_get_host_name(PG_FUNCTION_ARGS){ + char* ip_address = text_to_cstring(PG_GETARG_TEXT_PP(0)); + + if(PG_ARGISNULL(0)){ + ip_address = "127.0.0.1"; + } + + struct sockaddr_in addr; + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = inet_addr(ip_address); + + char buf[MAXLINE]; + + int rc = getnameinfo((struct sockaddr *)&addr,sizeof(struct sockaddr_in),buf,MAXLINE,NULL,0,NI_NAMEREQD); + + + if(rc != 0){ + elog(ERROR, + "error happen when fetch hostname: %s", + gai_strerror(rc)); + } + + text* result_text = cstring_to_text(buf); + + PG_RETURN_TEXT_P(result_text); +} diff --git a/contrib/gms_inaddr/gms_inaddr.h b/contrib/gms_inaddr/gms_inaddr.h new file mode 100644 index 000000000..6b3ef57f3 --- /dev/null +++ b/contrib/gms_inaddr/gms_inaddr.h @@ -0,0 +1,9 @@ +#ifndef __GMS_INADDR__ +#define __GMS_INADDR__ + +#include "postgres.h" + +extern "C" Datum gms_inaddr_get_host_address(PG_FUNCTION_ARGS); +extern "C" Datum gms_inaddr_get_host_name(PG_FUNCTION_ARGS); + +#endif // __GMS_INADDR__ diff --git a/contrib/gms_inaddr/sql/gms_inaddr.sql b/contrib/gms_inaddr/sql/gms_inaddr.sql new file mode 100644 index 000000000..dd2ccce32 --- /dev/null +++ b/contrib/gms_inaddr/sql/gms_inaddr.sql @@ -0,0 +1,25 @@ +create extension gms_output; +create extension gms_inaddr; + +begin +gms_output.enable; +gms_output.put_line(gms_inaddr.get_host_address('localhost')); +gms_output.put_line(gms_inaddr.get_host_name('127.0.0.1')); +end +/ + +begin +gms_output.enable; +gms_output.put_line(gms_inaddr.get_host_address('localhostxx')); +gms_output.put_line(gms_inaddr.get_host_name('127.0.0.1')); +end; +/ + +begin +gms_output.enable; +gms_output.put_line(gms_inaddr.get_host_name('10.254.180.400')); +end; +/ + +drop extension gms_output; +drop extension gms_inaddr; diff --git a/src/gausskernel/optimizer/commands/dropcmds.cpp b/src/gausskernel/optimizer/commands/dropcmds.cpp index 69d20a644..26cdd6256 100644 --- a/src/gausskernel/optimizer/commands/dropcmds.cpp +++ b/src/gausskernel/optimizer/commands/dropcmds.cpp @@ -99,6 +99,7 @@ static void DropExtensionInListIsSupported(List* objname) "gms_compress", "gms_stats", "gms_output", + "gms_inaddr", "gms_profiler", "gms_xmlgen", "gms_lob",