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/tsocket/tsocket.h"
22 #include "libsmb/cli_np_tstream.h"
25 #define DBGC_CLASS DBGC_RPC_CLI
27 struct rpc_tstream_state
{
28 struct tstream_context
*stream
;
29 struct tevent_queue
*read_queue
;
30 struct tevent_queue
*write_queue
;
34 static void rpc_tstream_disconnect(struct rpc_tstream_state
*s
)
36 TALLOC_FREE(s
->stream
);
39 static bool rpc_tstream_is_connected(void *priv
)
41 struct rpc_tstream_state
*transp
=
42 talloc_get_type_abort(priv
, struct rpc_tstream_state
);
45 if (!transp
->stream
) {
49 if (!tstream_is_cli_np(transp
->stream
)) {
53 ret
= tstream_pending_bytes(transp
->stream
);
61 static unsigned int rpc_tstream_set_timeout(void *priv
, unsigned int timeout
)
63 struct rpc_tstream_state
*transp
=
64 talloc_get_type_abort(priv
, struct rpc_tstream_state
);
68 ok
= rpc_tstream_is_connected(transp
);
73 if (tstream_is_cli_np(transp
->stream
)) {
74 transp
->timeout
= timeout
;
75 return tstream_cli_np_set_timeout(transp
->stream
, timeout
);
78 orig_timeout
= transp
->timeout
;
80 transp
->timeout
= timeout
;
85 struct rpc_tstream_next_vector_state
{
92 static void rpc_tstream_next_vector_init(
93 struct rpc_tstream_next_vector_state
*s
,
94 uint8_t *buf
, size_t len
)
99 s
->len
= MIN(len
, UINT16_MAX
);
102 static int rpc_tstream_next_vector(struct tstream_context
*stream
,
105 struct iovec
**_vector
,
108 struct rpc_tstream_next_vector_state
*state
=
109 (struct rpc_tstream_next_vector_state
*)private_data
;
110 struct iovec
*vector
;
114 if (state
->ofs
== state
->len
) {
120 pending
= tstream_pending_bytes(stream
);
125 if (pending
== 0 && state
->ofs
!= 0) {
126 /* return a short read */
133 /* we want at least one byte and recheck again */
136 size_t missing
= state
->len
- state
->ofs
;
137 if (pending
> missing
) {
138 /* there's more available */
139 state
->remaining
= pending
- missing
;
142 /* read what we can get and recheck in the next cycle */
147 vector
= talloc_array(mem_ctx
, struct iovec
, 1);
152 vector
[0].iov_base
= state
->buf
+ state
->ofs
;
153 vector
[0].iov_len
= wanted
;
155 state
->ofs
+= wanted
;
162 struct rpc_tstream_read_state
{
163 struct rpc_tstream_state
*transp
;
164 struct rpc_tstream_next_vector_state next_vector
;
168 static void rpc_tstream_read_done(struct tevent_req
*subreq
);
170 static struct tevent_req
*rpc_tstream_read_send(TALLOC_CTX
*mem_ctx
,
171 struct event_context
*ev
,
172 uint8_t *data
, size_t size
,
175 struct rpc_tstream_state
*transp
=
176 talloc_get_type_abort(priv
, struct rpc_tstream_state
);
177 struct tevent_req
*req
, *subreq
;
178 struct rpc_tstream_read_state
*state
;
179 struct timeval endtime
;
181 req
= tevent_req_create(mem_ctx
, &state
, struct rpc_tstream_read_state
);
185 if (!rpc_tstream_is_connected(transp
)) {
186 tevent_req_nterror(req
, NT_STATUS_CONNECTION_INVALID
);
187 return tevent_req_post(req
, ev
);
189 state
->transp
= transp
;
190 rpc_tstream_next_vector_init(&state
->next_vector
, data
, size
);
192 subreq
= tstream_readv_pdu_queue_send(state
, ev
,
195 rpc_tstream_next_vector
,
196 &state
->next_vector
);
197 if (subreq
== NULL
) {
198 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
199 return tevent_req_post(req
, ev
);
202 endtime
= timeval_current_ofs(0, transp
->timeout
* 1000);
203 if (!tevent_req_set_endtime(subreq
, ev
, endtime
)) {
207 tevent_req_set_callback(subreq
, rpc_tstream_read_done
, req
);
214 static void rpc_tstream_read_done(struct tevent_req
*subreq
)
216 struct tevent_req
*req
=
217 tevent_req_callback_data(subreq
, struct tevent_req
);
218 struct rpc_tstream_read_state
*state
=
219 tevent_req_data(req
, struct rpc_tstream_read_state
);
222 state
->nread
= tstream_readv_pdu_queue_recv(subreq
, &err
);
224 if (state
->nread
< 0) {
225 rpc_tstream_disconnect(state
->transp
);
226 tevent_req_nterror(req
, map_nt_error_from_unix(err
));
229 tevent_req_done(req
);
232 static NTSTATUS
rpc_tstream_read_recv(struct tevent_req
*req
, ssize_t
*size
)
234 struct rpc_tstream_read_state
*state
= tevent_req_data(
235 req
, struct rpc_tstream_read_state
);
238 if (tevent_req_is_nterror(req
, &status
)) {
241 *size
= state
->nread
;
245 struct rpc_tstream_write_state
{
246 struct event_context
*ev
;
247 struct rpc_tstream_state
*transp
;
252 static void rpc_tstream_write_done(struct tevent_req
*subreq
);
254 static struct tevent_req
*rpc_tstream_write_send(TALLOC_CTX
*mem_ctx
,
255 struct event_context
*ev
,
256 const uint8_t *data
, size_t size
,
259 struct rpc_tstream_state
*transp
=
260 talloc_get_type_abort(priv
, struct rpc_tstream_state
);
261 struct tevent_req
*req
, *subreq
;
262 struct rpc_tstream_write_state
*state
;
263 struct timeval endtime
;
265 req
= tevent_req_create(mem_ctx
, &state
, struct rpc_tstream_write_state
);
269 if (!rpc_tstream_is_connected(transp
)) {
270 tevent_req_nterror(req
, NT_STATUS_CONNECTION_INVALID
);
271 return tevent_req_post(req
, ev
);
274 state
->transp
= transp
;
275 state
->iov
.iov_base
= discard_const_p(void *, data
);
276 state
->iov
.iov_len
= size
;
278 subreq
= tstream_writev_queue_send(state
, ev
,
282 if (subreq
== NULL
) {
286 endtime
= timeval_current_ofs(0, transp
->timeout
* 1000);
287 if (!tevent_req_set_endtime(subreq
, ev
, endtime
)) {
291 tevent_req_set_callback(subreq
, rpc_tstream_write_done
, req
);
298 static void rpc_tstream_write_done(struct tevent_req
*subreq
)
300 struct tevent_req
*req
=
301 tevent_req_callback_data(subreq
, struct tevent_req
);
302 struct rpc_tstream_write_state
*state
=
303 tevent_req_data(req
, struct rpc_tstream_write_state
);
306 state
->nwritten
= tstream_writev_queue_recv(subreq
, &err
);
308 if (state
->nwritten
< 0) {
309 rpc_tstream_disconnect(state
->transp
);
310 tevent_req_nterror(req
, map_nt_error_from_unix(err
));
313 tevent_req_done(req
);
316 static NTSTATUS
rpc_tstream_write_recv(struct tevent_req
*req
, ssize_t
*sent
)
318 struct rpc_tstream_write_state
*state
=
319 tevent_req_data(req
, struct rpc_tstream_write_state
);
322 if (tevent_req_is_nterror(req
, &status
)) {
325 *sent
= state
->nwritten
;
329 struct rpc_tstream_trans_state
{
330 struct tevent_context
*ev
;
331 struct rpc_tstream_state
*transp
;
333 uint32_t max_rdata_len
;
337 static void rpc_tstream_trans_writev(struct tevent_req
*subreq
);
338 static void rpc_tstream_trans_readv_pdu(struct tevent_req
*subreq
);
340 static int rpc_tstream_trans_next_vector(struct tstream_context
*stream
,
343 struct iovec
**_vector
,
346 static struct tevent_req
*rpc_tstream_trans_send(TALLOC_CTX
*mem_ctx
,
347 struct tevent_context
*ev
,
348 uint8_t *data
, size_t data_len
,
349 uint32_t max_rdata_len
,
352 struct rpc_tstream_state
*transp
=
353 talloc_get_type_abort(priv
, struct rpc_tstream_state
);
354 struct tevent_req
*req
, *subreq
;
355 struct rpc_tstream_trans_state
*state
;
356 struct timeval endtime
;
358 req
= tevent_req_create(mem_ctx
, &state
,
359 struct rpc_tstream_trans_state
);
364 if (!rpc_tstream_is_connected(transp
)) {
365 tevent_req_nterror(req
, NT_STATUS_CONNECTION_INVALID
);
366 return tevent_req_post(req
, ev
);
369 state
->transp
= transp
;
370 state
->req
.iov_len
= data_len
;
371 state
->req
.iov_base
= discard_const_p(void *, data
);
372 state
->max_rdata_len
= max_rdata_len
;
374 endtime
= timeval_current_ofs(0, transp
->timeout
* 1000);
376 subreq
= tstream_writev_queue_send(state
, ev
,
380 if (tevent_req_nomem(subreq
, req
)) {
381 return tevent_req_post(req
, ev
);
383 if (!tevent_req_set_endtime(subreq
, ev
, endtime
)) {
384 return tevent_req_post(req
, ev
);
386 tevent_req_set_callback(subreq
, rpc_tstream_trans_writev
, req
);
388 if (tstream_is_cli_np(transp
->stream
)) {
389 tstream_cli_np_use_trans(transp
->stream
);
392 subreq
= tstream_readv_pdu_queue_send(state
, ev
,
395 rpc_tstream_trans_next_vector
,
397 if (tevent_req_nomem(subreq
, req
)) {
398 return tevent_req_post(req
, ev
);
400 if (!tevent_req_set_endtime(subreq
, ev
, endtime
)) {
401 return tevent_req_post(req
, ev
);
403 tevent_req_set_callback(subreq
, rpc_tstream_trans_readv_pdu
, req
);
408 static void rpc_tstream_trans_writev(struct tevent_req
*subreq
)
410 struct tevent_req
*req
=
411 tevent_req_callback_data(subreq
,
413 struct rpc_tstream_trans_state
*state
=
415 struct rpc_tstream_trans_state
);
419 ret
= tstream_writev_queue_recv(subreq
, &err
);
422 rpc_tstream_disconnect(state
->transp
);
423 tevent_req_nterror(req
, map_nt_error_from_unix(err
));
428 static int rpc_tstream_trans_next_vector(struct tstream_context
*stream
,
431 struct iovec
**_vector
,
434 struct rpc_tstream_trans_state
*state
=
435 talloc_get_type_abort(private_data
,
436 struct rpc_tstream_trans_state
);
437 struct iovec
*vector
;
439 if (state
->max_rdata_len
== state
->rep
.iov_len
) {
445 state
->rep
.iov_base
= talloc_array(state
, uint8_t,
446 state
->max_rdata_len
);
447 if (state
->rep
.iov_base
== NULL
) {
450 state
->rep
.iov_len
= state
->max_rdata_len
;
452 vector
= talloc_array(mem_ctx
, struct iovec
, 1);
457 vector
[0] = state
->rep
;
464 static void rpc_tstream_trans_readv_pdu(struct tevent_req
*subreq
)
466 struct tevent_req
*req
=
467 tevent_req_callback_data(subreq
,
469 struct rpc_tstream_trans_state
*state
=
471 struct rpc_tstream_trans_state
);
475 ret
= tstream_readv_pdu_queue_recv(subreq
, &err
);
478 rpc_tstream_disconnect(state
->transp
);
479 tevent_req_nterror(req
, map_nt_error_from_unix(err
));
483 tevent_req_done(req
);
486 static NTSTATUS
rpc_tstream_trans_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
487 uint8_t **prdata
, uint32_t *prdata_len
)
489 struct rpc_tstream_trans_state
*state
=
491 struct rpc_tstream_trans_state
);
494 if (tevent_req_is_nterror(req
, &status
)) {
498 *prdata
= (uint8_t *)talloc_move(mem_ctx
, &state
->rep
.iov_base
);
499 *prdata_len
= state
->rep
.iov_len
;
504 * @brief Initialize a tstream transport facility
505 * NOTE: this function will talloc_steal, the stream and the queues.
507 * @param mem_ctx - memory context used to allocate the transport
508 * @param stream - a ready to use tstream
509 * @param presult - the transport structure
511 * @return - a NT Status error code.
513 NTSTATUS
rpc_transport_tstream_init(TALLOC_CTX
*mem_ctx
,
514 struct tstream_context
**stream
,
515 struct rpc_cli_transport
**presult
)
517 struct rpc_cli_transport
*result
;
518 struct rpc_tstream_state
*state
;
520 result
= talloc(mem_ctx
, struct rpc_cli_transport
);
521 if (result
== NULL
) {
522 return NT_STATUS_NO_MEMORY
;
524 state
= talloc(result
, struct rpc_tstream_state
);
527 return NT_STATUS_NO_MEMORY
;
529 result
->priv
= state
;
531 state
->read_queue
= tevent_queue_create(state
, "read_queue");
532 if (state
->read_queue
== NULL
) {
534 return NT_STATUS_NO_MEMORY
;
536 state
->write_queue
= tevent_queue_create(state
, "write_queue");
537 if (state
->write_queue
== NULL
) {
539 return NT_STATUS_NO_MEMORY
;
542 state
->stream
= talloc_move(state
, stream
);
543 state
->timeout
= 10000; /* 10 seconds. */
545 if (tstream_is_cli_np(state
->stream
)) {
546 result
->trans_send
= rpc_tstream_trans_send
;
547 result
->trans_recv
= rpc_tstream_trans_recv
;
549 result
->trans_send
= NULL
;
550 result
->trans_recv
= NULL
;
552 result
->write_send
= rpc_tstream_write_send
;
553 result
->write_recv
= rpc_tstream_write_recv
;
554 result
->read_send
= rpc_tstream_read_send
;
555 result
->read_recv
= rpc_tstream_read_recv
;
556 result
->is_connected
= rpc_tstream_is_connected
;
557 result
->set_timeout
= rpc_tstream_set_timeout
;
563 struct cli_state
*rpc_pipe_np_smb_conn(struct rpc_pipe_client
*p
)
565 struct rpc_tstream_state
*transp
=
566 talloc_get_type_abort(p
->transport
->priv
,
567 struct rpc_tstream_state
);
570 ok
= rpccli_is_connected(p
);
575 if (!tstream_is_cli_np(transp
->stream
)) {
579 return tstream_cli_np_get_cli_state(transp
->stream
);