1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 |
|
---|
4 | Copyright (C) Stefan Metzmacher 2009
|
---|
5 |
|
---|
6 | ** NOTE! The following LGPL license applies to the tsocket
|
---|
7 | ** library. This does NOT imply that all of Samba is released
|
---|
8 | ** under the LGPL
|
---|
9 |
|
---|
10 | This library is free software; you can redistribute it and/or
|
---|
11 | modify it under the terms of the GNU Lesser General Public
|
---|
12 | License as published by the Free Software Foundation; either
|
---|
13 | version 3 of the License, or (at your option) any later version.
|
---|
14 |
|
---|
15 | This library is distributed in the hope that it will be useful,
|
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
18 | Lesser General Public License for more details.
|
---|
19 |
|
---|
20 | You should have received a copy of the GNU Lesser General Public
|
---|
21 | License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
---|
22 | */
|
---|
23 |
|
---|
24 | #include "replace.h"
|
---|
25 | #include "system/filesys.h"
|
---|
26 | #include "tsocket.h"
|
---|
27 | #include "tsocket_internal.h"
|
---|
28 |
|
---|
29 | struct tdgram_sendto_queue_state {
|
---|
30 | /* this structs are owned by the caller */
|
---|
31 | struct {
|
---|
32 | struct tevent_context *ev;
|
---|
33 | struct tdgram_context *dgram;
|
---|
34 | const uint8_t *buf;
|
---|
35 | size_t len;
|
---|
36 | const struct tsocket_address *dst;
|
---|
37 | } caller;
|
---|
38 | ssize_t ret;
|
---|
39 | };
|
---|
40 |
|
---|
41 | static void tdgram_sendto_queue_trigger(struct tevent_req *req,
|
---|
42 | void *private_data);
|
---|
43 | static void tdgram_sendto_queue_done(struct tevent_req *subreq);
|
---|
44 |
|
---|
45 | struct tevent_req *tdgram_sendto_queue_send(TALLOC_CTX *mem_ctx,
|
---|
46 | struct tevent_context *ev,
|
---|
47 | struct tdgram_context *dgram,
|
---|
48 | struct tevent_queue *queue,
|
---|
49 | const uint8_t *buf,
|
---|
50 | size_t len,
|
---|
51 | struct tsocket_address *dst)
|
---|
52 | {
|
---|
53 | struct tevent_req *req;
|
---|
54 | struct tdgram_sendto_queue_state *state;
|
---|
55 | bool ok;
|
---|
56 |
|
---|
57 | req = tevent_req_create(mem_ctx, &state,
|
---|
58 | struct tdgram_sendto_queue_state);
|
---|
59 | if (!req) {
|
---|
60 | return NULL;
|
---|
61 | }
|
---|
62 |
|
---|
63 | state->caller.ev = ev;
|
---|
64 | state->caller.dgram = dgram;
|
---|
65 | state->caller.buf = buf;
|
---|
66 | state->caller.len = len;
|
---|
67 | state->caller.dst = dst;
|
---|
68 | state->ret = -1;
|
---|
69 |
|
---|
70 | ok = tevent_queue_add(queue,
|
---|
71 | ev,
|
---|
72 | req,
|
---|
73 | tdgram_sendto_queue_trigger,
|
---|
74 | NULL);
|
---|
75 | if (!ok) {
|
---|
76 | tevent_req_nomem(NULL, req);
|
---|
77 | goto post;
|
---|
78 | }
|
---|
79 |
|
---|
80 | return req;
|
---|
81 |
|
---|
82 | post:
|
---|
83 | tevent_req_post(req, ev);
|
---|
84 | return req;
|
---|
85 | }
|
---|
86 |
|
---|
87 | static void tdgram_sendto_queue_trigger(struct tevent_req *req,
|
---|
88 | void *private_data)
|
---|
89 | {
|
---|
90 | struct tdgram_sendto_queue_state *state = tevent_req_data(req,
|
---|
91 | struct tdgram_sendto_queue_state);
|
---|
92 | struct tevent_req *subreq;
|
---|
93 |
|
---|
94 | subreq = tdgram_sendto_send(state,
|
---|
95 | state->caller.ev,
|
---|
96 | state->caller.dgram,
|
---|
97 | state->caller.buf,
|
---|
98 | state->caller.len,
|
---|
99 | state->caller.dst);
|
---|
100 | if (tevent_req_nomem(subreq, req)) {
|
---|
101 | return;
|
---|
102 | }
|
---|
103 | tevent_req_set_callback(subreq, tdgram_sendto_queue_done, req);
|
---|
104 | }
|
---|
105 |
|
---|
106 | static void tdgram_sendto_queue_done(struct tevent_req *subreq)
|
---|
107 | {
|
---|
108 | struct tevent_req *req = tevent_req_callback_data(subreq,
|
---|
109 | struct tevent_req);
|
---|
110 | struct tdgram_sendto_queue_state *state = tevent_req_data(req,
|
---|
111 | struct tdgram_sendto_queue_state);
|
---|
112 | ssize_t ret;
|
---|
113 | int sys_errno;
|
---|
114 |
|
---|
115 | ret = tdgram_sendto_recv(subreq, &sys_errno);
|
---|
116 | talloc_free(subreq);
|
---|
117 | if (ret == -1) {
|
---|
118 | tevent_req_error(req, sys_errno);
|
---|
119 | return;
|
---|
120 | }
|
---|
121 | state->ret = ret;
|
---|
122 |
|
---|
123 | tevent_req_done(req);
|
---|
124 | }
|
---|
125 |
|
---|
126 | ssize_t tdgram_sendto_queue_recv(struct tevent_req *req, int *perrno)
|
---|
127 | {
|
---|
128 | struct tdgram_sendto_queue_state *state = tevent_req_data(req,
|
---|
129 | struct tdgram_sendto_queue_state);
|
---|
130 | ssize_t ret;
|
---|
131 |
|
---|
132 | ret = tsocket_simple_int_recv(req, perrno);
|
---|
133 | if (ret == 0) {
|
---|
134 | ret = state->ret;
|
---|
135 | }
|
---|
136 |
|
---|
137 | tevent_req_received(req);
|
---|
138 | return ret;
|
---|
139 | }
|
---|
140 |
|
---|
141 | struct tstream_readv_pdu_state {
|
---|
142 | /* this structs are owned by the caller */
|
---|
143 | struct {
|
---|
144 | struct tevent_context *ev;
|
---|
145 | struct tstream_context *stream;
|
---|
146 | tstream_readv_pdu_next_vector_t next_vector_fn;
|
---|
147 | void *next_vector_private;
|
---|
148 | } caller;
|
---|
149 |
|
---|
150 | /*
|
---|
151 | * Each call to the callback resets iov and count
|
---|
152 | * the callback allocated the iov as child of our state,
|
---|
153 | * that means we are allowed to modify and free it.
|
---|
154 | *
|
---|
155 | * we should call the callback every time we filled the given
|
---|
156 | * vector and ask for a new vector. We return if the callback
|
---|
157 | * ask for 0 bytes.
|
---|
158 | */
|
---|
159 | struct iovec *vector;
|
---|
160 | size_t count;
|
---|
161 |
|
---|
162 | /*
|
---|
163 | * the total number of bytes we read,
|
---|
164 | * the return value of the _recv function
|
---|
165 | */
|
---|
166 | int total_read;
|
---|
167 | };
|
---|
168 |
|
---|
169 | static void tstream_readv_pdu_ask_for_next_vector(struct tevent_req *req);
|
---|
170 | static void tstream_readv_pdu_readv_done(struct tevent_req *subreq);
|
---|
171 |
|
---|
172 | struct tevent_req *tstream_readv_pdu_send(TALLOC_CTX *mem_ctx,
|
---|
173 | struct tevent_context *ev,
|
---|
174 | struct tstream_context *stream,
|
---|
175 | tstream_readv_pdu_next_vector_t next_vector_fn,
|
---|
176 | void *next_vector_private)
|
---|
177 | {
|
---|
178 | struct tevent_req *req;
|
---|
179 | struct tstream_readv_pdu_state *state;
|
---|
180 |
|
---|
181 | req = tevent_req_create(mem_ctx, &state,
|
---|
182 | struct tstream_readv_pdu_state);
|
---|
183 | if (!req) {
|
---|
184 | return NULL;
|
---|
185 | }
|
---|
186 |
|
---|
187 | state->caller.ev = ev;
|
---|
188 | state->caller.stream = stream;
|
---|
189 | state->caller.next_vector_fn = next_vector_fn;
|
---|
190 | state->caller.next_vector_private = next_vector_private;
|
---|
191 |
|
---|
192 | state->vector = NULL;
|
---|
193 | state->count = 0;
|
---|
194 | state->total_read = 0;
|
---|
195 |
|
---|
196 | tstream_readv_pdu_ask_for_next_vector(req);
|
---|
197 | if (!tevent_req_is_in_progress(req)) {
|
---|
198 | goto post;
|
---|
199 | }
|
---|
200 |
|
---|
201 | return req;
|
---|
202 |
|
---|
203 | post:
|
---|
204 | return tevent_req_post(req, ev);
|
---|
205 | }
|
---|
206 |
|
---|
207 | static void tstream_readv_pdu_ask_for_next_vector(struct tevent_req *req)
|
---|
208 | {
|
---|
209 | struct tstream_readv_pdu_state *state = tevent_req_data(req,
|
---|
210 | struct tstream_readv_pdu_state);
|
---|
211 | int ret;
|
---|
212 | size_t to_read = 0;
|
---|
213 | size_t i;
|
---|
214 | struct tevent_req *subreq;
|
---|
215 | bool optimize = false;
|
---|
216 | bool save_optimize = false;
|
---|
217 |
|
---|
218 | if (state->count > 0) {
|
---|
219 | /*
|
---|
220 | * This is not the first time we asked for a vector,
|
---|
221 | * which means parts of the pdu already arrived.
|
---|
222 | *
|
---|
223 | * In this case it make sense to enable
|
---|
224 | * a syscall/performance optimization if the
|
---|
225 | * low level tstream implementation supports it.
|
---|
226 | */
|
---|
227 | optimize = true;
|
---|
228 | }
|
---|
229 |
|
---|
230 | TALLOC_FREE(state->vector);
|
---|
231 | state->count = 0;
|
---|
232 |
|
---|
233 | ret = state->caller.next_vector_fn(state->caller.stream,
|
---|
234 | state->caller.next_vector_private,
|
---|
235 | state, &state->vector, &state->count);
|
---|
236 | if (ret == -1) {
|
---|
237 | tevent_req_error(req, errno);
|
---|
238 | return;
|
---|
239 | }
|
---|
240 |
|
---|
241 | if (state->count == 0) {
|
---|
242 | tevent_req_done(req);
|
---|
243 | return;
|
---|
244 | }
|
---|
245 |
|
---|
246 | for (i=0; i < state->count; i++) {
|
---|
247 | size_t tmp = to_read;
|
---|
248 | tmp += state->vector[i].iov_len;
|
---|
249 |
|
---|
250 | if (tmp < to_read) {
|
---|
251 | tevent_req_error(req, EMSGSIZE);
|
---|
252 | return;
|
---|
253 | }
|
---|
254 |
|
---|
255 | to_read = tmp;
|
---|
256 | }
|
---|
257 |
|
---|
258 | /*
|
---|
259 | * this is invalid the next vector function should have
|
---|
260 | * reported count == 0.
|
---|
261 | */
|
---|
262 | if (to_read == 0) {
|
---|
263 | tevent_req_error(req, EINVAL);
|
---|
264 | return;
|
---|
265 | }
|
---|
266 |
|
---|
267 | if (state->total_read + to_read < state->total_read) {
|
---|
268 | tevent_req_error(req, EMSGSIZE);
|
---|
269 | return;
|
---|
270 | }
|
---|
271 |
|
---|
272 | if (optimize) {
|
---|
273 | /*
|
---|
274 | * If the low level stream is a bsd socket
|
---|
275 | * we will get syscall optimization.
|
---|
276 | *
|
---|
277 | * If it is not a bsd socket
|
---|
278 | * tstream_bsd_optimize_readv() just returns.
|
---|
279 | */
|
---|
280 | save_optimize = tstream_bsd_optimize_readv(state->caller.stream,
|
---|
281 | true);
|
---|
282 | }
|
---|
283 | subreq = tstream_readv_send(state,
|
---|
284 | state->caller.ev,
|
---|
285 | state->caller.stream,
|
---|
286 | state->vector,
|
---|
287 | state->count);
|
---|
288 | if (optimize) {
|
---|
289 | tstream_bsd_optimize_readv(state->caller.stream,
|
---|
290 | save_optimize);
|
---|
291 | }
|
---|
292 | if (tevent_req_nomem(subreq, req)) {
|
---|
293 | return;
|
---|
294 | }
|
---|
295 | tevent_req_set_callback(subreq, tstream_readv_pdu_readv_done, req);
|
---|
296 | }
|
---|
297 |
|
---|
298 | static void tstream_readv_pdu_readv_done(struct tevent_req *subreq)
|
---|
299 | {
|
---|
300 | struct tevent_req *req = tevent_req_callback_data(subreq,
|
---|
301 | struct tevent_req);
|
---|
302 | struct tstream_readv_pdu_state *state = tevent_req_data(req,
|
---|
303 | struct tstream_readv_pdu_state);
|
---|
304 | int ret;
|
---|
305 | int sys_errno;
|
---|
306 |
|
---|
307 | ret = tstream_readv_recv(subreq, &sys_errno);
|
---|
308 | if (ret == -1) {
|
---|
309 | tevent_req_error(req, sys_errno);
|
---|
310 | return;
|
---|
311 | }
|
---|
312 |
|
---|
313 | state->total_read += ret;
|
---|
314 |
|
---|
315 | /* ask the callback for a new vector we should fill */
|
---|
316 | tstream_readv_pdu_ask_for_next_vector(req);
|
---|
317 | }
|
---|
318 |
|
---|
319 | int tstream_readv_pdu_recv(struct tevent_req *req, int *perrno)
|
---|
320 | {
|
---|
321 | struct tstream_readv_pdu_state *state = tevent_req_data(req,
|
---|
322 | struct tstream_readv_pdu_state);
|
---|
323 | int ret;
|
---|
324 |
|
---|
325 | ret = tsocket_simple_int_recv(req, perrno);
|
---|
326 | if (ret == 0) {
|
---|
327 | ret = state->total_read;
|
---|
328 | }
|
---|
329 |
|
---|
330 | tevent_req_received(req);
|
---|
331 | return ret;
|
---|
332 | }
|
---|
333 |
|
---|
334 | struct tstream_readv_pdu_queue_state {
|
---|
335 | /* this structs are owned by the caller */
|
---|
336 | struct {
|
---|
337 | struct tevent_context *ev;
|
---|
338 | struct tstream_context *stream;
|
---|
339 | tstream_readv_pdu_next_vector_t next_vector_fn;
|
---|
340 | void *next_vector_private;
|
---|
341 | } caller;
|
---|
342 | int ret;
|
---|
343 | };
|
---|
344 |
|
---|
345 | static void tstream_readv_pdu_queue_trigger(struct tevent_req *req,
|
---|
346 | void *private_data);
|
---|
347 | static void tstream_readv_pdu_queue_done(struct tevent_req *subreq);
|
---|
348 |
|
---|
349 | struct tevent_req *tstream_readv_pdu_queue_send(TALLOC_CTX *mem_ctx,
|
---|
350 | struct tevent_context *ev,
|
---|
351 | struct tstream_context *stream,
|
---|
352 | struct tevent_queue *queue,
|
---|
353 | tstream_readv_pdu_next_vector_t next_vector_fn,
|
---|
354 | void *next_vector_private)
|
---|
355 | {
|
---|
356 | struct tevent_req *req;
|
---|
357 | struct tstream_readv_pdu_queue_state *state;
|
---|
358 | bool ok;
|
---|
359 |
|
---|
360 | req = tevent_req_create(mem_ctx, &state,
|
---|
361 | struct tstream_readv_pdu_queue_state);
|
---|
362 | if (!req) {
|
---|
363 | return NULL;
|
---|
364 | }
|
---|
365 |
|
---|
366 | state->caller.ev = ev;
|
---|
367 | state->caller.stream = stream;
|
---|
368 | state->caller.next_vector_fn = next_vector_fn;
|
---|
369 | state->caller.next_vector_private = next_vector_private;
|
---|
370 | state->ret = -1;
|
---|
371 |
|
---|
372 | ok = tevent_queue_add(queue,
|
---|
373 | ev,
|
---|
374 | req,
|
---|
375 | tstream_readv_pdu_queue_trigger,
|
---|
376 | NULL);
|
---|
377 | if (!ok) {
|
---|
378 | tevent_req_nomem(NULL, req);
|
---|
379 | goto post;
|
---|
380 | }
|
---|
381 |
|
---|
382 | return req;
|
---|
383 |
|
---|
384 | post:
|
---|
385 | return tevent_req_post(req, ev);
|
---|
386 | }
|
---|
387 |
|
---|
388 | static void tstream_readv_pdu_queue_trigger(struct tevent_req *req,
|
---|
389 | void *private_data)
|
---|
390 | {
|
---|
391 | struct tstream_readv_pdu_queue_state *state = tevent_req_data(req,
|
---|
392 | struct tstream_readv_pdu_queue_state);
|
---|
393 | struct tevent_req *subreq;
|
---|
394 |
|
---|
395 | subreq = tstream_readv_pdu_send(state,
|
---|
396 | state->caller.ev,
|
---|
397 | state->caller.stream,
|
---|
398 | state->caller.next_vector_fn,
|
---|
399 | state->caller.next_vector_private);
|
---|
400 | if (tevent_req_nomem(subreq, req)) {
|
---|
401 | return;
|
---|
402 | }
|
---|
403 | tevent_req_set_callback(subreq, tstream_readv_pdu_queue_done ,req);
|
---|
404 | }
|
---|
405 |
|
---|
406 | static void tstream_readv_pdu_queue_done(struct tevent_req *subreq)
|
---|
407 | {
|
---|
408 | struct tevent_req *req = tevent_req_callback_data(subreq,
|
---|
409 | struct tevent_req);
|
---|
410 | struct tstream_readv_pdu_queue_state *state = tevent_req_data(req,
|
---|
411 | struct tstream_readv_pdu_queue_state);
|
---|
412 | int ret;
|
---|
413 | int sys_errno;
|
---|
414 |
|
---|
415 | ret = tstream_readv_pdu_recv(subreq, &sys_errno);
|
---|
416 | talloc_free(subreq);
|
---|
417 | if (ret == -1) {
|
---|
418 | tevent_req_error(req, sys_errno);
|
---|
419 | return;
|
---|
420 | }
|
---|
421 | state->ret = ret;
|
---|
422 |
|
---|
423 | tevent_req_done(req);
|
---|
424 | }
|
---|
425 |
|
---|
426 | int tstream_readv_pdu_queue_recv(struct tevent_req *req, int *perrno)
|
---|
427 | {
|
---|
428 | struct tstream_readv_pdu_queue_state *state = tevent_req_data(req,
|
---|
429 | struct tstream_readv_pdu_queue_state);
|
---|
430 | int ret;
|
---|
431 |
|
---|
432 | ret = tsocket_simple_int_recv(req, perrno);
|
---|
433 | if (ret == 0) {
|
---|
434 | ret = state->ret;
|
---|
435 | }
|
---|
436 |
|
---|
437 | tevent_req_received(req);
|
---|
438 | return ret;
|
---|
439 | }
|
---|
440 |
|
---|
441 | struct tstream_writev_queue_state {
|
---|
442 | /* this structs are owned by the caller */
|
---|
443 | struct {
|
---|
444 | struct tevent_context *ev;
|
---|
445 | struct tstream_context *stream;
|
---|
446 | const struct iovec *vector;
|
---|
447 | size_t count;
|
---|
448 | } caller;
|
---|
449 | int ret;
|
---|
450 | };
|
---|
451 |
|
---|
452 | static void tstream_writev_queue_trigger(struct tevent_req *req,
|
---|
453 | void *private_data);
|
---|
454 | static void tstream_writev_queue_done(struct tevent_req *subreq);
|
---|
455 |
|
---|
456 | struct tevent_req *tstream_writev_queue_send(TALLOC_CTX *mem_ctx,
|
---|
457 | struct tevent_context *ev,
|
---|
458 | struct tstream_context *stream,
|
---|
459 | struct tevent_queue *queue,
|
---|
460 | const struct iovec *vector,
|
---|
461 | size_t count)
|
---|
462 | {
|
---|
463 | struct tevent_req *req;
|
---|
464 | struct tstream_writev_queue_state *state;
|
---|
465 | bool ok;
|
---|
466 |
|
---|
467 | req = tevent_req_create(mem_ctx, &state,
|
---|
468 | struct tstream_writev_queue_state);
|
---|
469 | if (!req) {
|
---|
470 | return NULL;
|
---|
471 | }
|
---|
472 |
|
---|
473 | state->caller.ev = ev;
|
---|
474 | state->caller.stream = stream;
|
---|
475 | state->caller.vector = vector;
|
---|
476 | state->caller.count = count;
|
---|
477 | state->ret = -1;
|
---|
478 |
|
---|
479 | ok = tevent_queue_add(queue,
|
---|
480 | ev,
|
---|
481 | req,
|
---|
482 | tstream_writev_queue_trigger,
|
---|
483 | NULL);
|
---|
484 | if (!ok) {
|
---|
485 | tevent_req_nomem(NULL, req);
|
---|
486 | goto post;
|
---|
487 | }
|
---|
488 |
|
---|
489 | return req;
|
---|
490 |
|
---|
491 | post:
|
---|
492 | return tevent_req_post(req, ev);
|
---|
493 | }
|
---|
494 |
|
---|
495 | static void tstream_writev_queue_trigger(struct tevent_req *req,
|
---|
496 | void *private_data)
|
---|
497 | {
|
---|
498 | struct tstream_writev_queue_state *state = tevent_req_data(req,
|
---|
499 | struct tstream_writev_queue_state);
|
---|
500 | struct tevent_req *subreq;
|
---|
501 |
|
---|
502 | subreq = tstream_writev_send(state,
|
---|
503 | state->caller.ev,
|
---|
504 | state->caller.stream,
|
---|
505 | state->caller.vector,
|
---|
506 | state->caller.count);
|
---|
507 | if (tevent_req_nomem(subreq, req)) {
|
---|
508 | return;
|
---|
509 | }
|
---|
510 | tevent_req_set_callback(subreq, tstream_writev_queue_done ,req);
|
---|
511 | }
|
---|
512 |
|
---|
513 | static void tstream_writev_queue_done(struct tevent_req *subreq)
|
---|
514 | {
|
---|
515 | struct tevent_req *req = tevent_req_callback_data(subreq,
|
---|
516 | struct tevent_req);
|
---|
517 | struct tstream_writev_queue_state *state = tevent_req_data(req,
|
---|
518 | struct tstream_writev_queue_state);
|
---|
519 | int ret;
|
---|
520 | int sys_errno;
|
---|
521 |
|
---|
522 | ret = tstream_writev_recv(subreq, &sys_errno);
|
---|
523 | talloc_free(subreq);
|
---|
524 | if (ret == -1) {
|
---|
525 | tevent_req_error(req, sys_errno);
|
---|
526 | return;
|
---|
527 | }
|
---|
528 | state->ret = ret;
|
---|
529 |
|
---|
530 | tevent_req_done(req);
|
---|
531 | }
|
---|
532 |
|
---|
533 | int tstream_writev_queue_recv(struct tevent_req *req, int *perrno)
|
---|
534 | {
|
---|
535 | struct tstream_writev_queue_state *state = tevent_req_data(req,
|
---|
536 | struct tstream_writev_queue_state);
|
---|
537 | int ret;
|
---|
538 |
|
---|
539 | ret = tsocket_simple_int_recv(req, perrno);
|
---|
540 | if (ret == 0) {
|
---|
541 | ret = state->ret;
|
---|
542 | }
|
---|
543 |
|
---|
544 | tevent_req_received(req);
|
---|
545 | return ret;
|
---|
546 | }
|
---|
547 |
|
---|