All of lore.kernel.org
 help / color / mirror / Atom feed
* [LIBNFNETLINK] Introduce nfnl_listen_for_msecs
@ 2006-02-07  1:47 Pablo Neira Ayuso
  2006-02-07 14:00 ` Patrick McHardy
  0 siblings, 1 reply; 6+ messages in thread
From: Pablo Neira Ayuso @ 2006-02-07  1:47 UTC (permalink / raw)
  To: Netfilter Development Mailinglist; +Cc: Harald Welte

[-- 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 *),

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [LIBNFNETLINK] Introduce nfnl_listen_for_msecs
  2006-02-07  1:47 [LIBNFNETLINK] Introduce nfnl_listen_for_msecs Pablo Neira Ayuso
@ 2006-02-07 14:00 ` Patrick McHardy
  2006-02-07 14:33   ` Pablo Neira Ayuso
  0 siblings, 1 reply; 6+ messages in thread
From: Patrick McHardy @ 2006-02-07 14:00 UTC (permalink / raw)
  To: Pablo Neira Ayuso; +Cc: Harald Welte, Netfilter Development Mailinglist

Pablo Neira Ayuso wrote:
> Hi Harald,
> 
> The patch attached introduces a new function called
> nfnl_listen_for_msecs. This is useful for polling netlink events.

Can you give an example how this is going to be used?
Thanks.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [LIBNFNETLINK] Introduce nfnl_listen_for_msecs
  2006-02-07 14:00 ` Patrick McHardy
@ 2006-02-07 14:33   ` Pablo Neira Ayuso
  2006-02-07 14:39     ` Patrick McHardy
  0 siblings, 1 reply; 6+ messages in thread
From: Pablo Neira Ayuso @ 2006-02-07 14:33 UTC (permalink / raw)
  To: Patrick McHardy; +Cc: Harald Welte, Netfilter Development Mailinglist

Patrick McHardy wrote:
>>The patch attached introduces a new function called
>>nfnl_listen_for_msecs. This is useful for polling netlink events.
> 
> Can you give an example how this is going to be used?

A program waits for some specific event, if it doesn't happen in a given
period of time, it gives up.

-- 
Pablo

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [LIBNFNETLINK] Introduce nfnl_listen_for_msecs
  2006-02-07 14:33   ` Pablo Neira Ayuso
@ 2006-02-07 14:39     ` Patrick McHardy
  2006-02-07 16:37       ` Pablo Neira Ayuso
  0 siblings, 1 reply; 6+ messages in thread
From: Patrick McHardy @ 2006-02-07 14:39 UTC (permalink / raw)
  To: Pablo Neira Ayuso; +Cc: Harald Welte, Netfilter Development Mailinglist

Pablo Neira Ayuso wrote:
> Patrick McHardy wrote:
> 
>>>The patch attached introduces a new function called
>>>nfnl_listen_for_msecs. This is useful for polling netlink events.
>>
>>Can you give an example how this is going to be used?
> 
> 
> A program waits for some specific event, if it doesn't happen in a given
> period of time, it gives up.

I got that :) What I actually wanted to know is what kind of program
would need this, and if its not likely that most programs need their
own select-loop for dealing with other fds anyway. Or in other words,
I'm not convinced this feature belongs inside libnfnetlink.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [LIBNFNETLINK] Introduce nfnl_listen_for_msecs
  2006-02-07 14:39     ` Patrick McHardy
@ 2006-02-07 16:37       ` Pablo Neira Ayuso
  2006-02-07 19:55         ` Patrick McHardy
  0 siblings, 1 reply; 6+ messages in thread
From: Pablo Neira Ayuso @ 2006-02-07 16:37 UTC (permalink / raw)
  To: Patrick McHardy; +Cc: Harald Welte, Netfilter Development Mailinglist

Patrick McHardy wrote:
> Pablo Neira Ayuso wrote:
> 
>>Patrick McHardy wrote:
>>
>>
>>>>The patch attached introduces a new function called
>>>>nfnl_listen_for_msecs. This is useful for polling netlink events.
>>>
>>>Can you give an example how this is going to be used?
>>
>>A program waits for some specific event, if it doesn't happen in a given
>>period of time, it gives up.
> 
> I got that :) What I actually wanted to know is what kind of program
> would need this, and if its not likely that most programs need their
> own select-loop for dealing with other fds anyway. Or in other words,
> I'm not convinced this feature belongs inside libnfnetlink.

But if I want to poll events, I'll have to work with the netfilter
netlink socket from, say libnetfilter_conntrack or a normal program. So
it will have to know about the netlink details.

AFAICS that would be a layer violation since libnfnetlink contains all
the low level communication system with the netlink socket.

-- 
Pablo

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [LIBNFNETLINK] Introduce nfnl_listen_for_msecs
  2006-02-07 16:37       ` Pablo Neira Ayuso
@ 2006-02-07 19:55         ` Patrick McHardy
  0 siblings, 0 replies; 6+ messages in thread
From: Patrick McHardy @ 2006-02-07 19:55 UTC (permalink / raw)
  To: Pablo Neira Ayuso; +Cc: Harald Welte, Netfilter Development Mailinglist

Pablo Neira Ayuso wrote:
> Patrick McHardy wrote:
> 
>>I got that :) What I actually wanted to know is what kind of program
>>would need this, and if its not likely that most programs need their
>>own select-loop for dealing with other fds anyway. Or in other words,
>>I'm not convinced this feature belongs inside libnfnetlink.
> 
> 
> But if I want to poll events, I'll have to work with the netfilter
> netlink socket from, say libnetfilter_conntrack or a normal program. So
> it will have to know about the netlink details.

Not the details, just the fd.

> AFAICS that would be a layer violation since libnfnetlink contains all
> the low level communication system with the netlink socket.

>From a poll/select perspective, the fd itself has nothing to do with
nfnetlink, its just a fd. I don't think this belongs inside
libnfnetlink, it has no direct relationship to nfnetlink. Even
nfnl_listen is questionable, the only reason it exists is probably
that it got copied from iproute's libnetlink.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2006-02-07 19:55 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-02-07  1:47 [LIBNFNETLINK] Introduce nfnl_listen_for_msecs Pablo Neira Ayuso
2006-02-07 14:00 ` 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

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.