summaryrefslogtreecommitdiffstats
path: root/source3/lib
diff options
context:
space:
mode:
authorVolker Lendecke <vl@samba.org>2014-12-31 13:14:41 +0100
committerJeremy Allison <jra@samba.org>2015-01-06 00:33:09 +0100
commitcd46f7685b47bb40e2f794ec7380b8078627d87f (patch)
treebc40f9b18baae780fa9b486d5b189274ce4f47e3 /source3/lib
parent9bd7e52db0386b928a3d777e519ee23f2a0fedfb (diff)
downloadsamba-cd46f7685b47bb40e2f794ec7380b8078627d87f.tar.gz
samba-cd46f7685b47bb40e2f794ec7380b8078627d87f.tar.xz
samba-cd46f7685b47bb40e2f794ec7380b8078627d87f.zip
lib: Add msghdr_extract_fds
This is a copy of the extract_fd_array_from_msghdr routine in unix_msg.c, with a similar use pattern: First call it without an output array to get the length and then call it a second time to actually fill in the array. Signed-off-by: Volker Lendecke <vl@samba.org> Reviewed-by: Ralph Boehme <slow@samba.org>
Diffstat (limited to 'source3/lib')
-rw-r--r--source3/lib/msghdr.c28
-rw-r--r--source3/lib/msghdr.h2
2 files changed, 30 insertions, 0 deletions
diff --git a/source3/lib/msghdr.c b/source3/lib/msghdr.c
index 9d5f28bbb3..3449579b43 100644
--- a/source3/lib/msghdr.c
+++ b/source3/lib/msghdr.c
@@ -125,3 +125,31 @@ struct msghdr *msghdr_buf_msghdr(struct msghdr_buf *msg)
{
return &msg->msg;
}
+
+size_t msghdr_extract_fds(struct msghdr *msg, int *fds, size_t fds_size)
+{
+ struct cmsghdr *cmsg;
+ size_t num_fds;
+
+ for(cmsg = CMSG_FIRSTHDR(msg);
+ cmsg != NULL;
+ cmsg = CMSG_NXTHDR(msg, cmsg))
+ {
+ if ((cmsg->cmsg_type == SCM_RIGHTS) &&
+ (cmsg->cmsg_level == SOL_SOCKET)) {
+ break;
+ }
+ }
+
+ if (cmsg == NULL) {
+ return 0;
+ }
+
+ num_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
+
+ if ((num_fds != 0) && (fds_size >= num_fds)) {
+ memcpy(fds, CMSG_DATA(cmsg), num_fds * sizeof(int));
+ }
+
+ return num_fds;
+}
diff --git a/source3/lib/msghdr.h b/source3/lib/msghdr.h
index af9506c625..88829238a2 100644
--- a/source3/lib/msghdr.h
+++ b/source3/lib/msghdr.h
@@ -35,4 +35,6 @@ ssize_t msghdr_copy(struct msghdr_buf *msg, size_t msgsize,
const int *fds, size_t num_fds);
struct msghdr *msghdr_buf_msghdr(struct msghdr_buf *msg);
+size_t msghdr_extract_fds(struct msghdr *msg, int *fds, size_t num_fds);
+
#endif