[SCM] Samba Shared Repository - branch master updated

Kai Blin kai at samba.org
Thu Dec 9 15:58:01 MST 2010


The branch, master has been updated
       via  1f2518d s4 libcli: Add libcli_echo lib and torture test
       via  9df1b40 s4: Implement UDP echo server example
      from  5fcbb16 s4:pyrpc_util: s/typename/type_name to avoid c++ warnings

http://gitweb.samba.org/?p=samba.git;a=shortlog;h=master


- Log -----------------------------------------------------------------
commit 1f2518df5a4f21bb7a2bd2abc601517d7988c507
Author: Kai Blin <kai at samba.org>
Date:   Mon Nov 15 23:01:57 2010 +0100

    s4 libcli: Add libcli_echo lib and torture test
    
    Autobuild-User: Kai Blin <kai at samba.org>
    Autobuild-Date: Thu Dec  9 23:57:03 CET 2010 on sn-devel-104

commit 9df1b408c1b2432728ecc3d114854535f168b47a
Author: Kai Blin <kai at samba.org>
Date:   Sun Nov 7 10:05:56 2010 +0100

    s4: Implement UDP echo server example
    
    This is a simple UDP-based echo server. It is mainly intended as an
    example on how to do server service tasks in s4.

-----------------------------------------------------------------------

Summary of changes:
 libcli/echo/echo.c                |  204 ++++++++++++++++++++++
 libcli/echo/libecho.h             |   56 ++++++
 libcli/echo/tests/echo.c          |   93 ++++++++++
 libcli/echo/tests/wscript_build   |    8 +
 libcli/echo/wscript_build         |    7 +
 selftest/target/Samba4.pm         |    1 +
 source4/echo_server/echo_server.c |  345 +++++++++++++++++++++++++++++++++++++
 source4/echo_server/echo_server.h |   33 ++++
 source4/echo_server/wscript_build |    9 +
 source4/selftest/tests.py         |    2 +
 source4/wscript_build             |    2 +
 11 files changed, 760 insertions(+), 0 deletions(-)
 create mode 100644 libcli/echo/echo.c
 create mode 100644 libcli/echo/libecho.h
 create mode 100644 libcli/echo/tests/echo.c
 create mode 100644 libcli/echo/tests/wscript_build
 create mode 100644 libcli/echo/wscript_build
 create mode 100644 source4/echo_server/echo_server.c
 create mode 100644 source4/echo_server/echo_server.h
 create mode 100644 source4/echo_server/wscript_build


Changeset truncated at 500 lines:

