2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 2009
6 ** NOTE! The following LGPL license applies to the tsocket
7 ** library. This does NOT imply that all of Samba is released
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.
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.
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/>.
25 #include "system/filesys.h"
27 #include "tsocket_internal.h"
29 struct tdgram_sendto_queue_state {
30 /* this structs are owned by the caller */
32 struct tevent_context *ev;
33 struct tdgram_context *dgram;
36 const struct tsocket_address *dst;
41 static void tdgram_sendto_queue_trigger(struct tevent_req *req,
43 static void tdgram_sendto_queue_done(struct tevent_req *subreq);
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,
51 struct tsocket_address *dst)
53 struct tevent_req *req;
54 struct tdgram_sendto_queue_state *state;
57 req = tevent_req_create(mem_ctx, &state,
58 struct tdgram_sendto_queue_state);
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;
70 ok = tevent_queue_add(queue,
73 tdgram_sendto_queue_trigger,
76 tevent_req_nomem(NULL, req);
83 tevent_req_post(req, ev);
87 static void tdgram_sendto_queue_trigger(struct tevent_req *req,
90 struct tdgram_sendto_queue_state *state = tevent_req_data(req,
91 struct tdgram_sendto_queue_state);
92 struct tevent_req *subreq;
94 subreq = tdgram_sendto_send(state,
100 if (tevent_req_nomem(subreq, req)) {
103 tevent_req_set_callback(subreq, tdgram_sendto_queue_done, req);
106 static void tdgram_sendto_queue_done(struct tevent_req *subreq)
108 struct tevent_req *req = tevent_req_callback_data(subreq,
110 struct tdgram_sendto_queue_state *state = tevent_req_data(req,
111 struct tdgram_sendto_queue_state);
115 ret = tdgram_sendto_recv(subreq, &sys_errno);
118 tevent_req_error(req, sys_errno);
123 tevent_req_done(req);
126 ssize_t tdgram_sendto_queue_recv(struct tevent_req *req, int *perrno)
128 struct tdgram_sendto_queue_state *state = tevent_req_data(req,
129 struct tdgram_sendto_queue_state);
132 ret = tsocket_simple_int_recv(req, perrno);
137 tevent_req_received(req);
141 struct tstream_readv_pdu_state {
142 /* this structs are owned by the caller */
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;
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.
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
159 struct iovec *vector;
163 * the total number of bytes we read,
164 * the return value of the _recv function
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);
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)
178 struct tevent_req *req;
179 struct tstream_readv_pdu_state *state;
181 req = tevent_req_create(mem_ctx, &state,
182 struct tstream_readv_pdu_state);
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;
192 state->vector = NULL;
194 state->total_read = 0;
196 tstream_readv_pdu_ask_for_next_vector(req);
197 if (!tevent_req_is_in_progress(req)) {
204 return tevent_req_post(req, ev);
207 static void tstream_readv_pdu_ask_for_next_vector(struct tevent_req *req)
209 struct tstream_readv_pdu_state *state = tevent_req_data(req,
210 struct tstream_readv_pdu_state);
214 struct tevent_req *subreq;
216 TALLOC_FREE(state->vector);
219 ret = state->caller.next_vector_fn(state->caller.stream,
220 state->caller.next_vector_private,
221 state, &state->vector, &state->count);
223 tevent_req_error(req, errno);
227 if (state->count == 0) {
228 tevent_req_done(req);
232 for (i=0; i < state->count; i++) {
233 size_t tmp = to_read;
234 tmp += state->vector[i].iov_len;
237 tevent_req_error(req, EMSGSIZE);
245 * this is invalid the next vector function should have
246 * reported count == 0.
249 tevent_req_error(req, EINVAL);
253 if (state->total_read + to_read < state->total_read) {
254 tevent_req_error(req, EMSGSIZE);
258 subreq = tstream_readv_send(state,
260 state->caller.stream,
263 if (tevent_req_nomem(subreq, req)) {
266 tevent_req_set_callback(subreq, tstream_readv_pdu_readv_done, req);
269 static void tstream_readv_pdu_readv_done(struct tevent_req *subreq)
271 struct tevent_req *req = tevent_req_callback_data(subreq,
273 struct tstream_readv_pdu_state *state = tevent_req_data(req,
274 struct tstream_readv_pdu_state);
278 ret = tstream_readv_recv(subreq, &sys_errno);
280 tevent_req_error(req, sys_errno);
284 state->total_read += ret;
286 /* ask the callback for a new vector we should fill */
287 tstream_readv_pdu_ask_for_next_vector(req);
290 int tstream_readv_pdu_recv(struct tevent_req *req, int *perrno)
292 struct tstream_readv_pdu_state *state = tevent_req_data(req,
293 struct tstream_readv_pdu_state);
296 ret = tsocket_simple_int_recv(req, perrno);
298 ret = state->total_read;
301 tevent_req_received(req);
305 struct tstream_readv_pdu_queue_state {
306 /* this structs are owned by the caller */
308 struct tevent_context *ev;
309 struct tstream_context *stream;
310 tstream_readv_pdu_next_vector_t next_vector_fn;
311 void *next_vector_private;
316 static void tstream_readv_pdu_queue_trigger(struct tevent_req *req,
318 static void tstream_readv_pdu_queue_done(struct tevent_req *subreq);
320 struct tevent_req *tstream_readv_pdu_queue_send(TALLOC_CTX *mem_ctx,
321 struct tevent_context *ev,
322 struct tstream_context *stream,
323 struct tevent_queue *queue,
324 tstream_readv_pdu_next_vector_t next_vector_fn,
325 void *next_vector_private)
327 struct tevent_req *req;
328 struct tstream_readv_pdu_queue_state *state;
331 req = tevent_req_create(mem_ctx, &state,
332 struct tstream_readv_pdu_queue_state);
337 state->caller.ev = ev;
338 state->caller.stream = stream;
339 state->caller.next_vector_fn = next_vector_fn;
340 state->caller.next_vector_private = next_vector_private;
343 ok = tevent_queue_add(queue,
346 tstream_readv_pdu_queue_trigger,
349 tevent_req_nomem(NULL, req);
356 return tevent_req_post(req, ev);
359 static void tstream_readv_pdu_queue_trigger(struct tevent_req *req,
362 struct tstream_readv_pdu_queue_state *state = tevent_req_data(req,
363 struct tstream_readv_pdu_queue_state);
364 struct tevent_req *subreq;
366 subreq = tstream_readv_pdu_send(state,
368 state->caller.stream,
369 state->caller.next_vector_fn,
370 state->caller.next_vector_private);
371 if (tevent_req_nomem(subreq, req)) {
374 tevent_req_set_callback(subreq, tstream_readv_pdu_queue_done ,req);
377 static void tstream_readv_pdu_queue_done(struct tevent_req *subreq)
379 struct tevent_req *req = tevent_req_callback_data(subreq,
381 struct tstream_readv_pdu_queue_state *state = tevent_req_data(req,
382 struct tstream_readv_pdu_queue_state);
386 ret = tstream_readv_pdu_recv(subreq, &sys_errno);
389 tevent_req_error(req, sys_errno);
394 tevent_req_done(req);
397 int tstream_readv_pdu_queue_recv(struct tevent_req *req, int *perrno)
399 struct tstream_readv_pdu_queue_state *state = tevent_req_data(req,
400 struct tstream_readv_pdu_queue_state);
403 ret = tsocket_simple_int_recv(req, perrno);
408 tevent_req_received(req);
412 struct tstream_writev_queue_state {
413 /* this structs are owned by the caller */
415 struct tevent_context *ev;
416 struct tstream_context *stream;
417 const struct iovec *vector;
423 static void tstream_writev_queue_trigger(struct tevent_req *req,
425 static void tstream_writev_queue_done(struct tevent_req *subreq);
427 struct tevent_req *tstream_writev_queue_send(TALLOC_CTX *mem_ctx,
428 struct tevent_context *ev,
429 struct tstream_context *stream,
430 struct tevent_queue *queue,
431 const struct iovec *vector,
434 struct tevent_req *req;
435 struct tstream_writev_queue_state *state;
438 req = tevent_req_create(mem_ctx, &state,
439 struct tstream_writev_queue_state);
444 state->caller.ev = ev;
445 state->caller.stream = stream;
446 state->caller.vector = vector;
447 state->caller.count = count;
450 ok = tevent_queue_add(queue,
453 tstream_writev_queue_trigger,
456 tevent_req_nomem(NULL, req);
463 return tevent_req_post(req, ev);
466 static void tstream_writev_queue_trigger(struct tevent_req *req,
469 struct tstream_writev_queue_state *state = tevent_req_data(req,
470 struct tstream_writev_queue_state);
471 struct tevent_req *subreq;
473 subreq = tstream_writev_send(state,
475 state->caller.stream,
476 state->caller.vector,
477 state->caller.count);
478 if (tevent_req_nomem(subreq, req)) {
481 tevent_req_set_callback(subreq, tstream_writev_queue_done ,req);
484 static void tstream_writev_queue_done(struct tevent_req *subreq)
486 struct tevent_req *req = tevent_req_callback_data(subreq,
488 struct tstream_writev_queue_state *state = tevent_req_data(req,
489 struct tstream_writev_queue_state);
493 ret = tstream_writev_recv(subreq, &sys_errno);
496 tevent_req_error(req, sys_errno);
501 tevent_req_done(req);
504 int tstream_writev_queue_recv(struct tevent_req *req, int *perrno)
506 struct tstream_writev_queue_state *state = tevent_req_data(req,
507 struct tstream_writev_queue_state);
510 ret = tsocket_simple_int_recv(req, perrno);
515 tevent_req_received(req);