lib: Add msghdr_extract_fds
authorVolker Lendecke <vl@samba.org>
Wed, 31 Dec 2014 12:14:41 +0000 (13:14 +0100)
committerJeremy Allison <jra@samba.org>
Mon, 5 Jan 2015 23:33:09 +0000 (00:33 +0100)
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>
source3/lib/msghdr.c
source3/lib/msghdr.h

index 9d5f28bbb3a7c85062b31a26f130c37d1692c2ea..3449579b4389243c66cc141e656a127baac055ea 100644 (file)
@@ -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;
+}
index af9506c625c4348f8988a7a7fd8c934a0c2c2e84..88829238a2bf2b6ab658c3fee35e103219369de0 100644 (file)
@@ -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