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>
This commit is contained in:
Holger Eitzenberger 2008-12-01 14:38:06 +01:00 committed by Thomas Graf
parent 37f9855f4c
commit 72aa861c48
2 changed files with 54 additions and 20 deletions

View File

@ -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);

View File

@ -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;
}