summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Graf <tgraf@redhat.com>2012-10-09 16:15:08 +0200
committerThomas Graf <tgraf@redhat.com>2012-10-09 16:16:54 +0200
commite4192ff97f3fccbe8347e88139aca9ba49af29ab (patch)
tree0d68cae2f3ba9c72f94ed7c2e0f404c80b247d66
parent72c2cb9e299b0510ae76e90b0b4fcc040ab485c7 (diff)
downloadlibnl-e4192ff97f3fccbe8347e88139aca9ba49af29ab.tar.gz
nl: Provide API to specify the default buffer size when receiving netlink messages
New functions: nl_socket_set_msg_buf_size(sk, size) nl_socket_get_msg_buf_size(sk) Default remains getpagesize() Signed-off-by: Thomas Graf <tgraf@redhat.com>
-rw-r--r--include/netlink-types.h1
-rw-r--r--include/netlink/socket.h2
-rw-r--r--lib/nl.c2
-rw-r--r--lib/socket.c30
4 files changed, 34 insertions, 1 deletions
diff --git a/include/netlink-types.h b/include/netlink-types.h
index 2e80b05..e885571 100644
--- a/include/netlink-types.h
+++ b/include/netlink-types.h
@@ -69,6 +69,7 @@ struct nl_sock
unsigned int s_seq_expect;
int s_flags;
struct nl_cb * s_cb;
+ size_t s_bufsize;
};
struct nl_cache
diff --git a/include/netlink/socket.h b/include/netlink/socket.h
index d0f5a6a..1007eba 100644
--- a/include/netlink/socket.h
+++ b/include/netlink/socket.h
@@ -49,6 +49,8 @@ extern int nl_socket_modify_err_cb(struct nl_sock *, enum nl_cb_kind,
nl_recvmsg_err_cb_t, void *);
extern int nl_socket_set_buffer_size(struct nl_sock *, int, int);
+extern int nl_socket_set_msg_buf_size(struct nl_sock *, size_t);
+extern size_t nl_socket_get_msg_buf_size(struct nl_sock *);
extern int nl_socket_set_passcred(struct nl_sock *, int);
extern int nl_socket_recv_pktinfo(struct nl_sock *, int);
diff --git a/lib/nl.c b/lib/nl.c
index ea3e087..5b37c2a 100644
--- a/lib/nl.c
+++ b/lib/nl.c
@@ -447,7 +447,7 @@ int nl_recv(struct nl_sock *sk, struct sockaddr_nl *nla,
if (page_size == 0)
page_size = getpagesize();
- iov.iov_len = page_size;
+ iov.iov_len = sk->s_bufsize ? : page_size;
iov.iov_base = *buf = malloc(iov.iov_len);
if (sk->s_flags & NL_SOCK_PASSCRED) {
diff --git a/lib/socket.c b/lib/socket.c
index 7e3ebf6..d53a714 100644
--- a/lib/socket.c
+++ b/lib/socket.c
@@ -587,6 +587,36 @@ int nl_socket_set_buffer_size(struct nl_sock *sk, int rxbuf, int txbuf)
}
/**
+ * Set default message buffer size of netlink socket.
+ * @arg sk Netlink socket.
+ * @arg bufsize Default message buffer size in bytes.
+ *
+ * Sets the default message buffer size to the specified length in bytes.
+ * The default message buffer size limits the maximum message size the
+ * socket will be able to receive. It is generally recommneded to specify
+ * a buffer size no less than the size of a memory page.
+ *
+ * @return 0 on success or a negative error code.
+ */
+int nl_socket_set_msg_buf_size(struct nl_sock *sk, size_t bufsize)
+{
+ sk->s_bufsize = bufsize;
+
+ return 0;
+}
+
+/**
+ * Get default message buffer size of netlink socket.
+ * @arg sk Netlink socket.
+ *
+ * @return Size of default message buffer.
+ */
+size_t nl_socket_get_msg_buf_size(struct nl_sock *sk)
+{
+ return sk->s_bufsize;
+}
+
+/**
* Enable/disable credential passing on netlink socket.
* @arg sk Netlink socket.
* @arg state New state (0 - disabled, 1 - enabled)