s3: improve WHATSNEW around kerberos changes
[Samba.git] / lib / tsocket / tsocket_helpers.c
blob3a41a3efc3df71ee7e7297696e282c158bdb9cbf
1 /*
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
8 ** under the LGPL
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/>.
24 #include "replace.h"
25 #include "system/filesys.h"
26 #include "tsocket.h"
27 #include "tsocket_internal.h"
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;
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);
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)
53 struct tevent_req *req;
54 struct tdgram_sendto_queue_state *state;
55 bool ok;
57 req = tevent_req_create(mem_ctx, &state,
58 struct tdgram_sendto_queue_state);
59 if (!req) {
60 return NULL;
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;
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;
80 return req;
82 post:
83 tevent_req_post(req, ev);
84 return req;
87 static void tdgram_sendto_queue_trigger(struct tevent_req *req,
88 void *private_data)
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,
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;
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,
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;
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;
121 state->ret = ret;
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);
130 ssize_t ret;
132 ret = tsocket_simple_int_recv(req, perrno);
133 if (ret == 0) {
134 ret = state->ret;
137 tevent_req_received(req);
138 return ret;
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;
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
157 * ask for 0 bytes.
159 struct iovec *vector;
160 size_t count;
163 * the total number of bytes we read,
164 * the return value of the _recv function
166 int total_read;
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);
183 if (!req) {
184 return NULL;
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;
193 state->count = 0;
194 state->total_read = 0;
196 tstream_readv_pdu_ask_for_next_vector(req);
197 if (!tevent_req_is_in_progress(req)) {
198 goto post;
201 return req;
203 post:
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);
211 int ret;
212 size_t to_read = 0;
213 size_t i;
214 struct tevent_req *subreq;
216 TALLOC_FREE(state->vector);
217 state->count = 0;
219 ret = state->caller.next_vector_fn(state->caller.stream,
220 state->caller.next_vector_private,
221 state, &state->vector, &state->count);
222 if (ret == -1) {
223 tevent_req_error(req, errno);
224 return;
227 if (state->count == 0) {
228 tevent_req_done(req);
229 return;
232 for (i=0; i < state->count; i++) {
233 size_t tmp = to_read;
234 tmp += state->vector[i].iov_len;
236 if (tmp < to_read) {
237 tevent_req_error(req, EMSGSIZE);
238 return;
241 to_read = tmp;
245 * this is invalid the next vector function should have
246 * reported count == 0.
248 if (to_read == 0) {
249 tevent_req_error(req, EINVAL);
250 return;
253 if (state->total_read + to_read < state->total_read) {
254 tevent_req_error(req, EMSGSIZE);
255 return;
258 subreq = tstream_readv_send(state,
259 state->caller.ev,
260 state->caller.stream,
261 state->vector,
262 state->count);
263 if (tevent_req_nomem(subreq, req)) {
264 return;
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,
272 struct tevent_req);
273 struct tstream_readv_pdu_state *state = tevent_req_data(req,
274 struct tstream_readv_pdu_state);
275 int ret;
276 int sys_errno;
278 ret = tstream_readv_recv(subreq, &sys_errno);
279 if (ret == -1) {
280 tevent_req_error(req, sys_errno);
281 return;
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);
294 int ret;
296 ret = tsocket_simple_int_recv(req, perrno);
297 if (ret == 0) {
298 ret = state->total_read;
301 tevent_req_received(req);
302 return ret;
305 struct tstream_readv_pdu_queue_state {
306 /* this structs are owned by the caller */
307 struct {
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;
312 } caller;
313 int ret;
316 static void tstream_readv_pdu_queue_trigger(struct tevent_req *req,
317 void *private_data);
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;
329 bool ok;
331 req = tevent_req_create(mem_ctx, &state,
332 struct tstream_readv_pdu_queue_state);
333 if (!req) {
334 return NULL;
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;
341 state->ret = -1;
343 ok = tevent_queue_add(queue,
345 req,
346 tstream_readv_pdu_queue_trigger,
347 NULL);
348 if (!ok) {
349 tevent_req_nomem(NULL, req);
350 goto post;
353 return req;
355 post:
356 return tevent_req_post(req, ev);
359 static void tstream_readv_pdu_queue_trigger(struct tevent_req *req,
360 void *private_data)
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,
367 state->caller.ev,
368 state->caller.stream,
369 state->caller.next_vector_fn,
370 state->caller.next_vector_private);
371 if (tevent_req_nomem(subreq, req)) {
372 return;
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,
380 struct tevent_req);
381 struct tstream_readv_pdu_queue_state *state = tevent_req_data(req,
382 struct tstream_readv_pdu_queue_state);
383 int ret;
384 int sys_errno;
386 ret = tstream_readv_pdu_recv(subreq, &sys_errno);
387 talloc_free(subreq);
388 if (ret == -1) {
389 tevent_req_error(req, sys_errno);
390 return;
392 state->ret = ret;
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);
401 int ret;
403 ret = tsocket_simple_int_recv(req, perrno);
404 if (ret == 0) {
405 ret = state->ret;
408 tevent_req_received(req);
409 return ret;
412 struct tstream_writev_queue_state {
413 /* this structs are owned by the caller */
414 struct {
415 struct tevent_context *ev;
416 struct tstream_context *stream;
417 const struct iovec *vector;
418 size_t count;
419 } caller;
420 int ret;
423 static void tstream_writev_queue_trigger(struct tevent_req *req,
424 void *private_data);
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,
432 size_t count)
434 struct tevent_req *req;
435 struct tstream_writev_queue_state *state;
436 bool ok;
438 req = tevent_req_create(mem_ctx, &state,
439 struct tstream_writev_queue_state);
440 if (!req) {
441 return NULL;
444 state->caller.ev = ev;
445 state->caller.stream = stream;
446 state->caller.vector = vector;
447 state->caller.count = count;
448 state->ret = -1;
450 ok = tevent_queue_add(queue,
452 req,
453 tstream_writev_queue_trigger,
454 NULL);
455 if (!ok) {
456 tevent_req_nomem(NULL, req);
457 goto post;
460 return req;
462 post:
463 return tevent_req_post(req, ev);
466 static void tstream_writev_queue_trigger(struct tevent_req *req,
467 void *private_data)
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,
474 state->caller.ev,
475 state->caller.stream,
476 state->caller.vector,
477 state->caller.count);
478 if (tevent_req_nomem(subreq, req)) {
479 return;
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,
487 struct tevent_req);
488 struct tstream_writev_queue_state *state = tevent_req_data(req,
489 struct tstream_writev_queue_state);
490 int ret;
491 int sys_errno;
493 ret = tstream_writev_recv(subreq, &sys_errno);
494 talloc_free(subreq);
495 if (ret == -1) {
496 tevent_req_error(req, sys_errno);
497 return;
499 state->ret = ret;
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);
508 int ret;
510 ret = tsocket_simple_int_recv(req, perrno);
511 if (ret == 0) {
512 ret = state->ret;
515 tevent_req_received(req);
516 return ret;