[PATCH 1/2] new feature nl_send_iovec(), nl_auto_complete() and code refactoring.

Karl Hiramoto karl at hiramoto.org
Wed Feb 24 15:29:13 EST 2010


Create new function nl_send_iovec() to be used to send multiple 'struct iovec'
through the netlink socket.  This will be used for NF_QUEUE, to send
packet payload of a modified packet.

Refactor nl_send() to use nl_send_iovec() sending a single struct iovec.

Create new function nl_auto_complete() by refactoring nl_send_auto_complete(),
so other functions that call nl_send may also use nl_auto_complete()

Signed-off-by: Karl Hiramoto <karl at hiramoto.org>
---
 include/netlink/netlink.h |    4 ++
 lib/nl.c                  |   69 +++++++++++++++++++++++++++++---------------
 2 files changed, 49 insertions(+), 24 deletions(-)

diff --git a/include/netlink/netlink.h b/include/netlink/netlink.h
index 689b755..34eb773 100644
--- a/include/netlink/netlink.h
+++ b/include/netlink/netlink.h
@@ -50,6 +50,10 @@ extern int			nl_sendto(struct nl_sock *, void *, size_t);
 extern int			nl_sendmsg(struct nl_sock *, struct nl_msg *,
 					   struct msghdr *);
 extern int			nl_send(struct nl_sock *, struct nl_msg *);
+extern int			nl_send_iovec(struct nl_sock *, struct nl_msg *,
+					      const struct iovec *, unsigned);
+extern void			nl_auto_complete(struct nl_sock *,
+						      struct nl_msg *);
 extern int			nl_send_auto_complete(struct nl_sock *,
 						      struct nl_msg *);
 extern int			nl_send_simple(struct nl_sock *, int, int,
diff --git a/lib/nl.c b/lib/nl.c
index 15c3ede..ed501f7 100644
--- a/lib/nl.c
+++ b/lib/nl.c
@@ -207,14 +207,6 @@ int nl_sendmsg(struct nl_sock *sk, struct nl_msg *msg, struct msghdr *hdr)
 	struct nl_cb *cb;
 	int ret;
 
-	struct iovec iov = {
-		.iov_base = (void *) nlmsg_hdr(msg),
-		.iov_len = nlmsg_hdr(msg)->nlmsg_len,
-	};
-
-	hdr->msg_iov = &iov;
-	hdr->msg_iovlen = 1;
-
 	nlmsg_set_src(msg, &sk->s_local);
 
 	cb = sk->s_cb;
@@ -226,6 +218,7 @@ int nl_sendmsg(struct nl_sock *sk, struct nl_msg *msg, struct msghdr *hdr)
 	if (ret < 0)
 		return -nl_syserr2nlerr(errno);
 
+	NL_DBG(4, "sent %d bytes\n", ret);
 	return ret;
 }
 
@@ -234,17 +227,20 @@ int nl_sendmsg(struct nl_sock *sk, struct nl_msg *msg, struct msghdr *hdr)
  * Send netlink message.
  * @arg sk		Netlink socket.
  * @arg msg		Netlink message to be sent.
+ * @arg iov		iovec to be sent.
+ * @arg iovlen		number of struct iovec to be sent.
  * @see nl_sendmsg()
  * @return Number of characters sent on success or a negative error code.
  */
-int nl_send(struct nl_sock *sk, struct nl_msg *msg)
+int nl_send_iovec(struct nl_sock *sk, struct nl_msg *msg, const struct iovec *iov, unsigned iovlen)
 {
 	struct sockaddr_nl *dst;
 	struct ucred *creds;
-	
 	struct msghdr hdr = {
 		.msg_name = (void *) &sk->s_peer,
 		.msg_namelen = sizeof(struct sockaddr_nl),
+		.msg_iov = iov,
+		.msg_iovlen = iovlen,
 	};
 
 	/* Overwrite destination if specified in the message itself, defaults
@@ -273,22 +269,28 @@ int nl_send(struct nl_sock *sk, struct nl_msg *msg)
 	return nl_sendmsg(sk, msg, &hdr);
 }
 
+
+
 /**
- * Send netlink message and check & extend header values as needed.
- * @arg sk		Netlink socket.
- * @arg msg		Netlink message to be sent.
- *
- * Checks the netlink message \c nlh for completness and extends it
- * as required before sending it out. Checked fields include pid,
- * sequence nr, and flags.
- *
- * @see nl_send()
- * @return Number of characters sent or a negative error code.
- */
-int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
+* Send netlink message.
+* @arg sk		Netlink socket.
+* @arg msg		Netlink message to be sent.
+* @see nl_sendmsg()
+* @return Number of characters sent on success or a negative error code.
+*/
+int nl_send(struct nl_sock *sk, struct nl_msg *msg)
+{
+	struct iovec iov = {
+		.iov_base = (void *) nlmsg_hdr(msg),
+		.iov_len = nlmsg_hdr(msg)->nlmsg_len,
+	};
+
+	return nl_send_iovec(sk, msg, &iov, 1);
+}
+
+void nl_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
 {
 	struct nlmsghdr *nlh;
-	struct nl_cb *cb = sk->s_cb;
 
 	nlh = nlmsg_hdr(msg);
 	if (nlh->nlmsg_pid == 0)
@@ -299,11 +301,30 @@ int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
 
 	if (msg->nm_protocol == -1)
 		msg->nm_protocol = sk->s_proto;
-	
+
 	nlh->nlmsg_flags |= NLM_F_REQUEST;
 
 	if (!(sk->s_flags & NL_NO_AUTO_ACK))
 		nlh->nlmsg_flags |= NLM_F_ACK;
+}
+
+/**
+ * Send netlink message and check & extend header values as needed.
+ * @arg sk		Netlink socket.
+ * @arg msg		Netlink message to be sent.
+ *
+ * Checks the netlink message \c nlh for completness and extends it
+ * as required before sending it out. Checked fields include pid,
+ * sequence nr, and flags.
+ *
+ * @see nl_send()
+ * @return Number of characters sent or a negative error code.
+ */
+int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
+{
+	struct nl_cb *cb = sk->s_cb;
+
+	nl_auto_complete(sk, msg);
 
 	if (cb->cb_send_ow)
 		return cb->cb_send_ow(sk, msg);
-- 
1.6.6.2




More information about the libnl mailing list