From 5b8a0c7479776f7781e497dd5fdacc17d9aad0ae Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Mon, 15 Jul 2013 17:45:32 +0200 Subject: include: don't force compiler to enter C++11 mode Thomas reports that g++ forces you to enable C++11 mode if you want to use . Tested with g++ (GCC) 4.4.4. He reports that this compiles fine without the special headers. So let's get rid of them. This reverts bb8c40e. Reported-by: Thomas Jarosch Signed-off-by: Pablo Neira Ayuso Tested-by: Thomas Jarosch --- include/libmnl/libmnl.h | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) (limited to 'include/libmnl') diff --git a/include/libmnl/libmnl.h b/include/libmnl/libmnl.h index a647fd9..223709c 100644 --- a/include/libmnl/libmnl.h +++ b/include/libmnl/libmnl.h @@ -1,14 +1,9 @@ #ifndef _LIBMNL_H_ #define _LIBMNL_H_ -#ifdef __cplusplus -# include -# include -#else -# include /* not in C++ */ -# include -# include -#endif +#include +#include +#include #include #include /* for sa_family_t */ #include -- cgit v1.2.3 From e374664f48724e5b13a848bad5c5353349f0ae38 Mon Sep 17 00:00:00 2001 From: Ken-ichirou MATSUZAWA Date: Thu, 25 Sep 2014 09:33:27 +0900 Subject: 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 Signed-off-by: Pablo Neira Ayuso --- include/libmnl/libmnl.h | 1 + src/libmnl.map | 4 ++++ src/socket.c | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) (limited to 'include/libmnl') 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 0c3cd72..86657d4 100644 --- a/src/socket.c +++ b/src/socket.c @@ -128,6 +128,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() -- cgit v1.2.3 From 9ac5b6fb437e24c328a30776e30c39b906b06f66 Mon Sep 17 00:00:00 2001 From: Markus Teich Date: Thu, 26 Feb 2015 14:38:03 +0100 Subject: header: use sysconf() instead of getpagesize() The sysconf(_SC_PAGESIZE) call is more portable since it does not need _BSD_SOURCE defined. Signed-off-by: Pablo Neira Ayuso --- include/libmnl/libmnl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/libmnl') diff --git a/include/libmnl/libmnl.h b/include/libmnl/libmnl.h index 0de6678..3a589bc 100644 --- a/include/libmnl/libmnl.h +++ b/include/libmnl/libmnl.h @@ -17,7 +17,7 @@ extern "C" { */ #define MNL_SOCKET_AUTOPID 0 -#define MNL_SOCKET_BUFFER_SIZE (getpagesize() < 8192L ? getpagesize() : 8192L) +#define MNL_SOCKET_BUFFER_SIZE (sysconf(_SC_PAGESIZE) < 8192L ? sysconf(_SC_PAGESIZE) : 8192L) struct mnl_socket; -- cgit v1.2.3 From 1891e0e2cefced50e7bfdacd50942cefe5bf73ba Mon Sep 17 00:00:00 2001 From: Guillaume Nault Date: Fri, 2 Oct 2015 22:12:33 +0200 Subject: socket: introduce mnl_socket_open2() Define mnl_socket_open2() so that user can pass a set of SOCK_* flags at socket creation time. Signed-off-by: Guillaume Nault Signed-off-by: Pablo Neira Ayuso --- include/libmnl/libmnl.h | 3 ++- src/libmnl.map | 1 + src/socket.c | 41 ++++++++++++++++++++++++++++++++--------- 3 files changed, 35 insertions(+), 10 deletions(-) (limited to 'include/libmnl') diff --git a/include/libmnl/libmnl.h b/include/libmnl/libmnl.h index 3a589bc..5adb13c 100644 --- a/include/libmnl/libmnl.h +++ b/include/libmnl/libmnl.h @@ -21,7 +21,8 @@ extern "C" { struct mnl_socket; -extern struct mnl_socket *mnl_socket_open(int type); +extern struct mnl_socket *mnl_socket_open(int bus); +extern struct mnl_socket *mnl_socket_open2(int bus, int flags); 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); diff --git a/src/libmnl.map b/src/libmnl.map index 3c147a7..e5920e5 100644 --- a/src/libmnl.map +++ b/src/libmnl.map @@ -74,5 +74,6 @@ LIBMNL_1.1 { } LIBMNL_1.0; LIBMNL_1.2 { + mnl_socket_open2; mnl_socket_fdopen; } LIBMNL_1.1; diff --git a/src/socket.c b/src/socket.c index 86657d4..d63ab87 100644 --- a/src/socket.c +++ b/src/socket.c @@ -103,14 +103,7 @@ unsigned int mnl_socket_get_portid(const struct mnl_socket *nl) } EXPORT_SYMBOL(mnl_socket_get_portid); -/** - * mnl_socket_open - open a netlink socket - * \param bus the netlink socket bus ID (see NETLINK_* constants) - * - * 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_open(int bus) +static struct mnl_socket *__mnl_socket_open(int bus, int flags) { struct mnl_socket *nl; @@ -118,7 +111,7 @@ struct mnl_socket *mnl_socket_open(int bus) if (nl == NULL) return NULL; - nl->fd = socket(AF_NETLINK, SOCK_RAW, bus); + nl->fd = socket(AF_NETLINK, SOCK_RAW | flags, bus); if (nl->fd == -1) { free(nl); return NULL; @@ -126,8 +119,38 @@ struct mnl_socket *mnl_socket_open(int bus) return nl; } + +/** + * mnl_socket_open - open a netlink socket + * \param bus the netlink socket bus ID (see NETLINK_* constants) + * + * 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_open(int bus) +{ + return __mnl_socket_open(bus, 0); +} EXPORT_SYMBOL(mnl_socket_open); +/** + * mnl_socket_open2 - open a netlink socket with appropriate flags + * \param bus the netlink socket bus ID (see NETLINK_* constants) + * \param flags the netlink socket flags (see SOCK_* constants in socket(2)) + * + * This is similar to mnl_socket_open(), but allows to set flags like + * SOCK_CLOEXEC at socket creation time (useful for multi-threaded programs + * performing exec calls). + * + * 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_open2(int bus, int flags) +{ + return __mnl_socket_open(bus, flags); +} +EXPORT_SYMBOL(mnl_socket_open2); + /** * mnl_socket_fdopen - associates a mnl_socket object with pre-existing socket. * \param fd pre-existing socket descriptor. -- cgit v1.2.3