summaryrefslogtreecommitdiff
path: root/usr/src/lib/smbsrv/libmlsvc/common/mlsvc_client.c
blob: ac8cc3003c05e7d1b1fe3e12b623086922ef0ceb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * Context functions to support the RPC interface library.
 */

#include <sys/errno.h>
#include <strings.h>

#include <smbsrv/libsmb.h>
#include <smbsrv/libsmbrdr.h>
#include <smbsrv/mlsvc_util.h>

static int mlsvc_xa_init(struct mlrpc_client *, struct mlrpc_xaction *,
    mlrpc_heap_t *);
static int mlsvc_xa_exchange(struct mlrpc_client *, struct mlrpc_xaction *);
static int mlsvc_xa_read(struct mlrpc_client *, struct mlrpc_xaction *);
static int mlsvc_xa_preserve(struct mlrpc_client *, struct mlrpc_xaction *,
    mlrpc_heapref_t *);
static int mlsvc_xa_destruct(struct mlrpc_client *, struct mlrpc_xaction *);
static void mlsvc_xa_release(struct mlrpc_client *, mlrpc_heapref_t *heapref);

/*
 * mlsvc_rpc_bind
 *
 * This the entry point for all client RPC services. This call must be
 * made to initialize an RPC context structure and bind to the remote
 * service before any RPCs can be exchanged with that service. The
 * descriptor is a wrapper that is used to associate an RPC handle with
 * the context data for that specific instance of the interface. The
 * handle is zeroed to ensure that it doesn't look like a valid handle.
 * The context handle is assigned to point at the RPC handle so that we
 * know when to free the context. As each handle is initialized it will
 * include a pointer to this context but only when we close this initial
 * RPC handle can the context be freed.
 *
 * On success, return a pointer to the descriptor. Otherwise return a
 * null pointer.
 */
int
mlsvc_rpc_bind(mlsvc_handle_t *desc, int fid, char *service)
{
	struct mlsvc_rpc_context *context;
	int rc;

	bzero(&desc->handle, sizeof (ms_handle_t));

	context = malloc(sizeof (struct mlsvc_rpc_context));
	if ((desc->context = context) == NULL)
		return (-1);

	bzero(context, sizeof (struct mlsvc_rpc_context));
	context->cli.context = context;

	mlrpc_binding_pool_initialize(&context->cli.binding_list,
	    context->binding_pool, CTXT_N_BINDING_POOL);

	context->fid = fid;
	context->handle = &desc->handle;
	context->cli.xa_init = mlsvc_xa_init;
	context->cli.xa_exchange = mlsvc_xa_exchange;
	context->cli.xa_read = mlsvc_xa_read;
	context->cli.xa_preserve = mlsvc_xa_preserve;
	context->cli.xa_destruct = mlsvc_xa_destruct;
	context->cli.xa_release = mlsvc_xa_release;

	rc = mlrpc_c_bind(&context->cli, service, &context->binding);
	if (MLRPC_DRC_IS_FAULT(rc)) {
		free(context);
		desc->context = NULL;
		return (-1);
	}

	return (rc);
}

/*
 * mlsvc_rpc_init
 *
 * This function must be called by client side applications before
 * calling mlsvc_rpc_call to allocate a heap. The heap must be
 * destroyed by either calling mlrpc_heap_destroy or mlsvc_rpc_free.
 * Use mlrpc_heap_destroy if mlsvc_rpc_call has not yet been called.
 * Otherwise use mlsvc_rpc_free.
 *
 * Returns 0 on success. Otherwise returns -1 to indicate an error.
 */
int
mlsvc_rpc_init(mlrpc_heapref_t *heapref)
{
	bzero(heapref, sizeof (mlrpc_heapref_t));

	if ((heapref->heap = mlrpc_heap_create()) == NULL)
		return (-1);

	return (0);
}

/*
 * mlsvc_rpc_call
 *
 * This function should be called by the client RPC interface functions
 * to make an RPC call. The remote service is identified by the context
 * handle, which should have been initialized with by mlsvc_rpc_bind.
 */
int
mlsvc_rpc_call(struct mlsvc_rpc_context *context, int opnum, void *params,
    mlrpc_heapref_t *heapref)
{
	return (mlrpc_c_call(context->binding, opnum, params, heapref));
}

/*
 * mlsvc_rpc_free
 *
 * This function should be called by the client RPC interface functions
 * to free the heap after an RPC call returns.
 */
void
mlsvc_rpc_free(struct mlsvc_rpc_context *context, mlrpc_heapref_t *heapref)
{
	mlrpc_c_free_heap(context->binding, heapref);
}

/*
 * The following functions provide the callback interface in the
 * context handle.
 */
