datatype: Display pre-defined inet_service values in host byte order
PATCH v2 1/1 datatype: Display pre-defined inet_service values in host byte order
nft describe displays, to the user, which values are available for a selector, then the values should be in host byte order.
---
v2: Created a function to convert different types and number of bytes
include/datatype.h | 3 ++- src/datatype.c | 27 ++++++++++++++++++++++++--- src/expression.c | 3 ++- 3 files changed, 28 insertions(+), 5 deletions(-)
include/datatype.h +2 -1
@@ -191,7 +191,8 @@ extern struct error_record *symbolic_constant_parse(const struct expr *sym,extern void symbolic_constant_print (const struct symbol_table *tbl,const struct expr *expr,bool quotes);extern void symbol_table_print (const struct symbol_table *tbl, -const struct datatype *dtype); +const struct datatype *dtype, +enum byteorder byteorder);extern struct symbol_table *rt_symbol_table_init (const char *filename);extern void rt_symbol_table_free (struct symbol_table *tbl);
src/datatype.c +24 -3
@@ -180,15 +180,36 @@ void symbolic_constant_print(const struct symbol_table *tbl,printf ("%s" , s->identifier); } +static void big_to_host_byteorder (void *value,const unsigned int nbytes) +{ +unsigned char *p = value; +unsigned int i; + +for (i =0 ; i < nbytes /2 ; i++) { + p[i] ^= p[nbytes -1 - i]; + p[nbytes -1 - i] ^= p[i]; + p[i] ^= p[nbytes -1 - i]; + } +} +void symbol_table_print (const struct symbol_table *tbl, -const struct datatype *dtype) +const struct datatype *dtype, +enum byteorder byteorder) {const struct symbolic_constant *s;unsigned int size =2 * dtype->size /BITS_PER_BYTE ; +unsigned long int value; + +for (s = tbl->symbols; s->identifier !=NULL ; s++) { + value = s->value; + +if (byteorder ==BYTEORDER_BIG_ENDIAN ) +big_to_host_byteorder (&value, + dtype->size /BITS_PER_BYTE ); -for (s = tbl->symbols; s->identifier !=NULL ; s++)printf ("\t%-30s\t0x%.*" PRIx64"\n" , - s->identifier, size, s->value); + s->identifier, size, value); + } }static void invalid_type_print (const struct expr *expr)
src/expression.c +2 -1
@@ -115,7 +115,8 @@ void expr_describe(const struct expr *expr)if (expr->dtype->sym_tbl !=NULL ) {printf ("\npre-defined symbolic constants:\n" ); -symbol_table_print (expr->dtype->sym_tbl, expr->dtype); +symbol_table_print (expr->dtype->sym_tbl, expr->dtype, + expr->byteorder); } }
Signature · 1 line
2.7.4
Pablo Neira Ayuso
maintainerHi Elise,
Quoted 37 lines · Elise Lennion
nft describe displays, to the user, which values are available for a selector, then the values should be in host byte order.
Reported-by Pablo Neira Ayuso <[email protected]>Fixes ccc5da470e76 ("datatype: Replace getnameinfo() by internal lookup table")Signed-off-by Elise Lennion <[email protected]>---
v2: Created a function to convert different types and number of bytes
include/datatype.h | 3 ++- src/datatype.c | 27 ++++++++++++++++++++++++--- src/expression.c | 3 ++- 3 files changed, 28 insertions(+), 5 deletions(-)include/datatype.h +2
-1@@ -191,7 +191,8 @@ extern struct error_record *symbolic_constant_parse(const struct expr *sym,extern void symbolic_constant_print (const struct symbol_table *tbl,const struct expr *expr,bool quotes);extern void symbol_table_print (const struct symbol_table *tbl, -const struct datatype *dtype); +const struct datatype *dtype, +enum byteorder byteorder);extern struct symbol_table *rt_symbol_table_init (const char *filename);extern void rt_symbol_table_free (struct symbol_table *tbl);src/datatype.c +0
-0@@ -180,15 +180,36 @@ void symbolic_constant_print(const struct symbol_table *tbl,printf ("%s" , s->identifier); }
src/datatype.c void symbolic_constant_print(const struct symbol_table *tbl, · line 192
Quoted 10 lines
src/datatype.c +10
-0+static void big_to_host_byteorder (void *value,const unsigned int nbytes) +{ +unsigned char *p = value; +unsigned int i; + +for (i =0 ; i < nbytes /2 ; i++) { + p[i] ^= p[nbytes -1 - i]; + p[nbytes -1 - i] ^= p[i]; + p[i] ^= p[nbytes -1 - i]; + }
Sorry, probably I should have specified a bit more.
I suggest you use libgmp for this, that allows any arbitrary word size. See mpz_init() to initialize the variable, then mpz_switch_byteorder() to change byteorder. Then, export it via mpz_export_data() back to fixed standard datatypes.
libgmp is well documented, please have a look at it.
Thanks.
Quoted 53 lines · Pablo Neira Ayuso
Hi Elise,
Quoted 37 lines · Elise Lennion
nft describe displays, to the user, which values are available for a selector, then the values should be in host byte order.
Reported-by Pablo Neira Ayuso <[email protected]>Fixes ccc5da470e76 ("datatype: Replace getnameinfo() by internal lookup table")Signed-off-by Elise Lennion <[email protected]>---
v2: Created a function to convert different types and number of bytes
include/datatype.h | 3 ++- src/datatype.c | 27 ++++++++++++++++++++++++--- src/expression.c | 3 ++- 3 files changed, 28 insertions(+), 5 deletions(-)include/datatype.h +2
-1@@ -191,7 +191,8 @@ extern struct error_record *symbolic_constant_parse(const struct expr *sym,extern void symbolic_constant_print (const struct symbol_table *tbl,const struct expr *expr,bool quotes);extern void symbol_table_print (const struct symbol_table *tbl, -const struct datatype *dtype); +const struct datatype *dtype, +enum byteorder byteorder);extern struct symbol_table *rt_symbol_table_init (const char *filename);extern void rt_symbol_table_free (struct symbol_table *tbl);src/datatype.c +0
-0@@ -180,15 +180,36 @@ void symbolic_constant_print(const struct symbol_table *tbl,printf ("%s" , s->identifier); }src/datatype.c void symbolic_constant_print(const struct symbol_table *tbl, · line 192
Quoted 10 lines
src/datatype.c +10
-0+static void big_to_host_byteorder (void *value,const unsigned int nbytes) +{ +unsigned char *p = value; +unsigned int i; + +for (i =0 ; i < nbytes /2 ; i++) { + p[i] ^= p[nbytes -1 - i]; + p[nbytes -1 - i] ^= p[i]; + p[i] ^= p[nbytes -1 - i]; + }Sorry, probably I should have specified a bit more.
Quoted 8 lines
I suggest you use libgmp for this, that allows any arbitrary word size. See
mpz_init()to initialize the variable, thenmpz_switch_byteorder()to change byteorder. Then, export it viampz_export_data()back to fixed standard datatypes.libgmp is well documented, please have a look at it.
Thanks.
Hello Pablo,
Should I write a function that switches byteorder of different types, for possible general use, or just use libgmp inside symbol_table_print?
In symbol_table_print the converted values have a specific type, so, something similar to the following is enough, right?
...
if (byteorder == BYTEORDER_BIG_ENDIAN) {
mpz_init_set_ui(v, value); mpz_switch_byteorder(v, nbytes); mpz_export_data(&value, v, BYTEORDER_HOST_ENDIAN, nbytes); mpz_clear(v);
}
...
Thank you,
elise
Pablo Neira Ayuso
maintainerQuoted 59 lines · Elise Lennion
Quoted 58 lines · Pablo Neira Ayuso
Hi Elise,
Quoted 37 lines · Elise Lennion
nft describe displays, to the user, which values are available for a selector, then the values should be in host byte order.
Reported-by Pablo Neira Ayuso <[email protected]>Fixes ccc5da470e76 ("datatype: Replace getnameinfo() by internal lookup table")Signed-off-by Elise Lennion <[email protected]>---
v2: Created a function to convert different types and number of bytes
include/datatype.h | 3 ++- src/datatype.c | 27 ++++++++++++++++++++++++--- src/expression.c | 3 ++- 3 files changed, 28 insertions(+), 5 deletions(-)include/datatype.h +2
-1@@ -191,7 +191,8 @@ extern struct error_record *symbolic_constant_parse(const struct expr *sym,extern void symbolic_constant_print (const struct symbol_table *tbl,const struct expr *expr,bool quotes);extern void symbol_table_print (const struct symbol_table *tbl, -const struct datatype *dtype); +const struct datatype *dtype, +enum byteorder byteorder);extern struct symbol_table *rt_symbol_table_init (const char *filename);extern void rt_symbol_table_free (struct symbol_table *tbl);src/datatype.c +0
-0@@ -180,15 +180,36 @@ void symbolic_constant_print(const struct symbol_table *tbl,printf ("%s" , s->identifier); }src/datatype.c void symbolic_constant_print(const struct symbol_table *tbl, · line 192
Quoted 10 lines
src/datatype.c +10
-0+static void big_to_host_byteorder (void *value,const unsigned int nbytes) +{ +unsigned char *p = value; +unsigned int i; + +for (i =0 ; i < nbytes /2 ; i++) { + p[i] ^= p[nbytes -1 - i]; + p[nbytes -1 - i] ^= p[i]; + p[i] ^= p[nbytes -1 - i]; + }Sorry, probably I should have specified a bit more.
I suggest you use libgmp for this, that allows any arbitrary word size. See
mpz_init()to initialize the variable, thenmpz_switch_byteorder()to change byteorder. Then, export it viampz_export_data()back to fixed standard datatypes.
Quoted 8 lines
Quoted 3 lines
libgmp is well documented, please have a look at it.
Thanks.
Hello Pablo,
Should I write a function that switches byteorder of different types, for possible general use, or just use libgmp inside
symbol_table_print?
A function that you can place under src/gmputil.c would be good, so we can reuse it later if we have similar needs.
Review
Quoted 11 lines
In
symbol_table_printthe converted values have a specific type, so, something similar to the following is enough, right?...
if (byteorder ==
BYTEORDER_BIG_ENDIAN) {(quoted diff) +0
-0mpz_init_set_ui(v, value); mpz_switch_byteorder(v, nbytes); mpz_export_data(&value, v, BYTEORDER_HOST_ENDIAN, nbytes); mpz_clear(v); }
Yes, I think that code is on the right track. Thanks.