s3:pylibsmb: Add flags used by .list() to SMB Py bindings
[bbaumbach/samba-autobuild/.git] / source3 / rpcclient / cmd_echo.c
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/ndr_echo_c.h"
24
25 static NTSTATUS cmd_echo_add_one(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
26                                  int argc, const char **argv)
27 {
28         struct dcerpc_binding_handle *b = cli->binding_handle;
29         uint32_t request = 1, response;
30         NTSTATUS status;
31
32         if (argc > 2) {
33                 printf("Usage: %s [num]\n", argv[0]);
34                 return NT_STATUS_OK;
35         }
36
37         if (argc == 2) {
38                 request = atoi(argv[1]);
39         }
40
41         status = dcerpc_echo_AddOne(b, mem_ctx, request, &response);
42         if (!NT_STATUS_IS_OK(status)) {
43                 goto done;
44         }
45
46         printf("%d + 1 = %d\n", request, response);
47
48 done:
49         return status;
50 }
51
52 static NTSTATUS cmd_echo_data(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
53                               int argc, const char **argv)
54 {
55         struct dcerpc_binding_handle *b = cli->binding_handle;
56         uint32_t size, i;
57         NTSTATUS status;
58         uint8_t *in_data = NULL, *out_data = NULL;
59
60         if (argc != 2) {
61                 printf("Usage: %s num\n", argv[0]);
62                 return NT_STATUS_OK;
63         }
64
65         size = atoi(argv[1]);
66         if ( (in_data = (uint8_t*)SMB_MALLOC(size)) == NULL ) {
67                 printf("Failure to allocate buff of %d bytes\n",
68                        size);
69                 status = NT_STATUS_NO_MEMORY;
70                 goto done;
71         }
72         if ( (out_data = (uint8_t*)SMB_MALLOC(size)) == NULL ) {
73                 printf("Failure to allocate buff of %d bytes\n",
74                        size);
75                 status = NT_STATUS_NO_MEMORY;
76                 goto done;
77         }
78
79         for (i = 0; i < size; i++) {
80                 in_data[i] = i & 0xff;
81         }
82
83         status = dcerpc_echo_EchoData(b, mem_ctx, size, in_data, out_data);
84         if (!NT_STATUS_IS_OK(status)) {
85                 goto done;
86         }
87
88         for (i = 0; i < size; i++) {
89                 if (in_data[i] != out_data[i]) {
90                         printf("mismatch at offset %d, %d != %d\n",
91                                i, in_data[i], out_data[i]);
92                         status = NT_STATUS_UNSUCCESSFUL;
93                 }
94         }
95
96 done:
97         SAFE_FREE(in_data);
98         SAFE_FREE(out_data);
99
100         return status;
101 }
102
103 static NTSTATUS cmd_echo_source_data(struct rpc_pipe_client *cli, 
104                                      TALLOC_CTX *mem_ctx, int argc, 
105                                      const char **argv)
106 {
107         struct dcerpc_binding_handle *b = cli->binding_handle;
108         uint32_t size, i;
109         NTSTATUS status;
110         uint8_t *out_data = NULL;
111
112         if (argc != 2) {
113                 printf("Usage: %s num\n", argv[0]);
114                 return NT_STATUS_OK;
115         }
116
117         size = atoi(argv[1]);
118         if ( (out_data = (uint8_t*)SMB_MALLOC(size)) == NULL ) {
119                 printf("Failure to allocate buff of %d bytes\n",
120                        size);
121                 status = NT_STATUS_NO_MEMORY;
122                 goto done;              
123         }
124         
125
126         status = dcerpc_echo_SourceData(b, mem_ctx, size, out_data);
127         if (!NT_STATUS_IS_OK(status)) {
128                 goto done;
129         }
130
131         for (i = 0; i < size; i++) {
132                 if (out_data && out_data[i] != (i & 0xff)) {
133                         printf("mismatch at offset %d, %d != %d\n",
134                                i, out_data[i], i & 0xff);
135                         status = NT_STATUS_UNSUCCESSFUL;
136                 }
137         }
138
139 done:
140
141         SAFE_FREE(out_data);
142         return status;
143 }
144
145 static NTSTATUS cmd_echo_sink_data(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
146                                    int argc, const char **argv)
147 {
148         struct dcerpc_binding_handle *b = cli->binding_handle;
149         uint32_t size, i;
150         NTSTATUS status;
151         uint8_t *in_data = NULL;
152
153         if (argc != 2) {
154                 printf("Usage: %s num\n", argv[0]);
155                 return NT_STATUS_OK;
156         }
157
158         size = atoi(argv[1]);
159         if ( (in_data = (uint8_t*)SMB_MALLOC(size)) == NULL ) {
160                 printf("Failure to allocate buff of %d bytes\n",
161                        size);
162                 status = NT_STATUS_NO_MEMORY;
163                 goto done;              
164         }
165
166         for (i = 0; i < size; i++) {
167                 in_data[i] = i & 0xff;
168         }
169
170         status = dcerpc_echo_SinkData(b, mem_ctx, size, in_data);
171         if (!NT_STATUS_IS_OK(status)) {
172                 goto done;
173         }
174
175 done:
176         SAFE_FREE(in_data);
177
178         return status;
179 }
180
181 /* List of commands exported by this module */
182
183 struct cmd_set echo_commands[] = {
184
185         { "ECHO" },
186
187         { "echoaddone", RPC_RTYPE_NTSTATUS, cmd_echo_add_one,     NULL, &ndr_table_rpcecho, NULL, "Add one to a number", "" },
188         { "echodata",   RPC_RTYPE_NTSTATUS, cmd_echo_data,        NULL, &ndr_table_rpcecho, NULL, "Echo data",           "" },
189         { "sinkdata",   RPC_RTYPE_NTSTATUS, cmd_echo_sink_data,   NULL, &ndr_table_rpcecho, NULL, "Sink data",           "" },
190         { "sourcedata", RPC_RTYPE_NTSTATUS, cmd_echo_source_data, NULL, &ndr_table_rpcecho, NULL, "Source data",         "" },
191         { NULL }
192 };