diff --git a/libcli/echo/echo.c b/libcli/echo/echo.c
new file mode 100644
index 0000000..46d1e28
--- /dev/null
+++ b/libcli/echo/echo.c
@@ -0,0 +1,204 @@
+/*
+   Unix SMB/CIFS implementation.
+
+   Echo example async client library
+
+   Copyright (C) 2010 Kai Blin  <kai at samba.org>
+
+   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/network.h"
+#include <tevent.h>
+#include "lib/tsocket/tsocket.h"
+#include "libcli/util/ntstatus.h"
+#include "libcli/echo/libecho.h"
+#include "lib/util/tevent_ntstatus.h"
+#include "libcli/util/error.h"
+
+/*
+ * Following the Samba convention for async functions, set up a state struct
+ * for this set of calls. The state is always called function_name_state for
+ * the set of async functions related to function_name_send().
+ */
+struct echo_request_state {
+	struct tevent_context *ev;
+	ssize_t orig_len;
+	struct tdgram_context *dgram;
+	char *message;
+};
+
+/* Declare callback functions used below. */
+static void echo_request_get_reply(struct tevent_req *subreq);
+static void echo_request_done(struct tevent_req *subreq);
+
+struct tevent_req *echo_request_send(TALLOC_CTX *mem_ctx,
+				     struct tevent_context *ev,
+				     const char *server_addr_string,
+				     const char *message)
+{
+	struct tevent_req *req, *subreq;
+	struct echo_request_state *state;
+	struct tsocket_address *local_addr, *server_addr;
+	struct tdgram_context *dgram;
+	int ret;
+
+	/*
+	 * Creating the initial tevent_req is the only place where returning
+	 * NULL is allowed. Everything after that should return a more
+	 * meaningful error using tevent_req_post().
+	 */
+	req = tevent_req_create(mem_ctx, &state, struct echo_request_state);
+	if (req == NULL) {
+		return NULL;
+	}
+
+	/*
+	 * We need to dispatch new async functions in the callbacks, hold
+	 * on to the event context.
+	 */
+	state->ev = ev;
+
+	/* libecho uses connected UDP sockets, take care of this here */
+	ret = tsocket_address_inet_from_strings(state, "ip", NULL, 0,
+						&local_addr);
+	if (ret != 0) {
+		tevent_req_nterror(req, map_nt_error_from_unix(ret));
+		return tevent_req_post(req, ev);
+	}
+
+	ret = tsocket_address_inet_from_strings(state, "ip", server_addr_string,
+						ECHO_PORT, &server_addr);
+	if (ret != 0) {
+		tevent_req_nterror(req, map_nt_error_from_unix(ret));
+		return tevent_req_post(req, ev);
+	}
+
+	ret = tdgram_inet_udp_socket(local_addr, server_addr, state, &dgram);
+	if (ret != 0) {
+		tevent_req_nterror(req, map_nt_error_from_unix(ret));
+		return tevent_req_post(req, ev);
+	}
+
+	state->dgram = dgram;
+	state->orig_len = strlen(message) + 1;
+
+	/* Start of a subrequest for the actual data sending */
+	subreq = tdgram_sendto_send(state, ev, dgram,
+				    (const uint8_t *) message,
+				    state->orig_len, NULL);
+	if (tevent_req_nomem(subreq, req)) {
+		return tevent_req_post(req, ev);
+	}
+
+	/*
+	 * And tell tevent what to call when the subreq is done. Note that the
+	 * original req structure is passed into the callback as callback data.
+	 * This is used to get to the state struct in callbacks.
+	 */
+	tevent_req_set_callback(subreq, echo_request_get_reply, req);
+	return req;
+}
+
+/*
+ * The following two callbacks both demonstrate the way of getting back the
+ * state struct in a callback function.
+ */
+
+static void echo_request_get_reply(struct tevent_req *subreq)
+{
+	/* Get the parent request struct from the callback data */
+	struct tevent_req *req = tevent_req_callback_data(subreq,
+						struct tevent_req);
+	/* And get the state struct from the parent request struct */
+	struct echo_request_state *state = tevent_req_data(req,
+						struct echo_request_state);
+	ssize_t len;
+	int err = 0;
+
+	len = tdgram_sendto_recv(subreq, &err);
+	TALLOC_FREE(subreq);
+
+	if (len == -1 && err != 0) {
+		tevent_req_nterror(req, map_nt_error_from_unix(err));
+		return;
+	}
+
+	if (len != state->orig_len) {
+		tevent_req_nterror(req, NT_STATUS_UNEXPECTED_NETWORK_ERROR);
+		return;
+	}
+
+	/* Send off the second subreq here, this time to receive the reply */
+	subreq = tdgram_recvfrom_send(state, state->ev, state->dgram);
+	if (tevent_req_nomem(subreq, req)) {
+		return;
+	}
+
+	/* And set the new callback */
+	tevent_req_set_callback(subreq, echo_request_done, req);
+	return;
+}
+
+static void echo_request_done(struct tevent_req *subreq)
+{
+	struct tevent_req *req = tevent_req_callback_data(subreq,
+						struct tevent_req);
+	struct echo_request_state *state = tevent_req_data(req,
+						struct echo_request_state);
+
+	ssize_t len;
+	int err = 0;
+
+	len = tdgram_recvfrom_recv(subreq, &err, state,
+				   (uint8_t **)&state->message,
+				   NULL);
+	TALLOC_FREE(subreq);
+
+	if (len == -1 && err != 0) {
+		tevent_req_nterror(req, map_nt_error_from_unix(err));
+		return;
+	}
+
+	state->message[len] = '\0';
+	/* Once the async function has completed, set tevent_req_done() */
+	tevent_req_done(req);
+}
+
+/*
+ * In the recv function, we usually need to move the data from the state struct
+ * to the memory area owned by the caller. Also, the function
+ * tevent_req_received() is called to take care of freeing the memory still
+ * associated with the request.
+ */
+
+NTSTATUS echo_request_recv(struct tevent_req *req,
+			   TALLOC_CTX *mem_ctx,
+			   char **message)
+{
+	struct echo_request_state *state = tevent_req_data(req,
+			struct echo_request_state);
+	NTSTATUS status;
+
+	if (tevent_req_is_nterror(req, &status)) {
+		tevent_req_received(req);
+		return status;
+	}
+
+	*message = talloc_move(mem_ctx, &state->message);
+	tevent_req_received(req);
+
+	return NT_STATUS_OK;
+}
diff --git a/libcli/echo/libecho.h b/libcli/echo/libecho.h
new file mode 100644
index 0000000..35a0986
--- /dev/null
+++ b/libcli/echo/libecho.h
@@ -0,0 +1,56 @@
+/*
+   Unix SMB/CIFS implementation.
+
+   Echo structures and headers, example async client library
+
+   Copyright (C) 2010 Kai Blin  <kai at samba.org>
+
+   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 __LIBECHO_H__
+#define __LIBECHO_H__
+
+/* The echo port is fixed, so just set a constant. */
+#define ECHO_PORT 7
+
+/** Send an echo request to an echo server
+ *
+ *@param mem_ctx        talloc memory context to use
+ *@param ev             tevent context to use
+ *@param server_address address of the server as a string
+ *@param message        echo message to send
+ *@return tevent_req with the active request or NULL on out-of-memory
+ */
+struct tevent_req *echo_request_send(TALLOC_CTX *mem_ctx,
+				     struct tevent_context *ev,
+				     const char *server_address,
+				     const char *message);
+
+/** Get the echo response from an echo server
+ *
+ * Once the echo_request_send async request is finished, you can call
+ * this function to collect the echo reply.
+ *
+ *@param req      tevent_req struct returned from echo_request_send
+ *@param mem_ctx  talloc memory context to use for the reply string
+ *@param message  pointer to a string that will be allocated and filled with
+                  the echo reply.
+ *@return NTSTATUS code depending on the async request result
+ */
+NTSTATUS echo_request_recv(struct tevent_req *req,
+			   TALLOC_CTX *mem_ctx,
+			   char **message);
+
+#endif /*__LIBECHO_H__*/
diff --git a/libcli/echo/tests/echo.c b/libcli/echo/tests/echo.c
new file mode 100644
index 0000000..931e8b6
--- /dev/null
+++ b/libcli/echo/tests/echo.c
@@ -0,0 +1,93 @@
+/*
+   Unix SMB/CIFS implementation.
+
+   Example echo torture tests
+
+   Copyright (C) 2010 Kai Blin  <kai at samba.org>
+
+   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 "includes.h"
+#include "torture/smbtorture.h"
+#include "libcli/resolve/resolve.h"
+#include <tevent.h>
+#include "libcli/util/ntstatus.h"
+#include "libcli/echo/libecho.h"
+
+/* Basic test function that sends an echo request and checks the reply */
+static bool echo_udp_basic(struct torture_context *tctx, const char *address)
+{
+	struct tevent_req *req;
+	NTSTATUS status;
+	const char *msg_send = "This is a test string\n";
+	char *msg_recv;
+
+	req = echo_request_send(tctx, tctx->ev, address, msg_send);
+	torture_assert(tctx, req != NULL,
+		       "echo_request_send returned non-null tevent_req");
+
+	while(tevent_req_is_in_progress(req)) {
+		tevent_loop_once(tctx->ev);
+	}
+
+	status = echo_request_recv(req, tctx, &msg_recv);
+	torture_assert_ntstatus_ok(tctx, status,
+				   "echo_request_recv returned ok");
+
+	torture_assert_str_equal(tctx, msg_recv, msg_send,
+				 "Echo server echoed request string");
+
+	return true;
+}
+
+/*Test case to set up the environment and perform UDP-based echo tests */
+static bool torture_echo_udp(struct torture_context *tctx)
+{
+	const char *address;
+	struct nbt_name name;
+	NTSTATUS status;
+	bool ret = true;
+
+	make_nbt_name_server(&name,
+			     torture_setting_string(tctx, "host", NULL));
+	status = resolve_name(lpcfg_resolve_context(tctx->lp_ctx), &name, tctx,
+			      &address, tctx->ev);
+	if (!NT_STATUS_IS_OK(status)) {
+		printf("Failed to resolve %s - %s\n", name.name,
+		       nt_errstr(status));
+		return false;
+	}
+
+	/* All tests are now called here */
+	ret &= echo_udp_basic(tctx, address);
+
+	return ret;
+}
+
+/* Test suite that bundles all the libecho tests */
+NTSTATUS torture_libcli_echo_init(void)
+{
+	struct torture_suite *suite;
+
+	suite = torture_suite_create(talloc_autofree_context(), "ECHO");
+	NT_STATUS_HAVE_NO_MEMORY(suite);
+
+	torture_suite_add_simple_test(suite, "UDP", torture_echo_udp);
+
+	suite->description = talloc_strdup(suite, "libcli/echo interface tests");
+	torture_register_suite(suite);
+
+	return NT_STATUS_OK;
+}
diff --git a/libcli/echo/tests/wscript_build b/libcli/echo/tests/wscript_build
new file mode 100644
index 0000000..7e58130
--- /dev/null
+++ b/libcli/echo/tests/wscript_build
@@ -0,0 +1,8 @@
+#!/usr/bin/env python
+
+bld.SAMBA_MODULE('TORTURE_LIBCLI_ECHO',
+        source='echo.c',
+        subsystem='smbtorture',
+        init_function='torture_libcli_echo_init',
+        deps='LIBTSOCKET UTIL_TEVENT LIBCLI_ECHO',
+        internal_module=True);
diff --git a/libcli/echo/wscript_build b/libcli/echo/wscript_build
new file mode 100644
index 0000000..eedb4ac
--- /dev/null
+++ b/libcli/echo/wscript_build
@@ -0,0 +1,7 @@
+#!/usr/bin/env python
+
+bld.SAMBA_SUBSYSTEM('LIBCLI_ECHO',
+        source='echo.c',
+        deps='LIBTSOCKET UTIL_TEVENT');
+
+bld.RECURSE('tests')
diff --git a/selftest/target/Samba4.pm b/selftest/target/Samba4.pm
index 37c9550..20c1a1b 100644
--- a/selftest/target/Samba4.pm
+++ b/selftest/target/Samba4.pm
@@ -602,6 +602,7 @@ sub provision_raw_step1($$)
 	panic action = $RealBin/gdb_backtrace \%PID% \%PROG%
 	wins support = yes
 	server role = $ctx->{server_role}
