diff options
| author | Volker Lendecke <vl@samba.org> | 2014-11-19 14:21:17 +0000 |
|---|---|---|
| committer | Jeremy Allison <jra@samba.org> | 2014-12-07 00:12:07 +0100 |
| commit | 214fc09a3443ab62772666a985975fa548b215fa (patch) | |
| tree | 3ff90a94c972deabf6a200de528e1ec1eeadfc64 /source3/lib | |
| parent | a8491cb95a2c181d40b0b94cceff8d69d137935c (diff) | |
lib: Split out iov_buf[len]
Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
Diffstat (limited to 'source3/lib')
| -rw-r--r-- | source3/lib/iov_buf.c | 65 | ||||
| -rw-r--r-- | source3/lib/iov_buf.h | 29 | ||||
| -rw-r--r-- | source3/lib/messages.c | 1 | ||||
| -rw-r--r-- | source3/lib/messages_ctdbd.c | 1 | ||||
| -rw-r--r-- | source3/lib/util_sock.c | 44 |
5 files changed, 97 insertions, 43 deletions
diff --git a/source3/lib/iov_buf.c b/source3/lib/iov_buf.c new file mode 100644 index 0000000000..dd99da3625 --- /dev/null +++ b/source3/lib/iov_buf.c @@ -0,0 +1,65 @@ +/* + * Unix SMB/CIFS implementation. + * Samba system utilities + * 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 "system/filesys.h" +#include "iov_buf.h" + +ssize_t iov_buflen(const struct iovec *iov, int iovcnt) +{ + size_t buflen = 0; + int i; + + for (i=0; i<iovcnt; i++) { + size_t thislen = iov[i].iov_len; + size_t tmp = buflen + thislen; + + if ((tmp < buflen) || (tmp < thislen)) { + /* overflow */ + return -1; + } + buflen = tmp; + } + return buflen; +} + +uint8_t *iov_buf(TALLOC_CTX *mem_ctx, const struct iovec *iov, int iovcnt) +{ + int i; + ssize_t buflen; + uint8_t *buf, *p; + + buflen = iov_buflen(iov, iovcnt); + if (buflen == -1) { + return NULL; + } + buf = talloc_array(mem_ctx, uint8_t, buflen); + if (buf == NULL) { + return NULL; + } + + p = buf; + for (i=0; i<iovcnt; i++) { + size_t len = iov[i].iov_len; + + memcpy(p, iov[i].iov_base, len); + p += len; + } + return buf; +} diff --git a/source3/lib/iov_buf.h b/source3/lib/iov_buf.h new file mode 100644 index 0000000000..a884bdbe0e --- /dev/null +++ b/source3/lib/iov_buf.h @@ -0,0 +1,29 @@ +/* + * Unix SMB/CIFS implementation. + * Samba system utilities + * 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_IOV_BUF_H__ +#define __LIB_IOV_BUF_H__ + +#include <unistd.h> +#include <talloc.h> + +ssize_t iov_buflen(const struct iovec *iov, int iovlen); +uint8_t *iov_buf(TALLOC_CTX *mem_ctx, const struct iovec *iov, int iovcnt); + +#endif diff --git a/source3/lib/messages.c b/source3/lib/messages.c index d4c580fdd2..5b4daa2932 100644 --- a/source3/lib/messages.c +++ b/source3/lib/messages.c @@ -52,6 +52,7 @@ #include "lib/util/tevent_unix.h" #include "lib/background.h" #include "lib/messages_dgm.h" +#include "lib/iov_buf.h" struct messaging_callback { struct messaging_callback *prev, *next; diff --git a/source3/lib/messages_ctdbd.c b/source3/lib/messages_ctdbd.c index eb7e929fc3..59f5976da0 100644 --- a/source3/lib/messages_ctdbd.c +++ b/source3/lib/messages_ctdbd.c @@ -20,6 +20,7 @@ #include "includes.h" #include "messages.h" #include "util_tdb.h" +#include "lib/iov_buf.h" /* * It is not possible to include ctdb.h and tdb_compat.h (included via diff --git a/source3/lib/util_sock.c b/source3/lib/util_sock.c index d93e22d702..163045a8a2 100644 --- a/source3/lib/util_sock.c +++ b/source3/lib/util_sock.c @@ -29,6 +29,7 @@ #include "../lib/util/tevent_ntstatus.h" #include "../lib/tsocket/tsocket.h" #include "lib/sys_rw.h" +#include "lib/iov_buf.h" const char *client_addr(int fd, char *addr, size_t addrlen) { @@ -202,49 +203,6 @@ NTSTATUS read_data_ntstatus(int fd, char *buffer, size_t N) return read_fd_with_timeout(fd, buffer, N, N, 0, NULL); } -ssize_t iov_buflen(const struct iovec *iov, int iovcnt) -{ - size_t buflen = 0; - int i; - - for (i=0; i<iovcnt; i++) { - size_t thislen = iov[i].iov_len; - size_t tmp = buflen + thislen; - - if ((tmp < buflen) || (tmp < thislen)) { - /* overflow */ - return -1; - } - buflen = tmp; - } - return buflen; -} - -uint8_t *iov_buf(TALLOC_CTX *mem_ctx, const struct iovec *iov, int iovcnt) -{ - int i; - ssize_t buflen; - uint8_t *buf, *p; - - buflen = iov_buflen(iov, iovcnt); - if (buflen == -1) { - return NULL; - } - buf = talloc_array(mem_ctx, uint8_t, buflen); - if (buf == NULL) { - return NULL; - } - - p = buf; - for (i=0; i<iovcnt; i++) { - size_t len = iov[i].iov_len; - - memcpy(p, iov[i].iov_base, len); - p += len; - } - return buf; -} - /**************************************************************************** Write all data from an iov array NB. This can be called with a non-socket fd, don't add dependencies |
