Do proper error handling if the socket is closed
[Samba/gebeck_regimport.git] / source3 / rpc_client / ndr.c
blob72a33137a653206a27b2817a5a2aaf47bb13abfd
1 /*
2 Unix SMB/CIFS implementation.
4 libndr interface
6 Copyright (C) Jelmer Vernooij 2006
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
25 NTSTATUS cli_do_rpc_ndr(struct rpc_pipe_client *cli,
26 TALLOC_CTX *mem_ctx,
27 const struct ndr_interface_table *table,
28 uint32 opnum, void *r)
30 prs_struct q_ps, r_ps;
31 const struct ndr_interface_call *call;
32 struct ndr_pull *pull;
33 DATA_BLOB blob;
34 struct ndr_push *push;
35 NTSTATUS status;
36 enum ndr_err_code ndr_err;
38 SMB_ASSERT(ndr_syntax_id_equal(&table->syntax_id,
39 &cli->abstract_syntax));
40 SMB_ASSERT(table->num_calls > opnum);
42 call = &table->calls[opnum];
44 push = ndr_push_init_ctx(mem_ctx);
45 if (!push) {
46 return NT_STATUS_NO_MEMORY;
49 ndr_err = call->ndr_push(push, NDR_IN, r);
50 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
51 return ndr_map_error2ntstatus(ndr_err);
54 blob = ndr_push_blob(push);
56 if (!prs_init_data_blob(&q_ps, &blob, mem_ctx)) {
57 return NT_STATUS_NO_MEMORY;
60 talloc_free(push);
62 prs_init_empty( &r_ps, mem_ctx, UNMARSHALL );
64 status = rpc_api_pipe_req(cli, opnum, &q_ps, &r_ps);
66 prs_mem_free( &q_ps );
68 if (!NT_STATUS_IS_OK(status)) {
69 prs_mem_free( &r_ps );
70 return status;
73 if (!prs_data_blob(&r_ps, &blob, mem_ctx)) {
74 prs_mem_free( &r_ps );
75 return NT_STATUS_NO_MEMORY;
78 prs_mem_free( &r_ps );
80 pull = ndr_pull_init_blob(&blob, mem_ctx);
81 if (pull == NULL) {
82 return NT_STATUS_NO_MEMORY;
85 /* have the ndr parser alloc memory for us */
86 pull->flags |= LIBNDR_FLAG_REF_ALLOC;
87 ndr_err = call->ndr_pull(pull, NDR_OUT, r);
88 talloc_free(pull);
90 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
91 return ndr_map_error2ntstatus(ndr_err);
94 return NT_STATUS_OK;