From de565785f5e1f097bd75f57331425c4185185f80 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Sun, 10 Jun 2007 17:02:09 +0000 Subject: r23410: Merge the core of the cluster code. I'm 100% certain I've forgotten to merge something, but the main code should be in. It's mainly in dbwrap_ctdb.c, ctdbd_conn.c and messages_ctdbd.c. There should be no changes to the non-cluster case, it does survive make test on my laptop. It survives some very basic tests with ctdbd enables, I did not do the full test suite for clusters yet. Phew... Volker (This used to be commit 15553d6327a3aecdd2b0b94a3656d04bf4106323) --- source3/lib/packet.c | 256 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 256 insertions(+) create mode 100644 source3/lib/packet.c (limited to 'source3/lib/packet.c') diff --git a/source3/lib/packet.c b/source3/lib/packet.c new file mode 100644 index 0000000000..2473a771e0 --- /dev/null +++ b/source3/lib/packet.c @@ -0,0 +1,256 @@ +/* + Unix SMB/CIFS implementation. + Packet handling + Copyright (C) Volker Lendecke 2007 + + 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 2 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, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include "includes.h" + +struct packet_context { + int fd; + struct data_blob in, out; +}; + +/* + * Close the underlying fd + */ +static int packet_context_destructor(struct packet_context *ctx) +{ + return close(ctx->fd); +} + +/* + * Initialize a packet context. The fd is given to the packet context, meaning + * that it is automatically closed when the packet context is freed. + */ +struct packet_context *packet_init(TALLOC_CTX *mem_ctx, int fd) +{ + struct packet_context *result; + + if (!(result = TALLOC_ZERO_P(mem_ctx, struct packet_context))) { + return NULL; + } + + result->fd = fd; + talloc_set_destructor(result, packet_context_destructor); + return result; +} + +/* + * Pull data from the fd + */ +NTSTATUS packet_fd_read(struct packet_context *ctx) +{ + int res, available; + size_t new_size; + uint8 *in; + + res = ioctl(ctx->fd, FIONREAD, &available); + + if (res == -1) { + DEBUG(10, ("ioctl(FIONREAD) failed: %s\n", strerror(errno))); + return map_nt_error_from_unix(errno); + } + + SMB_ASSERT(available >= 0); + + if (available == 0) { + return NT_STATUS_END_OF_FILE; + } + + new_size = ctx->in.length + available; + + if (new_size < ctx->in.length) { + DEBUG(0, ("integer wrap\n")); + return NT_STATUS_NO_MEMORY; + } + + if (!(in = TALLOC_REALLOC_ARRAY(ctx, ctx->in.data, uint8, new_size))) { + DEBUG(10, ("talloc failed\n")); + return NT_STATUS_NO_MEMORY; + } + + res = recv(ctx->fd, in + ctx->in.length, available, 0); + + if (res < 0) { + DEBUG(10, ("recv failed: %s\n", strerror(errno))); + return map_nt_error_from_unix(errno); + } + + if (res == 0) { + return NT_STATUS_END_OF_FILE; + } + + ctx->in.data = in; + ctx->in.length += available; + + return NT_STATUS_OK; +} + +NTSTATUS packet_fd_read_sync(struct packet_context *ctx) +{ + int res; + fd_set r_fds; + + FD_ZERO(&r_fds); + FD_SET(ctx->fd, &r_fds); + + res = sys_select(ctx->fd+1, &r_fds, NULL, NULL, NULL); + + if (res == -1) { + DEBUG(10, ("select returned %s\n", strerror(errno))); + return map_nt_error_from_unix(errno); + } + + return packet_fd_read(ctx); +} + +BOOL packet_handler(struct packet_context *ctx, + BOOL (*full_req)(const struct data_blob *data, + size_t *length, + void *private_data), + NTSTATUS (*callback)(const struct data_blob *data, + void *private_data), + void *private_data, + NTSTATUS *status) +{ + size_t length; + struct data_blob data; + + if (!full_req(&ctx->in, &length, private_data)) { + return False; + } + + SMB_ASSERT(length <= ctx->in.length); + + data.data = ctx->in.data; + data.length = length; + + *status = callback(&data, private_data); + + memmove(ctx->in.data, ctx->in.data + length, + ctx->in.length - length); + ctx->in.length -= length; + + return True; +} + +/* + * How many bytes of outgoing data do we have pending? + */ +size_t packet_outgoing_bytes(struct packet_context *ctx) +{ + return ctx->out.length; +} + +/* + * Push data to the fd + */ +NTSTATUS packet_fd_write(struct packet_context *ctx) +{ + ssize_t sent; + + sent = send(ctx->fd, ctx->out.data, ctx->out.length, 0); + + if (sent == -1) { + DEBUG(0, ("send failed: %s\n", strerror(errno))); + return map_nt_error_from_unix(errno); + } + + memmove(ctx->out.data, ctx->out.data + sent, + ctx->out.length - sent); + ctx->out.length -= sent; + + return NT_STATUS_OK; +} + +/* + * Sync flush all outgoing bytes + */ +NTSTATUS packet_flush(struct packet_context *ctx) +{ + while (ctx->out.length != 0) { + NTSTATUS status = packet_fd_write(ctx); + if (!NT_STATUS_IS_OK(status)) { + return status; + } + } + return NT_STATUS_OK; +} + +/* + * Send a list of DATA_BLOBs + * + * Example: packet_send(ctx, 2, data_blob_const(&size, sizeof(size)), + * data_blob_const(buf, size)); + */ +NTSTATUS packet_send(struct packet_context *ctx, int num_blobs, ...) +{ + va_list ap; + int i; + size_t len; + uint8 *out; + + len = ctx->out.length; + + va_start(ap, num_blobs); + for (i=0; iout.data, uint8, len))) { + DEBUG(0, ("talloc failed\n")); + return NT_STATUS_NO_MEMORY; + } + + ctx->out.data = out; + + va_start(ap, num_blobs); + for (i=0; iout.data+ctx->out.length, blob.data, blob.length); + ctx->out.length += blob.length; + } + va_end(ap); + + SMB_ASSERT(ctx->out.length == len); + return NT_STATUS_OK; +} + +/* + * Get the packet context's file descriptor + */ +int packet_get_fd(struct packet_context *ctx) +{ + return ctx->fd; +} + -- cgit From d824b98f80ba186030cbb70b3a1e5daf80469ecd Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 9 Jul 2007 19:25:36 +0000 Subject: r23779: Change from v2 or later to v3 or later. Jeremy. (This used to be commit 407e6e695b8366369b7c76af1ff76869b45347b3) --- source3/lib/packet.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/packet.c') diff --git a/source3/lib/packet.c b/source3/lib/packet.c index 2473a771e0..153c49d34c 100644 --- a/source3/lib/packet.c +++ b/source3/lib/packet.c @@ -5,7 +5,7 @@ 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 2 of the License, or + 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, -- cgit From 5e54558c6dea67b56bbfaba5698f3a434d3dffb6 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Tue, 10 Jul 2007 00:52:41 +0000 Subject: r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text (This used to be commit b0132e94fc5fef936aa766fb99a306b3628e9f07) --- source3/lib/packet.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'source3/lib/packet.c') diff --git a/source3/lib/packet.c b/source3/lib/packet.c index 153c49d34c..d63ca2ef27 100644 --- a/source3/lib/packet.c +++ b/source3/lib/packet.c @@ -14,8 +14,7 @@ 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, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program. If not, see . */ #include "includes.h" -- cgit From 798fc28a6a1c660ea65cfd56c4c50ab425b728cd Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Tue, 24 Jul 2007 11:48:35 +0000 Subject: r24033: We need to be able to cope with packet_handler calling itself recursively (This used to be commit b74797a31eae1c59c1a23934a3302987de9ab87c) --- source3/lib/packet.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'source3/lib/packet.c') diff --git a/source3/lib/packet.c b/source3/lib/packet.c index d63ca2ef27..a3a33643ff 100644 --- a/source3/lib/packet.c +++ b/source3/lib/packet.c @@ -136,15 +136,16 @@ BOOL packet_handler(struct packet_context *ctx, SMB_ASSERT(length <= ctx->in.length); - data.data = ctx->in.data; - data.length = length; - - *status = callback(&data, private_data); + data = data_blob(ctx->in.data, length); memmove(ctx->in.data, ctx->in.data + length, ctx->in.length - length); ctx->in.length -= length; + *status = callback(&data, private_data); + + data_blob_free(&data); + return True; } -- cgit From 4b6713e7cc43ecc3637203884b22efeb839407c3 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Tue, 28 Aug 2007 15:09:47 +0000 Subject: r24744: Increase length by what we got from recv, not from ioctl (This used to be commit 23c8f4f74bfb761894034ce9375258411c80d74d) --- source3/lib/packet.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'source3/lib/packet.c') diff --git a/source3/lib/packet.c b/source3/lib/packet.c index a3a33643ff..c5335bcf15 100644 --- a/source3/lib/packet.c +++ b/source3/lib/packet.c @@ -83,6 +83,8 @@ NTSTATUS packet_fd_read(struct packet_context *ctx) return NT_STATUS_NO_MEMORY; } + ctx->in.data = in; + res = recv(ctx->fd, in + ctx->in.length, available, 0); if (res < 0) { @@ -94,8 +96,7 @@ NTSTATUS packet_fd_read(struct packet_context *ctx) return NT_STATUS_END_OF_FILE; } - ctx->in.data = in; - ctx->in.length += available; + ctx->in.length += res; return NT_STATUS_OK; } -- cgit From 30191d1a5704ad2b158386b511558972d539ce47 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 18 Oct 2007 17:40:25 -0700 Subject: RIP BOOL. Convert BOOL -> bool. I found a few interesting bugs in various places whilst doing this (places that assumed BOOL == int). I also need to fix the Samba4 pidl generation (next checkin). Jeremy. (This used to be commit f35a266b3cbb3e5fa6a86be60f34fe340a3ca71f) --- source3/lib/packet.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/packet.c') diff --git a/source3/lib/packet.c b/source3/lib/packet.c index c5335bcf15..e0486165f3 100644 --- a/source3/lib/packet.c +++ b/source3/lib/packet.c @@ -119,8 +119,8 @@ NTSTATUS packet_fd_read_sync(struct packet_context *ctx) return packet_fd_read(ctx); } -BOOL packet_handler(struct packet_context *ctx, - BOOL (*full_req)(const struct data_blob *data, +bool packet_handler(struct packet_context *ctx, + bool (*full_req)(const struct data_blob *data, size_t *length, void *private_data), NTSTATUS (*callback)(const struct data_blob *data, -- cgit