1/*
2   Unix SMB/CIFS implementation.
3   RPC pipe client
4
5   Copyright (C) Tim Potter 2003
6
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 3 of the License, or
10   (at your option) any later version.
11
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program.  If not, see <http://www.gnu.org/licenses/>.
19*/
20
21#include "includes.h"
22#include "rpcclient.h"
23#include "../librpc/gen_ndr/cli_echo.h"
24
25static NTSTATUS cmd_echo_add_one(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
26				 int argc, const char **argv)
27{
28	uint32 request = 1, response;
29	NTSTATUS result;
30
31	if (argc > 2) {
32		printf("Usage: %s [num]\n", argv[0]);
33		return NT_STATUS_OK;
34	}
35
36	if (argc == 2)
37		request = atoi(argv[1]);
38
39	result = rpccli_echo_AddOne(cli, mem_ctx, request, &response);
40
41	if (!NT_STATUS_IS_OK(result))
42		goto done;
43
44	printf("%d + 1 = %d\n", request, response);
45
46done:
47	return result;
48}
49
50static NTSTATUS cmd_echo_data(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
51			      int argc, const char **argv)
52{
53	uint32 size, i;
54	NTSTATUS result;
55	uint8_t *in_data = NULL, *out_data = NULL;
56
57	if (argc != 2) {
58		printf("Usage: %s num\n", argv[0]);
59		return NT_STATUS_OK;
60	}
61
62	size = atoi(argv[1]);
63	if ( (in_data = (uint8_t*)SMB_MALLOC(size)) == NULL ) {
64		printf("Failure to allocate buff of %d bytes\n",
65		       size);
66		result = NT_STATUS_NO_MEMORY;
67		goto done;
68	}
69	if ( (out_data = (uint8_t*)SMB_MALLOC(size)) == NULL ) {
70		printf("Failure to allocate buff of %d bytes\n",
71		       size);
72		result = NT_STATUS_NO_MEMORY;
73		goto done;
74	}
75
76	for (i = 0; i < size; i++)
77		in_data[i] = i & 0xff;
78
79	result = rpccli_echo_EchoData(cli, mem_ctx, size, in_data, out_data);
80
81	if (!NT_STATUS_IS_OK(result))
82		goto done;
83
84	for (i = 0; i < size; i++) {
85		if (in_data[i] != out_data[i]) {
86			printf("mismatch at offset %d, %d != %d\n",
87			       i, in_data[i], out_data[i]);
88			result = NT_STATUS_UNSUCCESSFUL;
89		}
90	}
91
92done:
93	SAFE_FREE(in_data);
94	SAFE_FREE(out_data);
95
96	return result;
97}
98
99static NTSTATUS cmd_echo_source_data(struct rpc_pipe_client *cli,
100				     TALLOC_CTX *mem_ctx, int argc,
101				     const char **argv)
102{
103	uint32 size, i;
104	NTSTATUS result;
105	uint8_t *out_data = NULL;
106
107	if (argc != 2) {
108		printf("Usage: %s num\n", argv[0]);
109		return NT_STATUS_OK;
110	}
111
112	size = atoi(argv[1]);
113	if ( (out_data = (uint8_t*)SMB_MALLOC(size)) == NULL ) {
114		printf("Failure to allocate buff of %d bytes\n",
115		       size);
116		result = NT_STATUS_NO_MEMORY;
117		goto done;
118	}
119
120
121	result = rpccli_echo_SourceData(cli, mem_ctx, size, out_data);
122
123	if (!NT_STATUS_IS_OK(result))
124		goto done;
125
126	for (i = 0; i < size; i++) {
127		if (out_data && out_data[i] != (i & 0xff)) {
128			printf("mismatch at offset %d, %d != %d\n",
129			       i, out_data[i], i & 0xff);
130			result = NT_STATUS_UNSUCCESSFUL;
131		}
132	}
133
134done:
135
136	SAFE_FREE(out_data);
137	return result;
138}
139
140static NTSTATUS cmd_echo_sink_data(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
141				   int argc, const char **argv)
142{
143	uint32 size, i;
144	NTSTATUS result;
145	uint8_t *in_data = NULL;
146
147	if (argc != 2) {
148		printf("Usage: %s num\n", argv[0]);
149		return NT_STATUS_OK;
150	}
151
152	size = atoi(argv[1]);
153	if ( (in_data = (uint8_t*)SMB_MALLOC(size)) == NULL ) {
154		printf("Failure to allocate buff of %d bytes\n",
155		       size);
156		result = NT_STATUS_NO_MEMORY;
157		goto done;
158	}
159
160	for (i = 0; i < size; i++)
161		in_data[i] = i & 0xff;
162
163	result = rpccli_echo_SinkData(cli, mem_ctx, size, in_data);
164
165	if (!NT_STATUS_IS_OK(result))
166		goto done;
167
168done:
169	SAFE_FREE(in_data);
170
171	return result;
172}
173
174/* List of commands exported by this module */
175
176struct cmd_set echo_commands[] = {
177
178	{ "ECHO" },
179
180	{ "echoaddone", RPC_RTYPE_NTSTATUS, cmd_echo_add_one,     NULL, &ndr_table_rpcecho.syntax_id, NULL, "Add one to a number", "" },
181	{ "echodata",   RPC_RTYPE_NTSTATUS, cmd_echo_data,        NULL, &ndr_table_rpcecho.syntax_id, NULL, "Echo data",           "" },
182	{ "sinkdata",   RPC_RTYPE_NTSTATUS, cmd_echo_sink_data,   NULL, &ndr_table_rpcecho.syntax_id, NULL, "Sink data",           "" },
183	{ "sourcedata", RPC_RTYPE_NTSTATUS, cmd_echo_source_data, NULL, &ndr_table_rpcecho.syntax_id, NULL, "Source data",         "" },
184	{ NULL }
185};
186