libnetlink: style cleanups
Follow kernel style related cleanups: * break long lines * remove unnecessary void * cast
This commit is contained in:
parent
1b109a30bf
commit
2c500a4dc2
119
lib/libnetlink.c
119
lib/libnetlink.c
|
|
@ -43,7 +43,7 @@ void rtnl_close(struct rtnl_handle *rth)
|
|||
}
|
||||
}
|
||||
|
||||
int rtnl_open_byproto(struct rtnl_handle *rth, unsigned subscriptions,
|
||||
int rtnl_open_byproto(struct rtnl_handle *rth, unsigned int subscriptions,
|
||||
int protocol)
|
||||
{
|
||||
socklen_t addr_len;
|
||||
|
|
@ -58,12 +58,14 @@ int rtnl_open_byproto(struct rtnl_handle *rth, unsigned subscriptions,
|
|||
return -1;
|
||||
}
|
||||
|
||||
if (setsockopt(rth->fd,SOL_SOCKET,SO_SNDBUF,&sndbuf,sizeof(sndbuf)) < 0) {
|
||||
if (setsockopt(rth->fd, SOL_SOCKET, SO_SNDBUF,
|
||||
&sndbuf, sizeof(sndbuf)) < 0) {
|
||||
perror("SO_SNDBUF");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (setsockopt(rth->fd,SOL_SOCKET,SO_RCVBUF,&rcvbuf,sizeof(rcvbuf)) < 0) {
|
||||
if (setsockopt(rth->fd, SOL_SOCKET, SO_RCVBUF,
|
||||
&rcvbuf, sizeof(rcvbuf)) < 0) {
|
||||
perror("SO_RCVBUF");
|
||||
return -1;
|
||||
}
|
||||
|
|
@ -72,12 +74,14 @@ int rtnl_open_byproto(struct rtnl_handle *rth, unsigned subscriptions,
|
|||
rth->local.nl_family = AF_NETLINK;
|
||||
rth->local.nl_groups = subscriptions;
|
||||
|
||||
if (bind(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local)) < 0) {
|
||||
if (bind(rth->fd, (struct sockaddr *)&rth->local,
|
||||
sizeof(rth->local)) < 0) {
|
||||
perror("Cannot bind netlink socket");
|
||||
return -1;
|
||||
}
|
||||
addr_len = sizeof(rth->local);
|
||||
if (getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len) < 0) {
|
||||
if (getsockname(rth->fd, (struct sockaddr *)&rth->local,
|
||||
&addr_len) < 0) {
|
||||
perror("Cannot getsockname");
|
||||
return -1;
|
||||
}
|
||||
|
|
@ -86,14 +90,15 @@ int rtnl_open_byproto(struct rtnl_handle *rth, unsigned subscriptions,
|
|||
return -1;
|
||||
}
|
||||
if (rth->local.nl_family != AF_NETLINK) {
|
||||
fprintf(stderr, "Wrong address family %d\n", rth->local.nl_family);
|
||||
fprintf(stderr, "Wrong address family %d\n",
|
||||
rth->local.nl_family);
|
||||
return -1;
|
||||
}
|
||||
rth->seq = time(NULL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int rtnl_open(struct rtnl_handle *rth, unsigned subscriptions)
|
||||
int rtnl_open(struct rtnl_handle *rth, unsigned int subscriptions)
|
||||
{
|
||||
return rtnl_open_byproto(rth, subscriptions, NETLINK_ROUTE);
|
||||
}
|
||||
|
|
@ -123,7 +128,7 @@ int rtnl_wilddump_req_filter(struct rtnl_handle *rth, int family, int type,
|
|||
.ext_filter_mask = filt_mask,
|
||||
};
|
||||
|
||||
return send(rth->fd, (void*)&req, sizeof(req), 0);
|
||||
return send(rth->fd, &req, sizeof(req), 0);
|
||||
}
|
||||
|
||||
int rtnl_wilddump_req_filter_fn(struct rtnl_handle *rth, int family, int type,
|
||||
|
|
@ -169,7 +174,7 @@ int rtnl_wilddump_stats_req_filter(struct rtnl_handle *rth, int fam, int type,
|
|||
req.ifsm.family = fam;
|
||||
req.ifsm.filter_mask = filt_mask;
|
||||
|
||||
return send(rth->fd, (void *)&req, sizeof(req), 0);
|
||||
return send(rth->fd, &req, sizeof(req), 0);
|
||||
}
|
||||
|
||||
int rtnl_send(struct rtnl_handle *rth, const void *buf, int len)
|
||||
|
|
@ -198,7 +203,8 @@ int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int len)
|
|||
for (h = (struct nlmsghdr *)resp; NLMSG_OK(h, status);
|
||||
h = NLMSG_NEXT(h, status)) {
|
||||
if (h->nlmsg_type == NLMSG_ERROR) {
|
||||
struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
|
||||
struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h);
|
||||
|
||||
if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
|
||||
fprintf(stderr, "ERROR truncated\n");
|
||||
else
|
||||
|
|
@ -237,7 +243,7 @@ int rtnl_dump_request_n(struct rtnl_handle *rth, struct nlmsghdr *n)
|
|||
{
|
||||
struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
|
||||
struct iovec iov = {
|
||||
.iov_base = (void*) n,
|
||||
.iov_base = n,
|
||||
.iov_len = n->nlmsg_len
|
||||
};
|
||||
struct msghdr msg = {
|
||||
|
|
@ -295,7 +301,8 @@ int rtnl_dump_filter_l(struct rtnl_handle *rth,
|
|||
fwrite(buf, 1, NLMSG_ALIGN(status), rth->dump_fp);
|
||||
|
||||
for (a = arg; a->filter; a++) {
|
||||
struct nlmsghdr *h = (struct nlmsghdr*)buf;
|
||||
struct nlmsghdr *h = (struct nlmsghdr *)buf;
|
||||
|
||||
msglen = status;
|
||||
|
||||
while (NLMSG_OK(h, msglen)) {
|
||||
|
|
@ -316,7 +323,8 @@ int rtnl_dump_filter_l(struct rtnl_handle *rth,
|
|||
break; /* process next filter */
|
||||
}
|
||||
if (h->nlmsg_type == NLMSG_ERROR) {
|
||||
struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
|
||||
struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h);
|
||||
|
||||
if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
|
||||
fprintf(stderr,
|
||||
"ERROR truncated\n");
|
||||
|
|
@ -377,11 +385,11 @@ int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
|
|||
struct nlmsghdr *answer, size_t maxlen)
|
||||
{
|
||||
int status;
|
||||
unsigned seq;
|
||||
unsigned int seq;
|
||||
struct nlmsghdr *h;
|
||||
struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
|
||||
struct iovec iov = {
|
||||
.iov_base = (void*) n,
|
||||
.iov_base = n,
|
||||
.iov_len = n->nlmsg_len
|
||||
};
|
||||
struct msghdr msg = {
|
||||
|
|
@ -420,19 +428,23 @@ int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
|
|||
return -1;
|
||||
}
|
||||
if (msg.msg_namelen != sizeof(nladdr)) {
|
||||
fprintf(stderr, "sender address length == %d\n", msg.msg_namelen);
|
||||
fprintf(stderr,
|
||||
"sender address length == %d\n",
|
||||
msg.msg_namelen);
|
||||
exit(1);
|
||||
}
|
||||
for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
|
||||
for (h = (struct nlmsghdr *)buf; status >= sizeof(*h); ) {
|
||||
int len = h->nlmsg_len;
|
||||
int l = len - sizeof(*h);
|
||||
|
||||
if (l < 0 || len>status) {
|
||||
if (l < 0 || len > status) {
|
||||
if (msg.msg_flags & MSG_TRUNC) {
|
||||
fprintf(stderr, "Truncated message\n");
|
||||
return -1;
|
||||
}
|
||||
fprintf(stderr, "!!!malformed message: len=%d\n", len);
|
||||
fprintf(stderr,
|
||||
"!!!malformed message: len=%d\n",
|
||||
len);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
|
@ -441,12 +453,13 @@ int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
|
|||
h->nlmsg_seq != seq) {
|
||||
/* Don't forget to skip that message. */
|
||||
status -= NLMSG_ALIGN(len);
|
||||
h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
|
||||
h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (h->nlmsg_type == NLMSG_ERROR) {
|
||||
struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
|
||||
struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h);
|
||||
|
||||
if (l < sizeof(struct nlmsgerr)) {
|
||||
fprintf(stderr, "ERROR truncated\n");
|
||||
} else if (!err->error) {
|
||||
|
|
@ -473,7 +486,7 @@ int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
|
|||
fprintf(stderr, "Unexpected reply!!!\n");
|
||||
|
||||
status -= NLMSG_ALIGN(len);
|
||||
h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
|
||||
h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
|
||||
}
|
||||
|
||||
if (msg.msg_flags & MSG_TRUNC) {
|
||||
|
|
@ -545,7 +558,9 @@ int rtnl_listen(struct rtnl_handle *rtnl,
|
|||
return -1;
|
||||
}
|
||||
if (msg.msg_namelen != sizeof(nladdr)) {
|
||||
fprintf(stderr, "Sender address length == %d\n", msg.msg_namelen);
|
||||
fprintf(stderr,
|
||||
"Sender address length == %d\n",
|
||||
msg.msg_namelen);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
|
@ -563,17 +578,19 @@ int rtnl_listen(struct rtnl_handle *rtnl,
|
|||
}
|
||||
}
|
||||
|
||||
for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
|
||||
for (h = (struct nlmsghdr *)buf; status >= sizeof(*h); ) {
|
||||
int err;
|
||||
int len = h->nlmsg_len;
|
||||
int l = len - sizeof(*h);
|
||||
|
||||
if (l<0 || len>status) {
|
||||
if (l < 0 || len > status) {
|
||||
if (msg.msg_flags & MSG_TRUNC) {
|
||||
fprintf(stderr, "Truncated message\n");
|
||||
return -1;
|
||||
}
|
||||
fprintf(stderr, "!!!malformed message: len=%d\n", len);
|
||||
fprintf(stderr,
|
||||
"!!!malformed message: len=%d\n",
|
||||
len);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
|
@ -582,7 +599,7 @@ int rtnl_listen(struct rtnl_handle *rtnl,
|
|||
return err;
|
||||
|
||||
status -= NLMSG_ALIGN(len);
|
||||
h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
|
||||
h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
|
||||
}
|
||||
if (msg.msg_flags & MSG_TRUNC) {
|
||||
fprintf(stderr, "Message truncated\n");
|
||||
|
|
@ -600,8 +617,8 @@ int rtnl_from_file(FILE *rtnl, rtnl_listen_filter_t handler,
|
|||
{
|
||||
int status;
|
||||
struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
|
||||
char buf[16384];
|
||||
struct nlmsghdr *h = (void*)buf;
|
||||
char buf[16384];
|
||||
struct nlmsghdr *h = (struct nlmsghdr *)buf;
|
||||
|
||||
while (1) {
|
||||
int err, len;
|
||||
|
|
@ -621,7 +638,7 @@ int rtnl_from_file(FILE *rtnl, rtnl_listen_filter_t handler,
|
|||
len = h->nlmsg_len;
|
||||
l = len - sizeof(*h);
|
||||
|
||||
if (l<0 || len>sizeof(buf)) {
|
||||
if (l < 0 || len > sizeof(buf)) {
|
||||
fprintf(stderr, "!!!malformed message: len=%d @%lu\n",
|
||||
len, ftell(rtnl));
|
||||
return -1;
|
||||
|
|
@ -681,7 +698,9 @@ int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
|
|||
struct rtattr *rta;
|
||||
|
||||
if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
|
||||
fprintf(stderr, "addattr_l ERROR: message exceeded bound of %d\n",maxlen);
|
||||
fprintf(stderr,
|
||||
"addattr_l ERROR: message exceeded bound of %d\n",
|
||||
maxlen);
|
||||
return -1;
|
||||
}
|
||||
rta = NLMSG_TAIL(n);
|
||||
|
|
@ -695,7 +714,9 @@ int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
|
|||
int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len)
|
||||
{
|
||||
if (NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len) > maxlen) {
|
||||
fprintf(stderr, "addraw_l ERROR: message exceeded bound of %d\n",maxlen);
|
||||
fprintf(stderr,
|
||||
"addraw_l ERROR: message exceeded bound of %d\n",
|
||||
maxlen);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
@ -744,10 +765,12 @@ int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data)
|
|||
struct rtattr *subrta;
|
||||
|
||||
if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
|
||||
fprintf(stderr,"rta_addattr32: Error! max allowed bound %d exceeded\n",maxlen);
|
||||
fprintf(stderr,
|
||||
"rta_addattr32: Error! max allowed bound %d exceeded\n",
|
||||
maxlen);
|
||||
return -1;
|
||||
}
|
||||
subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
|
||||
subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len));
|
||||
subrta->rta_type = type;
|
||||
subrta->rta_len = len;
|
||||
memcpy(RTA_DATA(subrta), &data, 4);
|
||||
|
|
@ -762,10 +785,12 @@ int rta_addattr_l(struct rtattr *rta, int maxlen, int type,
|
|||
int len = RTA_LENGTH(alen);
|
||||
|
||||
if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) {
|
||||
fprintf(stderr,"rta_addattr_l: Error! max allowed bound %d exceeded\n",maxlen);
|
||||
fprintf(stderr,
|
||||
"rta_addattr_l: Error! max allowed bound %d exceeded\n",
|
||||
maxlen);
|
||||
return -1;
|
||||
}
|
||||
subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
|
||||
subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len));
|
||||
subrta->rta_type = type;
|
||||
subrta->rta_len = len;
|
||||
memcpy(RTA_DATA(subrta), data, alen);
|
||||
|
|
@ -819,14 +844,16 @@ int parse_rtattr_flags(struct rtattr *tb[], int max, struct rtattr *rta,
|
|||
type = rta->rta_type & ~flags;
|
||||
if ((type <= max) && (!tb[type]))
|
||||
tb[type] = rta;
|
||||
rta = RTA_NEXT(rta,len);
|
||||
rta = RTA_NEXT(rta, len);
|
||||
}
|
||||
if (len)
|
||||
fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len);
|
||||
fprintf(stderr, "!!!Deficit %d, rta_len=%d\n",
|
||||
len, rta->rta_len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int parse_rtattr_byindex(struct rtattr *tb[], int max, struct rtattr *rta, int len)
|
||||
int parse_rtattr_byindex(struct rtattr *tb[], int max,
|
||||
struct rtattr *rta, int len)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
|
|
@ -834,10 +861,11 @@ int parse_rtattr_byindex(struct rtattr *tb[], int max, struct rtattr *rta, int l
|
|||
while (RTA_OK(rta, len)) {
|
||||
if (rta->rta_type <= max && i < max)
|
||||
tb[i++] = rta;
|
||||
rta = RTA_NEXT(rta,len);
|
||||
rta = RTA_NEXT(rta, len);
|
||||
}
|
||||
if (len)
|
||||
fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len);
|
||||
fprintf(stderr, "!!!Deficit %d, rta_len=%d\n",
|
||||
len, rta->rta_len);
|
||||
return i;
|
||||
}
|
||||
|
||||
|
|
@ -848,13 +876,16 @@ struct rtattr *parse_rtattr_one(int type, struct rtattr *rta, int len)
|
|||
return rta;
|
||||
rta = RTA_NEXT(rta, len);
|
||||
}
|
||||
|
||||
if (len)
|
||||
fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len);
|
||||
fprintf(stderr, "!!!Deficit %d, rta_len=%d\n",
|
||||
len, rta->rta_len);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int __parse_rtattr_nested_compat(struct rtattr *tb[], int max, struct rtattr *rta,
|
||||
int len)
|
||||
int __parse_rtattr_nested_compat(struct rtattr *tb[], int max,
|
||||
struct rtattr *rta,
|
||||
int len)
|
||||
{
|
||||
if (RTA_PAYLOAD(rta) < len)
|
||||
return -1;
|
||||
|
|
|
|||
Loading…
Reference in New Issue