From e4192ff97f3fccbe8347e88139aca9ba49af29ab Mon Sep 17 00:00:00 2001 From: Thomas Graf Date: Tue, 9 Oct 2012 16:15:08 +0200 Subject: [PATCH] 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 --- include/netlink-types.h | 1 + include/netlink/socket.h | 2 ++ lib/nl.c | 2 +- lib/socket.c | 30 ++++++++++++++++++++++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) 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 @@ -586,6 +586,36 @@ int nl_socket_set_buffer_size(struct nl_sock *sk, int rxbuf, int txbuf) return 0; } +/** + * 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.