diff options
author | David Gibson <david@gibson.dropbear.id.au> | 2022-09-28 14:33:27 +1000 |
---|---|---|
committer | Stefano Brivio <sbrivio@redhat.com> | 2022-09-29 12:22:21 +0200 |
commit | 656acdfc26b0a03329db005e41d0aaf789afca29 (patch) | |
tree | b7b5ae1712babf29c36fc2802e7b9b7f9418aff0 /netlink.c | |
parent | 40901c54375065c28f402b09126e9e50f80e8dfa (diff) | |
download | passt-656acdfc26b0a03329db005e41d0aaf789afca29.tar passt-656acdfc26b0a03329db005e41d0aaf789afca29.tar.gz passt-656acdfc26b0a03329db005e41d0aaf789afca29.tar.bz2 passt-656acdfc26b0a03329db005e41d0aaf789afca29.tar.lz passt-656acdfc26b0a03329db005e41d0aaf789afca29.tar.xz passt-656acdfc26b0a03329db005e41d0aaf789afca29.tar.zst passt-656acdfc26b0a03329db005e41d0aaf789afca29.zip |
Avoid ugly 'end' members in netlink structures
We use a number of complex structures to format messages to send to
netlink. In some cases we add imaginary 'end' members not because they
actually mean something on the wire, but so that we can use offsetof() on
the member to determine the relevant size.
Adding extra things to the structures for this is kinda nasty. We can use
a different construct with offsetof and sizeof to avoid them. As a bonus
this removes some cppcheck warnings about unused struct members.
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Diffstat (limited to 'netlink.c')
-rw-r--r-- | netlink.c | 19 |
1 files changed, 10 insertions, 9 deletions
@@ -206,7 +206,6 @@ void nl_route(int ns, unsigned int ifi, sa_family_t af, void *gw) uint32_t d; struct rtattr rta_gw; uint32_t a; - uint8_t end; } r4; } set; } req = { @@ -234,7 +233,8 @@ void nl_route(int ns, unsigned int ifi, sa_family_t af, void *gw) if (af == AF_INET6) { size_t rta_len = RTA_LENGTH(sizeof(req.set.r6.d)); - req.nlh.nlmsg_len = sizeof(req); + req.nlh.nlmsg_len = offsetof(struct req_t, set.r6) + + sizeof(req.set.r6); req.set.r6.rta_dst.rta_type = RTA_DST; req.set.r6.rta_dst.rta_len = rta_len; @@ -245,7 +245,8 @@ void nl_route(int ns, unsigned int ifi, sa_family_t af, void *gw) } else { size_t rta_len = RTA_LENGTH(sizeof(req.set.r4.d)); - req.nlh.nlmsg_len = offsetof(struct req_t, set.r4.end); + req.nlh.nlmsg_len = offsetof(struct req_t, set.r4) + + sizeof(req.set.r4); req.set.r4.rta_dst.rta_type = RTA_DST; req.set.r4.rta_dst.rta_len = rta_len; @@ -312,8 +313,6 @@ void nl_addr(int ns, unsigned int ifi, sa_family_t af, uint32_t l; struct rtattr rta_a; uint32_t a; - - uint8_t end; } a4; struct { struct rtattr rta_l; @@ -343,7 +342,8 @@ void nl_addr(int ns, unsigned int ifi, sa_family_t af, if (af == AF_INET6) { size_t rta_len = RTA_LENGTH(sizeof(req.set.a6.l)); - req.nlh.nlmsg_len = sizeof(req); + req.nlh.nlmsg_len = offsetof(struct req_t, set.a6) + + sizeof(req.set.a6); memcpy(&req.set.a6.l, addr, sizeof(req.set.a6.l)); req.set.a6.rta_l.rta_len = rta_len; @@ -354,7 +354,8 @@ void nl_addr(int ns, unsigned int ifi, sa_family_t af, } else { size_t rta_len = RTA_LENGTH(sizeof(req.set.a4.l)); - req.nlh.nlmsg_len = offsetof(struct req_t, set.a4.end); + req.nlh.nlmsg_len = offsetof(struct req_t, set.a4) + + sizeof(req.set.a4); req.set.a4.l = req.set.a4.a = *(uint32_t *)addr; req.set.a4.rta_l.rta_len = rta_len; @@ -425,7 +426,6 @@ void nl_link(int ns, unsigned int ifi, void *mac, int up, int mtu) unsigned char mac[ETH_ALEN]; struct { unsigned int mtu; - uint8_t end; } mtu; } set; } req = { @@ -457,7 +457,8 @@ void nl_link(int ns, unsigned int ifi, void *mac, int up, int mtu) } if (mtu) { - req.nlh.nlmsg_len = offsetof(struct req_t, set.mtu.end); + req.nlh.nlmsg_len = offsetof(struct req_t, set.mtu) + + sizeof(req.set.mtu); req.set.mtu.mtu = mtu; req.rta.rta_type = IFLA_MTU; req.rta.rta_len = RTA_LENGTH(sizeof(unsigned int)); |