diff options
author | Holger Eitzenberger <heitzenberger@astaro.com> | 2008-12-01 13:38:06 (GMT) |
---|---|---|
committer | Thomas Graf <tgr@plip.localdomain> | 2008-12-03 18:54:21 (GMT) |
commit | 72aa861c48b6876e94d07470025155a6924a6983 (patch) | |
tree | 452e2614c83b91fb3791e08bf78e22388e584828 | |
parent | 37f9855f4c02960b483d4d992771837c5906405e (diff) | |
download | libnl-72aa861c48b6876e94d07470025155a6924a6983.zip libnl-72aa861c48b6876e94d07470025155a6924a6983.tar.gz libnl-72aa861c48b6876e94d07470025155a6924a6983.tar.bz2 |
Allow to pass multiple group values to membership functions
Instead of calling the membership functions several times it is
helpfull to extend the API and make the single group functions a
special case.
The value 0 (NFNLGRP_NONE) terminates this list.
Example use:
nl_socket_add_memberships(sock, group_1, group_2, 0);
nl_socket_drop_memberships(sock, group_1, group_2, 0);
Signed-off-by: Holger Eitzenberger <holger@eitzenberger.org>
-rw-r--r-- | include/netlink/socket.h | 16 | ||||
-rw-r--r-- | lib/socket.c | 58 |
2 files changed, 54 insertions, 20 deletions
diff --git a/include/netlink/socket.h b/include/netlink/socket.h index c10bc2a..5eefa08 100644 --- a/include/netlink/socket.h +++ b/include/netlink/socket.h @@ -30,12 +30,20 @@ extern uint32_t nl_socket_get_local_port(struct nl_sock *); extern void nl_socket_set_local_port(struct nl_sock *, uint32_t); -extern int nl_socket_add_membership(struct nl_sock *, - int); -extern int nl_socket_drop_membership(struct nl_sock *, - int); +extern int nl_socket_add_memberships(struct nl_sock *, + int, ...); +extern int nl_socket_drop_memberships(struct nl_sock *, + int, ...); extern void nl_join_groups(struct nl_sock *, int); +static inline int nl_socket_add_membership(struct nl_sock *sk, int group) { + return nl_socket_add_memberships(sk, group, 0); +} + +static inline int nl_socket_drop_membership(struct nl_sock *sk, int group) { + return nl_socket_drop_memberships(sk, group, 0); +} + extern uint32_t nl_socket_get_peer_port(struct nl_sock *); extern void nl_socket_set_peer_port(struct nl_sock *, uint32_t); diff --git a/lib/socket.c b/lib/socket.c index 1a11971..511eb14 100644 --- a/lib/socket.c +++ b/lib/socket.c @@ -345,13 +345,14 @@ void nl_socket_set_local_port(struct nl_sock *sk, uint32_t port) */ /** - * Join a group + * Join groups * @arg sk Netlink socket * @arg group Group identifier * - * Joins the specified group using the modern socket option which + * Joins the specified groups using the modern socket option which * is available since kernel version 2.6.14. It allows joining an - * almost arbitary number of groups without limitation. + * almost arbitary number of groups without limitation. The list + * of groups has to be terminated by 0 (%NFNLGRP_NONE). * * Make sure to use the correct group definitions as the older * bitmask definitions for nl_join_groups() are likely to still @@ -359,43 +360,68 @@ void nl_socket_set_local_port(struct nl_sock *sk, uint32_t port) * * @return 0 on sucess or a negative error code. */ -int nl_socket_add_membership(struct nl_sock *sk, int group) +int nl_socket_add_memberships(struct nl_sock *sk, int group, ...) { int err; + va_list ap; if (sk->s_fd == -1) return -NLE_BAD_SOCK; - err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, - &group, sizeof(group)); - if (err < 0) - return -nl_syserr2nlerr(errno); + va_start(ap, group); + + while (group != 0) { + if (group < 0) + return -NLE_INVAL; + + err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, + &group, sizeof(group)); + if (err < 0) + return -nl_syserr2nlerr(errno); + + group = va_arg(ap, int); + } + + va_end(ap); return 0; } /** - * Leave a group + * Leave groups * @arg sk Netlink socket * @arg group Group identifier * - * Leaves the specified group using the modern socket option - * which is available since kernel version 2.6.14. + * Leaves the specified groups using the modern socket option + * which is available since kernel version 2.6.14. The list of groups + * has to terminated by 0 (%NFNLGRP_NONE). * * @see nl_socket_add_membership * @return 0 on success or a negative error code. */ -int nl_socket_drop_membership(struct nl_sock *sk, int group) +int nl_socket_drop_memberships(struct nl_sock *sk, int group, ...) { int err; + va_list ap; if (sk->s_fd == -1) return -NLE_BAD_SOCK; - err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP, - &group, sizeof(group)); - if (err < 0) - return -nl_syserr2nlerr(errno); + va_start(ap, group); + + while (group != 0) { + if (group < 0) + return -NLE_INVAL; + + err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP, + &group, sizeof(group)); + if (err < 0) + return -nl_syserr2nlerr(errno); + + group = va_arg(ap, int); + } + + va_end(ap); return 0; } |