All of lore.kernel.org
 help / color / mirror / Atom feed
From: Antonio Quartulli <antonio@meshcoding.com>
To: b.a.t.m.a.n@lists.open-mesh.org
Cc: Antonio Quartulli <antonio@meshcoding.com>
Subject: [B.A.T.M.A.N.] [PATCHv2 next 1/3] batctl: introduce rtnl_open() helper function
Date: Sun,  5 Jan 2014 22:37:13 +0100	[thread overview]
Message-ID: <1388957835-2421-1-git-send-email-antonio@meshcoding.com> (raw)

Other part of batctl may require to open and send
a message to RTNL. Extract this code and put it in
a generic helper to simplify future uses.

Signed-off-by: Antonio Quartulli <antonio@meshcoding.com>
---

Changes from v1:
- fixed typ0
- removed len argument from rtnl_open() in 1/3
- removed error messages in 2/3
- avoided double free in 2/3
- simplified 3/3



 functions.c | 45 +++++++++++++++++++++++++++++----------------
 1 file changed, 29 insertions(+), 16 deletions(-)

diff --git a/functions.c b/functions.c
index 13ba190..50bb834 100644
--- a/functions.c
+++ b/functions.c
@@ -497,30 +497,27 @@ static void request_mac_resolve(int ai_family, const void *l3addr)
 	close(sock);
 }
 
-static int resolve_mac_from_cache_open(int ai_family)
+/**
+ * rtnl_open - open a socket to rtnl and send a request
+ * @nh: the header of the request to send
+ * @protocol: the protocol to use when opening the socket
+ *
+ * Return 0 on success or a negative error code otherwise
+ */
+static int rtnl_open(struct nlmsghdr *nh, int protocol)
 {
-	int socknl;
-	int ret;
-	struct {
-		struct nlmsghdr hdr;
-		struct ndmsg msg;
-	} nlreq;
-	struct sockaddr_nl addrnl;
 	static uint32_t nr_call = 0;
 	uint32_t pid = (++nr_call + getpid()) & 0x3FFFFF;
+	struct sockaddr_nl addrnl;
+	int socknl;
+	int ret;
 
 	memset(&addrnl, 0, sizeof(addrnl));
 	addrnl.nl_family = AF_NETLINK;
 	addrnl.nl_pid = pid;
 	addrnl.nl_groups = 0;
 
-	memset(&nlreq, 0, sizeof(nlreq));
-	nlreq.hdr.nlmsg_len = NLMSG_LENGTH(sizeof(nlreq.msg));
-	nlreq.hdr.nlmsg_type = RTM_GETNEIGH;
-	nlreq.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
-	nlreq.msg.ndm_family = ai_family;
-
-	socknl = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
+	socknl = socket(AF_NETLINK, SOCK_RAW, protocol);
 	if (socknl < 0)
 		goto out;
 
@@ -528,7 +525,7 @@ static int resolve_mac_from_cache_open(int ai_family)
 	if (ret < 0)
 		goto outclose;
 
-	ret = send(socknl, &nlreq, nlreq.hdr.nlmsg_len, 0);
+	ret = send(socknl, nh, nh->nlmsg_len, 0);
 	if (ret < 0)
 		goto outclose;
 out:
@@ -538,6 +535,22 @@ outclose:
 	return ret;
 }
 
+static int resolve_mac_from_cache_open(int ai_family)
+{
+	struct {
+		struct nlmsghdr hdr;
+		struct ndmsg msg;
+	} nlreq;
+
+	memset(&nlreq, 0, sizeof(nlreq));
+	nlreq.hdr.nlmsg_len = NLMSG_LENGTH(sizeof(nlreq.msg));
+	nlreq.hdr.nlmsg_type = RTM_GETNEIGH;
+	nlreq.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
+	nlreq.msg.ndm_family = ai_family;
+
+	return rtnl_open(&nlreq.hdr, NETLINK_ROUTE);
+}
+
 static ssize_t resolve_mac_from_cache_dump(int sock, void **buf, size_t *buflen)
 {
 	struct iovec iov;
-- 
1.8.5.2


             reply	other threads:[~2014-01-05 21:37 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-05 21:37 Antonio Quartulli [this message]
2014-01-05 21:37 ` [B.A.T.M.A.N.] [PATCHv2 next 2/3] batctl: implement vlan-to-link helper functions Antonio Quartulli
2014-01-05 21:37 ` [B.A.T.M.A.N.] [PATCHv2 next 3/3] batctl: add sys framework for VLAN support Antonio Quartulli

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=1388957835-2421-1-git-send-email-antonio@meshcoding.com \
    --to=antonio@meshcoding.com \
    --cc=b.a.t.m.a.n@lists.open-mesh.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.