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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
/*
* Unix SMB/CIFS implementation.
* RPC client transport over tstream
* Copyright (C) Simo Sorce 2010
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "lib/tsocket/tsocket.h"
#undef DBGC_CLASS
#define DBGC_CLASS DBGC_RPC_CLI
struct rpc_tstream_state {
struct tstream_context *stream;
struct tevent_queue *read_queue;
struct tevent_queue *write_queue;
int timeout;
};
static void rpc_tstream_disconnect(struct rpc_tstream_state *s)
{
TALLOC_FREE(s->stream);
}
static bool rpc_tstream_is_connected(void *priv)
{
struct rpc_tstream_state *transp =
talloc_get_type_abort(priv, struct rpc_tstream_state);
if (!transp->stream) {
return false;
}
return true;
}
static unsigned int rpc_tstream_set_timeout(void *priv, unsigned int timeout)
{
struct rpc_tstream_state *transp =
talloc_get_type_abort(priv, struct rpc_tstream_state);
int orig_timeout;
bool ok;
ok = rpc_tstream_is_connected(transp);
if (!ok) {
return 0;
}
orig_timeout = transp->timeout;
transp->timeout = timeout;
return orig_timeout;
}
struct rpc_tstream_next_vector_state {
uint8_t *buf;
size_t len;
off_t ofs;
size_t remaining;
};
static void rpc_tstream_next_vector_init(
struct rpc_tstream_next_vector_state *s,
uint8_t *buf, size_t len)
{
ZERO_STRUCTP(s);
s->buf = buf;
s->len = MIN(len, UINT16_MAX);
}
static int rpc_tstream_next_vector(struct tstream_context *stream,
void *private_data,
TALLOC_CTX *mem_ctx,
struct iovec **_vector,
size_t *count)
{
struct rpc_tstream_next_vector_state *state =
(struct rpc_tstream_next_vector_state *)private_data;
struct iovec *vector;
ssize_t pending;
size_t wanted;
if (state->ofs == state->len) {
*_vector = NULL;
*count = 0;
return 0;
}
pending = tstream_pending_bytes(stream);
if (pending == -1) {
return -1;
}
if (pending == 0 && state->ofs != 0) {
/* return a short read */
*_vector = NULL;
*count = 0;
return 0;
}
if (pending == 0) {
/* we want at least one byte and recheck again */
wanted = 1;
} else {
size_t missing = state->len - state->ofs;
if (pending > missing) {
/* there's more available */
state->remaining = pending - missing;
wanted = missing;
} else {
/* read what we can get and recheck in the next cycle */
wanted = pending;
}
}
vector = talloc_array(mem_ctx, struct iovec, 1);
if (!vector) {
return -1;
}
vector[0].iov_base = state->buf + state->ofs;
vector[0].iov_len = wanted;
state->ofs += wanted;
*_vector = vector;
*count = 1;
return 0;
}
struct rpc_tstream_read_state {
struct rpc_tstream_state *transp;
struct rpc_tstream_next_vector_state next_vector;
ssize_t nread;
};
static void rpc_tstream_read_done(struct tevent_req *subreq);
static struct tevent_req *rpc_tstream_read_send(TALLOC_CTX *mem_ctx,
struct event_context *ev,
uint8_t *data, size_t size,
void *priv)
{
struct rpc_tstream_state *transp =
talloc_get_type_abort(priv, struct rpc_tstream_state);
struct tevent_req *req, *subreq;
struct rpc_tstream_read_state *state;
struct timeval endtime;
req = tevent_req_create(mem_ctx, &state, struct rpc_tstream_read_state);
if (req == NULL) {
return NULL;
}
if (!rpc_tstream_is_connected(transp)) {
tevent_req_nterror(req, NT_STATUS_CONNECTION_INVALID);
return tevent_req_post(req, ev);
}
state->transp = transp;
rpc_tstream_next_vector_init(&state->next_vector, data, size);
subreq = tstream_readv_pdu_queue_send(state, ev,
transp->stream,
transp->read_queue,
rpc_tstream_next_vector,
&state->next_vector);
if (subreq == NULL) {
tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
return tevent_req_post(req, ev);
}
endtime = timeval_current_ofs(0, transp->timeout * 1000);
if (!tevent_req_set_endtime(subreq, ev, endtime)) {
goto fail;
}
tevent_req_set_callback(subreq, rpc_tstream_read_done, req);
return req;
fail:
TALLOC_FREE(req);
return NULL;
}
static void rpc_tstream_read_done(struct tevent_req *subreq)
{
struct tevent_req *req =
tevent_req_callback_data(subreq, struct tevent_req);
struct rpc_tstream_read_state *state =
tevent_req_data(req, struct rpc_tstream_read_state);
int err;
state->nread = tstream_readv_pdu_queue_recv(subreq, &err);
TALLOC_FREE(subreq);
if (state->nread < 0) {
rpc_tstream_disconnect(state->transp);
tevent_req_nterror(req, map_nt_error_from_unix(err));
return;
}
tevent_req_done(req);
}
static NTSTATUS rpc_tstream_read_recv(struct tevent_req *req, ssize_t *size)
{
struct rpc_tstream_read_state *state = tevent_req_data(
req, struct rpc_tstream_read_state);
NTSTATUS status;
if (tevent_req_is_nterror(req, &status)) {
return status;
}
*size = state->nread;
return NT_STATUS_OK;
}
struct rpc_tstream_write_state {
struct event_context *ev;
struct rpc_tstream_state *transp;
struct iovec iov;
ssize_t nwritten;
};
static void rpc_tstream_write_done(struct tevent_req *subreq);
static struct tevent_req *rpc_tstream_write_send(TALLOC_CTX *mem_ctx,
struct event_context *ev,
const uint8_t *data, size_t size,
void *priv)
{
struct rpc_tstream_state *transp =
talloc_get_type_abort(priv, struct rpc_tstream_state);
struct tevent_req *req, *subreq;
struct rpc_tstream_write_state *state;
struct timeval endtime;
req = tevent_req_create(mem_ctx, &state, struct rpc_tstream_write_state);
if (req == NULL) {
return NULL;
}
if (!rpc_tstream_is_connected(transp)) {
tevent_req_nterror(req, NT_STATUS_CONNECTION_INVALID);
return tevent_req_post(req, ev);
}
state->ev = ev;
state->transp = transp;
state->iov.iov_base = discard_const_p(void *, data);
state->iov.iov_len = size;
subreq = tstream_writev_queue_send(state, ev,
transp->stream,
transp->write_queue,
&state->iov, 1);
if (subreq == NULL) {
goto fail;
}
endtime = timeval_current_ofs(0, transp->timeout * 1000);
if (!tevent_req_set_endtime(subreq, ev, endtime)) {
goto fail;
}
tevent_req_set_callback(subreq, rpc_tstream_write_done, req);
return req;
fail:
TALLOC_FREE(req);
return NULL;
}
static void rpc_tstream_write_done(struct tevent_req *subreq)
{
struct tevent_req *req =
tevent_req_callback_data(subreq, struct tevent_req);
struct rpc_tstream_write_state *state =
tevent_req_data(req, struct rpc_tstream_write_state);
int err;
state->nwritten = tstream_writev_queue_recv(subreq, &err);
TALLOC_FREE(subreq);
if (state->nwritten < 0) {
rpc_tstream_disconnect(state->transp);
tevent_req_nterror(req, map_nt_error_from_unix(err));
return;
}
tevent_req_done(req);
}
static NTSTATUS rpc_tstream_write_recv(struct tevent_req *req, ssize_t *sent)
{
struct rpc_tstream_write_state *state =
tevent_req_data(req, struct rpc_tstream_write_state);
NTSTATUS status;
if (tevent_req_is_nterror(req, &status)) {
return status;
}
*sent = state->nwritten;
return NT_STATUS_OK;
}
/**
* @brief Initialize a tstream transport facility
* NOTE: this function will talloc_steal, the stream and the queues.
*
* @param mem_ctx - memory context used to allocate the transport
* @param stream - a ready to use tstream
* @param read_queue - pre-createted tstream read queue
* @param write_queue - pre-createted tstream write queue
* @param presult - the transport structure
*
* @return - a NT Status error code.
*/
NTSTATUS rpc_transport_tstream_init(TALLOC_CTX *mem_ctx,
struct tstream_context *stream,
struct tevent_queue *read_queue,
struct tevent_queue *write_queue,
struct rpc_cli_transport **presult)
{
struct rpc_cli_transport *result;
struct rpc_tstream_state *state;
result = talloc(mem_ctx, struct rpc_cli_transport);
if (result == NULL) {
return NT_STATUS_NO_MEMORY;
}
state = talloc(result, struct rpc_tstream_state);
if (state == NULL) {
TALLOC_FREE(result);
return NT_STATUS_NO_MEMORY;
}
result->priv = state;
state->stream = talloc_steal(state, stream);
state->read_queue = talloc_steal(state, read_queue);
state->write_queue = talloc_steal(state, write_queue);
state->timeout = 10000; /* 10 seconds. */
result->trans_send = NULL;
result->trans_recv = NULL;
result->write_send = rpc_tstream_write_send;
result->write_recv = rpc_tstream_write_recv;
result->read_send = rpc_tstream_read_send;
result->read_recv = rpc_tstream_read_recv;
result->is_connected = rpc_tstream_is_connected;
result->set_timeout = rpc_tstream_set_timeout;
*presult = result;
return NT_STATUS_OK;
}
|