diff mbox

[PATCHv2,libmnl] socket: creating a struct mnl_socket from a pre-existing socket

Message ID 20140925003327.GB20985@gmail.com
State Accepted
Delegated to: Pablo Neira
Headers show

Commit Message

Ken-ichirou MATSUZAWA Sept. 25, 2014, 12:33 a.m. UTC
This patch defines a new function mnl_socket_fdopen() which creates a
struct mnl_socket object from a pre-existing socket like obtained from
other process, different domain/type from the same prodess.

Signed-off-by: Ken-ichirou MATSUZAWA <chamas@h4.dion.ne.jp>
---
 include/libmnl/libmnl.h |  1 +
 src/libmnl.map          |  4 ++++
 src/socket.c            | 31 +++++++++++++++++++++++++++++++
 3 files changed, 36 insertions(+)

Comments

Pablo Neira Ayuso Sept. 25, 2014, 10:49 a.m. UTC | #1
On Thu, Sep 25, 2014 at 09:33:27AM +0900, Ken-ichirou MATSUZAWA wrote:
> This patch defines a new function mnl_socket_fdopen() which creates a
> struct mnl_socket object from a pre-existing socket like obtained from
> other process, different domain/type from the same prodess.

Applied, thanks.

--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/include/libmnl/libmnl.h b/include/libmnl/libmnl.h
index 223709c..0de6678 100644
--- a/include/libmnl/libmnl.h
+++ b/include/libmnl/libmnl.h
@@ -22,6 +22,7 @@  extern "C" {
 struct mnl_socket;
 
 extern struct mnl_socket *mnl_socket_open(int type);
+extern struct mnl_socket *mnl_socket_fdopen(int fd);
 extern int mnl_socket_bind(struct mnl_socket *nl, unsigned int groups, pid_t pid);
 extern int mnl_socket_close(struct mnl_socket *nl);
 extern int mnl_socket_get_fd(const struct mnl_socket *nl);
diff --git a/src/libmnl.map b/src/libmnl.map
index dbc332e..3c147a7 100644
--- a/src/libmnl.map
+++ b/src/libmnl.map
@@ -72,3 +72,7 @@  local: *;
 LIBMNL_1.1 {
   mnl_attr_parse_payload;
 } LIBMNL_1.0;
+
+LIBMNL_1.2 {
+  mnl_socket_fdopen;
+} LIBMNL_1.1;
diff --git a/src/socket.c b/src/socket.c
index 676a08a..0542f8f 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -129,6 +129,37 @@  struct mnl_socket *mnl_socket_open(int bus)
 EXPORT_SYMBOL(mnl_socket_open);
 
 /**
+ * mnl_socket_fdopen - associates a mnl_socket object with pre-existing socket.
+ * \param fd pre-existing socket descriptor.
+ *
+ * On error, it returns NULL and errno is appropriately set. Otherwise, it
+ * returns a valid pointer to the mnl_socket structure. It also set address if
+ * the fd is AF_NETLINK.
+ */
+struct mnl_socket *mnl_socket_fdopen(int fd)
+{
+	int ret;
+	struct mnl_socket *nl;
+	struct sockaddr_nl addr;
+	socklen_t addr_len = sizeof(struct sockaddr_nl);
+
+	ret = getsockname(fd, (struct sockaddr *) &addr, &addr_len);
+	if (ret == -1)
+		return NULL;
+
+	nl = calloc(sizeof(struct mnl_socket), 1);
+	if (nl == NULL)
+		return NULL;
+
+	nl->fd = fd;
+	if (addr.nl_family == AF_NETLINK)
+		nl->addr = addr;
+
+	return nl;
+}
+EXPORT_SYMBOL(mnl_socket_fdopen);
+
+/**
  * mnl_socket_bind - bind netlink socket
  * \param nl netlink socket obtained via mnl_socket_open()
  * \param groups the group of message you're interested in