+// SPDX-License-Identifier: GPL-2.0-or-later
/* Netlink helpers for zbuf
* Copyright (c) 2014-2015 Timo Teräs
- *
- * This file is free software: you may copy, redistribute and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 2 of the License, or
- * (at your option) any later version.
*/
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
#include <fcntl.h>
#include <errno.h>
#include <string.h>
struct nlmsghdr *n;
n = znl_push(zb, sizeof(*n));
- if (!n) return NULL;
+ if (!n)
+ return NULL;
- *n = (struct nlmsghdr) {
- .nlmsg_type = type,
- .nlmsg_flags = flags,
+ *n = (struct nlmsghdr){
+ .nlmsg_type = type, .nlmsg_flags = flags,
};
return n;
}
void znl_nlmsg_complete(struct zbuf *zb, struct nlmsghdr *n)
{
- n->nlmsg_len = zb->tail - (uint8_t*)n;
+ n->nlmsg_len = zb->tail - (uint8_t *)n;
}
struct nlmsghdr *znl_nlmsg_pull(struct zbuf *zb, struct zbuf *payload)
size_t plen;
n = znl_pull(zb, sizeof(*n));
- if (!n) return NULL;
+ if (!n)
+ return NULL;
plen = n->nlmsg_len - sizeof(*n);
zbuf_init(payload, znl_pull(zb, plen), plen, plen);
return n;
}
-struct rtattr *znl_rta_push(struct zbuf *zb, uint16_t type, const void *val, size_t len)
+struct rtattr *znl_rta_push(struct zbuf *zb, uint16_t type, const void *val,
+ size_t len)
{
struct rtattr *rta;
uint8_t *dst;
rta = znl_push(zb, ZNL_ALIGN(sizeof(*rta)) + ZNL_ALIGN(len));
- if (!rta) return NULL;
+ if (!rta)
+ return NULL;
- *rta = (struct rtattr) {
- .rta_type = type,
- .rta_len = ZNL_ALIGN(sizeof(*rta)) + len,
+ *rta = (struct rtattr){
+ .rta_type = type, .rta_len = ZNL_ALIGN(sizeof(*rta)) + len,
};
- dst = (uint8_t *)(rta+1);
+ dst = (uint8_t *)(rta + 1);
memcpy(dst, val, len);
- memset(dst+len, 0, ZNL_ALIGN(len) - len);
+ memset(dst + len, 0, ZNL_ALIGN(len) - len);
return rta;
}
struct rtattr *rta;
rta = znl_push(zb, sizeof(*rta));
- if (!rta) return NULL;
+ if (!rta)
+ return NULL;
- *rta = (struct rtattr) {
+ *rta = (struct rtattr){
.rta_type = type,
};
return rta;
void znl_rta_nested_complete(struct zbuf *zb, struct rtattr *rta)
{
- size_t len = zb->tail - (uint8_t*) rta;
+ size_t len = zb->tail - (uint8_t *)rta;
size_t align = ZNL_ALIGN(len) - len;
if (align) {
void *dst = zbuf_pushn(zb, align);
- if (dst) memset(dst, 0, align);
+ if (dst)
+ memset(dst, 0, align);
}
rta->rta_len = len;
}
size_t plen;
rta = znl_pull(zb, sizeof(*rta));
- if (!rta) return NULL;
+ if (!rta)
+ return NULL;
if (rta->rta_len > sizeof(*rta)) {
plen = rta->rta_len - sizeof(*rta);
memset(&addr, 0, sizeof(addr));
addr.nl_family = AF_NETLINK;
addr.nl_groups = groups;
- if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0)
+ if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
goto error;
return fd;
close(fd);
return -1;
}
-