+	server services = +echo
 	notify:inotify = false
 	ldb:nosync = true
 #We don't want to pass our self-tests if the PAC code is wrong
diff --git a/source4/echo_server/echo_server.c b/source4/echo_server/echo_server.c
new file mode 100644
index 0000000..02e26bd
--- /dev/null
+++ b/source4/echo_server/echo_server.c
@@ -0,0 +1,345 @@
+/*
+   Unix SMB/CIFS implementation.
+
+   Echo server service example
+
+   Copyright (C) 2010 Kai Blin  <kai at samba.org>
+
+   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 "includes.h"
+#include "echo_server/echo_server.h"
+/* Get at the config file settings */
+#include "param/param.h"
+/* This defines task_server_terminate */
+#include "smbd/process_model.h"
+/* We get load_interfaces from here */
+#include "socket/netif.h"
+/* NTSTATUS-related stuff */
+#include "libcli/util/ntstatus.h"
+/* tsocket-related functions */
+#include "lib/tsocket/tsocket.h"
+
+/* Structure to hold an echo server socket */
+struct echo_socket {
+	/* This can come handy for the task struct in there */
+	struct echo_server *echo;
+	struct tsocket_address *local_address;
+};
+
+/* Structure to hold udp socket */
+struct echo_udp_socket {
+	struct echo_socket *echo_socket;
+	struct tdgram_context *dgram;
+	struct tevent_queue *send_queue;
+};
+
+/*
+ * Main processing function.
+ *
+ * This is the start of the package processing.
+ * In the echo server it doesn't do much, but for more complicated servers,
+ * your code goes here (or at least is called from here.
+ */
+static NTSTATUS echo_process(struct echo_server *echo,
+			     TALLOC_CTX *mem_ctx,
+			     DATA_BLOB *in,
+			     DATA_BLOB *out)
+{
+	uint8_t *buf = talloc_memdup(mem_ctx, in->data, in->length);
+	NT_STATUS_HAVE_NO_MEMORY(buf);
+
+	out->data = buf;
+	out->length = in->length;
+
+	return NT_STATUS_OK;
+}
+
+/* Structure keeping track of a single UDP echo server call */
+struct echo_udp_call {
+	/* The UDP packet came from here, our reply goes there as well */
+	struct tsocket_address *src;
+	DATA_BLOB in;
+	DATA_BLOB out;
+};
+
+/** Prototype of the send callback */
+static void echo_udp_call_sendto_done(struct tevent_req *subreq);
+
+/* Callback to receive UDP packets */
+static void echo_udp_call_loop(struct tevent_req *subreq)
+{
+	/*


-- 
Samba Shared Repository


More information about the samba-cvs mailing list