datatype: Replace getnameinfo() by internal lookup table
PATCH v3 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.
v3: No change
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
@@ -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
@@ -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
@@ -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
Pablo Neira Ayuso
maintainerOn Mon, Nov 28, 2016 at 06:20:10PM -0200, Elise Lennion wrote:
[...]
src/services.c line 6
Quoted 12 lines
src/services.c new file +6
-0@@ -0,0 +1,343 @@ +#include <nftables.h> +#include <datatype.h> + +const struct symbol_table serv_tbl = { + .symbols = { +SYMBOL ("exec" ,2 ),
Please, use __constant_htons() so these ports become understandable.
Let's keep it simple, so we forget on binary search by now.
Very much looking forward to having this in the nft tree.
Thanks!