Skip to contentSkip to conversation

datatype: Replace getnameinfo() by internal lookup table

PATCH Superseded Elise Lennion 1 participant 1 message Nov 28, 2016 netfilter

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 datatype: Replace getnameinfo() by internal lookup table

To avoid exceeding the inputs number limit of the flex scanner used, when calling getnameinfo() in inet_service_type_print().

The new symbol_table was associated with inet_service_type, to enable listing all pre-defined services using nft command line tool.

The listed services are all well-known and registered ports of my local /etc/services file, from Ubuntu 16.04. Service numbers are converted to respect network byte order.

---

v2: Used symbol_table to list the services and put them in another file. Also used network byte order on service values to replace nft_service_lookup with symbolic_constant_print.

 include/datatype.h |   2 +
 src/Makefile.am    |   1 +
 src/datatype.c     |  15 +--
 src/services.c     | 343 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 348 insertions(+), 13 deletions(-)
 create mode 100644 src/services.c
include/datatype.h +2 -0
index 9f3f711..e53797d 100644
@@ -185,6 +185,8 @@ struct symbol_table {
 	struct symbolic_constant	symbols[];
 };
 
+extern const struct symbol_table serv_tbl;
+
 extern struct error_record *symbolic_constant_parse(const struct expr *sym,
 						    const struct symbol_table *tbl,
 						    struct expr **res);
src/Makefile.am +1 -0
index d021cb7..2a69e19 100644
@@ -52,6 +52,7 @@ nft_SOURCES =	main.c				\
 		erec.c				\
 		mnl.c				\
 		iface.c				\
+		services.c			\
 		scanner.l			\
 		parser_bison.y
 
src/datatype.c +2 -13
index 1e40287..1ae7db4 100644
@@ -559,19 +559,7 @@ const struct datatype inet_protocol_type = {
 
 static void inet_service_type_print(const struct expr *expr)
 {
-	struct sockaddr_in sin = { .sin_family = AF_INET };
-	char buf[NI_MAXSERV];
-	int err;
-
-	sin.sin_port = mpz_get_be16(expr->value);
-	err = getnameinfo((struct sockaddr *)&sin, sizeof(sin), NULL, 0,
-			  buf, sizeof(buf),
-			  numeric_output < NUMERIC_PORT ? 0 : NI_NUMERICSERV);
-	if (err != 0) {
-		getnameinfo((struct sockaddr *)&sin, sizeof(sin), NULL,
-			    0, buf, sizeof(buf), NI_NUMERICSERV);
-	}
-	printf("%s", buf);
+	symbolic_constant_print(&serv_tbl, expr, false);
 }
 
 static struct error_record *inet_service_type_parse(const struct expr *sym,
@@ -615,6 +603,7 @@ const struct datatype inet_service_type = {
 	.basetype	= &integer_type,
 	.print		= inet_service_type_print,
 	.parse		= inet_service_type_parse,
+	.sym_tbl	= &serv_tbl,
 };
 
 #define RT_SYM_TAB_INITIAL_SIZE		16
src/services.c new file +343 -0

343 lines not on this page · raw message

Signature · 1 line
2.7.4