From: Pablo Neira Ayuso <pablo@eurodev.net>
To: Netfilter Development Mailinglist <netfilter-devel@lists.netfilter.org>
Cc: Harald Welte <laforge@netfilter.org>
Subject: [LIBNFNETLINK] Introduce nfnl_listen_for_msecs
Date: Tue, 07 Feb 2006 02:47:38 +0100 [thread overview]
Message-ID: <43E7FC3A.4050209@eurodev.net> (raw)
[-- Attachment #1: Type: text/plain, Size: 297 bytes --]
Hi Harald,
The patch attached introduces a new function called
nfnl_listen_for_msecs. This is useful for polling netlink events.
I have enqueued a patch for libnetfilter_conntrack that implements
nfct_event_conntrack_for_msecs. I apply it by myself once this gets
applied ;)
cheers,
--
Pablo
[-- Attachment #2: x --]
[-- Type: text/plain, Size: 4470 bytes --]
Index: include/libnfnetlink/libnfnetlink.h
===================================================================
--- include/libnfnetlink/libnfnetlink.h (revision 6450)
+++ include/libnfnetlink/libnfnetlink.h (working copy)
@@ -76,6 +76,11 @@
extern int nfnl_listen(struct nfnl_handle *,
int (*)(struct sockaddr_nl *, struct nlmsghdr *, void *),
void *);
+/* simple challenge/response with timeout (polling) */
+extern int nfnl_listen_for_msecs(struct nfnl_handle *,
+ int (*)(struct sockaddr_nl *,
+ struct nlmsghdr *, void *),
+ void *, int timeout);
/* receiving */
extern ssize_t nfnl_recv(const struct nfnl_handle *h, unsigned char *buf, size_t len);
Index: src/libnfnetlink.c
===================================================================
--- src/libnfnetlink.c (revision 6450)
+++ src/libnfnetlink.c (working copy)
@@ -24,6 +24,9 @@
* 2006-01-26 Harald Welte <laforge@netfilter.org>:
* remove bogus nfnlh->local.nl_pid from nfnl_open ;)
* add 16bit attribute functions
+ *
+ * 2006-02-07 Pablo Neira Ayuso <pablo@netfilter.org>:
+ * introduce nfnl_listen_for_msecs
*/
#include <stdlib.h>
@@ -36,6 +39,7 @@
#include <sys/types.h>
#include <sys/socket.h>
+#include <poll.h>
#include <linux/netlink.h>
@@ -397,27 +401,11 @@
return status;
}
-/**
- * nfnl_listen: listen for one or more netlink messages
- *
- * nfnhl: libnfnetlink handle
- * handler: callback function to be called for every netlink message
- * - the callback handler should normally return 0
- * - but may return a negative error code which will cause
- * nfnl_listen to return immediately with the same error code
- * - or return a postivie error code which will cause
- * nfnl_listen to return after it has finished processing all
- * the netlink messages in the current packet
- * Thus a positive error code will terminate nfnl_listen "soon"
- * without any loss of data, a negative error code will terminate
- * nfnl_listen "very soon" and throw away data already read from
- * the netlink socket.
- * jarg: opaque argument passed on to callback
- *
- */
-int nfnl_listen(struct nfnl_handle *nfnlh,
- int (*handler)(struct sockaddr_nl *, struct nlmsghdr *n,
- void *), void *jarg)
+
+int nfnl_listen_for_msecs(struct nfnl_handle *nfnlh,
+ int (*handler)(struct sockaddr_nl *,
+ struct nlmsghdr *n, void *),
+ void *jarg, int timeout)
{
struct sockaddr_nl nladdr;
char buf[NFNL_BUFFSIZE];
@@ -434,12 +422,26 @@
0
};
+ struct pollfd ufds = {
+ .fd = nfnlh->fd,
+ .events = POLLIN | POLLPRI | POLLERR,
+ .revents = 0
+ };
+
memset(&nladdr, 0, sizeof(nladdr));
nladdr.nl_family = AF_NETLINK;
iov.iov_base = buf;
iov.iov_len = sizeof(buf);
while (! quit) {
+ if (poll(&ufds, 1, timeout) == -1) {
+ perror("poll error");
+ return -errno;
+ }
+ if (ufds.revents & POLLERR)
+ break;
+ if (!(ufds.revents & (POLLIN | POLLPRI)))
+ break;
remain = recvmsg(nfnlh->fd, &msg, 0);
if (remain < 0) {
if (errno == EINTR)
@@ -510,6 +512,31 @@
return quit;
}
+/**
+ * nfnl_listen: listen for one or more netlink messages
+ *
+ * nfnhl: libnfnetlink handle
+ * handler: callback function to be called for every netlink message
+ * - the callback handler should normally return 0
+ * - but may return a negative error code which will cause
+ * nfnl_listen to return immediately with the same error code
+ * - or return a postivie error code which will cause
+ * nfnl_listen to return after it has finished processing all
+ * the netlink messages in the current packet
+ * Thus a positive error code will terminate nfnl_listen "soon"
+ * without any loss of data, a negative error code will terminate
+ * nfnl_listen "very soon" and throw away data already read from
+ * the netlink socket.
+ * jarg: opaque argument passed on to callback
+ *
+ */
+int nfnl_listen(struct nfnl_handle *nfnlh,
+ int (*handler)(struct sockaddr_nl *, struct nlmsghdr *n,
+ void *), void *jarg)
+{
+ return nfnl_listen_for_msecs(nfnlh, handler, jarg, -1);
+}
+
int nfnl_talk(struct nfnl_handle *nfnlh, struct nlmsghdr *n, pid_t peer,
unsigned groups, struct nlmsghdr *answer,
int (*junk)(struct sockaddr_nl *, struct nlmsghdr *n, void *),
next reply other threads:[~2006-02-07 1:47 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-02-07 1:47 Pablo Neira Ayuso [this message]
2006-02-07 14:00 ` [LIBNFNETLINK] Introduce nfnl_listen_for_msecs Patrick McHardy
2006-02-07 14:33 ` Pablo Neira Ayuso
2006-02-07 14:39 ` Patrick McHardy
2006-02-07 16:37 ` Pablo Neira Ayuso
2006-02-07 19:55 ` Patrick McHardy
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=43E7FC3A.4050209@eurodev.net \
--to=pablo@eurodev.net \
--cc=laforge@netfilter.org \
--cc=netfilter-devel@lists.netfilter.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.