lib: Add msghdr.[ch]
authorVolker Lendecke <vl@samba.org>
Tue, 30 Dec 2014 13:36:46 +0000 (13:36 +0000)
committerJeremy Allison <jra@samba.org>
Mon, 5 Jan 2015 23:33:09 +0000 (00:33 +0100)
This is a little set of routines to deal with the ugly fd-passing macros.

This patch is the first step assisting the creation of msghrds for sending fds.
Receiving fd helpers will follow later.

The basic idea behind these routines is that they fill a variable-sized buffer.
They are supposed to be called twice per msghdr preparation. First with a
0-sized NULL output buffer to calculate the required bufsize, and then a second
time filling in the buffer as such.

This does not take care of the old msg_accrights way of passing file
descriptors. CMSG/SCM_RIGHTS is standardized for quite a while now, and I
believe this intreface can be made to also take care of msg_accrights if
needed.

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
source3/lib/msghdr.c [new file with mode: 0644]
source3/lib/msghdr.h [new file with mode: 0644]
source3/wscript_build

diff --git a/source3/lib/msghdr.c b/source3/lib/msghdr.c
new file mode 100644 (file)
index 0000000..9d5f28b
--- /dev/null
@@ -0,0 +1,127 @@
+/*
+ * Unix SMB/CIFS implementation.
+ * Copyright (C) Volker Lendecke 2014
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "replace.h"
+#include "lib/msghdr.h"
+#include "lib/iov_buf.h"
+#include <sys/socket.h>
+
+ssize_t msghdr_prep_fds(struct msghdr *msg, uint8_t *buf, size_t bufsize,
+                       const int *fds, size_t num_fds)
+{
+       size_t fds_size = sizeof(int) * MIN(num_fds, INT8_MAX);
+       size_t cmsg_len = CMSG_LEN(fds_size);
+       size_t cmsg_space = CMSG_SPACE(fds_size);
+       struct cmsghdr *cmsg;
+       void *fdptr;
+
+       if (num_fds == 0) {
+               if (msg != NULL) {
+                       msg->msg_control = NULL;
+                       msg->msg_controllen = 0;
+               }
+               return 0;
+       }
+       if (num_fds > INT8_MAX) {
+               return -1;
+       }
+       if (cmsg_space > bufsize) {
+               return cmsg_space;
+       }
+
+       msg->msg_control = buf;
+       msg->msg_controllen = cmsg_space;
+
+       cmsg = CMSG_FIRSTHDR(msg);
+       cmsg->cmsg_level = SOL_SOCKET;
+       cmsg->cmsg_type = SCM_RIGHTS;
+       cmsg->cmsg_len = cmsg_len;
+       fdptr = CMSG_DATA(cmsg);
+       memcpy(fdptr, fds, fds_size);
+       msg->msg_controllen = cmsg->cmsg_len;
+
+       return cmsg_space;
+}
+
+struct msghdr_buf {
+       struct msghdr msg;
+       struct sockaddr_storage addr;
+       struct iovec iov;
+       uint8_t buf[];
+};
+
+ssize_t msghdr_copy(struct msghdr_buf *msg, size_t msgsize,
+                   const void *addr, socklen_t addrlen,
+                   const struct iovec *iov, int iovcnt,
+                   const int *fds, size_t num_fds)
+{
+       size_t fd_len, iov_len, needed, bufsize;
+
+       bufsize = (msgsize > offsetof(struct msghdr_buf, buf)) ?
+               msgsize - offsetof(struct msghdr_buf, buf) : 0;
+
+       fd_len = msghdr_prep_fds(&msg->msg, msg->buf, bufsize, fds, num_fds);
+
+       if (bufsize >= fd_len) {
+               bufsize -= fd_len;
+       } else {
+               bufsize = 0;
+       }
+
+       if (msg != NULL) {
+
+               if (addr != NULL) {
+                       if (addrlen > sizeof(struct sockaddr_storage)) {
+                               errno = EMSGSIZE;
+                               return -1;
+                       }
+                       memcpy(&msg->addr, addr, addrlen);
+                       msg->msg.msg_name = &msg->addr;
+                       msg->msg.msg_namelen = addrlen;
+               } else {
+                       msg->msg.msg_name = NULL;
+                       msg->msg.msg_namelen = 0;
+               }
+
+               msg->iov.iov_base = msg->buf + fd_len;
+               msg->iov.iov_len = iov_buf(
+                       iov, iovcnt, msg->iov.iov_base, bufsize);
+               iov_len = msg->iov.iov_len;
+
+               msg->msg.msg_iov = &msg->iov;
+               msg->msg.msg_iovlen = 1;
+       } else {
+               iov_len = iov_buflen(iov, iovcnt);
+       }
+
+       needed = offsetof(struct msghdr_buf, buf) + fd_len;
+       if (needed < fd_len) {
+               return -1;
+       }
+       needed += iov_len;
+       if (needed < iov_len) {
+               return -1;
+       }
+
+       return needed;
+}
+
+struct msghdr *msghdr_buf_msghdr(struct msghdr_buf *msg)
+{
+       return &msg->msg;
+}
diff --git a/source3/lib/msghdr.h b/source3/lib/msghdr.h
new file mode 100644 (file)
index 0000000..af9506c
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Unix SMB/CIFS implementation.
+ * Copyright (C) Volker Lendecke 2014
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __LIB_MSGHDR_H__
+#define __LIB_MSGHDR_H__
+
+#include <stddef.h>
+#include <stdint.h>
+#include <sys/uio.h>
+#include <sys/socket.h>
+
+ssize_t msghdr_prep_fds(struct msghdr *msg, uint8_t *buf, size_t bufsize,
+                       const int *fds, size_t num_fds);
+
+struct msghdr_buf;
+
+ssize_t msghdr_copy(struct msghdr_buf *msg, size_t msgsize,
+                   const void *addr, socklen_t addrlen,
+                   const struct iovec *iov, int iovcnt,
+                   const int *fds, size_t num_fds);
+struct msghdr *msghdr_buf_msghdr(struct msghdr_buf *msg);
+
+#endif
index dc6b196b857e66b914e727079a7a4be637bc73c6..a6ef5849a447009907c254476c697f94b0ce6799 100755 (executable)
@@ -774,6 +774,11 @@ bld.SAMBA3_SUBSYSTEM('tdb-wrap3',
                     source='lib/util_tdb.c',
                     deps='talloc samba3-util')
 
+bld.SAMBA3_LIBRARY('msghdr',
+                   source='lib/msghdr.c',
+                   deps='replace iov_buf',
+                   private_library=True)
+
 bld.SAMBA3_LIBRARY('samba3-util',
                    source='''lib/util_sec.c lib/util_str.c lib/adt_tree.c lib/util_malloc.c lib/namearray.c lib/file_id.c''',
                    deps='samba-util charset',