2 * Unix SMB/CIFS implementation.
3 * RPC client transport over tstream
4 * Copyright (C) Simo Sorce 2010
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "../lib/util/tevent_ntstatus.h"
22 #include "rpc_client/rpc_transport.h"
23 #include "lib/tsocket/tsocket.h"
24 #include "libsmb/cli_np_tstream.h"
28 #define DBGC_CLASS DBGC_RPC_CLI
30 struct rpc_tstream_state
{
31 struct tstream_context
*stream
;
32 struct tevent_queue
*read_queue
;
33 struct tevent_queue
*write_queue
;
37 static void rpc_tstream_disconnect(struct rpc_tstream_state
*s
)
39 TALLOC_FREE(s
->stream
);
42 static bool rpc_tstream_is_connected(void *priv
)
44 struct rpc_tstream_state
*transp
=
45 talloc_get_type_abort(priv
, struct rpc_tstream_state
);
48 if (!transp
->stream
) {
52 if (!tstream_is_cli_np(transp
->stream
)) {
56 ret
= tstream_pending_bytes(transp
->stream
);
64 static unsigned int rpc_tstream_set_timeout(void *priv
, unsigned int timeout
)
66 struct rpc_tstream_state
*transp
=
67 talloc_get_type_abort(priv
, struct rpc_tstream_state
);
71 ok
= rpc_tstream_is_connected(transp
);
76 if (tstream_is_cli_np(transp
->stream
)) {
77 transp
->timeout
= timeout
;
78 return tstream_cli_np_set_timeout(transp
->stream
, timeout
);
81 orig_timeout
= transp
->timeout
;
83 transp
->timeout
= timeout
;
88 struct rpc_tstream_next_vector_state
{
95 static void rpc_tstream_next_vector_init(
96 struct rpc_tstream_next_vector_state
*s
,
97 uint8_t *buf
, size_t len
)
102 s
->len
= MIN(len
, UINT16_MAX
);
105 static int rpc_tstream_next_vector(struct tstream_context
*stream
,
108 struct iovec
**_vector
,
111 struct rpc_tstream_next_vector_state
*state
=
112 (struct rpc_tstream_next_vector_state
*)private_data
;
113 struct iovec
*vector
;
117 if (state
->ofs
== state
->len
) {
123 pending
= tstream_pending_bytes(stream
);
128 if (pending
== 0 && state
->ofs
!= 0) {
129 /* return a short read */
136 /* we want at least one byte and recheck again */
139 size_t missing
= state
->len
- state
->ofs
;
140 if (pending
> missing
) {
141 /* there's more available */
142 state
->remaining
= pending
- missing
;
145 /* read what we can get and recheck in the next cycle */
150 vector
= talloc_array(mem_ctx
, struct iovec
, 1);
155 vector
[0].iov_base
= state
->buf
+ state
->ofs
;
156 vector
[0].iov_len
= wanted
;
158 state
->ofs
+= wanted
;
165 struct rpc_tstream_read_state
{
166 struct rpc_tstream_state
*transp
;
167 struct rpc_tstream_next_vector_state next_vector
;
171 static void rpc_tstream_read_done(struct tevent_req
*subreq
);
173 static struct tevent_req
*rpc_tstream_read_send(TALLOC_CTX
*mem_ctx
,
174 struct tevent_context
*ev
,
175 uint8_t *data
, size_t size
,
178 struct rpc_tstream_state
*transp
=
179 talloc_get_type_abort(priv
, struct rpc_tstream_state
);
180 struct tevent_req
*req
, *subreq
;
181 struct rpc_tstream_read_state
*state
;
182 struct timeval endtime
;
184 req
= tevent_req_create(mem_ctx
, &state
, struct rpc_tstream_read_state
);
188 if (!rpc_tstream_is_connected(transp
)) {
189 NTSTATUS status
= NT_STATUS_CONNECTION_DISCONNECTED
;
190 if (tstream_is_cli_np(transp
->stream
)) {
191 status
= NT_STATUS_PIPE_DISCONNECTED
;
193 tevent_req_nterror(req
, status
);
194 return tevent_req_post(req
, ev
);
196 state
->transp
= transp
;
197 rpc_tstream_next_vector_init(&state
->next_vector
, data
, size
);
199 subreq
= tstream_readv_pdu_queue_send(state
, ev
,
202 rpc_tstream_next_vector
,
203 &state
->next_vector
);
204 if (subreq
== NULL
) {
205 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
206 return tevent_req_post(req
, ev
);
209 endtime
= timeval_current_ofs_msec(transp
->timeout
);
210 if (!tevent_req_set_endtime(subreq
, ev
, endtime
)) {
214 tevent_req_set_callback(subreq
, rpc_tstream_read_done
, req
);
221 static void rpc_tstream_read_done(struct tevent_req
*subreq
)
223 struct tevent_req
*req
=
224 tevent_req_callback_data(subreq
, struct tevent_req
);
225 struct rpc_tstream_read_state
*state
=
226 tevent_req_data(req
, struct rpc_tstream_read_state
);
229 state
->nread
= tstream_readv_pdu_queue_recv(subreq
, &err
);
231 if (state
->nread
< 0) {
232 rpc_tstream_disconnect(state
->transp
);
233 tevent_req_nterror(req
, map_nt_error_from_unix(err
));
236 tevent_req_done(req
);
239 static NTSTATUS
rpc_tstream_read_recv(struct tevent_req
*req
, ssize_t
*size
)
241 struct rpc_tstream_read_state
*state
= tevent_req_data(
242 req
, struct rpc_tstream_read_state
);
245 if (tevent_req_is_nterror(req
, &status
)) {
248 *size
= state
->nread
;
252 struct rpc_tstream_write_state
{
253 struct tevent_context
*ev
;
254 struct rpc_tstream_state
*transp
;
259 static void rpc_tstream_write_done(struct tevent_req
*subreq
);
261 static struct tevent_req
*rpc_tstream_write_send(TALLOC_CTX
*mem_ctx
,
262 struct tevent_context
*ev
,
263 const uint8_t *data
, size_t size
,
266 struct rpc_tstream_state
*transp
=
267 talloc_get_type_abort(priv
, struct rpc_tstream_state
);
268 struct tevent_req
*req
, *subreq
;
269 struct rpc_tstream_write_state
*state
;
270 struct timeval endtime
;
272 req
= tevent_req_create(mem_ctx
, &state
, struct rpc_tstream_write_state
);
276 if (!rpc_tstream_is_connected(transp
)) {
277 NTSTATUS status
= NT_STATUS_CONNECTION_DISCONNECTED
;
278 if (tstream_is_cli_np(transp
->stream
)) {
279 status
= NT_STATUS_PIPE_DISCONNECTED
;
281 tevent_req_nterror(req
, status
);
282 return tevent_req_post(req
, ev
);
285 state
->transp
= transp
;
286 state
->iov
.iov_base
= discard_const_p(void *, data
);
287 state
->iov
.iov_len
= size
;
289 subreq
= tstream_writev_queue_send(state
, ev
,
293 if (subreq
== NULL
) {
297 endtime
= timeval_current_ofs_msec(transp
->timeout
);
298 if (!tevent_req_set_endtime(subreq
, ev
, endtime
)) {
302 tevent_req_set_callback(subreq
, rpc_tstream_write_done
, req
);
309 static void rpc_tstream_write_done(struct tevent_req
*subreq
)
311 struct tevent_req
*req
=
312 tevent_req_callback_data(subreq
, struct tevent_req
);
313 struct rpc_tstream_write_state
*state
=
314 tevent_req_data(req
, struct rpc_tstream_write_state
);
317 state
->nwritten
= tstream_writev_queue_recv(subreq
, &err
);
319 if (state
->nwritten
< 0) {
320 rpc_tstream_disconnect(state
->transp
);
321 tevent_req_nterror(req
, map_nt_error_from_unix(err
));
324 tevent_req_done(req
);
327 static NTSTATUS
rpc_tstream_write_recv(struct tevent_req
*req
, ssize_t
*sent
)
329 struct rpc_tstream_write_state
*state
=
330 tevent_req_data(req
, struct rpc_tstream_write_state
);
333 if (tevent_req_is_nterror(req
, &status
)) {
336 *sent
= state
->nwritten
;
340 struct rpc_tstream_trans_state
{
341 struct tevent_context
*ev
;
342 struct rpc_tstream_state
*transp
;
344 uint32_t max_rdata_len
;
348 static void rpc_tstream_trans_writev(struct tevent_req
*subreq
);
349 static void rpc_tstream_trans_readv_pdu(struct tevent_req
*subreq
);
351 static int rpc_tstream_trans_next_vector(struct tstream_context
*stream
,
354 struct iovec
**_vector
,
357 static struct tevent_req
*rpc_tstream_trans_send(TALLOC_CTX
*mem_ctx
,
358 struct tevent_context
*ev
,
359 uint8_t *data
, size_t data_len
,
360 uint32_t max_rdata_len
,
363 struct rpc_tstream_state
*transp
=
364 talloc_get_type_abort(priv
, struct rpc_tstream_state
);
365 struct tevent_req
*req
, *subreq
;
366 struct rpc_tstream_trans_state
*state
;
367 struct timeval endtime
;
368 bool use_trans
= false;
370 req
= tevent_req_create(mem_ctx
, &state
,
371 struct rpc_tstream_trans_state
);
376 if (!rpc_tstream_is_connected(transp
)) {
377 NTSTATUS status
= NT_STATUS_CONNECTION_DISCONNECTED
;
378 if (tstream_is_cli_np(transp
->stream
)) {
379 status
= NT_STATUS_PIPE_DISCONNECTED
;
381 tevent_req_nterror(req
, status
);
382 return tevent_req_post(req
, ev
);
385 state
->transp
= transp
;
386 state
->req
.iov_len
= data_len
;
387 state
->req
.iov_base
= discard_const_p(void *, data
);
388 state
->max_rdata_len
= max_rdata_len
;
390 endtime
= timeval_current_ofs_msec(transp
->timeout
);
392 if (tstream_is_cli_np(transp
->stream
)) {
395 if (tevent_queue_length(transp
->write_queue
) > 0) {
398 if (tevent_queue_length(transp
->read_queue
) > 0) {
403 tstream_cli_np_use_trans(transp
->stream
);
406 subreq
= tstream_writev_queue_send(state
, ev
,
410 if (tevent_req_nomem(subreq
, req
)) {
411 return tevent_req_post(req
, ev
);
413 if (!tevent_req_set_endtime(subreq
, ev
, endtime
)) {
414 return tevent_req_post(req
, ev
);
416 tevent_req_set_callback(subreq
, rpc_tstream_trans_writev
, req
);
418 subreq
= tstream_readv_pdu_queue_send(state
, ev
,
421 rpc_tstream_trans_next_vector
,
423 if (tevent_req_nomem(subreq
, req
)) {
424 return tevent_req_post(req
, ev
);
426 if (!tevent_req_set_endtime(subreq
, ev
, endtime
)) {
427 return tevent_req_post(req
, ev
);
429 tevent_req_set_callback(subreq
, rpc_tstream_trans_readv_pdu
, req
);
434 static void rpc_tstream_trans_writev(struct tevent_req
*subreq
)
436 struct tevent_req
*req
=
437 tevent_req_callback_data(subreq
,
439 struct rpc_tstream_trans_state
*state
=
441 struct rpc_tstream_trans_state
);
445 ret
= tstream_writev_queue_recv(subreq
, &err
);
448 rpc_tstream_disconnect(state
->transp
);
449 tevent_req_nterror(req
, map_nt_error_from_unix(err
));
454 static int rpc_tstream_trans_next_vector(struct tstream_context
*stream
,
457 struct iovec
**_vector
,
460 struct rpc_tstream_trans_state
*state
=
461 talloc_get_type_abort(private_data
,
462 struct rpc_tstream_trans_state
);
463 struct iovec
*vector
;
465 if (state
->max_rdata_len
== state
->rep
.iov_len
) {
471 state
->rep
.iov_base
= talloc_array(state
, uint8_t,
472 state
->max_rdata_len
);
473 if (state
->rep
.iov_base
== NULL
) {
476 state
->rep
.iov_len
= state
->max_rdata_len
;
478 vector
= talloc_array(mem_ctx
, struct iovec
, 1);
483 vector
[0] = state
->rep
;
490 static void rpc_tstream_trans_readv_pdu(struct tevent_req
*subreq
)
492 struct tevent_req
*req
=
493 tevent_req_callback_data(subreq
,
495 struct rpc_tstream_trans_state
*state
=
497 struct rpc_tstream_trans_state
);
501 ret
= tstream_readv_pdu_queue_recv(subreq
, &err
);
504 rpc_tstream_disconnect(state
->transp
);
505 tevent_req_nterror(req
, map_nt_error_from_unix(err
));
509 tevent_req_done(req
);
512 static NTSTATUS
rpc_tstream_trans_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
513 uint8_t **prdata
, uint32_t *prdata_len
)
515 struct rpc_tstream_trans_state
*state
=
517 struct rpc_tstream_trans_state
);
520 if (tevent_req_is_nterror(req
, &status
)) {
524 *prdata
= (uint8_t *)talloc_move(mem_ctx
, &state
->rep
.iov_base
);
525 *prdata_len
= state
->rep
.iov_len
;
530 * @brief Initialize a tstream transport facility
531 * NOTE: this function will talloc_steal, the stream and the queues.
533 * @param mem_ctx - memory context used to allocate the transport
534 * @param stream - a ready to use tstream
535 * @param presult - the transport structure
537 * @return - a NT Status error code.
539 NTSTATUS
rpc_transport_tstream_init(TALLOC_CTX
*mem_ctx
,
540 struct tstream_context
**stream
,
541 struct rpc_cli_transport
**presult
)
543 struct rpc_cli_transport
*result
;
544 struct rpc_tstream_state
*state
;
546 result
= talloc(mem_ctx
, struct rpc_cli_transport
);
547 if (result
== NULL
) {
548 return NT_STATUS_NO_MEMORY
;
550 state
= talloc(result
, struct rpc_tstream_state
);
553 return NT_STATUS_NO_MEMORY
;
555 result
->priv
= state
;
557 state
->read_queue
= tevent_queue_create(state
, "read_queue");
558 if (state
->read_queue
== NULL
) {
560 return NT_STATUS_NO_MEMORY
;
562 state
->write_queue
= tevent_queue_create(state
, "write_queue");
563 if (state
->write_queue
== NULL
) {
565 return NT_STATUS_NO_MEMORY
;
568 state
->stream
= talloc_move(state
, stream
);
569 state
->timeout
= 10000; /* 10 seconds. */
571 if (tstream_is_cli_np(state
->stream
)) {
572 result
->trans_send
= rpc_tstream_trans_send
;
573 result
->trans_recv
= rpc_tstream_trans_recv
;
575 result
->trans_send
= NULL
;
576 result
->trans_recv
= NULL
;
578 result
->write_send
= rpc_tstream_write_send
;
579 result
->write_recv
= rpc_tstream_write_recv
;
580 result
->read_send
= rpc_tstream_read_send
;
581 result
->read_recv
= rpc_tstream_read_recv
;
582 result
->is_connected
= rpc_tstream_is_connected
;
583 result
->set_timeout
= rpc_tstream_set_timeout
;
589 struct cli_state
*rpc_pipe_np_smb_conn(struct rpc_pipe_client
*p
)
591 struct rpc_tstream_state
*transp
=
592 talloc_get_type_abort(p
->transport
->priv
,
593 struct rpc_tstream_state
);
596 ok
= rpccli_is_connected(p
);
601 if (!tstream_is_cli_np(transp
->stream
)) {
605 return tstream_cli_np_get_cli_state(transp
->stream
);