netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH libmnl] socket: creating a struct mnl_socket from a pre-existing socket
@ 2014-09-20  6:05 Ken-ichirou MATSUZAWA
  2014-09-22 10:24 ` Pablo Neira Ayuso
  2014-09-25  0:33 ` [PATCHv2 " Ken-ichirou MATSUZAWA
  0 siblings, 2 replies; 5+ messages in thread
From: Ken-ichirou MATSUZAWA @ 2014-09-20  6:05 UTC (permalink / raw)
  To: The netfilter developer mailinglist; +Cc: Florian Westphal

This patch defines a new function mnl_socket_fdopen() which
creates a struct mnl_socket object from a pre-existing netlink
socket obtained from other process. Now I think of the socket
is obtained from child process via send/recvmsg().

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

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..1ea8b8e 100644
--- a/src/libmnl.map
+++ b/src/libmnl.map
@@ -71,4 +71,5 @@ local: *;
 
 LIBMNL_1.1 {
   mnl_attr_parse_payload;
+  mnl_socket_fdopen;
 } LIBMNL_1.0;
diff --git a/src/socket.c b/src/socket.c
index 676a08a..bb9db18 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -129,6 +129,26 @@ struct mnl_socket *mnl_socket_open(int bus)
 EXPORT_SYMBOL(mnl_socket_open);
 
 /**
+ * mnl_socket_fdopen - associates a mnl_socket object with netlink socket.
+ * \param fd pre-existing netlink socket, obtained from other process.
+ *
+ * On error, it returns NULL and errno is appropriately set. Otherwise, it
+ * returns a valid pointer to the mnl_socket structure.
+ */
+struct mnl_socket *mnl_socket_fdopen(int fd)
+{
+	struct mnl_socket *nl;
+
+	nl = calloc(sizeof(struct mnl_socket), 1);
+	if (nl == NULL)
+		return NULL;
+
+	nl->fd = fd;
+	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
-- 
2.1.0


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

* Re: [PATCH libmnl] socket: creating a struct mnl_socket from a pre-existing socket
  2014-09-20  6:05 [PATCH libmnl] socket: creating a struct mnl_socket from a pre-existing socket Ken-ichirou MATSUZAWA
@ 2014-09-22 10:24 ` Pablo Neira Ayuso
  2014-09-22 10:54   ` Pablo Neira Ayuso
  2014-09-25  0:33 ` [PATCHv2 " Ken-ichirou MATSUZAWA
  1 sibling, 1 reply; 5+ messages in thread
From: Pablo Neira Ayuso @ 2014-09-22 10:24 UTC (permalink / raw)
  To: Ken-ichirou MATSUZAWA
  Cc: The netfilter developer mailinglist, Florian Westphal

On Sat, Sep 20, 2014 at 03:05:37PM +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 netlink
> socket obtained from other process. Now I think of the socket
> is obtained from child process via send/recvmsg().

OK, you can also use this to send netlink messages using different
domain/type sockets from the same process too, I'm going to attach
this to the description.

> Signed-off-by: Ken-ichirou MATSUZAWA <chamas@h4.dion.ne.jp>
> ---
>  include/libmnl/libmnl.h |  1 +
>  src/libmnl.map          |  1 +
>  src/socket.c            | 20 ++++++++++++++++++++
>  3 files changed, 22 insertions(+)
> 
> 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..1ea8b8e 100644
> --- a/src/libmnl.map
> +++ b/src/libmnl.map
> @@ -71,4 +71,5 @@ local: *;
>  
>  LIBMNL_1.1 {
>    mnl_attr_parse_payload;
> +  mnl_socket_fdopen;

I'm going to fix this and apply. mnl_socket_fdopen doesn't belong
here.

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

* Re: [PATCH libmnl] socket: creating a struct mnl_socket from a pre-existing socket
  2014-09-22 10:24 ` Pablo Neira Ayuso
@ 2014-09-22 10:54   ` Pablo Neira Ayuso
  0 siblings, 0 replies; 5+ messages in thread
From: Pablo Neira Ayuso @ 2014-09-22 10:54 UTC (permalink / raw)
  To: Ken-ichirou MATSUZAWA
  Cc: The netfilter developer mailinglist, Florian Westphal

On Mon, Sep 22, 2014 at 12:24:32PM +0200, Pablo Neira Ayuso wrote:
> On Sat, Sep 20, 2014 at 03:05:37PM +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 netlink
> > socket obtained from other process. Now I think of the socket
> > is obtained from child process via send/recvmsg().
> 
> OK, you can also use this to send netlink messages using different
> domain/type sockets from the same process too, I'm going to attach
> this to the description.

I just noticed that, for your usecase, nl->addr is going to be left
unset. So getsockname() is also needed to set nl->addr if the socket
is of netlink type, otherwise, the portID zero should already tell us
that this is not a netlink socket, since this portID is reserved to
the kernel when netlink is used.

Could you address this and resend the patch including my comments in
the previous email? Thanks.


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

* [PATCHv2 libmnl] socket: creating a struct mnl_socket from a pre-existing socket
  2014-09-20  6:05 [PATCH libmnl] socket: creating a struct mnl_socket from a pre-existing socket Ken-ichirou MATSUZAWA
  2014-09-22 10:24 ` Pablo Neira Ayuso
@ 2014-09-25  0:33 ` Ken-ichirou MATSUZAWA
  2014-09-25 10:49   ` Pablo Neira Ayuso
  1 sibling, 1 reply; 5+ messages in thread
From: Ken-ichirou MATSUZAWA @ 2014-09-25  0:33 UTC (permalink / raw)
  To: The netfilter developer mailinglist; +Cc: Florian Westphal, Pablo Neira Ayuso

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(+)

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
-- 
2.1.0


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

* Re: [PATCHv2 libmnl] socket: creating a struct mnl_socket from a pre-existing socket
  2014-09-25  0:33 ` [PATCHv2 " Ken-ichirou MATSUZAWA
@ 2014-09-25 10:49   ` Pablo Neira Ayuso
  0 siblings, 0 replies; 5+ messages in thread
From: Pablo Neira Ayuso @ 2014-09-25 10:49 UTC (permalink / raw)
  To: Ken-ichirou MATSUZAWA
  Cc: The netfilter developer mailinglist, Florian Westphal

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.


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

end of thread, other threads:[~2014-09-25 10:48 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-09-20  6:05 [PATCH libmnl] socket: creating a struct mnl_socket from a pre-existing socket Ken-ichirou MATSUZAWA
2014-09-22 10:24 ` Pablo Neira Ayuso
2014-09-22 10:54   ` Pablo Neira Ayuso
2014-09-25  0:33 ` [PATCHv2 " Ken-ichirou MATSUZAWA
2014-09-25 10:49   ` Pablo Neira Ayuso

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).