/*ARGSUSED*/
static int
mlsvc_xa_init(struct mlrpc_client *mcli, struct mlrpc_xaction *mxa,
    mlrpc_heap_t *heap)
{
	struct mlndr_stream *recv_mlnds = &mxa->recv_mlnds;
	struct mlndr_stream *send_mlnds = &mxa->send_mlnds;

	/*
	 * If the caller hasn't provided a heap, create one here.
	 */
	if (heap == 0) {
		if ((heap = mlrpc_heap_create()) == 0)
			return (-1);
	}

	mxa->heap = heap;

	mlnds_initialize(send_mlnds, 0, NDR_MODE_CALL_SEND, heap);
	mlnds_initialize(recv_mlnds, 16 * 1024, NDR_MODE_RETURN_RECV, heap);
	return (0);
}

/*
 * mlsvc_xa_exchange
 *
 * This is the entry pointy for an RPC client call exchange with
 * a server, which will result in an smbrdr SmbTransact request.
 *
 * SmbTransact should return the number of bytes received, which
 * we record as the PDU size, or a negative error code.
 */
static int
mlsvc_xa_exchange(struct mlrpc_client *mcli, struct mlrpc_xaction *mxa)
{
	struct mlsvc_rpc_context *context = mcli->context;
	struct mlndr_stream *recv_mlnds = &mxa->recv_mlnds;
	struct mlndr_stream *send_mlnds = &mxa->send_mlnds;
	int rc;

	rc = smbrdr_rpc_transact(context->fid,
	    (char *)send_mlnds->pdu_base_offset, send_mlnds->pdu_size,
	    (char *)recv_mlnds->pdu_base_offset, recv_mlnds->pdu_max_size);

	if (rc < 0)
		recv_mlnds->pdu_size = 0;
	else
		recv_mlnds->pdu_size = rc;

	return (rc);
}

/*
 * mlsvc_xa_read
 *
 * This entry point will be invoked if the xa-exchange response contained
 * only the first fragment of a multi-fragment response.  The RPC client
 * code will then make repeated xa-read requests to obtain the remaining
 * fragments, which will result in smbrdr SmbReadX requests.
 *
 * SmbReadX should return the number of bytes received, in which case we
 * expand the PDU size to include the received data, or a negative error
 * code.
 */
static int
mlsvc_xa_read(struct mlrpc_client *mcli, struct mlrpc_xaction *mxa)
{
	struct mlsvc_rpc_context *context = mcli->context;
	struct mlndr_stream *mlnds = &mxa->recv_mlnds;
	int len;
	int rc;

	if ((len = (mlnds->pdu_max_size - mlnds->pdu_size)) < 0)
		return (-1);

	rc = smbrdr_rpc_readx(context->fid,
	    (char *)mlnds->pdu_base_offset + mlnds->pdu_size, len);

	if (rc < 0)
		return (-1);

	mlnds->pdu_size += rc;

	if (mlnds->pdu_size > mlnds->pdu_max_size) {
		mlnds->pdu_size = mlnds->pdu_max_size;
		return (-1);
	}

	return (rc);
}

/*
 * mlsvc_xa_preserve
 *
 * This function is called to preserve the heap. We save a reference
 * to the heap and set the mxa heap pointer to null so that the heap
 * will not be discarded when mlsvc_xa_destruct is called.
 */
/*ARGSUSED*/
static int
mlsvc_xa_preserve(struct mlrpc_client *mcli, struct mlrpc_xaction *mxa,
    mlrpc_heapref_t *heapref)
{
	heapref->state = MLRPC_HRST_PRESERVED;
	heapref->heap = mxa->heap;
	heapref->recv_pdu_buf = (char *)mxa->recv_mlnds.pdu_base_addr;
	heapref->send_pdu_buf = (char *)mxa->send_mlnds.pdu_base_addr;

	mxa->heap = NULL;
	return (0);
}

/*
 * mlsvc_xa_destruct
 *
 * This function is called to dispose of the heap. If the heap has
 * been preserved via mlsvc_xa_preserve, the mxa heap pointer will
 * be null and we assume that the heap will be released later via
 * a call to mlsvc_xa_release. Otherwise we free the memory here.
 */
/*ARGSUSED*/
static int
mlsvc_xa_destruct(struct mlrpc_client *mcli, struct mlrpc_xaction *mxa)
{
	if (mxa->heap) {
		mlnds_destruct(&mxa->recv_mlnds);
		mlnds_destruct(&mxa->send_mlnds);
		mlrpc_heap_destroy(mxa->heap);
	}

	return (0);
}

/*
 * mlsvc_xa_release
 *
 * This function is called, via some indirection, as a result of a
 * call to mlsvc_rpc_free. This is where we free the heap memory
 * that was preserved during an RPC call.
 */
/*ARGSUSED*/
static void
mlsvc_xa_release(struct mlrpc_client *mcli, mlrpc_heapref_t *heapref)
{
	if (heapref == NULL)
		return;

	if (heapref->state == MLRPC_HRST_PRESERVED) {
		free(heapref->recv_pdu_buf);
		free(heapref->send_pdu_buf);
		mlrpc_heap_destroy(heapref->heap);
	}
}