summaryrefslogtreecommitdiff
path: root/src/socket.c
diff options
context:
space:
mode:
authorKen-ichirou MATSUZAWA <chamaken@gmail.com>2014-09-25 09:33:27 +0900
committerPablo Neira Ayuso <pablo@netfilter.org>2014-09-25 12:46:13 +0200
commite374664f48724e5b13a848bad5c5353349f0ae38 (patch)
tree3817b5e34fafa9e9ef38ca7edbe67df5ce21bea8 /src/socket.c
parent2c458b2eb479cbc83c83de79dcd14bec6acc90bd (diff)
downloadlibmnl-e374664f48724e5b13a848bad5c5353349f0ae38.tar.gz
socket: creating a struct mnl_socket from a pre-existing socket
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 and different domain/type from the same prodess. Signed-off-by: Ken-ichirou MATSUZAWA <chamas@h4.dion.ne.jp> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'src/socket.c')
-rw-r--r--src/socket.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/socket.c b/src/socket.c
index 0c3cd72..86657d4 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -129,6 +129,40 @@ 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 sets the portID
+ * if the socket fd is already bound and it is AF_NETLINK.
+ *
+ * Note that mnl_socket_get_portid() returns 0 if this function is used with
+ * non-netlink socket.
+ */
+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(1, sizeof(struct mnl_socket));
+ 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