src: use reentrant getprotobyname_r()/getprotobynumber_r()/getservbyport_r()
PATCH 1/1 src: use reentrant getprotobyname_r()/getprotobynumber_r()/getservbyport_r()
If the reentrant versions of the functions are available, use them so that libnftables is thread-safe in this regard.
--- configure.ac | 4 +++ include/utils.h | 4 +++ src/datatype.c | 29 +++++++--------- src/json.c | 21 ++++++------ src/rule.c | 6 ++-- src/utils.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 123 insertions(+), 31 deletions(-)
configure.ac +4 -0
@@ -108,6 +108,10 @@ AC_DEFINE([HAVE_LIBJANSSON], [1], [Define if you have libjansson]) ]) AM_CONDITIONAL([BUILD_JSON], [test "x$with_json" != xno]) +AC_CHECK_DECLS([getprotobyname_r, getprotobynumber_r, getservbyport_r], [], [], [[ +#include <netdb.h> +]]) + AC_CONFIG_FILES([ \ Makefile \ libnftables.pc \
include/utils.h +4 -0
@@ -138,4 +138,8 @@ extern char *xstrdup(const char *s);extern void xstrunescape (const char *in,char *out);extern int round_pow_2 (unsigned int value); +bool nft_getprotobynumber (int number,char *out_name,size_t name_len); +bool nft_getprotobyname (const char *name,uint8_t *out_proto); +bool nft_getservbyport (int port,const char *proto,char *out_name,size_t name_len); +#endif /* NFTABLES_UTILS_H */
src/datatype.c +12 -17
@@ -697,12 +697,11 @@ const struct datatype ip6addr_type = {static void inet_protocol_type_print (const struct expr *expr,struct output_ctx *octx) { -struct protoent *p; -if (!nft_output_numeric_proto (octx)) { - p =getprotobynumber (mpz_get_uint8 (expr->value)); -if (p !=NULL ) { -nft_print (octx,"%s" , p->p_name); +char name[1024 ]; + +if (nft_getprotobynumber (mpz_get_uint8 (expr->value), name,sizeof (name))) { +nft_print (octx,"%s" , name);return ; } }
@@ -711,15 +710,15 @@ static void inet_protocol_type_print(const struct expr *expr,static void inet_protocol_type_describe (struct output_ctx *octx) { -struct protoent *p;uint8_t protonum;for (protonum =0 ; protonum <UINT8_MAX ; protonum++) { - p =getprotobynumber (protonum); -if (!p) +char name[1024 ]; + +if (!nft_getprotobynumber (protonum, name,sizeof (name)))continue ; -nft_print (octx,"\t%-30s\t%u\n" , p->p_name, protonum); +nft_print (octx,"\t%-30s\t%u\n" , name, protonum); } }
@@ -727,7 +726,6 @@ static struct error_record *inet_protocol_type_parse(struct parse_ctx *ctx,const struct expr *sym, struct expr **res) { - struct protoent *p; uint8_t proto;uintmax_t i;char *end;
@@ -740,11 +738,8 @@ static struct error_record *inet_protocol_type_parse(struct parse_ctx *ctx, proto = i; }else { - p =getprotobyname (sym->identifier); -if (p ==NULL ) +if (!nft_getprotobyname (sym->identifier, &proto))return error (&sym->location,"Could not resolve protocol name" ); - - proto = p->p_proto; } *res = constant_expr_alloc(&sym->location, &inet_protocol_type,
@@ -768,12 +763,12 @@ const struct datatype inet_protocol_type = {static void inet_service_print (const struct expr *expr,struct output_ctx *octx) {uint16_t port =mpz_get_be16 (expr->value); -const struct servent *s =getservbyport (port,NULL ); +char name[1024 ]; -if (s ==NULL ) +if (!nft_getservbyport (port,NULL , name,sizeof (name)))nft_print (octx,"%hu" ,ntohs (port));else -nft_print (octx,"\"%s\"" , s->s_name); +nft_print (octx,"\"%s\"" , name); }void inet_service_type_print (const struct expr *expr,struct output_ctx *octx)
src/json.c +10 -11
@@ -297,10 +297,10 @@ static json_t *chain_print_json(const struct chain *chain)static json_t *proto_name_json (uint8_t proto) { -const struct protoent *p =getprotobynumber (proto); +char name[1024 ]; -if (p) -return json_string (p->p_name); +if (nft_getprotobynumber (proto, name,sizeof (name))) +return json_string (name);return json_integer (proto); }
@@ -1093,12 +1093,11 @@ json_t *boolean_type_json(const struct expr *expr, struct output_ctx *octx)json_t *inet_protocol_type_json (const struct expr *expr,struct output_ctx *octx) { -struct protoent *p; -if (!nft_output_numeric_proto (octx)) { - p =getprotobynumber (mpz_get_uint8 (expr->value)); -if (p !=NULL ) -return json_string (p->p_name); +char name[1024 ]; + +if (nft_getprotobynumber (mpz_get_uint8 (expr->value), name,sizeof (name))) +return json_string (name); }return integer_type_json (expr, octx); }
@@ -1106,13 +1105,13 @@ json_t *inet_protocol_type_json(const struct expr *expr,json_t *inet_service_type_json (const struct expr *expr,struct output_ctx *octx) {uint16_t port =mpz_get_be16 (expr->value); -const struct servent *s =NULL ; +char name[1024 ];if (!nft_output_service (octx) || - (s =getservbyport (port,NULL )) ==NULL ) + !nft_getservbyport (port,NULL , name,sizeof (name)))return json_integer (ntohs (port)); -return json_string (s->s_name); +return json_string (name); }json_t *mark_type_json (const struct expr *expr,struct output_ctx *octx)
src/rule.c +3 -3
@@ -1666,10 +1666,10 @@ struct obj *obj_lookup_fuzzy(const char *obj_name,static void print_proto_name_proto (uint8_t l4,struct output_ctx *octx) { -const struct protoent *p =getprotobynumber (l4); +char name[1024 ]; -if (p) -nft_print (octx,"%s" , p->p_name); +if (nft_getprotobynumber (l4, name,sizeof (name))) +nft_print (octx,"%s" , name);else nft_print (octx,"%d" , l4); }
src/utils.c +90 -0
99 lines not on this page · raw message
Signature · 1 line
2.41.0
Quoted 2 lines · Thomas Haller
+bool
nft_getprotobyname(const char *name, uint8_t *out_proto);
Knowing that proto can only be uint8, why not make this work like getc() where the return type is a type with a larger range?
int nft_getprotobyname(const char *name)
{
workworkwork();
if (error)
return -1;
return workresult;
}Quoted 8 lines · Jan Engelhardt
Quoted 2 lines · Thomas Haller
+bool
nft_getprotobyname(const char *name, uint8_t *out_proto);Knowing that proto can only be uint8, why not make this work like
getc()where the return type is a type with a larger range?
Quoted 8 lines
int
nft_getprotobyname(const char *name)
{
workworkwork();
if (error)
return -1;
return workresult;
}
Hi,
I will do (in version2).
Thanks!
Thomas
Pablo Neira Ayuso
maintainerHi Thomas,
Quoted 2 lines · Thomas Haller
If the reentrant versions of the functions are available, use them so that libnftables is thread-safe in this regard.
At netlink sequence tracking is not thread-safe, users hit EILSEQ errors when multiple threads recycle the same nft_ctx object. Updates are serialized by mutex per netns, batching is usually the way to go to amortize the cost of ruleset updates.
Are you planning to have a user of libnftables that is multi-thread?
Quoted 294 lines
Signed-off-by Thomas Haller <[email protected]>--- configure.ac | 4 +++ include/utils.h | 4 +++ src/datatype.c | 29 +++++++--------- src/json.c | 21 ++++++------ src/rule.c | 6 ++-- src/utils.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 123 insertions(+), 31 deletions(-)configure.ac +4
-0@@ -108,6 +108,10 @@ AC_DEFINE([HAVE_LIBJANSSON], [1], [Define if you have libjansson]) ]) AM_CONDITIONAL([BUILD_JSON], [test "x$with_json" != xno]) +AC_CHECK_DECLS([getprotobyname_r, getprotobynumber_r, getservbyport_r], [], [], [[ +#include <netdb.h> +]]) + AC_CONFIG_FILES([ \ Makefile \ libnftables.pc \include/utils.h +4
-0@@ -138,4 +138,8 @@ extern char *xstrdup(const char *s);extern void xstrunescape (const char *in,char *out);extern int round_pow_2 (unsigned int value); +bool nft_getprotobynumber (int number,char *out_name,size_t name_len); +bool nft_getprotobyname (const char *name,uint8_t *out_proto); +bool nft_getservbyport (int port,const char *proto,char *out_name,size_t name_len); +#endif /* NFTABLES_UTILS_H */ src/datatype.c +12
-17@@ -697,12 +697,11 @@ const struct datatype ip6addr_type = {static void inet_protocol_type_print (const struct expr *expr,struct output_ctx *octx) { -struct protoent *p; -if (!nft_output_numeric_proto (octx)) { - p =getprotobynumber (mpz_get_uint8 (expr->value)); -if (p !=NULL ) { -nft_print (octx,"%s" , p->p_name); +char name[1024 ]; + +if (nft_getprotobynumber (mpz_get_uint8 (expr->value), name,sizeof (name))) { +nft_print (octx,"%s" , name);return ; } }@@ -711,15 +710,15 @@ static void inet_protocol_type_print(const struct expr *expr,static void inet_protocol_type_describe (struct output_ctx *octx) { -struct protoent *p;uint8_t protonum;for (protonum =0 ; protonum <UINT8_MAX ; protonum++) { - p =getprotobynumber (protonum); -if (!p) +char name[1024 ]; + +if (!nft_getprotobynumber (protonum, name,sizeof (name)))continue ; -nft_print (octx,"\t%-30s\t%u\n" , p->p_name, protonum); +nft_print (octx,"\t%-30s\t%u\n" , name, protonum); } }@@ -727,7 +726,6 @@ static struct error_record *inet_protocol_type_parse(struct parse_ctx *ctx,const struct expr *sym, struct expr **res) { - struct protoent *p; uint8_t proto;uintmax_t i;char *end;@@ -740,11 +738,8 @@ static struct error_record *inet_protocol_type_parse(struct parse_ctx *ctx, proto = i; }else { - p =getprotobyname (sym->identifier); -if (p ==NULL ) +if (!nft_getprotobyname (sym->identifier, &proto))return error (&sym->location,"Could not resolve protocol name" ); - - proto = p->p_proto; } *res = constant_expr_alloc(&sym->location, &inet_protocol_type,@@ -768,12 +763,12 @@ const struct datatype inet_protocol_type = {static void inet_service_print (const struct expr *expr,struct output_ctx *octx) {uint16_t port =mpz_get_be16 (expr->value); -const struct servent *s =getservbyport (port,NULL ); +char name[1024 ]; -if (s ==NULL ) +if (!nft_getservbyport (port,NULL , name,sizeof (name)))nft_print (octx,"%hu" ,ntohs (port));else -nft_print (octx,"\"%s\"" , s->s_name); +nft_print (octx,"\"%s\"" , name); }void inet_service_type_print (const struct expr *expr,struct output_ctx *octx)src/json.c +10
-11@@ -297,10 +297,10 @@ static json_t *chain_print_json(const struct chain *chain)static json_t *proto_name_json (uint8_t proto) { -const struct protoent *p =getprotobynumber (proto); +char name[1024 ]; -if (p) -return json_string (p->p_name); +if (nft_getprotobynumber (proto, name,sizeof (name))) +return json_string (name);return json_integer (proto); }@@ -1093,12 +1093,11 @@ json_t *boolean_type_json(const struct expr *expr, struct output_ctx *octx)json_t *inet_protocol_type_json (const struct expr *expr,struct output_ctx *octx) { -struct protoent *p; -if (!nft_output_numeric_proto (octx)) { - p =getprotobynumber (mpz_get_uint8 (expr->value)); -if (p !=NULL ) -return json_string (p->p_name); +char name[1024 ]; + +if (nft_getprotobynumber (mpz_get_uint8 (expr->value), name,sizeof (name))) +return json_string (name); }return integer_type_json (expr, octx); }@@ -1106,13 +1105,13 @@ json_t *inet_protocol_type_json(const struct expr *expr,json_t *inet_service_type_json (const struct expr *expr,struct output_ctx *octx) {uint16_t port =mpz_get_be16 (expr->value); -const struct servent *s =NULL ; +char name[1024 ];if (!nft_output_service (octx) || - (s =getservbyport (port,NULL )) ==NULL ) + !nft_getservbyport (port,NULL , name,sizeof (name)))return json_integer (ntohs (port)); -return json_string (s->s_name); +return json_string (name); }json_t *mark_type_json (const struct expr *expr,struct output_ctx *octx)src/rule.c +3
-3@@ -1666,10 +1666,10 @@ struct obj *obj_lookup_fuzzy(const char *obj_name,static void print_proto_name_proto (uint8_t l4,struct output_ctx *octx) { -const struct protoent *p =getprotobynumber (l4); +char name[1024 ]; -if (p) -nft_print (octx,"%s" , p->p_name); +if (nft_getprotobynumber (l4, name,sizeof (name))) +nft_print (octx,"%s" , name);else nft_print (octx,"%d" , l4); }src/utils.c +90
-0@@ -14,6 +14,7 @@#include <stdio.h> #include <unistd.h> #include <string.h> +#include <netdb.h> #include <nftables.h> #include <utils.h> @@ -105,3 +106,92 @@ int round_pow_2(unsigned int n) {return 1UL <<fls (n -1 ); } + +bool nft_getprotobynumber (int proto,char *out_name,size_t name_len) +{ +const struct protoent *result; + +#if HAVE_DECL_GETPROTOBYNUMBER_R +struct protoent result_buf; +char buf[2048 ]; +int r; + + r =getprotobynumber_r (proto, + &result_buf, + buf, +sizeof (buf), + (struct protoent **) &result); +if (r !=0 || result != &result_buf) + result =NULL ; +#else + result =getprotobynumber (proto); +#endif + +if (!result) +return false; + +if (strlen (result->p_name) >= name_len) +return false; +strcpy (out_name, result->p_name); +return true; +} + +bool nft_getprotobyname (const char *name,uint8_t *out_proto) +{ +const struct protoent *result; + +#if HAVE_DECL_GETPROTOBYNAME_R +struct protoent result_buf; +char buf[2048 ]; +int r; + + r =getprotobyname_r (name, + &result_buf, + buf, +sizeof (buf), + (struct protoent **) &result); +if (r !=0 || result != &result_buf) + result =NULL ; +#else + result =getprotobyname (name); +#endif + +if (!result) +return false; + +if (result->p_proto <0 || result->p_proto >UINT8_MAX ) +return false; +if (out_proto) + *out_proto = result->p_proto; +return true; +} + +bool nft_getservbyport (int port,const char *proto,char *out_name,size_t name_len) +{ +const struct servent *result; + +#if HAVE_DECL_GETSERVBYPORT_R +struct servent result_buf; +char buf[2048 ]; +int r; + + r =getservbyport_r (port, + proto, + &result_buf, + buf, +sizeof (buf), + (struct servent **) &result); +if (r !=0 || result != &result_buf) + result =NULL ; +#else + result =getservbyport (port, proto); +#endif + +if (!result) +return false; + +if (strlen (result->s_name) >= name_len) +return false; +strcpy (out_name, result->s_name); +return true; +}Signature · 1 line
2.41.0
Hi Pablo,
Quoted 9 lines · Pablo Neira Ayuso
Quoted 3 lines · Thomas Haller
If the reentrant versions of the functions are available, use them so
that libnftables is thread-safe in this regard.At netlink sequence tracking is not thread-safe, users hit EILSEQ errors when multiple threads recycle the same
nft_ctxobject. Updates are serialized by mutex per netns, batching is usually the way to go to amortize the cost of ruleset updates.
The problem already happens when one thread is using libnftables and another thread calls one of those libc functions at an unfortunate moment. It doesn't require multi-threaded uses of libnftables itself.
Also, why couldn't you have two threads, handling one netns each, with separate nft_ctx objects?
Quoted 1 line
Are you planning to have a user of libnftables that is multi-thread?
No, I don't :) I was just interested in this topic.
Thomas
Pablo Neira Ayuso
maintainerHi Thomas,
Apologies for the late reply.
Quoted 8 lines · Thomas Haller
Hi Pablo,
Quoted 5 lines · Pablo Neira Ayuso
Quoted 3 lines · Thomas Haller
If the reentrant versions of the functions are available, use them so
that libnftables is thread-safe in this regard.
Quoted 8 lines
Quoted 4 lines
At netlink sequence tracking is not thread-safe, users hit EILSEQ errors when multiple threads recycle the same
nft_ctxobject. Updates are serialized by mutex per netns, batching is usually the way to go to amortize the cost of ruleset updates.The problem already happens when one thread is using libnftables and another thread calls one of those libc functions at an unfortunate moment. It doesn't require multi-threaded uses of libnftables itself.
Indeed.
Quoted 2 lines
Also, why couldn't you have two threads, handling one netns each, with separate
nft_ctxobjects?
You have to have one nft_ctx per thread, that should be sufficient, this probably needs to be documented.
PATCH v2 1/1 src: use reentrant getprotobyname_r()/getprotobynumber_r()/getservbyport_r()
In reply to Thomas HallerIf the reentrant versions of the functions are available, use them so that libnftables is thread-safe in this regard.
---
Changes to v1:
- have nft_getprotobyname() return a negative integer on error or the non-negative port number.
configure.ac | 4 +++ include/utils.h | 4 +++ src/datatype.c | 32 +++++++++--------- src/json.c | 21 ++++++------ src/rule.c | 6 ++-- src/utils.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 125 insertions(+), 30 deletions(-)
configure.ac +4 -0
@@ -108,6 +108,10 @@ AC_DEFINE([HAVE_LIBJANSSON], [1], [Define if you have libjansson]) ]) AM_CONDITIONAL([BUILD_JSON], [test "x$with_json" != xno]) +AC_CHECK_DECLS([getprotobyname_r, getprotobynumber_r, getservbyport_r], [], [], [[ +#include <netdb.h> +]]) + AC_CONFIG_FILES([ \ Makefile \ libnftables.pc \
include/utils.h +4 -0
@@ -138,4 +138,8 @@ extern char *xstrdup(const char *s);extern void xstrunescape (const char *in,char *out);extern int round_pow_2 (unsigned int value); +bool nft_getprotobynumber (int number,char *out_name,size_t name_len); +int nft_getprotobyname (const char *name); +bool nft_getservbyport (int port,const char *proto,char *out_name,size_t name_len); +#endif /* NFTABLES_UTILS_H */
src/datatype.c +16 -16
@@ -697,12 +697,11 @@ const struct datatype ip6addr_type = {static void inet_protocol_type_print (const struct expr *expr,struct output_ctx *octx) { -struct protoent *p; -if (!nft_output_numeric_proto (octx)) { - p =getprotobynumber (mpz_get_uint8 (expr->value)); -if (p !=NULL ) { -nft_print (octx,"%s" , p->p_name); +char name[1024 ]; + +if (nft_getprotobynumber (mpz_get_uint8 (expr->value), name,sizeof (name))) { +nft_print (octx,"%s" , name);return ; } }
@@ -711,15 +710,15 @@ static void inet_protocol_type_print(const struct expr *expr,static void inet_protocol_type_describe (struct output_ctx *octx) { -struct protoent *p;uint8_t protonum;for (protonum =0 ; protonum <UINT8_MAX ; protonum++) { - p =getprotobynumber (protonum); -if (!p) +char name[1024 ]; + +if (!nft_getprotobynumber (protonum, name,sizeof (name)))continue ; -nft_print (octx,"\t%-30s\t%u\n" , p->p_name, protonum); +nft_print (octx,"\t%-30s\t%u\n" , name, protonum); } }
@@ -727,7 +726,6 @@ static struct error_record *inet_protocol_type_parse(struct parse_ctx *ctx,const struct expr *sym, struct expr **res) { - struct protoent *p; uint8_t proto;uintmax_t i;char *end;
@@ -740,11 +738,13 @@ static struct error_record *inet_protocol_type_parse(struct parse_ctx *ctx, proto = i; }else { - p =getprotobyname (sym->identifier); -if (p ==NULL ) +int r; + + r =nft_getprotobyname (sym->identifier); +if (r <0 )return error (&sym->location,"Could not resolve protocol name" ); - proto = p->p_proto; + proto = r; } *res = constant_expr_alloc(&sym->location, &inet_protocol_type,
@@ -768,12 +768,12 @@ const struct datatype inet_protocol_type = {static void inet_service_print (const struct expr *expr,struct output_ctx *octx) {uint16_t port =mpz_get_be16 (expr->value); -const struct servent *s =getservbyport (port,NULL ); +char name[1024 ]; -if (s ==NULL ) +if (!nft_getservbyport (port,NULL , name,sizeof (name)))nft_print (octx,"%hu" ,ntohs (port));else -nft_print (octx,"\"%s\"" , s->s_name); +nft_print (octx,"\"%s\"" , name); }void inet_service_type_print (const struct expr *expr,struct output_ctx *octx)
src/json.c +10 -11
@@ -297,10 +297,10 @@ static json_t *chain_print_json(const struct chain *chain)static json_t *proto_name_json (uint8_t proto) { -const struct protoent *p =getprotobynumber (proto); +char name[1024 ]; -if (p) -return json_string (p->p_name); +if (nft_getprotobynumber (proto, name,sizeof (name))) +return json_string (name);return json_integer (proto); }
@@ -1093,12 +1093,11 @@ json_t *boolean_type_json(const struct expr *expr, struct output_ctx *octx)json_t *inet_protocol_type_json (const struct expr *expr,struct output_ctx *octx) { -struct protoent *p; -if (!nft_output_numeric_proto (octx)) { - p =getprotobynumber (mpz_get_uint8 (expr->value)); -if (p !=NULL ) -return json_string (p->p_name); +char name[1024 ]; + +if (nft_getprotobynumber (mpz_get_uint8 (expr->value), name,sizeof (name))) +return json_string (name); }return integer_type_json (expr, octx); }
@@ -1106,13 +1105,13 @@ json_t *inet_protocol_type_json(const struct expr *expr,json_t *inet_service_type_json (const struct expr *expr,struct output_ctx *octx) {uint16_t port =mpz_get_be16 (expr->value); -const struct servent *s =NULL ; +char name[1024 ];if (!nft_output_service (octx) || - (s =getservbyport (port,NULL )) ==NULL ) + !nft_getservbyport (port,NULL , name,sizeof (name)))return json_integer (ntohs (port)); -return json_string (s->s_name); +return json_string (name); }json_t *mark_type_json (const struct expr *expr,struct output_ctx *octx)
src/rule.c +3 -3
@@ -1666,10 +1666,10 @@ struct obj *obj_lookup_fuzzy(const char *obj_name,static void print_proto_name_proto (uint8_t l4,struct output_ctx *octx) { -const struct protoent *p =getprotobynumber (l4); +char name[1024 ]; -if (p) -nft_print (octx,"%s" , p->p_name); +if (nft_getprotobynumber (l4, name,sizeof (name))) +nft_print (octx,"%s" , name);else nft_print (octx,"%d" , l4); }
src/utils.c +88 -0
97 lines not on this page · raw message
Signature · 1 line
2.41.0
Pablo Neira Ayuso
maintainerHi Tomas,
A few pedantic comments of mine.
Quoted 51 lines · Thomas Haller
If the reentrant versions of the functions are available, use them so that libnftables is thread-safe in this regard.
Signed-off-by Thomas Haller <[email protected]>---
Changes to v1:- have
nft_getprotobyname()return a negative integer on error or the non-negative port number.configure.ac | 4 +++ include/utils.h | 4 +++ src/datatype.c | 32 +++++++++--------- src/json.c | 21 ++++++------ src/rule.c | 6 ++-- src/utils.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 125 insertions(+), 30 deletions(-)configure.ac +4
-0@@ -108,6 +108,10 @@ AC_DEFINE([HAVE_LIBJANSSON], [1], [Define if you have libjansson]) ]) AM_CONDITIONAL([BUILD_JSON], [test "x$with_json" != xno]) +AC_CHECK_DECLS([getprotobyname_r, getprotobynumber_r, getservbyport_r], [], [], [[ +#include <netdb.h> +]]) + AC_CONFIG_FILES([ \ Makefile \ libnftables.pc \include/utils.h +4
-0@@ -138,4 +138,8 @@ extern char *xstrdup(const char *s);extern void xstrunescape (const char *in,char *out);extern int round_pow_2 (unsigned int value); +bool nft_getprotobynumber (int number,char *out_name,size_t name_len); +int nft_getprotobyname (const char *name); +bool nft_getservbyport (int port,const char *proto,char *out_name,size_t name_len); +#endif /* NFTABLES_UTILS_H */ src/datatype.c +0
-0
src/datatype.c const struct datatype ip6addr_type = { · line 701
Quoted 10 lines
src/datatype.c +1
-5@@ -697,12 +697,11 @@ const struct datatype ip6addr_type = {static void inet_protocol_type_print (const struct expr *expr,struct output_ctx *octx) { -struct protoent *p; -if (!nft_output_numeric_proto (octx)) { - p =getprotobynumber (mpz_get_uint8 (expr->value)); -if (p !=NULL ) { -nft_print (octx,"%s" , p->p_name); +char name[1024 ];
Is there any definition that could be used instead of 1024. Same comment for all other hardcoded buffers. Or maybe add a definition for this?
Quoted 150 lines
src/datatype.c +15
-11+ +if (nft_getprotobynumber (mpz_get_uint8 (expr->value), name,sizeof (name))) { +nft_print (octx,"%s" , name);return ; } }@@ -711,15 +710,15 @@ static void inet_protocol_type_print(const struct expr *expr,static void inet_protocol_type_describe (struct output_ctx *octx) { -struct protoent *p;uint8_t protonum;for (protonum =0 ; protonum <UINT8_MAX ; protonum++) { - p =getprotobynumber (protonum); -if (!p) +char name[1024 ]; + +if (!nft_getprotobynumber (protonum, name,sizeof (name)))continue ; -nft_print (octx,"\t%-30s\t%u\n" , p->p_name, protonum); +nft_print (octx,"\t%-30s\t%u\n" , name, protonum); } }@@ -727,7 +726,6 @@ static struct error_record *inet_protocol_type_parse(struct parse_ctx *ctx,const struct expr *sym, struct expr **res) { - struct protoent *p; uint8_t proto;uintmax_t i;char *end;@@ -740,11 +738,13 @@ static struct error_record *inet_protocol_type_parse(struct parse_ctx *ctx, proto = i; }else { - p =getprotobyname (sym->identifier); -if (p ==NULL ) +int r; + + r =nft_getprotobyname (sym->identifier); +if (r <0 )return error (&sym->location,"Could not resolve protocol name" ); - proto = p->p_proto; + proto = r; } *res = constant_expr_alloc(&sym->location, &inet_protocol_type,@@ -768,12 +768,12 @@ const struct datatype inet_protocol_type = {static void inet_service_print (const struct expr *expr,struct output_ctx *octx) {uint16_t port =mpz_get_be16 (expr->value); -const struct servent *s =getservbyport (port,NULL ); +char name[1024 ]; -if (s ==NULL ) +if (!nft_getservbyport (port,NULL , name,sizeof (name)))nft_print (octx,"%hu" ,ntohs (port));else -nft_print (octx,"\"%s\"" , s->s_name); +nft_print (octx,"\"%s\"" , name); }void inet_service_type_print (const struct expr *expr,struct output_ctx *octx)src/json.c +10
-11@@ -297,10 +297,10 @@ static json_t *chain_print_json(const struct chain *chain)static json_t *proto_name_json (uint8_t proto) { -const struct protoent *p =getprotobynumber (proto); +char name[1024 ]; -if (p) -return json_string (p->p_name); +if (nft_getprotobynumber (proto, name,sizeof (name))) +return json_string (name);return json_integer (proto); }@@ -1093,12 +1093,11 @@ json_t *boolean_type_json(const struct expr *expr, struct output_ctx *octx)json_t *inet_protocol_type_json (const struct expr *expr,struct output_ctx *octx) { -struct protoent *p; -if (!nft_output_numeric_proto (octx)) { - p =getprotobynumber (mpz_get_uint8 (expr->value)); -if (p !=NULL ) -return json_string (p->p_name); +char name[1024 ]; + +if (nft_getprotobynumber (mpz_get_uint8 (expr->value), name,sizeof (name))) +return json_string (name); }return integer_type_json (expr, octx); }@@ -1106,13 +1105,13 @@ json_t *inet_protocol_type_json(const struct expr *expr,json_t *inet_service_type_json (const struct expr *expr,struct output_ctx *octx) {uint16_t port =mpz_get_be16 (expr->value); -const struct servent *s =NULL ; +char name[1024 ];if (!nft_output_service (octx) || - (s =getservbyport (port,NULL )) ==NULL ) + !nft_getservbyport (port,NULL , name,sizeof (name)))return json_integer (ntohs (port)); -return json_string (s->s_name); +return json_string (name); }json_t *mark_type_json (const struct expr *expr,struct output_ctx *octx)src/rule.c +3
-3@@ -1666,10 +1666,10 @@ struct obj *obj_lookup_fuzzy(const char *obj_name,static void print_proto_name_proto (uint8_t l4,struct output_ctx *octx) { -const struct protoent *p =getprotobynumber (l4); +char name[1024 ]; -if (p) -nft_print (octx,"%s" , p->p_name); +if (nft_getprotobynumber (l4, name,sizeof (name))) +nft_print (octx,"%s" , name);else nft_print (octx,"%d" , l4); }src/utils.c +1
-0@@ -14,6 +14,7 @@#include <stdio.h> #include <unistd.h> #include <string.h> +#include <netdb.h> #include <nftables.h> #include <utils.h>
src/utils.c int round_pow_2(unsigned int n) · line 109
Quoted 4 lines
src/utils.c +1
-0@@ -105,3 +106,90 @@ int round_pow_2(unsigned int n) {return 1UL <<fls (n -1 ); } +
Could you move this new code to a new file instead of utils.c? We are slowing moving towards GPLv2 or any later for new code. Probably netdb.c or pick a better name that you like.
Quoted 9 lines
src/utils.c +9
-0+bool nft_getprotobynumber (int proto,char *out_name,size_t name_len) +{ +const struct protoent *result; + +#if HAVE_DECL_GETPROTOBYNUMBER_R +struct protoent result_buf; +char buf[2048 ]; +int r; +
src/utils.c int round_pow_2(unsigned int n) · line 128
Quoted 10 lines
src/utils.c +10
-0+ r =getprotobynumber_r (proto, + &result_buf, + buf, +sizeof (buf), + (struct protoent **) &result); +if (r !=0 || result != &result_buf) + result =NULL ; +#else + result =getprotobynumber (proto); +#endif
I'd suggest wrap this code with #ifdef's in a helper function.
Quoted 19 lines
src/utils.c +19
-0+ +if (!result) +return false; + +if (strlen (result->p_name) >= name_len) +return false; +strcpy (out_name, result->p_name); +return true; +} + +int nft_getprotobyname (const char *name) +{ +const struct protoent *result; + +#if HAVE_DECL_GETPROTOBYNAME_R +struct protoent result_buf; +char buf[2048 ]; +int r; +
src/utils.c int round_pow_2(unsigned int n) · line 157
Quoted 10 lines
src/utils.c +10
-0+ r =getprotobyname_r (name, + &result_buf, + buf, +sizeof (buf), + (struct protoent **) &result); +if (r !=0 || result != &result_buf) + result =NULL ; +#else + result =getprotobyname (name); +#endif
same here.
Quoted 19 lines
src/utils.c +19
-0+ +if (!result) +return -1 ; + +if (result->p_proto <0 || result->p_proto >UINT8_MAX ) +return -1 ; +return (uint8_t ) result->p_proto; +} + +bool nft_getservbyport (int port,const char *proto,char *out_name,size_t name_len) +{ +const struct servent *result; + +#if HAVE_DECL_GETSERVBYPORT_R +struct servent result_buf; +char buf[2048 ]; +int r; + + r = getservbyport_r(port,
src/utils.c int round_pow_2(unsigned int n) · line 186
Quoted 10 lines
src/utils.c +10
-0+ proto, + &result_buf, + buf, +sizeof (buf), + (struct servent**) &result); +if (r !=0 || result != &result_buf) + result =NULL ; +#else + result =getservbyport (port, proto); +#endif
same here.
Quoted 12 lines
src/utils.c +9
-0+ +if (!result) +return false; + +if (strlen (result->s_name) >= name_len) +return false; +strcpy (out_name, result->s_name); +return true; +}Signature · 1 line
2.41.0
Hi Pablo,
Quoted 9 lines · Pablo Neira Ayuso
Quoted 8 lines
- struct protoent *p;
-
if (!nft_output_numeric_proto(octx)) {
- p = getprotobynumber(mpz_get_uint8(expr->value)); - if (p != NULL) {
- nft_print(octx, "%s",p->p_name); + char name[1024];
Quoted 4 lines
Is there any definition that could be used instead of 1024. Same comment for all other hardcoded buffers. Or maybe add a definition for
this?
Added defines instead. See v3.
[...]
Quoted 11 lines
src/utils.c int round_pow_2(unsigned int n) · line 109
Quoted 7 lines
#include <
nftables.h>
#include <utils.h>src/utils.c +1
-0@@ -105,3 +106,90 @@ int round_pow_2(unsigned int n) { return 1UL << fls(n - 1); } +Could you move this new code to a new file instead of
utils.c? We are slowing moving towards GPLv2 or any later for new code. Probablynetdb.cor pick a better name that you like.
This request leaves me with a lot of choices. I made them, but I guess you will have something to say about it. See v3.
Quoted 15 lines
Quoted 15 lines
(quoted diff) +1
-0+bool nft_getprotobynumber(int proto, char *out_name, size_t
name_len)src/utils.c +12
-0+{ + const struct protoent *result; + +#if HAVE_DECL_GETPROTOBYNUMBER_R + struct protoent result_buf; + char buf[2048]; + int r; + + r = getprotobynumber_r(proto, + &result_buf, + buf, + sizeof(buf),
Quoted 8 lines
src/utils.c int round_pow_2(unsigned int n) · line 128
Quoted 6 lines
+ (struct protoent **) &result); + if (r != 0 || result !=
&result_buf)
+ result = NULL;src/utils.c +3
-0+#else + result = getprotobynumber(proto); +#endif I'd suggest wrap this code with #ifdef's in a helper function.
I don't understand. nft_getprotobynumber() *is* that helper function to wrap the #if. This point is not addressed by v3 (??).
thanks,
Thomas
Pablo Neira Ayuso
maintainerQuoted 60 lines · Thomas Haller
Hi Pablo,
Quoted 9 lines · Pablo Neira Ayuso
Quoted 8 lines
- struct protoent *p;
-
if (!nft_output_numeric_proto(octx)) {
- p = getprotobynumber(mpz_get_uint8(expr->value)); - if (p != NULL) {
- nft_print(octx, "%s",p->p_name); + char name[1024];Quoted 4 lines
Is there any definition that could be used instead of 1024. Same comment for all other hardcoded buffers. Or maybe add a definition for
this?Added defines instead. See v3.
[...]
Quoted 11 lines
src/utils.c int round_pow_2(unsigned int n) · line 109
Quoted 7 lines
#include <
nftables.h>
#include <utils.h>src/utils.c +1
-0@@ -105,3 +106,90 @@ int round_pow_2(unsigned int n) { return 1UL << fls(n - 1); } +Could you move this new code to a new file instead of
utils.c? We are slowing moving towards GPLv2 or any later for new code. Probablynetdb.cor pick a better name that you like.This request leaves me with a lot of choices. I made them, but I guess you will have something to say about it. See v3.
Quoted 15 lines
Quoted 15 lines
(quoted diff) +1
-0+bool nft_getprotobynumber(int proto, char *out_name, size_t
name_len)src/utils.c +12
-0+{ + const struct protoent *result; + +#if HAVE_DECL_GETPROTOBYNUMBER_R + struct protoent result_buf; + char buf[2048]; + int r; + + r = getprotobynumber_r(proto, + &result_buf, + buf, + sizeof(buf),Quoted 8 lines
src/utils.c int round_pow_2(unsigned int n) · line 128
Quoted 6 lines
+ (struct protoent **) &result); + if (r != 0 || result !=
&result_buf)
+ result = NULL;src/utils.c +3
-0+#else + result = getprotobynumber(proto); +#endif I'd suggest wrap this code with #ifdef's in a helper function.
Quoted 2 lines
I don't understand.
nft_getprotobynumber()*is* that helper function to wrap the #if. This point is not addressed by v3 (??).
I mean, something like a smaller function:
static struct __nft_getprotobynumber(...)
that is wraps this code above and it returns const struct protoent. This helper function is called from nft_getprotobynumber().
But it is fine as it is, this is just a bit of bike shedding.
Pablo Neira Ayuso
maintainerQuoted 61 lines · Pablo Neira Ayuso
Quoted 60 lines · Thomas Haller
Hi Pablo,
Quoted 9 lines · Pablo Neira Ayuso
Quoted 8 lines
- struct protoent *p;
-
if (!nft_output_numeric_proto(octx)) {
- p = getprotobynumber(mpz_get_uint8(expr->value)); - if (p != NULL) {
- nft_print(octx, "%s",p->p_name); + char name[1024];Quoted 4 lines
Is there any definition that could be used instead of 1024. Same comment for all other hardcoded buffers. Or maybe add a definition for
this?Added defines instead. See v3.
[...]
Quoted 11 lines
src/utils.c int round_pow_2(unsigned int n) · line 109
Quoted 7 lines
#include <
nftables.h>
#include <utils.h>src/utils.c +1
-0@@ -105,3 +106,90 @@ int round_pow_2(unsigned int n) { return 1UL << fls(n - 1); } +Could you move this new code to a new file instead of
utils.c? We are slowing moving towards GPLv2 or any later for new code. Probablynetdb.cor pick a better name that you like.This request leaves me with a lot of choices. I made them, but I guess you will have something to say about it. See v3.
Quoted 23 lines
Quoted 11 lines
(quoted diff) +1
-0+bool nft_getprotobynumber(int proto, char *out_name, size_t
name_len)src/utils.c +8
-0+{ + const struct protoent *result; + +#if HAVE_DECL_GETPROTOBYNUMBER_R + struct protoent result_buf; + char buf[2048]; + int r; +src/utils.c int round_pow_2(unsigned int n) · line 128
Quoted 10 lines
src/utils.c +10
-0+ r = getprotobynumber_r(proto, + &result_buf, + buf, + sizeof(buf), + (struct protoent **) &result); + if (r != 0 || result != &result_buf) + result = NULL; +#else + result = getprotobynumber(proto); +#endif I'd suggest wrap this code with #ifdef's in a helper function.
Quoted 6 lines
Quoted 2 lines
I don't understand.
nft_getprotobynumber()*is* that helper function to wrap the #if. This point is not addressed by v3 (??).I mean, something like a smaller function:
static struct
__nft_getprotobynumber(...)
static const struct protoent *__nft_getprotobynumber(...)
Quoted 4 lines
that is wraps this code above and it returns const struct protoent. This helper function is called from
nft_getprotobynumber().But it is fine as it is, this is just a bit of bike shedding.
Quoted 69 lines · Pablo Neira Ayuso
Quoted 64 lines · Thomas Haller
Hi Pablo,
Quoted 10 lines · Pablo Neira Ayuso
Quoted 9 lines
- struct protoent *p;
-
if (!nft_output_numeric_proto(octx)) { - p = getprotobynumber(mpz_get_uint8(expr-Quoted 1 line
value));
- if (p != NULL) {
- nft_print(octx, "%s",p->p_name); + char name[1024];Quoted 5 lines
Is there any definition that could be used instead of 1024. Same comment for all other hardcoded buffers. Or maybe add a definition
for
this?Added defines instead. See v3.
[...]
Quoted 4 lines
Quoted 4 lines
#include <
nftables.h>
#include <utils.h>(quoted diff) +0
-0@@ -105,3 +106,90 @@ int round_pow_2(unsigned int n) {Quoted 8 lines
src/utils.c int round_pow_2(unsigned int n) · line 109
Quoted 3 lines
src/utils.c +1
-0return 1UL << fls(n - 1); } +Could you move this new code to a new file instead of
utils.c? We are
slowing moving towards GPLv2 or any later for new code. Probablynetdb.cor pick a better name that you like.This request leaves me with a lot of choices. I made them, but I guess
you will have something to say about it. See v3.Quoted 15 lines
Quoted 15 lines
(quoted diff) +1
-0+bool nft_getprotobynumber(int proto, char *out_name, size_t
name_len)src/utils.c +12
-0+{ + const struct protoent *result; + +#if HAVE_DECL_GETPROTOBYNUMBER_R + struct protoent result_buf; + char buf[2048]; + int r; + + r = getprotobynumber_r(proto, + &result_buf, + buf, + sizeof(buf),Quoted 8 lines
src/utils.c int round_pow_2(unsigned int n) · line 128
Quoted 6 lines
+ (struct protoent **) &result); + if (r != 0 || result !=
&result_buf)
+ result = NULL;src/utils.c +3
-0+#else + result = getprotobynumber(proto); +#endif I'd suggest wrap this code with #ifdef's in a helper function.
Quoted 3 lines
I don't understand.
nft_getprotobynumber()*is* that helper function to
wrap the #if. This point is not addressed by v3 (??).
Quoted 9 lines
I mean, something like a smaller function:
static struct
__nft_getprotobynumber(...)that is wraps this code above and it returns const struct protoent. This helper function is called from
nft_getprotobynumber().But it is fine as it is, this is just a bit of bike shedding.
I see. Yes, possible.
I don't find that very useful, because the idea is that nft_getprotobyname() etc. wraps the underlying libc functions and contains the #if parts. And there shall be no other place that directly call getprotobyname()/getprotobyname_r(). So __nft_getprotobynumber() would be a static function with only one caller (and no plan to ever have more).
Thomas