socket: creating a struct mnl_socket from a pre-existing socket
PATCH 1/1 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 netlink socket obtained from other process. Now I think of the socket is obtained from child process via send/recvmsg().
--- include/libmnl/libmnl.h | 1 + src/libmnl.map | 1 + src/socket.c | 20 ++++++++++++++++++++ 3 files changed, 22 insertions(+)
include/libmnl/libmnl.h +1 -0
@@ -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);
src/libmnl.map +1 -0
@@ -71,4 +71,5 @@ local: *; LIBMNL_1.1 { mnl_attr_parse_payload; + mnl_socket_fdopen; } LIBMNL_1.0;
src/socket.c +20 -0
@@ -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
Signature · 1 line
2.1.0
Pablo Neira Ayuso
maintainerQuoted 4 lines · Ken-ichirou MATSUZAWA
This patch defines a new function
mnl_socket_fdopen()which creates a structmnl_socketobject 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.
Quoted 24 lines
Signed-off-by Ken-ichirou MATSUZAWA <[email protected]>--- include/libmnl/libmnl.h | 1 + src/libmnl.map | 1 + src/socket.c | 20 ++++++++++++++++++++ 3 files changed, 22 insertions(+)include/libmnl/libmnl.h +1
-0@@ -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);src/libmnl.map +0
-0
src/libmnl.map local: *; · line 74
Quoted 4 lines
src/libmnl.map +1
-0@@ -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.
Pablo Neira Ayuso
maintainerQuoted 9 lines · Pablo Neira Ayuso
Quoted 4 lines · Ken-ichirou MATSUZAWA
This patch defines a new function
mnl_socket_fdopen()which creates a structmnl_socketobject 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.
PATCH v2 1/1 socket: creating a struct mnl_socket from a pre-existing socket
In reply to Ken-ichirou MATSUZAWAThis 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.
--- include/libmnl/libmnl.h | 1 + src/libmnl.map | 4 ++++ src/socket.c | 31 +++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+)
include/libmnl/libmnl.h +1 -0
@@ -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);
src/libmnl.map +4 -0
@@ -72,3 +72,7 @@ local: *; LIBMNL_1.1 { mnl_attr_parse_payload; } LIBMNL_1.0; + +LIBMNL_1.2 { + mnl_socket_fdopen; +} LIBMNL_1.1;
src/socket.c +31 -0
@@ -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
Signature · 1 line
2.1.0
Quoted 3 lines · Ken-ichirou MATSUZAWA
This patch defines a new function
mnl_socket_fdopen()which creates a structmnl_socketobject from a pre-existing socket like obtained from other process, different domain/type from the same prodess.
Applied, thanks.