summaryrefslogtreecommitdiffstats
path: root/src/utils.c
diff options
context:
space:
mode:
authorPhil Sutter <phil@nwl.cc>2018-06-22 14:18:57 +0200
committerFlorian Westphal <fw@strlen.de>2018-06-26 20:19:28 +0200
commit9737856067b97cbb869e04fc6b6e65c1d859f521 (patch)
tree12ffbb13658b842ddfb3090380d953e3efbb63f2 /src/utils.c
parentd1eeae816d39cb426654a721188428fd6d27959b (diff)
utils: Fix nftnl_get_value() on big endian
This function basically did: | memcpy(out, val, <len of requested type>); which works only for little endian integer types. Fix this by assigning the 64bit input value to a variable of the right size and use that as input for above memcpy() call. Signed-off-by: Phil Sutter <phil@nwl.cc>
Diffstat (limited to 'src/utils.c')
-rw-r--r--src/utils.c44
1 files changed, 42 insertions, 2 deletions
diff --git a/src/utils.c b/src/utils.c
index 3e44960..4d9ee78 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -72,6 +72,15 @@ static struct {
int nftnl_get_value(enum nftnl_type type, void *val, void *out)
{
+ union {
+ uint8_t u8;
+ uint16_t u16;
+ uint32_t u32;
+ int8_t s8;
+ int16_t s16;
+ int32_t s32;
+ } values;
+ void *valuep = NULL;
int64_t sval;
uint64_t uval;
@@ -85,7 +94,6 @@ int nftnl_get_value(enum nftnl_type type, void *val, void *out)
errno = ERANGE;
return -1;
}
- memcpy(out, &uval, basetype[type].len);
break;
case NFTNL_TYPE_S8:
case NFTNL_TYPE_S16:
@@ -97,10 +105,42 @@ int nftnl_get_value(enum nftnl_type type, void *val, void *out)
errno = ERANGE;
return -1;
}
- memcpy(out, &sval, basetype[type].len);
break;
}
+ switch (type) {
+ case NFTNL_TYPE_U8:
+ values.u8 = uval;
+ valuep = &values.u8;
+ break;
+ case NFTNL_TYPE_U16:
+ values.u16 = uval;
+ valuep = &values.u16;
+ break;
+ case NFTNL_TYPE_U32:
+ values.u32 = uval;
+ valuep = &values.u32;
+ break;
+ case NFTNL_TYPE_U64:
+ valuep = &uval;
+ break;
+ case NFTNL_TYPE_S8:
+ values.s8 = sval;
+ valuep = &values.s8;
+ break;
+ case NFTNL_TYPE_S16:
+ values.s16 = sval;
+ valuep = &values.s16;
+ break;
+ case NFTNL_TYPE_S32:
+ values.s32 = sval;
+ valuep = &values.s32;
+ break;
+ case NFTNL_TYPE_S64:
+ valuep = &sval;
+ break;
+ }
+ memcpy(out, valuep, basetype[type].len);
return 0;
}